1 //===- GlobalOpt.cpp - Optimize Global Variables --------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass transforms simple global variables that never have their address 10 // taken. If obviously true, it marks read/write globals as constant, deletes 11 // variables only stored to, etc. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/IPO/GlobalOpt.h" 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/Statistic.h" 21 #include "llvm/ADT/Twine.h" 22 #include "llvm/ADT/iterator_range.h" 23 #include "llvm/Analysis/BlockFrequencyInfo.h" 24 #include "llvm/Analysis/ConstantFolding.h" 25 #include "llvm/Analysis/MemoryBuiltins.h" 26 #include "llvm/Analysis/TargetLibraryInfo.h" 27 #include "llvm/Analysis/TargetTransformInfo.h" 28 #include "llvm/BinaryFormat/Dwarf.h" 29 #include "llvm/IR/Attributes.h" 30 #include "llvm/IR/BasicBlock.h" 31 #include "llvm/IR/CallingConv.h" 32 #include "llvm/IR/Constant.h" 33 #include "llvm/IR/Constants.h" 34 #include "llvm/IR/DataLayout.h" 35 #include "llvm/IR/DebugInfoMetadata.h" 36 #include "llvm/IR/DerivedTypes.h" 37 #include "llvm/IR/Dominators.h" 38 #include "llvm/IR/Function.h" 39 #include "llvm/IR/GetElementPtrTypeIterator.h" 40 #include "llvm/IR/GlobalAlias.h" 41 #include "llvm/IR/GlobalValue.h" 42 #include "llvm/IR/GlobalVariable.h" 43 #include "llvm/IR/IRBuilder.h" 44 #include "llvm/IR/InstrTypes.h" 45 #include "llvm/IR/Instruction.h" 46 #include "llvm/IR/Instructions.h" 47 #include "llvm/IR/IntrinsicInst.h" 48 #include "llvm/IR/Module.h" 49 #include "llvm/IR/Operator.h" 50 #include "llvm/IR/Type.h" 51 #include "llvm/IR/Use.h" 52 #include "llvm/IR/User.h" 53 #include "llvm/IR/Value.h" 54 #include "llvm/IR/ValueHandle.h" 55 #include "llvm/InitializePasses.h" 56 #include "llvm/Pass.h" 57 #include "llvm/Support/AtomicOrdering.h" 58 #include "llvm/Support/Casting.h" 59 #include "llvm/Support/CommandLine.h" 60 #include "llvm/Support/Debug.h" 61 #include "llvm/Support/ErrorHandling.h" 62 #include "llvm/Support/MathExtras.h" 63 #include "llvm/Support/raw_ostream.h" 64 #include "llvm/Transforms/IPO.h" 65 #include "llvm/Transforms/Utils/CtorUtils.h" 66 #include "llvm/Transforms/Utils/Evaluator.h" 67 #include "llvm/Transforms/Utils/GlobalStatus.h" 68 #include "llvm/Transforms/Utils/Local.h" 69 #include <cassert> 70 #include <cstdint> 71 #include <utility> 72 #include <vector> 73 74 using namespace llvm; 75 76 #define DEBUG_TYPE "globalopt" 77 78 STATISTIC(NumMarked , "Number of globals marked constant"); 79 STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr"); 80 STATISTIC(NumSRA , "Number of aggregate globals broken into scalars"); 81 STATISTIC(NumHeapSRA , "Number of heap objects SRA'd"); 82 STATISTIC(NumSubstitute,"Number of globals with initializers stored into them"); 83 STATISTIC(NumDeleted , "Number of globals deleted"); 84 STATISTIC(NumGlobUses , "Number of global uses devirtualized"); 85 STATISTIC(NumLocalized , "Number of globals localized"); 86 STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans"); 87 STATISTIC(NumFastCallFns , "Number of functions converted to fastcc"); 88 STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated"); 89 STATISTIC(NumNestRemoved , "Number of nest attributes removed"); 90 STATISTIC(NumAliasesResolved, "Number of global aliases resolved"); 91 STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated"); 92 STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed"); 93 STATISTIC(NumInternalFunc, "Number of internal functions"); 94 STATISTIC(NumColdCC, "Number of functions marked coldcc"); 95 96 static cl::opt<bool> 97 EnableColdCCStressTest("enable-coldcc-stress-test", 98 cl::desc("Enable stress test of coldcc by adding " 99 "calling conv to all internal functions."), 100 cl::init(false), cl::Hidden); 101 102 static cl::opt<int> ColdCCRelFreq( 103 "coldcc-rel-freq", cl::Hidden, cl::init(2), cl::ZeroOrMore, 104 cl::desc( 105 "Maximum block frequency, expressed as a percentage of caller's " 106 "entry frequency, for a call site to be considered cold for enabling" 107 "coldcc")); 108 109 /// Is this global variable possibly used by a leak checker as a root? If so, 110 /// we might not really want to eliminate the stores to it. 111 static bool isLeakCheckerRoot(GlobalVariable *GV) { 112 // A global variable is a root if it is a pointer, or could plausibly contain 113 // a pointer. There are two challenges; one is that we could have a struct 114 // the has an inner member which is a pointer. We recurse through the type to 115 // detect these (up to a point). The other is that we may actually be a union 116 // of a pointer and another type, and so our LLVM type is an integer which 117 // gets converted into a pointer, or our type is an [i8 x #] with a pointer 118 // potentially contained here. 119 120 if (GV->hasPrivateLinkage()) 121 return false; 122 123 SmallVector<Type *, 4> Types; 124 Types.push_back(GV->getValueType()); 125 126 unsigned Limit = 20; 127 do { 128 Type *Ty = Types.pop_back_val(); 129 switch (Ty->getTypeID()) { 130 default: break; 131 case Type::PointerTyID: 132 return true; 133 case Type::FixedVectorTyID: 134 case Type::ScalableVectorTyID: 135 if (cast<VectorType>(Ty)->getElementType()->isPointerTy()) 136 return true; 137 break; 138 case Type::ArrayTyID: 139 Types.push_back(cast<ArrayType>(Ty)->getElementType()); 140 break; 141 case Type::StructTyID: { 142 StructType *STy = cast<StructType>(Ty); 143 if (STy->isOpaque()) return true; 144 for (StructType::element_iterator I = STy->element_begin(), 145 E = STy->element_end(); I != E; ++I) { 146 Type *InnerTy = *I; 147 if (isa<PointerType>(InnerTy)) return true; 148 if (isa<StructType>(InnerTy) || isa<ArrayType>(InnerTy) || 149 isa<VectorType>(InnerTy)) 150 Types.push_back(InnerTy); 151 } 152 break; 153 } 154 } 155 if (--Limit == 0) return true; 156 } while (!Types.empty()); 157 return false; 158 } 159 160 /// Given a value that is stored to a global but never read, determine whether 161 /// it's safe to remove the store and the chain of computation that feeds the 162 /// store. 163 static bool IsSafeComputationToRemove( 164 Value *V, function_ref<TargetLibraryInfo &(Function &)> GetTLI) { 165 do { 166 if (isa<Constant>(V)) 167 return true; 168 if (!V->hasOneUse()) 169 return false; 170 if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) || 171 isa<GlobalValue>(V)) 172 return false; 173 if (isAllocationFn(V, GetTLI)) 174 return true; 175 176 Instruction *I = cast<Instruction>(V); 177 if (I->mayHaveSideEffects()) 178 return false; 179 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { 180 if (!GEP->hasAllConstantIndices()) 181 return false; 182 } else if (I->getNumOperands() != 1) { 183 return false; 184 } 185 186 V = I->getOperand(0); 187 } while (true); 188 } 189 190 /// This GV is a pointer root. Loop over all users of the global and clean up 191 /// any that obviously don't assign the global a value that isn't dynamically 192 /// allocated. 193 static bool 194 CleanupPointerRootUsers(GlobalVariable *GV, 195 function_ref<TargetLibraryInfo &(Function &)> GetTLI) { 196 // A brief explanation of leak checkers. The goal is to find bugs where 197 // pointers are forgotten, causing an accumulating growth in memory 198 // usage over time. The common strategy for leak checkers is to explicitly 199 // allow the memory pointed to by globals at exit. This is popular because it 200 // also solves another problem where the main thread of a C++ program may shut 201 // down before other threads that are still expecting to use those globals. To 202 // handle that case, we expect the program may create a singleton and never 203 // destroy it. 204 205 bool Changed = false; 206 207 // If Dead[n].first is the only use of a malloc result, we can delete its 208 // chain of computation and the store to the global in Dead[n].second. 209 SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead; 210 211 // Constants can't be pointers to dynamically allocated memory. 212 for (Value::user_iterator UI = GV->user_begin(), E = GV->user_end(); 213 UI != E;) { 214 User *U = *UI++; 215 if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 216 Value *V = SI->getValueOperand(); 217 if (isa<Constant>(V)) { 218 Changed = true; 219 SI->eraseFromParent(); 220 } else if (Instruction *I = dyn_cast<Instruction>(V)) { 221 if (I->hasOneUse()) 222 Dead.push_back(std::make_pair(I, SI)); 223 } 224 } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) { 225 if (isa<Constant>(MSI->getValue())) { 226 Changed = true; 227 MSI->eraseFromParent(); 228 } else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) { 229 if (I->hasOneUse()) 230 Dead.push_back(std::make_pair(I, MSI)); 231 } 232 } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) { 233 GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource()); 234 if (MemSrc && MemSrc->isConstant()) { 235 Changed = true; 236 MTI->eraseFromParent(); 237 } else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) { 238 if (I->hasOneUse()) 239 Dead.push_back(std::make_pair(I, MTI)); 240 } 241 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { 242 if (CE->use_empty()) { 243 CE->destroyConstant(); 244 Changed = true; 245 } 246 } else if (Constant *C = dyn_cast<Constant>(U)) { 247 if (isSafeToDestroyConstant(C)) { 248 C->destroyConstant(); 249 // This could have invalidated UI, start over from scratch. 250 Dead.clear(); 251 CleanupPointerRootUsers(GV, GetTLI); 252 return true; 253 } 254 } 255 } 256 257 for (int i = 0, e = Dead.size(); i != e; ++i) { 258 if (IsSafeComputationToRemove(Dead[i].first, GetTLI)) { 259 Dead[i].second->eraseFromParent(); 260 Instruction *I = Dead[i].first; 261 do { 262 if (isAllocationFn(I, GetTLI)) 263 break; 264 Instruction *J = dyn_cast<Instruction>(I->getOperand(0)); 265 if (!J) 266 break; 267 I->eraseFromParent(); 268 I = J; 269 } while (true); 270 I->eraseFromParent(); 271 Changed = true; 272 } 273 } 274 275 return Changed; 276 } 277 278 /// We just marked GV constant. Loop over all users of the global, cleaning up 279 /// the obvious ones. This is largely just a quick scan over the use list to 280 /// clean up the easy and obvious cruft. This returns true if it made a change. 281 static bool CleanupConstantGlobalUsers( 282 Value *V, Constant *Init, const DataLayout &DL, 283 function_ref<TargetLibraryInfo &(Function &)> GetTLI) { 284 bool Changed = false; 285 // Note that we need to use a weak value handle for the worklist items. When 286 // we delete a constant array, we may also be holding pointer to one of its 287 // elements (or an element of one of its elements if we're dealing with an 288 // array of arrays) in the worklist. 289 SmallVector<WeakTrackingVH, 8> WorkList(V->users()); 290 while (!WorkList.empty()) { 291 Value *UV = WorkList.pop_back_val(); 292 if (!UV) 293 continue; 294 295 User *U = cast<User>(UV); 296 297 if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 298 if (Init) { 299 if (auto *Casted = 300 ConstantFoldLoadThroughBitcast(Init, LI->getType(), DL)) { 301 // Replace the load with the initializer. 302 LI->replaceAllUsesWith(Casted); 303 LI->eraseFromParent(); 304 Changed = true; 305 } 306 } 307 } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 308 // Store must be unreachable or storing Init into the global. 309 SI->eraseFromParent(); 310 Changed = true; 311 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { 312 if (CE->getOpcode() == Instruction::GetElementPtr) { 313 Constant *SubInit = nullptr; 314 if (Init) 315 SubInit = ConstantFoldLoadThroughGEPConstantExpr( 316 Init, CE, V->getType()->getPointerElementType(), DL); 317 Changed |= CleanupConstantGlobalUsers(CE, SubInit, DL, GetTLI); 318 } else if ((CE->getOpcode() == Instruction::BitCast && 319 CE->getType()->isPointerTy()) || 320 CE->getOpcode() == Instruction::AddrSpaceCast) { 321 // Pointer cast, delete any stores and memsets to the global. 322 Changed |= CleanupConstantGlobalUsers(CE, nullptr, DL, GetTLI); 323 } 324 325 if (CE->use_empty()) { 326 CE->destroyConstant(); 327 Changed = true; 328 } 329 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { 330 // Do not transform "gepinst (gep constexpr (GV))" here, because forming 331 // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold 332 // and will invalidate our notion of what Init is. 333 Constant *SubInit = nullptr; 334 if (!isa<ConstantExpr>(GEP->getOperand(0))) { 335 ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>( 336 ConstantFoldInstruction(GEP, DL, &GetTLI(*GEP->getFunction()))); 337 if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr) 338 SubInit = ConstantFoldLoadThroughGEPConstantExpr( 339 Init, CE, V->getType()->getPointerElementType(), DL); 340 341 // If the initializer is an all-null value and we have an inbounds GEP, 342 // we already know what the result of any load from that GEP is. 343 // TODO: Handle splats. 344 if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds()) 345 SubInit = Constant::getNullValue(GEP->getResultElementType()); 346 } 347 Changed |= CleanupConstantGlobalUsers(GEP, SubInit, DL, GetTLI); 348 349 if (GEP->use_empty()) { 350 GEP->eraseFromParent(); 351 Changed = true; 352 } 353 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv 354 if (MI->getRawDest() == V) { 355 MI->eraseFromParent(); 356 Changed = true; 357 } 358 359 } else if (Constant *C = dyn_cast<Constant>(U)) { 360 // If we have a chain of dead constantexprs or other things dangling from 361 // us, and if they are all dead, nuke them without remorse. 362 if (isSafeToDestroyConstant(C)) { 363 C->destroyConstant(); 364 CleanupConstantGlobalUsers(V, Init, DL, GetTLI); 365 return true; 366 } 367 } 368 } 369 return Changed; 370 } 371 372 static bool isSafeSROAElementUse(Value *V); 373 374 /// Return true if the specified GEP is a safe user of a derived 375 /// expression from a global that we want to SROA. 376 static bool isSafeSROAGEP(User *U) { 377 // Check to see if this ConstantExpr GEP is SRA'able. In particular, we 378 // don't like < 3 operand CE's, and we don't like non-constant integer 379 // indices. This enforces that all uses are 'gep GV, 0, C, ...' for some 380 // value of C. 381 if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) || 382 !cast<Constant>(U->getOperand(1))->isNullValue()) 383 return false; 384 385 gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U); 386 ++GEPI; // Skip over the pointer index. 387 388 // For all other level we require that the indices are constant and inrange. 389 // In particular, consider: A[0][i]. We cannot know that the user isn't doing 390 // invalid things like allowing i to index an out-of-range subscript that 391 // accesses A[1]. This can also happen between different members of a struct 392 // in llvm IR. 393 for (; GEPI != E; ++GEPI) { 394 if (GEPI.isStruct()) 395 continue; 396 397 ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand()); 398 if (!IdxVal || (GEPI.isBoundedSequential() && 399 IdxVal->getZExtValue() >= GEPI.getSequentialNumElements())) 400 return false; 401 } 402 403 return llvm::all_of(U->users(), 404 [](User *UU) { return isSafeSROAElementUse(UU); }); 405 } 406 407 /// Return true if the specified instruction is a safe user of a derived 408 /// expression from a global that we want to SROA. 409 static bool isSafeSROAElementUse(Value *V) { 410 // We might have a dead and dangling constant hanging off of here. 411 if (Constant *C = dyn_cast<Constant>(V)) 412 return isSafeToDestroyConstant(C); 413 414 Instruction *I = dyn_cast<Instruction>(V); 415 if (!I) return false; 416 417 // Loads are ok. 418 if (isa<LoadInst>(I)) return true; 419 420 // Stores *to* the pointer are ok. 421 if (StoreInst *SI = dyn_cast<StoreInst>(I)) 422 return SI->getOperand(0) != V; 423 424 // Otherwise, it must be a GEP. Check it and its users are safe to SRA. 425 return isa<GetElementPtrInst>(I) && isSafeSROAGEP(I); 426 } 427 428 /// Look at all uses of the global and decide whether it is safe for us to 429 /// perform this transformation. 430 static bool GlobalUsersSafeToSRA(GlobalValue *GV) { 431 for (User *U : GV->users()) { 432 // The user of the global must be a GEP Inst or a ConstantExpr GEP. 433 if (!isa<GetElementPtrInst>(U) && 434 (!isa<ConstantExpr>(U) || 435 cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr)) 436 return false; 437 438 // Check the gep and it's users are safe to SRA 439 if (!isSafeSROAGEP(U)) 440 return false; 441 } 442 443 return true; 444 } 445 446 static bool IsSRASequential(Type *T) { 447 return isa<ArrayType>(T) || isa<VectorType>(T); 448 } 449 static uint64_t GetSRASequentialNumElements(Type *T) { 450 if (ArrayType *AT = dyn_cast<ArrayType>(T)) 451 return AT->getNumElements(); 452 return cast<FixedVectorType>(T)->getNumElements(); 453 } 454 static Type *GetSRASequentialElementType(Type *T) { 455 if (ArrayType *AT = dyn_cast<ArrayType>(T)) 456 return AT->getElementType(); 457 return cast<VectorType>(T)->getElementType(); 458 } 459 static bool CanDoGlobalSRA(GlobalVariable *GV) { 460 Constant *Init = GV->getInitializer(); 461 462 if (isa<StructType>(Init->getType())) { 463 // nothing to check 464 } else if (IsSRASequential(Init->getType())) { 465 if (GetSRASequentialNumElements(Init->getType()) > 16 && 466 GV->hasNUsesOrMore(16)) 467 return false; // It's not worth it. 468 } else 469 return false; 470 471 return GlobalUsersSafeToSRA(GV); 472 } 473 474 /// Copy over the debug info for a variable to its SRA replacements. 475 static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV, 476 uint64_t FragmentOffsetInBits, 477 uint64_t FragmentSizeInBits, 478 uint64_t VarSize) { 479 SmallVector<DIGlobalVariableExpression *, 1> GVs; 480 GV->getDebugInfo(GVs); 481 for (auto *GVE : GVs) { 482 DIVariable *Var = GVE->getVariable(); 483 DIExpression *Expr = GVE->getExpression(); 484 // If the FragmentSize is smaller than the variable, 485 // emit a fragment expression. 486 if (FragmentSizeInBits < VarSize) { 487 if (auto E = DIExpression::createFragmentExpression( 488 Expr, FragmentOffsetInBits, FragmentSizeInBits)) 489 Expr = *E; 490 else 491 return; 492 } 493 auto *NGVE = DIGlobalVariableExpression::get(GVE->getContext(), Var, Expr); 494 NGV->addDebugInfo(NGVE); 495 } 496 } 497 498 /// Perform scalar replacement of aggregates on the specified global variable. 499 /// This opens the door for other optimizations by exposing the behavior of the 500 /// program in a more fine-grained way. We have determined that this 501 /// transformation is safe already. We return the first global variable we 502 /// insert so that the caller can reprocess it. 503 static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) { 504 // Make sure this global only has simple uses that we can SRA. 505 if (!CanDoGlobalSRA(GV)) 506 return nullptr; 507 508 assert(GV->hasLocalLinkage()); 509 Constant *Init = GV->getInitializer(); 510 Type *Ty = Init->getType(); 511 uint64_t VarSize = DL.getTypeSizeInBits(Ty); 512 513 std::map<unsigned, GlobalVariable *> NewGlobals; 514 515 // Get the alignment of the global, either explicit or target-specific. 516 Align StartAlignment = 517 DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getType()); 518 519 // Loop over all users and create replacement variables for used aggregate 520 // elements. 521 for (User *GEP : GV->users()) { 522 assert(((isa<ConstantExpr>(GEP) && cast<ConstantExpr>(GEP)->getOpcode() == 523 Instruction::GetElementPtr) || 524 isa<GetElementPtrInst>(GEP)) && 525 "NonGEP CE's are not SRAable!"); 526 527 // Ignore the 1th operand, which has to be zero or else the program is quite 528 // broken (undefined). Get the 2nd operand, which is the structure or array 529 // index. 530 unsigned ElementIdx = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue(); 531 if (NewGlobals.count(ElementIdx) == 1) 532 continue; // we`ve already created replacement variable 533 assert(NewGlobals.count(ElementIdx) == 0); 534 535 Type *ElTy = nullptr; 536 if (StructType *STy = dyn_cast<StructType>(Ty)) 537 ElTy = STy->getElementType(ElementIdx); 538 else 539 ElTy = GetSRASequentialElementType(Ty); 540 assert(ElTy); 541 542 Constant *In = Init->getAggregateElement(ElementIdx); 543 assert(In && "Couldn't get element of initializer?"); 544 545 GlobalVariable *NGV = new GlobalVariable( 546 ElTy, false, GlobalVariable::InternalLinkage, In, 547 GV->getName() + "." + Twine(ElementIdx), GV->getThreadLocalMode(), 548 GV->getType()->getAddressSpace()); 549 NGV->setExternallyInitialized(GV->isExternallyInitialized()); 550 NGV->copyAttributesFrom(GV); 551 NewGlobals.insert(std::make_pair(ElementIdx, NGV)); 552 553 if (StructType *STy = dyn_cast<StructType>(Ty)) { 554 const StructLayout &Layout = *DL.getStructLayout(STy); 555 556 // Calculate the known alignment of the field. If the original aggregate 557 // had 256 byte alignment for example, something might depend on that: 558 // propagate info to each field. 559 uint64_t FieldOffset = Layout.getElementOffset(ElementIdx); 560 Align NewAlign = commonAlignment(StartAlignment, FieldOffset); 561 if (NewAlign > DL.getABITypeAlign(STy->getElementType(ElementIdx))) 562 NGV->setAlignment(NewAlign); 563 564 // Copy over the debug info for the variable. 565 uint64_t Size = DL.getTypeAllocSizeInBits(NGV->getValueType()); 566 uint64_t FragmentOffsetInBits = Layout.getElementOffsetInBits(ElementIdx); 567 transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size, VarSize); 568 } else { 569 uint64_t EltSize = DL.getTypeAllocSize(ElTy); 570 Align EltAlign = DL.getABITypeAlign(ElTy); 571 uint64_t FragmentSizeInBits = DL.getTypeAllocSizeInBits(ElTy); 572 573 // Calculate the known alignment of the field. If the original aggregate 574 // had 256 byte alignment for example, something might depend on that: 575 // propagate info to each field. 576 Align NewAlign = commonAlignment(StartAlignment, EltSize * ElementIdx); 577 if (NewAlign > EltAlign) 578 NGV->setAlignment(NewAlign); 579 transferSRADebugInfo(GV, NGV, FragmentSizeInBits * ElementIdx, 580 FragmentSizeInBits, VarSize); 581 } 582 } 583 584 if (NewGlobals.empty()) 585 return nullptr; 586 587 Module::GlobalListType &Globals = GV->getParent()->getGlobalList(); 588 for (auto NewGlobalVar : NewGlobals) 589 Globals.push_back(NewGlobalVar.second); 590 591 LLVM_DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV << "\n"); 592 593 Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext())); 594 595 // Loop over all of the uses of the global, replacing the constantexpr geps, 596 // with smaller constantexpr geps or direct references. 597 while (!GV->use_empty()) { 598 User *GEP = GV->user_back(); 599 assert(((isa<ConstantExpr>(GEP) && 600 cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)|| 601 isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!"); 602 603 // Ignore the 1th operand, which has to be zero or else the program is quite 604 // broken (undefined). Get the 2nd operand, which is the structure or array 605 // index. 606 unsigned ElementIdx = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue(); 607 assert(NewGlobals.count(ElementIdx) == 1); 608 609 Value *NewPtr = NewGlobals[ElementIdx]; 610 Type *NewTy = NewGlobals[ElementIdx]->getValueType(); 611 612 // Form a shorter GEP if needed. 613 if (GEP->getNumOperands() > 3) { 614 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) { 615 SmallVector<Constant*, 8> Idxs; 616 Idxs.push_back(NullInt); 617 for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i) 618 Idxs.push_back(CE->getOperand(i)); 619 NewPtr = 620 ConstantExpr::getGetElementPtr(NewTy, cast<Constant>(NewPtr), Idxs); 621 } else { 622 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP); 623 SmallVector<Value*, 8> Idxs; 624 Idxs.push_back(NullInt); 625 for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i) 626 Idxs.push_back(GEPI->getOperand(i)); 627 NewPtr = GetElementPtrInst::Create( 628 NewTy, NewPtr, Idxs, GEPI->getName() + "." + Twine(ElementIdx), 629 GEPI); 630 } 631 } 632 GEP->replaceAllUsesWith(NewPtr); 633 634 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP)) 635 GEPI->eraseFromParent(); 636 else 637 cast<ConstantExpr>(GEP)->destroyConstant(); 638 } 639 640 // Delete the old global, now that it is dead. 641 Globals.erase(GV); 642 ++NumSRA; 643 644 assert(NewGlobals.size() > 0); 645 return NewGlobals.begin()->second; 646 } 647 648 /// Return true if all users of the specified value will trap if the value is 649 /// dynamically null. PHIs keeps track of any phi nodes we've seen to avoid 650 /// reprocessing them. 651 static bool AllUsesOfValueWillTrapIfNull(const Value *V, 652 SmallPtrSetImpl<const PHINode*> &PHIs) { 653 for (const User *U : V->users()) { 654 if (const Instruction *I = dyn_cast<Instruction>(U)) { 655 // If null pointer is considered valid, then all uses are non-trapping. 656 // Non address-space 0 globals have already been pruned by the caller. 657 if (NullPointerIsDefined(I->getFunction())) 658 return false; 659 } 660 if (isa<LoadInst>(U)) { 661 // Will trap. 662 } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) { 663 if (SI->getOperand(0) == V) { 664 //cerr << "NONTRAPPING USE: " << *U; 665 return false; // Storing the value. 666 } 667 } else if (const CallInst *CI = dyn_cast<CallInst>(U)) { 668 if (CI->getCalledOperand() != V) { 669 //cerr << "NONTRAPPING USE: " << *U; 670 return false; // Not calling the ptr 671 } 672 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) { 673 if (II->getCalledOperand() != V) { 674 //cerr << "NONTRAPPING USE: " << *U; 675 return false; // Not calling the ptr 676 } 677 } else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) { 678 if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false; 679 } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) { 680 if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false; 681 } else if (const PHINode *PN = dyn_cast<PHINode>(U)) { 682 // If we've already seen this phi node, ignore it, it has already been 683 // checked. 684 if (PHIs.insert(PN).second && !AllUsesOfValueWillTrapIfNull(PN, PHIs)) 685 return false; 686 } else { 687 //cerr << "NONTRAPPING USE: " << *U; 688 return false; 689 } 690 } 691 return true; 692 } 693 694 /// Return true if all uses of any loads from GV will trap if the loaded value 695 /// is null. Note that this also permits comparisons of the loaded value 696 /// against null, as a special case. 697 static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) { 698 for (const User *U : GV->users()) 699 if (const LoadInst *LI = dyn_cast<LoadInst>(U)) { 700 SmallPtrSet<const PHINode*, 8> PHIs; 701 if (!AllUsesOfValueWillTrapIfNull(LI, PHIs)) 702 return false; 703 } else if (isa<StoreInst>(U)) { 704 // Ignore stores to the global. 705 } else { 706 // We don't know or understand this user, bail out. 707 //cerr << "UNKNOWN USER OF GLOBAL!: " << *U; 708 return false; 709 } 710 return true; 711 } 712 713 static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) { 714 bool Changed = false; 715 for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) { 716 Instruction *I = cast<Instruction>(*UI++); 717 // Uses are non-trapping if null pointer is considered valid. 718 // Non address-space 0 globals are already pruned by the caller. 719 if (NullPointerIsDefined(I->getFunction())) 720 return false; 721 if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 722 LI->setOperand(0, NewV); 723 Changed = true; 724 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 725 if (SI->getOperand(1) == V) { 726 SI->setOperand(1, NewV); 727 Changed = true; 728 } 729 } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) { 730 CallBase *CB = cast<CallBase>(I); 731 if (CB->getCalledOperand() == V) { 732 // Calling through the pointer! Turn into a direct call, but be careful 733 // that the pointer is not also being passed as an argument. 734 CB->setCalledOperand(NewV); 735 Changed = true; 736 bool PassedAsArg = false; 737 for (unsigned i = 0, e = CB->arg_size(); i != e; ++i) 738 if (CB->getArgOperand(i) == V) { 739 PassedAsArg = true; 740 CB->setArgOperand(i, NewV); 741 } 742 743 if (PassedAsArg) { 744 // Being passed as an argument also. Be careful to not invalidate UI! 745 UI = V->user_begin(); 746 } 747 } 748 } else if (CastInst *CI = dyn_cast<CastInst>(I)) { 749 Changed |= OptimizeAwayTrappingUsesOfValue(CI, 750 ConstantExpr::getCast(CI->getOpcode(), 751 NewV, CI->getType())); 752 if (CI->use_empty()) { 753 Changed = true; 754 CI->eraseFromParent(); 755 } 756 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { 757 // Should handle GEP here. 758 SmallVector<Constant*, 8> Idxs; 759 Idxs.reserve(GEPI->getNumOperands()-1); 760 for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end(); 761 i != e; ++i) 762 if (Constant *C = dyn_cast<Constant>(*i)) 763 Idxs.push_back(C); 764 else 765 break; 766 if (Idxs.size() == GEPI->getNumOperands()-1) 767 Changed |= OptimizeAwayTrappingUsesOfValue( 768 GEPI, ConstantExpr::getGetElementPtr(GEPI->getSourceElementType(), 769 NewV, Idxs)); 770 if (GEPI->use_empty()) { 771 Changed = true; 772 GEPI->eraseFromParent(); 773 } 774 } 775 } 776 777 return Changed; 778 } 779 780 /// The specified global has only one non-null value stored into it. If there 781 /// are uses of the loaded value that would trap if the loaded value is 782 /// dynamically null, then we know that they cannot be reachable with a null 783 /// optimize away the load. 784 static bool OptimizeAwayTrappingUsesOfLoads( 785 GlobalVariable *GV, Constant *LV, const DataLayout &DL, 786 function_ref<TargetLibraryInfo &(Function &)> GetTLI) { 787 bool Changed = false; 788 789 // Keep track of whether we are able to remove all the uses of the global 790 // other than the store that defines it. 791 bool AllNonStoreUsesGone = true; 792 793 // Replace all uses of loads with uses of uses of the stored value. 794 for (Value::user_iterator GUI = GV->user_begin(), E = GV->user_end(); GUI != E;){ 795 User *GlobalUser = *GUI++; 796 if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) { 797 Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV); 798 // If we were able to delete all uses of the loads 799 if (LI->use_empty()) { 800 LI->eraseFromParent(); 801 Changed = true; 802 } else { 803 AllNonStoreUsesGone = false; 804 } 805 } else if (isa<StoreInst>(GlobalUser)) { 806 // Ignore the store that stores "LV" to the global. 807 assert(GlobalUser->getOperand(1) == GV && 808 "Must be storing *to* the global"); 809 } else { 810 AllNonStoreUsesGone = false; 811 812 // If we get here we could have other crazy uses that are transitively 813 // loaded. 814 assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) || 815 isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) || 816 isa<BitCastInst>(GlobalUser) || 817 isa<GetElementPtrInst>(GlobalUser)) && 818 "Only expect load and stores!"); 819 } 820 } 821 822 if (Changed) { 823 LLVM_DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV 824 << "\n"); 825 ++NumGlobUses; 826 } 827 828 // If we nuked all of the loads, then none of the stores are needed either, 829 // nor is the global. 830 if (AllNonStoreUsesGone) { 831 if (isLeakCheckerRoot(GV)) { 832 Changed |= CleanupPointerRootUsers(GV, GetTLI); 833 } else { 834 Changed = true; 835 CleanupConstantGlobalUsers(GV, nullptr, DL, GetTLI); 836 } 837 if (GV->use_empty()) { 838 LLVM_DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n"); 839 Changed = true; 840 GV->eraseFromParent(); 841 ++NumDeleted; 842 } 843 } 844 return Changed; 845 } 846 847 /// Walk the use list of V, constant folding all of the instructions that are 848 /// foldable. 849 static void ConstantPropUsersOf(Value *V, const DataLayout &DL, 850 TargetLibraryInfo *TLI) { 851 for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; ) 852 if (Instruction *I = dyn_cast<Instruction>(*UI++)) 853 if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) { 854 I->replaceAllUsesWith(NewC); 855 856 // Advance UI to the next non-I use to avoid invalidating it! 857 // Instructions could multiply use V. 858 while (UI != E && *UI == I) 859 ++UI; 860 if (isInstructionTriviallyDead(I, TLI)) 861 I->eraseFromParent(); 862 } 863 } 864 865 /// This function takes the specified global variable, and transforms the 866 /// program as if it always contained the result of the specified malloc. 867 /// Because it is always the result of the specified malloc, there is no reason 868 /// to actually DO the malloc. Instead, turn the malloc into a global, and any 869 /// loads of GV as uses of the new global. 870 static GlobalVariable * 871 OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, CallInst *CI, Type *AllocTy, 872 ConstantInt *NElements, const DataLayout &DL, 873 TargetLibraryInfo *TLI) { 874 LLVM_DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI 875 << '\n'); 876 877 Type *GlobalType; 878 if (NElements->getZExtValue() == 1) 879 GlobalType = AllocTy; 880 else 881 // If we have an array allocation, the global variable is of an array. 882 GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue()); 883 884 // Create the new global variable. The contents of the malloc'd memory is 885 // undefined, so initialize with an undef value. 886 GlobalVariable *NewGV = new GlobalVariable( 887 *GV->getParent(), GlobalType, false, GlobalValue::InternalLinkage, 888 UndefValue::get(GlobalType), GV->getName() + ".body", nullptr, 889 GV->getThreadLocalMode()); 890 891 // If there are bitcast users of the malloc (which is typical, usually we have 892 // a malloc + bitcast) then replace them with uses of the new global. Update 893 // other users to use the global as well. 894 BitCastInst *TheBC = nullptr; 895 while (!CI->use_empty()) { 896 Instruction *User = cast<Instruction>(CI->user_back()); 897 if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) { 898 if (BCI->getType() == NewGV->getType()) { 899 BCI->replaceAllUsesWith(NewGV); 900 BCI->eraseFromParent(); 901 } else { 902 BCI->setOperand(0, NewGV); 903 } 904 } else { 905 if (!TheBC) 906 TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI); 907 User->replaceUsesOfWith(CI, TheBC); 908 } 909 } 910 911 Constant *RepValue = NewGV; 912 if (NewGV->getType() != GV->getValueType()) 913 RepValue = ConstantExpr::getBitCast(RepValue, GV->getValueType()); 914 915 // If there is a comparison against null, we will insert a global bool to 916 // keep track of whether the global was initialized yet or not. 917 GlobalVariable *InitBool = 918 new GlobalVariable(Type::getInt1Ty(GV->getContext()), false, 919 GlobalValue::InternalLinkage, 920 ConstantInt::getFalse(GV->getContext()), 921 GV->getName()+".init", GV->getThreadLocalMode()); 922 bool InitBoolUsed = false; 923 924 // Loop over all uses of GV, processing them in turn. 925 while (!GV->use_empty()) { 926 if (StoreInst *SI = dyn_cast<StoreInst>(GV->user_back())) { 927 // The global is initialized when the store to it occurs. 928 new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 929 Align(1), SI->getOrdering(), SI->getSyncScopeID(), SI); 930 SI->eraseFromParent(); 931 continue; 932 } 933 934 LoadInst *LI = cast<LoadInst>(GV->user_back()); 935 while (!LI->use_empty()) { 936 Use &LoadUse = *LI->use_begin(); 937 ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser()); 938 if (!ICI) { 939 LoadUse = RepValue; 940 continue; 941 } 942 943 // Replace the cmp X, 0 with a use of the bool value. 944 // Sink the load to where the compare was, if atomic rules allow us to. 945 Value *LV = new LoadInst(InitBool->getValueType(), InitBool, 946 InitBool->getName() + ".val", false, Align(1), 947 LI->getOrdering(), LI->getSyncScopeID(), 948 LI->isUnordered() ? (Instruction *)ICI : LI); 949 InitBoolUsed = true; 950 switch (ICI->getPredicate()) { 951 default: llvm_unreachable("Unknown ICmp Predicate!"); 952 case ICmpInst::ICMP_ULT: 953 case ICmpInst::ICMP_SLT: // X < null -> always false 954 LV = ConstantInt::getFalse(GV->getContext()); 955 break; 956 case ICmpInst::ICMP_ULE: 957 case ICmpInst::ICMP_SLE: 958 case ICmpInst::ICMP_EQ: 959 LV = BinaryOperator::CreateNot(LV, "notinit", ICI); 960 break; 961 case ICmpInst::ICMP_NE: 962 case ICmpInst::ICMP_UGE: 963 case ICmpInst::ICMP_SGE: 964 case ICmpInst::ICMP_UGT: 965 case ICmpInst::ICMP_SGT: 966 break; // no change. 967 } 968 ICI->replaceAllUsesWith(LV); 969 ICI->eraseFromParent(); 970 } 971 LI->eraseFromParent(); 972 } 973 974 // If the initialization boolean was used, insert it, otherwise delete it. 975 if (!InitBoolUsed) { 976 while (!InitBool->use_empty()) // Delete initializations 977 cast<StoreInst>(InitBool->user_back())->eraseFromParent(); 978 delete InitBool; 979 } else 980 GV->getParent()->getGlobalList().insert(GV->getIterator(), InitBool); 981 982 // Now the GV is dead, nuke it and the malloc.. 983 GV->eraseFromParent(); 984 CI->eraseFromParent(); 985 986 // To further other optimizations, loop over all users of NewGV and try to 987 // constant prop them. This will promote GEP instructions with constant 988 // indices into GEP constant-exprs, which will allow global-opt to hack on it. 989 ConstantPropUsersOf(NewGV, DL, TLI); 990 if (RepValue != NewGV) 991 ConstantPropUsersOf(RepValue, DL, TLI); 992 993 return NewGV; 994 } 995 996 /// Scan the use-list of V checking to make sure that there are no complex uses 997 /// of V. We permit simple things like dereferencing the pointer, but not 998 /// storing through the address, unless it is to the specified global. 999 static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V, 1000 const GlobalVariable *GV, 1001 SmallPtrSetImpl<const PHINode*> &PHIs) { 1002 for (const User *U : V->users()) { 1003 const Instruction *Inst = cast<Instruction>(U); 1004 1005 if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) { 1006 continue; // Fine, ignore. 1007 } 1008 1009 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 1010 if (SI->getOperand(0) == V && SI->getOperand(1) != GV) 1011 return false; // Storing the pointer itself... bad. 1012 continue; // Otherwise, storing through it, or storing into GV... fine. 1013 } 1014 1015 // Must index into the array and into the struct. 1016 if (isa<GetElementPtrInst>(Inst) && Inst->getNumOperands() >= 3) { 1017 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs)) 1018 return false; 1019 continue; 1020 } 1021 1022 if (const PHINode *PN = dyn_cast<PHINode>(Inst)) { 1023 // PHIs are ok if all uses are ok. Don't infinitely recurse through PHI 1024 // cycles. 1025 if (PHIs.insert(PN).second) 1026 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs)) 1027 return false; 1028 continue; 1029 } 1030 1031 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) { 1032 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs)) 1033 return false; 1034 continue; 1035 } 1036 1037 return false; 1038 } 1039 return true; 1040 } 1041 1042 /// The Alloc pointer is stored into GV somewhere. Transform all uses of the 1043 /// allocation into loads from the global and uses of the resultant pointer. 1044 /// Further, delete the store into GV. This assumes that these value pass the 1045 /// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate. 1046 static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc, 1047 GlobalVariable *GV) { 1048 while (!Alloc->use_empty()) { 1049 Instruction *U = cast<Instruction>(*Alloc->user_begin()); 1050 Instruction *InsertPt = U; 1051 if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 1052 // If this is the store of the allocation into the global, remove it. 1053 if (SI->getOperand(1) == GV) { 1054 SI->eraseFromParent(); 1055 continue; 1056 } 1057 } else if (PHINode *PN = dyn_cast<PHINode>(U)) { 1058 // Insert the load in the corresponding predecessor, not right before the 1059 // PHI. 1060 InsertPt = PN->getIncomingBlock(*Alloc->use_begin())->getTerminator(); 1061 } else if (isa<BitCastInst>(U)) { 1062 // Must be bitcast between the malloc and store to initialize the global. 1063 ReplaceUsesOfMallocWithGlobal(U, GV); 1064 U->eraseFromParent(); 1065 continue; 1066 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) { 1067 // If this is a "GEP bitcast" and the user is a store to the global, then 1068 // just process it as a bitcast. 1069 if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse()) 1070 if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->user_back())) 1071 if (SI->getOperand(1) == GV) { 1072 // Must be bitcast GEP between the malloc and store to initialize 1073 // the global. 1074 ReplaceUsesOfMallocWithGlobal(GEPI, GV); 1075 GEPI->eraseFromParent(); 1076 continue; 1077 } 1078 } 1079 1080 // Insert a load from the global, and use it instead of the malloc. 1081 Value *NL = 1082 new LoadInst(GV->getValueType(), GV, GV->getName() + ".val", InsertPt); 1083 U->replaceUsesOfWith(Alloc, NL); 1084 } 1085 } 1086 1087 /// Verify that all uses of V (a load, or a phi of a load) are simple enough to 1088 /// perform heap SRA on. This permits GEP's that index through the array and 1089 /// struct field, icmps of null, and PHIs. 1090 static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V, 1091 SmallPtrSetImpl<const PHINode*> &LoadUsingPHIs, 1092 SmallPtrSetImpl<const PHINode*> &LoadUsingPHIsPerLoad) { 1093 // We permit two users of the load: setcc comparing against the null 1094 // pointer, and a getelementptr of a specific form. 1095 for (const User *U : V->users()) { 1096 const Instruction *UI = cast<Instruction>(U); 1097 1098 // Comparison against null is ok. 1099 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UI)) { 1100 if (!isa<ConstantPointerNull>(ICI->getOperand(1))) 1101 return false; 1102 continue; 1103 } 1104 1105 // getelementptr is also ok, but only a simple form. 1106 if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(UI)) { 1107 // Must index into the array and into the struct. 1108 if (GEPI->getNumOperands() < 3) 1109 return false; 1110 1111 // Otherwise the GEP is ok. 1112 continue; 1113 } 1114 1115 if (const PHINode *PN = dyn_cast<PHINode>(UI)) { 1116 if (!LoadUsingPHIsPerLoad.insert(PN).second) 1117 // This means some phi nodes are dependent on each other. 1118 // Avoid infinite looping! 1119 return false; 1120 if (!LoadUsingPHIs.insert(PN).second) 1121 // If we have already analyzed this PHI, then it is safe. 1122 continue; 1123 1124 // Make sure all uses of the PHI are simple enough to transform. 1125 if (!LoadUsesSimpleEnoughForHeapSRA(PN, 1126 LoadUsingPHIs, LoadUsingPHIsPerLoad)) 1127 return false; 1128 1129 continue; 1130 } 1131 1132 // Otherwise we don't know what this is, not ok. 1133 return false; 1134 } 1135 1136 return true; 1137 } 1138 1139 /// If all users of values loaded from GV are simple enough to perform HeapSRA, 1140 /// return true. 1141 static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV, 1142 Instruction *StoredVal) { 1143 SmallPtrSet<const PHINode*, 32> LoadUsingPHIs; 1144 SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad; 1145 for (const User *U : GV->users()) 1146 if (const LoadInst *LI = dyn_cast<LoadInst>(U)) { 1147 if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs, 1148 LoadUsingPHIsPerLoad)) 1149 return false; 1150 LoadUsingPHIsPerLoad.clear(); 1151 } 1152 1153 // If we reach here, we know that all uses of the loads and transitive uses 1154 // (through PHI nodes) are simple enough to transform. However, we don't know 1155 // that all inputs the to the PHI nodes are in the same equivalence sets. 1156 // Check to verify that all operands of the PHIs are either PHIS that can be 1157 // transformed, loads from GV, or MI itself. 1158 for (const PHINode *PN : LoadUsingPHIs) { 1159 for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) { 1160 Value *InVal = PN->getIncomingValue(op); 1161 1162 // PHI of the stored value itself is ok. 1163 if (InVal == StoredVal) continue; 1164 1165 if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) { 1166 // One of the PHIs in our set is (optimistically) ok. 1167 if (LoadUsingPHIs.count(InPN)) 1168 continue; 1169 return false; 1170 } 1171 1172 // Load from GV is ok. 1173 if (const LoadInst *LI = dyn_cast<LoadInst>(InVal)) 1174 if (LI->getOperand(0) == GV) 1175 continue; 1176 1177 // UNDEF? NULL? 1178 1179 // Anything else is rejected. 1180 return false; 1181 } 1182 } 1183 1184 return true; 1185 } 1186 1187 static Value *GetHeapSROAValue(Value *V, unsigned FieldNo, 1188 DenseMap<Value *, std::vector<Value *>> &InsertedScalarizedValues, 1189 std::vector<std::pair<PHINode *, unsigned>> &PHIsToRewrite) { 1190 std::vector<Value *> &FieldVals = InsertedScalarizedValues[V]; 1191 1192 if (FieldNo >= FieldVals.size()) 1193 FieldVals.resize(FieldNo+1); 1194 1195 // If we already have this value, just reuse the previously scalarized 1196 // version. 1197 if (Value *FieldVal = FieldVals[FieldNo]) 1198 return FieldVal; 1199 1200 // Depending on what instruction this is, we have several cases. 1201 Value *Result; 1202 if (LoadInst *LI = dyn_cast<LoadInst>(V)) { 1203 // This is a scalarized version of the load from the global. Just create 1204 // a new Load of the scalarized global. 1205 Value *V = GetHeapSROAValue(LI->getOperand(0), FieldNo, 1206 InsertedScalarizedValues, PHIsToRewrite); 1207 Result = new LoadInst(V->getType()->getPointerElementType(), V, 1208 LI->getName() + ".f" + Twine(FieldNo), LI); 1209 } else { 1210 PHINode *PN = cast<PHINode>(V); 1211 // PN's type is pointer to struct. Make a new PHI of pointer to struct 1212 // field. 1213 1214 PointerType *PTy = cast<PointerType>(PN->getType()); 1215 StructType *ST = cast<StructType>(PTy->getElementType()); 1216 1217 unsigned AS = PTy->getAddressSpace(); 1218 PHINode *NewPN = 1219 PHINode::Create(PointerType::get(ST->getElementType(FieldNo), AS), 1220 PN->getNumIncomingValues(), 1221 PN->getName()+".f"+Twine(FieldNo), PN); 1222 Result = NewPN; 1223 PHIsToRewrite.push_back(std::make_pair(PN, FieldNo)); 1224 } 1225 1226 return FieldVals[FieldNo] = Result; 1227 } 1228 1229 /// Given a load instruction and a value derived from the load, rewrite the 1230 /// derived value to use the HeapSRoA'd load. 1231 static void RewriteHeapSROALoadUser(Instruction *LoadUser, 1232 DenseMap<Value *, std::vector<Value *>> &InsertedScalarizedValues, 1233 std::vector<std::pair<PHINode *, unsigned>> &PHIsToRewrite) { 1234 // If this is a comparison against null, handle it. 1235 if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) { 1236 assert(isa<ConstantPointerNull>(SCI->getOperand(1))); 1237 // If we have a setcc of the loaded pointer, we can use a setcc of any 1238 // field. 1239 Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0, 1240 InsertedScalarizedValues, PHIsToRewrite); 1241 1242 Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr, 1243 Constant::getNullValue(NPtr->getType()), 1244 SCI->getName()); 1245 SCI->replaceAllUsesWith(New); 1246 SCI->eraseFromParent(); 1247 return; 1248 } 1249 1250 // Handle 'getelementptr Ptr, Idx, i32 FieldNo ...' 1251 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) { 1252 assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2)) 1253 && "Unexpected GEPI!"); 1254 1255 // Load the pointer for this field. 1256 unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue(); 1257 Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo, 1258 InsertedScalarizedValues, PHIsToRewrite); 1259 1260 // Create the new GEP idx vector. 1261 SmallVector<Value*, 8> GEPIdx; 1262 GEPIdx.push_back(GEPI->getOperand(1)); 1263 GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end()); 1264 1265 Value *NGEPI = GetElementPtrInst::Create(GEPI->getResultElementType(), NewPtr, GEPIdx, 1266 GEPI->getName(), GEPI); 1267 GEPI->replaceAllUsesWith(NGEPI); 1268 GEPI->eraseFromParent(); 1269 return; 1270 } 1271 1272 // Recursively transform the users of PHI nodes. This will lazily create the 1273 // PHIs that are needed for individual elements. Keep track of what PHIs we 1274 // see in InsertedScalarizedValues so that we don't get infinite loops (very 1275 // antisocial). If the PHI is already in InsertedScalarizedValues, it has 1276 // already been seen first by another load, so its uses have already been 1277 // processed. 1278 PHINode *PN = cast<PHINode>(LoadUser); 1279 if (!InsertedScalarizedValues.insert(std::make_pair(PN, 1280 std::vector<Value *>())).second) 1281 return; 1282 1283 // If this is the first time we've seen this PHI, recursively process all 1284 // users. 1285 for (auto UI = PN->user_begin(), E = PN->user_end(); UI != E;) { 1286 Instruction *User = cast<Instruction>(*UI++); 1287 RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite); 1288 } 1289 } 1290 1291 /// We are performing Heap SRoA on a global. Ptr is a value loaded from the 1292 /// global. Eliminate all uses of Ptr, making them use FieldGlobals instead. 1293 /// All uses of loaded values satisfy AllGlobalLoadUsesSimpleEnoughForHeapSRA. 1294 static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load, 1295 DenseMap<Value *, std::vector<Value *>> &InsertedScalarizedValues, 1296 std::vector<std::pair<PHINode *, unsigned> > &PHIsToRewrite) { 1297 for (auto UI = Load->user_begin(), E = Load->user_end(); UI != E;) { 1298 Instruction *User = cast<Instruction>(*UI++); 1299 RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite); 1300 } 1301 1302 if (Load->use_empty()) { 1303 Load->eraseFromParent(); 1304 InsertedScalarizedValues.erase(Load); 1305 } 1306 } 1307 1308 /// CI is an allocation of an array of structures. Break it up into multiple 1309 /// allocations of arrays of the fields. 1310 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI, 1311 Value *NElems, const DataLayout &DL, 1312 const TargetLibraryInfo *TLI) { 1313 LLVM_DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI 1314 << '\n'); 1315 Type *MAT = getMallocAllocatedType(CI, TLI); 1316 StructType *STy = cast<StructType>(MAT); 1317 1318 // There is guaranteed to be at least one use of the malloc (storing 1319 // it into GV). If there are other uses, change them to be uses of 1320 // the global to simplify later code. This also deletes the store 1321 // into GV. 1322 ReplaceUsesOfMallocWithGlobal(CI, GV); 1323 1324 // Okay, at this point, there are no users of the malloc. Insert N 1325 // new mallocs at the same place as CI, and N globals. 1326 std::vector<Value *> FieldGlobals; 1327 std::vector<Value *> FieldMallocs; 1328 1329 SmallVector<OperandBundleDef, 1> OpBundles; 1330 CI->getOperandBundlesAsDefs(OpBundles); 1331 1332 unsigned AS = GV->getType()->getPointerAddressSpace(); 1333 for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){ 1334 Type *FieldTy = STy->getElementType(FieldNo); 1335 PointerType *PFieldTy = PointerType::get(FieldTy, AS); 1336 1337 GlobalVariable *NGV = new GlobalVariable( 1338 *GV->getParent(), PFieldTy, false, GlobalValue::InternalLinkage, 1339 Constant::getNullValue(PFieldTy), GV->getName() + ".f" + Twine(FieldNo), 1340 nullptr, GV->getThreadLocalMode()); 1341 NGV->copyAttributesFrom(GV); 1342 FieldGlobals.push_back(NGV); 1343 1344 unsigned TypeSize = DL.getTypeAllocSize(FieldTy); 1345 if (StructType *ST = dyn_cast<StructType>(FieldTy)) 1346 TypeSize = DL.getStructLayout(ST)->getSizeInBytes(); 1347 Type *IntPtrTy = DL.getIntPtrType(CI->getType()); 1348 Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy, 1349 ConstantInt::get(IntPtrTy, TypeSize), 1350 NElems, OpBundles, nullptr, 1351 CI->getName() + ".f" + Twine(FieldNo)); 1352 FieldMallocs.push_back(NMI); 1353 new StoreInst(NMI, NGV, CI); 1354 } 1355 1356 // The tricky aspect of this transformation is handling the case when malloc 1357 // fails. In the original code, malloc failing would set the result pointer 1358 // of malloc to null. In this case, some mallocs could succeed and others 1359 // could fail. As such, we emit code that looks like this: 1360 // F0 = malloc(field0) 1361 // F1 = malloc(field1) 1362 // F2 = malloc(field2) 1363 // if (F0 == 0 || F1 == 0 || F2 == 0) { 1364 // if (F0) { free(F0); F0 = 0; } 1365 // if (F1) { free(F1); F1 = 0; } 1366 // if (F2) { free(F2); F2 = 0; } 1367 // } 1368 // The malloc can also fail if its argument is too large. 1369 Constant *ConstantZero = ConstantInt::get(CI->getArgOperand(0)->getType(), 0); 1370 Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getArgOperand(0), 1371 ConstantZero, "isneg"); 1372 for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) { 1373 Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i], 1374 Constant::getNullValue(FieldMallocs[i]->getType()), 1375 "isnull"); 1376 RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI); 1377 } 1378 1379 // Split the basic block at the old malloc. 1380 BasicBlock *OrigBB = CI->getParent(); 1381 BasicBlock *ContBB = 1382 OrigBB->splitBasicBlock(CI->getIterator(), "malloc_cont"); 1383 1384 // Create the block to check the first condition. Put all these blocks at the 1385 // end of the function as they are unlikely to be executed. 1386 BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(), 1387 "malloc_ret_null", 1388 OrigBB->getParent()); 1389 1390 // Remove the uncond branch from OrigBB to ContBB, turning it into a cond 1391 // branch on RunningOr. 1392 OrigBB->getTerminator()->eraseFromParent(); 1393 BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB); 1394 1395 // Within the NullPtrBlock, we need to emit a comparison and branch for each 1396 // pointer, because some may be null while others are not. 1397 for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) { 1398 Value *GVVal = 1399 new LoadInst(cast<GlobalVariable>(FieldGlobals[i])->getValueType(), 1400 FieldGlobals[i], "tmp", NullPtrBlock); 1401 Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal, 1402 Constant::getNullValue(GVVal->getType())); 1403 BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it", 1404 OrigBB->getParent()); 1405 BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next", 1406 OrigBB->getParent()); 1407 Instruction *BI = BranchInst::Create(FreeBlock, NextBlock, 1408 Cmp, NullPtrBlock); 1409 1410 // Fill in FreeBlock. 1411 CallInst::CreateFree(GVVal, OpBundles, BI); 1412 new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i], 1413 FreeBlock); 1414 BranchInst::Create(NextBlock, FreeBlock); 1415 1416 NullPtrBlock = NextBlock; 1417 } 1418 1419 BranchInst::Create(ContBB, NullPtrBlock); 1420 1421 // CI is no longer needed, remove it. 1422 CI->eraseFromParent(); 1423 1424 /// As we process loads, if we can't immediately update all uses of the load, 1425 /// keep track of what scalarized loads are inserted for a given load. 1426 DenseMap<Value *, std::vector<Value *>> InsertedScalarizedValues; 1427 InsertedScalarizedValues[GV] = FieldGlobals; 1428 1429 std::vector<std::pair<PHINode *, unsigned>> PHIsToRewrite; 1430 1431 // Okay, the malloc site is completely handled. All of the uses of GV are now 1432 // loads, and all uses of those loads are simple. Rewrite them to use loads 1433 // of the per-field globals instead. 1434 for (auto UI = GV->user_begin(), E = GV->user_end(); UI != E;) { 1435 Instruction *User = cast<Instruction>(*UI++); 1436 1437 if (LoadInst *LI = dyn_cast<LoadInst>(User)) { 1438 RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite); 1439 continue; 1440 } 1441 1442 // Must be a store of null. 1443 StoreInst *SI = cast<StoreInst>(User); 1444 assert(isa<ConstantPointerNull>(SI->getOperand(0)) && 1445 "Unexpected heap-sra user!"); 1446 1447 // Insert a store of null into each global. 1448 for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) { 1449 Type *ValTy = cast<GlobalValue>(FieldGlobals[i])->getValueType(); 1450 Constant *Null = Constant::getNullValue(ValTy); 1451 new StoreInst(Null, FieldGlobals[i], SI); 1452 } 1453 // Erase the original store. 1454 SI->eraseFromParent(); 1455 } 1456 1457 // While we have PHIs that are interesting to rewrite, do it. 1458 while (!PHIsToRewrite.empty()) { 1459 PHINode *PN = PHIsToRewrite.back().first; 1460 unsigned FieldNo = PHIsToRewrite.back().second; 1461 PHIsToRewrite.pop_back(); 1462 PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]); 1463 assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi"); 1464 1465 // Add all the incoming values. This can materialize more phis. 1466 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 1467 Value *InVal = PN->getIncomingValue(i); 1468 InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues, 1469 PHIsToRewrite); 1470 FieldPN->addIncoming(InVal, PN->getIncomingBlock(i)); 1471 } 1472 } 1473 1474 // Drop all inter-phi links and any loads that made it this far. 1475 for (DenseMap<Value *, std::vector<Value *>>::iterator 1476 I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end(); 1477 I != E; ++I) { 1478 if (PHINode *PN = dyn_cast<PHINode>(I->first)) 1479 PN->dropAllReferences(); 1480 else if (LoadInst *LI = dyn_cast<LoadInst>(I->first)) 1481 LI->dropAllReferences(); 1482 } 1483 1484 // Delete all the phis and loads now that inter-references are dead. 1485 for (DenseMap<Value *, std::vector<Value *>>::iterator 1486 I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end(); 1487 I != E; ++I) { 1488 if (PHINode *PN = dyn_cast<PHINode>(I->first)) 1489 PN->eraseFromParent(); 1490 else if (LoadInst *LI = dyn_cast<LoadInst>(I->first)) 1491 LI->eraseFromParent(); 1492 } 1493 1494 // The old global is now dead, remove it. 1495 GV->eraseFromParent(); 1496 1497 ++NumHeapSRA; 1498 return cast<GlobalVariable>(FieldGlobals[0]); 1499 } 1500 1501 /// This function is called when we see a pointer global variable with a single 1502 /// value stored it that is a malloc or cast of malloc. 1503 static bool tryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV, CallInst *CI, 1504 Type *AllocTy, 1505 AtomicOrdering Ordering, 1506 const DataLayout &DL, 1507 TargetLibraryInfo *TLI) { 1508 // If this is a malloc of an abstract type, don't touch it. 1509 if (!AllocTy->isSized()) 1510 return false; 1511 1512 // We can't optimize this global unless all uses of it are *known* to be 1513 // of the malloc value, not of the null initializer value (consider a use 1514 // that compares the global's value against zero to see if the malloc has 1515 // been reached). To do this, we check to see if all uses of the global 1516 // would trap if the global were null: this proves that they must all 1517 // happen after the malloc. 1518 if (!AllUsesOfLoadedValueWillTrapIfNull(GV)) 1519 return false; 1520 1521 // We can't optimize this if the malloc itself is used in a complex way, 1522 // for example, being stored into multiple globals. This allows the 1523 // malloc to be stored into the specified global, loaded icmp'd, and 1524 // GEP'd. These are all things we could transform to using the global 1525 // for. 1526 SmallPtrSet<const PHINode*, 8> PHIs; 1527 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs)) 1528 return false; 1529 1530 // If we have a global that is only initialized with a fixed size malloc, 1531 // transform the program to use global memory instead of malloc'd memory. 1532 // This eliminates dynamic allocation, avoids an indirection accessing the 1533 // data, and exposes the resultant global to further GlobalOpt. 1534 // We cannot optimize the malloc if we cannot determine malloc array size. 1535 Value *NElems = getMallocArraySize(CI, DL, TLI, true); 1536 if (!NElems) 1537 return false; 1538 1539 if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems)) 1540 // Restrict this transformation to only working on small allocations 1541 // (2048 bytes currently), as we don't want to introduce a 16M global or 1542 // something. 1543 if (NElements->getZExtValue() * DL.getTypeAllocSize(AllocTy) < 2048) { 1544 OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, DL, TLI); 1545 return true; 1546 } 1547 1548 // If the allocation is an array of structures, consider transforming this 1549 // into multiple malloc'd arrays, one for each field. This is basically 1550 // SRoA for malloc'd memory. 1551 1552 if (Ordering != AtomicOrdering::NotAtomic) 1553 return false; 1554 1555 // If this is an allocation of a fixed size array of structs, analyze as a 1556 // variable size array. malloc [100 x struct],1 -> malloc struct, 100 1557 if (NElems == ConstantInt::get(CI->getArgOperand(0)->getType(), 1)) 1558 if (ArrayType *AT = dyn_cast<ArrayType>(AllocTy)) 1559 AllocTy = AT->getElementType(); 1560 1561 StructType *AllocSTy = dyn_cast<StructType>(AllocTy); 1562 if (!AllocSTy) 1563 return false; 1564 1565 // This the structure has an unreasonable number of fields, leave it 1566 // alone. 1567 if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 && 1568 AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) { 1569 1570 // If this is a fixed size array, transform the Malloc to be an alloc of 1571 // structs. malloc [100 x struct],1 -> malloc struct, 100 1572 if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) { 1573 Type *IntPtrTy = DL.getIntPtrType(CI->getType()); 1574 unsigned TypeSize = DL.getStructLayout(AllocSTy)->getSizeInBytes(); 1575 Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize); 1576 Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements()); 1577 SmallVector<OperandBundleDef, 1> OpBundles; 1578 CI->getOperandBundlesAsDefs(OpBundles); 1579 Instruction *Malloc = 1580 CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy, AllocSize, NumElements, 1581 OpBundles, nullptr, CI->getName()); 1582 Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI); 1583 CI->replaceAllUsesWith(Cast); 1584 CI->eraseFromParent(); 1585 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc)) 1586 CI = cast<CallInst>(BCI->getOperand(0)); 1587 else 1588 CI = cast<CallInst>(Malloc); 1589 } 1590 1591 PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, DL, TLI, true), DL, 1592 TLI); 1593 return true; 1594 } 1595 1596 return false; 1597 } 1598 1599 // Try to optimize globals based on the knowledge that only one value (besides 1600 // its initializer) is ever stored to the global. 1601 static bool 1602 optimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, 1603 AtomicOrdering Ordering, const DataLayout &DL, 1604 function_ref<TargetLibraryInfo &(Function &)> GetTLI) { 1605 // Ignore no-op GEPs and bitcasts. 1606 StoredOnceVal = StoredOnceVal->stripPointerCasts(); 1607 1608 // If we are dealing with a pointer global that is initialized to null and 1609 // only has one (non-null) value stored into it, then we can optimize any 1610 // users of the loaded value (often calls and loads) that would trap if the 1611 // value was null. 1612 if (GV->getInitializer()->getType()->isPointerTy() && 1613 GV->getInitializer()->isNullValue() && 1614 !NullPointerIsDefined( 1615 nullptr /* F */, 1616 GV->getInitializer()->getType()->getPointerAddressSpace())) { 1617 if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) { 1618 if (GV->getInitializer()->getType() != SOVC->getType()) 1619 SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType()); 1620 1621 // Optimize away any trapping uses of the loaded value. 1622 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, GetTLI)) 1623 return true; 1624 } else if (CallInst *CI = extractMallocCall(StoredOnceVal, GetTLI)) { 1625 auto *TLI = &GetTLI(*CI->getFunction()); 1626 Type *MallocType = getMallocAllocatedType(CI, TLI); 1627 if (MallocType && tryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, 1628 Ordering, DL, TLI)) 1629 return true; 1630 } 1631 } 1632 1633 return false; 1634 } 1635 1636 /// At this point, we have learned that the only two values ever stored into GV 1637 /// are its initializer and OtherVal. See if we can shrink the global into a 1638 /// boolean and select between the two values whenever it is used. This exposes 1639 /// the values to other scalar optimizations. 1640 static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) { 1641 Type *GVElType = GV->getValueType(); 1642 1643 // If GVElType is already i1, it is already shrunk. If the type of the GV is 1644 // an FP value, pointer or vector, don't do this optimization because a select 1645 // between them is very expensive and unlikely to lead to later 1646 // simplification. In these cases, we typically end up with "cond ? v1 : v2" 1647 // where v1 and v2 both require constant pool loads, a big loss. 1648 if (GVElType == Type::getInt1Ty(GV->getContext()) || 1649 GVElType->isFloatingPointTy() || 1650 GVElType->isPointerTy() || GVElType->isVectorTy()) 1651 return false; 1652 1653 // Walk the use list of the global seeing if all the uses are load or store. 1654 // If there is anything else, bail out. 1655 for (User *U : GV->users()) 1656 if (!isa<LoadInst>(U) && !isa<StoreInst>(U)) 1657 return false; 1658 1659 LLVM_DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV << "\n"); 1660 1661 // Create the new global, initializing it to false. 1662 GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()), 1663 false, 1664 GlobalValue::InternalLinkage, 1665 ConstantInt::getFalse(GV->getContext()), 1666 GV->getName()+".b", 1667 GV->getThreadLocalMode(), 1668 GV->getType()->getAddressSpace()); 1669 NewGV->copyAttributesFrom(GV); 1670 GV->getParent()->getGlobalList().insert(GV->getIterator(), NewGV); 1671 1672 Constant *InitVal = GV->getInitializer(); 1673 assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) && 1674 "No reason to shrink to bool!"); 1675 1676 SmallVector<DIGlobalVariableExpression *, 1> GVs; 1677 GV->getDebugInfo(GVs); 1678 1679 // If initialized to zero and storing one into the global, we can use a cast 1680 // instead of a select to synthesize the desired value. 1681 bool IsOneZero = false; 1682 bool EmitOneOrZero = true; 1683 auto *CI = dyn_cast<ConstantInt>(OtherVal); 1684 if (CI && CI->getValue().getActiveBits() <= 64) { 1685 IsOneZero = InitVal->isNullValue() && CI->isOne(); 1686 1687 auto *CIInit = dyn_cast<ConstantInt>(GV->getInitializer()); 1688 if (CIInit && CIInit->getValue().getActiveBits() <= 64) { 1689 uint64_t ValInit = CIInit->getZExtValue(); 1690 uint64_t ValOther = CI->getZExtValue(); 1691 uint64_t ValMinus = ValOther - ValInit; 1692 1693 for(auto *GVe : GVs){ 1694 DIGlobalVariable *DGV = GVe->getVariable(); 1695 DIExpression *E = GVe->getExpression(); 1696 const DataLayout &DL = GV->getParent()->getDataLayout(); 1697 unsigned SizeInOctets = 1698 DL.getTypeAllocSizeInBits(NewGV->getType()->getElementType()) / 8; 1699 1700 // It is expected that the address of global optimized variable is on 1701 // top of the stack. After optimization, value of that variable will 1702 // be ether 0 for initial value or 1 for other value. The following 1703 // expression should return constant integer value depending on the 1704 // value at global object address: 1705 // val * (ValOther - ValInit) + ValInit: 1706 // DW_OP_deref DW_OP_constu <ValMinus> 1707 // DW_OP_mul DW_OP_constu <ValInit> DW_OP_plus DW_OP_stack_value 1708 SmallVector<uint64_t, 12> Ops = { 1709 dwarf::DW_OP_deref_size, SizeInOctets, 1710 dwarf::DW_OP_constu, ValMinus, 1711 dwarf::DW_OP_mul, dwarf::DW_OP_constu, ValInit, 1712 dwarf::DW_OP_plus}; 1713 bool WithStackValue = true; 1714 E = DIExpression::prependOpcodes(E, Ops, WithStackValue); 1715 DIGlobalVariableExpression *DGVE = 1716 DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E); 1717 NewGV->addDebugInfo(DGVE); 1718 } 1719 EmitOneOrZero = false; 1720 } 1721 } 1722 1723 if (EmitOneOrZero) { 1724 // FIXME: This will only emit address for debugger on which will 1725 // be written only 0 or 1. 1726 for(auto *GV : GVs) 1727 NewGV->addDebugInfo(GV); 1728 } 1729 1730 while (!GV->use_empty()) { 1731 Instruction *UI = cast<Instruction>(GV->user_back()); 1732 if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { 1733 // Change the store into a boolean store. 1734 bool StoringOther = SI->getOperand(0) == OtherVal; 1735 // Only do this if we weren't storing a loaded value. 1736 Value *StoreVal; 1737 if (StoringOther || SI->getOperand(0) == InitVal) { 1738 StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()), 1739 StoringOther); 1740 } else { 1741 // Otherwise, we are storing a previously loaded copy. To do this, 1742 // change the copy from copying the original value to just copying the 1743 // bool. 1744 Instruction *StoredVal = cast<Instruction>(SI->getOperand(0)); 1745 1746 // If we've already replaced the input, StoredVal will be a cast or 1747 // select instruction. If not, it will be a load of the original 1748 // global. 1749 if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) { 1750 assert(LI->getOperand(0) == GV && "Not a copy!"); 1751 // Insert a new load, to preserve the saved value. 1752 StoreVal = new LoadInst(NewGV->getValueType(), NewGV, 1753 LI->getName() + ".b", false, Align(1), 1754 LI->getOrdering(), LI->getSyncScopeID(), LI); 1755 } else { 1756 assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) && 1757 "This is not a form that we understand!"); 1758 StoreVal = StoredVal->getOperand(0); 1759 assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!"); 1760 } 1761 } 1762 StoreInst *NSI = 1763 new StoreInst(StoreVal, NewGV, false, Align(1), SI->getOrdering(), 1764 SI->getSyncScopeID(), SI); 1765 NSI->setDebugLoc(SI->getDebugLoc()); 1766 } else { 1767 // Change the load into a load of bool then a select. 1768 LoadInst *LI = cast<LoadInst>(UI); 1769 LoadInst *NLI = new LoadInst(NewGV->getValueType(), NewGV, 1770 LI->getName() + ".b", false, Align(1), 1771 LI->getOrdering(), LI->getSyncScopeID(), LI); 1772 Instruction *NSI; 1773 if (IsOneZero) 1774 NSI = new ZExtInst(NLI, LI->getType(), "", LI); 1775 else 1776 NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI); 1777 NSI->takeName(LI); 1778 // Since LI is split into two instructions, NLI and NSI both inherit the 1779 // same DebugLoc 1780 NLI->setDebugLoc(LI->getDebugLoc()); 1781 NSI->setDebugLoc(LI->getDebugLoc()); 1782 LI->replaceAllUsesWith(NSI); 1783 } 1784 UI->eraseFromParent(); 1785 } 1786 1787 // Retain the name of the old global variable. People who are debugging their 1788 // programs may expect these variables to be named the same. 1789 NewGV->takeName(GV); 1790 GV->eraseFromParent(); 1791 return true; 1792 } 1793 1794 static bool deleteIfDead( 1795 GlobalValue &GV, SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) { 1796 GV.removeDeadConstantUsers(); 1797 1798 if (!GV.isDiscardableIfUnused() && !GV.isDeclaration()) 1799 return false; 1800 1801 if (const Comdat *C = GV.getComdat()) 1802 if (!GV.hasLocalLinkage() && NotDiscardableComdats.count(C)) 1803 return false; 1804 1805 bool Dead; 1806 if (auto *F = dyn_cast<Function>(&GV)) 1807 Dead = (F->isDeclaration() && F->use_empty()) || F->isDefTriviallyDead(); 1808 else 1809 Dead = GV.use_empty(); 1810 if (!Dead) 1811 return false; 1812 1813 LLVM_DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n"); 1814 GV.eraseFromParent(); 1815 ++NumDeleted; 1816 return true; 1817 } 1818 1819 static bool isPointerValueDeadOnEntryToFunction( 1820 const Function *F, GlobalValue *GV, 1821 function_ref<DominatorTree &(Function &)> LookupDomTree) { 1822 // Find all uses of GV. We expect them all to be in F, and if we can't 1823 // identify any of the uses we bail out. 1824 // 1825 // On each of these uses, identify if the memory that GV points to is 1826 // used/required/live at the start of the function. If it is not, for example 1827 // if the first thing the function does is store to the GV, the GV can 1828 // possibly be demoted. 1829 // 1830 // We don't do an exhaustive search for memory operations - simply look 1831 // through bitcasts as they're quite common and benign. 1832 const DataLayout &DL = GV->getParent()->getDataLayout(); 1833 SmallVector<LoadInst *, 4> Loads; 1834 SmallVector<StoreInst *, 4> Stores; 1835 for (auto *U : GV->users()) { 1836 if (Operator::getOpcode(U) == Instruction::BitCast) { 1837 for (auto *UU : U->users()) { 1838 if (auto *LI = dyn_cast<LoadInst>(UU)) 1839 Loads.push_back(LI); 1840 else if (auto *SI = dyn_cast<StoreInst>(UU)) 1841 Stores.push_back(SI); 1842 else 1843 return false; 1844 } 1845 continue; 1846 } 1847 1848 Instruction *I = dyn_cast<Instruction>(U); 1849 if (!I) 1850 return false; 1851 assert(I->getParent()->getParent() == F); 1852 1853 if (auto *LI = dyn_cast<LoadInst>(I)) 1854 Loads.push_back(LI); 1855 else if (auto *SI = dyn_cast<StoreInst>(I)) 1856 Stores.push_back(SI); 1857 else 1858 return false; 1859 } 1860 1861 // We have identified all uses of GV into loads and stores. Now check if all 1862 // of them are known not to depend on the value of the global at the function 1863 // entry point. We do this by ensuring that every load is dominated by at 1864 // least one store. 1865 auto &DT = LookupDomTree(*const_cast<Function *>(F)); 1866 1867 // The below check is quadratic. Check we're not going to do too many tests. 1868 // FIXME: Even though this will always have worst-case quadratic time, we 1869 // could put effort into minimizing the average time by putting stores that 1870 // have been shown to dominate at least one load at the beginning of the 1871 // Stores array, making subsequent dominance checks more likely to succeed 1872 // early. 1873 // 1874 // The threshold here is fairly large because global->local demotion is a 1875 // very powerful optimization should it fire. 1876 const unsigned Threshold = 100; 1877 if (Loads.size() * Stores.size() > Threshold) 1878 return false; 1879 1880 for (auto *L : Loads) { 1881 auto *LTy = L->getType(); 1882 if (none_of(Stores, [&](const StoreInst *S) { 1883 auto *STy = S->getValueOperand()->getType(); 1884 // The load is only dominated by the store if DomTree says so 1885 // and the number of bits loaded in L is less than or equal to 1886 // the number of bits stored in S. 1887 return DT.dominates(S, L) && 1888 DL.getTypeStoreSize(LTy).getFixedSize() <= 1889 DL.getTypeStoreSize(STy).getFixedSize(); 1890 })) 1891 return false; 1892 } 1893 // All loads have known dependences inside F, so the global can be localized. 1894 return true; 1895 } 1896 1897 /// C may have non-instruction users. Can all of those users be turned into 1898 /// instructions? 1899 static bool allNonInstructionUsersCanBeMadeInstructions(Constant *C) { 1900 // We don't do this exhaustively. The most common pattern that we really need 1901 // to care about is a constant GEP or constant bitcast - so just looking 1902 // through one single ConstantExpr. 1903 // 1904 // The set of constants that this function returns true for must be able to be 1905 // handled by makeAllConstantUsesInstructions. 1906 for (auto *U : C->users()) { 1907 if (isa<Instruction>(U)) 1908 continue; 1909 if (!isa<ConstantExpr>(U)) 1910 // Non instruction, non-constantexpr user; cannot convert this. 1911 return false; 1912 for (auto *UU : U->users()) 1913 if (!isa<Instruction>(UU)) 1914 // A constantexpr used by another constant. We don't try and recurse any 1915 // further but just bail out at this point. 1916 return false; 1917 } 1918 1919 return true; 1920 } 1921 1922 /// C may have non-instruction users, and 1923 /// allNonInstructionUsersCanBeMadeInstructions has returned true. Convert the 1924 /// non-instruction users to instructions. 1925 static void makeAllConstantUsesInstructions(Constant *C) { 1926 SmallVector<ConstantExpr*,4> Users; 1927 for (auto *U : C->users()) { 1928 if (isa<ConstantExpr>(U)) 1929 Users.push_back(cast<ConstantExpr>(U)); 1930 else 1931 // We should never get here; allNonInstructionUsersCanBeMadeInstructions 1932 // should not have returned true for C. 1933 assert( 1934 isa<Instruction>(U) && 1935 "Can't transform non-constantexpr non-instruction to instruction!"); 1936 } 1937 1938 SmallVector<Value*,4> UUsers; 1939 for (auto *U : Users) { 1940 UUsers.clear(); 1941 append_range(UUsers, U->users()); 1942 for (auto *UU : UUsers) { 1943 Instruction *UI = cast<Instruction>(UU); 1944 Instruction *NewU = U->getAsInstruction(); 1945 NewU->insertBefore(UI); 1946 UI->replaceUsesOfWith(U, NewU); 1947 } 1948 // We've replaced all the uses, so destroy the constant. (destroyConstant 1949 // will update value handles and metadata.) 1950 U->destroyConstant(); 1951 } 1952 } 1953 1954 /// Analyze the specified global variable and optimize 1955 /// it if possible. If we make a change, return true. 1956 static bool 1957 processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS, 1958 function_ref<TargetLibraryInfo &(Function &)> GetTLI, 1959 function_ref<DominatorTree &(Function &)> LookupDomTree) { 1960 auto &DL = GV->getParent()->getDataLayout(); 1961 // If this is a first class global and has only one accessing function and 1962 // this function is non-recursive, we replace the global with a local alloca 1963 // in this function. 1964 // 1965 // NOTE: It doesn't make sense to promote non-single-value types since we 1966 // are just replacing static memory to stack memory. 1967 // 1968 // If the global is in different address space, don't bring it to stack. 1969 if (!GS.HasMultipleAccessingFunctions && 1970 GS.AccessingFunction && 1971 GV->getValueType()->isSingleValueType() && 1972 GV->getType()->getAddressSpace() == 0 && 1973 !GV->isExternallyInitialized() && 1974 allNonInstructionUsersCanBeMadeInstructions(GV) && 1975 GS.AccessingFunction->doesNotRecurse() && 1976 isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV, 1977 LookupDomTree)) { 1978 const DataLayout &DL = GV->getParent()->getDataLayout(); 1979 1980 LLVM_DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n"); 1981 Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction 1982 ->getEntryBlock().begin()); 1983 Type *ElemTy = GV->getValueType(); 1984 // FIXME: Pass Global's alignment when globals have alignment 1985 AllocaInst *Alloca = new AllocaInst(ElemTy, DL.getAllocaAddrSpace(), nullptr, 1986 GV->getName(), &FirstI); 1987 if (!isa<UndefValue>(GV->getInitializer())) 1988 new StoreInst(GV->getInitializer(), Alloca, &FirstI); 1989 1990 makeAllConstantUsesInstructions(GV); 1991 1992 GV->replaceAllUsesWith(Alloca); 1993 GV->eraseFromParent(); 1994 ++NumLocalized; 1995 return true; 1996 } 1997 1998 bool Changed = false; 1999 2000 // If the global is never loaded (but may be stored to), it is dead. 2001 // Delete it now. 2002 if (!GS.IsLoaded) { 2003 LLVM_DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV << "\n"); 2004 2005 if (isLeakCheckerRoot(GV)) { 2006 // Delete any constant stores to the global. 2007 Changed = CleanupPointerRootUsers(GV, GetTLI); 2008 } else { 2009 // Delete any stores we can find to the global. We may not be able to 2010 // make it completely dead though. 2011 Changed = 2012 CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, GetTLI); 2013 } 2014 2015 // If the global is dead now, delete it. 2016 if (GV->use_empty()) { 2017 GV->eraseFromParent(); 2018 ++NumDeleted; 2019 Changed = true; 2020 } 2021 return Changed; 2022 2023 } 2024 if (GS.StoredType <= GlobalStatus::InitializerStored) { 2025 LLVM_DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n"); 2026 2027 // Don't actually mark a global constant if it's atomic because atomic loads 2028 // are implemented by a trivial cmpxchg in some edge-cases and that usually 2029 // requires write access to the variable even if it's not actually changed. 2030 if (GS.Ordering == AtomicOrdering::NotAtomic) { 2031 assert(!GV->isConstant() && "Expected a non-constant global"); 2032 GV->setConstant(true); 2033 Changed = true; 2034 } 2035 2036 // Clean up any obviously simplifiable users now. 2037 Changed |= CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, GetTLI); 2038 2039 // If the global is dead now, just nuke it. 2040 if (GV->use_empty()) { 2041 LLVM_DEBUG(dbgs() << " *** Marking constant allowed us to simplify " 2042 << "all users and delete global!\n"); 2043 GV->eraseFromParent(); 2044 ++NumDeleted; 2045 return true; 2046 } 2047 2048 // Fall through to the next check; see if we can optimize further. 2049 ++NumMarked; 2050 } 2051 if (!GV->getInitializer()->getType()->isSingleValueType()) { 2052 const DataLayout &DL = GV->getParent()->getDataLayout(); 2053 if (SRAGlobal(GV, DL)) 2054 return true; 2055 } 2056 if (GS.StoredType == GlobalStatus::StoredOnce && GS.StoredOnceValue) { 2057 // If the initial value for the global was an undef value, and if only 2058 // one other value was stored into it, we can just change the 2059 // initializer to be the stored value, then delete all stores to the 2060 // global. This allows us to mark it constant. 2061 if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) 2062 if (isa<UndefValue>(GV->getInitializer())) { 2063 // Change the initial value here. 2064 GV->setInitializer(SOVConstant); 2065 2066 // Clean up any obviously simplifiable users now. 2067 CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, GetTLI); 2068 2069 if (GV->use_empty()) { 2070 LLVM_DEBUG(dbgs() << " *** Substituting initializer allowed us to " 2071 << "simplify all users and delete global!\n"); 2072 GV->eraseFromParent(); 2073 ++NumDeleted; 2074 } 2075 ++NumSubstitute; 2076 return true; 2077 } 2078 2079 // Try to optimize globals based on the knowledge that only one value 2080 // (besides its initializer) is ever stored to the global. 2081 if (optimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, DL, 2082 GetTLI)) 2083 return true; 2084 2085 // Otherwise, if the global was not a boolean, we can shrink it to be a 2086 // boolean. 2087 if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) { 2088 if (GS.Ordering == AtomicOrdering::NotAtomic) { 2089 if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) { 2090 ++NumShrunkToBool; 2091 return true; 2092 } 2093 } 2094 } 2095 } 2096 2097 return Changed; 2098 } 2099 2100 /// Analyze the specified global variable and optimize it if possible. If we 2101 /// make a change, return true. 2102 static bool 2103 processGlobal(GlobalValue &GV, 2104 function_ref<TargetLibraryInfo &(Function &)> GetTLI, 2105 function_ref<DominatorTree &(Function &)> LookupDomTree) { 2106 if (GV.getName().startswith("llvm.")) 2107 return false; 2108 2109 GlobalStatus GS; 2110 2111 if (GlobalStatus::analyzeGlobal(&GV, GS)) 2112 return false; 2113 2114 bool Changed = false; 2115 if (!GS.IsCompared && !GV.hasGlobalUnnamedAddr()) { 2116 auto NewUnnamedAddr = GV.hasLocalLinkage() ? GlobalValue::UnnamedAddr::Global 2117 : GlobalValue::UnnamedAddr::Local; 2118 if (NewUnnamedAddr != GV.getUnnamedAddr()) { 2119 GV.setUnnamedAddr(NewUnnamedAddr); 2120 NumUnnamed++; 2121 Changed = true; 2122 } 2123 } 2124 2125 // Do more involved optimizations if the global is internal. 2126 if (!GV.hasLocalLinkage()) 2127 return Changed; 2128 2129 auto *GVar = dyn_cast<GlobalVariable>(&GV); 2130 if (!GVar) 2131 return Changed; 2132 2133 if (GVar->isConstant() || !GVar->hasInitializer()) 2134 return Changed; 2135 2136 return processInternalGlobal(GVar, GS, GetTLI, LookupDomTree) || Changed; 2137 } 2138 2139 /// Walk all of the direct calls of the specified function, changing them to 2140 /// FastCC. 2141 static void ChangeCalleesToFastCall(Function *F) { 2142 for (User *U : F->users()) { 2143 if (isa<BlockAddress>(U)) 2144 continue; 2145 cast<CallBase>(U)->setCallingConv(CallingConv::Fast); 2146 } 2147 } 2148 2149 static AttributeList StripAttr(LLVMContext &C, AttributeList Attrs, 2150 Attribute::AttrKind A) { 2151 unsigned AttrIndex; 2152 if (Attrs.hasAttrSomewhere(A, &AttrIndex)) 2153 return Attrs.removeAttribute(C, AttrIndex, A); 2154 return Attrs; 2155 } 2156 2157 static void RemoveAttribute(Function *F, Attribute::AttrKind A) { 2158 F->setAttributes(StripAttr(F->getContext(), F->getAttributes(), A)); 2159 for (User *U : F->users()) { 2160 if (isa<BlockAddress>(U)) 2161 continue; 2162 CallBase *CB = cast<CallBase>(U); 2163 CB->setAttributes(StripAttr(F->getContext(), CB->getAttributes(), A)); 2164 } 2165 } 2166 2167 /// Return true if this is a calling convention that we'd like to change. The 2168 /// idea here is that we don't want to mess with the convention if the user 2169 /// explicitly requested something with performance implications like coldcc, 2170 /// GHC, or anyregcc. 2171 static bool hasChangeableCC(Function *F) { 2172 CallingConv::ID CC = F->getCallingConv(); 2173 2174 // FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc? 2175 if (CC != CallingConv::C && CC != CallingConv::X86_ThisCall) 2176 return false; 2177 2178 // FIXME: Change CC for the whole chain of musttail calls when possible. 2179 // 2180 // Can't change CC of the function that either has musttail calls, or is a 2181 // musttail callee itself 2182 for (User *U : F->users()) { 2183 if (isa<BlockAddress>(U)) 2184 continue; 2185 CallInst* CI = dyn_cast<CallInst>(U); 2186 if (!CI) 2187 continue; 2188 2189 if (CI->isMustTailCall()) 2190 return false; 2191 } 2192 2193 for (BasicBlock &BB : *F) 2194 if (BB.getTerminatingMustTailCall()) 2195 return false; 2196 2197 return true; 2198 } 2199 2200 /// Return true if the block containing the call site has a BlockFrequency of 2201 /// less than ColdCCRelFreq% of the entry block. 2202 static bool isColdCallSite(CallBase &CB, BlockFrequencyInfo &CallerBFI) { 2203 const BranchProbability ColdProb(ColdCCRelFreq, 100); 2204 auto *CallSiteBB = CB.getParent(); 2205 auto CallSiteFreq = CallerBFI.getBlockFreq(CallSiteBB); 2206 auto CallerEntryFreq = 2207 CallerBFI.getBlockFreq(&(CB.getCaller()->getEntryBlock())); 2208 return CallSiteFreq < CallerEntryFreq * ColdProb; 2209 } 2210 2211 // This function checks if the input function F is cold at all call sites. It 2212 // also looks each call site's containing function, returning false if the 2213 // caller function contains other non cold calls. The input vector AllCallsCold 2214 // contains a list of functions that only have call sites in cold blocks. 2215 static bool 2216 isValidCandidateForColdCC(Function &F, 2217 function_ref<BlockFrequencyInfo &(Function &)> GetBFI, 2218 const std::vector<Function *> &AllCallsCold) { 2219 2220 if (F.user_empty()) 2221 return false; 2222 2223 for (User *U : F.users()) { 2224 if (isa<BlockAddress>(U)) 2225 continue; 2226 2227 CallBase &CB = cast<CallBase>(*U); 2228 Function *CallerFunc = CB.getParent()->getParent(); 2229 BlockFrequencyInfo &CallerBFI = GetBFI(*CallerFunc); 2230 if (!isColdCallSite(CB, CallerBFI)) 2231 return false; 2232 if (!llvm::is_contained(AllCallsCold, CallerFunc)) 2233 return false; 2234 } 2235 return true; 2236 } 2237 2238 static void changeCallSitesToColdCC(Function *F) { 2239 for (User *U : F->users()) { 2240 if (isa<BlockAddress>(U)) 2241 continue; 2242 cast<CallBase>(U)->setCallingConv(CallingConv::Cold); 2243 } 2244 } 2245 2246 // This function iterates over all the call instructions in the input Function 2247 // and checks that all call sites are in cold blocks and are allowed to use the 2248 // coldcc calling convention. 2249 static bool 2250 hasOnlyColdCalls(Function &F, 2251 function_ref<BlockFrequencyInfo &(Function &)> GetBFI) { 2252 for (BasicBlock &BB : F) { 2253 for (Instruction &I : BB) { 2254 if (CallInst *CI = dyn_cast<CallInst>(&I)) { 2255 // Skip over isline asm instructions since they aren't function calls. 2256 if (CI->isInlineAsm()) 2257 continue; 2258 Function *CalledFn = CI->getCalledFunction(); 2259 if (!CalledFn) 2260 return false; 2261 if (!CalledFn->hasLocalLinkage()) 2262 return false; 2263 // Skip over instrinsics since they won't remain as function calls. 2264 if (CalledFn->getIntrinsicID() != Intrinsic::not_intrinsic) 2265 continue; 2266 // Check if it's valid to use coldcc calling convention. 2267 if (!hasChangeableCC(CalledFn) || CalledFn->isVarArg() || 2268 CalledFn->hasAddressTaken()) 2269 return false; 2270 BlockFrequencyInfo &CallerBFI = GetBFI(F); 2271 if (!isColdCallSite(*CI, CallerBFI)) 2272 return false; 2273 } 2274 } 2275 } 2276 return true; 2277 } 2278 2279 static bool hasMustTailCallers(Function *F) { 2280 for (User *U : F->users()) { 2281 CallBase *CB = dyn_cast<CallBase>(U); 2282 if (!CB) { 2283 assert(isa<BlockAddress>(U) && 2284 "Expected either CallBase or BlockAddress"); 2285 continue; 2286 } 2287 if (CB->isMustTailCall()) 2288 return true; 2289 } 2290 return false; 2291 } 2292 2293 static bool hasInvokeCallers(Function *F) { 2294 for (User *U : F->users()) 2295 if (isa<InvokeInst>(U)) 2296 return true; 2297 return false; 2298 } 2299 2300 static void RemovePreallocated(Function *F) { 2301 RemoveAttribute(F, Attribute::Preallocated); 2302 2303 auto *M = F->getParent(); 2304 2305 IRBuilder<> Builder(M->getContext()); 2306 2307 // Cannot modify users() while iterating over it, so make a copy. 2308 SmallVector<User *, 4> PreallocatedCalls(F->users()); 2309 for (User *U : PreallocatedCalls) { 2310 CallBase *CB = dyn_cast<CallBase>(U); 2311 if (!CB) 2312 continue; 2313 2314 assert( 2315 !CB->isMustTailCall() && 2316 "Shouldn't call RemotePreallocated() on a musttail preallocated call"); 2317 // Create copy of call without "preallocated" operand bundle. 2318 SmallVector<OperandBundleDef, 1> OpBundles; 2319 CB->getOperandBundlesAsDefs(OpBundles); 2320 CallBase *PreallocatedSetup = nullptr; 2321 for (auto *It = OpBundles.begin(); It != OpBundles.end(); ++It) { 2322 if (It->getTag() == "preallocated") { 2323 PreallocatedSetup = cast<CallBase>(*It->input_begin()); 2324 OpBundles.erase(It); 2325 break; 2326 } 2327 } 2328 assert(PreallocatedSetup && "Did not find preallocated bundle"); 2329 uint64_t ArgCount = 2330 cast<ConstantInt>(PreallocatedSetup->getArgOperand(0))->getZExtValue(); 2331 2332 assert((isa<CallInst>(CB) || isa<InvokeInst>(CB)) && 2333 "Unknown indirect call type"); 2334 CallBase *NewCB = CallBase::Create(CB, OpBundles, CB); 2335 CB->replaceAllUsesWith(NewCB); 2336 NewCB->takeName(CB); 2337 CB->eraseFromParent(); 2338 2339 Builder.SetInsertPoint(PreallocatedSetup); 2340 auto *StackSave = 2341 Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stacksave)); 2342 2343 Builder.SetInsertPoint(NewCB->getNextNonDebugInstruction()); 2344 Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackrestore), 2345 StackSave); 2346 2347 // Replace @llvm.call.preallocated.arg() with alloca. 2348 // Cannot modify users() while iterating over it, so make a copy. 2349 // @llvm.call.preallocated.arg() can be called with the same index multiple 2350 // times. So for each @llvm.call.preallocated.arg(), we see if we have 2351 // already created a Value* for the index, and if not, create an alloca and 2352 // bitcast right after the @llvm.call.preallocated.setup() so that it 2353 // dominates all uses. 2354 SmallVector<Value *, 2> ArgAllocas(ArgCount); 2355 SmallVector<User *, 2> PreallocatedArgs(PreallocatedSetup->users()); 2356 for (auto *User : PreallocatedArgs) { 2357 auto *UseCall = cast<CallBase>(User); 2358 assert(UseCall->getCalledFunction()->getIntrinsicID() == 2359 Intrinsic::call_preallocated_arg && 2360 "preallocated token use was not a llvm.call.preallocated.arg"); 2361 uint64_t AllocArgIndex = 2362 cast<ConstantInt>(UseCall->getArgOperand(1))->getZExtValue(); 2363 Value *AllocaReplacement = ArgAllocas[AllocArgIndex]; 2364 if (!AllocaReplacement) { 2365 auto AddressSpace = UseCall->getType()->getPointerAddressSpace(); 2366 auto *ArgType = UseCall 2367 ->getAttribute(AttributeList::FunctionIndex, 2368 Attribute::Preallocated) 2369 .getValueAsType(); 2370 auto *InsertBefore = PreallocatedSetup->getNextNonDebugInstruction(); 2371 Builder.SetInsertPoint(InsertBefore); 2372 auto *Alloca = 2373 Builder.CreateAlloca(ArgType, AddressSpace, nullptr, "paarg"); 2374 auto *BitCast = Builder.CreateBitCast( 2375 Alloca, Type::getInt8PtrTy(M->getContext()), UseCall->getName()); 2376 ArgAllocas[AllocArgIndex] = BitCast; 2377 AllocaReplacement = BitCast; 2378 } 2379 2380 UseCall->replaceAllUsesWith(AllocaReplacement); 2381 UseCall->eraseFromParent(); 2382 } 2383 // Remove @llvm.call.preallocated.setup(). 2384 cast<Instruction>(PreallocatedSetup)->eraseFromParent(); 2385 } 2386 } 2387 2388 static bool 2389 OptimizeFunctions(Module &M, 2390 function_ref<TargetLibraryInfo &(Function &)> GetTLI, 2391 function_ref<TargetTransformInfo &(Function &)> GetTTI, 2392 function_ref<BlockFrequencyInfo &(Function &)> GetBFI, 2393 function_ref<DominatorTree &(Function &)> LookupDomTree, 2394 SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) { 2395 2396 bool Changed = false; 2397 2398 std::vector<Function *> AllCallsCold; 2399 for (Module::iterator FI = M.begin(), E = M.end(); FI != E;) { 2400 Function *F = &*FI++; 2401 if (hasOnlyColdCalls(*F, GetBFI)) 2402 AllCallsCold.push_back(F); 2403 } 2404 2405 // Optimize functions. 2406 for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) { 2407 Function *F = &*FI++; 2408 2409 // Don't perform global opt pass on naked functions; we don't want fast 2410 // calling conventions for naked functions. 2411 if (F->hasFnAttribute(Attribute::Naked)) 2412 continue; 2413 2414 // Functions without names cannot be referenced outside this module. 2415 if (!F->hasName() && !F->isDeclaration() && !F->hasLocalLinkage()) 2416 F->setLinkage(GlobalValue::InternalLinkage); 2417 2418 if (deleteIfDead(*F, NotDiscardableComdats)) { 2419 Changed = true; 2420 continue; 2421 } 2422 2423 // LLVM's definition of dominance allows instructions that are cyclic 2424 // in unreachable blocks, e.g.: 2425 // %pat = select i1 %condition, @global, i16* %pat 2426 // because any instruction dominates an instruction in a block that's 2427 // not reachable from entry. 2428 // So, remove unreachable blocks from the function, because a) there's 2429 // no point in analyzing them and b) GlobalOpt should otherwise grow 2430 // some more complicated logic to break these cycles. 2431 // Removing unreachable blocks might invalidate the dominator so we 2432 // recalculate it. 2433 if (!F->isDeclaration()) { 2434 if (removeUnreachableBlocks(*F)) { 2435 auto &DT = LookupDomTree(*F); 2436 DT.recalculate(*F); 2437 Changed = true; 2438 } 2439 } 2440 2441 Changed |= processGlobal(*F, GetTLI, LookupDomTree); 2442 2443 if (!F->hasLocalLinkage()) 2444 continue; 2445 2446 // If we have an inalloca parameter that we can safely remove the 2447 // inalloca attribute from, do so. This unlocks optimizations that 2448 // wouldn't be safe in the presence of inalloca. 2449 // FIXME: We should also hoist alloca affected by this to the entry 2450 // block if possible. 2451 if (F->getAttributes().hasAttrSomewhere(Attribute::InAlloca) && 2452 !F->hasAddressTaken() && !hasMustTailCallers(F)) { 2453 RemoveAttribute(F, Attribute::InAlloca); 2454 Changed = true; 2455 } 2456 2457 // FIXME: handle invokes 2458 // FIXME: handle musttail 2459 if (F->getAttributes().hasAttrSomewhere(Attribute::Preallocated)) { 2460 if (!F->hasAddressTaken() && !hasMustTailCallers(F) && 2461 !hasInvokeCallers(F)) { 2462 RemovePreallocated(F); 2463 Changed = true; 2464 } 2465 continue; 2466 } 2467 2468 if (hasChangeableCC(F) && !F->isVarArg() && !F->hasAddressTaken()) { 2469 NumInternalFunc++; 2470 TargetTransformInfo &TTI = GetTTI(*F); 2471 // Change the calling convention to coldcc if either stress testing is 2472 // enabled or the target would like to use coldcc on functions which are 2473 // cold at all call sites and the callers contain no other non coldcc 2474 // calls. 2475 if (EnableColdCCStressTest || 2476 (TTI.useColdCCForColdCall(*F) && 2477 isValidCandidateForColdCC(*F, GetBFI, AllCallsCold))) { 2478 F->setCallingConv(CallingConv::Cold); 2479 changeCallSitesToColdCC(F); 2480 Changed = true; 2481 NumColdCC++; 2482 } 2483 } 2484 2485 if (hasChangeableCC(F) && !F->isVarArg() && 2486 !F->hasAddressTaken()) { 2487 // If this function has a calling convention worth changing, is not a 2488 // varargs function, and is only called directly, promote it to use the 2489 // Fast calling convention. 2490 F->setCallingConv(CallingConv::Fast); 2491 ChangeCalleesToFastCall(F); 2492 ++NumFastCallFns; 2493 Changed = true; 2494 } 2495 2496 if (F->getAttributes().hasAttrSomewhere(Attribute::Nest) && 2497 !F->hasAddressTaken()) { 2498 // The function is not used by a trampoline intrinsic, so it is safe 2499 // to remove the 'nest' attribute. 2500 RemoveAttribute(F, Attribute::Nest); 2501 ++NumNestRemoved; 2502 Changed = true; 2503 } 2504 } 2505 return Changed; 2506 } 2507 2508 static bool 2509 OptimizeGlobalVars(Module &M, 2510 function_ref<TargetLibraryInfo &(Function &)> GetTLI, 2511 function_ref<DominatorTree &(Function &)> LookupDomTree, 2512 SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) { 2513 bool Changed = false; 2514 2515 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); 2516 GVI != E; ) { 2517 GlobalVariable *GV = &*GVI++; 2518 // Global variables without names cannot be referenced outside this module. 2519 if (!GV->hasName() && !GV->isDeclaration() && !GV->hasLocalLinkage()) 2520 GV->setLinkage(GlobalValue::InternalLinkage); 2521 // Simplify the initializer. 2522 if (GV->hasInitializer()) 2523 if (auto *C = dyn_cast<Constant>(GV->getInitializer())) { 2524 auto &DL = M.getDataLayout(); 2525 // TLI is not used in the case of a Constant, so use default nullptr 2526 // for that optional parameter, since we don't have a Function to 2527 // provide GetTLI anyway. 2528 Constant *New = ConstantFoldConstant(C, DL, /*TLI*/ nullptr); 2529 if (New != C) 2530 GV->setInitializer(New); 2531 } 2532 2533 if (deleteIfDead(*GV, NotDiscardableComdats)) { 2534 Changed = true; 2535 continue; 2536 } 2537 2538 Changed |= processGlobal(*GV, GetTLI, LookupDomTree); 2539 } 2540 return Changed; 2541 } 2542 2543 /// Evaluate a piece of a constantexpr store into a global initializer. This 2544 /// returns 'Init' modified to reflect 'Val' stored into it. At this point, the 2545 /// GEP operands of Addr [0, OpNo) have been stepped into. 2546 static Constant *EvaluateStoreInto(Constant *Init, Constant *Val, 2547 ConstantExpr *Addr, unsigned OpNo) { 2548 // Base case of the recursion. 2549 if (OpNo == Addr->getNumOperands()) { 2550 assert(Val->getType() == Init->getType() && "Type mismatch!"); 2551 return Val; 2552 } 2553 2554 SmallVector<Constant*, 32> Elts; 2555 if (StructType *STy = dyn_cast<StructType>(Init->getType())) { 2556 // Break up the constant into its elements. 2557 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) 2558 Elts.push_back(Init->getAggregateElement(i)); 2559 2560 // Replace the element that we are supposed to. 2561 ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo)); 2562 unsigned Idx = CU->getZExtValue(); 2563 assert(Idx < STy->getNumElements() && "Struct index out of range!"); 2564 Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1); 2565 2566 // Return the modified struct. 2567 return ConstantStruct::get(STy, Elts); 2568 } 2569 2570 ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo)); 2571 uint64_t NumElts; 2572 if (ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) 2573 NumElts = ATy->getNumElements(); 2574 else 2575 NumElts = cast<FixedVectorType>(Init->getType())->getNumElements(); 2576 2577 // Break up the array into elements. 2578 for (uint64_t i = 0, e = NumElts; i != e; ++i) 2579 Elts.push_back(Init->getAggregateElement(i)); 2580 2581 assert(CI->getZExtValue() < NumElts); 2582 Elts[CI->getZExtValue()] = 2583 EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1); 2584 2585 if (Init->getType()->isArrayTy()) 2586 return ConstantArray::get(cast<ArrayType>(Init->getType()), Elts); 2587 return ConstantVector::get(Elts); 2588 } 2589 2590 /// We have decided that Addr (which satisfies the predicate 2591 /// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen. 2592 static void CommitValueTo(Constant *Val, Constant *Addr) { 2593 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) { 2594 assert(GV->hasInitializer()); 2595 GV->setInitializer(Val); 2596 return; 2597 } 2598 2599 ConstantExpr *CE = cast<ConstantExpr>(Addr); 2600 GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); 2601 GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2)); 2602 } 2603 2604 /// Given a map of address -> value, where addresses are expected to be some form 2605 /// of either a global or a constant GEP, set the initializer for the address to 2606 /// be the value. This performs mostly the same function as CommitValueTo() 2607 /// and EvaluateStoreInto() but is optimized to be more efficient for the common 2608 /// case where the set of addresses are GEPs sharing the same underlying global, 2609 /// processing the GEPs in batches rather than individually. 2610 /// 2611 /// To give an example, consider the following C++ code adapted from the clang 2612 /// regression tests: 2613 /// struct S { 2614 /// int n = 10; 2615 /// int m = 2 * n; 2616 /// S(int a) : n(a) {} 2617 /// }; 2618 /// 2619 /// template<typename T> 2620 /// struct U { 2621 /// T *r = &q; 2622 /// T q = 42; 2623 /// U *p = this; 2624 /// }; 2625 /// 2626 /// U<S> e; 2627 /// 2628 /// The global static constructor for 'e' will need to initialize 'r' and 'p' of 2629 /// the outer struct, while also initializing the inner 'q' structs 'n' and 'm' 2630 /// members. This batch algorithm will simply use general CommitValueTo() method 2631 /// to handle the complex nested S struct initialization of 'q', before 2632 /// processing the outermost members in a single batch. Using CommitValueTo() to 2633 /// handle member in the outer struct is inefficient when the struct/array is 2634 /// very large as we end up creating and destroy constant arrays for each 2635 /// initialization. 2636 /// For the above case, we expect the following IR to be generated: 2637 /// 2638 /// %struct.U = type { %struct.S*, %struct.S, %struct.U* } 2639 /// %struct.S = type { i32, i32 } 2640 /// @e = global %struct.U { %struct.S* gep inbounds (%struct.U, %struct.U* @e, 2641 /// i64 0, i32 1), 2642 /// %struct.S { i32 42, i32 84 }, %struct.U* @e } 2643 /// The %struct.S { i32 42, i32 84 } inner initializer is treated as a complex 2644 /// constant expression, while the other two elements of @e are "simple". 2645 static void BatchCommitValueTo(const DenseMap<Constant*, Constant*> &Mem) { 2646 SmallVector<std::pair<GlobalVariable*, Constant*>, 32> GVs; 2647 SmallVector<std::pair<ConstantExpr*, Constant*>, 32> ComplexCEs; 2648 SmallVector<std::pair<ConstantExpr*, Constant*>, 32> SimpleCEs; 2649 SimpleCEs.reserve(Mem.size()); 2650 2651 for (const auto &I : Mem) { 2652 if (auto *GV = dyn_cast<GlobalVariable>(I.first)) { 2653 GVs.push_back(std::make_pair(GV, I.second)); 2654 } else { 2655 ConstantExpr *GEP = cast<ConstantExpr>(I.first); 2656 // We don't handle the deeply recursive case using the batch method. 2657 if (GEP->getNumOperands() > 3) 2658 ComplexCEs.push_back(std::make_pair(GEP, I.second)); 2659 else 2660 SimpleCEs.push_back(std::make_pair(GEP, I.second)); 2661 } 2662 } 2663 2664 // The algorithm below doesn't handle cases like nested structs, so use the 2665 // slower fully general method if we have to. 2666 for (auto ComplexCE : ComplexCEs) 2667 CommitValueTo(ComplexCE.second, ComplexCE.first); 2668 2669 for (auto GVPair : GVs) { 2670 assert(GVPair.first->hasInitializer()); 2671 GVPair.first->setInitializer(GVPair.second); 2672 } 2673 2674 if (SimpleCEs.empty()) 2675 return; 2676 2677 // We cache a single global's initializer elements in the case where the 2678 // subsequent address/val pair uses the same one. This avoids throwing away and 2679 // rebuilding the constant struct/vector/array just because one element is 2680 // modified at a time. 2681 SmallVector<Constant *, 32> Elts; 2682 Elts.reserve(SimpleCEs.size()); 2683 GlobalVariable *CurrentGV = nullptr; 2684 2685 auto commitAndSetupCache = [&](GlobalVariable *GV, bool Update) { 2686 Constant *Init = GV->getInitializer(); 2687 Type *Ty = Init->getType(); 2688 if (Update) { 2689 if (CurrentGV) { 2690 assert(CurrentGV && "Expected a GV to commit to!"); 2691 Type *CurrentInitTy = CurrentGV->getInitializer()->getType(); 2692 // We have a valid cache that needs to be committed. 2693 if (StructType *STy = dyn_cast<StructType>(CurrentInitTy)) 2694 CurrentGV->setInitializer(ConstantStruct::get(STy, Elts)); 2695 else if (ArrayType *ArrTy = dyn_cast<ArrayType>(CurrentInitTy)) 2696 CurrentGV->setInitializer(ConstantArray::get(ArrTy, Elts)); 2697 else 2698 CurrentGV->setInitializer(ConstantVector::get(Elts)); 2699 } 2700 if (CurrentGV == GV) 2701 return; 2702 // Need to clear and set up cache for new initializer. 2703 CurrentGV = GV; 2704 Elts.clear(); 2705 unsigned NumElts; 2706 if (auto *STy = dyn_cast<StructType>(Ty)) 2707 NumElts = STy->getNumElements(); 2708 else if (auto *ATy = dyn_cast<ArrayType>(Ty)) 2709 NumElts = ATy->getNumElements(); 2710 else 2711 NumElts = cast<FixedVectorType>(Ty)->getNumElements(); 2712 for (unsigned i = 0, e = NumElts; i != e; ++i) 2713 Elts.push_back(Init->getAggregateElement(i)); 2714 } 2715 }; 2716 2717 for (auto CEPair : SimpleCEs) { 2718 ConstantExpr *GEP = CEPair.first; 2719 Constant *Val = CEPair.second; 2720 2721 GlobalVariable *GV = cast<GlobalVariable>(GEP->getOperand(0)); 2722 commitAndSetupCache(GV, GV != CurrentGV); 2723 ConstantInt *CI = cast<ConstantInt>(GEP->getOperand(2)); 2724 Elts[CI->getZExtValue()] = Val; 2725 } 2726 // The last initializer in the list needs to be committed, others 2727 // will be committed on a new initializer being processed. 2728 commitAndSetupCache(CurrentGV, true); 2729 } 2730 2731 /// Evaluate static constructors in the function, if we can. Return true if we 2732 /// can, false otherwise. 2733 static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL, 2734 TargetLibraryInfo *TLI) { 2735 // Call the function. 2736 Evaluator Eval(DL, TLI); 2737 Constant *RetValDummy; 2738 bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy, 2739 SmallVector<Constant*, 0>()); 2740 2741 if (EvalSuccess) { 2742 ++NumCtorsEvaluated; 2743 2744 // We succeeded at evaluation: commit the result. 2745 LLVM_DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '" 2746 << F->getName() << "' to " 2747 << Eval.getMutatedMemory().size() << " stores.\n"); 2748 BatchCommitValueTo(Eval.getMutatedMemory()); 2749 for (GlobalVariable *GV : Eval.getInvariants()) 2750 GV->setConstant(true); 2751 } 2752 2753 return EvalSuccess; 2754 } 2755 2756 static int compareNames(Constant *const *A, Constant *const *B) { 2757 Value *AStripped = (*A)->stripPointerCasts(); 2758 Value *BStripped = (*B)->stripPointerCasts(); 2759 return AStripped->getName().compare(BStripped->getName()); 2760 } 2761 2762 static void setUsedInitializer(GlobalVariable &V, 2763 const SmallPtrSetImpl<GlobalValue *> &Init) { 2764 if (Init.empty()) { 2765 V.eraseFromParent(); 2766 return; 2767 } 2768 2769 // Type of pointer to the array of pointers. 2770 PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0); 2771 2772 SmallVector<Constant *, 8> UsedArray; 2773 for (GlobalValue *GV : Init) { 2774 Constant *Cast 2775 = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy); 2776 UsedArray.push_back(Cast); 2777 } 2778 // Sort to get deterministic order. 2779 array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames); 2780 ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size()); 2781 2782 Module *M = V.getParent(); 2783 V.removeFromParent(); 2784 GlobalVariable *NV = 2785 new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage, 2786 ConstantArray::get(ATy, UsedArray), ""); 2787 NV->takeName(&V); 2788 NV->setSection("llvm.metadata"); 2789 delete &V; 2790 } 2791 2792 namespace { 2793 2794 /// An easy to access representation of llvm.used and llvm.compiler.used. 2795 class LLVMUsed { 2796 SmallPtrSet<GlobalValue *, 4> Used; 2797 SmallPtrSet<GlobalValue *, 4> CompilerUsed; 2798 GlobalVariable *UsedV; 2799 GlobalVariable *CompilerUsedV; 2800 2801 public: 2802 LLVMUsed(Module &M) { 2803 SmallVector<GlobalValue *, 4> Vec; 2804 UsedV = collectUsedGlobalVariables(M, Vec, false); 2805 Used = {Vec.begin(), Vec.end()}; 2806 Vec.clear(); 2807 CompilerUsedV = collectUsedGlobalVariables(M, Vec, true); 2808 CompilerUsed = {Vec.begin(), Vec.end()}; 2809 } 2810 2811 using iterator = SmallPtrSet<GlobalValue *, 4>::iterator; 2812 using used_iterator_range = iterator_range<iterator>; 2813 2814 iterator usedBegin() { return Used.begin(); } 2815 iterator usedEnd() { return Used.end(); } 2816 2817 used_iterator_range used() { 2818 return used_iterator_range(usedBegin(), usedEnd()); 2819 } 2820 2821 iterator compilerUsedBegin() { return CompilerUsed.begin(); } 2822 iterator compilerUsedEnd() { return CompilerUsed.end(); } 2823 2824 used_iterator_range compilerUsed() { 2825 return used_iterator_range(compilerUsedBegin(), compilerUsedEnd()); 2826 } 2827 2828 bool usedCount(GlobalValue *GV) const { return Used.count(GV); } 2829 2830 bool compilerUsedCount(GlobalValue *GV) const { 2831 return CompilerUsed.count(GV); 2832 } 2833 2834 bool usedErase(GlobalValue *GV) { return Used.erase(GV); } 2835 bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); } 2836 bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; } 2837 2838 bool compilerUsedInsert(GlobalValue *GV) { 2839 return CompilerUsed.insert(GV).second; 2840 } 2841 2842 void syncVariablesAndSets() { 2843 if (UsedV) 2844 setUsedInitializer(*UsedV, Used); 2845 if (CompilerUsedV) 2846 setUsedInitializer(*CompilerUsedV, CompilerUsed); 2847 } 2848 }; 2849 2850 } // end anonymous namespace 2851 2852 static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) { 2853 if (GA.use_empty()) // No use at all. 2854 return false; 2855 2856 assert((!U.usedCount(&GA) || !U.compilerUsedCount(&GA)) && 2857 "We should have removed the duplicated " 2858 "element from llvm.compiler.used"); 2859 if (!GA.hasOneUse()) 2860 // Strictly more than one use. So at least one is not in llvm.used and 2861 // llvm.compiler.used. 2862 return true; 2863 2864 // Exactly one use. Check if it is in llvm.used or llvm.compiler.used. 2865 return !U.usedCount(&GA) && !U.compilerUsedCount(&GA); 2866 } 2867 2868 static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V, 2869 const LLVMUsed &U) { 2870 unsigned N = 2; 2871 assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) && 2872 "We should have removed the duplicated " 2873 "element from llvm.compiler.used"); 2874 if (U.usedCount(&V) || U.compilerUsedCount(&V)) 2875 ++N; 2876 return V.hasNUsesOrMore(N); 2877 } 2878 2879 static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U) { 2880 if (!GA.hasLocalLinkage()) 2881 return true; 2882 2883 return U.usedCount(&GA) || U.compilerUsedCount(&GA); 2884 } 2885 2886 static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U, 2887 bool &RenameTarget) { 2888 RenameTarget = false; 2889 bool Ret = false; 2890 if (hasUseOtherThanLLVMUsed(GA, U)) 2891 Ret = true; 2892 2893 // If the alias is externally visible, we may still be able to simplify it. 2894 if (!mayHaveOtherReferences(GA, U)) 2895 return Ret; 2896 2897 // If the aliasee has internal linkage, give it the name and linkage 2898 // of the alias, and delete the alias. This turns: 2899 // define internal ... @f(...) 2900 // @a = alias ... @f 2901 // into: 2902 // define ... @a(...) 2903 Constant *Aliasee = GA.getAliasee(); 2904 GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts()); 2905 if (!Target->hasLocalLinkage()) 2906 return Ret; 2907 2908 // Do not perform the transform if multiple aliases potentially target the 2909 // aliasee. This check also ensures that it is safe to replace the section 2910 // and other attributes of the aliasee with those of the alias. 2911 if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U)) 2912 return Ret; 2913 2914 RenameTarget = true; 2915 return true; 2916 } 2917 2918 static bool 2919 OptimizeGlobalAliases(Module &M, 2920 SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) { 2921 bool Changed = false; 2922 LLVMUsed Used(M); 2923 2924 for (GlobalValue *GV : Used.used()) 2925 Used.compilerUsedErase(GV); 2926 2927 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); 2928 I != E;) { 2929 GlobalAlias *J = &*I++; 2930 2931 // Aliases without names cannot be referenced outside this module. 2932 if (!J->hasName() && !J->isDeclaration() && !J->hasLocalLinkage()) 2933 J->setLinkage(GlobalValue::InternalLinkage); 2934 2935 if (deleteIfDead(*J, NotDiscardableComdats)) { 2936 Changed = true; 2937 continue; 2938 } 2939 2940 // If the alias can change at link time, nothing can be done - bail out. 2941 if (J->isInterposable()) 2942 continue; 2943 2944 Constant *Aliasee = J->getAliasee(); 2945 GlobalValue *Target = dyn_cast<GlobalValue>(Aliasee->stripPointerCasts()); 2946 // We can't trivially replace the alias with the aliasee if the aliasee is 2947 // non-trivial in some way. 2948 // TODO: Try to handle non-zero GEPs of local aliasees. 2949 if (!Target) 2950 continue; 2951 Target->removeDeadConstantUsers(); 2952 2953 // Make all users of the alias use the aliasee instead. 2954 bool RenameTarget; 2955 if (!hasUsesToReplace(*J, Used, RenameTarget)) 2956 continue; 2957 2958 J->replaceAllUsesWith(ConstantExpr::getBitCast(Aliasee, J->getType())); 2959 ++NumAliasesResolved; 2960 Changed = true; 2961 2962 if (RenameTarget) { 2963 // Give the aliasee the name, linkage and other attributes of the alias. 2964 Target->takeName(&*J); 2965 Target->setLinkage(J->getLinkage()); 2966 Target->setDSOLocal(J->isDSOLocal()); 2967 Target->setVisibility(J->getVisibility()); 2968 Target->setDLLStorageClass(J->getDLLStorageClass()); 2969 2970 if (Used.usedErase(&*J)) 2971 Used.usedInsert(Target); 2972 2973 if (Used.compilerUsedErase(&*J)) 2974 Used.compilerUsedInsert(Target); 2975 } else if (mayHaveOtherReferences(*J, Used)) 2976 continue; 2977 2978 // Delete the alias. 2979 M.getAliasList().erase(J); 2980 ++NumAliasesRemoved; 2981 Changed = true; 2982 } 2983 2984 Used.syncVariablesAndSets(); 2985 2986 return Changed; 2987 } 2988 2989 static Function * 2990 FindCXAAtExit(Module &M, function_ref<TargetLibraryInfo &(Function &)> GetTLI) { 2991 // Hack to get a default TLI before we have actual Function. 2992 auto FuncIter = M.begin(); 2993 if (FuncIter == M.end()) 2994 return nullptr; 2995 auto *TLI = &GetTLI(*FuncIter); 2996 2997 LibFunc F = LibFunc_cxa_atexit; 2998 if (!TLI->has(F)) 2999 return nullptr; 3000 3001 Function *Fn = M.getFunction(TLI->getName(F)); 3002 if (!Fn) 3003 return nullptr; 3004 3005 // Now get the actual TLI for Fn. 3006 TLI = &GetTLI(*Fn); 3007 3008 // Make sure that the function has the correct prototype. 3009 if (!TLI->getLibFunc(*Fn, F) || F != LibFunc_cxa_atexit) 3010 return nullptr; 3011 3012 return Fn; 3013 } 3014 3015 /// Returns whether the given function is an empty C++ destructor and can 3016 /// therefore be eliminated. 3017 /// Note that we assume that other optimization passes have already simplified 3018 /// the code so we simply check for 'ret'. 3019 static bool cxxDtorIsEmpty(const Function &Fn) { 3020 // FIXME: We could eliminate C++ destructors if they're readonly/readnone and 3021 // nounwind, but that doesn't seem worth doing. 3022 if (Fn.isDeclaration()) 3023 return false; 3024 3025 for (auto &I : Fn.getEntryBlock()) { 3026 if (isa<DbgInfoIntrinsic>(I)) 3027 continue; 3028 if (isa<ReturnInst>(I)) 3029 return true; 3030 break; 3031 } 3032 return false; 3033 } 3034 3035 static bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) { 3036 /// Itanium C++ ABI p3.3.5: 3037 /// 3038 /// After constructing a global (or local static) object, that will require 3039 /// destruction on exit, a termination function is registered as follows: 3040 /// 3041 /// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); 3042 /// 3043 /// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the 3044 /// call f(p) when DSO d is unloaded, before all such termination calls 3045 /// registered before this one. It returns zero if registration is 3046 /// successful, nonzero on failure. 3047 3048 // This pass will look for calls to __cxa_atexit where the function is trivial 3049 // and remove them. 3050 bool Changed = false; 3051 3052 for (auto I = CXAAtExitFn->user_begin(), E = CXAAtExitFn->user_end(); 3053 I != E;) { 3054 // We're only interested in calls. Theoretically, we could handle invoke 3055 // instructions as well, but neither llvm-gcc nor clang generate invokes 3056 // to __cxa_atexit. 3057 CallInst *CI = dyn_cast<CallInst>(*I++); 3058 if (!CI) 3059 continue; 3060 3061 Function *DtorFn = 3062 dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts()); 3063 if (!DtorFn || !cxxDtorIsEmpty(*DtorFn)) 3064 continue; 3065 3066 // Just remove the call. 3067 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); 3068 CI->eraseFromParent(); 3069 3070 ++NumCXXDtorsRemoved; 3071 3072 Changed |= true; 3073 } 3074 3075 return Changed; 3076 } 3077 3078 static bool optimizeGlobalsInModule( 3079 Module &M, const DataLayout &DL, 3080 function_ref<TargetLibraryInfo &(Function &)> GetTLI, 3081 function_ref<TargetTransformInfo &(Function &)> GetTTI, 3082 function_ref<BlockFrequencyInfo &(Function &)> GetBFI, 3083 function_ref<DominatorTree &(Function &)> LookupDomTree) { 3084 SmallPtrSet<const Comdat *, 8> NotDiscardableComdats; 3085 bool Changed = false; 3086 bool LocalChange = true; 3087 while (LocalChange) { 3088 LocalChange = false; 3089 3090 NotDiscardableComdats.clear(); 3091 for (const GlobalVariable &GV : M.globals()) 3092 if (const Comdat *C = GV.getComdat()) 3093 if (!GV.isDiscardableIfUnused() || !GV.use_empty()) 3094 NotDiscardableComdats.insert(C); 3095 for (Function &F : M) 3096 if (const Comdat *C = F.getComdat()) 3097 if (!F.isDefTriviallyDead()) 3098 NotDiscardableComdats.insert(C); 3099 for (GlobalAlias &GA : M.aliases()) 3100 if (const Comdat *C = GA.getComdat()) 3101 if (!GA.isDiscardableIfUnused() || !GA.use_empty()) 3102 NotDiscardableComdats.insert(C); 3103 3104 // Delete functions that are trivially dead, ccc -> fastcc 3105 LocalChange |= OptimizeFunctions(M, GetTLI, GetTTI, GetBFI, LookupDomTree, 3106 NotDiscardableComdats); 3107 3108 // Optimize global_ctors list. 3109 LocalChange |= optimizeGlobalCtorsList(M, [&](Function *F) { 3110 return EvaluateStaticConstructor(F, DL, &GetTLI(*F)); 3111 }); 3112 3113 // Optimize non-address-taken globals. 3114 LocalChange |= 3115 OptimizeGlobalVars(M, GetTLI, LookupDomTree, NotDiscardableComdats); 3116 3117 // Resolve aliases, when possible. 3118 LocalChange |= OptimizeGlobalAliases(M, NotDiscardableComdats); 3119 3120 // Try to remove trivial global destructors if they are not removed 3121 // already. 3122 Function *CXAAtExitFn = FindCXAAtExit(M, GetTLI); 3123 if (CXAAtExitFn) 3124 LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn); 3125 3126 Changed |= LocalChange; 3127 } 3128 3129 // TODO: Move all global ctors functions to the end of the module for code 3130 // layout. 3131 3132 return Changed; 3133 } 3134 3135 PreservedAnalyses GlobalOptPass::run(Module &M, ModuleAnalysisManager &AM) { 3136 auto &DL = M.getDataLayout(); 3137 auto &FAM = 3138 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 3139 auto LookupDomTree = [&FAM](Function &F) -> DominatorTree &{ 3140 return FAM.getResult<DominatorTreeAnalysis>(F); 3141 }; 3142 auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & { 3143 return FAM.getResult<TargetLibraryAnalysis>(F); 3144 }; 3145 auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & { 3146 return FAM.getResult<TargetIRAnalysis>(F); 3147 }; 3148 3149 auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & { 3150 return FAM.getResult<BlockFrequencyAnalysis>(F); 3151 }; 3152 3153 if (!optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetBFI, LookupDomTree)) 3154 return PreservedAnalyses::all(); 3155 return PreservedAnalyses::none(); 3156 } 3157 3158 namespace { 3159 3160 struct GlobalOptLegacyPass : public ModulePass { 3161 static char ID; // Pass identification, replacement for typeid 3162 3163 GlobalOptLegacyPass() : ModulePass(ID) { 3164 initializeGlobalOptLegacyPassPass(*PassRegistry::getPassRegistry()); 3165 } 3166 3167 bool runOnModule(Module &M) override { 3168 if (skipModule(M)) 3169 return false; 3170 3171 auto &DL = M.getDataLayout(); 3172 auto LookupDomTree = [this](Function &F) -> DominatorTree & { 3173 return this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); 3174 }; 3175 auto GetTLI = [this](Function &F) -> TargetLibraryInfo & { 3176 return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 3177 }; 3178 auto GetTTI = [this](Function &F) -> TargetTransformInfo & { 3179 return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 3180 }; 3181 3182 auto GetBFI = [this](Function &F) -> BlockFrequencyInfo & { 3183 return this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); 3184 }; 3185 3186 return optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetBFI, 3187 LookupDomTree); 3188 } 3189 3190 void getAnalysisUsage(AnalysisUsage &AU) const override { 3191 AU.addRequired<TargetLibraryInfoWrapperPass>(); 3192 AU.addRequired<TargetTransformInfoWrapperPass>(); 3193 AU.addRequired<DominatorTreeWrapperPass>(); 3194 AU.addRequired<BlockFrequencyInfoWrapperPass>(); 3195 } 3196 }; 3197 3198 } // end anonymous namespace 3199 3200 char GlobalOptLegacyPass::ID = 0; 3201 3202 INITIALIZE_PASS_BEGIN(GlobalOptLegacyPass, "globalopt", 3203 "Global Variable Optimizer", false, false) 3204 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 3205 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 3206 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) 3207 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 3208 INITIALIZE_PASS_END(GlobalOptLegacyPass, "globalopt", 3209 "Global Variable Optimizer", false, false) 3210 3211 ModulePass *llvm::createGlobalOptimizerPass() { 3212 return new GlobalOptLegacyPass(); 3213 } 3214