1 //===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file defines ObjC ARC optimizations. ARC stands for Automatic 10 /// Reference Counting and is a system for managing reference counts for objects 11 /// in Objective C. 12 /// 13 /// This specific file deals with early optimizations which perform certain 14 /// cleanup operations. 15 /// 16 /// WARNING: This file knows about certain library functions. It recognizes them 17 /// by name, and hardwires knowledge of their semantics. 18 /// 19 /// WARNING: This file knows about how certain Objective-C library functions are 20 /// used. Naive LLVM IR transformations which would otherwise be 21 /// behavior-preserving may break these assumptions. 22 /// 23 //===----------------------------------------------------------------------===// 24 25 #include "ObjCARC.h" 26 #include "llvm/IR/Function.h" 27 #include "llvm/IR/InstIterator.h" 28 #include "llvm/IR/Instruction.h" 29 #include "llvm/IR/Instructions.h" 30 #include "llvm/IR/Value.h" 31 #include "llvm/InitializePasses.h" 32 #include "llvm/Pass.h" 33 #include "llvm/PassRegistry.h" 34 #include "llvm/Support/Casting.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/raw_ostream.h" 37 38 #define DEBUG_TYPE "objc-arc-expand" 39 40 namespace llvm { 41 class Module; 42 } 43 44 using namespace llvm; 45 using namespace llvm::objcarc; 46 47 namespace { 48 /// Early ARC transformations. 49 class ObjCARCExpand : public FunctionPass { 50 void getAnalysisUsage(AnalysisUsage &AU) const override; 51 bool doInitialization(Module &M) override; 52 bool runOnFunction(Function &F) override; 53 54 /// A flag indicating whether this optimization pass should run. 55 bool Run; 56 57 public: 58 static char ID; 59 ObjCARCExpand() : FunctionPass(ID) { 60 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry()); 61 } 62 }; 63 } 64 65 char ObjCARCExpand::ID = 0; 66 INITIALIZE_PASS(ObjCARCExpand, 67 "objc-arc-expand", "ObjC ARC expansion", false, false) 68 69 Pass *llvm::createObjCARCExpandPass() { 70 return new ObjCARCExpand(); 71 } 72 73 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const { 74 AU.setPreservesCFG(); 75 } 76 77 bool ObjCARCExpand::doInitialization(Module &M) { 78 Run = ModuleHasARC(M); 79 return false; 80 } 81 82 bool ObjCARCExpand::runOnFunction(Function &F) { 83 if (!EnableARCOpts) 84 return false; 85 86 // If nothing in the Module uses ARC, don't do anything. 87 if (!Run) 88 return false; 89 90 bool Changed = false; 91 92 LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName() 93 << "\n"); 94 95 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) { 96 Instruction *Inst = &*I; 97 98 LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n"); 99 100 switch (GetBasicARCInstKind(Inst)) { 101 case ARCInstKind::Retain: 102 case ARCInstKind::RetainRV: 103 case ARCInstKind::Autorelease: 104 case ARCInstKind::AutoreleaseRV: 105 case ARCInstKind::FusedRetainAutorelease: 106 case ARCInstKind::FusedRetainAutoreleaseRV: { 107 // These calls return their argument verbatim, as a low-level 108 // optimization. However, this makes high-level optimizations 109 // harder. Undo any uses of this optimization that the front-end 110 // emitted here. We'll redo them in the contract pass. 111 Changed = true; 112 Value *Value = cast<CallInst>(Inst)->getArgOperand(0); 113 LLVM_DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst 114 << "\n" 115 " New = " 116 << *Value << "\n"); 117 Inst->replaceAllUsesWith(Value); 118 break; 119 } 120 default: 121 break; 122 } 123 } 124 125 LLVM_DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n"); 126 127 return Changed; 128 } 129