LLVM API Documentation
00001 //===- SparsePropagation.cpp - Sparse Conditional Property Propagation ----===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements an abstract sparse conditional propagation algorithm, 00011 // modeled after SCCP, but with a customizable lattice function. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "sparseprop" 00016 #include "llvm/Analysis/SparsePropagation.h" 00017 #include "llvm/Constants.h" 00018 #include "llvm/Function.h" 00019 #include "llvm/Instructions.h" 00020 #include "llvm/LLVMContext.h" 00021 #include "llvm/Support/Debug.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 using namespace llvm; 00024 00025 //===----------------------------------------------------------------------===// 00026 // AbstractLatticeFunction Implementation 00027 //===----------------------------------------------------------------------===// 00028 00029 AbstractLatticeFunction::~AbstractLatticeFunction() {} 00030 00031 /// PrintValue - Render the specified lattice value to the specified stream. 00032 void AbstractLatticeFunction::PrintValue(LatticeVal V, std::ostream &OS) { 00033 if (V == UndefVal) 00034 OS << "undefined"; 00035 else if (V == OverdefinedVal) 00036 OS << "overdefined"; 00037 else if (V == UntrackedVal) 00038 OS << "untracked"; 00039 else 00040 OS << "unknown lattice value"; 00041 } 00042 00043 //===----------------------------------------------------------------------===// 00044 // SparseSolver Implementation 00045 //===----------------------------------------------------------------------===// 00046 00047 /// getOrInitValueState - Return the LatticeVal object that corresponds to the 00048 /// value, initializing the value's state if it hasn't been entered into the 00049 /// map yet. This function is necessary because not all values should start 00050 /// out in the underdefined state... Arguments should be overdefined, and 00051 /// constants should be marked as constants. 00052 /// 00053 SparseSolver::LatticeVal SparseSolver::getOrInitValueState(Value *V) { 00054 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V); 00055 if (I != ValueState.end()) return I->second; // Common case, in the map 00056 00057 LatticeVal LV; 00058 if (LatticeFunc->IsUntrackedValue(V)) 00059 return LatticeFunc->getUntrackedVal(); 00060 else if (Constant *C = dyn_cast<Constant>(V)) 00061 LV = LatticeFunc->ComputeConstant(C); 00062 else if (Argument *A = dyn_cast<Argument>(V)) 00063 LV = LatticeFunc->ComputeArgument(A); 00064 else if (!isa<Instruction>(V)) 00065 // All other non-instructions are overdefined. 00066 LV = LatticeFunc->getOverdefinedVal(); 00067 else 00068 // All instructions are underdefined by default. 00069 LV = LatticeFunc->getUndefVal(); 00070 00071 // If this value is untracked, don't add it to the map. 00072 if (LV == LatticeFunc->getUntrackedVal()) 00073 return LV; 00074 return ValueState[V] = LV; 00075 } 00076 00077 /// UpdateState - When the state for some instruction is potentially updated, 00078 /// this function notices and adds I to the worklist if needed. 00079 void SparseSolver::UpdateState(Instruction &Inst, LatticeVal V) { 00080 DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(&Inst); 00081 if (I != ValueState.end() && I->second == V) 00082 return; // No change. 00083 00084 // An update. Visit uses of I. 00085 ValueState[&Inst] = V; 00086 InstWorkList.push_back(&Inst); 00087 } 00088 00089 /// MarkBlockExecutable - This method can be used by clients to mark all of 00090 /// the blocks that are known to be intrinsically live in the processed unit. 00091 void SparseSolver::MarkBlockExecutable(BasicBlock *BB) { 00092 DEBUG(errs() << "Marking Block Executable: " << BB->getName() << "\n"); 00093 BBExecutable.insert(BB); // Basic block is executable! 00094 BBWorkList.push_back(BB); // Add the block to the work list! 00095 } 00096 00097 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB 00098 /// work list if it is not already executable... 00099 void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) { 00100 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second) 00101 return; // This edge is already known to be executable! 00102 00103 DEBUG(errs() << "Marking Edge Executable: " << Source->getName() 00104 << " -> " << Dest->getName() << "\n"); 00105 00106 if (BBExecutable.count(Dest)) { 00107 // The destination is already executable, but we just made an edge 00108 // feasible that wasn't before. Revisit the PHI nodes in the block 00109 // because they have potentially new operands. 00110 for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I) 00111 visitPHINode(*cast<PHINode>(I)); 00112 00113 } else { 00114 MarkBlockExecutable(Dest); 00115 } 00116 } 00117 00118 00119 /// getFeasibleSuccessors - Return a vector of booleans to indicate which 00120 /// successors are reachable from a given terminator instruction. 00121 void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI, 00122 SmallVectorImpl<bool> &Succs, 00123 bool AggressiveUndef) { 00124 Succs.resize(TI.getNumSuccessors()); 00125 if (TI.getNumSuccessors() == 0) return; 00126 00127 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) { 00128 if (BI->isUnconditional()) { 00129 Succs[0] = true; 00130 return; 00131 } 00132 00133 LatticeVal BCValue; 00134 if (AggressiveUndef) 00135 BCValue = getOrInitValueState(BI->getCondition()); 00136 else 00137 BCValue = getLatticeState(BI->getCondition()); 00138 00139 if (BCValue == LatticeFunc->getOverdefinedVal() || 00140 BCValue == LatticeFunc->getUntrackedVal()) { 00141 // Overdefined condition variables can branch either way. 00142 Succs[0] = Succs[1] = true; 00143 return; 00144 } 00145 00146 // If undefined, neither is feasible yet. 00147 if (BCValue == LatticeFunc->getUndefVal()) 00148 return; 00149 00150 Constant *C = LatticeFunc->GetConstant(BCValue, BI->getCondition(), *this); 00151 if (C == 0 || !isa<ConstantInt>(C)) { 00152 // Non-constant values can go either way. 00153 Succs[0] = Succs[1] = true; 00154 return; 00155 } 00156 00157 // Constant condition variables mean the branch can only go a single way 00158 Succs[C == ConstantInt::getFalse(*Context)] = true; 00159 return; 00160 } 00161 00162 if (isa<InvokeInst>(TI)) { 00163 // Invoke instructions successors are always executable. 00164 // TODO: Could ask the lattice function if the value can throw. 00165 Succs[0] = Succs[1] = true; 00166 return; 00167 } 00168 00169 SwitchInst &SI = cast<SwitchInst>(TI); 00170 LatticeVal SCValue; 00171 if (AggressiveUndef) 00172 SCValue = getOrInitValueState(SI.getCondition()); 00173 else 00174 SCValue = getLatticeState(SI.getCondition()); 00175 00176 if (SCValue == LatticeFunc->getOverdefinedVal() || 00177 SCValue == LatticeFunc->getUntrackedVal()) { 00178 // All destinations are executable! 00179 Succs.assign(TI.getNumSuccessors(), true); 00180 return; 00181 } 00182 00183 // If undefined, neither is feasible yet. 00184 if (SCValue == LatticeFunc->getUndefVal()) 00185 return; 00186 00187 Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this); 00188 if (C == 0 || !isa<ConstantInt>(C)) { 00189 // All destinations are executable! 00190 Succs.assign(TI.getNumSuccessors(), true); 00191 return; 00192 } 00193 00194 Succs[SI.findCaseValue(cast<ConstantInt>(C))] = true; 00195 } 00196 00197 00198 /// isEdgeFeasible - Return true if the control flow edge from the 'From' 00199 /// basic block to the 'To' basic block is currently feasible... 00200 bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To, 00201 bool AggressiveUndef) { 00202 SmallVector<bool, 16> SuccFeasible; 00203 TerminatorInst *TI = From->getTerminator(); 00204 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef); 00205 00206 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 00207 if (TI->getSuccessor(i) == To && SuccFeasible[i]) 00208 return true; 00209 00210 return false; 00211 } 00212 00213 void SparseSolver::visitTerminatorInst(TerminatorInst &TI) { 00214 SmallVector<bool, 16> SuccFeasible; 00215 getFeasibleSuccessors(TI, SuccFeasible, true); 00216 00217 BasicBlock *BB = TI.getParent(); 00218 00219 // Mark all feasible successors executable... 00220 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i) 00221 if (SuccFeasible[i]) 00222 markEdgeExecutable(BB, TI.getSuccessor(i)); 00223 } 00224 00225 void SparseSolver::visitPHINode(PHINode &PN) { 00226 LatticeVal PNIV = getOrInitValueState(&PN); 00227 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal(); 00228 00229 // If this value is already overdefined (common) just return. 00230 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal()) 00231 return; // Quick exit 00232 00233 // Super-extra-high-degree PHI nodes are unlikely to ever be interesting, 00234 // and slow us down a lot. Just mark them overdefined. 00235 if (PN.getNumIncomingValues() > 64) { 00236 UpdateState(PN, Overdefined); 00237 return; 00238 } 00239 00240 // Look at all of the executable operands of the PHI node. If any of them 00241 // are overdefined, the PHI becomes overdefined as well. Otherwise, ask the 00242 // transfer function to give us the merge of the incoming values. 00243 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 00244 // If the edge is not yet known to be feasible, it doesn't impact the PHI. 00245 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true)) 00246 continue; 00247 00248 // Merge in this value. 00249 LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i)); 00250 if (OpVal != PNIV) 00251 PNIV = LatticeFunc->MergeValues(PNIV, OpVal); 00252 00253 if (PNIV == Overdefined) 00254 break; // Rest of input values don't matter. 00255 } 00256 00257 // Update the PHI with the compute value, which is the merge of the inputs. 00258 UpdateState(PN, PNIV); 00259 } 00260 00261 00262 void SparseSolver::visitInst(Instruction &I) { 00263 // PHIs are handled by the propagation logic, they are never passed into the 00264 // transfer functions. 00265 if (PHINode *PN = dyn_cast<PHINode>(&I)) 00266 return visitPHINode(*PN); 00267 00268 // Otherwise, ask the transfer function what the result is. If this is 00269 // something that we care about, remember it. 00270 LatticeVal IV = LatticeFunc->ComputeInstructionState(I, *this); 00271 if (IV != LatticeFunc->getUntrackedVal()) 00272 UpdateState(I, IV); 00273 00274 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I)) 00275 visitTerminatorInst(*TI); 00276 } 00277 00278 void SparseSolver::Solve(Function &F) { 00279 MarkBlockExecutable(&F.getEntryBlock()); 00280 00281 // Process the work lists until they are empty! 00282 while (!BBWorkList.empty() || !InstWorkList.empty()) { 00283 // Process the instruction work list. 00284 while (!InstWorkList.empty()) { 00285 Instruction *I = InstWorkList.back(); 00286 InstWorkList.pop_back(); 00287 00288 DEBUG(errs() << "\nPopped off I-WL: " << *I); 00289 00290 // "I" got into the work list because it made a transition. See if any 00291 // users are both live and in need of updating. 00292 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 00293 UI != E; ++UI) { 00294 Instruction *U = cast<Instruction>(*UI); 00295 if (BBExecutable.count(U->getParent())) // Inst is executable? 00296 visitInst(*U); 00297 } 00298 } 00299 00300 // Process the basic block work list. 00301 while (!BBWorkList.empty()) { 00302 BasicBlock *BB = BBWorkList.back(); 00303 BBWorkList.pop_back(); 00304 00305 DEBUG(errs() << "\nPopped off BBWL: " << *BB); 00306 00307 // Notify all instructions in this basic block that they are newly 00308 // executable. 00309 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 00310 visitInst(*I); 00311 } 00312 } 00313 } 00314 00315 void SparseSolver::Print(Function &F, std::ostream &OS) const { 00316 OS << "\nFUNCTION: " << F.getNameStr() << "\n"; 00317 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { 00318 if (!BBExecutable.count(BB)) 00319 OS << "INFEASIBLE: "; 00320 OS << "\t"; 00321 if (BB->hasName()) 00322 OS << BB->getNameStr() << ":\n"; 00323 else 00324 OS << "; anon bb\n"; 00325 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 00326 LatticeFunc->PrintValue(getLatticeState(I), OS); 00327 OS << *I; 00328 } 00329 00330 OS << "\n"; 00331 } 00332 } 00333