blob: 1defda4d8453ab006116b9fed4a110e69d9f6aa5 [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)) {
Chris Lattner3fb2ddd2002-07-16 21:41:31 +000071 if (BO->getOpcode() != Instruction::Add ||
72 // Avoid add sbyte* %X, %X cases...
73 BO->getOperand(0) == BO->getOperand(1))
Chris Lattnera8b6d432001-12-05 06:34:00 +000074 return false;
75 } else {
76 return false;
77 }
78 }
79
Chris Lattner697954c2002-01-20 22:54:45 +000080 std::vector<Value*> Indices;
Chris Lattner7e708292002-06-25 16:13:24 +000081 Value *Src = CI.getOperand(0);
Chris Lattnera8b6d432001-12-05 06:34:00 +000082 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
Chris Lattnera8b6d432001-12-05 06:34:00 +000083 if (Result == 0) return false; // Not convertable...
84
85 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
86
87 // If we have a getelementptr capability... transform all of the
88 // add instruction uses into getelementptr's.
Chris Lattner7e708292002-06-25 16:13:24 +000089 while (!CI.use_empty()) {
90 BinaryOperator *I = cast<BinaryOperator>(*CI.use_begin());
Chris Lattnera8b6d432001-12-05 06:34:00 +000091 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
92 "Use is not a valid add instruction!");
93
94 // Get the value added to the cast result pointer...
Chris Lattner7e708292002-06-25 16:13:24 +000095 Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0);
Chris Lattnera8b6d432001-12-05 06:34:00 +000096
Chris Lattner45ef5c22002-03-21 06:22:23 +000097 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
Chris Lattnera8b6d432001-12-05 06:34:00 +000098 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
Chris Lattner45ef5c22002-03-21 06:22:23 +000099
100 if (GEP->getType() == I->getType()) {
101 // Replace the old add instruction with the shiny new GEP inst
102 ReplaceInstWithInst(I, GEP);
103 } else {
104 // If the type produced by the gep instruction differs from the original
105 // add instruction type, insert a cast now.
106 //
107
Chris Lattner7e708292002-06-25 16:13:24 +0000108 // Insert the GEP instruction before the old add instruction...
109 I->getParent()->getInstList().insert(I, GEP);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000110
111 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
Chris Lattner7e708292002-06-25 16:13:24 +0000112 GEP = new CastInst(GEP, I->getType());
Chris Lattner45ef5c22002-03-21 06:22:23 +0000113
114 // Replace the old add instruction with the shiny new GEP inst
Chris Lattner7e708292002-06-25 16:13:24 +0000115 ReplaceInstWithInst(I, GEP);
Chris Lattner45ef5c22002-03-21 06:22:23 +0000116 }
117
Chris Lattnera8b6d432001-12-05 06:34:00 +0000118 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
119 }
120 return true;
121}
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000122
123// Peephole optimize the following instructions:
124// %t1 = cast ulong <const int> to {<...>} *
125// %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
126//
127// or
128// %t1 = cast {<...>}* %SP to int*
129// %t5 = cast ulong <const int> to int*
130// %t2 = add int* %t1, %t5 ;; int is same size as field
131//
132// Into: %t3 = getelementptr {<...>} * %SP, <element indices>
133// %t2 = cast <eltype> * %t3 to {<...>}*
134//
135static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
136 Value *AddOp1, CastInst *AddOp2) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000137 const CompositeType *CompTy;
138 Value *OffsetVal = AddOp2->getOperand(0);
139 Value *SrcPtr; // Of type pointer to struct...
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000140
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000141 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000142 SrcPtr = AddOp1; // Handle the first case...
143 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
144 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000145 CompTy = getPointedToComposite(SrcPtr->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000146 }
147
148 // Only proceed if we have detected all of our conditions successfully...
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000149 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000150 return false;
151
Chris Lattner697954c2002-01-20 22:54:45 +0000152 std::vector<Value*> Indices;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000153 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
154 return false; // Not convertable... perhaps next time
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000155
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000156 if (getPointedToComposite(AddOp1->getType())) { // case 1
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000157 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
158 } else {
159 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
160 }
161
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000162 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
163 AddOp2->getName());
Chris Lattner7e708292002-06-25 16:13:24 +0000164 BI = ++BB->getInstList().insert(BI, GEP);
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000165
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000166 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000167 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
168 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
169 return true;
170}
171
Chris Lattnerd32a9612001-11-01 02:42:08 +0000172static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
Chris Lattner7e708292002-06-25 16:13:24 +0000173 Instruction *I = BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000174
175 if (CastInst *CI = dyn_cast<CastInst>(I)) {
176 Value *Src = CI->getOperand(0);
177 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
178 const Type *DestTy = CI->getType();
179
Chris Lattnere99c66b2001-11-01 17:05:27 +0000180 // Peephole optimize the following instruction:
181 // %V2 = cast <ty> %V to <ty>
182 //
183 // Into: <nothing>
184 //
185 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000186 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
187 CI->replaceAllUsesWith(Src);
188 if (!Src->hasName() && CI->hasName()) {
Chris Lattner697954c2002-01-20 22:54:45 +0000189 std::string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000190 CI->setName("");
191 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000192 }
Chris Lattner3c019372002-05-10 15:29:25 +0000193
194 // DCE the instruction now, to avoid having the iterative version of DCE
195 // have to worry about it.
196 //
Chris Lattner7e708292002-06-25 16:13:24 +0000197 BI = BB->getInstList().erase(BI);
Chris Lattner3c019372002-05-10 15:29:25 +0000198
199 ++NumCastOfCast;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000200 return true;
201 }
202
Chris Lattnerd32a9612001-11-01 02:42:08 +0000203 // Check to see if it's a cast of an instruction that does not depend on the
204 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000205 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000206 ValueTypeCache ConvertedTypes;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000207
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000208 // Check to see if we can convert the source of the cast to match the
209 // destination type of the cast...
Chris Lattnera8b6d432001-12-05 06:34:00 +0000210 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000211 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
Chris Lattnera8b6d432001-12-05 06:34:00 +0000212 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
Chris Lattnerd5543802001-12-14 16:37:52 +0000213 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000214
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000215 DEBUG(cerr << "\nCONVERTING SRC EXPR TYPE:\n");
Chris Lattnera8b6d432001-12-05 06:34:00 +0000216 ValueMapCache ValueMap;
217 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
218 if (Constant *CPV = dyn_cast<Constant>(E))
219 CI->replaceAllUsesWith(CPV);
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000220
Chris Lattnera8b6d432001-12-05 06:34:00 +0000221 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
222 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000223 DEBUG(cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent());
Chris Lattner3c019372002-05-10 15:29:25 +0000224 ++NumExprTreesConv;
Chris Lattnerd5543802001-12-14 16:37:52 +0000225 return true;
226 }
227
Chris Lattnerd20a98e2002-05-24 20:41:51 +0000228 // Check to see if we can convert the users of the cast value to match the
229 // source type of the cast...
Chris Lattnerd5543802001-12-14 16:37:52 +0000230 //
231 ConvertedTypes.clear();
Chris Lattnera66c7bf2002-07-16 18:12:55 +0000232 ConvertedTypes[Src] = Src->getType(); // Make sure the source doesn't change type
Chris Lattnerd5543802001-12-14 16:37:52 +0000233 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
234 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
235
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000236 DEBUG(cerr << "\nCONVERTING EXPR TYPE:\n");
Chris Lattnerd5543802001-12-14 16:37:52 +0000237 ValueMapCache ValueMap;
238 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
239
240 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
241 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000242 DEBUG(cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent());
Chris Lattner3c019372002-05-10 15:29:25 +0000243 ++NumExprTreesConv;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000244 return true;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000245 }
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000246 }
247
248 // Otherwise find out it this cast is a cast to a pointer type, which is
249 // then added to some other pointer, then loaded or stored through. If
250 // so, convert the add into a getelementptr instruction...
251 //
252 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
253 if (HandleCastToPointer(BI, DestPTy)) {
254 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattner3c019372002-05-10 15:29:25 +0000255 ++NumGEPInstFormed;
Chris Lattnerf17a09d2001-12-06 18:06:13 +0000256 return true;
Chris Lattnera8b6d432001-12-05 06:34:00 +0000257 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000258 }
259
Chris Lattnere99c66b2001-11-01 17:05:27 +0000260 // Check to see if we are casting from a structure pointer to a pointer to
261 // the first element of the structure... to avoid munching other peepholes,
262 // we only let this happen if there are no add uses of the cast.
263 //
264 // Peephole optimize the following instructions:
265 // %t1 = cast {<...>} * %StructPtr to <ty> *
266 //
267 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
268 // %t1 = cast <eltype> * %t1 to <ty> *
269 //
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000270 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
Chris Lattnere99c66b2001-11-01 17:05:27 +0000271 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
272
273 // Loop over uses of the cast, checking for add instructions. If an add
274 // exists, this is probably a part of a more complex GEP, so we don't
275 // want to mess around with the cast.
276 //
277 bool HasAddUse = false;
278 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
279 I != E; ++I)
280 if (isa<Instruction>(*I) &&
281 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
282 HasAddUse = true; break;
283 }
284
285 // If it doesn't have an add use, check to see if the dest type is
286 // losslessly convertable to one of the types in the start of the struct
287 // type.
288 //
289 if (!HasAddUse) {
Chris Lattner7a176752001-12-04 00:03:30 +0000290 const Type *DestPointedTy = DestPTy->getElementType();
Chris Lattnere99c66b2001-11-01 17:05:27 +0000291 unsigned Depth = 1;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000292 const CompositeType *CurCTy = CTy;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000293 const Type *ElTy = 0;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000294
295 // Build the index vector, full of all zeros
Chris Lattner697954c2002-01-20 22:54:45 +0000296 std::vector<Value*> Indices;
Chris Lattnerd5543802001-12-14 16:37:52 +0000297 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
298 while (CurCTy && !isa<PointerType>(CurCTy)) {
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000299 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
300 // Check for a zero element struct type... if we have one, bail.
301 if (CurSTy->getElementTypes().size() == 0) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000302
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000303 // Grab the first element of the struct type, which must lie at
304 // offset zero in the struct.
305 //
306 ElTy = CurSTy->getElementTypes()[0];
307 } else {
308 ElTy = cast<ArrayType>(CurCTy)->getElementType();
309 }
310
311 // Insert a zero to index through this type...
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
Chris Lattnere99c66b2001-11-01 17:05:27 +0000313
314 // Did we find what we're looking for?
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000315 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000316
317 // Nope, go a level deeper.
318 ++Depth;
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000319 CurCTy = dyn_cast<CompositeType>(ElTy);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000320 ElTy = 0;
321 }
322
323 // Did we find what we were looking for? If so, do the transformation
324 if (ElTy) {
325 PRINT_PEEPHOLE1("cast-for-first:in", CI);
326
Chris Lattnere99c66b2001-11-01 17:05:27 +0000327 // Insert the new T cast instruction... stealing old T's name
328 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
329 CI->getName());
330 CI->setName("");
Chris Lattner7e708292002-06-25 16:13:24 +0000331 BI = ++BB->getInstList().insert(BI, GEP);
Chris Lattnere99c66b2001-11-01 17:05:27 +0000332
333 // Make the old cast instruction reference the new GEP instead of
334 // the old src value.
335 //
336 CI->setOperand(0, GEP);
337
338 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
Chris Lattner3c019372002-05-10 15:29:25 +0000339 ++NumGEPInstFormed;
Chris Lattnere99c66b2001-11-01 17:05:27 +0000340 return true;
341 }
342 }
343 }
Chris Lattnere99c66b2001-11-01 17:05:27 +0000344
Chris Lattner8d38e542001-11-01 03:12:34 +0000345 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
346 Value *Val = SI->getOperand(0);
Chris Lattner65ea1712001-11-14 11:27:58 +0000347 Value *Pointer = SI->getPointerOperand();
Chris Lattner8d38e542001-11-01 03:12:34 +0000348
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000349 // Peephole optimize the following instructions:
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000350 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
351 // store <T2> %V, <T2>* %t
352 //
353 // Into:
354 // %t = cast <T2> %V to <T1>
355 // store <T1> %t2, <T1>* %P
356 //
Chris Lattnerd5543802001-12-14 16:37:52 +0000357 // Note: This is not taken care of by expr conversion because there might
358 // not be a cast available for the store to convert the incoming value of.
359 // This code is basically here to make sure that pointers don't have casts
360 // if possible.
361 //
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000362 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
363 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000364 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattner3cc7dde2001-11-26 16:58:14 +0000365 // convertable types?
Chris Lattner7a176752001-12-04 00:03:30 +0000366 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000367 !SI->hasIndices()) { // No subscripts yet!
368 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
369
370 // Insert the new T cast instruction... stealing old T's name
Chris Lattner7a176752001-12-04 00:03:30 +0000371 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000372 CI->getName());
373 CI->setName("");
Chris Lattner7e708292002-06-25 16:13:24 +0000374 BI = ++BB->getInstList().insert(BI, NCI);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000375
376 // Replace the old store with a new one!
377 ReplaceInstWithInst(BB->getInstList(), BI,
378 SI = new StoreInst(NCI, CastSrc));
379 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
Chris Lattner3c019372002-05-10 15:29:25 +0000380 ++NumLoadStorePeepholes;
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000381 return true;
382 }
383
Chris Lattnerc99428f2002-05-14 05:23:45 +0000384 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
385 Value *Pointer = LI->getOperand(0);
386 const Type *PtrElType =
387 cast<PointerType>(Pointer->getType())->getElementType();
388
389 // Peephole optimize the following instructions:
390 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertable to T2
391 // %t = load <T2>* %P
392 //
393 // Into:
394 // %t = load <T1>* %P
395 // %Val = cast <T1> to <T2>
396 //
397 // Note: This is not taken care of by expr conversion because there might
398 // not be a cast available for the store to convert the incoming value of.
399 // This code is basically here to make sure that pointers don't have casts
400 // if possible.
401 //
402 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
403 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
Chris Lattner7e708292002-06-25 16:13:24 +0000404 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
Chris Lattnerc99428f2002-05-14 05:23:45 +0000405 // convertable types?
406 if (PtrElType->isLosslesslyConvertableTo(CSPT->getElementType()) &&
407 !LI->hasIndices()) { // No subscripts yet!
408 PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI);
409
410 // Create the new load instruction... loading the pre-casted value
411 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName());
412
413 // Insert the new T cast instruction... stealing old T's name
414 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName());
Chris Lattner7e708292002-06-25 16:13:24 +0000415 BI = ++BB->getInstList().insert(BI, NewLI);
Chris Lattnerc99428f2002-05-14 05:23:45 +0000416
417 // Replace the old store with a new one!
418 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
419 PRINT_PEEPHOLE3("load-src-cast:out", NCI, CastSrc, NewLI);
420 ++NumLoadStorePeepholes;
421 return true;
422 }
423
Chris Lattnerd32a9612001-11-01 02:42:08 +0000424 } else if (I->getOpcode() == Instruction::Add &&
425 isa<CastInst>(I->getOperand(1))) {
426
Chris Lattnerd5b48ca2001-11-14 11:02:49 +0000427 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
Chris Lattner3c019372002-05-10 15:29:25 +0000428 cast<CastInst>(I->getOperand(1)))) {
429 ++NumGEPInstFormed;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000430 return true;
Chris Lattner3c019372002-05-10 15:29:25 +0000431 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000432 }
433
434 return false;
435}
436
437
438
439
Chris Lattner7e708292002-06-25 16:13:24 +0000440static bool DoRaisePass(Function &F) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000441 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000442 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattnerd32a9612001-11-01 02:42:08 +0000443 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000444 DEBUG(cerr << "Processing: " << *BI);
Chris Lattner16da4942002-05-26 20:18:18 +0000445 if (dceInstruction(BI) || doConstantPropogation(BI)) {
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000446 Changed = true;
Chris Lattner3c019372002-05-10 15:29:25 +0000447 ++NumDCEorCP;
Chris Lattnerb3abf9d2002-05-22 17:27:12 +0000448 DEBUG(cerr << "***\t\t^^-- DeadCode Elinated!\n");
Chris Lattner7e708292002-06-25 16:13:24 +0000449 } else if (PeepholeOptimize(BB, BI)) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000450 Changed = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000451 } else {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000452 ++BI;
Chris Lattner7e708292002-06-25 16:13:24 +0000453 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000454 }
Chris Lattner7e708292002-06-25 16:13:24 +0000455
Chris Lattnerd32a9612001-11-01 02:42:08 +0000456 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 Lattner7e708292002-06-25 16:13:24 +0000463static bool doRPR(Function &F) {
464 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 Lattner7e708292002-06-25 16:13:24 +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