1; RUN: opt < %s -slsr -nary-reassociate -S | FileCheck %s
2; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s --check-prefix=PTX
3
4target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
5
6; foo((a + b) + c);
7; foo((a + b * 2) + c);
8; foo((a + b * 3) + c);
9;   =>
10; abc = (a + b) + c;
11; foo(abc);
12; ab2c = abc + b;
13; foo(ab2c);
14; ab3c = ab2c + b;
15; foo(ab3c);
16define void @nary_reassociate_after_slsr(i32 %a, i32 %b, i32 %c) {
17; CHECK-LABEL: @nary_reassociate_after_slsr(
18; PTX-LABEL: .visible .func nary_reassociate_after_slsr(
19; PTX: ld.param.u32 [[b:%r[0-9]+]], [nary_reassociate_after_slsr_param_1];
20  %ab = add i32 %a, %b
21  %abc = add i32 %ab, %c
22  call void @foo(i32 %abc)
23; CHECK: call void @foo(i32 %abc)
24; PTX: st.param.b32 [param0+0], [[abc:%r[0-9]+]];
25
26  %b2 = shl i32 %b, 1
27  %ab2 = add i32 %a, %b2
28  %ab2c = add i32 %ab2, %c
29; CHECK-NEXT: %ab2c = add i32 %abc, %b
30; PTX: add.s32 [[ab2c:%r[0-9]+]], [[abc]], [[b]]
31  call void @foo(i32 %ab2c)
32; CHECK-NEXT: call void @foo(i32 %ab2c)
33; PTX: st.param.b32 [param0+0], [[ab2c]];
34
35  %b3 = mul i32 %b, 3
36  %ab3 = add i32 %a, %b3
37  %ab3c = add i32 %ab3, %c
38; CHECK-NEXT: %ab3c = add i32 %ab2c, %b
39; PTX: add.s32 [[ab3c:%r[0-9]+]], [[ab2c]], [[b]]
40  call void @foo(i32 %ab3c)
41; CHECK-NEXT: call void @foo(i32 %ab3c)
42; PTX: st.param.b32 [param0+0], [[ab3c]];
43
44  ret void
45}
46
47declare void @foo(i32)
48