///////////////////////////////////// //findLastSyn() // //Find the last instruction in BB. Any instructions before this one must be replicated except Synchpoint ///////////////////////////////////// Instruction* InsDuplica::findLastCond (BasicBlock *BB) { TerminatorInst *lastIns = BB->getTerminator(); assert(!(isa<SwitchInst>(lastIns)) && "Find a SwitchInst! You need to lower SwitchInst."); BranchInst * BI = dyn_cast<BranchInst>(lastIns); if (BI && (BI->isConditional())) { Value *cond = BI->getCondition(); Instruction* condIns = dyn_cast<Instruction>(cond); //find condIns. But we have to make sure condIns is the second to the last instruction in BB assert(condIns && "Branch Condition must not be trivial"); if ((condIns->getNextNode())!=lastIns) { //assert((condIns->getParent() == BB) && "condIns is not in the same BB as br!"); //if condIns is not in the same BB as br. We have to leave it there if (condIns->getParent() != BB) return lastIns; //if condIns is a PHINode, since we can not move, we just return BI //if condIns has more than one use, better not to reorder if ((isa<PHINode>(condIns))||!(condIns->hasOneUse())) return lastIns; condIns->moveBefore(lastIns); //we moved condInst right before br #ifdef Jing_DEBUG std::cerr << "adjust order of condIns "<< condIns->getName() <<" in " << BB->getName() <<"\n"; #endif } #ifdef Jing_DEBUG std::cerr << "findLastCond returns " << condIns->getName() <<"\n"; #endif return condIns; } //return the terminator instruction return lastIns; }
// Look for control dependencies on a read. bool branchesOn(BasicBlock *bb, Value *load, ICmpInst **icmpOut, int *outIdx) { // XXX: make this platform configured; on some platforms maybe an // atomic cmpxchg does /not/ behave like it branches on the old value if (isa<AtomicCmpXchgInst>(load) || isa<AtomicRMWInst>(load)) { if (icmpOut) *icmpOut = nullptr; if (outIdx) *outIdx = 0; return true; } // TODO: we should be able to follow values through phi nodes, // since we are path dependent anyways. BranchInst *br = dyn_cast<BranchInst>(bb->getTerminator()); if (!br || !br->isConditional()) return false; // TODO: We only check one level of things. Check deeper? // We pretty heavily restrict what operations we handle here. // Some would just be wrong (like call), but really icmp is // the main one, so. Probably we should be able to also // pick through casts and wideness changes. ICmpInst *icmp = dyn_cast<ICmpInst>(br->getCondition()); if (!icmp) return false; int idx = 0; for (auto v : icmp->operand_values()) { if (getRealValue(v) == load) { if (icmpOut) *icmpOut = icmp; if (outIdx) *outIdx = idx; return true; } ++idx; } return false; }
/// EliminateFallThrough - Merge basic blocks which are connected /// by a single edge, where one of the basic blocks has a single successor /// pointing to the other basic block, which has a single predecessor. bool CodeGenPrepare::EliminateFallThrough(Function &F) { bool Changed = false; // Scan all of the blocks in the function, except for the entry block. for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { BasicBlock *BB = I++; // If the destination block has a single pred, then this is a trivial // edge, just collapse it. BasicBlock *SinglePred = BB->getSinglePredecessor(); // Don't merge if BB's address is taken. if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue; BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); if (Term && !Term->isConditional()) { Changed = true; DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n"); // Remember if SinglePred was the entry block of the function. // If so, we will need to move BB back to the entry position. bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); MergeBasicBlockIntoOnlyPred(BB, this); if (isEntry && BB != &BB->getParent()->getEntryBlock()) BB->moveBefore(&BB->getParent()->getEntryBlock()); // We have erased a block. Update the iterator. I = BB; } } return Changed; }
//It receives a BasicBLock and makes table of predicates and its respective gated instructions void bSSA::makeTable (BasicBlock *BB, Function *F) { Value *condition; TerminatorInst *ti = BB->getTerminator(); BranchInst *bi = NULL; SwitchInst *si=NULL; PostDominatorTree &PD = getAnalysis<PostDominatorTree>(*F); ProcessedBB.clear(); if ((bi = dyn_cast<BranchInst>(ti)) && bi->isConditional()) { //If the terminator instruction is a conditional branch condition = bi->getCondition(); //Including the predicate on the predicatesVector predicatesVector.push_back(new Pred(condition)); //Make a "Flooding" on each sucessor gated the instruction on Influence Region of the predicate for (unsigned int i=0; i<bi->getNumSuccessors(); i++) { findIR (BB, bi->getSuccessor(i),PD); } }else if ((si = dyn_cast<SwitchInst>(ti))) { condition = si->getCondition(); //Including the predicate on the predicatesVector predicatesVector.push_back(new Pred(condition)); //Make a "Flooding" on each sucessor gated the instruction on Influence Region of the predicate for (unsigned int i=0; i<si->getNumSuccessors(); i++) { findIR (BB, si->getSuccessor(i),PD); } } }
void MakeDispatcherPass::ConvertCmp(Function& function) { typedef std::vector< Instruction * > InstList; InstList insts; for (Function::iterator BB = function.begin(), bbE = function.end(); BB != bbE; ++BB) { for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { if (isa< CmpInst >(I)) { insts.push_back(I); } if (isa< BranchInst >(I)) { BasicBlock::iterator save = I; BranchInst* branchInst = dynamic_cast< BranchInst *>(&*I); if (branchInst->isConditional() && !insts.empty()) { Value* valbranch = NULL; valbranch = branchInst->getCondition(); ShowType(dynamic_cast<CmpInst*>(insts[0])); CreateInt3(BB, I); I++; save->eraseFromParent(); insts.pop_back(); continue; } } I++; } } }
bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) { BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); if (!BI || !BI->isConditional()) return false; Value *Cond = BI->getCondition(); ICmpInst *CI = dyn_cast<ICmpInst>(Cond); if (!CI) return false; Value *RHS = CI->getOperand(1); ConstantInt *CV = dyn_cast<ConstantInt>(RHS); if (!CV) return false; bool isProb; if (CV->isZero()) { switch (CI->getPredicate()) { case CmpInst::ICMP_EQ: // X == 0 -> Unlikely isProb = false; break; case CmpInst::ICMP_NE: // X != 0 -> Likely isProb = true; break; case CmpInst::ICMP_SLT: // X < 0 -> Unlikely isProb = false; break; case CmpInst::ICMP_SGT: // X > 0 -> Likely isProb = true; break; default: return false; } } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { // InstCombine canonicalizes X <= 0 into X < 1. // X <= 0 -> Unlikely isProb = false; } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) { // InstCombine canonicalizes X >= 0 into X > -1. // X >= 0 -> Likely isProb = true; } else { return false; } BasicBlock *Taken = BI->getSuccessor(0); BasicBlock *NonTaken = BI->getSuccessor(1); if (!isProb) std::swap(Taken, NonTaken); BP->setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT); BP->setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT); return true; }
bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) { BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); if (!BI || !BI->isConditional()) return false; Value *Cond = BI->getCondition(); FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond); if (!FCmp) return false; bool isProb; if (FCmp->isEquality()) { // f1 == f2 -> Unlikely // f1 != f2 -> Likely isProb = !FCmp->isTrueWhenEqual(); } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) { // !isnan -> Likely isProb = true; } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) { // isnan -> Unlikely isProb = false; } else { return false; } unsigned TakenIdx = 0, NonTakenIdx = 1; if (!isProb) std::swap(TakenIdx, NonTakenIdx); setEdgeWeight(BB, TakenIdx, FPH_TAKEN_WEIGHT); setEdgeWeight(BB, NonTakenIdx, FPH_NONTAKEN_WEIGHT); return true; }
// Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion // between two pointer or pointer and NULL will fail. bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) { BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); if (!BI || !BI->isConditional()) return false; Value *Cond = BI->getCondition(); ICmpInst *CI = dyn_cast<ICmpInst>(Cond); if (!CI || !CI->isEquality()) return false; Value *LHS = CI->getOperand(0); if (!LHS->getType()->isPointerTy()) return false; assert(CI->getOperand(1)->getType()->isPointerTy()); // p != 0 -> isProb = true // p == 0 -> isProb = false // p != q -> isProb = true // p == q -> isProb = false; unsigned TakenIdx = 0, NonTakenIdx = 1; bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE; if (!isProb) std::swap(TakenIdx, NonTakenIdx); setEdgeWeight(BB, TakenIdx, PH_TAKEN_WEIGHT); setEdgeWeight(BB, NonTakenIdx, PH_NONTAKEN_WEIGHT); return true; }
/// \brief Analyze the predecessors of each block and build up predicates void StructurizeCFG::gatherPredicates(RegionNode *N) { RegionInfo *RI = ParentRegion->getRegionInfo(); BasicBlock *BB = N->getEntry(); BBPredicates &Pred = Predicates[BB]; BBPredicates &LPred = LoopPreds[BB]; for (BasicBlock *P : predecessors(BB)) { // Ignore it if it's a branch from outside into our region entry if (!ParentRegion->contains(P)) continue; Region *R = RI->getRegionFor(P); if (R == ParentRegion) { // It's a top level block in our region BranchInst *Term = cast<BranchInst>(P->getTerminator()); for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { BasicBlock *Succ = Term->getSuccessor(i); if (Succ != BB) continue; if (Visited.count(P)) { // Normal forward edge if (Term->isConditional()) { // Try to treat it like an ELSE block BasicBlock *Other = Term->getSuccessor(!i); if (Visited.count(Other) && !Loops.count(Other) && !Pred.count(Other) && !Pred.count(P)) { Pred[Other] = BoolFalse; Pred[P] = BoolTrue; continue; } } Pred[P] = buildCondition(Term, i, false); } else { // Back edge LPred[P] = buildCondition(Term, i, true); } } } else { // It's an exit from a sub region while (R->getParent() != ParentRegion) R = R->getParent(); // Edge from inside a subregion to its entry, ignore it if (*R == *N) continue; BasicBlock *Entry = R->getEntry(); if (Visited.count(Entry)) Pred[Entry] = BoolTrue; else LPred[Entry] = BoolFalse; } } }
bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) { BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); if (!BI || !BI->isConditional()) return false; Value *Cond = BI->getCondition(); ICmpInst *CI = dyn_cast<ICmpInst>(Cond); if (!CI) return false; Value *RHS = CI->getOperand(1); ConstantInt *CV = dyn_cast<ConstantInt>(RHS); if (!CV || !CV->isZero()) return false; bool isProb; switch (CI->getPredicate()) { case CmpInst::ICMP_EQ: // Equal to zero is not expected to be taken. isProb = false; break; case CmpInst::ICMP_NE: // Not equal to zero is expected. isProb = true; break; case CmpInst::ICMP_SLT: // Less or equal to zero is not expected. // X < 0 -> Unlikely isProb = false; break; case CmpInst::ICMP_UGT: case CmpInst::ICMP_SGT: // Greater or equal to zero is expected. // X > 0 -> Likely isProb = true; break; default: return false; }; BasicBlock *Taken = BI->getSuccessor(0); BasicBlock *NonTaken = BI->getSuccessor(1); if (!isProb) std::swap(Taken, NonTaken); BP->setEdgeWeight(BB, Taken, ZH_TAKEN_WEIGHT); BP->setEdgeWeight(BB, NonTaken, ZH_NONTAKEN_WEIGHT); return true; }
bool ProfilingPass::is_used_by_branch(Instruction *I) { for (Value::use_iterator i = I->use_begin(), e = I->use_end(); i != e; ++i) { Instruction *use = dyn_cast<Instruction>(*i); BranchInst *bi = dyn_cast<BranchInst>(use); if(!bi) continue; if(bi->isConditional()) { return true;} } return false; }
// Tries to remove a sanity check; returns true if it worked. bool AsapPass::optimizeCheckAway(llvm::Instruction *Inst) { BranchInst *BI = cast<BranchInst>(Inst); assert(BI->isConditional() && "Sanity check must be conditional branch."); unsigned int RegularBranch = getRegularBranch(BI, SCI); bool Changed = false; if (RegularBranch == 0) { BI->setCondition(ConstantInt::getTrue(Inst->getContext())); Changed = true; } else if (RegularBranch == 1) { BI->setCondition(ConstantInt::getFalse(Inst->getContext())); Changed = true; } else { // This can happen, e.g., in the following case: // array[-1] = a + b; // is transformed into // if (a + b overflows) // report_overflow() // else // report_index_out_of_bounds(); // In this case, removing the sanity check does not help much, so we // just do nothing. // Thanks to Will Dietz for his explanation at // http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-April/071958.html dbgs() << "Warning: Sanity check with no regular branch found.\n"; dbgs() << "The sanity check has been kept intact.\n"; } if (PrintRemovedChecks && Changed) { DebugLoc DL = getSanityCheckDebugLoc(BI, RegularBranch); printDebugLoc(DL, BI->getContext(), dbgs()); dbgs() << ": SanityCheck with cost "; dbgs() << *BI->getMetadata("cost")->getOperand(0); if (MDNode *IA = DL.getInlinedAt()) { dbgs() << " (inlined at "; printDebugLoc(DebugLoc(IA), BI->getContext(), dbgs()); dbgs() << ")"; } BasicBlock *Succ = BI->getSuccessor(RegularBranch == 0 ? 1 : 0); if (const CallInst *CI = SCI->findSanityCheckCall(Succ)) { dbgs() << " " << CI->getCalledFunction()->getName(); } dbgs() << "\n"; } return Changed; }
void Graph::DFS2_visit(DFSNode * DFS) { DFS->C = GRAY; if (DFS->T == UNKNOWN){ Instruction *I = dynamic_cast<Instruction *>(DFS->Bb->getTerminator()); BranchInst *BI = static_cast<BranchInst *>(I); if (BI->isConditional()){ //branchmap[BI] = dfs->bb; BranchMap[BI->getCondition()] = DFS->Bb; if (TypeStack.empty()) DFS->T = IF; else if (TypeStack.top() != ENDIF) DFS->T = IF; //else if (TypeStack.top() == ENDIF && Time == DFS->DTime - 2) // DFS->T = ELSEIF; else if (DFS->Bb->getName().substr(0,7) == "if.else") DFS->T = ELSEIF; //else if (TypeStack.top() == ELSEIF) // DFS->T = ELSEIF; else if(TypeStack.top() == ENDIF && (Time != DFS->DTime - 1)) DFS->T = IF; else DEBUG (errs() << "\n\n\n\n\n\n\nError at: " << " Top = " << TypeStack.top() << " Time = " << Time << " DTime = " << DFS->DTime << "\n\n\n\n\n\n\n\n\n"); } else { DEBUG (errs() << "Bb: " <<DFS->Bb << " is not conditional\n"); } } if (DFS->T != UNKNOWN){ Time = DFS->DTime; TypeStack.push(DFS->T); DEBUG (errs() << "Stack: " << TypeStack.top() <<'\n'); } DEBUG (errs() << "Type of: " << DFS->Bb << " is: " << DFS->T << " global time: " << Time << " dfstime: " << DFS->DTime << '\n'); // for each vector adjacent to dfs std::vector<DFSNode *> Svec = DFS->Vertex; for (std::vector<DFSNode *>::iterator It = Svec.begin(); It != Svec.end(); ++It) { DFSNode *DN = *It; if (DN->C == WHITE) DFS2_visit(DN); } DFS->C = BLACK; if (DFS->T != UNKNOWN) Time = DFS->FTime; }
void MakeDispatcherPass::ConvertSwitch( Function& function ) { BasicBlock* entryBB = &function.getEntryBlock(); std::cout << ":Processing " << function.getName().str() << std::endl; for( Function::iterator i = function.begin(); i != function.end(); i++ ) { BasicBlock* basicBlock = &*i; Instruction* inst; TerminatorInst* terminator = basicBlock->getTerminator(); assert( terminator && "Basic block is not well formed and has no terminator!" ); if( isa< BranchInst >( terminator ) ) { // std::cout << "Branch Instruction !!!" << std::endl; BranchInst* branchInst = dynamic_cast< BranchInst *> ( basicBlock->getTerminator() ); std::cout << "Branch Instruction, Opcode = " << branchInst->getOpcodeName(); if (branchInst->isConditional()) { inst = dynamic_cast<Instruction *>(basicBlock->getTerminator()); std::cout << ", Is Conditional "; // i->eraseFromParent(); } else { std::cout << ", Is not conditionnal "; } // if (valbranch) // && !valbranch->getName().empty()) // { // valbranch->getType()->dump(); // std::cout << valbranch->getName().str(); // } // branchInst->eraseFromParent(); std::cout << std::endl; // std::cout << branchInst->getCondition()->getName.str() << std::endl; } } }
/// \brief Insert the missing branch conditions void StructurizeCFG::insertConditions(bool Loops) { BranchVector &Conds = Loops ? LoopConds : Conditions; Value *Default = Loops ? BoolTrue : BoolFalse; SSAUpdater PhiInserter; for (BranchVector::iterator I = Conds.begin(), E = Conds.end(); I != E; ++I) { BranchInst *Term = *I; assert(Term->isConditional()); BasicBlock *Parent = Term->getParent(); BasicBlock *SuccTrue = Term->getSuccessor(0); BasicBlock *SuccFalse = Term->getSuccessor(1); PhiInserter.Initialize(Boolean, ""); PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default); PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default); BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue]; NearestCommonDominator Dominator(DT); Dominator.addBlock(Parent, false); Value *ParentValue = 0; for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end(); PI != PE; ++PI) { if (PI->first == Parent) { ParentValue = PI->second; break; } PhiInserter.AddAvailableValue(PI->first, PI->second); Dominator.addBlock(PI->first); } if (ParentValue) { Term->setCondition(ParentValue); } else { if (!Dominator.wasResultExplicitMentioned()) PhiInserter.AddAvailableValue(Dominator.getResult(), Default); Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent)); } } }
//Start findIR method void hammock::processNode (BasicBlock *BB, PostDominatorTree &PD ) { TerminatorInst *ti = BB->getTerminator(); BranchInst *bi = NULL; SwitchInst *si=NULL; if ((bi = dyn_cast<BranchInst>(ti)) && bi->isConditional()) { //If the terminator instruction is a conditional branch for (unsigned int i=0; i<bi->getNumSuccessors(); i++) { findIR (BB, bi->getSuccessor(i),PD); } }else if ((si = dyn_cast<SwitchInst>(ti))) { for (unsigned int i=0; i<si->getNumSuccessors(); i++) { findIR (BB, si->getSuccessor(i),PD); } } }
/// MatchPointerHeuristic - Predict that a comparison of a pointer against /// null or of two pointers will fail. /// @returns a Prediction that is a pair in which the first element is the /// successor taken, and the second the successor not taken. Prediction BranchHeuristicsInfo::MatchPointerHeuristic(BasicBlock *root) const { // Last instruction of basic block. TerminatorInst *TI = root->getTerminator(); // Basic block successors. True and False branches. BasicBlock *trueSuccessor = TI->getSuccessor(0); BasicBlock *falseSuccessor = TI->getSuccessor(1); // Is the last instruction a Branch Instruction? BranchInst *BI = dyn_cast<BranchInst>(TI); if (!BI || !BI->isConditional()) return empty; // Conditional instruction. Value *cond = BI->getCondition(); // Pointer comparisons are integer comparisons. ICmpInst *II = dyn_cast<ICmpInst>(cond); if (!II) return empty; // An integer comparison has always two operands. Value *operand1 = II->getOperand(0); Value *operand2 = II->getOperand(1); // Obtain the type of comparison. enum ICmpInst::Predicate signedPred = II->getSignedPredicate(); // The heuristic states that it must be compared against null, // but in LLVM, null is also a PointerType, so it only requires // to test if there is a comparison between two pointers. if (signedPred == ICmpInst::ICMP_EQ && isa<PointerType>(operand1->getType()) && // NULL is a pointer type too isa<PointerType>(operand2->getType())) { // NULL is a pointer type too return std::make_pair(falseSuccessor, trueSuccessor); } else if (signedPred != ICmpInst::ICMP_EQ && isa<PointerType>(operand1->getType()) && isa<PointerType>(operand2->getType())) { return std::make_pair(trueSuccessor, falseSuccessor); } return empty; }
void vSSA::createSigmasIfNeeded(BasicBlock *BB) { TerminatorInst *ti = BB->getTerminator(); // If the condition used in the terminator instruction is a Comparison instruction: //for each operand of the CmpInst, create sigmas, depending on some conditions /* if(isa<BranchInst>(ti)){ BranchInst * bc = cast<BranchInst>(ti); if(bc->isConditional()){ Value * cond = bc->getCondition(); CmpInst *comparison = dyn_cast<CmpInst>(cond); for (User::const_op_iterator it = comparison->op_begin(), e = comparison->op_end(); it != e; ++it) { Value *operand = *it; if (isa<Instruction>(operand) || isa<Argument>(operand)) { insertSigmas(ti, operand); } } } } */ // CASE 1: Branch Instruction BranchInst *bi = NULL; SwitchInst *si = NULL; if ((bi = dyn_cast<BranchInst>(ti))) { if (bi->isConditional()) { Value *condition = bi->getCondition(); ICmpInst *comparison = dyn_cast<ICmpInst>(condition); if (comparison) { // Create sigmas for ICmp operands for (User::const_op_iterator opit = comparison->op_begin(), opend = comparison->op_end(); opit != opend; ++opit) { Value *operand = *opit; if (isa<Instruction>(operand) || isa<Argument>(operand)) { insertSigmas(ti, operand); // If the operand is a result of a indirect instruction (e.g. ZExt, SExt, Trunc), // Create sigmas for the operands of the operands too CastInst *cinst = NULL; if ((cinst = dyn_cast<CastInst>(operand))) { insertSigmas(ti, cinst->getOperand(0)); } } } } } } // CASE 2: Switch Instruction else if ((si = dyn_cast<SwitchInst>(ti))) { Value *condition = si->getCondition(); if (isa<Instruction>(condition) || isa<Argument>(condition)) { insertSigmas(ti, condition); // If the operand is a result of a indirect instruction (e.g. ZExt, SExt, Trunc), // Create sigmas for the operands of the operands too CastInst *cinst = NULL; if ((cinst = dyn_cast<CastInst>(condition))) { insertSigmas(ti, cinst->getOperand(0)); } } } }
bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) { BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); if (!BI || !BI->isConditional()) return false; Value *Cond = BI->getCondition(); ICmpInst *CI = dyn_cast<ICmpInst>(Cond); if (!CI) return false; Value *RHS = CI->getOperand(1); ConstantInt *CV = dyn_cast<ConstantInt>(RHS); if (!CV) return false; // If the LHS is the result of AND'ing a value with a single bit bitmask, // we don't have information about probabilities. if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0))) if (LHS->getOpcode() == Instruction::And) if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) if (AndRHS->getUniqueInteger().isPowerOf2()) return false; bool isProb; if (CV->isZero()) { switch (CI->getPredicate()) { case CmpInst::ICMP_EQ: // X == 0 -> Unlikely isProb = false; break; case CmpInst::ICMP_NE: // X != 0 -> Likely isProb = true; break; case CmpInst::ICMP_SLT: // X < 0 -> Unlikely isProb = false; break; case CmpInst::ICMP_SGT: // X > 0 -> Likely isProb = true; break; default: return false; } } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { // InstCombine canonicalizes X <= 0 into X < 1. // X <= 0 -> Unlikely isProb = false; } else if (CV->isAllOnesValue()) { switch (CI->getPredicate()) { case CmpInst::ICMP_EQ: // X == -1 -> Unlikely isProb = false; break; case CmpInst::ICMP_NE: // X != -1 -> Likely isProb = true; break; case CmpInst::ICMP_SGT: // InstCombine canonicalizes X >= 0 into X > -1. // X >= 0 -> Likely isProb = true; break; default: return false; } } else { return false; } unsigned TakenIdx = 0, NonTakenIdx = 1; if (!isProb) std::swap(TakenIdx, NonTakenIdx); BranchProbability TakenProb(ZH_TAKEN_WEIGHT, ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); setEdgeProbability(BB, TakenIdx, TakenProb); setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl()); return true; }
/// HandleFloatingPointIV - If the loop has floating induction variable /// then insert corresponding integer induction variable if possible. /// For example, /// for(double i = 0; i < 10000; ++i) /// bar(i) /// is converted into /// for(int i = 0; i < 10000; ++i) /// bar((double)i); /// void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PN) { unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); unsigned BackEdge = IncomingEdge^1; // Check incoming value. ConstantFP *InitValueVal = dyn_cast<ConstantFP>(PN->getIncomingValue(IncomingEdge)); int64_t InitValue; if (!InitValueVal || !ConvertToSInt(InitValueVal->getValueAPF(), InitValue)) return; // Check IV increment. Reject this PN if increment operation is not // an add or increment value can not be represented by an integer. BinaryOperator *Incr = dyn_cast<BinaryOperator>(PN->getIncomingValue(BackEdge)); if (Incr == 0 || Incr->getOpcode() != Instruction::FAdd) return; // If this is not an add of the PHI with a constantfp, or if the constant fp // is not an integer, bail out. ConstantFP *IncValueVal = dyn_cast<ConstantFP>(Incr->getOperand(1)); int64_t IncValue; if (IncValueVal == 0 || Incr->getOperand(0) != PN || !ConvertToSInt(IncValueVal->getValueAPF(), IncValue)) return; // Check Incr uses. One user is PN and the other user is an exit condition // used by the conditional terminator. Value::use_iterator IncrUse = Incr->use_begin(); Instruction *U1 = cast<Instruction>(IncrUse++); if (IncrUse == Incr->use_end()) return; Instruction *U2 = cast<Instruction>(IncrUse++); if (IncrUse != Incr->use_end()) return; // Find exit condition, which is an fcmp. If it doesn't exist, or if it isn't // only used by a branch, we can't transform it. FCmpInst *Compare = dyn_cast<FCmpInst>(U1); if (!Compare) Compare = dyn_cast<FCmpInst>(U2); if (Compare == 0 || !Compare->hasOneUse() || !isa<BranchInst>(Compare->use_back())) return; BranchInst *TheBr = cast<BranchInst>(Compare->use_back()); // We need to verify that the branch actually controls the iteration count // of the loop. If not, the new IV can overflow and no one will notice. // The branch block must be in the loop and one of the successors must be out // of the loop. assert(TheBr->isConditional() && "Can't use fcmp if not conditional"); if (!L->contains(TheBr->getParent()) || (L->contains(TheBr->getSuccessor(0)) && L->contains(TheBr->getSuccessor(1)))) return; // If it isn't a comparison with an integer-as-fp (the exit value), we can't // transform it. ConstantFP *ExitValueVal = dyn_cast<ConstantFP>(Compare->getOperand(1)); int64_t ExitValue; if (ExitValueVal == 0 || !ConvertToSInt(ExitValueVal->getValueAPF(), ExitValue)) return; // Find new predicate for integer comparison. CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; switch (Compare->getPredicate()) { default: return; // Unknown comparison. case CmpInst::FCMP_OEQ: case CmpInst::FCMP_UEQ: NewPred = CmpInst::ICMP_EQ; break; case CmpInst::FCMP_ONE: case CmpInst::FCMP_UNE: NewPred = CmpInst::ICMP_NE; break; case CmpInst::FCMP_OGT: case CmpInst::FCMP_UGT: NewPred = CmpInst::ICMP_SGT; break; case CmpInst::FCMP_OGE: case CmpInst::FCMP_UGE: NewPred = CmpInst::ICMP_SGE; break; case CmpInst::FCMP_OLT: case CmpInst::FCMP_ULT: NewPred = CmpInst::ICMP_SLT; break; case CmpInst::FCMP_OLE: case CmpInst::FCMP_ULE: NewPred = CmpInst::ICMP_SLE; break; } // We convert the floating point induction variable to a signed i32 value if // we can. This is only safe if the comparison will not overflow in a way // that won't be trapped by the integer equivalent operations. Check for this // now. // TODO: We could use i64 if it is native and the range requires it. // The start/stride/exit values must all fit in signed i32. if (!isInt<32>(InitValue) || !isInt<32>(IncValue) || !isInt<32>(ExitValue)) return; // If not actually striding (add x, 0.0), avoid touching the code. if (IncValue == 0) return; // Positive and negative strides have different safety conditions. if (IncValue > 0) { // If we have a positive stride, we require the init to be less than the // exit value and an equality or less than comparison. if (InitValue >= ExitValue || NewPred == CmpInst::ICMP_SGT || NewPred == CmpInst::ICMP_SGE) return; uint32_t Range = uint32_t(ExitValue-InitValue); if (NewPred == CmpInst::ICMP_SLE) { // Normalize SLE -> SLT, check for infinite loop. if (++Range == 0) return; // Range overflows. } unsigned Leftover = Range % uint32_t(IncValue); // If this is an equality comparison, we require that the strided value // exactly land on the exit value, otherwise the IV condition will wrap // around and do things the fp IV wouldn't. if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) && Leftover != 0) return; // If the stride would wrap around the i32 before exiting, we can't // transform the IV. if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue) return; } else { // If we have a negative stride, we require the init to be greater than the // exit value and an equality or greater than comparison. if (InitValue >= ExitValue || NewPred == CmpInst::ICMP_SLT || NewPred == CmpInst::ICMP_SLE) return; uint32_t Range = uint32_t(InitValue-ExitValue); if (NewPred == CmpInst::ICMP_SGE) { // Normalize SGE -> SGT, check for infinite loop. if (++Range == 0) return; // Range overflows. } unsigned Leftover = Range % uint32_t(-IncValue); // If this is an equality comparison, we require that the strided value // exactly land on the exit value, otherwise the IV condition will wrap // around and do things the fp IV wouldn't. if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) && Leftover != 0) return; // If the stride would wrap around the i32 before exiting, we can't // transform the IV. if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue) return; } const IntegerType *Int32Ty = Type::getInt32Ty(PN->getContext()); // Insert new integer induction variable. PHINode *NewPHI = PHINode::Create(Int32Ty, PN->getName()+".int", PN); NewPHI->addIncoming(ConstantInt::get(Int32Ty, InitValue), PN->getIncomingBlock(IncomingEdge)); Value *NewAdd = BinaryOperator::CreateAdd(NewPHI, ConstantInt::get(Int32Ty, IncValue), Incr->getName()+".int", Incr); NewPHI->addIncoming(NewAdd, PN->getIncomingBlock(BackEdge)); ICmpInst *NewCompare = new ICmpInst(TheBr, NewPred, NewAdd, ConstantInt::get(Int32Ty, ExitValue), Compare->getName()); // In the following deletions, PN may become dead and may be deleted. // Use a WeakVH to observe whether this happens. WeakVH WeakPH = PN; // Delete the old floating point exit comparison. The branch starts using the // new comparison. NewCompare->takeName(Compare); Compare->replaceAllUsesWith(NewCompare); RecursivelyDeleteTriviallyDeadInstructions(Compare); // Delete the old floating point increment. Incr->replaceAllUsesWith(UndefValue::get(Incr->getType())); RecursivelyDeleteTriviallyDeadInstructions(Incr); // If the FP induction variable still has uses, this is because something else // in the loop uses its value. In order to canonicalize the induction // variable, we chose to eliminate the IV and rewrite it in terms of an // int->fp cast. // // We give preference to sitofp over uitofp because it is faster on most // platforms. if (WeakPH) { Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv", PN->getParent()->getFirstNonPHI()); PN->replaceAllUsesWith(Conv); RecursivelyDeleteTriviallyDeadInstructions(PN); } // Add a new IVUsers entry for the newly-created integer PHI. IU->AddUsersIfInteresting(NewPHI); }
bool RangedAddressSanitizer::runOnFunction(Function &F) { if (getenv("FASAN_DISABLE")) { SPM_DEBUG( dbgs() << "FASan : disabled\n" ); return false; } DL_ = &getAnalysis<DataLayout>(); DT_ = &getAnalysis<DominatorTree>(); LI_ = &getAnalysis<LoopInfo>(); RI_ = &getAnalysis<ReduceIndexation>(); #ifdef ENABLE_REUSE RE_ = &getAnalysis<RelativeExecutions>(); #endif RMM_ = &getAnalysis<RelativeMinMax>(); Module_ = F.getParent(); Context_ = &Module_->getContext(); Type *VoidTy = Type::getVoidTy(*Context_); IntegerType *IntTy = IntegerType::getInt64Ty(*Context_); IntegerType *BoolTy = IntegerType::getInt1Ty(*Context_); PointerType *IntPtrTy = PointerType::getUnqual(IntTy); PointerType *VoidPtrTy = PointerType::getInt8PtrTy(*Context_); SPM_DEBUG( F.dump() ); outs() << "[IterationInfo]\n"; for (Loop * loop : *LI_) { ii_visitLoop(loop); } outs() << "[EndOfIterationInfo]\n"; #if 0 // disabled initialization,shutdown sequence for FASan if (F.getName() == "main") { SPM_DEBUG(dbgs() << "RangedAddressSanitizer: inserting hwloc calls into " "main function\n"); FunctionType *FnType = FunctionType::get(VoidTy, ArrayRef<Type*>(), false); IRBuilder<> IRB(&(*F.getEntryBlock().begin())); Constant *Init = Module_->getOrInsertFunction("__spm_init", FnType); IRB.CreateCall(Init); Constant *End = Module_->getOrInsertFunction("__spm_end", FnType); for (auto &BB : F) { TerminatorInst *TI = BB.getTerminator(); if (isa<ReturnInst>(TI)) { IRB.SetInsertPoint(TI); IRB.CreateCall(End); } } } #endif if (!ClFunc.empty() && F.getName() != ClFunc) { SPM_DEBUG(dbgs() << "RangedAddressSanitizer: skipping function " << F.getName() << "\n"); return false; } Calls_.clear(); SPM_DEBUG(dbgs() << "RangedAddressSanitizer: processing function " << F.getName() << "\n"); std::vector<Type*> ReuseFnFormals = { VoidPtrTy, IntTy, IntTy, IntTy }; FunctionType *ReuseFnType = FunctionType::get(BoolTy, ReuseFnFormals, false); ReuseFn_ = F.getParent()->getOrInsertFunction("__fasan_check", ReuseFnType); ReuseFnDestroy_ = F.getParent()->getOrInsertFunction("__spm_give", ReuseFnType); // Visit all loops in bottom-up order (innter-most loops first) std::set<BasicBlock*> Processed; auto Entry = DT_->getRootNode(); for (auto ET = po_begin(Entry), EE = po_end(Entry); ET != EE; ++ET) { BasicBlock *Header = (*ET)->getBlock(); if (LI_->isLoopHeader(Header)) { SPM_DEBUG(dbgs() << "RangedAddressSanitizer: processing loop at " << Header->getName() << "\n"); Loop *L = LI_->getLoopFor(Header); if (L->getNumBackEdges() != 1 || std::distance(pred_begin(Header), pred_end(Header)) != 2) { SPM_DEBUG(dbgs() << "RangedAddressSanitizer: loop has multiple " << "backedges or multiple incoming outer blocks\n"); continue; } SPM_DEBUG(dbgs() << "RangedAddressSanitizer: processing loop at " << Header->getName() << "\n"); // visit all memory acccesses in this loop for (auto BB = L->block_begin(), BE = L->block_end(); BB != BE; ++BB) { if (!Processed.count(*BB)) { Processed.insert(*BB); for (auto &I : *(*BB)) generateCallFor(L, &I); } } } } // FAsan logic goes here std::map<const BasicBlock*,BasicBlock*> clonedBlockMap; // keeps track of cloned regions to avoid redundant cloning std::vector<CallInst*> ToInline; for (auto &CI : Calls_) { BasicBlock * Preheader = CI.Preheader; // TODO decide whether it is worthwhile to optimize for this case // insert range check IRBuilder<> IRB(Preheader->getTerminator()); Value *VoidArray = IRB.CreateBitCast(CI.Array, VoidPtrTy); std::vector<Value*> Args = { VoidArray, CI.Min, CI.Max, CI.Reuse }; CallInst *CR = IRB.CreateCall(ReuseFn_, Args); ToInline.push_back(CR); // verify if this loop was already instrumented TerminatorInst * preHeaderTerm = CR->getParent()->getTerminator(); BranchInst * preHeaderBranch = dyn_cast<BranchInst>(preHeaderTerm); if (preHeaderBranch && preHeaderBranch->isConditional()) { // discover the structure of the instrumented code (safe and default region) // abort, if this does not look like instrumented code BasicBlock * firstTarget = preHeaderBranch->getSuccessor(0); BasicBlock * secondTarget = preHeaderBranch->getSuccessor(1); BasicBlock * safeHeader, * defHeader; if (clonedBlockMap.count(firstTarget)) { defHeader = firstTarget; safeHeader = clonedBlockMap[firstTarget]; assert(safeHeader == secondTarget); } else { assert(clonedBlockMap.count(secondTarget)); defHeader = secondTarget; safeHeader = clonedBlockMap[secondTarget]; assert(safeHeader == firstTarget); } SPM_DEBUG( dbgs() << "FASan: (Unsupported) second array in safe region controlled by " << * preHeaderBranch << "\n" ); Loop * defLoop = LI_->getLoopFor(defHeader); assert(defLoop && "default region is not a loop!"); Loop::block_iterator itBodyBlock,S,E; S = defLoop->block_begin(); E = defLoop->block_end(); // mark accesses in cloned region as safe for (itBodyBlock = S;itBodyBlock != E; ++itBodyBlock) { BasicBlock * defBodyBlock = *itBodyBlock; BasicBlock * safeBodyBlock = clonedBlockMap[defBodyBlock]; for(auto & inst : *safeBodyBlock) { markSafeArrayUse(&inst, CI.Array); } } // add conjunctive test Value * oldCond = preHeaderBranch->getCondition(); Value * joinedCond = IRB.CreateAnd(oldCond, CR, "allsafe"); preHeaderBranch->setCondition(joinedCond); } else { // get loop Loop* finalLoop = CI.FinalLoop; Loop::block_iterator itBodyBlock,S,E; S = finalLoop->block_begin(); E = finalLoop->block_end(); // clone loop body (cloned loop will run unchecked) ValueToValueMapTy cloneMap; BasicBlock * clonedHeader = 0; std::vector<BasicBlock*> clonedBlocks; for (itBodyBlock = S;itBodyBlock != E; ++itBodyBlock) { const BasicBlock * bodyBlock = *itBodyBlock; BasicBlock * clonedBlock = CloneBasicBlock(bodyBlock, cloneMap, "_checked", &F, 0); cloneMap[bodyBlock] = clonedBlock; clonedBlockMap[bodyBlock] = clonedBlock; clonedBlocks.push_back(clonedBlock); if (bodyBlock == finalLoop->getHeader()) { clonedHeader = clonedBlock; SPM_DEBUG( dbgs() << "FASan: loop header case at " << bodyBlock->getName() << "\n" ); } else { SPM_DEBUG( dbgs() << "FASan: non-header block at " << bodyBlock->getName() << "\n" ); } } if (!clonedHeader) { // TODO run clean-up code SPM_DEBUG( dbgs() << "FASan: could not find header!\n"); abort(); } // Remap uses inside cloned region (mark pointers in the region as unguarded) for (BasicBlock * block : clonedBlocks) { for(auto & inst : *block) { RemapInstruction(&inst, cloneMap, RF_IgnoreMissingEntries); markSafeArrayUse(&inst, CI.Array); } } // TODO fix PHI-nodes in exit blocks // Rewire terminator of the range check to branch to the cloned region TerminatorInst * checkTermInst = CR->getParent()->getTerminator(); if (BranchInst * checkBranchInst = dyn_cast<BranchInst>(checkTermInst)) { if (checkBranchInst->isUnconditional()) { BasicBlock * defTarget = checkBranchInst->getSuccessor(0); BranchInst * modifiedBranchInst = BranchInst::Create(clonedHeader, defTarget, CR, checkBranchInst); checkBranchInst->replaceAllUsesWith(modifiedBranchInst); checkBranchInst->eraseFromParent(); } else { SPM_DEBUG( dbgs() << "FASan: Unexpected conditional branch (preheader should branch unconditional, other array checks will introduce conditional branches) " << * checkTermInst << "\n" ); abort(); } } else { SPM_DEBUG( dbgs() << "FASan: unsupported terminator type " << * checkTermInst << "\n" ); abort(); } } #if 0 IRB.SetInsertPoint(&(*CI.Final->begin())); IRB.CreateCall(ReuseFnDestroy_, Args); #endif SPM_DEBUG(dbgs() << "RangedAddressSanitizer: call instruction: " << *CR << "\n"); } // inline calls #ifdef FASAN_INLINE_RUNTIME for (CallInst * call : ToInline) { assert(call); InlineFunctionInfo IFI; InlineFunction(call, IFI, false); } #endif SPM_DEBUG( F.dump() ); return true; }
void CPFlowFunction::visitBranchInst(BranchInst &BI) { CPLatticePoint* result = new CPLatticePoint(*(info_in_casted.back())); info_in_casted.pop_back(); BranchInst* current = &BI; if (BI.isConditional()) { Value* cond = BI.getCondition(); if (isa<ICmpInst>(cond)) { std::pair<Use*, Use *> branches = helper::getOps(BI); Use* true_branch = branches.first; Use* false_branch = branches.second; ICmpInst* cmp = dyn_cast<ICmpInst>(cond); std::pair<Use*, Use *> operands = helper::getOps(*cmp); Use* rhs = operands.second; Use* lhs = operands.first; ConstantInt* rhs_const = NULL; ConstantInt* lhs_const = NULL; // get the rhs/lhs as a constant int if (isa<ConstantInt>(rhs)) { rhs_const = dyn_cast<ConstantInt>(rhs); } else if (result->representation.count(rhs->get()) > 0) { rhs_const = result->representation[rhs->get()]; } else { rhs_const = ConstantInt::get(context, llvm::APInt(32, 0, true)); } if (isa<ConstantInt>(lhs)) { lhs_const = dyn_cast<ConstantInt>(lhs->get()); } else if (result->representation.count(lhs->get()) > 0) { lhs_const = result->representation[lhs->get()]; } else { lhs_const = ConstantInt::get(context, llvm::APInt(32, 0, true)); } // Create successors CPLatticePoint* true_branchCLP = new CPLatticePoint(false, false, std::map<Value*,ConstantInt*>(result->representation)); CPLatticePoint* false_branchCLP = new CPLatticePoint(false, false, std::map<Value*,ConstantInt*>(result->representation)); // get the predicate int predicate = 0; predicate = cmp->isSigned() ? cmp->getSignedPredicate() : cmp->getUnsignedPredicate(); if (predicate == CmpInst::ICMP_EQ) { if (isa<ConstantInt>(lhs)) { true_branchCLP->representation[rhs->get()] = lhs_const; } else if (isa<ConstantInt>(rhs)) { true_branchCLP->representation[lhs->get()] = rhs_const; } out_map[true_branch->get()] = true_branchCLP; out_map[false_branch->get()] = false_branchCLP; } else if (predicate == CmpInst::ICMP_NE) { if (isa<ConstantInt>(lhs)) { false_branchCLP->representation[rhs->get()] = lhs_const; } else if (isa<ConstantInt>(rhs)) { false_branchCLP->representation[lhs->get()] = rhs_const; } out_map[true_branch->get()] = true_branchCLP; out_map[false_branch->get()] = false_branchCLP; } else { for (std::map<Value *, LatticePoint *>::iterator it=out_map.begin(); it != out_map.end(); ++it){ Value* elm = it->first; out_map[elm] = new CPLatticePoint(*result); } } } else { for (std::map<Value *, LatticePoint *>::iterator it=out_map.begin(); it != out_map.end(); ++it){ Value* elm = it->first; out_map[elm] = new CPLatticePoint(*result); } } } else { for (std::map<Value *, LatticePoint *>::iterator it=out_map.begin(); it != out_map.end(); ++it){ Value* elm = it->first; out_map[elm] = new CPLatticePoint(*result); } } }
/// This works like CloneAndPruneFunctionInto, except that it does not clone the /// entire function. Instead it starts at an instruction provided by the caller /// and copies (and prunes) only the code reachable from that instruction. void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, const Instruction *StartingInst, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl<ReturnInst *> &Returns, const char *NameSuffix, ClonedCodeInfo *CodeInfo) { assert(NameSuffix && "NameSuffix cannot be null!"); ValueMapTypeRemapper *TypeMapper = nullptr; ValueMaterializer *Materializer = nullptr; #ifndef NDEBUG // If the cloning starts at the beginning of the function, verify that // the function arguments are mapped. if (!StartingInst) for (const Argument &II : OldFunc->args()) assert(VMap.count(&II) && "No mapping from source argument specified!"); #endif PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, NameSuffix, CodeInfo); const BasicBlock *StartingBB; if (StartingInst) StartingBB = StartingInst->getParent(); else { StartingBB = &OldFunc->getEntryBlock(); StartingInst = &StartingBB->front(); } // Clone the entry block, and anything recursively reachable from it. std::vector<const BasicBlock*> CloneWorklist; PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist); while (!CloneWorklist.empty()) { const BasicBlock *BB = CloneWorklist.back(); CloneWorklist.pop_back(); PFC.CloneBlock(BB, BB->begin(), CloneWorklist); } // Loop over all of the basic blocks in the old function. If the block was // reachable, we have cloned it and the old block is now in the value map: // insert it into the new function in the right order. If not, ignore it. // // Defer PHI resolution until rest of function is resolved. SmallVector<const PHINode*, 16> PHIToResolve; for (const BasicBlock &BI : *OldFunc) { Value *V = VMap[&BI]; BasicBlock *NewBB = cast_or_null<BasicBlock>(V); if (!NewBB) continue; // Dead block. // Add the new block to the new function. NewFunc->getBasicBlockList().push_back(NewBB); // Handle PHI nodes specially, as we have to remove references to dead // blocks. for (BasicBlock::const_iterator I = BI.begin(), E = BI.end(); I != E; ++I) { // PHI nodes may have been remapped to non-PHI nodes by the caller or // during the cloning process. if (const PHINode *PN = dyn_cast<PHINode>(I)) { if (isa<PHINode>(VMap[PN])) PHIToResolve.push_back(PN); else break; } else { break; } } // Finally, remap the terminator instructions, as those can't be remapped // until all BBs are mapped. RemapInstruction(NewBB->getTerminator(), VMap, ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, TypeMapper, Materializer); } // Defer PHI resolution until rest of function is resolved, PHI resolution // requires the CFG to be up-to-date. for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { const PHINode *OPN = PHIToResolve[phino]; unsigned NumPreds = OPN->getNumIncomingValues(); const BasicBlock *OldBB = OPN->getParent(); BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]); // Map operands for blocks that are live and remove operands for blocks // that are dead. for (; phino != PHIToResolve.size() && PHIToResolve[phino]->getParent() == OldBB; ++phino) { OPN = PHIToResolve[phino]; PHINode *PN = cast<PHINode>(VMap[OPN]); for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { Value *V = VMap[PN->getIncomingBlock(pred)]; if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) { Value *InVal = MapValue(PN->getIncomingValue(pred), VMap, ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); assert(InVal && "Unknown input value?"); PN->setIncomingValue(pred, InVal); PN->setIncomingBlock(pred, MappedBlock); } else { PN->removeIncomingValue(pred, false); --pred, --e; // Revisit the next entry. } } } // The loop above has removed PHI entries for those blocks that are dead // and has updated others. However, if a block is live (i.e. copied over) // but its terminator has been changed to not go to this block, then our // phi nodes will have invalid entries. Update the PHI nodes in this // case. PHINode *PN = cast<PHINode>(NewBB->begin()); NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB)); if (NumPreds != PN->getNumIncomingValues()) { assert(NumPreds < PN->getNumIncomingValues()); // Count how many times each predecessor comes to this block. std::map<BasicBlock*, unsigned> PredCount; for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); PI != E; ++PI) --PredCount[*PI]; // Figure out how many entries to remove from each PHI. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) ++PredCount[PN->getIncomingBlock(i)]; // At this point, the excess predecessor entries are positive in the // map. Loop over all of the PHIs and remove excess predecessor // entries. BasicBlock::iterator I = NewBB->begin(); for (; (PN = dyn_cast<PHINode>(I)); ++I) { for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(), E = PredCount.end(); PCI != E; ++PCI) { BasicBlock *Pred = PCI->first; for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove) PN->removeIncomingValue(Pred, false); } } } // If the loops above have made these phi nodes have 0 or 1 operand, // replace them with undef or the input value. We must do this for // correctness, because 0-operand phis are not valid. PN = cast<PHINode>(NewBB->begin()); if (PN->getNumIncomingValues() == 0) { BasicBlock::iterator I = NewBB->begin(); BasicBlock::const_iterator OldI = OldBB->begin(); while ((PN = dyn_cast<PHINode>(I++))) { Value *NV = UndefValue::get(PN->getType()); PN->replaceAllUsesWith(NV); assert(VMap[&*OldI] == PN && "VMap mismatch"); VMap[&*OldI] = NV; PN->eraseFromParent(); ++OldI; } } } // Make a second pass over the PHINodes now that all of them have been // remapped into the new function, simplifying the PHINode and performing any // recursive simplifications exposed. This will transparently update the // WeakVH in the VMap. Notably, we rely on that so that if we coalesce // two PHINodes, the iteration over the old PHIs remains valid, and the // mapping will just map us to the new node (which may not even be a PHI // node). for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx) if (PHINode *PN = dyn_cast<PHINode>(VMap[PHIToResolve[Idx]])) recursivelySimplifyInstruction(PN); // Now that the inlined function body has been fully constructed, go through // and zap unconditional fall-through branches. This happens all the time when // specializing code: code specialization turns conditional branches into // uncond branches, and this code folds them. Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator(); Function::iterator I = Begin; while (I != NewFunc->end()) { // Check if this block has become dead during inlining or other // simplifications. Note that the first block will appear dead, as it has // not yet been wired up properly. if (I != Begin && (pred_begin(&*I) == pred_end(&*I) || I->getSinglePredecessor() == &*I)) { BasicBlock *DeadBB = &*I++; DeleteDeadBlock(DeadBB); continue; } // We need to simplify conditional branches and switches with a constant // operand. We try to prune these out when cloning, but if the // simplification required looking through PHI nodes, those are only // available after forming the full basic block. That may leave some here, // and we still want to prune the dead code as early as possible. ConstantFoldTerminator(&*I); BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); if (!BI || BI->isConditional()) { ++I; continue; } BasicBlock *Dest = BI->getSuccessor(0); if (!Dest->getSinglePredecessor()) { ++I; continue; } // We shouldn't be able to get single-entry PHI nodes here, as instsimplify // above should have zapped all of them.. assert(!isa<PHINode>(Dest->begin())); // We know all single-entry PHI nodes in the inlined function have been // removed, so we just need to splice the blocks. BI->eraseFromParent(); // Make all PHI nodes that referred to Dest now refer to I as their source. Dest->replaceAllUsesWith(&*I); // Move all the instructions in the succ to the pred. I->getInstList().splice(I->end(), Dest->getInstList()); // Remove the dest block. Dest->eraseFromParent(); // Do not increment I, iteratively merge all things this block branches to. } // Make a final pass over the basic blocks from the old function to gather // any return instructions which survived folding. We have to do this here // because we can iteratively remove and merge returns above. for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(), E = NewFunc->end(); I != E; ++I) if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) Returns.push_back(RI); }
void DSWP::buildPDG(Loop *L) { //Initialize PDG for (Loop::block_iterator bi = L->getBlocks().begin(); bi != L->getBlocks().end(); bi++) { BasicBlock *BB = *bi; for (BasicBlock::iterator ui = BB->begin(); ui != BB->end(); ui++) { Instruction *inst = &(*ui); //standardlize the name for all expr if (util.hasNewDef(inst)) { inst->setName(util.genId()); dname[inst] = inst->getNameStr(); } else { dname[inst] = util.genId(); } pdg[inst] = new vector<Edge>(); rev[inst] = new vector<Edge>(); } } //LoopInfo &li = getAnalysis<LoopInfo>(); /* * Memory dependency analysis */ MemoryDependenceAnalysis &mda = getAnalysis<MemoryDependenceAnalysis>(); for (Loop::block_iterator bi = L->getBlocks().begin(); bi != L->getBlocks().end(); bi++) { BasicBlock *BB = *bi; for (BasicBlock::iterator ii = BB->begin(); ii != BB->end(); ii++) { Instruction *inst = &(*ii); //data dependence = register dependence + memory dependence //begin register dependence for (Value::use_iterator ui = ii->use_begin(); ui != ii->use_end(); ui++) { if (Instruction *user = dyn_cast<Instruction>(*ui)) { addEdge(inst, user, REG); } } //finish register dependence //begin memory dependence MemDepResult mdr = mda.getDependency(inst); //TODO not sure clobbers mean!! if (mdr.isDef()) { Instruction *dep = mdr.getInst(); if (isa<LoadInst>(inst)) { if (isa<StoreInst>(dep)) { addEdge(dep, inst, DTRUE); //READ AFTER WRITE } } if (isa<StoreInst>(inst)) { if (isa<LoadInst>(dep)) { addEdge(dep, inst, DANTI); //WRITE AFTER READ } if (isa<StoreInst>(dep)) { addEdge(dep, inst, DOUT); //WRITE AFTER WRITE } } //READ AFTER READ IS INSERT AFTER PDG BUILD } //end memory dependence }//for ii }//for bi /* * begin control dependence */ PostDominatorTree &pdt = getAnalysis<PostDominatorTree>(); //cout << pdt.getRootNode()->getBlock()->getNameStr() << endl; /* * alien code part 1 */ LoopInfo *LI = &getAnalysis<LoopInfo>(); std::set<BranchInst*> backedgeParents; for (Loop::block_iterator bi = L->getBlocks().begin(); bi != L->getBlocks().end(); bi++) { BasicBlock *BB = *bi; for (BasicBlock::iterator ii = BB->begin(); ii != BB->end(); ii++) { Instruction *inst = ii; if (BranchInst *brInst = dyn_cast<BranchInst>(inst)) { // get the loop this instruction (and therefore basic block) belongs to Loop *instLoop = LI->getLoopFor(BB); bool branchesToHeader = false; for (int i = brInst->getNumSuccessors() - 1; i >= 0 && !branchesToHeader; i--) { // if the branch could exit, store it if (LI->getLoopFor(brInst->getSuccessor(i)) != instLoop) { branchesToHeader = true; } } if (branchesToHeader) { backedgeParents.insert(brInst); } } } } //build information for predecessor of blocks in post dominator tree for (Function::iterator bi = func->begin(); bi != func->end(); bi++) { BasicBlock *BB = bi; DomTreeNode *dn = pdt.getNode(BB); for (DomTreeNode::iterator di = dn->begin(); di != dn->end(); di++) { BasicBlock *CB = (*di)->getBlock(); pre[CB] = BB; } } // // //add dependency within a basicblock // for (Loop::block_iterator bi = L->getBlocks().begin(); bi != L->getBlocks().end(); bi++) { // BasicBlock *BB = *bi; // Instruction *pre = NULL; // for (BasicBlock::iterator ui = BB->begin(); ui != BB->end(); ui++) { // Instruction *inst = &(*ui); // if (pre != NULL) { // addEdge(pre, inst, CONTROL); // } // pre = inst; // } // } // //the special kind of dependence need loop peeling ? I don't know whether this is needed // for (Loop::block_iterator bi = L->getBlocks().begin(); bi != L->getBlocks().end(); bi++) { // BasicBlock *BB = *bi; // for (succ_iterator PI = succ_begin(BB); PI != succ_end(BB); ++PI) { // BasicBlock *succ = *PI; // // checkControlDependence(BB, succ, pdt); // } // } /* * alien code part 2 */ // add normal control dependencies // loop through each instruction for (Loop::block_iterator bbIter = L->block_begin(); bbIter != L->block_end(); ++bbIter) { BasicBlock *bb = *bbIter; // check the successors of this basic block if (BranchInst *branchInst = dyn_cast<BranchInst>(bb->getTerminator())) { if (branchInst->getNumSuccessors() > 1) { BasicBlock * succ = branchInst->getSuccessor(0); // if the successor is nested shallower than the current basic block, continue if (LI->getLoopDepth(bb) < LI->getLoopDepth(succ)) { continue; } // otherwise, add all instructions to graph as control dependence while (succ != NULL && succ != bb && LI->getLoopDepth(succ) >= LI->getLoopDepth(bb)) { Instruction *terminator = bb->getTerminator(); for (BasicBlock::iterator succInstIter = succ->begin(); &(*succInstIter) != succ->getTerminator(); ++succInstIter) { addEdge(terminator, &(*succInstIter), CONTROL); } if (BranchInst *succBrInst = dyn_cast<BranchInst>(succ->getTerminator())) { if (succBrInst->getNumSuccessors() > 1) { addEdge(terminator, succ->getTerminator(), CONTROL); } } if (BranchInst *br = dyn_cast<BranchInst>(succ->getTerminator())) { if (br->getNumSuccessors() == 1) { succ = br->getSuccessor(0); } else { succ = NULL; } } else { succ = NULL; } } } } } /* * alien code part 3 */ for (std::set<BranchInst*>::iterator exitIter = backedgeParents.begin(); exitIter != backedgeParents.end(); ++exitIter) { BranchInst *exitBranch = *exitIter; if (exitBranch->isConditional()) { BasicBlock *header = LI->getLoopFor(exitBranch->getParent())->getHeader(); for (BasicBlock::iterator ctrlIter = header->begin(); ctrlIter != header->end(); ++ctrlIter) { addEdge(exitBranch, &(*ctrlIter), CONTROL); } } } //end control dependence }
/// \brief Simplify one loop and queue further loops for simplification. /// /// FIXME: Currently this accepts both lots of analyses that it uses and a raw /// Pass pointer. The Pass pointer is used by numerous utilities to update /// specific analyses. Rather than a pass it would be much cleaner and more /// explicit if they accepted the analysis directly and then updated it. static bool simplifyOneLoop(Loop *L, SmallVectorImpl<Loop *> &Worklist, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, Pass *PP, AssumptionCache *AC) { bool Changed = false; ReprocessLoop: // Check to see that no blocks (other than the header) in this loop have // predecessors that are not in the loop. This is not valid for natural // loops, but can occur if the blocks are unreachable. Since they are // unreachable we can just shamelessly delete those CFG edges! for (Loop::block_iterator BB = L->block_begin(), E = L->block_end(); BB != E; ++BB) { if (*BB == L->getHeader()) continue; SmallPtrSet<BasicBlock*, 4> BadPreds; for (pred_iterator PI = pred_begin(*BB), PE = pred_end(*BB); PI != PE; ++PI) { BasicBlock *P = *PI; if (!L->contains(P)) BadPreds.insert(P); } // Delete each unique out-of-loop (and thus dead) predecessor. for (BasicBlock *P : BadPreds) { DEBUG(dbgs() << "LoopSimplify: Deleting edge from dead predecessor " << P->getName() << "\n"); // Inform each successor of each dead pred. for (succ_iterator SI = succ_begin(P), SE = succ_end(P); SI != SE; ++SI) (*SI)->removePredecessor(P); // Zap the dead pred's terminator and replace it with unreachable. TerminatorInst *TI = P->getTerminator(); TI->replaceAllUsesWith(UndefValue::get(TI->getType())); P->getTerminator()->eraseFromParent(); new UnreachableInst(P->getContext(), P); Changed = true; } } // If there are exiting blocks with branches on undef, resolve the undef in // the direction which will exit the loop. This will help simplify loop // trip count computations. SmallVector<BasicBlock*, 8> ExitingBlocks; L->getExitingBlocks(ExitingBlocks); for (SmallVectorImpl<BasicBlock *>::iterator I = ExitingBlocks.begin(), E = ExitingBlocks.end(); I != E; ++I) if (BranchInst *BI = dyn_cast<BranchInst>((*I)->getTerminator())) if (BI->isConditional()) { if (UndefValue *Cond = dyn_cast<UndefValue>(BI->getCondition())) { DEBUG(dbgs() << "LoopSimplify: Resolving \"br i1 undef\" to exit in " << (*I)->getName() << "\n"); BI->setCondition(ConstantInt::get(Cond->getType(), !L->contains(BI->getSuccessor(0)))); // This may make the loop analyzable, force SCEV recomputation. if (SE) SE->forgetLoop(L); Changed = true; } } // Does the loop already have a preheader? If so, don't insert one. BasicBlock *Preheader = L->getLoopPreheader(); if (!Preheader) { Preheader = InsertPreheaderForLoop(L, PP); if (Preheader) { ++NumInserted; Changed = true; } } // Next, check to make sure that all exit nodes of the loop only have // predecessors that are inside of the loop. This check guarantees that the // loop preheader/header will dominate the exit blocks. If the exit block has // predecessors from outside of the loop, split the edge now. SmallVector<BasicBlock*, 8> ExitBlocks; L->getExitBlocks(ExitBlocks); SmallSetVector<BasicBlock *, 8> ExitBlockSet(ExitBlocks.begin(), ExitBlocks.end()); for (SmallSetVector<BasicBlock *, 8>::iterator I = ExitBlockSet.begin(), E = ExitBlockSet.end(); I != E; ++I) { BasicBlock *ExitBlock = *I; for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock); PI != PE; ++PI) // Must be exactly this loop: no subloops, parent loops, or non-loop preds // allowed. if (!L->contains(*PI)) { if (rewriteLoopExitBlock(L, ExitBlock, DT, LI, PP)) { ++NumInserted; Changed = true; } break; } } // If the header has more than two predecessors at this point (from the // preheader and from multiple backedges), we must adjust the loop. BasicBlock *LoopLatch = L->getLoopLatch(); if (!LoopLatch) { // If this is really a nested loop, rip it out into a child loop. Don't do // this for loops with a giant number of backedges, just factor them into a // common backedge instead. if (L->getNumBackEdges() < 8) { if (Loop *OuterL = separateNestedLoop(L, Preheader, DT, LI, SE, PP, AC)) { ++NumNested; // Enqueue the outer loop as it should be processed next in our // depth-first nest walk. Worklist.push_back(OuterL); // This is a big restructuring change, reprocess the whole loop. Changed = true; // GCC doesn't tail recursion eliminate this. // FIXME: It isn't clear we can't rely on LLVM to TRE this. goto ReprocessLoop; } } // If we either couldn't, or didn't want to, identify nesting of the loops, // insert a new block that all backedges target, then make it jump to the // loop header. LoopLatch = insertUniqueBackedgeBlock(L, Preheader, DT, LI); if (LoopLatch) { ++NumInserted; Changed = true; } } const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); // Scan over the PHI nodes in the loop header. Since they now have only two // incoming values (the loop is canonicalized), we may have simplified the PHI // down to 'X = phi [X, Y]', which should be replaced with 'Y'. PHINode *PN; for (BasicBlock::iterator I = L->getHeader()->begin(); (PN = dyn_cast<PHINode>(I++)); ) if (Value *V = SimplifyInstruction(PN, DL, nullptr, DT, AC)) { if (SE) SE->forgetValue(PN); PN->replaceAllUsesWith(V); PN->eraseFromParent(); } // If this loop has multiple exits and the exits all go to the same // block, attempt to merge the exits. This helps several passes, such // as LoopRotation, which do not support loops with multiple exits. // SimplifyCFG also does this (and this code uses the same utility // function), however this code is loop-aware, where SimplifyCFG is // not. That gives it the advantage of being able to hoist // loop-invariant instructions out of the way to open up more // opportunities, and the disadvantage of having the responsibility // to preserve dominator information. bool UniqueExit = true; if (!ExitBlocks.empty()) for (unsigned i = 1, e = ExitBlocks.size(); i != e; ++i) if (ExitBlocks[i] != ExitBlocks[0]) { UniqueExit = false; break; } if (UniqueExit) { for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { BasicBlock *ExitingBlock = ExitingBlocks[i]; if (!ExitingBlock->getSinglePredecessor()) continue; BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); if (!BI || !BI->isConditional()) continue; CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition()); if (!CI || CI->getParent() != ExitingBlock) continue; // Attempt to hoist out all instructions except for the // comparison and the branch. bool AllInvariant = true; bool AnyInvariant = false; for (BasicBlock::iterator I = ExitingBlock->begin(); &*I != BI; ) { Instruction *Inst = I++; // Skip debug info intrinsics. if (isa<DbgInfoIntrinsic>(Inst)) continue; if (Inst == CI) continue; if (!L->makeLoopInvariant(Inst, AnyInvariant, Preheader ? Preheader->getTerminator() : nullptr)) { AllInvariant = false; break; } } if (AnyInvariant) { Changed = true; // The loop disposition of all SCEV expressions that depend on any // hoisted values have also changed. if (SE) SE->forgetLoopDispositions(L); } if (!AllInvariant) continue; // The block has now been cleared of all instructions except for // a comparison and a conditional branch. SimplifyCFG may be able // to fold it now. if (!FoldBranchToCommonDest(BI)) continue; // Success. The block is now dead, so remove it from the loop, // update the dominator tree and delete it. DEBUG(dbgs() << "LoopSimplify: Eliminating exiting block " << ExitingBlock->getName() << "\n"); // Notify ScalarEvolution before deleting this block. Currently assume the // parent loop doesn't change (spliting edges doesn't count). If blocks, // CFG edges, or other values in the parent loop change, then we need call // to forgetLoop() for the parent instead. if (SE) SE->forgetLoop(L); assert(pred_begin(ExitingBlock) == pred_end(ExitingBlock)); Changed = true; LI->removeBlock(ExitingBlock); DomTreeNode *Node = DT->getNode(ExitingBlock); const std::vector<DomTreeNodeBase<BasicBlock> *> &Children = Node->getChildren(); while (!Children.empty()) { DomTreeNode *Child = Children.front(); DT->changeImmediateDominator(Child, Node->getIDom()); } DT->eraseNode(ExitingBlock); BI->getSuccessor(0)->removePredecessor(ExitingBlock); BI->getSuccessor(1)->removePredecessor(ExitingBlock); ExitingBlock->eraseFromParent(); } } return Changed; }
void SanityCheckInstructionsPass::findInstructions(Function *F) { // A list of instructions that are used by sanity checks. They become sanity // check instructions if it turns out they're not used by anything else. SmallPtrSet<Instruction*, 128> Worklist; // A list of basic blocks that contain sanity check instructions. They // become sanity check blocks if it turns out they don't contain anything // else. SmallPtrSet<BasicBlock*, 64> BlockWorklist; // A map from instructions to the checks that use them. std::map<Instruction*, SmallPtrSet<Instruction*, 4> > ChecksByInstruction; for (BasicBlock &BB: *F) { if (findSanityCheckCall(&BB)) { SanityCheckBlocks[F].insert(&BB); // All instructions inside sanity check blocks are sanity check instructions for (Instruction &I: BB) { Worklist.insert(&I); } // All branches to sanity check blocks are sanity check branches for (User *U: BB.users()) { if (Instruction *Inst = dyn_cast<Instruction>(U)) { Worklist.insert(Inst); } BranchInst *BI = dyn_cast<BranchInst>(U); if (BI && BI->isConditional()) { SanityCheckBranches[F].insert(BI); ChecksByInstruction[BI].insert(BI); } } } } while (!Worklist.empty()) { // Alternate between emptying the worklist... while (!Worklist.empty()) { Instruction *Inst = *Worklist.begin(); Worklist.erase(Inst); if (onlyUsedInSanityChecks(Inst)) { if (SanityCheckInstructions[F].insert(Inst)) { for (Use &U: Inst->operands()) { if (Instruction *Op = dyn_cast<Instruction>(U.get())) { Worklist.insert(Op); // Copy ChecksByInstruction from Inst to Op auto CBI = ChecksByInstruction.find(Inst); if (CBI != ChecksByInstruction.end()) { ChecksByInstruction[Op].insert(CBI->second.begin(), CBI->second.end()); } } } BlockWorklist.insert(Inst->getParent()); // Fill InstructionsBySanityCheck from the inverse ChecksByInstruction auto CBI = ChecksByInstruction.find(Inst); if (CBI != ChecksByInstruction.end()) { for (Instruction *CI : CBI->second) { InstructionsBySanityCheck[CI].insert(Inst); } } } } } // ... and checking whether this causes basic blocks to contain only // sanity checks. This would in turn cause terminators to be added to // the worklist. while (!BlockWorklist.empty()) { BasicBlock *BB = *BlockWorklist.begin(); BlockWorklist.erase(BB); bool allInstructionsAreSanityChecks = true; for (Instruction &I: *BB) { if (!SanityCheckInstructions.at(BB->getParent()).count(&I)) { allInstructionsAreSanityChecks = false; break; } } if (allInstructionsAreSanityChecks) { for (User *U: BB->users()) { if (Instruction *Inst = dyn_cast<Instruction>(U)) { Worklist.insert(Inst); } } } } } }
/// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, /// except that it does some simple constant prop and DCE on the fly. The /// effect of this is to copy significantly less code in cases where (for /// example) a function call with constant arguments is inlined, and those /// constant arguments cause a significant amount of code in the callee to be /// dead. Since this doesn't produce an exact copy of the input, it can't be /// used for things like CloneFunction or CloneModule. void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl<ReturnInst*> &Returns, const char *NameSuffix, ClonedCodeInfo *CodeInfo, const TargetData *TD, Instruction *TheCall) { assert(NameSuffix && "NameSuffix cannot be null!"); #ifndef NDEBUG for (Function::const_arg_iterator II = OldFunc->arg_begin(), E = OldFunc->arg_end(); II != E; ++II) assert(VMap.count(II) && "No mapping from source argument specified!"); #endif PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, Returns, NameSuffix, CodeInfo, TD); // Clone the entry block, and anything recursively reachable from it. std::vector<const BasicBlock*> CloneWorklist; CloneWorklist.push_back(&OldFunc->getEntryBlock()); while (!CloneWorklist.empty()) { const BasicBlock *BB = CloneWorklist.back(); CloneWorklist.pop_back(); PFC.CloneBlock(BB, CloneWorklist); } // Loop over all of the basic blocks in the old function. If the block was // reachable, we have cloned it and the old block is now in the value map: // insert it into the new function in the right order. If not, ignore it. // // Defer PHI resolution until rest of function is resolved. SmallVector<const PHINode*, 16> PHIToResolve; for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); BI != BE; ++BI) { Value *V = VMap[BI]; BasicBlock *NewBB = cast_or_null<BasicBlock>(V); if (NewBB == 0) continue; // Dead block. // Add the new block to the new function. NewFunc->getBasicBlockList().push_back(NewBB); // Loop over all of the instructions in the block, fixing up operand // references as we go. This uses VMap to do all the hard work. // BasicBlock::iterator I = NewBB->begin(); DebugLoc TheCallDL; if (TheCall) TheCallDL = TheCall->getDebugLoc(); // Handle PHI nodes specially, as we have to remove references to dead // blocks. if (PHINode *PN = dyn_cast<PHINode>(I)) { // Skip over all PHI nodes, remembering them for later. BasicBlock::const_iterator OldI = BI->begin(); for (; (PN = dyn_cast<PHINode>(I)); ++I, ++OldI) PHIToResolve.push_back(cast<PHINode>(OldI)); } // Otherwise, remap the rest of the instructions normally. for (; I != NewBB->end(); ++I) RemapInstruction(I, VMap, ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); } // Defer PHI resolution until rest of function is resolved, PHI resolution // requires the CFG to be up-to-date. for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { const PHINode *OPN = PHIToResolve[phino]; unsigned NumPreds = OPN->getNumIncomingValues(); const BasicBlock *OldBB = OPN->getParent(); BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]); // Map operands for blocks that are live and remove operands for blocks // that are dead. for (; phino != PHIToResolve.size() && PHIToResolve[phino]->getParent() == OldBB; ++phino) { OPN = PHIToResolve[phino]; PHINode *PN = cast<PHINode>(VMap[OPN]); for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { Value *V = VMap[PN->getIncomingBlock(pred)]; if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) { Value *InVal = MapValue(PN->getIncomingValue(pred), VMap, ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); assert(InVal && "Unknown input value?"); PN->setIncomingValue(pred, InVal); PN->setIncomingBlock(pred, MappedBlock); } else { PN->removeIncomingValue(pred, false); --pred, --e; // Revisit the next entry. } } } // The loop above has removed PHI entries for those blocks that are dead // and has updated others. However, if a block is live (i.e. copied over) // but its terminator has been changed to not go to this block, then our // phi nodes will have invalid entries. Update the PHI nodes in this // case. PHINode *PN = cast<PHINode>(NewBB->begin()); NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB)); if (NumPreds != PN->getNumIncomingValues()) { assert(NumPreds < PN->getNumIncomingValues()); // Count how many times each predecessor comes to this block. std::map<BasicBlock*, unsigned> PredCount; for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); PI != E; ++PI) --PredCount[*PI]; // Figure out how many entries to remove from each PHI. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) ++PredCount[PN->getIncomingBlock(i)]; // At this point, the excess predecessor entries are positive in the // map. Loop over all of the PHIs and remove excess predecessor // entries. BasicBlock::iterator I = NewBB->begin(); for (; (PN = dyn_cast<PHINode>(I)); ++I) { for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(), E = PredCount.end(); PCI != E; ++PCI) { BasicBlock *Pred = PCI->first; for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove) PN->removeIncomingValue(Pred, false); } } } // If the loops above have made these phi nodes have 0 or 1 operand, // replace them with undef or the input value. We must do this for // correctness, because 0-operand phis are not valid. PN = cast<PHINode>(NewBB->begin()); if (PN->getNumIncomingValues() == 0) { BasicBlock::iterator I = NewBB->begin(); BasicBlock::const_iterator OldI = OldBB->begin(); while ((PN = dyn_cast<PHINode>(I++))) { Value *NV = UndefValue::get(PN->getType()); PN->replaceAllUsesWith(NV); assert(VMap[OldI] == PN && "VMap mismatch"); VMap[OldI] = NV; PN->eraseFromParent(); ++OldI; } } // NOTE: We cannot eliminate single entry phi nodes here, because of // VMap. Single entry phi nodes can have multiple VMap entries // pointing at them. Thus, deleting one would require scanning the VMap // to update any entries in it that would require that. This would be // really slow. } // Now that the inlined function body has been fully constructed, go through // and zap unconditional fall-through branches. This happen all the time when // specializing code: code specialization turns conditional branches into // uncond branches, and this code folds them. Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]); while (I != NewFunc->end()) { BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); if (!BI || BI->isConditional()) { ++I; continue; } // Note that we can't eliminate uncond branches if the destination has // single-entry PHI nodes. Eliminating the single-entry phi nodes would // require scanning the VMap to update any entries that point to the phi // node. BasicBlock *Dest = BI->getSuccessor(0); if (!Dest->getSinglePredecessor() || isa<PHINode>(Dest->begin())) { ++I; continue; } // We know all single-entry PHI nodes in the inlined function have been // removed, so we just need to splice the blocks. BI->eraseFromParent(); // Make all PHI nodes that referred to Dest now refer to I as their source. Dest->replaceAllUsesWith(I); // Move all the instructions in the succ to the pred. I->getInstList().splice(I->end(), Dest->getInstList()); // Remove the dest block. Dest->eraseFromParent(); // Do not increment I, iteratively merge all things this block branches to. } }
/// Rotate loop LP. Return true if the loop is rotated. bool LoopRotate::rotateLoop(Loop *L) { // If the loop has only one block then there is not much to rotate. if (L->getBlocks().size() == 1) return false; BasicBlock *OrigHeader = L->getHeader(); BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator()); if (BI == 0 || BI->isUnconditional()) return false; // If the loop header is not one of the loop exiting blocks then // either this loop is already rotated or it is not // suitable for loop rotation transformations. if (!L->isLoopExiting(OrigHeader)) return false; // Updating PHInodes in loops with multiple exits adds complexity. // Keep it simple, and restrict loop rotation to loops with one exit only. // In future, lift this restriction and support for multiple exits if // required. SmallVector<BasicBlock*, 8> ExitBlocks; L->getExitBlocks(ExitBlocks); if (ExitBlocks.size() > 1) return false; // Check size of original header and reject loop if it is very big. { CodeMetrics Metrics; Metrics.analyzeBasicBlock(OrigHeader); if (Metrics.NumInsts > MAX_HEADER_SIZE) return false; } // Now, this loop is suitable for rotation. BasicBlock *OrigPreheader = L->getLoopPreheader(); BasicBlock *OrigLatch = L->getLoopLatch(); // If the loop could not be converted to canonical form, it must have an // indirectbr in it, just give up. if (OrigPreheader == 0 || OrigLatch == 0) return false; // Anything ScalarEvolution may know about this loop or the PHI nodes // in its header will soon be invalidated. if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>()) SE->forgetLoop(L); // Find new Loop header. NewHeader is a Header's one and only successor // that is inside loop. Header's other successor is outside the // loop. Otherwise loop is not suitable for rotation. BasicBlock *Exit = BI->getSuccessor(0); BasicBlock *NewHeader = BI->getSuccessor(1); if (L->contains(Exit)) std::swap(Exit, NewHeader); assert(NewHeader && "Unable to determine new loop header"); assert(L->contains(NewHeader) && !L->contains(Exit) && "Unable to determine loop header and exit blocks"); // This code assumes that the new header has exactly one predecessor. // Remove any single-entry PHI nodes in it. assert(NewHeader->getSinglePredecessor() && "New header doesn't have one pred!"); FoldSingleEntryPHINodes(NewHeader); // Begin by walking OrigHeader and populating ValueMap with an entry for // each Instruction. BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end(); ValueToValueMapTy ValueMap; // For PHI nodes, the value available in OldPreHeader is just the // incoming value from OldPreHeader. for (; PHINode *PN = dyn_cast<PHINode>(I); ++I) ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreheader)); // For the rest of the instructions, either hoist to the OrigPreheader if // possible or create a clone in the OldPreHeader if not. TerminatorInst *LoopEntryBranch = OrigPreheader->getTerminator(); while (I != E) { Instruction *Inst = I++; // If the instruction's operands are invariant and it doesn't read or write // memory, then it is safe to hoist. Doing this doesn't change the order of // execution in the preheader, but does prevent the instruction from // executing in each iteration of the loop. This means it is safe to hoist // something that might trap, but isn't safe to hoist something that reads // memory (without proving that the loop doesn't write). if (L->hasLoopInvariantOperands(Inst) && !Inst->mayReadFromMemory() && !Inst->mayWriteToMemory() && !isa<TerminatorInst>(Inst) && !isa<DbgInfoIntrinsic>(Inst)) { Inst->moveBefore(LoopEntryBranch); continue; } // Otherwise, create a duplicate of the instruction. Instruction *C = Inst->clone(); // Eagerly remap the operands of the instruction. RemapInstruction(C, ValueMap, RF_NoModuleLevelChanges|RF_IgnoreMissingEntries); // With the operands remapped, see if the instruction constant folds or is // otherwise simplifyable. This commonly occurs because the entry from PHI // nodes allows icmps and other instructions to fold. Value *V = SimplifyInstruction(C); if (V && LI->replacementPreservesLCSSAForm(C, V)) { // If so, then delete the temporary instruction and stick the folded value // in the map. delete C; ValueMap[Inst] = V; } else { // Otherwise, stick the new instruction into the new block! C->setName(Inst->getName()); C->insertBefore(LoopEntryBranch); ValueMap[Inst] = C; } } // Along with all the other instructions, we just cloned OrigHeader's // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's // successors by duplicating their incoming values for OrigHeader. TerminatorInst *TI = OrigHeader->getTerminator(); for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin(); PHINode *PN = dyn_cast<PHINode>(BI); ++BI) PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreheader); // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove // OrigPreHeader's old terminator (the original branch into the loop), and // remove the corresponding incoming values from the PHI nodes in OrigHeader. LoopEntryBranch->eraseFromParent(); // If there were any uses of instructions in the duplicated block outside the // loop, update them, inserting PHI nodes as required RewriteUsesOfClonedInstructions(OrigHeader, OrigPreheader, ValueMap); // NewHeader is now the header of the loop. L->moveToHeader(NewHeader); assert(L->getHeader() == NewHeader && "Latch block is our new header"); // At this point, we've finished our major CFG changes. As part of cloning // the loop into the preheader we've simplified instructions and the // duplicated conditional branch may now be branching on a constant. If it is // branching on a constant and if that constant means that we enter the loop, // then we fold away the cond branch to an uncond branch. This simplifies the // loop in cases important for nested loops, and it also means we don't have // to split as many edges. BranchInst *PHBI = cast<BranchInst>(OrigPreheader->getTerminator()); assert(PHBI->isConditional() && "Should be clone of BI condbr!"); if (!isa<ConstantInt>(PHBI->getCondition()) || PHBI->getSuccessor(cast<ConstantInt>(PHBI->getCondition())->isZero()) != NewHeader) { // The conditional branch can't be folded, handle the general case. // Update DominatorTree to reflect the CFG change we just made. Then split // edges as necessary to preserve LoopSimplify form. if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) { // Since OrigPreheader now has the conditional branch to Exit block, it is // the dominator of Exit. DT->changeImmediateDominator(Exit, OrigPreheader); DT->changeImmediateDominator(NewHeader, OrigPreheader); // Update OrigHeader to be dominated by the new header block. DT->changeImmediateDominator(OrigHeader, OrigLatch); } // Right now OrigPreHeader has two successors, NewHeader and ExitBlock, and // thus is not a preheader anymore. Split the edge to form a real preheader. BasicBlock *NewPH = SplitCriticalEdge(OrigPreheader, NewHeader, this); NewPH->setName(NewHeader->getName() + ".lr.ph"); // Preserve canonical loop form, which means that 'Exit' should have only one // predecessor. BasicBlock *ExitSplit = SplitCriticalEdge(L->getLoopLatch(), Exit, this); ExitSplit->moveBefore(Exit); } else { // We can fold the conditional branch in the preheader, this makes things // simpler. The first step is to remove the extra edge to the Exit block. Exit->removePredecessor(OrigPreheader, true /*preserve LCSSA*/); BranchInst::Create(NewHeader, PHBI); PHBI->eraseFromParent(); // With our CFG finalized, update DomTree if it is available. if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) { // Update OrigHeader to be dominated by the new header block. DT->changeImmediateDominator(NewHeader, OrigPreheader); DT->changeImmediateDominator(OrigHeader, OrigLatch); } } assert(L->getLoopPreheader() && "Invalid loop preheader after loop rotation"); assert(L->getLoopLatch() && "Invalid loop latch after loop rotation"); // Now that the CFG and DomTree are in a consistent state again, try to merge // the OrigHeader block into OrigLatch. This will succeed if they are // connected by an unconditional branch. This is just a cleanup so the // emitted code isn't too gross in this common case. MergeBlockIntoPredecessor(OrigHeader, this); ++NumRotated; return true; }
/// GetIfCondition - Given a basic block (BB) with two predecessors, /// check to see if the merge at this block is due /// to an "if condition". If so, return the boolean condition that determines /// which entry into BB will be taken. Also, return by references the block /// that will be entered from if the condition is true, and the block that will /// be entered if the condition is false. /// /// This does no checking to see if the true/false blocks have large or unsavory /// instructions in them. Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse) { PHINode *SomePHI = dyn_cast<PHINode>(BB->begin()); BasicBlock *Pred1 = NULL; BasicBlock *Pred2 = NULL; if (SomePHI) { if (SomePHI->getNumIncomingValues() != 2) return NULL; Pred1 = SomePHI->getIncomingBlock(0); Pred2 = SomePHI->getIncomingBlock(1); } else { pred_iterator PI = pred_begin(BB), PE = pred_end(BB); if (PI == PE) // No predecessor return NULL; Pred1 = *PI++; if (PI == PE) // Only one predecessor return NULL; Pred2 = *PI++; if (PI != PE) // More than two predecessors return NULL; } // We can only handle branches. Other control flow will be lowered to // branches if possible anyway. BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator()); BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator()); if (Pred1Br == 0 || Pred2Br == 0) return 0; // Eliminate code duplication by ensuring that Pred1Br is conditional if // either are. if (Pred2Br->isConditional()) { // If both branches are conditional, we don't have an "if statement". In // reality, we could transform this case, but since the condition will be // required anyway, we stand no chance of eliminating it, so the xform is // probably not profitable. if (Pred1Br->isConditional()) return 0; std::swap(Pred1, Pred2); std::swap(Pred1Br, Pred2Br); } if (Pred1Br->isConditional()) { // The only thing we have to watch out for here is to make sure that Pred2 // doesn't have incoming edges from other blocks. If it does, the condition // doesn't dominate BB. if (Pred2->getSinglePredecessor() == 0) return 0; // If we found a conditional branch predecessor, make sure that it branches // to BB and Pred2Br. If it doesn't, this isn't an "if statement". if (Pred1Br->getSuccessor(0) == BB && Pred1Br->getSuccessor(1) == Pred2) { IfTrue = Pred1; IfFalse = Pred2; } else if (Pred1Br->getSuccessor(0) == Pred2 && Pred1Br->getSuccessor(1) == BB) { IfTrue = Pred2; IfFalse = Pred1; } else { // We know that one arm of the conditional goes to BB, so the other must // go somewhere unrelated, and this must not be an "if statement". return 0; } return Pred1Br->getCondition(); } // Ok, if we got here, both predecessors end with an unconditional branch to // BB. Don't panic! If both blocks only have a single (identical) // predecessor, and THAT is a conditional branch, then we're all ok! BasicBlock *CommonPred = Pred1->getSinglePredecessor(); if (CommonPred == 0 || CommonPred != Pred2->getSinglePredecessor()) return 0; // Otherwise, if this is a conditional branch, then we can use it! BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator()); if (BI == 0) return 0; assert(BI->isConditional() && "Two successors but not conditional?"); if (BI->getSuccessor(0) == Pred1) { IfTrue = Pred1; IfFalse = Pred2; } else { IfTrue = Pred2; IfFalse = Pred1; } return BI->getCondition(); }
/// If \param [in] BB has more than one predecessor that is a conditional /// branch, attempt to use parallel and/or for the branch condition. \returns /// true on success. /// /// Before: /// ...... /// %cmp10 = fcmp une float %tmp1, %tmp2 /// br i1 %cmp1, label %if.then, label %lor.rhs /// /// lor.rhs: /// ...... /// %cmp11 = fcmp une float %tmp3, %tmp4 /// br i1 %cmp11, label %if.then, label %ifend /// /// if.end: // the merge block /// ...... /// /// if.then: // has two predecessors, both of them contains conditional branch. /// ...... /// br label %if.end; /// /// After: /// ...... /// %cmp10 = fcmp une float %tmp1, %tmp2 /// ...... /// %cmp11 = fcmp une float %tmp3, %tmp4 /// %cmp12 = or i1 %cmp10, %cmp11 // parallel-or mode. /// br i1 %cmp12, label %if.then, label %ifend /// /// if.end: /// ...... /// /// if.then: /// ...... /// br label %if.end; /// /// Current implementation handles two cases. /// Case 1: \param BB is on the else-path. /// /// BB1 /// / | /// BB2 | /// / \ | /// BB3 \ | where, BB1, BB2 contain conditional branches. /// \ | / BB3 contains unconditional branch. /// \ | / BB4 corresponds to \param BB which is also the merge. /// BB => BB4 /// /// /// Corresponding source code: /// /// if (a == b && c == d) /// statement; // BB3 /// /// Case 2: \param BB BB is on the then-path. /// /// BB1 /// / | /// | BB2 /// \ / | where BB1, BB2 contain conditional branches. /// BB => BB3 | BB3 contains unconditiona branch and corresponds /// \ / to \param BB. BB4 is the merge. /// BB4 /// /// Corresponding source code: /// /// if (a == b || c == d) /// statement; // BB3 /// /// In both cases, \param BB is the common successor of conditional branches. /// In Case 1, \param BB (BB4) has an unconditional branch (BB3) as /// its predecessor. In Case 2, \param BB (BB3) only has conditional branches /// as its predecessors. /// bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder, Pass *P) { PHINode *PHI = dyn_cast<PHINode>(BB->begin()); if (PHI) return false; // For simplicity, avoid cases containing PHI nodes. BasicBlock *LastCondBlock = NULL; BasicBlock *FirstCondBlock = NULL; BasicBlock *UnCondBlock = NULL; int Idx = -1; // Check predecessors of \param BB. SmallPtrSet<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB)); for (SmallPtrSetIterator<BasicBlock *> PI = Preds.begin(), PE = Preds.end(); PI != PE; ++PI) { BasicBlock *Pred = *PI; BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator()); // All predecessors should terminate with a branch. if (!PBI) return false; BasicBlock *PP = Pred->getSinglePredecessor(); if (PBI->isUnconditional()) { // Case 1: Pred (BB3) is an unconditional block, it should // have a single predecessor (BB2) that is also a predecessor // of \param BB (BB4) and should not have address-taken. // There should exist only one such unconditional // branch among the predecessors. if (UnCondBlock || !PP || (Preds.count(PP) == 0) || Pred->hasAddressTaken()) return false; UnCondBlock = Pred; continue; } // Only conditional branches are allowed beyond this point. assert(PBI->isConditional()); // Condition's unique use should be the branch instruction. Value *PC = PBI->getCondition(); if (!PC || !PC->hasOneUse()) return false; if (PP && Preds.count(PP)) { // These are internal condition blocks to be merged from, e.g., // BB2 in both cases. // Should not be address-taken. if (Pred->hasAddressTaken()) return false; // Instructions in the internal condition blocks should be safe // to hoist up. for (BasicBlock::iterator BI = Pred->begin(), BE = PBI; BI != BE;) { Instruction *CI = BI++; if (isa<PHINode>(CI) || !isSafeToSpeculativelyExecute(CI)) return false; } } else { // This is the condition block to be merged into, e.g. BB1 in // both cases. if (FirstCondBlock) return false; FirstCondBlock = Pred; } // Find whether BB is uniformly on the true (or false) path // for all of its predecessors. BasicBlock *PS1 = PBI->getSuccessor(0); BasicBlock *PS2 = PBI->getSuccessor(1); BasicBlock *PS = (PS1 == BB) ? PS2 : PS1; int CIdx = (PS1 == BB) ? 0 : 1; if (Idx == -1) Idx = CIdx; else if (CIdx != Idx) return false; // PS is the successor which is not BB. Check successors to identify // the last conditional branch. if (Preds.count(PS) == 0) { // Case 2. LastCondBlock = Pred; } else { // Case 1 BranchInst *BPS = dyn_cast<BranchInst>(PS->getTerminator()); if (BPS && BPS->isUnconditional()) { // Case 1: PS(BB3) should be an unconditional branch. LastCondBlock = Pred; } } } if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock)) return false; TerminatorInst *TBB = LastCondBlock->getTerminator(); BasicBlock *PS1 = TBB->getSuccessor(0); BasicBlock *PS2 = TBB->getSuccessor(1); BranchInst *PBI1 = dyn_cast<BranchInst>(PS1->getTerminator()); BranchInst *PBI2 = dyn_cast<BranchInst>(PS2->getTerminator()); // If PS1 does not jump into PS2, but PS2 jumps into PS1, // attempt branch inversion. if (!PBI1 || !PBI1->isUnconditional() || (PS1->getTerminator()->getSuccessor(0) != PS2)) { // Check whether PS2 jumps into PS1. if (!PBI2 || !PBI2->isUnconditional() || (PS2->getTerminator()->getSuccessor(0) != PS1)) return false; // Do branch inversion. BasicBlock *CurrBlock = LastCondBlock; bool EverChanged = false; while (1) { BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator()); CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition()); CmpInst::Predicate Predicate = CI->getPredicate(); // Cannonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) { CI->setPredicate(ICmpInst::getInversePredicate(Predicate)); BI->swapSuccessors(); EverChanged = true; } if (CurrBlock == FirstCondBlock) break; CurrBlock = CurrBlock->getSinglePredecessor(); } return EverChanged; } // PS1 must have a conditional branch. if (!PBI1 || !PBI1->isUnconditional()) return false; // PS2 should not contain PHI node. PHI = dyn_cast<PHINode>(PS2->begin()); if (PHI) return false; // Do the transformation. BasicBlock *CB; BranchInst *PBI = dyn_cast<BranchInst>(FirstCondBlock->getTerminator()); bool Iteration = true; BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); Value *PC = PBI->getCondition(); do { CB = PBI->getSuccessor(1 - Idx); // Delete the conditional branch. FirstCondBlock->getInstList().pop_back(); FirstCondBlock->getInstList() .splice(FirstCondBlock->end(), CB->getInstList()); PBI = cast<BranchInst>(FirstCondBlock->getTerminator()); Value *CC = PBI->getCondition(); // Merge conditions. Builder.SetInsertPoint(PBI); Value *NC; if (Idx == 0) // Case 2, use parallel or. NC = Builder.CreateOr(PC, CC); else // Case 1, use parallel and. NC = Builder.CreateAnd(PC, CC); PBI->replaceUsesOfWith(CC, NC); PC = NC; if (CB == LastCondBlock) Iteration = false; // Remove internal conditional branches. CB->dropAllReferences(); // make CB unreachable and let downstream to delete the block. new UnreachableInst(CB->getContext(), CB); } while (Iteration); Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt); DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock); return true; }