LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 #define DEBUG_TYPE "objc-arc-expand"
27 
28 #include "ObjCARC.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/Instruction.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Value.h"
34 #include "llvm/Pass.h"
36 #include "llvm/PassRegistry.h"
37 #include "llvm/PassSupport.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/Debug.h"
42 
43 namespace llvm {
44  class Module;
45 }
46 
47 using namespace llvm;
48 using namespace llvm::objcarc;
49 
50 namespace {
51  /// \brief Early ARC transformations.
52  class ObjCARCExpand : public FunctionPass {
53  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
54  virtual bool doInitialization(Module &M);
55  virtual bool runOnFunction(Function &F);
56 
57  /// A flag indicating whether this optimization pass should run.
58  bool Run;
59 
60  public:
61  static char ID;
62  ObjCARCExpand() : FunctionPass(ID) {
64  }
65  };
66 }
67 
68 char ObjCARCExpand::ID = 0;
69 INITIALIZE_PASS(ObjCARCExpand,
70  "objc-arc-expand", "ObjC ARC expansion", false, false)
71 
73  return new ObjCARCExpand();
74 }
75 
76 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
77  AU.setPreservesCFG();
78 }
79 
80 bool ObjCARCExpand::doInitialization(Module &M) {
81  Run = ModuleHasARC(M);
82  return false;
83 }
84 
85 bool ObjCARCExpand::runOnFunction(Function &F) {
86  if (!EnableARCOpts)
87  return false;
88 
89  // If nothing in the Module uses ARC, don't do anything.
90  if (!Run)
91  return false;
92 
93  bool Changed = false;
94 
95  DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n");
96 
97  for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
98  Instruction *Inst = &*I;
99 
100  DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
101 
102  switch (GetBasicInstructionClass(Inst)) {
103  case IC_Retain:
104  case IC_RetainRV:
105  case IC_Autorelease:
106  case IC_AutoreleaseRV:
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  DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
116  " New = " << *Value << "\n");
117  Inst->replaceAllUsesWith(Value);
118  break;
119  }
120  default:
121  break;
122  }
123  }
124 
125  DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
126 
127  return Changed;
128 }
static PassRegistry * getPassRegistry()
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
static InstructionClass GetBasicInstructionClass(const Value *V)
Determine which objc runtime call instruction class V belongs to.
F(f)
StringRef getName() const
Definition: Value.cpp:167
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:128
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
Definition: ObjCARC.cpp:30
void replaceAllUsesWith(Value *V)
Definition: Value.cpp:303
Pass * createObjCARCExpandPass()
INITIALIZE_PASS(ObjCARCExpand,"objc-arc-expand","ObjC ARC expansion", false, false) Pass *llvm
void setPreservesCFG()
Definition: Pass.cpp:249
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
#define I(x, y, z)
Definition: MD5.cpp:54
objc_retainAutoreleasedReturnValue
LLVM Value Representation.
Definition: Value.h:66
#define DEBUG(X)
Definition: Debug.h:97
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:129
static bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
void initializeObjCARCExpandPass(PassRegistry &)