blob: 79dc84e25533f80b42796b5242b7ea2e5d9be164 [file] [log] [blame]
Chris Lattner52877652008-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
Chris Lattner52877652008-05-12 01:12:24 +000015#include "llvm/Analysis/SparsePropagation.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/Instructions.h"
Chris Lattner52877652008-05-12 01:12:24 +000019#include "llvm/Support/Debug.h"
Daniel Dunbar6115b392009-07-26 09:48:23 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner52877652008-05-12 01:12:24 +000021using namespace llvm;
22
Chandler Carruthf1221bd2014-04-22 02:48:03 +000023#define DEBUG_TYPE "sparseprop"
24
Chris Lattner52877652008-05-12 01:12:24 +000025//===----------------------------------------------------------------------===//
26// AbstractLatticeFunction Implementation
27//===----------------------------------------------------------------------===//
28
29AbstractLatticeFunction::~AbstractLatticeFunction() {}
30
31/// PrintValue - Render the specified lattice value to the specified stream.
Chris Lattnerb25de3f2009-08-23 04:37:46 +000032void AbstractLatticeFunction::PrintValue(LatticeVal V, raw_ostream &OS) {
Chris Lattner52877652008-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 Lattner09e3b652008-08-09 17:23:35 +000062 else if (Argument *A = dyn_cast<Argument>(V))
63 LV = LatticeFunc->ComputeArgument(A);
Chris Lattner52877652008-05-12 01:12:24 +000064 else if (!isa<Instruction>(V))
Chris Lattner09e3b652008-08-09 17:23:35 +000065 // All other non-instructions are overdefined.
Chris Lattner52877652008-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) {
David Greenefaa00b72009-12-23 22:28:01 +000092 DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << "\n");
Chris Lattner52877652008-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
David Greenefaa00b72009-12-23 22:28:01 +0000103 DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
Daniel Dunbar6115b392009-07-26 09:48:23 +0000104 << " -> " << Dest->getName() << "\n");
Dan Gohman3ec55202008-05-27 20:47:30 +0000105
Chris Lattner52877652008-05-12 01:12:24 +0000106 if (BBExecutable.count(Dest)) {
Chris Lattner52877652008-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 Lattnerf4798562008-05-20 03:39:39 +0000122 SmallVectorImpl<bool> &Succs,
123 bool AggressiveUndef) {
Chris Lattner52877652008-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 Lattnerf4798562008-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 Lattner52877652008-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);
Craig Topper9f008862014-04-15 04:59:12 +0000151 if (!C || !isa<ConstantInt>(C)) {
Chris Lattner52877652008-05-12 01:12:24 +0000152 // 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
Dan Gohmanf902c8c2009-12-18 23:42:08 +0000158 Succs[C->isNullValue()] = true;
Chris Lattner52877652008-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
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000169 if (isa<IndirectBrInst>(TI)) {
Chris Lattnerc5c281e2009-10-27 21:27:42 +0000170 Succs.assign(Succs.size(), true);
171 return;
172 }
173
Chris Lattner52877652008-05-12 01:12:24 +0000174 SwitchInst &SI = cast<SwitchInst>(TI);
Chris Lattnerf4798562008-05-20 03:39:39 +0000175 LatticeVal SCValue;
176 if (AggressiveUndef)
177 SCValue = getOrInitValueState(SI.getCondition());
178 else
179 SCValue = getLatticeState(SI.getCondition());
180
Chris Lattner52877652008-05-12 01:12:24 +0000181 if (SCValue == LatticeFunc->getOverdefinedVal() ||
182 SCValue == LatticeFunc->getUntrackedVal()) {
183 // All destinations are executable!
184 Succs.assign(TI.getNumSuccessors(), true);
185 return;
186 }
187
188 // If undefined, neither is feasible yet.
189 if (SCValue == LatticeFunc->getUndefVal())
190 return;
191
192 Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this);
Craig Topper9f008862014-04-15 04:59:12 +0000193 if (!C || !isa<ConstantInt>(C)) {
Chris Lattner52877652008-05-12 01:12:24 +0000194 // All destinations are executable!
195 Succs.assign(TI.getNumSuccessors(), true);
196 return;
197 }
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +0000198 SwitchInst::CaseIt Case = SI.findCaseValue(cast<ConstantInt>(C));
199 Succs[Case.getSuccessorIndex()] = true;
Chris Lattner52877652008-05-12 01:12:24 +0000200}
201
202
203/// isEdgeFeasible - Return true if the control flow edge from the 'From'
204/// basic block to the 'To' basic block is currently feasible...
Chris Lattnerf4798562008-05-20 03:39:39 +0000205bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
206 bool AggressiveUndef) {
Chris Lattner52877652008-05-12 01:12:24 +0000207 SmallVector<bool, 16> SuccFeasible;
208 TerminatorInst *TI = From->getTerminator();
Chris Lattnerf4798562008-05-20 03:39:39 +0000209 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
Chris Lattner52877652008-05-12 01:12:24 +0000210
211 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
212 if (TI->getSuccessor(i) == To && SuccFeasible[i])
213 return true;
214
215 return false;
216}
217
218void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
219 SmallVector<bool, 16> SuccFeasible;
Chris Lattnerf4798562008-05-20 03:39:39 +0000220 getFeasibleSuccessors(TI, SuccFeasible, true);
Chris Lattner52877652008-05-12 01:12:24 +0000221
222 BasicBlock *BB = TI.getParent();
223
224 // Mark all feasible successors executable...
225 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
226 if (SuccFeasible[i])
227 markEdgeExecutable(BB, TI.getSuccessor(i));
228}
229
230void SparseSolver::visitPHINode(PHINode &PN) {
Nick Lewycky3b8bd052009-09-19 19:00:06 +0000231 // 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.
Nick Lewycky7e6deb12009-09-19 18:33:36 +0000234 if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
235 LatticeVal IV = LatticeFunc->ComputeInstructionState(PN, *this);
236 if (IV != LatticeFunc->getUntrackedVal())
237 UpdateState(PN, IV);
238 return;
239 }
240
Chris Lattner52877652008-05-12 01:12:24 +0000241 LatticeVal PNIV = getOrInitValueState(&PN);
242 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
243
244 // If this value is already overdefined (common) just return.
245 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
246 return; // Quick exit
247
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);
252 return;
253 }
254
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.
Chris Lattnerf4798562008-05-20 03:39:39 +0000260 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
Chris Lattner52877652008-05-12 01:12:24 +0000261 continue;
262
263 // Merge in this value.
264 LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
265 if (OpVal != PNIV)
266 PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
267
268 if (PNIV == Overdefined)
269 break; // Rest of input values don't matter.
270 }
271
272 // Update the PHI with the compute value, which is the merge of the inputs.
273 UpdateState(PN, PNIV);
274}
275
276
277void 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);
282
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())
287 UpdateState(I, IV);
288
289 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
290 visitTerminatorInst(*TI);
291}
292
293void SparseSolver::Solve(Function &F) {
Dan Gohmana61379e2008-05-27 20:55:29 +0000294 MarkBlockExecutable(&F.getEntryBlock());
Chris Lattner52877652008-05-12 01:12:24 +0000295
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();
302
David Greenefaa00b72009-12-23 22:28:01 +0000303 DEBUG(dbgs() << "\nPopped off I-WL: " << *I << "\n");
Chris Lattner52877652008-05-12 01:12:24 +0000304
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.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000307 for (User *U : I->users()) {
308 Instruction *UI = cast<Instruction>(U);
309 if (BBExecutable.count(UI->getParent())) // Inst is executable?
310 visitInst(*UI);
Chris Lattner52877652008-05-12 01:12:24 +0000311 }
312 }
313
314 // Process the basic block work list.
315 while (!BBWorkList.empty()) {
316 BasicBlock *BB = BBWorkList.back();
317 BBWorkList.pop_back();
318
David Greenefaa00b72009-12-23 22:28:01 +0000319 DEBUG(dbgs() << "\nPopped off BBWL: " << *BB);
Chris Lattner52877652008-05-12 01:12:24 +0000320
321 // Notify all instructions in this basic block that they are newly
322 // executable.
Benjamin Krameraa209152016-06-26 17:27:42 +0000323 for (Instruction &I : *BB)
324 visitInst(I);
Chris Lattner52877652008-05-12 01:12:24 +0000325 }
326 }
327}
328
Chris Lattnerb25de3f2009-08-23 04:37:46 +0000329void SparseSolver::Print(Function &F, raw_ostream &OS) const {
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000330 OS << "\nFUNCTION: " << F.getName() << "\n";
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000331 for (auto &BB : F) {
332 if (!BBExecutable.count(&BB))
Chris Lattner52877652008-05-12 01:12:24 +0000333 OS << "INFEASIBLE: ";
334 OS << "\t";
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000335 if (BB.hasName())
336 OS << BB.getName() << ":\n";
Chris Lattner52877652008-05-12 01:12:24 +0000337 else
338 OS << "; anon bb\n";
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000339 for (auto &I : BB) {
340 LatticeFunc->PrintValue(getLatticeState(&I), OS);
341 OS << I << "\n";
Chris Lattner52877652008-05-12 01:12:24 +0000342 }
343
344 OS << "\n";
345 }
346}
347