blob: 0a54451bb1793e2e7ccc387d2d5f2ae2ccfbf084 [file] [log] [blame]
Michael Gottesman1e29ca12013-01-29 05:07:18 +00001//===- ObjCARCUtil.cpp - ObjC ARC Optimization --------*- mode: c++ -*-----===//
Michael Gottesman5ed40af2013-01-28 06:39:31 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file defines several utility functions used by various ARC
11/// optimizations which are IMHO too big to be in a header file.
12///
13/// WARNING: This file knows about certain library functions. It recognizes them
14/// by name, and hardwires knowledge of their semantics.
15///
16/// WARNING: This file knows about how certain Objective-C library functions are
17/// used. Naive LLVM IR transformations which would otherwise be
18/// behavior-preserving may break these assumptions.
19///
20//===----------------------------------------------------------------------===//
21
22#include "ObjCARC.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000023#include "llvm/IR/Intrinsics.h"
24
Michael Gottesman5ed40af2013-01-28 06:39:31 +000025using namespace llvm;
26using namespace llvm::objcarc;
27
28raw_ostream &llvm::objcarc::operator<<(raw_ostream &OS,
29 const InstructionClass Class) {
30 switch (Class) {
31 case IC_Retain:
32 return OS << "IC_Retain";
33 case IC_RetainRV:
34 return OS << "IC_RetainRV";
35 case IC_RetainBlock:
36 return OS << "IC_RetainBlock";
37 case IC_Release:
38 return OS << "IC_Release";
39 case IC_Autorelease:
40 return OS << "IC_Autorelease";
41 case IC_AutoreleaseRV:
42 return OS << "IC_AutoreleaseRV";
43 case IC_AutoreleasepoolPush:
44 return OS << "IC_AutoreleasepoolPush";
45 case IC_AutoreleasepoolPop:
46 return OS << "IC_AutoreleasepoolPop";
47 case IC_NoopCast:
48 return OS << "IC_NoopCast";
49 case IC_FusedRetainAutorelease:
50 return OS << "IC_FusedRetainAutorelease";
51 case IC_FusedRetainAutoreleaseRV:
52 return OS << "IC_FusedRetainAutoreleaseRV";
53 case IC_LoadWeakRetained:
54 return OS << "IC_LoadWeakRetained";
55 case IC_StoreWeak:
56 return OS << "IC_StoreWeak";
57 case IC_InitWeak:
58 return OS << "IC_InitWeak";
59 case IC_LoadWeak:
60 return OS << "IC_LoadWeak";
61 case IC_MoveWeak:
62 return OS << "IC_MoveWeak";
63 case IC_CopyWeak:
64 return OS << "IC_CopyWeak";
65 case IC_DestroyWeak:
66 return OS << "IC_DestroyWeak";
67 case IC_StoreStrong:
68 return OS << "IC_StoreStrong";
69 case IC_CallOrUser:
70 return OS << "IC_CallOrUser";
71 case IC_Call:
72 return OS << "IC_Call";
73 case IC_User:
74 return OS << "IC_User";
John McCall20182ac2013-03-22 21:38:36 +000075 case IC_IntrinsicUser:
76 return OS << "IC_IntrinsicUser";
Michael Gottesman5ed40af2013-01-28 06:39:31 +000077 case IC_None:
78 return OS << "IC_None";
79 }
80 llvm_unreachable("Unknown instruction class!");
81}
82
83InstructionClass llvm::objcarc::GetFunctionClass(const Function *F) {
84 Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
85
John McCall20182ac2013-03-22 21:38:36 +000086 // No (mandatory) arguments.
Michael Gottesman5ed40af2013-01-28 06:39:31 +000087 if (AI == AE)
88 return StringSwitch<InstructionClass>(F->getName())
89 .Case("objc_autoreleasePoolPush", IC_AutoreleasepoolPush)
John McCall20182ac2013-03-22 21:38:36 +000090 .Case("clang.arc.use", IC_IntrinsicUser)
Michael Gottesman5ed40af2013-01-28 06:39:31 +000091 .Default(IC_CallOrUser);
92
93 // One argument.
94 const Argument *A0 = AI++;
95 if (AI == AE)
96 // Argument is a pointer.
97 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType())) {
98 Type *ETy = PTy->getElementType();
99 // Argument is i8*.
100 if (ETy->isIntegerTy(8))
101 return StringSwitch<InstructionClass>(F->getName())
102 .Case("objc_retain", IC_Retain)
103 .Case("objc_retainAutoreleasedReturnValue", IC_RetainRV)
104 .Case("objc_retainBlock", IC_RetainBlock)
105 .Case("objc_release", IC_Release)
106 .Case("objc_autorelease", IC_Autorelease)
107 .Case("objc_autoreleaseReturnValue", IC_AutoreleaseRV)
108 .Case("objc_autoreleasePoolPop", IC_AutoreleasepoolPop)
109 .Case("objc_retainedObject", IC_NoopCast)
110 .Case("objc_unretainedObject", IC_NoopCast)
111 .Case("objc_unretainedPointer", IC_NoopCast)
112 .Case("objc_retain_autorelease", IC_FusedRetainAutorelease)
113 .Case("objc_retainAutorelease", IC_FusedRetainAutorelease)
114 .Case("objc_retainAutoreleaseReturnValue",IC_FusedRetainAutoreleaseRV)
115 .Default(IC_CallOrUser);
116
117 // Argument is i8**
118 if (PointerType *Pte = dyn_cast<PointerType>(ETy))
119 if (Pte->getElementType()->isIntegerTy(8))
120 return StringSwitch<InstructionClass>(F->getName())
121 .Case("objc_loadWeakRetained", IC_LoadWeakRetained)
122 .Case("objc_loadWeak", IC_LoadWeak)
123 .Case("objc_destroyWeak", IC_DestroyWeak)
124 .Default(IC_CallOrUser);
125 }
126
127 // Two arguments, first is i8**.
128 const Argument *A1 = AI++;
129 if (AI == AE)
130 if (PointerType *PTy = dyn_cast<PointerType>(A0->getType()))
131 if (PointerType *Pte = dyn_cast<PointerType>(PTy->getElementType()))
132 if (Pte->getElementType()->isIntegerTy(8))
133 if (PointerType *PTy1 = dyn_cast<PointerType>(A1->getType())) {
134 Type *ETy1 = PTy1->getElementType();
135 // Second argument is i8*
136 if (ETy1->isIntegerTy(8))
137 return StringSwitch<InstructionClass>(F->getName())
138 .Case("objc_storeWeak", IC_StoreWeak)
139 .Case("objc_initWeak", IC_InitWeak)
140 .Case("objc_storeStrong", IC_StoreStrong)
141 .Default(IC_CallOrUser);
142 // Second argument is i8**.
143 if (PointerType *Pte1 = dyn_cast<PointerType>(ETy1))
144 if (Pte1->getElementType()->isIntegerTy(8))
145 return StringSwitch<InstructionClass>(F->getName())
146 .Case("objc_moveWeak", IC_MoveWeak)
147 .Case("objc_copyWeak", IC_CopyWeak)
148 .Default(IC_CallOrUser);
149 }
150
151 // Anything else.
152 return IC_CallOrUser;
153}
Michael Gottesman778138e2013-01-29 03:03:03 +0000154
155/// \brief Determine what kind of construct V is.
156InstructionClass
157llvm::objcarc::GetInstructionClass(const Value *V) {
158 if (const Instruction *I = dyn_cast<Instruction>(V)) {
159 // Any instruction other than bitcast and gep with a pointer operand have a
160 // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
161 // to a subsequent use, rather than using it themselves, in this sense.
162 // As a short cut, several other opcodes are known to have no pointer
163 // operands of interest. And ret is never followed by a release, so it's
164 // not interesting to examine.
165 switch (I->getOpcode()) {
166 case Instruction::Call: {
167 const CallInst *CI = cast<CallInst>(I);
168 // Check for calls to special functions.
169 if (const Function *F = CI->getCalledFunction()) {
170 InstructionClass Class = GetFunctionClass(F);
171 if (Class != IC_CallOrUser)
172 return Class;
173
174 // None of the intrinsic functions do objc_release. For intrinsics, the
175 // only question is whether or not they may be users.
176 switch (F->getIntrinsicID()) {
177 case Intrinsic::returnaddress: case Intrinsic::frameaddress:
178 case Intrinsic::stacksave: case Intrinsic::stackrestore:
179 case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
180 case Intrinsic::objectsize: case Intrinsic::prefetch:
181 case Intrinsic::stackprotector:
182 case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64:
183 case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa:
184 case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext:
185 case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline:
186 case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
187 case Intrinsic::invariant_start: case Intrinsic::invariant_end:
188 // Don't let dbg info affect our results.
189 case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
190 // Short cut: Some intrinsics obviously don't use ObjC pointers.
191 return IC_None;
192 default:
193 break;
194 }
195 }
196 return GetCallSiteClass(CI);
197 }
198 case Instruction::Invoke:
199 return GetCallSiteClass(cast<InvokeInst>(I));
200 case Instruction::BitCast:
201 case Instruction::GetElementPtr:
202 case Instruction::Select: case Instruction::PHI:
203 case Instruction::Ret: case Instruction::Br:
204 case Instruction::Switch: case Instruction::IndirectBr:
205 case Instruction::Alloca: case Instruction::VAArg:
206 case Instruction::Add: case Instruction::FAdd:
207 case Instruction::Sub: case Instruction::FSub:
208 case Instruction::Mul: case Instruction::FMul:
209 case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv:
210 case Instruction::SRem: case Instruction::URem: case Instruction::FRem:
211 case Instruction::Shl: case Instruction::LShr: case Instruction::AShr:
212 case Instruction::And: case Instruction::Or: case Instruction::Xor:
213 case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc:
214 case Instruction::IntToPtr: case Instruction::FCmp:
215 case Instruction::FPTrunc: case Instruction::FPExt:
216 case Instruction::FPToUI: case Instruction::FPToSI:
217 case Instruction::UIToFP: case Instruction::SIToFP:
218 case Instruction::InsertElement: case Instruction::ExtractElement:
219 case Instruction::ShuffleVector:
220 case Instruction::ExtractValue:
221 break;
222 case Instruction::ICmp:
223 // Comparing a pointer with null, or any other constant, isn't an
224 // interesting use, because we don't care what the pointer points to, or
225 // about the values of any other dynamic reference-counted pointers.
226 if (IsPotentialRetainableObjPtr(I->getOperand(1)))
227 return IC_User;
228 break;
229 default:
230 // For anything else, check all the operands.
231 // Note that this includes both operands of a Store: while the first
232 // operand isn't actually being dereferenced, it is being stored to
233 // memory where we can no longer track who might read it and dereference
234 // it, so we have to consider it potentially used.
235 for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
236 OI != OE; ++OI)
237 if (IsPotentialRetainableObjPtr(*OI))
238 return IC_User;
239 }
240 }
241
242 // Otherwise, it's totally inert for ARC purposes.
243 return IC_None;
244}