LLVM  6.0.0svn
ARCRuntimeEntryPoints.h
Go to the documentation of this file.
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 //
10 /// \file
11 /// This file contains a class ARCRuntimeEntryPoints for use in
12 /// creating/managing references to entry points to the arc objective c runtime.
13 ///
14 /// WARNING: This file knows about certain library functions. It recognizes them
15 /// by name, and hardwires knowledge of their semantics.
16 ///
17 /// WARNING: This file knows about how certain Objective-C library functions are
18 /// used. Naive LLVM IR transformations which would otherwise be
19 /// behavior-preserving may break these assumptions.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
24 #define LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
25 
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/IR/Attributes.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Type.h"
32 #include <cassert>
33 
34 namespace llvm {
35 
36 class Constant;
37 class LLVMContext;
38 
39 namespace objcarc {
40 
43  Release,
44  Retain,
48  RetainRV,
51 };
52 
53 /// Declarations for ObjC runtime functions and constants. These are initialized
54 /// lazily to avoid cluttering up the Module with unused declarations.
56 public:
57  ARCRuntimeEntryPoints() = default;
58 
59  void init(Module *M) {
60  TheModule = M;
61  AutoreleaseRV = nullptr;
62  Release = nullptr;
63  Retain = nullptr;
64  RetainBlock = nullptr;
65  Autorelease = nullptr;
66  StoreStrong = nullptr;
67  RetainRV = nullptr;
68  RetainAutorelease = nullptr;
69  RetainAutoreleaseRV = nullptr;
70  }
71 
73  assert(TheModule != nullptr && "Not initialized.");
74 
75  switch (kind) {
77  return getI8XRetI8XEntryPoint(AutoreleaseRV,
78  "objc_autoreleaseReturnValue", true);
80  return getVoidRetI8XEntryPoint(Release, "objc_release");
82  return getI8XRetI8XEntryPoint(Retain, "objc_retain", true);
84  return getI8XRetI8XEntryPoint(RetainBlock, "objc_retainBlock", false);
86  return getI8XRetI8XEntryPoint(Autorelease, "objc_autorelease", true);
88  return getI8XRetI8XXI8XEntryPoint(StoreStrong, "objc_storeStrong");
90  return getI8XRetI8XEntryPoint(RetainRV,
91  "objc_retainAutoreleasedReturnValue", true);
93  return getI8XRetI8XEntryPoint(RetainAutorelease, "objc_retainAutorelease",
94  true);
96  return getI8XRetI8XEntryPoint(RetainAutoreleaseRV,
97  "objc_retainAutoreleaseReturnValue", true);
98  }
99 
100  llvm_unreachable("Switch should be a covered switch.");
101  }
102 
103 private:
104  /// Cached reference to the module which we will insert declarations into.
105  Module *TheModule = nullptr;
106 
107  /// Declaration for ObjC runtime function objc_autoreleaseReturnValue.
108  Constant *AutoreleaseRV = nullptr;
109 
110  /// Declaration for ObjC runtime function objc_release.
111  Constant *Release = nullptr;
112 
113  /// Declaration for ObjC runtime function objc_retain.
114  Constant *Retain = nullptr;
115 
116  /// Declaration for ObjC runtime function objc_retainBlock.
117  Constant *RetainBlock = nullptr;
118 
119  /// Declaration for ObjC runtime function objc_autorelease.
120  Constant *Autorelease = nullptr;
121 
122  /// Declaration for objc_storeStrong().
123  Constant *StoreStrong = nullptr;
124 
125  /// Declaration for objc_retainAutoreleasedReturnValue().
126  Constant *RetainRV = nullptr;
127 
128  /// Declaration for objc_retainAutorelease().
129  Constant *RetainAutorelease = nullptr;
130 
131  /// Declaration for objc_retainAutoreleaseReturnValue().
132  Constant *RetainAutoreleaseRV = nullptr;
133 
134  Constant *getVoidRetI8XEntryPoint(Constant *&Decl, StringRef Name) {
135  if (Decl)
136  return Decl;
137 
138  LLVMContext &C = TheModule->getContext();
139  Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
141  C, AttributeList::FunctionIndex, Attribute::NoUnwind);
143  /*isVarArg=*/false);
144  return Decl = TheModule->getOrInsertFunction(Name, Fty, Attr);
145  }
146 
147  Constant *getI8XRetI8XEntryPoint(Constant *&Decl, StringRef Name,
148  bool NoUnwind = false) {
149  if (Decl)
150  return Decl;
151 
152  LLVMContext &C = TheModule->getContext();
154  Type *Params[] = { I8X };
155  FunctionType *Fty = FunctionType::get(I8X, Params, /*isVarArg=*/false);
156  AttributeList Attr = AttributeList();
157 
158  if (NoUnwind)
160  Attribute::NoUnwind);
161 
162  return Decl = TheModule->getOrInsertFunction(Name, Fty, Attr);
163  }
164 
165  Constant *getI8XRetI8XXI8XEntryPoint(Constant *&Decl, StringRef Name) {
166  if (Decl)
167  return Decl;
168 
169  LLVMContext &C = TheModule->getContext();
171  Type *I8XX = PointerType::getUnqual(I8X);
172  Type *Params[] = { I8XX, I8X };
173 
175  C, AttributeList::FunctionIndex, Attribute::NoUnwind);
176  Attr = Attr.addParamAttribute(C, 0, Attribute::NoCapture);
177 
179  /*isVarArg=*/false);
180 
181  return Decl = TheModule->getOrInsertFunction(Name, Fty, Attr);
182  }
183 };
184 
185 } // end namespace objcarc
186 
187 } // end namespace llvm
188 
189 #endif // LLVM_LIB_TRANSFORMS_OBJCARC_ARCRUNTIMEENTRYPOINTS_H
uint64_t CallInst * C
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:142
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:398
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
This file contains the simple types necessary to represent the attributes associated with functions a...
Class to represent function types.
Definition: DerivedTypes.h:103
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
This is an important base class in LLVM.
Definition: Constant.h:42
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:161
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:297
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
Definition: DerivedTypes.h:482
Module.h This file contains the declarations for the Module class.
AttributeList addAttribute(LLVMContext &C, unsigned Index, Attribute::AttrKind Kind) const
Add an attribute to the attribute set at the given index.
Declarations for ObjC runtime functions and constants.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:174