1 //===- LevelRaise.cpp - Code to change LLVM to higher level ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the 'raising' part of the LevelChange API. This is
11 // useful because, in general, it makes the LLVM code terser and easier to
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Transforms/Utils/Local.h"
18 #include "TransformInternals.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/ADT/STLExtras.h"
29 // StartInst - This enables the -raise-start-inst=foo option to cause the level
30 // raising pass to start at instruction "foo", which is immensely useful for
33 static cl::opt<std::string>
34 StartInst("raise-start-inst", cl::Hidden, cl::value_desc("inst name"),
35 cl::desc("Start raise pass at the instruction with the specified name"));
38 NumLoadStorePeepholes("raise", "Number of load/store peepholes");
41 NumGEPInstFormed("raise", "Number of other getelementptr's formed");
44 NumExprTreesConv("raise", "Number of expression trees converted");
47 NumCastOfCast("raise", "Number of cast-of-self removed");
50 NumDCEorCP("raise", "Number of insts DCEd or constprop'd");
53 NumVarargCallChanges("raise", "Number of vararg call peepholes");
55 #define PRINT_PEEPHOLE(ID, NUM, I) \
56 DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I)
58 #define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
59 #define PRINT_PEEPHOLE2(ID, I1, I2) \
60 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
61 #define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
62 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
63 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
64 #define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \
65 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
66 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0)
69 struct RPR : public FunctionPass {
70 virtual bool runOnFunction(Function &F);
72 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.addRequired<TargetData>();
78 bool DoRaisePass(Function &F);
79 bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI);
82 RegisterOpt<RPR> X("raise", "Raise Pointer References");
86 FunctionPass *llvm::createRaisePointerReferencesPass() {
91 // isReinterpretingCast - Return true if the cast instruction specified will
92 // cause the operand to be "reinterpreted". A value is reinterpreted if the
93 // cast instruction would cause the underlying bits to change.
95 static inline bool isReinterpretingCast(const CastInst *CI) {
96 return!CI->getOperand(0)->getType()->isLosslesslyConvertibleTo(CI->getType());
100 // Peephole optimize the following instructions:
101 // %t1 = cast ? to x *
102 // %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand
104 // Into: %t3 = getelementptr {<...>} * %SP, <element indices>
105 // %t2 = cast <eltype> * %t3 to {<...>}*
107 static bool HandleCastToPointer(BasicBlock::iterator BI,
108 const PointerType *DestPTy,
109 const TargetData &TD) {
110 CastInst &CI = cast<CastInst>(*BI);
111 if (CI.use_empty()) return false;
113 // Scan all of the uses, looking for any uses that are not add or sub
114 // instructions. If we have non-adds, do not make this transformation.
116 bool HasSubUse = false; // Keep track of any subtracts...
117 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end();
119 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) {
120 if ((BO->getOpcode() != Instruction::Add &&
121 BO->getOpcode() != Instruction::Sub) ||
122 // Avoid add sbyte* %X, %X cases...
123 BO->getOperand(0) == BO->getOperand(1))
126 HasSubUse |= BO->getOpcode() == Instruction::Sub;
131 std::vector<Value*> Indices;
132 Value *Src = CI.getOperand(0);
133 const Type *Result = ConvertibleToGEP(DestPTy, Src, Indices, TD, &BI);
134 if (Result == 0) return false; // Not convertible...
136 // Cannot handle subtracts if there is more than one index required...
137 if (HasSubUse && Indices.size() != 1) return false;
139 PRINT_PEEPHOLE2("cast-add-to-gep:in", *Src, CI);
141 // If we have a getelementptr capability... transform all of the
142 // add instruction uses into getelementptr's.
143 while (!CI.use_empty()) {
144 BinaryOperator *I = cast<BinaryOperator>(*CI.use_begin());
145 assert((I->getOpcode() == Instruction::Add ||
146 I->getOpcode() == Instruction::Sub) &&
147 "Use is not a valid add instruction!");
149 // Get the value added to the cast result pointer...
150 Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0);
152 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
153 PRINT_PEEPHOLE1("cast-add-to-gep:i", *I);
155 // If the instruction is actually a subtract, we are guaranteed to only have
156 // one index (from code above), so we just need to negate the pointer index
158 if (I->getOpcode() == Instruction::Sub) {
159 Instruction *Neg = BinaryOperator::createNeg(GEP->getOperand(1),
160 GEP->getOperand(1)->getName()+".neg", I);
161 GEP->setOperand(1, Neg);
164 if (GEP->getType() == I->getType()) {
165 // Replace the old add instruction with the shiny new GEP inst
166 ReplaceInstWithInst(I, GEP);
168 // If the type produced by the gep instruction differs from the original
169 // add instruction type, insert a cast now.
172 // Insert the GEP instruction before the old add instruction...
173 I->getParent()->getInstList().insert(I, GEP);
175 PRINT_PEEPHOLE1("cast-add-to-gep:o", *GEP);
176 GEP = new CastInst(GEP, I->getType());
178 // Replace the old add instruction with the shiny new GEP inst
179 ReplaceInstWithInst(I, GEP);
182 PRINT_PEEPHOLE1("cast-add-to-gep:o", *GEP);
187 // Peephole optimize the following instructions:
188 // %t1 = cast ulong <const int> to {<...>} *
189 // %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
192 // %t1 = cast {<...>}* %SP to int*
193 // %t5 = cast ulong <const int> to int*
194 // %t2 = add int* %t1, %t5 ;; int is same size as field
196 // Into: %t3 = getelementptr {<...>} * %SP, <element indices>
197 // %t2 = cast <eltype> * %t3 to {<...>}*
199 static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI,
200 Value *AddOp1, CastInst *AddOp2,
201 const TargetData &TD) {
202 const CompositeType *CompTy;
203 Value *OffsetVal = AddOp2->getOperand(0);
204 Value *SrcPtr = 0; // Of type pointer to struct...
206 if ((CompTy = getPointedToComposite(AddOp1->getType()))) {
207 SrcPtr = AddOp1; // Handle the first case...
208 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
209 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
210 CompTy = getPointedToComposite(SrcPtr->getType());
213 // Only proceed if we have detected all of our conditions successfully...
214 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isInteger())
217 std::vector<Value*> Indices;
218 if (!ConvertibleToGEP(SrcPtr->getType(), OffsetVal, Indices, TD, &BI))
219 return false; // Not convertible... perhaps next time
221 if (getPointedToComposite(AddOp1->getType())) { // case 1
222 PRINT_PEEPHOLE2("add-to-gep1:in", *AddOp2, *BI);
224 PRINT_PEEPHOLE3("add-to-gep2:in", *AddOp1, *AddOp2, *BI);
227 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices,
228 AddOp2->getName(), BI);
230 Instruction *NCI = new CastInst(GEP, AddOp1->getType());
231 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
232 PRINT_PEEPHOLE2("add-to-gep:out", *GEP, *NCI);
236 bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
238 const TargetData &TD = getAnalysis<TargetData>();
240 if (CastInst *CI = dyn_cast<CastInst>(I)) {
241 Value *Src = CI->getOperand(0);
242 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
243 const Type *DestTy = CI->getType();
245 // Peephole optimize the following instruction:
246 // %V2 = cast <ty> %V to <ty>
250 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
251 PRINT_PEEPHOLE1("cast-of-self-ty", *CI);
252 CI->replaceAllUsesWith(Src);
253 if (!Src->hasName() && CI->hasName()) {
254 std::string Name = CI->getName();
259 // DCE the instruction now, to avoid having the iterative version of DCE
260 // have to worry about it.
262 BI = BB->getInstList().erase(BI);
268 // Check to see if it's a cast of an instruction that does not depend on the
269 // specific type of the operands to do it's job.
270 if (!isReinterpretingCast(CI)) {
271 ValueTypeCache ConvertedTypes;
273 // Check to see if we can convert the source of the cast to match the
274 // destination type of the cast...
276 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change
277 if (ExpressionConvertibleToType(Src, DestTy, ConvertedTypes, TD)) {
278 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", *Src, *CI, *BB->getParent());
280 DEBUG(std::cerr << "\nCONVERTING SRC EXPR TYPE:\n");
281 { // ValueMap must be destroyed before function verified!
282 ValueMapCache ValueMap;
283 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap, TD);
285 if (Constant *CPV = dyn_cast<Constant>(E))
286 CI->replaceAllUsesWith(CPV);
288 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", *E);
289 DEBUG(std::cerr << "DONE CONVERTING SRC EXPR TYPE: \n"
290 << *BB->getParent());
293 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
298 // Check to see if we can convert the users of the cast value to match the
299 // source type of the cast...
301 ConvertedTypes.clear();
302 // Make sure the source doesn't change type
303 ConvertedTypes[Src] = Src->getType();
304 if (ValueConvertibleToType(CI, Src->getType(), ConvertedTypes, TD)) {
305 //PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", *Src, *CI,
306 // *BB->getParent());
308 DEBUG(std::cerr << "\nCONVERTING EXPR TYPE:\n");
309 { // ValueMap must be destroyed before function verified!
310 ValueMapCache ValueMap;
311 ConvertValueToNewType(CI, Src, ValueMap, TD); // This will delete CI!
314 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", *Src);
315 DEBUG(std::cerr << "DONE CONVERTING EXPR TYPE: \n\n" <<
318 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
324 // Otherwise find out it this cast is a cast to a pointer type, which is
325 // then added to some other pointer, then loaded or stored through. If
326 // so, convert the add into a getelementptr instruction...
328 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
329 if (HandleCastToPointer(BI, DestPTy, TD)) {
330 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
336 // Check to see if we are casting from a structure pointer to a pointer to
337 // the first element of the structure... to avoid munching other peepholes,
338 // we only let this happen if there are no add uses of the cast.
340 // Peephole optimize the following instructions:
341 // %t1 = cast {<...>} * %StructPtr to <ty> *
343 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
344 // %t1 = cast <eltype> * %t1 to <ty> *
346 if (const CompositeType *CTy = getPointedToComposite(Src->getType()))
347 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
349 // Loop over uses of the cast, checking for add instructions. If an add
350 // exists, this is probably a part of a more complex GEP, so we don't
351 // want to mess around with the cast.
353 bool HasAddUse = false;
354 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
356 if (isa<Instruction>(*I) &&
357 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
358 HasAddUse = true; break;
361 // If it doesn't have an add use, check to see if the dest type is
362 // losslessly convertible to one of the types in the start of the struct
366 const Type *DestPointedTy = DestPTy->getElementType();
368 const CompositeType *CurCTy = CTy;
369 const Type *ElTy = 0;
371 // Build the index vector, full of all zeros
372 std::vector<Value*> Indices;
374 Indices.push_back(Constant::getNullValue(Type::UIntTy));
375 while (CurCTy && !isa<PointerType>(CurCTy)) {
376 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) {
377 // Check for a zero element struct type... if we have one, bail.
378 if (CurSTy->getNumElements() == 0) break;
380 // Grab the first element of the struct type, which must lie at
381 // offset zero in the struct.
383 ElTy = CurSTy->getElementType(0);
385 ElTy = cast<SequentialType>(CurCTy)->getElementType();
388 // Insert a zero to index through this type...
389 Indices.push_back(Constant::getNullValue(Type::UIntTy));
391 // Did we find what we're looking for?
392 if (ElTy->isLosslesslyConvertibleTo(DestPointedTy)) break;
394 // Nope, go a level deeper.
396 CurCTy = dyn_cast<CompositeType>(ElTy);
400 // Did we find what we were looking for? If so, do the transformation
402 PRINT_PEEPHOLE1("cast-for-first:in", *CI);
404 std::string Name = CI->getName(); CI->setName("");
406 // Insert the new T cast instruction... stealing old T's name
407 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
410 // Make the old cast instruction reference the new GEP instead of
411 // the old src value.
413 CI->setOperand(0, GEP);
415 PRINT_PEEPHOLE2("cast-for-first:out", *GEP, *CI);
422 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
423 Value *Val = SI->getOperand(0);
424 Value *Pointer = SI->getPointerOperand();
426 // Peephole optimize the following instructions:
427 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertible to T2
428 // store <T2> %V, <T2>* %t
431 // %t = cast <T2> %V to <T1>
432 // store <T1> %t2, <T1>* %P
434 // Note: This is not taken care of by expr conversion because there might
435 // not be a cast available for the store to convert the incoming value of.
436 // This code is basically here to make sure that pointers don't have casts
439 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
440 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
441 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
442 // convertible types?
443 if (Val->getType()->isLosslesslyConvertibleTo(CSPT->getElementType())) {
444 PRINT_PEEPHOLE3("st-src-cast:in ", *Pointer, *Val, *SI);
446 // Insert the new T cast instruction... stealing old T's name
447 std::string Name(CI->getName()); CI->setName("");
448 CastInst *NCI = new CastInst(Val, CSPT->getElementType(),
451 // Replace the old store with a new one!
452 ReplaceInstWithInst(BB->getInstList(), BI,
453 SI = new StoreInst(NCI, CastSrc));
454 PRINT_PEEPHOLE3("st-src-cast:out", *NCI, *CastSrc, *SI);
455 ++NumLoadStorePeepholes;
459 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
460 Value *Pointer = LI->getOperand(0);
461 const Type *PtrElType =
462 cast<PointerType>(Pointer->getType())->getElementType();
464 // Peephole optimize the following instructions:
465 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertible to T2
466 // %t = load <T2>* %P
469 // %t = load <T1>* %P
470 // %Val = cast <T1> to <T2>
472 // Note: This is not taken care of by expr conversion because there might
473 // not be a cast available for the store to convert the incoming value of.
474 // This code is basically here to make sure that pointers don't have casts
477 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
478 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
479 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
480 // convertible types?
481 if (PtrElType->isLosslesslyConvertibleTo(CSPT->getElementType())) {
482 PRINT_PEEPHOLE2("load-src-cast:in ", *Pointer, *LI);
484 // Create the new load instruction... loading the pre-casted value
485 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName(), BI);
487 // Insert the new T cast instruction... stealing old T's name
488 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName());
490 // Replace the old store with a new one!
491 ReplaceInstWithInst(BB->getInstList(), BI, NCI);
492 PRINT_PEEPHOLE3("load-src-cast:out", *NCI, *CastSrc, *NewLI);
493 ++NumLoadStorePeepholes;
497 } else if (I->getOpcode() == Instruction::Add &&
498 isa<CastInst>(I->getOperand(1))) {
500 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0),
501 cast<CastInst>(I->getOperand(1)), TD)) {
505 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
506 // If we have a call with all varargs arguments, convert the call to use the
507 // actual argument types present...
509 const PointerType *PTy = cast<PointerType>(CI->getCalledValue()->getType());
510 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
512 // Is the call to a vararg variable with no real parameters?
513 if (FTy->isVarArg() && FTy->getNumParams() == 0 &&
514 !CI->getCalledFunction()) {
515 // If so, insert a new cast instruction, casting it to a function type
516 // that matches the current arguments...
518 std::vector<const Type *> Params; // Parameter types...
519 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
520 Params.push_back(CI->getOperand(i)->getType());
522 FunctionType *NewFT = FunctionType::get(FTy->getReturnType(),
524 PointerType *NewPFunTy = PointerType::get(NewFT);
526 // Create a new cast, inserting it right before the function call...
528 Constant *ConstantCallSrc = 0;
529 if (Constant *CS = dyn_cast<Constant>(CI->getCalledValue()))
530 ConstantCallSrc = CS;
533 NewCast = ConstantExpr::getCast(ConstantCallSrc, NewPFunTy);
535 NewCast = new CastInst(CI->getCalledValue(), NewPFunTy,
536 CI->getCalledValue()->getName()+"_c",CI);
538 // Create a new call instruction...
539 CallInst *NewCall = new CallInst(NewCast,
540 std::vector<Value*>(CI->op_begin()+1, CI->op_end()));
542 ReplaceInstWithInst(CI, NewCall);
544 ++NumVarargCallChanges;
556 bool RPR::DoRaisePass(Function &F) {
557 bool Changed = false;
558 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
559 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
560 DEBUG(std::cerr << "LevelRaising: " << *BI);
561 if (dceInstruction(BI) || doConstantPropagation(BI)) {
564 DEBUG(std::cerr << "***\t\t^^-- Dead code eliminated!\n");
565 } else if (PeepholeOptimize(BB, BI)) {
576 // runOnFunction - Raise a function representation to a higher level.
577 bool RPR::runOnFunction(Function &F) {
578 DEBUG(std::cerr << "\n\n\nStarting to work on Function '" << F.getName()
581 // Insert casts for all incoming pointer pointer values that are treated as
584 bool Changed = false, LocalChange;
586 // If the StartInst option was specified, then Peephole optimize that
587 // instruction first if it occurs in this function.
589 if (!StartInst.empty()) {
590 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
591 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI)
592 if (BI->getName() == StartInst) {
593 bool SavedDebug = DebugFlag; // Save the DEBUG() controlling flag.
594 DebugFlag = true; // Turn on DEBUG's
595 Changed |= PeepholeOptimize(BB, BI);
596 DebugFlag = SavedDebug; // Restore DebugFlag to previous state
601 DEBUG(std::cerr << "Looping: \n" << F);
603 // Iterate over the function, refining it, until it converges on a stable
606 while (DoRaisePass(F)) LocalChange = true;
607 Changed |= LocalChange;
609 } while (LocalChange);