10b57cec5SDimitry Andric //===-- GlobalStatus.cpp - Compute status info for globals -----------------==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/Transforms/Utils/GlobalStatus.h"
100b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
110b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
120b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
130b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
140b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
150b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
160b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
170b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
180b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
190b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
200b57cec5SDimitry Andric #include "llvm/IR/Use.h"
210b57cec5SDimitry Andric #include "llvm/IR/User.h"
220b57cec5SDimitry Andric #include "llvm/IR/Value.h"
230b57cec5SDimitry Andric #include "llvm/Support/AtomicOrdering.h"
240b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
250b57cec5SDimitry Andric #include <algorithm>
260b57cec5SDimitry Andric #include <cassert>
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric /// Return the stronger of the two ordering. If the two orderings are acquire
310b57cec5SDimitry Andric /// and release, then return AcquireRelease.
320b57cec5SDimitry Andric ///
strongerOrdering(AtomicOrdering X,AtomicOrdering Y)330b57cec5SDimitry Andric static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
340b57cec5SDimitry Andric if ((X == AtomicOrdering::Acquire && Y == AtomicOrdering::Release) ||
350b57cec5SDimitry Andric (Y == AtomicOrdering::Acquire && X == AtomicOrdering::Release))
360b57cec5SDimitry Andric return AtomicOrdering::AcquireRelease;
370b57cec5SDimitry Andric return (AtomicOrdering)std::max((unsigned)X, (unsigned)Y);
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric /// It is safe to destroy a constant iff it is only used by constants itself.
4181ad6265SDimitry Andric /// Note that while constants cannot be cyclic, they can be tree-like, so we
4281ad6265SDimitry Andric /// should keep a visited set to avoid exponential runtime.
isSafeToDestroyConstant(const Constant * C)430b57cec5SDimitry Andric bool llvm::isSafeToDestroyConstant(const Constant *C) {
4481ad6265SDimitry Andric SmallVector<const Constant *, 8> Worklist;
4581ad6265SDimitry Andric SmallPtrSet<const Constant *, 8> Visited;
4681ad6265SDimitry Andric Worklist.push_back(C);
4781ad6265SDimitry Andric while (!Worklist.empty()) {
4881ad6265SDimitry Andric const Constant *C = Worklist.pop_back_val();
4981ad6265SDimitry Andric if (!Visited.insert(C).second)
5081ad6265SDimitry Andric continue;
5181ad6265SDimitry Andric if (isa<GlobalValue>(C) || isa<ConstantData>(C))
520b57cec5SDimitry Andric return false;
530b57cec5SDimitry Andric
5481ad6265SDimitry Andric for (const User *U : C->users()) {
5581ad6265SDimitry Andric if (const Constant *CU = dyn_cast<Constant>(U))
5681ad6265SDimitry Andric Worklist.push_back(CU);
5781ad6265SDimitry Andric else
580b57cec5SDimitry Andric return false;
5981ad6265SDimitry Andric }
6081ad6265SDimitry Andric }
610b57cec5SDimitry Andric return true;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric
analyzeGlobalAux(const Value * V,GlobalStatus & GS,SmallPtrSetImpl<const Value * > & VisitedUsers)640b57cec5SDimitry Andric static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
650b57cec5SDimitry Andric SmallPtrSetImpl<const Value *> &VisitedUsers) {
660b57cec5SDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
670b57cec5SDimitry Andric if (GV->isExternallyInitialized())
680b57cec5SDimitry Andric GS.StoredType = GlobalStatus::StoredOnce;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric for (const Use &U : V->uses()) {
710b57cec5SDimitry Andric const User *UR = U.getUser();
721fd87a68SDimitry Andric if (const Constant *C = dyn_cast<Constant>(UR)) {
731fd87a68SDimitry Andric const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
741fd87a68SDimitry Andric if (CE && isa<PointerType>(CE->getType())) {
751fd87a68SDimitry Andric // Recursively analyze pointer-typed constant expressions.
760b57cec5SDimitry Andric // FIXME: Do we need to add constexpr selects to VisitedUsers?
770b57cec5SDimitry Andric if (analyzeGlobalAux(CE, GS, VisitedUsers))
780b57cec5SDimitry Andric return true;
791fd87a68SDimitry Andric } else {
801fd87a68SDimitry Andric // Ignore dead constant users.
811fd87a68SDimitry Andric if (!isSafeToDestroyConstant(C))
821fd87a68SDimitry Andric return true;
831fd87a68SDimitry Andric }
840b57cec5SDimitry Andric } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
850b57cec5SDimitry Andric if (!GS.HasMultipleAccessingFunctions) {
860b57cec5SDimitry Andric const Function *F = I->getParent()->getParent();
870b57cec5SDimitry Andric if (!GS.AccessingFunction)
880b57cec5SDimitry Andric GS.AccessingFunction = F;
890b57cec5SDimitry Andric else if (GS.AccessingFunction != F)
900b57cec5SDimitry Andric GS.HasMultipleAccessingFunctions = true;
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
930b57cec5SDimitry Andric GS.IsLoaded = true;
940b57cec5SDimitry Andric // Don't hack on volatile loads.
950b57cec5SDimitry Andric if (LI->isVolatile())
960b57cec5SDimitry Andric return true;
970b57cec5SDimitry Andric GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
980b57cec5SDimitry Andric } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
990b57cec5SDimitry Andric // Don't allow a store OF the address, only stores TO the address.
1000b57cec5SDimitry Andric if (SI->getOperand(0) == V)
1010b57cec5SDimitry Andric return true;
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric // Don't hack on volatile stores.
1040b57cec5SDimitry Andric if (SI->isVolatile())
1050b57cec5SDimitry Andric return true;
1060b57cec5SDimitry Andric
10781ad6265SDimitry Andric ++GS.NumStores;
10881ad6265SDimitry Andric
1090b57cec5SDimitry Andric GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric // If this is a direct store to the global (i.e., the global is a scalar
1120b57cec5SDimitry Andric // value, not an aggregate), keep more specific information about
1130b57cec5SDimitry Andric // stores.
1140b57cec5SDimitry Andric if (GS.StoredType != GlobalStatus::Stored) {
11504eeddc0SDimitry Andric const Value *Ptr = SI->getPointerOperand()->stripPointerCasts();
116349cc55cSDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
1170b57cec5SDimitry Andric Value *StoredVal = SI->getOperand(0);
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric if (Constant *C = dyn_cast<Constant>(StoredVal)) {
1200b57cec5SDimitry Andric if (C->isThreadDependent()) {
1210b57cec5SDimitry Andric // The stored value changes between threads; don't track it.
1220b57cec5SDimitry Andric return true;
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
1270b57cec5SDimitry Andric if (GS.StoredType < GlobalStatus::InitializerStored)
1280b57cec5SDimitry Andric GS.StoredType = GlobalStatus::InitializerStored;
1290b57cec5SDimitry Andric } else if (isa<LoadInst>(StoredVal) &&
1300b57cec5SDimitry Andric cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
1310b57cec5SDimitry Andric if (GS.StoredType < GlobalStatus::InitializerStored)
1320b57cec5SDimitry Andric GS.StoredType = GlobalStatus::InitializerStored;
1330b57cec5SDimitry Andric } else if (GS.StoredType < GlobalStatus::StoredOnce) {
1340b57cec5SDimitry Andric GS.StoredType = GlobalStatus::StoredOnce;
135349cc55cSDimitry Andric GS.StoredOnceStore = SI;
1360b57cec5SDimitry Andric } else if (GS.StoredType == GlobalStatus::StoredOnce &&
137349cc55cSDimitry Andric GS.getStoredOnceValue() == StoredVal) {
1380b57cec5SDimitry Andric // noop.
1390b57cec5SDimitry Andric } else {
1400b57cec5SDimitry Andric GS.StoredType = GlobalStatus::Stored;
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric } else {
1430b57cec5SDimitry Andric GS.StoredType = GlobalStatus::Stored;
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric }
146e8d8bef9SDimitry Andric } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I) ||
147e8d8bef9SDimitry Andric isa<AddrSpaceCastInst>(I)) {
1480b57cec5SDimitry Andric // Skip over bitcasts and GEPs; we don't care about the type or offset
1490b57cec5SDimitry Andric // of the pointer.
1500b57cec5SDimitry Andric if (analyzeGlobalAux(I, GS, VisitedUsers))
1510b57cec5SDimitry Andric return true;
1520b57cec5SDimitry Andric } else if (isa<SelectInst>(I) || isa<PHINode>(I)) {
1530b57cec5SDimitry Andric // Look through selects and PHIs to find if the pointer is
1540b57cec5SDimitry Andric // conditionally accessed. Make sure we only visit an instruction
1550b57cec5SDimitry Andric // once; otherwise, we can get infinite recursion or exponential
1560b57cec5SDimitry Andric // compile time.
1570b57cec5SDimitry Andric if (VisitedUsers.insert(I).second)
1580b57cec5SDimitry Andric if (analyzeGlobalAux(I, GS, VisitedUsers))
1590b57cec5SDimitry Andric return true;
1600b57cec5SDimitry Andric } else if (isa<CmpInst>(I)) {
1610b57cec5SDimitry Andric GS.IsCompared = true;
1620b57cec5SDimitry Andric } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
1630b57cec5SDimitry Andric if (MTI->isVolatile())
1640b57cec5SDimitry Andric return true;
1650b57cec5SDimitry Andric if (MTI->getArgOperand(0) == V)
1660b57cec5SDimitry Andric GS.StoredType = GlobalStatus::Stored;
1670b57cec5SDimitry Andric if (MTI->getArgOperand(1) == V)
1680b57cec5SDimitry Andric GS.IsLoaded = true;
1690b57cec5SDimitry Andric } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
1700b57cec5SDimitry Andric assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
1710b57cec5SDimitry Andric if (MSI->isVolatile())
1720b57cec5SDimitry Andric return true;
1730b57cec5SDimitry Andric GS.StoredType = GlobalStatus::Stored;
1745ffd83dbSDimitry Andric } else if (const auto *CB = dyn_cast<CallBase>(I)) {
175*0fca6ea1SDimitry Andric if (CB->getIntrinsicID() == Intrinsic::threadlocal_address) {
176*0fca6ea1SDimitry Andric if (analyzeGlobalAux(I, GS, VisitedUsers))
177*0fca6ea1SDimitry Andric return true;
178*0fca6ea1SDimitry Andric } else {
1795ffd83dbSDimitry Andric if (!CB->isCallee(&U))
1800b57cec5SDimitry Andric return true;
1810b57cec5SDimitry Andric GS.IsLoaded = true;
182*0fca6ea1SDimitry Andric }
1830b57cec5SDimitry Andric } else {
1840b57cec5SDimitry Andric return true; // Any other non-load instruction might take address!
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric } else {
1870b57cec5SDimitry Andric // Otherwise must be some other user.
1880b57cec5SDimitry Andric return true;
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric
1920b57cec5SDimitry Andric return false;
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric GlobalStatus::GlobalStatus() = default;
1960b57cec5SDimitry Andric
analyzeGlobal(const Value * V,GlobalStatus & GS)1970b57cec5SDimitry Andric bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
1980b57cec5SDimitry Andric SmallPtrSet<const Value *, 16> VisitedUsers;
1990b57cec5SDimitry Andric return analyzeGlobalAux(V, GS, VisitedUsers);
2000b57cec5SDimitry Andric }
201