LLVM  8.0.1
ObjCARCExpand.cpp
Go to the documentation of this file.
1 //===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This file defines 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 deals with early optimizations which perform certain
15 /// cleanup operations.
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/IR/Function.h"
28 #include "llvm/IR/InstIterator.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Value.h"
32 #include "llvm/Pass.h"
34 #include "llvm/PassRegistry.h"
35 #include "llvm/PassSupport.h"
36 #include "llvm/Support/Casting.h"
37 #include "llvm/Support/Debug.h"
39 
40 #define DEBUG_TYPE "objc-arc-expand"
41 
42 namespace llvm {
43  class Module;
44 }
45 
46 using namespace llvm;
47 using namespace llvm::objcarc;
48 
49 namespace {
50  /// Early ARC transformations.
51  class ObjCARCExpand : public FunctionPass {
52  void getAnalysisUsage(AnalysisUsage &AU) const override;
53  bool doInitialization(Module &M) override;
54  bool runOnFunction(Function &F) override;
55 
56  /// A flag indicating whether this optimization pass should run.
57  bool Run;
58 
59  public:
60  static char ID;
61  ObjCARCExpand() : FunctionPass(ID) {
63  }
64  };
65 }
66 
67 char ObjCARCExpand::ID = 0;
68 INITIALIZE_PASS(ObjCARCExpand,
69  "objc-arc-expand", "ObjC ARC expansion", false, false)
70 
72  return new ObjCARCExpand();
73 }
74 
75 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
76  AU.setPreservesCFG();
77 }
78 
79 bool ObjCARCExpand::doInitialization(Module &M) {
80  Run = ModuleHasARC(M);
81  return false;
82 }
83 
85  if (!EnableARCOpts)
86  return false;
87 
88  // If nothing in the Module uses ARC, don't do anything.
89  if (!Run)
90  return false;
91 
92  bool Changed = false;
93 
94  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName()
95  << "\n");
96 
97  for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
98  Instruction *Inst = &*I;
99 
100  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
101 
102  switch (GetBasicARCInstKind(Inst)) {
103  case ARCInstKind::Retain:
109  // These calls return their argument verbatim, as a low-level
110  // optimization. However, this makes high-level optimizations
111  // harder. Undo any uses of this optimization that the front-end
112  // emitted here. We'll redo them in the contract pass.
113  Changed = true;
114  Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
115  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst
116  << "\n"
117  " New = "
118  << *Value << "\n");
119  Inst->replaceAllUsesWith(Value);
120  break;
121  }
122  default:
123  break;
124  }
125  }
126 
127  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
128 
129  return Changed;
130 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
F(f)
objc_autoreleaseReturnValue
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:132
This file defines common definitions/declarations used by the ObjC ARC Optimizer. ...
objc_retainAutoreleasedReturnValue
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
static bool runOnFunction(Function &F, bool PostInlining)
Pass * createObjCARCExpandPass()
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
INITIALIZE_PASS(ObjCARCExpand, "objc-arc-expand", "ObjC ARC expansion", false, false) Pass *llvm
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:286
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM Value Representation.
Definition: Value.h:73
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:133
#define LLVM_DEBUG(X)
Definition: Debug.h:123
void initializeObjCARCExpandPass(PassRegistry &)