LLVM  4.0.0
GlobalStatus.cpp
Go to the documentation of this file.
1 //===-- GlobalStatus.cpp - Compute status info for globals -----------------==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
16 
17 using namespace llvm;
18 
19 /// Return the stronger of the two ordering. If the two orderings are acquire
20 /// and release, then return AcquireRelease.
21 ///
26  return (AtomicOrdering)std::max((unsigned)X, (unsigned)Y);
27 }
28 
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.
32 ///
34  if (isa<GlobalValue>(C))
35  return false;
36 
37  if (isa<ConstantData>(C))
38  return false;
39 
40  for (const User *U : C->users())
41  if (const Constant *CU = dyn_cast<Constant>(U)) {
42  if (!isSafeToDestroyConstant(CU))
43  return false;
44  } else
45  return false;
46  return true;
47 }
48 
49 static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
51  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
52  if (GV->isExternallyInitialized())
54 
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;
59 
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()))
63  return true;
64 
65  if (analyzeGlobalAux(CE, GS, PhiUsers))
66  return true;
67  } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
69  const Function *F = I->getParent()->getParent();
70  if (!GS.AccessingFunction)
71  GS.AccessingFunction = F;
72  else if (GS.AccessingFunction != F)
74  }
75  if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
76  GS.IsLoaded = true;
77  // Don't hack on volatile loads.
78  if (LI->isVolatile())
79  return true;
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)
84  return true;
85 
86  // Don't hack on volatile stores.
87  if (SI->isVolatile())
88  return true;
89 
90  GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
91 
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
94  // stores.
95  if (GS.StoredType != GlobalStatus::Stored) {
96  if (const GlobalVariable *GV =
97  dyn_cast<GlobalVariable>(SI->getOperand(1))) {
98  Value *StoredVal = SI->getOperand(0);
99 
100  if (Constant *C = dyn_cast<Constant>(StoredVal)) {
101  if (C->isThreadDependent()) {
102  // The stored value changes between threads; don't track it.
103  return true;
104  }
105  }
106 
107  if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
110  } else if (isa<LoadInst>(StoredVal) &&
111  cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
114  } else if (GS.StoredType < GlobalStatus::StoredOnce) {
116  GS.StoredOnceValue = StoredVal;
117  } else if (GS.StoredType == GlobalStatus::StoredOnce &&
118  GS.StoredOnceValue == StoredVal) {
119  // noop.
120  } else {
122  }
123  } else {
125  }
126  }
127  } else if (isa<BitCastInst>(I)) {
128  if (analyzeGlobalAux(I, GS, PhiUsers))
129  return true;
130  } else if (isa<GetElementPtrInst>(I)) {
131  if (analyzeGlobalAux(I, GS, PhiUsers))
132  return true;
133  } else if (isa<SelectInst>(I)) {
134  if (analyzeGlobalAux(I, GS, PhiUsers))
135  return true;
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))
141  return true;
142  } else if (isa<CmpInst>(I)) {
143  GS.IsCompared = true;
144  } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
145  if (MTI->isVolatile())
146  return true;
147  if (MTI->getArgOperand(0) == V)
149  if (MTI->getArgOperand(1) == V)
150  GS.IsLoaded = true;
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())
154  return true;
156  } else if (auto C = ImmutableCallSite(I)) {
157  if (!C.isCallee(&U))
158  return true;
159  GS.IsLoaded = true;
160  } else {
161  return true; // Any other non-load instruction might take address!
162  }
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.
167  return true;
168  } else {
169  GS.HasNonInstructionUser = true;
170  // Otherwise must be some other user.
171  return true;
172  }
173  }
174 
175  return false;
176 }
177 
180  return analyzeGlobalAux(V, GS, PhiUsers);
181 }
182 
184  : IsCompared(false), IsLoaded(false), StoredType(NotStored),
185  StoredOnceValue(nullptr), AccessingFunction(nullptr),
186  HasMultipleAccessingFunctions(false), HasNonInstructionUser(false),
187  Ordering(AtomicOrdering::NotAtomic) {}
bool IsLoaded
True if the global is ever loaded.
Definition: GlobalStatus.h:34
iterator_range< use_iterator > uses()
Definition: Value.h:326
Value * StoredOnceValue
If only one value (besides the initializer constant) is ever stored to this global, keep track of what value it is.
Definition: GlobalStatus.h:58
const Function * AccessingFunction
These start out null/false.
Definition: GlobalStatus.h:63
bool HasMultipleAccessingFunctions
Definition: GlobalStatus.h:64
This class wraps the llvm.memset intrinsic.
An instruction for reading from memory.
Definition: Instructions.h:164
This global is stored to, but the only thing stored is the constant it was initialized with...
Definition: GlobalStatus.h:43
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:345
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
StoredType
Keep track of what stores to the global look like.
Definition: GlobalStatus.h:37
bool HasNonInstructionUser
Set to true if this global has a user that is not an instruction (e.g.
Definition: GlobalStatus.h:68
AtomicOrdering
Atomic ordering for LLVM's memory model.
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:873
#define F(x, y, z)
Definition: MD5.cpp:51
Function Alias Analysis false
An instruction for storing to memory.
Definition: Instructions.h:300
This global is stored to, but only its initializer and one other value is ever stored to it...
Definition: GlobalStatus.h:49
static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y)
Return the stronger of the two ordering.
As we analyze each global, keep track of some information about it.
Definition: GlobalStatus.h:28
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
This is an important base class in LLVM.
Definition: Constant.h:42
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:368
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
static bool analyzeGlobal(const Value *V, GlobalStatus &GS)
Look at all uses of the global and fill in the GlobalStatus structure.
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
This global is stored to by multiple values or something else that we cannot track.
Definition: GlobalStatus.h:53
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
iterator_range< user_iterator > users()
Definition: Value.h:370
This class wraps the llvm.memcpy/memmove intrinsics.
bool IsCompared
True if the global's address is used in a comparison.
Definition: GlobalStatus.h:30
AtomicOrdering Ordering
Set to the strongest atomic ordering requirement.
Definition: GlobalStatus.h:71
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:665
#define I(x, y, z)
Definition: MD5.cpp:54
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS, SmallPtrSetImpl< const PHINode * > &PhiUsers)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")