blob: af6555cc2034fde9d2fb558a62a96b576150390e [file] [log] [blame]
Chris Lattnerd32a9612001-11-01 02:42:08 +00001//===- LevelRaise.cpp - Code to change LLVM to higher level -----------------=//
2//
3// This file implements the 'raising' part of the LevelChange API. This is
4// useful because, in general, it makes the LLVM code terser and easier to
Chris Lattner3cc7dde2001-11-26 16:58:14 +00005// analyze.
Chris Lattnerd32a9612001-11-01 02:42:08 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Transforms/LevelChange.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000010#include "llvm/Transforms/Utils/Local.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000011#include "TransformInternals.h"
Chris Lattnerd32a9612001-11-01 02:42:08 +000012#include "llvm/iOther.h"
13#include "llvm/iMemory.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000014#include "llvm/Pass.h"
Chris Lattner968ddc92002-04-08 20:18:09 +000015#include "llvm/ConstantHandling.h"
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000016#include "llvm/Analysis/Expressions.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000017#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000018#include "Support/STLExtras.h"
Chris Lattner3c019372002-05-10 15:29:25 +000019#include "Support/StatisticReporter.h"
Chris Lattnerd32a9612001-11-01 02:42:08 +000020#include <algorithm>
21
Chris Lattner3c019372002-05-10 15:29:25 +000022static Statistic<> NumLoadStorePeepholes("raise\t\t- Number of load/store peepholes");
23static Statistic<> NumGEPInstFormed("raise\t\t- Number of other getelementptr's formed");
24static Statistic<> NumExprTreesConv("raise\t\t- Number of expression trees converted");
25static Statistic<> NumCastOfCast("raise\t\t- Number of cast-of-self removed");
26static Statistic<> NumDCEorCP("raise\t\t- Number of insts DCE'd or constprop'd");
27
28
Chris Lattner09009802001-11-26 19:36:58 +000029//#define DEBUG_PEEPHOLE_INSTS 1
Chris Lattnerd32a9612001-11-01 02:42:08 +000030
31#ifdef DEBUG_PEEPHOLE_INSTS
32#define PRINT_PEEPHOLE(ID, NUM, I) \
Chris Lattnerebcd28e2002-03-21 03:02:07 +000033 std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I;
Chris Lattnerd32a9612001-11-01 02:42:08 +000034#else
35#define PRINT_PEEPHOLE(ID, NUM, I)
36#endif
37
38#define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
39#define PRINT_PEEPHOLE2(ID, I1, I2) \
40 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
41#define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
42 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
43 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000044#define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \
45 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
46 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0)
Chris Lattnerd32a9612001-11-01 02:42:08 +000047
48
Chris Lattnerd32a9612001-11-01 02:42:08 +000049// isReinterpretingCast - Return true if the cast instruction specified will
50// cause the operand to be "reinterpreted". A value is reinterpreted if the
51// cast instruction would cause the underlying bits to change.
52//
53static inline bool isReinterpretingCast(const CastInst *CI) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +000054 return!CI->getOperand(0)->getType()->isLosslesslyConvertableTo(CI->getType());
Chris Lattnerd32a9612001-11-01 02:42:08 +000055}
56
57
Chris Lattnera8b6d432001-12-05 06:34:00 +000058// Peephole optimize the following instructions:
59// %t1 = cast ? to x *
60// %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand
61//
62// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
63// %t2 = cast <eltype> * %t3 to {<...>}*
64//
65static bool HandleCastToPointer(BasicBlock::iterator BI,
66 const PointerType *DestPTy) {
67 CastInst *CI = cast<CastInst>(*BI);
Chris Lattner45ef5c22002-03-21 06:22:23 +000068 if (CI->use_empty()) return false;
Chris Lattnerf3b976e2001-11-04 20:21:12 +000069
Chris Lattnera8b6d432001-12-05 06:34:00 +000070 // Scan all of the uses, looking for any uses that are not add
71 // instructions. If we have non-adds, do not make this transformation.
72 //
73 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
74 I != E; ++I) {
75 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
76 if (BO->getOpcode() != Instruction::Add)
77 return false;
78 } else {
79 return false;
80 }
81 }
82
Chris Lattner697954c2002-01-20 22:54:45 +000083 std::vector<Value*> Indices;
Chris Lattnera8b6d432001-12-05 06:34:00 +000084 Value *Src = CI->getOperand(0);
85 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
Chris Lattnera8b6d432001-12-05 06:34:00 +000086 if (Result == 0) return false; // Not convertable...
87
88 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
89
90 // If we have a getelementptr capability... transform all of the
91 // add instruction uses into getelementptr's.
Chris Lattnerb24196f2002-03-11 22:19:48 +000092 while (!CI->use_empty()) {
Chris Lattner45ef5c22002-03-21 06:22:23 +000093 BinaryOperator *I = cast<BinaryOperator>(*CI->use_begin());
Chris Lattnera8b6d432001-12-05 06:34:00 +000094 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
95 "Use is not a valid add instruction!");
96
97 // Get the value added to the cast result pointer...
98 Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0);
99
Chris Lattner45ef5c22002-03-21 06:22:23 +0000100 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
Chris Lattnera8b6d432001-12-05 06:34:00 +0000101 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000102
103 if (GEP->getType() == I->getType()) {
104 // Replace the old add instruction with the shiny new GEP inst
105 ReplaceInstWithInst(I, GEP);
106 } else {
107 // If the type produced by the gep instruction differs from the original
108 // add instruction type, insert a cast now.
109 //
110
111 // Insert the GEP instruction before the old add instruction... and get an
112 // iterator to point at the add instruction...
113 BasicBlock::iterator GEPI = InsertInstBeforeInst(GEP, I)+1;
114
115 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
116 CastInst *CI = new CastInst(GEP, I->getType());
117 GEP = CI;
118
119 // Replace the old add instruction with the shiny new GEP inst
120 ReplaceInstWithInst(I->getParent()->getInstList(), GEPI, GEP);
121 }
122
Chris Lattnera8b6d432001-12-05 06:34:00 +0000123 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
124 }
125 return true;
126}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000127
128// Peephole optimize the following instructions:
129// %t1 = cast ulong <const int> to {<...>} *
130// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
131//
132// or
133// %t1 = cast {<...>}* %SP to int*
134// %t5 = cast ulong <const int> to int*
135// %t2 = add int* %t1, %t5 ;; int is same size as field
136//
137// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
138// %t2 = cast <eltype> * %t3 to {<...>}*
139//
140static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
141 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000142 const CompositeType *CompTy;
143 Value *OffsetVal = AddOp2->getOperand(0);
144 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000145
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000146 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000147 SrcPtr = AddOp1; // Handle the first case...
148 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
149 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000150 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000151 }
152
153 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000154 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000155 return false;
156
Chris Lattner697954c2002-01-20 22:54:45 +0000157 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000158 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
159 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000160
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000161 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000162 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
163 } else {
164 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
165 }
166
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000167 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
168 AddOp2->getName());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000169 BI = BB->getInstList().insert(BI, GEP)+1;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000170
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000171 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000172 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
173 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
174 return true;
175}
176
Chris Lattnerd32a9612001-11-01 02:42:08 +0000177static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
178 Instruction *I = *BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000179
180 if (CastInst *CI = dyn_cast<CastInst>(I)) {
181 Value *Src = CI->getOperand(0);
182 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
183 const Type *DestTy = CI->getType();
184
Chris Lattnere99c66b2001-11-01 17:05:27 +0000185 // Peephole optimize the following instruction:
186 // %V2 = cast <ty> %V to <ty>
187 //
188 // Into: <nothing>
189 //
190 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000191 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
192 CI->replaceAllUsesWith(Src);
193 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000194 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000195 CI->setName("");
196 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000197 }
Chris Lattner3c019372002-05-10 15:29:25 +0000198
199 // DCE the instruction now, to avoid having the iterative version of DCE
200 // have to worry about it.
201 //
202 delete BB->getInstList().remove(BI);
203
204 ++NumCastOfCast;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000205 return true;
206 }
207
Chris Lattnerd32a9612001-11-01 02:42:08 +0000208 // Check to see if it's a cast of an instruction that does not depend on the
209 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000210 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000211 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000212
213 // Check to see if we can convert the users of the cast value to match the
214 // source type of the cast...
215 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000216 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000217 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000218 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000219
220#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnera8b6d432001-12-05 06:34:00 +0000221 cerr << "\nCONVERTING SRC EXPR TYPE:\n";
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000222#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000223 ValueMapCache ValueMap;
224 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
225 if (Constant *CPV = dyn_cast<Constant>(E))
226 CI->replaceAllUsesWith(CPV);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000227
Chris Lattnera8b6d432001-12-05 06:34:00 +0000228 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
229 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000230#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnerd5543802001-12-14 16:37:52 +0000231 cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent();
232#endif
Chris Lattner3c019372002-05-10 15:29:25 +0000233 ++NumExprTreesConv;
Chris Lattnerd5543802001-12-14 16:37:52 +0000234 return true;
235 }
236
237 // Check to see if we can convert the source of the cast to match the
238 // destination type of the cast...
239 //
240 ConvertedTypes.clear();
241 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
242 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
243
244#ifdef DEBUG_PEEPHOLE_INSTS
245 cerr << "\nCONVERTING EXPR TYPE:\n";
246#endif
247 ValueMapCache ValueMap;
248 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
249
250 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
251 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
252#ifdef DEBUG_PEEPHOLE_INSTS
253 cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent();
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000254#endif
Chris Lattner3c019372002-05-10 15:29:25 +0000255 ++NumExprTreesConv;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000256 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000257 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000258 }
259
260 // Otherwise find out it this cast is a cast to a pointer type, which is
261 // then added to some other pointer, then loaded or stored through. If
262 // so, convert the add into a getelementptr instruction...
263 //
264 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
265 if (HandleCastToPointer(BI, DestPTy)) {
266 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000267 ++NumGEPInstFormed;
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000268 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000269 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000270 }
271
Chris Lattnere99c66b2001-11-01 17:05:27 +0000272 // Check to see if we are casting from a structure pointer to a pointer to
273 // the first element of the structure... to avoid munching other peepholes,
274 // we only let this happen if there are no add uses of the cast.
275 //
276 // Peephole optimize the following instructions:
277 // %t1 = cast {<...>} * %StructPtr to <ty> *
278 //
279 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
280 // %t1 = cast <eltype> * %t1 to <ty> *
281 //
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000282#if 1
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000283 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000284 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
285
286 // Loop over uses of the cast, checking for add instructions. If an add
287 // exists, this is probably a part of a more complex GEP, so we don't
288 // want to mess around with the cast.
289 //
290 bool HasAddUse = false;
291 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
292 I != E; ++I)
293 if (isa<Instruction>(*I) &&
294 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
295 HasAddUse = true; break;
296 }
297
298 // If it doesn't have an add use, check to see if the dest type is
299 // losslessly convertable to one of the types in the start of the struct
300 // type.
301 //
302 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000303 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000304 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000305 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000306 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000307
308 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000309 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000310 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
311 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000312 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
313 // Check for a zero element struct type... if we have one, bail.
314 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000315
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000316 // Grab the first element of the struct type, which must lie at
317 // offset zero in the struct.
318 //
319 ElTy = CurSTy->getElementTypes()[0];
320 } else {
321 ElTy = cast<ArrayType>(CurCTy)->getElementType();
322 }
323
324 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000325 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000326
327 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000328 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000329
330 // Nope, go a level deeper.
331 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000332 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000333 ElTy = 0;
334 }
335
336 // Did we find what we were looking for? If so, do the transformation
337 if (ElTy) {
338 PRINT_PEEPHOLE1("cast-for-first:in", CI);
339
Chris Lattnere99c66b2001-11-01 17:05:27 +0000340 // Insert the new T cast instruction... stealing old T's name
341 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
342 CI->getName());
343 CI->setName("");
344 BI = BB->getInstList().insert(BI, GEP)+1;
345
346 // Make the old cast instruction reference the new GEP instead of
347 // the old src value.
348 //
349 CI->setOperand(0, GEP);
350
351 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
Chris Lattner3c019372002-05-10 15:29:25 +0000352 ++NumGEPInstFormed;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000353 return true;
354 }
355 }
356 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000357#endif
Chris Lattnere99c66b2001-11-01 17:05:27 +0000358
Chris Lattner3d775c32001-11-13 04:59:41 +0000359#if 1
Chris Lattner8d38e542001-11-01 03:12:34 +0000360 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
361 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000362 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000363
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000364 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000365 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
366 // store <T2> %V, <T2>* %t
367 //
368 // Into:
369 // %t = cast <T2> %V to <T1>
370 // store <T1> %t2, <T1>* %P
371 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000372 // Note: This is not taken care of by expr conversion because there might
373 // not be a cast available for the store to convert the incoming value of.
374 // This code is basically here to make sure that pointers don't have casts
375 // if possible.
376 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000377 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
378 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
379 if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000380 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000381 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000382 !SI->hasIndices()) { // No subscripts yet!
383 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
384
385 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000386 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000387 CI->getName());
388 CI->setName("");
389 BI = BB->getInstList().insert(BI, NCI)+1;
390
391 // Replace the old store with a new one!
392 ReplaceInstWithInst(BB->getInstList(), BI,
393 SI = new StoreInst(NCI, CastSrc));
394 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
Chris Lattner3c019372002-05-10 15:29:25 +0000395 ++NumLoadStorePeepholes;
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000396 return true;
397 }
398
Chris Lattnerd32a9612001-11-01 02:42:08 +0000399 } else if (I->getOpcode() == Instruction::Add &&
400 isa<CastInst>(I->getOperand(1))) {
401
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000402 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
Chris Lattner3c019372002-05-10 15:29:25 +0000403 cast<CastInst>(I->getOperand(1)))) {
404 ++NumGEPInstFormed;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000405 return true;
Chris Lattner3c019372002-05-10 15:29:25 +0000406 }
Chris Lattner3d775c32001-11-13 04:59:41 +0000407#endif
Chris Lattnerd32a9612001-11-01 02:42:08 +0000408 }
409
410 return false;
411}
412
413
414
415
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000416static bool DoRaisePass(Function *F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000417 bool Changed = false;
Chris Lattner237e6d12002-04-08 22:03:00 +0000418 for (Function::iterator MI = F->begin(), ME = F->end(); MI != ME; ++MI) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000419 BasicBlock *BB = *MI;
420 BasicBlock::InstListType &BIL = BB->getInstList();
421
422 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000423#if DEBUG_PEEPHOLE_INSTS
424 cerr << "Processing: " << *BI;
425#endif
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000426 if (dceInstruction(BIL, BI) || doConstantPropogation(BB, BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000427 Changed = true;
Chris Lattner3c019372002-05-10 15:29:25 +0000428 ++NumDCEorCP;
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000429#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnerb24196f2002-03-11 22:19:48 +0000430 cerr << "***\t\t^^-- DeadCode Elinated!\n";
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000431#endif
432 } else if (PeepholeOptimize(BB, BI))
Chris Lattnerd32a9612001-11-01 02:42:08 +0000433 Changed = true;
434 else
435 ++BI;
436 }
437 }
438 return Changed;
439}
440
441
Chris Lattnerf57b8452002-04-27 06:56:12 +0000442// RaisePointerReferences::doit - Raise a function representation to a higher
Chris Lattnerd32a9612001-11-01 02:42:08 +0000443// level.
444//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000445static bool doRPR(Function *F) {
Chris Lattner68b07b72001-11-01 07:00:51 +0000446#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000447 cerr << "\n\n\nStarting to work on Function '" << F->getName() << "'\n";
Chris Lattner68b07b72001-11-01 07:00:51 +0000448#endif
449
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000450 // Insert casts for all incoming pointer pointer values that are treated as
451 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000452 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000453 bool Changed = false, LocalChange;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000454
Chris Lattner4b770a32001-12-04 08:12:53 +0000455 do {
Chris Lattnera8b6d432001-12-05 06:34:00 +0000456#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000457 cerr << "Looping: \n" << F;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000458#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000459
Chris Lattnerf57b8452002-04-27 06:56:12 +0000460 // Iterate over the function, refining it, until it converges on a stable
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000461 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000462 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000463 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000464 Changed |= LocalChange;
465
466 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000467
468 return Changed;
469}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000470
471namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000472 struct RaisePointerReferences : public FunctionPass {
Chris Lattner96c466b2002-04-29 14:57:45 +0000473 const char *getPassName() const { return "Raise Pointer References"; }
474
Chris Lattnerf57b8452002-04-27 06:56:12 +0000475 virtual bool runOnFunction(Function *F) { return doRPR(F); }
Chris Lattner97e52e42002-04-28 21:27:06 +0000476
477 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
478 AU.preservesCFG();
479 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000480 };
481}
482
483Pass *createRaisePointerReferencesPass() {
484 return new RaisePointerReferences();
485}
486
487