[llvm] aca9c89 - [InstCombine] Avoid use of ConstantExpr::getIntegerCast()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 1 04:56:40 PDT 2023


Author: Nikita Popov
Date: 2023-11-01T12:56:30+01:00
New Revision: aca9c891a26eaa6e9078b19096cd9aa52d136091

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

LOG: [InstCombine] Avoid use of ConstantExpr::getIntegerCast()

Require that constants are ImmConstant for this transform, as we
may otherwise generate constant expressions, which are not
necessarily free.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/test/ThinLTO/X86/cfi-devirt.ll
    llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 55f410155e46a99..5bdb4d58412f429 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -29,11 +29,8 @@ using namespace PatternMatch;
 /// true for, actually insert the code to evaluate the expression.
 Value *InstCombinerImpl::EvaluateInDifferentType(Value *V, Type *Ty,
                                                  bool isSigned) {
-  if (Constant *C = dyn_cast<Constant>(V)) {
-    C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/);
-    // If we got a constantexpr back, try to simplify it with DL info.
-    return ConstantFoldConstant(C, DL, &TLI);
-  }
+  if (Constant *C = dyn_cast<Constant>(V))
+    return ConstantFoldIntegerCast(C, Ty, isSigned, DL);
 
   // Otherwise, it must be an instruction.
   Instruction *I = cast<Instruction>(V);
@@ -216,7 +213,7 @@ 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 (isa<Constant>(V))
+  if (match(V, m_ImmConstant()))
     return true;
   Value *X;
   if ((match(V, m_ZExtOrSExt(m_Value(X))) || match(V, m_Trunc(m_Value(X)))) &&
@@ -229,7 +226,6 @@ static bool canAlwaysEvaluateInType(Value *V, Type *Ty) {
 /// Filter out values that we can not evaluate in the destination type for free.
 /// This is a helper for canEvaluate*.
 static bool canNotEvaluateInType(Value *V, Type *Ty) {
-  assert(!isa<Constant>(V) && "Constant should already be handled.");
   if (!isa<Instruction>(V))
     return true;
   // We don't extend or shrink something that has multiple uses --  doing so

diff  --git a/llvm/test/ThinLTO/X86/cfi-devirt.ll b/llvm/test/ThinLTO/X86/cfi-devirt.ll
index 7df936c02a3419e..70b0ba2f8faa5ce 100644
--- a/llvm/test/ThinLTO/X86/cfi-devirt.ll
+++ b/llvm/test/ThinLTO/X86/cfi-devirt.ll
@@ -91,7 +91,7 @@ cont2:
   ; CHECK-IR: br i1 {{.*}}, label %trap, label %cont2
 
   ; We still have to call it as virtual.
-  ; CHECK-IR: %call3 = tail call i32 %5
+  ; CHECK-IR: %call3 = tail call i32 %7
   %call3 = tail call i32 %5(ptr nonnull %obj, i32 %call)
   ret i32 %call3
 }

diff  --git a/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll b/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
index bdb210bd26d39cb..442089eecfcbb93 100644
--- a/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
+++ b/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
@@ -22,8 +22,9 @@ define void @test1(ptr %ptr) {
 
 define i64 @OpenFilter(i64 %x) {
 ; CHECK-LABEL: @OpenFilter(
-; CHECK-NEXT:    [[T:%.*]] = sub i64 [[X:%.*]], zext (i8 ptrtoint (ptr @channel_wg4idx to i8) to i64)
-; CHECK-NEXT:    [[R:%.*]] = and i64 [[T]], 255
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 [[X:%.*]] to i8
+; CHECK-NEXT:    [[T:%.*]] = sub i8 [[TMP1]], ptrtoint (ptr @channel_wg4idx to i8)
+; CHECK-NEXT:    [[R:%.*]] = zext i8 [[T]] to i64
 ; CHECK-NEXT:    ret i64 [[R]]
 ;
   %sub = sub i64 %x, ptrtoint (ptr @channel_wg4idx to i64)


        


More information about the llvm-commits mailing list