1 //===- ARCRuntimeEntryPoints.h - ObjC ARC Optimization --*- C++ -*---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file contains a class ARCRuntimeEntryPoints for use in
11 /// creating/managing references to entry points to the arc objective c runtime.
13 /// WARNING: This file knows about certain library functions. It recognizes them
14 /// by name, and hardwires knowledge of their semantics.
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.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
23 #define LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
30 enum class ARCRuntimeEntryPointKind
{
42 /// Declarations for ObjC runtime functions and constants. These are initialized
43 /// lazily to avoid cluttering up the Module with unused declarations.
44 class ARCRuntimeEntryPoints
{
46 ARCRuntimeEntryPoints() : TheModule(nullptr),
47 AutoreleaseRV(nullptr),
54 RetainAutorelease(nullptr),
55 RetainAutoreleaseRV(nullptr) { }
57 void init(Module
*M
) {
59 AutoreleaseRV
= nullptr;
62 RetainBlock
= nullptr;
63 Autorelease
= nullptr;
64 StoreStrong
= nullptr;
66 RetainAutorelease
= nullptr;
67 RetainAutoreleaseRV
= nullptr;
70 Constant
*get(ARCRuntimeEntryPointKind kind
) {
71 assert(TheModule
!= nullptr && "Not initialized.");
74 case ARCRuntimeEntryPointKind::AutoreleaseRV
:
75 return getI8XRetI8XEntryPoint(AutoreleaseRV
,
76 "objc_autoreleaseReturnValue", true);
77 case ARCRuntimeEntryPointKind::Release
:
78 return getVoidRetI8XEntryPoint(Release
, "objc_release");
79 case ARCRuntimeEntryPointKind::Retain
:
80 return getI8XRetI8XEntryPoint(Retain
, "objc_retain", true);
81 case ARCRuntimeEntryPointKind::RetainBlock
:
82 return getI8XRetI8XEntryPoint(RetainBlock
, "objc_retainBlock", false);
83 case ARCRuntimeEntryPointKind::Autorelease
:
84 return getI8XRetI8XEntryPoint(Autorelease
, "objc_autorelease", true);
85 case ARCRuntimeEntryPointKind::StoreStrong
:
86 return getI8XRetI8XXI8XEntryPoint(StoreStrong
, "objc_storeStrong");
87 case ARCRuntimeEntryPointKind::RetainRV
:
88 return getI8XRetI8XEntryPoint(RetainRV
,
89 "objc_retainAutoreleasedReturnValue", true);
90 case ARCRuntimeEntryPointKind::RetainAutorelease
:
91 return getI8XRetI8XEntryPoint(RetainAutorelease
, "objc_retainAutorelease",
93 case ARCRuntimeEntryPointKind::RetainAutoreleaseRV
:
94 return getI8XRetI8XEntryPoint(RetainAutoreleaseRV
,
95 "objc_retainAutoreleaseReturnValue", true);
98 llvm_unreachable("Switch should be a covered switch.");
102 /// Cached reference to the module which we will insert declarations into.
105 /// Declaration for ObjC runtime function objc_autoreleaseReturnValue.
106 Constant
*AutoreleaseRV
;
107 /// Declaration for ObjC runtime function objc_release.
109 /// Declaration for ObjC runtime function objc_retain.
111 /// Declaration for ObjC runtime function objc_retainBlock.
112 Constant
*RetainBlock
;
113 /// Declaration for ObjC runtime function objc_autorelease.
114 Constant
*Autorelease
;
115 /// Declaration for objc_storeStrong().
116 Constant
*StoreStrong
;
117 /// Declaration for objc_retainAutoreleasedReturnValue().
119 /// Declaration for objc_retainAutorelease().
120 Constant
*RetainAutorelease
;
121 /// Declaration for objc_retainAutoreleaseReturnValue().
122 Constant
*RetainAutoreleaseRV
;
124 Constant
*getVoidRetI8XEntryPoint(Constant
*&Decl
, StringRef Name
) {
128 LLVMContext
&C
= TheModule
->getContext();
129 Type
*Params
[] = { PointerType::getUnqual(Type::getInt8Ty(C
)) };
130 AttributeList Attr
= AttributeList().addAttribute(
131 C
, AttributeList::FunctionIndex
, Attribute::NoUnwind
);
132 FunctionType
*Fty
= FunctionType::get(Type::getVoidTy(C
), Params
,
134 return Decl
= TheModule
->getOrInsertFunction(Name
, Fty
, Attr
);
137 Constant
*getI8XRetI8XEntryPoint(Constant
*&Decl
, StringRef Name
,
138 bool NoUnwind
= false) {
142 LLVMContext
&C
= TheModule
->getContext();
143 Type
*I8X
= PointerType::getUnqual(Type::getInt8Ty(C
));
144 Type
*Params
[] = { I8X
};
145 FunctionType
*Fty
= FunctionType::get(I8X
, Params
, /*isVarArg=*/false);
146 AttributeList Attr
= AttributeList();
149 Attr
= Attr
.addAttribute(C
, AttributeList::FunctionIndex
,
150 Attribute::NoUnwind
);
152 return Decl
= TheModule
->getOrInsertFunction(Name
, Fty
, Attr
);
155 Constant
*getI8XRetI8XXI8XEntryPoint(Constant
*&Decl
, StringRef Name
) {
159 LLVMContext
&C
= TheModule
->getContext();
160 Type
*I8X
= PointerType::getUnqual(Type::getInt8Ty(C
));
161 Type
*I8XX
= PointerType::getUnqual(I8X
);
162 Type
*Params
[] = { I8XX
, I8X
};
164 AttributeList Attr
= AttributeList().addAttribute(
165 C
, AttributeList::FunctionIndex
, Attribute::NoUnwind
);
166 Attr
= Attr
.addParamAttribute(C
, 0, Attribute::NoCapture
);
168 FunctionType
*Fty
= FunctionType::get(Type::getVoidTy(C
), Params
,
171 return Decl
= TheModule
->getOrInsertFunction(Name
, Fty
, Attr
);
174 }; // class ARCRuntimeEntryPoints
176 } // namespace objcarc