Files
clang-p2996/llvm/test/Transforms/ConstraintElimination/loops.ll
Florian Hahn 3d42d54955 [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

48 lines
2.4 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -constraint-elimination -S %s | FileCheck %s
; Make sure conditions in loops are not used to simplify themselves.
define void @loop1(float* %T, float* %x, i32 %points, i32 %trigint) {
; CHECK-LABEL: @loop1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[IDX_EXT:%.*]] = sext i32 [[POINTS:%.*]] to i64
; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds float, float* [[X:%.*]], i64 [[IDX_EXT]]
; CHECK-NEXT: [[ADD_PTR1:%.*]] = getelementptr inbounds float, float* [[ADD_PTR]], i64 -8
; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[POINTS]], 1
; CHECK-NEXT: [[IDX_EXT2:%.*]] = sext i32 [[SHR]] to i64
; CHECK-NEXT: [[ADD_PTR3:%.*]] = getelementptr inbounds float, float* [[X]], i64 [[IDX_EXT2]]
; CHECK-NEXT: [[ADD_PTR4:%.*]] = getelementptr inbounds float, float* [[ADD_PTR3]], i64 -8
; CHECK-NEXT: br label [[DO_BODY:%.*]]
; CHECK: do.body:
; CHECK-NEXT: [[X2_0:%.*]] = phi float* [ [[ADD_PTR4]], [[ENTRY:%.*]] ], [ [[ADD_PTR106:%.*]], [[DO_BODY]] ]
; CHECK-NEXT: [[X1_0:%.*]] = phi float* [ [[ADD_PTR1]], [[ENTRY]] ], [ [[ADD_PTR105:%.*]], [[DO_BODY]] ]
; CHECK-NEXT: [[ADD_PTR105]] = getelementptr inbounds float, float* [[X1_0]], i64 -8
; CHECK-NEXT: [[ADD_PTR106]] = getelementptr inbounds float, float* [[X2_0]], i64 -8
; CHECK-NEXT: [[CMP:%.*]] = icmp uge float* [[ADD_PTR106]], [[X]]
; CHECK-NEXT: br i1 [[CMP]], label [[DO_BODY]], label [[DO_END:%.*]]
; CHECK: do.end:
; CHECK-NEXT: ret void
;
entry:
%idx.ext = sext i32 %points to i64
%add.ptr = getelementptr inbounds float, float* %x, i64 %idx.ext
%add.ptr1 = getelementptr inbounds float, float* %add.ptr, i64 -8
%shr = ashr i32 %points, 1
%idx.ext2 = sext i32 %shr to i64
%add.ptr3 = getelementptr inbounds float, float* %x, i64 %idx.ext2
%add.ptr4 = getelementptr inbounds float, float* %add.ptr3, i64 -8
br label %do.body
do.body: ; preds = %do.body, %entry
%x2.0 = phi float* [ %add.ptr4, %entry ], [ %add.ptr106, %do.body ]
%x1.0 = phi float* [ %add.ptr1, %entry ], [ %add.ptr105, %do.body ]
%add.ptr105 = getelementptr inbounds float, float* %x1.0, i64 -8
%add.ptr106 = getelementptr inbounds float, float* %x2.0, i64 -8
%cmp = icmp uge float* %add.ptr106, %x
br i1 %cmp, label %do.body, label %do.end
do.end: ; preds = %do.body
ret void
}