diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp index 86411320ab248..bcff9a72b6572 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp @@ -143,6 +143,10 @@ bool InstCombinerImpl::foldIntegerTypedPHI(PHINode &PN) { BasicBlock *BB = std::get<0>(Incoming); Value *Arg = std::get<1>(Incoming); + // Arg could be a constant, constant expr, etc., which we don't cover here. + if (!isa(Arg) && !isa(Arg)) + return false; + // First look backward: if (auto *PI = dyn_cast(Arg)) { AvailablePtrVals.emplace_back(PI->getOperand(0)); diff --git a/llvm/test/Transforms/InstCombine/phi-int-users.ll b/llvm/test/Transforms/InstCombine/phi-int-users.ll new file mode 100644 index 0000000000000..6c98cc8a1c900 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/phi-int-users.ll @@ -0,0 +1,44 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt -S < %s -passes=instcombine | FileCheck %s + +; Verify that instcombine doesn't look at users of Constant in different +; functions for dominates() queries. + +define void @f1(i1 %a) { +; CHECK-LABEL: define void @f1( +; CHECK-SAME: i1 [[A:%.*]]) { +; CHECK-NEXT: br label %[[BB1:.*]] +; CHECK: [[BB1]]: +; CHECK-NEXT: br i1 [[A]], label %[[BB3:.*]], label %[[BB2:.*]] +; CHECK: [[BB2]]: +; CHECK-NEXT: br label %[[BB3]] +; CHECK: [[BB3]]: +; CHECK-NEXT: [[PHI:%.*]] = phi i64 [ 0, %[[BB2]] ], [ 1, %[[BB1]] ] +; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[PHI]] to ptr +; CHECK-NEXT: store i32 0, ptr [[INTTOPTR]], align 4 +; CHECK-NEXT: br label %[[BB1]] +; + br label %bb1 + +bb1: + br i1 %a, label %bb3, label %bb2 + +bb2: + br label %bb3 + +bb3: + %phi = phi i64 [ 0, %bb2 ], [ 1, %bb1 ] + %inttoptr = inttoptr i64 %phi to ptr + store i32 0, ptr %inttoptr, align 4 + br label %bb1 +} + +define void @f2() { +; CHECK-LABEL: define void @f2() { +; CHECK-NEXT: [[BB:.*:]] +; CHECK-NEXT: ret void +; +bb: + %inttoptr = inttoptr i64 0 to ptr + ret void +}