Skip to content

Commit d64d5ea

Browse files
authored
[RISCV][CodeGenPrepare] Remove duplicated transform for zext. NFC. (#72053)
After #71534 and #72052, the transform `zext -> zext nneg` in `RISCVCodeGenPrepare` is redundant.
1 parent 2238363 commit d64d5ea

File tree

4 files changed

+4
-95
lines changed

4 files changed

+4
-95
lines changed

llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ using namespace llvm;
2828
#define DEBUG_TYPE "riscv-codegenprepare"
2929
#define PASS_NAME "RISC-V CodeGenPrepare"
3030

31-
STATISTIC(NumZExtToSExt, "Number of SExt instructions converted to ZExt");
32-
3331
namespace {
3432

3533
class RISCVCodeGenPrepare : public FunctionPass,
@@ -52,49 +50,11 @@ class RISCVCodeGenPrepare : public FunctionPass,
5250
}
5351

5452
bool visitInstruction(Instruction &I) { return false; }
55-
bool visitZExtInst(ZExtInst &I);
5653
bool visitAnd(BinaryOperator &BO);
5754
};
5855

5956
} // end anonymous namespace
6057

61-
bool RISCVCodeGenPrepare::visitZExtInst(ZExtInst &ZExt) {
62-
if (!ST->is64Bit())
63-
return false;
64-
65-
if (ZExt.hasNonNeg())
66-
return false;
67-
68-
Value *Src = ZExt.getOperand(0);
69-
70-
// We only care about ZExt from i32 to i64.
71-
if (!ZExt.getType()->isIntegerTy(64) || !Src->getType()->isIntegerTy(32))
72-
return false;
73-
74-
// Look for an opportunity to infer nneg on a zext if we can determine that
75-
// the sign bit of X is zero via a dominating condition. This often occurs
76-
// with widened induction variables.
77-
if (isImpliedByDomCondition(ICmpInst::ICMP_SGE, Src,
78-
Constant::getNullValue(Src->getType()), &ZExt,
79-
*DL).value_or(false)) {
80-
ZExt.setNonNeg(true);
81-
++NumZExtToSExt;
82-
return true;
83-
}
84-
85-
// Convert (zext (abs(i32 X, i1 1))) -> (zext nneg (abs(i32 X, i1 1))). If abs of
86-
// INT_MIN is poison, the sign bit is zero.
87-
// TODO: Move this to instcombine now that we have zext nneg in IR.
88-
using namespace PatternMatch;
89-
if (match(Src, m_Intrinsic<Intrinsic::abs>(m_Value(), m_One()))) {
90-
ZExt.setNonNeg(true);
91-
++NumZExtToSExt;
92-
return true;
93-
}
94-
95-
return false;
96-
}
97-
9858
// Try to optimize (i64 (and (zext/sext (i32 X), C1))) if C1 has bit 31 set,
9959
// but bits 63:32 are zero. If we know that bit 31 of X is 0, we can fill
10060
// the upper 32 bits with ones.

llvm/test/CodeGen/RISCV/iabs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ define i64 @zext_abs32(i32 %x) {
494494
; RV64ZBB-NEXT: max a0, a0, a1
495495
; RV64ZBB-NEXT: ret
496496
%abs = tail call i32 @llvm.abs.i32(i32 %x, i1 true)
497-
%zext = zext i32 %abs to i64
497+
%zext = zext nneg i32 %abs to i64
498498
ret i64 %zext
499499
}
500500

llvm/test/CodeGen/RISCV/riscv-codegenprepare-asm.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ entry:
2424
br i1 %cmp3, label %for.body.preheader, label %for.cond.cleanup
2525

2626
for.body.preheader: ; preds = %entry
27-
%wide.trip.count = zext i32 %n to i64
27+
%wide.trip.count = zext nneg i32 %n to i64
2828
br label %for.body
2929

3030
for.cond.cleanup: ; preds = %for.body, %entry
@@ -84,7 +84,7 @@ entry:
8484
br i1 %cmp3, label %for.body.preheader, label %for.cond.cleanup
8585

8686
for.body.preheader: ; preds = %entry
87-
%wide.trip.count = zext i32 %n to i64
87+
%wide.trip.count = zext nneg i32 %n to i64
8888
%xtraiter = and i64 %wide.trip.count, 1
8989
%0 = icmp eq i32 %n, 1
9090
br i1 %0, label %for.cond.cleanup.loopexit.unr-lcssa, label %for.body.preheader.new

