This patch is a first draft of a new pass that adds a more flexible way
to eliminate compares based on more complex constraints collected from
dominating conditions.
In particular, it aims at simplifying conditions of the forms below
using a forward propagation approach, rather than instcomine-style
ad-hoc backwards walking of def-use chains.
if (x < y)
if (y < z)
if (x < z) <- simplify
or
if (x + 2 < y)
if (x + 1 < y) <- simplify assuming no wraps
The general approach is to collect conditions and blocks, sort them by
dominance and then iterate over the sorted list. Conditions are turned
into a linear inequality and add it to a system containing the linear
inequalities that hold on entry to the block. For blocks, we check each
compare against the system and see if it is implied by the constraints
in the system.
We also keep a stack of processed conditions and remove conditions from
the stack and the constraint system once they go out-of-scope (= do not
dominate the current block any longer).
Currently there still are the least the following areas for improvements
* Currently large unsigned constants cannot be added to the system
(coefficients must be represented as integers)
* The way constraints are managed currently is not very optimized.
Reviewed By: spatel
Differential Revision: https://reviews.llvm.org/D84547
137 lines
3.5 KiB
LLVM
137 lines
3.5 KiB
LLVM
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
|
; RUN: opt -constraint-elimination -S %s | FileCheck %s
|
|
|
|
; Test cases where both the true and false successors reach the same block,
|
|
; dominated by one of them.
|
|
|
|
declare void @use(i1)
|
|
|
|
define i32 @test1(i32 %x) {
|
|
; CHECK-LABEL: @test1(
|
|
; CHECK-NEXT: entry:
|
|
; CHECK-NEXT: [[C_1:%.*]] = icmp ule i32 [[X:%.*]], 10
|
|
; CHECK-NEXT: br i1 [[C_1]], label [[BB1:%.*]], label [[BB2:%.*]]
|
|
; CHECK: bb1:
|
|
; CHECK-NEXT: [[C_2:%.*]] = icmp ule i32 [[X]], 10
|
|
; CHECK-NEXT: call void @use(i1 true)
|
|
; CHECK-NEXT: br label [[BB2]]
|
|
; CHECK: bb2:
|
|
; CHECK-NEXT: [[C_3:%.*]] = icmp ugt i32 [[X]], 10
|
|
; CHECK-NEXT: call void @use(i1 [[C_3]])
|
|
; CHECK-NEXT: ret i32 20
|
|
;
|
|
entry:
|
|
%c.1 = icmp ule i32 %x, 10
|
|
br i1 %c.1, label %bb1, label %bb2
|
|
|
|
bb1:
|
|
%c.2 = icmp ule i32 %x, 10
|
|
call void @use(i1 %c.2)
|
|
br label %bb2
|
|
|
|
bb2:
|
|
%c.3 = icmp ugt i32 %x, 10
|
|
call void @use(i1 %c.3)
|
|
ret i32 20
|
|
}
|
|
|
|
|
|
define i32 @test2(i32 %x) {
|
|
; CHECK-LABEL: @test2(
|
|
; CHECK-NEXT: entry:
|
|
; CHECK-NEXT: [[C_1:%.*]] = icmp ule i32 [[X:%.*]], 10
|
|
; CHECK-NEXT: br i1 [[C_1]], label [[BB2:%.*]], label [[BB1:%.*]]
|
|
; CHECK: bb1:
|
|
; CHECK-NEXT: [[C_2:%.*]] = icmp ugt i32 [[X]], 10
|
|
; CHECK-NEXT: call void @use(i1 [[C_2]])
|
|
; CHECK-NEXT: ret i32 20
|
|
; CHECK: bb2:
|
|
; CHECK-NEXT: [[C_3:%.*]] = icmp ule i32 [[X]], 10
|
|
; CHECK-NEXT: call void @use(i1 true)
|
|
; CHECK-NEXT: br label [[BB1]]
|
|
;
|
|
entry:
|
|
%c.1 = icmp ule i32 %x, 10
|
|
br i1 %c.1, label %bb2, label %bb1
|
|
|
|
bb1:
|
|
%c.2 = icmp ugt i32 %x, 10
|
|
call void @use(i1 %c.2)
|
|
ret i32 20
|
|
|
|
bb2:
|
|
%c.3 = icmp ule i32 %x, 10
|
|
call void @use(i1 %c.3)
|
|
br label %bb1
|
|
}
|
|
|
|
|
|
; Test cases where the true/false successors are not domianted by the conditional branching block.
|
|
define i32 @test3(i32 %x, i1 %c) {
|
|
; CHECK-LABEL: @test3(
|
|
; CHECK-NEXT: entry:
|
|
; CHECK-NEXT: br i1 [[C:%.*]], label [[BB_COND:%.*]], label [[BB1:%.*]]
|
|
; CHECK: bb.cond:
|
|
; CHECK-NEXT: [[C_1:%.*]] = icmp ule i32 [[X:%.*]], 10
|
|
; CHECK-NEXT: br i1 [[C_1]], label [[BB1]], label [[BB2:%.*]]
|
|
; CHECK: bb1:
|
|
; CHECK-NEXT: [[C_2:%.*]] = icmp ule i32 [[X]], 10
|
|
; CHECK-NEXT: call void @use(i1 [[C_2]])
|
|
; CHECK-NEXT: ret i32 10
|
|
; CHECK: bb2:
|
|
; CHECK-NEXT: [[C_3:%.*]] = icmp ugt i32 [[X]], 10
|
|
; CHECK-NEXT: call void @use(i1 true)
|
|
; CHECK-NEXT: ret i32 20
|
|
;
|
|
entry:
|
|
br i1 %c, label %bb.cond, label %bb1
|
|
|
|
bb.cond:
|
|
%c.1 = icmp ule i32 %x, 10
|
|
br i1 %c.1, label %bb1, label %bb2
|
|
|
|
bb1:
|
|
%c.2 = icmp ule i32 %x, 10
|
|
call void @use(i1 %c.2)
|
|
ret i32 10
|
|
|
|
bb2:
|
|
%c.3 = icmp ugt i32 %x, 10
|
|
call void @use(i1 %c.3)
|
|
ret i32 20
|
|
}
|
|
|
|
define i32 @test4(i32 %x, i1 %c) {
|
|
; CHECK-LABEL: @test4(
|
|
; CHECK-NEXT: entry:
|
|
; CHECK-NEXT: br i1 [[C:%.*]], label [[BB_COND:%.*]], label [[BB2:%.*]]
|
|
; CHECK: bb.cond:
|
|
; CHECK-NEXT: [[C_1:%.*]] = icmp ule i32 [[X:%.*]], 10
|
|
; CHECK-NEXT: br i1 [[C_1]], label [[BB1:%.*]], label [[BB2]]
|
|
; CHECK: bb1:
|
|
; CHECK-NEXT: [[C_2:%.*]] = icmp ule i32 [[X]], 10
|
|
; CHECK-NEXT: call void @use(i1 true)
|
|
; CHECK-NEXT: ret i32 10
|
|
; CHECK: bb2:
|
|
; CHECK-NEXT: [[C_3:%.*]] = icmp ugt i32 [[X]], 10
|
|
; CHECK-NEXT: call void @use(i1 [[C_3]])
|
|
; CHECK-NEXT: ret i32 20
|
|
;
|
|
entry:
|
|
br i1 %c, label %bb.cond, label %bb2
|
|
|
|
bb.cond:
|
|
%c.1 = icmp ule i32 %x, 10
|
|
br i1 %c.1, label %bb1, label %bb2
|
|
|
|
bb1:
|
|
%c.2 = icmp ule i32 %x, 10
|
|
call void @use(i1 %c.2)
|
|
ret i32 10
|
|
|
|
bb2:
|
|
%c.3 = icmp ugt i32 %x, 10
|
|
call void @use(i1 %c.3)
|
|
ret i32 20
|
|
}
|