Skip to content

Commit fecef6b

Browse files
committedMay 22, 2018
[LoopVersioning] Don't modify the list that we iterate over in addPHINodes
Summary: In LoopVersioning::addPHINodes we need to iterate over all users for a value "Inst", and if the user is outside of the VersionedLoop we should replace the use of "Inst" by using the value "PN" instead. Replacing the use of "Inst" for a user of "Inst" also means that Inst->users() is modified. So it is not safe to do the replace while iterating over Inst->users() as we used to do. This patch splits the task into two steps. First we iterate over Inst->users() to find all users that should be updated. Those users are saved into a local data structure on the stack. And then, in the second step, we do the actual updates. This time iterating over the local data structure. Reviewers: mzolotukhin, anemet Reviewed By: mzolotukhin Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D47134 llvm-svn: 332958
1 parent 8446633 commit fecef6b

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed
 

‎llvm/lib/Transforms/Utils/LoopVersioning.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,12 @@ void LoopVersioning::addPHINodes(
140140
if (!PN) {
141141
PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver",
142142
&PHIBlock->front());
143-
for (auto *User : Inst->users())
144-
if (!VersionedLoop->contains(cast<Instruction>(User)->getParent()))
145-
User->replaceUsesOfWith(Inst, PN);
143+
SmallVector<User*, 8> UsersToUpdate;
144+
for (User *U : Inst->users())
145+
if (!VersionedLoop->contains(cast<Instruction>(U)->getParent()))
146+
UsersToUpdate.push_back(U);
147+
for (User *U : UsersToUpdate)
148+
U->replaceUsesOfWith(Inst, PN);
146149
PN->addIncoming(Inst, VersionedLoop->getExitingBlock());
147150
}
148151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
; RUN: opt < %s -loop-versioning -S -o - | FileCheck %s
2+
3+
; This test case used to end like this:
4+
;
5+
; Instruction does not dominate all uses!
6+
; %t2 = load i16, i16* @b, align 1, !tbaa !2, !alias.scope !6
7+
; %tobool = icmp eq i16 %t2, 0
8+
; LLVM ERROR: Broken function found, compilation aborted!
9+
;
10+
; due to a fault where we did not replace the use of %t2 in the icmp in
11+
; for.end, when adding a new PHI node for the versioned loops based on the
12+
; loop-defined values used outside of the loop.
13+
;
14+
; Verify that the code compiles, that we get a versioned loop, and that the
15+
; uses of %t2 in for.end and if.then are updated to use the value from the
16+
; added phi node.
17+
18+
; CHECK: define void @f1
19+
; CHECK: for.end:
20+
; CHECK-NEXT: %t2.lver = phi i16 [ %t2, %for.body ], [ %t2.lver.orig, %for.body.lver.orig ]
21+
; CHECK-NEXT: %tobool = icmp eq i16 %t2.lver, 0
22+
; CHECK: if.then:
23+
; CHECK-NEXT: store i16 %t2.lver
24+
25+
@a = dso_local global i16 0, align 1
26+
@b = dso_local global i16 0, align 1
27+
@c = dso_local global i16* null, align 1
28+
29+
define void @f1() {
30+
entry:
31+
%t0 = load i16*, i16** @c, align 1
32+
br label %for.cond
33+
34+
for.cond: ; preds = %for.cond.backedge, %entry
35+
br label %for.body
36+
37+
for.body: ; preds = %for.cond, %for.body
38+
%t1 = phi i64 [ 0, %for.cond ], [ %inc, %for.body ]
39+
%t2 = load i16, i16* @b, align 1, !tbaa !2
40+
store i16 %t2, i16* %t0, align 1, !tbaa !2
41+
%inc = add nuw nsw i64 %t1, 1
42+
%cmp = icmp ult i64 %inc, 3
43+
br i1 %cmp, label %for.body, label %for.end
44+
45+
for.end: ; preds = %for.body
46+
%tobool = icmp eq i16 %t2, 0
47+
br i1 %tobool, label %for.cond.backedge, label %if.then
48+
49+
for.cond.backedge: ; preds = %for.end, %if.then
50+
br label %for.cond
51+
52+
if.then: ; preds = %for.end
53+
store i16 %t2, i16* @a, align 1, !tbaa !2
54+
br label %for.cond.backedge
55+
}
56+
57+
!llvm.module.flags = !{!0}
58+
!llvm.ident = !{!1}
59+
60+
!0 = !{i32 1, !"wchar_size", i32 1}
61+
!1 = !{!"clang version 7.0.0"}
62+
!2 = !{!3, !3, i64 0}
63+
!3 = !{!"long long", !4, i64 0}
64+
!4 = !{!"omnipotent char", !5, i64 0}
65+
!5 = !{!"Simple C/C++ TBAA"}

0 commit comments

Comments
 (0)
Please sign in to comment.