Skip to content

Commit

Permalink
blah
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa committed Apr 26, 2024
1 parent 1355191 commit 95453fd
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clambcc/clambc-compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
121 changes: 121 additions & 0 deletions libclambcc/ClamBCRemovePointerPHIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ class ClamBCRemovePointerPHIs : public PassInfoMixin<ClamBCRemovePointerPHIs>
return false;
}


DEBUG_VALUE(pn);

IntegerType *pType = Type::getInt64Ty(pMod->getContext());
Constant *zero = ConstantInt::get(pType, 0);
Value *initValue = zero;
Expand Down Expand Up @@ -293,6 +296,118 @@ class ClamBCRemovePointerPHIs : public PassInfoMixin<ClamBCRemovePointerPHIs>
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<Instruction>(i);
if (canBreak && (not llvm::isa<PHINode>(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<Instruction *> users;
for (auto i = pn->user_begin(), e = pn->user_end(); i != e; i++) {
if (Instruction *pUser = llvm::dyn_cast<Instruction>(*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<PHINode>(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<BasicBlock>(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<Instruction>(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() {}

Expand All @@ -313,9 +428,15 @@ class ClamBCRemovePointerPHIs : public PassInfoMixin<ClamBCRemovePointerPHIs>
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
}
}

Expand Down

0 comments on commit 95453fd

Please sign in to comment.