[polly] [InstCombine] Canonicalise SextADD + GEP (PR #69581)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 20 06:20:41 PDT 2023


https://github.com/LiqinWeng updated https://github.com/llvm/llvm-project/pull/69581

>From feee18a13f246a9f281c39ea29025b4053d53e30 Mon Sep 17 00:00:00 2001
From: LiqinWeng <liqin.weng at spacemit.com>
Date: Thu, 19 Oct 2023 17:12:44 +0800
Subject: [PATCH] [InstCombine] Canonicalise SextADD + GEP

---
 .../InstCombine/InstructionCombining.cpp      | 19 ++++++++++
 llvm/test/CodeGen/AMDGPU/amdgpu-inline.ll     |  2 +-
 llvm/test/Transforms/InstCombine/array.ll     | 38 +++++++++++++++++++
 polly/test/Support/dumpmodule.ll              | 20 +++++-----
 4 files changed, 68 insertions(+), 11 deletions(-)
 create mode 100644 llvm/test/Transforms/InstCombine/array.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 559eb2ef4795eb1..9309952b33a36b8 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2335,6 +2335,25 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
       return GetElementPtrInst::Create(GEP.getResultElementType(), NewPtr,
                                        Idx2);
     }
+
+    Value *SIdx;
+    ConstantInt *C;
+    auto SextIntrinsic = dyn_cast<SExtInst>(GEP.getOperand(1));
+    if (match(GEP.getOperand(1), m_OneUse(m_SExt(m_Value(SIdx)))) &&
+        match(SextIntrinsic->getOperand(0),
+              m_OneUse(m_Add(m_Value(Idx1), m_ConstantInt(C))))) {
+      // %add1 = add nsw i32 %idx, C
+      // %sidx = sext i32 %add1 to i64
+      // %gep = getelementptr inbounds [50 x i32], ptr %array2, i64 %sidx
+      // as :
+      // %sidx = sext i32 %idx to i64
+      // %newptr = getelementptr inbounds [50 x i32], ptr %array2, i64 %sidx
+      // %newgep = getelementptr i32, i32* %newptr, i64 C
+      auto SIdx1 = Builder.CreateSExt(Idx1, GEP.getOperand(1)->getType());
+      auto *NewPtr = Builder.CreateGEP(GEP.getResultElementType(),
+                                       GEP.getPointerOperand(), SIdx1);
+      return GetElementPtrInst::Create(GEP.getResultElementType(), NewPtr, C);
+    }
   }
 
   if (!GEP.isInBounds()) {
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-inline.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-inline.ll
index b34df3ffca26420..ae934678bbec729 100644
--- a/llvm/test/CodeGen/AMDGPU/amdgpu-inline.ll
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-inline.ll
@@ -61,7 +61,7 @@ entry:
 
 ; GCN: define amdgpu_kernel void @test_inliner(
 ; GCN-INL1:     %c1 = tail call coldcc float @foo(
-; GCN-INLDEF:   %cmp.i = fcmp ogt float %tmp2, 0.000000e+00
+; GCN-INLDEF:   %cmp.i = fcmp ogt float %2, 0.000000e+00
 ; GCN-MAXBBDEF: %div.i{{[0-9]*}} = fdiv float 1.000000e+00, %c
 ; GCN-MAXBBDEF: %div.i{{[0-9]*}} = fdiv float 2.000000e+00, %tmp1.i
 ; GCN-MAXBB1:   call coldcc void @foo_private_ptr
diff --git a/llvm/test/Transforms/InstCombine/array.ll b/llvm/test/Transforms/InstCombine/array.ll
new file mode 100644
index 000000000000000..bf768d1f8d9173c
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/array.ll
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define void @test(ptr noundef %array2, i32 noundef signext %a, i32 noundef signext %b) {
+; CHECK-LABEL: define void @test(
+; CHECK-SAME: ptr noundef [[ARRAY2:%.*]], i32 noundef signext [[A:%.*]], i32 noundef signext [[B:%.*]]) {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 [[A]], 5
+; CHECK-NEXT:    [[IDXPROM:%.*]] = sext i32 [[ADD]] to i64
+; CHECK-NEXT:    [[SUB:%.*]] = add nsw i32 [[A]], 4
+; CHECK-NEXT:    [[IDXPROM1:%.*]] = sext i32 [[SUB]] to i64
+; CHECK-NEXT:    [[ARRAYIDX2:%.*]] = getelementptr inbounds [50 x i32], ptr [[ARRAY2]], i64 [[IDXPROM]], i64 [[IDXPROM1]]
+; CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
+; CHECK-NEXT:    [[ADD3:%.*]] = add nsw i32 [[TMP0]], 1
+; CHECK-NEXT:    store i32 [[ADD3]], ptr [[ARRAYIDX2]], align 4
+; CHECK-NEXT:    [[TMP1:%.*]] = sext i32 [[A]] to i64
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr [50 x i32], ptr [[ARRAY2]], i64 [[TMP1]]
+; CHECK-NEXT:    [[ARRAYIDX8:%.*]] = getelementptr [50 x i32], ptr [[TMP2]], i64 25, i64 [[IDXPROM]]
+; CHECK-NEXT:    store i32 [[ADD]], ptr [[ARRAYIDX8]], align 4
+; CHECK-NEXT:    ret void
+;
+entry:
+  %add = add nsw i32 %a, 5
+  %idxprom = sext i32 %add to i64
+  %arrayidx = getelementptr inbounds [50 x i32], ptr %array2, i64 %idxprom
+  %sub = sub nsw i32 %add, 1
+  %idxprom1 = sext i32 %sub to i64
+  %arrayidx2 = getelementptr inbounds [50 x i32], ptr %arrayidx, i64 0, i64 %idxprom1
+  %0 = load i32, ptr %arrayidx2, align 4
+  %add3 = add nsw i32 %0, 1
+  store i32 %add3, ptr %arrayidx2, align 4
+  %add4 = add nsw i32 %add, 20
+  %idxprom5 = sext i32 %add4 to i64
+  %arrayidx6 = getelementptr inbounds [50 x i32], ptr %array2, i64 %idxprom5
+  %arrayidx8 = getelementptr inbounds [50 x i32], ptr %arrayidx6, i64 0, i64 %idxprom
+  store i32 %add, ptr %arrayidx8, align 4
+  ret void
+}
diff --git a/polly/test/Support/dumpmodule.ll b/polly/test/Support/dumpmodule.ll
index 693fe4bc6cde3ea..34fe1d75f4420b2 100644
--- a/polly/test/Support/dumpmodule.ll
+++ b/polly/test/Support/dumpmodule.ll
@@ -1,3 +1,4 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
 ; RUN: opt %loadNPMPolly -O3 -polly -polly-position=early             -polly-dump-before-file=%t-npm-before-early.ll    --disable-output < %s && FileCheck --input-file=%t-npm-before-early.ll    --check-prefix=EARLY %s
 ; RUN: opt %loadNPMPolly -O3 -polly -polly-position=early             -polly-dump-after-file=%t-npm-after-early.ll      --disable-output < %s && FileCheck --input-file=%t-npm-after-early.ll     --check-prefix=EARLY --check-prefix=AFTEREARLY %s
 ;
@@ -24,11 +25,11 @@ for:
   %j.cmp = icmp slt i32 %j, %n
   br i1 %j.cmp, label %body, label %exit
 
-    body:
-      %idx = add i32 %i, %j
-      %arrayidx = getelementptr inbounds double, ptr %A, i32 %idx
-      store double 42.0, ptr %arrayidx
-      br label %inc
+  body:
+  %idx = add i32 %i, %j
+  %arrayidx = getelementptr inbounds double, ptr %A, i32 %idx
+  store double 42.0, ptr %arrayidx
+  br label %inc
 
 inc:
   %j.inc = add nuw nsw i32 %j, 1
@@ -51,9 +52,9 @@ for:
   %i.cmp = icmp slt i32 %i, %n
   br i1 %i.cmp, label %body, label %exit
 
-    body:
-      call void @callee(i32 %n, ptr %A, i32 %i)
-      br label %inc
+  body:
+  call void @callee(i32 %n, ptr %A, i32 %i)
+  br label %inc
 
 inc:
   %j.inc = add nuw nsw i32 %i, 1
@@ -68,8 +69,7 @@ return:
 
 
 ; EARLY-LABEL: @callee(
-; AFTEREARLY:  polly.split_new_and_old:
-; EARLY:         store double 4.200000e+01, ptr %arrayidx
+; AFTEREARLY:  polly.stmt.body:
 ; EARLY-LABEL: @caller(
 ; EARLY:         call void @callee(
 



More information about the llvm-commits mailing list