-/// isPHITranslatable - Return true if the specified computation is derived from
-/// a PHI node in the current block and if it is simple enough for us to handle.
-static bool isPHITranslatable(Instruction *Inst) {
- if (isa<PHINode>(Inst))
- return true;
-
- // We can handle bitcast of a PHI, but the PHI needs to be in the same block
- // as the bitcast.
- if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst))
- if (PHINode *PN = dyn_cast<PHINode>(BC->getOperand(0)))
- if (PN->getParent() == BC->getParent())
- return true;
-
- // We can translate a GEP that uses a PHI in the current block for at least
- // one of its operands.
- if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
- for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
- if (PHINode *PN = dyn_cast<PHINode>(GEP->getOperand(i)))
- if (PN->getParent() == GEP->getParent())
- return true;
- }
-
- // cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
- // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
- // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
-
- return false;
-}
-
-/// PHITranslateForPred - Given a computation that satisfied the
-/// isPHITranslatable predicate, see if we can translate the computation into
-/// the specified predecessor block. If so, return that value.
-static Value *PHITranslateForPred(Instruction *Inst, BasicBlock *Pred) {
- if (PHINode *PN = dyn_cast<PHINode>(Inst))
- return PN->getIncomingValueForBlock(Pred);
-
- // Handle bitcast of PHI.
- if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) {
- PHINode *BCPN = cast<PHINode>(BC->getOperand(0));
- Value *PHIIn = BCPN->getIncomingValueForBlock(Pred);
-
- // Constants are trivial to phi translate.
- if (Constant *C = dyn_cast<Constant>(PHIIn))
- return ConstantExpr::getBitCast(C, BC->getType());
-
- // Otherwise we have to see if a bitcasted version of the incoming pointer
- // is available. If so, we can use it, otherwise we have to fail.
- for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end();
- UI != E; ++UI) {
- if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI))
- if (BCI->getType() == BC->getType())
- return BCI;
- }
- return 0;
- }
-
- // Handle getelementptr with at least one PHI operand.
- if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
- SmallVector<Value*, 8> GEPOps;
- Value *APHIOp = 0;
- for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
- GEPOps.push_back(GEP->getOperand(i));
- if (PHINode *PN = dyn_cast<PHINode>(GEP->getOperand(i)))
- if (PN->getParent() == GEP->getParent())
- GEPOps.back() = APHIOp = PN->getIncomingValueForBlock(Pred);
- }
-
- // TODO: Simplify the GEP to handle 'gep x, 0' -> x etc.
-
- // Scan to see if we have this GEP available.
- for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end();
- UI != E; ++UI) {
- if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI))
- if (GEPI->getType() == GEPI->getType() &&
- GEPI->getNumOperands() == GEPOps.size() &&
- GEPI->getParent()->getParent() == Inst->getParent()->getParent()) {
- bool Mismatch = false;
- for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
- if (GEPI->getOperand(i) != GEPOps[i]) {
- Mismatch = true;
- break;
- }
- if (!Mismatch)
- return GEPI;
- }
- }
- return 0;
- }
-
- return 0;
-}
-
-