本文整理汇总了C++中BasicBlock::getFirstNonPHI方法的典型用法代码示例。如果您正苦于以下问题:C++ BasicBlock::getFirstNonPHI方法的具体用法?C++ BasicBlock::getFirstNonPHI怎么用?C++ BasicBlock::getFirstNonPHI使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BasicBlock
的用法示例。
在下文中一共展示了BasicBlock::getFirstNonPHI方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
//.........这里部分代码省略.........
// \ / \ / | \------/
// < \ / |
// \ / v
// ExitBB ExitBB
//
// depending on whether or not we know that it is executed at least once. If
// not, GuardBB checks if the loop is executed at least once. If this is the
// case we branch to PreHeaderBB and subsequently to the HeaderBB, which
// contains the loop iv 'polly.indvar', the incremented loop iv
// 'polly.indvar_next' as well as the condition to check if we execute another
// iteration of the loop. After the loop has finished, we branch to ExitBB.
Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
PollyIRBuilder &Builder, Pass *P, LoopInfo &LI,
DominatorTree &DT, BasicBlock *&ExitBB,
ICmpInst::Predicate Predicate,
LoopAnnotator *Annotator, bool Parallel,
bool UseGuard) {
Function *F = Builder.GetInsertBlock()->getParent();
LLVMContext &Context = F->getContext();
assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
assert(LoopIVType && "UB is not integer?");
BasicBlock *BeforeBB = Builder.GetInsertBlock();
BasicBlock *GuardBB =
UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr;
BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
BasicBlock *PreHeaderBB =
BasicBlock::Create(Context, "polly.loop_preheader", F);
if (Annotator) {
Annotator->Begin(HeaderBB);
if (Parallel)
Annotator->SetCurrentParallel();
}
// Update LoopInfo
Loop *OuterLoop = LI.getLoopFor(BeforeBB);
Loop *NewLoop = new Loop();
if (OuterLoop)
OuterLoop->addChildLoop(NewLoop);
else
LI.addTopLevelLoop(NewLoop);
if (OuterLoop && GuardBB)
OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase());
else if (OuterLoop)
OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase());
NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase());
// ExitBB
ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
ExitBB->setName("polly.loop_exit");
// BeforeBB
if (GuardBB) {
BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
DT.addNewBlock(GuardBB, BeforeBB);
// GuardBB
Builder.SetInsertPoint(GuardBB);
Value *LoopGuard;
LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
LoopGuard->setName("polly.loop_guard");
Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
DT.addNewBlock(PreHeaderBB, GuardBB);
} else {
BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB);
DT.addNewBlock(PreHeaderBB, BeforeBB);
}
// PreHeaderBB
Builder.SetInsertPoint(PreHeaderBB);
Builder.CreateBr(HeaderBB);
// HeaderBB
DT.addNewBlock(HeaderBB, PreHeaderBB);
Builder.SetInsertPoint(HeaderBB);
PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
IV->addIncoming(LB, PreHeaderBB);
Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
Value *LoopCondition;
UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
LoopCondition->setName("polly.loop_cond");
Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
IV->addIncoming(IncrementedIV, HeaderBB);
if (GuardBB)
DT.changeImmediateDominator(ExitBB, GuardBB);
else
DT.changeImmediateDominator(ExitBB, BeforeBB);
// The loop body should be added here.
Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
return IV;
}
示例2: removeImplausibleInstructions
void WinEHPrepare::removeImplausibleInstructions(Function &F) {
// Remove implausible terminators and replace them with UnreachableInst.
for (auto &Funclet : FuncletBlocks) {
BasicBlock *FuncletPadBB = Funclet.first;
std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second;
Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI);
auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad);
auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad);
for (BasicBlock *BB : BlocksInFunclet) {
for (Instruction &I : *BB) {
CallSite CS(&I);
if (!CS)
continue;
Value *FuncletBundleOperand = nullptr;
if (auto BU = CS.getOperandBundle(LLVMContext::OB_funclet))
FuncletBundleOperand = BU->Inputs.front();
if (FuncletBundleOperand == FuncletPad)
continue;
// Skip call sites which are nounwind intrinsics or inline asm.
auto *CalledFn =
dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts());
if (CalledFn && ((CalledFn->isIntrinsic() && CS.doesNotThrow()) ||
CS.isInlineAsm()))
continue;
// This call site was not part of this funclet, remove it.
if (CS.isInvoke()) {
// Remove the unwind edge if it was an invoke.
removeUnwindEdge(BB);
// Get a pointer to the new call.
BasicBlock::iterator CallI =
std::prev(BB->getTerminator()->getIterator());
auto *CI = cast<CallInst>(&*CallI);
changeToUnreachable(CI, /*UseLLVMTrap=*/false);
} else {
changeToUnreachable(&I, /*UseLLVMTrap=*/false);
}
// There are no more instructions in the block (except for unreachable),
// we are done.
break;
}
Instruction *TI = BB->getTerminator();
// CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad;
// The token consumed by a CatchReturnInst must match the funclet token.
bool IsUnreachableCatchret = false;
if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
// The token consumed by a CleanupReturnInst must match the funclet token.
bool IsUnreachableCleanupret = false;
if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
if (IsUnreachableRet || IsUnreachableCatchret ||
IsUnreachableCleanupret) {
changeToUnreachable(TI, /*UseLLVMTrap=*/false);
} else if (isa<InvokeInst>(TI)) {
if (Personality == EHPersonality::MSVC_CXX && CleanupPad) {
// Invokes within a cleanuppad for the MSVC++ personality never
// transfer control to their unwind edge: the personality will
// terminate the program.
removeUnwindEdge(BB);
}
}
}
}
}
示例3: if
Function *PartialInlinerImpl::unswitchFunction(Function *F) {
// First, verify that this function is an unswitching candidate...
BasicBlock *EntryBlock = &F->front();
BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator());
if (!BR || BR->isUnconditional())
return nullptr;
BasicBlock *ReturnBlock = nullptr;
BasicBlock *NonReturnBlock = nullptr;
unsigned ReturnCount = 0;
for (BasicBlock *BB : successors(EntryBlock)) {
if (isa<ReturnInst>(BB->getTerminator())) {
ReturnBlock = BB;
ReturnCount++;
} else
NonReturnBlock = BB;
}
if (ReturnCount != 1)
return nullptr;
// Clone the function, so that we can hack away on it.
ValueToValueMapTy VMap;
Function *DuplicateFunction = CloneFunction(F, VMap);
DuplicateFunction->setLinkage(GlobalValue::InternalLinkage);
BasicBlock *NewEntryBlock = cast<BasicBlock>(VMap[EntryBlock]);
BasicBlock *NewReturnBlock = cast<BasicBlock>(VMap[ReturnBlock]);
BasicBlock *NewNonReturnBlock = cast<BasicBlock>(VMap[NonReturnBlock]);
// Go ahead and update all uses to the duplicate, so that we can just
// use the inliner functionality when we're done hacking.
F->replaceAllUsesWith(DuplicateFunction);
// Special hackery is needed with PHI nodes that have inputs from more than
// one extracted block. For simplicity, just split the PHIs into a two-level
// sequence of PHIs, some of which will go in the extracted region, and some
// of which will go outside.
BasicBlock *PreReturn = NewReturnBlock;
NewReturnBlock = NewReturnBlock->splitBasicBlock(
NewReturnBlock->getFirstNonPHI()->getIterator());
BasicBlock::iterator I = PreReturn->begin();
Instruction *Ins = &NewReturnBlock->front();
while (I != PreReturn->end()) {
PHINode *OldPhi = dyn_cast<PHINode>(I);
if (!OldPhi)
break;
PHINode *RetPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
OldPhi->replaceAllUsesWith(RetPhi);
Ins = NewReturnBlock->getFirstNonPHI();
RetPhi->addIncoming(&*I, PreReturn);
RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(NewEntryBlock),
NewEntryBlock);
OldPhi->removeIncomingValue(NewEntryBlock);
++I;
}
NewEntryBlock->getTerminator()->replaceUsesOfWith(PreReturn, NewReturnBlock);
// Gather up the blocks that we're going to extract.
std::vector<BasicBlock *> ToExtract;
ToExtract.push_back(NewNonReturnBlock);
for (BasicBlock &BB : *DuplicateFunction)
if (&BB != NewEntryBlock && &BB != NewReturnBlock &&
&BB != NewNonReturnBlock)
ToExtract.push_back(&BB);
// The CodeExtractor needs a dominator tree.
DominatorTree DT;
DT.recalculate(*DuplicateFunction);
// Extract the body of the if.
Function *ExtractedFunction =
CodeExtractor(ToExtract, &DT).extractCodeRegion();
// Inline the top-level if test into all callers.
std::vector<User *> Users(DuplicateFunction->user_begin(),
DuplicateFunction->user_end());
for (User *User : Users)
if (CallInst *CI = dyn_cast<CallInst>(User))
InlineFunction(CI, IFI);
else if (InvokeInst *II = dyn_cast<InvokeInst>(User))
InlineFunction(II, IFI);
// Ditch the duplicate, since we're done with it, and rewrite all remaining
// users (function pointers, etc.) back to the original function.
DuplicateFunction->replaceAllUsesWith(F);
DuplicateFunction->eraseFromParent();
++NumPartialInlined;
return ExtractedFunction;
}
示例4: cleanup
void DSWP::cleanup(Loop *L, LPPassManager &LPM) {
// Move some instructions that may not have been inserted in the right
// place, delete the old loop, and clean up our aux data structures for this
// loop.
/*
* move the produce instructions, which have been inserted after the branch,
* in front of it
*/
for (int i = 0; i < MAX_THREAD; i++) {
for (Function::iterator bi = allFunc[i]->begin(),
be = allFunc[i]->end();
bi != be; ++bi) {
BasicBlock *bb = bi;
TerminatorInst *term = NULL;
for (BasicBlock::iterator ii = bb->begin(), ie = bb->end();
ii != ie; ++ii) {
Instruction *inst = ii;
if (isa<TerminatorInst>(inst)) {
term = dyn_cast<TerminatorInst>(inst);
break;
}
}
if (term == NULL) {
error("term cannot be null");
}
while (true) {
Instruction *last = &bb->getInstList().back();
if (isa<TerminatorInst>(last))
break;
last->moveBefore(term);
}
}
}
/*
* move the phi nodes to the top of the block
*/
for (int i = 0; i < MAX_THREAD; i++) {
for (Function::iterator bi = allFunc[i]->begin(),
be = allFunc[i]->end();
bi != be; ++bi) {
BasicBlock *bb = bi;
Instruction *first_nonphi = bb->getFirstNonPHI();
BasicBlock::iterator ii = bb->begin(), ie = bb->end();
// advance the iterator up to one past first_nonphi
while (&(*ii) != first_nonphi) { ++ii; }
++ii;
// move any phi nodes after the first nonphi to before it
for (BasicBlock::iterator i_next; ii != ie; ii = i_next) {
i_next = ii;
++i_next;
Instruction *inst = ii;
if (isa<PHINode>(inst)) {
inst->moveBefore(first_nonphi);
}
}
}
}
cout << "begin to delete loop" << endl;
for (Loop::block_iterator bi = L->block_begin(), be = L->block_end();
bi != be; ++bi) {
BasicBlock *BB = *bi;
for (BasicBlock::iterator ii = BB->begin(), i_next, ie = BB->end();
ii != ie; ii = i_next) {
i_next = ii;
++i_next;
Instruction &inst = *ii;
inst.replaceAllUsesWith(UndefValue::get(inst.getType()));
inst.eraseFromParent();
}
}
// Delete the basic blocks only afterwards
// so that backwards branch instructions don't break
for (Loop::block_iterator bi = L->block_begin(), be = L->block_end();
bi != be; ++bi) {
BasicBlock *BB = *bi;
BB->eraseFromParent();
}
LPM.deleteLoopFromQueue(L);
}
示例5: cloneCommonBlocks
void WinEHPrepare::cloneCommonBlocks(Function &F) {
// We need to clone all blocks which belong to multiple funclets. Values are
// remapped throughout the funclet to propagate both the new instructions
// *and* the new basic blocks themselves.
for (auto &Funclets : FuncletBlocks) {
BasicBlock *FuncletPadBB = Funclets.first;
std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second;
Value *FuncletToken;
if (FuncletPadBB == &F.getEntryBlock())
FuncletToken = ConstantTokenNone::get(F.getContext());
else
FuncletToken = FuncletPadBB->getFirstNonPHI();
std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone;
ValueToValueMapTy VMap;
for (BasicBlock *BB : BlocksInFunclet) {
ColorVector &ColorsForBB = BlockColors[BB];
// We don't need to do anything if the block is monochromatic.
size_t NumColorsForBB = ColorsForBB.size();
if (NumColorsForBB == 1)
continue;
DEBUG_WITH_TYPE("winehprepare-coloring",
dbgs() << " Cloning block \'" << BB->getName()
<< "\' for funclet \'" << FuncletPadBB->getName()
<< "\'.\n");
// Create a new basic block and copy instructions into it!
BasicBlock *CBB =
CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
// Insert the clone immediately after the original to ensure determinism
// and to keep the same relative ordering of any funclet's blocks.
CBB->insertInto(&F, BB->getNextNode());
// Add basic block mapping.
VMap[BB] = CBB;
// Record delta operations that we need to perform to our color mappings.
Orig2Clone.emplace_back(BB, CBB);
}
// If nothing was cloned, we're done cloning in this funclet.
if (Orig2Clone.empty())
continue;
// Update our color mappings to reflect that one block has lost a color and
// another has gained a color.
for (auto &BBMapping : Orig2Clone) {
BasicBlock *OldBlock = BBMapping.first;
BasicBlock *NewBlock = BBMapping.second;
BlocksInFunclet.push_back(NewBlock);
ColorVector &NewColors = BlockColors[NewBlock];
assert(NewColors.empty() && "A new block should only have one color!");
NewColors.push_back(FuncletPadBB);
DEBUG_WITH_TYPE("winehprepare-coloring",
dbgs() << " Assigned color \'" << FuncletPadBB->getName()
<< "\' to block \'" << NewBlock->getName()
<< "\'.\n");
BlocksInFunclet.erase(
std::remove(BlocksInFunclet.begin(), BlocksInFunclet.end(), OldBlock),
BlocksInFunclet.end());
ColorVector &OldColors = BlockColors[OldBlock];
OldColors.erase(
std::remove(OldColors.begin(), OldColors.end(), FuncletPadBB),
OldColors.end());
DEBUG_WITH_TYPE("winehprepare-coloring",
dbgs() << " Removed color \'" << FuncletPadBB->getName()
<< "\' from block \'" << OldBlock->getName()
<< "\'.\n");
}
// Loop over all of the instructions in this funclet, fixing up operand
// references as we go. This uses VMap to do all the hard work.
for (BasicBlock *BB : BlocksInFunclet)
// Loop over all instructions, fixing each one as we find it...
for (Instruction &I : *BB)
RemapInstruction(&I, VMap,
RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
// Catchrets targeting cloned blocks need to be updated separately from
// the loop above because they are not in the current funclet.
SmallVector<CatchReturnInst *, 2> FixupCatchrets;
for (auto &BBMapping : Orig2Clone) {
BasicBlock *OldBlock = BBMapping.first;
BasicBlock *NewBlock = BBMapping.second;
FixupCatchrets.clear();
for (BasicBlock *Pred : predecessors(OldBlock))
if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator()))
if (CatchRet->getCatchSwitchParentPad() == FuncletToken)
FixupCatchrets.push_back(CatchRet);
for (CatchReturnInst *CatchRet : FixupCatchrets)
CatchRet->setSuccessor(NewBlock);
}
//.........这里部分代码省略.........
示例6: OptimizeNoopCopyExpression
/// OptimizeNoopCopyExpression - If the specified cast instruction is a noop
/// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC),
/// sink it into user blocks to reduce the number of virtual
/// registers that must be created and coalesced.
///
/// Return true if any changes are made.
///
static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
// If this is a noop copy,
EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
EVT DstVT = TLI.getValueType(CI->getType());
// This is an fp<->int conversion?
if (SrcVT.isInteger() != DstVT.isInteger())
return false;
// If this is an extension, it will be a zero or sign extension, which
// isn't a noop.
if (SrcVT.bitsLT(DstVT)) return false;
// If these values will be promoted, find out what they will be promoted
// to. This helps us consider truncates on PPC as noop copies when they
// are.
if (TLI.getTypeAction(CI->getContext(), SrcVT) == TargetLowering::Promote)
SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
if (TLI.getTypeAction(CI->getContext(), DstVT) == TargetLowering::Promote)
DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
// If, after promotion, these are the same types, this is a noop copy.
if (SrcVT != DstVT)
return false;
BasicBlock *DefBB = CI->getParent();
/// InsertedCasts - Only insert a cast in each block once.
DenseMap<BasicBlock*, CastInst*> InsertedCasts;
bool MadeChange = false;
for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
UI != E; ) {
Use &TheUse = UI.getUse();
Instruction *User = cast<Instruction>(*UI);
// Figure out which BB this cast is used in. For PHI's this is the
// appropriate predecessor block.
BasicBlock *UserBB = User->getParent();
if (PHINode *PN = dyn_cast<PHINode>(User)) {
UserBB = PN->getIncomingBlock(UI);
}
// Preincrement use iterator so we don't invalidate it.
++UI;
// If this user is in the same block as the cast, don't change the cast.
if (UserBB == DefBB) continue;
// If we have already inserted a cast into this block, use it.
CastInst *&InsertedCast = InsertedCasts[UserBB];
if (!InsertedCast) {
BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
InsertedCast =
CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
InsertPt);
MadeChange = true;
}
// Replace a use of the cast with a use of the new cast.
TheUse = InsertedCast;
}
// If we removed all uses, nuke the cast.
if (CI->use_empty()) {
CI->eraseFromParent();
MadeChange = true;
}
return MadeChange;
}
示例7: runOnFunction
bool ProfilingPass::runOnFunction(Function &F)
{
LLVMContext& context = F.getContext();
Module *m = F.getParent();
string funcname = F.getNameStr();
string injectfunc("injectFault");
if((profileoption == 'p' || profileoption == 'c' || profileoption == 'i') && (funcname.find(injectfunc) != string::npos))
return false;
std::vector<Instruction*> insert_worklist;
for (inst_iterator In = inst_begin(F), E = inst_end(F); In != E; ++In)
{
Instruction *I = dyn_cast<Instruction>(&*In);
//errs()<<*I<<"\n";
if(profileoption == 'b')
{
if(CmpInst *ci = dyn_cast<CmpInst>(I))
if(is_used_by_branch(ci)){
vector<const Type*> argTypes(1);
argTypes[0] = Type::getInt32Ty(context); // enum for the options
FunctionType* countFuncType = FunctionType::get( Type::getVoidTy(context), argTypes, 0 );
Constant* countFunc = m->getOrInsertFunction("doProfiling", countFuncType); // get the injection function
vector<Value*> countArgs(1);
const IntegerType* itype = IntegerType::get(context,32);
Value* branchVal = ConstantInt::get(itype, BRANCH );
countArgs[0] = branchVal; //enum for branch
CallInst::Create( countFunc, countArgs.begin(),countArgs.end(), "", I);
}
}
else if(profileoption == 'd')
{
//see if the current instruction is a cmp instruction that leads to a conditional branch
//add the instrumentation to the defs of this cmp instruction
//--> Static time deduction since branch not known if executed or not
CmpInst *ci = dyn_cast<CmpInst>(I);
//errs() <<"reached here:\n";
if(!ci)
continue;
//traverse def-use chain
//int is_used_by_branch = 0;
if(!is_used_by_branch(I))
continue;
//the defines of this instruction I --> would be injectFaultCalls.
for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
{
Instruction *v = dyn_cast<Instruction>(*i);
if(!v)
continue;
//errs() <<"reached here:"<< *v << "\n";
//do profiling for the def
vector<const Type*> argTypes(1);
argTypes[0] = Type::getInt32Ty(context); // enum for the options
FunctionType* countFuncType = FunctionType::get( Type::getVoidTy(context), argTypes, 0 );
Constant* countFunc = m->getOrInsertFunction("doProfiling", countFuncType); // get the injection function
vector<Value*> countArgs(1);
const IntegerType* itype = IntegerType::get(context,32);
Value* defVal = ConstantInt::get(itype, DEF );
countArgs[0] = defVal; //enum for branch
Instruction *beforeInst;
if(isa<PHINode>(v))
{ BasicBlock *bb = v->getParent();
beforeInst = bb->getFirstNonPHI();
}
else
beforeInst = v;
CallInst::Create( countFunc, countArgs.begin(),countArgs.end(), "", beforeInst); // insert the profiling call before the def:v
}
}
else if(profileoption == 'a' )
{
Instruction *beforeInst;
//consider all instructions profiling
vector<const Type*> argTypes(1);
argTypes[0] = Type::getInt32Ty(context); // enum for the options
FunctionType* countFuncType = FunctionType::get( Type::getVoidTy(context), argTypes, 0 );
Constant* countFunc = m->getOrInsertFunction("doProfiling", countFuncType); // get the injection function
vector<Value*> countArgs(1);
const IntegerType* itype = IntegerType::get(context,32);
Value* allVal = ConstantInt::get(itype, ALL );
if(isa<PHINode>(I))
{ BasicBlock *bb = I->getParent();
beforeInst = bb->getFirstNonPHI();
}
else
beforeInst = I;
countArgs[0] = allVal; //enum for All inst
CallInst::Create( countFunc, countArgs.begin(),countArgs.end(), "", beforeInst); // Insert the inject call before the instruction
}
//in fact, here we only use backwardslice ('s')
else if(profileoption == 's')
{
const Type* returnType = I->getType();
if (returnType->isVoidTy() || !filter(I))//Here we can insert a new filter ///////////////////////////////////////////////
{
//errs()<<"filter not passed\n";
continue;
}
//.........这里部分代码省略.........
示例8: runOnFunction
bool ObjCARCContract::runOnFunction(Function &F) {
if (!EnableARCOpts)
return false;
// If nothing in the Module uses ARC, don't do anything.
if (!Run)
return false;
Changed = false;
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults());
DenseMap<BasicBlock *, ColorVector> BlockColors;
if (F.hasPersonalityFn() &&
isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
BlockColors = colorEHFunclets(F);
LLVM_DEBUG(llvm::dbgs() << "**** ObjCARC Contract ****\n");
// Track whether it's ok to mark objc_storeStrong calls with the "tail"
// keyword. Be conservative if the function has variadic arguments.
// It seems that functions which "return twice" are also unsafe for the
// "tail" argument, because they are setjmp, which could need to
// return to an earlier stack state.
bool TailOkForStoreStrongs =
!F.isVarArg() && !F.callsFunctionThatReturnsTwice();
// For ObjC library calls which return their argument, replace uses of the
// argument with uses of the call return value, if it dominates the use. This
// reduces register pressure.
SmallPtrSet<Instruction *, 4> DependingInstructions;
SmallPtrSet<const BasicBlock *, 4> Visited;
for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E;) {
Instruction *Inst = &*I++;
LLVM_DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
// First try to peephole Inst. If there is nothing further we can do in
// terms of undoing objc-arc-expand, process the next inst.
if (tryToPeepholeInstruction(F, Inst, I, DependingInstructions, Visited,
TailOkForStoreStrongs, BlockColors))
continue;
// Otherwise, try to undo objc-arc-expand.
// Don't use GetArgRCIdentityRoot because we don't want to look through bitcasts
// and such; to do the replacement, the argument must have type i8*.
// Function for replacing uses of Arg dominated by Inst.
auto ReplaceArgUses = [Inst, this](Value *Arg) {
// If we're compiling bugpointed code, don't get in trouble.
if (!isa<Instruction>(Arg) && !isa<Argument>(Arg))
return;
// Look through the uses of the pointer.
for (Value::use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
UI != UE; ) {
// Increment UI now, because we may unlink its element.
Use &U = *UI++;
unsigned OperandNo = U.getOperandNo();
// If the call's return value dominates a use of the call's argument
// value, rewrite the use to use the return value. We check for
// reachability here because an unreachable call is considered to
// trivially dominate itself, which would lead us to rewriting its
// argument in terms of its return value, which would lead to
// infinite loops in GetArgRCIdentityRoot.
if (!DT->isReachableFromEntry(U) || !DT->dominates(Inst, U))
continue;
Changed = true;
Instruction *Replacement = Inst;
Type *UseTy = U.get()->getType();
if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) {
// For PHI nodes, insert the bitcast in the predecessor block.
unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
BasicBlock *IncomingBB = PHI->getIncomingBlock(ValNo);
if (Replacement->getType() != UseTy) {
// A catchswitch is both a pad and a terminator, meaning a basic
// block with a catchswitch has no insertion point. Keep going up
// the dominator tree until we find a non-catchswitch.
BasicBlock *InsertBB = IncomingBB;
while (isa<CatchSwitchInst>(InsertBB->getFirstNonPHI())) {
InsertBB = DT->getNode(InsertBB)->getIDom()->getBlock();
}
assert(DT->dominates(Inst, &InsertBB->back()) &&
"Invalid insertion point for bitcast");
Replacement =
new BitCastInst(Replacement, UseTy, "", &InsertBB->back());
}
// While we're here, rewrite all edges for this PHI, rather
// than just one use at a time, to minimize the number of
// bitcasts we emit.
for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
if (PHI->getIncomingBlock(i) == IncomingBB) {
// Keep the UI iterator valid.
//.........这里部分代码省略.........
示例9: CloneBasicBlock
/// Create a clone of the blocks in a loop and connect them together.
/// If CreateRemainderLoop is false, loop structure will not be cloned,
/// otherwise a new loop will be created including all cloned blocks, and the
/// iterator of it switches to count NewIter down to 0.
/// The cloned blocks should be inserted between InsertTop and InsertBot.
/// If loop structure is cloned InsertTop should be new preheader, InsertBot
/// new loop exit.
/// Return the new cloned loop that is created when CreateRemainderLoop is true.
static Loop *
CloneLoopBlocks(Loop *L, Value *NewIter, const bool CreateRemainderLoop,
const bool UseEpilogRemainder, const bool UnrollRemainder,
BasicBlock *InsertTop,
BasicBlock *InsertBot, BasicBlock *Preheader,
std::vector<BasicBlock *> &NewBlocks, LoopBlocksDFS &LoopBlocks,
ValueToValueMapTy &VMap, DominatorTree *DT, LoopInfo *LI) {
StringRef suffix = UseEpilogRemainder ? "epil" : "prol";
BasicBlock *Header = L->getHeader();
BasicBlock *Latch = L->getLoopLatch();
Function *F = Header->getParent();
LoopBlocksDFS::RPOIterator BlockBegin = LoopBlocks.beginRPO();
LoopBlocksDFS::RPOIterator BlockEnd = LoopBlocks.endRPO();
Loop *ParentLoop = L->getParentLoop();
NewLoopsMap NewLoops;
NewLoops[ParentLoop] = ParentLoop;
if (!CreateRemainderLoop)
NewLoops[L] = ParentLoop;
// For each block in the original loop, create a new copy,
// and update the value map with the newly created values.
for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
BasicBlock *NewBB = CloneBasicBlock(*BB, VMap, "." + suffix, F);
NewBlocks.push_back(NewBB);
// If we're unrolling the outermost loop, there's no remainder loop,
// and this block isn't in a nested loop, then the new block is not
// in any loop. Otherwise, add it to loopinfo.
if (CreateRemainderLoop || LI->getLoopFor(*BB) != L || ParentLoop)
addClonedBlockToLoopInfo(*BB, NewBB, LI, NewLoops);
VMap[*BB] = NewBB;
if (Header == *BB) {
// For the first block, add a CFG connection to this newly
// created block.
InsertTop->getTerminator()->setSuccessor(0, NewBB);
}
if (DT) {
if (Header == *BB) {
// The header is dominated by the preheader.
DT->addNewBlock(NewBB, InsertTop);
} else {
// Copy information from original loop to unrolled loop.
BasicBlock *IDomBB = DT->getNode(*BB)->getIDom()->getBlock();
DT->addNewBlock(NewBB, cast<BasicBlock>(VMap[IDomBB]));
}
}
if (Latch == *BB) {
// For the last block, if CreateRemainderLoop is false, create a direct
// jump to InsertBot. If not, create a loop back to cloned head.
VMap.erase((*BB)->getTerminator());
BasicBlock *FirstLoopBB = cast<BasicBlock>(VMap[Header]);
BranchInst *LatchBR = cast<BranchInst>(NewBB->getTerminator());
IRBuilder<> Builder(LatchBR);
if (!CreateRemainderLoop) {
Builder.CreateBr(InsertBot);
} else {
PHINode *NewIdx = PHINode::Create(NewIter->getType(), 2,
suffix + ".iter",
FirstLoopBB->getFirstNonPHI());
Value *IdxSub =
Builder.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1),
NewIdx->getName() + ".sub");
Value *IdxCmp =
Builder.CreateIsNotNull(IdxSub, NewIdx->getName() + ".cmp");
Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot);
NewIdx->addIncoming(NewIter, InsertTop);
NewIdx->addIncoming(IdxSub, NewBB);
}
LatchBR->eraseFromParent();
}
}
// Change the incoming values to the ones defined in the preheader or
// cloned loop.
for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
PHINode *NewPHI = cast<PHINode>(VMap[&*I]);
if (!CreateRemainderLoop) {
if (UseEpilogRemainder) {
unsigned idx = NewPHI->getBasicBlockIndex(Preheader);
NewPHI->setIncomingBlock(idx, InsertTop);
NewPHI->removeIncomingValue(Latch, false);
} else {
VMap[&*I] = NewPHI->getIncomingValueForBlock(Preheader);
cast<BasicBlock>(VMap[Header])->getInstList().erase(NewPHI);
}
} else {
unsigned idx = NewPHI->getBasicBlockIndex(Preheader);
NewPHI->setIncomingBlock(idx, InsertTop);
BasicBlock *NewLatch = cast<BasicBlock>(VMap[Latch]);
//.........这里部分代码省略.........
示例10: copyLoopBodyToHeader
Value* ModuloSchedulerDriverPass::copyLoopBodyToHeader(Instruction* inst,
Instruction* induction, BasicBlock* header, int offset){
// Holds the body of the interesting loop
BasicBlock *body = inst->getParent();
assert(header && "Header is null");
assert(header->getTerminator() && "Header has no terminator");
// Maps the old instructions to the new Instructions
DenseMap<const Value *, Value *> ValueMap;
// Do the actual clone
stringstream iname;
iname<<"___"<<offset<<"___";
BasicBlock* newBB = CloneBasicBlock(body, ValueMap, iname.str().c_str());
// Fixing the dependencies for each of the instructions in the cloned BB
// They now depend on themselves rather on the old cloned BB.
for (BasicBlock::iterator it = newBB->begin(); it != newBB->end(); ++it) {
for (Instruction::op_iterator ops = (it)->op_begin(); ops != (it)->op_end(); ++ops) {
if (ValueMap.end() != ValueMap.find(*ops)) {
//*ops = ValueMap[*ops];
it->replaceUsesOfWith(*ops, ValueMap[*ops]);
}
}
}
// Fixing the PHI nodes since they are no longer needed
for (BasicBlock::iterator it = newBB->begin(); it != newBB->end(); ++it) {
if (PHINode *phi = dyn_cast<PHINode>(it)) {
// Taking the preheader entryfrom the PHI node
Value* prevalue = phi->getIncomingValue(phi->getBasicBlockIndex(header));
assert(prevalue && "no prevalue. Don't know what to do");
// If we are handling a PHI node which is the induction index ? A[PHI(i,0)] ?
// If so, turn it into A[i + offset]
if (ValueMap[induction] == phi) {
Instruction *add = subscripts::incrementValue(prevalue, offset);
//add->insertBefore(phi); This is the same as next line (compiles on LLVM2.1)
phi->getParent()->getInstList().insert(phi, add);
phi->replaceAllUsesWith(add);
} else {
// eliminating the PHI node all together
// This is just a regular variable or constant. No need to increment
// the index.
phi->replaceAllUsesWith(prevalue);
}
}
}
// Move all non PHI and non terminator instructions into the header.
while (!newBB->getFirstNonPHI()->isTerminator()) {
Instruction* inst = newBB->getFirstNonPHI();
if (dyn_cast<StoreInst>(inst)) {
inst->eraseFromParent();
} else {
inst->moveBefore(header->getTerminator());
}
}
newBB->dropAllReferences();
return ValueMap[inst];
}
示例11: IFI
/// InlineHalfPowrs - Inline a sequence of adjacent half_powr calls, rearranging
/// their control flow to better facilitate subsequent optimization.
Instruction *
SimplifyHalfPowrLibCalls::
InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs,
Instruction *InsertPt) {
std::vector<BasicBlock *> Bodies;
BasicBlock *NewBlock = 0;
for (unsigned i = 0, e = HalfPowrs.size(); i != e; ++i) {
CallInst *Call = cast<CallInst>(HalfPowrs[i]);
Function *Callee = Call->getCalledFunction();
// Minimally sanity-check the CFG of half_powr to ensure that it contains
// the kind of code we expect. If we're running this pass, we have
// reason to believe it will be what we expect.
Function::iterator I = Callee->begin();
BasicBlock *Prologue = I++;
if (I == Callee->end()) break;
BasicBlock *SubnormalHandling = I++;
if (I == Callee->end()) break;
BasicBlock *Body = I++;
if (I != Callee->end()) break;
if (SubnormalHandling->getSinglePredecessor() != Prologue)
break;
BranchInst *PBI = dyn_cast<BranchInst>(Prologue->getTerminator());
if (!PBI || !PBI->isConditional())
break;
BranchInst *SNBI = dyn_cast<BranchInst>(SubnormalHandling->getTerminator());
if (!SNBI || SNBI->isConditional())
break;
if (!isa<ReturnInst>(Body->getTerminator()))
break;
Instruction *NextInst = llvm::next(BasicBlock::iterator(Call));
// Inline the call, taking care of what code ends up where.
NewBlock = SplitBlock(NextInst->getParent(), NextInst, this);
InlineFunctionInfo IFI(0, TD);
bool B = InlineFunction(Call, IFI);
assert(B && "half_powr didn't inline?"); B=B;
BasicBlock *NewBody = NewBlock->getSinglePredecessor();
assert(NewBody);
Bodies.push_back(NewBody);
}
if (!NewBlock)
return InsertPt;
// Put the code for all the bodies into one block, to facilitate
// subsequent optimization.
(void)SplitEdge(NewBlock->getSinglePredecessor(), NewBlock, this);
for (unsigned i = 0, e = Bodies.size(); i != e; ++i) {
BasicBlock *Body = Bodies[i];
Instruction *FNP = Body->getFirstNonPHI();
// Splice the insts from body into NewBlock.
NewBlock->getInstList().splice(NewBlock->begin(), Body->getInstList(),
FNP, Body->getTerminator());
}
return NewBlock->begin();
}
示例12: PromoteAliasSet
//.........这里部分代码省略.........
// If we haven't seen a store yet, this is a live in use, otherwise
// use the stored value.
if (StoredValue) {
L->replaceAllUsesWith(StoredValue);
ReplacedLoads[L] = StoredValue;
} else {
LiveInLoads.push_back(L);
}
continue;
}
if (StoreInst *S = dyn_cast<StoreInst>(II)) {
// If this is a store to an unrelated pointer, ignore it.
if (!PointerMustAliases.count(S->getOperand(1))) continue;
// Remember that this is the active value in the block.
StoredValue = S->getOperand(0);
}
}
// The last stored value that happened is the live-out for the block.
assert(StoredValue && "Already checked that there is a store in block");
SSA.AddAvailableValue(BB, StoredValue);
BlockUses.clear();
}
// Now that all the intra-loop values are classified, set up the preheader.
// It gets a load of the pointer we're promoting, and it is the live-out value
// from the preheader.
LoadInst *PreheaderLoad = new LoadInst(SomePtr,SomePtr->getName()+".promoted",
Preheader->getTerminator());
SSA.AddAvailableValue(Preheader, PreheaderLoad);
// Now that the preheader is good to go, set up the exit blocks. Each exit
// block gets a store of the live-out values that feed them. Since we've
// already told the SSA updater about the defs in the loop and the preheader
// definition, it is all set and we can start using it.
SmallVector<BasicBlock*, 8> ExitBlocks;
CurLoop->getUniqueExitBlocks(ExitBlocks);
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
BasicBlock *ExitBlock = ExitBlocks[i];
Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
Instruction *InsertPos = ExitBlock->getFirstNonPHI();
new StoreInst(LiveInValue, SomePtr, InsertPos);
}
// Okay, now we rewrite all loads that use live-in values in the loop,
// inserting PHI nodes as necessary.
for (unsigned i = 0, e = LiveInLoads.size(); i != e; ++i) {
LoadInst *ALoad = LiveInLoads[i];
Value *NewVal = SSA.GetValueInMiddleOfBlock(ALoad->getParent());
ALoad->replaceAllUsesWith(NewVal);
CurAST->copyValue(ALoad, NewVal);
ReplacedLoads[ALoad] = NewVal;
}
// If the preheader load is itself a pointer, we need to tell alias analysis
// about the new pointer we created in the preheader block and about any PHI
// nodes that just got inserted.
if (PreheaderLoad->getType()->isPointerTy()) {
// Copy any value stored to or loaded from a must-alias of the pointer.
CurAST->copyValue(SomeValue, PreheaderLoad);
for (unsigned i = 0, e = NewPHIs.size(); i != e; ++i)
CurAST->copyValue(SomeValue, NewPHIs[i]);
}
// Now that everything is rewritten, delete the old instructions from the body
// of the loop. They should all be dead now.
for (unsigned i = 0, e = LoopUses.size(); i != e; ++i) {
Instruction *User = LoopUses[i];
// If this is a load that still has uses, then the load must have been added
// as a live value in the SSAUpdate data structure for a block (e.g. because
// the loaded value was stored later). In this case, we need to recursively
// propagate the updates until we get to the real value.
if (!User->use_empty()) {
Value *NewVal = ReplacedLoads[User];
assert(NewVal && "not a replaced load?");
// Propagate down to the ultimate replacee. The intermediately loads
// could theoretically already have been deleted, so we don't want to
// dereference the Value*'s.
DenseMap<Value*, Value*>::iterator RLI = ReplacedLoads.find(NewVal);
while (RLI != ReplacedLoads.end()) {
NewVal = RLI->second;
RLI = ReplacedLoads.find(NewVal);
}
User->replaceAllUsesWith(NewVal);
CurAST->copyValue(User, NewVal);
}
CurAST->deleteValue(User);
User->eraseFromParent();
}
// fwew, we're done!
}
示例13: sink
/// sink - When an instruction is found to only be used outside of the loop,
/// this function moves it to the exit blocks and patches up SSA form as needed.
/// This method is guaranteed to remove the original instruction from its
/// position, and may either delete it or move it to outside of the loop.
///
void LICM::sink(Instruction &I) {
DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
SmallVector<BasicBlock*, 8> ExitBlocks;
CurLoop->getUniqueExitBlocks(ExitBlocks);
if (isa<LoadInst>(I)) ++NumMovedLoads;
else if (isa<CallInst>(I)) ++NumMovedCalls;
++NumSunk;
Changed = true;
// The case where there is only a single exit node of this loop is common
// enough that we handle it as a special (more efficient) case. It is more
// efficient to handle because there are no PHI nodes that need to be placed.
if (ExitBlocks.size() == 1) {
if (!isExitBlockDominatedByBlockInLoop(ExitBlocks[0], I.getParent())) {
// Instruction is not used, just delete it.
CurAST->deleteValue(&I);
// If I has users in unreachable blocks, eliminate.
// If I is not void type then replaceAllUsesWith undef.
// This allows ValueHandlers and custom metadata to adjust itself.
if (!I.use_empty())
I.replaceAllUsesWith(UndefValue::get(I.getType()));
I.eraseFromParent();
} else {
// Move the instruction to the start of the exit block, after any PHI
// nodes in it.
I.moveBefore(ExitBlocks[0]->getFirstNonPHI());
// This instruction is no longer in the AST for the current loop, because
// we just sunk it out of the loop. If we just sunk it into an outer
// loop, we will rediscover the operation when we process it.
CurAST->deleteValue(&I);
}
return;
}
if (ExitBlocks.empty()) {
// The instruction is actually dead if there ARE NO exit blocks.
CurAST->deleteValue(&I);
// If I has users in unreachable blocks, eliminate.
// If I is not void type then replaceAllUsesWith undef.
// This allows ValueHandlers and custom metadata to adjust itself.
if (!I.use_empty())
I.replaceAllUsesWith(UndefValue::get(I.getType()));
I.eraseFromParent();
return;
}
// Otherwise, if we have multiple exits, use the SSAUpdater to do all of the
// hard work of inserting PHI nodes as necessary.
SmallVector<PHINode*, 8> NewPHIs;
SSAUpdater SSA(&NewPHIs);
if (!I.use_empty())
SSA.Initialize(I.getType(), I.getName());
// Insert a copy of the instruction in each exit block of the loop that is
// dominated by the instruction. Each exit block is known to only be in the
// ExitBlocks list once.
BasicBlock *InstOrigBB = I.getParent();
unsigned NumInserted = 0;
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
BasicBlock *ExitBlock = ExitBlocks[i];
if (!isExitBlockDominatedByBlockInLoop(ExitBlock, InstOrigBB))
continue;
// Insert the code after the last PHI node.
BasicBlock::iterator InsertPt = ExitBlock->getFirstNonPHI();
// If this is the first exit block processed, just move the original
// instruction, otherwise clone the original instruction and insert
// the copy.
Instruction *New;
if (NumInserted++ == 0) {
I.moveBefore(InsertPt);
New = &I;
} else {
New = I.clone();
if (!I.getName().empty())
New->setName(I.getName()+".le");
ExitBlock->getInstList().insert(InsertPt, New);
}
// Now that we have inserted the instruction, inform SSAUpdater.
if (!I.use_empty())
SSA.AddAvailableValue(ExitBlock, New);
}
// If the instruction doesn't dominate any exit blocks, it must be dead.
if (NumInserted == 0) {
CurAST->deleteValue(&I);
if (!I.use_empty())
//.........这里部分代码省略.........
示例14: OptimizeExtUses
bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
BasicBlock *DefBB = I->getParent();
// If both result of the {s|z}xt and its source are live out, rewrite all
// other uses of the source with result of extension.
Value *Src = I->getOperand(0);
if (Src->hasOneUse())
return false;
// Only do this xform if truncating is free.
if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
return false;
// Only safe to perform the optimization if the source is also defined in
// this block.
if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
return false;
bool DefIsLiveOut = false;
for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
UI != E; ++UI) {
Instruction *User = cast<Instruction>(*UI);
// Figure out which BB this ext is used in.
BasicBlock *UserBB = User->getParent();
if (UserBB == DefBB) continue;
DefIsLiveOut = true;
break;
}
if (!DefIsLiveOut)
return false;
// Make sure non of the uses are PHI nodes.
for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
UI != E; ++UI) {
Instruction *User = cast<Instruction>(*UI);
BasicBlock *UserBB = User->getParent();
if (UserBB == DefBB) continue;
// Be conservative. We don't want this xform to end up introducing
// reloads just before load / store instructions.
if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User))
return false;
}
// InsertedTruncs - Only insert one trunc in each block once.
DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
bool MadeChange = false;
for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
UI != E; ++UI) {
Use &TheUse = UI.getUse();
Instruction *User = cast<Instruction>(*UI);
// Figure out which BB this ext is used in.
BasicBlock *UserBB = User->getParent();
if (UserBB == DefBB) continue;
// Both src and def are live in this block. Rewrite the use.
Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
if (!InsertedTrunc) {
BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
}
// Replace a use of the {s|z}ext source with a use of the result.
TheUse = InsertedTrunc;
MadeChange = true;
}
return MadeChange;
}
示例15: runOnModule
virtual bool runOnModule(Module &M)
{
LLVMContext &C = M.getContext();
//errs() << "Pass called sucessfully!\n";
std::vector<BranchInst*> vCondBranch;
//Function *pi = (Function*)M.getOrInsertFunction("piInt", Type::getInt32Ty(C), Type::getInt32Ty(C), NULL);
//BasicBlock* piBlock = BasicBlock::Create(C, "piFuncBlock", pi);
//IRBuilder<> builder(piBlock);
//builder.CreateRet(pi->arg_begin());
//first go through and grab all of the conditional branches
for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
{
for(Function::iterator b = MI->begin(), end = MI->end(); b != end; ++b)
{
//leave func defs alone
if (!MI->isDeclaration())
{
if(BranchInst *bi = dyn_cast<BranchInst>(b->getTerminator()))
{
if(bi->isConditional())
{
//errs() << "Have a conditional branch!\n";
vCondBranch.push_back(bi);
}
}
}
}
}
for(std::vector<BranchInst*>::iterator i = vCondBranch.begin(), e = vCondBranch.end(); i != e; ++i)
{
DEBUG_PRINT("Examining a conditional branch!");
Value *ops[2];
if(CmpInst *cmp = dyn_cast<CmpInst>((*i)->getCondition()))
{
if (cmp->getNumOperands() < 2)
continue;
if (!cmp->isIntPredicate())
continue;
ops[0] = cmp->getOperand(0);
ops[1] = cmp->getOperand(1);
//for(unsigned int x = 0; x < (*i)->getNumSuccessors(); ++x)
//{
BasicBlock* trueBlock = (*i)->getSuccessor(0);
BasicBlock* falseBlock = (*i)->getSuccessor(1);
//BasicBlock* curr = (*i)->getSuccessor(x);
IRBuilder<> builder(trueBlock->getFirstNonPHI());
//builder.SetInsertPoint(curr->getFirstNonPHI());
if(!isa<Constant>(ops[0]))
{
if(isa<LoadInst>(ops[0]))
{
pred_iterator PI = pred_begin(trueBlock);
BasicBlock* Pred = *PI;
if(++PI != pred_end(trueBlock))
continue;
PHINode* pi;
pi = PHINode::Create(ops[0]->getType(), 1, "piFunc_t", trueBlock->begin());
pi->addIncoming(ops[0], Pred);
builder.SetInsertPoint(trueBlock->getFirstNonPHI());
builder.CreateStore(pi, ((LoadInst*)ops[0])->getOperand(0));
PI = pred_begin(falseBlock);
Pred = *PI;
if(++PI != pred_end(falseBlock))
continue;
pi = PHINode::Create(ops[0]->getType(), 1, "piFunc_f", falseBlock->begin());
pi->addIncoming(ops[0], Pred);
builder.SetInsertPoint(falseBlock->getFirstNonPHI());
builder.CreateStore(pi, ((LoadInst*)ops[0])->getOperand(0));
}
}
if(!isa<Constant>(ops[1]))
{
if(isa<LoadInst>(ops[1]))
{
pred_iterator PI = pred_begin(trueBlock);
BasicBlock* Pred = *PI;
if(++PI != pred_end(trueBlock))
continue;
PHINode* pi;
pi = PHINode::Create(ops[1]->getType(), 1, "piFunc_t", trueBlock->begin());
//.........这里部分代码省略.........