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.
410b57cec5SDimitry Andric /// Note that constants cannot be cyclic, so this test is pretty easy to
420b57cec5SDimitry Andric /// implement recursively.
430b57cec5SDimitry Andric ///
isSafeToDestroyConstant(const Constant * C)440b57cec5SDimitry Andric bool llvm::isSafeToDestroyConstant(const Constant *C) {
450b57cec5SDimitry Andric   if (isa<GlobalValue>(C))
460b57cec5SDimitry Andric     return false;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   if (isa<ConstantData>(C))
490b57cec5SDimitry Andric     return false;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   for (const User *U : C->users())
520b57cec5SDimitry Andric     if (const Constant *CU = dyn_cast<Constant>(U)) {
530b57cec5SDimitry Andric       if (!isSafeToDestroyConstant(CU))
540b57cec5SDimitry Andric         return false;
550b57cec5SDimitry Andric     } else
560b57cec5SDimitry Andric       return false;
570b57cec5SDimitry Andric   return true;
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
analyzeGlobalAux(const Value * V,GlobalStatus & GS,SmallPtrSetImpl<const Value * > & VisitedUsers)600b57cec5SDimitry Andric static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
610b57cec5SDimitry Andric                              SmallPtrSetImpl<const Value *> &VisitedUsers) {
620b57cec5SDimitry Andric   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
630b57cec5SDimitry Andric     if (GV->isExternallyInitialized())
640b57cec5SDimitry Andric       GS.StoredType = GlobalStatus::StoredOnce;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   for (const Use &U : V->uses()) {
670b57cec5SDimitry Andric     const User *UR = U.getUser();
680b57cec5SDimitry Andric     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(UR)) {
690b57cec5SDimitry Andric       GS.HasNonInstructionUser = true;
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric       // If the result of the constantexpr isn't pointer type, then we won't
720b57cec5SDimitry Andric       // know to expect it in various places.  Just reject early.
730b57cec5SDimitry Andric       if (!isa<PointerType>(CE->getType()))
740b57cec5SDimitry Andric         return true;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric       // FIXME: Do we need to add constexpr selects to VisitedUsers?
770b57cec5SDimitry Andric       if (analyzeGlobalAux(CE, GS, VisitedUsers))
780b57cec5SDimitry Andric         return true;
790b57cec5SDimitry Andric     } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
800b57cec5SDimitry Andric       if (!GS.HasMultipleAccessingFunctions) {
810b57cec5SDimitry Andric         const Function *F = I->getParent()->getParent();
820b57cec5SDimitry Andric         if (!GS.AccessingFunction)
830b57cec5SDimitry Andric           GS.AccessingFunction = F;
840b57cec5SDimitry Andric         else if (GS.AccessingFunction != F)
850b57cec5SDimitry Andric           GS.HasMultipleAccessingFunctions = true;
860b57cec5SDimitry Andric       }
870b57cec5SDimitry Andric       if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
880b57cec5SDimitry Andric         GS.IsLoaded = true;
890b57cec5SDimitry Andric         // Don't hack on volatile loads.
900b57cec5SDimitry Andric         if (LI->isVolatile())
910b57cec5SDimitry Andric           return true;
920b57cec5SDimitry Andric         GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
930b57cec5SDimitry Andric       } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
940b57cec5SDimitry Andric         // Don't allow a store OF the address, only stores TO the address.
950b57cec5SDimitry Andric         if (SI->getOperand(0) == V)
960b57cec5SDimitry Andric           return true;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric         // Don't hack on volatile stores.
990b57cec5SDimitry Andric         if (SI->isVolatile())
1000b57cec5SDimitry Andric           return true;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric         GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric         // If this is a direct store to the global (i.e., the global is a scalar
1050b57cec5SDimitry Andric         // value, not an aggregate), keep more specific information about
1060b57cec5SDimitry Andric         // stores.
1070b57cec5SDimitry Andric         if (GS.StoredType != GlobalStatus::Stored) {
1080b57cec5SDimitry Andric           if (const GlobalVariable *GV =
1090b57cec5SDimitry Andric                   dyn_cast<GlobalVariable>(SI->getOperand(1))) {
1100b57cec5SDimitry Andric             Value *StoredVal = SI->getOperand(0);
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric             if (Constant *C = dyn_cast<Constant>(StoredVal)) {
1130b57cec5SDimitry Andric               if (C->isThreadDependent()) {
1140b57cec5SDimitry Andric                 // The stored value changes between threads; don't track it.
1150b57cec5SDimitry Andric                 return true;
1160b57cec5SDimitry Andric               }
1170b57cec5SDimitry Andric             }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric             if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
1200b57cec5SDimitry Andric               if (GS.StoredType < GlobalStatus::InitializerStored)
1210b57cec5SDimitry Andric                 GS.StoredType = GlobalStatus::InitializerStored;
1220b57cec5SDimitry Andric             } else if (isa<LoadInst>(StoredVal) &&
1230b57cec5SDimitry Andric                        cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
1240b57cec5SDimitry Andric               if (GS.StoredType < GlobalStatus::InitializerStored)
1250b57cec5SDimitry Andric                 GS.StoredType = GlobalStatus::InitializerStored;
1260b57cec5SDimitry Andric             } else if (GS.StoredType < GlobalStatus::StoredOnce) {
1270b57cec5SDimitry Andric               GS.StoredType = GlobalStatus::StoredOnce;
1280b57cec5SDimitry Andric               GS.StoredOnceValue = StoredVal;
1290b57cec5SDimitry Andric             } else if (GS.StoredType == GlobalStatus::StoredOnce &&
1300b57cec5SDimitry Andric                        GS.StoredOnceValue == StoredVal) {
1310b57cec5SDimitry Andric               // noop.
1320b57cec5SDimitry Andric             } else {
1330b57cec5SDimitry Andric               GS.StoredType = GlobalStatus::Stored;
1340b57cec5SDimitry Andric             }
1350b57cec5SDimitry Andric           } else {
1360b57cec5SDimitry Andric             GS.StoredType = GlobalStatus::Stored;
1370b57cec5SDimitry Andric           }
1380b57cec5SDimitry Andric         }
139*af732203SDimitry Andric       } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I) ||
140*af732203SDimitry Andric                  isa<AddrSpaceCastInst>(I)) {
1410b57cec5SDimitry Andric         // Skip over bitcasts and GEPs; we don't care about the type or offset
1420b57cec5SDimitry Andric         // of the pointer.
1430b57cec5SDimitry Andric         if (analyzeGlobalAux(I, GS, VisitedUsers))
1440b57cec5SDimitry Andric           return true;
1450b57cec5SDimitry Andric       } else if (isa<SelectInst>(I) || isa<PHINode>(I)) {
1460b57cec5SDimitry Andric         // Look through selects and PHIs to find if the pointer is
1470b57cec5SDimitry Andric         // conditionally accessed. Make sure we only visit an instruction
1480b57cec5SDimitry Andric         // once; otherwise, we can get infinite recursion or exponential
1490b57cec5SDimitry Andric         // compile time.
1500b57cec5SDimitry Andric         if (VisitedUsers.insert(I).second)
1510b57cec5SDimitry Andric           if (analyzeGlobalAux(I, GS, VisitedUsers))
1520b57cec5SDimitry Andric             return true;
1530b57cec5SDimitry Andric       } else if (isa<CmpInst>(I)) {
1540b57cec5SDimitry Andric         GS.IsCompared = true;
1550b57cec5SDimitry Andric       } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
1560b57cec5SDimitry Andric         if (MTI->isVolatile())
1570b57cec5SDimitry Andric           return true;
1580b57cec5SDimitry Andric         if (MTI->getArgOperand(0) == V)
1590b57cec5SDimitry Andric           GS.StoredType = GlobalStatus::Stored;
1600b57cec5SDimitry Andric         if (MTI->getArgOperand(1) == V)
1610b57cec5SDimitry Andric           GS.IsLoaded = true;
1620b57cec5SDimitry Andric       } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
1630b57cec5SDimitry Andric         assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
1640b57cec5SDimitry Andric         if (MSI->isVolatile())
1650b57cec5SDimitry Andric           return true;
1660b57cec5SDimitry Andric         GS.StoredType = GlobalStatus::Stored;
1675ffd83dbSDimitry Andric       } else if (const auto *CB = dyn_cast<CallBase>(I)) {
1685ffd83dbSDimitry Andric         if (!CB->isCallee(&U))
1690b57cec5SDimitry Andric           return true;
1700b57cec5SDimitry Andric         GS.IsLoaded = true;
1710b57cec5SDimitry Andric       } else {
1720b57cec5SDimitry Andric         return true; // Any other non-load instruction might take address!
1730b57cec5SDimitry Andric       }
1740b57cec5SDimitry Andric     } else if (const Constant *C = dyn_cast<Constant>(UR)) {
1750b57cec5SDimitry Andric       GS.HasNonInstructionUser = true;
1760b57cec5SDimitry Andric       // We might have a dead and dangling constant hanging off of here.
1770b57cec5SDimitry Andric       if (!isSafeToDestroyConstant(C))
1780b57cec5SDimitry Andric         return true;
1790b57cec5SDimitry Andric     } else {
1800b57cec5SDimitry Andric       GS.HasNonInstructionUser = true;
1810b57cec5SDimitry Andric       // Otherwise must be some other user.
1820b57cec5SDimitry Andric       return true;
1830b57cec5SDimitry Andric     }
1840b57cec5SDimitry Andric   }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   return false;
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric GlobalStatus::GlobalStatus() = default;
1900b57cec5SDimitry Andric 
analyzeGlobal(const Value * V,GlobalStatus & GS)1910b57cec5SDimitry Andric bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
1920b57cec5SDimitry Andric   SmallPtrSet<const Value *, 16> VisitedUsers;
1930b57cec5SDimitry Andric   return analyzeGlobalAux(V, GS, VisitedUsers);
1940b57cec5SDimitry Andric }
195