1 //===- LevelRaise.cpp - Code to change LLVM to higher level -----------------=//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/LevelChange.h"
10 #include "llvm/Transforms/Utils/Local.h"
11 #include "TransformInternals.h"
12 #include "llvm/iOther.h"
13 #include "llvm/iMemory.h"
14 #include "llvm/Pass.h"
15 #include "llvm/ConstantHandling.h"
16 #include "llvm/Analysis/Expressions.h"
17 #include "llvm/Analysis/Verifier.h"
18 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
19 #include "Support/STLExtras.h"
20 #include "Support/StatisticReporter.h"
21 #include "Support/CommandLine.h"
25 // 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
29 static cl::String StartInst("raise-start-inst", "Start raise pass at the "
30 "instruction with the specified name", cl::Hidden);
32 static Statistic<> NumLoadStorePeepholes("raise\t\t- Number of load/store "
34 static Statistic<> NumGEPInstFormed("raise\t\t- Number of other "
35 "getelementptr's formed");
36 static Statistic<> NumExprTreesConv("raise\t\t- Number of expression trees"
38 static Statistic<> NumCastOfCast("raise\t\t- Number of cast-of-self removed");
39 static Statistic<> NumDCEorCP("raise\t\t- Number of insts DCEd or constprop'd");
42 #define PRINT_PEEPHOLE(ID, NUM, I) \
43 DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I)
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)
51 #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)
56 // 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.
60 static inline bool isReinterpretingCast(const CastInst *CI) {
61 return!CI->getOperand(0)->getType()->isLosslesslyConvertableTo(CI->getType());
65 // Peephole optimize the following instructions:
66 // %t1 = cast ? to x *
67 // %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand
69 // Into: %t3 = getelementptr {<...>} * %SP, <element indices>
70 // %t2 = cast <eltype> * %t3 to {<...>}*
72 static bool HandleCastToPointer(BasicBlock::iterator BI,
73 const PointerType *DestPTy) {
74 CastInst &CI = cast<CastInst>(*BI);
75 if (CI.use_empty()) return false;
77 // 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.
80 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end();
82 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
83 if (BO->getOpcode() != Instruction::Add ||
84 // Avoid add sbyte* %X, %X cases...
85 BO->getOperand(0) == BO->getOperand(1))
92 std::vector<Value*> Indices;
93 Value *Src = CI.getOperand(0);
94 const Type *Result = ConvertableToGEP(DestPTy, Src, Indices, &BI);
95 if (Result == 0) return false; // Not convertable...
97 PRINT_PEEPHOLE2("cast-add-to-gep:in", Src, CI);
99 // If we have a getelementptr capability... transform all of the
100 // add instruction uses into getelementptr's.
101 while (!CI.use_empty()) {
102 BinaryOperator *I = cast<BinaryOperator>(*CI.use_begin());
103 assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
104 "Use is not a valid add instruction!");
106 // Get the value added to the cast result pointer...
107 Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0);
109 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
110 PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
112 if (GEP->getType() == I->getType()) {
113 // Replace the old add instruction with the shiny new GEP inst
114 ReplaceInstWithInst(I, GEP);
116 // If the type produced by the gep instruction differs from the original
117 // add instruction type, insert a cast now.
120 // Insert the GEP instruction before the old add instruction...
121 I->getParent()->getInstList().insert(I, GEP);
123 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
124 GEP = new CastInst(GEP, I->getType());
126 // Replace the old add instruction with the shiny new GEP inst
127 ReplaceInstWithInst(I, GEP);
130 PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
135 // Peephole optimize the following instructions:
136 // %t1 = cast ulong <const int> to {<...>} *
137 // %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
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
144 // Into: %t3 = getelementptr {<...>} * %SP, <element indices>
145 // %t2 = cast <eltype> * %t3 to {<...>}*
147 static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
148 Value *AddOp1, CastInst *AddOp2) {
149 const CompositeType *CompTy;
150 Value *OffsetVal = AddOp2->getOperand(0);
151 Value *SrcPtr; // Of type pointer to struct...
153 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
154 SrcPtr = AddOp1; // Handle the first case...
155 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
156 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
157 CompTy = getPointedToComposite(SrcPtr->getType());
160 // Only proceed if we have detected all of our conditions successfully...
161 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isIntegral())
164 std::vector<Value*> Indices;
165 if (!ConvertableToGEP(SrcPtr->getType(), OffsetVal, Indices, &BI))
166 return false; // Not convertable... perhaps next time
168 if (getPointedToComposite(AddOp1->getType())) { // case 1
169 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, *BI);
171 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, *BI);
174 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
176 BI = ++BB->getInstList().insert(BI, GEP);
178 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
179 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
180 PRINT_PEEPHOLE2("add-to-gep:out", GEP, NCI);
184 static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
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();
192 // Peephole optimize the following instruction:
193 // %V2 = cast <ty> %V to <ty>
197 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
198 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
199 CI->replaceAllUsesWith(Src);
200 if (!Src->hasName() && CI->hasName()) {
201 std::string Name = CI->getName();
203 Src->setName(Name, BB->getParent()->getSymbolTable());
206 // DCE the instruction now, to avoid having the iterative version of DCE
207 // have to worry about it.
209 BI = BB->getInstList().erase(BI);
215 // 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.
217 if (!isReinterpretingCast(CI)) {
218 ValueTypeCache ConvertedTypes;
220 // Check to see if we can convert the source of the cast to match the
221 // destination type of the cast...
223 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
224 if (ExpressionConvertableToType(Src, DestTy, ConvertedTypes)) {
225 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", Src, CI, BB->getParent());
227 DEBUG(cerr << "\nCONVERTING SRC EXPR TYPE:\n");
228 { // ValueMap must be destroyed before function verified!
229 ValueMapCache ValueMap;
230 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap);
232 if (Constant *CPV = dyn_cast<Constant>(E))
233 CI->replaceAllUsesWith(CPV);
235 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", E);
236 DEBUG(cerr << "DONE CONVERTING SRC EXPR TYPE: \n" << BB->getParent());
239 DEBUG(assert(verifyFunction(*BB->getParent()) == false &&
240 "Function broken!"));
241 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
246 // Check to see if we can convert the users of the cast value to match the
247 // source type of the cast...
249 ConvertedTypes.clear();
250 ConvertedTypes[Src] = Src->getType(); // Make sure the source doesn't change type
251 if (ValueConvertableToType(CI, Src->getType(), ConvertedTypes)) {
252 PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", Src, CI, BB->getParent());
254 DEBUG(cerr << "\nCONVERTING EXPR TYPE:\n");
255 { // ValueMap must be destroyed before function verified!
256 ValueMapCache ValueMap;
257 ConvertValueToNewType(CI, Src, ValueMap); // This will delete CI!
260 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", Src);
261 DEBUG(cerr << "DONE CONVERTING EXPR TYPE: \n\n" << BB->getParent());
263 DEBUG(assert(verifyFunction(*BB->getParent()) == false &&
264 "Function broken!"));
265 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
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...
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.
283 // 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.
287 // Peephole optimize the following instructions:
288 // %t1 = cast {<...>} * %StructPtr to <ty> *
290 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
291 // %t1 = cast <eltype> * %t1 to <ty> *
293 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
294 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
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.
300 bool HasAddUse = false;
301 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
303 if (isa<Instruction>(*I) &&
304 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
305 HasAddUse = true; break;
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
313 const Type *DestPointedTy = DestPTy->getElementType();
315 const CompositeType *CurCTy = CTy;
316 const Type *ElTy = 0;
318 // Build the index vector, full of all zeros
319 std::vector<Value*> Indices;
320 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
321 while (CurCTy && !isa<PointerType>(CurCTy)) {
322 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;
326 // Grab the first element of the struct type, which must lie at
327 // offset zero in the struct.
329 ElTy = CurSTy->getElementTypes()[0];
331 ElTy = cast<ArrayType>(CurCTy)->getElementType();
334 // Insert a zero to index through this type...
335 Indices.push_back(ConstantUInt::get(CurCTy->getIndexType(), 0));
337 // Did we find what we're looking for?
338 if (ElTy->isLosslesslyConvertableTo(DestPointedTy)) break;
340 // Nope, go a level deeper.
342 CurCTy = dyn_cast<CompositeType>(ElTy);
346 // Did we find what we were looking for? If so, do the transformation
348 PRINT_PEEPHOLE1("cast-for-first:in", CI);
350 // Insert the new T cast instruction... stealing old T's name
351 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
354 BI = ++BB->getInstList().insert(BI, GEP);
356 // Make the old cast instruction reference the new GEP instead of
357 // the old src value.
359 CI->setOperand(0, GEP);
361 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
368 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
369 Value *Val = SI->getOperand(0);
370 Value *Pointer = SI->getPointerOperand();
372 // Peephole optimize the following instructions:
373 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
374 // store <T2> %V, <T2>* %t
377 // %t = cast <T2> %V to <T1>
378 // store <T1> %t2, <T1>* %P
380 // 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
385 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
386 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
387 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
388 // convertable types?
389 if (Val->getType()->isLosslesslyConvertableTo(CSPT->getElementType()) &&
390 !SI->hasIndices()) { // No subscripts yet!
391 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
393 // Insert the new T cast instruction... stealing old T's name
394 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
397 BI = ++BB->getInstList().insert(BI, NCI);
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);
403 ++NumLoadStorePeepholes;
407 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
408 Value *Pointer = LI->getOperand(0);
409 const Type *PtrElType =
410 cast<PointerType>(Pointer->getType())->getElementType();
412 // Peephole optimize the following instructions:
413 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertable to T2
414 // %t = load <T2>* %P
417 // %t = load <T1>* %P
418 // %Val = cast <T1> to <T2>
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
425 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
426 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
427 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
428 // convertable types?
429 if (PtrElType->isLosslesslyConvertableTo(CSPT->getElementType()) &&
430 !LI->hasIndices()) { // No subscripts yet!
431 PRINT_PEEPHOLE2("load-src-cast:in ", Pointer, LI);
433 // Create the new load instruction... loading the pre-casted value
434 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName());
436 // Insert the new T cast instruction... stealing old T's name
437 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName());
438 BI = ++BB->getInstList().insert(BI, NewLI);
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;
447 } else if (I->getOpcode() == Instruction::Add &&
448 isa<CastInst>(I->getOperand(1))) {
450 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
451 cast<CastInst>(I->getOperand(1)))) {
463 static bool DoRaisePass(Function &F) {
464 bool Changed = false;
465 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
466 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
467 DEBUG(cerr << "Processing: " << *BI);
468 if (dceInstruction(BI) || doConstantPropogation(BI)) {
471 DEBUG(cerr << "***\t\t^^-- DeadCode Elinated!\n");
472 } else if (PeepholeOptimize(BB, BI)) {
483 // RaisePointerReferences::doit - Raise a function representation to a higher
486 static bool doRPR(Function &F) {
487 DEBUG(cerr << "\n\n\nStarting to work on Function '" << F.getName() << "'\n");
489 // Insert casts for all incoming pointer pointer values that are treated as
492 bool Changed = false, LocalChange;
495 // If the StartInst option was specified, then Peephole optimize that
496 // instruction first if it occurs in this function.
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
510 DEBUG(cerr << "Looping: \n" << F);
512 // Iterate over the function, refining it, until it converges on a stable
515 while (DoRaisePass(F)) LocalChange = true;
516 Changed |= LocalChange;
518 } while (LocalChange);
524 struct RaisePointerReferences : public FunctionPass {
525 const char *getPassName() const { return "Raise Pointer References"; }
527 virtual bool runOnFunction(Function &F) { return doRPR(F); }
529 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
535 Pass *createRaisePointerReferencesPass() {
536 return new RaisePointerReferences();