-static void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
- BasicBlock::iterator &BI, Instruction *I) {
- assert(I->getParent() == 0 &&
- "ReplaceInstWithInst: Instruction already inserted into basic block!");
-
- // Insert the new instruction into the basic block...
- BI = BIL.insert(BI, I)+1;
-
- // Replace all uses of the old instruction, and delete it.
- ReplaceInstWithValue(BIL, BI, I);
-
- // Reexamine the instruction just inserted next time around the cleanup pass
- // loop.
- --BI;
-}
-
-
-
-typedef map<const Value*, const Type*> ValueTypeCache;
-typedef map<const Value*, Value*> ValueMapCache;
-
-
-
-// ExpressionConvertableToType - Return true if it is possible
-static bool ExpressionConvertableToType(Value *V, const Type *Ty) {
- Instruction *I = dyn_cast<Instruction>(V);
- if (I == 0) {
- // It's not an instruction, check to see if it's a constant... all constants
- // can be converted to an equivalent value (except pointers, they can't be
- // const prop'd in general).
- //
- if (isa<ConstPoolVal>(V) &&
- !isa<PointerType>(V->getType()) && !isa<PointerType>(Ty)) return true;
-
- return false; // Otherwise, we can't convert!
- }
- if (I->getType() == Ty) return false; // Expression already correct type!
-
- switch (I->getOpcode()) {
- case Instruction::Cast:
- // We can convert the expr if the cast destination type is losslessly
- // convertable to the requested type.
- return losslessCastableTypes(Ty, I->getType());
-
- case Instruction::Add:
- case Instruction::Sub:
- return ExpressionConvertableToType(I->getOperand(0), Ty) &&
- ExpressionConvertableToType(I->getOperand(1), Ty);
- case Instruction::Shr:
- if (Ty->isSigned() != V->getType()->isSigned()) return false;
- // FALL THROUGH
- case Instruction::Shl:
- return ExpressionConvertableToType(I->getOperand(0), Ty);
-
- case Instruction::Load: {
- LoadInst *LI = cast<LoadInst>(I);
- if (LI->hasIndices()) return false;
- return ExpressionConvertableToType(LI->getPtrOperand(),
- PointerType::get(Ty));
- }
- case Instruction::GetElementPtr: {
- // GetElementPtr's are directly convertable to a pointer type if they have
- // a number of zeros at the end. Because removing these values does not
- // change the logical offset of the GEP, it is okay and fair to remove them.
- // This can change this:
- // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
- // %t2 = cast %List * * %t1 to %List *
- // into
- // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
- //
- GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
- const PointerType *PTy = dyn_cast<PointerType>(Ty);
- if (!PTy) return false;
-
- // Check to see if there are zero elements that we can remove from the
- // index array. If there are, check to see if removing them causes us to
- // get to the right type...
- //
- vector<ConstPoolVal*> Indices = GEP->getIndices();
- const Type *BaseType = GEP->getPtrOperand()->getType();
-
- while (Indices.size() &&
- cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
- Indices.pop_back();
- const Type *ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices,
- true);
- if (ElTy == PTy->getValueType())
- return true; // Found a match!!
- }
- break; // No match, maybe next time.
- }
- }
- return false;
-}
-
-
-static Value *ConvertExpressionToType(Value *V, const Type *Ty) {
- assert(ExpressionConvertableToType(V, Ty) && "Value is not convertable!");
- Instruction *I = dyn_cast<Instruction>(V);
- if (I == 0)
- if (ConstPoolVal *CPV = cast<ConstPoolVal>(V)) {
- // Constants are converted by constant folding the cast that is required.
- // We assume here that all casts are implemented for constant prop.
- Value *Result = opt::ConstantFoldCastInstruction(CPV, Ty);
- if (!Result) cerr << "Couldn't fold " << CPV << " to " << Ty << endl;
- assert(Result && "ConstantFoldCastInstruction Failed!!!");
- return Result;
- }
-