diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h index 093309cb8bbee..b730a36488780 100644 --- a/llvm/include/llvm/Analysis/ValueTracking.h +++ b/llvm/include/llvm/Analysis/ValueTracking.h @@ -1024,8 +1024,8 @@ findValuesAffectedByCondition(Value *Cond, bool IsAssume, LLVM_ABI Value *stripNullTest(Value *V); LLVM_ABI const Value *stripNullTest(const Value *V); -/// Enumerates all possible values of V and inserts them into the set \p -/// Constants. If \p AllowUndefOrPoison is false, it fails when V may contain +/// Enumerates all possible immediate values of V and inserts them into the set +/// \p Constants. If \p AllowUndefOrPoison is false, it fails when V may contain /// undef/poison elements. Returns true if the result is complete. Otherwise, /// the result is incomplete (more than MaxCount values). /// NOTE: The constant values are not distinct. diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 41ff816a33262..b20542c65f491 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -10485,7 +10485,8 @@ bool llvm::collectPossibleValues(const Value *V, SmallPtrSet Visited; SmallVector Worklist; auto Push = [&](const Value *V) -> bool { - if (auto *C = dyn_cast(V)) { + Constant *C; + if (match(const_cast(V), m_ImmConstant(C))) { if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C)) return false; // Check existence first to avoid unnecessary allocations. diff --git a/llvm/test/Transforms/SimplifyCFG/switch-on-const.ll b/llvm/test/Transforms/SimplifyCFG/switch-on-const.ll index 1ab1b5e8bd838..541bdf5ef996e 100644 --- a/llvm/test/Transforms/SimplifyCFG/switch-on-const.ll +++ b/llvm/test/Transforms/SimplifyCFG/switch-on-const.ll @@ -280,6 +280,36 @@ default: ret void } +@G = constant i16 zeroinitializer, align 1 + +; Make sure we don't remove edges when the condition is a unresolved constant expression. + +define i16 @switch_on_nonimmediate_constant_expr() { +; CHECK-LABEL: @switch_on_nonimmediate_constant_expr( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[SWITCH_SELECTCMP:%.*]] = icmp eq i32 ptrtoint (ptr @G to i32), 2 +; CHECK-NEXT: [[SWITCH_SELECT:%.*]] = select i1 [[SWITCH_SELECTCMP]], i16 456, i16 789 +; CHECK-NEXT: [[SWITCH_SELECTCMP1:%.*]] = icmp eq i32 ptrtoint (ptr @G to i32), 1 +; CHECK-NEXT: [[SWITCH_SELECT2:%.*]] = select i1 [[SWITCH_SELECTCMP1]], i16 123, i16 [[SWITCH_SELECT]] +; CHECK-NEXT: ret i16 [[SWITCH_SELECT2]] +; +entry: + switch i32 ptrtoint (ptr @G to i32), label %sw.default [ + i32 1, label %sw.bb + i32 2, label %sw.bb1 + ] + +sw.bb: + ret i16 123 + +sw.bb1: + ret i16 456 + +sw.default: + ret i16 789 +} + + declare void @llvm.trap() nounwind noreturn declare void @bees.a() nounwind declare void @bees.b() nounwind