blob: c5ac8c98b97d3bf5c498c07cad2336b94e755ca5 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- LevelRaise.cpp - Code to change LLVM to higher level ---------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd32a9612001-11-01 02:42:08 +00009//
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 Lattner3cc7dde2001-11-26 16:58:14 +000012// analyze.
Chris Lattnerd32a9612001-11-01 02:42:08 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattnerbf881002003-09-01 20:45:33 +000016#include "llvm/Transforms/Scalar.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000017#include "llvm/Transforms/Utils/Local.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000018#include "TransformInternals.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/Instructions.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000020#include "llvm/Pass.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000021#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/STLExtras.h"
Chris Lattnerd32a9612001-11-01 02:42:08 +000026#include <algorithm>
Chris Lattnere7999022003-12-23 07:43:38 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner3378a5b2002-07-16 23:49:24 +000029// 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 Lattner5ff62e92002-07-22 02:10:13 +000033static cl::opt<std::string>
34StartInst("raise-start-inst", cl::Hidden, cl::value_desc("inst name"),
35 cl::desc("Start raise pass at the instruction with the specified name"));
Chris Lattner3378a5b2002-07-16 23:49:24 +000036
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000037static Statistic
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000038NumLoadStorePeepholes("raise", "Number of load/store peepholes");
Chris Lattner5ff62e92002-07-22 02:10:13 +000039
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000040static Statistic
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000041NumGEPInstFormed("raise", "Number of other getelementptr's formed");
Chris Lattner5ff62e92002-07-22 02:10:13 +000042
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000043static Statistic
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000044NumExprTreesConv("raise", "Number of expression trees converted");
Chris Lattner5ff62e92002-07-22 02:10:13 +000045
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000046static Statistic
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000047NumCastOfCast("raise", "Number of cast-of-self removed");
Chris Lattner5ff62e92002-07-22 02:10:13 +000048
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000049static Statistic
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000050NumDCEorCP("raise", "Number of insts DCEd or constprop'd");
Chris Lattner3c019372002-05-10 15:29:25 +000051
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000052static Statistic
Chris Lattner61b92c02002-10-08 22:19:25 +000053NumVarargCallChanges("raise", "Number of vararg call peepholes");
54
Chris Lattnerd32a9612001-11-01 02:42:08 +000055#define PRINT_PEEPHOLE(ID, NUM, I) \
Bill Wendling62c804a2006-11-26 09:17:06 +000056 DOUT << "Inst P/H " << ID << "[" << NUM << "] " << I
Chris Lattnerd32a9612001-11-01 02:42:08 +000057
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 Lattnerd5b48ca2001-11-14 11:02:49 +000064#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 Lattnerd32a9612001-11-01 02:42:08 +000067
Chris Lattner16125fb2003-04-24 18:25:27 +000068namespace {
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 Lattner7f8897f2006-08-27 22:42:52 +000082 RegisterPass<RPR> X("raise", "Raise Pointer References");
Chris Lattner16125fb2003-04-24 18:25:27 +000083}
84
Brian Gaeked0fde302003-11-11 22:41:34 +000085
Chris Lattnerded6d0c2004-09-20 04:43:57 +000086FunctionPass *llvm::createRaisePointerReferencesPass() {
Chris Lattner16125fb2003-04-24 18:25:27 +000087 return new RPR();
88}
89
Chris Lattner16125fb2003-04-24 18:25:27 +000090bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
Chris Lattner7e708292002-06-25 16:13:24 +000091 Instruction *I = BI;
Chris Lattner16125fb2003-04-24 18:25:27 +000092 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattnerd32a9612001-11-01 02:42:08 +000093
94 if (CastInst *CI = dyn_cast<CastInst>(I)) {
95 Value *Src = CI->getOperand(0);
Chris Lattnerd32a9612001-11-01 02:42:08 +000096 const Type *DestTy = CI->getType();
97
Chris Lattnere99c66b2001-11-01 17:05:27 +000098 // Peephole optimize the following instruction:
99 // %V2 = cast <ty> %V to <ty>
100 //
101 // Into: <nothing>
102 //
103 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattner30b43442004-07-15 02:06:12 +0000104 PRINT_PEEPHOLE1("cast-of-self-ty", *CI);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000105 CI->replaceAllUsesWith(Src);
106 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000107 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000108 CI->setName("");
Chris Lattner7acff252005-03-05 19:05:20 +0000109 Src->setName(Name);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000110 }
Chris Lattner3c019372002-05-10 15:29:25 +0000111
112 // DCE the instruction now, to avoid having the iterative version of DCE
113 // have to worry about it.
114 //
Chris Lattner7e708292002-06-25 16:13:24 +0000115 BI = BB->getInstList().erase(BI);
Chris Lattner3c019372002-05-10 15:29:25 +0000116
117 ++NumCastOfCast;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000118 return true;
119 }
120
Chris Lattnerd32a9612001-11-01 02:42:08 +0000121 // Check to see if it's a cast of an instruction that does not depend on the
122 // specific type of the operands to do it's job.
Reid Spencer3da59db2006-11-27 01:05:10 +0000123 if (CI->isLosslessCast()) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000124 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000125
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000126 // Check to see if we can convert the source of the cast to match the
127 // destination type of the cast...
Chris Lattnera8b6d432001-12-05 06:34:00 +0000128 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000129 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Misha Brukmanf117cc92003-05-20 18:45:36 +0000130 if (ExpressionConvertibleToType(Src, DestTy, ConvertedTypes, TD)) {
Chris Lattner30b43442004-07-15 02:06:12 +0000131 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", *Src, *CI, *BB->getParent());
Misha Brukmanfd939082005-04-21 23:48:37 +0000132
Bill Wendling62c804a2006-11-26 09:17:06 +0000133 DOUT << "\nCONVERTING SRC EXPR TYPE:\n";
Chris Lattnerb1b42622002-07-17 17:11:33 +0000134 { // ValueMap must be destroyed before function verified!
135 ValueMapCache ValueMap;
Chris Lattner16125fb2003-04-24 18:25:27 +0000136 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap, TD);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000137
Chris Lattnerb1b42622002-07-17 17:11:33 +0000138 if (Constant *CPV = dyn_cast<Constant>(E))
139 CI->replaceAllUsesWith(CPV);
Misha Brukmanfd939082005-04-21 23:48:37 +0000140
Chris Lattner30b43442004-07-15 02:06:12 +0000141 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", *E);
Bill Wendling62c804a2006-11-26 09:17:06 +0000142 DOUT << "DONE CONVERTING SRC EXPR TYPE: \n"
143 << *BB->getParent();
Chris Lattnerb1b42622002-07-17 17:11:33 +0000144 }
Chris Lattner3378a5b2002-07-16 23:49:24 +0000145
Chris Lattnerb1b42622002-07-17 17:11:33 +0000146 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000147 ++NumExprTreesConv;
Chris Lattnerd5543802001-12-14 16:37:52 +0000148 return true;
149 }
150
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000151 // Check to see if we can convert the users of the cast value to match the
152 // source type of the cast...
Chris Lattnerd5543802001-12-14 16:37:52 +0000153 //
154 ConvertedTypes.clear();
Chris Lattner61b92c02002-10-08 22:19:25 +0000155 // Make sure the source doesn't change type
156 ConvertedTypes[Src] = Src->getType();
Misha Brukmanf117cc92003-05-20 18:45:36 +0000157 if (ValueConvertibleToType(CI, Src->getType(), ConvertedTypes, TD)) {
Chris Lattner5e2e2722004-08-08 01:27:56 +0000158 //PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", *Src, *CI,
159 // *BB->getParent());
Chris Lattnerd5543802001-12-14 16:37:52 +0000160
Bill Wendling62c804a2006-11-26 09:17:06 +0000161 DOUT << "\nCONVERTING EXPR TYPE:\n";
Chris Lattnerb1b42622002-07-17 17:11:33 +0000162 { // ValueMap must be destroyed before function verified!
163 ValueMapCache ValueMap;
Chris Lattner16125fb2003-04-24 18:25:27 +0000164 ConvertValueToNewType(CI, Src, ValueMap, TD); // This will delete CI!
Chris Lattnerb1b42622002-07-17 17:11:33 +0000165 }
Chris Lattnerd5543802001-12-14 16:37:52 +0000166
Chris Lattner30b43442004-07-15 02:06:12 +0000167 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", *Src);
Bill Wendling62c804a2006-11-26 09:17:06 +0000168 DOUT << "DONE CONVERTING EXPR TYPE: \n\n" << *BB->getParent();
Chris Lattner3378a5b2002-07-16 23:49:24 +0000169
Chris Lattnerb1b42622002-07-17 17:11:33 +0000170 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000171 ++NumExprTreesConv;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000172 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000173 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000174 }
175
Chris Lattnere99c66b2001-11-01 17:05:27 +0000176 // Check to see if we are casting from a structure pointer to a pointer to
177 // the first element of the structure... to avoid munching other peepholes,
178 // we only let this happen if there are no add uses of the cast.
179 //
180 // Peephole optimize the following instructions:
181 // %t1 = cast {<...>} * %StructPtr to <ty> *
182 //
183 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
184 // %t1 = cast <eltype> * %t1 to <ty> *
185 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000186 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000187 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
188
189 // Loop over uses of the cast, checking for add instructions. If an add
190 // exists, this is probably a part of a more complex GEP, so we don't
191 // want to mess around with the cast.
192 //
193 bool HasAddUse = false;
194 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
195 I != E; ++I)
196 if (isa<Instruction>(*I) &&
197 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
198 HasAddUse = true; break;
199 }
200
201 // If it doesn't have an add use, check to see if the dest type is
Misha Brukmanf117cc92003-05-20 18:45:36 +0000202 // losslessly convertible to one of the types in the start of the struct
Chris Lattnere99c66b2001-11-01 17:05:27 +0000203 // type.
204 //
205 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000206 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000207 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000208 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000209 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000210
211 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000212 std::vector<Value*> Indices;
Chris Lattner559d5192004-01-09 05:53:38 +0000213
Chris Lattner28977af2004-04-05 01:30:19 +0000214 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Chris Lattnerd5543802001-12-14 16:37:52 +0000215 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000216 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
217 // Check for a zero element struct type... if we have one, bail.
Chris Lattnerd21cd802004-02-09 04:37:31 +0000218 if (CurSTy->getNumElements() == 0) break;
Misha Brukmanfd939082005-04-21 23:48:37 +0000219
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000220 // Grab the first element of the struct type, which must lie at
221 // offset zero in the struct.
222 //
Chris Lattnerd21cd802004-02-09 04:37:31 +0000223 ElTy = CurSTy->getElementType(0);
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000224 } else {
Chris Lattner7d719c32005-01-19 16:16:35 +0000225 ElTy = cast<SequentialType>(CurCTy)->getElementType();
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000226 }
227
228 // Insert a zero to index through this type...
Chris Lattner28977af2004-04-05 01:30:19 +0000229 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000230
231 // Did we find what we're looking for?
Reid Spencer3da59db2006-11-27 01:05:10 +0000232 if (ElTy->canLosslesslyBitCastTo(DestPointedTy)) break;
Misha Brukmanfd939082005-04-21 23:48:37 +0000233
Chris Lattnere99c66b2001-11-01 17:05:27 +0000234 // Nope, go a level deeper.
235 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000236 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000237 ElTy = 0;
238 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000239
Chris Lattnere99c66b2001-11-01 17:05:27 +0000240 // Did we find what we were looking for? If so, do the transformation
241 if (ElTy) {
Chris Lattner30b43442004-07-15 02:06:12 +0000242 PRINT_PEEPHOLE1("cast-for-first:in", *CI);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000243
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000244 std::string Name = CI->getName(); CI->setName("");
245
Chris Lattnere99c66b2001-11-01 17:05:27 +0000246 // Insert the new T cast instruction... stealing old T's name
247 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000248 Name, BI);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000249
250 // Make the old cast instruction reference the new GEP instead of
Reid Spencer3da59db2006-11-27 01:05:10 +0000251 // the old src value.
252 if (CI->getOperand(0)->getType() == GEP->getType()) {
253 // If the source types are the same we can safely replace the
254 // first operand of the CastInst because the opcode won't
255 // change as a result.
256 CI->setOperand(0, GEP);
257 } else {
258 // The existing and new operand 0 types are different so we must
259 // replace CI with a new CastInst so that we are assured to
260 // get the correct cast opcode.
Reid Spencer7b06bd52006-12-13 00:50:17 +0000261 CastInst *NewCI = new BitCastInst(GEP, CI->getType(),
262 CI->getName(), CI);
Reid Spencer3da59db2006-11-27 01:05:10 +0000263 CI->replaceAllUsesWith(NewCI);
264 CI->eraseFromParent();
265 CI = NewCI;
266 BI = NewCI; // Don't let the iterator invalidate
267 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000268
Chris Lattner30b43442004-07-15 02:06:12 +0000269 PRINT_PEEPHOLE2("cast-for-first:out", *GEP, *CI);
Chris Lattner3c019372002-05-10 15:29:25 +0000270 ++NumGEPInstFormed;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000271 return true;
272 }
273 }
274 }
Chris Lattnere99c66b2001-11-01 17:05:27 +0000275
Chris Lattner8d38e542001-11-01 03:12:34 +0000276 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
277 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000278 Value *Pointer = SI->getPointerOperand();
Misha Brukmanfd939082005-04-21 23:48:37 +0000279
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000280 // Peephole optimize the following instructions:
Reid Spencer3da59db2006-11-27 01:05:10 +0000281 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly castable to T2
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000282 // store <T2> %V, <T2>* %t
283 //
Misha Brukmanfd939082005-04-21 23:48:37 +0000284 // Into:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000285 // %t = cast <T2> %V to <T1>
286 // store <T1> %t2, <T1>* %P
287 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000288 // Note: This is not taken care of by expr conversion because there might
289 // not be a cast available for the store to convert the incoming value of.
290 // This code is basically here to make sure that pointers don't have casts
291 // if possible.
292 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000293 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
294 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000295 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Misha Brukmanf117cc92003-05-20 18:45:36 +0000296 // convertible types?
Reid Spencer3da59db2006-11-27 01:05:10 +0000297 if (Val->getType()->canLosslesslyBitCastTo(CSPT->getElementType()))
298 {
Chris Lattner30b43442004-07-15 02:06:12 +0000299 PRINT_PEEPHOLE3("st-src-cast:in ", *Pointer, *Val, *SI);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000300
301 // Insert the new T cast instruction... stealing old T's name
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000302 std::string Name(CI->getName()); CI->setName("");
Reid Spencer3da59db2006-11-27 01:05:10 +0000303 CastInst *NCI = CastInst::create(Instruction::BitCast, Val,
304 CSPT->getElementType(), Name, BI);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000305
306 // Replace the old store with a new one!
307 ReplaceInstWithInst(BB->getInstList(), BI,
308 SI = new StoreInst(NCI, CastSrc));
Chris Lattner30b43442004-07-15 02:06:12 +0000309 PRINT_PEEPHOLE3("st-src-cast:out", *NCI, *CastSrc, *SI);
Chris Lattner3c019372002-05-10 15:29:25 +0000310 ++NumLoadStorePeepholes;
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000311 return true;
312 }
313
Chris Lattnerc99428f2002-05-14 05:23:45 +0000314 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
315 Value *Pointer = LI->getOperand(0);
316 const Type *PtrElType =
317 cast<PointerType>(Pointer->getType())->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000318
Chris Lattnerc99428f2002-05-14 05:23:45 +0000319 // Peephole optimize the following instructions:
Misha Brukmanf117cc92003-05-20 18:45:36 +0000320 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertible to T2
Chris Lattnerc99428f2002-05-14 05:23:45 +0000321 // %t = load <T2>* %P
322 //
Misha Brukmanfd939082005-04-21 23:48:37 +0000323 // Into:
Chris Lattnerc99428f2002-05-14 05:23:45 +0000324 // %t = load <T1>* %P
325 // %Val = cast <T1> to <T2>
326 //
327 // Note: This is not taken care of by expr conversion because there might
328 // not be a cast available for the store to convert the incoming value of.
329 // This code is basically here to make sure that pointers don't have casts
330 // if possible.
331 //
332 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
333 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000334 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Misha Brukmanf117cc92003-05-20 18:45:36 +0000335 // convertible types?
Reid Spencer3da59db2006-11-27 01:05:10 +0000336 if (PtrElType->canLosslesslyBitCastTo(CSPT->getElementType())) {
Chris Lattner30b43442004-07-15 02:06:12 +0000337 PRINT_PEEPHOLE2("load-src-cast:in ", *Pointer, *LI);
Chris Lattnerc99428f2002-05-14 05:23:45 +0000338
339 // Create the new load instruction... loading the pre-casted value
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000340 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName(), BI);
Misha Brukmanfd939082005-04-21 23:48:37 +0000341
Chris Lattnerc99428f2002-05-14 05:23:45 +0000342 // Insert the new T cast instruction... stealing old T's name
Reid Spencer3da59db2006-11-27 01:05:10 +0000343 CastInst *NCI =
344 CastInst::create(Instruction::BitCast, NewLI, LI->getType(),
345 CI->getName());
Chris Lattnerc99428f2002-05-14 05:23:45 +0000346
347 // Replace the old store with a new one!
348 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
Chris Lattner30b43442004-07-15 02:06:12 +0000349 PRINT_PEEPHOLE3("load-src-cast:out", *NCI, *CastSrc, *NewLI);
Chris Lattnerc99428f2002-05-14 05:23:45 +0000350 ++NumLoadStorePeepholes;
351 return true;
352 }
353
Chris Lattner61b92c02002-10-08 22:19:25 +0000354 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
355 // If we have a call with all varargs arguments, convert the call to use the
356 // actual argument types present...
357 //
358 const PointerType *PTy = cast<PointerType>(CI->getCalledValue()->getType());
359 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
360
361 // Is the call to a vararg variable with no real parameters?
Chris Lattnercdeb81d2003-05-01 21:02:53 +0000362 if (FTy->isVarArg() && FTy->getNumParams() == 0 &&
363 !CI->getCalledFunction()) {
Chris Lattner61b92c02002-10-08 22:19:25 +0000364 // If so, insert a new cast instruction, casting it to a function type
365 // that matches the current arguments...
366 //
367 std::vector<const Type *> Params; // Parameter types...
368 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
369 Params.push_back(CI->getOperand(i)->getType());
370
371 FunctionType *NewFT = FunctionType::get(FTy->getReturnType(),
372 Params, false);
373 PointerType *NewPFunTy = PointerType::get(NewFT);
374
375 // Create a new cast, inserting it right before the function call...
Chris Lattner95549282003-04-28 01:25:38 +0000376 Value *NewCast;
Chris Lattner95549282003-04-28 01:25:38 +0000377 if (Constant *CS = dyn_cast<Constant>(CI->getCalledValue()))
Reid Spencer3da59db2006-11-27 01:05:10 +0000378 NewCast = ConstantExpr::getBitCast(CS, NewPFunTy);
Chris Lattner95549282003-04-28 01:25:38 +0000379 else
Reid Spencer3da59db2006-11-27 01:05:10 +0000380 NewCast = CastInst::create(Instruction::BitCast, CI->getCalledValue(),
381 NewPFunTy,
382 CI->getCalledValue()->getName()+"_c", CI);
Chris Lattner61b92c02002-10-08 22:19:25 +0000383
384 // Create a new call instruction...
385 CallInst *NewCall = new CallInst(NewCast,
386 std::vector<Value*>(CI->op_begin()+1, CI->op_end()));
Chris Lattner65af1ab2005-05-14 12:28:32 +0000387 if (CI->isTailCall()) NewCall->setTailCall();
388 NewCall->setCallingConv(CI->getCallingConv());
Chris Lattner61b92c02002-10-08 22:19:25 +0000389 ++BI;
390 ReplaceInstWithInst(CI, NewCall);
Misha Brukmanfd939082005-04-21 23:48:37 +0000391
Chris Lattner61b92c02002-10-08 22:19:25 +0000392 ++NumVarargCallChanges;
393 return true;
394 }
395
Chris Lattnerd32a9612001-11-01 02:42:08 +0000396 }
397
398 return false;
399}
400
401
402
403
Chris Lattner16125fb2003-04-24 18:25:27 +0000404bool RPR::DoRaisePass(Function &F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000405 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000406 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattnerd32a9612001-11-01 02:42:08 +0000407 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Bill Wendling62c804a2006-11-26 09:17:06 +0000408 DOUT << "LevelRaising: " << *BI;
Misha Brukman82c89b92003-05-20 21:01:22 +0000409 if (dceInstruction(BI) || doConstantPropagation(BI)) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000410 Changed = true;
Chris Lattner3c019372002-05-10 15:29:25 +0000411 ++NumDCEorCP;
Bill Wendling62c804a2006-11-26 09:17:06 +0000412 DOUT << "***\t\t^^-- Dead code eliminated!\n";
Chris Lattner7e708292002-06-25 16:13:24 +0000413 } else if (PeepholeOptimize(BB, BI)) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000414 Changed = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000415 } else {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000416 ++BI;
Chris Lattner7e708292002-06-25 16:13:24 +0000417 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000418 }
Chris Lattner7e708292002-06-25 16:13:24 +0000419
Chris Lattnerd32a9612001-11-01 02:42:08 +0000420 return Changed;
421}
422
423
Chris Lattner16125fb2003-04-24 18:25:27 +0000424// runOnFunction - Raise a function representation to a higher level.
425bool RPR::runOnFunction(Function &F) {
Bill Wendling62c804a2006-11-26 09:17:06 +0000426 DOUT << "\n\n\nStarting to work on Function '" << F.getName() << "'\n";
Chris Lattner68b07b72001-11-01 07:00:51 +0000427
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000428 // Insert casts for all incoming pointer pointer values that are treated as
429 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000430 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000431 bool Changed = false, LocalChange;
Chris Lattner3378a5b2002-07-16 23:49:24 +0000432
433 // If the StartInst option was specified, then Peephole optimize that
434 // instruction first if it occurs in this function.
435 //
436 if (!StartInst.empty()) {
437 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
438 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI)
439 if (BI->getName() == StartInst) {
440 bool SavedDebug = DebugFlag; // Save the DEBUG() controlling flag.
441 DebugFlag = true; // Turn on DEBUG's
442 Changed |= PeepholeOptimize(BB, BI);
443 DebugFlag = SavedDebug; // Restore DebugFlag to previous state
444 }
445 }
446
Chris Lattner4b770a32001-12-04 08:12:53 +0000447 do {
Bill Wendling62c804a2006-11-26 09:17:06 +0000448 DOUT << "Looping: \n" << F;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000449
Chris Lattnerf57b8452002-04-27 06:56:12 +0000450 // Iterate over the function, refining it, until it converges on a stable
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000451 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000452 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000453 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000454 Changed |= LocalChange;
455
456 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000457
458 return Changed;
459}
Brian Gaeked0fde302003-11-11 22:41:34 +0000460