blob: b0bae9755844024a1e9f359b7270692757ccf4bf [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 Lattnerd32a9612001-11-01 02:42:08 +000029#define PRINT_PEEPHOLE(ID, NUM, I) \
Chris Lattnerb3abf9d2002-05-22 17:27:12 +000030 DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I)
Chris Lattnerd32a9612001-11-01 02:42:08 +000031
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 Lattnera8b6d432001-12-05 06:34:00 +000052// Peephole optimize the following instructions:
53// %t1 = cast ? to x *
54// %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand
55//
56// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
57// %t2 = cast <eltype> * %t3 to {<...>}*
58//
59static bool HandleCastToPointer(BasicBlock::iterator BI,
60 const PointerType *DestPTy) {
61 CastInst *CI = cast<CastInst>(*BI);
Chris Lattner45ef5c22002-03-21 06:22:23 +000062 if (CI->use_empty()) return false;
Chris Lattnerf3b976e2001-11-04 20:21:12 +000063
Chris Lattnera8b6d432001-12-05 06:34:00 +000064 // Scan all of the uses, looking for any uses that are not add
65 // instructions. If we have non-adds, do not make this transformation.
66 //
67 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
68 I != E; ++I) {
69 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
70 if (BO->getOpcode() != Instruction::Add)
71 return false;
72 } else {
73 return false;
74 }
75 }
76
Chris Lattner697954c2002-01-20 22:54:45 +000077 std::vector<Value*> Indices;
Chris Lattnera8b6d432001-12-05 06:34:00 +000078 Value *Src = CI->getOperand(0);
79 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
Chris Lattnera8b6d432001-12-05 06:34:00 +000080 if (Result == 0) return false; // Not convertable...
81
82 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
83
84 // If we have a getelementptr capability... transform all of the
85 // add instruction uses into getelementptr's.
Chris Lattnerb24196f2002-03-11 22:19:48 +000086 while (!CI->use_empty()) {
Chris Lattner45ef5c22002-03-21 06:22:23 +000087 BinaryOperator *I = cast<BinaryOperator>(*CI->use_begin());
Chris Lattnera8b6d432001-12-05 06:34:00 +000088 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
89 "Use is not a valid add instruction!");
90
91 // Get the value added to the cast result pointer...
92 Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0);
93
Chris Lattner45ef5c22002-03-21 06:22:23 +000094 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
Chris Lattnera8b6d432001-12-05 06:34:00 +000095 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
Chris Lattner45ef5c22002-03-21 06:22:23 +000096
97 if (GEP->getType() == I->getType()) {
98 // Replace the old add instruction with the shiny new GEP inst
99 ReplaceInstWithInst(I, GEP);
100 } else {
101 // If the type produced by the gep instruction differs from the original
102 // add instruction type, insert a cast now.
103 //
104
105 // Insert the GEP instruction before the old add instruction... and get an
106 // iterator to point at the add instruction...
107 BasicBlock::iterator GEPI = InsertInstBeforeInst(GEP, I)+1;
108
109 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
110 CastInst *CI = new CastInst(GEP, I->getType());
111 GEP = CI;
112
113 // Replace the old add instruction with the shiny new GEP inst
114 ReplaceInstWithInst(I->getParent()->getInstList(), GEPI, GEP);
115 }
116
Chris Lattnera8b6d432001-12-05 06:34:00 +0000117 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
118 }
119 return true;
120}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000121
122// Peephole optimize the following instructions:
123// %t1 = cast ulong <const int> to {<...>} *
124// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
125//
126// or
127// %t1 = cast {<...>}* %SP to int*
128// %t5 = cast ulong <const int> to int*
129// %t2 = add int* %t1, %t5 ;; int is same size as field
130//
131// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
132// %t2 = cast <eltype> * %t3 to {<...>}*
133//
134static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
135 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000136 const CompositeType *CompTy;
137 Value *OffsetVal = AddOp2->getOperand(0);
138 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000139
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000140 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000141 SrcPtr = AddOp1; // Handle the first case...
142 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
143 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000144 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000145 }
146
147 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000148 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000149 return false;
150
Chris Lattner697954c2002-01-20 22:54:45 +0000151 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000152 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
153 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000154
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000155 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000156 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
157 } else {
158 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
159 }
160
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000161 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
162 AddOp2->getName());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000163 BI = BB->getInstList().insert(BI, GEP)+1;
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000164
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000165 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000166 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
167 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
168 return true;
169}
170
Chris Lattnerd32a9612001-11-01 02:42:08 +0000171static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
172 Instruction *I = *BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000173
174 if (CastInst *CI = dyn_cast<CastInst>(I)) {
175 Value *Src = CI->getOperand(0);
176 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
177 const Type *DestTy = CI->getType();
178
Chris Lattnere99c66b2001-11-01 17:05:27 +0000179 // Peephole optimize the following instruction:
180 // %V2 = cast <ty> %V to <ty>
181 //
182 // Into: <nothing>
183 //
184 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000185 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
186 CI->replaceAllUsesWith(Src);
187 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000188 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000189 CI->setName("");
190 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000191 }
Chris Lattner3c019372002-05-10 15:29:25 +0000192
193 // DCE the instruction now, to avoid having the iterative version of DCE
194 // have to worry about it.
195 //
196 delete BB->getInstList().remove(BI);
197
198 ++NumCastOfCast;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000199 return true;
200 }
201
Chris Lattnerd32a9612001-11-01 02:42:08 +0000202 // Check to see if it's a cast of an instruction that does not depend on the
203 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000204 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000205 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000206
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000207 // Check to see if we can convert the source of the cast to match the
208 // destination type of the cast...
Chris Lattnera8b6d432001-12-05 06:34:00 +0000209 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000210 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000211 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000212 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000213
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000214 DEBUG(cerr << "\nCONVERTING SRC EXPR TYPE:\n");
Chris Lattnera8b6d432001-12-05 06:34:00 +0000215 ValueMapCache ValueMap;
216 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
217 if (Constant *CPV = dyn_cast<Constant>(E))
218 CI->replaceAllUsesWith(CPV);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000219
Chris Lattnera8b6d432001-12-05 06:34:00 +0000220 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
221 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000222 DEBUG(cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent());
Chris Lattner3c019372002-05-10 15:29:25 +0000223 ++NumExprTreesConv;
Chris Lattnerd5543802001-12-14 16:37:52 +0000224 return true;
225 }
226
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000227 // Check to see if we can convert the users of the cast value to match the
228 // source type of the cast...
Chris Lattnerd5543802001-12-14 16:37:52 +0000229 //
230 ConvertedTypes.clear();
231 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
232 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
233
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000234 DEBUG(cerr << "\nCONVERTING EXPR TYPE:\n");
Chris Lattnerd5543802001-12-14 16:37:52 +0000235 ValueMapCache ValueMap;
236 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
237
238 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
239 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000240 DEBUG(cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent());
Chris Lattner3c019372002-05-10 15:29:25 +0000241 ++NumExprTreesConv;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000242 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000243 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000244 }
245
246 // Otherwise find out it this cast is a cast to a pointer type, which is
247 // then added to some other pointer, then loaded or stored through. If
248 // so, convert the add into a getelementptr instruction...
249 //
250 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
251 if (HandleCastToPointer(BI, DestPTy)) {
252 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000253 ++NumGEPInstFormed;
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000254 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000255 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000256 }
257
Chris Lattnere99c66b2001-11-01 17:05:27 +0000258 // Check to see if we are casting from a structure pointer to a pointer to
259 // the first element of the structure... to avoid munching other peepholes,
260 // we only let this happen if there are no add uses of the cast.
261 //
262 // Peephole optimize the following instructions:
263 // %t1 = cast {<...>} * %StructPtr to <ty> *
264 //
265 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
266 // %t1 = cast <eltype> * %t1 to <ty> *
267 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000268 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000269 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
270
271 // Loop over uses of the cast, checking for add instructions. If an add
272 // exists, this is probably a part of a more complex GEP, so we don't
273 // want to mess around with the cast.
274 //
275 bool HasAddUse = false;
276 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
277 I != E; ++I)
278 if (isa<Instruction>(*I) &&
279 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
280 HasAddUse = true; break;
281 }
282
283 // If it doesn't have an add use, check to see if the dest type is
284 // losslessly convertable to one of the types in the start of the struct
285 // type.
286 //
287 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000288 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000289 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000290 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000291 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000292
293 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000294 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000295 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
296 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000297 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
298 // Check for a zero element struct type... if we have one, bail.
299 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000300
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000301 // Grab the first element of the struct type, which must lie at
302 // offset zero in the struct.
303 //
304 ElTy = CurSTy->getElementTypes()[0];
305 } else {
306 ElTy = cast<ArrayType>(CurCTy)->getElementType();
307 }
308
309 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000310 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000311
312 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000313 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000314
315 // Nope, go a level deeper.
316 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000317 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000318 ElTy = 0;
319 }
320
321 // Did we find what we were looking for? If so, do the transformation
322 if (ElTy) {
323 PRINT_PEEPHOLE1("cast-for-first:in", CI);
324
Chris Lattnere99c66b2001-11-01 17:05:27 +0000325 // Insert the new T cast instruction... stealing old T's name
326 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
327 CI->getName());
328 CI->setName("");
329 BI = BB->getInstList().insert(BI, GEP)+1;
330
331 // Make the old cast instruction reference the new GEP instead of
332 // the old src value.
333 //
334 CI->setOperand(0, GEP);
335
336 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
Chris Lattner3c019372002-05-10 15:29:25 +0000337 ++NumGEPInstFormed;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000338 return true;
339 }
340 }
341 }
Chris Lattnere99c66b2001-11-01 17:05:27 +0000342
Chris Lattner8d38e542001-11-01 03:12:34 +0000343 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
344 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000345 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000346
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000347 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000348 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
349 // store <T2> %V, <T2>* %t
350 //
351 // Into:
352 // %t = cast <T2> %V to <T1>
353 // store <T1> %t2, <T1>* %P
354 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000355 // Note: This is not taken care of by expr conversion because there might
356 // not be a cast available for the store to convert the incoming value of.
357 // This code is basically here to make sure that pointers don't have casts
358 // if possible.
359 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000360 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
361 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
362 if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000363 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000364 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000365 !SI->hasIndices()) { // No subscripts yet!
366 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
367
368 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000369 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000370 CI->getName());
371 CI->setName("");
372 BI = BB->getInstList().insert(BI, NCI)+1;
373
374 // Replace the old store with a new one!
375 ReplaceInstWithInst(BB->getInstList(), BI,
376 SI = new StoreInst(NCI, CastSrc));
377 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
Chris Lattner3c019372002-05-10 15:29:25 +0000378 ++NumLoadStorePeepholes;
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000379 return true;
380 }
381
Chris Lattnerc99428f2002-05-14 05:23:45 +0000382 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
383 Value *Pointer = LI->getOperand(0);
384 const Type *PtrElType =
385 cast<PointerType>(Pointer->getType())->getElementType();
386
387 // Peephole optimize the following instructions:
388 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertable to T2
389 // %t = load <T2>* %P
390 //
391 // Into:
392 // %t = load <T1>* %P
393 // %Val = cast <T1> to <T2>
394 //
395 // Note: This is not taken care of by expr conversion because there might
396 // not be a cast available for the store to convert the incoming value of.
397 // This code is basically here to make sure that pointers don't have casts
398 // if possible.
399 //
400 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
401 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
402 if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
403 // convertable types?
404 if (PtrElType->isLosslesslyConvertableTo(CSPT->getElementType()) &&
405 !LI->hasIndices()) { // No subscripts yet!
406 PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI);
407
408 // Create the new load instruction... loading the pre-casted value
409 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName());
410
411 // Insert the new T cast instruction... stealing old T's name
412 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName());
413 BI = BB->getInstList().insert(BI, NewLI)+1;
414
415 // Replace the old store with a new one!
416 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
417 PRINT_PEEPHOLE3("load-src-cast:out", NCI, CastSrc, NewLI);
418 ++NumLoadStorePeepholes;
419 return true;
420 }
421
Chris Lattnerd32a9612001-11-01 02:42:08 +0000422 } else if (I->getOpcode() == Instruction::Add &&
423 isa<CastInst>(I->getOperand(1))) {
424
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000425 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
Chris Lattner3c019372002-05-10 15:29:25 +0000426 cast<CastInst>(I->getOperand(1)))) {
427 ++NumGEPInstFormed;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000428 return true;
Chris Lattner3c019372002-05-10 15:29:25 +0000429 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000430 }
431
432 return false;
433}
434
435
436
437
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000438static bool DoRaisePass(Function *F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000439 bool Changed = false;
Chris Lattner237e6d12002-04-08 22:03:00 +0000440 for (Function::iterator MI = F->begin(), ME = F->end(); MI != ME; ++MI) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000441 BasicBlock *BB = *MI;
442 BasicBlock::InstListType &BIL = BB->getInstList();
443
444 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000445 DEBUG(cerr << "Processing: " << *BI);
Chris Lattner16da4942002-05-26 20:18:18 +0000446 if (dceInstruction(BI) || doConstantPropogation(BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000447 Changed = true;
Chris Lattner3c019372002-05-10 15:29:25 +0000448 ++NumDCEorCP;
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000449 DEBUG(cerr << "***\t\t^^-- DeadCode Elinated!\n");
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000450 } else if (PeepholeOptimize(BB, BI))
Chris Lattnerd32a9612001-11-01 02:42:08 +0000451 Changed = true;
452 else
453 ++BI;
454 }
455 }
456 return Changed;
457}
458
459
Chris Lattnerf57b8452002-04-27 06:56:12 +0000460// RaisePointerReferences::doit - Raise a function representation to a higher
Chris Lattnerd32a9612001-11-01 02:42:08 +0000461// level.
462//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000463static bool doRPR(Function *F) {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000464 DEBUG(cerr << "\n\n\nStarting to work on Function '" << F->getName()<< "'\n");
Chris Lattner68b07b72001-11-01 07:00:51 +0000465
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000466 // Insert casts for all incoming pointer pointer values that are treated as
467 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000468 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000469 bool Changed = false, LocalChange;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000470
Chris Lattner4b770a32001-12-04 08:12:53 +0000471 do {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000472 DEBUG(cerr << "Looping: \n" << F);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000473
Chris Lattnerf57b8452002-04-27 06:56:12 +0000474 // Iterate over the function, refining it, until it converges on a stable
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000475 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000476 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000477 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000478 Changed |= LocalChange;
479
480 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000481
482 return Changed;
483}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000484
485namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000486 struct RaisePointerReferences : public FunctionPass {
Chris Lattner96c466b2002-04-29 14:57:45 +0000487 const char *getPassName() const { return "Raise Pointer References"; }
488
Chris Lattnerf57b8452002-04-27 06:56:12 +0000489 virtual bool runOnFunction(Function *F) { return doRPR(F); }
Chris Lattner97e52e42002-04-28 21:27:06 +0000490
491 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
492 AU.preservesCFG();
493 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000494 };
495}
496
497Pass *createRaisePointerReferencesPass() {
498 return new RaisePointerReferences();
499}
500
501