Skip to content

Commit b789165

Browse files
committedApr 19, 2018
[NewGVN] Add ops as dependency if we cannot find a leader for ValueOp.
If those operands change, we might find a leader for ValueOp, which could enable new phi-of-op creation. This fixes a case where we missed creating a phi-of-ops node. With D43865 and this patch, bootstrapping clang/llvm works with -enable-newgvn, whereas without it, the "value changed after iteration" assertion is triggered. Reviewers: dberlin, davide Reviewed By: dberlin Differential Revision: https://reviews.llvm.org/D42180 llvm-svn: 330334
1 parent d92c37e commit b789165

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed
 

‎llvm/lib/Transforms/Scalar/NewGVN.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -2753,6 +2753,7 @@ NewGVN::makePossiblePHIOfOps(Instruction *I,
27532753
// Clone the instruction, create an expression from it that is
27542754
// translated back into the predecessor, and see if we have a leader.
27552755
Instruction *ValueOp = I->clone();
2756+
SmallPtrSet<Value *, 4> CurrentDeps;
27562757
if (MemAccess)
27572758
TempToMemory.insert({ValueOp, MemAccess});
27582759
bool SafeForPHIOfOps = true;
@@ -2764,7 +2765,7 @@ NewGVN::makePossiblePHIOfOps(Instruction *I,
27642765
if (isa<PHINode>(Op)) {
27652766
Op = Op->DoPHITranslation(PHIBlock, PredBB);
27662767
if (Op != OrigOp && Op != I)
2767-
Deps.insert(Op);
2768+
CurrentDeps.insert(Op);
27682769
} else if (auto *ValuePHI = RealToTemp.lookup(Op)) {
27692770
if (getBlockForValue(ValuePHI) == PHIBlock)
27702771
Op = ValuePHI->getIncomingValueForBlock(PredBB);
@@ -2783,8 +2784,16 @@ NewGVN::makePossiblePHIOfOps(Instruction *I,
27832784
: findLeaderForInst(ValueOp, Visited,
27842785
MemAccess, I, PredBB);
27852786
ValueOp->deleteValue();
2786-
if (!FoundVal)
2787+
if (!FoundVal) {
2788+
// We failed to find a leader for the current ValueOp, but this might
2789+
// change in case of the translated operands change.
2790+
if (SafeForPHIOfOps)
2791+
for (auto Dep : CurrentDeps)
2792+
addAdditionalUsers(Dep, I);
2793+
27872794
return nullptr;
2795+
}
2796+
Deps.insert(CurrentDeps.begin(), CurrentDeps.end());
27882797
} else {
27892798
DEBUG(dbgs() << "Skipping phi of ops operand for incoming block "
27902799
<< getBlockName(PredBB)
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -newgvn -S | FileCheck %s
3+
4+
define void @sort(i64 %.16) {
5+
; CHECK-LABEL: @sort(
6+
; CHECK-NEXT: Entry:
7+
; CHECK-NEXT: [[TMP0:%.*]] = extractvalue { i64, i1 } undef, 0
8+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[TMP0]], 2
9+
; CHECK-NEXT: br i1 undef, label [[DIVZEROFAIL2_I:%.*]], label [[WHILEBODY_LR_PH:%.*]]
10+
; CHECK: DivZeroFail2.i:
11+
; CHECK-NEXT: unreachable
12+
; CHECK: WhileBody.lr.ph:
13+
; CHECK-NEXT: [[TMP2:%.*]] = udiv i64 [[DOT16:%.*]], [[TMP1]]
14+
; CHECK-NEXT: br label [[WHILEBODY:%.*]]
15+
; CHECK: WhileBody:
16+
; CHECK-NEXT: [[ITERATOR:%.*]] = phi i64 [ 0, [[WHILEBODY_LR_PH]] ], [ [[TMP6:%.*]], [[BOUNDSCHECKOK276:%.*]] ]
17+
; CHECK-NEXT: [[TMP3:%.*]] = tail call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 [[ITERATOR]], i64 [[TMP2]])
18+
; CHECK-NEXT: [[TMP4:%.*]] = extractvalue { i64, i1 } [[TMP3]], 0
19+
; CHECK-NEXT: [[TMP5:%.*]] = tail call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 [[TMP4]], i64 1)
20+
; CHECK-NEXT: [[TMP6]] = extractvalue { i64, i1 } [[TMP5]], 0
21+
; CHECK-NEXT: br i1 false, label [[BOUNDSCHECKFAIL275:%.*]], label [[BOUNDSCHECKOK276]]
22+
; CHECK: WhileEnd:
23+
; CHECK-NEXT: ret void
24+
; CHECK: BoundsCheckFail275:
25+
; CHECK-NEXT: unreachable
26+
; CHECK: BoundsCheckOk276:
27+
; CHECK-NEXT: [[TMP7:%.*]] = icmp ult i64 [[TMP6]], [[DOT16]]
28+
; CHECK-NEXT: br i1 [[TMP7]], label [[WHILEBODY]], label [[WHILEEND:%.*]]
29+
;
30+
Entry:
31+
%0 = extractvalue { i64, i1 } undef, 0
32+
%1 = lshr i64 %0, 2
33+
br i1 undef, label %DivZeroFail2.i, label %WhileBody.lr.ph
34+
35+
DivZeroFail2.i: ; preds = %Entry
36+
unreachable
37+
38+
WhileBody.lr.ph: ; preds = %Entry
39+
%2 = udiv i64 %.16, %1
40+
br label %WhileBody
41+
42+
WhileBody: ; preds = %BoundsCheckOk276, %WhileBody.lr.ph
43+
%iterator = phi i64 [ 0, %WhileBody.lr.ph ], [ %6, %BoundsCheckOk276 ]
44+
%3 = tail call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 %iterator, i64 %2)
45+
%4 = extractvalue { i64, i1 } %3, 0
46+
%5 = tail call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 %4, i64 1)
47+
%6 = extractvalue { i64, i1 } %5, 0
48+
%7 = icmp ugt i64 %iterator, %.16
49+
br i1 %7, label %BoundsCheckFail275, label %BoundsCheckOk276
50+
51+
WhileEnd: ; preds = %BoundsCheckOk276
52+
ret void
53+
54+
BoundsCheckFail275: ; preds = %WhileBody
55+
unreachable
56+
57+
BoundsCheckOk276: ; preds = %WhileBody
58+
%8 = icmp ult i64 %6, %.16
59+
br i1 %8, label %WhileBody, label %WhileEnd
60+
}
61+
62+
declare { i64, i1 } @llvm.uadd.with.overflow.i64(i64, i64)

0 commit comments

Comments
 (0)
Please sign in to comment.