diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -1459,11 +1459,13 @@ // Recurse, but cap the recursion to one level, because we don't // want to waste time spinning around in loops. + // TODO: See if we can base recursion limiter on number of incoming phi + // edges so we don't overly clamp analysis. computeKnownBits(IncValue, Known2, MaxAnalysisRecursionDepth - 1, RecQ); - // If this failed, see if we can use a conditional branch into the phi + // See if we can further use a conditional branch into the phi // to help us determine the range of the value. - if (Known2.isUnknown()) { + if (!Known2.isConstant()) { ICmpInst::Predicate Pred; const APInt *RHSC; BasicBlock *TrueSucc, *FalseSucc; @@ -1478,7 +1480,7 @@ Pred = CmpInst::getInversePredicate(Pred); // Get the knownbits implied by the incoming phi condition. auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC); - Known2 = CR.toKnownBits(); + Known2 = Known2.unionWith(CR.toKnownBits()); } } } diff --git a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll --- a/llvm/test/Analysis/ValueTracking/phi-known-bits.ll +++ b/llvm/test/Analysis/ValueTracking/phi-known-bits.ll @@ -375,13 +375,10 @@ define i8 @phi_ugt_high_bits_and_known(i8 %xx) { ; CHECK-LABEL: @phi_ugt_high_bits_and_known( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[X:%.*]] = or i8 [[XX:%.*]], 1 -; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[X]], -65 +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[XX:%.*]], -65 ; CHECK-NEXT: br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]] ; CHECK: T: -; CHECK-NEXT: [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ] -; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 65 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 65 ; CHECK: F: ; CHECK-NEXT: br label [[T]] ;