Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2242,6 +2242,27 @@ return &BI; } + // See if we can determine the value of a use based on a dominating condition. + Value *Op0; + ConstantInt *C; + if (match(&BI, m_Br(m_ICmp(Pred, m_Value(Op0), m_ConstantInt(C)), TrueDest, + FalseDest)) && + (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) && + Op0->getNumUses() >= 2) { + BasicBlock *TargetBB = Pred == ICmpInst::ICMP_EQ ? TrueDest : FalseDest; + if (TargetBB->getSinglePredecessor()) { + // This branch dominates TargetBB and we known that Op0 == C in TargetBB. + for (User *U : Op0->users()) + if (Instruction *UI = dyn_cast(U)) + if (UI->getParent() == TargetBB) + for (unsigned OpNum = 0; OpNum < UI->getNumOperands(); ++OpNum) + if (UI->getOperand(OpNum) == Op0) + U->setOperand(OpNum, C); + + // FIXME: We've changed things, but not the branch. IC need to know this. + } + } + return nullptr; }