llvm/test/Transforms/SimplifyCFG/multiple-phis.ll
Balaram Makam 788841cb66 [SimplifyCFG] Defer folding unconditional branches to LateSimplifyCFG if it can destroy canonical loop structure.
Summary:
When simplifying unconditional branches from empty blocks, we pre-test if the
BB belongs to a set of loop headers and keep the block to prevent passes from
destroying canonical loop structure. However, the current algorithm fails if
the destination of the branch is a loop header. Especially when such a loop's
latch block is folded into loop header it results in additional backedges and
LoopSimplify turns it into a nested loop which prevent later optimizations
from being applied (e.g., loop  unrolling and loop interleaving).

This patch augments the existing algorithm by further checking if the
destination of the branch belongs to a set of loop headers and defer
eliminating it if yes to LateSimplifyCFG.

Fixes PR33605: https://bugs.llvm.org/show_bug.cgi?id=33605

Reviewers: efriedma, mcrosier, pacxx, hsung, davidxl

Reviewed By: efriedma

Subscribers: ashutosh.nema, gberry, javed.absar, llvm-commits

Differential Revision: https://reviews.llvm.org/D35411

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308422 91177308-0d34-0410-b5e6-96231b3b80d8
2017-07-19 08:53:34 +00:00

40 lines
1.5 KiB
LLVM

; RUN: opt -latesimplifycfg -S < %s | FileCheck %s
; It's not worthwhile to if-convert one of the phi nodes and leave
; the other behind, because that still requires a branch. If
; SimplifyCFG if-converts one of the phis, it should do both.
; CHECK: %div.high.addr.0 = select i1 %cmp1, i32 %div, i32 %high.addr.0
; CHECK-NEXT: %low.0.add2 = select i1 %cmp1, i32 %low.0, i32 %add2
; CHECK-NEXT: br label %while.cond
define i32 @upper_bound(i32* %r, i32 %high, i32 %k) nounwind {
entry:
br label %while.cond
while.cond: ; preds = %if.then, %if.else, %entry
%high.addr.0 = phi i32 [ %high, %entry ], [ %div, %if.then ], [ %high.addr.0, %if.else ]
%low.0 = phi i32 [ 0, %entry ], [ %low.0, %if.then ], [ %add2, %if.else ]
%cmp = icmp ult i32 %low.0, %high.addr.0
br i1 %cmp, label %while.body, label %while.end
while.body: ; preds = %while.cond
%add = add i32 %low.0, %high.addr.0
%div = udiv i32 %add, 2
%idxprom = zext i32 %div to i64
%arrayidx = getelementptr inbounds i32, i32* %r, i64 %idxprom
%0 = load i32, i32* %arrayidx
%cmp1 = icmp ult i32 %k, %0
br i1 %cmp1, label %if.then, label %if.else
if.then: ; preds = %while.body
br label %while.cond
if.else: ; preds = %while.body
%add2 = add i32 %div, 1
br label %while.cond
while.end: ; preds = %while.cond
ret i32 %low.0
}