Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 1 | //===- LevelRaise.cpp - Code to change LLVM to higher level -----------------=// |
| 2 | // |
| 3 | // This file implements the 'raising' part of the LevelChange API. This is |
| 4 | // useful because, in general, it makes the LLVM code terser and easier to |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 5 | // analyze. |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Transforms/LevelChange.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 10 | #include "TransformInternals.h" |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 11 | #include "llvm/Method.h" |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 12 | #include "llvm/iOther.h" |
| 13 | #include "llvm/iMemory.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 14 | #include "llvm/ConstantVals.h" |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 15 | #include "llvm/Optimizations/ConstantHandling.h" |
Chris Lattner | 68b07b7 | 2001-11-01 07:00:51 +0000 | [diff] [blame] | 16 | #include "llvm/Optimizations/DCE.h" |
Chris Lattner | bd70bb9 | 2001-11-26 18:58:55 +0000 | [diff] [blame] | 17 | #include "llvm/Optimizations/ConstantProp.h" |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Expressions.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 19 | #include "Support/STLExtras.h" |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | |
| 22 | #include "llvm/Assembly/Writer.h" |
| 23 | |
Chris Lattner | 0900980 | 2001-11-26 19:36:58 +0000 | [diff] [blame] | 24 | //#define DEBUG_PEEPHOLE_INSTS 1 |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 25 | |
| 26 | #ifdef DEBUG_PEEPHOLE_INSTS |
| 27 | #define PRINT_PEEPHOLE(ID, NUM, I) \ |
| 28 | cerr << "Inst P/H " << ID << "[" << NUM << "] " << I; |
| 29 | #else |
| 30 | #define PRINT_PEEPHOLE(ID, NUM, I) |
| 31 | #endif |
| 32 | |
| 33 | #define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0) |
| 34 | #define PRINT_PEEPHOLE2(ID, I1, I2) \ |
| 35 | do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0) |
| 36 | #define PRINT_PEEPHOLE3(ID, I1, I2, I3) \ |
| 37 | do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \ |
| 38 | PRINT_PEEPHOLE(ID, 2, I3); } while (0) |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 39 | #define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \ |
| 40 | do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \ |
| 41 | PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0) |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 42 | |
| 43 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 44 | // isReinterpretingCast - Return true if the cast instruction specified will |
| 45 | // cause the operand to be "reinterpreted". A value is reinterpreted if the |
| 46 | // cast instruction would cause the underlying bits to change. |
| 47 | // |
| 48 | static inline bool isReinterpretingCast(const CastInst *CI) { |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 49 | return!CI->getOperand(0)->getType()->isLosslesslyConvertableTo(CI->getType()); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 53 | |
| 54 | |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 55 | |
| 56 | // Peephole optimize the following instructions: |
| 57 | // %t1 = cast ulong <const int> to {<...>} * |
| 58 | // %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand |
| 59 | // |
| 60 | // or |
| 61 | // %t1 = cast {<...>}* %SP to int* |
| 62 | // %t5 = cast ulong <const int> to int* |
| 63 | // %t2 = add int* %t1, %t5 ;; int is same size as field |
| 64 | // |
| 65 | // Into: %t3 = getelementptr {<...>} * %SP, <element indices> |
| 66 | // %t2 = cast <eltype> * %t3 to {<...>}* |
| 67 | // |
| 68 | static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI, |
| 69 | Value *AddOp1, CastInst *AddOp2) { |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 70 | const CompositeType *CompTy; |
| 71 | Value *OffsetVal = AddOp2->getOperand(0); |
| 72 | Value *SrcPtr; // Of type pointer to struct... |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 74 | if ((CompTy = getPointedToComposite(AddOp1->getType()))) { |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 75 | SrcPtr = AddOp1; // Handle the first case... |
| 76 | } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) { |
| 77 | SrcPtr = AddOp1c->getOperand(0); // Handle the second case... |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 78 | CompTy = getPointedToComposite(SrcPtr->getType()); |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Only proceed if we have detected all of our conditions successfully... |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 82 | if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral()) |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 83 | return false; |
| 84 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 85 | vector<Value*> Indices; |
| 86 | if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI)) |
| 87 | return false; // Not convertable... perhaps next time |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 89 | if (getPointedToComposite(AddOp1->getType())) { // case 1 |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 90 | PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI); |
| 91 | } else { |
| 92 | PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI); |
| 93 | } |
| 94 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 95 | GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices, |
| 96 | AddOp2->getName()); |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 97 | BI = BB->getInstList().insert(BI, GEP)+1; |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 99 | Instruction *NCI = new CastInst(GEP, AddOp1->getType()); |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 100 | ReplaceInstWithInst(BB->getInstList(), BI, NCI); |
| 101 | PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI); |
| 102 | return true; |
| 103 | } |
| 104 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 105 | static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { |
| 106 | Instruction *I = *BI; |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 107 | |
| 108 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 109 | Value *Src = CI->getOperand(0); |
| 110 | Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source |
| 111 | const Type *DestTy = CI->getType(); |
| 112 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 113 | // Peephole optimize the following instruction: |
| 114 | // %V2 = cast <ty> %V to <ty> |
| 115 | // |
| 116 | // Into: <nothing> |
| 117 | // |
| 118 | if (DestTy == Src->getType()) { // Check for a cast to same type as src!! |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 119 | PRINT_PEEPHOLE1("cast-of-self-ty", CI); |
| 120 | CI->replaceAllUsesWith(Src); |
| 121 | if (!Src->hasName() && CI->hasName()) { |
| 122 | string Name = CI->getName(); |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 123 | CI->setName(""); |
| 124 | Src->setName(Name, BB->getParent()->getSymbolTable()); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 129 | // Peephole optimize the following instructions: |
| 130 | // %tmp = cast <ty> %V to <ty2> |
| 131 | // %V = cast <ty2> %tmp to <ty3> ; Where ty & ty2 are same size |
| 132 | // |
| 133 | // Into: cast <ty> %V to <ty3> |
| 134 | // |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 135 | if (SrcI) |
| 136 | if (CastInst *CSrc = dyn_cast<CastInst>(SrcI)) |
| 137 | if (isReinterpretingCast(CI) + isReinterpretingCast(CSrc) < 2) { |
| 138 | // We can only do c-c elimination if, at most, one cast does a |
| 139 | // reinterpretation of the input data. |
| 140 | // |
| 141 | // If legal, make this cast refer the the original casts argument! |
| 142 | // |
| 143 | PRINT_PEEPHOLE2("cast-cast:in ", CI, CSrc); |
| 144 | CI->setOperand(0, CSrc->getOperand(0)); |
| 145 | PRINT_PEEPHOLE1("cast-cast:out", CI); |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | // Check to see if it's a cast of an instruction that does not depend on the |
| 150 | // specific type of the operands to do it's job. |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 151 | if (!isReinterpretingCast(CI)) { |
Chris Lattner | b980e18 | 2001-11-04 21:32:11 +0000 | [diff] [blame] | 152 | ValueTypeCache ConvertedTypes; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 153 | if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) { |
| 154 | PRINT_PEEPHOLE2("CAST-DEST-EXPR-CONV:in ", Src, CI); |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 156 | #ifdef DEBUG_PEEPHOLE_INSTS |
| 157 | cerr << "\nCONVERTING EXPR TYPE:\n"; |
| 158 | #endif |
Chris Lattner | b980e18 | 2001-11-04 21:32:11 +0000 | [diff] [blame] | 159 | ValueMapCache ValueMap; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 160 | ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI! |
Chris Lattner | e4f4d8c | 2001-11-05 18:30:53 +0000 | [diff] [blame] | 161 | |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 162 | BI = BB->begin(); // Rescan basic block. BI might be invalidated. |
Chris Lattner | e34443d | 2001-11-06 08:34:29 +0000 | [diff] [blame] | 163 | PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 164 | #ifdef DEBUG_PEEPHOLE_INSTS |
| 165 | cerr << "DONE CONVERTING EXPR TYPE: \n\n";// << BB->getParent(); |
| 166 | #endif |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 167 | return true; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 168 | } else { |
| 169 | ConvertedTypes.clear(); |
| 170 | if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) { |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 171 | PRINT_PEEPHOLE2("CAST-SRC-EXPR-CONV:in ", Src, CI); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 172 | |
| 173 | #ifdef DEBUG_PEEPHOLE_INSTS |
| 174 | cerr << "\nCONVERTING SRC EXPR TYPE:\n"; |
| 175 | #endif |
| 176 | ValueMapCache ValueMap; |
| 177 | Value *E = ConvertExpressionToType(Src, DestTy, ValueMap); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 178 | if (Constant *CPV = dyn_cast<Constant>(E)) |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 179 | CI->replaceAllUsesWith(CPV); |
| 180 | |
| 181 | BI = BB->begin(); // Rescan basic block. BI might be invalidated. |
| 182 | PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E); |
| 183 | #ifdef DEBUG_PEEPHOLE_INSTS |
| 184 | cerr << "DONE CONVERTING SRC EXPR TYPE: \n\n";// << BB->getParent(); |
| 185 | #endif |
| 186 | return true; |
| 187 | } |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 189 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 192 | // Check to see if we are casting from a structure pointer to a pointer to |
| 193 | // the first element of the structure... to avoid munching other peepholes, |
| 194 | // we only let this happen if there are no add uses of the cast. |
| 195 | // |
| 196 | // Peephole optimize the following instructions: |
| 197 | // %t1 = cast {<...>} * %StructPtr to <ty> * |
| 198 | // |
| 199 | // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...> |
| 200 | // %t1 = cast <eltype> * %t1 to <ty> * |
| 201 | // |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 202 | #if 1 |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 203 | if (const CompositeType *CTy = getPointedToComposite(Src->getType())) |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 204 | if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) { |
| 205 | |
| 206 | // Loop over uses of the cast, checking for add instructions. If an add |
| 207 | // exists, this is probably a part of a more complex GEP, so we don't |
| 208 | // want to mess around with the cast. |
| 209 | // |
| 210 | bool HasAddUse = false; |
| 211 | for (Value::use_iterator I = CI->use_begin(), E = CI->use_end(); |
| 212 | I != E; ++I) |
| 213 | if (isa<Instruction>(*I) && |
| 214 | cast<Instruction>(*I)->getOpcode() == Instruction::Add) { |
| 215 | HasAddUse = true; break; |
| 216 | } |
| 217 | |
| 218 | // If it doesn't have an add use, check to see if the dest type is |
| 219 | // losslessly convertable to one of the types in the start of the struct |
| 220 | // type. |
| 221 | // |
| 222 | if (!HasAddUse) { |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 223 | const Type *DestPointedTy = DestPTy->getElementType(); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 224 | unsigned Depth = 1; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 225 | const CompositeType *CurCTy = CTy; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 226 | const Type *ElTy = 0; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 227 | |
| 228 | // Build the index vector, full of all zeros |
| 229 | vector<Value*> Indices; |
| 230 | |
| 231 | while (CurCTy) { |
| 232 | if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) { |
| 233 | // Check for a zero element struct type... if we have one, bail. |
| 234 | if (CurSTy->getElementTypes().size() == 0) break; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 236 | // Grab the first element of the struct type, which must lie at |
| 237 | // offset zero in the struct. |
| 238 | // |
| 239 | ElTy = CurSTy->getElementTypes()[0]; |
| 240 | } else { |
| 241 | ElTy = cast<ArrayType>(CurCTy)->getElementType(); |
| 242 | } |
| 243 | |
| 244 | // Insert a zero to index through this type... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 245 | Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0)); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 246 | |
| 247 | // Did we find what we're looking for? |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 248 | if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 249 | |
| 250 | // Nope, go a level deeper. |
| 251 | ++Depth; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 252 | CurCTy = dyn_cast<CompositeType>(ElTy); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 253 | ElTy = 0; |
| 254 | } |
| 255 | |
| 256 | // Did we find what we were looking for? If so, do the transformation |
| 257 | if (ElTy) { |
| 258 | PRINT_PEEPHOLE1("cast-for-first:in", CI); |
| 259 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 260 | // Insert the new T cast instruction... stealing old T's name |
| 261 | GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices, |
| 262 | CI->getName()); |
| 263 | CI->setName(""); |
| 264 | BI = BB->getInstList().insert(BI, GEP)+1; |
| 265 | |
| 266 | // Make the old cast instruction reference the new GEP instead of |
| 267 | // the old src value. |
| 268 | // |
| 269 | CI->setOperand(0, GEP); |
| 270 | |
| 271 | PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI); |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | } |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 276 | #endif |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 3d775c3 | 2001-11-13 04:59:41 +0000 | [diff] [blame] | 278 | #if 1 |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 279 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 280 | Value *Val = SI->getOperand(0); |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 281 | Value *Pointer = SI->getPointerOperand(); |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 282 | |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 283 | // Peephole optimize the following instructions: |
| 284 | // %t1 = getelementptr {<...>} * %StructPtr, <element indices> |
| 285 | // store <elementty> %v, <elementty> * %t1 |
| 286 | // |
| 287 | // Into: store <elementty> %v, {<...>} * %StructPtr, <element indices> |
| 288 | // |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 289 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Pointer)) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 290 | // Append any indices that the store instruction has onto the end of the |
| 291 | // ones that the GEP is carrying... |
| 292 | // |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 293 | vector<Value*> Indices(GEP->copyIndices()); |
| 294 | Indices.insert(Indices.end(), SI->idx_begin(), SI->idx_end()); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 296 | PRINT_PEEPHOLE2("gep-store:in", GEP, SI); |
| 297 | ReplaceInstWithInst(BB->getInstList(), BI, |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 298 | SI = new StoreInst(Val, GEP->getPointerOperand(), |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 299 | Indices)); |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 300 | PRINT_PEEPHOLE1("gep-store:out", SI); |
| 301 | return true; |
| 302 | } |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 303 | |
| 304 | // Peephole optimize the following instructions: |
| 305 | // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2 |
| 306 | // store <T2> %V, <T2>* %t |
| 307 | // |
| 308 | // Into: |
| 309 | // %t = cast <T2> %V to <T1> |
| 310 | // store <T1> %t2, <T1>* %P |
| 311 | // |
| 312 | if (CastInst *CI = dyn_cast<CastInst>(Pointer)) |
| 313 | if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType |
| 314 | if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType())) |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 315 | // convertable types? |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 316 | if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) && |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 317 | !SI->hasIndices()) { // No subscripts yet! |
| 318 | PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI); |
| 319 | |
| 320 | // Insert the new T cast instruction... stealing old T's name |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 321 | CastInst *NCI = new CastInst(Val, CSPT->getElementType(), |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 322 | CI->getName()); |
| 323 | CI->setName(""); |
| 324 | BI = BB->getInstList().insert(BI, NCI)+1; |
| 325 | |
| 326 | // Replace the old store with a new one! |
| 327 | ReplaceInstWithInst(BB->getInstList(), BI, |
| 328 | SI = new StoreInst(NCI, CastSrc)); |
| 329 | PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI); |
| 330 | return true; |
| 331 | } |
| 332 | |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 333 | |
| 334 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 335 | Value *Pointer = LI->getPointerOperand(); |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 336 | |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 337 | // Peephole optimize the following instructions: |
| 338 | // %t1 = getelementptr {<...>} * %StructPtr, <element indices> |
| 339 | // %V = load <elementty> * %t1 |
| 340 | // |
| 341 | // Into: load {<...>} * %StructPtr, <element indices> |
| 342 | // |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 343 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Pointer)) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 344 | // Append any indices that the load instruction has onto the end of the |
| 345 | // ones that the GEP is carrying... |
| 346 | // |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 347 | vector<Value*> Indices(GEP->copyIndices()); |
| 348 | Indices.insert(Indices.end(), LI->idx_begin(), LI->idx_end()); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 350 | PRINT_PEEPHOLE2("gep-load:in", GEP, LI); |
| 351 | ReplaceInstWithInst(BB->getInstList(), BI, |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 352 | LI = new LoadInst(GEP->getPointerOperand(), |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 353 | Indices)); |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 354 | PRINT_PEEPHOLE1("gep-load:out", LI); |
| 355 | return true; |
| 356 | } |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 357 | |
| 358 | |
| 359 | // Peephole optimize the following instructions: |
| 360 | // %t1 = cast <ty> * %t0 to <ty2> * |
| 361 | // %V = load <ty2> * %t1 |
| 362 | // |
| 363 | // Into: %t1 = load <ty> * %t0 |
| 364 | // %V = cast <ty> %t1 to <ty2> |
| 365 | // |
| 366 | // The idea behind this transformation is that if the expression type |
| 367 | // conversion engine could not convert the cast into some other nice form, |
| 368 | // that there is something fundementally wrong with the current shape of |
| 369 | // the program. Move the cast through the load and try again. This will |
| 370 | // leave the original cast instruction, to presumably become dead. |
| 371 | // |
| 372 | if (CastInst *CI = dyn_cast<CastInst>(Pointer)) { |
| 373 | Value *SrcVal = CI->getOperand(0); |
| 374 | const PointerType *SrcTy = dyn_cast<PointerType>(SrcVal->getType()); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 375 | const Type *ElTy = SrcTy ? SrcTy->getElementType() : 0; |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 376 | |
| 377 | // Make sure that nothing will be lost in the new cast... |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 378 | if (!LI->hasIndices() && SrcTy && |
| 379 | ElTy->isLosslesslyConvertableTo(LI->getType())) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 380 | PRINT_PEEPHOLE2("CL-LoadCast:in ", CI, LI); |
| 381 | |
| 382 | string CName = CI->getName(); CI->setName(""); |
| 383 | LoadInst *NLI = new LoadInst(SrcVal, LI->getName()); |
| 384 | LI->setName(""); // Take over the old load's name |
| 385 | |
| 386 | // Insert the load before the old load |
| 387 | BI = BB->getInstList().insert(BI, NLI)+1; |
| 388 | |
| 389 | // Replace the old load with a new cast... |
| 390 | ReplaceInstWithInst(BB->getInstList(), BI, |
| 391 | CI = new CastInst(NLI, LI->getType(), CName)); |
| 392 | PRINT_PEEPHOLE2("CL-LoadCast:out", NLI, CI); |
| 393 | |
| 394 | return true; |
| 395 | } |
| 396 | } |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 397 | } else if (I->getOpcode() == Instruction::Add && |
| 398 | isa<CastInst>(I->getOperand(1))) { |
| 399 | |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 400 | if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0), |
| 401 | cast<CastInst>(I->getOperand(1)))) |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 402 | return true; |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 3d775c3 | 2001-11-13 04:59:41 +0000 | [diff] [blame] | 404 | #endif |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | static bool DoRaisePass(Method *M) { |
| 414 | bool Changed = false; |
| 415 | for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) { |
| 416 | BasicBlock *BB = *MI; |
| 417 | BasicBlock::InstListType &BIL = BB->getInstList(); |
| 418 | |
| 419 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { |
Chris Lattner | bd70bb9 | 2001-11-26 18:58:55 +0000 | [diff] [blame] | 420 | if (opt::DeadCodeElimination::dceInstruction(BIL, BI) || |
| 421 | opt::ConstantPropogation::doConstantPropogation(BB, BI)) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 422 | Changed = true; |
| 423 | #ifdef DEBUG_PEEPHOLE_INSTS |
| 424 | cerr << "DeadCode Elinated!\n"; |
| 425 | #endif |
| 426 | } else if (PeepholeOptimize(BB, BI)) |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 427 | Changed = true; |
| 428 | else |
| 429 | ++BI; |
| 430 | } |
| 431 | } |
| 432 | return Changed; |
| 433 | } |
| 434 | |
| 435 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 436 | |
| 437 | |
| 438 | // DoInsertArrayCast - If the argument value has a pointer type, and if the |
| 439 | // argument value is used as an array, insert a cast before the specified |
| 440 | // basic block iterator that casts the value to an array pointer. Return the |
| 441 | // new cast instruction (in the CastResult var), or null if no cast is inserted. |
| 442 | // |
| 443 | static bool DoInsertArrayCast(Value *V, BasicBlock *BB, |
| 444 | BasicBlock::iterator InsertBefore) { |
| 445 | const PointerType *ThePtrType = dyn_cast<PointerType>(V->getType()); |
| 446 | if (!ThePtrType) return false; |
| 447 | |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 448 | const Type *ElTy = ThePtrType->getElementType(); |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 449 | if (isa<MethodType>(ElTy) || isa<ArrayType>(ElTy)) return false; |
| 450 | |
| 451 | unsigned ElementSize = TD.getTypeSize(ElTy); |
| 452 | bool InsertCast = false; |
| 453 | |
| 454 | for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { |
| 455 | Instruction *Inst = cast<Instruction>(*I); |
| 456 | switch (Inst->getOpcode()) { |
| 457 | case Instruction::Cast: // There is already a cast instruction! |
| 458 | if (const PointerType *PT = dyn_cast<const PointerType>(Inst->getType())) |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 459 | if (const ArrayType *AT = dyn_cast<const ArrayType>(PT->getElementType())) |
| 460 | if (AT->getElementType() == ThePtrType->getElementType()) { |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 461 | // Cast already exists! Don't mess around with it. |
| 462 | return false; // No changes made to program though... |
| 463 | } |
| 464 | break; |
| 465 | case Instruction::Add: { // Analyze pointer arithmetic... |
| 466 | Value *OtherOp = Inst->getOperand(Inst->getOperand(0) == V); |
| 467 | analysis::ExprType Expr = analysis::ClassifyExpression(OtherOp); |
| 468 | |
| 469 | // This looks like array addressing iff: |
| 470 | // A. The constant of the index is larger than the size of the element |
| 471 | // type. |
| 472 | // B. The scale factor is >= the size of the type. |
| 473 | // |
| 474 | if (Expr.Offset && getConstantValue(Expr.Offset) >= (int)ElementSize) // A |
| 475 | InsertCast = true; |
| 476 | |
| 477 | if (Expr.Scale && getConstantValue(Expr.Scale) >= (int)ElementSize) // B |
| 478 | InsertCast = true; |
| 479 | |
| 480 | break; |
| 481 | } |
| 482 | default: break; // Not an interesting use... |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (!InsertCast) return false; // There is no reason to insert a cast! |
| 487 | |
| 488 | // Calculate the destination pointer type |
| 489 | const PointerType *DestTy = PointerType::get(ArrayType::get(ElTy)); |
| 490 | |
| 491 | // Check to make sure that all uses of the value can be converted over to use |
| 492 | // the newly typed value. |
| 493 | // |
| 494 | ValueTypeCache ConvertedTypes; |
| 495 | if (!ValueConvertableToType(V, DestTy, ConvertedTypes)) { |
| 496 | cerr << "FAILED to convert types of values for " << V << "\n"; |
| 497 | ConvertedTypes.clear(); |
| 498 | ValueConvertableToType(V, DestTy, ConvertedTypes); |
| 499 | return false; |
| 500 | } |
| 501 | ConvertedTypes.clear(); |
| 502 | |
| 503 | // Insert a cast! |
| 504 | CastInst *TheCast = |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 505 | new CastInst(Constant::getNullConstant(V->getType()), DestTy, V->getName()); |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 506 | BB->getInstList().insert(InsertBefore, TheCast); |
| 507 | |
| 508 | cerr << "Inserting cast for " << V << endl; |
| 509 | |
| 510 | // Convert users of the old value over to use the cast result... |
| 511 | ValueMapCache VMC; |
| 512 | ConvertValueToNewType(V, TheCast, VMC); |
| 513 | |
| 514 | // The cast is the only thing that is allowed to reference the value... |
| 515 | TheCast->setOperand(0, V); |
| 516 | |
| 517 | cerr << "Inserted ptr-array cast: " << TheCast; |
| 518 | return true; // Made a change! |
| 519 | } |
| 520 | |
| 521 | |
| 522 | // DoInsertArrayCasts - Loop over all "incoming" values in the specified method, |
| 523 | // inserting a cast for pointer values that are used as arrays. For our |
| 524 | // purposes, an incoming value is considered to be either a value that is |
| 525 | // either a method parameter, or a pointer returned from a function call. |
| 526 | // |
| 527 | static bool DoInsertArrayCasts(Method *M) { |
| 528 | assert(!M->isExternal() && "Can't handle external methods!"); |
| 529 | |
| 530 | // Insert casts for all arguments to the function... |
| 531 | bool Changed = false; |
| 532 | BasicBlock *CurBB = M->front(); |
| 533 | |
| 534 | for (Method::ArgumentListType::iterator AI = M->getArgumentList().begin(), |
| 535 | AE = M->getArgumentList().end(); AI != AE; ++AI) { |
| 536 | |
| 537 | Changed |= DoInsertArrayCast(*AI, CurBB, CurBB->begin()); |
| 538 | } |
| 539 | |
| 540 | // TODO: insert casts for alloca, malloc, and function call results. Also, |
| 541 | // look for pointers that already have casts, to add to the map. |
| 542 | |
| 543 | return Changed; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 549 | // RaisePointerReferences::doit - Raise a method representation to a higher |
| 550 | // level. |
| 551 | // |
| 552 | bool RaisePointerReferences::doit(Method *M) { |
| 553 | if (M->isExternal()) return false; |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 68b07b7 | 2001-11-01 07:00:51 +0000 | [diff] [blame] | 555 | #ifdef DEBUG_PEEPHOLE_INSTS |
| 556 | cerr << "\n\n\nStarting to work on Method '" << M->getName() << "'\n"; |
| 557 | #endif |
| 558 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 559 | // Insert casts for all incoming pointer pointer values that are treated as |
| 560 | // arrays... |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 561 | // |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 562 | bool Changed = false, LocalChange; |
| 563 | do { |
| 564 | LocalChange = DoInsertArrayCasts(M); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 566 | // Iterate over the method, refining it, until it converges on a stable |
| 567 | // state |
| 568 | while (DoRaisePass(M)) LocalChange = true; |
| 569 | Changed |= LocalChange; |
| 570 | |
| 571 | } while (LocalChange); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 572 | |
| 573 | return Changed; |
| 574 | } |