Skip to content

Commit bd39569

Browse files
Petar AvramovicPetar Avramovic
Petar Avramovic
authored and
Petar Avramovic
committedFeb 26, 2019
[MIPS GlobalISel] Select G_UADDO
Lower G_UADDO. Legalize G_UADDO for MIPS32 Differential Revision: https://reviews.llvm.org/D58671 llvm-svn: 354900
1 parent 4f171d2 commit bd39569

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed
 

Diff for: ‎llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,18 @@ LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
15451545
case TargetOpcode::G_CTTZ:
15461546
case TargetOpcode::G_CTPOP:
15471547
return lowerBitCount(MI, TypeIdx, Ty);
1548+
case G_UADDO: {
1549+
unsigned Res = MI.getOperand(0).getReg();
1550+
unsigned CarryOut = MI.getOperand(1).getReg();
1551+
unsigned LHS = MI.getOperand(2).getReg();
1552+
unsigned RHS = MI.getOperand(3).getReg();
1553+
1554+
MIRBuilder.buildAdd(Res, LHS, RHS);
1555+
MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, RHS);
1556+
1557+
MI.eraseFromParent();
1558+
return Legalized;
1559+
}
15481560
case G_UADDE: {
15491561
unsigned Res = MI.getOperand(0).getReg();
15501562
unsigned CarryOut = MI.getOperand(1).getReg();

Diff for: ‎llvm/lib/Target/Mips/MipsLegalizerInfo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ MipsLegalizerInfo::MipsLegalizerInfo(const MipsSubtarget &ST) {
3232
.legalFor({s32})
3333
.minScalar(0, s32);
3434

35-
getActionDefinitionsBuilder({G_UADDE, G_USUBO, G_USUBE})
35+
getActionDefinitionsBuilder({G_UADDO, G_UADDE, G_USUBO, G_USUBE})
3636
.lowerFor({{s32, s1}});
3737

3838
getActionDefinitionsBuilder({G_LOAD, G_STORE})

Diff for: ‎llvm/test/CodeGen/Mips/GlobalISel/legalizer/add.mir

+33
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
define void @add_i16_aext() {entry: ret void}
1212
define void @add_i64() {entry: ret void}
1313
define void @add_i128() {entry: ret void}
14+
define void @uadd_with_overflow(i32 %lhs, i32 %rhs, i32* %padd, i1* %pcarry_flag) { ret void }
1415

1516
...
1617
---
@@ -334,3 +335,35 @@ body: |
334335
RetRA implicit $v0, implicit $v1, implicit $a0, implicit $a1
335336
336337
...
338+
---
339+
name: uadd_with_overflow
340+
alignment: 2
341+
tracksRegLiveness: true
342+
body: |
343+
bb.1 (%ir-block.0):
344+
liveins: $a0, $a1, $a2, $a3
345+
346+
; MIPS32-LABEL: name: uadd_with_overflow
347+
; MIPS32: liveins: $a0, $a1, $a2, $a3
348+
; MIPS32: [[COPY:%[0-9]+]]:_(s32) = COPY $a0
349+
; MIPS32: [[COPY1:%[0-9]+]]:_(s32) = COPY $a1
350+
; MIPS32: [[COPY2:%[0-9]+]]:_(p0) = COPY $a2
351+
; MIPS32: [[COPY3:%[0-9]+]]:_(p0) = COPY $a3
352+
; MIPS32: [[ADD:%[0-9]+]]:_(s32) = G_ADD [[COPY]], [[COPY1]]
353+
; MIPS32: [[ICMP:%[0-9]+]]:_(s32) = G_ICMP intpred(ult), [[ADD]](s32), [[COPY1]]
354+
; MIPS32: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
355+
; MIPS32: [[COPY4:%[0-9]+]]:_(s32) = COPY [[ICMP]](s32)
356+
; MIPS32: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY4]], [[C]]
357+
; MIPS32: G_STORE [[AND]](s32), [[COPY3]](p0) :: (store 1 into %ir.pcarry_flag)
358+
; MIPS32: G_STORE [[ADD]](s32), [[COPY2]](p0) :: (store 4 into %ir.padd)
359+
; MIPS32: RetRA
360+
%0:_(s32) = COPY $a0
361+
%1:_(s32) = COPY $a1
362+
%2:_(p0) = COPY $a2
363+
%3:_(p0) = COPY $a3
364+
%4:_(s32), %5:_(s1) = G_UADDO %0, %1
365+
G_STORE %5(s1), %3(p0) :: (store 1 into %ir.pcarry_flag)
366+
G_STORE %4(s32), %2(p0) :: (store 4 into %ir.padd)
367+
RetRA
368+
369+
...

Diff for: ‎llvm/test/CodeGen/Mips/GlobalISel/llvm-ir/add.ll

+21
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,24 @@ entry:
160160
%add = add i128 %b, %a
161161
ret i128 %add
162162
}
163+
164+
declare { i32, i1 } @llvm.uadd.with.overflow.i32(i32, i32)
165+
define void @uadd_with_overflow(i32 %lhs, i32 %rhs, i32* %padd, i1* %pcarry_flag) {
166+
; MIPS32-LABEL: uadd_with_overflow:
167+
; MIPS32: # %bb.0:
168+
; MIPS32-NEXT: addu $4, $4, $5
169+
; MIPS32-NEXT: sltu $5, $4, $5
170+
; MIPS32-NEXT: lui $1, 0
171+
; MIPS32-NEXT: ori $1, $1, 1
172+
; MIPS32-NEXT: and $1, $5, $1
173+
; MIPS32-NEXT: sb $1, 0($7)
174+
; MIPS32-NEXT: sw $4, 0($6)
175+
; MIPS32-NEXT: jr $ra
176+
; MIPS32-NEXT: nop
177+
%res = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %lhs, i32 %rhs)
178+
%carry_flag = extractvalue { i32, i1 } %res, 1
179+
%add = extractvalue { i32, i1 } %res, 0
180+
store i1 %carry_flag, i1* %pcarry_flag
181+
store i32 %add, i32* %padd
182+
ret void
183+
}

0 commit comments

Comments
 (0)
Please sign in to comment.