blob: 8a74745dd3fb62c379f3e9bbc0079117fd10234d [file] [log] [blame]
Chris Lattnerab7d9cc2008-05-12 01:12:24 +00001//===- 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.
12//
13//===----------------------------------------------------------------------===//
14
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"
Owen Anderson76f600b2009-07-06 22:37:39 +000020#include "llvm/LLVMContext.h"
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000021#include "llvm/Support/Debug.h"
Daniel Dunbar460f6562009-07-26 09:48:23 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26// AbstractLatticeFunction Implementation
27//===----------------------------------------------------------------------===//
28
29AbstractLatticeFunction::~AbstractLatticeFunction() {}
30
31/// PrintValue - Render the specified lattice value to the specified stream.
Chris Lattnerbdff5482009-08-23 04:37:46 +000032void AbstractLatticeFunction::PrintValue(LatticeVal V, raw_ostream &OS) {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000033 if (V == UndefVal)
34 OS << "undefined";
35 else if (V == OverdefinedVal)
36 OS << "overdefined";
37 else if (V == UntrackedVal)
38 OS << "untracked";
39 else
40 OS << "unknown lattice value";
41}
42
43//===----------------------------------------------------------------------===//
44// SparseSolver Implementation
45//===----------------------------------------------------------------------===//
46
47/// getOrInitValueState - Return the LatticeVal object that corresponds to the
48/// value, initializing the value's state if it hasn't been entered into the
49/// map yet. This function is necessary because not all values should start
50/// out in the underdefined state... Arguments should be overdefined, and
51/// constants should be marked as constants.
52///
53SparseSolver::LatticeVal SparseSolver::getOrInitValueState(Value *V) {
54 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V);
55 if (I != ValueState.end()) return I->second; // Common case, in the map
56
57 LatticeVal LV;
58 if (LatticeFunc->IsUntrackedValue(V))
59 return LatticeFunc->getUntrackedVal();
60 else if (Constant *C = dyn_cast<Constant>(V))
61 LV = LatticeFunc->ComputeConstant(C);
Chris Lattnerafcde472008-08-09 17:23:35 +000062 else if (Argument *A = dyn_cast<Argument>(V))
63 LV = LatticeFunc->ComputeArgument(A);
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000064 else if (!isa<Instruction>(V))
Chris Lattnerafcde472008-08-09 17:23:35 +000065 // All other non-instructions are overdefined.
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000066 LV = LatticeFunc->getOverdefinedVal();
67 else
68 // All instructions are underdefined by default.
69 LV = LatticeFunc->getUndefVal();
70
71 // If this value is untracked, don't add it to the map.
72 if (LV == LatticeFunc->getUntrackedVal())
73 return LV;
74 return ValueState[V] = LV;
75}
76
77/// UpdateState - When the state for some instruction is potentially updated,
78/// this function notices and adds I to the worklist if needed.
79void SparseSolver::UpdateState(Instruction &Inst, LatticeVal V) {
80 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(&Inst);
81 if (I != ValueState.end() && I->second == V)
82 return; // No change.
83
84 // An update. Visit uses of I.
85 ValueState[&Inst] = V;
86 InstWorkList.push_back(&Inst);
87}
88
89/// MarkBlockExecutable - This method can be used by clients to mark all of
90/// the blocks that are known to be intrinsically live in the processed unit.
91void SparseSolver::MarkBlockExecutable(BasicBlock *BB) {
Daniel Dunbar460f6562009-07-26 09:48:23 +000092 DEBUG(errs() << "Marking Block Executable: " << BB->getName() << "\n");
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000093 BBExecutable.insert(BB); // Basic block is executable!
94 BBWorkList.push_back(BB); // Add the block to the work list!
95}
96
97/// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
98/// work list if it is not already executable...
99void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
100 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
101 return; // This edge is already known to be executable!
102
Daniel Dunbar460f6562009-07-26 09:48:23 +0000103 DEBUG(errs() << "Marking Edge Executable: " << Source->getName()
104 << " -> " << Dest->getName() << "\n");
Dan Gohmanb22d6ac2008-05-27 20:47:30 +0000105
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000106 if (BBExecutable.count(Dest)) {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000107 // The destination is already executable, but we just made an edge
108 // feasible that wasn't before. Revisit the PHI nodes in the block
109 // because they have potentially new operands.
110 for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
111 visitPHINode(*cast<PHINode>(I));
112
113 } else {
114 MarkBlockExecutable(Dest);
115 }
116}
117
118
119/// getFeasibleSuccessors - Return a vector of booleans to indicate which
120/// successors are reachable from a given terminator instruction.
121void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000122 SmallVectorImpl<bool> &Succs,
123 bool AggressiveUndef) {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000124 Succs.resize(TI.getNumSuccessors());
125 if (TI.getNumSuccessors() == 0) return;
126
127 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
128 if (BI->isUnconditional()) {
129 Succs[0] = true;
130 return;
131 }
132
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000133 LatticeVal BCValue;
134 if (AggressiveUndef)
135 BCValue = getOrInitValueState(BI->getCondition());
136 else
137 BCValue = getLatticeState(BI->getCondition());
138
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000139 if (BCValue == LatticeFunc->getOverdefinedVal() ||
140 BCValue == LatticeFunc->getUntrackedVal()) {
141 // Overdefined condition variables can branch either way.
142 Succs[0] = Succs[1] = true;
143 return;
144 }
145
146 // If undefined, neither is feasible yet.
147 if (BCValue == LatticeFunc->getUndefVal())
148 return;
149
150 Constant *C = LatticeFunc->GetConstant(BCValue, BI->getCondition(), *this);
151 if (C == 0 || !isa<ConstantInt>(C)) {
152 // Non-constant values can go either way.
153 Succs[0] = Succs[1] = true;
154 return;
155 }
156
157 // Constant condition variables mean the branch can only go a single way
Owen Anderson5defacc2009-07-31 17:39:07 +0000158 Succs[C == ConstantInt::getFalse(*Context)] = true;
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000159 return;
160 }
161
162 if (isa<InvokeInst>(TI)) {
163 // Invoke instructions successors are always executable.
164 // TODO: Could ask the lattice function if the value can throw.
165 Succs[0] = Succs[1] = true;
166 return;
167 }
168
169 SwitchInst &SI = cast<SwitchInst>(TI);
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000170 LatticeVal SCValue;
171 if (AggressiveUndef)
172 SCValue = getOrInitValueState(SI.getCondition());
173 else
174 SCValue = getLatticeState(SI.getCondition());
175
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000176 if (SCValue == LatticeFunc->getOverdefinedVal() ||
177 SCValue == LatticeFunc->getUntrackedVal()) {
178 // All destinations are executable!
179 Succs.assign(TI.getNumSuccessors(), true);
180 return;
181 }
182
183 // If undefined, neither is feasible yet.
184 if (SCValue == LatticeFunc->getUndefVal())
185 return;
186
187 Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this);
188 if (C == 0 || !isa<ConstantInt>(C)) {
189 // All destinations are executable!
190 Succs.assign(TI.getNumSuccessors(), true);
191 return;
192 }
193
194 Succs[SI.findCaseValue(cast<ConstantInt>(C))] = true;
195}
196
197
198/// isEdgeFeasible - Return true if the control flow edge from the 'From'
199/// basic block to the 'To' basic block is currently feasible...
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000200bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
201 bool AggressiveUndef) {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000202 SmallVector<bool, 16> SuccFeasible;
203 TerminatorInst *TI = From->getTerminator();
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000204 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000205
206 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
207 if (TI->getSuccessor(i) == To && SuccFeasible[i])
208 return true;
209
210 return false;
211}
212
213void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
214 SmallVector<bool, 16> SuccFeasible;
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000215 getFeasibleSuccessors(TI, SuccFeasible, true);
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000216
217 BasicBlock *BB = TI.getParent();
218
219 // Mark all feasible successors executable...
220 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
221 if (SuccFeasible[i])
222 markEdgeExecutable(BB, TI.getSuccessor(i));
223}
224
225void SparseSolver::visitPHINode(PHINode &PN) {
Nick Lewycky875646f2009-09-19 18:33:36 +0000226 if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
227 LatticeVal IV = LatticeFunc->ComputeInstructionState(PN, *this);
228 if (IV != LatticeFunc->getUntrackedVal())
229 UpdateState(PN, IV);
230 return;
231 }
232
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000233 LatticeVal PNIV = getOrInitValueState(&PN);
234 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
235
236 // If this value is already overdefined (common) just return.
237 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
238 return; // Quick exit
239
240 // Super-extra-high-degree PHI nodes are unlikely to ever be interesting,
241 // and slow us down a lot. Just mark them overdefined.
242 if (PN.getNumIncomingValues() > 64) {
243 UpdateState(PN, Overdefined);
244 return;
245 }
246
247 // Look at all of the executable operands of the PHI node. If any of them
248 // are overdefined, the PHI becomes overdefined as well. Otherwise, ask the
249 // transfer function to give us the merge of the incoming values.
250 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
251 // If the edge is not yet known to be feasible, it doesn't impact the PHI.
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000252 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000253 continue;
254
255 // Merge in this value.
256 LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
257 if (OpVal != PNIV)
258 PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
259
260 if (PNIV == Overdefined)
261 break; // Rest of input values don't matter.
262 }
263
264 // Update the PHI with the compute value, which is the merge of the inputs.
265 UpdateState(PN, PNIV);
266}
267
268
269void SparseSolver::visitInst(Instruction &I) {
270 // PHIs are handled by the propagation logic, they are never passed into the
271 // transfer functions.
272 if (PHINode *PN = dyn_cast<PHINode>(&I))
273 return visitPHINode(*PN);
274
275 // Otherwise, ask the transfer function what the result is. If this is
276 // something that we care about, remember it.
277 LatticeVal IV = LatticeFunc->ComputeInstructionState(I, *this);
278 if (IV != LatticeFunc->getUntrackedVal())
279 UpdateState(I, IV);
280
281 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
282 visitTerminatorInst(*TI);
283}
284
285void SparseSolver::Solve(Function &F) {
Dan Gohmanef61af02008-05-27 20:55:29 +0000286 MarkBlockExecutable(&F.getEntryBlock());
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000287
288 // Process the work lists until they are empty!
289 while (!BBWorkList.empty() || !InstWorkList.empty()) {
290 // Process the instruction work list.
291 while (!InstWorkList.empty()) {
292 Instruction *I = InstWorkList.back();
293 InstWorkList.pop_back();
294
Nick Lewycky1134dc52009-09-18 07:36:47 +0000295 DEBUG(errs() << "\nPopped off I-WL: " << *I << "\n");
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000296
297 // "I" got into the work list because it made a transition. See if any
298 // users are both live and in need of updating.
299 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
300 UI != E; ++UI) {
301 Instruction *U = cast<Instruction>(*UI);
302 if (BBExecutable.count(U->getParent())) // Inst is executable?
303 visitInst(*U);
304 }
305 }
306
307 // Process the basic block work list.
308 while (!BBWorkList.empty()) {
309 BasicBlock *BB = BBWorkList.back();
310 BBWorkList.pop_back();
311
Daniel Dunbar460f6562009-07-26 09:48:23 +0000312 DEBUG(errs() << "\nPopped off BBWL: " << *BB);
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000313
314 // Notify all instructions in this basic block that they are newly
315 // executable.
316 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
317 visitInst(*I);
318 }
319 }
320}
321
Chris Lattnerbdff5482009-08-23 04:37:46 +0000322void SparseSolver::Print(Function &F, raw_ostream &OS) const {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000323 OS << "\nFUNCTION: " << F.getNameStr() << "\n";
324 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
325 if (!BBExecutable.count(BB))
326 OS << "INFEASIBLE: ";
327 OS << "\t";
328 if (BB->hasName())
329 OS << BB->getNameStr() << ":\n";
330 else
331 OS << "; anon bb\n";
332 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
333 LatticeFunc->PrintValue(getLatticeState(I), OS);
Nick Lewycky1134dc52009-09-18 07:36:47 +0000334 OS << *I << "\n";
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000335 }
336
337 OS << "\n";
338 }
339}
340