blob: 51d863d10df9cef5388340ff51046cb0f1b96b4b [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 Lattner968ddc92002-04-08 20:18:09 +000016#include "llvm/ConstantHandling.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000017#include "llvm/Transforms/Scalar/DCE.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000018#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
Chris Lattner09009802001-11-26 19:36:58 +000023//#define DEBUG_PEEPHOLE_INSTS 1
Chris Lattnerd32a9612001-11-01 02:42:08 +000024
25#ifdef DEBUG_PEEPHOLE_INSTS
26#define PRINT_PEEPHOLE(ID, NUM, I) \
Chris Lattnerebcd28e2002-03-21 03:02:07 +000027 std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I;
Chris Lattnerd32a9612001-11-01 02:42:08 +000028#else
29#define PRINT_PEEPHOLE(ID, NUM, I)
30#endif
31
32#define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
33#define PRINT_PEEPHOLE2(ID, I1, I2) \
34 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
35#define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
36 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
37 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000038#define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \
39 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
40 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0)
Chris Lattnerd32a9612001-11-01 02:42:08 +000041
42
Chris Lattnerd32a9612001-11-01 02:42:08 +000043// isReinterpretingCast - Return true if the cast instruction specified will
44// cause the operand to be "reinterpreted". A value is reinterpreted if the
45// cast instruction would cause the underlying bits to change.
46//
47static inline bool isReinterpretingCast(const CastInst *CI) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +000048 return!CI->getOperand(0)->getType()->isLosslesslyConvertableTo(CI->getType());
Chris Lattnerd32a9612001-11-01 02:42:08 +000049}
50
51
Chris Lattnerf3b976e2001-11-04 20:21:12 +000052
Chris Lattnera8b6d432001-12-05 06:34:00 +000053// Peephole optimize the following instructions:
54// %t1 = cast ? to x *
55// %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand
56//
57// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
58// %t2 = cast <eltype> * %t3 to {<...>}*
59//
60static bool HandleCastToPointer(BasicBlock::iterator BI,
61 const PointerType *DestPTy) {
62 CastInst *CI = cast<CastInst>(*BI);
Chris Lattner45ef5c22002-03-21 06:22:23 +000063 if (CI->use_empty()) return false;
Chris Lattnerf3b976e2001-11-04 20:21:12 +000064
Chris Lattnera8b6d432001-12-05 06:34:00 +000065 // Scan all of the uses, looking for any uses that are not add
66 // instructions. If we have non-adds, do not make this transformation.
67 //
68 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
69 I != E; ++I) {
70 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
71 if (BO->getOpcode() != Instruction::Add)
72 return false;
73 } else {
74 return false;
75 }
76 }
77
Chris Lattner697954c2002-01-20 22:54:45 +000078 std::vector<Value*> Indices;
Chris Lattnera8b6d432001-12-05 06:34:00 +000079 Value *Src = CI->getOperand(0);
80 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
Chris Lattnera8b6d432001-12-05 06:34:00 +000081 if (Result == 0) return false; // Not convertable...
82
83 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
84
85 // If we have a getelementptr capability... transform all of the
86 // add instruction uses into getelementptr's.
Chris Lattnerb24196f2002-03-11 22:19:48 +000087 while (!CI->use_empty()) {
Chris Lattner45ef5c22002-03-21 06:22:23 +000088 BinaryOperator *I = cast<BinaryOperator>(*CI->use_begin());
Chris Lattnera8b6d432001-12-05 06:34:00 +000089 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
90 "Use is not a valid add instruction!");
91
92 // Get the value added to the cast result pointer...
93 Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0);
94
Chris Lattner45ef5c22002-03-21 06:22:23 +000095 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
Chris Lattnera8b6d432001-12-05 06:34:00 +000096 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
Chris Lattner45ef5c22002-03-21 06:22:23 +000097
98 if (GEP->getType() == I->getType()) {
99 // Replace the old add instruction with the shiny new GEP inst
100 ReplaceInstWithInst(I, GEP);
101 } else {
102 // If the type produced by the gep instruction differs from the original
103 // add instruction type, insert a cast now.
104 //
105
106 // Insert the GEP instruction before the old add instruction... and get an
107 // iterator to point at the add instruction...
108 BasicBlock::iterator GEPI = InsertInstBeforeInst(GEP, I)+1;
109
110 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
111 CastInst *CI = new CastInst(GEP, I->getType());
112 GEP = CI;
113
114 // Replace the old add instruction with the shiny new GEP inst
115 ReplaceInstWithInst(I->getParent()->getInstList(), GEPI, GEP);
116 }
117
Chris Lattnera8b6d432001-12-05 06:34:00 +0000118 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
119 }
120 return true;
121}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000122
123// Peephole optimize the following instructions:
124// %t1 = cast ulong <const int> to {<...>} *
125// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
126//
127// or
128// %t1 = cast {<...>}* %SP to int*
129// %t5 = cast ulong <const int> to int*
130// %t2 = add int* %t1, %t5 ;; int is same size as field
131//
132// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
133// %t2 = cast <eltype> * %t3 to {<...>}*
134//
135static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
136 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000137 const CompositeType *CompTy;
138 Value *OffsetVal = AddOp2->getOperand(0);
139 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000140
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000141 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000142 SrcPtr = AddOp1; // Handle the first case...
143 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
144 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000145 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000146 }
147
148 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000149 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000150 return false;
151
Chris Lattner697954c2002-01-20 22:54:45 +0000152 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000153 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
154 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000155
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000156 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000157 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
158 } else {
159 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
160 }
161
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000162 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
163 AddOp2->getName());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000164 BI = BB->getInstList().insert(BI, GEP)+1;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000165
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000166 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000167 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
168 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
169 return true;
170}
171
Chris Lattnerd32a9612001-11-01 02:42:08 +0000172static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
173 Instruction *I = *BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000174
175 if (CastInst *CI = dyn_cast<CastInst>(I)) {
176 Value *Src = CI->getOperand(0);
177 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
178 const Type *DestTy = CI->getType();
179
Chris Lattnere99c66b2001-11-01 17:05:27 +0000180 // Peephole optimize the following instruction:
181 // %V2 = cast <ty> %V to <ty>
182 //
183 // Into: <nothing>
184 //
185 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000186 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
187 CI->replaceAllUsesWith(Src);
188 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000189 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000190 CI->setName("");
191 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000192 }
193 return true;
194 }
195
Chris Lattnere99c66b2001-11-01 17:05:27 +0000196 // Peephole optimize the following instructions:
197 // %tmp = cast <ty> %V to <ty2>
Chris Lattnera8b6d432001-12-05 06:34:00 +0000198 // %V = cast <ty2> %tmp to <ty3> ; Where ty & ty2 are same size
Chris Lattnere99c66b2001-11-01 17:05:27 +0000199 //
200 // Into: cast <ty> %V to <ty3>
201 //
Chris Lattnerd32a9612001-11-01 02:42:08 +0000202 if (SrcI)
203 if (CastInst *CSrc = dyn_cast<CastInst>(SrcI))
204 if (isReinterpretingCast(CI) + isReinterpretingCast(CSrc) < 2) {
205 // We can only do c-c elimination if, at most, one cast does a
206 // reinterpretation of the input data.
207 //
208 // If legal, make this cast refer the the original casts argument!
209 //
210 PRINT_PEEPHOLE2("cast-cast:in ", CI, CSrc);
211 CI->setOperand(0, CSrc->getOperand(0));
212 PRINT_PEEPHOLE1("cast-cast:out", CI);
213 return true;
214 }
215
216 // Check to see if it's a cast of an instruction that does not depend on the
217 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000218 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000219 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000220
221 // Check to see if we can convert the users of the cast value to match the
222 // source type of the cast...
223 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000224 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000225 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000226 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000227
228#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnera8b6d432001-12-05 06:34:00 +0000229 cerr << "\nCONVERTING SRC EXPR TYPE:\n";
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000230#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000231 ValueMapCache ValueMap;
232 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
233 if (Constant *CPV = dyn_cast<Constant>(E))
234 CI->replaceAllUsesWith(CPV);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000235
Chris Lattnera8b6d432001-12-05 06:34:00 +0000236 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
237 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000238#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnerd5543802001-12-14 16:37:52 +0000239 cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent();
240#endif
241 return true;
242 }
243
244 // Check to see if we can convert the source of the cast to match the
245 // destination type of the cast...
246 //
247 ConvertedTypes.clear();
248 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
249 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
250
251#ifdef DEBUG_PEEPHOLE_INSTS
252 cerr << "\nCONVERTING EXPR TYPE:\n";
253#endif
254 ValueMapCache ValueMap;
255 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
256
257 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
258 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
259#ifdef DEBUG_PEEPHOLE_INSTS
260 cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent();
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000261#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000262 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000263 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000264 }
265
266 // Otherwise find out it this cast is a cast to a pointer type, which is
267 // then added to some other pointer, then loaded or stored through. If
268 // so, convert the add into a getelementptr instruction...
269 //
270 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
271 if (HandleCastToPointer(BI, DestPTy)) {
272 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
273 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000274 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000275 }
276
Chris Lattnere99c66b2001-11-01 17:05:27 +0000277 // Check to see if we are casting from a structure pointer to a pointer to
278 // the first element of the structure... to avoid munching other peepholes,
279 // we only let this happen if there are no add uses of the cast.
280 //
281 // Peephole optimize the following instructions:
282 // %t1 = cast {<...>} * %StructPtr to <ty> *
283 //
284 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
285 // %t1 = cast <eltype> * %t1 to <ty> *
286 //
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000287#if 1
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000288 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000289 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
290
291 // Loop over uses of the cast, checking for add instructions. If an add
292 // exists, this is probably a part of a more complex GEP, so we don't
293 // want to mess around with the cast.
294 //
295 bool HasAddUse = false;
296 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
297 I != E; ++I)
298 if (isa<Instruction>(*I) &&
299 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
300 HasAddUse = true; break;
301 }
302
303 // If it doesn't have an add use, check to see if the dest type is
304 // losslessly convertable to one of the types in the start of the struct
305 // type.
306 //
307 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000308 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000309 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000310 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000311 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000312
313 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000314 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000315 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
316 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000317 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
318 // Check for a zero element struct type... if we have one, bail.
319 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000320
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000321 // Grab the first element of the struct type, which must lie at
322 // offset zero in the struct.
323 //
324 ElTy = CurSTy->getElementTypes()[0];
325 } else {
326 ElTy = cast<ArrayType>(CurCTy)->getElementType();
327 }
328
329 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000330 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000331
332 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000333 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000334
335 // Nope, go a level deeper.
336 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000337 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000338 ElTy = 0;
339 }
340
341 // Did we find what we were looking for? If so, do the transformation
342 if (ElTy) {
343 PRINT_PEEPHOLE1("cast-for-first:in", CI);
344
Chris Lattnere99c66b2001-11-01 17:05:27 +0000345 // Insert the new T cast instruction... stealing old T's name
346 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
347 CI->getName());
348 CI->setName("");
349 BI = BB->getInstList().insert(BI, GEP)+1;
350
351 // Make the old cast instruction reference the new GEP instead of
352 // the old src value.
353 //
354 CI->setOperand(0, GEP);
355
356 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
357 return true;
358 }
359 }
360 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000361#endif
Chris Lattnere99c66b2001-11-01 17:05:27 +0000362
Chris Lattner3d775c32001-11-13 04:59:41 +0000363#if 1
Chris Lattner8d38e542001-11-01 03:12:34 +0000364 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
365 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000366 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000367
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000368 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000369 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
370 // store <T2> %V, <T2>* %t
371 //
372 // Into:
373 // %t = cast <T2> %V to <T1>
374 // store <T1> %t2, <T1>* %P
375 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000376 // Note: This is not taken care of by expr conversion because there might
377 // not be a cast available for the store to convert the incoming value of.
378 // This code is basically here to make sure that pointers don't have casts
379 // if possible.
380 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000381 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
382 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
383 if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000384 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000385 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000386 !SI->hasIndices()) { // No subscripts yet!
387 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
388
389 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000390 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000391 CI->getName());
392 CI->setName("");
393 BI = BB->getInstList().insert(BI, NCI)+1;
394
395 // Replace the old store with a new one!
396 ReplaceInstWithInst(BB->getInstList(), BI,
397 SI = new StoreInst(NCI, CastSrc));
398 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
399 return true;
400 }
401
Chris Lattnerd32a9612001-11-01 02:42:08 +0000402 } else if (I->getOpcode() == Instruction::Add &&
403 isa<CastInst>(I->getOperand(1))) {
404
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000405 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
406 cast<CastInst>(I->getOperand(1))))
Chris Lattnerd32a9612001-11-01 02:42:08 +0000407 return true;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000408
Chris Lattner3d775c32001-11-13 04:59:41 +0000409#endif
Chris Lattnerd32a9612001-11-01 02:42:08 +0000410 }
411
412 return false;
413}
414
415
416
417
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000418static bool DoRaisePass(Function *F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000419 bool Changed = false;
Chris Lattner237e6d12002-04-08 22:03:00 +0000420 for (Function::iterator MI = F->begin(), ME = F->end(); MI != ME; ++MI) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000421 BasicBlock *BB = *MI;
422 BasicBlock::InstListType &BIL = BB->getInstList();
423
424 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000425#if DEBUG_PEEPHOLE_INSTS
426 cerr << "Processing: " << *BI;
427#endif
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000428 if (dceInstruction(BIL, BI) || doConstantPropogation(BB, BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000429 Changed = true;
430#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattnerb24196f2002-03-11 22:19:48 +0000431 cerr << "***\t\t^^-- DeadCode Elinated!\n";
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000432#endif
433 } else if (PeepholeOptimize(BB, BI))
Chris Lattnerd32a9612001-11-01 02:42:08 +0000434 Changed = true;
435 else
436 ++BI;
437 }
438 }
439 return Changed;
440}
441
442
443// RaisePointerReferences::doit - Raise a method representation to a higher
444// level.
445//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000446static bool doRPR(Function *F) {
Chris Lattner68b07b72001-11-01 07:00:51 +0000447#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000448 cerr << "\n\n\nStarting to work on Function '" << F->getName() << "'\n";
Chris Lattner68b07b72001-11-01 07:00:51 +0000449#endif
450
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000451 // Insert casts for all incoming pointer pointer values that are treated as
452 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000453 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000454 bool Changed = false, LocalChange;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000455
Chris Lattner4b770a32001-12-04 08:12:53 +0000456 do {
Chris Lattnera8b6d432001-12-05 06:34:00 +0000457#ifdef DEBUG_PEEPHOLE_INSTS
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000458 cerr << "Looping: \n" << F;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000459#endif
Chris Lattnera8b6d432001-12-05 06:34:00 +0000460
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000461 // Iterate over the method, refining it, until it converges on a stable
462 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000463 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000464 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000465 Changed |= LocalChange;
466
467 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000468
469 return Changed;
470}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000471
472namespace {
473 struct RaisePointerReferences : public MethodPass {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000474 virtual bool runOnMethod(Function *F) { return doRPR(F); }
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000475 };
476}
477
478Pass *createRaisePointerReferencesPass() {
479 return new RaisePointerReferences();
480}
481
482