blob: 04e98d8f55776867e1d33a54e05e58ac19b7afe8 [file] [log] [blame]
Michael Gottesman08904e32013-01-28 03:28:38 +00001//===- ObjCARCExpand.cpp - ObjC ARC Optimization --------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael Gottesman08904e32013-01-28 03:28:38 +00006//
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
Michael Gottesman08904e32013-01-28 03:28:38 +000025#include "ObjCARC.h"
Michael Gottesman9bfcf282013-01-28 05:51:58 +000026#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000027#include "llvm/IR/InstIterator.h"
Michael Gottesman9bfcf282013-01-28 05:51:58 +000028#include "llvm/IR/Instruction.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/Value.h"
31#include "llvm/Pass.h"
32#include "llvm/PassAnalysisSupport.h"
33#include "llvm/PassRegistry.h"
34#include "llvm/PassSupport.h"
35#include "llvm/Support/Casting.h"
36#include "llvm/Support/Debug.h"
Michael Gottesman9bfcf282013-01-28 05:51:58 +000037#include "llvm/Support/raw_ostream.h"
38
Chandler Carruth964daaa2014-04-22 02:55:47 +000039#define DEBUG_TYPE "objc-arc-expand"
40
Michael Gottesman9bfcf282013-01-28 05:51:58 +000041namespace llvm {
42 class Module;
43}
44
Michael Gottesman08904e32013-01-28 03:28:38 +000045using namespace llvm;
46using namespace llvm::objcarc;
47
48namespace {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000049 /// Early ARC transformations.
Michael Gottesman08904e32013-01-28 03:28:38 +000050 class ObjCARCExpand : public FunctionPass {
Craig Topper3e4c6972014-03-05 09:10:37 +000051 void getAnalysisUsage(AnalysisUsage &AU) const override;
52 bool doInitialization(Module &M) override;
53 bool runOnFunction(Function &F) override;
Michael Gottesman08904e32013-01-28 03:28:38 +000054
55 /// A flag indicating whether this optimization pass should run.
56 bool Run;
57
58 public:
59 static char ID;
60 ObjCARCExpand() : FunctionPass(ID) {
61 initializeObjCARCExpandPass(*PassRegistry::getPassRegistry());
62 }
63 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000064}
Michael Gottesman08904e32013-01-28 03:28:38 +000065
66char ObjCARCExpand::ID = 0;
67INITIALIZE_PASS(ObjCARCExpand,
68 "objc-arc-expand", "ObjC ARC expansion", false, false)
69
70Pass *llvm::createObjCARCExpandPass() {
71 return new ObjCARCExpand();
72}
73
74void ObjCARCExpand::getAnalysisUsage(AnalysisUsage &AU) const {
75 AU.setPreservesCFG();
76}
77
78bool ObjCARCExpand::doInitialization(Module &M) {
79 Run = ModuleHasARC(M);
80 return false;
81}
82
83bool ObjCARCExpand::runOnFunction(Function &F) {
84 if (!EnableARCOpts)
85 return false;
86
87 // If nothing in the Module uses ARC, don't do anything.
88 if (!Run)
89 return false;
90
91 bool Changed = false;
92
Nicola Zaghend34e60c2018-05-14 12:53:11 +000093 LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting Function: " << F.getName()
94 << "\n");
Michael Gottesman08904e32013-01-28 03:28:38 +000095
96 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
97 Instruction *Inst = &*I;
98
Nicola Zaghend34e60c2018-05-14 12:53:11 +000099 LLVM_DEBUG(dbgs() << "ObjCARCExpand: Visiting: " << *Inst << "\n");
Michael Gottesman08904e32013-01-28 03:28:38 +0000100
Michael Gottesman6f729fa2015-02-19 19:51:32 +0000101 switch (GetBasicARCInstKind(Inst)) {
102 case ARCInstKind::Retain:
103 case ARCInstKind::RetainRV:
104 case ARCInstKind::Autorelease:
105 case ARCInstKind::AutoreleaseRV:
106 case ARCInstKind::FusedRetainAutorelease:
107 case ARCInstKind::FusedRetainAutoreleaseRV: {
Michael Gottesman08904e32013-01-28 03:28:38 +0000108 // These calls return their argument verbatim, as a low-level
109 // optimization. However, this makes high-level optimizations
110 // harder. Undo any uses of this optimization that the front-end
111 // emitted here. We'll redo them in the contract pass.
112 Changed = true;
113 Value *Value = cast<CallInst>(Inst)->getArgOperand(0);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000114 LLVM_DEBUG(dbgs() << "ObjCARCExpand: Old = " << *Inst
115 << "\n"
116 " New = "
117 << *Value << "\n");
Michael Gottesman08904e32013-01-28 03:28:38 +0000118 Inst->replaceAllUsesWith(Value);
119 break;
120 }
121 default:
122 break;
123 }
124 }
125
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000126 LLVM_DEBUG(dbgs() << "ObjCARCExpand: Finished List.\n\n");
Michael Gottesman08904e32013-01-28 03:28:38 +0000127
128 return Changed;
129}