Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 1 | //===-- TransformInternals.cpp - Implement shared functions for transforms --=// |
| 2 | // |
| 3 | // This file defines shared functions used by the different components of the |
| 4 | // Transforms library. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "TransformInternals.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 9 | #include "llvm/Type.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 10 | #include "llvm/ConstantVals.h" |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/Expressions.h" |
Chris Lattner | 0ac5429 | 2002-04-09 19:08:28 +0000 | [diff] [blame] | 12 | #include "llvm/Function.h" |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 13 | #include "llvm/iOther.h" |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 15 | |
| 16 | // TargetData Hack: Eventually we will have annotations given to us by the |
| 17 | // backend so that we know stuff about type size and alignments. For now |
| 18 | // though, just use this, because it happens to match the model that GCC uses. |
| 19 | // |
| 20 | const TargetData TD("LevelRaise: Should be GCC though!"); |
| 21 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 22 | // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 23 | // with a value, then remove and delete the original instruction. |
| 24 | // |
| 25 | void ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 26 | BasicBlock::iterator &BI, Value *V) { |
| 27 | Instruction *I = *BI; |
| 28 | // Replaces all of the uses of the instruction with uses of the value |
| 29 | I->replaceAllUsesWith(V); |
| 30 | |
| 31 | // Remove the unneccesary instruction now... |
| 32 | BIL.remove(BI); |
| 33 | |
| 34 | // Make sure to propogate a name if there is one already... |
| 35 | if (I->hasName() && !V->hasName()) |
| 36 | V->setName(I->getName(), BIL.getParent()->getSymbolTable()); |
| 37 | |
| 38 | // Remove the dead instruction now... |
| 39 | delete I; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | // ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 44 | // instruction specified by I. The original instruction is deleted and BI is |
| 45 | // updated to point to the new instruction. |
| 46 | // |
| 47 | void ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 48 | BasicBlock::iterator &BI, Instruction *I) { |
| 49 | assert(I->getParent() == 0 && |
| 50 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 51 | |
| 52 | // Insert the new instruction into the basic block... |
Chris Lattner | c4db754 | 2002-03-11 22:21:04 +0000 | [diff] [blame] | 53 | BI = BIL.insert(BI, I)+1; // Increment BI to point to instruction to delete |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 54 | |
| 55 | // Replace all uses of the old instruction, and delete it. |
| 56 | ReplaceInstWithValue(BIL, BI, I); |
| 57 | |
Chris Lattner | c4db754 | 2002-03-11 22:21:04 +0000 | [diff] [blame] | 58 | // Move BI back to point to the newly inserted instruction |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 59 | --BI; |
| 60 | } |
| 61 | |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 62 | void ReplaceInstWithInst(Instruction *From, Instruction *To) { |
| 63 | BasicBlock *BB = From->getParent(); |
| 64 | BasicBlock::InstListType &BIL = BB->getInstList(); |
| 65 | BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), From); |
| 66 | assert(BI != BIL.end() && "Inst not in it's parents BB!"); |
| 67 | ReplaceInstWithInst(BIL, BI, To); |
| 68 | } |
| 69 | |
Chris Lattner | 8e86542 | 2002-03-21 06:24:00 +0000 | [diff] [blame] | 70 | // InsertInstBeforeInst - Insert 'NewInst' into the basic block that 'Existing' |
| 71 | // is already in, and put it right before 'Existing'. This instruction should |
| 72 | // only be used when there is no iterator to Existing already around. The |
| 73 | // returned iterator points to the new instruction. |
| 74 | // |
| 75 | BasicBlock::iterator InsertInstBeforeInst(Instruction *NewInst, |
| 76 | Instruction *Existing) { |
| 77 | BasicBlock *BB = Existing->getParent(); |
| 78 | BasicBlock::InstListType &BIL = BB->getInstList(); |
| 79 | BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), Existing); |
| 80 | assert(BI != BIL.end() && "Inst not in it's parents BB!"); |
| 81 | return BIL.insert(BI, NewInst); |
| 82 | } |
| 83 | |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 86 | static const Type *getStructOffsetStep(const StructType *STy, unsigned &Offset, |
| 87 | std::vector<Value*> &Indices) { |
| 88 | assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!"); |
| 89 | const StructLayout *SL = TD.getStructLayout(STy); |
| 90 | |
| 91 | // This loop terminates always on a 0 <= i < MemberOffsets.size() |
| 92 | unsigned i; |
| 93 | for (i = 0; i < SL->MemberOffsets.size()-1; ++i) |
| 94 | if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1]) |
| 95 | break; |
| 96 | |
| 97 | assert(Offset >= SL->MemberOffsets[i] && |
| 98 | (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1])); |
| 99 | |
| 100 | // Make sure to save the current index... |
| 101 | Indices.push_back(ConstantUInt::get(Type::UByteTy, i)); |
| 102 | Offset = SL->MemberOffsets[i]; |
| 103 | return STy->getContainedType(i); |
| 104 | } |
| 105 | |
| 106 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 107 | // getStructOffsetType - Return a vector of offsets that are to be used to index |
| 108 | // into the specified struct type to get as close as possible to index as we |
| 109 | // can. Note that it is possible that we cannot get exactly to Offset, in which |
| 110 | // case we update offset to be the offset we actually obtained. The resultant |
| 111 | // leaf type is returned. |
| 112 | // |
| 113 | // If StopEarly is set to true (the default), the first object with the |
| 114 | // specified type is returned, even if it is a struct type itself. In this |
| 115 | // case, this routine will not drill down to the leaf type. Set StopEarly to |
| 116 | // false if you want a leaf |
| 117 | // |
| 118 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 119 | std::vector<Value*> &Indices, |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 120 | bool StopEarly = true) { |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 121 | if (Offset == 0 && StopEarly && !Indices.empty()) |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 122 | return Ty; // Return the leaf type |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 124 | unsigned ThisOffset; |
| 125 | const Type *NextType; |
| 126 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 127 | ThisOffset = Offset; |
| 128 | NextType = getStructOffsetStep(STy, ThisOffset, Indices); |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 129 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 130 | assert(Offset < TD.getTypeSize(ATy) && "Offset not in composite!"); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 132 | NextType = ATy->getElementType(); |
| 133 | unsigned ChildSize = TD.getTypeSize(NextType); |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 134 | Indices.push_back(ConstantUInt::get(Type::UIntTy, Offset/ChildSize)); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 135 | ThisOffset = (Offset/ChildSize)*ChildSize; |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 136 | } else { |
| 137 | Offset = 0; // Return the offset that we were able to acheive |
| 138 | return Ty; // Return the leaf type |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 141 | unsigned SubOffs = Offset - ThisOffset; |
Chris Lattner | 4736d06 | 2002-03-07 21:18:00 +0000 | [diff] [blame] | 142 | const Type *LeafTy = getStructOffsetType(NextType, SubOffs, |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 143 | Indices, StopEarly); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 144 | Offset = ThisOffset + SubOffs; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 145 | return LeafTy; |
| 146 | } |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 147 | |
| 148 | // ConvertableToGEP - This function returns true if the specified value V is |
| 149 | // a valid index into a pointer of type Ty. If it is valid, Idx is filled in |
| 150 | // with the values that would be appropriate to make this a getelementptr |
| 151 | // instruction. The type returned is the root type that the GEP would point to |
| 152 | // |
| 153 | const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 154 | std::vector<Value*> &Indices, |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 155 | BasicBlock::iterator *BI = 0) { |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 156 | const CompositeType *CompTy = dyn_cast<CompositeType>(Ty); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 157 | if (CompTy == 0) return 0; |
| 158 | |
| 159 | // See if the cast is of an integer expression that is either a constant, |
| 160 | // or a value scaled by some amount with a possible offset. |
| 161 | // |
| 162 | analysis::ExprType Expr = analysis::ClassifyExpression(OffsetVal); |
| 163 | |
Chris Lattner | 169bffe | 2002-04-16 22:10:36 +0000 | [diff] [blame^] | 164 | // Get the offset and scale values if they exists... |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 165 | // A scale of zero with Expr.Var != 0 means a scale of 1. |
| 166 | // |
Chris Lattner | 169bffe | 2002-04-16 22:10:36 +0000 | [diff] [blame^] | 167 | int Offset = Expr.Offset ? getConstantValue(Expr.Offset) : 0; |
| 168 | int Scale = Expr.Scale ? getConstantValue(Expr.Scale) : 0; |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 170 | if (Expr.Var && Scale == 0) Scale = 1; // Scale != 0 if Expr.Var != 0 |
| 171 | |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 172 | // Loop over the Scale and Offset values, filling in the Indices vector for |
| 173 | // our final getelementptr instruction. |
| 174 | // |
| 175 | const Type *NextTy = CompTy; |
| 176 | do { |
| 177 | if (!isa<CompositeType>(NextTy)) |
| 178 | return 0; // Type must not be ready for processing... |
| 179 | CompTy = cast<CompositeType>(NextTy); |
| 180 | |
| 181 | if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) { |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 182 | // Step into the appropriate element of the structure... |
Chris Lattner | 169bffe | 2002-04-16 22:10:36 +0000 | [diff] [blame^] | 183 | unsigned ActualOffset = (Offset < 0) ? 0 : (unsigned)Offset; |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 184 | NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 185 | Offset -= ActualOffset; |
| 186 | } else { |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 187 | const Type *ElTy = cast<SequentialType>(CompTy)->getElementType(); |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 188 | if (!ElTy->isSized()) |
| 189 | return 0; // Type is unreasonable... escape! |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 190 | unsigned ElSize = TD.getTypeSize(ElTy); |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 191 | int ElSizeS = (int)ElSize; |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 192 | |
| 193 | // See if the user is indexing into a different cell of this array... |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 194 | if (Scale && (Scale >= ElSizeS || -Scale >= ElSizeS)) { |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 195 | // A scale n*ElSize might occur if we are not stepping through |
| 196 | // array by one. In this case, we will have to insert math to munge |
| 197 | // the index. |
| 198 | // |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 199 | int ScaleAmt = Scale/ElSizeS; |
| 200 | if (Scale-ScaleAmt*ElSizeS) |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 201 | return 0; // Didn't scale by a multiple of element size, bail out |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 202 | Scale = 0; // Scale is consumed |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 203 | |
Chris Lattner | ca180c7 | 2002-04-09 20:53:36 +0000 | [diff] [blame] | 204 | int Index = Offset/ElSize; // is zero unless Offset > ElSize |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 205 | Offset -= Index*ElSize; // Consume part of the offset |
| 206 | |
| 207 | if (BI) { // Generate code? |
| 208 | BasicBlock *BB = (**BI)->getParent(); |
| 209 | if (Expr.Var->getType() != Type::UIntTy) { |
| 210 | CastInst *IdxCast = new CastInst(Expr.Var, Type::UIntTy); |
| 211 | if (Expr.Var->hasName()) |
| 212 | IdxCast->setName(Expr.Var->getName()+"-idxcast"); |
| 213 | *BI = BB->getInstList().insert(*BI, IdxCast)+1; |
| 214 | Expr.Var = IdxCast; |
| 215 | } |
| 216 | |
Chris Lattner | 7c3f415 | 2001-12-07 04:26:02 +0000 | [diff] [blame] | 217 | if (ScaleAmt && ScaleAmt != 1) { |
| 218 | // If we have to scale up our index, do so now |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 219 | Value *ScaleAmtVal = ConstantUInt::get(Type::UIntTy, |
| 220 | (unsigned)ScaleAmt); |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 221 | Instruction *Scaler = BinaryOperator::create(Instruction::Mul, |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 222 | Expr.Var, ScaleAmtVal); |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 223 | if (Expr.Var->hasName()) |
| 224 | Scaler->setName(Expr.Var->getName()+"-scale"); |
| 225 | |
| 226 | *BI = BB->getInstList().insert(*BI, Scaler)+1; |
| 227 | Expr.Var = Scaler; |
| 228 | } |
| 229 | |
| 230 | if (Index) { // Add an offset to the index |
Chris Lattner | ca180c7 | 2002-04-09 20:53:36 +0000 | [diff] [blame] | 231 | Value *IndexAmt = ConstantUInt::get(Type::UIntTy, (unsigned)Index); |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 232 | Instruction *Offseter = BinaryOperator::create(Instruction::Add, |
| 233 | Expr.Var, IndexAmt); |
| 234 | if (Expr.Var->hasName()) |
| 235 | Offseter->setName(Expr.Var->getName()+"-offset"); |
| 236 | *BI = BB->getInstList().insert(*BI, Offseter)+1; |
| 237 | Expr.Var = Offseter; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | Indices.push_back(Expr.Var); |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 242 | Expr.Var = 0; |
Chris Lattner | ca180c7 | 2002-04-09 20:53:36 +0000 | [diff] [blame] | 243 | } else if (Offset >= (int)ElSize || -Offset >= (int)ElSize) { |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 244 | // Calculate the index that we are entering into the array cell with |
| 245 | unsigned Index = Offset/ElSize; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 246 | Indices.push_back(ConstantUInt::get(Type::UIntTy, Index)); |
Chris Lattner | ca180c7 | 2002-04-09 20:53:36 +0000 | [diff] [blame] | 247 | Offset -= (int)(Index*ElSize); // Consume part of the offset |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 754cf41 | 2002-03-14 16:37:04 +0000 | [diff] [blame] | 249 | } else if (isa<ArrayType>(CompTy) || Indices.empty()) { |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 250 | // Must be indexing a small amount into the first cell of the array |
| 251 | // Just index into element zero of the array here. |
| 252 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 253 | Indices.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 254 | } else { |
| 255 | return 0; // Hrm. wierd, can't handle this case. Bail |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 256 | } |
| 257 | NextTy = ElTy; |
| 258 | } |
| 259 | } while (Offset || Scale); // Go until we're done! |
| 260 | |
| 261 | return NextTy; |
| 262 | } |