Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 4c64dfe

Browse files
author
Serguei Katkov
committed
[SimplifyCFG] Re-apply Relax restriction for folding unconditional branches
The commit rL308422 introduces a restriction for folding unconditional branches. Specifically if empty block with unconditional branch leads to header of the loop then elimination of this basic block is prohibited. However it seems this condition is redundantly strict. If elimination of this basic block does not introduce more back edges then we can eliminate this block. The patch implements this relax of restriction. The test profile/Linux/counter_promo_nest.c in compiler-rt project is updated to meet this change. Reviewers: efriedma, mcrosier, pacxx, hsung, davidxl Reviewed By: pacxx Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D42691 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324572 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 62cd19d commit 4c64dfe

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -5733,9 +5733,12 @@ bool SimplifyCFGOpt::SimplifyUncondBranch(BranchInst *BI,
57335733
// header. (This is for early invocations before loop simplify and
57345734
// vectorization to keep canonical loop forms for nested loops. These blocks
57355735
// can be eliminated when the pass is invoked later in the back-end.)
5736+
// Note that if BB has only one predecessor then we do not introduce new
5737+
// backedge, so we can eliminate BB.
57365738
bool NeedCanonicalLoop =
57375739
Options.NeedCanonicalLoop &&
5738-
(LoopHeaders && (LoopHeaders->count(BB) || LoopHeaders->count(Succ)));
5740+
(LoopHeaders && std::distance(pred_begin(BB), pred_end(BB)) > 1 &&
5741+
(LoopHeaders->count(BB) || LoopHeaders->count(Succ)));
57395742
BasicBlock::iterator I = BB->getFirstNonPHIOrDbg()->getIterator();
57405743
if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
57415744
!NeedCanonicalLoop && TryToSimplifyUncondBranchFromEmptyBlock(BB))

test/Transforms/LoopUnroll/peel-loop.ll

+4-9
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
; CHECK: store i32 2, i32* %[[INC2]], align 4
2020
; CHECK: %[[CMP3:.*]] = icmp eq i32 %k, 3
2121
; CHECK: br i1 %[[CMP3]], label %for.end, label %[[LOOP_PH:.*]]
22-
; CHECK: [[LOOP_PH]]:
23-
; CHECK: br label %[[LOOP:.*]]
24-
; CHECK: [[LOOP]]:
25-
; CHECK: %[[IV:.*]] = phi i32 [ 3, %[[LOOP_PH]] ], [ {{.*}}, %[[LOOP]] ]
22+
; CHECK: for.end:
23+
; CHECK: ret void
2624

2725
define void @basic(i32* %p, i32 %k) #0 {
2826
entry:
@@ -68,11 +66,8 @@ for.end: ; preds = %for.cond.for.end_cr
6866
; CHECK: store i32 2, i32* %[[INC2]], align 4
6967
; CHECK: %[[CMP3:.*]] = icmp eq i32 %k, 3
7068
; CHECK: br i1 %[[CMP3]], label %for.end, label %[[LOOP_PH:.*]]
71-
; CHECK: [[LOOP_PH]]:
72-
; CHECK: br label %[[LOOP:.*]]
73-
; CHECK: [[LOOP]]:
74-
; CHECK: %[[IV:.*]] = phi i32 [ 3, %[[LOOP_PH]] ], [ %[[IV:.*]], %[[LOOP]] ]
75-
; CHECK: %ret = phi i32 [ 0, %entry ], [ 1, %[[NEXT0]] ], [ 2, %[[NEXT1]] ], [ 3, %[[NEXT2]] ], [ %[[IV]], %[[LOOP]] ]
69+
; CHECK: for.end:
70+
; CHECK: %ret = phi i32 [ 0, %entry ], [ 1, %[[NEXT0]] ], [ 2, %[[NEXT1]] ], [ 3, %[[NEXT2]] ], [ %inc, %for.body ]
7671
; CHECK: ret i32 %ret
7772
define i32 @output(i32* %p, i32 %k) #0 {
7873
entry:

test/Transforms/LoopUnswitch/2015-06-17-Metadata.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ for.body: ; preds = %for.inc, %for.body.
1616
%cmp1 = icmp eq i32 %a, 12345
1717
br i1 %cmp1, label %if.then, label %if.else, !prof !0
1818
; CHECK: %cmp1 = icmp eq i32 %a, 12345
19-
; CHECK-NEXT: br i1 %cmp1, label %for.body.preheader.split.us, label %for.body.preheader.split, !prof !0
19+
; CHECK-NEXT: br i1 %cmp1, label %for.body.us, label %for.body, !prof !0
2020
if.then: ; preds = %for.body
2121
; CHECK: for.body.us:
2222
; CHECK: add nsw i32 %{{.*}}, 123
@@ -53,7 +53,7 @@ entry:
5353
br label %for.body
5454
;CHECK: entry:
5555
;CHECK-NEXT: %cmp1 = icmp eq i32 1, 2
56-
;CHECK-NEXT: br i1 %cmp1, label %entry.split, label %for.cond.cleanup.split, !prof !1
56+
;CHECK-NEXT: br i1 %cmp1, label %for.body, label %for.cond.cleanup.split, !prof !1
5757
;CHECK: for.body:
5858
for.body: ; preds = %for.inc, %entry
5959
%inc.i = phi i32 [ 0, %entry ], [ %inc, %if.then ]

test/Transforms/LoopUnswitch/infinite-loop.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
; CHECK-NEXT: br i1 %a, label %entry.split, label %abort0.split
1717

1818
; CHECK: entry.split:
19-
; CHECK-NEXT: br i1 %b, label %entry.split.split, label %abort1.split
19+
; CHECK-NEXT: br i1 %b, label %for.body, label %abort1.split
2020

2121
; CHECK: for.body:
2222
; CHECK-NEXT: br label %for.body
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt < %s -simplifycfg -S | FileCheck %s
2+
3+
; Check that we can get rid of empty block leading to header
4+
; if it does not introduce new edge.
5+
define i32 @test(i32 %c) {
6+
entry:
7+
br label %header
8+
header:
9+
%i = phi i32 [0, %entry], [%i.1, %backedge]
10+
%i.1 = add i32 %i, 1
11+
%cmp = icmp slt i32 %i.1, %c
12+
br i1 %cmp, label %backedge, label %exit
13+
; CHECK-NOT: backedge:
14+
backedge:
15+
br label %header
16+
exit:
17+
ret i32 %i
18+
}

0 commit comments

Comments
 (0)