mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-18 10:31:57 +00:00
148e8c9b8b
IRCE eliminates range checks of the form 0 <= A * I + B < Length by splitting a loop's iteration space into three segments in a way that the check is completely redundant in the middle segment. As an example, IRCE will convert len = < known positive > for (i = 0; i < n; i++) { if (0 <= i && i < len) { do_something(); } else { throw_out_of_bounds(); } } to len = < known positive > limit = smin(n, len) // no first segment for (i = 0; i < limit; i++) { if (0 <= i && i < len) { // this check is fully redundant do_something(); } else { throw_out_of_bounds(); } } for (i = limit; i < n; i++) { if (0 <= i && i < len) { do_something(); } else { throw_out_of_bounds(); } } IRCE can deal with multiple range checks in the same loop (it takes the intersection of the ranges that will make each of them redundant individually). Currently IRCE does not do any profitability analysis. That is a TODO. Please note that the status of this pass is *experimental*, and it is not part of any default pass pipeline. Having said that, I will love to get feedback and general input from people interested in trying this out. This pass was originally r226201. It was reverted because it used C++ features not supported by MSVC 2012. Differential Revision: http://reviews.llvm.org/D6693 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226238 91177308-0d34-0410-b5e6-96231b3b80d8
111 lines
3.6 KiB
LLVM
111 lines
3.6 KiB
LLVM
; RUN: opt -irce -S < %s | FileCheck %s
|
|
|
|
define void @single_access_no_preloop_no_offset(i32 *%arr, i32 *%a_len_ptr, i32 %n) {
|
|
entry:
|
|
%len = load i32* %a_len_ptr, !range !0
|
|
%first.itr.check = icmp sgt i32 %n, 0
|
|
br i1 %first.itr.check, label %loop, label %exit
|
|
|
|
loop:
|
|
%idx = phi i32 [ 0, %entry ] , [ %idx.next, %in.bounds ]
|
|
%idx.next = add i32 %idx, 1
|
|
%abc = icmp slt i32 %idx, %len
|
|
br i1 %abc, label %in.bounds, label %out.of.bounds
|
|
|
|
in.bounds:
|
|
%addr = getelementptr i32* %arr, i32 %idx
|
|
store i32 0, i32* %addr
|
|
%next = icmp slt i32 %idx.next, %n
|
|
br i1 %next, label %loop, label %exit
|
|
|
|
out.of.bounds:
|
|
ret void
|
|
|
|
exit:
|
|
ret void
|
|
}
|
|
|
|
; CHECK-LABEL: single_access_no_preloop
|
|
|
|
; CHECK-LABEL: loop:
|
|
; CHECK: br i1 true, label %in.bounds, label %out.of.bounds
|
|
|
|
; CHECK-LABEL: main.exit.selector:
|
|
; CHECK-NEXT: [[continue:%[^ ]+]] = icmp slt i32 %idx.next, %n
|
|
; CHECK-NEXT: br i1 [[continue]], label %main.pseudo.exit, label %exit.loopexit
|
|
|
|
; CHECK-LABEL: main.pseudo.exit:
|
|
; CHECK-NEXT: %idx.copy = phi i32 [ 0, %loop.preheader ], [ %idx.next, %main.exit.selector ]
|
|
; CHECK-NEXT: br label %postloop
|
|
|
|
; CHECK-LABEL: postloop:
|
|
; CHECK-NEXT: br label %loop.postloop
|
|
|
|
; CHECK-LABEL: loop.postloop:
|
|
; CHECK-NEXT: %idx.postloop = phi i32 [ %idx.next.postloop, %in.bounds.postloop ], [ %idx.copy, %postloop ]
|
|
; CHECK-NEXT: %idx.next.postloop = add i32 %idx.postloop, 1
|
|
; CHECK-NEXT: %abc.postloop = icmp slt i32 %idx.postloop, %len
|
|
; CHECK-NEXT: br i1 %abc.postloop, label %in.bounds.postloop, label %out.of.bounds
|
|
|
|
; CHECK-LABEL: in.bounds.postloop:
|
|
; CHECK-NEXT: %addr.postloop = getelementptr i32* %arr, i32 %idx.postloop
|
|
; CHECK-NEXT: store i32 0, i32* %addr.postloop
|
|
; CHECK-NEXT: %next.postloop = icmp slt i32 %idx.next.postloop, %n
|
|
; CHECK-NEXT: br i1 %next.postloop, label %loop.postloop, label %exit.loopexit
|
|
|
|
|
|
define void @single_access_no_preloop_with_offset(i32 *%arr, i32 *%a_len_ptr, i32 %n) {
|
|
entry:
|
|
%len = load i32* %a_len_ptr, !range !0
|
|
%first.itr.check = icmp sgt i32 %n, 0
|
|
br i1 %first.itr.check, label %loop, label %exit
|
|
|
|
loop:
|
|
%idx = phi i32 [ 0, %entry ] , [ %idx.next, %in.bounds ]
|
|
%idx.next = add i32 %idx, 1
|
|
%idx.for.abc = add i32 %idx, 4
|
|
%abc = icmp slt i32 %idx.for.abc, %len
|
|
br i1 %abc, label %in.bounds, label %out.of.bounds
|
|
|
|
in.bounds:
|
|
%addr = getelementptr i32* %arr, i32 %idx.for.abc
|
|
store i32 0, i32* %addr
|
|
%next = icmp slt i32 %idx.next, %n
|
|
br i1 %next, label %loop, label %exit
|
|
|
|
out.of.bounds:
|
|
ret void
|
|
|
|
exit:
|
|
ret void
|
|
}
|
|
|
|
; CHECK-LABEL: single_access_no_preloop_with_offset
|
|
|
|
; CHECK-LABEL: loop.preheader:
|
|
; CHECK: [[safe_range_end:[^ ]+]] = sub i32 %len, 4
|
|
; CHECK: [[exit_main_loop_at_cmp:[^ ]+]] = icmp slt i32 %n, [[safe_range_end]]
|
|
; CHECK: [[exit_main_loop_at:[^ ]+]] = select i1 [[exit_main_loop_at_cmp]], i32 %n, i32 [[safe_range_end]]
|
|
; CHECK: [[enter_main_loop:[^ ]+]] = icmp slt i32 0, [[exit_main_loop_at]]
|
|
; CHECK: br i1 [[enter_main_loop]], label %loop, label %main.pseudo.exit
|
|
|
|
; CHECK-LABEL: loop:
|
|
; CHECK: br i1 true, label %in.bounds, label %out.of.bounds
|
|
|
|
; CHECK-LABEL: in.bounds:
|
|
; CHECK: [[continue_main_loop:[^ ]+]] = icmp slt i32 %idx.next, [[exit_main_loop_at]]
|
|
; CHECK: br i1 [[continue_main_loop]], label %loop, label %main.exit.selector
|
|
|
|
; CHECK-LABEL: main.pseudo.exit:
|
|
; CHECK: %idx.copy = phi i32 [ 0, %loop.preheader ], [ %idx.next, %main.exit.selector ]
|
|
; CHECK: br label %postloop
|
|
|
|
; CHECK-LABEL: loop.postloop:
|
|
; CHECK: %idx.postloop = phi i32 [ %idx.next.postloop, %in.bounds.postloop ], [ %idx.copy, %postloop ]
|
|
|
|
; CHECK-LABEL: in.bounds.postloop:
|
|
; CHECK: %next.postloop = icmp slt i32 %idx.next.postloop, %n
|
|
; CHECK: br i1 %next.postloop, label %loop.postloop, label %exit.loopexit
|
|
|
|
!0 = !{i32 0, i32 2147483647}
|