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