1 //===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
11 /// Reference Counting and is a system for managing reference counts for objects
14 /// This specific file deals with early optimizations which perform certain
15 /// cleanup operations.
17 /// WARNING: This file knows about certain library functions. It recognizes them
18 /// by name, and hardwires knowledge of their semantics.
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.
24 //===----------------------------------------------------------------------===//
26 #define DEBUG_TYPE "objc-arc-expand"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/InstIterator.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/raw_ostream.h"
48 using namespace llvm::objcarc;
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);
57 /// A flag indicating whether this optimization pass should run.
62 ObjCARCExpand() : FunctionPass(ID) {
63 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
68 char ObjCARCExpand::ID = 0;
69 INITIALIZE_PASS(ObjCARCExpand,
70 "objc-arc-expand", "ObjC ARC expansion", false, false)
72 Pass *llvm::createObjCARCExpandPass() {
73 return new ObjCARCExpand();
76 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
80 bool ObjCARCExpand::doInitialization(Module &M) {
81 Run = ModuleHasARC(M);
85 bool ObjCARCExpand::runOnFunction(Function &F) {
89 // If nothing in the Module uses ARC, don't do anything.
95 DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n");
97 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
98 Instruction *Inst = &*I;
100 DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
102 switch (GetBasicInstructionClass(Inst)) {
106 case IC_AutoreleaseRV:
107 case IC_FusedRetainAutorelease:
108 case IC_FusedRetainAutoreleaseRV: {
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.
114 Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
115 DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
116 " New = " << *Value << "\n");
117 Inst->replaceAllUsesWith(Value);
125 DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");