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"
30 #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"
49 using namespace llvm::objcarc;
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);
58 /// A flag indicating whether this optimization pass should run.
63 ObjCARCExpand() : FunctionPass(ID) {
64 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
69 char ObjCARCExpand::ID = 0;
70 INITIALIZE_PASS(ObjCARCExpand,
71 "objc-arc-expand", "ObjC ARC expansion", false, false)
73 Pass *llvm::createObjCARCExpandPass() {
74 return new ObjCARCExpand();
77 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
81 bool ObjCARCExpand::doInitialization(Module &M) {
82 Run = ModuleHasARC(M);
86 bool ObjCARCExpand::runOnFunction(Function &F) {
90 // If nothing in the Module uses ARC, don't do anything.
96 DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() << "\n");
98 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
99 Instruction *Inst = &*I;
101 DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
103 switch (GetBasicInstructionClass(Inst)) {
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.
115 Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
116 DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst << "\n"
117 " New = " << *Value << "\n");
118 Inst->replaceAllUsesWith(Value);
126 DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");