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