blob: 943000c96613e0ec969e5dfb94b97bcf204d7be6 [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 Lattnerdac58ad2006-01-22 23:32:06 +000027#include <iostream>
Chris Lattnere7999022003-12-23 07:43:38 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner3378a5b2002-07-16 23:49:24 +000030// 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 Lattner5ff62e92002-07-22 02:10:13 +000034static cl::opt<std::string>
35StartInst("raise-start-inst", cl::Hidden, cl::value_desc("inst name"),
36 cl::desc("Start raise pass at the instruction with the specified name"));
Chris Lattner3378a5b2002-07-16 23:49:24 +000037
Chris Lattner5ff62e92002-07-22 02:10:13 +000038static Statistic<>
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000039NumLoadStorePeepholes("raise", "Number of load/store peepholes");
Chris Lattner5ff62e92002-07-22 02:10:13 +000040
Misha Brukmanfd939082005-04-21 23:48:37 +000041static Statistic<>
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000042NumGEPInstFormed("raise", "Number of other getelementptr's formed");
Chris Lattner5ff62e92002-07-22 02:10:13 +000043
44static Statistic<>
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000045NumExprTreesConv("raise", "Number of expression trees converted");
Chris Lattner5ff62e92002-07-22 02:10:13 +000046
47static Statistic<>
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000048NumCastOfCast("raise", "Number of cast-of-self removed");
Chris Lattner5ff62e92002-07-22 02:10:13 +000049
50static Statistic<>
Chris Lattner6ee6bbe2002-10-01 22:38:37 +000051NumDCEorCP("raise", "Number of insts DCEd or constprop'd");
Chris Lattner3c019372002-05-10 15:29:25 +000052
Chris Lattner61b92c02002-10-08 22:19:25 +000053static Statistic<>
54NumVarargCallChanges("raise", "Number of vararg call peepholes");
55
Chris Lattnerd32a9612001-11-01 02:42:08 +000056#define PRINT_PEEPHOLE(ID, NUM, I) \
Chris Lattnerb3abf9d2002-05-22 17:27:12 +000057 DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I)
Chris Lattnerd32a9612001-11-01 02:42:08 +000058
59#define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
60#define PRINT_PEEPHOLE2(ID, I1, I2) \
61 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
62#define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
63 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
64 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000065#define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \
66 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
67 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0)
Chris Lattnerd32a9612001-11-01 02:42:08 +000068
Chris Lattner16125fb2003-04-24 18:25:27 +000069namespace {
70 struct RPR : public FunctionPass {
71 virtual bool runOnFunction(Function &F);
72
73 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.setPreservesCFG();
75 AU.addRequired<TargetData>();
76 }
77
78 private:
79 bool DoRaisePass(Function &F);
80 bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI);
81 };
82
83 RegisterOpt<RPR> X("raise", "Raise Pointer References");
84}
85
Brian Gaeked0fde302003-11-11 22:41:34 +000086
Chris Lattnerded6d0c2004-09-20 04:43:57 +000087FunctionPass *llvm::createRaisePointerReferencesPass() {
Chris Lattner16125fb2003-04-24 18:25:27 +000088 return new RPR();
89}
90
91
Chris Lattnerd32a9612001-11-01 02:42:08 +000092// isReinterpretingCast - Return true if the cast instruction specified will
93// cause the operand to be "reinterpreted". A value is reinterpreted if the
94// cast instruction would cause the underlying bits to change.
95//
96static inline bool isReinterpretingCast(const CastInst *CI) {
Misha Brukmanf117cc92003-05-20 18:45:36 +000097 return!CI->getOperand(0)->getType()->isLosslesslyConvertibleTo(CI->getType());
Chris Lattnerd32a9612001-11-01 02:42:08 +000098}
99
Chris Lattner16125fb2003-04-24 18:25:27 +0000100bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000101 Instruction *I = BI;
Chris Lattner16125fb2003-04-24 18:25:27 +0000102 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattnerd32a9612001-11-01 02:42:08 +0000103
104 if (CastInst *CI = dyn_cast<CastInst>(I)) {
105 Value *Src = CI->getOperand(0);
106 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
107 const Type *DestTy = CI->getType();
108
Chris Lattnere99c66b2001-11-01 17:05:27 +0000109 // Peephole optimize the following instruction:
110 // %V2 = cast <ty> %V to <ty>
111 //
112 // Into: <nothing>
113 //
114 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattner30b43442004-07-15 02:06:12 +0000115 PRINT_PEEPHOLE1("cast-of-self-ty", *CI);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000116 CI->replaceAllUsesWith(Src);
117 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000118 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000119 CI->setName("");
Chris Lattner7acff252005-03-05 19:05:20 +0000120 Src->setName(Name);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000121 }
Chris Lattner3c019372002-05-10 15:29:25 +0000122
123 // DCE the instruction now, to avoid having the iterative version of DCE
124 // have to worry about it.
125 //
Chris Lattner7e708292002-06-25 16:13:24 +0000126 BI = BB->getInstList().erase(BI);
Chris Lattner3c019372002-05-10 15:29:25 +0000127
128 ++NumCastOfCast;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000129 return true;
130 }
131
Chris Lattnerd32a9612001-11-01 02:42:08 +0000132 // Check to see if it's a cast of an instruction that does not depend on the
133 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000134 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000135 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000136
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000137 // Check to see if we can convert the source of the cast to match the
138 // destination type of the cast...
Chris Lattnera8b6d432001-12-05 06:34:00 +0000139 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000140 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Misha Brukmanf117cc92003-05-20 18:45:36 +0000141 if (ExpressionConvertibleToType(Src, DestTy, ConvertedTypes, TD)) {
Chris Lattner30b43442004-07-15 02:06:12 +0000142 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", *Src, *CI, *BB->getParent());
Misha Brukmanfd939082005-04-21 23:48:37 +0000143
Chris Lattner6806f562003-08-01 22:15:03 +0000144 DEBUG(std::cerr << "\nCONVERTING SRC EXPR TYPE:\n");
Chris Lattnerb1b42622002-07-17 17:11:33 +0000145 { // ValueMap must be destroyed before function verified!
146 ValueMapCache ValueMap;
Chris Lattner16125fb2003-04-24 18:25:27 +0000147 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap, TD);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000148
Chris Lattnerb1b42622002-07-17 17:11:33 +0000149 if (Constant *CPV = dyn_cast<Constant>(E))
150 CI->replaceAllUsesWith(CPV);
Misha Brukmanfd939082005-04-21 23:48:37 +0000151
Chris Lattner30b43442004-07-15 02:06:12 +0000152 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", *E);
Chris Lattner6806f562003-08-01 22:15:03 +0000153 DEBUG(std::cerr << "DONE CONVERTING SRC EXPR TYPE: \n"
Chris Lattner30b43442004-07-15 02:06:12 +0000154 << *BB->getParent());
Chris Lattnerb1b42622002-07-17 17:11:33 +0000155 }
Chris Lattner3378a5b2002-07-16 23:49:24 +0000156
Chris Lattnerb1b42622002-07-17 17:11:33 +0000157 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000158 ++NumExprTreesConv;
Chris Lattnerd5543802001-12-14 16:37:52 +0000159 return true;
160 }
161
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000162 // Check to see if we can convert the users of the cast value to match the
163 // source type of the cast...
Chris Lattnerd5543802001-12-14 16:37:52 +0000164 //
165 ConvertedTypes.clear();
Chris Lattner61b92c02002-10-08 22:19:25 +0000166 // Make sure the source doesn't change type
167 ConvertedTypes[Src] = Src->getType();
Misha Brukmanf117cc92003-05-20 18:45:36 +0000168 if (ValueConvertibleToType(CI, Src->getType(), ConvertedTypes, TD)) {
Chris Lattner5e2e2722004-08-08 01:27:56 +0000169 //PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", *Src, *CI,
170 // *BB->getParent());
Chris Lattnerd5543802001-12-14 16:37:52 +0000171
Chris Lattner6806f562003-08-01 22:15:03 +0000172 DEBUG(std::cerr << "\nCONVERTING EXPR TYPE:\n");
Chris Lattnerb1b42622002-07-17 17:11:33 +0000173 { // ValueMap must be destroyed before function verified!
174 ValueMapCache ValueMap;
Chris Lattner16125fb2003-04-24 18:25:27 +0000175 ConvertValueToNewType(CI, Src, ValueMap, TD); // This will delete CI!
Chris Lattnerb1b42622002-07-17 17:11:33 +0000176 }
Chris Lattnerd5543802001-12-14 16:37:52 +0000177
Chris Lattner30b43442004-07-15 02:06:12 +0000178 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", *Src);
179 DEBUG(std::cerr << "DONE CONVERTING EXPR TYPE: \n\n" <<
180 *BB->getParent());
Chris Lattner3378a5b2002-07-16 23:49:24 +0000181
Chris Lattnerb1b42622002-07-17 17:11:33 +0000182 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000183 ++NumExprTreesConv;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000184 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000185 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000186 }
187
Chris Lattnere99c66b2001-11-01 17:05:27 +0000188 // Check to see if we are casting from a structure pointer to a pointer to
189 // the first element of the structure... to avoid munching other peepholes,
190 // we only let this happen if there are no add uses of the cast.
191 //
192 // Peephole optimize the following instructions:
193 // %t1 = cast {<...>} * %StructPtr to <ty> *
194 //
195 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
196 // %t1 = cast <eltype> * %t1 to <ty> *
197 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000198 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000199 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
200
201 // Loop over uses of the cast, checking for add instructions. If an add
202 // exists, this is probably a part of a more complex GEP, so we don't
203 // want to mess around with the cast.
204 //
205 bool HasAddUse = false;
206 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
207 I != E; ++I)
208 if (isa<Instruction>(*I) &&
209 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
210 HasAddUse = true; break;
211 }
212
213 // If it doesn't have an add use, check to see if the dest type is
Misha Brukmanf117cc92003-05-20 18:45:36 +0000214 // losslessly convertible to one of the types in the start of the struct
Chris Lattnere99c66b2001-11-01 17:05:27 +0000215 // type.
216 //
217 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000218 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000219 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000220 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000221 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000222
223 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000224 std::vector<Value*> Indices;
Chris Lattner559d5192004-01-09 05:53:38 +0000225
Chris Lattner28977af2004-04-05 01:30:19 +0000226 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Chris Lattnerd5543802001-12-14 16:37:52 +0000227 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000228 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
229 // Check for a zero element struct type... if we have one, bail.
Chris Lattnerd21cd802004-02-09 04:37:31 +0000230 if (CurSTy->getNumElements() == 0) break;
Misha Brukmanfd939082005-04-21 23:48:37 +0000231
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000232 // Grab the first element of the struct type, which must lie at
233 // offset zero in the struct.
234 //
Chris Lattnerd21cd802004-02-09 04:37:31 +0000235 ElTy = CurSTy->getElementType(0);
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000236 } else {
Chris Lattner7d719c32005-01-19 16:16:35 +0000237 ElTy = cast<SequentialType>(CurCTy)->getElementType();
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000238 }
239
240 // Insert a zero to index through this type...
Chris Lattner28977af2004-04-05 01:30:19 +0000241 Indices.push_back(Constant::getNullValue(Type::UIntTy));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000242
243 // Did we find what we're looking for?
Misha Brukmanf117cc92003-05-20 18:45:36 +0000244 if (ElTy->isLosslesslyConvertibleTo(DestPointedTy)) break;
Misha Brukmanfd939082005-04-21 23:48:37 +0000245
Chris Lattnere99c66b2001-11-01 17:05:27 +0000246 // Nope, go a level deeper.
247 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000248 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000249 ElTy = 0;
250 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000251
Chris Lattnere99c66b2001-11-01 17:05:27 +0000252 // Did we find what we were looking for? If so, do the transformation
253 if (ElTy) {
Chris Lattner30b43442004-07-15 02:06:12 +0000254 PRINT_PEEPHOLE1("cast-for-first:in", *CI);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000255
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000256 std::string Name = CI->getName(); CI->setName("");
257
Chris Lattnere99c66b2001-11-01 17:05:27 +0000258 // Insert the new T cast instruction... stealing old T's name
259 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000260 Name, BI);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000261
262 // Make the old cast instruction reference the new GEP instead of
263 // the old src value.
264 //
265 CI->setOperand(0, GEP);
Misha Brukmanfd939082005-04-21 23:48:37 +0000266
Chris Lattner30b43442004-07-15 02:06:12 +0000267 PRINT_PEEPHOLE2("cast-for-first:out", *GEP, *CI);
Chris Lattner3c019372002-05-10 15:29:25 +0000268 ++NumGEPInstFormed;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000269 return true;
270 }
271 }
272 }
Chris Lattnere99c66b2001-11-01 17:05:27 +0000273
Chris Lattner8d38e542001-11-01 03:12:34 +0000274 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
275 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000276 Value *Pointer = SI->getPointerOperand();
Misha Brukmanfd939082005-04-21 23:48:37 +0000277
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000278 // Peephole optimize the following instructions:
Misha Brukmanf117cc92003-05-20 18:45:36 +0000279 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertible to T2
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000280 // store <T2> %V, <T2>* %t
281 //
Misha Brukmanfd939082005-04-21 23:48:37 +0000282 // Into:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000283 // %t = cast <T2> %V to <T1>
284 // store <T1> %t2, <T1>* %P
285 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000286 // Note: This is not taken care of by expr conversion because there might
287 // not be a cast available for the store to convert the incoming value of.
288 // This code is basically here to make sure that pointers don't have casts
289 // if possible.
290 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000291 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
292 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000293 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Misha Brukmanf117cc92003-05-20 18:45:36 +0000294 // convertible types?
295 if (Val->getType()->isLosslesslyConvertibleTo(CSPT->getElementType())) {
Chris Lattner30b43442004-07-15 02:06:12 +0000296 PRINT_PEEPHOLE3("st-src-cast:in ", *Pointer, *Val, *SI);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000297
298 // Insert the new T cast instruction... stealing old T's name
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000299 std::string Name(CI->getName()); CI->setName("");
Chris Lattner7a176752001-12-04 00:03:30 +0000300 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000301 Name, BI);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000302
303 // Replace the old store with a new one!
304 ReplaceInstWithInst(BB->getInstList(), BI,
305 SI = new StoreInst(NCI, CastSrc));
Chris Lattner30b43442004-07-15 02:06:12 +0000306 PRINT_PEEPHOLE3("st-src-cast:out", *NCI, *CastSrc, *SI);
Chris Lattner3c019372002-05-10 15:29:25 +0000307 ++NumLoadStorePeepholes;
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000308 return true;
309 }
310
Chris Lattnerc99428f2002-05-14 05:23:45 +0000311 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
312 Value *Pointer = LI->getOperand(0);
313 const Type *PtrElType =
314 cast<PointerType>(Pointer->getType())->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000315
Chris Lattnerc99428f2002-05-14 05:23:45 +0000316 // Peephole optimize the following instructions:
Misha Brukmanf117cc92003-05-20 18:45:36 +0000317 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertible to T2
Chris Lattnerc99428f2002-05-14 05:23:45 +0000318 // %t = load <T2>* %P
319 //
Misha Brukmanfd939082005-04-21 23:48:37 +0000320 // Into:
Chris Lattnerc99428f2002-05-14 05:23:45 +0000321 // %t = load <T1>* %P
322 // %Val = cast <T1> to <T2>
323 //
324 // Note: This is not taken care of by expr conversion because there might
325 // not be a cast available for the store to convert the incoming value of.
326 // This code is basically here to make sure that pointers don't have casts
327 // if possible.
328 //
329 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
330 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000331 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Misha Brukmanf117cc92003-05-20 18:45:36 +0000332 // convertible types?
333 if (PtrElType->isLosslesslyConvertibleTo(CSPT->getElementType())) {
Chris Lattner30b43442004-07-15 02:06:12 +0000334 PRINT_PEEPHOLE2("load-src-cast:in ", *Pointer, *LI);
Chris Lattnerc99428f2002-05-14 05:23:45 +0000335
336 // Create the new load instruction... loading the pre-casted value
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000337 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName(), BI);
Misha Brukmanfd939082005-04-21 23:48:37 +0000338
Chris Lattnerc99428f2002-05-14 05:23:45 +0000339 // Insert the new T cast instruction... stealing old T's name
340 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName());
Chris Lattnerc99428f2002-05-14 05:23:45 +0000341
342 // Replace the old store with a new one!
343 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
Chris Lattner30b43442004-07-15 02:06:12 +0000344 PRINT_PEEPHOLE3("load-src-cast:out", *NCI, *CastSrc, *NewLI);
Chris Lattnerc99428f2002-05-14 05:23:45 +0000345 ++NumLoadStorePeepholes;
346 return true;
347 }
348
Chris Lattner61b92c02002-10-08 22:19:25 +0000349 } 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 Lattnercdeb81d2003-05-01 21:02:53 +0000357 if (FTy->isVarArg() && FTy->getNumParams() == 0 &&
358 !CI->getCalledFunction()) {
Chris Lattner61b92c02002-10-08 22:19:25 +0000359 // 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 Lattner95549282003-04-28 01:25:38 +0000371 Value *NewCast;
372 Constant *ConstantCallSrc = 0;
373 if (Constant *CS = dyn_cast<Constant>(CI->getCalledValue()))
374 ConstantCallSrc = CS;
Chris Lattner95549282003-04-28 01:25:38 +0000375
376 if (ConstantCallSrc)
377 NewCast = ConstantExpr::getCast(ConstantCallSrc, NewPFunTy);
378 else
379 NewCast = new CastInst(CI->getCalledValue(), NewPFunTy,
380 CI->getCalledValue()->getName()+"_c",CI);
Chris Lattner61b92c02002-10-08 22:19:25 +0000381
382 // Create a new call instruction...
383 CallInst *NewCall = new CallInst(NewCast,
384 std::vector<Value*>(CI->op_begin()+1, CI->op_end()));
Chris Lattner65af1ab2005-05-14 12:28:32 +0000385 if (CI->isTailCall()) NewCall->setTailCall();
386 NewCall->setCallingConv(CI->getCallingConv());
Chris Lattner61b92c02002-10-08 22:19:25 +0000387 ++BI;
388 ReplaceInstWithInst(CI, NewCall);
Misha Brukmanfd939082005-04-21 23:48:37 +0000389
Chris Lattner61b92c02002-10-08 22:19:25 +0000390 ++NumVarargCallChanges;
391 return true;
392 }
393
Chris Lattnerd32a9612001-11-01 02:42:08 +0000394 }
395
396 return false;
397}
398
399
400
401
Chris Lattner16125fb2003-04-24 18:25:27 +0000402bool RPR::DoRaisePass(Function &F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000403 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000404 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattnerd32a9612001-11-01 02:42:08 +0000405 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Brian Gaekea9160a02004-07-01 19:27:10 +0000406 DEBUG(std::cerr << "LevelRaising: " << *BI);
Misha Brukman82c89b92003-05-20 21:01:22 +0000407 if (dceInstruction(BI) || doConstantPropagation(BI)) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000408 Changed = true;
Chris Lattner3c019372002-05-10 15:29:25 +0000409 ++NumDCEorCP;
Chris Lattner6806f562003-08-01 22:15:03 +0000410 DEBUG(std::cerr << "***\t\t^^-- Dead code eliminated!\n");
Chris Lattner7e708292002-06-25 16:13:24 +0000411 } else if (PeepholeOptimize(BB, BI)) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000412 Changed = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000413 } else {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000414 ++BI;
Chris Lattner7e708292002-06-25 16:13:24 +0000415 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000416 }
Chris Lattner7e708292002-06-25 16:13:24 +0000417
Chris Lattnerd32a9612001-11-01 02:42:08 +0000418 return Changed;
419}
420
421
Chris Lattner16125fb2003-04-24 18:25:27 +0000422// runOnFunction - Raise a function representation to a higher level.
423bool RPR::runOnFunction(Function &F) {
Chris Lattner6806f562003-08-01 22:15:03 +0000424 DEBUG(std::cerr << "\n\n\nStarting to work on Function '" << F.getName()
425 << "'\n");
Chris Lattner68b07b72001-11-01 07:00:51 +0000426
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000427 // Insert casts for all incoming pointer pointer values that are treated as
428 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000429 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000430 bool Changed = false, LocalChange;
Chris Lattner3378a5b2002-07-16 23:49:24 +0000431
432 // If the StartInst option was specified, then Peephole optimize that
433 // instruction first if it occurs in this function.
434 //
435 if (!StartInst.empty()) {
436 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
437 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI)
438 if (BI->getName() == StartInst) {
439 bool SavedDebug = DebugFlag; // Save the DEBUG() controlling flag.
440 DebugFlag = true; // Turn on DEBUG's
441 Changed |= PeepholeOptimize(BB, BI);
442 DebugFlag = SavedDebug; // Restore DebugFlag to previous state
443 }
444 }
445
Chris Lattner4b770a32001-12-04 08:12:53 +0000446 do {
Chris Lattner6806f562003-08-01 22:15:03 +0000447 DEBUG(std::cerr << "Looping: \n" << F);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000448
Chris Lattnerf57b8452002-04-27 06:56:12 +0000449 // Iterate over the function, refining it, until it converges on a stable
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000450 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000451 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000452 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000453 Changed |= LocalChange;
454
455 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000456
457 return Changed;
458}
Brian Gaeked0fde302003-11-11 22:41:34 +0000459