Skip to content

Commit d5e2d00

Browse files
sjoerdmeijeraokblast
authored andcommitted
[LoopInterchange] Add simplifyLCSSAPhis: remove phi from non-exit bb (llvm#160889)
This deals with a corner case of LCSSA phi nodes in the outer loop latch block: the loop was in LCSSA form, some transformations can come along (e.g. unswitch) and create an empty block: BB4: br label %BB5 BB5: %old.cond.lcssa = phi i16 [ %cond, %BB4 ] br outer.header Interchange then brings it in LCSSA form again and we get: BB4: %new.cond.lcssa = phi i16 [ %cond, %BB3 ] br label %BB5 BB5: %old.cond.lcssa = phi i16 [ %new.cond.lcssa, %BB4 ] Which means that we have a chain of LCSSA phi nodes from %new.cond.lcssa to %old.cond.lcssa. The problem is that interchange can reoder blocks BB4 and BB5 placing the use before the def if we don't check this. The solution is to simplify lcssa phis, and remove them from non-exit blocks if they are 1-input phi nodes. Fixes llvm#160068
1 parent 73dad4e commit d5e2d00

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "llvm/Support/raw_ostream.h"
4444
#include "llvm/Transforms/Scalar/LoopPassManager.h"
4545
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
46+
#include "llvm/Transforms/Utils/Local.h"
4647
#include "llvm/Transforms/Utils/LoopUtils.h"
4748
#include <cassert>
4849
#include <utility>
@@ -1872,6 +1873,51 @@ static void moveLCSSAPhis(BasicBlock *InnerExit, BasicBlock *InnerHeader,
18721873
InnerLatch->replacePhiUsesWith(InnerLatch, OuterLatch);
18731874
}
18741875

1876+
/// This deals with a corner case when a LCSSA phi node appears in a non-exit
1877+
/// block: the outer loop latch block does not need to be exit block of the
1878+
/// inner loop. Consider a loop that was in LCSSA form, but then some
1879+
/// transformation like loop-unswitch comes along and creates an empty block,
1880+
/// where BB5 in this example is the outer loop latch block:
1881+
///
1882+
/// BB4:
1883+
/// br label %BB5
1884+
/// BB5:
1885+
/// %old.cond.lcssa = phi i16 [ %cond, %BB4 ]
1886+
/// br outer.header
1887+
///
1888+
/// Interchange then brings it in LCSSA form again resulting in this chain of
1889+
/// single-input phi nodes:
1890+
///
1891+
/// BB4:
1892+
/// %new.cond.lcssa = phi i16 [ %cond, %BB3 ]
1893+
/// br label %BB5
1894+
/// BB5:
1895+
/// %old.cond.lcssa = phi i16 [ %new.cond.lcssa, %BB4 ]
1896+
///
1897+
/// The problem is that interchange can reoder blocks BB4 and BB5 placing the
1898+
/// use before the def if we don't check this. The solution is to simplify
1899+
/// lcssa phi nodes (remove) if they appear in non-exit blocks.
1900+
///
1901+
static void simplifyLCSSAPhis(Loop *OuterLoop, Loop *InnerLoop) {
1902+
BasicBlock *InnerLoopExit = InnerLoop->getExitBlock();
1903+
BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
1904+
1905+
// Do not modify lcssa phis where they actually belong, i.e. in exit blocks.
1906+
if (OuterLoopLatch == InnerLoopExit)
1907+
return;
1908+
1909+
// Collect and remove phis in non-exit blocks if they have 1 input.
1910+
SmallVector<PHINode *, 8> Phis(
1911+
llvm::make_pointer_range(OuterLoopLatch->phis()));
1912+
for (PHINode *Phi : Phis) {
1913+
assert(Phi->getNumIncomingValues() == 1 && "Single input phi expected");
1914+
LLVM_DEBUG(dbgs() << "Removing 1-input phi in non-exit block: " << *Phi
1915+
<< "\n");
1916+
Phi->replaceAllUsesWith(Phi->getIncomingValue(0));
1917+
Phi->eraseFromParent();
1918+
}
1919+
}
1920+
18751921
bool LoopInterchangeTransform::adjustLoopBranches() {
18761922
LLVM_DEBUG(dbgs() << "adjustLoopBranches called\n");
18771923
std::vector<DominatorTree::UpdateType> DTUpdates;
@@ -1882,6 +1928,9 @@ bool LoopInterchangeTransform::adjustLoopBranches() {
18821928
assert(OuterLoopPreHeader != OuterLoop->getHeader() &&
18831929
InnerLoopPreHeader != InnerLoop->getHeader() && OuterLoopPreHeader &&
18841930
InnerLoopPreHeader && "Guaranteed by loop-simplify form");
1931+
1932+
simplifyLCSSAPhis(OuterLoop, InnerLoop);
1933+
18851934
// Ensure that both preheaders do not contain PHI nodes and have single
18861935
// predecessors. This allows us to move them easily. We use
18871936
// InsertPreHeaderForLoop to create an 'extra' preheader, if the existing
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
2+
; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa -S | FileCheck %s
3+
4+
; This test is checking that blocks outer.body and outer.latch, where outer.body is the exit
5+
; block of the inner loop and outer.latch the latch of the outer loop, correctly
6+
; deal with the phi-node use-def chain %new.cond.lcssa -> %old.cond.lcssa. What we expect
7+
; here is that block outer.latch does not contain a phi node, because it is a single input
8+
; phi in a non-exit block.
9+
10+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
11+
12+
define i16 @main(ptr %a) {
13+
; CHECK-LABEL: define i16 @main(
14+
; CHECK-SAME: ptr [[A:%.*]]) {
15+
; CHECK-NEXT: [[ENTRY:.*:]]
16+
; CHECK-NEXT: br label %[[INNER_HEADER_PREHEADER:.*]]
17+
; CHECK: [[OUTER_HEADER_PREHEADER:.*]]:
18+
; CHECK-NEXT: br label %[[OUTER_HEADER:.*]]
19+
; CHECK: [[OUTER_HEADER]]:
20+
; CHECK-NEXT: [[I:%.*]] = phi i64 [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ], [ 1, %[[OUTER_HEADER_PREHEADER]] ]
21+
; CHECK-NEXT: br label %[[INNER_HEADER_SPLIT:.*]]
22+
; CHECK: [[INNER_HEADER_PREHEADER]]:
23+
; CHECK-NEXT: br label %[[INNER_HEADER:.*]]
24+
; CHECK: [[INNER_HEADER]]:
25+
; CHECK-NEXT: [[J:%.*]] = phi i16 [ [[TMP1:%.*]], %[[INNER_LATCH_SPLIT:.*]] ], [ 0, %[[INNER_HEADER_PREHEADER]] ]
26+
; CHECK-NEXT: br label %[[OUTER_HEADER_PREHEADER]]
27+
; CHECK: [[INNER_HEADER_SPLIT]]:
28+
; CHECK-NEXT: [[ARRAYIDX_US_US:%.*]] = getelementptr i16, ptr [[A]], i16 [[J]]
29+
; CHECK-NEXT: [[TMP0:%.*]] = load i16, ptr [[ARRAYIDX_US_US]], align 1
30+
; CHECK-NEXT: [[COND:%.*]] = select i1 false, i16 0, i16 0
31+
; CHECK-NEXT: br label %[[INNER_LATCH:.*]]
32+
; CHECK: [[INNER_LATCH]]:
33+
; CHECK-NEXT: [[J_NEXT:%.*]] = add i16 [[J]], 1
34+
; CHECK-NEXT: br label %[[OUTER_BODY:.*]]
35+
; CHECK: [[INNER_LATCH_SPLIT]]:
36+
; CHECK-NEXT: [[NEW_COND_LCSSA:%.*]] = phi i16 [ [[COND]], %[[OUTER_LATCH]] ]
37+
; CHECK-NEXT: [[TMP1]] = add i16 [[J]], 1
38+
; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[INNER_HEADER]]
39+
; CHECK: [[OUTER_BODY]]:
40+
; CHECK-NEXT: br label %[[OUTER_LATCH]]
41+
; CHECK: [[OUTER_LATCH]]:
42+
; CHECK-NEXT: [[I_NEXT]] = add i64 [[I]], 1
43+
; CHECK-NEXT: [[CMP286_US:%.*]] = icmp ugt i64 [[I]], 0
44+
; CHECK-NEXT: br i1 [[CMP286_US]], label %[[OUTER_HEADER]], label %[[INNER_LATCH_SPLIT]]
45+
; CHECK: [[EXIT]]:
46+
; CHECK-NEXT: [[OLD_COND_LCSSA_LCSSA:%.*]] = phi i16 [ [[NEW_COND_LCSSA]], %[[INNER_LATCH_SPLIT]] ]
47+
; CHECK-NEXT: ret i16 [[OLD_COND_LCSSA_LCSSA]]
48+
;
49+
entry:
50+
br label %outer.header
51+
52+
outer.header:
53+
%i = phi i64 [ 1, %entry ], [ %i.next, %outer.latch ]
54+
br label %inner.header
55+
56+
inner.header:
57+
%j = phi i16 [ 0, %outer.header ], [ %j.next, %inner.latch ]
58+
%arrayidx.us.us = getelementptr i16, ptr %a, i16 %j
59+
%0 = load i16, ptr %arrayidx.us.us, align 1
60+
%cond = select i1 false, i16 0, i16 0
61+
br label %inner.latch
62+
63+
inner.latch:
64+
%j.next = add i16 %j, 1
65+
br i1 true, label %outer.body, label %inner.header
66+
67+
outer.body:
68+
%new.cond.lcssa = phi i16 [ %cond, %inner.latch ]
69+
br label %outer.latch
70+
71+
outer.latch:
72+
%old.cond.lcssa = phi i16 [ %new.cond.lcssa, %outer.body ]
73+
%i.next = add i64 %i, 1
74+
%cmp286.us = icmp ugt i64 %i, 0
75+
br i1 %cmp286.us, label %outer.header, label %exit
76+
77+
exit:
78+
ret i16 %old.cond.lcssa
79+
}

0 commit comments

Comments
 (0)