xref: /aosp_15_r20/external/llvm/lib/Analysis/SparsePropagation.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- SparsePropagation.cpp - Sparse Conditional Property Propagation ----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements an abstract sparse conditional propagation algorithm,
11*9880d681SAndroid Build Coastguard Worker // modeled after SCCP, but with a customizable lattice function.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/SparsePropagation.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "sparseprop"
24*9880d681SAndroid Build Coastguard Worker 
25*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
26*9880d681SAndroid Build Coastguard Worker //                  AbstractLatticeFunction Implementation
27*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
28*9880d681SAndroid Build Coastguard Worker 
~AbstractLatticeFunction()29*9880d681SAndroid Build Coastguard Worker AbstractLatticeFunction::~AbstractLatticeFunction() {}
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker /// PrintValue - Render the specified lattice value to the specified stream.
PrintValue(LatticeVal V,raw_ostream & OS)32*9880d681SAndroid Build Coastguard Worker void AbstractLatticeFunction::PrintValue(LatticeVal V, raw_ostream &OS) {
33*9880d681SAndroid Build Coastguard Worker   if (V == UndefVal)
34*9880d681SAndroid Build Coastguard Worker     OS << "undefined";
35*9880d681SAndroid Build Coastguard Worker   else if (V == OverdefinedVal)
36*9880d681SAndroid Build Coastguard Worker     OS << "overdefined";
37*9880d681SAndroid Build Coastguard Worker   else if (V == UntrackedVal)
38*9880d681SAndroid Build Coastguard Worker     OS << "untracked";
39*9880d681SAndroid Build Coastguard Worker   else
40*9880d681SAndroid Build Coastguard Worker     OS << "unknown lattice value";
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
44*9880d681SAndroid Build Coastguard Worker //                          SparseSolver Implementation
45*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker /// getOrInitValueState - Return the LatticeVal object that corresponds to the
48*9880d681SAndroid Build Coastguard Worker /// value, initializing the value's state if it hasn't been entered into the
49*9880d681SAndroid Build Coastguard Worker /// map yet.   This function is necessary because not all values should start
50*9880d681SAndroid Build Coastguard Worker /// out in the underdefined state... Arguments should be overdefined, and
51*9880d681SAndroid Build Coastguard Worker /// constants should be marked as constants.
52*9880d681SAndroid Build Coastguard Worker ///
getOrInitValueState(Value * V)53*9880d681SAndroid Build Coastguard Worker SparseSolver::LatticeVal SparseSolver::getOrInitValueState(Value *V) {
54*9880d681SAndroid Build Coastguard Worker   DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V);
55*9880d681SAndroid Build Coastguard Worker   if (I != ValueState.end()) return I->second;  // Common case, in the map
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker   LatticeVal LV;
58*9880d681SAndroid Build Coastguard Worker   if (LatticeFunc->IsUntrackedValue(V))
59*9880d681SAndroid Build Coastguard Worker     return LatticeFunc->getUntrackedVal();
60*9880d681SAndroid Build Coastguard Worker   else if (Constant *C = dyn_cast<Constant>(V))
61*9880d681SAndroid Build Coastguard Worker     LV = LatticeFunc->ComputeConstant(C);
62*9880d681SAndroid Build Coastguard Worker   else if (Argument *A = dyn_cast<Argument>(V))
63*9880d681SAndroid Build Coastguard Worker     LV = LatticeFunc->ComputeArgument(A);
64*9880d681SAndroid Build Coastguard Worker   else if (!isa<Instruction>(V))
65*9880d681SAndroid Build Coastguard Worker     // All other non-instructions are overdefined.
66*9880d681SAndroid Build Coastguard Worker     LV = LatticeFunc->getOverdefinedVal();
67*9880d681SAndroid Build Coastguard Worker   else
68*9880d681SAndroid Build Coastguard Worker     // All instructions are underdefined by default.
69*9880d681SAndroid Build Coastguard Worker     LV = LatticeFunc->getUndefVal();
70*9880d681SAndroid Build Coastguard Worker 
71*9880d681SAndroid Build Coastguard Worker   // If this value is untracked, don't add it to the map.
72*9880d681SAndroid Build Coastguard Worker   if (LV == LatticeFunc->getUntrackedVal())
73*9880d681SAndroid Build Coastguard Worker     return LV;
74*9880d681SAndroid Build Coastguard Worker   return ValueState[V] = LV;
75*9880d681SAndroid Build Coastguard Worker }
76*9880d681SAndroid Build Coastguard Worker 
77*9880d681SAndroid Build Coastguard Worker /// UpdateState - When the state for some instruction is potentially updated,
78*9880d681SAndroid Build Coastguard Worker /// this function notices and adds I to the worklist if needed.
UpdateState(Instruction & Inst,LatticeVal V)79*9880d681SAndroid Build Coastguard Worker void SparseSolver::UpdateState(Instruction &Inst, LatticeVal V) {
80*9880d681SAndroid Build Coastguard Worker   DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(&Inst);
81*9880d681SAndroid Build Coastguard Worker   if (I != ValueState.end() && I->second == V)
82*9880d681SAndroid Build Coastguard Worker     return;  // No change.
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker   // An update.  Visit uses of I.
85*9880d681SAndroid Build Coastguard Worker   ValueState[&Inst] = V;
86*9880d681SAndroid Build Coastguard Worker   InstWorkList.push_back(&Inst);
87*9880d681SAndroid Build Coastguard Worker }
88*9880d681SAndroid Build Coastguard Worker 
89*9880d681SAndroid Build Coastguard Worker /// MarkBlockExecutable - This method can be used by clients to mark all of
90*9880d681SAndroid Build Coastguard Worker /// the blocks that are known to be intrinsically live in the processed unit.
MarkBlockExecutable(BasicBlock * BB)91*9880d681SAndroid Build Coastguard Worker void SparseSolver::MarkBlockExecutable(BasicBlock *BB) {
92*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << "\n");
93*9880d681SAndroid Build Coastguard Worker   BBExecutable.insert(BB);   // Basic block is executable!
94*9880d681SAndroid Build Coastguard Worker   BBWorkList.push_back(BB);  // Add the block to the work list!
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker 
97*9880d681SAndroid Build Coastguard Worker /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
98*9880d681SAndroid Build Coastguard Worker /// work list if it is not already executable...
markEdgeExecutable(BasicBlock * Source,BasicBlock * Dest)99*9880d681SAndroid Build Coastguard Worker void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
100*9880d681SAndroid Build Coastguard Worker   if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
101*9880d681SAndroid Build Coastguard Worker     return;  // This edge is already known to be executable!
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
104*9880d681SAndroid Build Coastguard Worker         << " -> " << Dest->getName() << "\n");
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker   if (BBExecutable.count(Dest)) {
107*9880d681SAndroid Build Coastguard Worker     // The destination is already executable, but we just made an edge
108*9880d681SAndroid Build Coastguard Worker     // feasible that wasn't before.  Revisit the PHI nodes in the block
109*9880d681SAndroid Build Coastguard Worker     // because they have potentially new operands.
110*9880d681SAndroid Build Coastguard Worker     for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
111*9880d681SAndroid Build Coastguard Worker       visitPHINode(*cast<PHINode>(I));
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker   } else {
114*9880d681SAndroid Build Coastguard Worker     MarkBlockExecutable(Dest);
115*9880d681SAndroid Build Coastguard Worker   }
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker 
118*9880d681SAndroid Build Coastguard Worker 
119*9880d681SAndroid Build Coastguard Worker /// getFeasibleSuccessors - Return a vector of booleans to indicate which
120*9880d681SAndroid Build Coastguard Worker /// successors are reachable from a given terminator instruction.
getFeasibleSuccessors(TerminatorInst & TI,SmallVectorImpl<bool> & Succs,bool AggressiveUndef)121*9880d681SAndroid Build Coastguard Worker void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
122*9880d681SAndroid Build Coastguard Worker                                          SmallVectorImpl<bool> &Succs,
123*9880d681SAndroid Build Coastguard Worker                                          bool AggressiveUndef) {
124*9880d681SAndroid Build Coastguard Worker   Succs.resize(TI.getNumSuccessors());
125*9880d681SAndroid Build Coastguard Worker   if (TI.getNumSuccessors() == 0) return;
126*9880d681SAndroid Build Coastguard Worker 
127*9880d681SAndroid Build Coastguard Worker   if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
128*9880d681SAndroid Build Coastguard Worker     if (BI->isUnconditional()) {
129*9880d681SAndroid Build Coastguard Worker       Succs[0] = true;
130*9880d681SAndroid Build Coastguard Worker       return;
131*9880d681SAndroid Build Coastguard Worker     }
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker     LatticeVal BCValue;
134*9880d681SAndroid Build Coastguard Worker     if (AggressiveUndef)
135*9880d681SAndroid Build Coastguard Worker       BCValue = getOrInitValueState(BI->getCondition());
136*9880d681SAndroid Build Coastguard Worker     else
137*9880d681SAndroid Build Coastguard Worker       BCValue = getLatticeState(BI->getCondition());
138*9880d681SAndroid Build Coastguard Worker 
139*9880d681SAndroid Build Coastguard Worker     if (BCValue == LatticeFunc->getOverdefinedVal() ||
140*9880d681SAndroid Build Coastguard Worker         BCValue == LatticeFunc->getUntrackedVal()) {
141*9880d681SAndroid Build Coastguard Worker       // Overdefined condition variables can branch either way.
142*9880d681SAndroid Build Coastguard Worker       Succs[0] = Succs[1] = true;
143*9880d681SAndroid Build Coastguard Worker       return;
144*9880d681SAndroid Build Coastguard Worker     }
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker     // If undefined, neither is feasible yet.
147*9880d681SAndroid Build Coastguard Worker     if (BCValue == LatticeFunc->getUndefVal())
148*9880d681SAndroid Build Coastguard Worker       return;
149*9880d681SAndroid Build Coastguard Worker 
150*9880d681SAndroid Build Coastguard Worker     Constant *C = LatticeFunc->GetConstant(BCValue, BI->getCondition(), *this);
151*9880d681SAndroid Build Coastguard Worker     if (!C || !isa<ConstantInt>(C)) {
152*9880d681SAndroid Build Coastguard Worker       // Non-constant values can go either way.
153*9880d681SAndroid Build Coastguard Worker       Succs[0] = Succs[1] = true;
154*9880d681SAndroid Build Coastguard Worker       return;
155*9880d681SAndroid Build Coastguard Worker     }
156*9880d681SAndroid Build Coastguard Worker 
157*9880d681SAndroid Build Coastguard Worker     // Constant condition variables mean the branch can only go a single way
158*9880d681SAndroid Build Coastguard Worker     Succs[C->isNullValue()] = true;
159*9880d681SAndroid Build Coastguard Worker     return;
160*9880d681SAndroid Build Coastguard Worker   }
161*9880d681SAndroid Build Coastguard Worker 
162*9880d681SAndroid Build Coastguard Worker   if (isa<InvokeInst>(TI)) {
163*9880d681SAndroid Build Coastguard Worker     // Invoke instructions successors are always executable.
164*9880d681SAndroid Build Coastguard Worker     // TODO: Could ask the lattice function if the value can throw.
165*9880d681SAndroid Build Coastguard Worker     Succs[0] = Succs[1] = true;
166*9880d681SAndroid Build Coastguard Worker     return;
167*9880d681SAndroid Build Coastguard Worker   }
168*9880d681SAndroid Build Coastguard Worker 
169*9880d681SAndroid Build Coastguard Worker   if (isa<IndirectBrInst>(TI)) {
170*9880d681SAndroid Build Coastguard Worker     Succs.assign(Succs.size(), true);
171*9880d681SAndroid Build Coastguard Worker     return;
172*9880d681SAndroid Build Coastguard Worker   }
173*9880d681SAndroid Build Coastguard Worker 
174*9880d681SAndroid Build Coastguard Worker   SwitchInst &SI = cast<SwitchInst>(TI);
175*9880d681SAndroid Build Coastguard Worker   LatticeVal SCValue;
176*9880d681SAndroid Build Coastguard Worker   if (AggressiveUndef)
177*9880d681SAndroid Build Coastguard Worker     SCValue = getOrInitValueState(SI.getCondition());
178*9880d681SAndroid Build Coastguard Worker   else
179*9880d681SAndroid Build Coastguard Worker     SCValue = getLatticeState(SI.getCondition());
180*9880d681SAndroid Build Coastguard Worker 
181*9880d681SAndroid Build Coastguard Worker   if (SCValue == LatticeFunc->getOverdefinedVal() ||
182*9880d681SAndroid Build Coastguard Worker       SCValue == LatticeFunc->getUntrackedVal()) {
183*9880d681SAndroid Build Coastguard Worker     // All destinations are executable!
184*9880d681SAndroid Build Coastguard Worker     Succs.assign(TI.getNumSuccessors(), true);
185*9880d681SAndroid Build Coastguard Worker     return;
186*9880d681SAndroid Build Coastguard Worker   }
187*9880d681SAndroid Build Coastguard Worker 
188*9880d681SAndroid Build Coastguard Worker   // If undefined, neither is feasible yet.
189*9880d681SAndroid Build Coastguard Worker   if (SCValue == LatticeFunc->getUndefVal())
190*9880d681SAndroid Build Coastguard Worker     return;
191*9880d681SAndroid Build Coastguard Worker 
192*9880d681SAndroid Build Coastguard Worker   Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this);
193*9880d681SAndroid Build Coastguard Worker   if (!C || !isa<ConstantInt>(C)) {
194*9880d681SAndroid Build Coastguard Worker     // All destinations are executable!
195*9880d681SAndroid Build Coastguard Worker     Succs.assign(TI.getNumSuccessors(), true);
196*9880d681SAndroid Build Coastguard Worker     return;
197*9880d681SAndroid Build Coastguard Worker   }
198*9880d681SAndroid Build Coastguard Worker   SwitchInst::CaseIt Case = SI.findCaseValue(cast<ConstantInt>(C));
199*9880d681SAndroid Build Coastguard Worker   Succs[Case.getSuccessorIndex()] = true;
200*9880d681SAndroid Build Coastguard Worker }
201*9880d681SAndroid Build Coastguard Worker 
202*9880d681SAndroid Build Coastguard Worker 
203*9880d681SAndroid Build Coastguard Worker /// isEdgeFeasible - Return true if the control flow edge from the 'From'
204*9880d681SAndroid Build Coastguard Worker /// basic block to the 'To' basic block is currently feasible...
isEdgeFeasible(BasicBlock * From,BasicBlock * To,bool AggressiveUndef)205*9880d681SAndroid Build Coastguard Worker bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
206*9880d681SAndroid Build Coastguard Worker                                   bool AggressiveUndef) {
207*9880d681SAndroid Build Coastguard Worker   SmallVector<bool, 16> SuccFeasible;
208*9880d681SAndroid Build Coastguard Worker   TerminatorInst *TI = From->getTerminator();
209*9880d681SAndroid Build Coastguard Worker   getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
210*9880d681SAndroid Build Coastguard Worker 
211*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
212*9880d681SAndroid Build Coastguard Worker     if (TI->getSuccessor(i) == To && SuccFeasible[i])
213*9880d681SAndroid Build Coastguard Worker       return true;
214*9880d681SAndroid Build Coastguard Worker 
215*9880d681SAndroid Build Coastguard Worker   return false;
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker 
visitTerminatorInst(TerminatorInst & TI)218*9880d681SAndroid Build Coastguard Worker void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
219*9880d681SAndroid Build Coastguard Worker   SmallVector<bool, 16> SuccFeasible;
220*9880d681SAndroid Build Coastguard Worker   getFeasibleSuccessors(TI, SuccFeasible, true);
221*9880d681SAndroid Build Coastguard Worker 
222*9880d681SAndroid Build Coastguard Worker   BasicBlock *BB = TI.getParent();
223*9880d681SAndroid Build Coastguard Worker 
224*9880d681SAndroid Build Coastguard Worker   // Mark all feasible successors executable...
225*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
226*9880d681SAndroid Build Coastguard Worker     if (SuccFeasible[i])
227*9880d681SAndroid Build Coastguard Worker       markEdgeExecutable(BB, TI.getSuccessor(i));
228*9880d681SAndroid Build Coastguard Worker }
229*9880d681SAndroid Build Coastguard Worker 
visitPHINode(PHINode & PN)230*9880d681SAndroid Build Coastguard Worker void SparseSolver::visitPHINode(PHINode &PN) {
231*9880d681SAndroid Build Coastguard Worker   // The lattice function may store more information on a PHINode than could be
232*9880d681SAndroid Build Coastguard Worker   // computed from its incoming values.  For example, SSI form stores its sigma
233*9880d681SAndroid Build Coastguard Worker   // functions as PHINodes with a single incoming value.
234*9880d681SAndroid Build Coastguard Worker   if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
235*9880d681SAndroid Build Coastguard Worker     LatticeVal IV = LatticeFunc->ComputeInstructionState(PN, *this);
236*9880d681SAndroid Build Coastguard Worker     if (IV != LatticeFunc->getUntrackedVal())
237*9880d681SAndroid Build Coastguard Worker       UpdateState(PN, IV);
238*9880d681SAndroid Build Coastguard Worker     return;
239*9880d681SAndroid Build Coastguard Worker   }
240*9880d681SAndroid Build Coastguard Worker 
241*9880d681SAndroid Build Coastguard Worker   LatticeVal PNIV = getOrInitValueState(&PN);
242*9880d681SAndroid Build Coastguard Worker   LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
243*9880d681SAndroid Build Coastguard Worker 
244*9880d681SAndroid Build Coastguard Worker   // If this value is already overdefined (common) just return.
245*9880d681SAndroid Build Coastguard Worker   if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
246*9880d681SAndroid Build Coastguard Worker     return;  // Quick exit
247*9880d681SAndroid Build Coastguard Worker 
248*9880d681SAndroid Build Coastguard Worker   // Super-extra-high-degree PHI nodes are unlikely to ever be interesting,
249*9880d681SAndroid Build Coastguard Worker   // and slow us down a lot.  Just mark them overdefined.
250*9880d681SAndroid Build Coastguard Worker   if (PN.getNumIncomingValues() > 64) {
251*9880d681SAndroid Build Coastguard Worker     UpdateState(PN, Overdefined);
252*9880d681SAndroid Build Coastguard Worker     return;
253*9880d681SAndroid Build Coastguard Worker   }
254*9880d681SAndroid Build Coastguard Worker 
255*9880d681SAndroid Build Coastguard Worker   // Look at all of the executable operands of the PHI node.  If any of them
256*9880d681SAndroid Build Coastguard Worker   // are overdefined, the PHI becomes overdefined as well.  Otherwise, ask the
257*9880d681SAndroid Build Coastguard Worker   // transfer function to give us the merge of the incoming values.
258*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
259*9880d681SAndroid Build Coastguard Worker     // If the edge is not yet known to be feasible, it doesn't impact the PHI.
260*9880d681SAndroid Build Coastguard Worker     if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
261*9880d681SAndroid Build Coastguard Worker       continue;
262*9880d681SAndroid Build Coastguard Worker 
263*9880d681SAndroid Build Coastguard Worker     // Merge in this value.
264*9880d681SAndroid Build Coastguard Worker     LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
265*9880d681SAndroid Build Coastguard Worker     if (OpVal != PNIV)
266*9880d681SAndroid Build Coastguard Worker       PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
267*9880d681SAndroid Build Coastguard Worker 
268*9880d681SAndroid Build Coastguard Worker     if (PNIV == Overdefined)
269*9880d681SAndroid Build Coastguard Worker       break;  // Rest of input values don't matter.
270*9880d681SAndroid Build Coastguard Worker   }
271*9880d681SAndroid Build Coastguard Worker 
272*9880d681SAndroid Build Coastguard Worker   // Update the PHI with the compute value, which is the merge of the inputs.
273*9880d681SAndroid Build Coastguard Worker   UpdateState(PN, PNIV);
274*9880d681SAndroid Build Coastguard Worker }
275*9880d681SAndroid Build Coastguard Worker 
276*9880d681SAndroid Build Coastguard Worker 
visitInst(Instruction & I)277*9880d681SAndroid Build Coastguard Worker void SparseSolver::visitInst(Instruction &I) {
278*9880d681SAndroid Build Coastguard Worker   // PHIs are handled by the propagation logic, they are never passed into the
279*9880d681SAndroid Build Coastguard Worker   // transfer functions.
280*9880d681SAndroid Build Coastguard Worker   if (PHINode *PN = dyn_cast<PHINode>(&I))
281*9880d681SAndroid Build Coastguard Worker     return visitPHINode(*PN);
282*9880d681SAndroid Build Coastguard Worker 
283*9880d681SAndroid Build Coastguard Worker   // Otherwise, ask the transfer function what the result is.  If this is
284*9880d681SAndroid Build Coastguard Worker   // something that we care about, remember it.
285*9880d681SAndroid Build Coastguard Worker   LatticeVal IV = LatticeFunc->ComputeInstructionState(I, *this);
286*9880d681SAndroid Build Coastguard Worker   if (IV != LatticeFunc->getUntrackedVal())
287*9880d681SAndroid Build Coastguard Worker     UpdateState(I, IV);
288*9880d681SAndroid Build Coastguard Worker 
289*9880d681SAndroid Build Coastguard Worker   if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
290*9880d681SAndroid Build Coastguard Worker     visitTerminatorInst(*TI);
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker 
Solve(Function & F)293*9880d681SAndroid Build Coastguard Worker void SparseSolver::Solve(Function &F) {
294*9880d681SAndroid Build Coastguard Worker   MarkBlockExecutable(&F.getEntryBlock());
295*9880d681SAndroid Build Coastguard Worker 
296*9880d681SAndroid Build Coastguard Worker   // Process the work lists until they are empty!
297*9880d681SAndroid Build Coastguard Worker   while (!BBWorkList.empty() || !InstWorkList.empty()) {
298*9880d681SAndroid Build Coastguard Worker     // Process the instruction work list.
299*9880d681SAndroid Build Coastguard Worker     while (!InstWorkList.empty()) {
300*9880d681SAndroid Build Coastguard Worker       Instruction *I = InstWorkList.back();
301*9880d681SAndroid Build Coastguard Worker       InstWorkList.pop_back();
302*9880d681SAndroid Build Coastguard Worker 
303*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "\nPopped off I-WL: " << *I << "\n");
304*9880d681SAndroid Build Coastguard Worker 
305*9880d681SAndroid Build Coastguard Worker       // "I" got into the work list because it made a transition.  See if any
306*9880d681SAndroid Build Coastguard Worker       // users are both live and in need of updating.
307*9880d681SAndroid Build Coastguard Worker       for (User *U : I->users()) {
308*9880d681SAndroid Build Coastguard Worker         Instruction *UI = cast<Instruction>(U);
309*9880d681SAndroid Build Coastguard Worker         if (BBExecutable.count(UI->getParent()))   // Inst is executable?
310*9880d681SAndroid Build Coastguard Worker           visitInst(*UI);
311*9880d681SAndroid Build Coastguard Worker       }
312*9880d681SAndroid Build Coastguard Worker     }
313*9880d681SAndroid Build Coastguard Worker 
314*9880d681SAndroid Build Coastguard Worker     // Process the basic block work list.
315*9880d681SAndroid Build Coastguard Worker     while (!BBWorkList.empty()) {
316*9880d681SAndroid Build Coastguard Worker       BasicBlock *BB = BBWorkList.back();
317*9880d681SAndroid Build Coastguard Worker       BBWorkList.pop_back();
318*9880d681SAndroid Build Coastguard Worker 
319*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "\nPopped off BBWL: " << *BB);
320*9880d681SAndroid Build Coastguard Worker 
321*9880d681SAndroid Build Coastguard Worker       // Notify all instructions in this basic block that they are newly
322*9880d681SAndroid Build Coastguard Worker       // executable.
323*9880d681SAndroid Build Coastguard Worker       for (Instruction &I : *BB)
324*9880d681SAndroid Build Coastguard Worker         visitInst(I);
325*9880d681SAndroid Build Coastguard Worker     }
326*9880d681SAndroid Build Coastguard Worker   }
327*9880d681SAndroid Build Coastguard Worker }
328*9880d681SAndroid Build Coastguard Worker 
Print(Function & F,raw_ostream & OS) const329*9880d681SAndroid Build Coastguard Worker void SparseSolver::Print(Function &F, raw_ostream &OS) const {
330*9880d681SAndroid Build Coastguard Worker   OS << "\nFUNCTION: " << F.getName() << "\n";
331*9880d681SAndroid Build Coastguard Worker   for (auto &BB : F) {
332*9880d681SAndroid Build Coastguard Worker     if (!BBExecutable.count(&BB))
333*9880d681SAndroid Build Coastguard Worker       OS << "INFEASIBLE: ";
334*9880d681SAndroid Build Coastguard Worker     OS << "\t";
335*9880d681SAndroid Build Coastguard Worker     if (BB.hasName())
336*9880d681SAndroid Build Coastguard Worker       OS << BB.getName() << ":\n";
337*9880d681SAndroid Build Coastguard Worker     else
338*9880d681SAndroid Build Coastguard Worker       OS << "; anon bb\n";
339*9880d681SAndroid Build Coastguard Worker     for (auto &I : BB) {
340*9880d681SAndroid Build Coastguard Worker       LatticeFunc->PrintValue(getLatticeState(&I), OS);
341*9880d681SAndroid Build Coastguard Worker       OS << I << "\n";
342*9880d681SAndroid Build Coastguard Worker     }
343*9880d681SAndroid Build Coastguard Worker 
344*9880d681SAndroid Build Coastguard Worker     OS << "\n";
345*9880d681SAndroid Build Coastguard Worker   }
346*9880d681SAndroid Build Coastguard Worker }
347*9880d681SAndroid Build Coastguard Worker 
348