+/// ProcessBranchOnDuplicateCond - We found a block and a predecessor of that
+/// block that jump on exactly the same condition. This means that we almost
+/// always know the direction of the edge in the DESTBB:
+/// PREDBB:
+/// br COND, DESTBB, BBY
+/// DESTBB:
+/// br COND, BBZ, BBW
+///
+/// If DESTBB has multiple predecessors, we can't just constant fold the branch
+/// in DESTBB, we have to thread over it.
+bool JumpThreading::ProcessBranchOnDuplicateCond(BasicBlock *PredBB,
+ BasicBlock *BB) {
+ BranchInst *PredBI = cast<BranchInst>(PredBB->getTerminator());
+
+ // If both successors of PredBB go to DESTBB, we don't know anything. We can
+ // fold the branch to an unconditional one, which allows other recursive
+ // simplifications.
+ bool BranchDir;
+ if (PredBI->getSuccessor(1) != BB)
+ BranchDir = true;
+ else if (PredBI->getSuccessor(0) != BB)
+ BranchDir = false;
+ else {
+ DOUT << " In block '" << PredBB->getNameStart()
+ << "' folding terminator: " << *PredBB->getTerminator();
+ ++NumFolds;
+ ConstantFoldTerminator(PredBB);
+ return true;
+ }
+
+ BranchInst *DestBI = cast<BranchInst>(BB->getTerminator());
+
+ // If the dest block has one predecessor, just fix the branch condition to a
+ // constant and fold it.
+ if (BB->getSinglePredecessor()) {
+ DOUT << " In block '" << BB->getNameStart()
+ << "' folding condition to '" << BranchDir << "': "
+ << *BB->getTerminator();
+ ++NumFolds;
+ DestBI->setCondition(Context->getConstantInt(Type::Int1Ty, BranchDir));
+ ConstantFoldTerminator(BB);
+ return true;
+ }
+
+ // Otherwise we need to thread from PredBB to DestBB's successor which
+ // involves code duplication. Check to see if it is worth it.
+ unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
+ if (JumpThreadCost > Threshold) {
+ DOUT << " Not threading BB '" << BB->getNameStart()
+ << "' - Cost is too high: " << JumpThreadCost << "\n";
+ return false;
+ }
+
+ // Next, figure out which successor we are threading to.
+ BasicBlock *SuccBB = DestBI->getSuccessor(!BranchDir);
+
+ // Ok, try to thread it!
+ return ThreadEdge(BB, PredBB, SuccBB, JumpThreadCost);
+}
+
+/// ProcessSwitchOnDuplicateCond - We found a block and a predecessor of that
+/// block that switch on exactly the same condition. This means that we almost
+/// always know the direction of the edge in the DESTBB:
+/// PREDBB:
+/// switch COND [... DESTBB, BBY ... ]
+/// DESTBB:
+/// switch COND [... BBZ, BBW ]
+///
+/// Optimizing switches like this is very important, because simplifycfg builds
+/// switches out of repeated 'if' conditions.
+bool JumpThreading::ProcessSwitchOnDuplicateCond(BasicBlock *PredBB,
+ BasicBlock *DestBB) {
+ // Can't thread edge to self.
+ if (PredBB == DestBB)
+ return false;
+
+
+ SwitchInst *PredSI = cast<SwitchInst>(PredBB->getTerminator());
+ SwitchInst *DestSI = cast<SwitchInst>(DestBB->getTerminator());
+
+ // There are a variety of optimizations that we can potentially do on these
+ // blocks: we order them from most to least preferable.
+
+ // If DESTBB *just* contains the switch, then we can forward edges from PREDBB
+ // directly to their destination. This does not introduce *any* code size
+ // growth. Skip debug info first.
+ BasicBlock::iterator BBI = DestBB->begin();
+ while (isa<DbgInfoIntrinsic>(BBI))
+ BBI++;
+
+ // FIXME: Thread if it just contains a PHI.
+ if (isa<SwitchInst>(BBI)) {
+ bool MadeChange = false;
+ // Ignore the default edge for now.
+ for (unsigned i = 1, e = DestSI->getNumSuccessors(); i != e; ++i) {
+ ConstantInt *DestVal = DestSI->getCaseValue(i);
+ BasicBlock *DestSucc = DestSI->getSuccessor(i);
+
+ // Okay, DestSI has a case for 'DestVal' that goes to 'DestSucc'. See if
+ // PredSI has an explicit case for it. If so, forward. If it is covered
+ // by the default case, we can't update PredSI.
+ unsigned PredCase = PredSI->findCaseValue(DestVal);
+ if (PredCase == 0) continue;
+
+ // If PredSI doesn't go to DestBB on this value, then it won't reach the
+ // case on this condition.
+ if (PredSI->getSuccessor(PredCase) != DestBB &&
+ DestSI->getSuccessor(i) != DestBB)
+ continue;
+
+ // Otherwise, we're safe to make the change. Make sure that the edge from
+ // DestSI to DestSucc is not critical and has no PHI nodes.
+ DOUT << "FORWARDING EDGE " << *DestVal << " FROM: " << *PredSI;
+ DOUT << "THROUGH: " << *DestSI;
+
+ // If the destination has PHI nodes, just split the edge for updating
+ // simplicity.
+ if (isa<PHINode>(DestSucc->begin()) && !DestSucc->getSinglePredecessor()){
+ SplitCriticalEdge(DestSI, i, this);
+ DestSucc = DestSI->getSuccessor(i);
+ }
+ FoldSingleEntryPHINodes(DestSucc);
+ PredSI->setSuccessor(PredCase, DestSucc);
+ MadeChange = true;
+ }
+
+ if (MadeChange)
+ return true;
+ }
+
+ return false;
+}
+
+