ProgramStructure::ProgramStructure(Module &M) { for (Module::iterator f = M.begin(); f != M.end(); ++f) if (!f->isDeclaration() && !memoryManStuff(&*f)) for (inst_iterator i = inst_begin(*f); i != inst_end(*f); ++i) if (const StoreInst *s = dyn_cast<StoreInst>(&*i)) { const Value *l = elimConstExpr(s->getPointerOperand()); this->getContainer()[&*f].push_back(ProgramStructure::Command( hasExtraReference(l) ? CMD_VAR : CMD_DREF_VAR, l)); } }
bool isPointerManipulation(llvm::Instruction const* const I) { if (isa<AllocaInst>(I)) { return false; } else if (I->getOpcode() == llvm::Instruction::Load) { if (llvm::dyn_cast<llvm::PointerType const>( I->getOperand(0)->getType())-> getElementType()->isPointerTy()) return true; } else if (I->getOpcode() == llvm::Instruction::Store) { if (I->getOperand(0)->getType()->isPointerTy()) return true; } else if (I->getOpcode() == llvm::Instruction::BitCast) { if (I->getType()->isPointerTy() && I->getOperand(0)->getType()->isPointerTy()) return true; } else if (I->getOpcode() == llvm::Instruction::GetElementPtr) //else if (llvm::GetElementPtrInst const* const gep = // llvm::dyn_cast<llvm::GetElementPtrInst>(I)) { return true; } else if (const llvm::CallInst *C = llvm::dyn_cast<llvm::CallInst>(I)) { if (isInlineAssembly(C)) return false; return memoryManStuff(C->getCalledValue()); } else if (const PHINode *PHI = dyn_cast<PHINode>(I)) { return isPointerValue(PHI); } else if (const ExtractValueInst *EV = dyn_cast<const ExtractValueInst>(I)) { return isPointerValue(EV); } else if (const InsertValueInst *IV = dyn_cast<const InsertValueInst>(I)) { return isPointerValue(IV->getInsertedValueOperand()); } else if (isa<IntToPtrInst>(I)) { return true; } else if (const SelectInst *SEL = dyn_cast<SelectInst>(I)) { if (isPointerValue(SEL)) return true; } assert(!isPointerValue(I) && "Instruction cannot be a of pointer type here!"); return false; }
void buildCallMaps(Module const& M, FunctionsMap& F, CallsMap& C) { for (Module::const_iterator f = M.begin(); f != M.end(); ++f) { if (!f->isDeclaration()) F.insert(std::make_pair(f->getFunctionType(), &*f)); for (Function::const_iterator b = f->begin(); b != f->end(); ++b) { for (BasicBlock::const_iterator i = b->begin(); i != b->end(); ++i) if (const CallInst *CI = dyn_cast<CallInst>(&*i)) { if (!isInlineAssembly(CI) && !callToMemoryManStuff(CI)) C.insert(std::make_pair(getCalleePrototype(CI), CI)); } else if (const StoreInst *SI = dyn_cast<StoreInst>(&*i)) { const Value *r = SI->getValueOperand(); if (hasExtraReference(r) && memoryManStuff(r)) { const Function *fn = dyn_cast<Function>(r); F.insert(std::make_pair(fn->getFunctionType(), fn)); } } } } }
bool callToMemoryManStuff(llvm::CallInst const* const C) { return memoryManStuff(C->getCalledValue()); }