+/// ProcessJumpOnLogicalPHI - PN's basic block contains a conditional branch
+/// whose condition is an AND/OR where one side is PN. If PN has constant
+/// operands that permit us to evaluate the condition for some operand, thread
+/// through the block. For example with:
+/// br (and X, phi(Y, Z, false))
+/// the predecessor corresponding to the 'false' will always jump to the false
+/// destination of the branch.
+///
+bool JumpThreading::ProcessBranchOnLogical(Value *V, BasicBlock *BB,
+ bool isAnd) {
+ // If this is a binary operator tree of the same AND/OR opcode, check the
+ // LHS/RHS.
+ if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
+ if ((isAnd && BO->getOpcode() == Instruction::And) ||
+ (!isAnd && BO->getOpcode() == Instruction::Or)) {
+ if (ProcessBranchOnLogical(BO->getOperand(0), BB, isAnd))
+ return true;
+ if (ProcessBranchOnLogical(BO->getOperand(1), BB, isAnd))
+ return true;
+ }
+
+ // If this isn't a PHI node, we can't handle it.
+ PHINode *PN = dyn_cast<PHINode>(V);
+ if (!PN || PN->getParent() != BB) return false;
+
+ // We can only do the simplification for phi nodes of 'false' with AND or
+ // 'true' with OR. See if we have any entries in the phi for this.
+ unsigned PredNo = ~0U;
+ ConstantInt *PredCst = ConstantInt::get(Type::getInt1Ty(BB->getContext()),
+ !isAnd);
+ for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
+ if (PN->getIncomingValue(i) == PredCst) {
+ PredNo = i;
+ break;
+ }
+ }
+
+ // If no match, bail out.
+ if (PredNo == ~0U)
+ return false;
+
+ // If so, we can actually do this threading. Merge any common predecessors
+ // that will act the same.
+ BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredCst);
+
+ // Next, figure out which successor we are threading to. If this was an AND,
+ // the constant must be FALSE, and we must be targeting the 'false' block.
+ // If this is an OR, the constant must be TRUE, and we must be targeting the
+ // 'true' block.
+ BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(isAnd);
+
+ // Ok, try to thread it!
+ return ThreadEdge(BB, PredBB, SuccBB);
+}
+
+/// GetResultOfComparison - Given an icmp/fcmp predicate and the left and right
+/// hand sides of the compare instruction, try to determine the result. If the
+/// result can not be determined, a null pointer is returned.
+static Constant *GetResultOfComparison(CmpInst::Predicate pred,
+ Value *LHS, Value *RHS,
+ LLVMContext &Context) {
+ if (Constant *CLHS = dyn_cast<Constant>(LHS))
+ if (Constant *CRHS = dyn_cast<Constant>(RHS))
+ return ConstantExpr::getCompare(pred, CLHS, CRHS);
+
+ if (LHS == RHS)
+ if (isa<IntegerType>(LHS->getType()) || isa<PointerType>(LHS->getType()))
+ return ICmpInst::isTrueWhenEqual(pred) ?
+ ConstantInt::getTrue(Context) : ConstantInt::getFalse(Context);
+
+ return 0;
+}
+
+/// ProcessBranchOnCompare - We found a branch on a comparison between a phi
+/// node and a value. If we can identify when the comparison is true between
+/// the phi inputs and the value, we can fold the compare for that edge and
+/// thread through it.
+bool JumpThreading::ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB) {
+ PHINode *PN = cast<PHINode>(Cmp->getOperand(0));
+ Value *RHS = Cmp->getOperand(1);
+
+ // If the phi isn't in the current block, an incoming edge to this block
+ // doesn't control the destination.
+ if (PN->getParent() != BB)
+ return false;
+
+ // We can do this simplification if any comparisons fold to true or false.
+ // See if any do.
+ Value *PredVal = 0;
+ bool TrueDirection = false;
+ for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
+ PredVal = PN->getIncomingValue(i);
+
+ Constant *Res = GetResultOfComparison(Cmp->getPredicate(), PredVal,
+ RHS, Cmp->getContext());
+ if (!Res) {
+ PredVal = 0;
+ continue;
+ }
+
+ // If this folded to a constant expr, we can't do anything.
+ if (ConstantInt *ResC = dyn_cast<ConstantInt>(Res)) {
+ TrueDirection = ResC->getZExtValue();
+ break;
+ }
+ // If this folded to undef, just go the false way.
+ if (isa<UndefValue>(Res)) {
+ TrueDirection = false;
+ break;
+ }
+
+ // Otherwise, we can't fold this input.
+ PredVal = 0;
+ }
+
+ // If no match, bail out.
+ if (PredVal == 0)
+ return false;
+
+ // If so, we can actually do this threading. Merge any common predecessors
+ // that will act the same.
+ BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredVal);
+
+ // Next, get our successor.
+ BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(!TrueDirection);
+
+ // Ok, try to thread it!
+ return ThreadEdge(BB, PredBB, SuccBB);
+}
+
+