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