LLVM API Documentation
00001 //===- LevelRaise.cpp - Code to change LLVM to higher level ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the 'raising' part of the LevelChange API. This is 00011 // useful because, in general, it makes the LLVM code terser and easier to 00012 // analyze. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "llvm/Transforms/Scalar.h" 00017 #include "llvm/Transforms/Utils/Local.h" 00018 #include "TransformInternals.h" 00019 #include "llvm/Instructions.h" 00020 #include "llvm/Pass.h" 00021 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00022 #include "llvm/Support/CommandLine.h" 00023 #include "llvm/Support/Debug.h" 00024 #include "llvm/ADT/Statistic.h" 00025 #include "llvm/ADT/STLExtras.h" 00026 #include <algorithm> 00027 using namespace llvm; 00028 00029 // StartInst - This enables the -raise-start-inst=foo option to cause the level 00030 // raising pass to start at instruction "foo", which is immensely useful for 00031 // debugging! 00032 // 00033 static cl::opt<std::string> 00034 StartInst("raise-start-inst", cl::Hidden, cl::value_desc("inst name"), 00035 cl::desc("Start raise pass at the instruction with the specified name")); 00036 00037 static Statistic<> 00038 NumLoadStorePeepholes("raise", "Number of load/store peepholes"); 00039 00040 static Statistic<> 00041 NumGEPInstFormed("raise", "Number of other getelementptr's formed"); 00042 00043 static Statistic<> 00044 NumExprTreesConv("raise", "Number of expression trees converted"); 00045 00046 static Statistic<> 00047 NumCastOfCast("raise", "Number of cast-of-self removed"); 00048 00049 static Statistic<> 00050 NumDCEorCP("raise", "Number of insts DCEd or constprop'd"); 00051 00052 static Statistic<> 00053 NumVarargCallChanges("raise", "Number of vararg call peepholes"); 00054 00055 #define PRINT_PEEPHOLE(ID, NUM, I) \ 00056 DEBUG(std::cerr << "Inst P/H " << ID << "[" << NUM << "] " << I) 00057 00058 #define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0) 00059 #define PRINT_PEEPHOLE2(ID, I1, I2) \ 00060 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0) 00061 #define PRINT_PEEPHOLE3(ID, I1, I2, I3) \ 00062 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \ 00063 PRINT_PEEPHOLE(ID, 2, I3); } while (0) 00064 #define PRINT_PEEPHOLE4(ID, I1, I2, I3, I4) \ 00065 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \ 00066 PRINT_PEEPHOLE(ID, 2, I3); PRINT_PEEPHOLE(ID, 3, I4); } while (0) 00067 00068 namespace { 00069 struct RPR : public FunctionPass { 00070 virtual bool runOnFunction(Function &F); 00071 00072 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00073 AU.setPreservesCFG(); 00074 AU.addRequired<TargetData>(); 00075 } 00076 00077 private: 00078 bool DoRaisePass(Function &F); 00079 bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI); 00080 }; 00081 00082 RegisterOpt<RPR> X("raise", "Raise Pointer References"); 00083 } 00084 00085 00086 FunctionPass *llvm::createRaisePointerReferencesPass() { 00087 return new RPR(); 00088 } 00089 00090 00091 // isReinterpretingCast - Return true if the cast instruction specified will 00092 // cause the operand to be "reinterpreted". A value is reinterpreted if the 00093 // cast instruction would cause the underlying bits to change. 00094 // 00095 static inline bool isReinterpretingCast(const CastInst *CI) { 00096 return!CI->getOperand(0)->getType()->isLosslesslyConvertibleTo(CI->getType()); 00097 } 00098 00099 00100 // Peephole optimize the following instructions: 00101 // %t1 = cast ? to x * 00102 // %t2 = add x * %SP, %t1 ;; Constant must be 2nd operand 00103 // 00104 // Into: %t3 = getelementptr {<...>} * %SP, <element indices> 00105 // %t2 = cast <eltype> * %t3 to {<...>}* 00106 // 00107 static bool HandleCastToPointer(BasicBlock::iterator BI, 00108 const PointerType *DestPTy, 00109 const TargetData &TD) { 00110 CastInst &CI = cast<CastInst>(*BI); 00111 if (CI.use_empty()) return false; 00112 00113 // Scan all of the uses, looking for any uses that are not add or sub 00114 // instructions. If we have non-adds, do not make this transformation. 00115 // 00116 bool HasSubUse = false; // Keep track of any subtracts... 00117 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); 00118 I != E; ++I) 00119 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*I)) { 00120 if ((BO->getOpcode() != Instruction::Add && 00121 BO->getOpcode() != Instruction::Sub) || 00122 // Avoid add sbyte* %X, %X cases... 00123 BO->getOperand(0) == BO->getOperand(1)) 00124 return false; 00125 else 00126 HasSubUse |= BO->getOpcode() == Instruction::Sub; 00127 } else { 00128 return false; 00129 } 00130 00131 std::vector<Value*> Indices; 00132 Value *Src = CI.getOperand(0); 00133 const Type *Result = ConvertibleToGEP(DestPTy, Src, Indices, TD, &BI); 00134 if (Result == 0) return false; // Not convertible... 00135 00136 // Cannot handle subtracts if there is more than one index required... 00137 if (HasSubUse && Indices.size() != 1) return false; 00138 00139 PRINT_PEEPHOLE2("cast-add-to-gep:in", *Src, CI); 00140 00141 // If we have a getelementptr capability... transform all of the 00142 // add instruction uses into getelementptr's. 00143 while (!CI.use_empty()) { 00144 BinaryOperator *I = cast<BinaryOperator>(*CI.use_begin()); 00145 assert((I->getOpcode() == Instruction::Add || 00146 I->getOpcode() == Instruction::Sub) && 00147 "Use is not a valid add instruction!"); 00148 00149 // Get the value added to the cast result pointer... 00150 Value *OtherPtr = I->getOperand((I->getOperand(0) == &CI) ? 1 : 0); 00151 00152 Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName()); 00153 PRINT_PEEPHOLE1("cast-add-to-gep:i", *I); 00154 00155 // If the instruction is actually a subtract, we are guaranteed to only have 00156 // one index (from code above), so we just need to negate the pointer index 00157 // long value. 00158 if (I->getOpcode() == Instruction::Sub) { 00159 Instruction *Neg = BinaryOperator::createNeg(GEP->getOperand(1), 00160 GEP->getOperand(1)->getName()+".neg", I); 00161 GEP->setOperand(1, Neg); 00162 } 00163 00164 if (GEP->getType() == I->getType()) { 00165 // Replace the old add instruction with the shiny new GEP inst 00166 ReplaceInstWithInst(I, GEP); 00167 } else { 00168 // If the type produced by the gep instruction differs from the original 00169 // add instruction type, insert a cast now. 00170 // 00171 00172 // Insert the GEP instruction before the old add instruction... 00173 I->getParent()->getInstList().insert(I, GEP); 00174 00175 PRINT_PEEPHOLE1("cast-add-to-gep:o", *GEP); 00176 GEP = new CastInst(GEP, I->getType()); 00177 00178 // Replace the old add instruction with the shiny new GEP inst 00179 ReplaceInstWithInst(I, GEP); 00180 } 00181 00182 PRINT_PEEPHOLE1("cast-add-to-gep:o", *GEP); 00183 } 00184 return true; 00185 } 00186 00187 // Peephole optimize the following instructions: 00188 // %t1 = cast ulong <const int> to {<...>} * 00189 // %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand 00190 // 00191 // or 00192 // %t1 = cast {<...>}* %SP to int* 00193 // %t5 = cast ulong <const int> to int* 00194 // %t2 = add int* %t1, %t5 ;; int is same size as field 00195 // 00196 // Into: %t3 = getelementptr {<...>} * %SP, <element indices> 00197 // %t2 = cast <eltype> * %t3 to {<...>}* 00198 // 00199 static bool PeepholeOptimizeAddCast(BasicBlock *BB, BasicBlock::iterator &BI, 00200 Value *AddOp1, CastInst *AddOp2, 00201 const TargetData &TD) { 00202 const CompositeType *CompTy; 00203 Value *OffsetVal = AddOp2->getOperand(0); 00204 Value *SrcPtr = 0; // Of type pointer to struct... 00205 00206 if ((CompTy = getPointedToComposite(AddOp1->getType()))) { 00207 SrcPtr = AddOp1; // Handle the first case... 00208 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) { 00209 SrcPtr = AddOp1c->getOperand(0); // Handle the second case... 00210 CompTy = getPointedToComposite(SrcPtr->getType()); 00211 } 00212 00213 // Only proceed if we have detected all of our conditions successfully... 00214 if (!CompTy || !SrcPtr || !OffsetVal->getType()->isInteger()) 00215 return false; 00216 00217 std::vector<Value*> Indices; 00218 if (!ConvertibleToGEP(SrcPtr->getType(), OffsetVal, Indices, TD, &BI)) 00219 return false; // Not convertible... perhaps next time 00220 00221 if (getPointedToComposite(AddOp1->getType())) { // case 1 00222 PRINT_PEEPHOLE2("add-to-gep1:in", *AddOp2, *BI); 00223 } else { 00224 PRINT_PEEPHOLE3("add-to-gep2:in", *AddOp1, *AddOp2, *BI); 00225 } 00226 00227 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Indices, 00228 AddOp2->getName(), BI); 00229 00230 Instruction *NCI = new CastInst(GEP, AddOp1->getType()); 00231 ReplaceInstWithInst(BB->getInstList(), BI, NCI); 00232 PRINT_PEEPHOLE2("add-to-gep:out", *GEP, *NCI); 00233 return true; 00234 } 00235 00236 bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { 00237 Instruction *I = BI; 00238 const TargetData &TD = getAnalysis<TargetData>(); 00239 00240 if (CastInst *CI = dyn_cast<CastInst>(I)) { 00241 Value *Src = CI->getOperand(0); 00242 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source 00243 const Type *DestTy = CI->getType(); 00244 00245 // Peephole optimize the following instruction: 00246 // %V2 = cast <ty> %V to <ty> 00247 // 00248 // Into: <nothing> 00249 // 00250 if (DestTy == Src->getType()) { // Check for a cast to same type as src!! 00251 PRINT_PEEPHOLE1("cast-of-self-ty", *CI); 00252 CI->replaceAllUsesWith(Src); 00253 if (!Src->hasName() && CI->hasName()) { 00254 std::string Name = CI->getName(); 00255 CI->setName(""); 00256 Src->setName(Name, &BB->getParent()->getSymbolTable()); 00257 } 00258 00259 // DCE the instruction now, to avoid having the iterative version of DCE 00260 // have to worry about it. 00261 // 00262 BI = BB->getInstList().erase(BI); 00263 00264 ++NumCastOfCast; 00265 return true; 00266 } 00267 00268 // Check to see if it's a cast of an instruction that does not depend on the 00269 // specific type of the operands to do it's job. 00270 if (!isReinterpretingCast(CI)) { 00271 ValueTypeCache ConvertedTypes; 00272 00273 // Check to see if we can convert the source of the cast to match the 00274 // destination type of the cast... 00275 // 00276 ConvertedTypes[CI] = CI->getType(); // Make sure the cast doesn't change 00277 if (ExpressionConvertibleToType(Src, DestTy, ConvertedTypes, TD)) { 00278 PRINT_PEEPHOLE3("CAST-SRC-EXPR-CONV:in ", *Src, *CI, *BB->getParent()); 00279 00280 DEBUG(std::cerr << "\nCONVERTING SRC EXPR TYPE:\n"); 00281 { // ValueMap must be destroyed before function verified! 00282 ValueMapCache ValueMap; 00283 Value *E = ConvertExpressionToType(Src, DestTy, ValueMap, TD); 00284 00285 if (Constant *CPV = dyn_cast<Constant>(E)) 00286 CI->replaceAllUsesWith(CPV); 00287 00288 PRINT_PEEPHOLE1("CAST-SRC-EXPR-CONV:out", *E); 00289 DEBUG(std::cerr << "DONE CONVERTING SRC EXPR TYPE: \n" 00290 << *BB->getParent()); 00291 } 00292 00293 BI = BB->begin(); // Rescan basic block. BI might be invalidated. 00294 ++NumExprTreesConv; 00295 return true; 00296 } 00297 00298 // Check to see if we can convert the users of the cast value to match the 00299 // source type of the cast... 00300 // 00301 ConvertedTypes.clear(); 00302 // Make sure the source doesn't change type 00303 ConvertedTypes[Src] = Src->getType(); 00304 if (ValueConvertibleToType(CI, Src->getType(), ConvertedTypes, TD)) { 00305 //PRINT_PEEPHOLE3("CAST-DEST-EXPR-CONV:in ", *Src, *CI, 00306 // *BB->getParent()); 00307 00308 DEBUG(std::cerr << "\nCONVERTING EXPR TYPE:\n"); 00309 { // ValueMap must be destroyed before function verified! 00310 ValueMapCache ValueMap; 00311 ConvertValueToNewType(CI, Src, ValueMap, TD); // This will delete CI! 00312 } 00313 00314 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", *Src); 00315 DEBUG(std::cerr << "DONE CONVERTING EXPR TYPE: \n\n" << 00316 *BB->getParent()); 00317 00318 BI = BB->begin(); // Rescan basic block. BI might be invalidated. 00319 ++NumExprTreesConv; 00320 return true; 00321 } 00322 } 00323 00324 // Otherwise find out it this cast is a cast to a pointer type, which is 00325 // then added to some other pointer, then loaded or stored through. If 00326 // so, convert the add into a getelementptr instruction... 00327 // 00328 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) { 00329 if (HandleCastToPointer(BI, DestPTy, TD)) { 00330 BI = BB->begin(); // Rescan basic block. BI might be invalidated. 00331 ++NumGEPInstFormed; 00332 return true; 00333 } 00334 } 00335 00336 // Check to see if we are casting from a structure pointer to a pointer to 00337 // the first element of the structure... to avoid munching other peepholes, 00338 // we only let this happen if there are no add uses of the cast. 00339 // 00340 // Peephole optimize the following instructions: 00341 // %t1 = cast {<...>} * %StructPtr to <ty> * 00342 // 00343 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...> 00344 // %t1 = cast <eltype> * %t1 to <ty> * 00345 // 00346 if (const CompositeType *CTy = getPointedToComposite(Src->getType())) 00347 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) { 00348 00349 // Loop over uses of the cast, checking for add instructions. If an add 00350 // exists, this is probably a part of a more complex GEP, so we don't 00351 // want to mess around with the cast. 00352 // 00353 bool HasAddUse = false; 00354 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end(); 00355 I != E; ++I) 00356 if (isa<Instruction>(*I) && 00357 cast<Instruction>(*I)->getOpcode() == Instruction::Add) { 00358 HasAddUse = true; break; 00359 } 00360 00361 // If it doesn't have an add use, check to see if the dest type is 00362 // losslessly convertible to one of the types in the start of the struct 00363 // type. 00364 // 00365 if (!HasAddUse) { 00366 const Type *DestPointedTy = DestPTy->getElementType(); 00367 unsigned Depth = 1; 00368 const CompositeType *CurCTy = CTy; 00369 const Type *ElTy = 0; 00370 00371 // Build the index vector, full of all zeros 00372 std::vector<Value*> Indices; 00373 00374 Indices.push_back(Constant::getNullValue(Type::UIntTy)); 00375 while (CurCTy && !isa<PointerType>(CurCTy)) { 00376 if (const StructType *CurSTy = dyn_cast<StructType>(CurCTy)) { 00377 // Check for a zero element struct type... if we have one, bail. 00378 if (CurSTy->getNumElements() == 0) break; 00379 00380 // Grab the first element of the struct type, which must lie at 00381 // offset zero in the struct. 00382 // 00383 ElTy = CurSTy->getElementType(0); 00384 } else { 00385 ElTy = cast<ArrayType>(CurCTy)->getElementType(); 00386 } 00387 00388 // Insert a zero to index through this type... 00389 Indices.push_back(Constant::getNullValue(Type::UIntTy)); 00390 00391 // Did we find what we're looking for? 00392 if (ElTy->isLosslesslyConvertibleTo(DestPointedTy)) break; 00393 00394 // Nope, go a level deeper. 00395 ++Depth; 00396 CurCTy = dyn_cast<CompositeType>(ElTy); 00397 ElTy = 0; 00398 } 00399 00400 // Did we find what we were looking for? If so, do the transformation 00401 if (ElTy) { 00402 PRINT_PEEPHOLE1("cast-for-first:in", *CI); 00403 00404 std::string Name = CI->getName(); CI->setName(""); 00405 00406 // Insert the new T cast instruction... stealing old T's name 00407 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices, 00408 Name, BI); 00409 00410 // Make the old cast instruction reference the new GEP instead of 00411 // the old src value. 00412 // 00413 CI->setOperand(0, GEP); 00414 00415 PRINT_PEEPHOLE2("cast-for-first:out", *GEP, *CI); 00416 ++NumGEPInstFormed; 00417 return true; 00418 } 00419 } 00420 } 00421 00422 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 00423 Value *Val = SI->getOperand(0); 00424 Value *Pointer = SI->getPointerOperand(); 00425 00426 // Peephole optimize the following instructions: 00427 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertible to T2 00428 // store <T2> %V, <T2>* %t 00429 // 00430 // Into: 00431 // %t = cast <T2> %V to <T1> 00432 // store <T1> %t2, <T1>* %P 00433 // 00434 // Note: This is not taken care of by expr conversion because there might 00435 // not be a cast available for the store to convert the incoming value of. 00436 // This code is basically here to make sure that pointers don't have casts 00437 // if possible. 00438 // 00439 if (CastInst *CI = dyn_cast<CastInst>(Pointer)) 00440 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType 00441 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType())) 00442 // convertible types? 00443 if (Val->getType()->isLosslesslyConvertibleTo(CSPT->getElementType())) { 00444 PRINT_PEEPHOLE3("st-src-cast:in ", *Pointer, *Val, *SI); 00445 00446 // Insert the new T cast instruction... stealing old T's name 00447 std::string Name(CI->getName()); CI->setName(""); 00448 CastInst *NCI = new CastInst(Val, CSPT->getElementType(), 00449 Name, BI); 00450 00451 // Replace the old store with a new one! 00452 ReplaceInstWithInst(BB->getInstList(), BI, 00453 SI = new StoreInst(NCI, CastSrc)); 00454 PRINT_PEEPHOLE3("st-src-cast:out", *NCI, *CastSrc, *SI); 00455 ++NumLoadStorePeepholes; 00456 return true; 00457 } 00458 00459 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 00460 Value *Pointer = LI->getOperand(0); 00461 const Type *PtrElType = 00462 cast<PointerType>(Pointer->getType())->getElementType(); 00463 00464 // Peephole optimize the following instructions: 00465 // %Val = cast <T1>* to <T2>* ;; If T1 is losslessly convertible to T2 00466 // %t = load <T2>* %P 00467 // 00468 // Into: 00469 // %t = load <T1>* %P 00470 // %Val = cast <T1> to <T2> 00471 // 00472 // Note: This is not taken care of by expr conversion because there might 00473 // not be a cast available for the store to convert the incoming value of. 00474 // This code is basically here to make sure that pointers don't have casts 00475 // if possible. 00476 // 00477 if (CastInst *CI = dyn_cast<CastInst>(Pointer)) 00478 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType 00479 if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType())) 00480 // convertible types? 00481 if (PtrElType->isLosslesslyConvertibleTo(CSPT->getElementType())) { 00482 PRINT_PEEPHOLE2("load-src-cast:in ", *Pointer, *LI); 00483 00484 // Create the new load instruction... loading the pre-casted value 00485 LoadInst *NewLI = new LoadInst(CastSrc, LI->getName(), BI); 00486 00487 // Insert the new T cast instruction... stealing old T's name 00488 CastInst *NCI = new CastInst(NewLI, LI->getType(), CI->getName()); 00489 00490 // Replace the old store with a new one! 00491 ReplaceInstWithInst(BB->getInstList(), BI, NCI); 00492 PRINT_PEEPHOLE3("load-src-cast:out", *NCI, *CastSrc, *NewLI); 00493 ++NumLoadStorePeepholes; 00494 return true; 00495 } 00496 00497 } else if (I->getOpcode() == Instruction::Add && 00498 isa<CastInst>(I->getOperand(1))) { 00499 00500 if (PeepholeOptimizeAddCast(BB, BI, I->getOperand(0), 00501 cast<CastInst>(I->getOperand(1)), TD)) { 00502 ++NumGEPInstFormed; 00503 return true; 00504 } 00505 } else if (CallInst *CI = dyn_cast<CallInst>(I)) { 00506 // If we have a call with all varargs arguments, convert the call to use the 00507 // actual argument types present... 00508 // 00509 const PointerType *PTy = cast<PointerType>(CI->getCalledValue()->getType()); 00510 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 00511 00512 // Is the call to a vararg variable with no real parameters? 00513 if (FTy->isVarArg() && FTy->getNumParams() == 0 && 00514 !CI->getCalledFunction()) { 00515 // If so, insert a new cast instruction, casting it to a function type 00516 // that matches the current arguments... 00517 // 00518 std::vector<const Type *> Params; // Parameter types... 00519 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) 00520 Params.push_back(CI->getOperand(i)->getType()); 00521 00522 FunctionType *NewFT = FunctionType::get(FTy->getReturnType(), 00523 Params, false); 00524 PointerType *NewPFunTy = PointerType::get(NewFT); 00525 00526 // Create a new cast, inserting it right before the function call... 00527 Value *NewCast; 00528 Constant *ConstantCallSrc = 0; 00529 if (Constant *CS = dyn_cast<Constant>(CI->getCalledValue())) 00530 ConstantCallSrc = CS; 00531 00532 if (ConstantCallSrc) 00533 NewCast = ConstantExpr::getCast(ConstantCallSrc, NewPFunTy); 00534 else 00535 NewCast = new CastInst(CI->getCalledValue(), NewPFunTy, 00536 CI->getCalledValue()->getName()+"_c",CI); 00537 00538 // Create a new call instruction... 00539 CallInst *NewCall = new CallInst(NewCast, 00540 std::vector<Value*>(CI->op_begin()+1, CI->op_end())); 00541 ++BI; 00542 ReplaceInstWithInst(CI, NewCall); 00543 00544 ++NumVarargCallChanges; 00545 return true; 00546 } 00547 00548 } 00549 00550 return false; 00551 } 00552 00553 00554 00555 00556 bool RPR::DoRaisePass(Function &F) { 00557 bool Changed = false; 00558 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) 00559 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { 00560 DEBUG(std::cerr << "LevelRaising: " << *BI); 00561 if (dceInstruction(BI) || doConstantPropagation(BI)) { 00562 Changed = true; 00563 ++NumDCEorCP; 00564 DEBUG(std::cerr << "***\t\t^^-- Dead code eliminated!\n"); 00565 } else if (PeepholeOptimize(BB, BI)) { 00566 Changed = true; 00567 } else { 00568 ++BI; 00569 } 00570 } 00571 00572 return Changed; 00573 } 00574 00575 00576 // runOnFunction - Raise a function representation to a higher level. 00577 bool RPR::runOnFunction(Function &F) { 00578 DEBUG(std::cerr << "\n\n\nStarting to work on Function '" << F.getName() 00579 << "'\n"); 00580 00581 // Insert casts for all incoming pointer pointer values that are treated as 00582 // arrays... 00583 // 00584 bool Changed = false, LocalChange; 00585 00586 // If the StartInst option was specified, then Peephole optimize that 00587 // instruction first if it occurs in this function. 00588 // 00589 if (!StartInst.empty()) { 00590 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) 00591 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) 00592 if (BI->getName() == StartInst) { 00593 bool SavedDebug = DebugFlag; // Save the DEBUG() controlling flag. 00594 DebugFlag = true; // Turn on DEBUG's 00595 Changed |= PeepholeOptimize(BB, BI); 00596 DebugFlag = SavedDebug; // Restore DebugFlag to previous state 00597 } 00598 } 00599 00600 do { 00601 DEBUG(std::cerr << "Looping: \n" << F); 00602 00603 // Iterate over the function, refining it, until it converges on a stable 00604 // state 00605 LocalChange = false; 00606 while (DoRaisePass(F)) LocalChange = true; 00607 Changed |= LocalChange; 00608 00609 } while (LocalChange); 00610 00611 return Changed; 00612 } 00613