X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FExprTypeConvert.cpp;h=85f9bb0714cf09b8d657fbe17f80c73bcf329fb6;hb=da3c8a76748bd366f63f676df312319829c169e8;hp=eea8607ed08aea3ffb822b58b362ca80d3d3c940;hpb=131454e91d51c01fa0193630390116ab55c7886e;p=oota-llvm.git diff --git a/lib/Transforms/ExprTypeConvert.cpp b/lib/Transforms/ExprTypeConvert.cpp index eea8607ed08..85f9bb0714c 100644 --- a/lib/Transforms/ExprTypeConvert.cpp +++ b/lib/Transforms/ExprTypeConvert.cpp @@ -1,4 +1,4 @@ -//===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type ---------------=// +//===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type -------------===// // // This file implements the part of level raising that checks to see if it is // possible to coerce an entire expression tree into a different type. If @@ -13,9 +13,8 @@ #include "llvm/ConstantHandling.h" #include "llvm/Analysis/Expressions.h" #include "Support/STLExtras.h" -#include "Support/StatisticReporter.h" +#include "Support/Statistic.h" #include -#include using std::cerr; static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, @@ -24,19 +23,6 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, ValueMapCache &VMC); -// AllIndicesZero - Return true if all of the indices of the specified memory -// access instruction are zero, indicating an effectively nil offset to the -// pointer value. -// -static bool AllIndicesZero(const MemAccessInst *MAI) { - for (User::const_op_iterator S = MAI->idx_begin(), E = MAI->idx_end(); - S != E; ++S) - if (!isa(S->get()) || !cast(S->get())->isNullValue()) - return false; - return true; -} - - // Peephole Malloc instructions: we take a look at the use chain of the // malloc instruction, and try to find out if the following conditions hold: // 1. The malloc is of the form: 'malloc [sbyte], uint ' @@ -57,7 +43,7 @@ static bool MallocConvertableToType(MallocInst *MI, const Type *Ty, if (!Ty->isSized()) return false; // Can only alloc something with a size // Analyze the number of bytes allocated... - analysis::ExprType Expr = analysis::ClassifyExpression(MI->getArraySize()); + ExprType Expr = ClassifyExpression(MI->getArraySize()); // Get information about the base datatype being allocated, before & after int ReqTypeSize = TD.getTypeSize(Ty); @@ -67,13 +53,13 @@ static bool MallocConvertableToType(MallocInst *MI, const Type *Ty, if (!Expr.Offset && !Expr.Scale && OldTypeSize == 1) return false; // Get the offset and scale of the allocation... - int OffsetVal = Expr.Offset ? getConstantValue(Expr.Offset) : 0; - int ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var ? 1 : 0); + int64_t OffsetVal = Expr.Offset ? getConstantValue(Expr.Offset) : 0; + int64_t ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) :(Expr.Var != 0); // The old type might not be of unit size, take old size into consideration // here... - int Offset = OffsetVal * OldTypeSize; - int Scale = ScaleVal * OldTypeSize; + int64_t Offset = OffsetVal * OldTypeSize; + int64_t Scale = ScaleVal * OldTypeSize; // In order to be successful, both the scale and the offset must be a multiple // of the requested data type's size. @@ -92,7 +78,7 @@ static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty, BasicBlock::iterator It = BB->end(); // Analyze the number of bytes allocated... - analysis::ExprType Expr = analysis::ClassifyExpression(MI->getArraySize()); + ExprType Expr = ClassifyExpression(MI->getArraySize()); const PointerType *AllocTy = cast(Ty); const Type *ElType = AllocTy->getElementType(); @@ -101,13 +87,13 @@ static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty, unsigned OldTypeSize = TD.getTypeSize(MI->getType()->getElementType()); // Get the offset and scale coefficients that we are allocating... - int OffsetVal = (Expr.Offset ? getConstantValue(Expr.Offset) : 0); - int ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var ? 1 : 0); + int64_t OffsetVal = (Expr.Offset ? getConstantValue(Expr.Offset) : 0); + int64_t ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var !=0); // The old type might not be of unit size, take old size into consideration // here... - unsigned Offset = (unsigned)OffsetVal * OldTypeSize / DataSize; - unsigned Scale = (unsigned)ScaleVal * OldTypeSize / DataSize; + unsigned Offset = (uint64_t)OffsetVal * OldTypeSize / DataSize; + unsigned Scale = (uint64_t)ScaleVal * OldTypeSize / DataSize; // Locate the malloc instruction, because we may be inserting instructions It = MI; @@ -115,21 +101,14 @@ static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty, // If we have a scale, apply it first... if (Expr.Var) { // Expr.Var is not neccesarily unsigned right now, insert a cast now. - if (Expr.Var->getType() != Type::UIntTy) { - Instruction *CI = new CastInst(Expr.Var, Type::UIntTy); - if (Expr.Var->hasName()) CI->setName(Expr.Var->getName()+"-uint"); - It = ++BB->getInstList().insert(It, CI); - Expr.Var = CI; - } + if (Expr.Var->getType() != Type::UIntTy) + Expr.Var = new CastInst(Expr.Var, Type::UIntTy, + Expr.Var->getName()+"-uint", It); - if (Scale != 1) { - Instruction *ScI = - BinaryOperator::create(Instruction::Mul, Expr.Var, - ConstantUInt::get(Type::UIntTy, Scale)); - if (Expr.Var->hasName()) ScI->setName(Expr.Var->getName()+"-scl"); - It = ++BB->getInstList().insert(It, ScI); - Expr.Var = ScI; - } + if (Scale != 1) + Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var, + ConstantUInt::get(Type::UIntTy, Scale), + Expr.Var->getName()+"-scl", It); } else { // If we are not scaling anything, just make the offset be the "var"... @@ -140,27 +119,19 @@ static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty, // If we have an offset now, add it in... if (Offset != 0) { assert(Expr.Var && "Var must be nonnull by now!"); - - Instruction *AddI = - BinaryOperator::create(Instruction::Add, Expr.Var, - ConstantUInt::get(Type::UIntTy, Offset)); - if (Expr.Var->hasName()) AddI->setName(Expr.Var->getName()+"-off"); - It = ++BB->getInstList().insert(It, AddI); - Expr.Var = AddI; + Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var, + ConstantUInt::get(Type::UIntTy, Offset), + Expr.Var->getName()+"-off", It); } - Instruction *NewI = new MallocInst(AllocTy, Expr.Var, Name); - assert(AllocTy == Ty); - return NewI; + return new MallocInst(AllocTy->getElementType(), Expr.Var, Name); } // 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 (!Ty->isFirstClassType()) return false; @@ -168,21 +139,18 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty, ValueTypeCache::iterator CTMI = CTMap.find(V); if (CTMI != CTMap.end()) return CTMI->second == Ty; + // If it's a constant... all constants can be converted to a different type We + // just ask the constant propogator to see if it can convert the value... + // + if (Constant *CPV = dyn_cast(V)) + return ConstantFoldCastInstruction(CPV, Ty); + + CTMap[V] = Ty; + if (V->getType() == Ty) return true; // Expression already correct type! Instruction *I = dyn_cast(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). We just ask the constant propogator to see if - // it can convert the value... - // - if (Constant *CPV = dyn_cast(V)) - if (ConstantFoldCastInstruction(CPV, Ty)) - return true; // Don't worry about deallocating, it's a constant. - - return false; // Otherwise, we can't convert! - } + if (I == 0) return false; // Otherwise, we can't convert! switch (I->getOpcode()) { case Instruction::Cast: @@ -203,26 +171,23 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty, case Instruction::Add: case Instruction::Sub: + if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false; if (!ExpressionConvertableToType(I->getOperand(0), Ty, CTMap) || !ExpressionConvertableToType(I->getOperand(1), Ty, CTMap)) return false; break; case Instruction::Shr: + if (!Ty->isInteger()) return false; if (Ty->isSigned() != V->getType()->isSigned()) return false; // FALL THROUGH case Instruction::Shl: + if (!Ty->isInteger()) return false; if (!ExpressionConvertableToType(I->getOperand(0), Ty, CTMap)) return false; break; case Instruction::Load: { LoadInst *LI = cast(I); - if (LI->hasIndices() && !AllIndicesZero(LI)) { - // We can't convert a load expression if it has indices... unless they are - // all zero. - return false; - } - if (!ExpressionConvertableToType(LI->getPointerOperand(), PointerType::get(Ty), CTMap)) return false; @@ -260,12 +225,12 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty, // index array. If there are, check to see if removing them causes us to // get to the right type... // - std::vector Indices = GEP->copyIndices(); + std::vector Indices(GEP->idx_begin(), GEP->idx_end()); const Type *BaseType = GEP->getPointerOperand()->getType(); const Type *ElTy = 0; - while (!Indices.empty() && isa(Indices.back()) && - cast(Indices.back())->getValue() == 0) { + while (!Indices.empty() && + Indices.back() == Constant::getNullValue(Indices.back()->getType())){ Indices.pop_back(); ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices, true); if (ElTy == PVTy) @@ -276,11 +241,11 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty, if (ElTy) break; // Found a number of zeros we can strip off! // Otherwise, we can convert a GEP from one form to the other iff the - // current gep is of the form 'getelementptr sbyte*, unsigned N + // current gep is of the form 'getelementptr sbyte*, long N // and we could convert this to an appropriate GEP for the new type. // if (GEP->getNumOperands() == 2 && - GEP->getOperand(1)->getType() == Type::UIntTy && + GEP->getOperand(1)->getType() == Type::LongTy && GEP->getType() == PointerType::get(Type::SByteTy)) { // Do not Check to see if our incoming pointer can be converted @@ -303,12 +268,12 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty, } // Otherwise, it could be that we have something like this: - // getelementptr [[sbyte] *] * %reg115, uint %reg138 ; [sbyte]** + // getelementptr [[sbyte] *] * %reg115, long %reg138 ; [sbyte]** // and want to convert it into something like this: - // getelemenptr [[int] *] * %reg115, uint %reg138 ; [int]** + // getelemenptr [[int] *] * %reg115, long %reg138 ; [int]** // if (GEP->getNumOperands() == 2 && - GEP->getOperand(1)->getType() == Type::UIntTy && + GEP->getOperand(1)->getType() == Type::LongTy && TD.getTypeSize(PTy->getElementType()) == TD.getTypeSize(GEP->getType()->getElementType())) { const PointerType *NewSrcTy = PointerType::get(PVTy); @@ -320,6 +285,24 @@ bool ExpressionConvertableToType(Value *V, const Type *Ty, return false; // No match, maybe next time. } + case Instruction::Call: { + if (isa(I->getOperand(0))) + return false; // Don't even try to change direct calls. + + // If this is a function pointer, we can convert the return type if we can + // convert the source function pointer. + // + const PointerType *PT = cast(I->getOperand(0)->getType()); + const FunctionType *FT = cast(PT->getElementType()); + std::vector ArgTys(FT->getParamTypes().begin(), + FT->getParamTypes().end()); + const FunctionType *NewTy = + FunctionType::get(Ty, ArgTys, FT->isVarArg()); + if (!ExpressionConvertableToType(I->getOperand(0), + PointerType::get(NewTy), CTMap)) + return false; + break; + } default: return false; } @@ -341,6 +324,8 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(V); if (VMCI != VMC.ExprMap.end()) { + const Value *GV = VMCI->second; + const Type *GTy = VMCI->second->getType(); assert(VMCI->second->getType() == Ty); if (Instruction *I = dyn_cast(V)) @@ -352,22 +337,21 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { DEBUG(cerr << "CETT: " << (void*)V << " " << V); Instruction *I = dyn_cast(V); - if (I == 0) - if (Constant *CPV = cast(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 = ConstantFoldCastInstruction(CPV, Ty); - assert(Result && "ConstantFoldCastInstruction Failed!!!"); - assert(Result->getType() == Ty && "Const prop of cast failed!"); - - // Add the instruction to the expression map - VMC.ExprMap[V] = Result; - return Result; - } + if (I == 0) { + Constant *CPV = cast(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 = ConstantFoldCastInstruction(CPV, Ty); + assert(Result && "ConstantFoldCastInstruction Failed!!!"); + assert(Result->getType() == Ty && "Const prop of cast failed!"); + + // Add the instruction to the expression map + //VMC.ExprMap[V] = Result; + return Result; + } BasicBlock *BB = I->getParent(); - BasicBlock::InstListType &BIL = BB->getInstList(); std::string Name = I->getName(); if (!Name.empty()) I->setName(""); Instruction *Res; // Result of conversion @@ -377,7 +361,9 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { switch (I->getOpcode()) { case Instruction::Cast: + assert(VMC.NewCasts.count(ValueHandle(VMC, I)) == 0); Res = new CastInst(I->getOperand(0), Ty, Name); + VMC.NewCasts.insert(ValueHandle(VMC, Res)); break; case Instruction::Add: @@ -400,7 +386,6 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { case Instruction::Load: { LoadInst *LI = cast(I); - assert(!LI->hasIndices() || AllIndicesZero(LI)); Res = new LoadInst(Constant::getNullValue(PointerType::get(Ty)), Name); VMC.ExprMap[I] = Res; @@ -421,7 +406,7 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { BasicBlock *BB = OldPN->getIncomingBlock(0); Value *OldVal = OldPN->getIncomingValue(0); ValueHandle OldValHandle(VMC, OldVal); - OldPN->removeIncomingValue(BB); + OldPN->removeIncomingValue(BB, false); Value *V = ConvertExpressionToType(OldVal, Ty, VMC); NewPN->addIncoming(V, BB); } @@ -450,25 +435,24 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { // index array. If there are, check to see if removing them causes us to // get to the right type... // - std::vector Indices = GEP->copyIndices(); + std::vector Indices(GEP->idx_begin(), GEP->idx_end()); const Type *BaseType = GEP->getPointerOperand()->getType(); const Type *PVTy = cast(Ty)->getElementType(); Res = 0; - while (!Indices.empty() && isa(Indices.back()) && - cast(Indices.back())->getValue() == 0) { + while (!Indices.empty() && + Indices.back() == Constant::getNullValue(Indices.back()->getType())){ Indices.pop_back(); if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) { - if (Indices.size() == 0) { - Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP - } else { + if (Indices.size() == 0) + Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP CAST + else Res = new GetElementPtrInst(GEP->getPointerOperand(), Indices, Name); - } break; } } if (Res == 0 && GEP->getNumOperands() == 2 && - GEP->getOperand(1)->getType() == Type::UIntTy && + GEP->getOperand(1)->getType() == Type::LongTy && GEP->getType() == PointerType::get(Type::SByteTy)) { // Otherwise, we can convert a GEP from one form to the other iff the @@ -501,8 +485,9 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { // if (Res == 0) { const PointerType *NewSrcTy = PointerType::get(PVTy); + std::vector Indices(GEP->idx_begin(), GEP->idx_end()); Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy), - GEP->copyIndices(), Name); + Indices, Name); VMC.ExprMap[I] = Res; Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), NewSrcTy, VMC)); @@ -510,9 +495,30 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { assert(Res && "Didn't find match!"); - break; // No match, maybe next time. + break; } + case Instruction::Call: { + assert(!isa(I->getOperand(0))); + + // If this is a function pointer, we can convert the return type if we can + // convert the source function pointer. + // + const PointerType *PT = cast(I->getOperand(0)->getType()); + const FunctionType *FT = cast(PT->getElementType()); + std::vector ArgTys(FT->getParamTypes().begin(), + FT->getParamTypes().end()); + const FunctionType *NewTy = + FunctionType::get(Ty, ArgTys, FT->isVarArg()); + const PointerType *NewPTy = PointerType::get(NewTy); + + Res = new CallInst(Constant::getNullValue(NewPTy), + std::vector(I->op_begin()+1, I->op_end()), + Name); + VMC.ExprMap[I] = Res; + Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), NewPTy, VMC)); + break; + } default: assert(0 && "Expression convertable, but don't know how to convert?"); return 0; @@ -520,7 +526,7 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { assert(Res->getType() == Ty && "Didn't convert expr to correct type!"); - BIL.insert(I, Res); + BB->getInstList().insert(I, Res); // Add the instruction to the expression map VMC.ExprMap[I] = Res; @@ -540,14 +546,6 @@ Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { DEBUG(cerr << "ExpIn: " << (void*)I << " " << I << "ExpOut: " << (void*)Res << " " << Res); - if (I->use_empty()) { - DEBUG(cerr << "EXPR DELETING: " << (void*)I << " " << I); - BIL.remove(I); - VMC.OperandsMapped.erase(I); - VMC.ExprMap.erase(I); - delete I; - } - return Res; } @@ -644,6 +642,8 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, } // FALLTHROUGH case Instruction::Sub: { + if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false; + Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0); return ValueConvertableToType(I, Ty, CTMap) && ExpressionConvertableToType(OtherOp, Ty, CTMap); @@ -658,6 +658,7 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, // FALL THROUGH case Instruction::Shl: assert(I->getOperand(0) == V); + if (!Ty->isInteger()) return false; return ValueConvertableToType(I, Ty, CTMap); case Instruction::Free: @@ -671,9 +672,6 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, if (const PointerType *PT = dyn_cast(Ty)) { LoadInst *LI = cast(I); - if (LI->hasIndices() && !AllIndicesZero(LI)) - return false; - const Type *LoadedTy = PT->getElementType(); // They could be loading the first element of a composite type... @@ -696,7 +694,6 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, case Instruction::Store: { StoreInst *SI = cast(I); - if (SI->hasIndices()) return false; if (V == I->getOperand(0)) { ValueTypeCache::iterator CTMI = CTMap.find(I->getOperand(1)); @@ -756,7 +753,8 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, } // Must move the same amount of data... - if (TD.getTypeSize(ElTy) != TD.getTypeSize(I->getOperand(0)->getType())) + if (!ElTy->isSized() || + TD.getTypeSize(ElTy) != TD.getTypeSize(I->getOperand(0)->getType())) return false; // Can convert store if the incoming value is convertable... @@ -785,7 +783,7 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, // if (DataSize != 1) { TempScale = BinaryOperator::create(Instruction::Mul, Index, - ConstantUInt::get(Type::UIntTy, + ConstantSInt::get(Type::LongTy, DataSize)); Index = TempScale; } @@ -819,8 +817,12 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, if (OpNum == 0) { const PointerType *PTy = dyn_cast(Ty); if (PTy == 0) return false; // Can't convert to a non-pointer type... - const FunctionType *MTy = dyn_cast(PTy->getElementType()); - if (MTy == 0) return false; // Can't convert to a non ptr to function... + const FunctionType *FTy = dyn_cast(PTy->getElementType()); + if (FTy == 0) return false; // Can't convert to a non ptr to function... + + // Do not allow converting to a call where all of the operands are ...'s + if (FTy->getNumParams() == 0 && FTy->isVarArg()) + return false; // Do not permit this conversion! // Perform sanity checks to make sure that new function type has the // correct number of arguments... @@ -830,12 +832,12 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, // Cannot convert to a type that requires more fixed arguments than // the call provides... // - if (NumArgs < MTy->getParamTypes().size()) return false; + if (NumArgs < FTy->getNumParams()) return false; // Unless this is a vararg function type, we cannot provide more arguments // than are desired... // - if (!MTy->isVarArg() && NumArgs > MTy->getParamTypes().size()) + if (!FTy->isVarArg() && NumArgs > FTy->getNumParams()) return false; // Okay, at this point, we know that the call and the function type match @@ -845,7 +847,7 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, // reason for this is that we prefer to have resolved functions but casted // arguments if possible. // - const FunctionType::ParamTypes &PTs = MTy->getParamTypes(); + const FunctionType::ParamTypes &PTs = FTy->getParamTypes(); for (unsigned i = 0, NA = PTs.size(); i < NA; ++i) if (!PTs[i]->isLosslesslyConvertableTo(I->getOperand(i+1)->getType())) return false; // Operands must have compatible types! @@ -854,14 +856,14 @@ static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, // converted. We succeed if we can change the return type if // neccesary... // - return ValueConvertableToType(I, MTy->getReturnType(), CTMap); + return ValueConvertableToType(I, FTy->getReturnType(), CTMap); } const PointerType *MPtr = cast(I->getOperand(0)->getType()); - const FunctionType *MTy = cast(MPtr->getElementType()); - if (!MTy->isVarArg()) return false; + const FunctionType *FTy = cast(MPtr->getElementType()); + if (!FTy->isVarArg()) return false; - if ((OpNum-1) < MTy->getParamTypes().size()) + if ((OpNum-1) < FTy->getParamTypes().size()) return false; // It's not in the varargs section... // If we get this far, we know the value is in the varargs section of the @@ -904,8 +906,8 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, BasicBlock *BB = I->getParent(); assert(BB != 0 && "Instruction not embedded in basic block!"); - BasicBlock::InstListType &BIL = BB->getInstList(); - std::string Name = I->getName(); if (!Name.empty()) I->setName(""); + std::string Name = I->getName(); + I->setName(""); Instruction *Res; // Result of conversion //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl; @@ -919,8 +921,18 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, switch (I->getOpcode()) { case Instruction::Cast: - assert(I->getOperand(0) == OldVal); - Res = new CastInst(NewVal, I->getType(), Name); + if (VMC.NewCasts.count(ValueHandle(VMC, I))) { + // This cast has already had it's value converted, causing a new cast to + // be created. We don't want to create YET ANOTHER cast instruction + // representing the original one, so just modify the operand of this cast + // instruction, which we know is newly created. + I->setOperand(0, NewVal); + I->setName(Name); // give I its name back + return; + + } else { + Res = new CastInst(NewVal, I->getType(), Name); + } break; case Instruction::Add: @@ -974,38 +986,88 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, const Type *LoadedTy = cast(NewVal->getType())->getElementType(); - std::vector Indices; - Indices.push_back(ConstantUInt::get(Type::UIntTy, 0)); + Value *Src = NewVal; if (const CompositeType *CT = dyn_cast(LoadedTy)) { + std::vector Indices; + Indices.push_back(ConstantSInt::get(Type::LongTy, 0)); + unsigned Offset = 0; // No offset, get first leaf. LoadedTy = getStructOffsetType(CT, Offset, Indices, false); - } - assert(LoadedTy->isFirstClassType()); + assert(LoadedTy->isFirstClassType()); - Res = new LoadInst(NewVal, Indices, Name); + if (Indices.size() != 1) { // Do not generate load X, 0 + // Insert the GEP instruction before this load. + Src = new GetElementPtrInst(Src, Indices, Name+".idx", I); + } + } + + Res = new LoadInst(Src, Name); assert(Res->getType()->isFirstClassType() && "Load of structure or array!"); break; } case Instruction::Store: { if (I->getOperand(0) == OldVal) { // Replace the source value - const PointerType *NewPT = PointerType::get(NewTy); - Res = new StoreInst(NewVal, Constant::getNullValue(NewPT)); - VMC.ExprMap[I] = Res; - Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), NewPT, VMC)); + // Check to see if operand #1 has already been converted... + ValueMapCache::ExprMapTy::iterator VMCI = + VMC.ExprMap.find(I->getOperand(1)); + if (VMCI != VMC.ExprMap.end()) { + // Comments describing this stuff are in the OperandConvertableToType + // switch statement for Store... + // + const Type *ElTy = + cast(VMCI->second->getType())->getElementType(); + + Value *SrcPtr = VMCI->second; + + if (ElTy != NewTy) { + // We check that this is a struct in the initial scan... + const StructType *SElTy = cast(ElTy); + + std::vector Indices; + Indices.push_back(Constant::getNullValue(Type::LongTy)); + + unsigned Offset = 0; + const Type *Ty = getStructOffsetType(ElTy, Offset, Indices, false); + assert(Offset == 0 && "Offset changed!"); + assert(NewTy == Ty && "Did not convert to correct type!"); + + // Insert the GEP instruction before this store. + SrcPtr = new GetElementPtrInst(SrcPtr, Indices, + SrcPtr->getName()+".idx", I); + } + Res = new StoreInst(NewVal, SrcPtr); + + VMC.ExprMap[I] = Res; + } else { + // Otherwise, we haven't converted Operand #1 over yet... + const PointerType *NewPT = PointerType::get(NewTy); + Res = new StoreInst(NewVal, Constant::getNullValue(NewPT)); + VMC.ExprMap[I] = Res; + Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), + NewPT, VMC)); + } } else { // Replace the source pointer const Type *ValTy = cast(NewTy)->getElementType(); - std::vector Indices; + + Value *SrcPtr = NewVal; if (isa(ValTy)) { + std::vector Indices; + Indices.push_back(Constant::getNullValue(Type::LongTy)); + unsigned Offset = 0; - Indices.push_back(ConstantUInt::get(Type::UIntTy, 0)); ValTy = getStructOffsetType(ValTy, Offset, Indices, false); + assert(Offset == 0 && ValTy); + + // Insert the GEP instruction before this store. + SrcPtr = new GetElementPtrInst(SrcPtr, Indices, + SrcPtr->getName()+".idx", I); } - Res = new StoreInst(Constant::getNullValue(ValTy), NewVal, Indices); + Res = new StoreInst(Constant::getNullValue(ValTy), SrcPtr); VMC.ExprMap[I] = Res; Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), ValTy, VMC)); } @@ -1025,8 +1087,8 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, if (DataSize != 1) { // Insert a multiply of the old element type is not a unit size... Index = BinaryOperator::create(Instruction::Mul, Index, - ConstantUInt::get(Type::UIntTy, DataSize)); - It = ++BIL.insert(It, cast(Index)); + ConstantSInt::get(Type::LongTy, DataSize), + "scale", It); } // Perform the conversion now... @@ -1059,9 +1121,9 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, // to getelementptr long * %reg123, uint %N // ... where the type must simply stay the same size... // - Res = new GetElementPtrInst(NewVal, - cast(I)->copyIndices(), - Name); + GetElementPtrInst *GEP = cast(I); + std::vector Indices(GEP->idx_begin(), GEP->idx_end()); + Res = new GetElementPtrInst(NewVal, Indices, Name); } #endif break; @@ -1074,7 +1136,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, while (OldPN->getNumOperands()) { BasicBlock *BB = OldPN->getIncomingBlock(0); Value *OldVal = OldPN->getIncomingValue(0); - OldPN->removeIncomingValue(BB); + OldPN->removeIncomingValue(BB, false); Value *V = ConvertExpressionToType(OldVal, NewTy, VMC); NewPN->addIncoming(V, BB); } @@ -1107,8 +1169,8 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, // Create a cast to convert it to the right type, we know that this // is a lossless cast... // - Params[i] = new CastInst(Params[i], PTs[i], "call.resolve.cast"); - It = ++BIL.insert(It, cast(Params[i])); + Params[i] = new CastInst(Params[i], PTs[i], "callarg.cast." + + Params[i]->getName(), It); } Meth = NewVal; // Update call destination to new value @@ -1132,8 +1194,8 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, // stream. // BasicBlock::iterator It = I; - assert(It != BIL.end() && "Instruction not in own basic block??"); - BIL.insert(It, Res); // Keep It pointing to old instruction + assert(It != BB->end() && "Instruction not in own basic block??"); + BB->getInstList().insert(It, Res); // Keep It pointing to old instruction DEBUG(cerr << "COT CREATED: " << (void*)Res << " " << Res << "In: " << (void*)I << " " << I << "Out: " << (void*)Res @@ -1153,21 +1215,9 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, Use->replaceUsesOfWith(I, Res); } - if (I->use_empty()) { - // Now we just need to remove the old instruction so we don't get infinite - // loops. Note that we cannot use DCE because DCE won't remove a store - // instruction, for example. - // - DEBUG(cerr << "DELETING: " << (void*)I << " " << I); - BIL.remove(I); - VMC.OperandsMapped.erase(I); - VMC.ExprMap.erase(I); - delete I; - } else { - for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); - UI != UE; ++UI) - assert(isa((Value*)*UI) &&"Uses of Instruction remain!!!"); - } + for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); + UI != UE; ++UI) + assert(isa((Value*)*UI) &&"Uses of Instruction remain!!!"); } } @@ -1178,6 +1228,12 @@ ValueHandle::ValueHandle(ValueMapCache &VMC, Value *V) Operands.push_back(Use(V, this)); } +ValueHandle::ValueHandle(const ValueHandle &VH) + : Instruction(Type::VoidTy, UserOp1, ""), Cache(VH.Cache) { + //DEBUG(cerr << "VH AQUIRING: " << (void*)V << " " << V); + Operands.push_back(Use((Value*)VH.getOperand(0), this)); +} + static void RecursiveDelete(ValueMapCache &Cache, Instruction *I) { if (!I || !I->use_empty()) return;