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"
24 //===----------------------------------------------------------------------===//
25 // AbstractLatticeFunction Implementation
26 //===----------------------------------------------------------------------===//
28 AbstractLatticeFunction::~AbstractLatticeFunction() {}
30 /// PrintValue - Render the specified lattice value to the specified stream.
31 void AbstractLatticeFunction::PrintValue(LatticeVal V, std::ostream &OS) {
34 else if (V == OverdefinedVal)
36 else if (V == UntrackedVal)
39 OS << "unknown lattice value";
42 //===----------------------------------------------------------------------===//
43 // SparseSolver Implementation
44 //===----------------------------------------------------------------------===//
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.
52 SparseSolver::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
57 if (LatticeFunc->IsUntrackedValue(V))
58 return LatticeFunc->getUntrackedVal();
59 else if (Constant *C = dyn_cast<Constant>(V))
60 LV = LatticeFunc->ComputeConstant(C);
61 else if (Argument *A = dyn_cast<Argument>(V))
62 LV = LatticeFunc->ComputeArgument(A);
63 else if (!isa<Instruction>(V))
64 // All other non-instructions are overdefined.
65 LV = LatticeFunc->getOverdefinedVal();
67 // All instructions are underdefined by default.
68 LV = LatticeFunc->getUndefVal();
70 // If this value is untracked, don't add it to the map.
71 if (LV == LatticeFunc->getUntrackedVal())
73 return ValueState[V] = LV;
76 /// UpdateState - When the state for some instruction is potentially updated,
77 /// this function notices and adds I to the worklist if needed.
78 void SparseSolver::UpdateState(Instruction &Inst, LatticeVal V) {
79 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(&Inst);
80 if (I != ValueState.end() && I->second == V)
83 // An update. Visit uses of I.
84 ValueState[&Inst] = V;
85 InstWorkList.push_back(&Inst);
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.
90 void SparseSolver::MarkBlockExecutable(BasicBlock *BB) {
91 DOUT << "Marking Block Executable: " << BB->getNameStart() << "\n";
92 BBExecutable.insert(BB); // Basic block is executable!
93 BBWorkList.push_back(BB); // Add the block to the work list!
96 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
97 /// work list if it is not already executable...
98 void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
99 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
100 return; // This edge is already known to be executable!
102 DOUT << "Marking Edge Executable: " << Source->getNameStart()
103 << " -> " << Dest->getNameStart() << "\n";
105 if (BBExecutable.count(Dest)) {
106 // 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));
113 MarkBlockExecutable(Dest);
118 /// getFeasibleSuccessors - Return a vector of booleans to indicate which
119 /// successors are reachable from a given terminator instruction.
120 void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
121 SmallVectorImpl<bool> &Succs,
122 bool AggressiveUndef) {
123 Succs.resize(TI.getNumSuccessors());
124 if (TI.getNumSuccessors() == 0) return;
126 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
127 if (BI->isUnconditional()) {
134 BCValue = getOrInitValueState(BI->getCondition());
136 BCValue = getLatticeState(BI->getCondition());
138 if (BCValue == LatticeFunc->getOverdefinedVal() ||
139 BCValue == LatticeFunc->getUntrackedVal()) {
140 // Overdefined condition variables can branch either way.
141 Succs[0] = Succs[1] = true;
145 // If undefined, neither is feasible yet.
146 if (BCValue == LatticeFunc->getUndefVal())
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;
156 // Constant condition variables mean the branch can only go a single way
157 Succs[C == Context->getFalse()] = true;
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;
168 SwitchInst &SI = cast<SwitchInst>(TI);
171 SCValue = getOrInitValueState(SI.getCondition());
173 SCValue = getLatticeState(SI.getCondition());
175 if (SCValue == LatticeFunc->getOverdefinedVal() ||
176 SCValue == LatticeFunc->getUntrackedVal()) {
177 // All destinations are executable!
178 Succs.assign(TI.getNumSuccessors(), true);
182 // If undefined, neither is feasible yet.
183 if (SCValue == LatticeFunc->getUndefVal())
186 Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this);
187 if (C == 0 || !isa<ConstantInt>(C)) {
188 // All destinations are executable!
189 Succs.assign(TI.getNumSuccessors(), true);
193 Succs[SI.findCaseValue(cast<ConstantInt>(C))] = true;
197 /// isEdgeFeasible - Return true if the control flow edge from the 'From'
198 /// basic block to the 'To' basic block is currently feasible...
199 bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
200 bool AggressiveUndef) {
201 SmallVector<bool, 16> SuccFeasible;
202 TerminatorInst *TI = From->getTerminator();
203 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
205 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
206 if (TI->getSuccessor(i) == To && SuccFeasible[i])
212 void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
213 SmallVector<bool, 16> SuccFeasible;
214 getFeasibleSuccessors(TI, SuccFeasible, true);
216 BasicBlock *BB = TI.getParent();
218 // Mark all feasible successors executable...
219 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
221 markEdgeExecutable(BB, TI.getSuccessor(i));
224 void SparseSolver::visitPHINode(PHINode &PN) {
225 LatticeVal PNIV = getOrInitValueState(&PN);
226 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
228 // If this value is already overdefined (common) just return.
229 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
230 return; // Quick exit
232 // Super-extra-high-degree PHI nodes are unlikely to ever be interesting,
233 // and slow us down a lot. Just mark them overdefined.
234 if (PN.getNumIncomingValues() > 64) {
235 UpdateState(PN, Overdefined);
239 // Look at all of the executable operands of the PHI node. If any of them
240 // are overdefined, the PHI becomes overdefined as well. Otherwise, ask the
241 // transfer function to give us the merge of the incoming values.
242 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
243 // If the edge is not yet known to be feasible, it doesn't impact the PHI.
244 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
247 // Merge in this value.
248 LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
250 PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
252 if (PNIV == Overdefined)
253 break; // Rest of input values don't matter.
256 // Update the PHI with the compute value, which is the merge of the inputs.
257 UpdateState(PN, PNIV);
261 void SparseSolver::visitInst(Instruction &I) {
262 // PHIs are handled by the propagation logic, they are never passed into the
263 // transfer functions.
264 if (PHINode *PN = dyn_cast<PHINode>(&I))
265 return visitPHINode(*PN);
267 // Otherwise, ask the transfer function what the result is. If this is
268 // something that we care about, remember it.
269 LatticeVal IV = LatticeFunc->ComputeInstructionState(I, *this);
270 if (IV != LatticeFunc->getUntrackedVal())
273 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
274 visitTerminatorInst(*TI);
277 void SparseSolver::Solve(Function &F) {
278 MarkBlockExecutable(&F.getEntryBlock());
280 // Process the work lists until they are empty!
281 while (!BBWorkList.empty() || !InstWorkList.empty()) {
282 // Process the instruction work list.
283 while (!InstWorkList.empty()) {
284 Instruction *I = InstWorkList.back();
285 InstWorkList.pop_back();
287 DOUT << "\nPopped off I-WL: " << *I;
289 // "I" got into the work list because it made a transition. See if any
290 // users are both live and in need of updating.
291 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
293 Instruction *U = cast<Instruction>(*UI);
294 if (BBExecutable.count(U->getParent())) // Inst is executable?
299 // Process the basic block work list.
300 while (!BBWorkList.empty()) {
301 BasicBlock *BB = BBWorkList.back();
302 BBWorkList.pop_back();
304 DOUT << "\nPopped off BBWL: " << *BB;
306 // Notify all instructions in this basic block that they are newly
308 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
314 void SparseSolver::Print(Function &F, std::ostream &OS) const {
315 OS << "\nFUNCTION: " << F.getNameStr() << "\n";
316 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
317 if (!BBExecutable.count(BB))
318 OS << "INFEASIBLE: ";
321 OS << BB->getNameStr() << ":\n";
324 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
325 LatticeFunc->PrintValue(getLatticeState(I), OS);