From 95453fdfab3cd5a02f60887f95ba485dafb5938a Mon Sep 17 00:00:00 2001 From: Andy Ragusa Date: Fri, 26 Apr 2024 15:09:12 -0700 Subject: [PATCH] blah --- clambcc/clambc-compiler.py | 2 +- libclambcc/ClamBCRemovePointerPHIs.cpp | 121 +++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/clambcc/clambc-compiler.py b/clambcc/clambc-compiler.py index fa630f4c87..f932065cee 100755 --- a/clambcc/clambc-compiler.py +++ b/clambcc/clambc-compiler.py @@ -577,7 +577,7 @@ def createInputSourceFile(clangLLVM: ClangLLVM, name: str, args: list, options: , 'clambc-rebuild' , 'verify' # , 'clambc-remove-pointer-phis' - , 'verify' +# , 'verify' , 'clambc-trace' , 'verify' , 'clambc-outline-endianness-calls' diff --git a/libclambcc/ClamBCRemovePointerPHIs.cpp b/libclambcc/ClamBCRemovePointerPHIs.cpp index 05c398f7e1..1dce1c0183 100644 --- a/libclambcc/ClamBCRemovePointerPHIs.cpp +++ b/libclambcc/ClamBCRemovePointerPHIs.cpp @@ -185,6 +185,9 @@ class ClamBCRemovePointerPHIs : public PassInfoMixin return false; } + + DEBUG_VALUE(pn); + IntegerType *pType = Type::getInt64Ty(pMod->getContext()); Constant *zero = ConstantInt::get(pType, 0); Value *initValue = zero; @@ -293,6 +296,118 @@ class ClamBCRemovePointerPHIs : public PassInfoMixin return true; } + /*The idea here is that we get the insertion point for where our calculated value + * will be saved. pInst is the value we want to save, so it will have to be*/ + Instruction * getInsertionPoint(Instruction * pInst){ + BasicBlock * pBB = pInst->getParent(); + bool canBreak = false; + Instruction * pRet = nullptr; + for (auto i = pBB->begin(), e = pBB->end(); i != e; i++){ + pRet = llvm::cast(i); + if (canBreak && (not llvm::isa(pRet))){ + break; + } + if (pRet == pInst){ + canBreak = true; + } + } + return pRet; + } + + /* Load the value from our AllocaInst, and + * replace the PHINode everywhere it is used.*/ + virtual void replaceUses(PHINode * pn, AllocaInst * pai){ + std::vector users; + for (auto i = pn->user_begin(), e = pn->user_end(); i != e; i++) { + if (Instruction *pUser = llvm::dyn_cast(*i)) { + users.push_back(pUser); + } + } + + for (size_t i = 0; i < users.size(); i++) { + Instruction *pUser = users[i]; + Instruction * insPt = nullptr; + + if (PHINode * pnUser = llvm::dyn_cast(pUser)){ + for (size_t j = 0; j < pnUser->getNumIncomingValues(); j++){ + if (pn == pnUser->getIncomingValue(j)){ + insPt = pnUser->getIncomingBlock(j)->getTerminator(); + break; + } + } + } else { + insPt = pUser; + } + + DEBUG_VALUE(pai); + DEBUG_VALUE(pai->getType()); + DEBUG_VALUE(pUser); + DEBUG_VALUE(pUser->getType()); + + LoadInst * pli = new LoadInst(pn->getType(), pai, "ClamBCRemovePointerPHIs_load_", insPt); + for (size_t j = 0; j < pUser->getNumOperands(); j++){ + if (pn == pUser->getOperand(j)){ + pUser->setOperand(j, pli); + break; + } + } + + } + + } + + bool handlePHI2(PHINode *pn) + { + if (not pn->getType()->isPointerTy()) { + return false; + } + + Value *pBasePtr = findBasePointer(pn); + if (nullptr == pBasePtr) { /*No unique base pointer.*/ + return false; + } + + /* Create our storage variable in the first basic block, + * right before the branch instruction.*/ + Function * pFunc = pn->getParent()->getParent(); + BasicBlock * pEntry = llvm::cast(pFunc->begin()); + Instruction * insPt = pEntry->getTerminator(); + AllocaInst * pai = new AllocaInst(pn->getType(), + pFunc->getAddressSpace(), "ClamBCRemovePointerPHIs_", insPt); + + DEBUG_VALUE(pn); + /* Go to all the locations that are incoming to our phi block, and store + * the value the phi is using as input in our alloca instruction. + */ + for (size_t i = 0; i < pn->getNumIncomingValues(); i++) { + BasicBlock * pBB = pn->getIncomingBlock(i); +#if 0 + Instruction *incoming = llvm::dyn_cast(pn->getIncomingValue(i)); + if (nullptr == incoming){ + DEBUG_VALUE(pn); + DEBUG_VALUE(pn->getIncomingValue(i)); + assert (0 && "HOW DID THIS HAPPEN?"); + } + + insPt = getInsertionPoint(incoming); + new StoreInst(incoming, pai, insPt); +#else + insPt = pBB->getTerminator(); + new StoreInst(pn->getIncomingValue(i), pai, insPt); +#endif + } + + replaceUses(pn, pai); + pn->eraseFromParent(); + + + + return true; + + } + + + public: ClamBCRemovePointerPHIs() {} @@ -313,9 +428,15 @@ class ClamBCRemovePointerPHIs : public PassInfoMixin for (size_t i = 0; i < phis.size(); i++) { PHINode *pn = phis[i]; +#if 0 if (handlePHI(pn)) { ret = true; } +#else + if (handlePHI2(pn)) { + ret = true; + } +#endif } }