[llvm] [InstCombine] Canonicalise ZextADD + GEP (PR #69818)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 21 06:07:22 PDT 2023
https://github.com/LiqinWeng updated https://github.com/llvm/llvm-project/pull/69818
>From dc7bcce37476d0ba1c89b8e6bba3060970d3c0ca Mon Sep 17 00:00:00 2001
From: LiqinWeng <liqin.weng at spacemit.com>
Date: Sat, 21 Oct 2023 15:58:25 +0800
Subject: [PATCH] [InstCombine] Canonicalise ZextADD + GEP
---
.../InstCombine/InstructionCombining.cpp | 19 +++
llvm/test/Transforms/InstCombine/array.ll | 120 ++++++++++++++++++
2 files changed, 139 insertions(+)
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..36f3f54930e6091 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 ZextIntrinsic = dyn_cast<ZExtInst>(GEP.getOperand(1));
+ if (match(GEP.getOperand(1), m_OneUse(m_ZExt(m_Value(SIdx)))) &&
+ match(ZextIntrinsic->getOperand(0),
+ m_OneUse(m_Add(m_Value(Idx1), m_ConstantInt(C))))) {
+ // %add = add i32 %idx1, C
+ // %zidx = zext i32 %add to i64
+ // %gep = getelementptr i32, i32* %ptr, i64 %zidx
+ // as :
+ // %zidx = zext i32 %idx1 to i64
+ // %newptr = getelementptr i32, i32* %ptr, i64 %zidx
+ // %newgep = getelementptr i32, i32* %newptr, i64 C
+ auto ZIdx1 = Builder.CreateZExt(Idx1, GEP.getOperand(1)->getType());
+ auto *NewPtr = Builder.CreateGEP(GEP.getResultElementType(),
+ GEP.getPointerOperand(), ZIdx1);
+ return GetElementPtrInst::Create(GEP.getResultElementType(), NewPtr, C);
+ }
}
if (!GEP.isInBounds()) {
diff --git a/llvm/test/Transforms/InstCombine/array.ll b/llvm/test/Transforms/InstCombine/array.ll
new file mode 100644
index 000000000000000..beb826199929c89
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/array.ll
@@ -0,0 +1,120 @@
+; 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 @test1(ptr nocapture noundef writeonly %array1, i32 noundef signext %a, i32 noundef signext %b) {
+; CHECK-LABEL: define void @test1(
+; CHECK-SAME: ptr nocapture noundef writeonly [[ARRAY1:%.*]], i32 noundef signext [[A:%.*]], i32 noundef signext [[B:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[ADD:%.*]] = add i32 [[A]], 5
+; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[A]] to i64
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[ARRAY1]], i64 [[TMP0]]
+; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr i32, ptr [[TMP1]], i64 6
+; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
+; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[A]] to i64
+; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i32, ptr [[ARRAY1]], i64 [[TMP2]]
+; CHECK-NEXT: [[ARRAYIDX4:%.*]] = getelementptr i32, ptr [[TMP3]], i64 35
+; CHECK-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX4]], align 4
+; CHECK-NEXT: ret void
+;
+entry:
+ %add = add i32 %a, 5
+ %add1 = add i32 %a, 6
+ %idxprom = zext i32 %add1 to i64
+ %arrayidx = getelementptr inbounds i32, ptr %array1, i64 %idxprom
+ store i32 %b, ptr %arrayidx, align 4
+ %add2 = add i32 %a, 35
+ %idxprom3 = zext i32 %add2 to i64
+ %arrayidx4 = getelementptr inbounds i32, ptr %array1, i64 %idxprom3
+ store i32 %add, ptr %arrayidx4, align 4
+ ret void
+}
+
+define void @test2(ptr nocapture noundef writeonly %array1, i32 noundef signext %a, i32 noundef signext %b) {
+; CHECK-LABEL: define void @test2(
+; CHECK-SAME: ptr nocapture noundef writeonly [[ARRAY1:%.*]], i32 noundef signext [[A:%.*]], i32 noundef signext [[B:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 5
+; CHECK-NEXT: [[ADD1:%.*]] = add nsw i32 [[A]], 6
+; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[ADD1]] to i64
+; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[ARRAY1]], i64 [[IDXPROM]]
+; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
+; CHECK-NEXT: [[ADD2:%.*]] = add nsw i32 [[A]], 35
+; CHECK-NEXT: [[IDXPROM3:%.*]] = sext i32 [[ADD2]] to i64
+; CHECK-NEXT: [[ARRAYIDX4:%.*]] = getelementptr inbounds i32, ptr [[ARRAY1]], i64 [[IDXPROM3]]
+; CHECK-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX4]], align 4
+; CHECK-NEXT: ret void
+;
+entry:
+ %add = add nsw i32 %a, 5
+ %add1 = add nsw i32 %a, 6
+ %idxprom = sext i32 %add1 to i64
+ %arrayidx = getelementptr inbounds i32, ptr %array1, i64 %idxprom
+ store i32 %b, ptr %arrayidx, align 4
+ %add2 = add nsw i32 %a, 35
+ %idxprom3 = sext i32 %add2 to i64
+ %arrayidx4 = getelementptr inbounds i32, ptr %array1, i64 %idxprom3
+ store i32 %add, ptr %arrayidx4, align 4
+ ret void
+}
+
+define void @test3(ptr noundef %array2, i32 noundef signext %a) {
+; CHECK-LABEL: define void @test3(
+; CHECK-SAME: ptr noundef [[ARRAY2:%.*]], i32 noundef signext [[A:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[ADD:%.*]] = add i32 [[A]], 5
+; CHECK-NEXT: [[IDXPROM:%.*]] = zext i32 [[ADD]] to i64
+; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [50 x i32], ptr [[ARRAY2]], i64 [[IDXPROM]], i64 [[IDXPROM]]
+; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
+; CHECK-NEXT: [[ADD3:%.*]] = add i32 [[TMP0]], 1
+; CHECK-NEXT: store i32 [[ADD3]], ptr [[ARRAYIDX2]], align 4
+; CHECK-NEXT: [[TMP1:%.*]] = zext 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 i32 %a, 5
+ %idxprom = zext i32 %add to i64
+ %arrayidx = getelementptr inbounds [50 x i32], ptr %array2, i64 %idxprom
+ %arrayidx2 = getelementptr inbounds [50 x i32], ptr %arrayidx, i64 0, i64 %idxprom
+ %0 = load i32, ptr %arrayidx2, align 4
+ %add3 = add i32 %0, 1
+ store i32 %add3, ptr %arrayidx2, align 4
+ %add4 = add i32 %add, 20
+ %idxprom5 = zext 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
+}
+
+define void @test4(ptr noundef %array2, i32 noundef signext %a) {
+; CHECK-LABEL: define void @test4(
+; CHECK-SAME: ptr noundef [[ARRAY2:%.*]], i32 noundef signext [[A:%.*]]) {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 5
+; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[ADD]] to i64
+; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [50 x i32], ptr [[ARRAY2]], i64 [[IDXPROM]], i64 [[IDXPROM]]
+; 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
+ %arrayidx2 = getelementptr inbounds [50 x i32], ptr %array2, i64 %idxprom, i64 %idxprom
+ %0 = load i32, ptr %arrayidx2, align 4
+ %add3 = add nsw i32 %0, 1
+ store i32 %add3, ptr %arrayidx2, align 4
+ %1 = sext i32 %a to i64
+ %2 = getelementptr [50 x i32], ptr %array2, i64 %1
+ %arrayidx8 = getelementptr [50 x i32], ptr %2, i64 25, i64 %idxprom
+ store i32 %add, ptr %arrayidx8, align 4
+ ret void
+}
More information about the llvm-commits
mailing list