- // 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;
- }
-
-
- BasicBlock *BB = I->getParent();
- BasicBlock::InstListType &BIL = BB->getInstList();
- string Name = I->getName(); if (!Name.empty()) I->setName("");
- Instruction *Res; // Result of conversion
-
- //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl;
-
- switch (I->getOpcode()) {
- case Instruction::Cast:
- Res = new CastInst(I->getOperand(0), Ty, Name);
- break;
-
- case Instruction::Add:
- case Instruction::Sub:
- Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
- ConvertExpressionToType(I->getOperand(0), Ty),
- ConvertExpressionToType(I->getOperand(1), Ty),
- Name);
- break;
-
- case Instruction::Shl:
- case Instruction::Shr:
- Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(),
- ConvertExpressionToType(I->getOperand(0), Ty),
- I->getOperand(1), Name);
- break;
-
- case Instruction::Load: {
- LoadInst *LI = cast<LoadInst>(I);
- assert(!LI->hasIndices());
- Res = new LoadInst(ConvertExpressionToType(LI->getPtrOperand(),
- PointerType::get(Ty)), Name);
- break;
- }
-
- 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);
-
- // 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();
- const Type *PVTy = cast<PointerType>(Ty)->getValueType();
- Res = 0;
- while (Indices.size() &&
- cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
- Indices.pop_back();
- if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
- if (Indices.size() == 0) {
- Res = new CastInst(GEP->getPtrOperand(), BaseType); // NOOP
- } else {
- Res = new GetElementPtrInst(GEP->getPtrOperand(), Indices, Name);
- }
- break;
- }
- }
- assert(Res && "Didn't find match!");
- break; // No match, maybe next time.
- }
-
- default:
- assert(0 && "Expression convertable, but don't know how to convert?");
- return 0;
- }
-
- BasicBlock::iterator It = find(BIL.begin(), BIL.end(), I);
- assert(It != BIL.end() && "Instruction not in own basic block??");
- BIL.insert(It, Res);
-
- //cerr << "RInst: " << Res << "BB After: " << BB << endl << endl;
-
- return Res;
-}
-
-static inline const Type *getTy(const Value *V, ValueTypeCache &CT) {
- ValueTypeCache::iterator I = CT.find(V);
- if (I == CT.end()) return V->getType();
- return I->second;
-}
-
-
-static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
- ValueTypeCache &ConvertedTypes);
-
-// RetValConvertableToType - Return true if it is possible
-static bool RetValConvertableToType(Value *V, const Type *Ty,
- ValueTypeCache &ConvertedTypes) {
- ValueTypeCache::iterator I = ConvertedTypes.find(V);
- if (I != ConvertedTypes.end()) return I->second == Ty;
- ConvertedTypes[V] = Ty;
-
- // It is safe to convert the specified value to the specified type IFF all of
- // the uses of the value can be converted to accept the new typed value.