GlobalOpt.cpp revision db973e60ced33cfa0332462b1a2d5ae1ae15ad25
1//===- GlobalOpt.cpp - Optimize Global Variables --------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 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. 7// 8//===----------------------------------------------------------------------===// 9// 10// This pass transforms simple global variables that never have their address 11// taken. If obviously true, it marks read/write globals as constant, deletes 12// variables only stored to, etc. 13// 14//===----------------------------------------------------------------------===// 15 16#define DEBUG_TYPE "globalopt" 17#include "llvm/Transforms/IPO.h" 18#include "llvm/CallingConv.h" 19#include "llvm/Constants.h" 20#include "llvm/DerivedTypes.h" 21#include "llvm/Instructions.h" 22#include "llvm/IntrinsicInst.h" 23#include "llvm/Module.h" 24#include "llvm/Pass.h" 25#include "llvm/Support/Debug.h" 26#include "llvm/Target/TargetData.h" 27#include "llvm/Transforms/Utils/Local.h" 28#include "llvm/ADT/Statistic.h" 29#include "llvm/ADT/StringExtras.h" 30#include <set> 31#include <algorithm> 32using namespace llvm; 33 34namespace { 35 Statistic<> NumMarked ("globalopt", "Number of globals marked constant"); 36 Statistic<> NumSRA ("globalopt", "Number of aggregate globals broken " 37 "into scalars"); 38 Statistic<> NumSubstitute("globalopt", 39 "Number of globals with initializers stored into them"); 40 Statistic<> NumDeleted ("globalopt", "Number of globals deleted"); 41 Statistic<> NumFnDeleted("globalopt", "Number of functions deleted"); 42 Statistic<> NumGlobUses ("globalopt", "Number of global uses devirtualized"); 43 Statistic<> NumLocalized("globalopt", "Number of globals localized"); 44 Statistic<> NumShrunkToBool("globalopt", 45 "Number of global vars shrunk to booleans"); 46 Statistic<> NumFastCallFns("globalopt", 47 "Number of functions converted to fastcc"); 48 Statistic<> NumEmptyCtor ("globalopt", "Number of empty ctors removed"); 49 50 struct GlobalOpt : public ModulePass { 51 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 52 AU.addRequired<TargetData>(); 53 } 54 55 bool runOnModule(Module &M); 56 57 private: 58 GlobalVariable *FindGlobalCtors(Module &M); 59 bool OptimizeFunctions(Module &M); 60 bool OptimizeGlobalVars(Module &M); 61 bool OptimizeGlobalCtorsList(GlobalVariable *&GCL); 62 bool ProcessInternalGlobal(GlobalVariable *GV, Module::global_iterator &GVI); 63 }; 64 65 RegisterOpt<GlobalOpt> X("globalopt", "Global Variable Optimizer"); 66} 67 68ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); } 69 70/// GlobalStatus - As we analyze each global, keep track of some information 71/// about it. If we find out that the address of the global is taken, none of 72/// this info will be accurate. 73struct GlobalStatus { 74 /// isLoaded - True if the global is ever loaded. If the global isn't ever 75 /// loaded it can be deleted. 76 bool isLoaded; 77 78 /// StoredType - Keep track of what stores to the global look like. 79 /// 80 enum StoredType { 81 /// NotStored - There is no store to this global. It can thus be marked 82 /// constant. 83 NotStored, 84 85 /// isInitializerStored - This global is stored to, but the only thing 86 /// stored is the constant it was initialized with. This is only tracked 87 /// for scalar globals. 88 isInitializerStored, 89 90 /// isStoredOnce - This global is stored to, but only its initializer and 91 /// one other value is ever stored to it. If this global isStoredOnce, we 92 /// track the value stored to it in StoredOnceValue below. This is only 93 /// tracked for scalar globals. 94 isStoredOnce, 95 96 /// isStored - This global is stored to by multiple values or something else 97 /// that we cannot track. 98 isStored 99 } StoredType; 100 101 /// StoredOnceValue - If only one value (besides the initializer constant) is 102 /// ever stored to this global, keep track of what value it is. 103 Value *StoredOnceValue; 104 105 // AccessingFunction/HasMultipleAccessingFunctions - These start out 106 // null/false. When the first accessing function is noticed, it is recorded. 107 // When a second different accessing function is noticed, 108 // HasMultipleAccessingFunctions is set to true. 109 Function *AccessingFunction; 110 bool HasMultipleAccessingFunctions; 111 112 // HasNonInstructionUser - Set to true if this global has a user that is not 113 // an instruction (e.g. a constant expr or GV initializer). 114 bool HasNonInstructionUser; 115 116 /// isNotSuitableForSRA - Keep track of whether any SRA preventing users of 117 /// the global exist. Such users include GEP instruction with variable 118 /// indexes, and non-gep/load/store users like constant expr casts. 119 bool isNotSuitableForSRA; 120 121 GlobalStatus() : isLoaded(false), StoredType(NotStored), StoredOnceValue(0), 122 AccessingFunction(0), HasMultipleAccessingFunctions(false), 123 HasNonInstructionUser(false), isNotSuitableForSRA(false) {} 124}; 125 126 127 128/// ConstantIsDead - Return true if the specified constant is (transitively) 129/// dead. The constant may be used by other constants (e.g. constant arrays and 130/// constant exprs) as long as they are dead, but it cannot be used by anything 131/// else. 132static bool ConstantIsDead(Constant *C) { 133 if (isa<GlobalValue>(C)) return false; 134 135 for (Value::use_iterator UI = C->use_begin(), E = C->use_end(); UI != E; ++UI) 136 if (Constant *CU = dyn_cast<Constant>(*UI)) { 137 if (!ConstantIsDead(CU)) return false; 138 } else 139 return false; 140 return true; 141} 142 143 144/// AnalyzeGlobal - Look at all uses of the global and fill in the GlobalStatus 145/// structure. If the global has its address taken, return true to indicate we 146/// can't do anything with it. 147/// 148static bool AnalyzeGlobal(Value *V, GlobalStatus &GS, 149 std::set<PHINode*> &PHIUsers) { 150 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) 151 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { 152 GS.HasNonInstructionUser = true; 153 154 if (AnalyzeGlobal(CE, GS, PHIUsers)) return true; 155 if (CE->getOpcode() != Instruction::GetElementPtr) 156 GS.isNotSuitableForSRA = true; 157 else if (!GS.isNotSuitableForSRA) { 158 // Check to see if this ConstantExpr GEP is SRA'able. In particular, we 159 // don't like < 3 operand CE's, and we don't like non-constant integer 160 // indices. 161 if (CE->getNumOperands() < 3 || !CE->getOperand(1)->isNullValue()) 162 GS.isNotSuitableForSRA = true; 163 else { 164 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) 165 if (!isa<ConstantInt>(CE->getOperand(i))) { 166 GS.isNotSuitableForSRA = true; 167 break; 168 } 169 } 170 } 171 172 } else if (Instruction *I = dyn_cast<Instruction>(*UI)) { 173 if (!GS.HasMultipleAccessingFunctions) { 174 Function *F = I->getParent()->getParent(); 175 if (GS.AccessingFunction == 0) 176 GS.AccessingFunction = F; 177 else if (GS.AccessingFunction != F) 178 GS.HasMultipleAccessingFunctions = true; 179 } 180 if (isa<LoadInst>(I)) { 181 GS.isLoaded = true; 182 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 183 // Don't allow a store OF the address, only stores TO the address. 184 if (SI->getOperand(0) == V) return true; 185 186 // If this is a direct store to the global (i.e., the global is a scalar 187 // value, not an aggregate), keep more specific information about 188 // stores. 189 if (GS.StoredType != GlobalStatus::isStored) 190 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(SI->getOperand(1))){ 191 Value *StoredVal = SI->getOperand(0); 192 if (StoredVal == GV->getInitializer()) { 193 if (GS.StoredType < GlobalStatus::isInitializerStored) 194 GS.StoredType = GlobalStatus::isInitializerStored; 195 } else if (isa<LoadInst>(StoredVal) && 196 cast<LoadInst>(StoredVal)->getOperand(0) == GV) { 197 // G = G 198 if (GS.StoredType < GlobalStatus::isInitializerStored) 199 GS.StoredType = GlobalStatus::isInitializerStored; 200 } else if (GS.StoredType < GlobalStatus::isStoredOnce) { 201 GS.StoredType = GlobalStatus::isStoredOnce; 202 GS.StoredOnceValue = StoredVal; 203 } else if (GS.StoredType == GlobalStatus::isStoredOnce && 204 GS.StoredOnceValue == StoredVal) { 205 // noop. 206 } else { 207 GS.StoredType = GlobalStatus::isStored; 208 } 209 } else { 210 GS.StoredType = GlobalStatus::isStored; 211 } 212 } else if (isa<GetElementPtrInst>(I)) { 213 if (AnalyzeGlobal(I, GS, PHIUsers)) return true; 214 215 // If the first two indices are constants, this can be SRA'd. 216 if (isa<GlobalVariable>(I->getOperand(0))) { 217 if (I->getNumOperands() < 3 || !isa<Constant>(I->getOperand(1)) || 218 !cast<Constant>(I->getOperand(1))->isNullValue() || 219 !isa<ConstantInt>(I->getOperand(2))) 220 GS.isNotSuitableForSRA = true; 221 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I->getOperand(0))){ 222 if (CE->getOpcode() != Instruction::GetElementPtr || 223 CE->getNumOperands() < 3 || I->getNumOperands() < 2 || 224 !isa<Constant>(I->getOperand(0)) || 225 !cast<Constant>(I->getOperand(0))->isNullValue()) 226 GS.isNotSuitableForSRA = true; 227 } else { 228 GS.isNotSuitableForSRA = true; 229 } 230 } else if (isa<SelectInst>(I)) { 231 if (AnalyzeGlobal(I, GS, PHIUsers)) return true; 232 GS.isNotSuitableForSRA = true; 233 } else if (PHINode *PN = dyn_cast<PHINode>(I)) { 234 // PHI nodes we can check just like select or GEP instructions, but we 235 // have to be careful about infinite recursion. 236 if (PHIUsers.insert(PN).second) // Not already visited. 237 if (AnalyzeGlobal(I, GS, PHIUsers)) return true; 238 GS.isNotSuitableForSRA = true; 239 } else if (isa<SetCondInst>(I)) { 240 GS.isNotSuitableForSRA = true; 241 } else if (isa<MemCpyInst>(I) || isa<MemMoveInst>(I)) { 242 if (I->getOperand(1) == V) 243 GS.StoredType = GlobalStatus::isStored; 244 if (I->getOperand(2) == V) 245 GS.isLoaded = true; 246 GS.isNotSuitableForSRA = true; 247 } else if (isa<MemSetInst>(I)) { 248 assert(I->getOperand(1) == V && "Memset only takes one pointer!"); 249 GS.StoredType = GlobalStatus::isStored; 250 GS.isNotSuitableForSRA = true; 251 } else { 252 return true; // Any other non-load instruction might take address! 253 } 254 } else if (Constant *C = dyn_cast<Constant>(*UI)) { 255 GS.HasNonInstructionUser = true; 256 // We might have a dead and dangling constant hanging off of here. 257 if (!ConstantIsDead(C)) 258 return true; 259 } else { 260 GS.HasNonInstructionUser = true; 261 // Otherwise must be some other user. 262 return true; 263 } 264 265 return false; 266} 267 268static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) { 269 ConstantInt *CI = dyn_cast<ConstantInt>(Idx); 270 if (!CI) return 0; 271 unsigned IdxV = (unsigned)CI->getRawValue(); 272 273 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg)) { 274 if (IdxV < CS->getNumOperands()) return CS->getOperand(IdxV); 275 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg)) { 276 if (IdxV < CA->getNumOperands()) return CA->getOperand(IdxV); 277 } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(Agg)) { 278 if (IdxV < CP->getNumOperands()) return CP->getOperand(IdxV); 279 } else if (isa<ConstantAggregateZero>(Agg)) { 280 if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) { 281 if (IdxV < STy->getNumElements()) 282 return Constant::getNullValue(STy->getElementType(IdxV)); 283 } else if (const SequentialType *STy = 284 dyn_cast<SequentialType>(Agg->getType())) { 285 return Constant::getNullValue(STy->getElementType()); 286 } 287 } else if (isa<UndefValue>(Agg)) { 288 if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) { 289 if (IdxV < STy->getNumElements()) 290 return UndefValue::get(STy->getElementType(IdxV)); 291 } else if (const SequentialType *STy = 292 dyn_cast<SequentialType>(Agg->getType())) { 293 return UndefValue::get(STy->getElementType()); 294 } 295 } 296 return 0; 297} 298 299static Constant *TraverseGEPInitializer(User *GEP, Constant *Init) { 300 if (Init == 0) return 0; 301 if (GEP->getNumOperands() == 1 || 302 !isa<Constant>(GEP->getOperand(1)) || 303 !cast<Constant>(GEP->getOperand(1))->isNullValue()) 304 return 0; 305 306 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) { 307 ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i)); 308 if (!Idx) return 0; 309 Init = getAggregateConstantElement(Init, Idx); 310 if (Init == 0) return 0; 311 } 312 return Init; 313} 314 315/// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all 316/// users of the global, cleaning up the obvious ones. This is largely just a 317/// quick scan over the use list to clean up the easy and obvious cruft. This 318/// returns true if it made a change. 319static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) { 320 bool Changed = false; 321 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) { 322 User *U = *UI++; 323 324 if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 325 if (Init) { 326 // Replace the load with the initializer. 327 LI->replaceAllUsesWith(Init); 328 LI->eraseFromParent(); 329 Changed = true; 330 } 331 } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 332 // Store must be unreachable or storing Init into the global. 333 SI->eraseFromParent(); 334 Changed = true; 335 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { 336 if (CE->getOpcode() == Instruction::GetElementPtr) { 337 Constant *SubInit = TraverseGEPInitializer(CE, Init); 338 Changed |= CleanupConstantGlobalUsers(CE, SubInit); 339 } else if (CE->getOpcode() == Instruction::Cast && 340 isa<PointerType>(CE->getType())) { 341 // Pointer cast, delete any stores and memsets to the global. 342 Changed |= CleanupConstantGlobalUsers(CE, 0); 343 } 344 345 if (CE->use_empty()) { 346 CE->destroyConstant(); 347 Changed = true; 348 } 349 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { 350 Constant *SubInit = TraverseGEPInitializer(GEP, Init); 351 Changed |= CleanupConstantGlobalUsers(GEP, SubInit); 352 353 if (GEP->use_empty()) { 354 GEP->eraseFromParent(); 355 Changed = true; 356 } 357 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv 358 if (MI->getRawDest() == V) { 359 MI->eraseFromParent(); 360 Changed = true; 361 } 362 363 } else if (Constant *C = dyn_cast<Constant>(U)) { 364 // If we have a chain of dead constantexprs or other things dangling from 365 // us, and if they are all dead, nuke them without remorse. 366 if (ConstantIsDead(C)) { 367 C->destroyConstant(); 368 // This could have invalidated UI, start over from scratch. 369 CleanupConstantGlobalUsers(V, Init); 370 return true; 371 } 372 } 373 } 374 return Changed; 375} 376 377/// SRAGlobal - Perform scalar replacement of aggregates on the specified global 378/// variable. This opens the door for other optimizations by exposing the 379/// behavior of the program in a more fine-grained way. We have determined that 380/// this transformation is safe already. We return the first global variable we 381/// insert so that the caller can reprocess it. 382static GlobalVariable *SRAGlobal(GlobalVariable *GV) { 383 assert(GV->hasInternalLinkage() && !GV->isConstant()); 384 Constant *Init = GV->getInitializer(); 385 const Type *Ty = Init->getType(); 386 387 std::vector<GlobalVariable*> NewGlobals; 388 Module::GlobalListType &Globals = GV->getParent()->getGlobalList(); 389 390 if (const StructType *STy = dyn_cast<StructType>(Ty)) { 391 NewGlobals.reserve(STy->getNumElements()); 392 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 393 Constant *In = getAggregateConstantElement(Init, 394 ConstantUInt::get(Type::UIntTy, i)); 395 assert(In && "Couldn't get element of initializer?"); 396 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false, 397 GlobalVariable::InternalLinkage, 398 In, GV->getName()+"."+utostr(i)); 399 Globals.insert(GV, NGV); 400 NewGlobals.push_back(NGV); 401 } 402 } else if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) { 403 unsigned NumElements = 0; 404 if (const ArrayType *ATy = dyn_cast<ArrayType>(STy)) 405 NumElements = ATy->getNumElements(); 406 else if (const PackedType *PTy = dyn_cast<PackedType>(STy)) 407 NumElements = PTy->getNumElements(); 408 else 409 assert(0 && "Unknown aggregate sequential type!"); 410 411 if (NumElements > 16 && GV->hasNUsesOrMore(16)) 412 return 0; // It's not worth it. 413 NewGlobals.reserve(NumElements); 414 for (unsigned i = 0, e = NumElements; i != e; ++i) { 415 Constant *In = getAggregateConstantElement(Init, 416 ConstantUInt::get(Type::UIntTy, i)); 417 assert(In && "Couldn't get element of initializer?"); 418 419 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false, 420 GlobalVariable::InternalLinkage, 421 In, GV->getName()+"."+utostr(i)); 422 Globals.insert(GV, NGV); 423 NewGlobals.push_back(NGV); 424 } 425 } 426 427 if (NewGlobals.empty()) 428 return 0; 429 430 DEBUG(std::cerr << "PERFORMING GLOBAL SRA ON: " << *GV); 431 432 Constant *NullInt = Constant::getNullValue(Type::IntTy); 433 434 // Loop over all of the uses of the global, replacing the constantexpr geps, 435 // with smaller constantexpr geps or direct references. 436 while (!GV->use_empty()) { 437 User *GEP = GV->use_back(); 438 assert(((isa<ConstantExpr>(GEP) && 439 cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)|| 440 isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!"); 441 442 // Ignore the 1th operand, which has to be zero or else the program is quite 443 // broken (undefined). Get the 2nd operand, which is the structure or array 444 // index. 445 unsigned Val = 446 (unsigned)cast<ConstantInt>(GEP->getOperand(2))->getRawValue(); 447 if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access. 448 449 Value *NewPtr = NewGlobals[Val]; 450 451 // Form a shorter GEP if needed. 452 if (GEP->getNumOperands() > 3) 453 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) { 454 std::vector<Constant*> Idxs; 455 Idxs.push_back(NullInt); 456 for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i) 457 Idxs.push_back(CE->getOperand(i)); 458 NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), Idxs); 459 } else { 460 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP); 461 std::vector<Value*> Idxs; 462 Idxs.push_back(NullInt); 463 for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i) 464 Idxs.push_back(GEPI->getOperand(i)); 465 NewPtr = new GetElementPtrInst(NewPtr, Idxs, 466 GEPI->getName()+"."+utostr(Val), GEPI); 467 } 468 GEP->replaceAllUsesWith(NewPtr); 469 470 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP)) 471 GEPI->eraseFromParent(); 472 else 473 cast<ConstantExpr>(GEP)->destroyConstant(); 474 } 475 476 // Delete the old global, now that it is dead. 477 Globals.erase(GV); 478 ++NumSRA; 479 480 // Loop over the new globals array deleting any globals that are obviously 481 // dead. This can arise due to scalarization of a structure or an array that 482 // has elements that are dead. 483 unsigned FirstGlobal = 0; 484 for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i) 485 if (NewGlobals[i]->use_empty()) { 486 Globals.erase(NewGlobals[i]); 487 if (FirstGlobal == i) ++FirstGlobal; 488 } 489 490 return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0; 491} 492 493/// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified 494/// value will trap if the value is dynamically null. 495static bool AllUsesOfValueWillTrapIfNull(Value *V) { 496 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) 497 if (isa<LoadInst>(*UI)) { 498 // Will trap. 499 } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { 500 if (SI->getOperand(0) == V) { 501 //std::cerr << "NONTRAPPING USE: " << **UI; 502 return false; // Storing the value. 503 } 504 } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { 505 if (CI->getOperand(0) != V) { 506 //std::cerr << "NONTRAPPING USE: " << **UI; 507 return false; // Not calling the ptr 508 } 509 } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { 510 if (II->getOperand(0) != V) { 511 //std::cerr << "NONTRAPPING USE: " << **UI; 512 return false; // Not calling the ptr 513 } 514 } else if (CastInst *CI = dyn_cast<CastInst>(*UI)) { 515 if (!AllUsesOfValueWillTrapIfNull(CI)) return false; 516 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) { 517 if (!AllUsesOfValueWillTrapIfNull(GEPI)) return false; 518 } else if (isa<SetCondInst>(*UI) && 519 isa<ConstantPointerNull>(UI->getOperand(1))) { 520 // Ignore setcc X, null 521 } else { 522 //std::cerr << "NONTRAPPING USE: " << **UI; 523 return false; 524 } 525 return true; 526} 527 528/// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads 529/// from GV will trap if the loaded value is null. Note that this also permits 530/// comparisons of the loaded value against null, as a special case. 531static bool AllUsesOfLoadedValueWillTrapIfNull(GlobalVariable *GV) { 532 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI!=E; ++UI) 533 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 534 if (!AllUsesOfValueWillTrapIfNull(LI)) 535 return false; 536 } else if (isa<StoreInst>(*UI)) { 537 // Ignore stores to the global. 538 } else { 539 // We don't know or understand this user, bail out. 540 //std::cerr << "UNKNOWN USER OF GLOBAL!: " << **UI; 541 return false; 542 } 543 544 return true; 545} 546 547static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) { 548 bool Changed = false; 549 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) { 550 Instruction *I = cast<Instruction>(*UI++); 551 if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 552 LI->setOperand(0, NewV); 553 Changed = true; 554 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 555 if (SI->getOperand(1) == V) { 556 SI->setOperand(1, NewV); 557 Changed = true; 558 } 559 } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) { 560 if (I->getOperand(0) == V) { 561 // Calling through the pointer! Turn into a direct call, but be careful 562 // that the pointer is not also being passed as an argument. 563 I->setOperand(0, NewV); 564 Changed = true; 565 bool PassedAsArg = false; 566 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) 567 if (I->getOperand(i) == V) { 568 PassedAsArg = true; 569 I->setOperand(i, NewV); 570 } 571 572 if (PassedAsArg) { 573 // Being passed as an argument also. Be careful to not invalidate UI! 574 UI = V->use_begin(); 575 } 576 } 577 } else if (CastInst *CI = dyn_cast<CastInst>(I)) { 578 Changed |= OptimizeAwayTrappingUsesOfValue(CI, 579 ConstantExpr::getCast(NewV, CI->getType())); 580 if (CI->use_empty()) { 581 Changed = true; 582 CI->eraseFromParent(); 583 } 584 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { 585 // Should handle GEP here. 586 std::vector<Constant*> Indices; 587 Indices.reserve(GEPI->getNumOperands()-1); 588 for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) 589 if (Constant *C = dyn_cast<Constant>(GEPI->getOperand(i))) 590 Indices.push_back(C); 591 else 592 break; 593 if (Indices.size() == GEPI->getNumOperands()-1) 594 Changed |= OptimizeAwayTrappingUsesOfValue(GEPI, 595 ConstantExpr::getGetElementPtr(NewV, Indices)); 596 if (GEPI->use_empty()) { 597 Changed = true; 598 GEPI->eraseFromParent(); 599 } 600 } 601 } 602 603 return Changed; 604} 605 606 607/// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null 608/// value stored into it. If there are uses of the loaded value that would trap 609/// if the loaded value is dynamically null, then we know that they cannot be 610/// reachable with a null optimize away the load. 611static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV) { 612 std::vector<LoadInst*> Loads; 613 bool Changed = false; 614 615 // Replace all uses of loads with uses of uses of the stored value. 616 for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); 617 GUI != E; ++GUI) 618 if (LoadInst *LI = dyn_cast<LoadInst>(*GUI)) { 619 Loads.push_back(LI); 620 Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV); 621 } else { 622 assert(isa<StoreInst>(*GUI) && "Only expect load and stores!"); 623 } 624 625 if (Changed) { 626 DEBUG(std::cerr << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV); 627 ++NumGlobUses; 628 } 629 630 // Delete all of the loads we can, keeping track of whether we nuked them all! 631 bool AllLoadsGone = true; 632 while (!Loads.empty()) { 633 LoadInst *L = Loads.back(); 634 if (L->use_empty()) { 635 L->eraseFromParent(); 636 Changed = true; 637 } else { 638 AllLoadsGone = false; 639 } 640 Loads.pop_back(); 641 } 642 643 // If we nuked all of the loads, then none of the stores are needed either, 644 // nor is the global. 645 if (AllLoadsGone) { 646 DEBUG(std::cerr << " *** GLOBAL NOW DEAD!\n"); 647 CleanupConstantGlobalUsers(GV, 0); 648 if (GV->use_empty()) { 649 GV->eraseFromParent(); 650 ++NumDeleted; 651 } 652 Changed = true; 653 } 654 return Changed; 655} 656 657/// ConstantPropUsersOf - Walk the use list of V, constant folding all of the 658/// instructions that are foldable. 659static void ConstantPropUsersOf(Value *V) { 660 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) 661 if (Instruction *I = dyn_cast<Instruction>(*UI++)) 662 if (Constant *NewC = ConstantFoldInstruction(I)) { 663 I->replaceAllUsesWith(NewC); 664 665 // Advance UI to the next non-I use to avoid invalidating it! 666 // Instructions could multiply use V. 667 while (UI != E && *UI == I) 668 ++UI; 669 I->eraseFromParent(); 670 } 671} 672 673/// OptimizeGlobalAddressOfMalloc - This function takes the specified global 674/// variable, and transforms the program as if it always contained the result of 675/// the specified malloc. Because it is always the result of the specified 676/// malloc, there is no reason to actually DO the malloc. Instead, turn the 677/// malloc into a global, and any laods of GV as uses of the new global. 678static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, 679 MallocInst *MI) { 680 DEBUG(std::cerr << "PROMOTING MALLOC GLOBAL: " << *GV << " MALLOC = " <<*MI); 681 ConstantInt *NElements = cast<ConstantInt>(MI->getArraySize()); 682 683 if (NElements->getRawValue() != 1) { 684 // If we have an array allocation, transform it to a single element 685 // allocation to make the code below simpler. 686 Type *NewTy = ArrayType::get(MI->getAllocatedType(), 687 (unsigned)NElements->getRawValue()); 688 MallocInst *NewMI = 689 new MallocInst(NewTy, Constant::getNullValue(Type::UIntTy), 690 MI->getName(), MI); 691 std::vector<Value*> Indices; 692 Indices.push_back(Constant::getNullValue(Type::IntTy)); 693 Indices.push_back(Indices[0]); 694 Value *NewGEP = new GetElementPtrInst(NewMI, Indices, 695 NewMI->getName()+".el0", MI); 696 MI->replaceAllUsesWith(NewGEP); 697 MI->eraseFromParent(); 698 MI = NewMI; 699 } 700 701 // Create the new global variable. The contents of the malloc'd memory is 702 // undefined, so initialize with an undef value. 703 Constant *Init = UndefValue::get(MI->getAllocatedType()); 704 GlobalVariable *NewGV = new GlobalVariable(MI->getAllocatedType(), false, 705 GlobalValue::InternalLinkage, Init, 706 GV->getName()+".body"); 707 GV->getParent()->getGlobalList().insert(GV, NewGV); 708 709 // Anything that used the malloc now uses the global directly. 710 MI->replaceAllUsesWith(NewGV); 711 712 Constant *RepValue = NewGV; 713 if (NewGV->getType() != GV->getType()->getElementType()) 714 RepValue = ConstantExpr::getCast(RepValue, GV->getType()->getElementType()); 715 716 // If there is a comparison against null, we will insert a global bool to 717 // keep track of whether the global was initialized yet or not. 718 GlobalVariable *InitBool = 719 new GlobalVariable(Type::BoolTy, false, GlobalValue::InternalLinkage, 720 ConstantBool::False, GV->getName()+".init"); 721 bool InitBoolUsed = false; 722 723 // Loop over all uses of GV, processing them in turn. 724 std::vector<StoreInst*> Stores; 725 while (!GV->use_empty()) 726 if (LoadInst *LI = dyn_cast<LoadInst>(GV->use_back())) { 727 while (!LI->use_empty()) { 728 Use &LoadUse = LI->use_begin().getUse(); 729 if (!isa<SetCondInst>(LoadUse.getUser())) 730 LoadUse = RepValue; 731 else { 732 // Replace the setcc X, 0 with a use of the bool value. 733 SetCondInst *SCI = cast<SetCondInst>(LoadUse.getUser()); 734 Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", SCI); 735 InitBoolUsed = true; 736 switch (SCI->getOpcode()) { 737 default: assert(0 && "Unknown opcode!"); 738 case Instruction::SetLT: 739 LV = ConstantBool::False; // X < null -> always false 740 break; 741 case Instruction::SetEQ: 742 case Instruction::SetLE: 743 LV = BinaryOperator::createNot(LV, "notinit", SCI); 744 break; 745 case Instruction::SetNE: 746 case Instruction::SetGE: 747 case Instruction::SetGT: 748 break; // no change. 749 } 750 SCI->replaceAllUsesWith(LV); 751 SCI->eraseFromParent(); 752 } 753 } 754 LI->eraseFromParent(); 755 } else { 756 StoreInst *SI = cast<StoreInst>(GV->use_back()); 757 // The global is initialized when the store to it occurs. 758 new StoreInst(ConstantBool::True, InitBool, SI); 759 SI->eraseFromParent(); 760 } 761 762 // If the initialization boolean was used, insert it, otherwise delete it. 763 if (!InitBoolUsed) { 764 while (!InitBool->use_empty()) // Delete initializations 765 cast<Instruction>(InitBool->use_back())->eraseFromParent(); 766 delete InitBool; 767 } else 768 GV->getParent()->getGlobalList().insert(GV, InitBool); 769 770 771 // Now the GV is dead, nuke it and the malloc. 772 GV->eraseFromParent(); 773 MI->eraseFromParent(); 774 775 // To further other optimizations, loop over all users of NewGV and try to 776 // constant prop them. This will promote GEP instructions with constant 777 // indices into GEP constant-exprs, which will allow global-opt to hack on it. 778 ConstantPropUsersOf(NewGV); 779 if (RepValue != NewGV) 780 ConstantPropUsersOf(RepValue); 781 782 return NewGV; 783} 784 785/// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking 786/// to make sure that there are no complex uses of V. We permit simple things 787/// like dereferencing the pointer, but not storing through the address, unless 788/// it is to the specified global. 789static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V, 790 GlobalVariable *GV) { 791 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI) 792 if (isa<LoadInst>(*UI) || isa<SetCondInst>(*UI)) { 793 // Fine, ignore. 794 } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { 795 if (SI->getOperand(0) == V && SI->getOperand(1) != GV) 796 return false; // Storing the pointer itself... bad. 797 // Otherwise, storing through it, or storing into GV... fine. 798 } else if (isa<GetElementPtrInst>(*UI) || isa<SelectInst>(*UI)) { 799 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(cast<Instruction>(*UI),GV)) 800 return false; 801 } else { 802 return false; 803 } 804 return true; 805 806} 807 808// OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge 809// that only one value (besides its initializer) is ever stored to the global. 810static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, 811 Module::global_iterator &GVI, TargetData &TD) { 812 if (CastInst *CI = dyn_cast<CastInst>(StoredOnceVal)) 813 StoredOnceVal = CI->getOperand(0); 814 else if (GetElementPtrInst *GEPI =dyn_cast<GetElementPtrInst>(StoredOnceVal)){ 815 // "getelementptr Ptr, 0, 0, 0" is really just a cast. 816 bool IsJustACast = true; 817 for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) 818 if (!isa<Constant>(GEPI->getOperand(i)) || 819 !cast<Constant>(GEPI->getOperand(i))->isNullValue()) { 820 IsJustACast = false; 821 break; 822 } 823 if (IsJustACast) 824 StoredOnceVal = GEPI->getOperand(0); 825 } 826 827 // If we are dealing with a pointer global that is initialized to null and 828 // only has one (non-null) value stored into it, then we can optimize any 829 // users of the loaded value (often calls and loads) that would trap if the 830 // value was null. 831 if (isa<PointerType>(GV->getInitializer()->getType()) && 832 GV->getInitializer()->isNullValue()) { 833 if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) { 834 if (GV->getInitializer()->getType() != SOVC->getType()) 835 SOVC = ConstantExpr::getCast(SOVC, GV->getInitializer()->getType()); 836 837 // Optimize away any trapping uses of the loaded value. 838 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC)) 839 return true; 840 } else if (MallocInst *MI = dyn_cast<MallocInst>(StoredOnceVal)) { 841 // If we have a global that is only initialized with a fixed size malloc, 842 // and if all users of the malloc trap, and if the malloc'd address is not 843 // put anywhere else, transform the program to use global memory instead 844 // of malloc'd memory. This eliminates dynamic allocation (good) and 845 // exposes the resultant global to further GlobalOpt (even better). Note 846 // that we restrict this transformation to only working on small 847 // allocations (2048 bytes currently), as we don't want to introduce a 16M 848 // global or something. 849 if (ConstantInt *NElements = dyn_cast<ConstantInt>(MI->getArraySize())) 850 if (MI->getAllocatedType()->isSized() && 851 NElements->getRawValue()* 852 TD.getTypeSize(MI->getAllocatedType()) < 2048 && 853 AllUsesOfLoadedValueWillTrapIfNull(GV) && 854 ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV)) { 855 GVI = OptimizeGlobalAddressOfMalloc(GV, MI); 856 return true; 857 } 858 } 859 } 860 861 return false; 862} 863 864/// ShrinkGlobalToBoolean - At this point, we have learned that the only two 865/// values ever stored into GV are its initializer and OtherVal. 866static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) { 867 // Create the new global, initializing it to false. 868 GlobalVariable *NewGV = new GlobalVariable(Type::BoolTy, false, 869 GlobalValue::InternalLinkage, ConstantBool::False, GV->getName()+".b"); 870 GV->getParent()->getGlobalList().insert(GV, NewGV); 871 872 Constant *InitVal = GV->getInitializer(); 873 assert(InitVal->getType() != Type::BoolTy && "No reason to shrink to bool!"); 874 875 // If initialized to zero and storing one into the global, we can use a cast 876 // instead of a select to synthesize the desired value. 877 bool IsOneZero = false; 878 if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) 879 IsOneZero = InitVal->isNullValue() && CI->equalsInt(1); 880 881 while (!GV->use_empty()) { 882 Instruction *UI = cast<Instruction>(GV->use_back()); 883 if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { 884 // Change the store into a boolean store. 885 bool StoringOther = SI->getOperand(0) == OtherVal; 886 // Only do this if we weren't storing a loaded value. 887 Value *StoreVal; 888 if (StoringOther || SI->getOperand(0) == InitVal) 889 StoreVal = ConstantBool::get(StoringOther); 890 else { 891 // Otherwise, we are storing a previously loaded copy. To do this, 892 // change the copy from copying the original value to just copying the 893 // bool. 894 Instruction *StoredVal = cast<Instruction>(SI->getOperand(0)); 895 896 // If we're already replaced the input, StoredVal will be a cast or 897 // select instruction. If not, it will be a load of the original 898 // global. 899 if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) { 900 assert(LI->getOperand(0) == GV && "Not a copy!"); 901 // Insert a new load, to preserve the saved value. 902 StoreVal = new LoadInst(NewGV, LI->getName()+".b", LI); 903 } else { 904 assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) && 905 "This is not a form that we understand!"); 906 StoreVal = StoredVal->getOperand(0); 907 assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!"); 908 } 909 } 910 new StoreInst(StoreVal, NewGV, SI); 911 } else if (!UI->use_empty()) { 912 // Change the load into a load of bool then a select. 913 LoadInst *LI = cast<LoadInst>(UI); 914 915 std::string Name = LI->getName(); LI->setName(""); 916 LoadInst *NLI = new LoadInst(NewGV, Name+".b", LI); 917 Value *NSI; 918 if (IsOneZero) 919 NSI = new CastInst(NLI, LI->getType(), Name, LI); 920 else 921 NSI = new SelectInst(NLI, OtherVal, InitVal, Name, LI); 922 LI->replaceAllUsesWith(NSI); 923 } 924 UI->eraseFromParent(); 925 } 926 927 GV->eraseFromParent(); 928} 929 930 931/// ProcessInternalGlobal - Analyze the specified global variable and optimize 932/// it if possible. If we make a change, return true. 933bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV, 934 Module::global_iterator &GVI) { 935 std::set<PHINode*> PHIUsers; 936 GlobalStatus GS; 937 PHIUsers.clear(); 938 GV->removeDeadConstantUsers(); 939 940 if (GV->use_empty()) { 941 DEBUG(std::cerr << "GLOBAL DEAD: " << *GV); 942 GV->eraseFromParent(); 943 ++NumDeleted; 944 return true; 945 } 946 947 if (!AnalyzeGlobal(GV, GS, PHIUsers)) { 948 // If this is a first class global and has only one accessing function 949 // and this function is main (which we know is not recursive we can make 950 // this global a local variable) we replace the global with a local alloca 951 // in this function. 952 // 953 // NOTE: It doesn't make sense to promote non first class types since we 954 // are just replacing static memory to stack memory. 955 if (!GS.HasMultipleAccessingFunctions && 956 GS.AccessingFunction && !GS.HasNonInstructionUser && 957 GV->getType()->getElementType()->isFirstClassType() && 958 GS.AccessingFunction->getName() == "main" && 959 GS.AccessingFunction->hasExternalLinkage()) { 960 DEBUG(std::cerr << "LOCALIZING GLOBAL: " << *GV); 961 Instruction* FirstI = GS.AccessingFunction->getEntryBlock().begin(); 962 const Type* ElemTy = GV->getType()->getElementType(); 963 AllocaInst* Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), FirstI); 964 if (!isa<UndefValue>(GV->getInitializer())) 965 new StoreInst(GV->getInitializer(), Alloca, FirstI); 966 967 GV->replaceAllUsesWith(Alloca); 968 GV->eraseFromParent(); 969 ++NumLocalized; 970 return true; 971 } 972 // If the global is never loaded (but may be stored to), it is dead. 973 // Delete it now. 974 if (!GS.isLoaded) { 975 DEBUG(std::cerr << "GLOBAL NEVER LOADED: " << *GV); 976 977 // Delete any stores we can find to the global. We may not be able to 978 // make it completely dead though. 979 bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer()); 980 981 // If the global is dead now, delete it. 982 if (GV->use_empty()) { 983 GV->eraseFromParent(); 984 ++NumDeleted; 985 Changed = true; 986 } 987 return Changed; 988 989 } else if (GS.StoredType <= GlobalStatus::isInitializerStored) { 990 DEBUG(std::cerr << "MARKING CONSTANT: " << *GV); 991 GV->setConstant(true); 992 993 // Clean up any obviously simplifiable users now. 994 CleanupConstantGlobalUsers(GV, GV->getInitializer()); 995 996 // If the global is dead now, just nuke it. 997 if (GV->use_empty()) { 998 DEBUG(std::cerr << " *** Marking constant allowed us to simplify " 999 "all users and delete global!\n"); 1000 GV->eraseFromParent(); 1001 ++NumDeleted; 1002 } 1003 1004 ++NumMarked; 1005 return true; 1006 } else if (!GS.isNotSuitableForSRA && 1007 !GV->getInitializer()->getType()->isFirstClassType()) { 1008 if (GlobalVariable *FirstNewGV = SRAGlobal(GV)) { 1009 GVI = FirstNewGV; // Don't skip the newly produced globals! 1010 return true; 1011 } 1012 } else if (GS.StoredType == GlobalStatus::isStoredOnce) { 1013 // If the initial value for the global was an undef value, and if only 1014 // one other value was stored into it, we can just change the 1015 // initializer to be an undef value, then delete all stores to the 1016 // global. This allows us to mark it constant. 1017 if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) 1018 if (isa<UndefValue>(GV->getInitializer())) { 1019 // Change the initial value here. 1020 GV->setInitializer(SOVConstant); 1021 1022 // Clean up any obviously simplifiable users now. 1023 CleanupConstantGlobalUsers(GV, GV->getInitializer()); 1024 1025 if (GV->use_empty()) { 1026 DEBUG(std::cerr << " *** Substituting initializer allowed us to " 1027 "simplify all users and delete global!\n"); 1028 GV->eraseFromParent(); 1029 ++NumDeleted; 1030 } else { 1031 GVI = GV; 1032 } 1033 ++NumSubstitute; 1034 return true; 1035 } 1036 1037 // Try to optimize globals based on the knowledge that only one value 1038 // (besides its initializer) is ever stored to the global. 1039 if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GVI, 1040 getAnalysis<TargetData>())) 1041 return true; 1042 1043 // Otherwise, if the global was not a boolean, we can shrink it to be a 1044 // boolean. 1045 if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) 1046 if (GV->getType()->getElementType() != Type::BoolTy && 1047 !GV->getType()->getElementType()->isFloatingPoint()) { 1048 DEBUG(std::cerr << " *** SHRINKING TO BOOL: " << *GV); 1049 ShrinkGlobalToBoolean(GV, SOVConstant); 1050 ++NumShrunkToBool; 1051 return true; 1052 } 1053 } 1054 } 1055 return false; 1056} 1057 1058/// OnlyCalledDirectly - Return true if the specified function is only called 1059/// directly. In other words, its address is never taken. 1060static bool OnlyCalledDirectly(Function *F) { 1061 for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ 1062 Instruction *User = dyn_cast<Instruction>(*UI); 1063 if (!User) return false; 1064 if (!isa<CallInst>(User) && !isa<InvokeInst>(User)) return false; 1065 1066 // See if the function address is passed as an argument. 1067 for (unsigned i = 1, e = User->getNumOperands(); i != e; ++i) 1068 if (User->getOperand(i) == F) return false; 1069 } 1070 return true; 1071} 1072 1073/// ChangeCalleesToFastCall - Walk all of the direct calls of the specified 1074/// function, changing them to FastCC. 1075static void ChangeCalleesToFastCall(Function *F) { 1076 for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ 1077 Instruction *User = cast<Instruction>(*UI); 1078 if (CallInst *CI = dyn_cast<CallInst>(User)) 1079 CI->setCallingConv(CallingConv::Fast); 1080 else 1081 cast<InvokeInst>(User)->setCallingConv(CallingConv::Fast); 1082 } 1083} 1084 1085bool GlobalOpt::OptimizeFunctions(Module &M) { 1086 bool Changed = false; 1087 // Optimize functions. 1088 for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) { 1089 Function *F = FI++; 1090 F->removeDeadConstantUsers(); 1091 if (F->use_empty() && (F->hasInternalLinkage() || 1092 F->hasLinkOnceLinkage())) { 1093 M.getFunctionList().erase(F); 1094 Changed = true; 1095 ++NumFnDeleted; 1096 } else if (F->hasInternalLinkage() && 1097 F->getCallingConv() == CallingConv::C && !F->isVarArg() && 1098 OnlyCalledDirectly(F)) { 1099 // If this function has C calling conventions, is not a varargs 1100 // function, and is only called directly, promote it to use the Fast 1101 // calling convention. 1102 F->setCallingConv(CallingConv::Fast); 1103 ChangeCalleesToFastCall(F); 1104 ++NumFastCallFns; 1105 Changed = true; 1106 } 1107 } 1108 return Changed; 1109} 1110 1111bool GlobalOpt::OptimizeGlobalVars(Module &M) { 1112 bool Changed = false; 1113 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); 1114 GVI != E; ) { 1115 GlobalVariable *GV = GVI++; 1116 if (!GV->isConstant() && GV->hasInternalLinkage() && 1117 GV->hasInitializer()) 1118 Changed |= ProcessInternalGlobal(GV, GVI); 1119 } 1120 return Changed; 1121} 1122 1123/// FindGlobalCtors - Find the llvm.globalctors list, verifying that all 1124/// initializers have an init priority of 65535. 1125GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) { 1126 for (Module::giterator I = M.global_begin(), E = M.global_end(); I != E; ++I) 1127 if (I->getName() == "llvm.global_ctors") { 1128 // Found it, verify it's an array of { int, void()* }. 1129 const ArrayType *ATy =dyn_cast<ArrayType>(I->getType()->getElementType()); 1130 if (!ATy) return 0; 1131 const StructType *STy = dyn_cast<StructType>(ATy->getElementType()); 1132 if (!STy || STy->getNumElements() != 2 || 1133 STy->getElementType(0) != Type::IntTy) return 0; 1134 const PointerType *PFTy = dyn_cast<PointerType>(STy->getElementType(1)); 1135 if (!PFTy) return 0; 1136 const FunctionType *FTy = dyn_cast<FunctionType>(PFTy->getElementType()); 1137 if (!FTy || FTy->getReturnType() != Type::VoidTy || FTy->isVarArg() || 1138 FTy->getNumParams() != 0) 1139 return 0; 1140 1141 // Verify that the initializer is simple enough for us to handle. 1142 if (!I->hasInitializer()) return 0; 1143 ConstantArray *CA = dyn_cast<ConstantArray>(I->getInitializer()); 1144 if (!CA) return 0; 1145 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) 1146 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(CA->getOperand(i))) { 1147 if (isa<ConstantPointerNull>(CS->getOperand(1))) 1148 continue; 1149 1150 // Must have a function or null ptr. 1151 if (!isa<Function>(CS->getOperand(1))) 1152 return 0; 1153 1154 // Init priority must be standard. 1155 ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0)); 1156 if (!CI || CI->getRawValue() != 65535) 1157 return 0; 1158 } else { 1159 return 0; 1160 } 1161 1162 return I; 1163 } 1164 return 0; 1165} 1166 1167/// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand, 1168/// return a list of the functions and null terminator as a vector. 1169static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) { 1170 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer()); 1171 std::vector<Function*> Result; 1172 Result.reserve(CA->getNumOperands()); 1173 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) { 1174 ConstantStruct *CS = cast<ConstantStruct>(CA->getOperand(i)); 1175 Result.push_back(dyn_cast<Function>(CS->getOperand(1))); 1176 } 1177 return Result; 1178} 1179 1180/// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the 1181/// specified array, returning the new global to use. 1182static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL, 1183 const std::vector<Function*> &Ctors) { 1184 // If we made a change, reassemble the initializer list. 1185 std::vector<Constant*> CSVals; 1186 CSVals.push_back(ConstantSInt::get(Type::IntTy, 65535)); 1187 CSVals.push_back(0); 1188 1189 // Create the new init list. 1190 std::vector<Constant*> CAList; 1191 for (unsigned i = 0, e = Ctors.size(); i != e; ++i) { 1192 if (Ctors[i]) 1193 CSVals[1] = Ctors[i]; 1194 else { 1195 const Type *FTy = FunctionType::get(Type::VoidTy, 1196 std::vector<const Type*>(), false); 1197 const PointerType *PFTy = PointerType::get(FTy); 1198 CSVals[1] = Constant::getNullValue(PFTy); 1199 CSVals[0] = ConstantSInt::get(Type::IntTy, 2147483647); 1200 } 1201 CAList.push_back(ConstantStruct::get(CSVals)); 1202 } 1203 1204 // Create the array initializer. 1205 const Type *StructTy = 1206 cast<ArrayType>(GCL->getType()->getElementType())->getElementType(); 1207 Constant *CA = ConstantArray::get(ArrayType::get(StructTy, CAList.size()), 1208 CAList); 1209 1210 // If we didn't change the number of elements, don't create a new GV. 1211 if (CA->getType() == GCL->getInitializer()->getType()) { 1212 GCL->setInitializer(CA); 1213 return GCL; 1214 } 1215 1216 // Create the new global and insert it next to the existing list. 1217 GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(), 1218 GCL->getLinkage(), CA, 1219 GCL->getName()); 1220 GCL->setName(""); 1221 GCL->getParent()->getGlobalList().insert(GCL, NGV); 1222 1223 // Nuke the old list, replacing any uses with the new one. 1224 if (!GCL->use_empty()) { 1225 Constant *V = NGV; 1226 if (V->getType() != GCL->getType()) 1227 V = ConstantExpr::getCast(V, GCL->getType()); 1228 GCL->replaceAllUsesWith(V); 1229 } 1230 GCL->eraseFromParent(); 1231 1232 if (Ctors.size()) 1233 return NGV; 1234 else 1235 return 0; 1236} 1237 1238 1239/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible. 1240/// Return true if anything changed. 1241bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) { 1242 std::vector<Function*> Ctors = ParseGlobalCtors(GCL); 1243 bool MadeChange = false; 1244 if (Ctors.empty()) return false; 1245 1246 // Loop over global ctors, optimizing them when we can. 1247 for (unsigned i = 0; i != Ctors.size(); ++i) { 1248 Function *F = Ctors[i]; 1249 // Found a null terminator in the middle of the list, prune off the rest of 1250 // the list. 1251 if (F == 0) { 1252 if (i != Ctors.size()-1) { 1253 Ctors.resize(i+1); 1254 MadeChange = true; 1255 } 1256 break; 1257 } 1258 1259 // If the function is empty, just remove it from the ctor list. 1260 if (!F->empty() && isa<ReturnInst>(F->begin()->getTerminator()) && 1261 &F->begin()->front() == F->begin()->getTerminator()) { 1262 Ctors.erase(Ctors.begin()+i); 1263 MadeChange = true; 1264 --i; 1265 ++NumEmptyCtor; 1266 } 1267 } 1268 1269 if (!MadeChange) return false; 1270 1271 GCL = InstallGlobalCtors(GCL, Ctors); 1272 return true; 1273} 1274 1275 1276bool GlobalOpt::runOnModule(Module &M) { 1277 bool Changed = false; 1278 1279 // Try to find the llvm.globalctors list. 1280 GlobalVariable *GlobalCtors = FindGlobalCtors(M); 1281 1282 bool LocalChange = true; 1283 while (LocalChange) { 1284 LocalChange = false; 1285 1286 // Delete functions that are trivially dead, ccc -> fastcc 1287 LocalChange |= OptimizeFunctions(M); 1288 1289 // Optimize global_ctors list. 1290 if (GlobalCtors) 1291 LocalChange |= OptimizeGlobalCtorsList(GlobalCtors); 1292 1293 // Optimize non-address-taken globals. 1294 LocalChange |= OptimizeGlobalVars(M); 1295 Changed |= LocalChange; 1296 } 1297 1298 // TODO: Move all global ctors functions to the end of the module for code 1299 // layout. 1300 1301 return Changed; 1302} 1303