blob: 2eafcf05ee85d5c3ddb9d48218a20d68cc521a44 [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 Lattnerd32a9612001-11-01 02:42:08 +000011#include "llvm/Method.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 Lattnerf3b976e2001-11-04 20:21:12 +000065
Chris Lattnera8b6d432001-12-05 06:34:00 +000066 // Scan all of the uses, looking for any uses that are not add
67 // instructions. If we have non-adds, do not make this transformation.
68 //
69 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
70 I != E; ++I) {
71 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
72 if (BO->getOpcode() != Instruction::Add)
73 return false;
74 } else {
75 return false;
76 }
77 }
78
Chris Lattner697954c2002-01-20 22:54:45 +000079 std::vector<Value*> Indices;
Chris Lattnera8b6d432001-12-05 06:34:00 +000080 Value *Src = CI->getOperand(0);
81 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
Chris Lattnera8b6d432001-12-05 06:34:00 +000082 if (Result == 0) return false; // Not convertable...
83
84 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
85
86 // If we have a getelementptr capability... transform all of the
87 // add instruction uses into getelementptr's.
Chris Lattnerb24196f2002-03-11 22:19:48 +000088 while (!CI->use_empty()) {
89 Instruction *I = cast<Instruction>(*CI->use_begin());
Chris Lattnera8b6d432001-12-05 06:34:00 +000090 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
91 "Use is not a valid add instruction!");
92
93 // Get the value added to the cast result pointer...
94 Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0);
95
Chris Lattnerf17a09d2001-12-06 18:06:13 +000096 GetElementPtrInst *GEP = new GetElementPtrInst(OtherPtr, Indices);
Chris Lattnera8b6d432001-12-05 06:34:00 +000097 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
98
99 // Replace the old add instruction with the shiny new GEP inst
Chris Lattnerb24196f2002-03-11 22:19:48 +0000100 ReplaceInstWithInst(I, GEP);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000101 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
102 }
103 return true;
104}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000105
106// Peephole optimize the following instructions:
107// %t1 = cast ulong <const int> to {<...>} *
108// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
109//
110// or
111// %t1 = cast {<...>}* %SP to int*
112// %t5 = cast ulong <const int> to int*
113// %t2 = add int* %t1, %t5 ;; int is same size as field
114//
115// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
116// %t2 = cast <eltype> * %t3 to {<...>}*
117//
118static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
119 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000120 const CompositeType *CompTy;
121 Value *OffsetVal = AddOp2->getOperand(0);
122 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000123
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000124 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000125 SrcPtr = AddOp1; // Handle the first case...
126 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
127 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000128 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000129 }
130
131 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000132 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000133 return false;
134
Chris Lattner697954c2002-01-20 22:54:45 +0000135 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000136 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
137 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000138
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000139 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000140 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
141 } else {
142 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
143 }
144
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000145 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
146 AddOp2->getName());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000147 BI = BB->getInstList().insert(BI, GEP)+1;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000148
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000149 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000150 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
151 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
152 return true;
153}
154
Chris Lattnerd32a9612001-11-01 02:42:08 +0000155static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
156 Instruction *I = *BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000157
158 if (CastInst *CI = dyn_cast<CastInst>(I)) {
159 Value *Src = CI->getOperand(0);
160 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
161 const Type *DestTy = CI->getType();
162
Chris Lattnere99c66b2001-11-01 17:05:27 +0000163 // Peephole optimize the following instruction:
164 // %V2 = cast <ty> %V to <ty>
165 //
166 // Into: <nothing>
167 //
168 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000169 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
170 CI->replaceAllUsesWith(Src);
171 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000172 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000173 CI->setName("");
174 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000175 }
176 return true;
177 }
178
Chris Lattnere99c66b2001-11-01 17:05:27 +0000179 // Peephole optimize the following instructions:
180 // %tmp = cast <ty> %V to <ty2>
Chris Lattnera8b6d432001-12-05 06:34:00 +0000181 // %V = cast <ty2> %tmp to <ty3> ; Where ty & ty2 are same size
Chris Lattnere99c66b2001-11-01 17:05:27 +0000182 //
183 // Into: cast <ty> %V to <ty3>
184 //
Chris Lattnerd32a9612001-11-01 02:42:08 +0000185 if (SrcI)
186 if (CastInst *CSrc = dyn_cast<CastInst>(SrcI))
187 if (isReinterpretingCast(CI) + isReinterpretingCast(CSrc) < 2) {
188 // We can only do c-c elimination if, at most, one cast does a
189 // reinterpretation of the input data.
190 //
191 // If legal, make this cast refer the the original casts argument!
192 //
193 PRINT_PEEPHOLE2("cast-cast:in ", CI, CSrc);
194 CI->setOperand(0, CSrc->getOperand(0));
195 PRINT_PEEPHOLE1("cast-cast:out", CI);
196 return true;
197 }
198
199 // Check to see if it's a cast of an instruction that does not depend on the
200 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000201 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000202 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000203
204 // Check to see if we can convert the users of the cast value to match the
205 // source type of the cast...
206 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000207 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000208 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000209 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000210
211#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnera8b6d432001-12-05 06:34:00 +0000212 cerr << "\nCONVERTING SRC EXPR TYPE:\n";
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000213#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000214 ValueMapCache ValueMap;
215 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
216 if (Constant *CPV = dyn_cast<Constant>(E))
217 CI->replaceAllUsesWith(CPV);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000218
Chris Lattnera8b6d432001-12-05 06:34:00 +0000219 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
220 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000221#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnerd5543802001-12-14 16:37:52 +0000222 cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent();
223#endif
224 return true;
225 }
226
227 // Check to see if we can convert the source of the cast to match the
228 // destination type of the cast...
229 //
230 ConvertedTypes.clear();
231 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
232 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
233
234#ifdef DEBUG_PEEPHOLE_INSTS
235 cerr << "\nCONVERTING EXPR TYPE:\n";
236#endif
237 ValueMapCache ValueMap;
238 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
239
240 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
241 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
242#ifdef DEBUG_PEEPHOLE_INSTS
243 cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent();
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000244#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000245 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000246 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000247 }
248
249 // Otherwise find out it this cast is a cast to a pointer type, which is
250 // then added to some other pointer, then loaded or stored through. If
251 // so, convert the add into a getelementptr instruction...
252 //
253 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
254 if (HandleCastToPointer(BI, DestPTy)) {
255 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
256 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000257 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000258 }
259
Chris Lattnere99c66b2001-11-01 17:05:27 +0000260 // Check to see if we are casting from a structure pointer to a pointer to
261 // the first element of the structure... to avoid munching other peepholes,
262 // we only let this happen if there are no add uses of the cast.
263 //
264 // Peephole optimize the following instructions:
265 // %t1 = cast {<...>} * %StructPtr to <ty> *
266 //
267 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
268 // %t1 = cast <eltype> * %t1 to <ty> *
269 //
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000270#if 1
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000271 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000272 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
273
274 // Loop over uses of the cast, checking for add instructions. If an add
275 // exists, this is probably a part of a more complex GEP, so we don't
276 // want to mess around with the cast.
277 //
278 bool HasAddUse = false;
279 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
280 I != E; ++I)
281 if (isa<Instruction>(*I) &&
282 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
283 HasAddUse = true; break;
284 }
285
286 // If it doesn't have an add use, check to see if the dest type is
287 // losslessly convertable to one of the types in the start of the struct
288 // type.
289 //
290 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000291 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000292 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000293 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000294 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000295
296 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000297 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000298 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
299 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000300 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
301 // Check for a zero element struct type... if we have one, bail.
302 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000303
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000304 // Grab the first element of the struct type, which must lie at
305 // offset zero in the struct.
306 //
307 ElTy = CurSTy->getElementTypes()[0];
308 } else {
309 ElTy = cast<ArrayType>(CurCTy)->getElementType();
310 }
311
312 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000314
315 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000316 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000317
318 // Nope, go a level deeper.
319 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000320 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000321 ElTy = 0;
322 }
323
324 // Did we find what we were looking for? If so, do the transformation
325 if (ElTy) {
326 PRINT_PEEPHOLE1("cast-for-first:in", CI);
327
Chris Lattnere99c66b2001-11-01 17:05:27 +0000328 // Insert the new T cast instruction... stealing old T's name
329 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
330 CI->getName());
331 CI->setName("");
332 BI = BB->getInstList().insert(BI, GEP)+1;
333
334 // Make the old cast instruction reference the new GEP instead of
335 // the old src value.
336 //
337 CI->setOperand(0, GEP);
338
339 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
340 return true;
341 }
342 }
343 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000344#endif
Chris Lattnere99c66b2001-11-01 17:05:27 +0000345
Chris Lattner3d775c32001-11-13 04:59:41 +0000346#if 1
Chris Lattner8d38e542001-11-01 03:12:34 +0000347 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
348 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000349 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000350
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000351 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000352 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
353 // store <T2> %V, <T2>* %t
354 //
355 // Into:
356 // %t = cast <T2> %V to <T1>
357 // store <T1> %t2, <T1>* %P
358 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000359 // Note: This is not taken care of by expr conversion because there might
360 // not be a cast available for the store to convert the incoming value of.
361 // This code is basically here to make sure that pointers don't have casts
362 // if possible.
363 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000364 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
365 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
366 if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000367 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000368 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000369 !SI->hasIndices()) { // No subscripts yet!
370 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
371
372 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000373 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000374 CI->getName());
375 CI->setName("");
376 BI = BB->getInstList().insert(BI, NCI)+1;
377
378 // Replace the old store with a new one!
379 ReplaceInstWithInst(BB->getInstList(), BI,
380 SI = new StoreInst(NCI, CastSrc));
381 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
382 return true;
383 }
384
Chris Lattnerd32a9612001-11-01 02:42:08 +0000385 } else if (I->getOpcode() == Instruction::Add &&
386 isa<CastInst>(I->getOperand(1))) {
387
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000388 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
389 cast<CastInst>(I->getOperand(1))))
Chris Lattnerd32a9612001-11-01 02:42:08 +0000390 return true;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000391
Chris Lattner3d775c32001-11-13 04:59:41 +0000392#endif
Chris Lattnerd32a9612001-11-01 02:42:08 +0000393 }
394
395 return false;
396}
397
398
399
400
401static bool DoRaisePass(Method *M) {
402 bool Changed = false;
403 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
404 BasicBlock *BB = *MI;
405 BasicBlock::InstListType &BIL = BB->getInstList();
406
407 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000408#if DEBUG_PEEPHOLE_INSTS
409 cerr << "Processing: " << *BI;
410#endif
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000411 if (dceInstruction(BIL, BI) || doConstantPropogation(BB, BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000412 Changed = true;
413#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnerb24196f2002-03-11 22:19:48 +0000414 cerr << "***\t\t^^-- DeadCode Elinated!\n";
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000415#endif
416 } else if (PeepholeOptimize(BB, BI))
Chris Lattnerd32a9612001-11-01 02:42:08 +0000417 Changed = true;
418 else
419 ++BI;
420 }
421 }
422 return Changed;
423}
424
425
426// RaisePointerReferences::doit - Raise a method representation to a higher
427// level.
428//
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000429static bool doRPR(Method *M) {
Chris Lattner68b07b72001-11-01 07:00:51 +0000430#ifdef DEBUG_PEEPHOLE_INSTS
431 cerr << "\n\n\nStarting to work on Method '" << M->getName() << "'\n";
432#endif
433
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000434 // Insert casts for all incoming pointer pointer values that are treated as
435 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000436 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000437 bool Changed = false, LocalChange;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000438
Chris Lattner4b770a32001-12-04 08:12:53 +0000439 do {
Chris Lattnera8b6d432001-12-05 06:34:00 +0000440#ifdef DEBUG_PEEPHOLE_INSTS
441 cerr << "Looping: \n" << M;
442#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000443
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000444 // Iterate over the method, refining it, until it converges on a stable
445 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000446 LocalChange = false;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000447 while (DoRaisePass(M)) LocalChange = true;
448 Changed |= LocalChange;
449
450 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000451
452 return Changed;
453}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000454
455namespace {
456 struct RaisePointerReferences : public MethodPass {
457 virtual bool runOnMethod(Method *M) { return doRPR(M); }
458 };
459}
460
461Pass *createRaisePointerReferencesPass() {
462 return new RaisePointerReferences();
463}
464
465