[llvm] c15ccfb - [InstCombine] Fix select + cast fold with constant expression (PR64669)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 14 08:44:32 PDT 2023


Author: Nikita Popov
Date: 2023-08-14T17:44:23+02:00
New Revision: c15ccfb24afa67d3c3f54e52cc1d1afa564715ed

URL: https://github.com/llvm/llvm-project/commit/c15ccfb24afa67d3c3f54e52cc1d1afa564715ed
DIFF: https://github.com/llvm/llvm-project/commit/c15ccfb24afa67d3c3f54e52cc1d1afa564715ed.diff

LOG: [InstCombine] Fix select + cast fold with constant expression (PR64669)

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.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 01ca0894ac357b..2722160e72c149 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -908,7 +908,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) {

diff  --git a/llvm/test/Transforms/InstCombine/binop-select-cast-of-select-cond.ll b/llvm/test/Transforms/InstCombine/binop-select-cast-of-select-cond.ll
index eaec15b1672457..6cf8bb73c9ff76 100644
--- a/llvm/test/Transforms/InstCombine/binop-select-cast-of-select-cond.ll
+++ b/llvm/test/Transforms/InstCombine/binop-select-cast-of-select-cond.ll
@@ -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
 }
+
+ at b = external global [72 x i32]
+ at 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
+}


        


More information about the llvm-commits mailing list