]> git.proxmox.com Git - rustc.git/blame - src/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / lib / Transforms / Scalar / CorrelatedValuePropagation.cpp
CommitLineData
223e47cc
LB
1//===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Correlated Value Propagation pass.
11//
12//===----------------------------------------------------------------------===//
13
223e47cc 14#include "llvm/Transforms/Scalar.h"
970d7e83 15#include "llvm/ADT/Statistic.h"
223e47cc
LB
16#include "llvm/Analysis/InstructionSimplify.h"
17#include "llvm/Analysis/LazyValueInfo.h"
1a4d82fc 18#include "llvm/IR/CFG.h"
970d7e83
LB
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/Pass.h"
970d7e83
LB
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/raw_ostream.h"
223e47cc 25#include "llvm/Transforms/Utils/Local.h"
223e47cc
LB
26using namespace llvm;
27
1a4d82fc
JJ
28#define DEBUG_TYPE "correlated-value-propagation"
29
223e47cc
LB
30STATISTIC(NumPhis, "Number of phis propagated");
31STATISTIC(NumSelects, "Number of selects propagated");
32STATISTIC(NumMemAccess, "Number of memory access targets propagated");
33STATISTIC(NumCmps, "Number of comparisons propagated");
34STATISTIC(NumDeadCases, "Number of switch cases removed");
35
36namespace {
37 class CorrelatedValuePropagation : public FunctionPass {
38 LazyValueInfo *LVI;
39
40 bool processSelect(SelectInst *SI);
41 bool processPHI(PHINode *P);
42 bool processMemAccess(Instruction *I);
43 bool processCmp(CmpInst *C);
44 bool processSwitch(SwitchInst *SI);
45
46 public:
47 static char ID;
48 CorrelatedValuePropagation(): FunctionPass(ID) {
49 initializeCorrelatedValuePropagationPass(*PassRegistry::getPassRegistry());
50 }
51
1a4d82fc 52 bool runOnFunction(Function &F) override;
223e47cc 53
1a4d82fc 54 void getAnalysisUsage(AnalysisUsage &AU) const override {
223e47cc
LB
55 AU.addRequired<LazyValueInfo>();
56 }
57 };
58}
59
60char CorrelatedValuePropagation::ID = 0;
61INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation",
62 "Value Propagation", false, false)
63INITIALIZE_PASS_DEPENDENCY(LazyValueInfo)
64INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation",
65 "Value Propagation", false, false)
66
67// Public interface to the Value Propagation pass
68Pass *llvm::createCorrelatedValuePropagationPass() {
69 return new CorrelatedValuePropagation();
70}
71
72bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
73 if (S->getType()->isVectorTy()) return false;
74 if (isa<Constant>(S->getOperand(0))) return false;
75
1a4d82fc 76 Constant *C = LVI->getConstant(S->getOperand(0), S->getParent(), S);
223e47cc
LB
77 if (!C) return false;
78
79 ConstantInt *CI = dyn_cast<ConstantInt>(C);
80 if (!CI) return false;
81
82 Value *ReplaceWith = S->getOperand(1);
83 Value *Other = S->getOperand(2);
84 if (!CI->isOne()) std::swap(ReplaceWith, Other);
85 if (ReplaceWith == S) ReplaceWith = UndefValue::get(S->getType());
86
87 S->replaceAllUsesWith(ReplaceWith);
88 S->eraseFromParent();
89
90 ++NumSelects;
91
92 return true;
93}
94
95bool CorrelatedValuePropagation::processPHI(PHINode *P) {
96 bool Changed = false;
97
98 BasicBlock *BB = P->getParent();
99 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
100 Value *Incoming = P->getIncomingValue(i);
101 if (isa<Constant>(Incoming)) continue;
102
1a4d82fc 103 Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB, P);
970d7e83
LB
104
105 // Look if the incoming value is a select with a constant but LVI tells us
106 // that the incoming value can never be that constant. In that case replace
107 // the incoming value with the other value of the select. This often allows
108 // us to remove the select later.
109 if (!V) {
110 SelectInst *SI = dyn_cast<SelectInst>(Incoming);
111 if (!SI) continue;
112
113 Constant *C = dyn_cast<Constant>(SI->getFalseValue());
114 if (!C) continue;
115
116 if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C,
1a4d82fc 117 P->getIncomingBlock(i), BB, P) !=
970d7e83
LB
118 LazyValueInfo::False)
119 continue;
120
121 DEBUG(dbgs() << "CVP: Threading PHI over " << *SI << '\n');
122 V = SI->getTrueValue();
123 }
223e47cc 124
970d7e83 125 P->setIncomingValue(i, V);
223e47cc
LB
126 Changed = true;
127 }
128
1a4d82fc 129 // FIXME: Provide DL, TLI, DT, AT to SimplifyInstruction.
223e47cc
LB
130 if (Value *V = SimplifyInstruction(P)) {
131 P->replaceAllUsesWith(V);
132 P->eraseFromParent();
133 Changed = true;
134 }
135
136 if (Changed)
137 ++NumPhis;
138
139 return Changed;
140}
141
142bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
1a4d82fc 143 Value *Pointer = nullptr;
223e47cc
LB
144 if (LoadInst *L = dyn_cast<LoadInst>(I))
145 Pointer = L->getPointerOperand();
146 else
147 Pointer = cast<StoreInst>(I)->getPointerOperand();
148
149 if (isa<Constant>(Pointer)) return false;
150
1a4d82fc 151 Constant *C = LVI->getConstant(Pointer, I->getParent(), I);
223e47cc
LB
152 if (!C) return false;
153
154 ++NumMemAccess;
155 I->replaceUsesOfWith(Pointer, C);
156 return true;
157}
158
159/// processCmp - If the value of this comparison could be determined locally,
160/// constant propagation would already have figured it out. Instead, walk
161/// the predecessors and statically evaluate the comparison based on information
162/// available on that edge. If a given static evaluation is true on ALL
163/// incoming edges, then it's true universally and we can simplify the compare.
164bool CorrelatedValuePropagation::processCmp(CmpInst *C) {
165 Value *Op0 = C->getOperand(0);
166 if (isa<Instruction>(Op0) &&
167 cast<Instruction>(Op0)->getParent() == C->getParent())
168 return false;
169
170 Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
171 if (!Op1) return false;
172
173 pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent());
174 if (PI == PE) return false;
175
176 LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(),
1a4d82fc
JJ
177 C->getOperand(0), Op1, *PI,
178 C->getParent(), C);
223e47cc
LB
179 if (Result == LazyValueInfo::Unknown) return false;
180
181 ++PI;
182 while (PI != PE) {
183 LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(),
1a4d82fc
JJ
184 C->getOperand(0), Op1, *PI,
185 C->getParent(), C);
223e47cc
LB
186 if (Res != Result) return false;
187 ++PI;
188 }
189
190 ++NumCmps;
191
192 if (Result == LazyValueInfo::True)
193 C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext()));
194 else
195 C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext()));
196
197 C->eraseFromParent();
198
199 return true;
200}
201
202/// processSwitch - Simplify a switch instruction by removing cases which can
203/// never fire. If the uselessness of a case could be determined locally then
204/// constant propagation would already have figured it out. Instead, walk the
205/// predecessors and statically evaluate cases based on information available
206/// on that edge. Cases that cannot fire no matter what the incoming edge can
207/// safely be removed. If a case fires on every incoming edge then the entire
208/// switch can be removed and replaced with a branch to the case destination.
209bool CorrelatedValuePropagation::processSwitch(SwitchInst *SI) {
210 Value *Cond = SI->getCondition();
211 BasicBlock *BB = SI->getParent();
212
213 // If the condition was defined in same block as the switch then LazyValueInfo
214 // currently won't say anything useful about it, though in theory it could.
215 if (isa<Instruction>(Cond) && cast<Instruction>(Cond)->getParent() == BB)
216 return false;
217
218 // If the switch is unreachable then trying to improve it is a waste of time.
219 pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
220 if (PB == PE) return false;
221
222 // Analyse each switch case in turn. This is done in reverse order so that
223 // removing a case doesn't cause trouble for the iteration.
224 bool Changed = false;
225 for (SwitchInst::CaseIt CI = SI->case_end(), CE = SI->case_begin(); CI-- != CE;
226 ) {
227 ConstantInt *Case = CI.getCaseValue();
228
229 // Check to see if the switch condition is equal to/not equal to the case
230 // value on every incoming edge, equal/not equal being the same each time.
231 LazyValueInfo::Tristate State = LazyValueInfo::Unknown;
232 for (pred_iterator PI = PB; PI != PE; ++PI) {
233 // Is the switch condition equal to the case value?
234 LazyValueInfo::Tristate Value = LVI->getPredicateOnEdge(CmpInst::ICMP_EQ,
1a4d82fc
JJ
235 Cond, Case, *PI,
236 BB, SI);
223e47cc
LB
237 // Give up on this case if nothing is known.
238 if (Value == LazyValueInfo::Unknown) {
239 State = LazyValueInfo::Unknown;
240 break;
241 }
242
243 // If this was the first edge to be visited, record that all other edges
244 // need to give the same result.
245 if (PI == PB) {
246 State = Value;
247 continue;
248 }
249
250 // If this case is known to fire for some edges and known not to fire for
251 // others then there is nothing we can do - give up.
252 if (Value != State) {
253 State = LazyValueInfo::Unknown;
254 break;
255 }
256 }
257
258 if (State == LazyValueInfo::False) {
259 // This case never fires - remove it.
260 CI.getCaseSuccessor()->removePredecessor(BB);
261 SI->removeCase(CI); // Does not invalidate the iterator.
262
263 // The condition can be modified by removePredecessor's PHI simplification
264 // logic.
265 Cond = SI->getCondition();
266
267 ++NumDeadCases;
268 Changed = true;
269 } else if (State == LazyValueInfo::True) {
270 // This case always fires. Arrange for the switch to be turned into an
271 // unconditional branch by replacing the switch condition with the case
272 // value.
273 SI->setCondition(Case);
274 NumDeadCases += SI->getNumCases();
275 Changed = true;
276 break;
277 }
278 }
279
280 if (Changed)
281 // If the switch has been simplified to the point where it can be replaced
282 // by a branch then do so now.
283 ConstantFoldTerminator(BB);
284
285 return Changed;
286}
287
288bool CorrelatedValuePropagation::runOnFunction(Function &F) {
1a4d82fc
JJ
289 if (skipOptnoneFunction(F))
290 return false;
291
223e47cc
LB
292 LVI = &getAnalysis<LazyValueInfo>();
293
294 bool FnChanged = false;
295
296 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
297 bool BBChanged = false;
298 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
299 Instruction *II = BI++;
300 switch (II->getOpcode()) {
301 case Instruction::Select:
302 BBChanged |= processSelect(cast<SelectInst>(II));
303 break;
304 case Instruction::PHI:
305 BBChanged |= processPHI(cast<PHINode>(II));
306 break;
307 case Instruction::ICmp:
308 case Instruction::FCmp:
309 BBChanged |= processCmp(cast<CmpInst>(II));
310 break;
311 case Instruction::Load:
312 case Instruction::Store:
313 BBChanged |= processMemAccess(II);
314 break;
315 }
316 }
317
318 Instruction *Term = FI->getTerminator();
319 switch (Term->getOpcode()) {
320 case Instruction::Switch:
321 BBChanged |= processSwitch(cast<SwitchInst>(Term));
322 break;
323 }
324
325 FnChanged |= BBChanged;
326 }
327
328 return FnChanged;
329}