xref: /llvm-project/llvm/test/CodeGen/RISCV/addcarry.ll (revision 9122c5235ec85ce0c0ad337e862b006e7b349d84)
1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2; RUN: llc < %s -mtriple=riscv32 -mattr=+m | FileCheck %s --check-prefix=RISCV32
3
4; Test ADDCARRY node expansion on a target that does not currently support ADDCARRY.
5; Signed fixed point multiplication eventually expands down to an ADDCARRY.
6
7declare  i64 @llvm.smul.fix.i64  (i64, i64, i32)
8declare { i32, i1 } @llvm.uadd.with.overflow.i32(i32, i32)
9
10define i64 @addcarry(i64 %x, i64 %y) nounwind {
11; RISCV32-LABEL: addcarry:
12; RISCV32:       # %bb.0:
13; RISCV32-NEXT:    mul a4, a0, a3
14; RISCV32-NEXT:    mulhu a5, a0, a2
15; RISCV32-NEXT:    mul a6, a1, a2
16; RISCV32-NEXT:    mulhu a7, a0, a3
17; RISCV32-NEXT:    mulhu t0, a1, a2
18; RISCV32-NEXT:    add t1, a5, a4
19; RISCV32-NEXT:    add a7, a7, t0
20; RISCV32-NEXT:    add a4, t1, a6
21; RISCV32-NEXT:    sltu a5, t1, a5
22; RISCV32-NEXT:    sltu a6, a4, t1
23; RISCV32-NEXT:    add a5, a7, a5
24; RISCV32-NEXT:    add a5, a5, a6
25; RISCV32-NEXT:    mul a6, a1, a3
26; RISCV32-NEXT:    add a5, a5, a6
27; RISCV32-NEXT:    bgez a1, .LBB0_2
28; RISCV32-NEXT:  # %bb.1:
29; RISCV32-NEXT:    sub a5, a5, a2
30; RISCV32-NEXT:  .LBB0_2:
31; RISCV32-NEXT:    bgez a3, .LBB0_4
32; RISCV32-NEXT:  # %bb.3:
33; RISCV32-NEXT:    sub a5, a5, a0
34; RISCV32-NEXT:  .LBB0_4:
35; RISCV32-NEXT:    slli a5, a5, 30
36; RISCV32-NEXT:    srli a1, a4, 2
37; RISCV32-NEXT:    slli a4, a4, 30
38; RISCV32-NEXT:    mul a0, a0, a2
39; RISCV32-NEXT:    or a1, a5, a1
40; RISCV32-NEXT:    srli a0, a0, 2
41; RISCV32-NEXT:    or a0, a4, a0
42; RISCV32-NEXT:    ret
43  %tmp = call i64 @llvm.smul.fix.i64(i64 %x, i64 %y, i32 2);
44  ret i64 %tmp;
45}
46
47; negative test for combineCarryDiamond(): ADDCARRY not legal
48define { i32, i32, i1 } @addcarry_2x32(i32 %x0, i32 %x1, i32 %y0, i32 %y1) nounwind {
49; RISCV32-LABEL: addcarry_2x32:
50; RISCV32:       # %bb.0:
51; RISCV32-NEXT:    add a3, a1, a3
52; RISCV32-NEXT:    add a4, a2, a4
53; RISCV32-NEXT:    sltu a1, a3, a1
54; RISCV32-NEXT:    sltu a2, a4, a2
55; RISCV32-NEXT:    add a1, a4, a1
56; RISCV32-NEXT:    sltu a4, a1, a4
57; RISCV32-NEXT:    or a2, a2, a4
58; RISCV32-NEXT:    sw a3, 0(a0)
59; RISCV32-NEXT:    sw a1, 4(a0)
60; RISCV32-NEXT:    sb a2, 8(a0)
61; RISCV32-NEXT:    ret
62  %t0 = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x0, i32 %y0)
63  %s0 = extractvalue { i32, i1 } %t0, 0
64  %k0 = extractvalue { i32, i1 } %t0, 1
65
66  %t1 = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x1, i32 %y1)
67  %s1 = extractvalue { i32, i1 } %t1, 0
68  %k1 = extractvalue { i32, i1 } %t1, 1
69
70  %zk0 = zext i1 %k0 to i32
71  %t2 = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %s1, i32 %zk0)
72  %s2 = extractvalue { i32, i1 } %t2, 0
73  %k2 = extractvalue { i32, i1 } %t2, 1
74  %k = or i1 %k1, %k2
75
76  %r0 = insertvalue { i32, i32, i1 } poison, i32 %s0, 0
77  %r1 = insertvalue { i32, i32, i1 } %r0, i32 %s2, 1
78  %r = insertvalue { i32, i32, i1 } %r1, i1 %k, 2
79  ret { i32, i32, i1 } %r
80}
81