blob: 22ea6f3935fa3fb5948e331588d54a051544c240 [file] [log] [blame]
Chris Lattner871d2ce2002-09-09 20:25:21 +00001//===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type -------------===//
Chris Lattner901216d2001-11-04 23:24:20 +00002//
3// This file implements the part of level raising that checks to see if it is
4// possible to coerce an entire expression tree into a different type. If
5// convertable, other routines from this file will do the conversion.
6//
7//===----------------------------------------------------------------------===//
8
9#include "TransformInternals.h"
Chris Lattner901216d2001-11-04 23:24:20 +000010#include "llvm/iOther.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000011#include "llvm/iPHINode.h"
Chris Lattner901216d2001-11-04 23:24:20 +000012#include "llvm/iMemory.h"
Chris Lattner968ddc92002-04-08 20:18:09 +000013#include "llvm/ConstantHandling.h"
Chris Lattnera0fa5882001-11-26 16:57:31 +000014#include "llvm/Analysis/Expressions.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
Chris Lattnerb3abf9d2002-05-22 17:27:12 +000016#include "Support/StatisticReporter.h"
Chris Lattner901216d2001-11-04 23:24:20 +000017#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000018using std::cerr;
Chris Lattner901216d2001-11-04 23:24:20 +000019
Chris Lattnere4f4d8c2001-11-05 18:30:53 +000020static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
21 ValueTypeCache &ConvertedTypes);
22
23static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
24 ValueMapCache &VMC);
25
Chris Lattnera0fa5882001-11-26 16:57:31 +000026// Peephole Malloc instructions: we take a look at the use chain of the
27// malloc instruction, and try to find out if the following conditions hold:
28// 1. The malloc is of the form: 'malloc [sbyte], uint <constant>'
29// 2. The only users of the malloc are cast & add instructions
30// 3. Of the cast instructions, there is only one destination pointer type
31// [RTy] where the size of the pointed to object is equal to the number
32// of bytes allocated.
33//
34// If these conditions hold, we convert the malloc to allocate an [RTy]
35// element. TODO: This comment is out of date WRT arrays
36//
37static bool MallocConvertableToType(MallocInst *MI, const Type *Ty,
38 ValueTypeCache &CTMap) {
Chris Lattner8e640f12002-03-21 22:39:59 +000039 if (!isa<PointerType>(Ty)) return false; // Malloc always returns pointers
Chris Lattnera0fa5882001-11-26 16:57:31 +000040
41 // Deal with the type to allocate, not the pointer type...
Chris Lattner7a176752001-12-04 00:03:30 +000042 Ty = cast<PointerType>(Ty)->getElementType();
Chris Lattner97ac4ee2001-12-14 16:35:53 +000043 if (!Ty->isSized()) return false; // Can only alloc something with a size
Chris Lattnera0fa5882001-11-26 16:57:31 +000044
45 // Analyze the number of bytes allocated...
Chris Lattnerc74cb862002-08-30 22:53:53 +000046 ExprType Expr = ClassifyExpression(MI->getArraySize());
Chris Lattnera0fa5882001-11-26 16:57:31 +000047
Chris Lattner97ac4ee2001-12-14 16:35:53 +000048 // Get information about the base datatype being allocated, before & after
Chris Lattner48401462002-04-29 20:09:21 +000049 int ReqTypeSize = TD.getTypeSize(Ty);
Chris Lattner97ac4ee2001-12-14 16:35:53 +000050 unsigned OldTypeSize = TD.getTypeSize(MI->getType()->getElementType());
51
Chris Lattnera0fa5882001-11-26 16:57:31 +000052 // Must have a scale or offset to analyze it...
Chris Lattner5f4a5282002-03-21 23:02:37 +000053 if (!Expr.Offset && !Expr.Scale && OldTypeSize == 1) return false;
Chris Lattnera0fa5882001-11-26 16:57:31 +000054
Chris Lattner97ac4ee2001-12-14 16:35:53 +000055 // Get the offset and scale of the allocation...
56 int OffsetVal = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
57 int ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var ? 1 : 0);
Chris Lattnera0fa5882001-11-26 16:57:31 +000058
Chris Lattner97ac4ee2001-12-14 16:35:53 +000059 // The old type might not be of unit size, take old size into consideration
60 // here...
Chris Lattner48401462002-04-29 20:09:21 +000061 int Offset = OffsetVal * OldTypeSize;
62 int Scale = ScaleVal * OldTypeSize;
Chris Lattner97ac4ee2001-12-14 16:35:53 +000063
64 // In order to be successful, both the scale and the offset must be a multiple
65 // of the requested data type's size.
Chris Lattnera0fa5882001-11-26 16:57:31 +000066 //
Chris Lattner97ac4ee2001-12-14 16:35:53 +000067 if (Offset/ReqTypeSize*ReqTypeSize != Offset ||
68 Scale/ReqTypeSize*ReqTypeSize != Scale)
Chris Lattnera0fa5882001-11-26 16:57:31 +000069 return false; // Nope.
70
Chris Lattnera0fa5882001-11-26 16:57:31 +000071 return true;
Chris Lattnera0fa5882001-11-26 16:57:31 +000072}
73
74static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty,
Chris Lattner697954c2002-01-20 22:54:45 +000075 const std::string &Name,
76 ValueMapCache &VMC){
Chris Lattnera0fa5882001-11-26 16:57:31 +000077 BasicBlock *BB = MI->getParent();
78 BasicBlock::iterator It = BB->end();
79
80 // Analyze the number of bytes allocated...
Chris Lattnerc74cb862002-08-30 22:53:53 +000081 ExprType Expr = ClassifyExpression(MI->getArraySize());
Chris Lattnera0fa5882001-11-26 16:57:31 +000082
83 const PointerType *AllocTy = cast<PointerType>(Ty);
Chris Lattner7a176752001-12-04 00:03:30 +000084 const Type *ElType = AllocTy->getElementType();
Chris Lattnera0fa5882001-11-26 16:57:31 +000085
Chris Lattner97ac4ee2001-12-14 16:35:53 +000086 unsigned DataSize = TD.getTypeSize(ElType);
87 unsigned OldTypeSize = TD.getTypeSize(MI->getType()->getElementType());
88
89 // Get the offset and scale coefficients that we are allocating...
90 int OffsetVal = (Expr.Offset ? getConstantValue(Expr.Offset) : 0);
91 int ScaleVal = Expr.Scale ? getConstantValue(Expr.Scale) : (Expr.Var ? 1 : 0);
92
93 // The old type might not be of unit size, take old size into consideration
94 // here...
95 unsigned Offset = (unsigned)OffsetVal * OldTypeSize / DataSize;
96 unsigned Scale = (unsigned)ScaleVal * OldTypeSize / DataSize;
97
98 // Locate the malloc instruction, because we may be inserting instructions
Chris Lattner7e708292002-06-25 16:13:24 +000099 It = MI;
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000100
101 // If we have a scale, apply it first...
102 if (Expr.Var) {
103 // Expr.Var is not neccesarily unsigned right now, insert a cast now.
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000104 if (Expr.Var->getType() != Type::UIntTy)
105 Expr.Var = new CastInst(Expr.Var, Type::UIntTy,
106 Expr.Var->getName()+"-uint", It);
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000107
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000108 if (Scale != 1)
109 Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
110 ConstantUInt::get(Type::UIntTy, Scale),
111 Expr.Var->getName()+"-scl", It);
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000112
113 } else {
114 // If we are not scaling anything, just make the offset be the "var"...
115 Expr.Var = ConstantUInt::get(Type::UIntTy, Offset);
116 Offset = 0; Scale = 1;
Chris Lattnera0fa5882001-11-26 16:57:31 +0000117 }
118
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000119 // If we have an offset now, add it in...
120 if (Offset != 0) {
121 assert(Expr.Var && "Var must be nonnull by now!");
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000122 Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
123 ConstantUInt::get(Type::UIntTy, Offset),
124 Expr.Var->getName()+"-off", It);
Chris Lattnera0fa5882001-11-26 16:57:31 +0000125 }
Chris Lattnera0fa5882001-11-26 16:57:31 +0000126
127 Instruction *NewI = new MallocInst(AllocTy, Expr.Var, Name);
128
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000129 assert(AllocTy == Ty);
Chris Lattnera0fa5882001-11-26 16:57:31 +0000130 return NewI;
131}
132
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000133
Chris Lattner901216d2001-11-04 23:24:20 +0000134// ExpressionConvertableToType - Return true if it is possible
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000135bool ExpressionConvertableToType(Value *V, const Type *Ty,
136 ValueTypeCache &CTMap) {
Chris Lattner71344bd2001-11-06 08:34:17 +0000137 // Expression type must be holdable in a register.
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000138 if (!Ty->isFirstClassType())
Chris Lattner71344bd2001-11-06 08:34:17 +0000139 return false;
140
Chris Lattner901216d2001-11-04 23:24:20 +0000141 ValueTypeCache::iterator CTMI = CTMap.find(V);
142 if (CTMI != CTMap.end()) return CTMI->second == Ty;
Chris Lattnerce22ec12001-11-13 05:01:36 +0000143
Chris Lattner901216d2001-11-04 23:24:20 +0000144 CTMap[V] = Ty;
Chris Lattner280b09b2002-07-31 22:31:34 +0000145 if (V->getType() == Ty) return true; // Expression already correct type!
Chris Lattner901216d2001-11-04 23:24:20 +0000146
147 Instruction *I = dyn_cast<Instruction>(V);
148 if (I == 0) {
149 // It's not an instruction, check to see if it's a constant... all constants
150 // can be converted to an equivalent value (except pointers, they can't be
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000151 // const prop'd in general). We just ask the constant propogator to see if
152 // it can convert the value...
Chris Lattner901216d2001-11-04 23:24:20 +0000153 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000154 if (Constant *CPV = dyn_cast<Constant>(V))
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000155 if (ConstantFoldCastInstruction(CPV, Ty))
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000156 return true; // Don't worry about deallocating, it's a constant.
Chris Lattner901216d2001-11-04 23:24:20 +0000157
158 return false; // Otherwise, we can't convert!
159 }
Chris Lattnercf1ae7c2001-11-09 01:08:10 +0000160
Chris Lattner901216d2001-11-04 23:24:20 +0000161 switch (I->getOpcode()) {
162 case Instruction::Cast:
163 // We can convert the expr if the cast destination type is losslessly
164 // convertable to the requested type.
Chris Lattnera0fa5882001-11-26 16:57:31 +0000165 if (!Ty->isLosslesslyConvertableTo(I->getType())) return false;
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000166
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000167 // We also do not allow conversion of a cast that casts from a ptr to array
168 // of X to a *X. For example: cast [4 x %List *] * %val to %List * *
169 //
Chris Lattner7e708292002-06-25 16:13:24 +0000170 if (const PointerType *SPT =
171 dyn_cast<PointerType>(I->getOperand(0)->getType()))
172 if (const PointerType *DPT = dyn_cast<PointerType>(I->getType()))
173 if (const ArrayType *AT = dyn_cast<ArrayType>(SPT->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000174 if (AT->getElementType() == DPT->getElementType())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000175 return false;
Chris Lattnera0fa5882001-11-26 16:57:31 +0000176 break;
Chris Lattner901216d2001-11-04 23:24:20 +0000177
178 case Instruction::Add:
179 case Instruction::Sub:
Chris Lattnere29f6a82002-09-10 19:42:53 +0000180 if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false;
Chris Lattnerce22ec12001-11-13 05:01:36 +0000181 if (!ExpressionConvertableToType(I->getOperand(0), Ty, CTMap) ||
182 !ExpressionConvertableToType(I->getOperand(1), Ty, CTMap))
183 return false;
184 break;
Chris Lattner901216d2001-11-04 23:24:20 +0000185 case Instruction::Shr:
Chris Lattner871d2ce2002-09-09 20:25:21 +0000186 if (!Ty->isInteger()) return false;
Chris Lattner901216d2001-11-04 23:24:20 +0000187 if (Ty->isSigned() != V->getType()->isSigned()) return false;
188 // FALL THROUGH
189 case Instruction::Shl:
Chris Lattner871d2ce2002-09-09 20:25:21 +0000190 if (!Ty->isInteger()) return false;
Chris Lattnerce22ec12001-11-13 05:01:36 +0000191 if (!ExpressionConvertableToType(I->getOperand(0), Ty, CTMap))
192 return false;
193 break;
Chris Lattner901216d2001-11-04 23:24:20 +0000194
195 case Instruction::Load: {
196 LoadInst *LI = cast<LoadInst>(I);
Chris Lattner65ea1712001-11-14 11:27:58 +0000197 if (!ExpressionConvertableToType(LI->getPointerOperand(),
198 PointerType::get(Ty), CTMap))
Chris Lattnerce22ec12001-11-13 05:01:36 +0000199 return false;
200 break;
Chris Lattner901216d2001-11-04 23:24:20 +0000201 }
Chris Lattner71344bd2001-11-06 08:34:17 +0000202 case Instruction::PHINode: {
203 PHINode *PN = cast<PHINode>(I);
204 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
205 if (!ExpressionConvertableToType(PN->getIncomingValue(i), Ty, CTMap))
206 return false;
Chris Lattnerce22ec12001-11-13 05:01:36 +0000207 break;
Chris Lattner71344bd2001-11-06 08:34:17 +0000208 }
209
Chris Lattnera0fa5882001-11-26 16:57:31 +0000210 case Instruction::Malloc:
211 if (!MallocConvertableToType(cast<MallocInst>(I), Ty, CTMap))
212 return false;
213 break;
214
Chris Lattner901216d2001-11-04 23:24:20 +0000215 case Instruction::GetElementPtr: {
216 // GetElementPtr's are directly convertable to a pointer type if they have
217 // a number of zeros at the end. Because removing these values does not
218 // change the logical offset of the GEP, it is okay and fair to remove them.
219 // This can change this:
220 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
221 // %t2 = cast %List * * %t1 to %List *
222 // into
223 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
224 //
225 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
226 const PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattner036efec2001-12-07 04:40:30 +0000227 if (!PTy) return false; // GEP must always return a pointer...
228 const Type *PVTy = PTy->getElementType();
Chris Lattner901216d2001-11-04 23:24:20 +0000229
230 // Check to see if there are zero elements that we can remove from the
231 // index array. If there are, check to see if removing them causes us to
232 // get to the right type...
233 //
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000234 std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
Chris Lattner65ea1712001-11-14 11:27:58 +0000235 const Type *BaseType = GEP->getPointerOperand()->getType();
Chris Lattnera0fa5882001-11-26 16:57:31 +0000236 const Type *ElTy = 0;
Chris Lattner901216d2001-11-04 23:24:20 +0000237
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000238 while (!Indices.empty() && isa<ConstantUInt>(Indices.back()) &&
239 cast<ConstantUInt>(Indices.back())->getValue() == 0) {
Chris Lattner901216d2001-11-04 23:24:20 +0000240 Indices.pop_back();
Chris Lattner036efec2001-12-07 04:40:30 +0000241 ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices, true);
242 if (ElTy == PVTy)
Chris Lattnerce22ec12001-11-13 05:01:36 +0000243 break; // Found a match!!
Chris Lattnera0fa5882001-11-26 16:57:31 +0000244 ElTy = 0;
Chris Lattner901216d2001-11-04 23:24:20 +0000245 }
Chris Lattnera0fa5882001-11-26 16:57:31 +0000246
Chris Lattner036efec2001-12-07 04:40:30 +0000247 if (ElTy) break; // Found a number of zeros we can strip off!
248
249 // Otherwise, we can convert a GEP from one form to the other iff the
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000250 // current gep is of the form 'getelementptr sbyte*, unsigned N
Chris Lattner036efec2001-12-07 04:40:30 +0000251 // and we could convert this to an appropriate GEP for the new type.
252 //
253 if (GEP->getNumOperands() == 2 &&
254 GEP->getOperand(1)->getType() == Type::UIntTy &&
255 GEP->getType() == PointerType::get(Type::SByteTy)) {
Chris Lattner036efec2001-12-07 04:40:30 +0000256
257 // Do not Check to see if our incoming pointer can be converted
258 // to be a ptr to an array of the right type... because in more cases than
259 // not, it is simply not analyzable because of pointer/array
260 // discrepencies. To fix this, we will insert a cast before the GEP.
261 //
262
263 // Check to see if 'N' is an expression that can be converted to
264 // the appropriate size... if so, allow it.
265 //
Chris Lattner697954c2002-01-20 22:54:45 +0000266 std::vector<Value*> Indices;
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000267 const Type *ElTy = ConvertableToGEP(PTy, I->getOperand(1), Indices);
Chris Lattner868c2d32002-02-14 22:21:40 +0000268 if (ElTy == PVTy) {
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000269 if (!ExpressionConvertableToType(I->getOperand(0),
270 PointerType::get(ElTy), CTMap))
271 return false; // Can't continue, ExConToTy might have polluted set!
Chris Lattner036efec2001-12-07 04:40:30 +0000272 break;
273 }
274 }
275
276 // Otherwise, it could be that we have something like this:
277 // getelementptr [[sbyte] *] * %reg115, uint %reg138 ; [sbyte]**
278 // and want to convert it into something like this:
279 // getelemenptr [[int] *] * %reg115, uint %reg138 ; [int]**
280 //
281 if (GEP->getNumOperands() == 2 &&
282 GEP->getOperand(1)->getType() == Type::UIntTy &&
283 TD.getTypeSize(PTy->getElementType()) ==
284 TD.getTypeSize(GEP->getType()->getElementType())) {
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000285 const PointerType *NewSrcTy = PointerType::get(PVTy);
286 if (!ExpressionConvertableToType(I->getOperand(0), NewSrcTy, CTMap))
287 return false;
288 break;
Chris Lattner036efec2001-12-07 04:40:30 +0000289 }
290
Chris Lattnerce22ec12001-11-13 05:01:36 +0000291 return false; // No match, maybe next time.
Chris Lattner901216d2001-11-04 23:24:20 +0000292 }
Chris Lattnerce22ec12001-11-13 05:01:36 +0000293
294 default:
295 return false;
Chris Lattner901216d2001-11-04 23:24:20 +0000296 }
Chris Lattnerce22ec12001-11-13 05:01:36 +0000297
298 // Expressions are only convertable if all of the users of the expression can
299 // have this value converted. This makes use of the map to avoid infinite
300 // recursion.
301 //
Chris Lattnera0fa5882001-11-26 16:57:31 +0000302 for (Value::use_iterator It = I->use_begin(), E = I->use_end(); It != E; ++It)
303 if (!OperandConvertableToType(*It, I, Ty, CTMap))
304 return false;
Chris Lattnerce22ec12001-11-13 05:01:36 +0000305
306 return true;
Chris Lattner901216d2001-11-04 23:24:20 +0000307}
308
309
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000310Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
Chris Lattnerac0077e2001-12-05 19:39:15 +0000311 if (V->getType() == Ty) return V; // Already where we need to be?
312
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000313 ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(V);
Chris Lattnerce22ec12001-11-13 05:01:36 +0000314 if (VMCI != VMC.ExprMap.end()) {
Chris Lattner280b09b2002-07-31 22:31:34 +0000315 const Value *GV = VMCI->second;
316 const Type *GTy = VMCI->second->getType();
Chris Lattnerce22ec12001-11-13 05:01:36 +0000317 assert(VMCI->second->getType() == Ty);
Chris Lattner9750b612002-02-14 18:55:22 +0000318
319 if (Instruction *I = dyn_cast<Instruction>(V))
320 ValueHandle IHandle(VMC, I); // Remove I if it is unused now!
321
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000322 return VMCI->second;
Chris Lattnerce22ec12001-11-13 05:01:36 +0000323 }
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000324
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000325 DEBUG(cerr << "CETT: " << (void*)V << " " << V);
Chris Lattner71344bd2001-11-06 08:34:17 +0000326
Chris Lattner901216d2001-11-04 23:24:20 +0000327 Instruction *I = dyn_cast<Instruction>(V);
328 if (I == 0)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 if (Constant *CPV = cast<Constant>(V)) {
Chris Lattner901216d2001-11-04 23:24:20 +0000330 // Constants are converted by constant folding the cast that is required.
331 // We assume here that all casts are implemented for constant prop.
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000332 Value *Result = ConstantFoldCastInstruction(CPV, Ty);
Chris Lattner901216d2001-11-04 23:24:20 +0000333 assert(Result && "ConstantFoldCastInstruction Failed!!!");
Chris Lattnerce22ec12001-11-13 05:01:36 +0000334 assert(Result->getType() == Ty && "Const prop of cast failed!");
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000335
336 // Add the instruction to the expression map
337 VMC.ExprMap[V] = Result;
Chris Lattner901216d2001-11-04 23:24:20 +0000338 return Result;
339 }
340
341
342 BasicBlock *BB = I->getParent();
343 BasicBlock::InstListType &BIL = BB->getInstList();
Chris Lattner697954c2002-01-20 22:54:45 +0000344 std::string Name = I->getName(); if (!Name.empty()) I->setName("");
Chris Lattner901216d2001-11-04 23:24:20 +0000345 Instruction *Res; // Result of conversion
346
Chris Lattnerce22ec12001-11-13 05:01:36 +0000347 ValueHandle IHandle(VMC, I); // Prevent I from being removed!
Chris Lattner71344bd2001-11-06 08:34:17 +0000348
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000349 Constant *Dummy = Constant::getNullValue(Ty);
Chris Lattner71344bd2001-11-06 08:34:17 +0000350
Chris Lattner901216d2001-11-04 23:24:20 +0000351 switch (I->getOpcode()) {
352 case Instruction::Cast:
Chris Lattnerb1b42622002-07-17 17:11:33 +0000353 assert(VMC.NewCasts.count(ValueHandle(VMC, I)) == 0);
Chris Lattner901216d2001-11-04 23:24:20 +0000354 Res = new CastInst(I->getOperand(0), Ty, Name);
Chris Lattnerb1b42622002-07-17 17:11:33 +0000355 VMC.NewCasts.insert(ValueHandle(VMC, Res));
Chris Lattner901216d2001-11-04 23:24:20 +0000356 break;
357
358 case Instruction::Add:
359 case Instruction::Sub:
360 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
Chris Lattner71344bd2001-11-06 08:34:17 +0000361 Dummy, Dummy, Name);
362 VMC.ExprMap[I] = Res; // Add node to expression eagerly
363
364 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC));
365 Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), Ty, VMC));
Chris Lattner901216d2001-11-04 23:24:20 +0000366 break;
367
368 case Instruction::Shl:
369 case Instruction::Shr:
Chris Lattner75747cd2001-11-06 21:51:48 +0000370 Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), Dummy,
Chris Lattner901216d2001-11-04 23:24:20 +0000371 I->getOperand(1), Name);
Chris Lattner75747cd2001-11-06 21:51:48 +0000372 VMC.ExprMap[I] = Res;
373 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC));
Chris Lattner901216d2001-11-04 23:24:20 +0000374 break;
375
376 case Instruction::Load: {
377 LoadInst *LI = cast<LoadInst>(I);
Chris Lattnera0fa5882001-11-26 16:57:31 +0000378
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000379 Res = new LoadInst(Constant::getNullValue(PointerType::get(Ty)), Name);
Chris Lattner71344bd2001-11-06 08:34:17 +0000380 VMC.ExprMap[I] = Res;
Chris Lattner65ea1712001-11-14 11:27:58 +0000381 Res->setOperand(0, ConvertExpressionToType(LI->getPointerOperand(),
Chris Lattner71344bd2001-11-06 08:34:17 +0000382 PointerType::get(Ty), VMC));
Chris Lattnerce22ec12001-11-13 05:01:36 +0000383 assert(Res->getOperand(0)->getType() == PointerType::get(Ty));
384 assert(Ty == Res->getType());
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000385 assert(Res->getType()->isFirstClassType() && "Load of structure or array!");
Chris Lattner71344bd2001-11-06 08:34:17 +0000386 break;
387 }
388
389 case Instruction::PHINode: {
390 PHINode *OldPN = cast<PHINode>(I);
391 PHINode *NewPN = new PHINode(Ty, Name);
392
393 VMC.ExprMap[I] = NewPN; // Add node to expression eagerly
394 while (OldPN->getNumOperands()) {
395 BasicBlock *BB = OldPN->getIncomingBlock(0);
396 Value *OldVal = OldPN->getIncomingValue(0);
Chris Lattnerce22ec12001-11-13 05:01:36 +0000397 ValueHandle OldValHandle(VMC, OldVal);
Chris Lattner71344bd2001-11-06 08:34:17 +0000398 OldPN->removeIncomingValue(BB);
399 Value *V = ConvertExpressionToType(OldVal, Ty, VMC);
400 NewPN->addIncoming(V, BB);
401 }
402 Res = NewPN;
Chris Lattner901216d2001-11-04 23:24:20 +0000403 break;
404 }
405
Chris Lattnera0fa5882001-11-26 16:57:31 +0000406 case Instruction::Malloc: {
407 Res = ConvertMallocToType(cast<MallocInst>(I), Ty, Name, VMC);
408 break;
409 }
410
Chris Lattner901216d2001-11-04 23:24:20 +0000411 case Instruction::GetElementPtr: {
412 // GetElementPtr's are directly convertable to a pointer type if they have
413 // a number of zeros at the end. Because removing these values does not
414 // change the logical offset of the GEP, it is okay and fair to remove them.
415 // This can change this:
416 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
417 // %t2 = cast %List * * %t1 to %List *
418 // into
419 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
420 //
421 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
422
423 // Check to see if there are zero elements that we can remove from the
424 // index array. If there are, check to see if removing them causes us to
425 // get to the right type...
426 //
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000427 std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
Chris Lattner65ea1712001-11-14 11:27:58 +0000428 const Type *BaseType = GEP->getPointerOperand()->getType();
Chris Lattner7a176752001-12-04 00:03:30 +0000429 const Type *PVTy = cast<PointerType>(Ty)->getElementType();
Chris Lattner901216d2001-11-04 23:24:20 +0000430 Res = 0;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000431 while (!Indices.empty() && isa<ConstantUInt>(Indices.back()) &&
432 cast<ConstantUInt>(Indices.back())->getValue() == 0) {
Chris Lattner901216d2001-11-04 23:24:20 +0000433 Indices.pop_back();
434 if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
435 if (Indices.size() == 0) {
Chris Lattner65ea1712001-11-14 11:27:58 +0000436 Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP
Chris Lattner901216d2001-11-04 23:24:20 +0000437 } else {
Chris Lattner65ea1712001-11-14 11:27:58 +0000438 Res = new GetElementPtrInst(GEP->getPointerOperand(), Indices, Name);
Chris Lattner901216d2001-11-04 23:24:20 +0000439 }
440 break;
441 }
442 }
Chris Lattner036efec2001-12-07 04:40:30 +0000443
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000444 if (Res == 0 && GEP->getNumOperands() == 2 &&
445 GEP->getOperand(1)->getType() == Type::UIntTy &&
446 GEP->getType() == PointerType::get(Type::SByteTy)) {
447
Chris Lattner036efec2001-12-07 04:40:30 +0000448 // Otherwise, we can convert a GEP from one form to the other iff the
449 // current gep is of the form 'getelementptr [sbyte]*, unsigned N
450 // and we could convert this to an appropriate GEP for the new type.
451 //
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000452 const PointerType *NewSrcTy = PointerType::get(PVTy);
Chris Lattner7e708292002-06-25 16:13:24 +0000453 BasicBlock::iterator It = I;
Chris Lattner036efec2001-12-07 04:40:30 +0000454
455 // Check to see if 'N' is an expression that can be converted to
456 // the appropriate size... if so, allow it.
457 //
Chris Lattner697954c2002-01-20 22:54:45 +0000458 std::vector<Value*> Indices;
Chris Lattner036efec2001-12-07 04:40:30 +0000459 const Type *ElTy = ConvertableToGEP(NewSrcTy, I->getOperand(1),
460 Indices, &It);
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000461 if (ElTy) {
Chris Lattner036efec2001-12-07 04:40:30 +0000462 assert(ElTy == PVTy && "Internal error, setup wrong!");
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000463 Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy),
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000464 Indices, Name);
465 VMC.ExprMap[I] = Res;
466 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0),
467 NewSrcTy, VMC));
Chris Lattner036efec2001-12-07 04:40:30 +0000468 }
469 }
470
471 // Otherwise, it could be that we have something like this:
472 // getelementptr [[sbyte] *] * %reg115, uint %reg138 ; [sbyte]**
473 // and want to convert it into something like this:
474 // getelemenptr [[int] *] * %reg115, uint %reg138 ; [int]**
475 //
476 if (Res == 0) {
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000477 const PointerType *NewSrcTy = PointerType::get(PVTy);
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000478 std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000479 Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy),
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000480 Indices, Name);
Chris Lattner036efec2001-12-07 04:40:30 +0000481 VMC.ExprMap[I] = Res;
482 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0),
483 NewSrcTy, VMC));
484 }
485
486
Chris Lattner901216d2001-11-04 23:24:20 +0000487 assert(Res && "Didn't find match!");
488 break; // No match, maybe next time.
489 }
490
491 default:
492 assert(0 && "Expression convertable, but don't know how to convert?");
493 return 0;
494 }
495
Chris Lattnerce22ec12001-11-13 05:01:36 +0000496 assert(Res->getType() == Ty && "Didn't convert expr to correct type!");
497
Chris Lattner7e708292002-06-25 16:13:24 +0000498 BIL.insert(I, Res);
Chris Lattner901216d2001-11-04 23:24:20 +0000499
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000500 // Add the instruction to the expression map
501 VMC.ExprMap[I] = Res;
502
503 // Expressions are only convertable if all of the users of the expression can
504 // have this value converted. This makes use of the map to avoid infinite
505 // recursion.
506 //
507 unsigned NumUses = I->use_size();
508 for (unsigned It = 0; It < NumUses; ) {
509 unsigned OldSize = NumUses;
510 ConvertOperandToType(*(I->use_begin()+It), I, Res, VMC);
511 NumUses = I->use_size();
512 if (NumUses == OldSize) ++It;
513 }
514
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000515 DEBUG(cerr << "ExpIn: " << (void*)I << " " << I
516 << "ExpOut: " << (void*)Res << " " << Res);
Chris Lattner901216d2001-11-04 23:24:20 +0000517
518 return Res;
519}
520
521
522
Chris Lattnera0fa5882001-11-26 16:57:31 +0000523// ValueConvertableToType - Return true if it is possible
524bool ValueConvertableToType(Value *V, const Type *Ty,
Chris Lattner901216d2001-11-04 23:24:20 +0000525 ValueTypeCache &ConvertedTypes) {
526 ValueTypeCache::iterator I = ConvertedTypes.find(V);
527 if (I != ConvertedTypes.end()) return I->second == Ty;
528 ConvertedTypes[V] = Ty;
529
530 // It is safe to convert the specified value to the specified type IFF all of
531 // the uses of the value can be converted to accept the new typed value.
532 //
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000533 if (V->getType() != Ty) {
534 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
535 if (!OperandConvertableToType(*I, V, Ty, ConvertedTypes))
536 return false;
537 }
Chris Lattner901216d2001-11-04 23:24:20 +0000538
539 return true;
540}
541
542
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000543
544
545
Chris Lattner901216d2001-11-04 23:24:20 +0000546// OperandConvertableToType - Return true if it is possible to convert operand
547// V of User (instruction) U to the specified type. This is true iff it is
548// possible to change the specified instruction to accept this. CTMap is a map
549// of converted types, so that circular definitions will see the future type of
550// the expression, not the static current type.
551//
552static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
553 ValueTypeCache &CTMap) {
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000554 // if (V->getType() == Ty) return true; // Operand already the right type?
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000555
556 // Expression type must be holdable in a register.
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000557 if (!Ty->isFirstClassType())
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000558 return false;
559
Chris Lattner901216d2001-11-04 23:24:20 +0000560 Instruction *I = dyn_cast<Instruction>(U);
561 if (I == 0) return false; // We can't convert!
562
563 switch (I->getOpcode()) {
564 case Instruction::Cast:
565 assert(I->getOperand(0) == V);
566 // We can convert the expr if the cast destination type is losslessly
567 // convertable to the requested type.
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000568 // Also, do not change a cast that is a noop cast. For all intents and
569 // purposes it should be eliminated.
570 if (!Ty->isLosslesslyConvertableTo(I->getOperand(0)->getType()) ||
571 I->getType() == I->getOperand(0)->getType())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000572 return false;
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000573
Chris Lattnerbbad8582002-05-02 17:37:34 +0000574 // Do not allow a 'cast ushort %V to uint' to have it's first operand be
575 // converted to a 'short' type. Doing so changes the way sign promotion
576 // happens, and breaks things. Only allow the cast to take place if the
577 // signedness doesn't change... or if the current cast is not a lossy
578 // conversion.
579 //
580 if (!I->getType()->isLosslesslyConvertableTo(I->getOperand(0)->getType()) &&
581 I->getOperand(0)->getType()->isSigned() != Ty->isSigned())
582 return false;
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000583
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000584 // We also do not allow conversion of a cast that casts from a ptr to array
585 // of X to a *X. For example: cast [4 x %List *] * %val to %List * *
586 //
Chris Lattner7e708292002-06-25 16:13:24 +0000587 if (const PointerType *SPT =
588 dyn_cast<PointerType>(I->getOperand(0)->getType()))
589 if (const PointerType *DPT = dyn_cast<PointerType>(I->getType()))
590 if (const ArrayType *AT = dyn_cast<ArrayType>(SPT->getElementType()))
Chris Lattner7a176752001-12-04 00:03:30 +0000591 if (AT->getElementType() == DPT->getElementType())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000592 return false;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000593 return true;
Chris Lattner901216d2001-11-04 23:24:20 +0000594
595 case Instruction::Add:
Chris Lattnera8b6d432001-12-05 06:34:00 +0000596 if (isa<PointerType>(Ty)) {
597 Value *IndexVal = I->getOperand(V == I->getOperand(0) ? 1 : 0);
Chris Lattner697954c2002-01-20 22:54:45 +0000598 std::vector<Value*> Indices;
Chris Lattnera0fa5882001-11-26 16:57:31 +0000599 if (const Type *ETy = ConvertableToGEP(Ty, IndexVal, Indices)) {
600 const Type *RetTy = PointerType::get(ETy);
601
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000602 // Only successful if we can convert this type to the required type
Chris Lattnera0fa5882001-11-26 16:57:31 +0000603 if (ValueConvertableToType(I, RetTy, CTMap)) {
604 CTMap[I] = RetTy;
605 return true;
606 }
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000607 // We have to return failure here because ValueConvertableToType could
608 // have polluted our map
609 return false;
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000610 }
611 }
612 // FALLTHROUGH
Chris Lattner901216d2001-11-04 23:24:20 +0000613 case Instruction::Sub: {
Chris Lattnere29f6a82002-09-10 19:42:53 +0000614 if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false;
615
Chris Lattner901216d2001-11-04 23:24:20 +0000616 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
Chris Lattnera0fa5882001-11-26 16:57:31 +0000617 return ValueConvertableToType(I, Ty, CTMap) &&
Chris Lattner901216d2001-11-04 23:24:20 +0000618 ExpressionConvertableToType(OtherOp, Ty, CTMap);
619 }
620 case Instruction::SetEQ:
621 case Instruction::SetNE: {
622 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
623 return ExpressionConvertableToType(OtherOp, Ty, CTMap);
624 }
625 case Instruction::Shr:
626 if (Ty->isSigned() != V->getType()->isSigned()) return false;
627 // FALL THROUGH
628 case Instruction::Shl:
629 assert(I->getOperand(0) == V);
Chris Lattner871d2ce2002-09-09 20:25:21 +0000630 if (!Ty->isInteger()) return false;
Chris Lattnera0fa5882001-11-26 16:57:31 +0000631 return ValueConvertableToType(I, Ty, CTMap);
Chris Lattner901216d2001-11-04 23:24:20 +0000632
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000633 case Instruction::Free:
634 assert(I->getOperand(0) == V);
635 return isa<PointerType>(Ty); // Free can free any pointer type!
636
Chris Lattner901216d2001-11-04 23:24:20 +0000637 case Instruction::Load:
Chris Lattnera0fa5882001-11-26 16:57:31 +0000638 // Cannot convert the types of any subscripts...
639 if (I->getOperand(0) != V) return false;
640
Chris Lattner901216d2001-11-04 23:24:20 +0000641 if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
642 LoadInst *LI = cast<LoadInst>(I);
Chris Lattnera0fa5882001-11-26 16:57:31 +0000643
Chris Lattner7a176752001-12-04 00:03:30 +0000644 const Type *LoadedTy = PT->getElementType();
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000645
Chris Lattnera0fa5882001-11-26 16:57:31 +0000646 // They could be loading the first element of a composite type...
647 if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) {
648 unsigned Offset = 0; // No offset, get first leaf.
Chris Lattner697954c2002-01-20 22:54:45 +0000649 std::vector<Value*> Indices; // Discarded...
Chris Lattnera0fa5882001-11-26 16:57:31 +0000650 LoadedTy = getStructOffsetType(CT, Offset, Indices, false);
651 assert(Offset == 0 && "Offset changed from zero???");
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000652 }
653
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000654 if (!LoadedTy->isFirstClassType())
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000655 return false;
656
Chris Lattnera0fa5882001-11-26 16:57:31 +0000657 if (TD.getTypeSize(LoadedTy) != TD.getTypeSize(LI->getType()))
658 return false;
659
660 return ValueConvertableToType(LI, LoadedTy, CTMap);
Chris Lattner901216d2001-11-04 23:24:20 +0000661 }
662 return false;
663
664 case Instruction::Store: {
665 StoreInst *SI = cast<StoreInst>(I);
Chris Lattner901216d2001-11-04 23:24:20 +0000666
667 if (V == I->getOperand(0)) {
Chris Lattner599ca722002-03-21 21:21:13 +0000668 ValueTypeCache::iterator CTMI = CTMap.find(I->getOperand(1));
669 if (CTMI != CTMap.end()) { // Operand #1 is in the table already?
670 // If so, check to see if it's Ty*, or, more importantly, if it is a
671 // pointer to a structure where the first element is a Ty... this code
672 // is neccesary because we might be trying to change the source and
673 // destination type of the store (they might be related) and the dest
674 // pointer type might be a pointer to structure. Below we allow pointer
675 // to structures where the 0th element is compatible with the value,
676 // now we have to support the symmetrical part of this.
677 //
678 const Type *ElTy = cast<PointerType>(CTMI->second)->getElementType();
679
680 // Already a pointer to what we want? Trivially accept...
681 if (ElTy == Ty) return true;
682
683 // Tricky case now, if the destination is a pointer to structure,
684 // obviously the source is not allowed to be a structure (cannot copy
685 // a whole structure at a time), so the level raiser must be trying to
686 // store into the first field. Check for this and allow it now:
687 //
Chris Lattner7e708292002-06-25 16:13:24 +0000688 if (const StructType *SElTy = dyn_cast<StructType>(ElTy)) {
Chris Lattner599ca722002-03-21 21:21:13 +0000689 unsigned Offset = 0;
690 std::vector<Value*> Indices;
691 ElTy = getStructOffsetType(ElTy, Offset, Indices, false);
692 assert(Offset == 0 && "Offset changed!");
693 if (ElTy == 0) // Element at offset zero in struct doesn't exist!
694 return false; // Can only happen for {}*
695
696 if (ElTy == Ty) // Looks like the 0th element of structure is
697 return true; // compatible! Accept now!
698
699 // Otherwise we know that we can't work, so just stop trying now.
700 return false;
701 }
702 }
703
Chris Lattner901216d2001-11-04 23:24:20 +0000704 // Can convert the store if we can convert the pointer operand to match
705 // the new value type...
706 return ExpressionConvertableToType(I->getOperand(1), PointerType::get(Ty),
707 CTMap);
708 } else if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
Chris Lattnerac0077e2001-12-05 19:39:15 +0000709 const Type *ElTy = PT->getElementType();
Chris Lattner901216d2001-11-04 23:24:20 +0000710 assert(V == I->getOperand(1));
711
Chris Lattner921ef3a2002-03-07 21:17:35 +0000712 if (isa<StructType>(ElTy)) {
713 // We can change the destination pointer if we can store our first
714 // argument into the first element of the structure...
715 //
716 unsigned Offset = 0;
717 std::vector<Value*> Indices;
718 ElTy = getStructOffsetType(ElTy, Offset, Indices, false);
719 assert(Offset == 0 && "Offset changed!");
720 if (ElTy == 0) // Element at offset zero in struct doesn't exist!
721 return false; // Can only happen for {}*
722 }
723
Chris Lattner901216d2001-11-04 23:24:20 +0000724 // Must move the same amount of data...
Chris Lattner23014c92002-07-16 22:29:37 +0000725 if (!ElTy->isSized() ||
726 TD.getTypeSize(ElTy) != TD.getTypeSize(I->getOperand(0)->getType()))
Chris Lattnerac0077e2001-12-05 19:39:15 +0000727 return false;
Chris Lattner901216d2001-11-04 23:24:20 +0000728
729 // Can convert store if the incoming value is convertable...
Chris Lattnerac0077e2001-12-05 19:39:15 +0000730 return ExpressionConvertableToType(I->getOperand(0), ElTy, CTMap);
Chris Lattner901216d2001-11-04 23:24:20 +0000731 }
732 return false;
733 }
734
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000735 case Instruction::GetElementPtr:
736 if (V != I->getOperand(0) || !isa<PointerType>(Ty)) return false;
Chris Lattner036efec2001-12-07 04:40:30 +0000737
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000738 // If we have a two operand form of getelementptr, this is really little
739 // more than a simple addition. As with addition, check to see if the
740 // getelementptr instruction can be changed to index into the new type.
Chris Lattner036efec2001-12-07 04:40:30 +0000741 //
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000742 if (I->getNumOperands() == 2) {
743 const Type *OldElTy = cast<PointerType>(I->getType())->getElementType();
744 unsigned DataSize = TD.getTypeSize(OldElTy);
745 Value *Index = I->getOperand(1);
746 Instruction *TempScale = 0;
Chris Lattner036efec2001-12-07 04:40:30 +0000747
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000748 // If the old data element is not unit sized, we have to create a scale
749 // instruction so that ConvertableToGEP will know the REAL amount we are
750 // indexing by. Note that this is never inserted into the instruction
751 // stream, so we have to delete it when we're done.
752 //
753 if (DataSize != 1) {
754 TempScale = BinaryOperator::create(Instruction::Mul, Index,
755 ConstantUInt::get(Type::UIntTy,
756 DataSize));
757 Index = TempScale;
758 }
759
760 // Check to see if the second argument is an expression that can
761 // be converted to the appropriate size... if so, allow it.
762 //
Chris Lattner697954c2002-01-20 22:54:45 +0000763 std::vector<Value*> Indices;
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000764 const Type *ElTy = ConvertableToGEP(Ty, Index, Indices);
765 delete TempScale; // Free our temporary multiply if we made it
766
767 if (ElTy == 0) return false; // Cannot make conversion...
768 return ValueConvertableToType(I, PointerType::get(ElTy), CTMap);
769 }
770 return false;
Chris Lattnera0fa5882001-11-26 16:57:31 +0000771
Chris Lattner71344bd2001-11-06 08:34:17 +0000772 case Instruction::PHINode: {
773 PHINode *PN = cast<PHINode>(I);
774 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
775 if (!ExpressionConvertableToType(PN->getIncomingValue(i), Ty, CTMap))
776 return false;
Chris Lattnera0fa5882001-11-26 16:57:31 +0000777 return ValueConvertableToType(PN, Ty, CTMap);
Chris Lattner71344bd2001-11-06 08:34:17 +0000778 }
Chris Lattner901216d2001-11-04 23:24:20 +0000779
Chris Lattnera0fa5882001-11-26 16:57:31 +0000780 case Instruction::Call: {
781 User::op_iterator OI = find(I->op_begin(), I->op_end(), V);
782 assert (OI != I->op_end() && "Not using value!");
783 unsigned OpNum = OI - I->op_begin();
Chris Lattner901216d2001-11-04 23:24:20 +0000784
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000785 // Are we trying to change the function pointer value to a new type?
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000786 if (OpNum == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000787 const PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000788 if (PTy == 0) return false; // Can't convert to a non-pointer type...
Chris Lattner7e708292002-06-25 16:13:24 +0000789 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000790 if (MTy == 0) return false; // Can't convert to a non ptr to function...
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000791
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000792 // Perform sanity checks to make sure that new function type has the
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000793 // correct number of arguments...
794 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000795 unsigned NumArgs = I->getNumOperands()-1; // Don't include function ptr
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000796
797 // Cannot convert to a type that requires more fixed arguments than
798 // the call provides...
799 //
800 if (NumArgs < MTy->getParamTypes().size()) return false;
801
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000802 // Unless this is a vararg function type, we cannot provide more arguments
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000803 // than are desired...
804 //
805 if (!MTy->isVarArg() && NumArgs > MTy->getParamTypes().size())
806 return false;
807
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000808 // Okay, at this point, we know that the call and the function type match
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000809 // number of arguments. Now we see if we can convert the arguments
Chris Lattnere1a52f62002-03-11 17:27:34 +0000810 // themselves. Note that we do not require operands to be convertable,
811 // we can insert casts if they are convertible but not compatible. The
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000812 // reason for this is that we prefer to have resolved functions but casted
Chris Lattnere1a52f62002-03-11 17:27:34 +0000813 // arguments if possible.
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000814 //
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000815 const FunctionType::ParamTypes &PTs = MTy->getParamTypes();
Chris Lattner54d1e6c2002-02-14 19:44:09 +0000816 for (unsigned i = 0, NA = PTs.size(); i < NA; ++i)
817 if (!PTs[i]->isLosslesslyConvertableTo(I->getOperand(i+1)->getType()))
818 return false; // Operands must have compatible types!
819
820 // Okay, at this point, we know that all of the arguments can be
821 // converted. We succeed if we can change the return type if
822 // neccesary...
823 //
824 return ValueConvertableToType(I, MTy->getReturnType(), CTMap);
825 }
Chris Lattnera0fa5882001-11-26 16:57:31 +0000826
827 const PointerType *MPtr = cast<PointerType>(I->getOperand(0)->getType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000828 const FunctionType *MTy = cast<FunctionType>(MPtr->getElementType());
Chris Lattnera0fa5882001-11-26 16:57:31 +0000829 if (!MTy->isVarArg()) return false;
830
831 if ((OpNum-1) < MTy->getParamTypes().size())
832 return false; // It's not in the varargs section...
833
834 // If we get this far, we know the value is in the varargs section of the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000835 // function! We can convert if we don't reinterpret the value...
Chris Lattner901216d2001-11-04 23:24:20 +0000836 //
Chris Lattnera0fa5882001-11-26 16:57:31 +0000837 return Ty->isLosslesslyConvertableTo(V->getType());
Chris Lattner901216d2001-11-04 23:24:20 +0000838 }
Chris Lattner901216d2001-11-04 23:24:20 +0000839 }
840 return false;
841}
842
843
Chris Lattnera0fa5882001-11-26 16:57:31 +0000844void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC) {
Chris Lattnerce22ec12001-11-13 05:01:36 +0000845 ValueHandle VH(VMC, V);
Chris Lattner71344bd2001-11-06 08:34:17 +0000846
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000847 unsigned NumUses = V->use_size();
848 for (unsigned It = 0; It < NumUses; ) {
849 unsigned OldSize = NumUses;
850 ConvertOperandToType(*(V->use_begin()+It), V, NewVal, VMC);
851 NumUses = V->use_size();
852 if (NumUses == OldSize) ++It;
Chris Lattner901216d2001-11-04 23:24:20 +0000853 }
854}
855
856
857
858static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
859 ValueMapCache &VMC) {
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000860 if (isa<ValueHandle>(U)) return; // Valuehandles don't let go of operands...
861
Chris Lattner71344bd2001-11-06 08:34:17 +0000862 if (VMC.OperandsMapped.count(U)) return;
863 VMC.OperandsMapped.insert(U);
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000864
Chris Lattner71344bd2001-11-06 08:34:17 +0000865 ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(U);
866 if (VMCI != VMC.ExprMap.end())
867 return;
868
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000869
Chris Lattner901216d2001-11-04 23:24:20 +0000870 Instruction *I = cast<Instruction>(U); // Only Instructions convertable
871
872 BasicBlock *BB = I->getParent();
Chris Lattner131454e2002-07-16 17:33:13 +0000873 assert(BB != 0 && "Instruction not embedded in basic block!");
Chris Lattner901216d2001-11-04 23:24:20 +0000874 BasicBlock::InstListType &BIL = BB->getInstList();
Chris Lattnerb1b42622002-07-17 17:11:33 +0000875 std::string Name = I->getName();
876 I->setName("");
Chris Lattner901216d2001-11-04 23:24:20 +0000877 Instruction *Res; // Result of conversion
878
879 //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl;
880
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000881 // Prevent I from being removed...
Chris Lattnerce22ec12001-11-13 05:01:36 +0000882 ValueHandle IHandle(VMC, I);
Chris Lattner71344bd2001-11-06 08:34:17 +0000883
884 const Type *NewTy = NewVal->getType();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000885 Constant *Dummy = (NewTy != Type::VoidTy) ?
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000886 Constant::getNullValue(NewTy) : 0;
Chris Lattnere4f4d8c2001-11-05 18:30:53 +0000887
Chris Lattner901216d2001-11-04 23:24:20 +0000888 switch (I->getOpcode()) {
889 case Instruction::Cast:
Chris Lattnerb1b42622002-07-17 17:11:33 +0000890 if (VMC.NewCasts.count(ValueHandle(VMC, I))) {
891 // This cast has already had it's value converted, causing a new cast to
892 // be created. We don't want to create YET ANOTHER cast instruction
893 // representing the original one, so just modify the operand of this cast
894 // instruction, which we know is newly created.
895 I->setOperand(0, NewVal);
896 I->setName(Name); // give I its name back
897 return;
898
899 } else {
900 Res = new CastInst(NewVal, I->getType(), Name);
901 }
Chris Lattner901216d2001-11-04 23:24:20 +0000902 break;
903
904 case Instruction::Add:
Chris Lattnera8b6d432001-12-05 06:34:00 +0000905 if (isa<PointerType>(NewTy)) {
906 Value *IndexVal = I->getOperand(OldVal == I->getOperand(0) ? 1 : 0);
Chris Lattner697954c2002-01-20 22:54:45 +0000907 std::vector<Value*> Indices;
Chris Lattner7e708292002-06-25 16:13:24 +0000908 BasicBlock::iterator It = I;
Chris Lattnera0fa5882001-11-26 16:57:31 +0000909
910 if (const Type *ETy = ConvertableToGEP(NewTy, IndexVal, Indices, &It)) {
911 // If successful, convert the add to a GEP
Chris Lattner697954c2002-01-20 22:54:45 +0000912 //const Type *RetTy = PointerType::get(ETy);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000913 // First operand is actually the given pointer...
Chris Lattnera8b6d432001-12-05 06:34:00 +0000914 Res = new GetElementPtrInst(NewVal, Indices, Name);
Chris Lattner7a176752001-12-04 00:03:30 +0000915 assert(cast<PointerType>(Res->getType())->getElementType() == ETy &&
Chris Lattnera0fa5882001-11-26 16:57:31 +0000916 "ConvertableToGEP broken!");
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000917 break;
918 }
919 }
920 // FALLTHROUGH
921
Chris Lattner901216d2001-11-04 23:24:20 +0000922 case Instruction::Sub:
923 case Instruction::SetEQ:
924 case Instruction::SetNE: {
Chris Lattner71344bd2001-11-06 08:34:17 +0000925 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
926 Dummy, Dummy, Name);
927 VMC.ExprMap[I] = Res; // Add node to expression eagerly
928
Chris Lattner901216d2001-11-04 23:24:20 +0000929 unsigned OtherIdx = (OldVal == I->getOperand(0)) ? 1 : 0;
930 Value *OtherOp = I->getOperand(OtherIdx);
Chris Lattner71344bd2001-11-06 08:34:17 +0000931 Value *NewOther = ConvertExpressionToType(OtherOp, NewTy, VMC);
Chris Lattner901216d2001-11-04 23:24:20 +0000932
Chris Lattner71344bd2001-11-06 08:34:17 +0000933 Res->setOperand(OtherIdx, NewOther);
934 Res->setOperand(!OtherIdx, NewVal);
Chris Lattner901216d2001-11-04 23:24:20 +0000935 break;
936 }
937 case Instruction::Shl:
938 case Instruction::Shr:
939 assert(I->getOperand(0) == OldVal);
940 Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), NewVal,
941 I->getOperand(1), Name);
942 break;
943
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000944 case Instruction::Free: // Free can free any pointer type!
945 assert(I->getOperand(0) == OldVal);
946 Res = new FreeInst(NewVal);
947 break;
948
949
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000950 case Instruction::Load: {
951 assert(I->getOperand(0) == OldVal && isa<PointerType>(NewVal->getType()));
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000952 const Type *LoadedTy =
953 cast<PointerType>(NewVal->getType())->getElementType();
Chris Lattnera0fa5882001-11-26 16:57:31 +0000954
Chris Lattnerff9a9e52002-08-22 16:41:31 +0000955 Value *Src = NewVal;
Chris Lattnera0fa5882001-11-26 16:57:31 +0000956
957 if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) {
Chris Lattnerff9a9e52002-08-22 16:41:31 +0000958 std::vector<Value*> Indices;
959 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
960
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000961 unsigned Offset = 0; // No offset, get first leaf.
Chris Lattnera0fa5882001-11-26 16:57:31 +0000962 LoadedTy = getStructOffsetType(CT, Offset, Indices, false);
Chris Lattnerff9a9e52002-08-22 16:41:31 +0000963 assert(LoadedTy->isFirstClassType());
964
965 if (Indices.size() != 1) { // Do not generate load X, 0
Chris Lattnerff9a9e52002-08-22 16:41:31 +0000966 // Insert the GEP instruction before this load.
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000967 Src = new GetElementPtrInst(Src, Indices, Name+".idx", I);
Chris Lattnerff9a9e52002-08-22 16:41:31 +0000968 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000969 }
Chris Lattnerff9a9e52002-08-22 16:41:31 +0000970
971 Res = new LoadInst(Src, Name);
Chris Lattner97ac4ee2001-12-14 16:35:53 +0000972 assert(Res->getType()->isFirstClassType() && "Load of structure or array!");
Chris Lattner901216d2001-11-04 23:24:20 +0000973 break;
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000974 }
Chris Lattnera0fa5882001-11-26 16:57:31 +0000975
Chris Lattner901216d2001-11-04 23:24:20 +0000976 case Instruction::Store: {
977 if (I->getOperand(0) == OldVal) { // Replace the source value
Chris Lattner280b09b2002-07-31 22:31:34 +0000978 // Check to see if operand #1 has already been converted...
979 ValueMapCache::ExprMapTy::iterator VMCI =
980 VMC.ExprMap.find(I->getOperand(1));
981 if (VMCI != VMC.ExprMap.end()) {
982 // Comments describing this stuff are in the OperandConvertableToType
983 // switch statement for Store...
984 //
985 const Type *ElTy =
986 cast<PointerType>(VMCI->second->getType())->getElementType();
Chris Lattnerff9a9e52002-08-22 16:41:31 +0000987
988 Value *SrcPtr = VMCI->second;
989
990 if (ElTy != NewTy) {
Chris Lattner280b09b2002-07-31 22:31:34 +0000991 // We check that this is a struct in the initial scan...
992 const StructType *SElTy = cast<StructType>(ElTy);
993
Chris Lattner280b09b2002-07-31 22:31:34 +0000994 std::vector<Value*> Indices;
995 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
Chris Lattnerff9a9e52002-08-22 16:41:31 +0000996
997 unsigned Offset = 0;
Chris Lattner280b09b2002-07-31 22:31:34 +0000998 const Type *Ty = getStructOffsetType(ElTy, Offset, Indices, false);
999 assert(Offset == 0 && "Offset changed!");
1000 assert(NewTy == Ty && "Did not convert to correct type!");
1001
Chris Lattner2a7c23e2002-09-10 17:04:02 +00001002 // Insert the GEP instruction before this store.
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001003 SrcPtr = new GetElementPtrInst(SrcPtr, Indices,
Chris Lattner2a7c23e2002-09-10 17:04:02 +00001004 SrcPtr->getName()+".idx", I);
Chris Lattner280b09b2002-07-31 22:31:34 +00001005 }
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001006 Res = new StoreInst(NewVal, SrcPtr);
Chris Lattner280b09b2002-07-31 22:31:34 +00001007
1008 VMC.ExprMap[I] = Res;
1009 } else {
1010 // Otherwise, we haven't converted Operand #1 over yet...
1011 const PointerType *NewPT = PointerType::get(NewTy);
1012 Res = new StoreInst(NewVal, Constant::getNullValue(NewPT));
1013 VMC.ExprMap[I] = Res;
1014 Res->setOperand(1, ConvertExpressionToType(I->getOperand(1),
1015 NewPT, VMC));
1016 }
Chris Lattner901216d2001-11-04 23:24:20 +00001017 } else { // Replace the source pointer
Chris Lattner7a176752001-12-04 00:03:30 +00001018 const Type *ValTy = cast<PointerType>(NewTy)->getElementType();
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001019
1020 Value *SrcPtr = NewVal;
Chris Lattner921ef3a2002-03-07 21:17:35 +00001021
1022 if (isa<StructType>(ValTy)) {
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001023 std::vector<Value*> Indices;
Chris Lattnerac0077e2001-12-05 19:39:15 +00001024 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001025
1026 unsigned Offset = 0;
Chris Lattner921ef3a2002-03-07 21:17:35 +00001027 ValTy = getStructOffsetType(ValTy, Offset, Indices, false);
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001028
Chris Lattner921ef3a2002-03-07 21:17:35 +00001029 assert(Offset == 0 && ValTy);
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001030
Chris Lattner2a7c23e2002-09-10 17:04:02 +00001031 // Insert the GEP instruction before this store.
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001032 SrcPtr = new GetElementPtrInst(SrcPtr, Indices,
Chris Lattner2a7c23e2002-09-10 17:04:02 +00001033 SrcPtr->getName()+".idx", I);
Chris Lattnerac0077e2001-12-05 19:39:15 +00001034 }
Chris Lattner921ef3a2002-03-07 21:17:35 +00001035
Chris Lattnerff9a9e52002-08-22 16:41:31 +00001036 Res = new StoreInst(Constant::getNullValue(ValTy), SrcPtr);
Chris Lattner71344bd2001-11-06 08:34:17 +00001037 VMC.ExprMap[I] = Res;
1038 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), ValTy, VMC));
Chris Lattner901216d2001-11-04 23:24:20 +00001039 }
1040 break;
1041 }
1042
Chris Lattnera0fa5882001-11-26 16:57:31 +00001043
1044 case Instruction::GetElementPtr: {
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001045 // Convert a one index getelementptr into just about anything that is
1046 // desired.
Chris Lattnera0fa5882001-11-26 16:57:31 +00001047 //
Chris Lattner7e708292002-06-25 16:13:24 +00001048 BasicBlock::iterator It = I;
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001049 const Type *OldElTy = cast<PointerType>(I->getType())->getElementType();
1050 unsigned DataSize = TD.getTypeSize(OldElTy);
1051 Value *Index = I->getOperand(1);
1052
1053 if (DataSize != 1) {
1054 // Insert a multiply of the old element type is not a unit size...
1055 Index = BinaryOperator::create(Instruction::Mul, Index,
Chris Lattner2a7c23e2002-09-10 17:04:02 +00001056 ConstantUInt::get(Type::UIntTy, DataSize),
1057 "scale", It);
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001058 }
1059
1060 // Perform the conversion now...
Chris Lattner036efec2001-12-07 04:40:30 +00001061 //
Chris Lattner697954c2002-01-20 22:54:45 +00001062 std::vector<Value*> Indices;
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001063 const Type *ElTy = ConvertableToGEP(NewVal->getType(), Index, Indices, &It);
Chris Lattner036efec2001-12-07 04:40:30 +00001064 assert(ElTy != 0 && "GEP Conversion Failure!");
Chris Lattner036efec2001-12-07 04:40:30 +00001065 Res = new GetElementPtrInst(NewVal, Indices, Name);
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001066 assert(Res->getType() == PointerType::get(ElTy) &&
1067 "ConvertableToGet failed!");
Chris Lattnera0fa5882001-11-26 16:57:31 +00001068 }
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001069#if 0
1070 if (I->getType() == PointerType::get(Type::SByteTy)) {
1071 // Convert a getelementptr sbyte * %reg111, uint 16 freely back to
1072 // anything that is a pointer type...
1073 //
Chris Lattner7e708292002-06-25 16:13:24 +00001074 BasicBlock::iterator It = I;
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001075
1076 // Check to see if the second argument is an expression that can
1077 // be converted to the appropriate size... if so, allow it.
1078 //
Chris Lattner697954c2002-01-20 22:54:45 +00001079 std::vector<Value*> Indices;
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001080 const Type *ElTy = ConvertableToGEP(NewVal->getType(), I->getOperand(1),
1081 Indices, &It);
1082 assert(ElTy != 0 && "GEP Conversion Failure!");
1083
1084 Res = new GetElementPtrInst(NewVal, Indices, Name);
1085 } else {
1086 // Convert a getelementptr ulong * %reg123, uint %N
1087 // to getelementptr long * %reg123, uint %N
1088 // ... where the type must simply stay the same size...
1089 //
Chris Lattnercc63f1c2002-08-22 23:37:20 +00001090 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
1091 std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end());
1092 Res = new GetElementPtrInst(NewVal, Indices, Name);
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001093 }
1094#endif
1095 break;
Chris Lattnera0fa5882001-11-26 16:57:31 +00001096
Chris Lattner71344bd2001-11-06 08:34:17 +00001097 case Instruction::PHINode: {
1098 PHINode *OldPN = cast<PHINode>(I);
1099 PHINode *NewPN = new PHINode(NewTy, Name);
1100 VMC.ExprMap[I] = NewPN;
1101
1102 while (OldPN->getNumOperands()) {
1103 BasicBlock *BB = OldPN->getIncomingBlock(0);
1104 Value *OldVal = OldPN->getIncomingValue(0);
1105 OldPN->removeIncomingValue(BB);
1106 Value *V = ConvertExpressionToType(OldVal, NewTy, VMC);
1107 NewPN->addIncoming(V, BB);
1108 }
1109 Res = NewPN;
1110 break;
1111 }
1112
Chris Lattnera0fa5882001-11-26 16:57:31 +00001113 case Instruction::Call: {
1114 Value *Meth = I->getOperand(0);
Chris Lattner697954c2002-01-20 22:54:45 +00001115 std::vector<Value*> Params(I->op_begin()+1, I->op_end());
Chris Lattner901216d2001-11-04 23:24:20 +00001116
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001117 if (Meth == OldVal) { // Changing the function pointer?
Chris Lattner7e708292002-06-25 16:13:24 +00001118 const PointerType *NewPTy = cast<PointerType>(NewVal->getType());
1119 const FunctionType *NewTy = cast<FunctionType>(NewPTy->getElementType());
Chris Lattner2aac6bf2002-04-04 22:19:18 +00001120 const FunctionType::ParamTypes &PTs = NewTy->getParamTypes();
Chris Lattnera0fa5882001-11-26 16:57:31 +00001121
Chris Lattnere1a52f62002-03-11 17:27:34 +00001122 // Get an iterator to the call instruction so that we can insert casts for
1123 // operands if needbe. Note that we do not require operands to be
1124 // convertable, we can insert casts if they are convertible but not
1125 // compatible. The reason for this is that we prefer to have resolved
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001126 // functions but casted arguments if possible.
Chris Lattnere1a52f62002-03-11 17:27:34 +00001127 //
Chris Lattner7e708292002-06-25 16:13:24 +00001128 BasicBlock::iterator It = I;
Chris Lattnere1a52f62002-03-11 17:27:34 +00001129
Chris Lattner54d1e6c2002-02-14 19:44:09 +00001130 // Convert over all of the call operands to their new types... but only
1131 // convert over the part that is not in the vararg section of the call.
1132 //
1133 for (unsigned i = 0; i < PTs.size(); ++i)
Chris Lattnere1a52f62002-03-11 17:27:34 +00001134 if (Params[i]->getType() != PTs[i]) {
1135 // Create a cast to convert it to the right type, we know that this
1136 // is a lossless cast...
1137 //
Chris Lattner2a7c23e2002-09-10 17:04:02 +00001138 Params[i] = new CastInst(Params[i], PTs[i], "call.resolve.cast", It);
Chris Lattnere1a52f62002-03-11 17:27:34 +00001139 }
Chris Lattner54d1e6c2002-02-14 19:44:09 +00001140 Meth = NewVal; // Update call destination to new value
1141
1142 } else { // Changing an argument, must be in vararg area
1143 std::vector<Value*>::iterator OI =
1144 find(Params.begin(), Params.end(), OldVal);
1145 assert (OI != Params.end() && "Not using value!");
1146
1147 *OI = NewVal;
1148 }
1149
Chris Lattnera0fa5882001-11-26 16:57:31 +00001150 Res = new CallInst(Meth, Params, Name);
1151 break;
Chris Lattner901216d2001-11-04 23:24:20 +00001152 }
Chris Lattner901216d2001-11-04 23:24:20 +00001153 default:
1154 assert(0 && "Expression convertable, but don't know how to convert?");
1155 return;
1156 }
1157
Chris Lattner97ac4ee2001-12-14 16:35:53 +00001158 // If the instruction was newly created, insert it into the instruction
1159 // stream.
1160 //
Chris Lattner7e708292002-06-25 16:13:24 +00001161 BasicBlock::iterator It = I;
Chris Lattner901216d2001-11-04 23:24:20 +00001162 assert(It != BIL.end() && "Instruction not in own basic block??");
1163 BIL.insert(It, Res); // Keep It pointing to old instruction
1164
Chris Lattnerb3abf9d2002-05-22 17:27:12 +00001165 DEBUG(cerr << "COT CREATED: " << (void*)Res << " " << Res
1166 << "In: " << (void*)I << " " << I << "Out: " << (void*)Res
1167 << " " << Res);
Chris Lattner901216d2001-11-04 23:24:20 +00001168
Chris Lattner71344bd2001-11-06 08:34:17 +00001169 // Add the instruction to the expression map
1170 VMC.ExprMap[I] = Res;
1171
Chris Lattner901216d2001-11-04 23:24:20 +00001172 if (I->getType() != Res->getType())
Chris Lattnera0fa5882001-11-26 16:57:31 +00001173 ConvertValueToNewType(I, Res, VMC);
Chris Lattnere4f4d8c2001-11-05 18:30:53 +00001174 else {
1175 for (unsigned It = 0; It < I->use_size(); ) {
1176 User *Use = *(I->use_begin()+It);
1177 if (isa<ValueHandle>(Use)) // Don't remove ValueHandles!
1178 ++It;
1179 else
1180 Use->replaceUsesOfWith(I, Res);
1181 }
Chris Lattner901216d2001-11-04 23:24:20 +00001182
Chris Lattnerb1b42622002-07-17 17:11:33 +00001183 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
1184 UI != UE; ++UI)
1185 assert(isa<ValueHandle>((Value*)*UI) &&"Uses of Instruction remain!!!");
Chris Lattnere4f4d8c2001-11-05 18:30:53 +00001186 }
1187}
Chris Lattner901216d2001-11-04 23:24:20 +00001188
Chris Lattnerce22ec12001-11-13 05:01:36 +00001189
Chris Lattnera0fa5882001-11-26 16:57:31 +00001190ValueHandle::ValueHandle(ValueMapCache &VMC, Value *V)
1191 : Instruction(Type::VoidTy, UserOp1, ""), Cache(VMC) {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +00001192 //DEBUG(cerr << "VH AQUIRING: " << (void*)V << " " << V);
Chris Lattner71344bd2001-11-06 08:34:17 +00001193 Operands.push_back(Use(V, this));
1194}
1195
Chris Lattnerb1b42622002-07-17 17:11:33 +00001196ValueHandle::ValueHandle(const ValueHandle &VH)
1197 : Instruction(Type::VoidTy, UserOp1, ""), Cache(VH.Cache) {
1198 //DEBUG(cerr << "VH AQUIRING: " << (void*)V << " " << V);
1199 Operands.push_back(Use((Value*)VH.getOperand(0), this));
1200}
1201
Chris Lattnerce22ec12001-11-13 05:01:36 +00001202static void RecursiveDelete(ValueMapCache &Cache, Instruction *I) {
Chris Lattner71344bd2001-11-06 08:34:17 +00001203 if (!I || !I->use_empty()) return;
1204
1205 assert(I->getParent() && "Inst not in basic block!");
1206
Chris Lattnerb3abf9d2002-05-22 17:27:12 +00001207 //DEBUG(cerr << "VH DELETING: " << (void*)I << " " << I);
Chris Lattner71344bd2001-11-06 08:34:17 +00001208
1209 for (User::op_iterator OI = I->op_begin(), OE = I->op_end();
Chris Lattner9750b612002-02-14 18:55:22 +00001210 OI != OE; ++OI)
Chris Lattner7e708292002-06-25 16:13:24 +00001211 if (Instruction *U = dyn_cast<Instruction>(OI->get())) {
Chris Lattner71344bd2001-11-06 08:34:17 +00001212 *OI = 0;
Chris Lattner9750b612002-02-14 18:55:22 +00001213 RecursiveDelete(Cache, U);
Chris Lattner71344bd2001-11-06 08:34:17 +00001214 }
Chris Lattner71344bd2001-11-06 08:34:17 +00001215
1216 I->getParent()->getInstList().remove(I);
Chris Lattnerce22ec12001-11-13 05:01:36 +00001217
1218 Cache.OperandsMapped.erase(I);
1219 Cache.ExprMap.erase(I);
Chris Lattner71344bd2001-11-06 08:34:17 +00001220 delete I;
1221}
1222
Chris Lattnere4f4d8c2001-11-05 18:30:53 +00001223ValueHandle::~ValueHandle() {
1224 if (Operands[0]->use_size() == 1) {
1225 Value *V = Operands[0];
Chris Lattner71344bd2001-11-06 08:34:17 +00001226 Operands[0] = 0; // Drop use!
Chris Lattnere4f4d8c2001-11-05 18:30:53 +00001227
1228 // Now we just need to remove the old instruction so we don't get infinite
1229 // loops. Note that we cannot use DCE because DCE won't remove a store
1230 // instruction, for example.
1231 //
Chris Lattnerce22ec12001-11-13 05:01:36 +00001232 RecursiveDelete(Cache, dyn_cast<Instruction>(V));
Chris Lattnere4f4d8c2001-11-05 18:30:53 +00001233 } else {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +00001234 //DEBUG(cerr << "VH RELEASING: " << (void*)Operands[0].get() << " "
1235 // << Operands[0]->use_size() << " " << Operands[0]);
Chris Lattnere4f4d8c2001-11-05 18:30:53 +00001236 }
Chris Lattner901216d2001-11-04 23:24:20 +00001237}