1 //===- SparsePropagation.cpp - Sparse Conditional Property Propagation ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/LLVMContext.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
25 //===----------------------------------------------------------------------===//
26 // AbstractLatticeFunction Implementation
27 //===----------------------------------------------------------------------===//
29 AbstractLatticeFunction::~AbstractLatticeFunction() {}
31 /// PrintValue - Render the specified lattice value to the specified stream.
32 void AbstractLatticeFunction::PrintValue(LatticeVal V, raw_ostream &OS) {
35 else if (V == OverdefinedVal)
37 else if (V == UntrackedVal)
40 OS << "unknown lattice value";
43 //===----------------------------------------------------------------------===//
44 // SparseSolver Implementation
45 //===----------------------------------------------------------------------===//
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.
53 SparseSolver::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
58 if (LatticeFunc->IsUntrackedValue(V))
59 return LatticeFunc->getUntrackedVal();
60 else if (Constant *C = dyn_cast<Constant>(V))
61 LV = LatticeFunc->ComputeConstant(C);
62 else if (Argument *A = dyn_cast<Argument>(V))
63 LV = LatticeFunc->ComputeArgument(A);
64 else if (!isa<Instruction>(V))
65 // All other non-instructions are overdefined.
66 LV = LatticeFunc->getOverdefinedVal();
68 // All instructions are underdefined by default.
69 LV = LatticeFunc->getUndefVal();
71 // If this value is untracked, don't add it to the map.
72 if (LV == LatticeFunc->getUntrackedVal())
74 return ValueState[V] = LV;
77 /// UpdateState - When the state for some instruction is potentially updated,
78 /// this function notices and adds I to the worklist if needed.
79 void SparseSolver::UpdateState(Instruction &Inst, LatticeVal V) {
80 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(&Inst);
81 if (I != ValueState.end() && I->second == V)
84 // An update. Visit uses of I.
85 ValueState[&Inst] = V;
86 InstWorkList.push_back(&Inst);
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.
91 void SparseSolver::MarkBlockExecutable(BasicBlock *BB) {
92 DEBUG(errs() << "Marking Block Executable: " << BB->getName() << "\n");
93 BBExecutable.insert(BB); // Basic block is executable!
94 BBWorkList.push_back(BB); // Add the block to the work list!
97 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
98 /// work list if it is not already executable...
99 void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
100 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
101 return; // This edge is already known to be executable!
103 DEBUG(errs() << "Marking Edge Executable: " << Source->getName()
104 << " -> " << Dest->getName() << "\n");
106 if (BBExecutable.count(Dest)) {
107 // 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));
114 MarkBlockExecutable(Dest);
119 /// getFeasibleSuccessors - Return a vector of booleans to indicate which
120 /// successors are reachable from a given terminator instruction.
121 void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
122 SmallVectorImpl<bool> &Succs,
123 bool AggressiveUndef) {
124 Succs.resize(TI.getNumSuccessors());
125 if (TI.getNumSuccessors() == 0) return;
127 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
128 if (BI->isUnconditional()) {
135 BCValue = getOrInitValueState(BI->getCondition());
137 BCValue = getLatticeState(BI->getCondition());
139 if (BCValue == LatticeFunc->getOverdefinedVal() ||
140 BCValue == LatticeFunc->getUntrackedVal()) {
141 // Overdefined condition variables can branch either way.
142 Succs[0] = Succs[1] = true;
146 // If undefined, neither is feasible yet.
147 if (BCValue == LatticeFunc->getUndefVal())
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;
157 // Constant condition variables mean the branch can only go a single way
158 Succs[C == ConstantInt::getFalse(*Context)] = true;
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;
169 if (isa<IndirectBrInst>(TI)) {
170 Succs.assign(Succs.size(), true);
174 SwitchInst &SI = cast<SwitchInst>(TI);
177 SCValue = getOrInitValueState(SI.getCondition());
179 SCValue = getLatticeState(SI.getCondition());
181 if (SCValue == LatticeFunc->getOverdefinedVal() ||
182 SCValue == LatticeFunc->getUntrackedVal()) {
183 // All destinations are executable!
184 Succs.assign(TI.getNumSuccessors(), true);
188 // If undefined, neither is feasible yet.
189 if (SCValue == LatticeFunc->getUndefVal())
192 Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this);
193 if (C == 0 || !isa<ConstantInt>(C)) {
194 // All destinations are executable!
195 Succs.assign(TI.getNumSuccessors(), true);
199 Succs[SI.findCaseValue(cast<ConstantInt>(C))] = true;
203 /// isEdgeFeasible - Return true if the control flow edge from the 'From'
204 /// basic block to the 'To' basic block is currently feasible...
205 bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
206 bool AggressiveUndef) {
207 SmallVector<bool, 16> SuccFeasible;
208 TerminatorInst *TI = From->getTerminator();
209 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
211 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
212 if (TI->getSuccessor(i) == To && SuccFeasible[i])
218 void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
219 SmallVector<bool, 16> SuccFeasible;
220 getFeasibleSuccessors(TI, SuccFeasible, true);
222 BasicBlock *BB = TI.getParent();
224 // Mark all feasible successors executable...
225 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
227 markEdgeExecutable(BB, TI.getSuccessor(i));
230 void SparseSolver::visitPHINode(PHINode &PN) {
231 // The lattice function may store more information on a PHINode than could be
232 // computed from its incoming values. For example, SSI form stores its sigma
233 // functions as PHINodes with a single incoming value.
234 if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
235 LatticeVal IV = LatticeFunc->ComputeInstructionState(PN, *this);
236 if (IV != LatticeFunc->getUntrackedVal())
241 LatticeVal PNIV = getOrInitValueState(&PN);
242 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
244 // If this value is already overdefined (common) just return.
245 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
246 return; // Quick exit
248 // Super-extra-high-degree PHI nodes are unlikely to ever be interesting,
249 // and slow us down a lot. Just mark them overdefined.
250 if (PN.getNumIncomingValues() > 64) {
251 UpdateState(PN, Overdefined);
255 // Look at all of the executable operands of the PHI node. If any of them
256 // are overdefined, the PHI becomes overdefined as well. Otherwise, ask the
257 // transfer function to give us the merge of the incoming values.
258 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
259 // If the edge is not yet known to be feasible, it doesn't impact the PHI.
260 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
263 // Merge in this value.
264 LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
266 PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
268 if (PNIV == Overdefined)
269 break; // Rest of input values don't matter.
272 // Update the PHI with the compute value, which is the merge of the inputs.
273 UpdateState(PN, PNIV);
277 void SparseSolver::visitInst(Instruction &I) {
278 // PHIs are handled by the propagation logic, they are never passed into the
279 // transfer functions.
280 if (PHINode *PN = dyn_cast<PHINode>(&I))
281 return visitPHINode(*PN);
283 // Otherwise, ask the transfer function what the result is. If this is
284 // something that we care about, remember it.
285 LatticeVal IV = LatticeFunc->ComputeInstructionState(I, *this);
286 if (IV != LatticeFunc->getUntrackedVal())
289 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
290 visitTerminatorInst(*TI);
293 void SparseSolver::Solve(Function &F) {
294 MarkBlockExecutable(&F.getEntryBlock());
296 // Process the work lists until they are empty!
297 while (!BBWorkList.empty() || !InstWorkList.empty()) {
298 // Process the instruction work list.
299 while (!InstWorkList.empty()) {
300 Instruction *I = InstWorkList.back();
301 InstWorkList.pop_back();
303 DEBUG(errs() << "\nPopped off I-WL: " << *I << "\n");
305 // "I" got into the work list because it made a transition. See if any
306 // users are both live and in need of updating.
307 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
309 Instruction *U = cast<Instruction>(*UI);
310 if (BBExecutable.count(U->getParent())) // Inst is executable?
315 // Process the basic block work list.
316 while (!BBWorkList.empty()) {
317 BasicBlock *BB = BBWorkList.back();
318 BBWorkList.pop_back();
320 DEBUG(errs() << "\nPopped off BBWL: " << *BB);
322 // Notify all instructions in this basic block that they are newly
324 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
330 void SparseSolver::Print(Function &F, raw_ostream &OS) const {
331 OS << "\nFUNCTION: " << F.getNameStr() << "\n";
332 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
333 if (!BBExecutable.count(BB))
334 OS << "INFEASIBLE: ";
337 OS << BB->getNameStr() << ":\n";
340 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
341 LatticeFunc->PrintValue(getLatticeState(I), OS);