blob: 74ebcda8355c15dcb79828a75d96638888816da1 [file] [log] [blame]
Rafael Espindola3d7fc252013-10-21 17:14:55 +00001//===-- 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"
Chandler Carruth219b89b2014-03-04 11:01:28 +000012#include "llvm/IR/CallSite.h"
Rafael Espindola3d7fc252013-10-21 17:14:55 +000013#include "llvm/IR/GlobalVariable.h"
14#include "llvm/IR/IntrinsicInst.h"
15#include "llvm/Transforms/Utils/GlobalStatus.h"
16
17using namespace llvm;
18
19/// Return the stronger of the two ordering. If the two orderings are acquire
20/// and release, then return AcquireRelease.
21///
22static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
Davide Italianoc3e0ce82016-10-24 19:41:47 +000023 if ((X == AtomicOrdering::Acquire && Y == AtomicOrdering::Release) ||
24 (Y == AtomicOrdering::Acquire && X == AtomicOrdering::Release))
JF Bastien800f87a2016-04-06 21:19:33 +000025 return AtomicOrdering::AcquireRelease;
26 return (AtomicOrdering)std::max((unsigned)X, (unsigned)Y);
Rafael Espindola3d7fc252013-10-21 17:14:55 +000027}
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///
33bool llvm::isSafeToDestroyConstant(const Constant *C) {
34 if (isa<GlobalValue>(C))
35 return false;
36
Duncan P. N. Exon Smithc82c1142016-09-24 02:30:11 +000037 if (isa<ConstantData>(C))
Bruno Cardoso Lopese2a1fa32014-08-25 17:51:14 +000038 return false;
39
Chandler Carruthcdf47882014-03-09 03:16:01 +000040 for (const User *U : C->users())
41 if (const Constant *CU = dyn_cast<Constant>(U)) {
Rafael Espindola3d7fc252013-10-21 17:14:55 +000042 if (!isSafeToDestroyConstant(CU))
43 return false;
44 } else
45 return false;
46 return true;
47}
48
49static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
Craig Topper71b7b682014-08-21 05:55:13 +000050 SmallPtrSetImpl<const PHINode *> &PhiUsers) {
Oliver Stannard939724c2015-10-12 13:20:52 +000051 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
52 if (GV->isExternallyInitialized())
53 GS.StoredType = GlobalStatus::StoredOnce;
54
Chandler Carruthcdf47882014-03-09 03:16:01 +000055 for (const Use &U : V->uses()) {
56 const User *UR = U.getUser();
57 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(UR)) {
Rafael Espindola3d7fc252013-10-21 17:14:55 +000058 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;
Chandler Carruthcdf47882014-03-09 03:16:01 +000067 } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
Rafael Espindola3d7fc252013-10-21 17:14:55 +000068 if (!GS.HasMultipleAccessingFunctions) {
69 const Function *F = I->getParent()->getParent();
Craig Topperf40110f2014-04-25 05:29:35 +000070 if (!GS.AccessingFunction)
Rafael Espindola3d7fc252013-10-21 17:14:55 +000071 GS.AccessingFunction = F;
72 else if (GS.AccessingFunction != F)
73 GS.HasMultipleAccessingFunctions = true;
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
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000107 if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
Rafael Espindola3d7fc252013-10-21 17:14:55 +0000108 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) {
119 // noop.
120 } else {
121 GS.StoredType = GlobalStatus::Stored;
122 }
123 } else {
124 GS.StoredType = GlobalStatus::Stored;
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.
David Blaikie70573dc2014-11-19 07:49:26 +0000139 if (PhiUsers.insert(PN).second) // Not already visited.
Rafael Espindola3d7fc252013-10-21 17:14:55 +0000140 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)
148 GS.StoredType = GlobalStatus::Stored;
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;
155 GS.StoredType = GlobalStatus::Stored;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000156 } else if (auto C = ImmutableCallSite(I)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000157 if (!C.isCallee(&U))
Rafael Espindola7749d7c2013-10-25 21:29:52 +0000158 return true;
159 GS.IsLoaded = true;
Rafael Espindola3d7fc252013-10-21 17:14:55 +0000160 } else {
161 return true; // Any other non-load instruction might take address!
162 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000163 } else if (const Constant *C = dyn_cast<Constant>(UR)) {
Rafael Espindola3d7fc252013-10-21 17:14:55 +0000164 GS.HasNonInstructionUser = true;
165 // We might have a dead and dangling constant hanging off of here.
166 if (!isSafeToDestroyConstant(C))
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
178bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
179 SmallPtrSet<const PHINode *, 16> PhiUsers;
180 return analyzeGlobalAux(V, GS, PhiUsers);
181}
182
183GlobalStatus::GlobalStatus()
184 : IsCompared(false), IsLoaded(false), StoredType(NotStored),
Craig Topperf40110f2014-04-25 05:29:35 +0000185 StoredOnceValue(nullptr), AccessingFunction(nullptr),
Rafael Espindola3d7fc252013-10-21 17:14:55 +0000186 HasMultipleAccessingFunctions(false), HasNonInstructionUser(false),
JF Bastien800f87a2016-04-06 21:19:33 +0000187 Ordering(AtomicOrdering::NotAtomic) {}