[InstCombine] Fix select + cast fold with constant expression (PR64669)
authorNikita Popov <npopov@redhat.com>
Mon, 14 Aug 2023 15:39:48 +0000 (17:39 +0200)
committerTobias Hieta <tobias@hieta.se>
Tue, 22 Aug 2023 07:02:28 +0000 (09:02 +0200)
The zext constant expression was detected by the fold, but then
handled as a sext. Use ZExtOperator instead of ZExtInst to handle
constant expressions.

Fixes https://github.com/llvm/llvm-project/issues/64669.

(cherry picked from commit c15ccfb24afa67d3c3f54e52cc1d1afa564715ed)

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/binop-select-cast-of-select-cond.ll

index afd6e03..767b7c7 100644 (file)
@@ -906,7 +906,7 @@ InstCombinerImpl::foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I) {
 
   auto NewFoldedConst = [&](bool IsTrueArm, Value *V) {
     bool IsCastOpRHS = (CastOp == RHS);
-    bool IsZExt = isa<ZExtInst>(CastOp);
+    bool IsZExt = isa<ZExtOperator>(CastOp);
     Constant *C;
 
     if (IsTrueArm) {
index 0c859e5..f962210 100644 (file)
@@ -224,3 +224,19 @@ define <2 x i8> @vectorized_add(<2 x i1> %c, <2 x i8> %arg) {
   %add = add <2 x i8> %sel, %zext
   ret <2 x i8> %add
 }
+
+@b = external global [72 x i32]
+@c = external global i32
+
+define i64 @pr64669(i64 %a) {
+; CHECK-LABEL: define i64 @pr64669
+; CHECK-SAME: (i64 [[A:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = add i64 [[A]], 1
+; CHECK-NEXT:    [[ADD:%.*]] = select i1 icmp ne (ptr getelementptr inbounds ([72 x i32], ptr @b, i64 0, i64 25), ptr @c), i64 [[TMP1]], i64 0
+; CHECK-NEXT:    ret i64 [[ADD]]
+;
+  %mul = select i1 icmp ne (ptr getelementptr inbounds ([72 x i32], ptr @b, i64 0, i64 25), ptr @c), i64 %a, i64 0
+  %conv3 = zext i1 icmp ne (ptr getelementptr inbounds ([72 x i32], ptr @b, i64 0, i64 25), ptr @c) to i64
+  %add = add nsw i64 %mul, %conv3
+  ret i64 %add
+}