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

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 4 04:45:34 PST 2024


Author: Paul Walker
Date: 2024-12-04T12:45:30Z
New Revision: a88653a2cd4f22ff2ac4cb25214caf3e5fd27aff

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

LOG: [LLVM][IR] When evaluating GEP offsets don't assume ConstantInt is a scalar. (#117162)

Added: 
    

Modified: 
    llvm/lib/Analysis/ConstantFolding.cpp
    llvm/lib/IR/Operator.cpp
    llvm/test/Transforms/GVN/opaque-ptr.ll
    llvm/test/Transforms/InstCombine/gep-vector-indices.ll
    llvm/test/Transforms/InstSimplify/gep.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 1971c28fc4c4def..47b96e00c7765ae 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 e1580cf76096992..740ad4a8c0a3cd3 100644
--- a/llvm/lib/IR/Operator.cpp
+++ b/llvm/lib/IR/Operator.cpp
@@ -126,7 +126,8 @@ bool GEPOperator::accumulateConstantOffset(
     APInt &Offset, function_ref<bool(Value &, APInt &)> ExternalAnalysis) {
   // Fast path for canonical getelementptr i8 form.
   if (SourceType->isIntegerTy(8) && !ExternalAnalysis) {
-    if (auto *CI = dyn_cast<ConstantInt>(Index.front())) {
+    auto *CI = dyn_cast<ConstantInt>(Index.front());
+    if (CI && CI->getType()->isIntegerTy()) {
       Offset += CI->getValue().sextOrTrunc(Offset.getBitWidth());
       return true;
     }
@@ -165,7 +166,8 @@ bool GEPOperator::accumulateConstantOffset(
     Value *V = GTI.getOperand();
     StructType *STy = GTI.getStructTypeOrNull();
     // Handle ConstantInt if possible.
-    if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
+    auto *ConstOffset = dyn_cast<ConstantInt>(V);
+    if (ConstOffset && ConstOffset->getType()->isIntegerTy()) {
       if (ConstOffset->isZero())
         continue;
       // if the type is scalable and the constant is not zero (vscale * n * 0 =
@@ -226,7 +228,8 @@ bool GEPOperator::collectOffset(
     Value *V = GTI.getOperand();
     StructType *STy = GTI.getStructTypeOrNull();
     // Handle ConstantInt if possible.
-    if (auto ConstOffset = dyn_cast<ConstantInt>(V)) {
+    auto *ConstOffset = dyn_cast<ConstantInt>(V);
+    if (ConstOffset && ConstOffset->getType()->isIntegerTy()) {
       if (ConstOffset->isZero())
         continue;
       // If the type is scalable and the constant is not zero (vscale * n * 0 =

diff  --git a/llvm/test/Transforms/GVN/opaque-ptr.ll b/llvm/test/Transforms/GVN/opaque-ptr.ll
index 4a0f9d319501c88..8a7e42058485989 100644
--- a/llvm/test/Transforms/GVN/opaque-ptr.ll
+++ b/llvm/test/Transforms/GVN/opaque-ptr.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -passes=gvn < %s | FileCheck %s
+; RUN: opt -S -passes=gvn -use-constant-int-for-fixed-length-splat < %s | FileCheck %s
 
 declare void @use(ptr)
 declare void @use.i32(i32)
@@ -58,6 +59,10 @@ define void @gep_cse_offset_canonicalization(ptr %p, i64 %idx, i64 %idx2) {
 ; CHECK-NEXT:    call void @use(ptr [[GEP6]])
 ; CHECK-NEXT:    call void @use(ptr [[GEP6_SAME]])
 ; CHECK-NEXT:    call void @use(ptr [[GEP6_DIFFERENT]])
+; CHECK-NEXT:    %gep7 = getelementptr <16 x i32>, ptr %p, i64 1
+; CHECK-NEXT:    %gep7.
diff erent = getelementptr <16 x i32>, ptr %p, <16 x i64> splat (i64 1)
+; CHECK-NEXT:    call void @use(ptr %gep7)
+; CHECK-NEXT:    call void @use(<16 x ptr> %gep7.
diff erent)
 ; CHECK-NEXT:    ret void
 ;
   %gep1 = getelementptr i64, ptr %p, i64 1
@@ -101,6 +106,10 @@ define void @gep_cse_offset_canonicalization(ptr %p, i64 %idx, i64 %idx2) {
   call void @use(ptr %gep6)
   call void @use(ptr %gep6.same)
   call void @use(ptr %gep6.
diff erent)
+  %gep7 = getelementptr <16 x i32>, ptr %p, i64 1
+  %gep7.
diff erent = getelementptr <16 x i32>, ptr %p, <16 x i64> splat (i64 1)
+  call void @use(ptr %gep7)
+  call void @use(<16 x ptr> %gep7.
diff erent)
   ret void
 }
 

diff  --git a/llvm/test/Transforms/InstCombine/gep-vector-indices.ll b/llvm/test/Transforms/InstCombine/gep-vector-indices.ll
index e9534e45ec141d9..9f33f4e9c206a0d 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 b23494fc56aa4ea..a330f5cbc926819 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"
 


        


More information about the llvm-commits mailing list