]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/lib/Transforms/ObjCARC/ObjCARCUtil.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / lib / Transforms / ObjCARC / ObjCARCUtil.cpp
1 //===- ObjCARCUtil.cpp - 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 #include "llvm/IR/Intrinsics.h"
24
25 using namespace llvm;
26 using namespace llvm::objcarc;
27
28 raw_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";
75   case IC_IntrinsicUser:
76     return OS << "IC_IntrinsicUser";
77   case IC_None:
78     return OS << "IC_None";
79   }
80   llvm_unreachable("Unknown instruction class!");
81 }
82
83 InstructionClass llvm::objcarc::GetFunctionClass(const Function *F) {
84   Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
85
86   // No (mandatory) arguments.
87   if (AI == AE)
88     return StringSwitch<InstructionClass>(F->getName())
89       .Case("objc_autoreleasePoolPush",  IC_AutoreleasepoolPush)
90       .Case("clang.arc.use", IC_IntrinsicUser)
91       .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                   // Ignore annotation calls. This is important to stop the
149                   // optimizer from treating annotations as uses which would
150                   // make the state of the pointers they are attempting to
151                   // elucidate to be incorrect.
152                   .Case("llvm.arc.annotation.topdown.bbstart", IC_None)
153                   .Case("llvm.arc.annotation.topdown.bbend", IC_None)
154                   .Case("llvm.arc.annotation.bottomup.bbstart", IC_None)
155                   .Case("llvm.arc.annotation.bottomup.bbend", IC_None)
156                   .Default(IC_CallOrUser);
157           }
158
159   // Anything else.
160   return IC_CallOrUser;
161 }
162
163 /// \brief Determine what kind of construct V is.
164 InstructionClass
165 llvm::objcarc::GetInstructionClass(const Value *V) {
166   if (const Instruction *I = dyn_cast<Instruction>(V)) {
167     // Any instruction other than bitcast and gep with a pointer operand have a
168     // use of an objc pointer. Bitcasts, GEPs, Selects, PHIs transfer a pointer
169     // to a subsequent use, rather than using it themselves, in this sense.
170     // As a short cut, several other opcodes are known to have no pointer
171     // operands of interest. And ret is never followed by a release, so it's
172     // not interesting to examine.
173     switch (I->getOpcode()) {
174     case Instruction::Call: {
175       const CallInst *CI = cast<CallInst>(I);
176       // Check for calls to special functions.
177       if (const Function *F = CI->getCalledFunction()) {
178         InstructionClass Class = GetFunctionClass(F);
179         if (Class != IC_CallOrUser)
180           return Class;
181
182         // None of the intrinsic functions do objc_release. For intrinsics, the
183         // only question is whether or not they may be users.
184         switch (F->getIntrinsicID()) {
185         case Intrinsic::returnaddress: case Intrinsic::frameaddress:
186         case Intrinsic::stacksave: case Intrinsic::stackrestore:
187         case Intrinsic::vastart: case Intrinsic::vacopy: case Intrinsic::vaend:
188         case Intrinsic::objectsize: case Intrinsic::prefetch:
189         case Intrinsic::stackprotector:
190         case Intrinsic::eh_return_i32: case Intrinsic::eh_return_i64:
191         case Intrinsic::eh_typeid_for: case Intrinsic::eh_dwarf_cfa:
192         case Intrinsic::eh_sjlj_lsda: case Intrinsic::eh_sjlj_functioncontext:
193         case Intrinsic::init_trampoline: case Intrinsic::adjust_trampoline:
194         case Intrinsic::lifetime_start: case Intrinsic::lifetime_end:
195         case Intrinsic::invariant_start: case Intrinsic::invariant_end:
196         // Don't let dbg info affect our results.
197         case Intrinsic::dbg_declare: case Intrinsic::dbg_value:
198           // Short cut: Some intrinsics obviously don't use ObjC pointers.
199           return IC_None;
200         default:
201           break;
202         }
203       }
204       return GetCallSiteClass(CI);
205     }
206     case Instruction::Invoke:
207       return GetCallSiteClass(cast<InvokeInst>(I));
208     case Instruction::BitCast:
209     case Instruction::GetElementPtr:
210     case Instruction::Select: case Instruction::PHI:
211     case Instruction::Ret: case Instruction::Br:
212     case Instruction::Switch: case Instruction::IndirectBr:
213     case Instruction::Alloca: case Instruction::VAArg:
214     case Instruction::Add: case Instruction::FAdd:
215     case Instruction::Sub: case Instruction::FSub:
216     case Instruction::Mul: case Instruction::FMul:
217     case Instruction::SDiv: case Instruction::UDiv: case Instruction::FDiv:
218     case Instruction::SRem: case Instruction::URem: case Instruction::FRem:
219     case Instruction::Shl: case Instruction::LShr: case Instruction::AShr:
220     case Instruction::And: case Instruction::Or: case Instruction::Xor:
221     case Instruction::SExt: case Instruction::ZExt: case Instruction::Trunc:
222     case Instruction::IntToPtr: case Instruction::FCmp:
223     case Instruction::FPTrunc: case Instruction::FPExt:
224     case Instruction::FPToUI: case Instruction::FPToSI:
225     case Instruction::UIToFP: case Instruction::SIToFP:
226     case Instruction::InsertElement: case Instruction::ExtractElement:
227     case Instruction::ShuffleVector:
228     case Instruction::ExtractValue:
229       break;
230     case Instruction::ICmp:
231       // Comparing a pointer with null, or any other constant, isn't an
232       // interesting use, because we don't care what the pointer points to, or
233       // about the values of any other dynamic reference-counted pointers.
234       if (IsPotentialRetainableObjPtr(I->getOperand(1)))
235         return IC_User;
236       break;
237     default:
238       // For anything else, check all the operands.
239       // Note that this includes both operands of a Store: while the first
240       // operand isn't actually being dereferenced, it is being stored to
241       // memory where we can no longer track who might read it and dereference
242       // it, so we have to consider it potentially used.
243       for (User::const_op_iterator OI = I->op_begin(), OE = I->op_end();
244            OI != OE; ++OI)
245         if (IsPotentialRetainableObjPtr(*OI))
246           return IC_User;
247     }
248   }
249
250   // Otherwise, it's totally inert for ARC purposes.
251   return IC_None;
252 }