[llvm] [CodeGenPrepare] Replace deleted ext instr with the promoted value. (PR #71058)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 29 08:44:44 PST 2025


https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/71058

>From 17e2fc65ecda87156427706efc4eaf653396380e Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 2 Nov 2023 20:30:26 +0800
Subject: [PATCH 1/6] [CodeGenPrepare] Replace deleted sext instr with the
 promoted value.

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp           | 12 ++++++++++
 .../X86/codegen-prepare-addrmode-sext.ll      | 22 +++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 47486a30bba5ba..6b374b83ba6c3b 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -3074,6 +3074,15 @@ struct ExtAddrMode : public TargetLowering::AddrMode {
   void print(raw_ostream &OS) const;
   void dump() const;
 
+  void replaceWith(Value *From, Value *To) {
+    if (BaseReg == From)
+      BaseReg = To;
+    if (ScaledReg == From)
+      ScaledReg = To;
+    if (OriginalValue == From)
+      OriginalValue = To;
+  }
+
   FieldName compare(const ExtAddrMode &other) {
     // First check that the types are the same on each field, as differing types
     // is something we can't cope with later on.
@@ -5365,6 +5374,9 @@ bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode,
       TPT.rollback(LastKnownGood);
       return false;
     }
+
+    // SExt has been deleted. Make sure it is not referenced by the AddrMode.
+    AddrMode.replaceWith(Ext, PromotedOperand);
     return true;
   }
   case Instruction::Call:
diff --git a/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll b/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
index f3070cd55903bd..85f54e5624e258 100644
--- a/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
+++ b/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
@@ -620,3 +620,25 @@ define i8 @oneArgPromotionBlockSExtZExt(i1 %arg1, ptr %base) {
   %res = load i8, ptr %arrayidx
   ret i8 %res
 }
+
+; Check that we replace the deleted sext with the promoted value.
+; CHECK-LABEL: define void @pr70938(
+; CHECK-SAME: ptr [[F:%.*]]) {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i64 0, 0
+; CHECK-NEXT:    [[SUNKADDR:%.*]] = mul i64 [[ADD]], 2
+; CHECK-NEXT:    [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[F]], i64 [[SUNKADDR]]
+; CHECK-NEXT:    store i8 0, ptr [[SUNKADDR1]], align 1
+; CHECK-NEXT:    ret void
+define void @pr70938(ptr %f) {
+entry:
+  %add = add nsw i32 0, 0
+  %idxprom3 = sext i32 %add to i64
+  %arrayidx4 = getelementptr [2 x [1 x [2 x i8]]], ptr %f, i64 0, i64 %idxprom3
+  %arrayidx8 = getelementptr [2 x i8], ptr %arrayidx4, i64 0, i64 %idxprom3
+  br label %if.end
+
+if.end:                                           ; preds = %entry
+  store i8 0, ptr %arrayidx8, align 1
+  ret void
+}

>From 65eccd284cc814b9b6282429ee5750c7d94f72d0 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 2 Nov 2023 23:35:55 +0800
Subject: [PATCH 2/6] fixup! [CodeGenPrepare] Replace deleted sext instr with
 the promoted value.

---
 llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll b/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
index 85f54e5624e258..6c9334d6cce819 100644
--- a/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
+++ b/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
@@ -625,14 +625,15 @@ define i8 @oneArgPromotionBlockSExtZExt(i1 %arg1, ptr %base) {
 ; CHECK-LABEL: define void @pr70938(
 ; CHECK-SAME: ptr [[F:%.*]]) {
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[ADD:%.*]] = add nsw i64 0, 0
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i64 0, 1
 ; CHECK-NEXT:    [[SUNKADDR:%.*]] = mul i64 [[ADD]], 2
 ; CHECK-NEXT:    [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[F]], i64 [[SUNKADDR]]
-; CHECK-NEXT:    store i8 0, ptr [[SUNKADDR1]], align 1
+; CHECK-NEXT:    [[SUNKADDR2:%.*]] = getelementptr i8, ptr [[SUNKADDR1]], i64 1
+; CHECK-NEXT:    store i8 0, ptr [[SUNKADDR2]], align 1
 ; CHECK-NEXT:    ret void
 define void @pr70938(ptr %f) {
 entry:
-  %add = add nsw i32 0, 0
+  %add = add nsw i32 0, 1
   %idxprom3 = sext i32 %add to i64
   %arrayidx4 = getelementptr [2 x [1 x [2 x i8]]], ptr %f, i64 0, i64 %idxprom3
   %arrayidx8 = getelementptr [2 x i8], ptr %arrayidx4, i64 0, i64 %idxprom3

>From 2ca0d6d3d599a945b80ee1eafa7dc4dc672d11c2 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 4 Nov 2023 02:12:19 +0800
Subject: [PATCH 3/6] fixup! [CodeGenPrepare] Replace deleted sext instr with
 the promoted value.

Add comments for `replaceWith`.
---
 llvm/lib/CodeGen/CodeGenPrepare.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 6b374b83ba6c3b..08ed75cec8214d 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -3074,6 +3074,9 @@ struct ExtAddrMode : public TargetLowering::AddrMode {
   void print(raw_ostream &OS) const;
   void dump() const;
 
+  // Replace From in ExtAddrMode with To.
+  // E.g., SExt insts may be promoted and deleted. We should replace them with
+  // the promoted values.
   void replaceWith(Value *From, Value *To) {
     if (BaseReg == From)
       BaseReg = To;

>From 7c18708c2f267212b76adcbb8b74c8bc845568b3 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 8 Jan 2025 18:35:50 +0800
Subject: [PATCH 4/6] [CodeGenPrepare] Regenerate tests. NFC.

---
 llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll b/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
index 6c9334d6cce819..9f52ac5e3fd6ba 100644
--- a/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
+++ b/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
@@ -622,16 +622,17 @@ define i8 @oneArgPromotionBlockSExtZExt(i1 %arg1, ptr %base) {
 }
 
 ; Check that we replace the deleted sext with the promoted value.
+define void @pr70938(ptr %f) {
 ; CHECK-LABEL: define void @pr70938(
 ; CHECK-SAME: ptr [[F:%.*]]) {
-; CHECK-NEXT:  entry:
+; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    [[ADD:%.*]] = add nsw i64 0, 1
 ; CHECK-NEXT:    [[SUNKADDR:%.*]] = mul i64 [[ADD]], 2
 ; CHECK-NEXT:    [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[F]], i64 [[SUNKADDR]]
 ; CHECK-NEXT:    [[SUNKADDR2:%.*]] = getelementptr i8, ptr [[SUNKADDR1]], i64 1
 ; CHECK-NEXT:    store i8 0, ptr [[SUNKADDR2]], align 1
 ; CHECK-NEXT:    ret void
-define void @pr70938(ptr %f) {
+;
 entry:
   %add = add nsw i32 0, 1
   %idxprom3 = sext i32 %add to i64

>From 2c9970da5de697912fa2eb6a16e65fe1744020c2 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 8 Jan 2025 18:43:51 +0800
Subject: [PATCH 5/6] [CodeGenPrepare] Add more tests. NFC.

---
 .../X86/codegen-prepare-addrmode-sext.ll      | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll b/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
index 9f52ac5e3fd6ba..9755916f823789 100644
--- a/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
+++ b/llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll
@@ -644,3 +644,27 @@ if.end:                                           ; preds = %entry
   store i8 0, ptr %arrayidx8, align 1
   ret void
 }
+
+define void @pr119429() {
+; CHECK-LABEL: define void @pr119429() {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[AND:%.*]] = and i64 0, 0
+; CHECK-NEXT:    [[SUNKADDR:%.*]] = inttoptr i64 [[AND]] to ptr
+; CHECK-NEXT:    [[SUNKADDR1:%.*]] = mul i64 [[AND]], 2
+; CHECK-NEXT:    [[SUNKADDR2:%.*]] = getelementptr i8, ptr [[SUNKADDR]], i64 [[SUNKADDR1]]
+; CHECK-NEXT:    store i64 0, ptr [[SUNKADDR2]], align 8
+; CHECK-NEXT:    ret void
+;
+entry:
+  %and = and i32 0, 0
+  %conv1 = zext i32 %and to i64
+  %sub = add i64 %conv1, 0
+  br label %if.end
+
+if.end:
+  %mul = shl i64 %sub, 1
+  %add = add i64 %mul, %conv1
+  %ptr = inttoptr i64 %add to ptr
+  store i64 0, ptr %ptr, align 8
+  ret void
+}

>From f4be2b6031a4cfaa90952dd10fd5890b516bcaf5 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Thu, 30 Jan 2025 00:44:15 +0800
Subject: [PATCH 6/6] [CodeGenPrepare] Remove uncovered code.

---
 llvm/lib/CodeGen/CodeGenPrepare.cpp | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 08ed75cec8214d..088062afab177d 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -3078,12 +3078,8 @@ struct ExtAddrMode : public TargetLowering::AddrMode {
   // E.g., SExt insts may be promoted and deleted. We should replace them with
   // the promoted values.
   void replaceWith(Value *From, Value *To) {
-    if (BaseReg == From)
-      BaseReg = To;
     if (ScaledReg == From)
       ScaledReg = To;
-    if (OriginalValue == From)
-      OriginalValue = To;
   }
 
   FieldName compare(const ExtAddrMode &other) {



More information about the llvm-commits mailing list