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