Michael Gottesman | 1e29ca1 | 2013-01-29 05:07:18 +0000 | [diff] [blame] | 1 | //===- ObjCARCContract.cpp - ObjC ARC Optimization ------------------------===// |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 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 | /// \file |
| 10 | /// This file defines late ObjC ARC optimizations. ARC stands for Automatic |
| 11 | /// Reference Counting and is a system for managing reference counts for objects |
| 12 | /// in Objective C. |
| 13 | /// |
Michael Gottesman | 697d8b9 | 2013-02-07 04:12:57 +0000 | [diff] [blame] | 14 | /// This specific file mainly deals with ``contracting'' multiple lower level |
| 15 | /// operations into singular higher level operations through pattern matching. |
| 16 | /// |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 17 | /// WARNING: This file knows about certain library functions. It recognizes them |
| 18 | /// by name, and hardwires knowledge of their semantics. |
| 19 | /// |
| 20 | /// WARNING: This file knows about how certain Objective-C library functions are |
| 21 | /// used. Naive LLVM IR transformations which would otherwise be |
| 22 | /// behavior-preserving may break these assumptions. |
| 23 | /// |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | // TODO: ObjCARCContract could insert PHI nodes when uses aren't |
| 27 | // dominated by single calls. |
| 28 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 29 | #include "ObjCARC.h" |
Michael Gottesman | 0b912b2 | 2013-07-06 01:39:26 +0000 | [diff] [blame] | 30 | #include "ARCRuntimeEntryPoints.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 31 | #include "DependencyAnalysis.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 32 | #include "ProvenanceAnalysis.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Dominators.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 35 | #include "llvm/IR/InlineAsm.h" |
| 36 | #include "llvm/IR/Operator.h" |
Michael Gottesman | 13a5f1a | 2013-01-29 04:51:59 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | using namespace llvm::objcarc; |
| 42 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "objc-arc-contract" |
| 44 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 45 | STATISTIC(NumPeeps, "Number of calls peephole-optimized"); |
| 46 | STATISTIC(NumStoreStrongs, "Number objc_storeStrong calls formed"); |
| 47 | |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // Declarations |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | /// \brief Late ARC optimizations |
| 54 | /// |
| 55 | /// These change the IR in a way that makes it difficult to be analyzed by |
| 56 | /// ObjCARCOpt, so it's run late. |
| 57 | class ObjCARCContract : public FunctionPass { |
| 58 | bool Changed; |
| 59 | AliasAnalysis *AA; |
| 60 | DominatorTree *DT; |
| 61 | ProvenanceAnalysis PA; |
Michael Gottesman | 0b912b2 | 2013-07-06 01:39:26 +0000 | [diff] [blame] | 62 | ARCRuntimeEntryPoints EP; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 63 | |
| 64 | /// A flag indicating whether this optimization pass should run. |
| 65 | bool Run; |
| 66 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 67 | /// The inline asm string to insert between calls and RetainRV calls to make |
| 68 | /// the optimization work on targets which need it. |
John McCall | 3fe604f | 2016-01-27 19:05:08 +0000 | [diff] [blame] | 69 | const MDString *RVInstMarker; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 70 | |
| 71 | /// The set of inserted objc_storeStrong calls. If at the end of walking the |
| 72 | /// function we have found no alloca instructions, these calls can be marked |
| 73 | /// "tail". |
| 74 | SmallPtrSet<CallInst *, 8> StoreStrongCalls; |
| 75 | |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 76 | /// Returns true if we eliminated Inst. |
| 77 | bool tryToPeepholeInstruction(Function &F, Instruction *Inst, |
| 78 | inst_iterator &Iter, |
| 79 | SmallPtrSetImpl<Instruction *> &DepInsts, |
| 80 | SmallPtrSetImpl<const BasicBlock *> &Visited, |
| 81 | bool &TailOkForStoreStrong); |
| 82 | |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 83 | bool optimizeRetainCall(Function &F, Instruction *Retain); |
Michael Gottesman | 214ca90 | 2013-04-29 06:53:53 +0000 | [diff] [blame] | 84 | |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 85 | bool |
| 86 | contractAutorelease(Function &F, Instruction *Autorelease, |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 87 | ARCInstKind Class, |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 88 | SmallPtrSetImpl<Instruction *> &DependingInstructions, |
| 89 | SmallPtrSetImpl<const BasicBlock *> &Visited); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 90 | |
Michael Gottesman | dfa3e4b | 2015-02-19 00:42:34 +0000 | [diff] [blame] | 91 | void tryToContractReleaseIntoStoreStrong(Instruction *Release, |
| 92 | inst_iterator &Iter); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 93 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 94 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 95 | bool doInitialization(Module &M) override; |
| 96 | bool runOnFunction(Function &F) override; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 97 | |
| 98 | public: |
| 99 | static char ID; |
| 100 | ObjCARCContract() : FunctionPass(ID) { |
| 101 | initializeObjCARCContractPass(*PassRegistry::getPassRegistry()); |
| 102 | } |
| 103 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 104 | } |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 105 | |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
| 107 | // Implementation |
| 108 | //===----------------------------------------------------------------------===// |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 109 | |
Michael Gottesman | 214ca90 | 2013-04-29 06:53:53 +0000 | [diff] [blame] | 110 | /// Turn objc_retain into objc_retainAutoreleasedReturnValue if the operand is a |
| 111 | /// return value. We do this late so we do not disrupt the dataflow analysis in |
| 112 | /// ObjCARCOpt. |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 113 | bool ObjCARCContract::optimizeRetainCall(Function &F, Instruction *Retain) { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 114 | ImmutableCallSite CS(GetArgRCIdentityRoot(Retain)); |
Michael Gottesman | 214ca90 | 2013-04-29 06:53:53 +0000 | [diff] [blame] | 115 | const Instruction *Call = CS.getInstruction(); |
| 116 | if (!Call) |
| 117 | return false; |
| 118 | if (Call->getParent() != Retain->getParent()) |
| 119 | return false; |
| 120 | |
| 121 | // Check that the call is next to the retain. |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 122 | BasicBlock::const_iterator I = ++Call->getIterator(); |
| 123 | while (IsNoopInstruction(&*I)) |
| 124 | ++I; |
Michael Gottesman | 214ca90 | 2013-04-29 06:53:53 +0000 | [diff] [blame] | 125 | if (&*I != Retain) |
| 126 | return false; |
| 127 | |
| 128 | // Turn it to an objc_retainAutoreleasedReturnValue. |
| 129 | Changed = true; |
| 130 | ++NumPeeps; |
| 131 | |
| 132 | DEBUG(dbgs() << "Transforming objc_retain => " |
| 133 | "objc_retainAutoreleasedReturnValue since the operand is a " |
| 134 | "return value.\nOld: "<< *Retain << "\n"); |
| 135 | |
| 136 | // We do not have to worry about tail calls/does not throw since |
| 137 | // retain/retainRV have the same properties. |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 138 | Constant *Decl = EP.get(ARCRuntimeEntryPointKind::RetainRV); |
Michael Gottesman | 0b912b2 | 2013-07-06 01:39:26 +0000 | [diff] [blame] | 139 | cast<CallInst>(Retain)->setCalledFunction(Decl); |
Michael Gottesman | 214ca90 | 2013-04-29 06:53:53 +0000 | [diff] [blame] | 140 | |
| 141 | DEBUG(dbgs() << "New: " << *Retain << "\n"); |
| 142 | return true; |
| 143 | } |
| 144 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 145 | /// Merge an autorelease with a retain into a fused call. |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 146 | bool ObjCARCContract::contractAutorelease( |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 147 | Function &F, Instruction *Autorelease, ARCInstKind Class, |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 148 | SmallPtrSetImpl<Instruction *> &DependingInstructions, |
| 149 | SmallPtrSetImpl<const BasicBlock *> &Visited) { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 150 | const Value *Arg = GetArgRCIdentityRoot(Autorelease); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 151 | |
| 152 | // Check that there are no instructions between the retain and the autorelease |
| 153 | // (such as an autorelease_pop) which may change the count. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 154 | CallInst *Retain = nullptr; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 155 | if (Class == ARCInstKind::AutoreleaseRV) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 156 | FindDependencies(RetainAutoreleaseRVDep, Arg, |
| 157 | Autorelease->getParent(), Autorelease, |
| 158 | DependingInstructions, Visited, PA); |
| 159 | else |
| 160 | FindDependencies(RetainAutoreleaseDep, Arg, |
| 161 | Autorelease->getParent(), Autorelease, |
| 162 | DependingInstructions, Visited, PA); |
| 163 | |
| 164 | Visited.clear(); |
| 165 | if (DependingInstructions.size() != 1) { |
| 166 | DependingInstructions.clear(); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | Retain = dyn_cast_or_null<CallInst>(*DependingInstructions.begin()); |
| 171 | DependingInstructions.clear(); |
| 172 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 173 | if (!Retain || GetBasicARCInstKind(Retain) != ARCInstKind::Retain || |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 174 | GetArgRCIdentityRoot(Retain) != Arg) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 175 | return false; |
| 176 | |
| 177 | Changed = true; |
| 178 | ++NumPeeps; |
| 179 | |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 180 | DEBUG(dbgs() << " Fusing retain/autorelease!\n" |
| 181 | " Autorelease:" << *Autorelease << "\n" |
| 182 | " Retain: " << *Retain << "\n"); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 183 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 184 | Constant *Decl = EP.get(Class == ARCInstKind::AutoreleaseRV |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 185 | ? ARCRuntimeEntryPointKind::RetainAutoreleaseRV |
| 186 | : ARCRuntimeEntryPointKind::RetainAutorelease); |
Michael Gottesman | 0b912b2 | 2013-07-06 01:39:26 +0000 | [diff] [blame] | 187 | Retain->setCalledFunction(Decl); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 188 | |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 189 | DEBUG(dbgs() << " New RetainAutorelease: " << *Retain << "\n"); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 190 | |
| 191 | EraseInstruction(Autorelease); |
| 192 | return true; |
| 193 | } |
| 194 | |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 195 | static StoreInst *findSafeStoreForStoreStrongContraction(LoadInst *Load, |
| 196 | Instruction *Release, |
| 197 | ProvenanceAnalysis &PA, |
| 198 | AliasAnalysis *AA) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 199 | StoreInst *Store = nullptr; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 200 | bool SawRelease = false; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 201 | |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 202 | // Get the location associated with Load. |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 203 | MemoryLocation Loc = MemoryLocation::get(Load); |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 204 | |
| 205 | // Walk down to find the store and the release, which may be in either order. |
| 206 | for (auto I = std::next(BasicBlock::iterator(Load)), |
| 207 | E = Load->getParent()->end(); |
| 208 | I != E; ++I) { |
| 209 | // If we found the store we were looking for and saw the release, |
| 210 | // break. There is no more work to be done. |
| 211 | if (Store && SawRelease) |
| 212 | break; |
| 213 | |
| 214 | // Now we know that we have not seen either the store or the release. If I |
Eric Christopher | 572e03a | 2015-06-19 01:53:21 +0000 | [diff] [blame] | 215 | // is the release, mark that we saw the release and continue. |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 216 | Instruction *Inst = &*I; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 217 | if (Inst == Release) { |
| 218 | SawRelease = true; |
| 219 | continue; |
| 220 | } |
| 221 | |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 222 | // Otherwise, we check if Inst is a "good" store. Grab the instruction class |
| 223 | // of Inst. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 224 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 225 | |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 226 | // If Inst is an unrelated retain, we don't care about it. |
| 227 | // |
| 228 | // TODO: This is one area where the optimization could be made more |
| 229 | // aggressive. |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 230 | if (IsRetain(Class)) |
| 231 | continue; |
| 232 | |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 233 | // If we have seen the store, but not the release... |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 234 | if (Store) { |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 235 | // We need to make sure that it is safe to move the release from its |
| 236 | // current position to the store. This implies proving that any |
| 237 | // instruction in between Store and the Release conservatively can not use |
| 238 | // the RCIdentityRoot of Release. If we can prove we can ignore Inst, so |
| 239 | // continue... |
| 240 | if (!CanUse(Inst, Load, PA, Class)) { |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | // Otherwise, be conservative and return nullptr. |
| 245 | return nullptr; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 246 | } |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 247 | |
| 248 | // Ok, now we know we have not seen a store yet. See if Inst can write to |
| 249 | // our load location, if it can not, just ignore the instruction. |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 250 | if (!(AA->getModRefInfo(Inst, Loc) & MRI_Mod)) |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 251 | continue; |
| 252 | |
| 253 | Store = dyn_cast<StoreInst>(Inst); |
| 254 | |
| 255 | // If Inst can, then check if Inst is a simple store. If Inst is not a |
| 256 | // store or a store that is not simple, then we have some we do not |
| 257 | // understand writing to this memory implying we can not move the load |
| 258 | // over the write to any subsequent store that we may find. |
| 259 | if (!Store || !Store->isSimple()) |
| 260 | return nullptr; |
| 261 | |
| 262 | // Then make sure that the pointer we are storing to is Ptr. If so, we |
| 263 | // found our Store! |
| 264 | if (Store->getPointerOperand() == Loc.Ptr) |
| 265 | continue; |
| 266 | |
| 267 | // Otherwise, we have an unknown store to some other ptr that clobbers |
| 268 | // Loc.Ptr. Bail! |
| 269 | return nullptr; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 272 | // If we did not find the store or did not see the release, fail. |
| 273 | if (!Store || !SawRelease) |
| 274 | return nullptr; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 275 | |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 276 | // We succeeded! |
| 277 | return Store; |
| 278 | } |
| 279 | |
| 280 | static Instruction * |
| 281 | findRetainForStoreStrongContraction(Value *New, StoreInst *Store, |
| 282 | Instruction *Release, |
| 283 | ProvenanceAnalysis &PA) { |
| 284 | // Walk up from the Store to find the retain. |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 285 | BasicBlock::iterator I = Store->getIterator(); |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 286 | BasicBlock::iterator Begin = Store->getParent()->begin(); |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 287 | while (I != Begin && GetBasicARCInstKind(&*I) != ARCInstKind::Retain) { |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 288 | Instruction *Inst = &*I; |
| 289 | |
| 290 | // It is only safe to move the retain to the store if we can prove |
| 291 | // conservatively that nothing besides the release can decrement reference |
| 292 | // counts in between the retain and the store. |
| 293 | if (CanDecrementRefCount(Inst, New, PA) && Inst != Release) |
| 294 | return nullptr; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 295 | --I; |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 296 | } |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 297 | Instruction *Retain = &*I; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 298 | if (GetBasicARCInstKind(Retain) != ARCInstKind::Retain) |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 299 | return nullptr; |
| 300 | if (GetArgRCIdentityRoot(Retain) != New) |
| 301 | return nullptr; |
| 302 | return Retain; |
| 303 | } |
| 304 | |
| 305 | /// Attempt to merge an objc_release with a store, load, and objc_retain to form |
| 306 | /// an objc_storeStrong. An objc_storeStrong: |
| 307 | /// |
| 308 | /// objc_storeStrong(i8** %old_ptr, i8* new_value) |
| 309 | /// |
| 310 | /// is equivalent to the following IR sequence: |
| 311 | /// |
| 312 | /// ; Load old value. |
| 313 | /// %old_value = load i8** %old_ptr (1) |
| 314 | /// |
| 315 | /// ; Increment the new value and then release the old value. This must occur |
| 316 | /// ; in order in case old_value releases new_value in its destructor causing |
| 317 | /// ; us to potentially have a dangling ptr. |
| 318 | /// tail call i8* @objc_retain(i8* %new_value) (2) |
| 319 | /// tail call void @objc_release(i8* %old_value) (3) |
| 320 | /// |
| 321 | /// ; Store the new_value into old_ptr |
| 322 | /// store i8* %new_value, i8** %old_ptr (4) |
| 323 | /// |
| 324 | /// The safety of this optimization is based around the following |
| 325 | /// considerations: |
| 326 | /// |
| 327 | /// 1. We are forming the store strong at the store. Thus to perform this |
| 328 | /// optimization it must be safe to move the retain, load, and release to |
| 329 | /// (4). |
| 330 | /// 2. We need to make sure that any re-orderings of (1), (2), (3), (4) are |
| 331 | /// safe. |
| 332 | void ObjCARCContract::tryToContractReleaseIntoStoreStrong(Instruction *Release, |
| 333 | inst_iterator &Iter) { |
| 334 | // See if we are releasing something that we just loaded. |
| 335 | auto *Load = dyn_cast<LoadInst>(GetArgRCIdentityRoot(Release)); |
| 336 | if (!Load || !Load->isSimple()) |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 337 | return; |
Michael Gottesman | 0fc2acc | 2015-02-20 00:02:49 +0000 | [diff] [blame] | 338 | |
| 339 | // For now, require everything to be in one basic block. |
| 340 | BasicBlock *BB = Release->getParent(); |
| 341 | if (Load->getParent() != BB) |
| 342 | return; |
| 343 | |
| 344 | // First scan down the BB from Load, looking for a store of the RCIdentityRoot |
| 345 | // of Load's |
| 346 | StoreInst *Store = |
| 347 | findSafeStoreForStoreStrongContraction(Load, Release, PA, AA); |
| 348 | // If we fail, bail. |
| 349 | if (!Store) |
| 350 | return; |
| 351 | |
| 352 | // Then find what new_value's RCIdentity Root is. |
| 353 | Value *New = GetRCIdentityRoot(Store->getValueOperand()); |
| 354 | |
| 355 | // Then walk up the BB and look for a retain on New without any intervening |
| 356 | // instructions which conservatively might decrement ref counts. |
| 357 | Instruction *Retain = |
| 358 | findRetainForStoreStrongContraction(New, Store, Release, PA); |
| 359 | |
| 360 | // If we fail, bail. |
| 361 | if (!Retain) |
| 362 | return; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 363 | |
| 364 | Changed = true; |
| 365 | ++NumStoreStrongs; |
| 366 | |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 367 | DEBUG( |
| 368 | llvm::dbgs() << " Contracting retain, release into objc_storeStrong.\n" |
| 369 | << " Old:\n" |
| 370 | << " Store: " << *Store << "\n" |
| 371 | << " Release: " << *Release << "\n" |
| 372 | << " Retain: " << *Retain << "\n" |
| 373 | << " Load: " << *Load << "\n"); |
| 374 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 375 | LLVMContext &C = Release->getContext(); |
| 376 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
| 377 | Type *I8XX = PointerType::getUnqual(I8X); |
| 378 | |
| 379 | Value *Args[] = { Load->getPointerOperand(), New }; |
| 380 | if (Args[0]->getType() != I8XX) |
| 381 | Args[0] = new BitCastInst(Args[0], I8XX, "", Store); |
| 382 | if (Args[1]->getType() != I8X) |
| 383 | Args[1] = new BitCastInst(Args[1], I8X, "", Store); |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 384 | Constant *Decl = EP.get(ARCRuntimeEntryPointKind::StoreStrong); |
Michael Gottesman | 0b912b2 | 2013-07-06 01:39:26 +0000 | [diff] [blame] | 385 | CallInst *StoreStrong = CallInst::Create(Decl, Args, "", Store); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 386 | StoreStrong->setDoesNotThrow(); |
| 387 | StoreStrong->setDebugLoc(Store->getDebugLoc()); |
| 388 | |
| 389 | // We can't set the tail flag yet, because we haven't yet determined |
| 390 | // whether there are any escaping allocas. Remember this call, so that |
| 391 | // we can set the tail flag once we know it's safe. |
| 392 | StoreStrongCalls.insert(StoreStrong); |
| 393 | |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 394 | DEBUG(llvm::dbgs() << " New Store Strong: " << *StoreStrong << "\n"); |
| 395 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 396 | if (&*Iter == Store) ++Iter; |
| 397 | Store->eraseFromParent(); |
| 398 | Release->eraseFromParent(); |
| 399 | EraseInstruction(Retain); |
| 400 | if (Load->use_empty()) |
| 401 | Load->eraseFromParent(); |
| 402 | } |
| 403 | |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 404 | bool ObjCARCContract::tryToPeepholeInstruction( |
| 405 | Function &F, Instruction *Inst, inst_iterator &Iter, |
| 406 | SmallPtrSetImpl<Instruction *> &DependingInsts, |
| 407 | SmallPtrSetImpl<const BasicBlock *> &Visited, |
| 408 | bool &TailOkForStoreStrongs) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 409 | // Only these library routines return their argument. In particular, |
| 410 | // objc_retainBlock does not necessarily return its argument. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 411 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 412 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 413 | case ARCInstKind::FusedRetainAutorelease: |
| 414 | case ARCInstKind::FusedRetainAutoreleaseRV: |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 415 | return false; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 416 | case ARCInstKind::Autorelease: |
| 417 | case ARCInstKind::AutoreleaseRV: |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 418 | return contractAutorelease(F, Inst, Class, DependingInsts, Visited); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 419 | case ARCInstKind::Retain: |
Michael Gottesman | 214ca90 | 2013-04-29 06:53:53 +0000 | [diff] [blame] | 420 | // Attempt to convert retains to retainrvs if they are next to function |
| 421 | // calls. |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 422 | if (!optimizeRetainCall(F, Inst)) |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 423 | return false; |
Michael Gottesman | 214ca90 | 2013-04-29 06:53:53 +0000 | [diff] [blame] | 424 | // If we succeed in our optimization, fall through. |
| 425 | // FALLTHROUGH |
John McCall | 3fe604f | 2016-01-27 19:05:08 +0000 | [diff] [blame] | 426 | case ARCInstKind::RetainRV: |
| 427 | case ARCInstKind::ClaimRV: { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 428 | // If we're compiling for a target which needs a special inline-asm |
John McCall | 3fe604f | 2016-01-27 19:05:08 +0000 | [diff] [blame] | 429 | // marker to do the return value optimization, insert it now. |
| 430 | if (!RVInstMarker) |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 431 | return false; |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 432 | BasicBlock::iterator BBI = Inst->getIterator(); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 433 | BasicBlock *InstParent = Inst->getParent(); |
| 434 | |
John McCall | 3fe604f | 2016-01-27 19:05:08 +0000 | [diff] [blame] | 435 | // Step up to see if the call immediately precedes the RV call. |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 436 | // If it's an invoke, we have to cross a block boundary. And we have |
| 437 | // to carefully dodge no-op instructions. |
| 438 | do { |
Duncan P. N. Exon Smith | e9bc579 | 2016-02-21 20:39:50 +0000 | [diff] [blame] | 439 | if (BBI == InstParent->begin()) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 440 | BasicBlock *Pred = InstParent->getSinglePredecessor(); |
| 441 | if (!Pred) |
| 442 | goto decline_rv_optimization; |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 443 | BBI = Pred->getTerminator()->getIterator(); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | --BBI; |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 447 | } while (IsNoopInstruction(&*BBI)); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 448 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 449 | if (&*BBI == GetArgRCIdentityRoot(Inst)) { |
John McCall | 3fe604f | 2016-01-27 19:05:08 +0000 | [diff] [blame] | 450 | DEBUG(dbgs() << "Adding inline asm marker for the return value " |
| 451 | "optimization.\n"); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 452 | Changed = true; |
John McCall | 3fe604f | 2016-01-27 19:05:08 +0000 | [diff] [blame] | 453 | InlineAsm *IA = InlineAsm::get( |
| 454 | FunctionType::get(Type::getVoidTy(Inst->getContext()), |
| 455 | /*isVarArg=*/false), |
| 456 | RVInstMarker->getString(), |
| 457 | /*Constraints=*/"", /*hasSideEffects=*/true); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 458 | CallInst::Create(IA, "", Inst); |
| 459 | } |
| 460 | decline_rv_optimization: |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 461 | return false; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 462 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 463 | case ARCInstKind::InitWeak: { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 464 | // objc_initWeak(p, null) => *p = null |
| 465 | CallInst *CI = cast<CallInst>(Inst); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 466 | if (IsNullOrUndef(CI->getArgOperand(1))) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 467 | Value *Null = |
| 468 | ConstantPointerNull::get(cast<PointerType>(CI->getType())); |
| 469 | Changed = true; |
| 470 | new StoreInst(Null, CI->getArgOperand(0), CI); |
| 471 | |
| 472 | DEBUG(dbgs() << "OBJCARCContract: Old = " << *CI << "\n" |
| 473 | << " New = " << *Null << "\n"); |
| 474 | |
| 475 | CI->replaceAllUsesWith(Null); |
| 476 | CI->eraseFromParent(); |
| 477 | } |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 478 | return true; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 479 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 480 | case ARCInstKind::Release: |
Michael Gottesman | dfa3e4b | 2015-02-19 00:42:34 +0000 | [diff] [blame] | 481 | // Try to form an objc store strong from our release. If we fail, there is |
| 482 | // nothing further to do below, so continue. |
| 483 | tryToContractReleaseIntoStoreStrong(Inst, Iter); |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 484 | return true; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 485 | case ARCInstKind::User: |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 486 | // Be conservative if the function has any alloca instructions. |
| 487 | // Technically we only care about escaping alloca instructions, |
| 488 | // but this is sufficient to handle some interesting cases. |
| 489 | if (isa<AllocaInst>(Inst)) |
| 490 | TailOkForStoreStrongs = false; |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 491 | return true; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 492 | case ARCInstKind::IntrinsicUser: |
John McCall | 20182ac | 2013-03-22 21:38:36 +0000 | [diff] [blame] | 493 | // Remove calls to @clang.arc.use(...). |
| 494 | Inst->eraseFromParent(); |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 495 | return true; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 496 | default: |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 497 | return true; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 498 | } |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 499 | } |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 500 | |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 501 | //===----------------------------------------------------------------------===// |
| 502 | // Top Level Driver |
| 503 | //===----------------------------------------------------------------------===// |
| 504 | |
| 505 | bool ObjCARCContract::runOnFunction(Function &F) { |
| 506 | if (!EnableARCOpts) |
| 507 | return false; |
| 508 | |
| 509 | // If nothing in the Module uses ARC, don't do anything. |
| 510 | if (!Run) |
| 511 | return false; |
| 512 | |
| 513 | Changed = false; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 514 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 515 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 516 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 517 | PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults()); |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 518 | |
| 519 | DEBUG(llvm::dbgs() << "**** ObjCARC Contract ****\n"); |
| 520 | |
| 521 | // Track whether it's ok to mark objc_storeStrong calls with the "tail" |
| 522 | // keyword. Be conservative if the function has variadic arguments. |
| 523 | // It seems that functions which "return twice" are also unsafe for the |
| 524 | // "tail" argument, because they are setjmp, which could need to |
| 525 | // return to an earlier stack state. |
| 526 | bool TailOkForStoreStrongs = |
| 527 | !F.isVarArg() && !F.callsFunctionThatReturnsTwice(); |
| 528 | |
| 529 | // For ObjC library calls which return their argument, replace uses of the |
| 530 | // argument with uses of the call return value, if it dominates the use. This |
| 531 | // reduces register pressure. |
| 532 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 533 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 534 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E;) { |
| 535 | Instruction *Inst = &*I++; |
| 536 | |
| 537 | DEBUG(dbgs() << "Visiting: " << *Inst << "\n"); |
| 538 | |
| 539 | // First try to peephole Inst. If there is nothing further we can do in |
| 540 | // terms of undoing objc-arc-expand, process the next inst. |
| 541 | if (tryToPeepholeInstruction(F, Inst, I, DependingInstructions, Visited, |
| 542 | TailOkForStoreStrongs)) |
| 543 | continue; |
| 544 | |
| 545 | // Otherwise, try to undo objc-arc-expand. |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 546 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 547 | // Don't use GetArgRCIdentityRoot because we don't want to look through bitcasts |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 548 | // and such; to do the replacement, the argument must have type i8*. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 549 | Value *Arg = cast<CallInst>(Inst)->getArgOperand(0); |
Michael Gottesman | 1827973 | 2015-02-19 00:42:30 +0000 | [diff] [blame] | 550 | |
| 551 | // TODO: Change this to a do-while. |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 552 | for (;;) { |
| 553 | // If we're compiling bugpointed code, don't get in trouble. |
| 554 | if (!isa<Instruction>(Arg) && !isa<Argument>(Arg)) |
| 555 | break; |
| 556 | // Look through the uses of the pointer. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 557 | for (Value::use_iterator UI = Arg->use_begin(), UE = Arg->use_end(); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 558 | UI != UE; ) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 559 | // Increment UI now, because we may unlink its element. |
| 560 | Use &U = *UI++; |
| 561 | unsigned OperandNo = U.getOperandNo(); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 562 | |
| 563 | // If the call's return value dominates a use of the call's argument |
| 564 | // value, rewrite the use to use the return value. We check for |
| 565 | // reachability here because an unreachable call is considered to |
| 566 | // trivially dominate itself, which would lead us to rewriting its |
| 567 | // argument in terms of its return value, which would lead to |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 568 | // infinite loops in GetArgRCIdentityRoot. |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 569 | if (DT->isReachableFromEntry(U) && DT->dominates(Inst, U)) { |
| 570 | Changed = true; |
| 571 | Instruction *Replacement = Inst; |
| 572 | Type *UseTy = U.get()->getType(); |
| 573 | if (PHINode *PHI = dyn_cast<PHINode>(U.getUser())) { |
| 574 | // For PHI nodes, insert the bitcast in the predecessor block. |
| 575 | unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo); |
| 576 | BasicBlock *BB = PHI->getIncomingBlock(ValNo); |
| 577 | if (Replacement->getType() != UseTy) |
| 578 | Replacement = new BitCastInst(Replacement, UseTy, "", |
| 579 | &BB->back()); |
| 580 | // While we're here, rewrite all edges for this PHI, rather |
| 581 | // than just one use at a time, to minimize the number of |
| 582 | // bitcasts we emit. |
| 583 | for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) |
| 584 | if (PHI->getIncomingBlock(i) == BB) { |
| 585 | // Keep the UI iterator valid. |
Duncan P. N. Exon Smith | cb1c81a | 2014-03-18 22:32:43 +0000 | [diff] [blame] | 586 | if (UI != UE && |
| 587 | &PHI->getOperandUse( |
| 588 | PHINode::getOperandNumForIncomingValue(i)) == &*UI) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 589 | ++UI; |
| 590 | PHI->setIncomingValue(i, Replacement); |
| 591 | } |
| 592 | } else { |
| 593 | if (Replacement->getType() != UseTy) |
| 594 | Replacement = new BitCastInst(Replacement, UseTy, "", |
| 595 | cast<Instruction>(U.getUser())); |
| 596 | U.set(Replacement); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // If Arg is a no-op casted pointer, strip one level of casts and iterate. |
| 602 | if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg)) |
| 603 | Arg = BI->getOperand(0); |
| 604 | else if (isa<GEPOperator>(Arg) && |
| 605 | cast<GEPOperator>(Arg)->hasAllZeroIndices()) |
| 606 | Arg = cast<GEPOperator>(Arg)->getPointerOperand(); |
| 607 | else if (isa<GlobalAlias>(Arg) && |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 608 | !cast<GlobalAlias>(Arg)->isInterposable()) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 609 | Arg = cast<GlobalAlias>(Arg)->getAliasee(); |
| 610 | else |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // If this function has no escaping allocas or suspicious vararg usage, |
| 616 | // objc_storeStrong calls can be marked with the "tail" keyword. |
| 617 | if (TailOkForStoreStrongs) |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 618 | for (CallInst *CI : StoreStrongCalls) |
| 619 | CI->setTailCall(); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 620 | StoreStrongCalls.clear(); |
| 621 | |
| 622 | return Changed; |
| 623 | } |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 624 | |
| 625 | //===----------------------------------------------------------------------===// |
| 626 | // Misc Pass Manager |
| 627 | //===----------------------------------------------------------------------===// |
| 628 | |
| 629 | char ObjCARCContract::ID = 0; |
| 630 | INITIALIZE_PASS_BEGIN(ObjCARCContract, "objc-arc-contract", |
| 631 | "ObjC ARC contraction", false, false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 632 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 633 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 634 | INITIALIZE_PASS_END(ObjCARCContract, "objc-arc-contract", |
| 635 | "ObjC ARC contraction", false, false) |
| 636 | |
| 637 | void ObjCARCContract::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 638 | AU.addRequired<AAResultsWrapperPass>(); |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 639 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 640 | AU.setPreservesCFG(); |
| 641 | } |
| 642 | |
| 643 | Pass *llvm::createObjCARCContractPass() { return new ObjCARCContract(); } |
| 644 | |
| 645 | bool ObjCARCContract::doInitialization(Module &M) { |
| 646 | // If nothing in the Module uses ARC, don't do anything. |
| 647 | Run = ModuleHasARC(M); |
| 648 | if (!Run) |
| 649 | return false; |
| 650 | |
Michael Gottesman | 65cb737 | 2015-03-16 07:02:27 +0000 | [diff] [blame] | 651 | EP.init(&M); |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 652 | |
John McCall | 3fe604f | 2016-01-27 19:05:08 +0000 | [diff] [blame] | 653 | // Initialize RVInstMarker. |
| 654 | RVInstMarker = nullptr; |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 655 | if (NamedMDNode *NMD = |
| 656 | M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker")) |
| 657 | if (NMD->getNumOperands() == 1) { |
| 658 | const MDNode *N = NMD->getOperand(0); |
| 659 | if (N->getNumOperands() == 1) |
| 660 | if (const MDString *S = dyn_cast<MDString>(N->getOperand(0))) |
John McCall | 3fe604f | 2016-01-27 19:05:08 +0000 | [diff] [blame] | 661 | RVInstMarker = S; |
Michael Gottesman | 56bd6a0 | 2015-02-19 00:42:27 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | return false; |
| 665 | } |