Chris Lattner | 871d2ce | 2002-09-09 20:25:21 +0000 | [diff] [blame] | 1 | //===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type -------------===// |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 10 | #include "llvm/iOther.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 11 | #include "llvm/iPHINode.h" |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 12 | #include "llvm/iMemory.h" |
Chris Lattner | 968ddc9 | 2002-04-08 20:18:09 +0000 | [diff] [blame] | 13 | #include "llvm/ConstantHandling.h" |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/Expressions.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 15 | #include "Support/STLExtras.h" |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 16 | #include "Support/StatisticReporter.h" |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 18 | using std::cerr; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 19 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 20 | static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, |
| 21 | ValueTypeCache &ConvertedTypes); |
| 22 | |
| 23 | static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, |
| 24 | ValueMapCache &VMC); |
| 25 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 26 | // 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 | // |
| 37 | static bool MallocConvertableToType(MallocInst *MI, const Type *Ty, |
| 38 | ValueTypeCache &CTMap) { |
Chris Lattner | 8e640f1 | 2002-03-21 22:39:59 +0000 | [diff] [blame] | 39 | if (!isa<PointerType>(Ty)) return false; // Malloc always returns pointers |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 40 | |
| 41 | // Deal with the type to allocate, not the pointer type... |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 42 | Ty = cast<PointerType>(Ty)->getElementType(); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 43 | if (!Ty->isSized()) return false; // Can only alloc something with a size |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 44 | |
| 45 | // Analyze the number of bytes allocated... |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 46 | ExprType Expr = ClassifyExpression(MI->getArraySize()); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 48 | // Get information about the base datatype being allocated, before & after |
Chris Lattner | 4840146 | 2002-04-29 20:09:21 +0000 | [diff] [blame] | 49 | int ReqTypeSize = TD.getTypeSize(Ty); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 50 | unsigned OldTypeSize = TD.getTypeSize(MI->getType()->getElementType()); |
| 51 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 52 | // Must have a scale or offset to analyze it... |
Chris Lattner | 5f4a528 | 2002-03-21 23:02:37 +0000 | [diff] [blame] | 53 | if (!Expr.Offset && !Expr.Scale && OldTypeSize == 1) return false; |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 55 | // 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 59 | // The old type might not be of unit size, take old size into consideration |
| 60 | // here... |
Chris Lattner | 4840146 | 2002-04-29 20:09:21 +0000 | [diff] [blame] | 61 | int Offset = OffsetVal * OldTypeSize; |
| 62 | int Scale = ScaleVal * OldTypeSize; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 63 | |
| 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 66 | // |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 67 | if (Offset/ReqTypeSize*ReqTypeSize != Offset || |
| 68 | Scale/ReqTypeSize*ReqTypeSize != Scale) |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 69 | return false; // Nope. |
| 70 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 71 | return true; |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static Instruction *ConvertMallocToType(MallocInst *MI, const Type *Ty, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 75 | const std::string &Name, |
| 76 | ValueMapCache &VMC){ |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 77 | BasicBlock *BB = MI->getParent(); |
| 78 | BasicBlock::iterator It = BB->end(); |
| 79 | |
| 80 | // Analyze the number of bytes allocated... |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 81 | ExprType Expr = ClassifyExpression(MI->getArraySize()); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 82 | |
| 83 | const PointerType *AllocTy = cast<PointerType>(Ty); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 84 | const Type *ElType = AllocTy->getElementType(); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 86 | 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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 99 | It = MI; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 100 | |
| 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 Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 104 | if (Expr.Var->getType() != Type::UIntTy) |
| 105 | Expr.Var = new CastInst(Expr.Var, Type::UIntTy, |
| 106 | Expr.Var->getName()+"-uint", It); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 108 | 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 Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 112 | |
| 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 119 | // If we have an offset now, add it in... |
| 120 | if (Offset != 0) { |
| 121 | assert(Expr.Var && "Var must be nonnull by now!"); |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 122 | Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var, |
| 123 | ConstantUInt::get(Type::UIntTy, Offset), |
| 124 | Expr.Var->getName()+"-off", It); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 126 | |
| 127 | Instruction *NewI = new MallocInst(AllocTy, Expr.Var, Name); |
| 128 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 129 | assert(AllocTy == Ty); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 130 | return NewI; |
| 131 | } |
| 132 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 134 | // ExpressionConvertableToType - Return true if it is possible |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 135 | bool ExpressionConvertableToType(Value *V, const Type *Ty, |
| 136 | ValueTypeCache &CTMap) { |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 137 | // Expression type must be holdable in a register. |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 138 | if (!Ty->isFirstClassType()) |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 139 | return false; |
| 140 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 141 | ValueTypeCache::iterator CTMI = CTMap.find(V); |
| 142 | if (CTMI != CTMap.end()) return CTMI->second == Ty; |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 144 | CTMap[V] = Ty; |
Chris Lattner | 280b09b | 2002-07-31 22:31:34 +0000 | [diff] [blame] | 145 | if (V->getType() == Ty) return true; // Expression already correct type! |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 146 | |
| 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 Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 151 | // const prop'd in general). We just ask the constant propogator to see if |
| 152 | // it can convert the value... |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 153 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 154 | if (Constant *CPV = dyn_cast<Constant>(V)) |
Chris Lattner | 59b6b8e | 2002-01-21 23:17:48 +0000 | [diff] [blame] | 155 | if (ConstantFoldCastInstruction(CPV, Ty)) |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 156 | return true; // Don't worry about deallocating, it's a constant. |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 157 | |
| 158 | return false; // Otherwise, we can't convert! |
| 159 | } |
Chris Lattner | cf1ae7c | 2001-11-09 01:08:10 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 161 | 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 165 | if (!Ty->isLosslesslyConvertableTo(I->getType())) return false; |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 166 | |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 167 | // 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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 170 | 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 Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 174 | if (AT->getElementType() == DPT->getElementType()) |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 175 | return false; |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 176 | break; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 177 | |
| 178 | case Instruction::Add: |
| 179 | case Instruction::Sub: |
Chris Lattner | e29f6a8 | 2002-09-10 19:42:53 +0000 | [diff] [blame^] | 180 | if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false; |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 181 | if (!ExpressionConvertableToType(I->getOperand(0), Ty, CTMap) || |
| 182 | !ExpressionConvertableToType(I->getOperand(1), Ty, CTMap)) |
| 183 | return false; |
| 184 | break; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 185 | case Instruction::Shr: |
Chris Lattner | 871d2ce | 2002-09-09 20:25:21 +0000 | [diff] [blame] | 186 | if (!Ty->isInteger()) return false; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 187 | if (Ty->isSigned() != V->getType()->isSigned()) return false; |
| 188 | // FALL THROUGH |
| 189 | case Instruction::Shl: |
Chris Lattner | 871d2ce | 2002-09-09 20:25:21 +0000 | [diff] [blame] | 190 | if (!Ty->isInteger()) return false; |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 191 | if (!ExpressionConvertableToType(I->getOperand(0), Ty, CTMap)) |
| 192 | return false; |
| 193 | break; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 194 | |
| 195 | case Instruction::Load: { |
| 196 | LoadInst *LI = cast<LoadInst>(I); |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 197 | if (!ExpressionConvertableToType(LI->getPointerOperand(), |
| 198 | PointerType::get(Ty), CTMap)) |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 199 | return false; |
| 200 | break; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 201 | } |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 202 | 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 Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 207 | break; |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 210 | case Instruction::Malloc: |
| 211 | if (!MallocConvertableToType(cast<MallocInst>(I), Ty, CTMap)) |
| 212 | return false; |
| 213 | break; |
| 214 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 215 | 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 Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 227 | if (!PTy) return false; // GEP must always return a pointer... |
| 228 | const Type *PVTy = PTy->getElementType(); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 229 | |
| 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 Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 234 | std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end()); |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 235 | const Type *BaseType = GEP->getPointerOperand()->getType(); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 236 | const Type *ElTy = 0; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 237 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 238 | while (!Indices.empty() && isa<ConstantUInt>(Indices.back()) && |
| 239 | cast<ConstantUInt>(Indices.back())->getValue() == 0) { |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 240 | Indices.pop_back(); |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 241 | ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices, true); |
| 242 | if (ElTy == PVTy) |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 243 | break; // Found a match!! |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 244 | ElTy = 0; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 247 | 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 Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 250 | // current gep is of the form 'getelementptr sbyte*, unsigned N |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 251 | // 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 Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 256 | |
| 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 Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 266 | std::vector<Value*> Indices; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 267 | const Type *ElTy = ConvertableToGEP(PTy, I->getOperand(1), Indices); |
Chris Lattner | 868c2d3 | 2002-02-14 22:21:40 +0000 | [diff] [blame] | 268 | if (ElTy == PVTy) { |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 269 | if (!ExpressionConvertableToType(I->getOperand(0), |
| 270 | PointerType::get(ElTy), CTMap)) |
| 271 | return false; // Can't continue, ExConToTy might have polluted set! |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 272 | 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 Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 285 | const PointerType *NewSrcTy = PointerType::get(PVTy); |
| 286 | if (!ExpressionConvertableToType(I->getOperand(0), NewSrcTy, CTMap)) |
| 287 | return false; |
| 288 | break; |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 291 | return false; // No match, maybe next time. |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 293 | |
| 294 | default: |
| 295 | return false; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 296 | } |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 297 | |
| 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 302 | 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 Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 305 | |
| 306 | return true; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 310 | Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) { |
Chris Lattner | ac0077e | 2001-12-05 19:39:15 +0000 | [diff] [blame] | 311 | if (V->getType() == Ty) return V; // Already where we need to be? |
| 312 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 313 | ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(V); |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 314 | if (VMCI != VMC.ExprMap.end()) { |
Chris Lattner | 280b09b | 2002-07-31 22:31:34 +0000 | [diff] [blame] | 315 | const Value *GV = VMCI->second; |
| 316 | const Type *GTy = VMCI->second->getType(); |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 317 | assert(VMCI->second->getType() == Ty); |
Chris Lattner | 9750b61 | 2002-02-14 18:55:22 +0000 | [diff] [blame] | 318 | |
| 319 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 320 | ValueHandle IHandle(VMC, I); // Remove I if it is unused now! |
| 321 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 322 | return VMCI->second; |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 323 | } |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 324 | |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 325 | DEBUG(cerr << "CETT: " << (void*)V << " " << V); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 327 | Instruction *I = dyn_cast<Instruction>(V); |
| 328 | if (I == 0) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 329 | if (Constant *CPV = cast<Constant>(V)) { |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 330 | // Constants are converted by constant folding the cast that is required. |
| 331 | // We assume here that all casts are implemented for constant prop. |
Chris Lattner | 59b6b8e | 2002-01-21 23:17:48 +0000 | [diff] [blame] | 332 | Value *Result = ConstantFoldCastInstruction(CPV, Ty); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 333 | assert(Result && "ConstantFoldCastInstruction Failed!!!"); |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 334 | assert(Result->getType() == Ty && "Const prop of cast failed!"); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 335 | |
| 336 | // Add the instruction to the expression map |
| 337 | VMC.ExprMap[V] = Result; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 338 | return Result; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | BasicBlock *BB = I->getParent(); |
| 343 | BasicBlock::InstListType &BIL = BB->getInstList(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 344 | std::string Name = I->getName(); if (!Name.empty()) I->setName(""); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 345 | Instruction *Res; // Result of conversion |
| 346 | |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 347 | ValueHandle IHandle(VMC, I); // Prevent I from being removed! |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 349 | Constant *Dummy = Constant::getNullValue(Ty); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 351 | switch (I->getOpcode()) { |
| 352 | case Instruction::Cast: |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 353 | assert(VMC.NewCasts.count(ValueHandle(VMC, I)) == 0); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 354 | Res = new CastInst(I->getOperand(0), Ty, Name); |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 355 | VMC.NewCasts.insert(ValueHandle(VMC, Res)); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 356 | break; |
| 357 | |
| 358 | case Instruction::Add: |
| 359 | case Instruction::Sub: |
| 360 | Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(), |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 361 | 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 366 | break; |
| 367 | |
| 368 | case Instruction::Shl: |
| 369 | case Instruction::Shr: |
Chris Lattner | 75747cd | 2001-11-06 21:51:48 +0000 | [diff] [blame] | 370 | Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), Dummy, |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 371 | I->getOperand(1), Name); |
Chris Lattner | 75747cd | 2001-11-06 21:51:48 +0000 | [diff] [blame] | 372 | VMC.ExprMap[I] = Res; |
| 373 | Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC)); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 374 | break; |
| 375 | |
| 376 | case Instruction::Load: { |
| 377 | LoadInst *LI = cast<LoadInst>(I); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 379 | Res = new LoadInst(Constant::getNullValue(PointerType::get(Ty)), Name); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 380 | VMC.ExprMap[I] = Res; |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 381 | Res->setOperand(0, ConvertExpressionToType(LI->getPointerOperand(), |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 382 | PointerType::get(Ty), VMC)); |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 383 | assert(Res->getOperand(0)->getType() == PointerType::get(Ty)); |
| 384 | assert(Ty == Res->getType()); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 385 | assert(Res->getType()->isFirstClassType() && "Load of structure or array!"); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 386 | 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 Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 397 | ValueHandle OldValHandle(VMC, OldVal); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 398 | OldPN->removeIncomingValue(BB); |
| 399 | Value *V = ConvertExpressionToType(OldVal, Ty, VMC); |
| 400 | NewPN->addIncoming(V, BB); |
| 401 | } |
| 402 | Res = NewPN; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 403 | break; |
| 404 | } |
| 405 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 406 | case Instruction::Malloc: { |
| 407 | Res = ConvertMallocToType(cast<MallocInst>(I), Ty, Name, VMC); |
| 408 | break; |
| 409 | } |
| 410 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 411 | 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 Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 427 | std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end()); |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 428 | const Type *BaseType = GEP->getPointerOperand()->getType(); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 429 | const Type *PVTy = cast<PointerType>(Ty)->getElementType(); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 430 | Res = 0; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 431 | while (!Indices.empty() && isa<ConstantUInt>(Indices.back()) && |
| 432 | cast<ConstantUInt>(Indices.back())->getValue() == 0) { |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 433 | Indices.pop_back(); |
| 434 | if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) { |
| 435 | if (Indices.size() == 0) { |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 436 | Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 437 | } else { |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 438 | Res = new GetElementPtrInst(GEP->getPointerOperand(), Indices, Name); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 439 | } |
| 440 | break; |
| 441 | } |
| 442 | } |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 444 | if (Res == 0 && GEP->getNumOperands() == 2 && |
| 445 | GEP->getOperand(1)->getType() == Type::UIntTy && |
| 446 | GEP->getType() == PointerType::get(Type::SByteTy)) { |
| 447 | |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 448 | // 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 Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 452 | const PointerType *NewSrcTy = PointerType::get(PVTy); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 453 | BasicBlock::iterator It = I; |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 454 | |
| 455 | // Check to see if 'N' is an expression that can be converted to |
| 456 | // the appropriate size... if so, allow it. |
| 457 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 458 | std::vector<Value*> Indices; |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 459 | const Type *ElTy = ConvertableToGEP(NewSrcTy, I->getOperand(1), |
| 460 | Indices, &It); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 461 | if (ElTy) { |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 462 | assert(ElTy == PVTy && "Internal error, setup wrong!"); |
Chris Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 463 | Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy), |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 464 | Indices, Name); |
| 465 | VMC.ExprMap[I] = Res; |
| 466 | Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), |
| 467 | NewSrcTy, VMC)); |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 468 | } |
| 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 Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 477 | const PointerType *NewSrcTy = PointerType::get(PVTy); |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 478 | std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end()); |
Chris Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 479 | Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy), |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 480 | Indices, Name); |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 481 | VMC.ExprMap[I] = Res; |
| 482 | Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), |
| 483 | NewSrcTy, VMC)); |
| 484 | } |
| 485 | |
| 486 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 487 | 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 Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 496 | assert(Res->getType() == Ty && "Didn't convert expr to correct type!"); |
| 497 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 498 | BIL.insert(I, Res); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 499 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 500 | // 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 Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 515 | DEBUG(cerr << "ExpIn: " << (void*)I << " " << I |
| 516 | << "ExpOut: " << (void*)Res << " " << Res); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 517 | |
| 518 | return Res; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 523 | // ValueConvertableToType - Return true if it is possible |
| 524 | bool ValueConvertableToType(Value *V, const Type *Ty, |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 525 | 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 Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 533 | 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 538 | |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 543 | |
| 544 | |
| 545 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 546 | // 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 | // |
| 552 | static bool OperandConvertableToType(User *U, Value *V, const Type *Ty, |
| 553 | ValueTypeCache &CTMap) { |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 554 | // if (V->getType() == Ty) return true; // Operand already the right type? |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 555 | |
| 556 | // Expression type must be holdable in a register. |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 557 | if (!Ty->isFirstClassType()) |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 558 | return false; |
| 559 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 560 | 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 Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 568 | // 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 Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 572 | return false; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 573 | |
Chris Lattner | bbad858 | 2002-05-02 17:37:34 +0000 | [diff] [blame] | 574 | // 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 Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 583 | |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 584 | // 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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 587 | 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 Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 591 | if (AT->getElementType() == DPT->getElementType()) |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 592 | return false; |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 593 | return true; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 594 | |
| 595 | case Instruction::Add: |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 596 | if (isa<PointerType>(Ty)) { |
| 597 | Value *IndexVal = I->getOperand(V == I->getOperand(0) ? 1 : 0); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 598 | std::vector<Value*> Indices; |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 599 | if (const Type *ETy = ConvertableToGEP(Ty, IndexVal, Indices)) { |
| 600 | const Type *RetTy = PointerType::get(ETy); |
| 601 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 602 | // Only successful if we can convert this type to the required type |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 603 | if (ValueConvertableToType(I, RetTy, CTMap)) { |
| 604 | CTMap[I] = RetTy; |
| 605 | return true; |
| 606 | } |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 607 | // We have to return failure here because ValueConvertableToType could |
| 608 | // have polluted our map |
| 609 | return false; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | // FALLTHROUGH |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 613 | case Instruction::Sub: { |
Chris Lattner | e29f6a8 | 2002-09-10 19:42:53 +0000 | [diff] [blame^] | 614 | if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false; |
| 615 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 616 | Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 617 | return ValueConvertableToType(I, Ty, CTMap) && |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 618 | 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 Lattner | 871d2ce | 2002-09-09 20:25:21 +0000 | [diff] [blame] | 630 | if (!Ty->isInteger()) return false; |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 631 | return ValueConvertableToType(I, Ty, CTMap); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 633 | case Instruction::Free: |
| 634 | assert(I->getOperand(0) == V); |
| 635 | return isa<PointerType>(Ty); // Free can free any pointer type! |
| 636 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 637 | case Instruction::Load: |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 638 | // Cannot convert the types of any subscripts... |
| 639 | if (I->getOperand(0) != V) return false; |
| 640 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 641 | if (const PointerType *PT = dyn_cast<PointerType>(Ty)) { |
| 642 | LoadInst *LI = cast<LoadInst>(I); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 644 | const Type *LoadedTy = PT->getElementType(); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 645 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 646 | // 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 Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 649 | std::vector<Value*> Indices; // Discarded... |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 650 | LoadedTy = getStructOffsetType(CT, Offset, Indices, false); |
| 651 | assert(Offset == 0 && "Offset changed from zero???"); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 654 | if (!LoadedTy->isFirstClassType()) |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 655 | return false; |
| 656 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 657 | if (TD.getTypeSize(LoadedTy) != TD.getTypeSize(LI->getType())) |
| 658 | return false; |
| 659 | |
| 660 | return ValueConvertableToType(LI, LoadedTy, CTMap); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 661 | } |
| 662 | return false; |
| 663 | |
| 664 | case Instruction::Store: { |
| 665 | StoreInst *SI = cast<StoreInst>(I); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 666 | |
| 667 | if (V == I->getOperand(0)) { |
Chris Lattner | 599ca72 | 2002-03-21 21:21:13 +0000 | [diff] [blame] | 668 | 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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 688 | if (const StructType *SElTy = dyn_cast<StructType>(ElTy)) { |
Chris Lattner | 599ca72 | 2002-03-21 21:21:13 +0000 | [diff] [blame] | 689 | 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 704 | // 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 Lattner | ac0077e | 2001-12-05 19:39:15 +0000 | [diff] [blame] | 709 | const Type *ElTy = PT->getElementType(); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 710 | assert(V == I->getOperand(1)); |
| 711 | |
Chris Lattner | 921ef3a | 2002-03-07 21:17:35 +0000 | [diff] [blame] | 712 | 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 724 | // Must move the same amount of data... |
Chris Lattner | 23014c9 | 2002-07-16 22:29:37 +0000 | [diff] [blame] | 725 | if (!ElTy->isSized() || |
| 726 | TD.getTypeSize(ElTy) != TD.getTypeSize(I->getOperand(0)->getType())) |
Chris Lattner | ac0077e | 2001-12-05 19:39:15 +0000 | [diff] [blame] | 727 | return false; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 728 | |
| 729 | // Can convert store if the incoming value is convertable... |
Chris Lattner | ac0077e | 2001-12-05 19:39:15 +0000 | [diff] [blame] | 730 | return ExpressionConvertableToType(I->getOperand(0), ElTy, CTMap); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 731 | } |
| 732 | return false; |
| 733 | } |
| 734 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 735 | case Instruction::GetElementPtr: |
| 736 | if (V != I->getOperand(0) || !isa<PointerType>(Ty)) return false; |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 738 | // 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 Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 741 | // |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 742 | 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 Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 747 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 748 | // 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 Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 763 | std::vector<Value*> Indices; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 764 | 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 771 | |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 772 | 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 777 | return ValueConvertableToType(PN, Ty, CTMap); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 778 | } |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 779 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 780 | 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 784 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 785 | // Are we trying to change the function pointer value to a new type? |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 786 | if (OpNum == 0) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 787 | const PointerType *PTy = dyn_cast<PointerType>(Ty); |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 788 | if (PTy == 0) return false; // Can't convert to a non-pointer type... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 789 | const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 790 | if (MTy == 0) return false; // Can't convert to a non ptr to function... |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 791 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 792 | // Perform sanity checks to make sure that new function type has the |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 793 | // correct number of arguments... |
| 794 | // |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 795 | unsigned NumArgs = I->getNumOperands()-1; // Don't include function ptr |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 796 | |
| 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 Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 802 | // Unless this is a vararg function type, we cannot provide more arguments |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 803 | // than are desired... |
| 804 | // |
| 805 | if (!MTy->isVarArg() && NumArgs > MTy->getParamTypes().size()) |
| 806 | return false; |
| 807 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 808 | // Okay, at this point, we know that the call and the function type match |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 809 | // number of arguments. Now we see if we can convert the arguments |
Chris Lattner | e1a52f6 | 2002-03-11 17:27:34 +0000 | [diff] [blame] | 810 | // 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 Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 812 | // reason for this is that we prefer to have resolved functions but casted |
Chris Lattner | e1a52f6 | 2002-03-11 17:27:34 +0000 | [diff] [blame] | 813 | // arguments if possible. |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 814 | // |
Chris Lattner | 2aac6bf | 2002-04-04 22:19:18 +0000 | [diff] [blame] | 815 | const FunctionType::ParamTypes &PTs = MTy->getParamTypes(); |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 816 | 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 826 | |
| 827 | const PointerType *MPtr = cast<PointerType>(I->getOperand(0)->getType()); |
Chris Lattner | 2aac6bf | 2002-04-04 22:19:18 +0000 | [diff] [blame] | 828 | const FunctionType *MTy = cast<FunctionType>(MPtr->getElementType()); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 829 | 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 Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 835 | // function! We can convert if we don't reinterpret the value... |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 836 | // |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 837 | return Ty->isLosslesslyConvertableTo(V->getType()); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 838 | } |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 839 | } |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 844 | void ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC) { |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 845 | ValueHandle VH(VMC, V); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 846 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 847 | 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
| 856 | |
| 857 | |
| 858 | static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, |
| 859 | ValueMapCache &VMC) { |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 860 | if (isa<ValueHandle>(U)) return; // Valuehandles don't let go of operands... |
| 861 | |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 862 | if (VMC.OperandsMapped.count(U)) return; |
| 863 | VMC.OperandsMapped.insert(U); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 865 | ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(U); |
| 866 | if (VMCI != VMC.ExprMap.end()) |
| 867 | return; |
| 868 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 870 | Instruction *I = cast<Instruction>(U); // Only Instructions convertable |
| 871 | |
| 872 | BasicBlock *BB = I->getParent(); |
Chris Lattner | 131454e | 2002-07-16 17:33:13 +0000 | [diff] [blame] | 873 | assert(BB != 0 && "Instruction not embedded in basic block!"); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 874 | BasicBlock::InstListType &BIL = BB->getInstList(); |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 875 | std::string Name = I->getName(); |
| 876 | I->setName(""); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 877 | Instruction *Res; // Result of conversion |
| 878 | |
| 879 | //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl; |
| 880 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 881 | // Prevent I from being removed... |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 882 | ValueHandle IHandle(VMC, I); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 883 | |
| 884 | const Type *NewTy = NewVal->getType(); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 885 | Constant *Dummy = (NewTy != Type::VoidTy) ? |
Chris Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 886 | Constant::getNullValue(NewTy) : 0; |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 888 | switch (I->getOpcode()) { |
| 889 | case Instruction::Cast: |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 890 | 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 902 | break; |
| 903 | |
| 904 | case Instruction::Add: |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 905 | if (isa<PointerType>(NewTy)) { |
| 906 | Value *IndexVal = I->getOperand(OldVal == I->getOperand(0) ? 1 : 0); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 907 | std::vector<Value*> Indices; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 908 | BasicBlock::iterator It = I; |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 909 | |
| 910 | if (const Type *ETy = ConvertableToGEP(NewTy, IndexVal, Indices, &It)) { |
| 911 | // If successful, convert the add to a GEP |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 912 | //const Type *RetTy = PointerType::get(ETy); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 913 | // First operand is actually the given pointer... |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 914 | Res = new GetElementPtrInst(NewVal, Indices, Name); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 915 | assert(cast<PointerType>(Res->getType())->getElementType() == ETy && |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 916 | "ConvertableToGEP broken!"); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
| 919 | } |
| 920 | // FALLTHROUGH |
| 921 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 922 | case Instruction::Sub: |
| 923 | case Instruction::SetEQ: |
| 924 | case Instruction::SetNE: { |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 925 | Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(), |
| 926 | Dummy, Dummy, Name); |
| 927 | VMC.ExprMap[I] = Res; // Add node to expression eagerly |
| 928 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 929 | unsigned OtherIdx = (OldVal == I->getOperand(0)) ? 1 : 0; |
| 930 | Value *OtherOp = I->getOperand(OtherIdx); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 931 | Value *NewOther = ConvertExpressionToType(OtherOp, NewTy, VMC); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 933 | Res->setOperand(OtherIdx, NewOther); |
| 934 | Res->setOperand(!OtherIdx, NewVal); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 935 | 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 Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 944 | 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 Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 950 | case Instruction::Load: { |
| 951 | assert(I->getOperand(0) == OldVal && isa<PointerType>(NewVal->getType())); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 952 | const Type *LoadedTy = |
| 953 | cast<PointerType>(NewVal->getType())->getElementType(); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 954 | |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 955 | Value *Src = NewVal; |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 956 | |
| 957 | if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) { |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 958 | std::vector<Value*> Indices; |
| 959 | Indices.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
| 960 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 961 | unsigned Offset = 0; // No offset, get first leaf. |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 962 | LoadedTy = getStructOffsetType(CT, Offset, Indices, false); |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 963 | assert(LoadedTy->isFirstClassType()); |
| 964 | |
| 965 | if (Indices.size() != 1) { // Do not generate load X, 0 |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 966 | // Insert the GEP instruction before this load. |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 967 | Src = new GetElementPtrInst(Src, Indices, Name+".idx", I); |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 968 | } |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 969 | } |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 970 | |
| 971 | Res = new LoadInst(Src, Name); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 972 | assert(Res->getType()->isFirstClassType() && "Load of structure or array!"); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 973 | break; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 974 | } |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 975 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 976 | case Instruction::Store: { |
| 977 | if (I->getOperand(0) == OldVal) { // Replace the source value |
Chris Lattner | 280b09b | 2002-07-31 22:31:34 +0000 | [diff] [blame] | 978 | // 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 Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 987 | |
| 988 | Value *SrcPtr = VMCI->second; |
| 989 | |
| 990 | if (ElTy != NewTy) { |
Chris Lattner | 280b09b | 2002-07-31 22:31:34 +0000 | [diff] [blame] | 991 | // We check that this is a struct in the initial scan... |
| 992 | const StructType *SElTy = cast<StructType>(ElTy); |
| 993 | |
Chris Lattner | 280b09b | 2002-07-31 22:31:34 +0000 | [diff] [blame] | 994 | std::vector<Value*> Indices; |
| 995 | Indices.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 996 | |
| 997 | unsigned Offset = 0; |
Chris Lattner | 280b09b | 2002-07-31 22:31:34 +0000 | [diff] [blame] | 998 | 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 Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 1002 | // Insert the GEP instruction before this store. |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1003 | SrcPtr = new GetElementPtrInst(SrcPtr, Indices, |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 1004 | SrcPtr->getName()+".idx", I); |
Chris Lattner | 280b09b | 2002-07-31 22:31:34 +0000 | [diff] [blame] | 1005 | } |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1006 | Res = new StoreInst(NewVal, SrcPtr); |
Chris Lattner | 280b09b | 2002-07-31 22:31:34 +0000 | [diff] [blame] | 1007 | |
| 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1017 | } else { // Replace the source pointer |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 1018 | const Type *ValTy = cast<PointerType>(NewTy)->getElementType(); |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1019 | |
| 1020 | Value *SrcPtr = NewVal; |
Chris Lattner | 921ef3a | 2002-03-07 21:17:35 +0000 | [diff] [blame] | 1021 | |
| 1022 | if (isa<StructType>(ValTy)) { |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1023 | std::vector<Value*> Indices; |
Chris Lattner | ac0077e | 2001-12-05 19:39:15 +0000 | [diff] [blame] | 1024 | Indices.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1025 | |
| 1026 | unsigned Offset = 0; |
Chris Lattner | 921ef3a | 2002-03-07 21:17:35 +0000 | [diff] [blame] | 1027 | ValTy = getStructOffsetType(ValTy, Offset, Indices, false); |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | 921ef3a | 2002-03-07 21:17:35 +0000 | [diff] [blame] | 1029 | assert(Offset == 0 && ValTy); |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1030 | |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 1031 | // Insert the GEP instruction before this store. |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1032 | SrcPtr = new GetElementPtrInst(SrcPtr, Indices, |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 1033 | SrcPtr->getName()+".idx", I); |
Chris Lattner | ac0077e | 2001-12-05 19:39:15 +0000 | [diff] [blame] | 1034 | } |
Chris Lattner | 921ef3a | 2002-03-07 21:17:35 +0000 | [diff] [blame] | 1035 | |
Chris Lattner | ff9a9e5 | 2002-08-22 16:41:31 +0000 | [diff] [blame] | 1036 | Res = new StoreInst(Constant::getNullValue(ValTy), SrcPtr); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1037 | VMC.ExprMap[I] = Res; |
| 1038 | Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), ValTy, VMC)); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1039 | } |
| 1040 | break; |
| 1041 | } |
| 1042 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1043 | |
| 1044 | case Instruction::GetElementPtr: { |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1045 | // Convert a one index getelementptr into just about anything that is |
| 1046 | // desired. |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1047 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1048 | BasicBlock::iterator It = I; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1049 | 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 Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 1056 | ConstantUInt::get(Type::UIntTy, DataSize), |
| 1057 | "scale", It); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | // Perform the conversion now... |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 1061 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1062 | std::vector<Value*> Indices; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1063 | const Type *ElTy = ConvertableToGEP(NewVal->getType(), Index, Indices, &It); |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 1064 | assert(ElTy != 0 && "GEP Conversion Failure!"); |
Chris Lattner | 036efec | 2001-12-07 04:40:30 +0000 | [diff] [blame] | 1065 | Res = new GetElementPtrInst(NewVal, Indices, Name); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1066 | assert(Res->getType() == PointerType::get(ElTy) && |
| 1067 | "ConvertableToGet failed!"); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1068 | } |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1069 | #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 Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1074 | BasicBlock::iterator It = I; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1075 | |
| 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 Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1079 | std::vector<Value*> Indices; |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1080 | 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 Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 1090 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(I); |
| 1091 | std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end()); |
| 1092 | Res = new GetElementPtrInst(NewVal, Indices, Name); |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1093 | } |
| 1094 | #endif |
| 1095 | break; |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1096 | |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1097 | 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1113 | case Instruction::Call: { |
| 1114 | Value *Meth = I->getOperand(0); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1115 | std::vector<Value*> Params(I->op_begin()+1, I->op_end()); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1117 | if (Meth == OldVal) { // Changing the function pointer? |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1118 | const PointerType *NewPTy = cast<PointerType>(NewVal->getType()); |
| 1119 | const FunctionType *NewTy = cast<FunctionType>(NewPTy->getElementType()); |
Chris Lattner | 2aac6bf | 2002-04-04 22:19:18 +0000 | [diff] [blame] | 1120 | const FunctionType::ParamTypes &PTs = NewTy->getParamTypes(); |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | e1a52f6 | 2002-03-11 17:27:34 +0000 | [diff] [blame] | 1122 | // 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 Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1126 | // functions but casted arguments if possible. |
Chris Lattner | e1a52f6 | 2002-03-11 17:27:34 +0000 | [diff] [blame] | 1127 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1128 | BasicBlock::iterator It = I; |
Chris Lattner | e1a52f6 | 2002-03-11 17:27:34 +0000 | [diff] [blame] | 1129 | |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 1130 | // 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 Lattner | e1a52f6 | 2002-03-11 17:27:34 +0000 | [diff] [blame] | 1134 | 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 Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 1138 | Params[i] = new CastInst(Params[i], PTs[i], "call.resolve.cast", It); |
Chris Lattner | e1a52f6 | 2002-03-11 17:27:34 +0000 | [diff] [blame] | 1139 | } |
Chris Lattner | 54d1e6c | 2002-02-14 19:44:09 +0000 | [diff] [blame] | 1140 | 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 Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1150 | Res = new CallInst(Meth, Params, Name); |
| 1151 | break; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1152 | } |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1153 | default: |
| 1154 | assert(0 && "Expression convertable, but don't know how to convert?"); |
| 1155 | return; |
| 1156 | } |
| 1157 | |
Chris Lattner | 97ac4ee | 2001-12-14 16:35:53 +0000 | [diff] [blame] | 1158 | // If the instruction was newly created, insert it into the instruction |
| 1159 | // stream. |
| 1160 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1161 | BasicBlock::iterator It = I; |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1162 | assert(It != BIL.end() && "Instruction not in own basic block??"); |
| 1163 | BIL.insert(It, Res); // Keep It pointing to old instruction |
| 1164 | |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 1165 | DEBUG(cerr << "COT CREATED: " << (void*)Res << " " << Res |
| 1166 | << "In: " << (void*)I << " " << I << "Out: " << (void*)Res |
| 1167 | << " " << Res); |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1168 | |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1169 | // Add the instruction to the expression map |
| 1170 | VMC.ExprMap[I] = Res; |
| 1171 | |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1172 | if (I->getType() != Res->getType()) |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1173 | ConvertValueToNewType(I, Res, VMC); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 1174 | 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 Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 1183 | 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 Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 1186 | } |
| 1187 | } |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1188 | |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | a0fa588 | 2001-11-26 16:57:31 +0000 | [diff] [blame] | 1190 | ValueHandle::ValueHandle(ValueMapCache &VMC, Value *V) |
| 1191 | : Instruction(Type::VoidTy, UserOp1, ""), Cache(VMC) { |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 1192 | //DEBUG(cerr << "VH AQUIRING: " << (void*)V << " " << V); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1193 | Operands.push_back(Use(V, this)); |
| 1194 | } |
| 1195 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 1196 | ValueHandle::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 Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 1202 | static void RecursiveDelete(ValueMapCache &Cache, Instruction *I) { |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1203 | if (!I || !I->use_empty()) return; |
| 1204 | |
| 1205 | assert(I->getParent() && "Inst not in basic block!"); |
| 1206 | |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 1207 | //DEBUG(cerr << "VH DELETING: " << (void*)I << " " << I); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1208 | |
| 1209 | for (User::op_iterator OI = I->op_begin(), OE = I->op_end(); |
Chris Lattner | 9750b61 | 2002-02-14 18:55:22 +0000 | [diff] [blame] | 1210 | OI != OE; ++OI) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1211 | if (Instruction *U = dyn_cast<Instruction>(OI->get())) { |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1212 | *OI = 0; |
Chris Lattner | 9750b61 | 2002-02-14 18:55:22 +0000 | [diff] [blame] | 1213 | RecursiveDelete(Cache, U); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1214 | } |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1215 | |
| 1216 | I->getParent()->getInstList().remove(I); |
Chris Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 1217 | |
| 1218 | Cache.OperandsMapped.erase(I); |
| 1219 | Cache.ExprMap.erase(I); |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1220 | delete I; |
| 1221 | } |
| 1222 | |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 1223 | ValueHandle::~ValueHandle() { |
| 1224 | if (Operands[0]->use_size() == 1) { |
| 1225 | Value *V = Operands[0]; |
Chris Lattner | 71344bd | 2001-11-06 08:34:17 +0000 | [diff] [blame] | 1226 | Operands[0] = 0; // Drop use! |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 1227 | |
| 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 Lattner | ce22ec1 | 2001-11-13 05:01:36 +0000 | [diff] [blame] | 1232 | RecursiveDelete(Cache, dyn_cast<Instruction>(V)); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 1233 | } else { |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 1234 | //DEBUG(cerr << "VH RELEASING: " << (void*)Operands[0].get() << " " |
| 1235 | // << Operands[0]->use_size() << " " << Operands[0]); |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 1236 | } |
Chris Lattner | 901216d | 2001-11-04 23:24:20 +0000 | [diff] [blame] | 1237 | } |