LLVM  14.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 
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"
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)) {
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");
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) {
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 
121  if (!runImpl(F))
122  return PreservedAnalyses::all();
124  PA.preserveSet<CFGAnalyses>();
125  return PA;
126 }
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:155
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
InstIterator.h
llvm::Function
Definition: Function.h:62
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:56
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
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:177
llvm::objcarc::ARCInstKind::RetainRV
@ RetainRV
objc_retainAutoreleasedReturnValue
llvm::Instruction
Definition: Instruction.h:45
llvm::objcarc::ARCInstKind::FusedRetainAutorelease
@ FusedRetainAutorelease
objc_retainAutorelease
I
#define I(x, y, z)
Definition: MD5.cpp:59
INITIALIZE_PASS
INITIALIZE_PASS(ObjCARCExpand, "objc-arc-expand", "ObjC ARC expansion", false, false) Pass *llvm
Definition: ObjCARCExpand.cpp:108
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:119
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
llvm::initializeObjCARCExpandPass
void initializeObjCARCExpandPass(PassRegistry &)
llvm::CFGAnalyses
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:116
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
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:161
llvm::objcarc::ARCInstKind::FusedRetainAutoreleaseRV
@ FusedRetainAutoreleaseRV
objc_retainAutoreleaseReturnValue
Casting.h
Function.h
llvm::inst_end
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132
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:191
ObjCARC.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::inst_begin
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
llvm::InstIterator
Definition: InstIterator.h:32
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