blob: 7e27c5f7f0b9b44fbd4f6744407003d0d45d1ea1 [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 Lattner3378a5b2002-07-16 23:49:24 +000017#include "llvm/Analysis/Verifier.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000018#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/STLExtras.h"
Chris Lattner3c019372002-05-10 15:29:25 +000020#include "Support/StatisticReporter.h"
Chris Lattner3378a5b2002-07-16 23:49:24 +000021#include "Support/CommandLine.h"
Chris Lattnerd32a9612001-11-01 02:42:08 +000022#include <algorithm>
Anand Shuklacfb22d32002-06-25 20:55:50 +000023using std::cerr;
Chris Lattnerd32a9612001-11-01 02:42:08 +000024
Chris Lattner3378a5b2002-07-16 23:49:24 +000025// StartInst - This enables the -raise-start-inst=foo option to cause the level
26// raising pass to start at instruction "foo", which is immensely useful for
27// debugging!
28//
29static cl::String StartInst("raise-start-inst", "Start raise pass at the "
30 "instruction with the specified name", cl::Hidden);
31
32static Statistic<> NumLoadStorePeepholes("raise\t\t- Number of load/store "
33 "peepholes");
34static Statistic<> NumGEPInstFormed("raise\t\t- Number of other "
35 "getelementptr's formed");
36static Statistic<> NumExprTreesConv("raise\t\t- Number of expression trees"
37 " converted");
Chris Lattner3c019372002-05-10 15:29:25 +000038static Statistic<> NumCastOfCast("raise\t\t- Number of cast-of-self removed");
Chris Lattner3378a5b2002-07-16 23:49:24 +000039static Statistic<> NumDCEorCP("raise\t\t- Number of insts DCEd or constprop'd");
Chris Lattner3c019372002-05-10 15:29:25 +000040
41
Chris Lattnerd32a9612001-11-01 02:42:08 +000042#define PRINT_PEEPHOLE(ID, NUM, I) \
Chris Lattnerb3abf9d2002-05-22 17:27:12 +000043 DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I)
Chris Lattnerd32a9612001-11-01 02:42:08 +000044
45#define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
46#define PRINT_PEEPHOLE2(ID, I1, I2) \
47 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
48#define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
49 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
50 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000051#define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \
52 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
53 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0)
Chris Lattnerd32a9612001-11-01 02:42:08 +000054
55
Chris Lattnerd32a9612001-11-01 02:42:08 +000056// isReinterpretingCast - Return true if the cast instruction specified will
57// cause the operand to be "reinterpreted". A value is reinterpreted if the
58// cast instruction would cause the underlying bits to change.
59//
60static inline bool isReinterpretingCast(const CastInst *CI) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +000061 return!CI->getOperand(0)->getType()->isLosslesslyConvertableTo(CI->getType());
Chris Lattnerd32a9612001-11-01 02:42:08 +000062}
63
64
Chris Lattnera8b6d432001-12-05 06:34:00 +000065// Peephole optimize the following instructions:
66// %t1 = cast ? to x *
67// %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand
68//
69// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
70// %t2 = cast <eltype> * %t3 to {<...>}*
71//
72static bool HandleCastToPointer(BasicBlock::iterator BI,
73 const PointerType *DestPTy) {
Chris Lattner7e708292002-06-25 16:13:24 +000074 CastInst &CI = cast<CastInst>(*BI);
75 if (CI.use_empty()) return false;
Chris Lattnerf3b976e2001-11-04 20:21:12 +000076
Chris Lattnera8b6d432001-12-05 06:34:00 +000077 // Scan all of the uses, looking for any uses that are not add
78 // instructions. If we have non-adds, do not make this transformation.
79 //
Chris Lattner7e708292002-06-25 16:13:24 +000080 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end();
Chris Lattnera8b6d432001-12-05 06:34:00 +000081 I != E; ++I) {
82 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
Chris Lattner3fb2ddd2002-07-16 21:41:31 +000083 if (BO->getOpcode() != Instruction::Add ||
84 // Avoid add sbyte* %X, %X cases...
85 BO->getOperand(0) == BO->getOperand(1))
Chris Lattnera8b6d432001-12-05 06:34:00 +000086 return false;
87 } else {
88 return false;
89 }
90 }
91
Chris Lattner697954c2002-01-20 22:54:45 +000092 std::vector<Value*> Indices;
Chris Lattner7e708292002-06-25 16:13:24 +000093 Value *Src = CI.getOperand(0);
Chris Lattnera8b6d432001-12-05 06:34:00 +000094 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
Chris Lattnera8b6d432001-12-05 06:34:00 +000095 if (Result == 0) return false; // Not convertable...
96
97 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
98
99 // If we have a getelementptr capability... transform all of the
100 // add instruction uses into getelementptr's.
Chris Lattner7e708292002-06-25 16:13:24 +0000101 while (!CI.use_empty()) {
102 BinaryOperator *I = cast<BinaryOperator>(*CI.use_begin());
Chris Lattnera8b6d432001-12-05 06:34:00 +0000103 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
104 "Use is not a valid add instruction!");
105
106 // Get the value added to the cast result pointer...
Chris Lattner7e708292002-06-25 16:13:24 +0000107 Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000108
Chris Lattner45ef5c22002-03-21 06:22:23 +0000109 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
Chris Lattnera8b6d432001-12-05 06:34:00 +0000110 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000111
112 if (GEP->getType() == I->getType()) {
113 // Replace the old add instruction with the shiny new GEP inst
114 ReplaceInstWithInst(I, GEP);
115 } else {
116 // If the type produced by the gep instruction differs from the original
117 // add instruction type, insert a cast now.
118 //
119
Chris Lattner7e708292002-06-25 16:13:24 +0000120 // Insert the GEP instruction before the old add instruction...
121 I->getParent()->getInstList().insert(I, GEP);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000122
123 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
Chris Lattner7e708292002-06-25 16:13:24 +0000124 GEP = new CastInst(GEP, I->getType());
Chris Lattner45ef5c22002-03-21 06:22:23 +0000125
126 // Replace the old add instruction with the shiny new GEP inst
Chris Lattner7e708292002-06-25 16:13:24 +0000127 ReplaceInstWithInst(I, GEP);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000128 }
129
Chris Lattnera8b6d432001-12-05 06:34:00 +0000130 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
131 }
132 return true;
133}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000134
135// Peephole optimize the following instructions:
136// %t1 = cast ulong <const int> to {<...>} *
137// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
138//
139// or
140// %t1 = cast {<...>}* %SP to int*
141// %t5 = cast ulong <const int> to int*
142// %t2 = add int* %t1, %t5 ;; int is same size as field
143//
144// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
145// %t2 = cast <eltype> * %t3 to {<...>}*
146//
147static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
148 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000149 const CompositeType *CompTy;
150 Value *OffsetVal = AddOp2->getOperand(0);
151 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000152
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000153 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000154 SrcPtr = AddOp1; // Handle the first case...
155 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
156 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000157 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000158 }
159
160 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000161 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000162 return false;
163
Chris Lattner697954c2002-01-20 22:54:45 +0000164 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000165 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
166 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000167
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000168 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000169 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
170 } else {
171 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
172 }
173
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000174 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
175 AddOp2->getName());
Chris Lattner7e708292002-06-25 16:13:24 +0000176 BI = ++BB->getInstList().insert(BI, GEP);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000177
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000178 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000179 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
180 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
181 return true;
182}
183
Chris Lattnerd32a9612001-11-01 02:42:08 +0000184static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000185 Instruction *I = BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000186
187 if (CastInst *CI = dyn_cast<CastInst>(I)) {
188 Value *Src = CI->getOperand(0);
189 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
190 const Type *DestTy = CI->getType();
191
Chris Lattnere99c66b2001-11-01 17:05:27 +0000192 // Peephole optimize the following instruction:
193 // %V2 = cast <ty> %V to <ty>
194 //
195 // Into: <nothing>
196 //
197 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000198 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
199 CI->replaceAllUsesWith(Src);
200 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000201 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000202 CI->setName("");
203 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000204 }
Chris Lattner3c019372002-05-10 15:29:25 +0000205
206 // DCE the instruction now, to avoid having the iterative version of DCE
207 // have to worry about it.
208 //
Chris Lattner7e708292002-06-25 16:13:24 +0000209 BI = BB->getInstList().erase(BI);
Chris Lattner3c019372002-05-10 15:29:25 +0000210
211 ++NumCastOfCast;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000212 return true;
213 }
214
Chris Lattnerd32a9612001-11-01 02:42:08 +0000215 // Check to see if it's a cast of an instruction that does not depend on the
216 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000217 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000218 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000219
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000220 // Check to see if we can convert the source of the cast to match the
221 // destination type of the cast...
Chris Lattnera8b6d432001-12-05 06:34:00 +0000222 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000223 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000224 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000225 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000226
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000227 DEBUG(cerr << "\nCONVERTING SRC EXPR TYPE:\n");
Chris Lattnerb1b42622002-07-17 17:11:33 +0000228 { // ValueMap must be destroyed before function verified!
229 ValueMapCache ValueMap;
230 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000231
Chris Lattnerb1b42622002-07-17 17:11:33 +0000232 if (Constant *CPV = dyn_cast<Constant>(E))
233 CI->replaceAllUsesWith(CPV);
234
235 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
236 DEBUG(cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent());
237 }
Chris Lattner3378a5b2002-07-16 23:49:24 +0000238
239 DEBUG(assert(verifyFunction(*BB->getParent()) == false &&
240 "Function broken!"));
Chris Lattnerb1b42622002-07-17 17:11:33 +0000241 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000242 ++NumExprTreesConv;
Chris Lattnerd5543802001-12-14 16:37:52 +0000243 return true;
244 }
245
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000246 // Check to see if we can convert the users of the cast value to match the
247 // source type of the cast...
Chris Lattnerd5543802001-12-14 16:37:52 +0000248 //
249 ConvertedTypes.clear();
Chris Lattnera66c7bf2002-07-16 18:12:55 +0000250 ConvertedTypes[Src] = Src->getType(); // Make sure the source doesn't change type
Chris Lattnerd5543802001-12-14 16:37:52 +0000251 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
252 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
253
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000254 DEBUG(cerr << "\nCONVERTING EXPR TYPE:\n");
Chris Lattnerb1b42622002-07-17 17:11:33 +0000255 { // ValueMap must be destroyed before function verified!
256 ValueMapCache ValueMap;
257 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
258 }
Chris Lattnerd5543802001-12-14 16:37:52 +0000259
Chris Lattnerd5543802001-12-14 16:37:52 +0000260 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000261 DEBUG(cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent());
Chris Lattner3378a5b2002-07-16 23:49:24 +0000262
263 DEBUG(assert(verifyFunction(*BB->getParent()) == false &&
264 "Function broken!"));
Chris Lattnerb1b42622002-07-17 17:11:33 +0000265 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000266 ++NumExprTreesConv;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000267 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000268 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000269 }
270
271 // Otherwise find out it this cast is a cast to a pointer type, which is
272 // then added to some other pointer, then loaded or stored through. If
273 // so, convert the add into a getelementptr instruction...
274 //
275 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
276 if (HandleCastToPointer(BI, DestPTy)) {
277 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000278 ++NumGEPInstFormed;
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000279 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000280 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000281 }
282
Chris Lattnere99c66b2001-11-01 17:05:27 +0000283 // Check to see if we are casting from a structure pointer to a pointer to
284 // the first element of the structure... to avoid munching other peepholes,
285 // we only let this happen if there are no add uses of the cast.
286 //
287 // Peephole optimize the following instructions:
288 // %t1 = cast {<...>} * %StructPtr to <ty> *
289 //
290 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
291 // %t1 = cast <eltype> * %t1 to <ty> *
292 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000293 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000294 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
295
296 // Loop over uses of the cast, checking for add instructions. If an add
297 // exists, this is probably a part of a more complex GEP, so we don't
298 // want to mess around with the cast.
299 //
300 bool HasAddUse = false;
301 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
302 I != E; ++I)
303 if (isa<Instruction>(*I) &&
304 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
305 HasAddUse = true; break;
306 }
307
308 // If it doesn't have an add use, check to see if the dest type is
309 // losslessly convertable to one of the types in the start of the struct
310 // type.
311 //
312 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000313 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000314 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000315 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000316 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000317
318 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000319 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000320 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
321 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000322 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
323 // Check for a zero element struct type... if we have one, bail.
324 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000325
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000326 // Grab the first element of the struct type, which must lie at
327 // offset zero in the struct.
328 //
329 ElTy = CurSTy->getElementTypes()[0];
330 } else {
331 ElTy = cast<ArrayType>(CurCTy)->getElementType();
332 }
333
334 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000335 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000336
337 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000338 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000339
340 // Nope, go a level deeper.
341 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000342 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000343 ElTy = 0;
344 }
345
346 // Did we find what we were looking for? If so, do the transformation
347 if (ElTy) {
348 PRINT_PEEPHOLE1("cast-for-first:in", CI);
349
Chris Lattnere99c66b2001-11-01 17:05:27 +0000350 // Insert the new T cast instruction... stealing old T's name
351 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
352 CI->getName());
353 CI->setName("");
Chris Lattner7e708292002-06-25 16:13:24 +0000354 BI = ++BB->getInstList().insert(BI, GEP);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000355
356 // Make the old cast instruction reference the new GEP instead of
357 // the old src value.
358 //
359 CI->setOperand(0, GEP);
360
361 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
Chris Lattner3c019372002-05-10 15:29:25 +0000362 ++NumGEPInstFormed;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000363 return true;
364 }
365 }
366 }
Chris Lattnere99c66b2001-11-01 17:05:27 +0000367
Chris Lattner8d38e542001-11-01 03:12:34 +0000368 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
369 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000370 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000371
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000372 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000373 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
374 // store <T2> %V, <T2>* %t
375 //
376 // Into:
377 // %t = cast <T2> %V to <T1>
378 // store <T1> %t2, <T1>* %P
379 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000380 // Note: This is not taken care of by expr conversion because there might
381 // not be a cast available for the store to convert the incoming value of.
382 // This code is basically here to make sure that pointers don't have casts
383 // if possible.
384 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000385 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
386 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000387 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000388 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000389 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000390 !SI->hasIndices()) { // No subscripts yet!
391 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
392
393 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000394 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000395 CI->getName());
396 CI->setName("");
Chris Lattner7e708292002-06-25 16:13:24 +0000397 BI = ++BB->getInstList().insert(BI, NCI);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000398
399 // Replace the old store with a new one!
400 ReplaceInstWithInst(BB->getInstList(), BI,
401 SI = new StoreInst(NCI, CastSrc));
402 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
Chris Lattner3c019372002-05-10 15:29:25 +0000403 ++NumLoadStorePeepholes;
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000404 return true;
405 }
406
Chris Lattnerc99428f2002-05-14 05:23:45 +0000407 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
408 Value *Pointer = LI->getOperand(0);
409 const Type *PtrElType =
410 cast<PointerType>(Pointer->getType())->getElementType();
411
412 // Peephole optimize the following instructions:
413 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertable to T2
414 // %t = load <T2>* %P
415 //
416 // Into:
417 // %t = load <T1>* %P
418 // %Val = cast <T1> to <T2>
419 //
420 // Note: This is not taken care of by expr conversion because there might
421 // not be a cast available for the store to convert the incoming value of.
422 // This code is basically here to make sure that pointers don't have casts
423 // if possible.
424 //
425 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
426 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000427 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattnerc99428f2002-05-14 05:23:45 +0000428 // convertable types?
429 if (PtrElType->isLosslesslyConvertableTo(CSPT->getElementType()) &&
430 !LI->hasIndices()) { // No subscripts yet!
431 PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI);
432
433 // Create the new load instruction... loading the pre-casted value
434 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName());
435
436 // Insert the new T cast instruction... stealing old T's name
437 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName());
Chris Lattner7e708292002-06-25 16:13:24 +0000438 BI = ++BB->getInstList().insert(BI, NewLI);
Chris Lattnerc99428f2002-05-14 05:23:45 +0000439
440 // Replace the old store with a new one!
441 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
442 PRINT_PEEPHOLE3("load-src-cast:out", NCI, CastSrc, NewLI);
443 ++NumLoadStorePeepholes;
444 return true;
445 }
446
Chris Lattnerd32a9612001-11-01 02:42:08 +0000447 } else if (I->getOpcode() == Instruction::Add &&
448 isa<CastInst>(I->getOperand(1))) {
449
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000450 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
Chris Lattner3c019372002-05-10 15:29:25 +0000451 cast<CastInst>(I->getOperand(1)))) {
452 ++NumGEPInstFormed;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000453 return true;
Chris Lattner3c019372002-05-10 15:29:25 +0000454 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000455 }
456
457 return false;
458}
459
460
461
462
Chris Lattner7e708292002-06-25 16:13:24 +0000463static bool DoRaisePass(Function &F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000464 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000465 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattnerd32a9612001-11-01 02:42:08 +0000466 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000467 DEBUG(cerr << "Processing: " << *BI);
Chris Lattner16da4942002-05-26 20:18:18 +0000468 if (dceInstruction(BI) || doConstantPropogation(BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000469 Changed = true;
Chris Lattner3c019372002-05-10 15:29:25 +0000470 ++NumDCEorCP;
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000471 DEBUG(cerr << "***\t\t^^-- DeadCode Elinated!\n");
Chris Lattner7e708292002-06-25 16:13:24 +0000472 } else if (PeepholeOptimize(BB, BI)) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000473 Changed = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000474 } else {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000475 ++BI;
Chris Lattner7e708292002-06-25 16:13:24 +0000476 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000477 }
Chris Lattner7e708292002-06-25 16:13:24 +0000478
Chris Lattnerd32a9612001-11-01 02:42:08 +0000479 return Changed;
480}
481
482
Chris Lattnerf57b8452002-04-27 06:56:12 +0000483// RaisePointerReferences::doit - Raise a function representation to a higher
Chris Lattnerd32a9612001-11-01 02:42:08 +0000484// level.
485//
Chris Lattner7e708292002-06-25 16:13:24 +0000486static bool doRPR(Function &F) {
487 DEBUG(cerr << "\n\n\nStarting to work on Function '" << F.getName() << "'\n");
Chris Lattner68b07b72001-11-01 07:00:51 +0000488
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000489 // Insert casts for all incoming pointer pointer values that are treated as
490 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000491 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000492 bool Changed = false, LocalChange;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000493
Chris Lattner3378a5b2002-07-16 23:49:24 +0000494
495 // If the StartInst option was specified, then Peephole optimize that
496 // instruction first if it occurs in this function.
497 //
498 if (!StartInst.empty()) {
499 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
500 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI)
501 if (BI->getName() == StartInst) {
502 bool SavedDebug = DebugFlag; // Save the DEBUG() controlling flag.
503 DebugFlag = true; // Turn on DEBUG's
504 Changed |= PeepholeOptimize(BB, BI);
505 DebugFlag = SavedDebug; // Restore DebugFlag to previous state
506 }
507 }
508
Chris Lattner4b770a32001-12-04 08:12:53 +0000509 do {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000510 DEBUG(cerr << "Looping: \n" << F);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000511
Chris Lattnerf57b8452002-04-27 06:56:12 +0000512 // Iterate over the function, refining it, until it converges on a stable
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000513 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000514 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000515 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000516 Changed |= LocalChange;
517
518 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000519
520 return Changed;
521}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000522
523namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000524 struct RaisePointerReferences : public FunctionPass {
Chris Lattner96c466b2002-04-29 14:57:45 +0000525 const char *getPassName() const { return "Raise Pointer References"; }
526
Chris Lattner7e708292002-06-25 16:13:24 +0000527 virtual bool runOnFunction(Function &F) { return doRPR(F); }
Chris Lattner97e52e42002-04-28 21:27:06 +0000528
529 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
530 AU.preservesCFG();
531 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000532 };
533}
534
535Pass *createRaisePointerReferencesPass() {
536 return new RaisePointerReferences();
537}
538
539