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