Home | History | Annotate | Download | only in Scalar
      1 //===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
      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 the Correlated Value Propagation pass.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/Transforms/Scalar.h"
     15 #include "llvm/ADT/Statistic.h"
     16 #include "llvm/Analysis/InstructionSimplify.h"
     17 #include "llvm/Analysis/LazyValueInfo.h"
     18 #include "llvm/IR/CFG.h"
     19 #include "llvm/IR/Constants.h"
     20 #include "llvm/IR/Function.h"
     21 #include "llvm/IR/Instructions.h"
     22 #include "llvm/Pass.h"
     23 #include "llvm/Support/Debug.h"
     24 #include "llvm/Support/raw_ostream.h"
     25 #include "llvm/Transforms/Utils/Local.h"
     26 using namespace llvm;
     27 
     28 #define DEBUG_TYPE "correlated-value-propagation"
     29 
     30 STATISTIC(NumPhis,      "Number of phis propagated");
     31 STATISTIC(NumSelects,   "Number of selects propagated");
     32 STATISTIC(NumMemAccess, "Number of memory access targets propagated");
     33 STATISTIC(NumCmps,      "Number of comparisons propagated");
     34 STATISTIC(NumDeadCases, "Number of switch cases removed");
     35 
     36 namespace {
     37   class CorrelatedValuePropagation : public FunctionPass {
     38     LazyValueInfo *LVI;
     39 
     40     bool processSelect(SelectInst *SI);
     41     bool processPHI(PHINode *P);
     42     bool processMemAccess(Instruction *I);
     43     bool processCmp(CmpInst *C);
     44     bool processSwitch(SwitchInst *SI);
     45 
     46   public:
     47     static char ID;
     48     CorrelatedValuePropagation(): FunctionPass(ID) {
     49      initializeCorrelatedValuePropagationPass(*PassRegistry::getPassRegistry());
     50     }
     51 
     52     bool runOnFunction(Function &F) override;
     53 
     54     void getAnalysisUsage(AnalysisUsage &AU) const override {
     55       AU.addRequired<LazyValueInfo>();
     56     }
     57   };
     58 }
     59 
     60 char CorrelatedValuePropagation::ID = 0;
     61 INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation",
     62                 "Value Propagation", false, false)
     63 INITIALIZE_PASS_DEPENDENCY(LazyValueInfo)
     64 INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation",
     65                 "Value Propagation", false, false)
     66 
     67 // Public interface to the Value Propagation pass
     68 Pass *llvm::createCorrelatedValuePropagationPass() {
     69   return new CorrelatedValuePropagation();
     70 }
     71 
     72 bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
     73   if (S->getType()->isVectorTy()) return false;
     74   if (isa<Constant>(S->getOperand(0))) return false;
     75 
     76   Constant *C = LVI->getConstant(S->getOperand(0), S->getParent());
     77   if (!C) return false;
     78 
     79   ConstantInt *CI = dyn_cast<ConstantInt>(C);
     80   if (!CI) return false;
     81 
     82   Value *ReplaceWith = S->getOperand(1);
     83   Value *Other = S->getOperand(2);
     84   if (!CI->isOne()) std::swap(ReplaceWith, Other);
     85   if (ReplaceWith == S) ReplaceWith = UndefValue::get(S->getType());
     86 
     87   S->replaceAllUsesWith(ReplaceWith);
     88   S->eraseFromParent();
     89 
     90   ++NumSelects;
     91 
     92   return true;
     93 }
     94 
     95 bool CorrelatedValuePropagation::processPHI(PHINode *P) {
     96   bool Changed = false;
     97 
     98   BasicBlock *BB = P->getParent();
     99   for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
    100     Value *Incoming = P->getIncomingValue(i);
    101     if (isa<Constant>(Incoming)) continue;
    102 
    103     Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB);
    104 
    105     // Look if the incoming value is a select with a constant but LVI tells us
    106     // that the incoming value can never be that constant. In that case replace
    107     // the incoming value with the other value of the select. This often allows
    108     // us to remove the select later.
    109     if (!V) {
    110       SelectInst *SI = dyn_cast<SelectInst>(Incoming);
    111       if (!SI) continue;
    112 
    113       Constant *C = dyn_cast<Constant>(SI->getFalseValue());
    114       if (!C) continue;
    115 
    116       if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C,
    117                                   P->getIncomingBlock(i), BB) !=
    118           LazyValueInfo::False)
    119         continue;
    120 
    121       DEBUG(dbgs() << "CVP: Threading PHI over " << *SI << '\n');
    122       V = SI->getTrueValue();
    123     }
    124 
    125     P->setIncomingValue(i, V);
    126     Changed = true;
    127   }
    128 
    129   if (Value *V = SimplifyInstruction(P)) {
    130     P->replaceAllUsesWith(V);
    131     P->eraseFromParent();
    132     Changed = true;
    133   }
    134 
    135   if (Changed)
    136     ++NumPhis;
    137 
    138   return Changed;
    139 }
    140 
    141 bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
    142   Value *Pointer = nullptr;
    143   if (LoadInst *L = dyn_cast<LoadInst>(I))
    144     Pointer = L->getPointerOperand();
    145   else
    146     Pointer = cast<StoreInst>(I)->getPointerOperand();
    147 
    148   if (isa<Constant>(Pointer)) return false;
    149 
    150   Constant *C = LVI->getConstant(Pointer, I->getParent());
    151   if (!C) return false;
    152 
    153   ++NumMemAccess;
    154   I->replaceUsesOfWith(Pointer, C);
    155   return true;
    156 }
    157 
    158 /// processCmp - If the value of this comparison could be determined locally,
    159 /// constant propagation would already have figured it out.  Instead, walk
    160 /// the predecessors and statically evaluate the comparison based on information
    161 /// available on that edge.  If a given static evaluation is true on ALL
    162 /// incoming edges, then it's true universally and we can simplify the compare.
    163 bool CorrelatedValuePropagation::processCmp(CmpInst *C) {
    164   Value *Op0 = C->getOperand(0);
    165   if (isa<Instruction>(Op0) &&
    166       cast<Instruction>(Op0)->getParent() == C->getParent())
    167     return false;
    168 
    169   Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
    170   if (!Op1) return false;
    171 
    172   pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent());
    173   if (PI == PE) return false;
    174 
    175   LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(),
    176                                     C->getOperand(0), Op1, *PI, C->getParent());
    177   if (Result == LazyValueInfo::Unknown) return false;
    178 
    179   ++PI;
    180   while (PI != PE) {
    181     LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(),
    182                                     C->getOperand(0), Op1, *PI, C->getParent());
    183     if (Res != Result) return false;
    184     ++PI;
    185   }
    186 
    187   ++NumCmps;
    188 
    189   if (Result == LazyValueInfo::True)
    190     C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext()));
    191   else
    192     C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext()));
    193 
    194   C->eraseFromParent();
    195 
    196   return true;
    197 }
    198 
    199 /// processSwitch - Simplify a switch instruction by removing cases which can
    200 /// never fire.  If the uselessness of a case could be determined locally then
    201 /// constant propagation would already have figured it out.  Instead, walk the
    202 /// predecessors and statically evaluate cases based on information available
    203 /// on that edge.  Cases that cannot fire no matter what the incoming edge can
    204 /// safely be removed.  If a case fires on every incoming edge then the entire
    205 /// switch can be removed and replaced with a branch to the case destination.
    206 bool CorrelatedValuePropagation::processSwitch(SwitchInst *SI) {
    207   Value *Cond = SI->getCondition();
    208   BasicBlock *BB = SI->getParent();
    209 
    210   // If the condition was defined in same block as the switch then LazyValueInfo
    211   // currently won't say anything useful about it, though in theory it could.
    212   if (isa<Instruction>(Cond) && cast<Instruction>(Cond)->getParent() == BB)
    213     return false;
    214 
    215   // If the switch is unreachable then trying to improve it is a waste of time.
    216   pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
    217   if (PB == PE) return false;
    218 
    219   // Analyse each switch case in turn.  This is done in reverse order so that
    220   // removing a case doesn't cause trouble for the iteration.
    221   bool Changed = false;
    222   for (SwitchInst::CaseIt CI = SI->case_end(), CE = SI->case_begin(); CI-- != CE;
    223        ) {
    224     ConstantInt *Case = CI.getCaseValue();
    225 
    226     // Check to see if the switch condition is equal to/not equal to the case
    227     // value on every incoming edge, equal/not equal being the same each time.
    228     LazyValueInfo::Tristate State = LazyValueInfo::Unknown;
    229     for (pred_iterator PI = PB; PI != PE; ++PI) {
    230       // Is the switch condition equal to the case value?
    231       LazyValueInfo::Tristate Value = LVI->getPredicateOnEdge(CmpInst::ICMP_EQ,
    232                                                               Cond, Case, *PI, BB);
    233       // Give up on this case if nothing is known.
    234       if (Value == LazyValueInfo::Unknown) {
    235         State = LazyValueInfo::Unknown;
    236         break;
    237       }
    238 
    239       // If this was the first edge to be visited, record that all other edges
    240       // need to give the same result.
    241       if (PI == PB) {
    242         State = Value;
    243         continue;
    244       }
    245 
    246       // If this case is known to fire for some edges and known not to fire for
    247       // others then there is nothing we can do - give up.
    248       if (Value != State) {
    249         State = LazyValueInfo::Unknown;
    250         break;
    251       }
    252     }
    253 
    254     if (State == LazyValueInfo::False) {
    255       // This case never fires - remove it.
    256       CI.getCaseSuccessor()->removePredecessor(BB);
    257       SI->removeCase(CI); // Does not invalidate the iterator.
    258 
    259       // The condition can be modified by removePredecessor's PHI simplification
    260       // logic.
    261       Cond = SI->getCondition();
    262 
    263       ++NumDeadCases;
    264       Changed = true;
    265     } else if (State == LazyValueInfo::True) {
    266       // This case always fires.  Arrange for the switch to be turned into an
    267       // unconditional branch by replacing the switch condition with the case
    268       // value.
    269       SI->setCondition(Case);
    270       NumDeadCases += SI->getNumCases();
    271       Changed = true;
    272       break;
    273     }
    274   }
    275 
    276   if (Changed)
    277     // If the switch has been simplified to the point where it can be replaced
    278     // by a branch then do so now.
    279     ConstantFoldTerminator(BB);
    280 
    281   return Changed;
    282 }
    283 
    284 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
    285   if (skipOptnoneFunction(F))
    286     return false;
    287 
    288   LVI = &getAnalysis<LazyValueInfo>();
    289 
    290   bool FnChanged = false;
    291 
    292   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
    293     bool BBChanged = false;
    294     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
    295       Instruction *II = BI++;
    296       switch (II->getOpcode()) {
    297       case Instruction::Select:
    298         BBChanged |= processSelect(cast<SelectInst>(II));
    299         break;
    300       case Instruction::PHI:
    301         BBChanged |= processPHI(cast<PHINode>(II));
    302         break;
    303       case Instruction::ICmp:
    304       case Instruction::FCmp:
    305         BBChanged |= processCmp(cast<CmpInst>(II));
    306         break;
    307       case Instruction::Load:
    308       case Instruction::Store:
    309         BBChanged |= processMemAccess(II);
    310         break;
    311       }
    312     }
    313 
    314     Instruction *Term = FI->getTerminator();
    315     switch (Term->getOpcode()) {
    316     case Instruction::Switch:
    317       BBChanged |= processSwitch(cast<SwitchInst>(Term));
    318       break;
    319     }
    320 
    321     FnChanged |= BBChanged;
    322   }
    323 
    324   return FnChanged;
    325 }
    326