Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===- TransformInternals.cpp - Implement shared functions for transforms -===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines shared functions used by the different components of the |
| 11 | // Transforms library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "TransformInternals.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 16 | #include "llvm/Type.h" |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Expressions.h" |
Chris Lattner | 0ac5429 | 2002-04-09 19:08:28 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 19 | #include "llvm/iOther.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 20 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | namespace llvm { |
| 22 | |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 23 | static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 24 | std::vector<Value*> &Indices, |
| 25 | const TargetData &TD) { |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 26 | assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!"); |
| 27 | const StructLayout *SL = TD.getStructLayout(STy); |
| 28 | |
| 29 | // This loop terminates always on a 0 <= i < MemberOffsets.size() |
| 30 | unsigned i; |
| 31 | for (i = 0; i < SL->MemberOffsets.size()-1; ++i) |
| 32 | if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1]) |
| 33 | break; |
| 34 | |
| 35 | assert(Offset >= SL->MemberOffsets[i] && |
| 36 | (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1])); |
| 37 | |
| 38 | // Make sure to save the current index... |
| 39 | Indices.push_back(ConstantUInt::get(Type::UByteTy, i)); |
| 40 | Offset = SL->MemberOffsets[i]; |
| 41 | return STy->getContainedType(i); |
| 42 | } |
| 43 | |
| 44 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 45 | // getStructOffsetType - Return a vector of offsets that are to be used to index |
| 46 | // into the specified struct type to get as close as possible to index as we |
| 47 | // can. Note that it is possible that we cannot get exactly to Offset, in which |
| 48 | // case we update offset to be the offset we actually obtained. The resultant |
| 49 | // leaf type is returned. |
| 50 | // |
| 51 | // If StopEarly is set to true (the default), the first object with the |
| 52 | // specified type is returned, even if it is a struct type itself. In this |
| 53 | // case, this routine will not drill down to the leaf type. Set StopEarly to |
| 54 | // false if you want a leaf |
| 55 | // |
| 56 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 57 | std::vector<Value*> &Indices, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 58 | const TargetData &TD, bool StopEarly) { |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 59 | if (Offset == 0 && StopEarly && !Indices.empty()) |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 60 | return Ty; // Return the leaf type |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 62 | uint64_t ThisOffset; |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 63 | const Type *NextType; |
| 64 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
Chris Lattner | 9493101 | 2003-10-17 18:03:54 +0000 | [diff] [blame] | 65 | if (STy->getElementTypes().empty()) { |
| 66 | Offset = 0; |
| 67 | return STy; |
| 68 | } |
| 69 | |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 70 | ThisOffset = Offset; |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 71 | NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD); |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 72 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
Chris Lattner | e32487e | 2003-06-07 21:45:42 +0000 | [diff] [blame] | 73 | assert(Offset == 0 || Offset < TD.getTypeSize(ATy) && |
| 74 | "Offset not in composite!"); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 76 | NextType = ATy->getElementType(); |
| 77 | unsigned ChildSize = TD.getTypeSize(NextType); |
Chris Lattner | 106ff45 | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 78 | Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize)); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 79 | ThisOffset = (Offset/ChildSize)*ChildSize; |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 80 | } else { |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 81 | Offset = 0; // Return the offset that we were able to achieve |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 82 | return Ty; // Return the leaf type |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 85 | unsigned SubOffs = Offset - ThisOffset; |
Chris Lattner | 4736d06 | 2002-03-07 21:18:00 +0000 | [diff] [blame] | 86 | const Type *LeafTy = getStructOffsetType(NextType, SubOffs, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 87 | Indices, TD, StopEarly); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 88 | Offset = ThisOffset + SubOffs; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 89 | return LeafTy; |
| 90 | } |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 91 | |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 92 | // ConvertibleToGEP - This function returns true if the specified value V is |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 93 | // a valid index into a pointer of type Ty. If it is valid, Idx is filled in |
| 94 | // with the values that would be appropriate to make this a getelementptr |
| 95 | // instruction. The type returned is the root type that the GEP would point to |
| 96 | // |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 97 | const Type *ConvertibleToGEP(const Type *Ty, Value *OffsetVal, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 98 | std::vector<Value*> &Indices, |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 99 | const TargetData &TD, |
Chris Lattner | 0c0edf8 | 2002-07-25 06:17:51 +0000 | [diff] [blame] | 100 | BasicBlock::iterator *BI) { |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 101 | const CompositeType *CompTy = dyn_cast<CompositeType>(Ty); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 102 | if (CompTy == 0) return 0; |
| 103 | |
| 104 | // See if the cast is of an integer expression that is either a constant, |
| 105 | // or a value scaled by some amount with a possible offset. |
| 106 | // |
Chris Lattner | 9a0a41f | 2003-12-23 08:04:08 +0000 | [diff] [blame] | 107 | ExprType Expr = ClassifyExpr(OffsetVal); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 169bffe | 2002-04-16 22:10:36 +0000 | [diff] [blame] | 109 | // Get the offset and scale values if they exists... |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 110 | // A scale of zero with Expr.Var != 0 means a scale of 1. |
| 111 | // |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 112 | int64_t Offset = Expr.Offset ? getConstantValue(Expr.Offset) : 0; |
| 113 | int64_t Scale = Expr.Scale ? getConstantValue(Expr.Scale) : 0; |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 115 | if (Expr.Var && Scale == 0) Scale = 1; // Scale != 0 if Expr.Var != 0 |
| 116 | |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 117 | // Loop over the Scale and Offset values, filling in the Indices vector for |
| 118 | // our final getelementptr instruction. |
| 119 | // |
| 120 | const Type *NextTy = CompTy; |
| 121 | do { |
| 122 | if (!isa<CompositeType>(NextTy)) |
| 123 | return 0; // Type must not be ready for processing... |
| 124 | CompTy = cast<CompositeType>(NextTy); |
| 125 | |
| 126 | if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) { |
Chris Lattner | 9e77f7e | 2002-03-21 06:27:20 +0000 | [diff] [blame] | 127 | // Step into the appropriate element of the structure... |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 128 | uint64_t ActualOffset = (Offset < 0) ? 0 : (uint64_t)Offset; |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 129 | NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices, TD); |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 130 | Offset -= ActualOffset; |
| 131 | } else { |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 132 | const Type *ElTy = cast<SequentialType>(CompTy)->getElementType(); |
Chris Lattner | e5fa63a | 2003-01-23 02:39:10 +0000 | [diff] [blame] | 133 | if (!ElTy->isSized() || (isa<PointerType>(CompTy) && !Indices.empty())) |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 134 | return 0; // Type is unreasonable... escape! |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 135 | unsigned ElSize = TD.getTypeSize(ElTy); |
Chris Lattner | 8a334a4 | 2003-06-23 17:36:49 +0000 | [diff] [blame] | 136 | if (ElSize == 0) return 0; // Avoid division by zero... |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 137 | int64_t ElSizeS = ElSize; |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 138 | |
| 139 | // 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] | 140 | if (Scale && (Scale >= ElSizeS || -Scale >= ElSizeS)) { |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 141 | // A scale n*ElSize might occur if we are not stepping through |
| 142 | // array by one. In this case, we will have to insert math to munge |
| 143 | // the index. |
| 144 | // |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 145 | int64_t ScaleAmt = Scale/ElSizeS; |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 146 | if (Scale-ScaleAmt*ElSizeS) |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 147 | 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] | 148 | Scale = 0; // Scale is consumed |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 150 | int64_t Index = Offset/ElSize; // is zero unless Offset > ElSize |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 151 | Offset -= Index*ElSize; // Consume part of the offset |
| 152 | |
| 153 | if (BI) { // Generate code? |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 154 | BasicBlock *BB = (*BI)->getParent(); |
Chris Lattner | 106ff45 | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 155 | if (Expr.Var->getType() != Type::LongTy) |
| 156 | Expr.Var = new CastInst(Expr.Var, Type::LongTy, |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 157 | Expr.Var->getName()+"-idxcast", *BI); |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 7c3f415 | 2001-12-07 04:26:02 +0000 | [diff] [blame] | 159 | if (ScaleAmt && ScaleAmt != 1) { |
| 160 | // If we have to scale up our index, do so now |
Chris Lattner | b698455 | 2002-10-02 18:53:14 +0000 | [diff] [blame] | 161 | Value *ScaleAmtVal = ConstantSInt::get(Type::LongTy, ScaleAmt); |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 162 | Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var, |
| 163 | ScaleAmtVal, |
| 164 | Expr.Var->getName()+"-scale",*BI); |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (Index) { // Add an offset to the index |
Chris Lattner | b698455 | 2002-10-02 18:53:14 +0000 | [diff] [blame] | 168 | Value *IndexAmt = ConstantSInt::get(Type::LongTy, Index); |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 169 | Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var, |
| 170 | IndexAmt, |
| 171 | Expr.Var->getName()+"-offset", |
| 172 | *BI); |
Chris Lattner | 2dc48bd | 2001-12-05 19:41:16 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | Indices.push_back(Expr.Var); |
Chris Lattner | 99fb91c | 2002-03-21 03:04:38 +0000 | [diff] [blame] | 177 | Expr.Var = 0; |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 178 | } else if (Offset >= (int64_t)ElSize || -Offset >= (int64_t)ElSize) { |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 179 | // Calculate the index that we are entering into the array cell with |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 180 | uint64_t Index = Offset/ElSize; |
Chris Lattner | 106ff45 | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 181 | Indices.push_back(ConstantSInt::get(Type::LongTy, Index)); |
Chris Lattner | 8e2e5f7 | 2002-09-16 18:32:33 +0000 | [diff] [blame] | 182 | Offset -= (int64_t)(Index*ElSize); // Consume part of the offset |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 754cf41 | 2002-03-14 16:37:04 +0000 | [diff] [blame] | 184 | } else if (isa<ArrayType>(CompTy) || Indices.empty()) { |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 185 | // Must be indexing a small amount into the first cell of the array |
| 186 | // Just index into element zero of the array here. |
| 187 | // |
Chris Lattner | 106ff45 | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 188 | Indices.push_back(ConstantSInt::get(Type::LongTy, 0)); |
Chris Lattner | 7991c28 | 2001-12-14 16:38:59 +0000 | [diff] [blame] | 189 | } else { |
| 190 | return 0; // Hrm. wierd, can't handle this case. Bail |
Chris Lattner | 89a1c80 | 2001-11-26 16:59:47 +0000 | [diff] [blame] | 191 | } |
| 192 | NextTy = ElTy; |
| 193 | } |
| 194 | } while (Offset || Scale); // Go until we're done! |
| 195 | |
| 196 | return NextTy; |
| 197 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 198 | |
| 199 | } // End llvm namespace |