1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-02-01 05:01:59 +01:00
Florian Hahn 142a546861 [ConstraintElimination] Add constraint elimination pass.
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
2020-09-15 19:31:11 +01:00

41 lines
1.5 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -constraint-elimination -S %s | FileCheck %s
; Make sure we do not incorrectly add variables to the system.
define i1 @test(i32* %p1, i32* %p2, i32 %num_rows, i32 %start_row, i1 %c) {
; CHECK-LABEL: @test(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[NUM_ROWS:%.*]], [[START_ROW:%.*]]
; CHECK-NEXT: [[L3:%.*]] = load i32, i32* [[P1:%.*]], align 4
; CHECK-NEXT: [[CMP6:%.*]] = icmp ugt i32 [[L3]], [[START_ROW]]
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF_END36:%.*]], label [[IF_END36]]
; CHECK: if.end36:
; CHECK-NEXT: [[L1:%.*]] = load i32, i32* [[P2:%.*]], align 4
; CHECK-NEXT: [[CMP37:%.*]] = icmp ult i32 [[L1]], [[ADD]]
; CHECK-NEXT: br i1 [[CMP37]], label [[IF_THEN39:%.*]], label [[EXIT:%.*]]
; CHECK: if.then39:
; CHECK-NEXT: [[CMP41:%.*]] = icmp ult i32 [[L1]], [[START_ROW]]
; CHECK-NEXT: ret i1 [[CMP41]]
; CHECK: exit:
; CHECK-NEXT: ret i1 false
;
entry:
%add = add i32 %num_rows, %start_row
%l3 = load i32, i32* %p1, align 4
%cmp6 = icmp ugt i32 %l3, %start_row
br i1 %c, label %if.end36, label %if.end36
if.end36: ; preds = %if.then11
%l1 = load i32, i32* %p2, align 4
%cmp37 = icmp ult i32 %l1, %add
br i1 %cmp37, label %if.then39, label %exit
if.then39: ; preds = %if.end36
%cmp41 = icmp ult i32 %l1, %start_row
ret i1 %cmp41
exit: ; preds = %if.end36
ret i1 false
}