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