Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===- LevelRaise.cpp - Code to change LLVM to higher level ---------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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" |
Alkis Evlogimenos | eb62bc7 | 2004-07-29 12:17:34 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Chris Lattner | 497c60c | 2002-05-07 18:12:18 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | e799902 | 2003-12-23 07:43:38 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 3378a5b | 2002-07-16 23:49:24 +0000 | [diff] [blame] | 29 | // StartInst - This enables the -raise-start-inst=foo option to cause the level |
| 30 | // raising pass to start at instruction "foo", which is immensely useful for |
| 31 | // debugging! |
| 32 | // |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 33 | static cl::opt<std::string> |
| 34 | StartInst("raise-start-inst", cl::Hidden, cl::value_desc("inst name"), |
| 35 | cl::desc("Start raise pass at the instruction with the specified name")); |
Chris Lattner | 3378a5b | 2002-07-16 23:49:24 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 37 | static Statistic<> |
Chris Lattner | 6ee6bbe | 2002-10-01 22:38:37 +0000 | [diff] [blame] | 38 | NumLoadStorePeepholes("raise", "Number of load/store peepholes"); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 39 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 40 | static Statistic<> |
Chris Lattner | 6ee6bbe | 2002-10-01 22:38:37 +0000 | [diff] [blame] | 41 | NumGEPInstFormed("raise", "Number of other getelementptr's formed"); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 42 | |
| 43 | static Statistic<> |
Chris Lattner | 6ee6bbe | 2002-10-01 22:38:37 +0000 | [diff] [blame] | 44 | NumExprTreesConv("raise", "Number of expression trees converted"); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 45 | |
| 46 | static Statistic<> |
Chris Lattner | 6ee6bbe | 2002-10-01 22:38:37 +0000 | [diff] [blame] | 47 | NumCastOfCast("raise", "Number of cast-of-self removed"); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 48 | |
| 49 | static Statistic<> |
Chris Lattner | 6ee6bbe | 2002-10-01 22:38:37 +0000 | [diff] [blame] | 50 | NumDCEorCP("raise", "Number of insts DCEd or constprop'd"); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 61b92c0 | 2002-10-08 22:19:25 +0000 | [diff] [blame] | 52 | static Statistic<> |
| 53 | NumVarargCallChanges("raise", "Number of vararg call peepholes"); |
| 54 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 55 | #define PRINT_PEEPHOLE(ID, NUM, I) \ |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 56 | DOUT << "Inst P/H " << ID << "[" << NUM << "] " << I |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 57 | |
| 58 | #define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0) |
| 59 | #define PRINT_PEEPHOLE2(ID, I1, I2) \ |
| 60 | do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0) |
| 61 | #define PRINT_PEEPHOLE3(ID, I1, I2, I3) \ |
| 62 | do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \ |
| 63 | PRINT_PEEPHOLE(ID, 2, I3); } while (0) |
Chris Lattner | d5b48ca | 2001-11-14 11:02:49 +0000 | [diff] [blame] | 64 | #define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \ |
| 65 | do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \ |
| 66 | PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0) |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 68 | namespace { |
| 69 | struct RPR : public FunctionPass { |
| 70 | virtual bool runOnFunction(Function &F); |
| 71 | |
| 72 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 73 | AU.setPreservesCFG(); |
| 74 | AU.addRequired<TargetData>(); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | bool DoRaisePass(Function &F); |
| 79 | bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI); |
| 80 | }; |
| 81 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 82 | RegisterPass<RPR> X("raise", "Raise Pointer References"); |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 85 | |
Chris Lattner | ded6d0c | 2004-09-20 04:43:57 +0000 | [diff] [blame] | 86 | FunctionPass *llvm::createRaisePointerReferencesPass() { |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 87 | return new RPR(); |
| 88 | } |
| 89 | |
| 90 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 91 | // isReinterpretingCast - Return true if the cast instruction specified will |
| 92 | // cause the operand to be "reinterpreted". A value is reinterpreted if the |
| 93 | // cast instruction would cause the underlying bits to change. |
| 94 | // |
| 95 | static inline bool isReinterpretingCast(const CastInst *CI) { |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 96 | return!CI->getOperand(0)->getType()->isLosslesslyConvertibleTo(CI->getType()); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 99 | bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 100 | Instruction *I = BI; |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 101 | const TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 102 | |
| 103 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 104 | Value *Src = CI->getOperand(0); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 105 | const Type *DestTy = CI->getType(); |
| 106 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 107 | // Peephole optimize the following instruction: |
| 108 | // %V2 = cast <ty> %V to <ty> |
| 109 | // |
| 110 | // Into: <nothing> |
| 111 | // |
| 112 | if (DestTy == Src->getType()) { // Check for a cast to same type as src!! |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 113 | PRINT_PEEPHOLE1("cast-of-self-ty", *CI); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 114 | CI->replaceAllUsesWith(Src); |
| 115 | if (!Src->hasName() && CI->hasName()) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 116 | std::string Name = CI->getName(); |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 117 | CI->setName(""); |
Chris Lattner | 7acff25 | 2005-03-05 19:05:20 +0000 | [diff] [blame] | 118 | Src->setName(Name); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 119 | } |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 120 | |
| 121 | // DCE the instruction now, to avoid having the iterative version of DCE |
| 122 | // have to worry about it. |
| 123 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 124 | BI = BB->getInstList().erase(BI); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 125 | |
| 126 | ++NumCastOfCast; |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 130 | // Check to see if it's a cast of an instruction that does not depend on the |
| 131 | // specific type of the operands to do it's job. |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 132 | if (!isReinterpretingCast(CI)) { |
Chris Lattner | b980e18 | 2001-11-04 21:32:11 +0000 | [diff] [blame] | 133 | ValueTypeCache ConvertedTypes; |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 134 | |
Chris Lattner | d20a98e | 2002-05-24 20:41:51 +0000 | [diff] [blame] | 135 | // Check to see if we can convert the source of the cast to match the |
| 136 | // destination type of the cast... |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 137 | // |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 138 | ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 139 | if (ExpressionConvertibleToType(Src, DestTy, ConvertedTypes, TD)) { |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 140 | PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", *Src, *CI, *BB->getParent()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 141 | |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 142 | DOUT << "\nCONVERTING SRC EXPR TYPE:\n"; |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 143 | { // ValueMap must be destroyed before function verified! |
| 144 | ValueMapCache ValueMap; |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 145 | Value *E = ConvertExpressionToType(Src, DestTy, ValueMap, TD); |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame] | 146 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 147 | if (Constant *CPV = dyn_cast<Constant>(E)) |
| 148 | CI->replaceAllUsesWith(CPV); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 150 | PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", *E); |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 151 | DOUT << "DONE CONVERTING SRC EXPR TYPE: \n" |
| 152 | << *BB->getParent(); |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 153 | } |
Chris Lattner | 3378a5b | 2002-07-16 23:49:24 +0000 | [diff] [blame] | 154 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 155 | BI = BB->begin(); // Rescan basic block. BI might be invalidated. |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 156 | ++NumExprTreesConv; |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
Chris Lattner | d20a98e | 2002-05-24 20:41:51 +0000 | [diff] [blame] | 160 | // Check to see if we can convert the users of the cast value to match the |
| 161 | // source type of the cast... |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 162 | // |
| 163 | ConvertedTypes.clear(); |
Chris Lattner | 61b92c0 | 2002-10-08 22:19:25 +0000 | [diff] [blame] | 164 | // Make sure the source doesn't change type |
| 165 | ConvertedTypes[Src] = Src->getType(); |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 166 | if (ValueConvertibleToType(CI, Src->getType(), ConvertedTypes, TD)) { |
Chris Lattner | 5e2e272 | 2004-08-08 01:27:56 +0000 | [diff] [blame] | 167 | //PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", *Src, *CI, |
| 168 | // *BB->getParent()); |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 169 | |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 170 | DOUT << "\nCONVERTING EXPR TYPE:\n"; |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 171 | { // ValueMap must be destroyed before function verified! |
| 172 | ValueMapCache ValueMap; |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 173 | ConvertValueToNewType(CI, Src, ValueMap, TD); // This will delete CI! |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 176 | PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", *Src); |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 177 | DOUT << "DONE CONVERTING EXPR TYPE: \n\n" << *BB->getParent(); |
Chris Lattner | 3378a5b | 2002-07-16 23:49:24 +0000 | [diff] [blame] | 178 | |
Chris Lattner | b1b4262 | 2002-07-17 17:11:33 +0000 | [diff] [blame] | 179 | BI = BB->begin(); // Rescan basic block. BI might be invalidated. |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 180 | ++NumExprTreesConv; |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 181 | return true; |
Chris Lattner | f3b976e | 2001-11-04 20:21:12 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | f17a09d | 2001-12-06 18:06:13 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 185 | // Check to see if we are casting from a structure pointer to a pointer to |
| 186 | // the first element of the structure... to avoid munching other peepholes, |
| 187 | // we only let this happen if there are no add uses of the cast. |
| 188 | // |
| 189 | // Peephole optimize the following instructions: |
| 190 | // %t1 = cast {<...>} * %StructPtr to <ty> * |
| 191 | // |
| 192 | // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...> |
| 193 | // %t1 = cast <eltype> * %t1 to <ty> * |
| 194 | // |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 195 | if (const CompositeType *CTy = getPointedToComposite(Src->getType())) |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 196 | if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) { |
| 197 | |
| 198 | // Loop over uses of the cast, checking for add instructions. If an add |
| 199 | // exists, this is probably a part of a more complex GEP, so we don't |
| 200 | // want to mess around with the cast. |
| 201 | // |
| 202 | bool HasAddUse = false; |
| 203 | for (Value::use_iterator I = CI->use_begin(), E = CI->use_end(); |
| 204 | I != E; ++I) |
| 205 | if (isa<Instruction>(*I) && |
| 206 | cast<Instruction>(*I)->getOpcode() == Instruction::Add) { |
| 207 | HasAddUse = true; break; |
| 208 | } |
| 209 | |
| 210 | // 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] | 211 | // 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] | 212 | // type. |
| 213 | // |
| 214 | if (!HasAddUse) { |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 215 | const Type *DestPointedTy = DestPTy->getElementType(); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 216 | unsigned Depth = 1; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 217 | const CompositeType *CurCTy = CTy; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 218 | const Type *ElTy = 0; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 219 | |
| 220 | // Build the index vector, full of all zeros |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 221 | std::vector<Value*> Indices; |
Chris Lattner | 559d519 | 2004-01-09 05:53:38 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 223 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 224 | while (CurCTy && !isa<PointerType>(CurCTy)) { |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 225 | if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) { |
| 226 | // Check for a zero element struct type... if we have one, bail. |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 227 | if (CurSTy->getNumElements() == 0) break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 229 | // Grab the first element of the struct type, which must lie at |
| 230 | // offset zero in the struct. |
| 231 | // |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 232 | ElTy = CurSTy->getElementType(0); |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 233 | } else { |
Chris Lattner | 7d719c3 | 2005-01-19 16:16:35 +0000 | [diff] [blame] | 234 | ElTy = cast<SequentialType>(CurCTy)->getElementType(); |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // Insert a zero to index through this type... |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 238 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 239 | |
| 240 | // Did we find what we're looking for? |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 241 | if (ElTy->isLosslesslyConvertibleTo(DestPointedTy)) break; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 242 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 243 | // Nope, go a level deeper. |
| 244 | ++Depth; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 245 | CurCTy = dyn_cast<CompositeType>(ElTy); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 246 | ElTy = 0; |
| 247 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 248 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 249 | // Did we find what we were looking for? If so, do the transformation |
| 250 | if (ElTy) { |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 251 | PRINT_PEEPHOLE1("cast-for-first:in", *CI); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 253 | std::string Name = CI->getName(); CI->setName(""); |
| 254 | |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 255 | // Insert the new T cast instruction... stealing old T's name |
| 256 | GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices, |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 257 | Name, BI); |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 258 | |
| 259 | // Make the old cast instruction reference the new GEP instead of |
| 260 | // the old src value. |
| 261 | // |
| 262 | CI->setOperand(0, GEP); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 264 | PRINT_PEEPHOLE2("cast-for-first:out", *GEP, *CI); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 265 | ++NumGEPInstFormed; |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | } |
| 269 | } |
Chris Lattner | e99c66b | 2001-11-01 17:05:27 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 8d38e54 | 2001-11-01 03:12:34 +0000 | [diff] [blame] | 271 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 272 | Value *Val = SI->getOperand(0); |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 273 | Value *Pointer = SI->getPointerOperand(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 274 | |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 275 | // Peephole optimize the following instructions: |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 276 | // %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] | 277 | // store <T2> %V, <T2>* %t |
| 278 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 279 | // Into: |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 280 | // %t = cast <T2> %V to <T1> |
| 281 | // store <T1> %t2, <T1>* %P |
| 282 | // |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 283 | // Note: This is not taken care of by expr conversion because there might |
| 284 | // not be a cast available for the store to convert the incoming value of. |
| 285 | // This code is basically here to make sure that pointers don't have casts |
| 286 | // if possible. |
| 287 | // |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 288 | if (CastInst *CI = dyn_cast<CastInst>(Pointer)) |
| 289 | if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 290 | if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType())) |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 291 | // convertible types? |
| 292 | if (Val->getType()->isLosslesslyConvertibleTo(CSPT->getElementType())) { |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 293 | PRINT_PEEPHOLE3("st-src-cast:in ", *Pointer, *Val, *SI); |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 294 | |
| 295 | // Insert the new T cast instruction... stealing old T's name |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 296 | std::string Name(CI->getName()); CI->setName(""); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 297 | CastInst *NCI = new CastInst(Val, CSPT->getElementType(), |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 298 | Name, BI); |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 299 | |
| 300 | // Replace the old store with a new one! |
| 301 | ReplaceInstWithInst(BB->getInstList(), BI, |
| 302 | SI = new StoreInst(NCI, CastSrc)); |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 303 | PRINT_PEEPHOLE3("st-src-cast:out", *NCI, *CastSrc, *SI); |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 304 | ++NumLoadStorePeepholes; |
Chris Lattner | dedee7b | 2001-11-01 05:57:59 +0000 | [diff] [blame] | 305 | return true; |
| 306 | } |
| 307 | |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 308 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 309 | Value *Pointer = LI->getOperand(0); |
| 310 | const Type *PtrElType = |
| 311 | cast<PointerType>(Pointer->getType())->getElementType(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 312 | |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 313 | // Peephole optimize the following instructions: |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 314 | // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertible to T2 |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 315 | // %t = load <T2>* %P |
| 316 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 317 | // Into: |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 318 | // %t = load <T1>* %P |
| 319 | // %Val = cast <T1> to <T2> |
| 320 | // |
| 321 | // Note: This is not taken care of by expr conversion because there might |
| 322 | // not be a cast available for the store to convert the incoming value of. |
| 323 | // This code is basically here to make sure that pointers don't have casts |
| 324 | // if possible. |
| 325 | // |
| 326 | if (CastInst *CI = dyn_cast<CastInst>(Pointer)) |
| 327 | if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 328 | if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType())) |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 329 | // convertible types? |
| 330 | if (PtrElType->isLosslesslyConvertibleTo(CSPT->getElementType())) { |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 331 | PRINT_PEEPHOLE2("load-src-cast:in ", *Pointer, *LI); |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 332 | |
| 333 | // Create the new load instruction... loading the pre-casted value |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 334 | LoadInst *NewLI = new LoadInst(CastSrc, LI->getName(), BI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 335 | |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 336 | // Insert the new T cast instruction... stealing old T's name |
| 337 | CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName()); |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 338 | |
| 339 | // Replace the old store with a new one! |
| 340 | ReplaceInstWithInst(BB->getInstList(), BI, NCI); |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 341 | PRINT_PEEPHOLE3("load-src-cast:out", *NCI, *CastSrc, *NewLI); |
Chris Lattner | c99428f | 2002-05-14 05:23:45 +0000 | [diff] [blame] | 342 | ++NumLoadStorePeepholes; |
| 343 | return true; |
| 344 | } |
| 345 | |
Chris Lattner | 61b92c0 | 2002-10-08 22:19:25 +0000 | [diff] [blame] | 346 | } else if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 347 | // If we have a call with all varargs arguments, convert the call to use the |
| 348 | // actual argument types present... |
| 349 | // |
| 350 | const PointerType *PTy = cast<PointerType>(CI->getCalledValue()->getType()); |
| 351 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 352 | |
| 353 | // Is the call to a vararg variable with no real parameters? |
Chris Lattner | cdeb81d | 2003-05-01 21:02:53 +0000 | [diff] [blame] | 354 | if (FTy->isVarArg() && FTy->getNumParams() == 0 && |
| 355 | !CI->getCalledFunction()) { |
Chris Lattner | 61b92c0 | 2002-10-08 22:19:25 +0000 | [diff] [blame] | 356 | // If so, insert a new cast instruction, casting it to a function type |
| 357 | // that matches the current arguments... |
| 358 | // |
| 359 | std::vector<const Type *> Params; // Parameter types... |
| 360 | for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) |
| 361 | Params.push_back(CI->getOperand(i)->getType()); |
| 362 | |
| 363 | FunctionType *NewFT = FunctionType::get(FTy->getReturnType(), |
| 364 | Params, false); |
| 365 | PointerType *NewPFunTy = PointerType::get(NewFT); |
| 366 | |
| 367 | // Create a new cast, inserting it right before the function call... |
Chris Lattner | 9554928 | 2003-04-28 01:25:38 +0000 | [diff] [blame] | 368 | Value *NewCast; |
| 369 | Constant *ConstantCallSrc = 0; |
| 370 | if (Constant *CS = dyn_cast<Constant>(CI->getCalledValue())) |
| 371 | ConstantCallSrc = CS; |
Chris Lattner | 9554928 | 2003-04-28 01:25:38 +0000 | [diff] [blame] | 372 | |
| 373 | if (ConstantCallSrc) |
| 374 | NewCast = ConstantExpr::getCast(ConstantCallSrc, NewPFunTy); |
| 375 | else |
| 376 | NewCast = new CastInst(CI->getCalledValue(), NewPFunTy, |
| 377 | CI->getCalledValue()->getName()+"_c",CI); |
Chris Lattner | 61b92c0 | 2002-10-08 22:19:25 +0000 | [diff] [blame] | 378 | |
| 379 | // Create a new call instruction... |
| 380 | CallInst *NewCall = new CallInst(NewCast, |
| 381 | std::vector<Value*>(CI->op_begin()+1, CI->op_end())); |
Chris Lattner | 65af1ab | 2005-05-14 12:28:32 +0000 | [diff] [blame] | 382 | if (CI->isTailCall()) NewCall->setTailCall(); |
| 383 | NewCall->setCallingConv(CI->getCallingConv()); |
Chris Lattner | 61b92c0 | 2002-10-08 22:19:25 +0000 | [diff] [blame] | 384 | ++BI; |
| 385 | ReplaceInstWithInst(CI, NewCall); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 61b92c0 | 2002-10-08 22:19:25 +0000 | [diff] [blame] | 387 | ++NumVarargCallChanges; |
| 388 | return true; |
| 389 | } |
| 390 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 399 | bool RPR::DoRaisePass(Function &F) { |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 400 | bool Changed = false; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 401 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 402 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 403 | DOUT << "LevelRaising: " << *BI; |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 404 | if (dceInstruction(BI) || doConstantPropagation(BI)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 405 | Changed = true; |
Chris Lattner | 3c01937 | 2002-05-10 15:29:25 +0000 | [diff] [blame] | 406 | ++NumDCEorCP; |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 407 | DOUT << "***\t\t^^-- Dead code eliminated!\n"; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 408 | } else if (PeepholeOptimize(BB, BI)) { |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 409 | Changed = true; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 410 | } else { |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 411 | ++BI; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 412 | } |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 413 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 414 | |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 415 | return Changed; |
| 416 | } |
| 417 | |
| 418 | |
Chris Lattner | 16125fb | 2003-04-24 18:25:27 +0000 | [diff] [blame] | 419 | // runOnFunction - Raise a function representation to a higher level. |
| 420 | bool RPR::runOnFunction(Function &F) { |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 421 | DOUT << "\n\n\nStarting to work on Function '" << F.getName() << "'\n"; |
Chris Lattner | 68b07b7 | 2001-11-01 07:00:51 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 423 | // Insert casts for all incoming pointer pointer values that are treated as |
| 424 | // arrays... |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 425 | // |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 426 | bool Changed = false, LocalChange; |
Chris Lattner | 3378a5b | 2002-07-16 23:49:24 +0000 | [diff] [blame] | 427 | |
| 428 | // If the StartInst option was specified, then Peephole optimize that |
| 429 | // instruction first if it occurs in this function. |
| 430 | // |
| 431 | if (!StartInst.empty()) { |
| 432 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) |
| 433 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) |
| 434 | if (BI->getName() == StartInst) { |
| 435 | bool SavedDebug = DebugFlag; // Save the DEBUG() controlling flag. |
| 436 | DebugFlag = true; // Turn on DEBUG's |
| 437 | Changed |= PeepholeOptimize(BB, BI); |
| 438 | DebugFlag = SavedDebug; // Restore DebugFlag to previous state |
| 439 | } |
| 440 | } |
| 441 | |
Chris Lattner | 4b770a3 | 2001-12-04 08:12:53 +0000 | [diff] [blame] | 442 | do { |
Bill Wendling | 62c804a | 2006-11-26 09:17:06 +0000 | [diff] [blame^] | 443 | DOUT << "Looping: \n" << F; |
Chris Lattner | a8b6d43 | 2001-12-05 06:34:00 +0000 | [diff] [blame] | 444 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 445 | // Iterate over the function, refining it, until it converges on a stable |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 446 | // state |
Chris Lattner | d554380 | 2001-12-14 16:37:52 +0000 | [diff] [blame] | 447 | LocalChange = false; |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 448 | while (DoRaisePass(F)) LocalChange = true; |
Chris Lattner | 3cc7dde | 2001-11-26 16:58:14 +0000 | [diff] [blame] | 449 | Changed |= LocalChange; |
| 450 | |
| 451 | } while (LocalChange); |
Chris Lattner | d32a961 | 2001-11-01 02:42:08 +0000 | [diff] [blame] | 452 | |
| 453 | return Changed; |
| 454 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 455 | |