blob: b2008b08a134d05de3760b04053b13555797ea7d [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
Chris Lattnerb7135992002-07-23 19:57:08 +00009#include "llvm/Transforms/RaisePointerReferences.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//
Chris Lattner5ff62e92002-07-22 02:10:13 +000029static cl::opt<std::string>
30StartInst("raise-start-inst", cl::Hidden, cl::value_desc("inst name"),
31 cl::desc("Start raise pass at the instruction with the specified name"));
Chris Lattner3378a5b2002-07-16 23:49:24 +000032
Chris Lattner5ff62e92002-07-22 02:10:13 +000033static Statistic<>
34NumLoadStorePeepholes("raise\t\t- Number of load/store peepholes");
35
36static Statistic<>
37NumGEPInstFormed("raise\t\t- Number of other getelementptr's formed");
38
39static Statistic<>
40NumExprTreesConv("raise\t\t- Number of expression trees converted");
41
42static Statistic<>
43NumCastOfCast("raise\t\t- Number of cast-of-self removed");
44
45static Statistic<>
46NumDCEorCP("raise\t\t- Number of insts DCEd or constprop'd");
Chris Lattner3c019372002-05-10 15:29:25 +000047
48
Chris Lattnerd32a9612001-11-01 02:42:08 +000049#define PRINT_PEEPHOLE(ID, NUM, I) \
Chris Lattnerb3abf9d2002-05-22 17:27:12 +000050 DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I)
Chris Lattnerd32a9612001-11-01 02:42:08 +000051
52#define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
53#define PRINT_PEEPHOLE2(ID, I1, I2) \
54 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
55#define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
56 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
57 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
Chris Lattnerd5b48ca2001-11-14 11:02:49 +000058#define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \
59 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
60 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0)
Chris Lattnerd32a9612001-11-01 02:42:08 +000061
62
Chris Lattnerd32a9612001-11-01 02:42:08 +000063// isReinterpretingCast - Return true if the cast instruction specified will
64// cause the operand to be "reinterpreted". A value is reinterpreted if the
65// cast instruction would cause the underlying bits to change.
66//
67static inline bool isReinterpretingCast(const CastInst *CI) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +000068 return!CI->getOperand(0)->getType()->isLosslesslyConvertableTo(CI->getType());
Chris Lattnerd32a9612001-11-01 02:42:08 +000069}
70
71
Chris Lattnera8b6d432001-12-05 06:34:00 +000072// Peephole optimize the following instructions:
73// %t1 = cast ? to x *
74// %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand
75//
76// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
77// %t2 = cast <eltype> * %t3 to {<...>}*
78//
79static bool HandleCastToPointer(BasicBlock::iterator BI,
80 const PointerType *DestPTy) {
Chris Lattner7e708292002-06-25 16:13:24 +000081 CastInst &CI = cast<CastInst>(*BI);
82 if (CI.use_empty()) return false;
Chris Lattnerf3b976e2001-11-04 20:21:12 +000083
Chris Lattnera8b6d432001-12-05 06:34:00 +000084 // Scan all of the uses, looking for any uses that are not add
85 // instructions. If we have non-adds, do not make this transformation.
86 //
Chris Lattner7e708292002-06-25 16:13:24 +000087 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end();
Chris Lattnera8b6d432001-12-05 06:34:00 +000088 I != E; ++I) {
89 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
Chris Lattner3fb2ddd2002-07-16 21:41:31 +000090 if (BO->getOpcode() != Instruction::Add ||
91 // Avoid add sbyte* %X, %X cases...
92 BO->getOperand(0) == BO->getOperand(1))
Chris Lattnera8b6d432001-12-05 06:34:00 +000093 return false;
94 } else {
95 return false;
96 }
97 }
98
Chris Lattner697954c2002-01-20 22:54:45 +000099 std::vector<Value*> Indices;
Chris Lattner7e708292002-06-25 16:13:24 +0000100 Value *Src = CI.getOperand(0);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000101 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000102 if (Result == 0) return false; // Not convertable...
103
104 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
105
106 // If we have a getelementptr capability... transform all of the
107 // add instruction uses into getelementptr's.
Chris Lattner7e708292002-06-25 16:13:24 +0000108 while (!CI.use_empty()) {
109 BinaryOperator *I = cast<BinaryOperator>(*CI.use_begin());
Chris Lattnera8b6d432001-12-05 06:34:00 +0000110 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
111 "Use is not a valid add instruction!");
112
113 // Get the value added to the cast result pointer...
Chris Lattner7e708292002-06-25 16:13:24 +0000114 Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000115
Chris Lattner45ef5c22002-03-21 06:22:23 +0000116 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
Chris Lattnera8b6d432001-12-05 06:34:00 +0000117 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000118
119 if (GEP->getType() == I->getType()) {
120 // Replace the old add instruction with the shiny new GEP inst
121 ReplaceInstWithInst(I, GEP);
122 } else {
123 // If the type produced by the gep instruction differs from the original
124 // add instruction type, insert a cast now.
125 //
126
Chris Lattner7e708292002-06-25 16:13:24 +0000127 // Insert the GEP instruction before the old add instruction...
128 I->getParent()->getInstList().insert(I, GEP);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000129
130 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
Chris Lattner7e708292002-06-25 16:13:24 +0000131 GEP = new CastInst(GEP, I->getType());
Chris Lattner45ef5c22002-03-21 06:22:23 +0000132
133 // Replace the old add instruction with the shiny new GEP inst
Chris Lattner7e708292002-06-25 16:13:24 +0000134 ReplaceInstWithInst(I, GEP);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000135 }
136
Chris Lattnera8b6d432001-12-05 06:34:00 +0000137 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
138 }
139 return true;
140}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000141
142// Peephole optimize the following instructions:
143// %t1 = cast ulong <const int> to {<...>} *
144// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
145//
146// or
147// %t1 = cast {<...>}* %SP to int*
148// %t5 = cast ulong <const int> to int*
149// %t2 = add int* %t1, %t5 ;; int is same size as field
150//
151// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
152// %t2 = cast <eltype> * %t3 to {<...>}*
153//
154static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
155 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000156 const CompositeType *CompTy;
157 Value *OffsetVal = AddOp2->getOperand(0);
158 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000159
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000160 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000161 SrcPtr = AddOp1; // Handle the first case...
162 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
163 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000164 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000165 }
166
167 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000168 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000169 return false;
170
Chris Lattner697954c2002-01-20 22:54:45 +0000171 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000172 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
173 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000174
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000175 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000176 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
177 } else {
178 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
179 }
180
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000181 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
182 AddOp2->getName());
Chris Lattner7e708292002-06-25 16:13:24 +0000183 BI = ++BB->getInstList().insert(BI, GEP);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000184
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000185 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000186 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
187 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
188 return true;
189}
190
Chris Lattnerd32a9612001-11-01 02:42:08 +0000191static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000192 Instruction *I = BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000193
194 if (CastInst *CI = dyn_cast<CastInst>(I)) {
195 Value *Src = CI->getOperand(0);
196 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
197 const Type *DestTy = CI->getType();
198
Chris Lattnere99c66b2001-11-01 17:05:27 +0000199 // Peephole optimize the following instruction:
200 // %V2 = cast <ty> %V to <ty>
201 //
202 // Into: <nothing>
203 //
204 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000205 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
206 CI->replaceAllUsesWith(Src);
207 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000208 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000209 CI->setName("");
210 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000211 }
Chris Lattner3c019372002-05-10 15:29:25 +0000212
213 // DCE the instruction now, to avoid having the iterative version of DCE
214 // have to worry about it.
215 //
Chris Lattner7e708292002-06-25 16:13:24 +0000216 BI = BB->getInstList().erase(BI);
Chris Lattner3c019372002-05-10 15:29:25 +0000217
218 ++NumCastOfCast;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000219 return true;
220 }
221
Chris Lattnerd32a9612001-11-01 02:42:08 +0000222 // Check to see if it's a cast of an instruction that does not depend on the
223 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000224 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000225 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000226
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000227 // Check to see if we can convert the source of the cast to match the
228 // destination type of the cast...
Chris Lattnera8b6d432001-12-05 06:34:00 +0000229 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000230 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000231 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000232 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000233
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000234 DEBUG(cerr << "\nCONVERTING SRC EXPR TYPE:\n");
Chris Lattnerb1b42622002-07-17 17:11:33 +0000235 { // ValueMap must be destroyed before function verified!
236 ValueMapCache ValueMap;
237 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000238
Chris Lattnerb1b42622002-07-17 17:11:33 +0000239 if (Constant *CPV = dyn_cast<Constant>(E))
240 CI->replaceAllUsesWith(CPV);
241
242 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
243 DEBUG(cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent());
244 }
Chris Lattner3378a5b2002-07-16 23:49:24 +0000245
246 DEBUG(assert(verifyFunction(*BB->getParent()) == false &&
247 "Function broken!"));
Chris Lattnerb1b42622002-07-17 17:11:33 +0000248 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000249 ++NumExprTreesConv;
Chris Lattnerd5543802001-12-14 16:37:52 +0000250 return true;
251 }
252
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000253 // Check to see if we can convert the users of the cast value to match the
254 // source type of the cast...
Chris Lattnerd5543802001-12-14 16:37:52 +0000255 //
256 ConvertedTypes.clear();
Chris Lattnera66c7bf2002-07-16 18:12:55 +0000257 ConvertedTypes[Src] = Src->getType(); // Make sure the source doesn't change type
Chris Lattnerd5543802001-12-14 16:37:52 +0000258 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
259 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
260
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000261 DEBUG(cerr << "\nCONVERTING EXPR TYPE:\n");
Chris Lattnerb1b42622002-07-17 17:11:33 +0000262 { // ValueMap must be destroyed before function verified!
263 ValueMapCache ValueMap;
264 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
265 }
Chris Lattnerd5543802001-12-14 16:37:52 +0000266
Chris Lattnerd5543802001-12-14 16:37:52 +0000267 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000268 DEBUG(cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent());
Chris Lattner3378a5b2002-07-16 23:49:24 +0000269
270 DEBUG(assert(verifyFunction(*BB->getParent()) == false &&
271 "Function broken!"));
Chris Lattnerb1b42622002-07-17 17:11:33 +0000272 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000273 ++NumExprTreesConv;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000274 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000275 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000276 }
277
278 // Otherwise find out it this cast is a cast to a pointer type, which is
279 // then added to some other pointer, then loaded or stored through. If
280 // so, convert the add into a getelementptr instruction...
281 //
282 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
283 if (HandleCastToPointer(BI, DestPTy)) {
284 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000285 ++NumGEPInstFormed;
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000286 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000287 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000288 }
289
Chris Lattnere99c66b2001-11-01 17:05:27 +0000290 // Check to see if we are casting from a structure pointer to a pointer to
291 // the first element of the structure... to avoid munching other peepholes,
292 // we only let this happen if there are no add uses of the cast.
293 //
294 // Peephole optimize the following instructions:
295 // %t1 = cast {<...>} * %StructPtr to <ty> *
296 //
297 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
298 // %t1 = cast <eltype> * %t1 to <ty> *
299 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000300 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000301 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
302
303 // Loop over uses of the cast, checking for add instructions. If an add
304 // exists, this is probably a part of a more complex GEP, so we don't
305 // want to mess around with the cast.
306 //
307 bool HasAddUse = false;
308 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
309 I != E; ++I)
310 if (isa<Instruction>(*I) &&
311 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
312 HasAddUse = true; break;
313 }
314
315 // If it doesn't have an add use, check to see if the dest type is
316 // losslessly convertable to one of the types in the start of the struct
317 // type.
318 //
319 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000320 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000321 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000322 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000323 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000324
325 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000326 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000327 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
328 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000329 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
330 // Check for a zero element struct type... if we have one, bail.
331 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000332
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000333 // Grab the first element of the struct type, which must lie at
334 // offset zero in the struct.
335 //
336 ElTy = CurSTy->getElementTypes()[0];
337 } else {
338 ElTy = cast<ArrayType>(CurCTy)->getElementType();
339 }
340
341 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000343
344 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000345 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000346
347 // Nope, go a level deeper.
348 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000349 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000350 ElTy = 0;
351 }
352
353 // Did we find what we were looking for? If so, do the transformation
354 if (ElTy) {
355 PRINT_PEEPHOLE1("cast-for-first:in", CI);
356
Chris Lattnere99c66b2001-11-01 17:05:27 +0000357 // Insert the new T cast instruction... stealing old T's name
358 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
359 CI->getName());
360 CI->setName("");
Chris Lattner7e708292002-06-25 16:13:24 +0000361 BI = ++BB->getInstList().insert(BI, GEP);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000362
363 // Make the old cast instruction reference the new GEP instead of
364 // the old src value.
365 //
366 CI->setOperand(0, GEP);
367
368 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
Chris Lattner3c019372002-05-10 15:29:25 +0000369 ++NumGEPInstFormed;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000370 return true;
371 }
372 }
373 }
Chris Lattnere99c66b2001-11-01 17:05:27 +0000374
Chris Lattner8d38e542001-11-01 03:12:34 +0000375 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
376 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000377 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000378
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000379 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000380 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
381 // store <T2> %V, <T2>* %t
382 //
383 // Into:
384 // %t = cast <T2> %V to <T1>
385 // store <T1> %t2, <T1>* %P
386 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000387 // Note: This is not taken care of by expr conversion because there might
388 // not be a cast available for the store to convert the incoming value of.
389 // This code is basically here to make sure that pointers don't have casts
390 // if possible.
391 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000392 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
393 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000394 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000395 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000396 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000397 !SI->hasIndices()) { // No subscripts yet!
398 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
399
400 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000401 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000402 CI->getName());
403 CI->setName("");
Chris Lattner7e708292002-06-25 16:13:24 +0000404 BI = ++BB->getInstList().insert(BI, NCI);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000405
406 // Replace the old store with a new one!
407 ReplaceInstWithInst(BB->getInstList(), BI,
408 SI = new StoreInst(NCI, CastSrc));
409 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
Chris Lattner3c019372002-05-10 15:29:25 +0000410 ++NumLoadStorePeepholes;
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000411 return true;
412 }
413
Chris Lattnerc99428f2002-05-14 05:23:45 +0000414 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
415 Value *Pointer = LI->getOperand(0);
416 const Type *PtrElType =
417 cast<PointerType>(Pointer->getType())->getElementType();
418
419 // Peephole optimize the following instructions:
420 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertable to T2
421 // %t = load <T2>* %P
422 //
423 // Into:
424 // %t = load <T1>* %P
425 // %Val = cast <T1> to <T2>
426 //
427 // Note: This is not taken care of by expr conversion because there might
428 // not be a cast available for the store to convert the incoming value of.
429 // This code is basically here to make sure that pointers don't have casts
430 // if possible.
431 //
432 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
433 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000434 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattnerc99428f2002-05-14 05:23:45 +0000435 // convertable types?
436 if (PtrElType->isLosslesslyConvertableTo(CSPT->getElementType()) &&
437 !LI->hasIndices()) { // No subscripts yet!
438 PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI);
439
440 // Create the new load instruction... loading the pre-casted value
441 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName());
442
443 // Insert the new T cast instruction... stealing old T's name
444 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName());
Chris Lattner7e708292002-06-25 16:13:24 +0000445 BI = ++BB->getInstList().insert(BI, NewLI);
Chris Lattnerc99428f2002-05-14 05:23:45 +0000446
447 // Replace the old store with a new one!
448 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
449 PRINT_PEEPHOLE3("load-src-cast:out", NCI, CastSrc, NewLI);
450 ++NumLoadStorePeepholes;
451 return true;
452 }
453
Chris Lattnerd32a9612001-11-01 02:42:08 +0000454 } else if (I->getOpcode() == Instruction::Add &&
455 isa<CastInst>(I->getOperand(1))) {
456
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000457 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
Chris Lattner3c019372002-05-10 15:29:25 +0000458 cast<CastInst>(I->getOperand(1)))) {
459 ++NumGEPInstFormed;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000460 return true;
Chris Lattner3c019372002-05-10 15:29:25 +0000461 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000462 }
463
464 return false;
465}
466
467
468
469
Chris Lattner7e708292002-06-25 16:13:24 +0000470static bool DoRaisePass(Function &F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000471 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000472 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattnerd32a9612001-11-01 02:42:08 +0000473 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000474 DEBUG(cerr << "Processing: " << *BI);
Chris Lattner16da4942002-05-26 20:18:18 +0000475 if (dceInstruction(BI) || doConstantPropogation(BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000476 Changed = true;
Chris Lattner3c019372002-05-10 15:29:25 +0000477 ++NumDCEorCP;
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000478 DEBUG(cerr << "***\t\t^^-- DeadCode Elinated!\n");
Chris Lattner7e708292002-06-25 16:13:24 +0000479 } else if (PeepholeOptimize(BB, BI)) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000480 Changed = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000481 } else {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000482 ++BI;
Chris Lattner7e708292002-06-25 16:13:24 +0000483 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000484 }
Chris Lattner7e708292002-06-25 16:13:24 +0000485
Chris Lattnerd32a9612001-11-01 02:42:08 +0000486 return Changed;
487}
488
489
Chris Lattnerf57b8452002-04-27 06:56:12 +0000490// RaisePointerReferences::doit - Raise a function representation to a higher
Chris Lattnerd32a9612001-11-01 02:42:08 +0000491// level.
492//
Chris Lattner7e708292002-06-25 16:13:24 +0000493static bool doRPR(Function &F) {
494 DEBUG(cerr << "\n\n\nStarting to work on Function '" << F.getName() << "'\n");
Chris Lattner68b07b72001-11-01 07:00:51 +0000495
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000496 // Insert casts for all incoming pointer pointer values that are treated as
497 // arrays...
Chris Lattnerd32a9612001-11-01 02:42:08 +0000498 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000499 bool Changed = false, LocalChange;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000500
Chris Lattner3378a5b2002-07-16 23:49:24 +0000501
502 // If the StartInst option was specified, then Peephole optimize that
503 // instruction first if it occurs in this function.
504 //
505 if (!StartInst.empty()) {
506 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
507 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI)
508 if (BI->getName() == StartInst) {
509 bool SavedDebug = DebugFlag; // Save the DEBUG() controlling flag.
510 DebugFlag = true; // Turn on DEBUG's
511 Changed |= PeepholeOptimize(BB, BI);
512 DebugFlag = SavedDebug; // Restore DebugFlag to previous state
513 }
514 }
515
Chris Lattner4b770a32001-12-04 08:12:53 +0000516 do {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000517 DEBUG(cerr << "Looping: \n" << F);
Chris Lattnera8b6d432001-12-05 06:34:00 +0000518
Chris Lattnerf57b8452002-04-27 06:56:12 +0000519 // Iterate over the function, refining it, until it converges on a stable
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000520 // state
Chris Lattnerd5543802001-12-14 16:37:52 +0000521 LocalChange = false;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000522 while (DoRaisePass(F)) LocalChange = true;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000523 Changed |= LocalChange;
524
525 } while (LocalChange);
Chris Lattnerd32a9612001-11-01 02:42:08 +0000526
527 return Changed;
528}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000529
530namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000531 struct RaisePointerReferences : public FunctionPass {
Chris Lattner33494522002-07-23 18:02:23 +0000532
533 // FIXME: constructor should save and use target data here!!
534 RaisePointerReferences(const TargetData &TD) {}
Chris Lattner96c466b2002-04-29 14:57:45 +0000535
Chris Lattner7e708292002-06-25 16:13:24 +0000536 virtual bool runOnFunction(Function &F) { return doRPR(F); }
Chris Lattner97e52e42002-04-28 21:27:06 +0000537
538 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
539 AU.preservesCFG();
540 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000541 };
542}
543
Chris Lattner33494522002-07-23 18:02:23 +0000544Pass *createRaisePointerReferencesPass(const TargetData &TD) {
545 return new RaisePointerReferences(TD);
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000546}
547
Chris Lattnera6275cc2002-07-26 21:12:46 +0000548static RegisterOpt<RaisePointerReferences>
Chris Lattner33494522002-07-23 18:02:23 +0000549X("raise", "Raise Pointer References", createRaisePointerReferencesPass);