1*0b57cec5SDimitry Andric //===-- GlobalStatus.cpp - Compute status info for globals -----------------==//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/GlobalStatus.h"
10*0b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
11*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
12*0b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
13*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
14*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
15*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
17*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
18*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
19*0b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
20*0b57cec5SDimitry Andric #include "llvm/IR/Use.h"
21*0b57cec5SDimitry Andric #include "llvm/IR/User.h"
22*0b57cec5SDimitry Andric #include "llvm/IR/Value.h"
23*0b57cec5SDimitry Andric #include "llvm/Support/AtomicOrdering.h"
24*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
25*0b57cec5SDimitry Andric #include <algorithm>
26*0b57cec5SDimitry Andric #include <cassert>
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric using namespace llvm;
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric /// Return the stronger of the two ordering. If the two orderings are acquire
31*0b57cec5SDimitry Andric /// and release, then return AcquireRelease.
32*0b57cec5SDimitry Andric ///
strongerOrdering(AtomicOrdering X,AtomicOrdering Y)33*0b57cec5SDimitry Andric static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
34*0b57cec5SDimitry Andric   if ((X == AtomicOrdering::Acquire && Y == AtomicOrdering::Release) ||
35*0b57cec5SDimitry Andric       (Y == AtomicOrdering::Acquire && X == AtomicOrdering::Release))
36*0b57cec5SDimitry Andric     return AtomicOrdering::AcquireRelease;
37*0b57cec5SDimitry Andric   return (AtomicOrdering)std::max((unsigned)X, (unsigned)Y);
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric /// It is safe to destroy a constant iff it is only used by constants itself.
41*0b57cec5SDimitry Andric /// Note that while constants cannot be cyclic, they can be tree-like, so we
42*0b57cec5SDimitry Andric /// should keep a visited set to avoid exponential runtime.
isSafeToDestroyConstant(const Constant * C)43*0b57cec5SDimitry Andric bool llvm::isSafeToDestroyConstant(const Constant *C) {
44*0b57cec5SDimitry Andric   SmallVector<const Constant *, 8> Worklist;
45*0b57cec5SDimitry Andric   SmallPtrSet<const Constant *, 8> Visited;
46*0b57cec5SDimitry Andric   Worklist.push_back(C);
47*0b57cec5SDimitry Andric   while (!Worklist.empty()) {
48*0b57cec5SDimitry Andric     const Constant *C = Worklist.pop_back_val();
49*0b57cec5SDimitry Andric     if (!Visited.insert(C).second)
50*0b57cec5SDimitry Andric       continue;
51*0b57cec5SDimitry Andric     if (isa<GlobalValue>(C) || isa<ConstantData>(C))
52*0b57cec5SDimitry Andric       return false;
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric     for (const User *U : C->users()) {
55*0b57cec5SDimitry Andric       if (const Constant *CU = dyn_cast<Constant>(U))
56*0b57cec5SDimitry Andric         Worklist.push_back(CU);
57*0b57cec5SDimitry Andric       else
58*0b57cec5SDimitry Andric         return false;
59*0b57cec5SDimitry Andric     }
60*0b57cec5SDimitry Andric   }
61*0b57cec5SDimitry Andric   return true;
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric 
analyzeGlobalAux(const Value * V,GlobalStatus & GS,SmallPtrSetImpl<const Value * > & VisitedUsers)64*0b57cec5SDimitry Andric static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS,
65*0b57cec5SDimitry Andric                              SmallPtrSetImpl<const Value *> &VisitedUsers) {
66*0b57cec5SDimitry Andric   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
67*0b57cec5SDimitry Andric     if (GV->isExternallyInitialized())
68*0b57cec5SDimitry Andric       GS.StoredType = GlobalStatus::StoredOnce;
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric   for (const Use &U : V->uses()) {
71*0b57cec5SDimitry Andric     const User *UR = U.getUser();
72*0b57cec5SDimitry Andric     if (const Constant *C = dyn_cast<Constant>(UR)) {
73*0b57cec5SDimitry Andric       const ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
74*0b57cec5SDimitry Andric       if (CE && isa<PointerType>(CE->getType())) {
75*0b57cec5SDimitry Andric         // Recursively analyze pointer-typed constant expressions.
76*0b57cec5SDimitry Andric         // FIXME: Do we need to add constexpr selects to VisitedUsers?
77*0b57cec5SDimitry Andric         if (analyzeGlobalAux(CE, GS, VisitedUsers))
78*0b57cec5SDimitry Andric           return true;
79*0b57cec5SDimitry Andric       } else {
80*0b57cec5SDimitry Andric         // Ignore dead constant users.
81*0b57cec5SDimitry Andric         if (!isSafeToDestroyConstant(C))
82*0b57cec5SDimitry Andric           return true;
83*0b57cec5SDimitry Andric       }
84*0b57cec5SDimitry Andric     } else if (const Instruction *I = dyn_cast<Instruction>(UR)) {
85*0b57cec5SDimitry Andric       if (!GS.HasMultipleAccessingFunctions) {
86*0b57cec5SDimitry Andric         const Function *F = I->getParent()->getParent();
87*0b57cec5SDimitry Andric         if (!GS.AccessingFunction)
88*0b57cec5SDimitry Andric           GS.AccessingFunction = F;
89*0b57cec5SDimitry Andric         else if (GS.AccessingFunction != F)
90*0b57cec5SDimitry Andric           GS.HasMultipleAccessingFunctions = true;
91*0b57cec5SDimitry Andric       }
92*0b57cec5SDimitry Andric       if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
93*0b57cec5SDimitry Andric         GS.IsLoaded = true;
94*0b57cec5SDimitry Andric         // Don't hack on volatile loads.
95*0b57cec5SDimitry Andric         if (LI->isVolatile())
96*0b57cec5SDimitry Andric           return true;
97*0b57cec5SDimitry Andric         GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering());
98*0b57cec5SDimitry Andric       } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
99*0b57cec5SDimitry Andric         // Don't allow a store OF the address, only stores TO the address.
100*0b57cec5SDimitry Andric         if (SI->getOperand(0) == V)
101*0b57cec5SDimitry Andric           return true;
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric         // Don't hack on volatile stores.
104*0b57cec5SDimitry Andric         if (SI->isVolatile())
105*0b57cec5SDimitry Andric           return true;
106*0b57cec5SDimitry Andric 
107*0b57cec5SDimitry Andric         ++GS.NumStores;
108*0b57cec5SDimitry Andric 
109*0b57cec5SDimitry Andric         GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering());
110*0b57cec5SDimitry Andric 
111*0b57cec5SDimitry Andric         // If this is a direct store to the global (i.e., the global is a scalar
112*0b57cec5SDimitry Andric         // value, not an aggregate), keep more specific information about
113*0b57cec5SDimitry Andric         // stores.
114*0b57cec5SDimitry Andric         if (GS.StoredType != GlobalStatus::Stored) {
115*0b57cec5SDimitry Andric           const Value *Ptr = SI->getPointerOperand()->stripPointerCasts();
116*0b57cec5SDimitry Andric           if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
117*0b57cec5SDimitry Andric             Value *StoredVal = SI->getOperand(0);
118*0b57cec5SDimitry Andric 
119*0b57cec5SDimitry Andric             if (Constant *C = dyn_cast<Constant>(StoredVal)) {
120*0b57cec5SDimitry Andric               if (C->isThreadDependent()) {
121*0b57cec5SDimitry Andric                 // The stored value changes between threads; don't track it.
122*0b57cec5SDimitry Andric                 return true;
123*0b57cec5SDimitry Andric               }
124*0b57cec5SDimitry Andric             }
125*0b57cec5SDimitry Andric 
126*0b57cec5SDimitry Andric             if (GV->hasInitializer() && StoredVal == GV->getInitializer()) {
127*0b57cec5SDimitry Andric               if (GS.StoredType < GlobalStatus::InitializerStored)
128*0b57cec5SDimitry Andric                 GS.StoredType = GlobalStatus::InitializerStored;
129*0b57cec5SDimitry Andric             } else if (isa<LoadInst>(StoredVal) &&
130*0b57cec5SDimitry Andric                        cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
131*0b57cec5SDimitry Andric               if (GS.StoredType < GlobalStatus::InitializerStored)
132*0b57cec5SDimitry Andric                 GS.StoredType = GlobalStatus::InitializerStored;
133*0b57cec5SDimitry Andric             } else if (GS.StoredType < GlobalStatus::StoredOnce) {
134*0b57cec5SDimitry Andric               GS.StoredType = GlobalStatus::StoredOnce;
135*0b57cec5SDimitry Andric               GS.StoredOnceStore = SI;
136*0b57cec5SDimitry Andric             } else if (GS.StoredType == GlobalStatus::StoredOnce &&
137*0b57cec5SDimitry Andric                        GS.getStoredOnceValue() == StoredVal) {
138*0b57cec5SDimitry Andric               // noop.
139*0b57cec5SDimitry Andric             } else {
140*0b57cec5SDimitry Andric               GS.StoredType = GlobalStatus::Stored;
141*0b57cec5SDimitry Andric             }
142*0b57cec5SDimitry Andric           } else {
143*0b57cec5SDimitry Andric             GS.StoredType = GlobalStatus::Stored;
144*0b57cec5SDimitry Andric           }
145*0b57cec5SDimitry Andric         }
146*0b57cec5SDimitry Andric       } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I) ||
147*0b57cec5SDimitry Andric                  isa<AddrSpaceCastInst>(I)) {
148*0b57cec5SDimitry Andric         // Skip over bitcasts and GEPs; we don't care about the type or offset
149*0b57cec5SDimitry Andric         // of the pointer.
150*0b57cec5SDimitry Andric         if (analyzeGlobalAux(I, GS, VisitedUsers))
151*0b57cec5SDimitry Andric           return true;
152*0b57cec5SDimitry Andric       } else if (isa<SelectInst>(I) || isa<PHINode>(I)) {
153*0b57cec5SDimitry Andric         // Look through selects and PHIs to find if the pointer is
154*0b57cec5SDimitry Andric         // conditionally accessed. Make sure we only visit an instruction
155*0b57cec5SDimitry Andric         // once; otherwise, we can get infinite recursion or exponential
156*0b57cec5SDimitry Andric         // compile time.
157*0b57cec5SDimitry Andric         if (VisitedUsers.insert(I).second)
158*0b57cec5SDimitry Andric           if (analyzeGlobalAux(I, GS, VisitedUsers))
159*0b57cec5SDimitry Andric             return true;
160*0b57cec5SDimitry Andric       } else if (isa<CmpInst>(I)) {
161*0b57cec5SDimitry Andric         GS.IsCompared = true;
162*0b57cec5SDimitry Andric       } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
163*0b57cec5SDimitry Andric         if (MTI->isVolatile())
164*0b57cec5SDimitry Andric           return true;
165*0b57cec5SDimitry Andric         if (MTI->getArgOperand(0) == V)
166*0b57cec5SDimitry Andric           GS.StoredType = GlobalStatus::Stored;
167*0b57cec5SDimitry Andric         if (MTI->getArgOperand(1) == V)
168*0b57cec5SDimitry Andric           GS.IsLoaded = true;
169*0b57cec5SDimitry Andric       } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
170*0b57cec5SDimitry Andric         assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
171*0b57cec5SDimitry Andric         if (MSI->isVolatile())
172*0b57cec5SDimitry Andric           return true;
173*0b57cec5SDimitry Andric         GS.StoredType = GlobalStatus::Stored;
174*0b57cec5SDimitry Andric       } else if (const auto *CB = dyn_cast<CallBase>(I)) {
175*0b57cec5SDimitry Andric         if (!CB->isCallee(&U))
176*0b57cec5SDimitry Andric           return true;
177*0b57cec5SDimitry Andric         GS.IsLoaded = true;
178*0b57cec5SDimitry Andric       } else {
179*0b57cec5SDimitry Andric         return true; // Any other non-load instruction might take address!
180*0b57cec5SDimitry Andric       }
181*0b57cec5SDimitry Andric     } else {
182*0b57cec5SDimitry Andric       // Otherwise must be some other user.
183*0b57cec5SDimitry Andric       return true;
184*0b57cec5SDimitry Andric     }
185*0b57cec5SDimitry Andric   }
186*0b57cec5SDimitry Andric 
187*0b57cec5SDimitry Andric   return false;
188*0b57cec5SDimitry Andric }
189*0b57cec5SDimitry Andric 
190*0b57cec5SDimitry Andric GlobalStatus::GlobalStatus() = default;
191*0b57cec5SDimitry Andric 
analyzeGlobal(const Value * V,GlobalStatus & GS)192*0b57cec5SDimitry Andric bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) {
193*0b57cec5SDimitry Andric   SmallPtrSet<const Value *, 16> VisitedUsers;
194*0b57cec5SDimitry Andric   return analyzeGlobalAux(V, GS, VisitedUsers);
195 }
196