blob: aecf78dc648165b3f28f5ee6515224e2ccb7fdc8 [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 Lattner59cd9f12001-11-04 23:24:06 +000010#include "TransformInternals.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000011#include "llvm/Function.h"
Chris Lattnerd32a9612001-11-01 02:42:08 +000012#include "llvm/iOther.h"
13#include "llvm/iMemory.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000014#include "llvm/ConstantVals.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000015#include "llvm/Pass.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000016#include "llvm/Transforms/Scalar/DCE.h"
17#include "llvm/Transforms/Scalar/ConstantHandling.h"
18#include "llvm/Transforms/Scalar/ConstantProp.h"
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000019#include "llvm/Analysis/Expressions.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/STLExtras.h"
Chris Lattnerd32a9612001-11-01 02:42:08 +000021#include <algorithm>
22
23#include "llvm/Assembly/Writer.h"
24
Chris Lattner09009802001-11-26 19:36:58 +000025//#define DEBUG_PEEPHOLE_INSTS 1
Chris Lattnerd32a9612001-11-01 02:42:08 +000026
27#ifdef DEBUG_PEEPHOLE_INSTS
28#define PRINT_PEEPHOLE(ID, NUM, I) \
Chris Lattnerebcd28e2002-03-21 03:02:07 +000029 std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I;
Chris Lattnerd32a9612001-11-01 02:42:08 +000030#else
31#define PRINT_PEEPHOLE(ID, NUM, I)
32#endif
33
34#define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
35#define PRINT_PEEPHOLE2(ID, I1, I2) \
36 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
37#define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
38 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
39 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000040#define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \
41 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
42 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0)
Chris Lattnerd32a9612001-11-01 02:42:08 +000043
44
Chris Lattnerd32a9612001-11-01 02:42:08 +000045// isReinterpretingCast - Return true if the cast instruction specified will
46// cause the operand to be "reinterpreted". A value is reinterpreted if the
47// cast instruction would cause the underlying bits to change.
48//
49static inline bool isReinterpretingCast(const CastInst *CI) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +000050 return!CI->getOperand(0)->getType()->isLosslesslyConvertableTo(CI->getType());
Chris Lattnerd32a9612001-11-01 02:42:08 +000051}
52
53
Chris Lattnerf3b976e2001-11-04 20:21:12 +000054
Chris Lattnera8b6d432001-12-05 06:34:00 +000055// Peephole optimize the following instructions:
56// %t1 = cast ? to x *
57// %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand
58//
59// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
60// %t2 = cast <eltype> * %t3 to {<...>}*
61//
62static bool HandleCastToPointer(BasicBlock::iterator BI,
63 const PointerType *DestPTy) {
64 CastInst *CI = cast<CastInst>(*BI);
Chris Lattner45ef5c22002-03-21 06:22:23 +000065 if (CI->use_empty()) return false;
Chris Lattnerf3b976e2001-11-04 20:21:12 +000066
Chris Lattnera8b6d432001-12-05 06:34:00 +000067 // Scan all of the uses, looking for any uses that are not add
68 // instructions. If we have non-adds, do not make this transformation.
69 //
70 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
71 I != E; ++I) {
72 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
73 if (BO->getOpcode() != Instruction::Add)
74 return false;
75 } else {
76 return false;
77 }
78 }
79
Chris Lattner697954c2002-01-20 22:54:45 +000080 std::vector<Value*> Indices;
Chris Lattnera8b6d432001-12-05 06:34:00 +000081 Value *Src = CI->getOperand(0);
82 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
Chris Lattnera8b6d432001-12-05 06:34:00 +000083 if (Result == 0) return false; // Not convertable...
84
85 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
86
87 // If we have a getelementptr capability... transform all of the
88 // add instruction uses into getelementptr's.
Chris Lattnerb24196f2002-03-11 22:19:48 +000089 while (!CI->use_empty()) {
Chris Lattner45ef5c22002-03-21 06:22:23 +000090 BinaryOperator *I = cast<BinaryOperator>(*CI->use_begin());
Chris Lattnera8b6d432001-12-05 06:34:00 +000091 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
92 "Use is not a valid add instruction!");
93
94 // Get the value added to the cast result pointer...
95 Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0);
96
Chris Lattner45ef5c22002-03-21 06:22:23 +000097 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
Chris Lattnera8b6d432001-12-05 06:34:00 +000098 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
Chris Lattner45ef5c22002-03-21 06:22:23 +000099
100 if (GEP->getType() == I->getType()) {
101 // Replace the old add instruction with the shiny new GEP inst
102 ReplaceInstWithInst(I, GEP);
103 } else {
104 // If the type produced by the gep instruction differs from the original
105 // add instruction type, insert a cast now.
106 //
107
108 // Insert the GEP instruction before the old add instruction... and get an
109 // iterator to point at the add instruction...
110 BasicBlock::iterator GEPI = InsertInstBeforeInst(GEP, I)+1;
111
112 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
113 CastInst *CI = new CastInst(GEP, I->getType());
114 GEP = CI;
115
116 // Replace the old add instruction with the shiny new GEP inst
117 ReplaceInstWithInst(I->getParent()->getInstList(), GEPI, GEP);
118 }
119
Chris Lattnera8b6d432001-12-05 06:34:00 +0000120 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
121 }
122 return true;
123}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000124
125// Peephole optimize the following instructions:
126// %t1 = cast ulong <const int> to {<...>} *
127// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
128//
129// or
130// %t1 = cast {<...>}* %SP to int*
131// %t5 = cast ulong <const int> to int*
132// %t2 = add int* %t1, %t5 ;; int is same size as field
133//
134// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
135// %t2 = cast <eltype> * %t3 to {<...>}*
136//
137static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
138 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000139 const CompositeType *CompTy;
140 Value *OffsetVal = AddOp2->getOperand(0);
141 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000142
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000143 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000144 SrcPtr = AddOp1; // Handle the first case...
145 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
146 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000147 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000148 }
149
150 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000151 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000152 return false;
153
Chris Lattner697954c2002-01-20 22:54:45 +0000154 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000155 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
156 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000157
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000158 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000159 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
160 } else {
161 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
162 }
163
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000164 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
165 AddOp2->getName());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000166 BI = BB->getInstList().insert(BI, GEP)+1;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000167
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000168 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000169 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
170 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
171 return true;
172}
173
Chris Lattnerd32a9612001-11-01 02:42:08 +0000174static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
175 Instruction *I = *BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000176
177 if (CastInst *CI = dyn_cast<CastInst>(I)) {
178 Value *Src = CI->getOperand(0);
179 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
180 const Type *DestTy = CI->getType();
181
Chris Lattnere99c66b2001-11-01 17:05:27 +0000182 // Peephole optimize the following instruction:
183 // %V2 = cast <ty> %V to <ty>
184 //
185 // Into: <nothing>
186 //
187 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000188 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
189 CI->replaceAllUsesWith(Src);
190 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000191 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000192 CI->setName("");
193 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000194 }
195 return true;
196 }
197
Chris Lattnere99c66b2001-11-01 17:05:27 +0000198 // Peephole optimize the following instructions:
199 // %tmp = cast <ty> %V to <ty2>
Chris Lattnera8b6d432001-12-05 06:34:00 +0000200 // %V = cast <ty2> %tmp to <ty3> ; Where ty & ty2 are same size
Chris Lattnere99c66b2001-11-01 17:05:27 +0000201 //
202 // Into: cast <ty> %V to <ty3>
203 //
Chris Lattnerd32a9612001-11-01 02:42:08 +0000204 if (SrcI)
205 if (CastInst *CSrc = dyn_cast<CastInst>(SrcI))
206 if (isReinterpretingCast(CI) + isReinterpretingCast(CSrc) < 2) {
207 // We can only do c-c elimination if, at most, one cast does a
208 // reinterpretation of the input data.
209 //
210 // If legal, make this cast refer the the original casts argument!
211 //
212 PRINT_PEEPHOLE2("cast-cast:in ", CI, CSrc);
213 CI->setOperand(0, CSrc->getOperand(0));
214 PRINT_PEEPHOLE1("cast-cast:out", CI);
215 return true;
216 }
217
218 // Check to see if it's a cast of an instruction that does not depend on the
219 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000220 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000221 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000222
223 // Check to see if we can convert the users of the cast value to match the
224 // source type of the cast...
225 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000226 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000227 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000228 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000229
230#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnera8b6d432001-12-05 06:34:00 +0000231 cerr << "\nCONVERTING SRC EXPR TYPE:\n";
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000232#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000233 ValueMapCache ValueMap;
234 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
235 if (Constant *CPV = dyn_cast<Constant>(E))
236 CI->replaceAllUsesWith(CPV);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000237
Chris Lattnera8b6d432001-12-05 06:34:00 +0000238 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
239 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000240#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnerd5543802001-12-14 16:37:52 +0000241 cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent();
242#endif
243 return true;
244 }
245
246 // Check to see if we can convert the source of the cast to match the
247 // destination type of the cast...
248 //
249 ConvertedTypes.clear();
250 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
251 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
252
253#ifdef DEBUG_PEEPHOLE_INSTS
254 cerr << "\nCONVERTING EXPR TYPE:\n";
255#endif
256 ValueMapCache ValueMap;
257 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
258
259 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
260 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
261#ifdef DEBUG_PEEPHOLE_INSTS
262 cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent();
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000263#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000264 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000265 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000266 }
267
268 // Otherwise find out it this cast is a cast to a pointer type, which is
269 // then added to some other pointer, then loaded or stored through. If
270 // so, convert the add into a getelementptr instruction...
271 //
272 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
273 if (HandleCastToPointer(BI, DestPTy)) {
274 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
275 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000276 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000277 }
278
Chris Lattnere99c66b2001-11-01 17:05:27 +0000279 // Check to see if we are casting from a structure pointer to a pointer to
280 // the first element of the structure... to avoid munching other peepholes,
281 // we only let this happen if there are no add uses of the cast.
282 //
283 // Peephole optimize the following instructions:
284 // %t1 = cast {<...>} * %StructPtr to <ty> *
285 //
286 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
287 // %t1 = cast <eltype> * %t1 to <ty> *
288 //
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000289#if 1
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000290 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000291 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
292
293 // Loop over uses of the cast, checking for add instructions. If an add
294 // exists, this is probably a part of a more complex GEP, so we don't
295 // want to mess around with the cast.
296 //
297 bool HasAddUse = false;
298 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
299 I != E; ++I)
300 if (isa<Instruction>(*I) &&
301 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
302 HasAddUse = true; break;
303 }
304
305 // If it doesn't have an add use, check to see if the dest type is
306 // losslessly convertable to one of the types in the start of the struct
307 // type.
308 //
309 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000310 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000311 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000312 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000313 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000314
315 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000316 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000317 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
318 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000319 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
320 // Check for a zero element struct type... if we have one, bail.
321 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000322
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000323 // Grab the first element of the struct type, which must lie at
324 // offset zero in the struct.
325 //
326 ElTy = CurSTy->getElementTypes()[0];
327 } else {
328 ElTy = cast<ArrayType>(CurCTy)->getElementType();
329 }
330
331 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000332 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000333
334 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000335 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000336
337 // Nope, go a level deeper.
338 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000339 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000340 ElTy = 0;
341 }
342
343 // Did we find what we were looking for? If so, do the transformation
344 if (ElTy) {
345 PRINT_PEEPHOLE1("cast-for-first:in", CI);
346
Chris Lattnere99c66b2001-11-01 17:05:27 +0000347 // Insert the new T cast instruction... stealing old T's name
348 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
349 CI->getName());
350 CI->setName("");
351 BI = BB->getInstList().insert(BI, GEP)+1;
352
353 // Make the old cast instruction reference the new GEP instead of
354 // the old src value.
355 //
356 CI->setOperand(0, GEP);
357
358 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
359 return true;
360 }
361 }
362 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000363#endif
Chris Lattnere99c66b2001-11-01 17:05:27 +0000364
Chris Lattner3d775c32001-11-13 04:59:41 +0000365#if 1
Chris Lattner8d38e542001-11-01 03:12:34 +0000366 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
367 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000368 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000369
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000370 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000371 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
372 // store <T2> %V, <T2>* %t
373 //
374 // Into:
375 // %t = cast <T2> %V to <T1>
376 // store <T1> %t2, <T1>* %P
377 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000378 // Note: This is not taken care of by expr conversion because there might
379 // not be a cast available for the store to convert the incoming value of.
380 // This code is basically here to make sure that pointers don't have casts
381 // if possible.
382 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000383 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
384 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
385 if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000386 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000387 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000388 !SI->hasIndices()) { // No subscripts yet!
389 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
390
391 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000392 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000393 CI->getName());
394 CI->setName("");
395 BI = BB->getInstList().insert(BI, NCI)+1;
396
397 // Replace the old store with a new one!
398 ReplaceInstWithInst(BB->getInstList(), BI,
399 SI = new StoreInst(NCI, CastSrc));
400 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
401 return true;
402 }
403
Chris Lattnerd32a9612001-11-01 02:42:08 +0000404 } else if (I->getOpcode() == Instruction::Add &&
405 isa<CastInst>(I->getOperand(1))) {
406
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000407 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
408 cast<CastInst>(I->getOperand(1))))
Chris Lattnerd32a9612001-11-01 02:42:08 +0000409 return true;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000410
Chris Lattner3d775c32001-11-13 04:59:41 +0000411#endif
Chris Lattnerd32a9612001-11-01 02:42:08 +0000412 }
413
414 return false;
415}
416
417
418
419
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000420static bool DoRaisePass(Function *F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000421 bool Changed = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000422 for (Method::iterator MI = F->begin(), ME = F->end(); MI != ME; ++MI) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000423 BasicBlock *BB = *MI;
424 BasicBlock::InstListType &BIL = BB->getInstList();
425
426 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000427#if DEBUG_PEEPHOLE_INSTS
428 cerr << "Processing: " << *BI;
429#endif
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000430 if (dceInstruction(BIL, BI) || doConstantPropogation(BB, BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000431 Changed = true;
432#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnerb24196f2002-03-11 22:19:48 +0000433 cerr << "***\t\t^^-- DeadCode Elinated!\n";
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000434#endif
435 } else if (PeepholeOptimize(BB, BI))
Chris Lattnerd32a9612001-11-01 02:42:08 +0000436 Changed = true;
437 else
438 ++BI;
439 }
440 }
441 return Changed;
442}
443
444
445// RaisePointerReferences::doit - Raise a method representation to a higher
446// level.
447//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000448static bool doRPR(Function *F) {
Chris Lattner68b07b72001-11-01 07:00:51 +0000449#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000450 cerr << "\n\n\nStarting to work on Function '" << F->getName() << "'\n";
Chris Lattner68b07b72001-11-01 07:00:51 +0000451#endif
452
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000453 // Insert casts for all incoming pointer pointer values that are treated as
454 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000455 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000456 bool Changed = false, LocalChange;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000457
Chris Lattner4b770a32001-12-04 08:12:53 +0000458 do {
Chris Lattnera8b6d432001-12-05 06:34:00 +0000459#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000460 cerr << "Looping: \n" << F;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000461#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000462
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000463 // Iterate over the method, refining it, until it converges on a stable
464 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000465 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000466 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000467 Changed |= LocalChange;
468
469 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000470
471 return Changed;
472}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000473
474namespace {
475 struct RaisePointerReferences : public MethodPass {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000476 virtual bool runOnMethod(Function *F) { return doRPR(F); }
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000477 };
478}
479
480Pass *createRaisePointerReferencesPass() {
481 return new RaisePointerReferences();
482}
483
484