Skip to content

Commit

Permalink
CODE CLEANUP, IF ANYTHING IS BROKEN, START HERE
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa committed May 3, 2024
1 parent 5744d8b commit 62dd49a
Showing 1 changed file with 4 additions and 105 deletions.
109 changes: 4 additions & 105 deletions libclambcc/ClamBCRemovePointerPHIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ using namespace llvm;
#include <set>


/*
* This Pass is only needed for 0.103 on Windows, so when we no longer need to support 0.103, it can be removed.
*/

namespace
{
class ClamBCRemovePointerPHIs : public PassInfoMixin<ClamBCRemovePointerPHIs>
Expand Down Expand Up @@ -352,103 +356,6 @@ class ClamBCRemovePointerPHIs : public PassInfoMixin<ClamBCRemovePointerPHIs>

}

FunctionType * getSaveFunctionType(){
static FunctionType * pRet = nullptr;

if (nullptr == pRet){

Type * vt = Type::getVoidTy(pMod->getContext());
Type * pt = Type::getInt8Ty(pMod->getContext())->getPointerTo();
Type * it = Type::getInt32Ty(pMod->getContext());
pRet = FunctionType::get(vt, {pt, it, pt, it}, false);

}

return pRet;

}
Function * getSaveFunction(){
static Function * pRet = nullptr;
if (nullptr == pRet){
FunctionType * ft = getSaveFunctionType();
pRet = Function::Create(ft, GlobalValue::InternalLinkage, "__save_pointer", pMod);
pRet->addFnAttr(Attribute::OptimizeNone);
pRet->addFnAttr(Attribute::NoInline);

BasicBlock * pEntry = BasicBlock::Create(pMod->getContext(), "entry", pRet, nullptr);
CastInst * pCast = CastInst::CreatePointerCast(pRet->getArg(0), pRet->getArg(0)->getType()->getPointerTo(), "ClamBCRemovePointerPHIs_pointerCast_", pEntry);
new StoreInst(pRet->getArg(2), pCast, pEntry);
ReturnInst::Create(pMod->getContext(), nullptr, pEntry);

DEBUG_VALUE(pRet);

}
return pRet;
}

void savePointer(Value * src, Value * dst, Instruction * insPt){
Function * saveFunction = getSaveFunction();
ConstantInt * pci = ConstantInt::get(Type::getInt32Ty(pMod->getContext()), 8);
CastInst * pCast = CastInst::CreatePointerCast(dst, src->getType(), "ClamBCRemovePointerPHIs_pointerCast_", insPt);
CallInst::Create(getSaveFunctionType(), saveFunction, {pCast, pci, src, pci}, "", insPt);
}

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();
#if 1
new StoreInst(pn->getIncomingValue(i), pai, insPt);
#else
savePointer(pn->getIncomingValue(i), pai, insPt);
#endif
#endif
}

replaceUses(pn, pai);
pn->eraseFromParent();



return true;

}



public:
ClamBCRemovePointerPHIs() {}

Expand All @@ -458,8 +365,6 @@ return pRet;
pMod = &m;
bool ret = false;

llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << ">" << "HAVING ISSUES WITH POINTER PHIS ON 0.103 windows, keep changes isolated to this pass so that we can remove it when we no longer have to support 0.103" << "<END>\n";

for (auto i = pMod->begin(), e = pMod->end(); i != e; i++) {
llvm::Function *pFunc = llvm::dyn_cast<Function>(i);
if (nullptr == pFunc) {
Expand All @@ -469,15 +374,9 @@ return pRet;
for (size_t i = 0; i < phis.size(); i++) {
PHINode *pn = phis[i];

#if 1
if (handlePHI(pn)) {
ret = true;
}
#else
if (handlePHI2(pn)) {
ret = true;
}
#endif
}
}

Expand Down

0 comments on commit 62dd49a

Please sign in to comment.