Index: llvm/trunk/lib/Analysis/LazyValueInfo.cpp =================================================================== --- llvm/trunk/lib/Analysis/LazyValueInfo.cpp +++ llvm/trunk/lib/Analysis/LazyValueInfo.cpp @@ -1262,8 +1262,40 @@ Instruction *CxtI) { const DataLayout &DL = CxtI->getModule()->getDataLayout(); LVILatticeVal Result = getCache(PImpl, AC, &DL, DT).getValueAt(V, CxtI); - - return getPredicateResult(Pred, C, Result, DL, TLI); + Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI); + if (Ret != Unknown) + return Ret; + + // TODO: Move this logic inside getValueAt so that it can be cached rather + // than re-queried on each call. This would also allow us to merge the + // underlying lattice values to get more information + if (CxtI) { + // For a comparison where the V is outside this block, it's possible + // that we've branched on it before. Look to see if the value is known + // on all incoming edges. + BasicBlock *BB = CxtI->getParent(); + pred_iterator PI = pred_begin(BB), PE = pred_end(BB); + if (PI != PE && + (!isa(V) || + cast(V)->getParent() != BB)) { + // For predecessor edge, determine if the comparison is true or false + // on that edge. If they're all true or all false, we can conclude + // the value of the comparison in this block. + Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI); + if (Baseline != Unknown) { + // Check that all remaining incoming values match the first one. + while (++PI != PE) { + Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI); + if (Ret != Baseline) break; + } + // If we terminated early, then one of the values didn't match. + if (PI == PE) { + return Baseline; + } + } + } + } + return Unknown; } void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, Index: llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp =================================================================== --- llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp +++ llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp @@ -758,67 +758,33 @@ if (CmpInst *CondCmp = dyn_cast(CondInst)) { - // For a comparison where the LHS is outside this block, it's possible - // that we've branched on it before. Used LVI to see if we can simplify - // the branch based on that. + // If we're branching on a conditional, LVI might be able to determine + // it's value at the the branch instruction. We only handle comparisons + // against a constant at this time. + // TODO: This should be extended to handle switches as well. BranchInst *CondBr = dyn_cast(BB->getTerminator()); Constant *CondConst = dyn_cast(CondCmp->getOperand(1)); - pred_iterator PI = pred_begin(BB), PE = pred_end(BB); - if (CondBr && CondConst && CondBr->isConditional() && PI != PE && - (!isa(CondCmp->getOperand(0)) || - cast(CondCmp->getOperand(0))->getParent() != BB)) { - // For predecessor edge, determine if the comparison is true or false - // on that edge. If they're all true or all false, we can simplify the - // branch. - // FIXME: We could handle mixed true/false by duplicating code. - LazyValueInfo::Tristate Baseline = - LVI->getPredicateOnEdge(CondCmp->getPredicate(), CondCmp->getOperand(0), - CondConst, *PI, BB, CondCmp); - if (Baseline != LazyValueInfo::Unknown) { - // Check that all remaining incoming values match the first one. - while (++PI != PE) { - LazyValueInfo::Tristate Ret = - LVI->getPredicateOnEdge(CondCmp->getPredicate(), - CondCmp->getOperand(0), CondConst, *PI, BB, - CondCmp); - if (Ret != Baseline) break; - } - - // If we terminated early, then one of the values didn't match. - if (PI == PE) { - unsigned ToRemove = Baseline == LazyValueInfo::True ? 1 : 0; - unsigned ToKeep = Baseline == LazyValueInfo::True ? 0 : 1; - CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true); - BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr); - CondBr->eraseFromParent(); - if (CondCmp->use_empty()) - CondCmp->eraseFromParent(); - else if (CondCmp->getParent() == BB) { - // If the fact we just learned is true for all uses of the - // condition, replace it with a constant value - auto *CI = Baseline == LazyValueInfo::True ? - ConstantInt::getTrue(CondCmp->getType()) : - ConstantInt::getFalse(CondCmp->getType()); - CondCmp->replaceAllUsesWith(CI); - CondCmp->eraseFromParent(); - } - return true; - } - } - - } else if (CondBr && CondConst && CondBr->isConditional()) { - // There might be an invariant in the same block with the conditional - // that can determine the predicate. - + if (CondBr && CondConst && CondBr->isConditional()) { LazyValueInfo::Tristate Ret = LVI->getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0), - CondConst, CondCmp); + CondConst, CondBr); if (Ret != LazyValueInfo::Unknown) { unsigned ToRemove = Ret == LazyValueInfo::True ? 1 : 0; unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1; CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true); BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr); CondBr->eraseFromParent(); + if (CondCmp->use_empty()) + CondCmp->eraseFromParent(); + else if (CondCmp->getParent() == BB) { + // If the fact we just learned is true for all uses of the + // condition, replace it with a constant value + auto *CI = Ret == LazyValueInfo::True ? + ConstantInt::getTrue(CondCmp->getType()) : + ConstantInt::getFalse(CondCmp->getType()); + CondCmp->replaceAllUsesWith(CI); + CondCmp->eraseFromParent(); + } return true; } }