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/GlobalVariable.h"
13 #include "llvm/IR/IntrinsicInst.h"
14 #include "llvm/Support/CallSite.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 == Acquire && Y == Release)
24 return AcquireRelease;
25 if (Y == Acquire && X == Release)
26 return AcquireRelease;
27 return (AtomicOrdering)std::max(X, Y);
30 /// It is safe to destroy a constant iff it is only used by constants itself.
31 /// Note that constants cannot be cyclic, so this test is pretty easy to
32 /// implement recursively.
34 bool llvm::isSafeToDestroyConstant(const Constant *C) {
35 if (isa<GlobalValue>(C))
38 for (Value::const_use_iterator UI = C->use_begin(), E = C->use_end(); UI != E;
40 if (const Constant *CU = dyn_cast<Constant>(*UI)) {
41 if (!isSafeToDestroyConstant(CU))
48 static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
49 SmallPtrSet<const PHINode *, 16> &PhiUsers) {
50 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;
53 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
54 GS.HasNonInstructionUser = true;
56 // If the result of the constantexpr isn't pointer type, then we won't
57 // know to expect it in various places. Just reject early.
58 if (!isa<PointerType>(CE->getType()))
61 if (analyzeGlobalAux(CE, GS, PhiUsers))
63 } else if (const Instruction *I = dyn_cast<Instruction>(U)) {
64 if (!GS.HasMultipleAccessingFunctions) {
65 const Function *F = I->getParent()->getParent();
66 if (GS.AccessingFunction == 0)
67 GS.AccessingFunction = F;
68 else if (GS.AccessingFunction != F)
69 GS.HasMultipleAccessingFunctions = true;
71 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
73 // Don't hack on volatile loads.
76 GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
77 } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
78 // Don't allow a store OF the address, only stores TO the address.
79 if (SI->getOperand(0) == V)
82 // Don't hack on volatile stores.
86 GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
88 // If this is a direct store to the global (i.e., the global is a scalar
89 // value, not an aggregate), keep more specific information about
91 if (GS.StoredType != GlobalStatus::Stored) {
92 if (const GlobalVariable *GV =
93 dyn_cast<GlobalVariable>(SI->getOperand(1))) {
94 Value *StoredVal = SI->getOperand(0);
96 if (Constant *C = dyn_cast<Constant>(StoredVal)) {
97 if (C->isThreadDependent()) {
98 // The stored value changes between threads; don't track it.
103 if (StoredVal == GV->getInitializer()) {
104 if (GS.StoredType < GlobalStatus::InitializerStored)
105 GS.StoredType = GlobalStatus::InitializerStored;
106 } else if (isa<LoadInst>(StoredVal) &&
107 cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
108 if (GS.StoredType < GlobalStatus::InitializerStored)
109 GS.StoredType = GlobalStatus::InitializerStored;
110 } else if (GS.StoredType < GlobalStatus::StoredOnce) {
111 GS.StoredType = GlobalStatus::StoredOnce;
112 GS.StoredOnceValue = StoredVal;
113 } else if (GS.StoredType == GlobalStatus::StoredOnce &&
114 GS.StoredOnceValue == StoredVal) {
117 GS.StoredType = GlobalStatus::Stored;
120 GS.StoredType = GlobalStatus::Stored;
123 } else if (isa<BitCastInst>(I)) {
124 if (analyzeGlobalAux(I, GS, PhiUsers))
126 } else if (isa<GetElementPtrInst>(I)) {
127 if (analyzeGlobalAux(I, GS, PhiUsers))
129 } else if (isa<SelectInst>(I)) {
130 if (analyzeGlobalAux(I, GS, PhiUsers))
132 } else if (const PHINode *PN = dyn_cast<PHINode>(I)) {
133 // PHI nodes we can check just like select or GEP instructions, but we
134 // have to be careful about infinite recursion.
135 if (PhiUsers.insert(PN)) // Not already visited.
136 if (analyzeGlobalAux(I, GS, PhiUsers))
138 } else if (isa<CmpInst>(I)) {
139 GS.IsCompared = true;
140 } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
141 if (MTI->isVolatile())
143 if (MTI->getArgOperand(0) == V)
144 GS.StoredType = GlobalStatus::Stored;
145 if (MTI->getArgOperand(1) == V)
147 } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
148 assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
149 if (MSI->isVolatile())
151 GS.StoredType = GlobalStatus::Stored;
152 } else if (ImmutableCallSite C = I) {
157 return true; // Any other non-load instruction might take address!
159 } else if (const Constant *C = dyn_cast<Constant>(U)) {
160 GS.HasNonInstructionUser = true;
161 // We might have a dead and dangling constant hanging off of here.
162 if (!isSafeToDestroyConstant(C))
165 GS.HasNonInstructionUser = true;
166 // Otherwise must be some other user.
174 bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
175 SmallPtrSet<const PHINode *, 16> PhiUsers;
176 return analyzeGlobalAux(V, GS, PhiUsers);
179 GlobalStatus::GlobalStatus()
180 : IsCompared(false), IsLoaded(false), StoredType(NotStored),
181 StoredOnceValue(0), AccessingFunction(0),
182 HasMultipleAccessingFunctions(false), HasNonInstructionUser(false),
183 Ordering(NotAtomic) {}