Merge branch 'master' into msp430
[llvm/msp430.git] / lib / Analysis / SparsePropagation.cpp
blob543306854ceda9ad186812a85a7d26151ed0b80f
1 //===- SparsePropagation.cpp - Sparse Conditional Property Propagation ----===//
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 an abstract sparse conditional propagation algorithm,
11 // modeled after SCCP, but with a customizable lattice function.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "sparseprop"
16 #include "llvm/Analysis/SparsePropagation.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Support/Debug.h"
21 using namespace llvm;
23 //===----------------------------------------------------------------------===//
24 // AbstractLatticeFunction Implementation
25 //===----------------------------------------------------------------------===//
27 AbstractLatticeFunction::~AbstractLatticeFunction() {}
29 /// PrintValue - Render the specified lattice value to the specified stream.
30 void AbstractLatticeFunction::PrintValue(LatticeVal V, std::ostream &OS) {
31 if (V == UndefVal)
32 OS << "undefined";
33 else if (V == OverdefinedVal)
34 OS << "overdefined";
35 else if (V == UntrackedVal)
36 OS << "untracked";
37 else
38 OS << "unknown lattice value";
41 //===----------------------------------------------------------------------===//
42 // SparseSolver Implementation
43 //===----------------------------------------------------------------------===//
45 /// getOrInitValueState - Return the LatticeVal object that corresponds to the
46 /// value, initializing the value's state if it hasn't been entered into the
47 /// map yet. This function is necessary because not all values should start
48 /// out in the underdefined state... Arguments should be overdefined, and
49 /// constants should be marked as constants.
50 ///
51 SparseSolver::LatticeVal SparseSolver::getOrInitValueState(Value *V) {
52 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V);
53 if (I != ValueState.end()) return I->second; // Common case, in the map
55 LatticeVal LV;
56 if (LatticeFunc->IsUntrackedValue(V))
57 return LatticeFunc->getUntrackedVal();
58 else if (Constant *C = dyn_cast<Constant>(V))
59 LV = LatticeFunc->ComputeConstant(C);
60 else if (Argument *A = dyn_cast<Argument>(V))
61 LV = LatticeFunc->ComputeArgument(A);
62 else if (!isa<Instruction>(V))
63 // All other non-instructions are overdefined.
64 LV = LatticeFunc->getOverdefinedVal();
65 else
66 // All instructions are underdefined by default.
67 LV = LatticeFunc->getUndefVal();
69 // If this value is untracked, don't add it to the map.
70 if (LV == LatticeFunc->getUntrackedVal())
71 return LV;
72 return ValueState[V] = LV;
75 /// UpdateState - When the state for some instruction is potentially updated,
76 /// this function notices and adds I to the worklist if needed.
77 void SparseSolver::UpdateState(Instruction &Inst, LatticeVal V) {
78 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(&Inst);
79 if (I != ValueState.end() && I->second == V)
80 return; // No change.
82 // An update. Visit uses of I.
83 ValueState[&Inst] = V;
84 InstWorkList.push_back(&Inst);
87 /// MarkBlockExecutable - This method can be used by clients to mark all of
88 /// the blocks that are known to be intrinsically live in the processed unit.
89 void SparseSolver::MarkBlockExecutable(BasicBlock *BB) {
90 DOUT << "Marking Block Executable: " << BB->getNameStart() << "\n";
91 BBExecutable.insert(BB); // Basic block is executable!
92 BBWorkList.push_back(BB); // Add the block to the work list!
95 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
96 /// work list if it is not already executable...
97 void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
98 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
99 return; // This edge is already known to be executable!
101 DOUT << "Marking Edge Executable: " << Source->getNameStart()
102 << " -> " << Dest->getNameStart() << "\n";
104 if (BBExecutable.count(Dest)) {
105 // The destination is already executable, but we just made an edge
106 // feasible that wasn't before. Revisit the PHI nodes in the block
107 // because they have potentially new operands.
108 for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
109 visitPHINode(*cast<PHINode>(I));
111 } else {
112 MarkBlockExecutable(Dest);
117 /// getFeasibleSuccessors - Return a vector of booleans to indicate which
118 /// successors are reachable from a given terminator instruction.
119 void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
120 SmallVectorImpl<bool> &Succs,
121 bool AggressiveUndef) {
122 Succs.resize(TI.getNumSuccessors());
123 if (TI.getNumSuccessors() == 0) return;
125 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
126 if (BI->isUnconditional()) {
127 Succs[0] = true;
128 return;
131 LatticeVal BCValue;
132 if (AggressiveUndef)
133 BCValue = getOrInitValueState(BI->getCondition());
134 else
135 BCValue = getLatticeState(BI->getCondition());
137 if (BCValue == LatticeFunc->getOverdefinedVal() ||
138 BCValue == LatticeFunc->getUntrackedVal()) {
139 // Overdefined condition variables can branch either way.
140 Succs[0] = Succs[1] = true;
141 return;
144 // If undefined, neither is feasible yet.
145 if (BCValue == LatticeFunc->getUndefVal())
146 return;
148 Constant *C = LatticeFunc->GetConstant(BCValue, BI->getCondition(), *this);
149 if (C == 0 || !isa<ConstantInt>(C)) {
150 // Non-constant values can go either way.
151 Succs[0] = Succs[1] = true;
152 return;
155 // Constant condition variables mean the branch can only go a single way
156 Succs[C == ConstantInt::getFalse()] = true;
157 return;
160 if (isa<InvokeInst>(TI)) {
161 // Invoke instructions successors are always executable.
162 // TODO: Could ask the lattice function if the value can throw.
163 Succs[0] = Succs[1] = true;
164 return;
167 SwitchInst &SI = cast<SwitchInst>(TI);
168 LatticeVal SCValue;
169 if (AggressiveUndef)
170 SCValue = getOrInitValueState(SI.getCondition());
171 else
172 SCValue = getLatticeState(SI.getCondition());
174 if (SCValue == LatticeFunc->getOverdefinedVal() ||
175 SCValue == LatticeFunc->getUntrackedVal()) {
176 // All destinations are executable!
177 Succs.assign(TI.getNumSuccessors(), true);
178 return;
181 // If undefined, neither is feasible yet.
182 if (SCValue == LatticeFunc->getUndefVal())
183 return;
185 Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this);
186 if (C == 0 || !isa<ConstantInt>(C)) {
187 // All destinations are executable!
188 Succs.assign(TI.getNumSuccessors(), true);
189 return;
192 Succs[SI.findCaseValue(cast<ConstantInt>(C))] = true;
196 /// isEdgeFeasible - Return true if the control flow edge from the 'From'
197 /// basic block to the 'To' basic block is currently feasible...
198 bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
199 bool AggressiveUndef) {
200 SmallVector<bool, 16> SuccFeasible;
201 TerminatorInst *TI = From->getTerminator();
202 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
204 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
205 if (TI->getSuccessor(i) == To && SuccFeasible[i])
206 return true;
208 return false;
211 void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
212 SmallVector<bool, 16> SuccFeasible;
213 getFeasibleSuccessors(TI, SuccFeasible, true);
215 BasicBlock *BB = TI.getParent();
217 // Mark all feasible successors executable...
218 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
219 if (SuccFeasible[i])
220 markEdgeExecutable(BB, TI.getSuccessor(i));
223 void SparseSolver::visitPHINode(PHINode &PN) {
224 LatticeVal PNIV = getOrInitValueState(&PN);
225 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
227 // If this value is already overdefined (common) just return.
228 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
229 return; // Quick exit
231 // Super-extra-high-degree PHI nodes are unlikely to ever be interesting,
232 // and slow us down a lot. Just mark them overdefined.
233 if (PN.getNumIncomingValues() > 64) {
234 UpdateState(PN, Overdefined);
235 return;
238 // Look at all of the executable operands of the PHI node. If any of them
239 // are overdefined, the PHI becomes overdefined as well. Otherwise, ask the
240 // transfer function to give us the merge of the incoming values.
241 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
242 // If the edge is not yet known to be feasible, it doesn't impact the PHI.
243 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
244 continue;
246 // Merge in this value.
247 LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
248 if (OpVal != PNIV)
249 PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
251 if (PNIV == Overdefined)
252 break; // Rest of input values don't matter.
255 // Update the PHI with the compute value, which is the merge of the inputs.
256 UpdateState(PN, PNIV);
260 void SparseSolver::visitInst(Instruction &I) {
261 // PHIs are handled by the propagation logic, they are never passed into the
262 // transfer functions.
263 if (PHINode *PN = dyn_cast<PHINode>(&I))
264 return visitPHINode(*PN);
266 // Otherwise, ask the transfer function what the result is. If this is
267 // something that we care about, remember it.
268 LatticeVal IV = LatticeFunc->ComputeInstructionState(I, *this);
269 if (IV != LatticeFunc->getUntrackedVal())
270 UpdateState(I, IV);
272 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
273 visitTerminatorInst(*TI);
276 void SparseSolver::Solve(Function &F) {
277 MarkBlockExecutable(&F.getEntryBlock());
279 // Process the work lists until they are empty!
280 while (!BBWorkList.empty() || !InstWorkList.empty()) {
281 // Process the instruction work list.
282 while (!InstWorkList.empty()) {
283 Instruction *I = InstWorkList.back();
284 InstWorkList.pop_back();
286 DOUT << "\nPopped off I-WL: " << *I;
288 // "I" got into the work list because it made a transition. See if any
289 // users are both live and in need of updating.
290 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
291 UI != E; ++UI) {
292 Instruction *U = cast<Instruction>(*UI);
293 if (BBExecutable.count(U->getParent())) // Inst is executable?
294 visitInst(*U);
298 // Process the basic block work list.
299 while (!BBWorkList.empty()) {
300 BasicBlock *BB = BBWorkList.back();
301 BBWorkList.pop_back();
303 DOUT << "\nPopped off BBWL: " << *BB;
305 // Notify all instructions in this basic block that they are newly
306 // executable.
307 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
308 visitInst(*I);
313 void SparseSolver::Print(Function &F, std::ostream &OS) const {
314 OS << "\nFUNCTION: " << F.getNameStr() << "\n";
315 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
316 if (!BBExecutable.count(BB))
317 OS << "INFEASIBLE: ";
318 OS << "\t";
319 if (BB->hasName())
320 OS << BB->getNameStr() << ":\n";
321 else
322 OS << "; anon bb\n";
323 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
324 LatticeFunc->PrintValue(getLatticeState(I), OS);
325 OS << *I;
328 OS << "\n";