- 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->getIndexVec();
- 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;
-}
-
-
-
-// DoInsertArrayCast - If the argument value has a pointer type, and if the
-// argument value is used as an array, insert a cast before the specified
-// basic block iterator that casts the value to an array pointer. Return the
-// new cast instruction (in the CastResult var), or null if no cast is inserted.
-//
-static bool DoInsertArrayCast(Method *CurMeth, Value *V, BasicBlock *BB,
- BasicBlock::iterator &InsertBefore,
- CastInst *&CastResult) {
- const PointerType *ThePtrType = dyn_cast<PointerType>(V->getType());
- if (!ThePtrType) return false;
- bool InsertCast = false;
-
- for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
- Instruction *Inst = cast<Instruction>(*I);
- switch (Inst->getOpcode()) {
- default: break; // Not an interesting use...
- case Instruction::Add: // It's being used as an array index!
- //case Instruction::Sub:
- InsertCast = true;
- break;
- case Instruction::Cast: // There is already a cast instruction!
- if (const PointerType *PT = dyn_cast<const PointerType>(Inst->getType()))
- if (const ArrayType *AT = dyn_cast<const ArrayType>(PT->getValueType()))
- if (AT->getElementType() == ThePtrType->getValueType()) {
- // Cast already exists! Return the existing one!
- CastResult = cast<CastInst>(Inst);
- return false; // No changes made to program though...
- }
- break;
- }
- }
-
- if (!InsertCast) return false; // There is no reason to insert a cast!
-
- // Insert a cast!
- const Type *ElTy = ThePtrType->getValueType();
- const PointerType *DestTy = PointerType::get(ArrayType::get(ElTy));
-
- CastResult = new CastInst(V, DestTy);
- BB->getInstList().insert(InsertBefore, CastResult);
- //cerr << "Inserted cast: " << CastResult;
- return true; // Made a change!
-}