1f785676fSDimitry Andric //===-- GlobalStatus.cpp - Compute status info for globals -----------------==//
2f785676fSDimitry Andric //
3f785676fSDimitry Andric // The LLVM Compiler Infrastructure
4f785676fSDimitry Andric //
5f785676fSDimitry Andric // This file is distributed under the University of Illinois Open Source
6f785676fSDimitry Andric // License. See LICENSE.TXT for details.
7f785676fSDimitry Andric //
8f785676fSDimitry Andric //===----------------------------------------------------------------------===//
9f785676fSDimitry Andric
10db17bf38SDimitry Andric #include "llvm/Transforms/Utils/GlobalStatus.h"
11f785676fSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
12f785676fSDimitry Andric #include "llvm/IR/BasicBlock.h"
1391bc56edSDimitry Andric #include "llvm/IR/CallSite.h"
147a7e6055SDimitry Andric #include "llvm/IR/Constant.h"
157a7e6055SDimitry Andric #include "llvm/IR/Constants.h"
167a7e6055SDimitry Andric #include "llvm/IR/GlobalValue.h"
17f785676fSDimitry Andric #include "llvm/IR/GlobalVariable.h"
187a7e6055SDimitry Andric #include "llvm/IR/InstrTypes.h"
197a7e6055SDimitry Andric #include "llvm/IR/Instruction.h"
207a7e6055SDimitry Andric #include "llvm/IR/Instructions.h"
21f785676fSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
227a7e6055SDimitry Andric #include "llvm/IR/Use.h"
237a7e6055SDimitry Andric #include "llvm/IR/User.h"
247a7e6055SDimitry Andric #include "llvm/IR/Value.h"
257a7e6055SDimitry Andric #include "llvm/Support/AtomicOrdering.h"
267a7e6055SDimitry Andric #include "llvm/Support/Casting.h"
277a7e6055SDimitry Andric #include <algorithm>
287a7e6055SDimitry Andric #include <cassert>
29f785676fSDimitry Andric
30f785676fSDimitry Andric using namespace llvm;
31f785676fSDimitry Andric
32f785676fSDimitry Andric /// Return the stronger of the two ordering. If the two orderings are acquire
33f785676fSDimitry Andric /// and release, then return AcquireRelease.
34f785676fSDimitry Andric ///
strongerOrdering(AtomicOrdering X,AtomicOrdering Y)35f785676fSDimitry Andric static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
36d88c1a5aSDimitry Andric if ((X == AtomicOrdering::Acquire && Y == AtomicOrdering::Release) ||
37d88c1a5aSDimitry Andric (Y == AtomicOrdering::Acquire && X == AtomicOrdering::Release))
383ca95b02SDimitry Andric return AtomicOrdering::AcquireRelease;
393ca95b02SDimitry Andric return (AtomicOrdering)std::max((unsigned)X, (unsigned)Y);
40f785676fSDimitry Andric }
41f785676fSDimitry Andric
42f785676fSDimitry Andric /// It is safe to destroy a constant iff it is only used by constants itself.
43f785676fSDimitry Andric /// Note that constants cannot be cyclic, so this test is pretty easy to
44f785676fSDimitry Andric /// implement recursively.
45f785676fSDimitry Andric ///
isSafeToDestroyConstant(const Constant * C)46f785676fSDimitry Andric bool llvm::isSafeToDestroyConstant(const Constant *C) {
47f785676fSDimitry Andric if (isa<GlobalValue>(C))
48f785676fSDimitry Andric return false;
49f785676fSDimitry Andric
50d88c1a5aSDimitry Andric if (isa<ConstantData>(C))
5139d628a0SDimitry Andric return false;
5239d628a0SDimitry Andric
5391bc56edSDimitry Andric for (const User *U : C->users())
5491bc56edSDimitry Andric if (const Constant *CU = dyn_cast<Constant>(U)) {
55f785676fSDimitry Andric if (!isSafeToDestroyConstant(CU))
56f785676fSDimitry Andric return false;
57f785676fSDimitry Andric } else
58f785676fSDimitry Andric return false;
59f785676fSDimitry Andric return true;
60f785676fSDimitry Andric }
61f785676fSDimitry Andric
analyzeGlobalAux(const Value * V,GlobalStatus & GS,SmallPtrSetImpl<const Value * > & VisitedUsers)62f785676fSDimitry Andric static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
63*4ba319b5SDimitry Andric SmallPtrSetImpl<const Value *> &VisitedUsers) {
647d523365SDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
657d523365SDimitry Andric if (GV->isExternallyInitialized())
667d523365SDimitry Andric GS.StoredType = GlobalStatus::StoredOnce;
677d523365SDimitry Andric
6891bc56edSDimitry Andric for (const Use &U : V->uses()) {
6991bc56edSDimitry Andric const User *UR = U.getUser();
7091bc56edSDimitry Andric if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(UR)) {
71f785676fSDimitry Andric GS.HasNonInstructionUser = true;
72f785676fSDimitry Andric
73f785676fSDimitry Andric // If the result of the constantexpr isn't pointer type, then we won't
74f785676fSDimitry Andric // know to expect it in various places. Just reject early.
75f785676fSDimitry Andric if (!isa<PointerType>(CE->getType()))
76f785676fSDimitry Andric return true;
77f785676fSDimitry Andric
78*4ba319b5SDimitry Andric // FIXME: Do we need to add constexpr selects to VisitedUsers?
79*4ba319b5SDimitry Andric if (analyzeGlobalAux(CE, GS, VisitedUsers))
80f785676fSDimitry Andric return true;
8191bc56edSDimitry Andric } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
82f785676fSDimitry Andric if (!GS.HasMultipleAccessingFunctions) {
83f785676fSDimitry Andric const Function *F = I->getParent()->getParent();
8491bc56edSDimitry Andric if (!GS.AccessingFunction)
85f785676fSDimitry Andric GS.AccessingFunction = F;
86f785676fSDimitry Andric else if (GS.AccessingFunction != F)
87f785676fSDimitry Andric GS.HasMultipleAccessingFunctions = true;
88f785676fSDimitry Andric }
89f785676fSDimitry Andric if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
90f785676fSDimitry Andric GS.IsLoaded = true;
91f785676fSDimitry Andric // Don't hack on volatile loads.
92f785676fSDimitry Andric if (LI->isVolatile())
93f785676fSDimitry Andric return true;
94f785676fSDimitry Andric GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
95f785676fSDimitry Andric } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
96f785676fSDimitry Andric // Don't allow a store OF the address, only stores TO the address.
97f785676fSDimitry Andric if (SI->getOperand(0) == V)
98f785676fSDimitry Andric return true;
99f785676fSDimitry Andric
100f785676fSDimitry Andric // Don't hack on volatile stores.
101f785676fSDimitry Andric if (SI->isVolatile())
102f785676fSDimitry Andric return true;
103f785676fSDimitry Andric
104f785676fSDimitry Andric GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
105f785676fSDimitry Andric
106f785676fSDimitry Andric // If this is a direct store to the global (i.e., the global is a scalar
107f785676fSDimitry Andric // value, not an aggregate), keep more specific information about
108f785676fSDimitry Andric // stores.
109f785676fSDimitry Andric if (GS.StoredType != GlobalStatus::Stored) {
110f785676fSDimitry Andric if (const GlobalVariable *GV =
111f785676fSDimitry Andric dyn_cast<GlobalVariable>(SI->getOperand(1))) {
112f785676fSDimitry Andric Value *StoredVal = SI->getOperand(0);
113f785676fSDimitry Andric
114f785676fSDimitry Andric if (Constant *C = dyn_cast<Constant>(StoredVal)) {
115f785676fSDimitry Andric if (C->isThreadDependent()) {
116f785676fSDimitry Andric // The stored value changes between threads; don't track it.
117f785676fSDimitry Andric return true;
118f785676fSDimitry Andric }
119f785676fSDimitry Andric }
120f785676fSDimitry Andric
1213ca95b02SDimitry Andric if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
122f785676fSDimitry Andric if (GS.StoredType < GlobalStatus::InitializerStored)
123f785676fSDimitry Andric GS.StoredType = GlobalStatus::InitializerStored;
124f785676fSDimitry Andric } else if (isa<LoadInst>(StoredVal) &&
125f785676fSDimitry Andric cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
126f785676fSDimitry Andric if (GS.StoredType < GlobalStatus::InitializerStored)
127f785676fSDimitry Andric GS.StoredType = GlobalStatus::InitializerStored;
128f785676fSDimitry Andric } else if (GS.StoredType < GlobalStatus::StoredOnce) {
129f785676fSDimitry Andric GS.StoredType = GlobalStatus::StoredOnce;
130f785676fSDimitry Andric GS.StoredOnceValue = StoredVal;
131f785676fSDimitry Andric } else if (GS.StoredType == GlobalStatus::StoredOnce &&
132f785676fSDimitry Andric GS.StoredOnceValue == StoredVal) {
133f785676fSDimitry Andric // noop.
134f785676fSDimitry Andric } else {
135f785676fSDimitry Andric GS.StoredType = GlobalStatus::Stored;
136f785676fSDimitry Andric }
137f785676fSDimitry Andric } else {
138f785676fSDimitry Andric GS.StoredType = GlobalStatus::Stored;
139f785676fSDimitry Andric }
140f785676fSDimitry Andric }
141*4ba319b5SDimitry Andric } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I)) {
142*4ba319b5SDimitry Andric // Skip over bitcasts and GEPs; we don't care about the type or offset
143*4ba319b5SDimitry Andric // of the pointer.
144*4ba319b5SDimitry Andric if (analyzeGlobalAux(I, GS, VisitedUsers))
145f785676fSDimitry Andric return true;
146*4ba319b5SDimitry Andric } else if (isa<SelectInst>(I) || isa<PHINode>(I)) {
147*4ba319b5SDimitry Andric // Look through selects and PHIs to find if the pointer is
148*4ba319b5SDimitry Andric // conditionally accessed. Make sure we only visit an instruction
149*4ba319b5SDimitry Andric // once; otherwise, we can get infinite recursion or exponential
150*4ba319b5SDimitry Andric // compile time.
151*4ba319b5SDimitry Andric if (VisitedUsers.insert(I).second)
152*4ba319b5SDimitry Andric if (analyzeGlobalAux(I, GS, VisitedUsers))
153f785676fSDimitry Andric return true;
154f785676fSDimitry Andric } else if (isa<CmpInst>(I)) {
155f785676fSDimitry Andric GS.IsCompared = true;
156f785676fSDimitry Andric } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
157f785676fSDimitry Andric if (MTI->isVolatile())
158f785676fSDimitry Andric return true;
159f785676fSDimitry Andric if (MTI->getArgOperand(0) == V)
160f785676fSDimitry Andric GS.StoredType = GlobalStatus::Stored;
161f785676fSDimitry Andric if (MTI->getArgOperand(1) == V)
162f785676fSDimitry Andric GS.IsLoaded = true;
163f785676fSDimitry Andric } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
164f785676fSDimitry Andric assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
165f785676fSDimitry Andric if (MSI->isVolatile())
166f785676fSDimitry Andric return true;
167f785676fSDimitry Andric GS.StoredType = GlobalStatus::Stored;
168ff0cc061SDimitry Andric } else if (auto C = ImmutableCallSite(I)) {
16991bc56edSDimitry Andric if (!C.isCallee(&U))
170f785676fSDimitry Andric return true;
171f785676fSDimitry Andric GS.IsLoaded = true;
172f785676fSDimitry Andric } else {
173f785676fSDimitry Andric return true; // Any other non-load instruction might take address!
174f785676fSDimitry Andric }
17591bc56edSDimitry Andric } else if (const Constant *C = dyn_cast<Constant>(UR)) {
176f785676fSDimitry Andric GS.HasNonInstructionUser = true;
177f785676fSDimitry Andric // We might have a dead and dangling constant hanging off of here.
178f785676fSDimitry Andric if (!isSafeToDestroyConstant(C))
179f785676fSDimitry Andric return true;
180f785676fSDimitry Andric } else {
181f785676fSDimitry Andric GS.HasNonInstructionUser = true;
182f785676fSDimitry Andric // Otherwise must be some other user.
183f785676fSDimitry Andric return true;
184f785676fSDimitry Andric }
185f785676fSDimitry Andric }
186f785676fSDimitry Andric
187f785676fSDimitry Andric return false;
188f785676fSDimitry Andric }
189f785676fSDimitry Andric
1907a7e6055SDimitry Andric GlobalStatus::GlobalStatus() = default;
1917a7e6055SDimitry Andric
analyzeGlobal(const Value * V,GlobalStatus & GS)192f785676fSDimitry Andric bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
193*4ba319b5SDimitry Andric SmallPtrSet<const Value *, 16> VisitedUsers;
194*4ba319b5SDimitry Andric return analyzeGlobalAux(V, GS, VisitedUsers);
195f785676fSDimitry Andric }
196