LLVM  17.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/Support/Casting.h"
33 #include "llvm/Support/Debug.h"
36 
37 #define DEBUG_TYPE "objc-arc-expand"
38 
39 using namespace llvm;
40 using namespace llvm::objcarc;
41 
42 namespace {
43 static bool runImpl(Function &F) {
44  if (!EnableARCOpts)
45  return false;
46 
47  // If nothing in the Module uses ARC, don't do anything.
48  if (!ModuleHasARC(*F.getParent()))
49  return false;
50 
51  bool Changed = false;
52 
53  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName()
54  << "\n");
55 
56  for (Instruction &Inst : instructions(&F)) {
57  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << Inst << "\n");
58 
59  switch (GetBasicARCInstKind(&Inst)) {
66  // These calls return their argument verbatim, as a low-level
67  // optimization. However, this makes high-level optimizations
68  // harder. Undo any uses of this optimization that the front-end
69  // emitted here. We'll redo them in the contract pass.
70  Changed = true;
71  Value *Value = cast<CallInst>(&Inst)->getArgOperand(0);
72  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Old = " << Inst
73  << "\n"
74  " New = "
75  << *Value << "\n");
76  Inst.replaceAllUsesWith(Value);
77  break;
78  }
79  default:
80  break;
81  }
82  }
83 
84  LLVM_DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
85 
86  return Changed;
87 }
88 
89 } // namespace
90 
93  if (!runImpl(F))
94  return PreservedAnalyses::all();
97  return PA;
98 }
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:18
InstIterator.h
llvm::Function
Definition: Function.h:59
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::objcarc::ARCInstKind::RetainRV
@ RetainRV
objc_retainAutoreleasedReturnValue
llvm::Instruction
Definition: Instruction.h:41
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
llvm::objcarc::ARCInstKind::FusedRetainAutorelease
@ FusedRetainAutorelease
objc_retainAutorelease
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:91
llvm::CFGAnalyses
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:113
ObjCARCAnalysisUtils.h
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
runImpl
static bool runImpl(Function &F, const TargetLowering &TLI)
Definition: ExpandLargeDivRem.cpp:56
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
raw_ostream.h
Value.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