2002-09-09 20:25:21 +00:00
|
|
|
//===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type -------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-04 23:24:20 +00:00
|
|
|
//
|
|
|
|
// 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
|
2003-05-20 18:45:36 +00:00
|
|
|
// convertible, other routines from this file will do the conversion.
|
2001-11-04 23:24:20 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TransformInternals.h"
|
2004-01-12 18:12:44 +00:00
|
|
|
#include "llvm/Constants.h"
|
2001-11-04 23:24:20 +00:00
|
|
|
#include "llvm/iOther.h"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-11-04 23:24:20 +00:00
|
|
|
#include "llvm/iMemory.h"
|
2004-01-12 18:12:44 +00:00
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
#include "llvm/Analysis/Expressions.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2003-08-01 22:15:03 +00:00
|
|
|
#include "Support/Debug.h"
|
2001-11-04 23:24:20 +00:00
|
|
|
#include <algorithm>
|
2003-12-23 07:43:38 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-05-20 18:45:36 +00:00
|
|
|
static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty,
|
2003-04-24 18:25:27 +00:00
|
|
|
ValueTypeCache &ConvertedTypes,
|
|
|
|
const TargetData &TD);
|
2001-11-26 16:57:31 +00:00
|
|
|
|
|
|
|
static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
|
2003-04-24 18:25:27 +00:00
|
|
|
ValueMapCache &VMC, const TargetData &TD);
|
2001-11-26 16:57:31 +00:00
|
|
|
|
|
|
|
// 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>'
|
|
|
|
// 2. The only users of the malloc are cast & add instructions
|
|
|
|
// 3. Of the cast instructions, there is only one destination pointer type
|
|
|
|
// [RTy] where the size of the pointed to object is equal to the number
|
|
|
|
// of bytes allocated.
|
|
|
|
//
|
|
|
|
// If these conditions hold, we convert the malloc to allocate an [RTy]
|
|
|
|
// element. TODO: This comment is out of date WRT arrays
|
|
|
|
//
|
2003-05-20 18:45:36 +00:00
|
|
|
static bool MallocConvertibleToType(MallocInst *MI, const Type *Ty,
|
2003-04-24 18:25:27 +00:00
|
|
|
ValueTypeCache &CTMap,
|
|
|
|
const TargetData &TD) {
|
2002-03-21 22:39:59 +00:00
|
|
|
if (!isa<PointerType>(Ty)) return false; // Malloc always returns pointers
|
2001-11-26 16:57:31 +00:00
|
|
|
|
|
|
|
// Deal with the type to allocate, not the pointer type...
|
2001-12-04 00:03:30 +00:00
|
|
|
Ty = cast<PointerType>(Ty)->getElementType();
|
2001-12-14 16:35:53 +00:00
|
|
|
if (!Ty->isSized()) return false; // Can only alloc something with a size
|
2001-11-26 16:57:31 +00:00
|
|
|
|
|
|
|
// Analyze the number of bytes allocated...
|
2003-12-23 08:04:08 +00:00
|
|
|
ExprType Expr = ClassifyExpr(MI->getArraySize());
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
// Get information about the base datatype being allocated, before & after
|
2002-04-29 20:09:21 +00:00
|
|
|
int ReqTypeSize = TD.getTypeSize(Ty);
|
2003-06-23 17:36:49 +00:00
|
|
|
if (ReqTypeSize == 0) return false;
|
2001-12-14 16:35:53 +00:00
|
|
|
unsigned OldTypeSize = TD.getTypeSize(MI->getType()->getElementType());
|
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
// Must have a scale or offset to analyze it...
|
2002-03-21 23:02:37 +00:00
|
|
|
if (!Expr.Offset && !Expr.Scale && OldTypeSize == 1) return false;
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
// Get the offset and scale of the allocation...
|
2002-09-16 18:32:33 +00:00
|
|
|
int64_t OffsetVal = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
|
|
|
|
int64_t ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) :(Expr.Var != 0);
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
// The old type might not be of unit size, take old size into consideration
|
|
|
|
// here...
|
2002-09-16 18:32:33 +00:00
|
|
|
int64_t Offset = OffsetVal * OldTypeSize;
|
|
|
|
int64_t Scale = ScaleVal * OldTypeSize;
|
2001-12-14 16:35:53 +00:00
|
|
|
|
|
|
|
// In order to be successful, both the scale and the offset must be a multiple
|
|
|
|
// of the requested data type's size.
|
2001-11-26 16:57:31 +00:00
|
|
|
//
|
2001-12-14 16:35:53 +00:00
|
|
|
if (Offset/ReqTypeSize*ReqTypeSize != Offset ||
|
|
|
|
Scale/ReqTypeSize*ReqTypeSize != Scale)
|
2001-11-26 16:57:31 +00:00
|
|
|
return false; // Nope.
|
|
|
|
|
|
|
|
return true;
|
2001-11-08 20:19:56 +00:00
|
|
|
}
|
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty,
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::string &Name,
|
2003-04-24 18:25:27 +00:00
|
|
|
ValueMapCache &VMC,
|
|
|
|
const TargetData &TD){
|
2001-11-26 16:57:31 +00:00
|
|
|
BasicBlock *BB = MI->getParent();
|
|
|
|
BasicBlock::iterator It = BB->end();
|
2001-11-08 20:19:56 +00:00
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
// Analyze the number of bytes allocated...
|
2003-12-23 08:04:08 +00:00
|
|
|
ExprType Expr = ClassifyExpr(MI->getArraySize());
|
2001-11-05 18:30:53 +00:00
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
const PointerType *AllocTy = cast<PointerType>(Ty);
|
2001-12-04 00:03:30 +00:00
|
|
|
const Type *ElType = AllocTy->getElementType();
|
2001-11-05 18:30:53 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
unsigned DataSize = TD.getTypeSize(ElType);
|
|
|
|
unsigned OldTypeSize = TD.getTypeSize(MI->getType()->getElementType());
|
|
|
|
|
|
|
|
// Get the offset and scale coefficients that we are allocating...
|
2002-09-16 18:32:33 +00:00
|
|
|
int64_t OffsetVal = (Expr.Offset ? getConstantValue(Expr.Offset) : 0);
|
|
|
|
int64_t ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var !=0);
|
2001-12-14 16:35:53 +00:00
|
|
|
|
|
|
|
// The old type might not be of unit size, take old size into consideration
|
|
|
|
// here...
|
2002-09-16 18:32:33 +00:00
|
|
|
unsigned Offset = (uint64_t)OffsetVal * OldTypeSize / DataSize;
|
|
|
|
unsigned Scale = (uint64_t)ScaleVal * OldTypeSize / DataSize;
|
2001-12-14 16:35:53 +00:00
|
|
|
|
|
|
|
// Locate the malloc instruction, because we may be inserting instructions
|
2002-06-25 16:13:24 +00:00
|
|
|
It = MI;
|
2001-12-14 16:35:53 +00:00
|
|
|
|
|
|
|
// If we have a scale, apply it first...
|
|
|
|
if (Expr.Var) {
|
2003-10-10 17:57:28 +00:00
|
|
|
// Expr.Var is not necessarily unsigned right now, insert a cast now.
|
2002-09-10 17:04:02 +00:00
|
|
|
if (Expr.Var->getType() != Type::UIntTy)
|
|
|
|
Expr.Var = new CastInst(Expr.Var, Type::UIntTy,
|
|
|
|
Expr.Var->getName()+"-uint", It);
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2002-09-10 17:04:02 +00:00
|
|
|
if (Scale != 1)
|
|
|
|
Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
|
|
|
|
ConstantUInt::get(Type::UIntTy, Scale),
|
|
|
|
Expr.Var->getName()+"-scl", It);
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
} else {
|
|
|
|
// If we are not scaling anything, just make the offset be the "var"...
|
|
|
|
Expr.Var = ConstantUInt::get(Type::UIntTy, Offset);
|
|
|
|
Offset = 0; Scale = 1;
|
2001-11-26 16:57:31 +00:00
|
|
|
}
|
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
// If we have an offset now, add it in...
|
|
|
|
if (Offset != 0) {
|
|
|
|
assert(Expr.Var && "Var must be nonnull by now!");
|
2002-09-10 17:04:02 +00:00
|
|
|
Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
|
|
|
|
ConstantUInt::get(Type::UIntTy, Offset),
|
|
|
|
Expr.Var->getName()+"-off", It);
|
2001-11-26 16:57:31 +00:00
|
|
|
}
|
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
assert(AllocTy == Ty);
|
2002-09-13 22:28:45 +00:00
|
|
|
return new MallocInst(AllocTy->getElementType(), Expr.Var, Name);
|
2001-11-26 16:57:31 +00:00
|
|
|
}
|
2001-11-05 18:30:53 +00:00
|
|
|
|
|
|
|
|
2003-05-20 18:45:36 +00:00
|
|
|
// ExpressionConvertibleToType - Return true if it is possible
|
2003-12-23 07:43:38 +00:00
|
|
|
bool llvm::ExpressionConvertibleToType(Value *V, const Type *Ty,
|
2003-04-24 18:25:27 +00:00
|
|
|
ValueTypeCache &CTMap, const TargetData &TD) {
|
2001-11-06 08:34:17 +00:00
|
|
|
// Expression type must be holdable in a register.
|
2001-12-14 16:35:53 +00:00
|
|
|
if (!Ty->isFirstClassType())
|
2001-11-06 08:34:17 +00:00
|
|
|
return false;
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
ValueTypeCache::iterator CTMI = CTMap.find(V);
|
|
|
|
if (CTMI != CTMap.end()) return CTMI->second == Ty;
|
2001-11-13 05:01:36 +00:00
|
|
|
|
2003-07-24 17:31:56 +00:00
|
|
|
// If it's a constant... all constants can be converted to a different
|
2004-01-12 18:12:44 +00:00
|
|
|
// type.
|
2002-11-04 05:46:37 +00:00
|
|
|
//
|
|
|
|
if (Constant *CPV = dyn_cast<Constant>(V))
|
2004-01-12 18:12:44 +00:00
|
|
|
return true;
|
2002-11-04 05:46:37 +00:00
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
CTMap[V] = Ty;
|
2002-07-31 22:31:34 +00:00
|
|
|
if (V->getType() == Ty) return true; // Expression already correct type!
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
2002-11-04 05:46:37 +00:00
|
|
|
if (I == 0) return false; // Otherwise, we can't convert!
|
2001-11-09 01:08:10 +00:00
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Cast:
|
|
|
|
// We can convert the expr if the cast destination type is losslessly
|
2003-05-20 18:45:36 +00:00
|
|
|
// convertible to the requested type.
|
|
|
|
if (!Ty->isLosslesslyConvertibleTo(I->getType())) return false;
|
2002-05-22 17:27:12 +00:00
|
|
|
|
2001-11-14 11:02:49 +00:00
|
|
|
// We also do not allow conversion of a cast that casts from a ptr to array
|
|
|
|
// of X to a *X. For example: cast [4 x %List *] * %val to %List * *
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
if (const PointerType *SPT =
|
|
|
|
dyn_cast<PointerType>(I->getOperand(0)->getType()))
|
|
|
|
if (const PointerType *DPT = dyn_cast<PointerType>(I->getType()))
|
|
|
|
if (const ArrayType *AT = dyn_cast<ArrayType>(SPT->getElementType()))
|
2001-12-04 00:03:30 +00:00
|
|
|
if (AT->getElementType() == DPT->getElementType())
|
2001-11-14 11:02:49 +00:00
|
|
|
return false;
|
2001-11-26 16:57:31 +00:00
|
|
|
break;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
2002-09-10 19:42:53 +00:00
|
|
|
if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false;
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!ExpressionConvertibleToType(I->getOperand(0), Ty, CTMap, TD) ||
|
|
|
|
!ExpressionConvertibleToType(I->getOperand(1), Ty, CTMap, TD))
|
2001-11-13 05:01:36 +00:00
|
|
|
return false;
|
|
|
|
break;
|
2001-11-04 23:24:20 +00:00
|
|
|
case Instruction::Shr:
|
2002-09-09 20:25:21 +00:00
|
|
|
if (!Ty->isInteger()) return false;
|
2001-11-04 23:24:20 +00:00
|
|
|
if (Ty->isSigned() != V->getType()->isSigned()) return false;
|
|
|
|
// FALL THROUGH
|
|
|
|
case Instruction::Shl:
|
2002-09-09 20:25:21 +00:00
|
|
|
if (!Ty->isInteger()) return false;
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!ExpressionConvertibleToType(I->getOperand(0), Ty, CTMap, TD))
|
2001-11-13 05:01:36 +00:00
|
|
|
return false;
|
|
|
|
break;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
case Instruction::Load: {
|
|
|
|
LoadInst *LI = cast<LoadInst>(I);
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!ExpressionConvertibleToType(LI->getPointerOperand(),
|
2003-04-24 18:25:27 +00:00
|
|
|
PointerType::get(Ty), CTMap, TD))
|
2001-11-13 05:01:36 +00:00
|
|
|
return false;
|
|
|
|
break;
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
2003-10-19 21:34:28 +00:00
|
|
|
case Instruction::PHI: {
|
2001-11-06 08:34:17 +00:00
|
|
|
PHINode *PN = cast<PHINode>(I);
|
|
|
|
for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!ExpressionConvertibleToType(PN->getIncomingValue(i), Ty, CTMap, TD))
|
2001-11-06 08:34:17 +00:00
|
|
|
return false;
|
2001-11-13 05:01:36 +00:00
|
|
|
break;
|
2001-11-06 08:34:17 +00:00
|
|
|
}
|
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
case Instruction::Malloc:
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!MallocConvertibleToType(cast<MallocInst>(I), Ty, CTMap, TD))
|
2001-11-26 16:57:31 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
case Instruction::GetElementPtr: {
|
2003-05-20 18:45:36 +00:00
|
|
|
// GetElementPtr's are directly convertible to a pointer type if they have
|
2001-11-04 23:24:20 +00:00
|
|
|
// 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);
|
2001-12-07 04:40:30 +00:00
|
|
|
if (!PTy) return false; // GEP must always return a pointer...
|
|
|
|
const Type *PVTy = PTy->getElementType();
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
// 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...
|
|
|
|
//
|
2002-08-22 23:37:20 +00:00
|
|
|
std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
|
2001-11-14 11:27:58 +00:00
|
|
|
const Type *BaseType = GEP->getPointerOperand()->getType();
|
2001-11-26 16:57:31 +00:00
|
|
|
const Type *ElTy = 0;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2002-09-11 01:21:29 +00:00
|
|
|
while (!Indices.empty() &&
|
|
|
|
Indices.back() == Constant::getNullValue(Indices.back()->getType())){
|
2001-11-04 23:24:20 +00:00
|
|
|
Indices.pop_back();
|
2001-12-07 04:40:30 +00:00
|
|
|
ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices, true);
|
|
|
|
if (ElTy == PVTy)
|
2001-11-13 05:01:36 +00:00
|
|
|
break; // Found a match!!
|
2001-11-26 16:57:31 +00:00
|
|
|
ElTy = 0;
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2001-12-07 04:40:30 +00:00
|
|
|
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
|
2002-09-11 01:21:29 +00:00
|
|
|
// current gep is of the form 'getelementptr sbyte*, long N
|
2001-12-07 04:40:30 +00:00
|
|
|
// and we could convert this to an appropriate GEP for the new type.
|
|
|
|
//
|
|
|
|
if (GEP->getNumOperands() == 2 &&
|
|
|
|
GEP->getType() == PointerType::get(Type::SByteTy)) {
|
|
|
|
|
|
|
|
// Do not Check to see if our incoming pointer can be converted
|
|
|
|
// to be a ptr to an array of the right type... because in more cases than
|
|
|
|
// not, it is simply not analyzable because of pointer/array
|
2003-10-10 17:57:28 +00:00
|
|
|
// discrepancies. To fix this, we will insert a cast before the GEP.
|
2001-12-07 04:40:30 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
// Check to see if 'N' is an expression that can be converted to
|
|
|
|
// the appropriate size... if so, allow it.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Indices;
|
2003-05-20 18:45:36 +00:00
|
|
|
const Type *ElTy = ConvertibleToGEP(PTy, I->getOperand(1), Indices, TD);
|
2002-02-14 22:21:40 +00:00
|
|
|
if (ElTy == PVTy) {
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!ExpressionConvertibleToType(I->getOperand(0),
|
2003-04-24 18:25:27 +00:00
|
|
|
PointerType::get(ElTy), CTMap, TD))
|
2001-12-14 16:35:53 +00:00
|
|
|
return false; // Can't continue, ExConToTy might have polluted set!
|
2001-12-07 04:40:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it could be that we have something like this:
|
2002-09-11 01:21:29 +00:00
|
|
|
// getelementptr [[sbyte] *] * %reg115, long %reg138 ; [sbyte]**
|
2001-12-07 04:40:30 +00:00
|
|
|
// and want to convert it into something like this:
|
2002-09-11 01:21:29 +00:00
|
|
|
// getelemenptr [[int] *] * %reg115, long %reg138 ; [int]**
|
2001-12-07 04:40:30 +00:00
|
|
|
//
|
|
|
|
if (GEP->getNumOperands() == 2 &&
|
2003-01-14 19:04:43 +00:00
|
|
|
PTy->getElementType()->isSized() &&
|
2001-12-07 04:40:30 +00:00
|
|
|
TD.getTypeSize(PTy->getElementType()) ==
|
|
|
|
TD.getTypeSize(GEP->getType()->getElementType())) {
|
2001-12-14 16:35:53 +00:00
|
|
|
const PointerType *NewSrcTy = PointerType::get(PVTy);
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!ExpressionConvertibleToType(I->getOperand(0), NewSrcTy, CTMap, TD))
|
2001-12-14 16:35:53 +00:00
|
|
|
return false;
|
|
|
|
break;
|
2001-12-07 04:40:30 +00:00
|
|
|
}
|
|
|
|
|
2001-11-13 05:01:36 +00:00
|
|
|
return false; // No match, maybe next time.
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
2001-11-13 05:01:36 +00:00
|
|
|
|
2002-11-13 06:54:20 +00:00
|
|
|
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());
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!ExpressionConvertibleToType(I->getOperand(0),
|
2003-04-24 18:25:27 +00:00
|
|
|
PointerType::get(NewTy), CTMap, TD))
|
2002-11-13 06:54:20 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2001-11-13 05:01:36 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
|
2003-05-20 18:45:36 +00:00
|
|
|
// Expressions are only convertible if all of the users of the expression can
|
2001-11-13 05:01:36 +00:00
|
|
|
// have this value converted. This makes use of the map to avoid infinite
|
|
|
|
// recursion.
|
|
|
|
//
|
2001-11-26 16:57:31 +00:00
|
|
|
for (Value::use_iterator It = I->use_begin(), E = I->use_end(); It != E; ++It)
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!OperandConvertibleToType(*It, I, Ty, CTMap, TD))
|
2001-11-26 16:57:31 +00:00
|
|
|
return false;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-13 05:01:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
|
2003-12-23 07:43:38 +00:00
|
|
|
Value *llvm::ConvertExpressionToType(Value *V, const Type *Ty,
|
|
|
|
ValueMapCache &VMC, const TargetData &TD) {
|
2001-12-05 19:39:15 +00:00
|
|
|
if (V->getType() == Ty) return V; // Already where we need to be?
|
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(V);
|
2001-11-13 05:01:36 +00:00
|
|
|
if (VMCI != VMC.ExprMap.end()) {
|
2002-07-31 22:31:34 +00:00
|
|
|
const Value *GV = VMCI->second;
|
|
|
|
const Type *GTy = VMCI->second->getType();
|
2001-11-13 05:01:36 +00:00
|
|
|
assert(VMCI->second->getType() == Ty);
|
2002-02-14 18:55:22 +00:00
|
|
|
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
ValueHandle IHandle(VMC, I); // Remove I if it is unused now!
|
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
return VMCI->second;
|
2001-11-13 05:01:36 +00:00
|
|
|
}
|
2001-11-05 18:30:53 +00:00
|
|
|
|
2003-10-16 16:48:53 +00:00
|
|
|
DEBUG(std::cerr << "CETT: " << (void*)V << " " << V);
|
2001-11-06 08:34:17 +00:00
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
2002-11-04 05:46:37 +00:00
|
|
|
if (I == 0) {
|
2002-11-04 05:50:42 +00:00
|
|
|
Constant *CPV = cast<Constant>(V);
|
2002-11-04 05:46:37 +00:00
|
|
|
// Constants are converted by constant folding the cast that is required.
|
|
|
|
// We assume here that all casts are implemented for constant prop.
|
2004-01-12 18:12:44 +00:00
|
|
|
Value *Result = ConstantExpr::getCast(CPV, Ty);
|
2002-11-04 05:46:37 +00:00
|
|
|
// Add the instruction to the expression map
|
|
|
|
//VMC.ExprMap[V] = Result;
|
|
|
|
return Result;
|
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
BasicBlock *BB = I->getParent();
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string Name = I->getName(); if (!Name.empty()) I->setName("");
|
2001-11-04 23:24:20 +00:00
|
|
|
Instruction *Res; // Result of conversion
|
|
|
|
|
2001-11-13 05:01:36 +00:00
|
|
|
ValueHandle IHandle(VMC, I); // Prevent I from being removed!
|
2001-11-06 08:34:17 +00:00
|
|
|
|
2002-04-27 02:25:14 +00:00
|
|
|
Constant *Dummy = Constant::getNullValue(Ty);
|
2001-11-06 08:34:17 +00:00
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Cast:
|
2002-07-17 17:11:33 +00:00
|
|
|
assert(VMC.NewCasts.count(ValueHandle(VMC, I)) == 0);
|
2001-11-04 23:24:20 +00:00
|
|
|
Res = new CastInst(I->getOperand(0), Ty, Name);
|
2002-07-17 17:11:33 +00:00
|
|
|
VMC.NewCasts.insert(ValueHandle(VMC, Res));
|
2001-11-04 23:24:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
|
2001-11-06 08:34:17 +00:00
|
|
|
Dummy, Dummy, Name);
|
|
|
|
VMC.ExprMap[I] = Res; // Add node to expression eagerly
|
|
|
|
|
2003-04-24 18:25:27 +00:00
|
|
|
Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC, TD));
|
|
|
|
Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), Ty, VMC, TD));
|
2001-11-04 23:24:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::Shr:
|
2001-11-06 21:51:48 +00:00
|
|
|
Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), Dummy,
|
2001-11-04 23:24:20 +00:00
|
|
|
I->getOperand(1), Name);
|
2001-11-06 21:51:48 +00:00
|
|
|
VMC.ExprMap[I] = Res;
|
2003-04-24 18:25:27 +00:00
|
|
|
Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC, TD));
|
2001-11-04 23:24:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Load: {
|
|
|
|
LoadInst *LI = cast<LoadInst>(I);
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2002-04-27 02:25:14 +00:00
|
|
|
Res = new LoadInst(Constant::getNullValue(PointerType::get(Ty)), Name);
|
2001-11-06 08:34:17 +00:00
|
|
|
VMC.ExprMap[I] = Res;
|
2001-11-14 11:27:58 +00:00
|
|
|
Res->setOperand(0, ConvertExpressionToType(LI->getPointerOperand(),
|
2003-04-24 18:25:27 +00:00
|
|
|
PointerType::get(Ty), VMC, TD));
|
2001-11-13 05:01:36 +00:00
|
|
|
assert(Res->getOperand(0)->getType() == PointerType::get(Ty));
|
|
|
|
assert(Ty == Res->getType());
|
2001-12-14 16:35:53 +00:00
|
|
|
assert(Res->getType()->isFirstClassType() && "Load of structure or array!");
|
2001-11-06 08:34:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-10-19 21:34:28 +00:00
|
|
|
case Instruction::PHI: {
|
2001-11-06 08:34:17 +00:00
|
|
|
PHINode *OldPN = cast<PHINode>(I);
|
|
|
|
PHINode *NewPN = new PHINode(Ty, Name);
|
|
|
|
|
|
|
|
VMC.ExprMap[I] = NewPN; // Add node to expression eagerly
|
|
|
|
while (OldPN->getNumOperands()) {
|
|
|
|
BasicBlock *BB = OldPN->getIncomingBlock(0);
|
|
|
|
Value *OldVal = OldPN->getIncomingValue(0);
|
2001-11-13 05:01:36 +00:00
|
|
|
ValueHandle OldValHandle(VMC, OldVal);
|
2002-10-08 21:36:34 +00:00
|
|
|
OldPN->removeIncomingValue(BB, false);
|
2003-04-24 18:25:27 +00:00
|
|
|
Value *V = ConvertExpressionToType(OldVal, Ty, VMC, TD);
|
2001-11-06 08:34:17 +00:00
|
|
|
NewPN->addIncoming(V, BB);
|
|
|
|
}
|
|
|
|
Res = NewPN;
|
2001-11-04 23:24:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
case Instruction::Malloc: {
|
2003-04-24 18:25:27 +00:00
|
|
|
Res = ConvertMallocToType(cast<MallocInst>(I), Ty, Name, VMC, TD);
|
2001-11-26 16:57:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
case Instruction::GetElementPtr: {
|
2003-05-20 18:45:36 +00:00
|
|
|
// GetElementPtr's are directly convertible to a pointer type if they have
|
2001-11-04 23:24:20 +00:00
|
|
|
// 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...
|
|
|
|
//
|
2002-08-22 23:37:20 +00:00
|
|
|
std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
|
2001-11-14 11:27:58 +00:00
|
|
|
const Type *BaseType = GEP->getPointerOperand()->getType();
|
2001-12-04 00:03:30 +00:00
|
|
|
const Type *PVTy = cast<PointerType>(Ty)->getElementType();
|
2001-11-04 23:24:20 +00:00
|
|
|
Res = 0;
|
2002-09-11 01:21:29 +00:00
|
|
|
while (!Indices.empty() &&
|
|
|
|
Indices.back() == Constant::getNullValue(Indices.back()->getType())){
|
2001-11-04 23:24:20 +00:00
|
|
|
Indices.pop_back();
|
|
|
|
if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
|
2002-09-11 01:21:29 +00:00
|
|
|
if (Indices.size() == 0)
|
|
|
|
Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP CAST
|
|
|
|
else
|
2001-11-14 11:27:58 +00:00
|
|
|
Res = new GetElementPtrInst(GEP->getPointerOperand(), Indices, Name);
|
2001-11-04 23:24:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-12-07 04:40:30 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
if (Res == 0 && GEP->getNumOperands() == 2 &&
|
|
|
|
GEP->getType() == PointerType::get(Type::SByteTy)) {
|
|
|
|
|
2001-12-07 04:40:30 +00:00
|
|
|
// Otherwise, we can convert a GEP from one form to the other iff the
|
2003-11-29 05:31:25 +00:00
|
|
|
// current gep is of the form 'getelementptr sbyte*, unsigned N
|
2001-12-07 04:40:30 +00:00
|
|
|
// and we could convert this to an appropriate GEP for the new type.
|
|
|
|
//
|
2001-12-14 16:35:53 +00:00
|
|
|
const PointerType *NewSrcTy = PointerType::get(PVTy);
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock::iterator It = I;
|
2001-12-07 04:40:30 +00:00
|
|
|
|
|
|
|
// Check to see if 'N' is an expression that can be converted to
|
|
|
|
// the appropriate size... if so, allow it.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Indices;
|
2003-05-20 18:45:36 +00:00
|
|
|
const Type *ElTy = ConvertibleToGEP(NewSrcTy, I->getOperand(1),
|
2003-04-24 18:25:27 +00:00
|
|
|
Indices, TD, &It);
|
2001-12-14 16:35:53 +00:00
|
|
|
if (ElTy) {
|
2001-12-07 04:40:30 +00:00
|
|
|
assert(ElTy == PVTy && "Internal error, setup wrong!");
|
2002-04-27 02:25:14 +00:00
|
|
|
Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy),
|
2001-12-14 16:35:53 +00:00
|
|
|
Indices, Name);
|
|
|
|
VMC.ExprMap[I] = Res;
|
|
|
|
Res->setOperand(0, ConvertExpressionToType(I->getOperand(0),
|
2003-04-24 18:25:27 +00:00
|
|
|
NewSrcTy, VMC, TD));
|
2001-12-07 04:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it could be that we have something like this:
|
|
|
|
// getelementptr [[sbyte] *] * %reg115, uint %reg138 ; [sbyte]**
|
|
|
|
// and want to convert it into something like this:
|
|
|
|
// getelemenptr [[int] *] * %reg115, uint %reg138 ; [int]**
|
|
|
|
//
|
|
|
|
if (Res == 0) {
|
2001-12-14 16:35:53 +00:00
|
|
|
const PointerType *NewSrcTy = PointerType::get(PVTy);
|
2002-08-22 23:37:20 +00:00
|
|
|
std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
|
2002-04-27 02:25:14 +00:00
|
|
|
Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy),
|
2002-08-22 23:37:20 +00:00
|
|
|
Indices, Name);
|
2001-12-07 04:40:30 +00:00
|
|
|
VMC.ExprMap[I] = Res;
|
|
|
|
Res->setOperand(0, ConvertExpressionToType(I->getOperand(0),
|
2003-04-24 18:25:27 +00:00
|
|
|
NewSrcTy, VMC, TD));
|
2001-12-07 04:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
assert(Res && "Didn't find match!");
|
2002-11-13 06:54:20 +00:00
|
|
|
break;
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
|
2002-11-13 06:54:20 +00:00
|
|
|
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);
|
2003-02-14 03:07:17 +00:00
|
|
|
if (Ty == Type::VoidTy)
|
|
|
|
Name = ""; // Make sure not to name calls that now return void!
|
2002-11-13 06:54:20 +00:00
|
|
|
|
|
|
|
Res = new CallInst(Constant::getNullValue(NewPTy),
|
|
|
|
std::vector<Value*>(I->op_begin()+1, I->op_end()),
|
|
|
|
Name);
|
|
|
|
VMC.ExprMap[I] = Res;
|
2003-04-24 18:25:27 +00:00
|
|
|
Res->setOperand(0, ConvertExpressionToType(I->getOperand(0),NewPTy,VMC,TD));
|
2002-11-13 06:54:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
default:
|
2003-05-20 18:45:36 +00:00
|
|
|
assert(0 && "Expression convertible, but don't know how to convert?");
|
2001-11-04 23:24:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-13 05:01:36 +00:00
|
|
|
assert(Res->getType() == Ty && "Didn't convert expr to correct type!");
|
|
|
|
|
2002-09-10 22:38:06 +00:00
|
|
|
BB->getInstList().insert(I, Res);
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
// Add the instruction to the expression map
|
|
|
|
VMC.ExprMap[I] = Res;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2003-10-16 16:48:53 +00:00
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
unsigned NumUses = I->use_size();
|
|
|
|
for (unsigned It = 0; It < NumUses; ) {
|
|
|
|
unsigned OldSize = NumUses;
|
2003-10-16 16:48:53 +00:00
|
|
|
Value::use_iterator UI = I->use_begin();
|
|
|
|
std::advance(UI, It);
|
|
|
|
ConvertOperandToType(*UI, I, Res, VMC, TD);
|
2001-11-05 18:30:53 +00:00
|
|
|
NumUses = I->use_size();
|
|
|
|
if (NumUses == OldSize) ++It;
|
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2003-10-16 16:48:53 +00:00
|
|
|
DEBUG(std::cerr << "ExpIn: " << (void*)I << " " << I
|
|
|
|
<< "ExpOut: " << (void*)Res << " " << Res);
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
return Res;
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-05-20 18:45:36 +00:00
|
|
|
// ValueConvertibleToType - Return true if it is possible
|
2003-12-23 07:43:38 +00:00
|
|
|
bool llvm::ValueConvertibleToType(Value *V, const Type *Ty,
|
|
|
|
ValueTypeCache &ConvertedTypes,
|
|
|
|
const TargetData &TD) {
|
2001-11-04 23:24:20 +00:00
|
|
|
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.
|
|
|
|
//
|
2002-02-14 19:44:09 +00:00
|
|
|
if (V->getType() != Ty) {
|
|
|
|
for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!OperandConvertibleToType(*I, V, Ty, ConvertedTypes, TD))
|
2002-02-14 19:44:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-05-20 18:45:36 +00:00
|
|
|
// OperandConvertibleToType - Return true if it is possible to convert operand
|
2001-11-04 23:24:20 +00:00
|
|
|
// V of User (instruction) U to the specified type. This is true iff it is
|
|
|
|
// possible to change the specified instruction to accept this. CTMap is a map
|
|
|
|
// of converted types, so that circular definitions will see the future type of
|
|
|
|
// the expression, not the static current type.
|
|
|
|
//
|
2003-05-20 18:45:36 +00:00
|
|
|
static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty,
|
2003-04-24 18:25:27 +00:00
|
|
|
ValueTypeCache &CTMap,
|
|
|
|
const TargetData &TD) {
|
2001-12-14 16:35:53 +00:00
|
|
|
// if (V->getType() == Ty) return true; // Operand already the right type?
|
2001-11-08 20:19:56 +00:00
|
|
|
|
|
|
|
// Expression type must be holdable in a register.
|
2001-12-14 16:35:53 +00:00
|
|
|
if (!Ty->isFirstClassType())
|
2001-11-08 20:19:56 +00:00
|
|
|
return false;
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
Instruction *I = dyn_cast<Instruction>(U);
|
|
|
|
if (I == 0) return false; // We can't convert!
|
|
|
|
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Cast:
|
|
|
|
assert(I->getOperand(0) == V);
|
|
|
|
// We can convert the expr if the cast destination type is losslessly
|
2003-05-20 18:45:36 +00:00
|
|
|
// convertible to the requested type.
|
2001-12-14 16:35:53 +00:00
|
|
|
// Also, do not change a cast that is a noop cast. For all intents and
|
|
|
|
// purposes it should be eliminated.
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!Ty->isLosslesslyConvertibleTo(I->getOperand(0)->getType()) ||
|
2001-12-14 16:35:53 +00:00
|
|
|
I->getType() == I->getOperand(0)->getType())
|
2001-11-14 11:02:49 +00:00
|
|
|
return false;
|
2001-12-14 16:35:53 +00:00
|
|
|
|
2002-05-02 17:37:34 +00:00
|
|
|
// Do not allow a 'cast ushort %V to uint' to have it's first operand be
|
|
|
|
// converted to a 'short' type. Doing so changes the way sign promotion
|
|
|
|
// happens, and breaks things. Only allow the cast to take place if the
|
|
|
|
// signedness doesn't change... or if the current cast is not a lossy
|
|
|
|
// conversion.
|
|
|
|
//
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!I->getType()->isLosslesslyConvertibleTo(I->getOperand(0)->getType()) &&
|
2002-05-02 17:37:34 +00:00
|
|
|
I->getOperand(0)->getType()->isSigned() != Ty->isSigned())
|
|
|
|
return false;
|
2001-12-14 16:35:53 +00:00
|
|
|
|
2001-11-14 11:02:49 +00:00
|
|
|
// We also do not allow conversion of a cast that casts from a ptr to array
|
|
|
|
// of X to a *X. For example: cast [4 x %List *] * %val to %List * *
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
if (const PointerType *SPT =
|
|
|
|
dyn_cast<PointerType>(I->getOperand(0)->getType()))
|
|
|
|
if (const PointerType *DPT = dyn_cast<PointerType>(I->getType()))
|
|
|
|
if (const ArrayType *AT = dyn_cast<ArrayType>(SPT->getElementType()))
|
2001-12-04 00:03:30 +00:00
|
|
|
if (AT->getElementType() == DPT->getElementType())
|
2001-11-14 11:02:49 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
case Instruction::Add:
|
2001-12-05 06:34:00 +00:00
|
|
|
if (isa<PointerType>(Ty)) {
|
|
|
|
Value *IndexVal = I->getOperand(V == I->getOperand(0) ? 1 : 0);
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Indices;
|
2003-05-20 18:45:36 +00:00
|
|
|
if (const Type *ETy = ConvertibleToGEP(Ty, IndexVal, Indices, TD)) {
|
2001-11-26 16:57:31 +00:00
|
|
|
const Type *RetTy = PointerType::get(ETy);
|
|
|
|
|
2001-11-08 20:19:56 +00:00
|
|
|
// Only successful if we can convert this type to the required type
|
2003-05-20 18:45:36 +00:00
|
|
|
if (ValueConvertibleToType(I, RetTy, CTMap, TD)) {
|
2001-11-26 16:57:31 +00:00
|
|
|
CTMap[I] = RetTy;
|
|
|
|
return true;
|
|
|
|
}
|
2003-05-20 18:45:36 +00:00
|
|
|
// We have to return failure here because ValueConvertibleToType could
|
2001-12-14 16:35:53 +00:00
|
|
|
// have polluted our map
|
|
|
|
return false;
|
2001-11-08 20:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
2001-11-04 23:24:20 +00:00
|
|
|
case Instruction::Sub: {
|
2002-09-10 19:42:53 +00:00
|
|
|
if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false;
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
|
2003-05-20 18:45:36 +00:00
|
|
|
return ValueConvertibleToType(I, Ty, CTMap, TD) &&
|
|
|
|
ExpressionConvertibleToType(OtherOp, Ty, CTMap, TD);
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
case Instruction::SetEQ:
|
|
|
|
case Instruction::SetNE: {
|
|
|
|
Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
|
2003-05-20 18:45:36 +00:00
|
|
|
return ExpressionConvertibleToType(OtherOp, Ty, CTMap, TD);
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
case Instruction::Shr:
|
|
|
|
if (Ty->isSigned() != V->getType()->isSigned()) return false;
|
|
|
|
// FALL THROUGH
|
|
|
|
case Instruction::Shl:
|
2003-01-30 17:10:41 +00:00
|
|
|
if (I->getOperand(1) == V) return false; // Cannot change shift amount type
|
2002-09-09 20:25:21 +00:00
|
|
|
if (!Ty->isInteger()) return false;
|
2003-05-20 18:45:36 +00:00
|
|
|
return ValueConvertibleToType(I, Ty, CTMap, TD);
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
case Instruction::Free:
|
|
|
|
assert(I->getOperand(0) == V);
|
|
|
|
return isa<PointerType>(Ty); // Free can free any pointer type!
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
case Instruction::Load:
|
2001-11-26 16:57:31 +00:00
|
|
|
// Cannot convert the types of any subscripts...
|
|
|
|
if (I->getOperand(0) != V) return false;
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
|
|
|
|
LoadInst *LI = cast<LoadInst>(I);
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2001-12-04 00:03:30 +00:00
|
|
|
const Type *LoadedTy = PT->getElementType();
|
2001-11-26 16:57:31 +00:00
|
|
|
|
|
|
|
// They could be loading the first element of a composite type...
|
|
|
|
if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) {
|
|
|
|
unsigned Offset = 0; // No offset, get first leaf.
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Indices; // Discarded...
|
2003-04-24 18:25:27 +00:00
|
|
|
LoadedTy = getStructOffsetType(CT, Offset, Indices, TD, false);
|
2001-11-26 16:57:31 +00:00
|
|
|
assert(Offset == 0 && "Offset changed from zero???");
|
2001-11-08 20:19:56 +00:00
|
|
|
}
|
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
if (!LoadedTy->isFirstClassType())
|
2001-11-26 16:57:31 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (TD.getTypeSize(LoadedTy) != TD.getTypeSize(LI->getType()))
|
2001-11-04 23:24:20 +00:00
|
|
|
return false;
|
|
|
|
|
2003-05-20 18:45:36 +00:00
|
|
|
return ValueConvertibleToType(LI, LoadedTy, CTMap, TD);
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Instruction::Store: {
|
|
|
|
StoreInst *SI = cast<StoreInst>(I);
|
|
|
|
|
|
|
|
if (V == I->getOperand(0)) {
|
2002-03-21 21:21:13 +00:00
|
|
|
ValueTypeCache::iterator CTMI = CTMap.find(I->getOperand(1));
|
|
|
|
if (CTMI != CTMap.end()) { // Operand #1 is in the table already?
|
|
|
|
// If so, check to see if it's Ty*, or, more importantly, if it is a
|
|
|
|
// pointer to a structure where the first element is a Ty... this code
|
2003-08-18 14:43:39 +00:00
|
|
|
// is necessary because we might be trying to change the source and
|
2002-03-21 21:21:13 +00:00
|
|
|
// destination type of the store (they might be related) and the dest
|
|
|
|
// pointer type might be a pointer to structure. Below we allow pointer
|
|
|
|
// to structures where the 0th element is compatible with the value,
|
|
|
|
// now we have to support the symmetrical part of this.
|
|
|
|
//
|
|
|
|
const Type *ElTy = cast<PointerType>(CTMI->second)->getElementType();
|
|
|
|
|
|
|
|
// Already a pointer to what we want? Trivially accept...
|
|
|
|
if (ElTy == Ty) return true;
|
|
|
|
|
|
|
|
// Tricky case now, if the destination is a pointer to structure,
|
|
|
|
// obviously the source is not allowed to be a structure (cannot copy
|
|
|
|
// a whole structure at a time), so the level raiser must be trying to
|
|
|
|
// store into the first field. Check for this and allow it now:
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
if (const StructType *SElTy = dyn_cast<StructType>(ElTy)) {
|
2002-03-21 21:21:13 +00:00
|
|
|
unsigned Offset = 0;
|
|
|
|
std::vector<Value*> Indices;
|
2003-04-24 18:25:27 +00:00
|
|
|
ElTy = getStructOffsetType(ElTy, Offset, Indices, TD, false);
|
2002-03-21 21:21:13 +00:00
|
|
|
assert(Offset == 0 && "Offset changed!");
|
|
|
|
if (ElTy == 0) // Element at offset zero in struct doesn't exist!
|
|
|
|
return false; // Can only happen for {}*
|
|
|
|
|
|
|
|
if (ElTy == Ty) // Looks like the 0th element of structure is
|
|
|
|
return true; // compatible! Accept now!
|
|
|
|
|
|
|
|
// Otherwise we know that we can't work, so just stop trying now.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
// Can convert the store if we can convert the pointer operand to match
|
|
|
|
// the new value type...
|
2003-05-20 18:45:36 +00:00
|
|
|
return ExpressionConvertibleToType(I->getOperand(1), PointerType::get(Ty),
|
2003-04-24 18:25:27 +00:00
|
|
|
CTMap, TD);
|
2001-11-04 23:24:20 +00:00
|
|
|
} else if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
|
2001-12-05 19:39:15 +00:00
|
|
|
const Type *ElTy = PT->getElementType();
|
2001-11-04 23:24:20 +00:00
|
|
|
assert(V == I->getOperand(1));
|
|
|
|
|
2002-03-07 21:17:35 +00:00
|
|
|
if (isa<StructType>(ElTy)) {
|
|
|
|
// We can change the destination pointer if we can store our first
|
|
|
|
// argument into the first element of the structure...
|
|
|
|
//
|
|
|
|
unsigned Offset = 0;
|
|
|
|
std::vector<Value*> Indices;
|
2003-04-24 18:25:27 +00:00
|
|
|
ElTy = getStructOffsetType(ElTy, Offset, Indices, TD, false);
|
2002-03-07 21:17:35 +00:00
|
|
|
assert(Offset == 0 && "Offset changed!");
|
|
|
|
if (ElTy == 0) // Element at offset zero in struct doesn't exist!
|
|
|
|
return false; // Can only happen for {}*
|
|
|
|
}
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
// Must move the same amount of data...
|
2002-07-16 22:29:37 +00:00
|
|
|
if (!ElTy->isSized() ||
|
|
|
|
TD.getTypeSize(ElTy) != TD.getTypeSize(I->getOperand(0)->getType()))
|
2001-12-05 19:39:15 +00:00
|
|
|
return false;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2003-11-29 05:31:25 +00:00
|
|
|
// Can convert store if the incoming value is convertible and if the
|
|
|
|
// result will preserve semantics...
|
|
|
|
const Type *Op0Ty = I->getOperand(0)->getType();
|
|
|
|
if (!(Op0Ty->isIntegral() ^ ElTy->isIntegral()) &&
|
|
|
|
!(Op0Ty->isFloatingPoint() ^ ElTy->isFloatingPoint()))
|
|
|
|
return ExpressionConvertibleToType(I->getOperand(0), ElTy, CTMap, TD);
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
if (V != I->getOperand(0) || !isa<PointerType>(Ty)) return false;
|
2001-12-07 04:40:30 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
// If we have a two operand form of getelementptr, this is really little
|
|
|
|
// more than a simple addition. As with addition, check to see if the
|
|
|
|
// getelementptr instruction can be changed to index into the new type.
|
2001-12-07 04:40:30 +00:00
|
|
|
//
|
2001-12-14 16:35:53 +00:00
|
|
|
if (I->getNumOperands() == 2) {
|
|
|
|
const Type *OldElTy = cast<PointerType>(I->getType())->getElementType();
|
|
|
|
unsigned DataSize = TD.getTypeSize(OldElTy);
|
|
|
|
Value *Index = I->getOperand(1);
|
|
|
|
Instruction *TempScale = 0;
|
|
|
|
|
|
|
|
// If the old data element is not unit sized, we have to create a scale
|
2003-05-20 18:45:36 +00:00
|
|
|
// instruction so that ConvertibleToGEP will know the REAL amount we are
|
2001-12-14 16:35:53 +00:00
|
|
|
// indexing by. Note that this is never inserted into the instruction
|
|
|
|
// stream, so we have to delete it when we're done.
|
|
|
|
//
|
|
|
|
if (DataSize != 1) {
|
2004-01-09 05:53:38 +00:00
|
|
|
// FIXME, PR82
|
2001-12-14 16:35:53 +00:00
|
|
|
TempScale = BinaryOperator::create(Instruction::Mul, Index,
|
2002-09-11 01:21:29 +00:00
|
|
|
ConstantSInt::get(Type::LongTy,
|
2001-12-14 16:35:53 +00:00
|
|
|
DataSize));
|
|
|
|
Index = TempScale;
|
|
|
|
}
|
2001-12-07 04:40:30 +00:00
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
// Check to see if the second argument is an expression that can
|
|
|
|
// be converted to the appropriate size... if so, allow it.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Indices;
|
2003-05-20 18:45:36 +00:00
|
|
|
const Type *ElTy = ConvertibleToGEP(Ty, Index, Indices, TD);
|
2001-12-14 16:35:53 +00:00
|
|
|
delete TempScale; // Free our temporary multiply if we made it
|
|
|
|
|
|
|
|
if (ElTy == 0) return false; // Cannot make conversion...
|
2003-05-20 18:45:36 +00:00
|
|
|
return ValueConvertibleToType(I, PointerType::get(ElTy), CTMap, TD);
|
2001-12-14 16:35:53 +00:00
|
|
|
}
|
|
|
|
return false;
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2003-10-19 21:34:28 +00:00
|
|
|
case Instruction::PHI: {
|
2001-11-06 08:34:17 +00:00
|
|
|
PHINode *PN = cast<PHINode>(I);
|
|
|
|
for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!ExpressionConvertibleToType(PN->getIncomingValue(i), Ty, CTMap, TD))
|
2001-11-06 08:34:17 +00:00
|
|
|
return false;
|
2003-05-20 18:45:36 +00:00
|
|
|
return ValueConvertibleToType(PN, Ty, CTMap, TD);
|
2001-11-06 08:34:17 +00:00
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
case Instruction::Call: {
|
|
|
|
User::op_iterator OI = find(I->op_begin(), I->op_end(), V);
|
|
|
|
assert (OI != I->op_end() && "Not using value!");
|
|
|
|
unsigned OpNum = OI - I->op_begin();
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// Are we trying to change the function pointer value to a new type?
|
2002-02-14 19:44:09 +00:00
|
|
|
if (OpNum == 0) {
|
2002-06-25 16:13:24 +00:00
|
|
|
const PointerType *PTy = dyn_cast<PointerType>(Ty);
|
2002-02-14 19:44:09 +00:00
|
|
|
if (PTy == 0) return false; // Can't convert to a non-pointer type...
|
2002-10-09 00:16:00 +00:00
|
|
|
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!
|
2002-02-14 19:44:09 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// Perform sanity checks to make sure that new function type has the
|
2002-02-14 19:44:09 +00:00
|
|
|
// correct number of arguments...
|
|
|
|
//
|
2002-04-07 20:49:59 +00:00
|
|
|
unsigned NumArgs = I->getNumOperands()-1; // Don't include function ptr
|
2002-02-14 19:44:09 +00:00
|
|
|
|
|
|
|
// Cannot convert to a type that requires more fixed arguments than
|
|
|
|
// the call provides...
|
|
|
|
//
|
2002-10-09 00:16:00 +00:00
|
|
|
if (NumArgs < FTy->getNumParams()) return false;
|
2002-02-14 19:44:09 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// Unless this is a vararg function type, we cannot provide more arguments
|
2002-02-14 19:44:09 +00:00
|
|
|
// than are desired...
|
|
|
|
//
|
2002-10-09 00:16:00 +00:00
|
|
|
if (!FTy->isVarArg() && NumArgs > FTy->getNumParams())
|
2002-02-14 19:44:09 +00:00
|
|
|
return false;
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// Okay, at this point, we know that the call and the function type match
|
2002-02-14 19:44:09 +00:00
|
|
|
// number of arguments. Now we see if we can convert the arguments
|
2003-05-20 18:45:36 +00:00
|
|
|
// themselves. Note that we do not require operands to be convertible,
|
2002-03-11 17:27:34 +00:00
|
|
|
// we can insert casts if they are convertible but not compatible. The
|
2002-04-07 20:49:59 +00:00
|
|
|
// reason for this is that we prefer to have resolved functions but casted
|
2002-03-11 17:27:34 +00:00
|
|
|
// arguments if possible.
|
2002-02-14 19:44:09 +00:00
|
|
|
//
|
2002-10-09 00:16:00 +00:00
|
|
|
const FunctionType::ParamTypes &PTs = FTy->getParamTypes();
|
2002-02-14 19:44:09 +00:00
|
|
|
for (unsigned i = 0, NA = PTs.size(); i < NA; ++i)
|
2003-05-20 18:45:36 +00:00
|
|
|
if (!PTs[i]->isLosslesslyConvertibleTo(I->getOperand(i+1)->getType()))
|
2002-02-14 19:44:09 +00:00
|
|
|
return false; // Operands must have compatible types!
|
|
|
|
|
|
|
|
// Okay, at this point, we know that all of the arguments can be
|
|
|
|
// converted. We succeed if we can change the return type if
|
2003-08-18 14:43:39 +00:00
|
|
|
// necessary...
|
2002-02-14 19:44:09 +00:00
|
|
|
//
|
2003-05-20 18:45:36 +00:00
|
|
|
return ValueConvertibleToType(I, FTy->getReturnType(), CTMap, TD);
|
2002-02-14 19:44:09 +00:00
|
|
|
}
|
2001-11-26 16:57:31 +00:00
|
|
|
|
|
|
|
const PointerType *MPtr = cast<PointerType>(I->getOperand(0)->getType());
|
2002-10-09 00:16:00 +00:00
|
|
|
const FunctionType *FTy = cast<FunctionType>(MPtr->getElementType());
|
|
|
|
if (!FTy->isVarArg()) return false;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2002-10-09 00:16:00 +00:00
|
|
|
if ((OpNum-1) < FTy->getParamTypes().size())
|
2001-11-26 16:57:31 +00:00
|
|
|
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
|
2002-04-07 20:49:59 +00:00
|
|
|
// function! We can convert if we don't reinterpret the value...
|
2001-11-26 16:57:31 +00:00
|
|
|
//
|
2003-05-20 18:45:36 +00:00
|
|
|
return Ty->isLosslesslyConvertibleTo(V->getType());
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-23 07:43:38 +00:00
|
|
|
void llvm::ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC,
|
|
|
|
const TargetData &TD) {
|
2001-11-13 05:01:36 +00:00
|
|
|
ValueHandle VH(VMC, V);
|
2001-11-06 08:34:17 +00:00
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
unsigned NumUses = V->use_size();
|
|
|
|
for (unsigned It = 0; It < NumUses; ) {
|
|
|
|
unsigned OldSize = NumUses;
|
2003-10-16 16:48:53 +00:00
|
|
|
Value::use_iterator UI = V->use_begin();
|
|
|
|
std::advance(UI, It);
|
|
|
|
ConvertOperandToType(*UI, V, NewVal, VMC, TD);
|
2001-11-05 18:30:53 +00:00
|
|
|
NumUses = V->use_size();
|
|
|
|
if (NumUses == OldSize) ++It;
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
|
2003-04-24 18:25:27 +00:00
|
|
|
ValueMapCache &VMC, const TargetData &TD) {
|
2001-11-05 18:30:53 +00:00
|
|
|
if (isa<ValueHandle>(U)) return; // Valuehandles don't let go of operands...
|
|
|
|
|
2001-11-06 08:34:17 +00:00
|
|
|
if (VMC.OperandsMapped.count(U)) return;
|
|
|
|
VMC.OperandsMapped.insert(U);
|
|
|
|
|
|
|
|
ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(U);
|
|
|
|
if (VMCI != VMC.ExprMap.end())
|
|
|
|
return;
|
2001-11-05 18:30:53 +00:00
|
|
|
|
|
|
|
|
2003-05-20 18:45:36 +00:00
|
|
|
Instruction *I = cast<Instruction>(U); // Only Instructions convertible
|
2001-11-04 23:24:20 +00:00
|
|
|
|
|
|
|
BasicBlock *BB = I->getParent();
|
2002-07-16 17:33:13 +00:00
|
|
|
assert(BB != 0 && "Instruction not embedded in basic block!");
|
2002-07-17 17:11:33 +00:00
|
|
|
std::string Name = I->getName();
|
|
|
|
I->setName("");
|
2001-11-04 23:24:20 +00:00
|
|
|
Instruction *Res; // Result of conversion
|
|
|
|
|
2003-10-16 16:48:53 +00:00
|
|
|
//std::cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I
|
|
|
|
// << "BB Before: " << BB << endl;
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
// Prevent I from being removed...
|
2001-11-13 05:01:36 +00:00
|
|
|
ValueHandle IHandle(VMC, I);
|
2001-11-06 08:34:17 +00:00
|
|
|
|
|
|
|
const Type *NewTy = NewVal->getType();
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *Dummy = (NewTy != Type::VoidTy) ?
|
2002-04-27 02:25:14 +00:00
|
|
|
Constant::getNullValue(NewTy) : 0;
|
2001-11-05 18:30:53 +00:00
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case Instruction::Cast:
|
2002-07-17 17:11:33 +00:00
|
|
|
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);
|
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Add:
|
2001-12-05 06:34:00 +00:00
|
|
|
if (isa<PointerType>(NewTy)) {
|
|
|
|
Value *IndexVal = I->getOperand(OldVal == I->getOperand(0) ? 1 : 0);
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Indices;
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock::iterator It = I;
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2003-05-20 18:45:36 +00:00
|
|
|
if (const Type *ETy = ConvertibleToGEP(NewTy, IndexVal, Indices, TD,&It)){
|
2001-11-26 16:57:31 +00:00
|
|
|
// If successful, convert the add to a GEP
|
2002-01-20 22:54:45 +00:00
|
|
|
//const Type *RetTy = PointerType::get(ETy);
|
2001-11-08 20:19:56 +00:00
|
|
|
// First operand is actually the given pointer...
|
2001-12-05 06:34:00 +00:00
|
|
|
Res = new GetElementPtrInst(NewVal, Indices, Name);
|
2001-12-04 00:03:30 +00:00
|
|
|
assert(cast<PointerType>(Res->getType())->getElementType() == ETy &&
|
2003-05-20 18:45:36 +00:00
|
|
|
"ConvertibleToGEP broken!");
|
2001-11-08 20:19:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::SetEQ:
|
|
|
|
case Instruction::SetNE: {
|
2001-11-06 08:34:17 +00:00
|
|
|
Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
|
|
|
|
Dummy, Dummy, Name);
|
|
|
|
VMC.ExprMap[I] = Res; // Add node to expression eagerly
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
unsigned OtherIdx = (OldVal == I->getOperand(0)) ? 1 : 0;
|
|
|
|
Value *OtherOp = I->getOperand(OtherIdx);
|
2003-04-24 18:25:27 +00:00
|
|
|
Value *NewOther = ConvertExpressionToType(OtherOp, NewTy, VMC, TD);
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-06 08:34:17 +00:00
|
|
|
Res->setOperand(OtherIdx, NewOther);
|
|
|
|
Res->setOperand(!OtherIdx, NewVal);
|
2001-11-04 23:24:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::Shr:
|
|
|
|
assert(I->getOperand(0) == OldVal);
|
|
|
|
Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), NewVal,
|
|
|
|
I->getOperand(1), Name);
|
|
|
|
break;
|
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
case Instruction::Free: // Free can free any pointer type!
|
|
|
|
assert(I->getOperand(0) == OldVal);
|
|
|
|
Res = new FreeInst(NewVal);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2001-11-08 20:19:56 +00:00
|
|
|
case Instruction::Load: {
|
|
|
|
assert(I->getOperand(0) == OldVal && isa<PointerType>(NewVal->getType()));
|
2001-12-14 16:35:53 +00:00
|
|
|
const Type *LoadedTy =
|
|
|
|
cast<PointerType>(NewVal->getType())->getElementType();
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2002-08-22 16:41:31 +00:00
|
|
|
Value *Src = NewVal;
|
2001-11-26 16:57:31 +00:00
|
|
|
|
|
|
|
if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) {
|
2002-08-22 16:41:31 +00:00
|
|
|
std::vector<Value*> Indices;
|
2004-01-09 05:53:38 +00:00
|
|
|
// FIXME, PR82
|
2002-09-11 01:21:29 +00:00
|
|
|
Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
|
2002-08-22 16:41:31 +00:00
|
|
|
|
2001-11-08 20:19:56 +00:00
|
|
|
unsigned Offset = 0; // No offset, get first leaf.
|
2003-04-24 18:25:27 +00:00
|
|
|
LoadedTy = getStructOffsetType(CT, Offset, Indices, TD, false);
|
2002-08-22 16:41:31 +00:00
|
|
|
assert(LoadedTy->isFirstClassType());
|
2002-08-14 22:11:52 +00:00
|
|
|
|
2002-08-22 16:41:31 +00:00
|
|
|
if (Indices.size() != 1) { // Do not generate load X, 0
|
|
|
|
// Insert the GEP instruction before this load.
|
2002-09-10 17:04:02 +00:00
|
|
|
Src = new GetElementPtrInst(Src, Indices, Name+".idx", I);
|
2002-08-22 16:41:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Res = new LoadInst(Src, Name);
|
2001-12-14 16:35:53 +00:00
|
|
|
assert(Res->getType()->isFirstClassType() && "Load of structure or array!");
|
2001-11-04 23:24:20 +00:00
|
|
|
break;
|
2001-11-08 20:19:56 +00:00
|
|
|
}
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
case Instruction::Store: {
|
|
|
|
if (I->getOperand(0) == OldVal) { // Replace the source value
|
2002-07-31 22:31:34 +00:00
|
|
|
// 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()) {
|
2003-05-20 18:45:36 +00:00
|
|
|
// Comments describing this stuff are in the OperandConvertibleToType
|
2002-07-31 22:31:34 +00:00
|
|
|
// switch statement for Store...
|
|
|
|
//
|
|
|
|
const Type *ElTy =
|
|
|
|
cast<PointerType>(VMCI->second->getType())->getElementType();
|
2002-08-22 16:41:31 +00:00
|
|
|
|
|
|
|
Value *SrcPtr = VMCI->second;
|
|
|
|
|
|
|
|
if (ElTy != NewTy) {
|
2002-07-31 22:31:34 +00:00
|
|
|
// We check that this is a struct in the initial scan...
|
|
|
|
const StructType *SElTy = cast<StructType>(ElTy);
|
|
|
|
|
|
|
|
std::vector<Value*> Indices;
|
2004-01-09 05:53:38 +00:00
|
|
|
// FIXME, PR82
|
2002-09-11 01:21:29 +00:00
|
|
|
Indices.push_back(Constant::getNullValue(Type::LongTy));
|
2002-08-22 16:41:31 +00:00
|
|
|
|
|
|
|
unsigned Offset = 0;
|
2003-04-24 18:25:27 +00:00
|
|
|
const Type *Ty = getStructOffsetType(ElTy, Offset, Indices, TD,false);
|
2002-07-31 22:31:34 +00:00
|
|
|
assert(Offset == 0 && "Offset changed!");
|
|
|
|
assert(NewTy == Ty && "Did not convert to correct type!");
|
|
|
|
|
2002-09-10 17:04:02 +00:00
|
|
|
// Insert the GEP instruction before this store.
|
2002-08-22 16:41:31 +00:00
|
|
|
SrcPtr = new GetElementPtrInst(SrcPtr, Indices,
|
2002-09-10 17:04:02 +00:00
|
|
|
SrcPtr->getName()+".idx", I);
|
2002-07-31 22:31:34 +00:00
|
|
|
}
|
2002-08-22 16:41:31 +00:00
|
|
|
Res = new StoreInst(NewVal, SrcPtr);
|
2002-07-31 22:31:34 +00:00
|
|
|
|
|
|
|
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),
|
2003-04-24 18:25:27 +00:00
|
|
|
NewPT, VMC, TD));
|
2002-07-31 22:31:34 +00:00
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
} else { // Replace the source pointer
|
2001-12-04 00:03:30 +00:00
|
|
|
const Type *ValTy = cast<PointerType>(NewTy)->getElementType();
|
2002-08-22 16:41:31 +00:00
|
|
|
|
|
|
|
Value *SrcPtr = NewVal;
|
2002-03-07 21:17:35 +00:00
|
|
|
|
|
|
|
if (isa<StructType>(ValTy)) {
|
2002-08-22 16:41:31 +00:00
|
|
|
std::vector<Value*> Indices;
|
2004-01-09 05:53:38 +00:00
|
|
|
// FIXME: PR82
|
2002-09-11 01:21:29 +00:00
|
|
|
Indices.push_back(Constant::getNullValue(Type::LongTy));
|
2002-08-22 16:41:31 +00:00
|
|
|
|
|
|
|
unsigned Offset = 0;
|
2003-04-24 18:25:27 +00:00
|
|
|
ValTy = getStructOffsetType(ValTy, Offset, Indices, TD, false);
|
2002-08-22 16:41:31 +00:00
|
|
|
|
2002-03-07 21:17:35 +00:00
|
|
|
assert(Offset == 0 && ValTy);
|
2002-08-22 16:41:31 +00:00
|
|
|
|
2002-09-10 17:04:02 +00:00
|
|
|
// Insert the GEP instruction before this store.
|
2002-08-22 16:41:31 +00:00
|
|
|
SrcPtr = new GetElementPtrInst(SrcPtr, Indices,
|
2002-09-10 17:04:02 +00:00
|
|
|
SrcPtr->getName()+".idx", I);
|
2001-12-05 19:39:15 +00:00
|
|
|
}
|
2002-03-07 21:17:35 +00:00
|
|
|
|
2002-08-22 16:41:31 +00:00
|
|
|
Res = new StoreInst(Constant::getNullValue(ValTy), SrcPtr);
|
2001-11-06 08:34:17 +00:00
|
|
|
VMC.ExprMap[I] = Res;
|
2003-04-24 18:25:27 +00:00
|
|
|
Res->setOperand(0, ConvertExpressionToType(I->getOperand(0),
|
|
|
|
ValTy, VMC, TD));
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
|
|
|
|
case Instruction::GetElementPtr: {
|
2001-12-14 16:35:53 +00:00
|
|
|
// Convert a one index getelementptr into just about anything that is
|
|
|
|
// desired.
|
2001-11-26 16:57:31 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock::iterator It = I;
|
2001-12-14 16:35:53 +00:00
|
|
|
const Type *OldElTy = cast<PointerType>(I->getType())->getElementType();
|
|
|
|
unsigned DataSize = TD.getTypeSize(OldElTy);
|
|
|
|
Value *Index = I->getOperand(1);
|
|
|
|
|
|
|
|
if (DataSize != 1) {
|
|
|
|
// Insert a multiply of the old element type is not a unit size...
|
|
|
|
Index = BinaryOperator::create(Instruction::Mul, Index,
|
2004-01-09 05:53:38 +00:00
|
|
|
// FIXME: PR82
|
2002-09-11 01:21:29 +00:00
|
|
|
ConstantSInt::get(Type::LongTy, DataSize),
|
2002-09-10 17:04:02 +00:00
|
|
|
"scale", It);
|
2001-12-14 16:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the conversion now...
|
2001-12-07 04:40:30 +00:00
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Indices;
|
2003-05-20 18:45:36 +00:00
|
|
|
const Type *ElTy = ConvertibleToGEP(NewVal->getType(),Index,Indices,TD,&It);
|
2001-12-07 04:40:30 +00:00
|
|
|
assert(ElTy != 0 && "GEP Conversion Failure!");
|
|
|
|
Res = new GetElementPtrInst(NewVal, Indices, Name);
|
2001-12-14 16:35:53 +00:00
|
|
|
assert(Res->getType() == PointerType::get(ElTy) &&
|
2003-05-20 18:45:36 +00:00
|
|
|
"ConvertibleToGet failed!");
|
2001-11-26 16:57:31 +00:00
|
|
|
}
|
2001-12-14 16:35:53 +00:00
|
|
|
#if 0
|
|
|
|
if (I->getType() == PointerType::get(Type::SByteTy)) {
|
|
|
|
// Convert a getelementptr sbyte * %reg111, uint 16 freely back to
|
|
|
|
// anything that is a pointer type...
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock::iterator It = I;
|
2001-12-14 16:35:53 +00:00
|
|
|
|
|
|
|
// Check to see if the second argument is an expression that can
|
|
|
|
// be converted to the appropriate size... if so, allow it.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Indices;
|
2003-05-20 18:45:36 +00:00
|
|
|
const Type *ElTy = ConvertibleToGEP(NewVal->getType(), I->getOperand(1),
|
2003-04-24 18:25:27 +00:00
|
|
|
Indices, TD, &It);
|
2001-12-14 16:35:53 +00:00
|
|
|
assert(ElTy != 0 && "GEP Conversion Failure!");
|
|
|
|
|
|
|
|
Res = new GetElementPtrInst(NewVal, Indices, Name);
|
|
|
|
} else {
|
|
|
|
// Convert a getelementptr ulong * %reg123, uint %N
|
|
|
|
// to getelementptr long * %reg123, uint %N
|
|
|
|
// ... where the type must simply stay the same size...
|
|
|
|
//
|
2002-08-22 23:37:20 +00:00
|
|
|
GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
|
|
|
|
std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
|
|
|
|
Res = new GetElementPtrInst(NewVal, Indices, Name);
|
2001-12-14 16:35:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
2001-11-26 16:57:31 +00:00
|
|
|
|
2003-10-19 21:34:28 +00:00
|
|
|
case Instruction::PHI: {
|
2001-11-06 08:34:17 +00:00
|
|
|
PHINode *OldPN = cast<PHINode>(I);
|
|
|
|
PHINode *NewPN = new PHINode(NewTy, Name);
|
|
|
|
VMC.ExprMap[I] = NewPN;
|
|
|
|
|
|
|
|
while (OldPN->getNumOperands()) {
|
|
|
|
BasicBlock *BB = OldPN->getIncomingBlock(0);
|
|
|
|
Value *OldVal = OldPN->getIncomingValue(0);
|
2002-10-08 21:36:34 +00:00
|
|
|
OldPN->removeIncomingValue(BB, false);
|
2003-04-24 18:25:27 +00:00
|
|
|
Value *V = ConvertExpressionToType(OldVal, NewTy, VMC, TD);
|
2001-11-06 08:34:17 +00:00
|
|
|
NewPN->addIncoming(V, BB);
|
|
|
|
}
|
|
|
|
Res = NewPN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
case Instruction::Call: {
|
|
|
|
Value *Meth = I->getOperand(0);
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Value*> Params(I->op_begin()+1, I->op_end());
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
if (Meth == OldVal) { // Changing the function pointer?
|
2002-06-25 16:13:24 +00:00
|
|
|
const PointerType *NewPTy = cast<PointerType>(NewVal->getType());
|
|
|
|
const FunctionType *NewTy = cast<FunctionType>(NewPTy->getElementType());
|
2002-04-04 22:19:18 +00:00
|
|
|
const FunctionType::ParamTypes &PTs = NewTy->getParamTypes();
|
2002-02-14 19:44:09 +00:00
|
|
|
|
2003-02-14 03:07:17 +00:00
|
|
|
if (NewTy->getReturnType() == Type::VoidTy)
|
|
|
|
Name = ""; // Make sure not to name a void call!
|
|
|
|
|
2002-03-11 17:27:34 +00:00
|
|
|
// Get an iterator to the call instruction so that we can insert casts for
|
2003-10-10 17:57:28 +00:00
|
|
|
// operands if need be. Note that we do not require operands to be
|
2003-05-20 18:45:36 +00:00
|
|
|
// convertible, we can insert casts if they are convertible but not
|
2002-03-11 17:27:34 +00:00
|
|
|
// compatible. The reason for this is that we prefer to have resolved
|
2002-04-07 20:49:59 +00:00
|
|
|
// functions but casted arguments if possible.
|
2002-03-11 17:27:34 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock::iterator It = I;
|
2002-03-11 17:27:34 +00:00
|
|
|
|
2002-02-14 19:44:09 +00:00
|
|
|
// Convert over all of the call operands to their new types... but only
|
|
|
|
// convert over the part that is not in the vararg section of the call.
|
|
|
|
//
|
|
|
|
for (unsigned i = 0; i < PTs.size(); ++i)
|
2002-03-11 17:27:34 +00:00
|
|
|
if (Params[i]->getType() != PTs[i]) {
|
|
|
|
// Create a cast to convert it to the right type, we know that this
|
|
|
|
// is a lossless cast...
|
|
|
|
//
|
2002-11-01 04:49:06 +00:00
|
|
|
Params[i] = new CastInst(Params[i], PTs[i], "callarg.cast." +
|
|
|
|
Params[i]->getName(), It);
|
2002-03-11 17:27:34 +00:00
|
|
|
}
|
2002-02-14 19:44:09 +00:00
|
|
|
Meth = NewVal; // Update call destination to new value
|
|
|
|
|
|
|
|
} else { // Changing an argument, must be in vararg area
|
|
|
|
std::vector<Value*>::iterator OI =
|
|
|
|
find(Params.begin(), Params.end(), OldVal);
|
|
|
|
assert (OI != Params.end() && "Not using value!");
|
|
|
|
|
|
|
|
*OI = NewVal;
|
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
Res = new CallInst(Meth, Params, Name);
|
|
|
|
break;
|
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
default:
|
2003-05-20 18:45:36 +00:00
|
|
|
assert(0 && "Expression convertible, but don't know how to convert?");
|
2001-11-04 23:24:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-12-14 16:35:53 +00:00
|
|
|
// If the instruction was newly created, insert it into the instruction
|
|
|
|
// stream.
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock::iterator It = I;
|
2002-09-10 22:38:06 +00:00
|
|
|
assert(It != BB->end() && "Instruction not in own basic block??");
|
|
|
|
BB->getInstList().insert(It, Res); // Keep It pointing to old instruction
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2003-10-16 16:48:53 +00:00
|
|
|
DEBUG(std::cerr << "COT CREATED: " << (void*)Res << " " << Res
|
|
|
|
<< "In: " << (void*)I << " " << I << "Out: " << (void*)Res
|
|
|
|
<< " " << Res);
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-06 08:34:17 +00:00
|
|
|
// Add the instruction to the expression map
|
|
|
|
VMC.ExprMap[I] = Res;
|
|
|
|
|
2001-11-04 23:24:20 +00:00
|
|
|
if (I->getType() != Res->getType())
|
2003-04-24 18:25:27 +00:00
|
|
|
ConvertValueToNewType(I, Res, VMC, TD);
|
2001-11-05 18:30:53 +00:00
|
|
|
else {
|
2003-10-16 16:48:53 +00:00
|
|
|
bool FromStart = true;
|
|
|
|
Value::use_iterator UI;
|
|
|
|
while (1) {
|
|
|
|
if (FromStart) UI = I->use_begin();
|
|
|
|
if (UI == I->use_end()) break;
|
|
|
|
|
|
|
|
if (isa<ValueHandle>(*UI)) {
|
|
|
|
++UI;
|
|
|
|
FromStart = false;
|
|
|
|
} else {
|
|
|
|
User *U = *UI;
|
|
|
|
if (!FromStart) --UI;
|
|
|
|
U->replaceUsesOfWith(I, Res);
|
|
|
|
if (!FromStart) ++UI;
|
|
|
|
}
|
2001-11-05 18:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-13 05:01:36 +00:00
|
|
|
|
2001-11-26 16:57:31 +00:00
|
|
|
ValueHandle::ValueHandle(ValueMapCache &VMC, Value *V)
|
|
|
|
: Instruction(Type::VoidTy, UserOp1, ""), Cache(VMC) {
|
2003-10-16 16:48:53 +00:00
|
|
|
//DEBUG(std::cerr << "VH AQUIRING: " << (void*)V << " " << V);
|
2001-11-06 08:34:17 +00:00
|
|
|
Operands.push_back(Use(V, this));
|
|
|
|
}
|
|
|
|
|
2002-07-17 17:11:33 +00:00
|
|
|
ValueHandle::ValueHandle(const ValueHandle &VH)
|
|
|
|
: Instruction(Type::VoidTy, UserOp1, ""), Cache(VH.Cache) {
|
2003-10-16 16:48:53 +00:00
|
|
|
//DEBUG(std::cerr << "VH AQUIRING: " << (void*)V << " " << V);
|
2002-07-17 17:11:33 +00:00
|
|
|
Operands.push_back(Use((Value*)VH.getOperand(0), this));
|
|
|
|
}
|
|
|
|
|
2001-11-13 05:01:36 +00:00
|
|
|
static void RecursiveDelete(ValueMapCache &Cache, Instruction *I) {
|
2001-11-06 08:34:17 +00:00
|
|
|
if (!I || !I->use_empty()) return;
|
|
|
|
|
|
|
|
assert(I->getParent() && "Inst not in basic block!");
|
|
|
|
|
2003-10-16 16:48:53 +00:00
|
|
|
//DEBUG(std::cerr << "VH DELETING: " << (void*)I << " " << I);
|
2001-11-06 08:34:17 +00:00
|
|
|
|
|
|
|
for (User::op_iterator OI = I->op_begin(), OE = I->op_end();
|
2002-02-14 18:55:22 +00:00
|
|
|
OI != OE; ++OI)
|
2003-05-29 15:12:27 +00:00
|
|
|
if (Instruction *U = dyn_cast<Instruction>(OI)) {
|
2001-11-06 08:34:17 +00:00
|
|
|
*OI = 0;
|
2002-02-14 18:55:22 +00:00
|
|
|
RecursiveDelete(Cache, U);
|
2001-11-06 08:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
I->getParent()->getInstList().remove(I);
|
2001-11-13 05:01:36 +00:00
|
|
|
|
|
|
|
Cache.OperandsMapped.erase(I);
|
|
|
|
Cache.ExprMap.erase(I);
|
2001-11-06 08:34:17 +00:00
|
|
|
delete I;
|
|
|
|
}
|
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
ValueHandle::~ValueHandle() {
|
2003-10-15 16:48:29 +00:00
|
|
|
if (Operands[0]->hasOneUse()) {
|
2001-11-05 18:30:53 +00:00
|
|
|
Value *V = Operands[0];
|
2001-11-06 08:34:17 +00:00
|
|
|
Operands[0] = 0; // Drop use!
|
2001-11-04 23:24:20 +00:00
|
|
|
|
2001-11-05 18:30:53 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2001-11-13 05:01:36 +00:00
|
|
|
RecursiveDelete(Cache, dyn_cast<Instruction>(V));
|
2001-11-05 18:30:53 +00:00
|
|
|
} else {
|
2003-10-16 16:48:53 +00:00
|
|
|
//DEBUG(std::cerr << "VH RELEASING: " << (void*)Operands[0].get() << " "
|
|
|
|
// << Operands[0]->use_size() << " " << Operands[0]);
|
2001-11-05 18:30:53 +00:00
|
|
|
}
|
2001-11-04 23:24:20 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|