llvm/test/CodeGen/RISCV/riscv-codegenprepare.ll

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt %s -S -riscv-codegenprepare -mtriple=riscv64 | FileCheck %s
33

4-
; Test that we can convert the %wide.trip.count zext to a sext. The dominating
5-
; condition %cmp3 ruled out %n being negative.
6-
define void @test1(ptr nocapture noundef %a, i32 noundef signext %n) {
7-
; CHECK-LABEL: @test1(
8-
; CHECK-NEXT: entry:
9-
; CHECK-NEXT: [[CMP3:%.*]] = icmp sgt i32 [[N:%.*]], 0
10-
; CHECK-NEXT: br i1 [[CMP3]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_COND_CLEANUP:%.*]]
11-
; CHECK: for.body.preheader:
12-
; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext nneg i32 [[N]] to i64
13-
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
14-
; CHECK: for.cond.cleanup.loopexit:
15-
; CHECK-NEXT: br label [[FOR_COND_CLEANUP]]
16-
; CHECK: for.cond.cleanup:
17-
; CHECK-NEXT: ret void
18-
; CHECK: for.body:
19-
; CHECK-NEXT: [[LSR_IV5:%.*]] = phi i64 [ [[WIDE_TRIP_COUNT]], [[FOR_BODY_PREHEADER]] ], [ [[LSR_IV_NEXT:%.*]], [[FOR_BODY]] ]
20-
; CHECK-NEXT: [[LSR_IV:%.*]] = phi ptr [ [[A:%.*]], [[FOR_BODY_PREHEADER]] ], [ [[UGLYGEP:%.*]], [[FOR_BODY]] ]
21-
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[LSR_IV]], align 4
22-
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP0]], 4
23-
; CHECK-NEXT: store i32 [[ADD]], ptr [[LSR_IV]], align 4
24-
; CHECK-NEXT: [[UGLYGEP]] = getelementptr i8, ptr [[LSR_IV]], i64 4
25-
; CHECK-NEXT: [[LSR_IV_NEXT]] = add nsw i64 [[LSR_IV5]], -1
26-
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[LSR_IV_NEXT]], 0
27-
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_COND_CLEANUP_LOOPEXIT:%.*]], label [[FOR_BODY]]
28-
;
29-
entry:
30-
%cmp3 = icmp sgt i32 %n, 0
31-
br i1 %cmp3, label %for.body.preheader, label %for.cond.cleanup
32-
33-
for.body.preheader: ; preds = %entry
34-
%wide.trip.count = zext i32 %n to i64
35-
br label %for.body
36-
37-
for.cond.cleanup.loopexit: ; preds = %for.body
38-
br label %for.cond.cleanup
39-
40-
for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry
41-
ret void
42-
43-
for.body: ; preds = %for.body.preheader, %for.body
44-
%lsr.iv5 = phi i64 [ %wide.trip.count, %for.body.preheader ], [ %lsr.iv.next, %for.body ]
45-
%lsr.iv = phi ptr [ %a, %for.body.preheader ], [ %uglygep, %for.body ]
46-
%0 = load i32, ptr %lsr.iv, align 4
47-
%add = add nsw i32 %0, 4
48-
store i32 %add, ptr %lsr.iv, align 4
49-
%uglygep = getelementptr i8, ptr %lsr.iv, i64 4
50-
%lsr.iv.next = add nsw i64 %lsr.iv5, -1
51-
%exitcond.not = icmp eq i64 %lsr.iv.next, 0
52-
br i1 %exitcond.not, label %for.cond.cleanup.loopexit, label %for.body
53-
}
54-
554
; Make sure we convert the 4294967294 in for.body.preheader.new to -2 based on
565
; the upper 33 bits being zero by the dominating condition %cmp3.
576
define void @test2(ptr nocapture noundef %a, i32 noundef signext %n) {
@@ -101,7 +50,7 @@ entry:
10150
br i1 %cmp3, label %for.body.preheader, label %for.cond.cleanup
10251

10352
for.body.preheader: ; preds = %entry
104-
%wide.trip.count = zext i32 %n to i64
53+
%wide.trip.count = zext nneg i32 %n to i64
10554
%xtraiter = and i64 %wide.trip.count, 1
10655
%0 = icmp eq i32 %n, 1
10756
br i1 %0, label %for.cond.cleanup.loopexit.unr-lcssa, label %for.body.preheader.new

0 commit comments

Comments
 (0)