[llvm] [LLVM][IR] When evaluting GEP offsets don't assume ConstantInt is a scalar. (PR #117162)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 21 05:39:29 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Paul Walker (paulwalker-arm)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/117162.diff


4 Files Affected:

- (modified) llvm/lib/Analysis/ConstantFolding.cpp (+1-1) 
- (modified) llvm/lib/IR/Operator.cpp (+25-21) 
- (modified) llvm/test/Transforms/InstCombine/gep-vector-indices.ll (+1) 
- (modified) llvm/test/Transforms/InstSimplify/gep.ll (+1) 


``````````diff
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 1971c28fc4c4de..47b96e00c7765a 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -881,7 +881,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
   Type *IntIdxTy = DL.getIndexType(Ptr->getType());
 
   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
-    if (!isa<ConstantInt>(Ops[i]))
+    if (!isa<ConstantInt>(Ops[i]) || !Ops[i]->getType()->isIntegerTy())
       return nullptr;
 
   unsigned BitWidth = DL.getTypeSizeInBits(IntIdxTy);
diff --git a/llvm/lib/IR/Operator.cpp b/llvm/lib/IR/Operator.cpp
index 199eb4d90f5565..522e2ba9e59acf 100644
--- a/llvm/lib/IR/Operator.cpp
+++ b/llvm/lib/IR/Operator.cpp
@@ -127,8 +127,10 @@ bool GEPOperator::accumulateConstantOffset(
   // Fast path for canonical getelementptr i8 form.
   if (SourceType->isIntegerTy(8) && !ExternalAnalysis) {
     if (auto *CI = dyn_cast<ConstantInt>(Index.front())) {
-      Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());
-      return true;
+      if (CI->getType()->isIntegerTy()) {
+        Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());
+        return true;
+      }
     }
     return false;
   }
@@ -163,28 +165,30 @@ bool GEPOperator::accumulateConstantOffset(
     Value *V = GTI.getOperand();
     StructType *STy = GTI.getStructTypeOrNull();
     // Handle ConstantInt if possible.
-    if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
-      if (ConstOffset->isZero())
-        continue;
-      // if the type is scalable and the constant is not zero (vscale * n * 0 =
-      // 0) bailout.
-      if (ScalableType)
-        return false;
-      // Handle a struct index, which adds its field offset to the pointer.
-      if (STy) {
-        unsigned ElementIdx = ConstOffset->getZExtValue();
-        const StructLayout *SL = DL.getStructLayout(STy);
-        // Element offset is in bytes.
-        if (!AccumulateOffset(
-                APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
-                1))
+    if (V->getType()->isIntegerTy()) {
+      if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
+        if (ConstOffset->isZero())
+          continue;
+        // if the type is scalable and the constant is not zero (vscale * n * 0
+        // = 0) bailout.
+        if (ScalableType)
+          return false;
+        // Handle a struct index, which adds its field offset to the pointer.
+        if (STy) {
+          unsigned ElementIdx = ConstOffset->getZExtValue();
+          const StructLayout *SL = DL.getStructLayout(STy);
+          // Element offset is in bytes.
+          if (!AccumulateOffset(
+                  APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)),
+                  1))
+            return false;
+          continue;
+        }
+        if (!AccumulateOffset(ConstOffset->getValue(),
+                              GTI.getSequentialElementStride(DL)))
           return false;
         continue;
       }
-      if (!AccumulateOffset(ConstOffset->getValue(),
-                            GTI.getSequentialElementStride(DL)))
-        return false;
-      continue;
     }
 
     // The operand is not constant, check if an external analysis was provided.
diff --git a/llvm/test/Transforms/InstCombine/gep-vector-indices.ll b/llvm/test/Transforms/InstCombine/gep-vector-indices.ll
index e9534e45ec141d..9f33f4e9c206a0 100644
--- a/llvm/test/Transforms/InstCombine/gep-vector-indices.ll
+++ b/llvm/test/Transforms/InstCombine/gep-vector-indices.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -passes=instcombine %s -S | FileCheck %s
+; RUN: opt -passes=instcombine -use-constant-int-for-fixed-length-splat %s -S | FileCheck %s
 
 define ptr @vector_splat_indices_v2i64_ext0(ptr %a) {
 ; CHECK-LABEL: @vector_splat_indices_v2i64_ext0(
diff --git a/llvm/test/Transforms/InstSimplify/gep.ll b/llvm/test/Transforms/InstSimplify/gep.ll
index b23494fc56aa4e..a330f5cbc92681 100644
--- a/llvm/test/Transforms/InstSimplify/gep.ll
+++ b/llvm/test/Transforms/InstSimplify/gep.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
+; RUN: opt -S -passes=instsimplify -use-constant-int-for-fixed-length-splat < %s | FileCheck %s
 
 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/117162


More information about the llvm-commits mailing list