[llvm] c7f0e49 - [InstCombine] Fix canAlwaysEvaluateInTy() with constant exprs

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 1 06:52:59 PDT 2023


Author: Nikita Popov
Date: 2023-11-01T14:52:50+01:00
New Revision: c7f0e499158fa4471ac86af46e13f45ccbfba399

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

LOG: [InstCombine] Fix canAlwaysEvaluateInTy() with constant exprs

The m_ZExtOrSExt / m_Trunc in the following code can match constant
expressions, which we don't want here. Make sure we bail out early
for non-immediate constants.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/test/Transforms/InstCombine/zext.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 5bdb4d58412f429..e2f17aab9c442cf 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -213,8 +213,9 @@ Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
 /// Constants and extensions/truncates from the destination type are always
 /// free to be evaluated in that type. This is a helper for canEvaluate*.
 static bool canAlwaysEvaluateInType(Value *V, Type *Ty) {
-  if (match(V, m_ImmConstant()))
-    return true;
+  if (isa<Constant>(V))
+    return match(V, m_ImmConstant());
+
   Value *X;
   if ((match(V, m_ZExtOrSExt(m_Value(X))) || match(V, m_Trunc(m_Value(X)))) &&
       X->getType() == Ty)

diff  --git a/llvm/test/Transforms/InstCombine/zext.ll b/llvm/test/Transforms/InstCombine/zext.ll
index f595638ba9e3083..255d9764410cd1e 100644
--- a/llvm/test/Transforms/InstCombine/zext.ll
+++ b/llvm/test/Transforms/InstCombine/zext.ll
@@ -1,6 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 
+target datalayout = "n64"
+
 declare void @use1(i1)
 declare void @use32(i32)
 declare void @use_vec(<2 x i9>)
@@ -762,3 +764,16 @@ define i32  @zext_icmp_eq0_no_shift(ptr %ptr ) {
   %res = zext i1 %cmp to i32
   ret i32 %res
 }
+
+ at g = external global i8
+
+define i64 @evaluate_zexted_const_expr(i1 %c) {
+; CHECK-LABEL: @evaluate_zexted_const_expr(
+; CHECK-NEXT:    [[AND:%.*]] = select i1 [[C:%.*]], i7 trunc (i64 add (i64 ptrtoint (ptr @g to i64), i64 1) to i7), i7 trunc (i64 add (i64 ptrtoint (ptr @g to i64), i64 2) to i7)
+; CHECK-NEXT:    [[EXT:%.*]] = zext i7 [[AND]] to i64
+; CHECK-NEXT:    ret i64 [[EXT]]
+;
+  %and = select i1 %c, i7 trunc (i64 add (i64 ptrtoint (ptr @g to i64), i64 1) to i7), i7 trunc (i64 add (i64 ptrtoint (ptr @g to i64), i64 2) to i7)
+  %ext = zext i7 %and to i64
+  ret i64 %ext
+}


        


More information about the llvm-commits mailing list