LLVM  14.0.0git
ObjCARCAPElim.cpp
Go to the documentation of this file.
1 //===- ObjCARCAPElim.cpp - ObjC ARC Optimization --------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
11 /// Reference Counting and is a system for managing reference counts for objects
12 /// in Objective C.
13 ///
14 /// This specific file implements optimizations which remove extraneous
15 /// autorelease pools.
16 ///
17 /// WARNING: This file knows about certain library functions. It recognizes them
18 /// by name, and hardwires knowledge of their semantics.
19 ///
20 /// WARNING: This file knows about how certain Objective-C library functions are
21 /// used. Naive LLVM IR transformations which would otherwise be
22 /// behavior-preserving may break these assumptions.
23 ///
24 //===----------------------------------------------------------------------===//
25 
26 #include "ObjCARC.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/PassManager.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Support/Debug.h"
34 
35 using namespace llvm;
36 using namespace llvm::objcarc;
37 
38 #define DEBUG_TYPE "objc-arc-ap-elim"
39 
40 namespace {
41 
42 /// Interprocedurally determine if calls made by the given call site can
43 /// possibly produce autoreleases.
44 bool MayAutorelease(const CallBase &CB, unsigned Depth = 0) {
45  if (const Function *Callee = CB.getCalledFunction()) {
46  if (!Callee->hasExactDefinition())
47  return true;
48  for (const BasicBlock &BB : *Callee) {
49  for (const Instruction &I : BB)
50  if (const CallBase *JCB = dyn_cast<CallBase>(&I))
51  // This recursion depth limit is arbitrary. It's just great
52  // enough to cover known interesting testcases.
53  if (Depth < 3 && !JCB->onlyReadsMemory() &&
54  MayAutorelease(*JCB, Depth + 1))
55  return true;
56  }
57  return false;
58  }
59 
60  return true;
61 }
62 
63 bool OptimizeBB(BasicBlock *BB) {
64  bool Changed = false;
65 
66  Instruction *Push = nullptr;
67  for (Instruction &Inst : llvm::make_early_inc_range(*BB)) {
68  switch (GetBasicARCInstKind(&Inst)) {
70  Push = &Inst;
71  break;
73  // If this pop matches a push and nothing in between can autorelease,
74  // zap the pair.
75  if (Push && cast<CallInst>(&Inst)->getArgOperand(0) == Push) {
76  Changed = true;
77  LLVM_DEBUG(dbgs() << "ObjCARCAPElim::OptimizeBB: Zapping push pop "
78  "autorelease pair:\n"
79  " Pop: "
80  << Inst << "\n"
81  << " Push: " << *Push
82  << "\n");
83  Inst.eraseFromParent();
84  Push->eraseFromParent();
85  }
86  Push = nullptr;
87  break;
89  if (MayAutorelease(cast<CallBase>(Inst)))
90  Push = nullptr;
91  break;
92  default:
93  break;
94  }
95  }
96 
97  return Changed;
98 }
99 
100 bool runImpl(Module &M) {
101  if (!EnableARCOpts)
102  return false;
103 
104  // If nothing in the Module uses ARC, don't do anything.
105  if (!ModuleHasARC(M))
106  return false;
107  // Find the llvm.global_ctors variable, as the first step in
108  // identifying the global constructors. In theory, unnecessary autorelease
109  // pools could occur anywhere, but in practice it's pretty rare. Global
110  // ctors are a place where autorelease pools get inserted automatically,
111  // so it's pretty common for them to be unnecessary, and it's pretty
112  // profitable to eliminate them.
113  GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
114  if (!GV)
115  return false;
116 
118  "llvm.global_ctors is uncooperative!");
119 
120  bool Changed = false;
121 
122  // Dig the constructor functions out of GV's initializer.
123  ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
124  for (User::op_iterator OI = Init->op_begin(), OE = Init->op_end();
125  OI != OE; ++OI) {
126  Value *Op = *OI;
127  // llvm.global_ctors is an array of three-field structs where the second
128  // members are constructor functions.
129  Function *F = dyn_cast<Function>(cast<ConstantStruct>(Op)->getOperand(1));
130  // If the user used a constructor function with the wrong signature and
131  // it got bitcasted or whatever, look the other way.
132  if (!F)
133  continue;
134  // Only look at function definitions.
135  if (F->isDeclaration())
136  continue;
137  // Only look at functions with one basic block.
138  if (std::next(F->begin()) != F->end())
139  continue;
140  // Ok, a single-block constructor function definition. Try to optimize it.
141  Changed |= OptimizeBB(&F->front());
142  }
143 
144  return Changed;
145 }
146 
147 /// Autorelease pool elimination.
148 class ObjCARCAPElim : public ModulePass {
149  void getAnalysisUsage(AnalysisUsage &AU) const override;
150  bool runOnModule(Module &M) override;
151 
152 public:
153  static char ID;
154  ObjCARCAPElim() : ModulePass(ID) {
156  }
157 };
158 } // namespace
159 
160 char ObjCARCAPElim::ID = 0;
161 INITIALIZE_PASS(ObjCARCAPElim, "objc-arc-apelim",
162  "ObjC ARC autorelease pool elimination", false, false)
163 
164 Pass *llvm::createObjCARCAPElimPass() { return new ObjCARCAPElim(); }
165 
166 void ObjCARCAPElim::getAnalysisUsage(AnalysisUsage &AU) const {
167  AU.setPreservesCFG();
168 }
169 
170 bool ObjCARCAPElim::runOnModule(Module &M) {
171  if (skipModule(M))
172  return false;
173  return runImpl(M);
174 }
175 
177  if (!runImpl(M))
178  return PreservedAnalyses::all();
180  PA.preserveSet<CFGAnalyses>();
181  return PA;
182 }
llvm::objcarc::GetBasicARCInstKind
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
Definition: ObjCARCInstKind.h:104
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::Function
Definition: Function.h:61
llvm::ObjCARCAPElimPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: ObjCARCAPElim.cpp:176
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::Depth
@ Depth
Definition: SIMachineScheduler.h:36
INITIALIZE_PASS
INITIALIZE_PASS(ObjCARCAPElim, "objc-arc-apelim", "ObjC ARC autorelease pool elimination", false, false) Pass *llvm
Definition: ObjCARCAPElim.cpp:161
STLExtras.h
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
Constants.h
llvm::objcarc::ARCInstKind::CallOrUser
@ CallOrUser
could call objc_release and/or "use" pointers
llvm::CallBase::getCalledFunction
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1393
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
runImpl
static bool runImpl(const TargetLibraryInfo &TLI, Function &F)
Definition: ReplaceWithVeclib.cpp:177
llvm::ConstantArray
ConstantArray - Constant Array Declarations.
Definition: Constants.h:409
llvm::Instruction
Definition: Instruction.h:45
llvm::objcarc::ARCInstKind::AutoreleasepoolPop
@ AutoreleasepoolPop
objc_autoreleasePoolPop
llvm::GlobalVariable::getInitializer
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Definition: GlobalVariable.h:136
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:78
llvm::GlobalVariable::hasDefinitiveInitializer
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
Definition: GlobalVariable.h:110
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::make_early_inc_range
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:576
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::objcarc::EnableARCOpts
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
Definition: ObjCARCAnalysisUtils.cpp:23
llvm::objcarc
Definition: ObjCARCAliasAnalysis.h:29
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
llvm::CFGAnalyses
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:116
llvm::Init
Definition: Record.h:271
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:206
llvm::objcarc::ARCInstKind::AutoreleasepoolPush
@ AutoreleasepoolPush
objc_autoreleasePoolPush
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:321
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
PassManager.h
llvm::objcarc::ModuleHasARC
bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
Definition: ObjCARCAnalysisUtils.h:43
llvm::initializeObjCARCAPElimPass
void initializeObjCARCAPElimPass(PassRegistry &)
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
llvm::PreservedAnalyses::preserveSet
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:191
ObjCARC.h
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1161
llvm::createObjCARCAPElimPass
Pass * createObjCARCAPElimPass()
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
raw_ostream.h
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
Debug.h
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37