SparsePropagation.cpp revision afcde473c5baf292038ec494917f18c77a043340
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.
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"
20#include "llvm/Support/Debug.h"
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24//                  AbstractLatticeFunction Implementation
25//===----------------------------------------------------------------------===//
26
27AbstractLatticeFunction::~AbstractLatticeFunction() {}
28
29/// PrintValue - Render the specified lattice value to the specified stream.
30void 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";
39}
40
41//===----------------------------------------------------------------------===//
42//                          SparseSolver Implementation
43//===----------------------------------------------------------------------===//
44
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///
51SparseSolver::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
54
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();
68
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;
73}
74
75/// UpdateState - When the state for some instruction is potentially updated,
76/// this function notices and adds I to the worklist if needed.
77void 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.
81
82  // An update.  Visit uses of I.
83  ValueState[&Inst] = V;
84  InstWorkList.push_back(&Inst);
85}
86
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.
89void 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!
93}
94
95/// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
96/// work list if it is not already executable...
97void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
98  if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
99    return;  // This edge is already known to be executable!
100
101  DOUT << "Marking Edge Executable: " << Source->getNameStart()
102       << " -> " << Dest->getNameStart() << "\n";
103
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));
110
111  } else {
112    MarkBlockExecutable(Dest);
113  }
114}
115
116
117/// getFeasibleSuccessors - Return a vector of booleans to indicate which
118/// successors are reachable from a given terminator instruction.
119void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
120                                         SmallVectorImpl<bool> &Succs,
121                                         bool AggressiveUndef) {
122  Succs.resize(TI.getNumSuccessors());
123  if (TI.getNumSuccessors() == 0) return;
124
125  if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
126    if (BI->isUnconditional()) {
127      Succs[0] = true;
128      return;
129    }
130
131    LatticeVal BCValue;
132    if (AggressiveUndef)
133      BCValue = getOrInitValueState(BI->getCondition());
134    else
135      BCValue = getLatticeState(BI->getCondition());
136
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;
142    }
143
144    // If undefined, neither is feasible yet.
145    if (BCValue == LatticeFunc->getUndefVal())
146      return;
147
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;
153    }
154
155    // Constant condition variables mean the branch can only go a single way
156    Succs[C == ConstantInt::getFalse()] = true;
157    return;
158  }
159
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;
165  }
166
167  SwitchInst &SI = cast<SwitchInst>(TI);
168  LatticeVal SCValue;
169  if (AggressiveUndef)
170    SCValue = getOrInitValueState(SI.getCondition());
171  else
172    SCValue = getLatticeState(SI.getCondition());
173
174  if (SCValue == LatticeFunc->getOverdefinedVal() ||
175      SCValue == LatticeFunc->getUntrackedVal()) {
176    // All destinations are executable!
177    Succs.assign(TI.getNumSuccessors(), true);
178    return;
179  }
180
181  // If undefined, neither is feasible yet.
182  if (SCValue == LatticeFunc->getUndefVal())
183    return;
184
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;
190  }
191
192  Succs[SI.findCaseValue(cast<ConstantInt>(C))] = true;
193}
194
195
196/// isEdgeFeasible - Return true if the control flow edge from the 'From'
197/// basic block to the 'To' basic block is currently feasible...
198bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
199                                  bool AggressiveUndef) {
200  SmallVector<bool, 16> SuccFeasible;
201  TerminatorInst *TI = From->getTerminator();
202  getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
203
204  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
205    if (TI->getSuccessor(i) == To && SuccFeasible[i])
206      return true;
207
208  return false;
209}
210
211void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
212  SmallVector<bool, 16> SuccFeasible;
213  getFeasibleSuccessors(TI, SuccFeasible, true);
214
215  BasicBlock *BB = TI.getParent();
216
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));
221}
222
223void SparseSolver::visitPHINode(PHINode &PN) {
224  LatticeVal PNIV = getOrInitValueState(&PN);
225  LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
226
227  // If this value is already overdefined (common) just return.
228  if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
229    return;  // Quick exit
230
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;
236  }
237
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;
245
246    // Merge in this value.
247    LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
248    if (OpVal != PNIV)
249      PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
250
251    if (PNIV == Overdefined)
252      break;  // Rest of input values don't matter.
253  }
254
255  // Update the PHI with the compute value, which is the merge of the inputs.
256  UpdateState(PN, PNIV);
257}
258
259
260void 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);
265
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);
271
272  if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
273    visitTerminatorInst(*TI);
274}
275
276void SparseSolver::Solve(Function &F) {
277  MarkBlockExecutable(&F.getEntryBlock());
278
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();
285
286      DOUT << "\nPopped off I-WL: " << *I;
287
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);
295      }
296    }
297
298    // Process the basic block work list.
299    while (!BBWorkList.empty()) {
300      BasicBlock *BB = BBWorkList.back();
301      BBWorkList.pop_back();
302
303      DOUT << "\nPopped off BBWL: " << *BB;
304
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);
309    }
310  }
311}
312
313void SparseSolver::Print(Function &F, std::ostream &OS) {
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;
326    }
327
328    OS << "\n";
329  }
330}
331
332