Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallPtrSet.h" |
| 12 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 13 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | 5fa4396 | 2017-01-14 00:32:38 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Constant.h" |
| 15 | #include "llvm/IR/Constants.h" |
| 16 | #include "llvm/IR/GlobalValue.h" |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 17 | #include "llvm/IR/GlobalVariable.h" |
Eugene Zelenko | 5fa4396 | 2017-01-14 00:32:38 +0000 | [diff] [blame] | 18 | #include "llvm/IR/InstrTypes.h" |
| 19 | #include "llvm/IR/Instruction.h" |
| 20 | #include "llvm/IR/Instructions.h" |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 21 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 5fa4396 | 2017-01-14 00:32:38 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Use.h" |
| 23 | #include "llvm/IR/User.h" |
| 24 | #include "llvm/IR/Value.h" |
| 25 | #include "llvm/Support/AtomicOrdering.h" |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include <algorithm> |
| 28 | #include <cassert> |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | /// Return the stronger of the two ordering. If the two orderings are acquire |
| 33 | /// and release, then return AcquireRelease. |
| 34 | /// |
| 35 | static AtomicOrdering strongerOrdering(AtomicOrdering X, AtomicOrdering Y) { |
Davide Italiano | c3e0ce8 | 2016-10-24 19:41:47 +0000 | [diff] [blame] | 36 | if ((X == AtomicOrdering::Acquire && Y == AtomicOrdering::Release) || |
| 37 | (Y == AtomicOrdering::Acquire && X == AtomicOrdering::Release)) |
JF Bastien | 800f87a | 2016-04-06 21:19:33 +0000 | [diff] [blame] | 38 | return AtomicOrdering::AcquireRelease; |
| 39 | return (AtomicOrdering)std::max((unsigned)X, (unsigned)Y); |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /// It is safe to destroy a constant iff it is only used by constants itself. |
| 43 | /// Note that constants cannot be cyclic, so this test is pretty easy to |
| 44 | /// implement recursively. |
| 45 | /// |
| 46 | bool llvm::isSafeToDestroyConstant(const Constant *C) { |
| 47 | if (isa<GlobalValue>(C)) |
| 48 | return false; |
| 49 | |
Duncan P. N. Exon Smith | c82c114 | 2016-09-24 02:30:11 +0000 | [diff] [blame] | 50 | if (isa<ConstantData>(C)) |
Bruno Cardoso Lopes | e2a1fa3 | 2014-08-25 17:51:14 +0000 | [diff] [blame] | 51 | return false; |
| 52 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 53 | for (const User *U : C->users()) |
| 54 | if (const Constant *CU = dyn_cast<Constant>(U)) { |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 55 | if (!isSafeToDestroyConstant(CU)) |
| 56 | return false; |
| 57 | } else |
| 58 | return false; |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | static bool analyzeGlobalAux(const Value *V, GlobalStatus &GS, |
Eli Friedman | 79d297a | 2018-01-31 20:42:25 +0000 | [diff] [blame] | 63 | SmallPtrSetImpl<const Value *> &VisitedUsers) { |
Oliver Stannard | 939724c | 2015-10-12 13:20:52 +0000 | [diff] [blame] | 64 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 65 | if (GV->isExternallyInitialized()) |
| 66 | GS.StoredType = GlobalStatus::StoredOnce; |
| 67 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 68 | for (const Use &U : V->uses()) { |
| 69 | const User *UR = U.getUser(); |
| 70 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(UR)) { |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 71 | GS.HasNonInstructionUser = true; |
| 72 | |
| 73 | // If the result of the constantexpr isn't pointer type, then we won't |
| 74 | // know to expect it in various places. Just reject early. |
| 75 | if (!isa<PointerType>(CE->getType())) |
| 76 | return true; |
| 77 | |
Eli Friedman | 79d297a | 2018-01-31 20:42:25 +0000 | [diff] [blame] | 78 | // FIXME: Do we need to add constexpr selects to VisitedUsers? |
| 79 | if (analyzeGlobalAux(CE, GS, VisitedUsers)) |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 80 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 81 | } else if (const Instruction *I = dyn_cast<Instruction>(UR)) { |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 82 | if (!GS.HasMultipleAccessingFunctions) { |
| 83 | const Function *F = I->getParent()->getParent(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 84 | if (!GS.AccessingFunction) |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 85 | GS.AccessingFunction = F; |
| 86 | else if (GS.AccessingFunction != F) |
| 87 | GS.HasMultipleAccessingFunctions = true; |
| 88 | } |
| 89 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 90 | GS.IsLoaded = true; |
| 91 | // Don't hack on volatile loads. |
| 92 | if (LI->isVolatile()) |
| 93 | return true; |
| 94 | GS.Ordering = strongerOrdering(GS.Ordering, LI->getOrdering()); |
| 95 | } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 96 | // Don't allow a store OF the address, only stores TO the address. |
| 97 | if (SI->getOperand(0) == V) |
| 98 | return true; |
| 99 | |
| 100 | // Don't hack on volatile stores. |
| 101 | if (SI->isVolatile()) |
| 102 | return true; |
| 103 | |
| 104 | GS.Ordering = strongerOrdering(GS.Ordering, SI->getOrdering()); |
| 105 | |
| 106 | // If this is a direct store to the global (i.e., the global is a scalar |
| 107 | // value, not an aggregate), keep more specific information about |
| 108 | // stores. |
| 109 | if (GS.StoredType != GlobalStatus::Stored) { |
| 110 | if (const GlobalVariable *GV = |
| 111 | dyn_cast<GlobalVariable>(SI->getOperand(1))) { |
| 112 | Value *StoredVal = SI->getOperand(0); |
| 113 | |
| 114 | if (Constant *C = dyn_cast<Constant>(StoredVal)) { |
| 115 | if (C->isThreadDependent()) { |
| 116 | // The stored value changes between threads; don't track it. |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 121 | if (GV->hasInitializer() && StoredVal == GV->getInitializer()) { |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 122 | if (GS.StoredType < GlobalStatus::InitializerStored) |
| 123 | GS.StoredType = GlobalStatus::InitializerStored; |
| 124 | } else if (isa<LoadInst>(StoredVal) && |
| 125 | cast<LoadInst>(StoredVal)->getOperand(0) == GV) { |
| 126 | if (GS.StoredType < GlobalStatus::InitializerStored) |
| 127 | GS.StoredType = GlobalStatus::InitializerStored; |
| 128 | } else if (GS.StoredType < GlobalStatus::StoredOnce) { |
| 129 | GS.StoredType = GlobalStatus::StoredOnce; |
| 130 | GS.StoredOnceValue = StoredVal; |
| 131 | } else if (GS.StoredType == GlobalStatus::StoredOnce && |
| 132 | GS.StoredOnceValue == StoredVal) { |
| 133 | // noop. |
| 134 | } else { |
| 135 | GS.StoredType = GlobalStatus::Stored; |
| 136 | } |
| 137 | } else { |
| 138 | GS.StoredType = GlobalStatus::Stored; |
| 139 | } |
| 140 | } |
Eli Friedman | 79d297a | 2018-01-31 20:42:25 +0000 | [diff] [blame] | 141 | } else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I)) { |
| 142 | // Skip over bitcasts and GEPs; we don't care about the type or offset |
| 143 | // of the pointer. |
| 144 | if (analyzeGlobalAux(I, GS, VisitedUsers)) |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 145 | return true; |
Eli Friedman | 79d297a | 2018-01-31 20:42:25 +0000 | [diff] [blame] | 146 | } else if (isa<SelectInst>(I) || isa<PHINode>(I)) { |
| 147 | // Look through selects and PHIs to find if the pointer is |
| 148 | // conditionally accessed. Make sure we only visit an instruction |
| 149 | // once; otherwise, we can get infinite recursion or exponential |
| 150 | // compile time. |
| 151 | if (VisitedUsers.insert(I).second) |
| 152 | if (analyzeGlobalAux(I, GS, VisitedUsers)) |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 153 | return true; |
| 154 | } else if (isa<CmpInst>(I)) { |
| 155 | GS.IsCompared = true; |
| 156 | } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) { |
| 157 | if (MTI->isVolatile()) |
| 158 | return true; |
| 159 | if (MTI->getArgOperand(0) == V) |
| 160 | GS.StoredType = GlobalStatus::Stored; |
| 161 | if (MTI->getArgOperand(1) == V) |
| 162 | GS.IsLoaded = true; |
| 163 | } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) { |
| 164 | assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!"); |
| 165 | if (MSI->isVolatile()) |
| 166 | return true; |
| 167 | GS.StoredType = GlobalStatus::Stored; |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 168 | } else if (auto C = ImmutableCallSite(I)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 169 | if (!C.isCallee(&U)) |
Rafael Espindola | 7749d7c | 2013-10-25 21:29:52 +0000 | [diff] [blame] | 170 | return true; |
| 171 | GS.IsLoaded = true; |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 172 | } else { |
| 173 | return true; // Any other non-load instruction might take address! |
| 174 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 175 | } else if (const Constant *C = dyn_cast<Constant>(UR)) { |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 176 | GS.HasNonInstructionUser = true; |
| 177 | // We might have a dead and dangling constant hanging off of here. |
| 178 | if (!isSafeToDestroyConstant(C)) |
| 179 | return true; |
| 180 | } else { |
| 181 | GS.HasNonInstructionUser = true; |
| 182 | // Otherwise must be some other user. |
| 183 | return true; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
Eugene Zelenko | 5fa4396 | 2017-01-14 00:32:38 +0000 | [diff] [blame] | 190 | GlobalStatus::GlobalStatus() = default; |
| 191 | |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 192 | bool GlobalStatus::analyzeGlobal(const Value *V, GlobalStatus &GS) { |
Eli Friedman | 79d297a | 2018-01-31 20:42:25 +0000 | [diff] [blame] | 193 | SmallPtrSet<const Value *, 16> VisitedUsers; |
| 194 | return analyzeGlobalAux(V, GS, VisitedUsers); |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 195 | } |