#include "llvm/ConstantHandling.h"
#include "llvm/Analysis/Expressions.h"
#include "Support/STLExtras.h"
-#include "Support/StatisticReporter.h"
+#include "Support/Statistic.h"
#include <algorithm>
using std::cerr;
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.
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;
}
assert(AllocTy == Ty);
- return new MallocInst(AllocTy, Expr.Var, Name);
+ return new MallocInst(AllocTy->getElementType(), Expr.Var, Name);
}
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<Constant>(V))
+ return ConstantFoldCastInstruction(CPV, Ty);
+
+
CTMap[V] = Ty;
if (V->getType() == Ty) return true; // Expression already correct type!
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). We just ask the constant propogator to see if
- // it can convert the value...
- //
- if (Constant *CPV = dyn_cast<Constant>(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:
const Type *BaseType = GEP->getPointerOperand()->getType();
const Type *ElTy = 0;
- while (!Indices.empty() && isa<ConstantUInt>(Indices.back()) &&
- cast<ConstantUInt>(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)
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
}
// 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 &&
+ PTy->getElementType()->isSized() &&
TD.getTypeSize(PTy->getElementType()) ==
TD.getTypeSize(GEP->getType()->getElementType())) {
const PointerType *NewSrcTy = PointerType::get(PVTy);
return false; // No match, maybe next time.
}
+ case Instruction::Call: {
+ if (isa<Function>(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<PointerType>(I->getOperand(0)->getType());
+ const FunctionType *FT = cast<FunctionType>(PT->getElementType());
+ std::vector<const Type *> 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;
}
DEBUG(cerr << "CETT: " << (void*)V << " " << V);
Instruction *I = dyn_cast<Instruction>(V);
- if (I == 0)
- if (Constant *CPV = cast<Constant>(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<Constant>(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 *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);
}
const Type *BaseType = GEP->getPointerOperand()->getType();
const Type *PVTy = cast<PointerType>(Ty)->getElementType();
Res = 0;
- while (!Indices.empty() && isa<ConstantUInt>(Indices.back()) &&
- cast<ConstantUInt>(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
assert(Res && "Didn't find match!");
- break; // No match, maybe next time.
+ break;
}
+ case Instruction::Call: {
+ assert(!isa<Function>(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<PointerType>(I->getOperand(0)->getType());
+ const FunctionType *FT = cast<FunctionType>(PT->getElementType());
+ std::vector<const Type *> ArgTys(FT->getParamTypes().begin(),
+ FT->getParamTypes().end());
+ const FunctionType *NewTy =
+ FunctionType::get(Ty, ArgTys, FT->isVarArg());
+ const PointerType *NewPTy = PointerType::get(NewTy);
+ if (Ty == Type::VoidTy)
+ Name = ""; // Make sure not to name calls that now return void!
+
+ Res = new CallInst(Constant::getNullValue(NewPTy),
+ std::vector<Value*>(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;
if (Ty->isSigned() != V->getType()->isSigned()) return false;
// FALL THROUGH
case Instruction::Shl:
- assert(I->getOperand(0) == V);
+ if (I->getOperand(1) == V) return false; // Cannot change shift amount type
if (!Ty->isInteger()) return false;
return ValueConvertableToType(I, Ty, CTMap);
//
if (DataSize != 1) {
TempScale = BinaryOperator::create(Instruction::Mul, Index,
- ConstantUInt::get(Type::UIntTy,
+ ConstantSInt::get(Type::LongTy,
DataSize));
Index = TempScale;
}
if (OpNum == 0) {
const PointerType *PTy = dyn_cast<PointerType>(Ty);
if (PTy == 0) return false; // Can't convert to a non-pointer type...
- const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
- if (MTy == 0) return false; // Can't convert to a non ptr to function...
+ const FunctionType *FTy = dyn_cast<FunctionType>(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...
// 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
// 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!
// 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<PointerType>(I->getOperand(0)->getType());
- const FunctionType *MTy = cast<FunctionType>(MPtr->getElementType());
- if (!MTy->isVarArg()) return false;
+ const FunctionType *FTy = cast<FunctionType>(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
if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) {
std::vector<Value*> Indices;
- Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+ Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
unsigned Offset = 0; // No offset, get first leaf.
LoadedTy = getStructOffsetType(CT, Offset, Indices, false);
const StructType *SElTy = cast<StructType>(ElTy);
std::vector<Value*> Indices;
- Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+ Indices.push_back(Constant::getNullValue(Type::LongTy));
unsigned Offset = 0;
const Type *Ty = getStructOffsetType(ElTy, Offset, Indices, false);
if (isa<StructType>(ValTy)) {
std::vector<Value*> Indices;
- Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+ Indices.push_back(Constant::getNullValue(Type::LongTy));
unsigned Offset = 0;
ValTy = getStructOffsetType(ValTy, Offset, Indices, false);
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),
+ ConstantSInt::get(Type::LongTy, DataSize),
"scale", It);
}
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);
}
const FunctionType *NewTy = cast<FunctionType>(NewPTy->getElementType());
const FunctionType::ParamTypes &PTs = NewTy->getParamTypes();
+ if (NewTy->getReturnType() == Type::VoidTy)
+ Name = ""; // Make sure not to name a void call!
+
// Get an iterator to the call instruction so that we can insert casts for
// operands if needbe. Note that we do not require operands to be
// convertable, we can insert casts if they are convertible but not
// 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);
+ Params[i] = new CastInst(Params[i], PTs[i], "callarg.cast." +
+ Params[i]->getName(), It);
}
Meth = NewVal; // Update call destination to new value