1 //===-- GlobalStatus.cpp - Compute status info for globals -----------------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/SmallPtrSet.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/CallSite.h"
13 #include "llvm/IR/GlobalVariable.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/Transforms/Utils/GlobalStatus.h"
19 /// Return the stronger of the two ordering. If the two orderings are acquire
20 /// and release, then return AcquireRelease.
22 static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
23 if ((X == AtomicOrdering::Acquire && Y == AtomicOrdering::Release) ||
24 (Y == AtomicOrdering::Acquire && X == AtomicOrdering::Release))
25 return AtomicOrdering::AcquireRelease;
26 return (AtomicOrdering)std::max((unsigned)X, (unsigned)Y);
29 /// It is safe to destroy a constant iff it is only used by constants itself.
30 /// Note that constants cannot be cyclic, so this test is pretty easy to
31 /// implement recursively.
33 bool llvm::isSafeToDestroyConstant(const Constant *C) {
34 if (isa<GlobalValue>(C))
37 if (isa<ConstantData>(C))
40 for (const User *U : C->users())
41 if (const Constant *CU = dyn_cast<Constant>(U)) {
42 if (!isSafeToDestroyConstant(CU))
49 static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
50 SmallPtrSetImpl<const PHINode *> &PhiUsers) {
51 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
52 if (GV->isExternallyInitialized())
53 GS.StoredType = GlobalStatus::StoredOnce;
55 for (const Use &U : V->uses()) {
56 const User *UR = U.getUser();
57 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(UR)) {
58 GS.HasNonInstructionUser = true;
60 // If the result of the constantexpr isn't pointer type, then we won't
61 // know to expect it in various places. Just reject early.
62 if (!isa<PointerType>(CE->getType()))
65 if (analyzeGlobalAux(CE, GS, PhiUsers))
67 } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
68 if (!GS.HasMultipleAccessingFunctions) {
69 const Function *F = I->getParent()->getParent();
70 if (!GS.AccessingFunction)
71 GS.AccessingFunction = F;
72 else if (GS.AccessingFunction != F)
73 GS.HasMultipleAccessingFunctions = true;
75 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
77 // Don't hack on volatile loads.
80 GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
81 } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
82 // Don't allow a store OF the address, only stores TO the address.
83 if (SI->getOperand(0) == V)
86 // Don't hack on volatile stores.
90 GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
92 // If this is a direct store to the global (i.e., the global is a scalar
93 // value, not an aggregate), keep more specific information about
95 if (GS.StoredType != GlobalStatus::Stored) {
96 if (const GlobalVariable *GV =
97 dyn_cast<GlobalVariable>(SI->getOperand(1))) {
98 Value *StoredVal = SI->getOperand(0);
100 if (Constant *C = dyn_cast<Constant>(StoredVal)) {
101 if (C->isThreadDependent()) {
102 // The stored value changes between threads; don't track it.
107 if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
108 if (GS.StoredType < GlobalStatus::InitializerStored)
109 GS.StoredType = GlobalStatus::InitializerStored;
110 } else if (isa<LoadInst>(StoredVal) &&
111 cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
112 if (GS.StoredType < GlobalStatus::InitializerStored)
113 GS.StoredType = GlobalStatus::InitializerStored;
114 } else if (GS.StoredType < GlobalStatus::StoredOnce) {
115 GS.StoredType = GlobalStatus::StoredOnce;
116 GS.StoredOnceValue = StoredVal;
117 } else if (GS.StoredType == GlobalStatus::StoredOnce &&
118 GS.StoredOnceValue == StoredVal) {
121 GS.StoredType = GlobalStatus::Stored;
124 GS.StoredType = GlobalStatus::Stored;
127 } else if (isa<BitCastInst>(I)) {
128 if (analyzeGlobalAux(I, GS, PhiUsers))
130 } else if (isa<GetElementPtrInst>(I)) {
131 if (analyzeGlobalAux(I, GS, PhiUsers))
133 } else if (isa<SelectInst>(I)) {
134 if (analyzeGlobalAux(I, GS, PhiUsers))
136 } else if (const PHINode *PN = dyn_cast<PHINode>(I)) {
137 // PHI nodes we can check just like select or GEP instructions, but we
138 // have to be careful about infinite recursion.
139 if (PhiUsers.insert(PN).second) // Not already visited.
140 if (analyzeGlobalAux(I, GS, PhiUsers))
142 } else if (isa<CmpInst>(I)) {
143 GS.IsCompared = true;
144 } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
145 if (MTI->isVolatile())
147 if (MTI->getArgOperand(0) == V)
148 GS.StoredType = GlobalStatus::Stored;
149 if (MTI->getArgOperand(1) == V)
151 } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
152 assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
153 if (MSI->isVolatile())
155 GS.StoredType = GlobalStatus::Stored;
156 } else if (auto C = ImmutableCallSite(I)) {
161 return true; // Any other non-load instruction might take address!
163 } else if (const Constant *C = dyn_cast<Constant>(UR)) {
164 GS.HasNonInstructionUser = true;
165 // We might have a dead and dangling constant hanging off of here.
166 if (!isSafeToDestroyConstant(C))
169 GS.HasNonInstructionUser = true;
170 // Otherwise must be some other user.
178 bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
179 SmallPtrSet<const PHINode *, 16> PhiUsers;
180 return analyzeGlobalAux(V, GS, PhiUsers);
183 GlobalStatus::GlobalStatus()
184 : IsCompared(false), IsLoaded(false), StoredType(NotStored),
185 StoredOnceValue(nullptr), AccessingFunction(nullptr),
186 HasMultipleAccessingFunctions(false), HasNonInstructionUser(false),
187 Ordering(AtomicOrdering::NotAtomic) {}