LLVM  15.0.0git
ObjCARCExpand.cpp
Go to the documentation of this file.
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 
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"
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 (Instruction &Inst : instructions(&F)) {
60  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << Inst << "\n");
61 
62  switch (GetBasicARCInstKind(&Inst)) {
69  // These calls return their argument verbatim, as a low-level
70  // optimization. However, this makes high-level optimizations
71  // harder. Undo any uses of this optimization that the front-end
72  // emitted here. We'll redo them in the contract pass.
73  Changed = true;
74  Value *Value = cast<CallInst>(&Inst)->getArgOperand(0);
75  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Old = " << Inst
76  << "\n"
77  " New = "
78  << *Value << "\n");
79  Inst.replaceAllUsesWith(Value);
80  break;
81  }
82  default:
83  break;
84  }
85  }
86 
87  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
88 
89  return Changed;
90 }
91 
92 /// Early ARC transformations.
93 class ObjCARCExpand : public FunctionPass {
94  void getAnalysisUsage(AnalysisUsage &AU) const override;
95  bool runOnFunction(Function &F) override;
96 
97 public:
98  static char ID;
99  ObjCARCExpand() : FunctionPass(ID) {
101  }
102 };
103 } // namespace
104 
105 char ObjCARCExpand::ID = 0;
106 INITIALIZE_PASS(ObjCARCExpand, "objc-arc-expand", "ObjC ARC expansion", false,
107  false)
108 
109 Pass *llvm::createObjCARCExpandPass() { return new ObjCARCExpand(); }
110 
111 void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
112  AU.setPreservesCFG();
113 }
114 
115 bool ObjCARCExpand::runOnFunction(Function &F) { return runImpl(F); }
116 
119  if (!runImpl(F))
120  return PreservedAnalyses::all();
122  PA.preserveSet<CFGAnalyses>();
123  return PA;
124 }
llvm::objcarc::GetBasicARCInstKind
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
Definition: ObjCARCInstKind.h:104
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
InstIterator.h
llvm::Function
Definition: Function.h:60
Pass.h
PassRegistry.h
llvm::objcarc::ARCInstKind::Autorelease
@ Autorelease
objc_autorelease
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Instruction.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::createObjCARCExpandPass
Pass * createObjCARCExpandPass()
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
runImpl
static bool runImpl(const TargetLibraryInfo &TLI, Function &F)
Definition: ReplaceWithVeclib.cpp:176
llvm::objcarc::ARCInstKind::RetainRV
@ RetainRV
objc_retainAutoreleasedReturnValue
llvm::Instruction
Definition: Instruction.h:42
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
llvm::objcarc::ARCInstKind::FusedRetainAutorelease
@ FusedRetainAutorelease
objc_retainAutorelease
INITIALIZE_PASS
INITIALIZE_PASS(ObjCARCExpand, "objc-arc-expand", "ObjC ARC expansion", false, false) Pass *llvm
Definition: ObjCARCExpand.cpp:106
llvm::objcarc::EnableARCOpts
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
Definition: ObjCARCAnalysisUtils.cpp:23
llvm::objcarc
Definition: ObjCARCAliasAnalysis.h:29
llvm::ObjCARCExpandPass::run
PreservedAnalyses run(Function &M, FunctionAnalysisManager &AM)
Definition: ObjCARCExpand.cpp:117
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
llvm::initializeObjCARCExpandPass
void initializeObjCARCExpandPass(PassRegistry &)
llvm::CFGAnalyses
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:113
ObjCARCAnalysisUtils.h
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::objcarc::ARCInstKind::FusedRetainAutoreleaseRV
@ FusedRetainAutoreleaseRV
objc_retainAutoreleaseReturnValue
Casting.h
Function.h
PassManager.h
llvm::objcarc::ModuleHasARC
bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
Definition: ObjCARCAnalysisUtils.h:43
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
Instructions.h
llvm::PreservedAnalyses::preserveSet
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:188
ObjCARC.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
raw_ostream.h
Value.h
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::objcarc::ARCInstKind::Retain
@ Retain
objc_retain
Debug.h
llvm::objcarc::ARCInstKind::AutoreleaseRV
@ AutoreleaseRV
objc_autoreleaseReturnValue
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37