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 | 497c60c | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 11 | #include "TransformInternals.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 | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 14 | #include "llvm/Pass.h" |
Chris Lattner | 968ddc9 | 2002-04-08 20:18:09 +0000 | [diff] [blame] | 15 | #include "llvm/ConstantHandling.h" |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/Expressions.h" |
Chris Lattner | 497c60c | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 18 | #include "Support/STLExtras.h" |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 19 | #include "Support/StatisticReporter.h" |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 21 | using std::cerr; |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 23 | static Statistic<> NumLoadStorePeepholes("raise\t\t- Number of load/store peepholes"); |
| 24 | static Statistic<> NumGEPInstFormed("raise\t\t- Number of other getelementptr's formed"); |
| 25 | static Statistic<> NumExprTreesConv("raise\t\t- Number of expression trees converted"); |
| 26 | static Statistic<> NumCastOfCast("raise\t\t- Number of cast-of-self removed"); |
| 27 | static Statistic<> NumDCEorCP("raise\t\t- Number of insts DCE'd or constprop'd"); |
| 28 | |
| 29 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 30 | #define PRINT_PEEPHOLE(ID, NUM, I) \ |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 31 | DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I) |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 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 | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 53 | // Peephole optimize the following instructions: |
| 54 | // %t1 = cast ? to x * |
| 55 | // %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand |
| 56 | // |
| 57 | // Into: %t3 = getelementptr {<...>} * %SP, <element indices> |
| 58 | // %t2 = cast <eltype> * %t3 to {<...>}* |
| 59 | // |
| 60 | static bool HandleCastToPointer(BasicBlock::iterator BI, |
| 61 | const PointerType *DestPTy) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 62 | CastInst &CI = cast<CastInst>(*BI); |
| 63 | if (CI.use_empty()) return false; |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 64 | |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 65 | // Scan all of the uses, looking for any uses that are not add |
| 66 | // instructions. If we have non-adds, do not make this transformation. |
| 67 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 68 | for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 69 | I != E; ++I) { |
| 70 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) { |
| 71 | if (BO->getOpcode() != Instruction::Add) |
| 72 | return false; |
| 73 | } else { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 78 | std::vector<Value*> Indices; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 79 | Value *Src = CI.getOperand(0); |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 80 | const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI); |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 81 | if (Result == 0) return false; // Not convertable... |
| 82 | |
| 83 | PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI); |
| 84 | |
| 85 | // If we have a getelementptr capability... transform all of the |
| 86 | // add instruction uses into getelementptr's. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 87 | while (!CI.use_empty()) { |
| 88 | BinaryOperator *I = cast<BinaryOperator>(*CI.use_begin()); |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 89 | assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 && |
| 90 | "Use is not a valid add instruction!"); |
| 91 | |
| 92 | // Get the value added to the cast result pointer... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 93 | Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0); |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 45ef5c2 | 2002-03-21 06:22:23 +0000 | [diff] [blame] | 95 | Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName()); |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 96 | PRINT_PEEPHOLE1("cast-add-to-gep:i", I); |
Chris Lattner | 45ef5c2 | 2002-03-21 06:22:23 +0000 | [diff] [blame] | 97 | |
| 98 | if (GEP->getType() == I->getType()) { |
| 99 | // Replace the old add instruction with the shiny new GEP inst |
| 100 | ReplaceInstWithInst(I, GEP); |
| 101 | } else { |
| 102 | // If the type produced by the gep instruction differs from the original |
| 103 | // add instruction type, insert a cast now. |
| 104 | // |
| 105 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 106 | // Insert the GEP instruction before the old add instruction... |
| 107 | I->getParent()->getInstList().insert(I, GEP); |
Chris Lattner | 45ef5c2 | 2002-03-21 06:22:23 +0000 | [diff] [blame] | 108 | |
| 109 | PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 110 | GEP = new CastInst(GEP, I->getType()); |
Chris Lattner | 45ef5c2 | 2002-03-21 06:22:23 +0000 | [diff] [blame] | 111 | |
| 112 | // Replace the old add instruction with the shiny new GEP inst |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 113 | ReplaceInstWithInst(I, GEP); |
Chris Lattner | 45ef5c2 | 2002-03-21 06:22:23 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 116 | PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP); |
| 117 | } |
| 118 | return true; |
| 119 | } |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 120 | |
| 121 | // Peephole optimize the following instructions: |
| 122 | // %t1 = cast ulong <const int> to {<...>} * |
| 123 | // %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand |
| 124 | // |
| 125 | // or |
| 126 | // %t1 = cast {<...>}* %SP to int* |
| 127 | // %t5 = cast ulong <const int> to int* |
| 128 | // %t2 = add int* %t1, %t5 ;; int is same size as field |
| 129 | // |
| 130 | // Into: %t3 = getelementptr {<...>} * %SP, <element indices> |
| 131 | // %t2 = cast <eltype> * %t3 to {<...>}* |
| 132 | // |
| 133 | static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI, |
| 134 | Value *AddOp1, CastInst *AddOp2) { |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 135 | const CompositeType *CompTy; |
| 136 | Value *OffsetVal = AddOp2->getOperand(0); |
| 137 | Value *SrcPtr; // Of type pointer to struct... |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 139 | if ((CompTy = getPointedToComposite(AddOp1->getType()))) { |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 140 | SrcPtr = AddOp1; // Handle the first case... |
| 141 | } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) { |
| 142 | SrcPtr = AddOp1c->getOperand(0); // Handle the second case... |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 143 | CompTy = getPointedToComposite(SrcPtr->getType()); |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Only proceed if we have detected all of our conditions successfully... |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 147 | if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral()) |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 148 | return false; |
| 149 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 150 | std::vector<Value*> Indices; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 151 | if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI)) |
| 152 | return false; // Not convertable... perhaps next time |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 154 | if (getPointedToComposite(AddOp1->getType())) { // case 1 |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 155 | PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI); |
| 156 | } else { |
| 157 | PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI); |
| 158 | } |
| 159 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 160 | GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices, |
| 161 | AddOp2->getName()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 162 | BI = ++BB->getInstList().insert(BI, GEP); |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 164 | Instruction *NCI = new CastInst(GEP, AddOp1->getType()); |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 165 | ReplaceInstWithInst(BB->getInstList(), BI, NCI); |
| 166 | PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI); |
| 167 | return true; |
| 168 | } |
| 169 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 170 | static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 171 | Instruction *I = BI; |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 172 | |
| 173 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 174 | Value *Src = CI->getOperand(0); |
| 175 | Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source |
| 176 | const Type *DestTy = CI->getType(); |
| 177 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 178 | // Peephole optimize the following instruction: |
| 179 | // %V2 = cast <ty> %V to <ty> |
| 180 | // |
| 181 | // Into: <nothing> |
| 182 | // |
| 183 | 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] | 184 | PRINT_PEEPHOLE1("cast-of-self-ty", CI); |
| 185 | CI->replaceAllUsesWith(Src); |
| 186 | if (!Src->hasName() && CI->hasName()) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 187 | std::string Name = CI->getName(); |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 188 | CI->setName(""); |
| 189 | Src->setName(Name, BB->getParent()->getSymbolTable()); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 191 | |
| 192 | // DCE the instruction now, to avoid having the iterative version of DCE |
| 193 | // have to worry about it. |
| 194 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 195 | BI = BB->getInstList().erase(BI); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 196 | |
| 197 | ++NumCastOfCast; |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 201 | // Check to see if it's a cast of an instruction that does not depend on the |
| 202 | // specific type of the operands to do it's job. |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 203 | if (!isReinterpretingCast(CI)) { |
Chris Lattner | b980e18 | 2001-11-04 21:32:11 +0000 | [diff] [blame] | 204 | ValueTypeCache ConvertedTypes; |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 205 | |
Chris Lattner | d20a98e | 2002-05-24 20:41:51 +0000 | [diff] [blame] | 206 | // Check to see if we can convert the source of the cast to match the |
| 207 | // destination type of the cast... |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 208 | // |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 209 | ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 210 | if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) { |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 211 | PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent()); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 212 | |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 213 | DEBUG(cerr << "\nCONVERTING SRC EXPR TYPE:\n"); |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 214 | ValueMapCache ValueMap; |
| 215 | Value *E = ConvertExpressionToType(Src, DestTy, ValueMap); |
| 216 | if (Constant *CPV = dyn_cast<Constant>(E)) |
| 217 | CI->replaceAllUsesWith(CPV); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 218 | |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 219 | BI = BB->begin(); // Rescan basic block. BI might be invalidated. |
| 220 | PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E); |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 221 | DEBUG(cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent()); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 222 | ++NumExprTreesConv; |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | |
Chris Lattner | d20a98e | 2002-05-24 20:41:51 +0000 | [diff] [blame] | 226 | // Check to see if we can convert the users of the cast value to match the |
| 227 | // source type of the cast... |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 228 | // |
| 229 | ConvertedTypes.clear(); |
Chris Lattner | a66c7bf | 2002-07-16 18:12:55 +0000 | [diff] [blame^] | 230 | ConvertedTypes[Src] = Src->getType(); // Make sure the source doesn't change type |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 231 | if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) { |
| 232 | PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent()); |
| 233 | |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 234 | DEBUG(cerr << "\nCONVERTING EXPR TYPE:\n"); |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 235 | ValueMapCache ValueMap; |
| 236 | ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI! |
| 237 | |
| 238 | BI = BB->begin(); // Rescan basic block. BI might be invalidated. |
| 239 | PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src); |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 240 | DEBUG(cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent()); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 241 | ++NumExprTreesConv; |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 242 | return true; |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | f17a09d | 2001-12-06 18:06:13 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Otherwise find out it this cast is a cast to a pointer type, which is |
| 247 | // then added to some other pointer, then loaded or stored through. If |
| 248 | // so, convert the add into a getelementptr instruction... |
| 249 | // |
| 250 | if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) { |
| 251 | if (HandleCastToPointer(BI, DestPTy)) { |
| 252 | BI = BB->begin(); // Rescan basic block. BI might be invalidated. |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 253 | ++NumGEPInstFormed; |
Chris Lattner | f17a09d | 2001-12-06 18:06:13 +0000 | [diff] [blame] | 254 | return true; |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 255 | } |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 258 | // Check to see if we are casting from a structure pointer to a pointer to |
| 259 | // the first element of the structure... to avoid munching other peepholes, |
| 260 | // we only let this happen if there are no add uses of the cast. |
| 261 | // |
| 262 | // Peephole optimize the following instructions: |
| 263 | // %t1 = cast {<...>} * %StructPtr to <ty> * |
| 264 | // |
| 265 | // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...> |
| 266 | // %t1 = cast <eltype> * %t1 to <ty> * |
| 267 | // |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 268 | if (const CompositeType *CTy = getPointedToComposite(Src->getType())) |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 269 | if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) { |
| 270 | |
| 271 | // Loop over uses of the cast, checking for add instructions. If an add |
| 272 | // exists, this is probably a part of a more complex GEP, so we don't |
| 273 | // want to mess around with the cast. |
| 274 | // |
| 275 | bool HasAddUse = false; |
| 276 | for (Value::use_iterator I = CI->use_begin(), E = CI->use_end(); |
| 277 | I != E; ++I) |
| 278 | if (isa<Instruction>(*I) && |
| 279 | cast<Instruction>(*I)->getOpcode() == Instruction::Add) { |
| 280 | HasAddUse = true; break; |
| 281 | } |
| 282 | |
| 283 | // If it doesn't have an add use, check to see if the dest type is |
| 284 | // losslessly convertable to one of the types in the start of the struct |
| 285 | // type. |
| 286 | // |
| 287 | if (!HasAddUse) { |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 288 | const Type *DestPointedTy = DestPTy->getElementType(); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 289 | unsigned Depth = 1; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 290 | const CompositeType *CurCTy = CTy; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 291 | const Type *ElTy = 0; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 292 | |
| 293 | // Build the index vector, full of all zeros |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 294 | std::vector<Value*> Indices; |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 295 | Indices.push_back(ConstantUInt::get(Type::UIntTy, 0)); |
| 296 | while (CurCTy && !isa<PointerType>(CurCTy)) { |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 297 | if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) { |
| 298 | // Check for a zero element struct type... if we have one, bail. |
| 299 | if (CurSTy->getElementTypes().size() == 0) break; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 301 | // Grab the first element of the struct type, which must lie at |
| 302 | // offset zero in the struct. |
| 303 | // |
| 304 | ElTy = CurSTy->getElementTypes()[0]; |
| 305 | } else { |
| 306 | ElTy = cast<ArrayType>(CurCTy)->getElementType(); |
| 307 | } |
| 308 | |
| 309 | // Insert a zero to index through this type... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 310 | Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0)); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 311 | |
| 312 | // Did we find what we're looking for? |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 313 | if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 314 | |
| 315 | // Nope, go a level deeper. |
| 316 | ++Depth; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 317 | CurCTy = dyn_cast<CompositeType>(ElTy); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 318 | ElTy = 0; |
| 319 | } |
| 320 | |
| 321 | // Did we find what we were looking for? If so, do the transformation |
| 322 | if (ElTy) { |
| 323 | PRINT_PEEPHOLE1("cast-for-first:in", CI); |
| 324 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 325 | // Insert the new T cast instruction... stealing old T's name |
| 326 | GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices, |
| 327 | CI->getName()); |
| 328 | CI->setName(""); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 329 | BI = ++BB->getInstList().insert(BI, GEP); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 330 | |
| 331 | // Make the old cast instruction reference the new GEP instead of |
| 332 | // the old src value. |
| 333 | // |
| 334 | CI->setOperand(0, GEP); |
| 335 | |
| 336 | PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 337 | ++NumGEPInstFormed; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 338 | return true; |
| 339 | } |
| 340 | } |
| 341 | } |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 343 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 344 | Value *Val = SI->getOperand(0); |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 345 | Value *Pointer = SI->getPointerOperand(); |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 346 | |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 347 | // Peephole optimize the following instructions: |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 348 | // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2 |
| 349 | // store <T2> %V, <T2>* %t |
| 350 | // |
| 351 | // Into: |
| 352 | // %t = cast <T2> %V to <T1> |
| 353 | // store <T1> %t2, <T1>* %P |
| 354 | // |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 355 | // Note: This is not taken care of by expr conversion because there might |
| 356 | // not be a cast available for the store to convert the incoming value of. |
| 357 | // This code is basically here to make sure that pointers don't have casts |
| 358 | // if possible. |
| 359 | // |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 360 | if (CastInst *CI = dyn_cast<CastInst>(Pointer)) |
| 361 | if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 362 | if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType())) |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 363 | // convertable types? |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 364 | if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) && |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 365 | !SI->hasIndices()) { // No subscripts yet! |
| 366 | PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI); |
| 367 | |
| 368 | // Insert the new T cast instruction... stealing old T's name |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 369 | CastInst *NCI = new CastInst(Val, CSPT->getElementType(), |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 370 | CI->getName()); |
| 371 | CI->setName(""); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 372 | BI = ++BB->getInstList().insert(BI, NCI); |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 373 | |
| 374 | // Replace the old store with a new one! |
| 375 | ReplaceInstWithInst(BB->getInstList(), BI, |
| 376 | SI = new StoreInst(NCI, CastSrc)); |
| 377 | PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 378 | ++NumLoadStorePeepholes; |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 379 | return true; |
| 380 | } |
| 381 | |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 382 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 383 | Value *Pointer = LI->getOperand(0); |
| 384 | const Type *PtrElType = |
| 385 | cast<PointerType>(Pointer->getType())->getElementType(); |
| 386 | |
| 387 | // Peephole optimize the following instructions: |
| 388 | // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertable to T2 |
| 389 | // %t = load <T2>* %P |
| 390 | // |
| 391 | // Into: |
| 392 | // %t = load <T1>* %P |
| 393 | // %Val = cast <T1> to <T2> |
| 394 | // |
| 395 | // Note: This is not taken care of by expr conversion because there might |
| 396 | // not be a cast available for the store to convert the incoming value of. |
| 397 | // This code is basically here to make sure that pointers don't have casts |
| 398 | // if possible. |
| 399 | // |
| 400 | if (CastInst *CI = dyn_cast<CastInst>(Pointer)) |
| 401 | if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 402 | if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType())) |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 403 | // convertable types? |
| 404 | if (PtrElType->isLosslesslyConvertableTo(CSPT->getElementType()) && |
| 405 | !LI->hasIndices()) { // No subscripts yet! |
| 406 | PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI); |
| 407 | |
| 408 | // Create the new load instruction... loading the pre-casted value |
| 409 | LoadInst *NewLI = new LoadInst(CastSrc, LI->getName()); |
| 410 | |
| 411 | // Insert the new T cast instruction... stealing old T's name |
| 412 | CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 413 | BI = ++BB->getInstList().insert(BI, NewLI); |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 414 | |
| 415 | // Replace the old store with a new one! |
| 416 | ReplaceInstWithInst(BB->getInstList(), BI, NCI); |
| 417 | PRINT_PEEPHOLE3("load-src-cast:out", NCI, CastSrc, NewLI); |
| 418 | ++NumLoadStorePeepholes; |
| 419 | return true; |
| 420 | } |
| 421 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 422 | } else if (I->getOpcode() == Instruction::Add && |
| 423 | isa<CastInst>(I->getOperand(1))) { |
| 424 | |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 425 | if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0), |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 426 | cast<CastInst>(I->getOperand(1)))) { |
| 427 | ++NumGEPInstFormed; |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 428 | return true; |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 429 | } |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 438 | static bool DoRaisePass(Function &F) { |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 439 | bool Changed = false; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 440 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 441 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 442 | DEBUG(cerr << "Processing: " << *BI); |
Chris Lattner | 16da494 | 2002-05-26 20:18:18 +0000 | [diff] [blame] | 443 | if (dceInstruction(BI) || doConstantPropogation(BI)) { |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 444 | Changed = true; |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 445 | ++NumDCEorCP; |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 446 | DEBUG(cerr << "***\t\t^^-- DeadCode Elinated!\n"); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 447 | } else if (PeepholeOptimize(BB, BI)) { |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 448 | Changed = true; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 449 | } else { |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 450 | ++BI; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 451 | } |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 452 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 453 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 454 | return Changed; |
| 455 | } |
| 456 | |
| 457 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 458 | // RaisePointerReferences::doit - Raise a function representation to a higher |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 459 | // level. |
| 460 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 461 | static bool doRPR(Function &F) { |
| 462 | DEBUG(cerr << "\n\n\nStarting to work on Function '" << F.getName() << "'\n"); |
Chris Lattner | 68b07b7 | 2001-11-01 07:00:51 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 464 | // Insert casts for all incoming pointer pointer values that are treated as |
| 465 | // arrays... |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 466 | // |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 467 | bool Changed = false, LocalChange; |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 4b770a3 | 2001-12-04 08:12:53 +0000 | [diff] [blame] | 469 | do { |
Chris Lattner | b3abf9d | 2002-05-22 17:27:12 +0000 | [diff] [blame] | 470 | DEBUG(cerr << "Looping: \n" << F); |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 471 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 472 | // Iterate over the function, refining it, until it converges on a stable |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 473 | // state |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 474 | LocalChange = false; |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 475 | while (DoRaisePass(F)) LocalChange = true; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 476 | Changed |= LocalChange; |
| 477 | |
| 478 | } while (LocalChange); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 479 | |
| 480 | return Changed; |
| 481 | } |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 482 | |
| 483 | namespace { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 484 | struct RaisePointerReferences : public FunctionPass { |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 485 | const char *getPassName() const { return "Raise Pointer References"; } |
| 486 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 487 | virtual bool runOnFunction(Function &F) { return doRPR(F); } |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 488 | |
| 489 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 490 | AU.preservesCFG(); |
| 491 | } |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 492 | }; |
| 493 | } |
| 494 | |
| 495 | Pass *createRaisePointerReferencesPass() { |
| 496 | return new RaisePointerReferences(); |
| 497 | } |
| 498 | |
| 499 | |