Remove the default clause from a fully-covering switch
[llvm-core.git] / lib / Transforms / ObjCARC / ARCRuntimeEntryPoints.h
blobcb3b5757f8d0c387591cfda4eb9848eeb7b92ac4
1 //===- ARCRuntimeEntryPoints.h - ObjC ARC Optimization --*- 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 contains a class ARCRuntimeEntryPoints for use in
11 /// creating/managing references to entry points to the arc objective c runtime.
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 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
23 #define LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
25 #include "ObjCARC.h"
27 namespace llvm {
28 namespace objcarc {
30 enum class ARCRuntimeEntryPointKind {
31 AutoreleaseRV,
32 Release,
33 Retain,
34 RetainBlock,
35 Autorelease,
36 StoreStrong,
37 RetainRV,
38 RetainAutorelease,
39 RetainAutoreleaseRV,
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 {
45 public:
46 ARCRuntimeEntryPoints() : TheModule(nullptr),
47 AutoreleaseRV(nullptr),
48 Release(nullptr),
49 Retain(nullptr),
50 RetainBlock(nullptr),
51 Autorelease(nullptr),
52 StoreStrong(nullptr),
53 RetainRV(nullptr),
54 RetainAutorelease(nullptr),
55 RetainAutoreleaseRV(nullptr) { }
57 void init(Module *M) {
58 TheModule = M;
59 AutoreleaseRV = nullptr;
60 Release = nullptr;
61 Retain = nullptr;
62 RetainBlock = nullptr;
63 Autorelease = nullptr;
64 StoreStrong = nullptr;
65 RetainRV = nullptr;
66 RetainAutorelease = nullptr;
67 RetainAutoreleaseRV = nullptr;
70 Constant *get(ARCRuntimeEntryPointKind kind) {
71 assert(TheModule != nullptr && "Not initialized.");
73 switch (kind) {
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",
92 true);
93 case ARCRuntimeEntryPointKind::RetainAutoreleaseRV:
94 return getI8XRetI8XEntryPoint(RetainAutoreleaseRV,
95 "objc_retainAutoreleaseReturnValue", true);
98 llvm_unreachable("Switch should be a covered switch.");
101 private:
102 /// Cached reference to the module which we will insert declarations into.
103 Module *TheModule;
105 /// Declaration for ObjC runtime function objc_autoreleaseReturnValue.
106 Constant *AutoreleaseRV;
107 /// Declaration for ObjC runtime function objc_release.
108 Constant *Release;
109 /// Declaration for ObjC runtime function objc_retain.
110 Constant *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().
118 Constant *RetainRV;
119 /// Declaration for objc_retainAutorelease().
120 Constant *RetainAutorelease;
121 /// Declaration for objc_retainAutoreleaseReturnValue().
122 Constant *RetainAutoreleaseRV;
124 Constant *getVoidRetI8XEntryPoint(Constant *&Decl, StringRef Name) {
125 if (Decl)
126 return Decl;
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,
133 /*isVarArg=*/false);
134 return Decl = TheModule->getOrInsertFunction(Name, Fty, Attr);
137 Constant *getI8XRetI8XEntryPoint(Constant *&Decl, StringRef Name,
138 bool NoUnwind = false) {
139 if (Decl)
140 return Decl;
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();
148 if (NoUnwind)
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) {
156 if (Decl)
157 return Decl;
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,
169 /*isVarArg=*/false);
171 return Decl = TheModule->getOrInsertFunction(Name, Fty, Attr);
174 }; // class ARCRuntimeEntryPoints
176 } // namespace objcarc
177 } // namespace llvm
179 #endif