-//===- 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
#include "Support/STLExtras.h"
#include "Support/StatisticReporter.h"
#include <algorithm>
-#include <iostream>
using std::cerr;
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<Constant>(S->get()) || !cast<Constant>(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 <constant>'
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);
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<PointerType>(Ty);
const Type *ElType = AllocTy->getElementType();
// 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"...
// 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, 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;
if (CTMI != CTMap.end()) return CTMI->second == Ty;
CTMap[V] = Ty;
+ if (V->getType() == Ty) return true; // Expression already correct type!
Instruction *I = dyn_cast<Instruction>(V);
if (I == 0) {
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<LoadInst>(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;
// index array. If there are, check to see if removing them causes us to
// get to the right type...
//
- std::vector<Value*> Indices = GEP->copyIndices();
+ std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
const Type *BaseType = GEP->getPointerOperand()->getType();
const Type *ElTy = 0;
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<Instruction>(V))
BasicBlock *BB = I->getParent();
- BasicBlock::InstListType &BIL = BB->getInstList();
std::string Name = I->getName(); if (!Name.empty()) I->setName("");
Instruction *Res; // Result of conversion
case Instruction::Load: {
LoadInst *LI = cast<LoadInst>(I);
- assert(!LI->hasIndices() || AllIndicesZero(LI));
Res = new LoadInst(Constant::getNullValue(PointerType::get(Ty)), Name);
VMC.ExprMap[I] = Res;
// index array. If there are, check to see if removing them causes us to
// get to the right type...
//
- std::vector<Value*> Indices = GEP->copyIndices();
+ std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
const Type *BaseType = GEP->getPointerOperand()->getType();
const Type *PVTy = cast<PointerType>(Ty)->getElementType();
Res = 0;
//
if (Res == 0) {
const PointerType *NewSrcTy = PointerType::get(PVTy);
+ std::vector<Value*> 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));
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;
}
// 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);
// FALL THROUGH
case Instruction::Shl:
assert(I->getOperand(0) == V);
+ if (!Ty->isInteger()) return false;
return ValueConvertableToType(I, Ty, CTMap);
case Instruction::Free:
if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
LoadInst *LI = cast<LoadInst>(I);
- if (LI->hasIndices() && !AllIndicesZero(LI))
- return false;
-
const Type *LoadedTy = PT->getElementType();
// They could be loading the first element of a composite type...
case Instruction::Store: {
StoreInst *SI = cast<StoreInst>(I);
- if (SI->hasIndices()) return false;
if (V == I->getOperand(0)) {
ValueTypeCache::iterator CTMI = CTMap.find(I->getOperand(1));
BasicBlock *BB = I->getParent();
assert(BB != 0 && "Instruction not embedded in basic block!");
- BasicBlock::InstListType &BIL = BB->getInstList();
std::string Name = I->getName();
I->setName("");
Instruction *Res; // Result of conversion
const Type *LoadedTy =
cast<PointerType>(NewVal->getType())->getElementType();
- std::vector<Value*> Indices;
- Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+ Value *Src = NewVal;
if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) {
+ std::vector<Value*> Indices;
+ Indices.push_back(ConstantUInt::get(Type::UIntTy, 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<PointerType>(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<StructType>(ElTy);
+
+ std::vector<Value*> Indices;
+ Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+
+ 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<PointerType>(NewTy)->getElementType();
- std::vector<Value*> Indices;
+
+ Value *SrcPtr = NewVal;
if (isa<StructType>(ValTy)) {
- unsigned Offset = 0;
+ std::vector<Value*> Indices;
Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
+
+ unsigned Offset = 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));
}
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<Instruction>(Index));
+ ConstantUInt::get(Type::UIntTy, DataSize),
+ "scale", It);
}
// Perform the conversion now...
// to getelementptr long * %reg123, uint %N
// ... where the type must simply stay the same size...
//
- Res = new GetElementPtrInst(NewVal,
- cast<GetElementPtrInst>(I)->copyIndices(),
- Name);
+ GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
+ std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
+ Res = new GetElementPtrInst(NewVal, Indices, Name);
}
#endif
break;
// 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<Instruction>(Params[i]));
+ Params[i] = new CastInst(Params[i], PTs[i], "call.resolve.cast", It);
}
Meth = NewVal; // Update call destination to new value
// 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