blob: 487bdad6dc369f2514475d0c5c0173c4f7132bc5 [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"
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000020#include "llvm/Support/Debug.h"
Daniel Dunbar460f6562009-07-26 09:48:23 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
25// AbstractLatticeFunction Implementation
26//===----------------------------------------------------------------------===//
27
28AbstractLatticeFunction::~AbstractLatticeFunction() {}
29
30/// PrintValue - Render the specified lattice value to the specified stream.
Chris Lattnerbdff5482009-08-23 04:37:46 +000031void AbstractLatticeFunction::PrintValue(LatticeVal V, raw_ostream &OS) {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000032 if (V == UndefVal)
33 OS << "undefined";
34 else if (V == OverdefinedVal)
35 OS << "overdefined";
36 else if (V == UntrackedVal)
37 OS << "untracked";
38 else
39 OS << "unknown lattice value";
40}
41
42//===----------------------------------------------------------------------===//
43// SparseSolver Implementation
44//===----------------------------------------------------------------------===//
45
46/// getOrInitValueState - Return the LatticeVal object that corresponds to the
47/// value, initializing the value's state if it hasn't been entered into the
48/// map yet. This function is necessary because not all values should start
49/// out in the underdefined state... Arguments should be overdefined, and
50/// constants should be marked as constants.
51///
52SparseSolver::LatticeVal SparseSolver::getOrInitValueState(Value *V) {
53 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V);
54 if (I != ValueState.end()) return I->second; // Common case, in the map
55
56 LatticeVal LV;
57 if (LatticeFunc->IsUntrackedValue(V))
58 return LatticeFunc->getUntrackedVal();
59 else if (Constant *C = dyn_cast<Constant>(V))
60 LV = LatticeFunc->ComputeConstant(C);
Chris Lattnerafcde472008-08-09 17:23:35 +000061 else if (Argument *A = dyn_cast<Argument>(V))
62 LV = LatticeFunc->ComputeArgument(A);
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000063 else if (!isa<Instruction>(V))
Chris Lattnerafcde472008-08-09 17:23:35 +000064 // All other non-instructions are overdefined.
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000065 LV = LatticeFunc->getOverdefinedVal();
66 else
67 // All instructions are underdefined by default.
68 LV = LatticeFunc->getUndefVal();
69
70 // If this value is untracked, don't add it to the map.
71 if (LV == LatticeFunc->getUntrackedVal())
72 return LV;
73 return ValueState[V] = LV;
74}
75
76/// UpdateState - When the state for some instruction is potentially updated,
77/// this function notices and adds I to the worklist if needed.
78void SparseSolver::UpdateState(Instruction &Inst, LatticeVal V) {
79 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(&Inst);
80 if (I != ValueState.end() && I->second == V)
81 return; // No change.
82
83 // An update. Visit uses of I.
84 ValueState[&Inst] = V;
85 InstWorkList.push_back(&Inst);
86}
87
88/// MarkBlockExecutable - This method can be used by clients to mark all of
89/// the blocks that are known to be intrinsically live in the processed unit.
90void SparseSolver::MarkBlockExecutable(BasicBlock *BB) {
Daniel Dunbar460f6562009-07-26 09:48:23 +000091 DEBUG(errs() << "Marking Block Executable: " << BB->getName() << "\n");
Chris Lattnerab7d9cc2008-05-12 01:12:24 +000092 BBExecutable.insert(BB); // Basic block is executable!
93 BBWorkList.push_back(BB); // Add the block to the work list!
94}
95
96/// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
97/// work list if it is not already executable...
98void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
99 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
100 return; // This edge is already known to be executable!
101
Daniel Dunbar460f6562009-07-26 09:48:23 +0000102 DEBUG(errs() << "Marking Edge Executable: " << Source->getName()
103 << " -> " << Dest->getName() << "\n");
Dan Gohmanb22d6ac2008-05-27 20:47:30 +0000104
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000105 if (BBExecutable.count(Dest)) {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000106 // The destination is already executable, but we just made an edge
107 // feasible that wasn't before. Revisit the PHI nodes in the block
108 // because they have potentially new operands.
109 for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
110 visitPHINode(*cast<PHINode>(I));
111
112 } else {
113 MarkBlockExecutable(Dest);
114 }
115}
116
117
118/// getFeasibleSuccessors - Return a vector of booleans to indicate which
119/// successors are reachable from a given terminator instruction.
120void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000121 SmallVectorImpl<bool> &Succs,
122 bool AggressiveUndef) {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000123 Succs.resize(TI.getNumSuccessors());
124 if (TI.getNumSuccessors() == 0) return;
125
126 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
127 if (BI->isUnconditional()) {
128 Succs[0] = true;
129 return;
130 }
131
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000132 LatticeVal BCValue;
133 if (AggressiveUndef)
134 BCValue = getOrInitValueState(BI->getCondition());
135 else
136 BCValue = getLatticeState(BI->getCondition());
137
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000138 if (BCValue == LatticeFunc->getOverdefinedVal() ||
139 BCValue == LatticeFunc->getUntrackedVal()) {
140 // Overdefined condition variables can branch either way.
141 Succs[0] = Succs[1] = true;
142 return;
143 }
144
145 // If undefined, neither is feasible yet.
146 if (BCValue == LatticeFunc->getUndefVal())
147 return;
148
149 Constant *C = LatticeFunc->GetConstant(BCValue, BI->getCondition(), *this);
150 if (C == 0 || !isa<ConstantInt>(C)) {
151 // Non-constant values can go either way.
152 Succs[0] = Succs[1] = true;
153 return;
154 }
155
156 // Constant condition variables mean the branch can only go a single way
Dan Gohman43ea5052009-12-18 23:42:08 +0000157 Succs[C->isNullValue()] = true;
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000158 return;
159 }
160
161 if (isa<InvokeInst>(TI)) {
162 // Invoke instructions successors are always executable.
163 // TODO: Could ask the lattice function if the value can throw.
164 Succs[0] = Succs[1] = true;
165 return;
166 }
167
Chris Lattnerab21db72009-10-28 00:19:10 +0000168 if (isa<IndirectBrInst>(TI)) {
Chris Lattner2688bcb2009-10-27 21:27:42 +0000169 Succs.assign(Succs.size(), true);
170 return;
171 }
172
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000173 SwitchInst &SI = cast<SwitchInst>(TI);
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000174 LatticeVal SCValue;
175 if (AggressiveUndef)
176 SCValue = getOrInitValueState(SI.getCondition());
177 else
178 SCValue = getLatticeState(SI.getCondition());
179
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000180 if (SCValue == LatticeFunc->getOverdefinedVal() ||
181 SCValue == LatticeFunc->getUntrackedVal()) {
182 // All destinations are executable!
183 Succs.assign(TI.getNumSuccessors(), true);
184 return;
185 }
186
187 // If undefined, neither is feasible yet.
188 if (SCValue == LatticeFunc->getUndefVal())
189 return;
190
191 Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this);
192 if (C == 0 || !isa<ConstantInt>(C)) {
193 // All destinations are executable!
194 Succs.assign(TI.getNumSuccessors(), true);
195 return;
196 }
197
198 Succs[SI.findCaseValue(cast<ConstantInt>(C))] = true;
199}
200
201
202/// isEdgeFeasible - Return true if the control flow edge from the 'From'
203/// basic block to the 'To' basic block is currently feasible...
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000204bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
205 bool AggressiveUndef) {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000206 SmallVector<bool, 16> SuccFeasible;
207 TerminatorInst *TI = From->getTerminator();
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000208 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000209
210 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
211 if (TI->getSuccessor(i) == To && SuccFeasible[i])
212 return true;
213
214 return false;
215}
216
217void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
218 SmallVector<bool, 16> SuccFeasible;
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000219 getFeasibleSuccessors(TI, SuccFeasible, true);
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000220
221 BasicBlock *BB = TI.getParent();
222
223 // Mark all feasible successors executable...
224 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
225 if (SuccFeasible[i])
226 markEdgeExecutable(BB, TI.getSuccessor(i));
227}
228
229void SparseSolver::visitPHINode(PHINode &PN) {
Nick Lewyckyc2fc1fe2009-09-19 19:00:06 +0000230 // The lattice function may store more information on a PHINode than could be
231 // computed from its incoming values. For example, SSI form stores its sigma
232 // functions as PHINodes with a single incoming value.
Nick Lewycky875646f2009-09-19 18:33:36 +0000233 if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
234 LatticeVal IV = LatticeFunc->ComputeInstructionState(PN, *this);
235 if (IV != LatticeFunc->getUntrackedVal())
236 UpdateState(PN, IV);
237 return;
238 }
239
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000240 LatticeVal PNIV = getOrInitValueState(&PN);
241 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
242
243 // If this value is already overdefined (common) just return.
244 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
245 return; // Quick exit
246
247 // Super-extra-high-degree PHI nodes are unlikely to ever be interesting,
248 // and slow us down a lot. Just mark them overdefined.
249 if (PN.getNumIncomingValues() > 64) {
250 UpdateState(PN, Overdefined);
251 return;
252 }
253
254 // Look at all of the executable operands of the PHI node. If any of them
255 // are overdefined, the PHI becomes overdefined as well. Otherwise, ask the
256 // transfer function to give us the merge of the incoming values.
257 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
258 // If the edge is not yet known to be feasible, it doesn't impact the PHI.
Chris Lattner28a8dbc2008-05-20 03:39:39 +0000259 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000260 continue;
261
262 // Merge in this value.
263 LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
264 if (OpVal != PNIV)
265 PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
266
267 if (PNIV == Overdefined)
268 break; // Rest of input values don't matter.
269 }
270
271 // Update the PHI with the compute value, which is the merge of the inputs.
272 UpdateState(PN, PNIV);
273}
274
275
276void SparseSolver::visitInst(Instruction &I) {
277 // PHIs are handled by the propagation logic, they are never passed into the
278 // transfer functions.
279 if (PHINode *PN = dyn_cast<PHINode>(&I))
280 return visitPHINode(*PN);
281
282 // Otherwise, ask the transfer function what the result is. If this is
283 // something that we care about, remember it.
284 LatticeVal IV = LatticeFunc->ComputeInstructionState(I, *this);
285 if (IV != LatticeFunc->getUntrackedVal())
286 UpdateState(I, IV);
287
288 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
289 visitTerminatorInst(*TI);
290}
291
292void SparseSolver::Solve(Function &F) {
Dan Gohmanef61af02008-05-27 20:55:29 +0000293 MarkBlockExecutable(&F.getEntryBlock());
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000294
295 // Process the work lists until they are empty!
296 while (!BBWorkList.empty() || !InstWorkList.empty()) {
297 // Process the instruction work list.
298 while (!InstWorkList.empty()) {
299 Instruction *I = InstWorkList.back();
300 InstWorkList.pop_back();
301
Nick Lewycky1134dc52009-09-18 07:36:47 +0000302 DEBUG(errs() << "\nPopped off I-WL: " << *I << "\n");
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000303
304 // "I" got into the work list because it made a transition. See if any
305 // users are both live and in need of updating.
306 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
307 UI != E; ++UI) {
308 Instruction *U = cast<Instruction>(*UI);
309 if (BBExecutable.count(U->getParent())) // Inst is executable?
310 visitInst(*U);
311 }
312 }
313
314 // Process the basic block work list.
315 while (!BBWorkList.empty()) {
316 BasicBlock *BB = BBWorkList.back();
317 BBWorkList.pop_back();
318
Daniel Dunbar460f6562009-07-26 09:48:23 +0000319 DEBUG(errs() << "\nPopped off BBWL: " << *BB);
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000320
321 // Notify all instructions in this basic block that they are newly
322 // executable.
323 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
324 visitInst(*I);
325 }
326 }
327}
328
Chris Lattnerbdff5482009-08-23 04:37:46 +0000329void SparseSolver::Print(Function &F, raw_ostream &OS) const {
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000330 OS << "\nFUNCTION: " << F.getNameStr() << "\n";
331 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
332 if (!BBExecutable.count(BB))
333 OS << "INFEASIBLE: ";
334 OS << "\t";
335 if (BB->hasName())
336 OS << BB->getNameStr() << ":\n";
337 else
338 OS << "; anon bb\n";
339 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
340 LatticeFunc->PrintValue(getLatticeState(I), OS);
Nick Lewycky1134dc52009-09-18 07:36:47 +0000341 OS << *I << "\n";
Chris Lattnerab7d9cc2008-05-12 01:12:24 +0000342 }
343
344 OS << "\n";
345 }
346}
347