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