1; RUN: llc -mtriple=aarch64-linux-gnu -verify-machineinstrs -disable-block-placement -o - %s | FileCheck %s
2; LSR used to pick a sub-optimal solution due to the target responding
3; conservatively to isLegalAddImmediate for negative values.
4
5declare void @foo(i32)
6
7define void @test(i32 %px) {
8; CHECK_LABEL: test:
9; CHECK_LABEL: %entry
10; CHECK: subs [[REG0:w[0-9]+]],
11; CHECK: csel {{w[0-9]+}}, wzr, [[REG0]]
12entry:
13  %sub = add nsw i32 %px, -1
14  %cmp = icmp slt i32 %px, 1
15  %.sub = select i1 %cmp, i32 0, i32 %sub
16  br label %for.body
17
18for.body:
19; CHECK_LABEL: %for.body
20; CHECK:  cmp
21; CHECK-NEXT:  b.eq
22; CHECK-LABEL:  %if.then3
23  %x.015 = phi i32 [ %inc, %for.inc ], [ %.sub, %entry ]
24  %cmp2 = icmp eq i32 %x.015, %px
25  br i1 %cmp2, label %for.inc, label %if.then3
26
27if.then3:
28  tail call void @foo(i32 %x.015)
29  br label %for.inc
30
31for.inc:
32; CHECK_LABEL: %for.inc
33; CHECK:  cmp
34; CHECK-NEXT:  add
35; CHECK-NEXT:  b.le
36; CHECK_LABEL: %for.cond.cleanup
37  %inc = add nsw i32 %x.015, 1
38  %cmp1 = icmp sgt i32 %x.015, %px
39  br i1 %cmp1, label %for.cond.cleanup.loopexit, label %for.body
40
41for.cond.cleanup.loopexit:
42  br label %for.cond.cleanup
43
44for.cond.cleanup:
45  ret void
46}
47