Lines Matching defs:GV
119 static bool isLeakCheckerRoot(GlobalVariable *GV) {
128 if (GV->hasPrivateLinkage())
132 Types.push_back(GV->getValueType());
196 /// This GV is a pointer root. Loop over all users of the global and clean up
200 CleanupPointerRootUsers(GlobalVariable *GV,
217 SmallVector<User *> Worklist(GV->users());
271 GV->removeDeadConstantUsers();
275 /// We just marked GV constant. Loop over all users of the global, cleaning up
278 static bool CleanupConstantGlobalUsers(GlobalVariable *GV,
280 Constant *Init = GV->getInitializer();
281 SmallVector<User *, 8> WorkList(GV->users());
322 if (PtrOp == GV) {
332 if (getUnderlyingObject(MI->getRawDest()) == GV)
342 GV->removeDeadConstantUsers();
358 GlobalVariable *GV, const DataLayout &DL) {
366 AppendUses(GV);
387 if (Ptr != GV || Offset.getActiveBits() >= 64)
400 ConstantFoldLoadFromConst(GV->getInitializer(), Ty, Offset, DL);
403 << *GV << " with type " << *Ty << " at offset "
446 static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV,
451 GV->getDebugInfo(GVs);
526 static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
527 assert(GV->hasLocalLinkage());
531 if (!collectSRATypes(Parts, GV, DL) || Parts.empty())
535 if (Parts.size() == 1 && Parts.begin()->second.Ty == GV->getValueType())
566 if (Offset > DL.getTypeAllocSize(GV->getValueType()))
569 LLVM_DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV << "\n");
573 DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
574 uint64_t VarSize = DL.getTypeSizeInBits(GV->getValueType());
581 *GV->getParent(), Ty, false, GlobalVariable::InternalLinkage,
582 Initializer, GV->getName() + "." + Twine(NameSuffix++), GV,
583 GV->getThreadLocalMode(), GV->getAddressSpace());
585 NGV->copyAttributesFrom(GV);
596 transferSRADebugInfo(GV, NGV, OffsetForTy * 8,
609 AppendUsers(GV);
624 assert(Ptr == GV && "Load/store must be from/to global");
650 GV->removeDeadConstantUsers();
651 GV->eraseFromParent();
712 /// Return true if all uses of any loads from GV will trap if the loaded value
715 static bool allUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
717 Worklist.push_back(GV);
730 if (CE->stripPointerCasts() != GV)
744 /// Get all the loads/store uses for global variable \p GV.
745 static void allUsesOfLoadAndStores(GlobalVariable *GV,
748 Worklist.push_back(GV);
835 GlobalVariable *GV, Constant *LV, const DataLayout &DL,
844 for (User *GlobalUser : llvm::make_early_inc_range(GV->users())) {
856 assert(GlobalUser->getOperand(1) == GV &&
873 LLVM_DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV
881 if (isLeakCheckerRoot(GV)) {
882 Changed |= CleanupPointerRootUsers(GV, GetTLI);
885 CleanupConstantGlobalUsers(GV, DL);
887 if (GV->use_empty()) {
890 GV->eraseFromParent();
919 /// loads of GV as uses of the new global.
921 OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI,
925 LLVM_DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI
929 Type *GlobalType = ArrayType::get(Type::getInt8Ty(GV->getContext()),
935 *GV->getParent(), GlobalType, false, GlobalValue::InternalLinkage,
936 UndefValue::get(GlobalType), GV->getName() + ".body", nullptr,
937 GV->getThreadLocalMode());
956 Type::getInt1Ty(GV->getContext()), false, GlobalValue::InternalLinkage,
957 ConstantInt::getFalse(GV->getContext()), GV->getName() + ".init",
958 GV->getThreadLocalMode(), GV->getAddressSpace());
961 // Loop over all instruction uses of GV, processing them in turn.
963 allUsesOfLoadAndStores(GV, Guses);
969 GV->getContext(),
995 LV = ConstantInt::getFalse(GV->getContext());
998 LV = ConstantInt::getTrue(GV->getContext());
1020 GV->getParent()->insertGlobalVariable(GV->getIterator(), InitBool);
1022 // Now the GV is dead, nuke it and the allocation..
1023 GV->eraseFromParent();
1034 /// Scan the use-list of GV checking to make sure that there are no complex uses
1035 /// of GV. We permit simple things like dereferencing the pointer, but not
1039 const GlobalVariable *GV) {
1056 SI->getPointerOperand()->stripPointerCasts() != GV)
1057 return false; // Storing the pointer not into GV... bad.
1058 continue; // Otherwise, storing through it, or storing into GV... fine.
1077 static bool tryToOptimizeStoreOfAllocationToGlobal(GlobalVariable *GV,
1107 if (!allUsesOfLoadedValueWillTrapIfNull(GV))
1114 if (!valueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV))
1117 OptimizeGlobalAddressOfAllocation(GV, CI, AllocSize, InitVal, DL, TLI);
1124 optimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
1134 if (GV->getInitializer()->getType()->isPointerTy() &&
1135 GV->getInitializer()->isNullValue() &&
1139 GV->getInitializer()->getType()->getPointerAddressSpace())) {
1142 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, GetTLI))
1147 if (tryToOptimizeStoreOfAllocationToGlobal(GV, CI, DL, TLI))
1156 /// At this point, we have learned that the only two values ever stored into GV
1160 static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
1161 Type *GVElType = GV->getValueType();
1163 // If GVElType is already i1, it is already shrunk. If the type of the GV is
1168 if (GVElType == Type::getInt1Ty(GV->getContext()) ||
1175 for (User *U : GV->users()) {
1182 LLVM_DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV << "\n");
1185 GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),
1188 ConstantInt::getFalse(GV->getContext()),
1189 GV->getName()+".b",
1190 GV->getThreadLocalMode(),
1191 GV->getType()->getAddressSpace());
1192 NewGV->copyAttributesFrom(GV);
1193 GV->getParent()->insertGlobalVariable(GV->getIterator(), NewGV);
1195 Constant *InitVal = GV->getInitializer();
1196 assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
1200 GV->getDebugInfo(GVs);
1210 auto *CIInit = dyn_cast<ConstantInt>(GV->getInitializer());
1219 const DataLayout &DL = GV->getDataLayout();
1249 for(auto *GV : GVs)
1250 NewGV->addDebugInfo(GV);
1253 while (!GV->use_empty()) {
1254 Instruction *UI = cast<Instruction>(GV->user_back());
1261 StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
1273 assert(LI->getOperand(0) == GV && "Not a copy!");
1313 NewGV->takeName(GV);
1314 GV->eraseFromParent();
1319 deleteIfDead(GlobalValue &GV,
1322 GV.removeDeadConstantUsers();
1324 if (!GV.isDiscardableIfUnused() && !GV.isDeclaration())
1327 if (const Comdat *C = GV.getComdat())
1328 if (!GV.hasLocalLinkage() && NotDiscardableComdats.count(C))
1332 if (auto *F = dyn_cast<Function>(&GV))
1335 Dead = GV.use_empty();
1339 LLVM_DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n");
1340 if (auto *F = dyn_cast<Function>(&GV)) {
1344 ReplaceableMetadataImpl::SalvageDebugInfo(GV);
1345 GV.eraseFromParent();
1351 const Function *F, GlobalValue *GV,
1353 // Find all uses of GV. We expect them all to be in F, and if we can't
1356 // On each of these uses, identify if the memory that GV points to is
1358 // if the first thing the function does is store to the GV, the GV can
1363 const DataLayout &DL = GV->getDataLayout();
1366 for (auto *U : GV->users()) {
1380 // We have identified all uses of GV into loads and stores. Now check if all
1420 GlobalVariable *GV, const StoreInst *StoredOnceStore,
1430 for (User *U : GV->users()) {
1455 processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,
1459 auto &DL = GV->getDataLayout();
1470 GV->getValueType()->isSingleValueType() &&
1471 GV->getType()->getAddressSpace() == DL.getAllocaAddrSpace() &&
1472 !GV->isExternallyInitialized() &&
1474 isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV,
1476 const DataLayout &DL = GV->getDataLayout();
1478 LLVM_DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n");
1481 Type *ElemTy = GV->getValueType();
1484 nullptr, GV->getName(), FirstI);
1485 if (!isa<UndefValue>(GV->getInitializer()))
1486 new StoreInst(GV->getInitializer(), Alloca, FirstI);
1488 GV->replaceAllUsesWith(Alloca);
1489 GV->eraseFromParent();
1499 LLVM_DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV << "\n");
1501 if (isLeakCheckerRoot(GV)) {
1503 Changed = CleanupPointerRootUsers(GV, GetTLI);
1507 Changed = CleanupConstantGlobalUsers(GV, DL);
1511 if (GV->use_empty()) {
1512 GV->eraseFromParent();
1520 LLVM_DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
1526 assert(!GV->isConstant() && "Expected a non-constant global");
1527 GV->setConstant(true);
1532 Changed |= CleanupConstantGlobalUsers(GV, DL);
1535 if (GV->use_empty()) {
1538 GV->eraseFromParent();
1546 if (!GV->getInitializer()->getType()->isSingleValueType()) {
1547 const DataLayout &DL = GV->getDataLayout();
1548 if (SRAGlobal(GV, DL))
1557 GV->getType()->getAddressSpace());
1566 if (SOVConstant && isa<UndefValue>(GV->getInitializer()) &&
1568 DL.getTypeAllocSize(GV->getValueType()) &&
1570 if (SOVConstant->getType() == GV->getValueType()) {
1572 GV->setInitializer(SOVConstant);
1576 *GV->getParent(), SOVConstant->getType(), GV->isConstant(),
1577 GV->getLinkage(), SOVConstant, "", GV, GV->getThreadLocalMode(),
1578 GV->getAddressSpace());
1579 NGV->takeName(GV);
1580 NGV->copyAttributesFrom(GV);
1581 GV->replaceAllUsesWith(NGV);
1582 GV->eraseFromParent();
1583 GV = NGV;
1587 CleanupConstantGlobalUsers(GV, DL);
1589 if (GV->use_empty()) {
1592 GV->eraseFromParent();
1601 if (optimizeOnceStoredGlobal(GV, StoredOnceValue, DL, GetTLI))
1607 if (forwardStoredOnceStore(GV, GS.StoredOnceStore, LookupDomTree))
1613 (!isa<UndefValue>(GV->getInitializer()) ||
1615 if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
1628 processGlobal(GlobalValue &GV,
1632 if (GV.getName().starts_with("llvm."))
1637 if (GlobalStatus::analyzeGlobal(&GV, GS))
1641 if (!GS.IsCompared && !GV.hasGlobalUnnamedAddr()) {
1642 auto NewUnnamedAddr = GV.hasLocalLinkage() ? GlobalValue::UnnamedAddr::Global
1644 if (NewUnnamedAddr != GV.getUnnamedAddr()) {
1645 GV.setUnnamedAddr(NewUnnamedAddr);
1652 if (!GV.hasLocalLinkage())
1655 auto *GVar = dyn_cast<GlobalVariable>(&GV);
2149 GlobalVariable *GV,
2152 if (!GV->hasInitializer() || !GV->isConstant() || !GV->hasLocalLinkage() ||
2153 !GV->hasGlobalUnnamedAddr())
2156 for (auto *User : GV->users()) {
2168 dyn_cast<ConstantDataArray>(GV->getInitializer());
2192 return tryWidenGlobalArrayAndDests(F, GV, NumBytesToPad, NumBytesToCopy,
2207 for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
2209 if (!GV.hasName() && !GV.isDeclaration() && !GV.hasLocalLinkage())
2210 GV.setLinkage(GlobalValue::InternalLinkage);
2212 if (GV.hasInitializer())
2213 if (auto *C = dyn_cast<Constant>(GV.getInitializer())) {
2220 GV.setInitializer(New);
2223 if (deleteIfDead(GV, NotDiscardableComdats)) {
2230 Changed |= tryWidenGlobalArraysUsedByMemcpy(&GV, GetTTI);
2232 Changed |= processGlobal(GV, GetTTI, GetTLI, LookupDomTree);
2260 for (GlobalVariable *GV : Eval.getInvariants())
2261 GV->setConstant(true);
2290 for (GlobalValue *GV : Init) {
2291 Constant *Cast = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, PtrTy);
2345 bool usedCount(GlobalValue *GV) const { return Used.count(GV); }
2347 bool compilerUsedCount(GlobalValue *GV) const {
2348 return CompilerUsed.count(GV);
2351 bool usedErase(GlobalValue *GV) { return Used.erase(GV); }
2352 bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); }
2353 bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; }
2355 bool compilerUsedInsert(GlobalValue *GV) {
2356 return CompilerUsed.insert(GV).second;
2385 static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {
2386 if (!GV.hasLocalLinkage())
2389 return U.usedCount(&GV) || U.compilerUsedCount(&GV);
2428 for (GlobalValue *GV : Used.used())
2429 Used.compilerUsedErase(GV);
2431 // Return whether GV is explicitly or implicitly dso_local and not replaceable
2433 auto IsModuleLocal = [](GlobalValue &GV) {
2434 return !GlobalValue::isInterposableLinkage(GV.getLinkage()) &&
2435 (GV.isDSOLocal() || GV.isImplicitDSOLocal());
2832 for (const GlobalVariable &GV : M.globals())
2833 if (const Comdat *C = GV.getComdat())
2834 if (!GV.isDiscardableIfUnused() || !GV.use_empty())