// ExpressionConvertableToType - Return true if it is possible
bool ExpressionConvertableToType(Value *V, const Type *Ty,
ValueTypeCache &CTMap) {
+ if (V->getType() == Ty) return true; // Expression already correct type!
+
// Expression type must be holdable in a register.
if (!isFirstClassType(Ty))
return false;
if (CTMI != CTMap.end()) return CTMI->second == Ty;
CTMap[V] = Ty;
- // Expressions are only convertable if all of the users of the expression can
- // have this value converted. This makes use of the map to avoid infinite
- // recursion.
- //
- if (isa<Instruction>(V)) {
- for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
- if (!OperandConvertableToType(*I, V, Ty, CTMap))
- return false;
- }
-
Instruction *I = dyn_cast<Instruction>(V);
if (I == 0) {
// It's not an instruction, check to see if it's a constant... all constants
return false; // Otherwise, we can't convert!
}
- if (I->getType() == Ty) return false; // Expression already correct type!
+
+ // Expressions are only convertable if all of the users of the expression can
+ // have this value converted. This makes use of the map to avoid infinite
+ // recursion.
+ //
+ if (isa<Instruction>(V)) {
+ for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
+ if (!OperandConvertableToType(*I, V, Ty, CTMap))
+ return false;
+ }
switch (I->getOpcode()) {
case Instruction::Cast:
case Instruction::Load: {
LoadInst *LI = cast<LoadInst>(I);
- if (LI->hasIndices()) return false;
+ if (LI->hasIndices()) {
+ // We can't convert a load expression if it has indices... unless they are
+ // all zero.
+ const vector<ConstPoolVal*> &CPV = LI->getIndices();
+ for (unsigned i = 0; i < CPV.size(); ++i)
+ if (!CPV[i]->isNullValue()) return false;
+ }
return ExpressionConvertableToType(LI->getPtrOperand(),
PointerType::get(Ty), CTMap);
case Instruction::Load: {
LoadInst *LI = cast<LoadInst>(I);
- assert(!LI->hasIndices());
+#ifndef NDEBUG
+ if (LI->hasIndices()) {
+ // We can't convert a load expression if it has indices... unless they are
+ // all zero.
+ const vector<ConstPoolVal*> &CPV = LI->getIndices();
+ for (unsigned i = 0; i < CPV.size(); ++i)
+ assert(CPV[i]->isNullValue() && "Load index not 0!");
+ }
+#endif
Res = new LoadInst(ConstPoolVal::getNullConstant(PointerType::get(Ty)),
Name);
VMC.ExprMap[I] = Res;
}
// FALLTHROUGH
case Instruction::Sub: {
- CTMap[I] = Ty;
Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
return RetValConvertableToType(I, Ty, CTMap) &&
ExpressionConvertableToType(OtherOp, Ty, CTMap);
// FALL THROUGH
case Instruction::Shl:
assert(I->getOperand(0) == V);
- CTMap[I] = Ty;
return RetValConvertableToType(I, Ty, CTMap);
case Instruction::Load:
// See if the leaf type is compatible with the old return type...
if (TD.getTypeSize(Ty) != TD.getTypeSize(LI->getType()))
return false;
-
- CTMap[LI] = Ty;
return RetValConvertableToType(LI, Ty, CTMap);
}
return false;
if (TD.getTypeSize(PVTy) != TD.getTypeSize(LI->getType()))
return false;
- CTMap[LI] = PVTy;
return RetValConvertableToType(LI, PVTy, CTMap);
}
return false;
}
case Instruction::PHINode: {
- CTMap[I] = Ty;
PHINode *PN = cast<PHINode>(I);
for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
if (!ExpressionConvertableToType(PN->getIncomingValue(i), Ty, CTMap))