1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
4 ; Here we can remove range check because:
5 ; length >= min_length (from entry)
6 ; idx < min_length (from in_bounds check)
7 ; therefore, idx < length is trivially true.
9 define i32 @test_01(ptr %p, ptr %array, i32 %min_length) {
10 ; CHECK-LABEL: @test_01(
12 ; CHECK-NEXT: br label [[LOOP:%.*]]
14 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
15 ; CHECK-NEXT: [[ELEM_PTR:%.*]] = getelementptr i32, ptr [[P:%.*]], i32 [[IV]]
16 ; CHECK-NEXT: [[IDX:%.*]] = load i32, ptr [[ELEM_PTR]], align 4
17 ; CHECK-NEXT: [[IN_BOUNDS:%.*]] = icmp ult i32 [[IDX]], [[MIN_LENGTH:%.*]]
18 ; CHECK-NEXT: br i1 [[IN_BOUNDS]], label [[RANGE_CHECK_BLOCK:%.*]], label [[OUT_OF_BOUNDS:%.*]]
19 ; CHECK: range_check_block:
20 ; CHECK-NEXT: br i1 true, label [[BACKEDGE]], label [[RANGE_CHECK_FAILED:%.*]]
22 ; CHECK-NEXT: [[ARR_PTR:%.*]] = getelementptr i32, ptr [[ARRAY:%.*]], i32 [[IDX]]
23 ; CHECK-NEXT: store i32 [[IV]], ptr [[ARR_PTR]], align 4
24 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
25 ; CHECK-NEXT: [[LOOP_COND:%.*]] = call i1 @cond()
26 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
28 ; CHECK-NEXT: [[IV_LCSSA:%.*]] = phi i32 [ [[IV]], [[BACKEDGE]] ]
29 ; CHECK-NEXT: ret i32 [[IV_LCSSA]]
30 ; CHECK: out_of_bounds:
31 ; CHECK-NEXT: ret i32 -1
32 ; CHECK: range_check_failed:
33 ; CHECK-NEXT: [[IV_LCSSA_RC:%.*]] = phi i32 [ [[IV]], [[RANGE_CHECK_BLOCK]] ]
34 ; CHECK-NEXT: call void @failed_range_check(i32 [[IV_LCSSA_RC]])
35 ; CHECK-NEXT: unreachable
40 loop: ; preds = %backedge, %entry
41 %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
42 %elem_ptr = getelementptr i32, ptr %p, i32 %iv
43 %idx = load i32, ptr %elem_ptr, align 4
44 %in_bounds = icmp ult i32 %idx, %min_length
45 br i1 %in_bounds, label %range_check_block, label %out_of_bounds
47 range_check_block: ; preds = %loop
48 %range_check = icmp ult i32 %idx, %min_length
49 br i1 %range_check, label %backedge, label %range_check_failed
51 backedge: ; preds = %range_check_block
52 %arr_ptr = getelementptr i32, ptr %array, i32 %idx
53 store i32 %iv, ptr %arr_ptr, align 4
54 %iv.next = add i32 %iv, 1
55 %loop_cond = call i1 @cond()
56 br i1 %loop_cond, label %loop, label %exit
58 exit: ; preds = %backedge
59 %iv.lcssa = phi i32 [ %iv, %backedge ]
62 out_of_bounds: ; preds = %loop
65 range_check_failed: ; preds = %range_check_block
66 %iv.lcssa.rc = phi i32 [ %iv, %range_check_block ]
67 call void @failed_range_check(i32 %iv.lcssa.rc)
71 define i32 @test_02(ptr %p, ptr %array, i32 %length, i32 %min_length) {
72 ; CHECK-LABEL: @test_02(
74 ; CHECK-NEXT: [[LENGTH_CHECK:%.*]] = icmp uge i32 [[LENGTH:%.*]], [[MIN_LENGTH:%.*]]
75 ; CHECK-NEXT: br i1 [[LENGTH_CHECK]], label [[LOOP:%.*]], label [[FAILED:%.*]]
77 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
78 ; CHECK-NEXT: [[ELEM_PTR:%.*]] = getelementptr i32, ptr [[P:%.*]], i32 [[IV]]
79 ; CHECK-NEXT: [[IDX:%.*]] = load i32, ptr [[ELEM_PTR]], align 4
80 ; CHECK-NEXT: [[IN_BOUNDS:%.*]] = icmp ult i32 [[IDX]], [[MIN_LENGTH]]
81 ; CHECK-NEXT: br i1 [[IN_BOUNDS]], label [[RANGE_CHECK_BLOCK:%.*]], label [[OUT_OF_BOUNDS:%.*]]
82 ; CHECK: range_check_block:
83 ; CHECK-NEXT: br i1 true, label [[BACKEDGE]], label [[RANGE_CHECK_FAILED:%.*]]
85 ; CHECK-NEXT: [[ARR_PTR:%.*]] = getelementptr i32, ptr [[ARRAY:%.*]], i32 [[IDX]]
86 ; CHECK-NEXT: store i32 [[IV]], ptr [[ARR_PTR]], align 4
87 ; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
88 ; CHECK-NEXT: [[LOOP_COND:%.*]] = call i1 @cond()
89 ; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
91 ; CHECK-NEXT: [[IV_LCSSA:%.*]] = phi i32 [ [[IV]], [[BACKEDGE]] ]
92 ; CHECK-NEXT: ret i32 [[IV_LCSSA]]
94 ; CHECK-NEXT: unreachable
95 ; CHECK: out_of_bounds:
96 ; CHECK-NEXT: ret i32 -1
97 ; CHECK: range_check_failed:
98 ; CHECK-NEXT: [[IV_LCSSA_RC:%.*]] = phi i32 [ [[IV]], [[RANGE_CHECK_BLOCK]] ]
99 ; CHECK-NEXT: call void @failed_range_check(i32 [[IV_LCSSA_RC]])
100 ; CHECK-NEXT: unreachable
103 %length_check = icmp uge i32 %length, %min_length
104 br i1 %length_check, label %loop, label %failed
106 loop: ; preds = %backedge, %entry
107 %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
108 %elem_ptr = getelementptr i32, ptr %p, i32 %iv
109 %idx = load i32, ptr %elem_ptr, align 4
110 %in_bounds = icmp ult i32 %idx, %min_length
111 br i1 %in_bounds, label %range_check_block, label %out_of_bounds
113 range_check_block: ; preds = %loop
114 %range_check = icmp ult i32 %idx, %length
115 br i1 %range_check, label %backedge, label %range_check_failed
117 backedge: ; preds = %range_check_block
118 %arr_ptr = getelementptr i32, ptr %array, i32 %idx
119 store i32 %iv, ptr %arr_ptr, align 4
120 %iv.next = add i32 %iv, 1
121 %loop_cond = call i1 @cond()
122 br i1 %loop_cond, label %loop, label %exit
124 exit: ; preds = %backedge
125 %iv.lcssa = phi i32 [ %iv, %backedge ]
128 failed: ; preds = %entry
131 out_of_bounds: ; preds = %loop
134 range_check_failed: ; preds = %range_check_block
135 %iv.lcssa.rc = phi i32 [ %iv, %range_check_block ]
136 call void @failed_range_check(i32 %iv.lcssa.rc)
142 declare void @failed_range_check(i32)