Skip to content

Commit 18e6679

Browse files
committed
[LoopInterchange] Also look at lcssa phis in outer loop latch block
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 observation is that %old.cond.lcssa is unused, so instead of moving and renaming these phi nodes, just delete it if it's trivially dead. If it isn't trivially dead, it is handled elsewhere. The loop should still be in LCSSA form, and if it isn't, formLCSSARecursively is called after the interchange rewrite. Fixes #160068
1 parent acb826e commit 18e6679

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "llvm/Transforms/Scalar/LoopPassManager.h"
4545
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
4646
#include "llvm/Transforms/Utils/LoopUtils.h"
47+
#include "llvm/Transforms/Utils/Local.h"
4748
#include <cassert>
4849
#include <utility>
4950
#include <vector>
@@ -1837,6 +1838,38 @@ static void moveLCSSAPhis(BasicBlock *InnerExit, BasicBlock *InnerHeader,
18371838
for (PHINode *P : LcssaInnerLatch)
18381839
P->moveBefore(InnerExit->getFirstNonPHIIt());
18391840

1841+
// This deals with a corner case of LCSSA phi nodes in the outer loop latch
1842+
// block: the loop was in LCSSA form, some transformations can come along
1843+
// (e.g. unswitch) and create an empty block:
1844+
//
1845+
// BB4:
1846+
// br label %BB5
1847+
// BB5:
1848+
// %old.cond.lcssa = phi i16 [ %cond, %BB4 ]
1849+
// br outer.header
1850+
//
1851+
// Interchange then brings it in LCSSA form again and we get:
1852+
//
1853+
// BB4:
1854+
// %new.cond.lcssa = phi i16 [ %cond, %BB3 ]
1855+
// br label %BB5
1856+
// BB5:
1857+
// %old.cond.lcssa = phi i16 [ %new.cond.lcssa, %BB4 ]
1858+
//
1859+
// Which means that we have a chain of LCSSA phi nodes from %new.cond.lcssa
1860+
// to %old.cond.lcssa. The problem is that interchange can reoder blocks BB4
1861+
// and BB5 placing the use before the def if we don't check this. The
1862+
// observation is that %old.cond.lcssa is unused, so instead of moving and
1863+
// renaming these phi nodes, just delete it if it's trivially dead. If it
1864+
// isn't trivially dead, it is handled above. The loop should still be in
1865+
// LCSSA form, and if it isn't, formLCSSARecursively is called after the
1866+
// interchange rewrite.
1867+
SmallVector<PHINode *, 8> LcssaOuterLatch(
1868+
llvm::make_pointer_range(OuterLatch->phis()));
1869+
for (PHINode *P : LcssaOuterLatch)
1870+
if (isInstructionTriviallyDead(P))
1871+
P->eraseFromParent();
1872+
18401873
// Deal with LCSSA PHI nodes in the loop nest exit block. For PHIs that have
18411874
// incoming values defined in the outer loop, we have to add a new PHI
18421875
// in the inner loop latch, which became the exit block of the outer loop,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --prefix-filecheck-ir-name TEST --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 BB4 and BB5, where BB4 is the exit
5+
; block of the inner loop and BB5 the latch of the outer loop, correctly
6+
; deal with the phi-node use-def chain %new.cond.lcssa -> %old.cond.lcssa.
7+
8+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
9+
10+
define i16 @main() {
11+
; CHECK-LABEL: define i16 @main() {
12+
; CHECK-NEXT: [[ENTRY:.*:]]
13+
; CHECK-NEXT: br label %[[BB2_PREHEADER:.*]]
14+
; CHECK: [[BB1_PREHEADER:.*]]:
15+
; CHECK-NEXT: br label %[[TESTBB1:.*]]
16+
; CHECK: [[TESTBB1]]:
17+
; CHECK-NEXT: [[I:%.*]] = phi i64 [ [[I_NEXT:%.*]], %[[BB5:.*]] ], [ 1, %[[BB1_PREHEADER]] ]
18+
; CHECK-NEXT: br label %[[BB2_SPLIT:.*]]
19+
; CHECK: [[BB2_PREHEADER]]:
20+
; CHECK-NEXT: br label %[[TESTBB2:.*]]
21+
; CHECK: [[TESTBB2]]:
22+
; CHECK-NEXT: [[J:%.*]] = phi i16 [ [[TMP1:%.*]], %[[BB3_SPLIT:.*]] ], [ 0, %[[BB2_PREHEADER]] ]
23+
; CHECK-NEXT: br label %[[BB1_PREHEADER]]
24+
; CHECK: [[BB2_SPLIT]]:
25+
; CHECK-NEXT: [[ARRAYIDX_US_US:%.*]] = getelementptr i16, ptr null, i16 [[J]]
26+
; CHECK-NEXT: [[TMP0:%.*]] = load i16, ptr [[ARRAYIDX_US_US]], align 1
27+
; CHECK-NEXT: [[COND:%.*]] = select i1 false, i16 0, i16 0
28+
; CHECK-NEXT: br label %[[TESTBB3:.*]]
29+
; CHECK: [[TESTBB3]]:
30+
; CHECK-NEXT: [[J_NEXT:%.*]] = add i16 [[J]], 1
31+
; CHECK-NEXT: br label %[[TESTBB4:.*]]
32+
; CHECK: [[BB3_SPLIT]]:
33+
; CHECK-NEXT: [[NEW_COND_LCSSA:%.*]] = phi i16 [ [[COND]], %[[BB5]] ]
34+
; CHECK-NEXT: [[TMP1]] = add i16 [[J]], 1
35+
; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[TESTBB2]]
36+
; CHECK: [[TESTBB4]]:
37+
; CHECK-NEXT: br label %[[BB5]]
38+
; CHECK: [[BB5]]:
39+
; CHECK-NEXT: [[I_NEXT]] = add i64 [[I]], 1
40+
; CHECK-NEXT: [[CMP286_US:%.*]] = icmp ugt i64 [[I]], 0
41+
; CHECK-NEXT: br i1 [[CMP286_US]], label %[[TESTBB1]], label %[[BB3_SPLIT]]
42+
; CHECK: [[EXIT]]:
43+
; CHECK-NEXT: ret i16 0
44+
;
45+
entry:
46+
br label %BB1
47+
48+
BB1:
49+
%i = phi i64 [ 1, %entry ], [ %i.next, %BB5 ]
50+
br label %BB2
51+
52+
BB2:
53+
%j = phi i16 [ 0, %BB1 ], [ %j.next, %BB3 ]
54+
%arrayidx.us.us = getelementptr i16, ptr null, i16 %j
55+
%0 = load i16, ptr %arrayidx.us.us, align 1
56+
%cond = select i1 false, i16 0, i16 0
57+
br label %BB3
58+
59+
BB3:
60+
%j.next = add i16 %j, 1
61+
br i1 true, label %BB4, label %BB2
62+
63+
BB4:
64+
%new.cond.lcssa = phi i16 [ %cond, %BB3 ]
65+
br label %BB5
66+
67+
BB5:
68+
%old.cond.lcssa = phi i16 [ %new.cond.lcssa, %BB4 ]
69+
%i.next = add i64 %i, 1
70+
%cmp286.us = icmp ugt i64 %i, 0
71+
br i1 %cmp286.us, label %BB1, label %exit
72+
73+
exit:
74+
ret i16 0
75+
}

0 commit comments

Comments
 (0)