LLVM API Documentation
00001 //===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===// 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 the Correlated Value Propagation pass. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Transforms/Scalar.h" 00015 #include "llvm/ADT/Statistic.h" 00016 #include "llvm/Analysis/InstructionSimplify.h" 00017 #include "llvm/Analysis/LazyValueInfo.h" 00018 #include "llvm/IR/CFG.h" 00019 #include "llvm/IR/Constants.h" 00020 #include "llvm/IR/Function.h" 00021 #include "llvm/IR/Instructions.h" 00022 #include "llvm/Pass.h" 00023 #include "llvm/Support/Debug.h" 00024 #include "llvm/Support/raw_ostream.h" 00025 #include "llvm/Transforms/Utils/Local.h" 00026 using namespace llvm; 00027 00028 #define DEBUG_TYPE "correlated-value-propagation" 00029 00030 STATISTIC(NumPhis, "Number of phis propagated"); 00031 STATISTIC(NumSelects, "Number of selects propagated"); 00032 STATISTIC(NumMemAccess, "Number of memory access targets propagated"); 00033 STATISTIC(NumCmps, "Number of comparisons propagated"); 00034 STATISTIC(NumDeadCases, "Number of switch cases removed"); 00035 00036 namespace { 00037 class CorrelatedValuePropagation : public FunctionPass { 00038 LazyValueInfo *LVI; 00039 00040 bool processSelect(SelectInst *SI); 00041 bool processPHI(PHINode *P); 00042 bool processMemAccess(Instruction *I); 00043 bool processCmp(CmpInst *C); 00044 bool processSwitch(SwitchInst *SI); 00045 00046 public: 00047 static char ID; 00048 CorrelatedValuePropagation(): FunctionPass(ID) { 00049 initializeCorrelatedValuePropagationPass(*PassRegistry::getPassRegistry()); 00050 } 00051 00052 bool runOnFunction(Function &F) override; 00053 00054 void getAnalysisUsage(AnalysisUsage &AU) const override { 00055 AU.addRequired<LazyValueInfo>(); 00056 } 00057 }; 00058 } 00059 00060 char CorrelatedValuePropagation::ID = 0; 00061 INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation", 00062 "Value Propagation", false, false) 00063 INITIALIZE_PASS_DEPENDENCY(LazyValueInfo) 00064 INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation", 00065 "Value Propagation", false, false) 00066 00067 // Public interface to the Value Propagation pass 00068 Pass *llvm::createCorrelatedValuePropagationPass() { 00069 return new CorrelatedValuePropagation(); 00070 } 00071 00072 bool CorrelatedValuePropagation::processSelect(SelectInst *S) { 00073 if (S->getType()->isVectorTy()) return false; 00074 if (isa<Constant>(S->getOperand(0))) return false; 00075 00076 Constant *C = LVI->getConstant(S->getOperand(0), S->getParent(), S); 00077 if (!C) return false; 00078 00079 ConstantInt *CI = dyn_cast<ConstantInt>(C); 00080 if (!CI) return false; 00081 00082 Value *ReplaceWith = S->getOperand(1); 00083 Value *Other = S->getOperand(2); 00084 if (!CI->isOne()) std::swap(ReplaceWith, Other); 00085 if (ReplaceWith == S) ReplaceWith = UndefValue::get(S->getType()); 00086 00087 S->replaceAllUsesWith(ReplaceWith); 00088 S->eraseFromParent(); 00089 00090 ++NumSelects; 00091 00092 return true; 00093 } 00094 00095 bool CorrelatedValuePropagation::processPHI(PHINode *P) { 00096 bool Changed = false; 00097 00098 BasicBlock *BB = P->getParent(); 00099 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) { 00100 Value *Incoming = P->getIncomingValue(i); 00101 if (isa<Constant>(Incoming)) continue; 00102 00103 Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB, P); 00104 00105 // Look if the incoming value is a select with a constant but LVI tells us 00106 // that the incoming value can never be that constant. In that case replace 00107 // the incoming value with the other value of the select. This often allows 00108 // us to remove the select later. 00109 if (!V) { 00110 SelectInst *SI = dyn_cast<SelectInst>(Incoming); 00111 if (!SI) continue; 00112 00113 Constant *C = dyn_cast<Constant>(SI->getFalseValue()); 00114 if (!C) continue; 00115 00116 if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, 00117 P->getIncomingBlock(i), BB, P) != 00118 LazyValueInfo::False) 00119 continue; 00120 00121 DEBUG(dbgs() << "CVP: Threading PHI over " << *SI << '\n'); 00122 V = SI->getTrueValue(); 00123 } 00124 00125 P->setIncomingValue(i, V); 00126 Changed = true; 00127 } 00128 00129 // FIXME: Provide DL, TLI, DT, AT to SimplifyInstruction. 00130 if (Value *V = SimplifyInstruction(P)) { 00131 P->replaceAllUsesWith(V); 00132 P->eraseFromParent(); 00133 Changed = true; 00134 } 00135 00136 if (Changed) 00137 ++NumPhis; 00138 00139 return Changed; 00140 } 00141 00142 bool CorrelatedValuePropagation::processMemAccess(Instruction *I) { 00143 Value *Pointer = nullptr; 00144 if (LoadInst *L = dyn_cast<LoadInst>(I)) 00145 Pointer = L->getPointerOperand(); 00146 else 00147 Pointer = cast<StoreInst>(I)->getPointerOperand(); 00148 00149 if (isa<Constant>(Pointer)) return false; 00150 00151 Constant *C = LVI->getConstant(Pointer, I->getParent(), I); 00152 if (!C) return false; 00153 00154 ++NumMemAccess; 00155 I->replaceUsesOfWith(Pointer, C); 00156 return true; 00157 } 00158 00159 /// processCmp - If the value of this comparison could be determined locally, 00160 /// constant propagation would already have figured it out. Instead, walk 00161 /// the predecessors and statically evaluate the comparison based on information 00162 /// available on that edge. If a given static evaluation is true on ALL 00163 /// incoming edges, then it's true universally and we can simplify the compare. 00164 bool CorrelatedValuePropagation::processCmp(CmpInst *C) { 00165 Value *Op0 = C->getOperand(0); 00166 if (isa<Instruction>(Op0) && 00167 cast<Instruction>(Op0)->getParent() == C->getParent()) 00168 return false; 00169 00170 Constant *Op1 = dyn_cast<Constant>(C->getOperand(1)); 00171 if (!Op1) return false; 00172 00173 pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent()); 00174 if (PI == PE) return false; 00175 00176 LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(), 00177 C->getOperand(0), Op1, *PI, 00178 C->getParent(), C); 00179 if (Result == LazyValueInfo::Unknown) return false; 00180 00181 ++PI; 00182 while (PI != PE) { 00183 LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(), 00184 C->getOperand(0), Op1, *PI, 00185 C->getParent(), C); 00186 if (Res != Result) return false; 00187 ++PI; 00188 } 00189 00190 ++NumCmps; 00191 00192 if (Result == LazyValueInfo::True) 00193 C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext())); 00194 else 00195 C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext())); 00196 00197 C->eraseFromParent(); 00198 00199 return true; 00200 } 00201 00202 /// processSwitch - Simplify a switch instruction by removing cases which can 00203 /// never fire. If the uselessness of a case could be determined locally then 00204 /// constant propagation would already have figured it out. Instead, walk the 00205 /// predecessors and statically evaluate cases based on information available 00206 /// on that edge. Cases that cannot fire no matter what the incoming edge can 00207 /// safely be removed. If a case fires on every incoming edge then the entire 00208 /// switch can be removed and replaced with a branch to the case destination. 00209 bool CorrelatedValuePropagation::processSwitch(SwitchInst *SI) { 00210 Value *Cond = SI->getCondition(); 00211 BasicBlock *BB = SI->getParent(); 00212 00213 // If the condition was defined in same block as the switch then LazyValueInfo 00214 // currently won't say anything useful about it, though in theory it could. 00215 if (isa<Instruction>(Cond) && cast<Instruction>(Cond)->getParent() == BB) 00216 return false; 00217 00218 // If the switch is unreachable then trying to improve it is a waste of time. 00219 pred_iterator PB = pred_begin(BB), PE = pred_end(BB); 00220 if (PB == PE) return false; 00221 00222 // Analyse each switch case in turn. This is done in reverse order so that 00223 // removing a case doesn't cause trouble for the iteration. 00224 bool Changed = false; 00225 for (SwitchInst::CaseIt CI = SI->case_end(), CE = SI->case_begin(); CI-- != CE; 00226 ) { 00227 ConstantInt *Case = CI.getCaseValue(); 00228 00229 // Check to see if the switch condition is equal to/not equal to the case 00230 // value on every incoming edge, equal/not equal being the same each time. 00231 LazyValueInfo::Tristate State = LazyValueInfo::Unknown; 00232 for (pred_iterator PI = PB; PI != PE; ++PI) { 00233 // Is the switch condition equal to the case value? 00234 LazyValueInfo::Tristate Value = LVI->getPredicateOnEdge(CmpInst::ICMP_EQ, 00235 Cond, Case, *PI, 00236 BB, SI); 00237 // Give up on this case if nothing is known. 00238 if (Value == LazyValueInfo::Unknown) { 00239 State = LazyValueInfo::Unknown; 00240 break; 00241 } 00242 00243 // If this was the first edge to be visited, record that all other edges 00244 // need to give the same result. 00245 if (PI == PB) { 00246 State = Value; 00247 continue; 00248 } 00249 00250 // If this case is known to fire for some edges and known not to fire for 00251 // others then there is nothing we can do - give up. 00252 if (Value != State) { 00253 State = LazyValueInfo::Unknown; 00254 break; 00255 } 00256 } 00257 00258 if (State == LazyValueInfo::False) { 00259 // This case never fires - remove it. 00260 CI.getCaseSuccessor()->removePredecessor(BB); 00261 SI->removeCase(CI); // Does not invalidate the iterator. 00262 00263 // The condition can be modified by removePredecessor's PHI simplification 00264 // logic. 00265 Cond = SI->getCondition(); 00266 00267 ++NumDeadCases; 00268 Changed = true; 00269 } else if (State == LazyValueInfo::True) { 00270 // This case always fires. Arrange for the switch to be turned into an 00271 // unconditional branch by replacing the switch condition with the case 00272 // value. 00273 SI->setCondition(Case); 00274 NumDeadCases += SI->getNumCases(); 00275 Changed = true; 00276 break; 00277 } 00278 } 00279 00280 if (Changed) 00281 // If the switch has been simplified to the point where it can be replaced 00282 // by a branch then do so now. 00283 ConstantFoldTerminator(BB); 00284 00285 return Changed; 00286 } 00287 00288 bool CorrelatedValuePropagation::runOnFunction(Function &F) { 00289 if (skipOptnoneFunction(F)) 00290 return false; 00291 00292 LVI = &getAnalysis<LazyValueInfo>(); 00293 00294 bool FnChanged = false; 00295 00296 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) { 00297 bool BBChanged = false; 00298 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) { 00299 Instruction *II = BI++; 00300 switch (II->getOpcode()) { 00301 case Instruction::Select: 00302 BBChanged |= processSelect(cast<SelectInst>(II)); 00303 break; 00304 case Instruction::PHI: 00305 BBChanged |= processPHI(cast<PHINode>(II)); 00306 break; 00307 case Instruction::ICmp: 00308 case Instruction::FCmp: 00309 BBChanged |= processCmp(cast<CmpInst>(II)); 00310 break; 00311 case Instruction::Load: 00312 case Instruction::Store: 00313 BBChanged |= processMemAccess(II); 00314 break; 00315 } 00316 } 00317 00318 Instruction *Term = FI->getTerminator(); 00319 switch (Term->getOpcode()) { 00320 case Instruction::Switch: 00321 BBChanged |= processSwitch(cast<SwitchInst>(Term)); 00322 break; 00323 } 00324 00325 FnChanged |= BBChanged; 00326 } 00327 00328 return FnChanged; 00329 }