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