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/PassManager.h"
31 #include "llvm/IR/Value.h"
32 #include "llvm/InitializePasses.h"
33 #include "llvm/Pass.h"
34 #include "llvm/PassRegistry.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Transforms/ObjCARC.h"
39 
40 #define DEBUG_TYPE "objc-arc-expand"
41 
42 using namespace llvm;
43 using namespace llvm::objcarc;
44 
45 namespace {
46 static bool runImpl(Function &F) {
47   if (!EnableARCOpts)
48     return false;
49 
50   // If nothing in the Module uses ARC, don't do anything.
51   if (!ModuleHasARC(*F.getParent()))
52     return false;
53 
54   bool Changed = false;
55 
56   LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName()
57                     << "\n");
58 
59   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
60     Instruction *Inst = &*I;
61 
62     LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
63 
64     switch (GetBasicARCInstKind(Inst)) {
65     case ARCInstKind::Retain:
66     case ARCInstKind::RetainRV:
67     case ARCInstKind::Autorelease:
68     case ARCInstKind::AutoreleaseRV:
69     case ARCInstKind::FusedRetainAutorelease:
70     case ARCInstKind::FusedRetainAutoreleaseRV: {
71       // These calls return their argument verbatim, as a low-level
72       // optimization. However, this makes high-level optimizations
73       // harder. Undo any uses of this optimization that the front-end
74       // emitted here. We'll redo them in the contract pass.
75       Changed = true;
76       Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
77       LLVM_DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst
78                         << "\n"
79                            "               New = "
80                         << *Value << "\n");
81       Inst->replaceAllUsesWith(Value);
82       break;
83     }
84     default:
85       break;
86     }
87   }
88 
89   LLVM_DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
90 
91   return Changed;
92 }
93 
94 /// Early ARC transformations.
95 class ObjCARCExpand : public FunctionPass {
96   void getAnalysisUsage(AnalysisUsage &AU) const override;
97   bool runOnFunction(Function &F) override;
98 
99 public:
100   static char ID;
101   ObjCARCExpand() : FunctionPass(ID) {
102     initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
103   }
104 };
105 } // namespace
106 
107 char ObjCARCExpand::ID = 0;
108 INITIALIZE_PASS(ObjCARCExpand, "objc-arc-expand", "ObjC ARC expansion", false,
109                 false)
110 
111 Pass *llvm::createObjCARCExpandPass() { return new ObjCARCExpand(); }
112 
113 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
114   AU.setPreservesCFG();
115 }
116 
117 bool ObjCARCExpand::runOnFunction(Function &F) { return runImpl(F); }
118 
119 PreservedAnalyses ObjCARCExpandPass::run(Function &F,
120                                          FunctionAnalysisManager &AM) {
121   if (!runImpl(F))
122     return PreservedAnalyses::all();
123   PreservedAnalyses PA;
124   PA.preserveSet<CFGAnalyses>();
125   return PA;
126 }
127