[llvm] af50d6e - [SeparateConstOffsetFromGEP] Avoid use of ConstantExpr::getCast()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 2 01:39:57 PDT 2023


Author: Nikita Popov
Date: 2023-11-02T09:39:48+01:00
New Revision: af50d6efc92dc9c9bf581aacb3086cb3c3a4f329

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

LOG: [SeparateConstOffsetFromGEP] Avoid use of ConstantExpr::getCast()

Try to constant fold, otherwise fall back to generating an
instruction.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
index ba4ecf8b8853e17..b8c9d9d100f117a 100644
--- a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -661,15 +661,16 @@ Value *ConstantOffsetExtractor::applyExts(Value *V) {
   // in the reversed order.
   for (CastInst *I : llvm::reverse(ExtInsts)) {
     if (Constant *C = dyn_cast<Constant>(Current)) {
-      // If Current is a constant, apply s/zext using ConstantExpr::getCast.
-      // ConstantExpr::getCast emits a ConstantInt if C is a ConstantInt.
-      Current = ConstantExpr::getCast(I->getOpcode(), C, I->getType());
-    } else {
-      Instruction *Ext = I->clone();
-      Ext->setOperand(0, Current);
-      Ext->insertBefore(IP);
-      Current = Ext;
+      // Try to constant fold the cast.
+      Current = ConstantFoldCastOperand(I->getOpcode(), C, I->getType(), DL);
+      if (Current)
+        continue;
     }
+
+    Instruction *Ext = I->clone();
+    Ext->setOperand(0, Current);
+    Ext->insertBefore(IP);
+    Current = Ext;
   }
   return Current;
 }


        


More information about the llvm-commits mailing list