llvm.org GIT mirror llvm / 029e482
Merging r322006: ------------------------------------------------------------------------ r322006 | davide | 2018-01-08 17:34:06 +0100 (Mon, 08 Jan 2018) | 19 lines [CVP] Replace incoming values from unreachable blocks with undef. This is an attempt of fixing PR35807. Due to the non-standard definition of dominance in LLVM, where uses in unreachable blocks are dominated by anything, you can have, in an unreachable block: %patatino = OP1 %patatino, CONSTANT When `SimplifyInstruction` receives a PHI where an incoming value is of the aforementioned form, in some cases, loops indefinitely. What I propose here instead is keeping track of the incoming values from unreachable blocks, and replacing them with undef. It fixes this case, and it seems to be good regardless (even if we can't prove that the value is constant, as it's coming from an unreachable block, we can ignore it). Differential Revision: https://reviews.llvm.org/D41812 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_60@323738 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 1 year, 7 months ago
2 changed file(s) with 89 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
1313 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
1414 #include "llvm/ADT/DepthFirstIterator.h"
1515 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/PostOrderIterator.h"
1617 #include "llvm/ADT/SmallVector.h"
1718 #include "llvm/ADT/Statistic.h"
1819 #include "llvm/Analysis/GlobalsModRef.h"
119120 return true;
120121 }
121122
122 static bool processPHI(PHINode *P, LazyValueInfo *LVI,
123 const SimplifyQuery &SQ) {
123 static bool processPHI(PHINode *P, LazyValueInfo *LVI, const SimplifyQuery &SQ,
124 DenseSet &ReachableBlocks) {
124125 bool Changed = false;
125126
126127 BasicBlock *BB = P->getParent();
128129 Value *Incoming = P->getIncomingValue(i);
129130 if (isa(Incoming)) continue;
130131
131 Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB, P);
132 // If the incoming value is coming from an unreachable block, replace
133 // it with undef and go on. This is good for two reasons:
134 // 1) We skip an LVI query for an unreachable block
135 // 2) We transform the incoming value so that the code below doesn't
136 // mess around with IR in unreachable blocks.
137 BasicBlock *IncomingBB = P->getIncomingBlock(i);
138 if (!ReachableBlocks.count(IncomingBB)) {
139 P->setIncomingValue(i, UndefValue::get(P->getType()));
140 continue;
141 }
142
143 Value *V = LVI->getConstantOnEdge(Incoming, IncomingBB, BB, P);
132144
133145 // Look if the incoming value is a select with a scalar condition for which
134146 // LVI can tells us the value. In that case replace the incoming value with
560572
561573 static bool runImpl(Function &F, LazyValueInfo *LVI, const SimplifyQuery &SQ) {
562574 bool FnChanged = false;
575
576 // Compute reachability from the entry block of this function via an RPO
577 // walk. We use this information when processing PHIs.
578 DenseSet ReachableBlocks;
579 ReversePostOrderTraversal RPOT(&F);
580 for (BasicBlock *BB : RPOT)
581 ReachableBlocks.insert(BB);
582
563583 // Visiting in a pre-order depth-first traversal causes us to simplify early
564584 // blocks before querying later blocks (which require us to analyze early
565585 // blocks). Eagerly simplifying shallow blocks means there is strictly less
574594 BBChanged |= processSelect(cast(II), LVI);
575595 break;
576596 case Instruction::PHI:
577 BBChanged |= processPHI(cast(II), LVI, SQ);
597 BBChanged |= processPHI(cast(II), LVI, SQ, ReachableBlocks);
578598 break;
579599 case Instruction::ICmp:
580600 case Instruction::FCmp:
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt -correlated-propagation -S %s | FileCheck %s
2
3 target triple = "x86_64-apple-darwin17.4.0"
4
5 define void @patatino() {
6 ; CHECK-LABEL: @patatino(
7 ; CHECK-NEXT: br i1 undef, label [[BB3:%.*]], label [[BB4:%.*]]
8 ; CHECK: bb3:
9 ; CHECK-NEXT: br label [[BB3]]
10 ; CHECK: bb4:
11 ; CHECK-NEXT: br i1 undef, label [[BB40:%.*]], label [[BB22:%.*]]
12 ; CHECK: bb7:
13 ; CHECK-NEXT: br label [[BB14:%.*]]
14 ; CHECK: bb12:
15 ; CHECK-NEXT: br label [[BB14]]
16 ; CHECK: bb14:
17 ; CHECK-NEXT: [[TMP19:%.*]] = icmp sgt i32 undef, undef
18 ; CHECK-NEXT: [[TMP20:%.*]] = select i1 [[TMP19]], i64 [[TMP20]], i64 0
19 ; CHECK-NEXT: br i1 undef, label [[BB40]], label [[BB7:%.*]]
20 ; CHECK: bb22:
21 ; CHECK-NEXT: br label [[BB24:%.*]]
22 ; CHECK: bb24:
23 ; CHECK-NEXT: br label [[BB32:%.*]]
24 ; CHECK: bb32:
25 ; CHECK-NEXT: br i1 undef, label [[BB40]], label [[BB24]]
26 ; CHECK: bb40:
27 ; CHECK-NEXT: ret void
28 ;
29 br i1 undef, label %bb3, label %bb4
30
31 bb3:
32 br label %bb3
33
34 bb4:
35 br i1 undef, label %bb40, label %bb22
36
37 bb7:
38 br label %bb14
39
40 bb12:
41 br label %bb14
42
43 ; This block is unreachable. Due to the non-standard definition of
44 ; dominance in LLVM where uses in unreachable blocks are dominated
45 ; by anything, it contains an instruction of the form
46 ; %def = OP %def, %something
47 bb14:
48 %tmp19 = icmp sgt i32 undef, undef
49 %tmp20 = select i1 %tmp19, i64 %tmp20, i64 0
50 br i1 undef, label %bb40, label %bb7
51
52 bb22:
53 br label %bb24
54
55 bb24:
56 br label %bb32
57
58 bb32:
59 br i1 undef, label %bb40, label %bb24
60
61 bb40:
62 %tmp41 = phi i64 [ 4, %bb4 ], [ %tmp20, %bb14 ], [ undef, %bb32 ]
63 ret void
64 }