Skip to content

Commit d91853e

Browse files
committed
fix: modify lowering
1 parent 422c880 commit d91853e

File tree

3 files changed

+64
-77
lines changed

3 files changed

+64
-77
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9303,27 +9303,20 @@ LegalizerHelper::lowerSADDO_SSUBO(MachineInstr &MI) {
93039303
LegalizerHelper::LegalizeResult LegalizerHelper::lowerSADDE(MachineInstr &MI) {
93049304
auto [Res, OvOut, LHS, RHS, CarryIn] = MI.getFirst5Regs();
93059305
const LLT Ty = MRI.getType(Res);
9306-
const LLT BoolTy = MRI.getType(OvOut);
93079306

9308-
// Step 1: tmp = LHS + RHS
9307+
// sum = LHS + RHS + zext(CarryIn)
93099308
auto Tmp = MIRBuilder.buildAdd(Ty, LHS, RHS);
9309+
auto CarryZ = MIRBuilder.buildZExt(Ty, CarryIn);
9310+
auto Sum = MIRBuilder.buildAdd(Ty, Tmp, CarryZ);
9311+
MIRBuilder.buildCopy(Res, Sum);
93109312

9311-
// ov0 = (tmp < lhs) XOR (rhs < 0)
9312-
auto TmpLtLHS = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, Tmp, LHS);
9313-
auto Zero = MIRBuilder.buildConstant(Ty, 0);
9314-
auto RHSLt0 = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, RHS, Zero);
9315-
auto Ov0 = MIRBuilder.buildXor(BoolTy, TmpLtLHS, RHSLt0);
9316-
9317-
// Step 2: sum = tmp + zext(CarryIn)
9318-
auto CarryInZ = MIRBuilder.buildZExt(Ty, CarryIn);
9319-
MIRBuilder.buildAdd(Res, Tmp, CarryInZ);
9313+
// OvOut = icmp slt ((sum ^ lhs) & (sum ^ rhs)), 0
9314+
auto AX = MIRBuilder.buildXor(Ty, Sum, LHS);
9315+
auto BX = MIRBuilder.buildXor(Ty, Sum, RHS);
9316+
auto T = MIRBuilder.buildAnd(Ty, AX, BX);
93209317

9321-
// ov1 = CarryIn & (sum < tmp)
9322-
auto SumLtTmp = MIRBuilder.buildICmp(CmpInst::ICMP_SLT, BoolTy, Res, Tmp);
9323-
auto Ov1 = MIRBuilder.buildAnd(BoolTy, SumLtTmp, CarryIn);
9324-
9325-
// ov = ov0 | ov1
9326-
MIRBuilder.buildOr(OvOut, Ov0, Ov1);
9318+
auto Zero = MIRBuilder.buildConstant(Ty, 0);
9319+
MIRBuilder.buildICmp(CmpInst::ICMP_SLT, OvOut, T, Zero);
93279320

