blob: 633f9884842d6308cfb24cbadd6d50b587484ee9 [file] [log] [blame]
Michael Gottesman08904e32013-01-28 03:28:38 +00001//===- 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"
Michael Gottesman9bfcf282013-01-28 05:51:58 +000027
Michael Gottesman08904e32013-01-28 03:28:38 +000028#include "ObjCARC.h"
29
Michael Gottesman9bfcf282013-01-28 05:51:58 +000030#include "llvm/ADT/StringRef.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/Instruction.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/Value.h"
35#include "llvm/Pass.h"
36#include "llvm/PassAnalysisSupport.h"
37#include "llvm/PassRegistry.h"
38#include "llvm/PassSupport.h"
39#include "llvm/Support/Casting.h"
40#include "llvm/Support/Debug.h"
41#include "llvm/Support/InstIterator.h"
42#include "llvm/Support/raw_ostream.h"
43
44namespace llvm {
45 class Module;
46}
47
Michael Gottesman08904e32013-01-28 03:28:38 +000048using namespace llvm;
49using namespace llvm::objcarc;
50
51namespace {
52 /// \brief Early ARC transformations.
53 class ObjCARCExpand : public FunctionPass {
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
55 virtual bool doInitialization(Module &M);
56 virtual bool runOnFunction(Function &F);
57
58 /// A flag indicating whether this optimization pass should run.
59 bool Run;
60
61 public:
62 static char ID;
63 ObjCARCExpand() : FunctionPass(ID) {
64 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
65 }
66 };
67}
68
69char ObjCARCExpand::ID = 0;
70INITIALIZE_PASS(ObjCARCExpand,
71 "objc-arc-expand", "ObjC ARC expansion", false, false)
72
73Pass *llvm::createObjCARCExpandPass() {
74 return new ObjCARCExpand();
75}
76
77void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
78 AU.setPreservesCFG();
79}
80
81bool ObjCARCExpand::doInitialization(Module &M) {
82 Run = ModuleHasARC(M);
83 return false;
84}
85
86bool ObjCARCExpand::runOnFunction(Function &F) {
87 if (!EnableARCOpts)
88 return false;
89
90 // If nothing in the Module uses ARC, don't do anything.
91 if (!Run)
92 return false;
93
94 bool Changed = false;
95
96 DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n");
97
98 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
99 Instruction *Inst = &*I;
100
101 DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
102
103 switch (GetBasicInstructionClass(Inst)) {
104 case IC_Retain:
105 case IC_RetainRV:
106 case IC_Autorelease:
107 case IC_AutoreleaseRV:
108 case IC_FusedRetainAutorelease:
109 case IC_FusedRetainAutoreleaseRV: {
110 // These calls return their argument verbatim, as a low-level
111 // optimization. However, this makes high-level optimizations
112 // harder. Undo any uses of this optimization that the front-end
113 // emitted here. We'll redo them in the contract pass.
114 Changed = true;
115 Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
116 DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
117 " New = " << *Value << "\n");
118 Inst->replaceAllUsesWith(Value);
119 break;
120 }
121 default:
122 break;
123 }
124 }
125
126 DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
127
128 return Changed;
129}