blob: afc2ad5d7a670bcf0ad7b7acc15e421e8b751556 [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>
Anand Shuklacfb22d32002-06-25 20:55:50 +000021using std::cerr;
Chris Lattnerd32a9612001-11-01 02:42:08 +000022
Chris Lattner3c019372002-05-10 15:29:25 +000023static Statistic<> NumLoadStorePeepholes("raise\t\t- Number of load/store peepholes");
24static Statistic<> NumGEPInstFormed("raise\t\t- Number of other getelementptr's formed");
25static Statistic<> NumExprTreesConv("raise\t\t- Number of expression trees converted");
26static Statistic<> NumCastOfCast("raise\t\t- Number of cast-of-self removed");
27static Statistic<> NumDCEorCP("raise\t\t- Number of insts DCE'd or constprop'd");
28
29
Chris Lattnerd32a9612001-11-01 02:42:08 +000030#define PRINT_PEEPHOLE(ID, NUM, I) \
Chris Lattnerb3abf9d2002-05-22 17:27:12 +000031 DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I)
Chris Lattnerd32a9612001-11-01 02:42:08 +000032
33#define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
34#define PRINT_PEEPHOLE2(ID, I1, I2) \
35 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
36#define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
37 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
38 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000039#define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \
40 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
41 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0)
Chris Lattnerd32a9612001-11-01 02:42:08 +000042
43
Chris Lattnerd32a9612001-11-01 02:42:08 +000044// isReinterpretingCast - Return true if the cast instruction specified will
45// cause the operand to be "reinterpreted". A value is reinterpreted if the
46// cast instruction would cause the underlying bits to change.
47//
48static inline bool isReinterpretingCast(const CastInst *CI) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +000049 return!CI->getOperand(0)->getType()->isLosslesslyConvertableTo(CI->getType());
Chris Lattnerd32a9612001-11-01 02:42:08 +000050}
51
52
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) {
Chris Lattner7e708292002-06-25 16:13:24 +000062 CastInst &CI = cast<CastInst>(*BI);
63 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 //
Chris Lattner7e708292002-06-25 16:13:24 +000068 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end();
Chris Lattnera8b6d432001-12-05 06:34:00 +000069 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 Lattner7e708292002-06-25 16:13:24 +000079 Value *Src = CI.getOperand(0);
Chris Lattnera8b6d432001-12-05 06:34:00 +000080 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 Lattner7e708292002-06-25 16:13:24 +000087 while (!CI.use_empty()) {
88 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...
Chris Lattner7e708292002-06-25 16:13:24 +000093 Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0);
Chris Lattnera8b6d432001-12-05 06:34:00 +000094
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
Chris Lattner7e708292002-06-25 16:13:24 +0000106 // Insert the GEP instruction before the old add instruction...
107 I->getParent()->getInstList().insert(I, GEP);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000108
109 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
Chris Lattner7e708292002-06-25 16:13:24 +0000110 GEP = new CastInst(GEP, I->getType());
Chris Lattner45ef5c22002-03-21 06:22:23 +0000111
112 // Replace the old add instruction with the shiny new GEP inst
Chris Lattner7e708292002-06-25 16:13:24 +0000113 ReplaceInstWithInst(I, GEP);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000114 }
115
Chris Lattnera8b6d432001-12-05 06:34:00 +0000116 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
117 }
118 return true;
119}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000120
121// Peephole optimize the following instructions:
122// %t1 = cast ulong <const int> to {<...>} *
123// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
124//
125// or
126// %t1 = cast {<...>}* %SP to int*
127// %t5 = cast ulong <const int> to int*
128// %t2 = add int* %t1, %t5 ;; int is same size as field
129//
130// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
131// %t2 = cast <eltype> * %t3 to {<...>}*
132//
133static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
134 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000135 const CompositeType *CompTy;
136 Value *OffsetVal = AddOp2->getOperand(0);
137 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000138
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000139 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000140 SrcPtr = AddOp1; // Handle the first case...
141 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
142 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000143 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000144 }
145
146 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000147 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000148 return false;
149
Chris Lattner697954c2002-01-20 22:54:45 +0000150 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000151 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
152 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000153
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000154 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000155 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
156 } else {
157 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
158 }
159
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000160 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
161 AddOp2->getName());
Chris Lattner7e708292002-06-25 16:13:24 +0000162 BI = ++BB->getInstList().insert(BI, GEP);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000163
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000164 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000165 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
166 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
167 return true;
168}
169
Chris Lattnerd32a9612001-11-01 02:42:08 +0000170static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000171 Instruction *I = BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000172
173 if (CastInst *CI = dyn_cast<CastInst>(I)) {
174 Value *Src = CI->getOperand(0);
175 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
176 const Type *DestTy = CI->getType();
177
Chris Lattnere99c66b2001-11-01 17:05:27 +0000178 // Peephole optimize the following instruction:
179 // %V2 = cast <ty> %V to <ty>
180 //
181 // Into: <nothing>
182 //
183 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000184 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
185 CI->replaceAllUsesWith(Src);
186 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000187 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000188 CI->setName("");
189 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000190 }
Chris Lattner3c019372002-05-10 15:29:25 +0000191
192 // DCE the instruction now, to avoid having the iterative version of DCE
193 // have to worry about it.
194 //
Chris Lattner7e708292002-06-25 16:13:24 +0000195 BI = BB->getInstList().erase(BI);
Chris Lattner3c019372002-05-10 15:29:25 +0000196
197 ++NumCastOfCast;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000198 return true;
199 }
200
Chris Lattnerd32a9612001-11-01 02:42:08 +0000201 // Check to see if it's a cast of an instruction that does not depend on the
202 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000203 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000204 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000205
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000206 // Check to see if we can convert the source of the cast to match the
207 // destination type of the cast...
Chris Lattnera8b6d432001-12-05 06:34:00 +0000208 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000209 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000210 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000211 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000212
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000213 DEBUG(cerr << "\nCONVERTING SRC EXPR TYPE:\n");
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 Lattnerb3abf9d2002-05-22 17:27:12 +0000221 DEBUG(cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent());
Chris Lattner3c019372002-05-10 15:29:25 +0000222 ++NumExprTreesConv;
Chris Lattnerd5543802001-12-14 16:37:52 +0000223 return true;
224 }
225
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000226 // Check to see if we can convert the users of the cast value to match the
227 // source type of the cast...
Chris Lattnerd5543802001-12-14 16:37:52 +0000228 //
229 ConvertedTypes.clear();
230 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
231 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
232
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000233 DEBUG(cerr << "\nCONVERTING EXPR TYPE:\n");
Chris Lattnerd5543802001-12-14 16:37:52 +0000234 ValueMapCache ValueMap;
235 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
236
237 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
238 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000239 DEBUG(cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent());
Chris Lattner3c019372002-05-10 15:29:25 +0000240 ++NumExprTreesConv;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000241 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000242 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000243 }
244
245 // Otherwise find out it this cast is a cast to a pointer type, which is
246 // then added to some other pointer, then loaded or stored through. If
247 // so, convert the add into a getelementptr instruction...
248 //
249 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
250 if (HandleCastToPointer(BI, DestPTy)) {
251 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000252 ++NumGEPInstFormed;
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000253 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000254 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000255 }
256
Chris Lattnere99c66b2001-11-01 17:05:27 +0000257 // Check to see if we are casting from a structure pointer to a pointer to
258 // the first element of the structure... to avoid munching other peepholes,
259 // we only let this happen if there are no add uses of the cast.
260 //
261 // Peephole optimize the following instructions:
262 // %t1 = cast {<...>} * %StructPtr to <ty> *
263 //
264 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
265 // %t1 = cast <eltype> * %t1 to <ty> *
266 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000267 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000268 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
269
270 // Loop over uses of the cast, checking for add instructions. If an add
271 // exists, this is probably a part of a more complex GEP, so we don't
272 // want to mess around with the cast.
273 //
274 bool HasAddUse = false;
275 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
276 I != E; ++I)
277 if (isa<Instruction>(*I) &&
278 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
279 HasAddUse = true; break;
280 }
281
282 // If it doesn't have an add use, check to see if the dest type is
283 // losslessly convertable to one of the types in the start of the struct
284 // type.
285 //
286 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000287 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000288 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000289 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000290 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000291
292 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000293 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000294 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
295 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000296 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
297 // Check for a zero element struct type... if we have one, bail.
298 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000299
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000300 // Grab the first element of the struct type, which must lie at
301 // offset zero in the struct.
302 //
303 ElTy = CurSTy->getElementTypes()[0];
304 } else {
305 ElTy = cast<ArrayType>(CurCTy)->getElementType();
306 }
307
308 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000309 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000310
311 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000312 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000313
314 // Nope, go a level deeper.
315 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000316 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000317 ElTy = 0;
318 }
319
320 // Did we find what we were looking for? If so, do the transformation
321 if (ElTy) {
322 PRINT_PEEPHOLE1("cast-for-first:in", CI);
323
Chris Lattnere99c66b2001-11-01 17:05:27 +0000324 // Insert the new T cast instruction... stealing old T's name
325 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
326 CI->getName());
327 CI->setName("");
Chris Lattner7e708292002-06-25 16:13:24 +0000328 BI = ++BB->getInstList().insert(BI, GEP);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000329
330 // Make the old cast instruction reference the new GEP instead of
331 // the old src value.
332 //
333 CI->setOperand(0, GEP);
334
335 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
Chris Lattner3c019372002-05-10 15:29:25 +0000336 ++NumGEPInstFormed;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000337 return true;
338 }
339 }
340 }
Chris Lattnere99c66b2001-11-01 17:05:27 +0000341
Chris Lattner8d38e542001-11-01 03:12:34 +0000342 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
343 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000344 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000345
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000346 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000347 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
348 // store <T2> %V, <T2>* %t
349 //
350 // Into:
351 // %t = cast <T2> %V to <T1>
352 // store <T1> %t2, <T1>* %P
353 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000354 // Note: This is not taken care of by expr conversion because there might
355 // not be a cast available for the store to convert the incoming value of.
356 // This code is basically here to make sure that pointers don't have casts
357 // if possible.
358 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000359 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
360 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000361 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000362 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000363 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000364 !SI->hasIndices()) { // No subscripts yet!
365 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
366
367 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000368 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000369 CI->getName());
370 CI->setName("");
Chris Lattner7e708292002-06-25 16:13:24 +0000371 BI = ++BB->getInstList().insert(BI, NCI);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000372
373 // Replace the old store with a new one!
374 ReplaceInstWithInst(BB->getInstList(), BI,
375 SI = new StoreInst(NCI, CastSrc));
376 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
Chris Lattner3c019372002-05-10 15:29:25 +0000377 ++NumLoadStorePeepholes;
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000378 return true;
379 }
380
Chris Lattnerc99428f2002-05-14 05:23:45 +0000381 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
382 Value *Pointer = LI->getOperand(0);
383 const Type *PtrElType =
384 cast<PointerType>(Pointer->getType())->getElementType();
385
386 // Peephole optimize the following instructions:
387 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertable to T2
388 // %t = load <T2>* %P
389 //
390 // Into:
391 // %t = load <T1>* %P
392 // %Val = cast <T1> to <T2>
393 //
394 // Note: This is not taken care of by expr conversion because there might
395 // not be a cast available for the store to convert the incoming value of.
396 // This code is basically here to make sure that pointers don't have casts
397 // if possible.
398 //
399 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
400 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000401 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattnerc99428f2002-05-14 05:23:45 +0000402 // convertable types?
403 if (PtrElType->isLosslesslyConvertableTo(CSPT->getElementType()) &&
404 !LI->hasIndices()) { // No subscripts yet!
405 PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI);
406
407 // Create the new load instruction... loading the pre-casted value
408 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName());
409
410 // Insert the new T cast instruction... stealing old T's name
411 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName());
Chris Lattner7e708292002-06-25 16:13:24 +0000412 BI = ++BB->getInstList().insert(BI, NewLI);
Chris Lattnerc99428f2002-05-14 05:23:45 +0000413
414 // Replace the old store with a new one!
415 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
416 PRINT_PEEPHOLE3("load-src-cast:out", NCI, CastSrc, NewLI);
417 ++NumLoadStorePeepholes;
418 return true;
419 }
420
Chris Lattnerd32a9612001-11-01 02:42:08 +0000421 } else if (I->getOpcode() == Instruction::Add &&
422 isa<CastInst>(I->getOperand(1))) {
423
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000424 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
Chris Lattner3c019372002-05-10 15:29:25 +0000425 cast<CastInst>(I->getOperand(1)))) {
426 ++NumGEPInstFormed;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000427 return true;
Chris Lattner3c019372002-05-10 15:29:25 +0000428 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000429 }
430
431 return false;
432}
433
434
435
436
Chris Lattner7e708292002-06-25 16:13:24 +0000437static bool DoRaisePass(Function &F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000438 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000439 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattnerd32a9612001-11-01 02:42:08 +0000440 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000441 DEBUG(cerr << "Processing: " << *BI);
Chris Lattner16da4942002-05-26 20:18:18 +0000442 if (dceInstruction(BI) || doConstantPropogation(BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000443 Changed = true;
Chris Lattner3c019372002-05-10 15:29:25 +0000444 ++NumDCEorCP;
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000445 DEBUG(cerr << "***\t\t^^-- DeadCode Elinated!\n");
Chris Lattner7e708292002-06-25 16:13:24 +0000446 } else if (PeepholeOptimize(BB, BI)) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000447 Changed = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000448 } else {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000449 ++BI;
Chris Lattner7e708292002-06-25 16:13:24 +0000450 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000451 }
Chris Lattner7e708292002-06-25 16:13:24 +0000452
Chris Lattnerd32a9612001-11-01 02:42:08 +0000453 return Changed;
454}
455
456
Chris Lattnerf57b8452002-04-27 06:56:12 +0000457// RaisePointerReferences::doit - Raise a function representation to a higher
Chris Lattnerd32a9612001-11-01 02:42:08 +0000458// level.
459//
Chris Lattner7e708292002-06-25 16:13:24 +0000460static bool doRPR(Function &F) {
461 DEBUG(cerr << "\n\n\nStarting to work on Function '" << F.getName() << "'\n");
Chris Lattner68b07b72001-11-01 07:00:51 +0000462
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000463 // Insert casts for all incoming pointer pointer values that are treated as
464 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000465 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000466 bool Changed = false, LocalChange;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000467
Chris Lattner4b770a32001-12-04 08:12:53 +0000468 do {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000469 DEBUG(cerr << "Looping: \n" << F);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000470
Chris Lattnerf57b8452002-04-27 06:56:12 +0000471 // Iterate over the function, refining it, until it converges on a stable
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000472 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000473 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000474 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000475 Changed |= LocalChange;
476
477 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000478
479 return Changed;
480}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000481
482namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000483 struct RaisePointerReferences : public FunctionPass {
Chris Lattner96c466b2002-04-29 14:57:45 +0000484 const char *getPassName() const { return "Raise Pointer References"; }
485
Chris Lattner7e708292002-06-25 16:13:24 +0000486 virtual bool runOnFunction(Function &F) { return doRPR(F); }
Chris Lattner97e52e42002-04-28 21:27:06 +0000487
488 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
489 AU.preservesCFG();
490 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000491 };
492}
493
494Pass *createRaisePointerReferencesPass() {
495 return new RaisePointerReferences();
496}
497
498