93289321
MI.eraseFromParent();
93299322
return Legalized;

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-sadde-rv32.mir

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,17 @@ body: |
9898
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(s32) = COPY $x11
9999
; CHECK-NEXT: [[COPY2:%[0-9]+]]:_(s32) = COPY $x12
100100
; CHECK-NEXT: [[ADD:%[0-9]+]]:_(s32) = G_ADD [[COPY]], [[COPY1]]
101-
; CHECK-NEXT: [[ICMP:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[ADD]](s32), [[COPY]]
102-
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
103-
; CHECK-NEXT: [[ICMP1:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[COPY1]](s32), [[C]]
104-
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s32) = G_XOR [[ICMP]], [[ICMP1]]
105-
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
106-
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY2]], [[C1]]
101+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
102+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY2]], [[C]]
107103
; CHECK-NEXT: [[ADD1:%[0-9]+]]:_(s32) = G_ADD [[ADD]], [[AND]]
108-
; CHECK-NEXT: [[ICMP2:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[ADD1]](s32), [[ADD]]
109-
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s32) = G_AND [[ICMP2]], [[COPY2]]
110-
; CHECK-NEXT: [[OR:%[0-9]+]]:_(s32) = G_OR [[XOR]], [[AND1]]
111-
; CHECK-NEXT: $x10 = COPY [[ADD1]](s32)
112-
; CHECK-NEXT: $x11 = COPY [[OR]](s32)
104+
; CHECK-NEXT: [[COPY3:%[0-9]+]]:_(s32) = COPY [[ADD1]](s32)
105+
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s32) = G_XOR [[ADD1]], [[COPY]]
106+
; CHECK-NEXT: [[XOR1:%[0-9]+]]:_(s32) = G_XOR [[ADD1]], [[COPY1]]
107+
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s32) = G_AND [[XOR]], [[XOR1]]
108+
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
109+
; CHECK-NEXT: [[ICMP:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[AND1]](s32), [[C1]]
110+
; CHECK-NEXT: $x10 = COPY [[COPY3]](s32)
111+
; CHECK-NEXT: $x11 = COPY [[ICMP]](s32)
113112
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11
114113
%0:_(s32) = COPY $x10
115114
%1:_(s32) = COPY $x11
@@ -137,27 +136,25 @@ body: |
137136
; CHECK-NEXT: [[COPY3:%[0-9]+]]:_(s32) = COPY $x13
138137
; CHECK-NEXT: [[COPY4:%[0-9]+]]:_(s32) = COPY $x14
139138
; CHECK-NEXT: [[ADD:%[0-9]+]]:_(s32) = G_ADD [[COPY]], [[COPY2]]
140-
; CHECK-NEXT: [[ICMP:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[ADD]](s32), [[COPY]]
141-
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
142-
; CHECK-NEXT: [[ICMP1:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[COPY2]](s32), [[C]]
143-
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s32) = G_XOR [[ICMP]], [[ICMP1]]
144-
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
145-
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY4]], [[C1]]
139+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
140+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY4]], [[C]]
146141
; CHECK-NEXT: [[ADD1:%[0-9]+]]:_(s32) = G_ADD [[ADD]], [[AND]]
147-
; CHECK-NEXT: [[ICMP2:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[ADD1]](s32), [[ADD]]
148-
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s32) = G_AND [[ICMP2]], [[COPY4]]
149-
; CHECK-NEXT: [[OR:%[0-9]+]]:_(s32) = G_OR [[XOR]], [[AND1]]
142+
; CHECK-NEXT: [[COPY5:%[0-9]+]]:_(s32) = COPY [[ADD1]](s32)
143+
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s32) = G_XOR [[ADD1]], [[COPY]]
144+
; CHECK-NEXT: [[XOR1:%[0-9]+]]:_(s32) = G_XOR [[ADD1]], [[COPY2]]
145+
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s32) = G_AND [[XOR]], [[XOR1]]
146+
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
147+
; CHECK-NEXT: [[ICMP:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[AND1]](s32), [[C1]]
150148
; CHECK-NEXT: [[ADD2:%[0-9]+]]:_(s32) = G_ADD [[COPY1]], [[COPY3]]
151-
; CHECK-NEXT: [[ICMP3:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[ADD2]](s32), [[COPY1]]
152-
; CHECK-NEXT: [[ICMP4:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[COPY3]](s32), [[C]]
153-
; CHECK-NEXT: [[XOR1:%[0-9]+]]:_(s32) = G_XOR [[ICMP3]], [[ICMP4]]
154-
; CHECK-NEXT: [[ADD3:%[0-9]+]]:_(s32) = G_ADD [[ADD2]], [[OR]]
155-
; CHECK-NEXT: [[ICMP5:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[ADD3]](s32), [[ADD2]]
156-
; CHECK-NEXT: [[AND2:%[0-9]+]]:_(s32) = G_AND [[ICMP5]], [[OR]]
157-
; CHECK-NEXT: [[OR1:%[0-9]+]]:_(s32) = G_OR [[XOR1]], [[AND2]]
158-
; CHECK-NEXT: $x10 = COPY [[ADD1]](s32)
159-
; CHECK-NEXT: $x11 = COPY [[ADD3]](s32)
160-
; CHECK-NEXT: $x12 = COPY [[OR1]](s32)
149+
; CHECK-NEXT: [[ADD3:%[0-9]+]]:_(s32) = G_ADD [[ADD2]], [[ICMP]]
150+
; CHECK-NEXT: [[COPY6:%[0-9]+]]:_(s32) = COPY [[ADD3]](s32)
151+
; CHECK-NEXT: [[XOR2:%[0-9]+]]:_(s32) = G_XOR [[ADD3]], [[COPY1]]
152+
; CHECK-NEXT: [[XOR3:%[0-9]+]]:_(s32) = G_XOR [[ADD3]], [[COPY3]]
153+
; CHECK-NEXT: [[AND2:%[0-9]+]]:_(s32) = G_AND [[XOR2]], [[XOR3]]
154+
; CHECK-NEXT: [[ICMP1:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[AND2]](s32), [[C1]]
155+
; CHECK-NEXT: $x10 = COPY [[COPY5]](s32)
156+
; CHECK-NEXT: $x11 = COPY [[COPY6]](s32)
157+
; CHECK-NEXT: $x12 = COPY [[ICMP1]](s32)
161158
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11, implicit $x12
162159
%0:_(s32) = COPY $x10
163160
%1:_(s32) = COPY $x11

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-sadde-rv64.mir

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,17 @@ body: |
136136
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(s64) = COPY $x11
137137
; CHECK-NEXT: [[COPY2:%[0-9]+]]:_(s64) = COPY $x12
138138
; CHECK-NEXT: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[COPY]], [[COPY1]]
139-
; CHECK-NEXT: [[ICMP:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[ADD]](s64), [[COPY]]
140-
; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 0
141-
; CHECK-NEXT: [[ICMP1:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[COPY1]](s64), [[C]]
142-
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s64) = G_XOR [[ICMP]], [[ICMP1]]
143-
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
144-
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY2]], [[C1]]
139+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
140+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY2]], [[C]]
145141
; CHECK-NEXT: [[ADD1:%[0-9]+]]:_(s64) = G_ADD [[ADD]], [[AND]]
146-
; CHECK-NEXT: [[ICMP2:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[ADD1]](s64), [[ADD]]
147-
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s64) = G_AND [[ICMP2]], [[COPY2]]
148-
; CHECK-NEXT: [[OR:%[0-9]+]]:_(s64) = G_OR [[XOR]], [[AND1]]
149-
; CHECK-NEXT: $x10 = COPY [[ADD1]](s64)
150-
; CHECK-NEXT: $x11 = COPY [[OR]](s64)
142+
; CHECK-NEXT: [[COPY3:%[0-9]+]]:_(s64) = COPY [[ADD1]](s64)
143+
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s64) = G_XOR [[ADD1]], [[COPY]]
144+
; CHECK-NEXT: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[ADD1]], [[COPY1]]
145+
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s64) = G_AND [[XOR]], [[XOR1]]
146+
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s64) = G_CONSTANT i64 0
147+
; CHECK-NEXT: [[ICMP:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[AND1]](s64), [[C1]]
148+
; CHECK-NEXT: $x10 = COPY [[COPY3]](s64)
149+
; CHECK-NEXT: $x11 = COPY [[ICMP]](s64)
151150
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11
152151
%0:_(s64) = COPY $x10
153152
%1:_(s64) = COPY $x11
@@ -175,27 +174,25 @@ body: |
175174
; CHECK-NEXT: [[COPY3:%[0-9]+]]:_(s64) = COPY $x13
176175
; CHECK-NEXT: [[COPY4:%[0-9]+]]:_(s64) = COPY $x14
177176
; CHECK-NEXT: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[COPY]], [[COPY2]]
178-
; CHECK-NEXT: [[ICMP:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[ADD]](s64), [[COPY]]
179-
; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 0
180-
; CHECK-NEXT: [[ICMP1:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[COPY2]](s64), [[C]]
181-
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s64) = G_XOR [[ICMP]], [[ICMP1]]
182-
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
183-
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY4]], [[C1]]
177+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
178+
; CHECK-NEXT: [[AND:%[0-9]+]]:_(s64) = G_AND [[COPY4]], [[C]]
184179
; CHECK-NEXT: [[ADD1:%[0-9]+]]:_(s64) = G_ADD [[ADD]], [[AND]]
185-
; CHECK-NEXT: [[ICMP2:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[ADD1]](s64), [[ADD]]
186-
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s64) = G_AND [[ICMP2]], [[COPY4]]
187-
; CHECK-NEXT: [[OR:%[0-9]+]]:_(s64) = G_OR [[XOR]], [[AND1]]
180+
; CHECK-NEXT: [[COPY5:%[0-9]+]]:_(s64) = COPY [[ADD1]](s64)
181+
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s64) = G_XOR [[ADD1]], [[COPY]]
182+
; CHECK-NEXT: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[ADD1]], [[COPY2]]
183+
; CHECK-NEXT: [[AND1:%[0-9]+]]:_(s64) = G_AND [[XOR]], [[XOR1]]
184+
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s64) = G_CONSTANT i64 0
185+
; CHECK-NEXT: [[ICMP:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[AND1]](s64), [[C1]]
188186
; CHECK-NEXT: [[ADD2:%[0-9]+]]:_(s64) = G_ADD [[COPY1]], [[COPY3]]
189-
; CHECK-NEXT: [[ICMP3:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[ADD2]](s64), [[COPY1]]
190-
; CHECK-NEXT: [[ICMP4:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[COPY3]](s64), [[C]]
191-
; CHECK-NEXT: [[XOR1:%[0-9]+]]:_(s64) = G_XOR [[ICMP3]], [[ICMP4]]
192-
; CHECK-NEXT: [[ADD3:%[0-9]+]]:_(s64) = G_ADD [[ADD2]], [[OR]]
193-
; CHECK-NEXT: [[ICMP5:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[ADD3]](s64), [[ADD2]]
194-
; CHECK-NEXT: [[AND2:%[0-9]+]]:_(s64) = G_AND [[ICMP5]], [[OR]]
195-
; CHECK-NEXT: [[OR1:%[0-9]+]]:_(s64) = G_OR [[XOR1]], [[AND2]]
196-
; CHECK-NEXT: $x10 = COPY [[ADD1]](s64)
197-
; CHECK-NEXT: $x11 = COPY [[ADD3]](s64)
198-
; CHECK-NEXT: $x12 = COPY [[OR1]](s64)
187+
; CHECK-NEXT: [[ADD3:%[0-9]+]]:_(s64) = G_ADD [[ADD2]], [[ICMP]]
188+
; CHECK-NEXT: [[COPY6:%[0-9]+]]:_(s64) = COPY [[ADD3]](s64)
189+
; CHECK-NEXT: [[XOR2:%[0-9]+]]:_(s64) = G_XOR [[ADD3]], [[COPY1]]
190+
; CHECK-NEXT: [[XOR3:%[0-9]+]]:_(s64) = G_XOR [[ADD3]], [[COPY3]]
191+
; CHECK-NEXT: [[AND2:%[0-9]+]]:_(s64) = G_AND [[XOR2]], [[XOR3]]
192+
; CHECK-NEXT: [[ICMP1:%[0-9]+]]:_(s64) = G_ICMP intpred(slt), [[AND2]](s64), [[C1]]
193+
; CHECK-NEXT: $x10 = COPY [[COPY5]](s64)
194+
; CHECK-NEXT: $x11 = COPY [[COPY6]](s64)
195+
; CHECK-NEXT: $x12 = COPY [[ICMP1]](s64)
199196
; CHECK-NEXT: PseudoRET implicit $x10, implicit $x11, implicit $x12
200197
%0:_(s64) = COPY $x10
201198
%1:_(s64) = COPY $x11

0 commit comments

Comments
 (0)