Home | History | Annotate | Download | only in Analysis
      1 //===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===//
      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 PHITransAddr class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/Analysis/PHITransAddr.h"
     15 #include "llvm/Constants.h"
     16 #include "llvm/Instructions.h"
     17 #include "llvm/Analysis/Dominators.h"
     18 #include "llvm/Analysis/InstructionSimplify.h"
     19 #include "llvm/Support/Debug.h"
     20 #include "llvm/Support/ErrorHandling.h"
     21 #include "llvm/Support/raw_ostream.h"
     22 using namespace llvm;
     23 
     24 static bool CanPHITrans(Instruction *Inst) {
     25   if (isa<PHINode>(Inst) ||
     26       isa<GetElementPtrInst>(Inst))
     27     return true;
     28 
     29   if (isa<CastInst>(Inst) &&
     30       Inst->isSafeToSpeculativelyExecute())
     31     return true;
     32 
     33   if (Inst->getOpcode() == Instruction::Add &&
     34       isa<ConstantInt>(Inst->getOperand(1)))
     35     return true;
     36 
     37   //   cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
     38   //   if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
     39   //     cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
     40   return false;
     41 }
     42 
     43 void PHITransAddr::dump() const {
     44   if (Addr == 0) {
     45     dbgs() << "PHITransAddr: null\n";
     46     return;
     47   }
     48   dbgs() << "PHITransAddr: " << *Addr << "\n";
     49   for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
     50     dbgs() << "  Input #" << i << " is " << *InstInputs[i] << "\n";
     51 }
     52 
     53 
     54 static bool VerifySubExpr(Value *Expr,
     55                           SmallVectorImpl<Instruction*> &InstInputs) {
     56   // If this is a non-instruction value, there is nothing to do.
     57   Instruction *I = dyn_cast<Instruction>(Expr);
     58   if (I == 0) return true;
     59 
     60   // If it's an instruction, it is either in Tmp or its operands recursively
     61   // are.
     62   SmallVectorImpl<Instruction*>::iterator Entry =
     63     std::find(InstInputs.begin(), InstInputs.end(), I);
     64   if (Entry != InstInputs.end()) {
     65     InstInputs.erase(Entry);
     66     return true;
     67   }
     68 
     69   // If it isn't in the InstInputs list it is a subexpr incorporated into the
     70   // address.  Sanity check that it is phi translatable.
     71   if (!CanPHITrans(I)) {
     72     errs() << "Non phi translatable instruction found in PHITransAddr:\n";
     73     errs() << *I << '\n';
     74     llvm_unreachable("Either something is missing from InstInputs or "
     75                      "CanPHITrans is wrong.");
     76     return false;
     77   }
     78 
     79   // Validate the operands of the instruction.
     80   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
     81     if (!VerifySubExpr(I->getOperand(i), InstInputs))
     82       return false;
     83 
     84   return true;
     85 }
     86 
     87 /// Verify - Check internal consistency of this data structure.  If the
     88 /// structure is valid, it returns true.  If invalid, it prints errors and
     89 /// returns false.
     90 bool PHITransAddr::Verify() const {
     91   if (Addr == 0) return true;
     92 
     93   SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end());
     94 
     95   if (!VerifySubExpr(Addr, Tmp))
     96     return false;
     97 
     98   if (!Tmp.empty()) {
     99     errs() << "PHITransAddr contains extra instructions:\n";
    100     for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
    101       errs() << "  InstInput #" << i << " is " << *InstInputs[i] << "\n";
    102     llvm_unreachable("This is unexpected.");
    103     return false;
    104   }
    105 
    106   // a-ok.
    107   return true;
    108 }
    109 
    110 
    111 /// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
    112 /// if we have some hope of doing it.  This should be used as a filter to
    113 /// avoid calling PHITranslateValue in hopeless situations.
    114 bool PHITransAddr::IsPotentiallyPHITranslatable() const {
    115   // If the input value is not an instruction, or if it is not defined in CurBB,
    116   // then we don't need to phi translate it.
    117   Instruction *Inst = dyn_cast<Instruction>(Addr);
    118   return Inst == 0 || CanPHITrans(Inst);
    119 }
    120 
    121 
    122 static void RemoveInstInputs(Value *V,
    123                              SmallVectorImpl<Instruction*> &InstInputs) {
    124   Instruction *I = dyn_cast<Instruction>(V);
    125   if (I == 0) return;
    126 
    127   // If the instruction is in the InstInputs list, remove it.
    128   SmallVectorImpl<Instruction*>::iterator Entry =
    129     std::find(InstInputs.begin(), InstInputs.end(), I);
    130   if (Entry != InstInputs.end()) {
    131     InstInputs.erase(Entry);
    132     return;
    133   }
    134 
    135   assert(!isa<PHINode>(I) && "Error, removing something that isn't an input");
    136 
    137   // Otherwise, it must have instruction inputs itself.  Zap them recursively.
    138   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
    139     if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
    140       RemoveInstInputs(Op, InstInputs);
    141   }
    142 }
    143 
    144 Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
    145                                          BasicBlock *PredBB,
    146                                          const DominatorTree *DT) {
    147   // If this is a non-instruction value, it can't require PHI translation.
    148   Instruction *Inst = dyn_cast<Instruction>(V);
    149   if (Inst == 0) return V;
    150 
    151   // Determine whether 'Inst' is an input to our PHI translatable expression.
    152   bool isInput = std::count(InstInputs.begin(), InstInputs.end(), Inst);
    153 
    154   // Handle inputs instructions if needed.
    155   if (isInput) {
    156     if (Inst->getParent() != CurBB) {
    157       // If it is an input defined in a different block, then it remains an
    158       // input.
    159       return Inst;
    160     }
    161 
    162     // If 'Inst' is defined in this block and is an input that needs to be phi
    163     // translated, we need to incorporate the value into the expression or fail.
    164 
    165     // In either case, the instruction itself isn't an input any longer.
    166     InstInputs.erase(std::find(InstInputs.begin(), InstInputs.end(), Inst));
    167 
    168     // If this is a PHI, go ahead and translate it.
    169     if (PHINode *PN = dyn_cast<PHINode>(Inst))
    170       return AddAsInput(PN->getIncomingValueForBlock(PredBB));
    171 
    172     // If this is a non-phi value, and it is analyzable, we can incorporate it
    173     // into the expression by making all instruction operands be inputs.
    174     if (!CanPHITrans(Inst))
    175       return 0;
    176 
    177     // All instruction operands are now inputs (and of course, they may also be
    178     // defined in this block, so they may need to be phi translated themselves.
    179     for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
    180       if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i)))
    181         InstInputs.push_back(Op);
    182   }
    183 
    184   // Ok, it must be an intermediate result (either because it started that way
    185   // or because we just incorporated it into the expression).  See if its
    186   // operands need to be phi translated, and if so, reconstruct it.
    187 
    188   if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
    189     if (!Cast->isSafeToSpeculativelyExecute()) return 0;
    190     Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
    191     if (PHIIn == 0) return 0;
    192     if (PHIIn == Cast->getOperand(0))
    193       return Cast;
    194 
    195     // Find an available version of this cast.
    196 
    197     // Constants are trivial to find.
    198     if (Constant *C = dyn_cast<Constant>(PHIIn))
    199       return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(),
    200                                               C, Cast->getType()));
    201 
    202     // Otherwise we have to see if a casted version of the incoming pointer
    203     // is available.  If so, we can use it, otherwise we have to fail.
    204     for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end();
    205          UI != E; ++UI) {
    206       if (CastInst *CastI = dyn_cast<CastInst>(*UI))
    207         if (CastI->getOpcode() == Cast->getOpcode() &&
    208             CastI->getType() == Cast->getType() &&
    209             (!DT || DT->dominates(CastI->getParent(), PredBB)))
    210           return CastI;
    211     }
    212     return 0;
    213   }
    214 
    215   // Handle getelementptr with at least one PHI translatable operand.
    216   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
    217     SmallVector<Value*, 8> GEPOps;
    218     bool AnyChanged = false;
    219     for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
    220       Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
    221       if (GEPOp == 0) return 0;
    222 
    223       AnyChanged |= GEPOp != GEP->getOperand(i);
    224       GEPOps.push_back(GEPOp);
    225     }
    226 
    227     if (!AnyChanged)
    228       return GEP;
    229 
    230     // Simplify the GEP to handle 'gep x, 0' -> x etc.
    231     if (Value *V = SimplifyGEPInst(GEPOps, TD, DT)) {
    232       for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
    233         RemoveInstInputs(GEPOps[i], InstInputs);
    234 
    235       return AddAsInput(V);
    236     }
    237 
    238     // Scan to see if we have this GEP available.
    239     Value *APHIOp = GEPOps[0];
    240     for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end();
    241          UI != E; ++UI) {
    242       if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI))
    243         if (GEPI->getType() == GEP->getType() &&
    244             GEPI->getNumOperands() == GEPOps.size() &&
    245             GEPI->getParent()->getParent() == CurBB->getParent() &&
    246             (!DT || DT->dominates(GEPI->getParent(), PredBB))) {
    247           bool Mismatch = false;
    248           for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
    249             if (GEPI->getOperand(i) != GEPOps[i]) {
    250               Mismatch = true;
    251               break;
    252             }
    253           if (!Mismatch)
    254             return GEPI;
    255         }
    256     }
    257     return 0;
    258   }
    259 
    260   // Handle add with a constant RHS.
    261   if (Inst->getOpcode() == Instruction::Add &&
    262       isa<ConstantInt>(Inst->getOperand(1))) {
    263     // PHI translate the LHS.
    264     Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
    265     bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
    266     bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
    267 
    268     Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
    269     if (LHS == 0) return 0;
    270 
    271     // If the PHI translated LHS is an add of a constant, fold the immediates.
    272     if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
    273       if (BOp->getOpcode() == Instruction::Add)
    274         if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
    275           LHS = BOp->getOperand(0);
    276           RHS = ConstantExpr::getAdd(RHS, CI);
    277           isNSW = isNUW = false;
    278 
    279           // If the old 'LHS' was an input, add the new 'LHS' as an input.
    280           if (std::count(InstInputs.begin(), InstInputs.end(), BOp)) {
    281             RemoveInstInputs(BOp, InstInputs);
    282             AddAsInput(LHS);
    283           }
    284         }
    285 
    286     // See if the add simplifies away.
    287     if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD, DT)) {
    288       // If we simplified the operands, the LHS is no longer an input, but Res
    289       // is.
    290       RemoveInstInputs(LHS, InstInputs);
    291       return AddAsInput(Res);
    292     }
    293 
    294     // If we didn't modify the add, just return it.
    295     if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
    296       return Inst;
    297 
    298     // Otherwise, see if we have this add available somewhere.
    299     for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end();
    300          UI != E; ++UI) {
    301       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*UI))
    302         if (BO->getOpcode() == Instruction::Add &&
    303             BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
    304             BO->getParent()->getParent() == CurBB->getParent() &&
    305             (!DT || DT->dominates(BO->getParent(), PredBB)))
    306           return BO;
    307     }
    308 
    309     return 0;
    310   }
    311 
    312   // Otherwise, we failed.
    313   return 0;
    314 }
    315 
    316 
    317 /// PHITranslateValue - PHI translate the current address up the CFG from
    318 /// CurBB to Pred, updating our state to reflect any needed changes.  If the
    319 /// dominator tree DT is non-null, the translated value must dominate
    320 /// PredBB.  This returns true on failure and sets Addr to null.
    321 bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
    322                                      const DominatorTree *DT) {
    323   assert(Verify() && "Invalid PHITransAddr!");
    324   Addr = PHITranslateSubExpr(Addr, CurBB, PredBB, DT);
    325   assert(Verify() && "Invalid PHITransAddr!");
    326 
    327   if (DT) {
    328     // Make sure the value is live in the predecessor.
    329     if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr))
    330       if (!DT->dominates(Inst->getParent(), PredBB))
    331         Addr = 0;
    332   }
    333 
    334   return Addr == 0;
    335 }
    336 
    337 /// PHITranslateWithInsertion - PHI translate this value into the specified
    338 /// predecessor block, inserting a computation of the value if it is
    339 /// unavailable.
    340 ///
    341 /// All newly created instructions are added to the NewInsts list.  This
    342 /// returns null on failure.
    343 ///
    344 Value *PHITransAddr::
    345 PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
    346                           const DominatorTree &DT,
    347                           SmallVectorImpl<Instruction*> &NewInsts) {
    348   unsigned NISize = NewInsts.size();
    349 
    350   // Attempt to PHI translate with insertion.
    351   Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
    352 
    353   // If successful, return the new value.
    354   if (Addr) return Addr;
    355 
    356   // If not, destroy any intermediate instructions inserted.
    357   while (NewInsts.size() != NISize)
    358     NewInsts.pop_back_val()->eraseFromParent();
    359   return 0;
    360 }
    361 
    362 
    363 /// InsertPHITranslatedPointer - Insert a computation of the PHI translated
    364 /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
    365 /// block.  All newly created instructions are added to the NewInsts list.
    366 /// This returns null on failure.
    367 ///
    368 Value *PHITransAddr::
    369 InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
    370                            BasicBlock *PredBB, const DominatorTree &DT,
    371                            SmallVectorImpl<Instruction*> &NewInsts) {
    372   // See if we have a version of this value already available and dominating
    373   // PredBB.  If so, there is no need to insert a new instance of it.
    374   PHITransAddr Tmp(InVal, TD);
    375   if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT))
    376     return Tmp.getAddr();
    377 
    378   // If we don't have an available version of this value, it must be an
    379   // instruction.
    380   Instruction *Inst = cast<Instruction>(InVal);
    381 
    382   // Handle cast of PHI translatable value.
    383   if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
    384     if (!Cast->isSafeToSpeculativelyExecute()) return 0;
    385     Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
    386                                               CurBB, PredBB, DT, NewInsts);
    387     if (OpVal == 0) return 0;
    388 
    389     // Otherwise insert a cast at the end of PredBB.
    390     CastInst *New = CastInst::Create(Cast->getOpcode(),
    391                                      OpVal, InVal->getType(),
    392                                      InVal->getName()+".phi.trans.insert",
    393                                      PredBB->getTerminator());
    394     NewInsts.push_back(New);
    395     return New;
    396   }
    397 
    398   // Handle getelementptr with at least one PHI operand.
    399   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
    400     SmallVector<Value*, 8> GEPOps;
    401     BasicBlock *CurBB = GEP->getParent();
    402     for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
    403       Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i),
    404                                                 CurBB, PredBB, DT, NewInsts);
    405       if (OpVal == 0) return 0;
    406       GEPOps.push_back(OpVal);
    407     }
    408 
    409     GetElementPtrInst *Result =
    410       GetElementPtrInst::Create(GEPOps[0], makeArrayRef(GEPOps).slice(1),
    411                                 InVal->getName()+".phi.trans.insert",
    412                                 PredBB->getTerminator());
    413     Result->setIsInBounds(GEP->isInBounds());
    414     NewInsts.push_back(Result);
    415     return Result;
    416   }
    417 
    418 #if 0
    419   // FIXME: This code works, but it is unclear that we actually want to insert
    420   // a big chain of computation in order to make a value available in a block.
    421   // This needs to be evaluated carefully to consider its cost trade offs.
    422 
    423   // Handle add with a constant RHS.
    424   if (Inst->getOpcode() == Instruction::Add &&
    425       isa<ConstantInt>(Inst->getOperand(1))) {
    426     // PHI translate the LHS.
    427     Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0),
    428                                               CurBB, PredBB, DT, NewInsts);
    429     if (OpVal == 0) return 0;
    430 
    431     BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1),
    432                                            InVal->getName()+".phi.trans.insert",
    433                                                     PredBB->getTerminator());
    434     Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap());
    435     Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap());
    436     NewInsts.push_back(Res);
    437     return Res;
    438   }
    439 #endif
    440 
    441   return 0;
    442 }
    443