1 //===--- CodeGenTypeCache.h - Commonly used LLVM types and info -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This structure provides a set of common types useful during IR emission.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPECACHE_H
14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPECACHE_H
15 
16 #include "clang/AST/CharUnits.h"
17 #include "clang/Basic/AddressSpaces.h"
18 #include "llvm/IR/CallingConv.h"
19 
20 namespace llvm {
21   class Type;
22   class IntegerType;
23   class PointerType;
24 }
25 
26 namespace clang {
27 namespace CodeGen {
28 
29 /// This structure provides a set of types that are commonly used
30 /// during IR emission.  It's initialized once in CodeGenModule's
31 /// constructor and then copied around into new CodeGenFunctions.
32 struct CodeGenTypeCache {
33   /// void
34   llvm::Type *VoidTy;
35 
36   /// i8, i16, i32, and i64
37   llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
38   /// half, bfloat, float, double
39   llvm::Type *HalfTy, *BFloatTy, *FloatTy, *DoubleTy;
40 
41   /// int
42   llvm::IntegerType *IntTy;
43 
44   /// char
45   llvm::IntegerType *CharTy;
46 
47   /// intptr_t, size_t, and ptrdiff_t, which we assume are the same size.
48   union {
49     llvm::IntegerType *IntPtrTy;
50     llvm::IntegerType *SizeTy;
51     llvm::IntegerType *PtrDiffTy;
52   };
53 
54   /// void* in address space 0
55   union {
56     llvm::PointerType *VoidPtrTy;
57     llvm::PointerType *Int8PtrTy;
58   };
59 
60   /// void** in address space 0
61   union {
62     llvm::PointerType *VoidPtrPtrTy;
63     llvm::PointerType *Int8PtrPtrTy;
64   };
65 
66   /// void* in alloca address space
67   union {
68     llvm::PointerType *AllocaVoidPtrTy;
69     llvm::PointerType *AllocaInt8PtrTy;
70   };
71 
72   /// The size and alignment of the builtin C type 'int'.  This comes
73   /// up enough in various ABI lowering tasks to be worth pre-computing.
74   union {
75     unsigned char IntSizeInBytes;
76     unsigned char IntAlignInBytes;
77   };
getIntSizeCodeGenTypeCache78   CharUnits getIntSize() const {
79     return CharUnits::fromQuantity(IntSizeInBytes);
80   }
getIntAlignCodeGenTypeCache81   CharUnits getIntAlign() const {
82     return CharUnits::fromQuantity(IntAlignInBytes);
83   }
84 
85   /// The width of a pointer into the generic address space.
86   unsigned char PointerWidthInBits;
87 
88   /// The size and alignment of a pointer into the generic address space.
89   union {
90     unsigned char PointerAlignInBytes;
91     unsigned char PointerSizeInBytes;
92   };
93 
94   /// The size and alignment of size_t.
95   union {
96     unsigned char SizeSizeInBytes; // sizeof(size_t)
97     unsigned char SizeAlignInBytes;
98   };
99 
100   LangAS ASTAllocaAddressSpace;
101 
getSizeSizeCodeGenTypeCache102   CharUnits getSizeSize() const {
103     return CharUnits::fromQuantity(SizeSizeInBytes);
104   }
getSizeAlignCodeGenTypeCache105   CharUnits getSizeAlign() const {
106     return CharUnits::fromQuantity(SizeAlignInBytes);
107   }
getPointerSizeCodeGenTypeCache108   CharUnits getPointerSize() const {
109     return CharUnits::fromQuantity(PointerSizeInBytes);
110   }
getPointerAlignCodeGenTypeCache111   CharUnits getPointerAlign() const {
112     return CharUnits::fromQuantity(PointerAlignInBytes);
113   }
114 
115   llvm::CallingConv::ID RuntimeCC;
getRuntimeCCCodeGenTypeCache116   llvm::CallingConv::ID getRuntimeCC() const { return RuntimeCC; }
117 
getASTAllocaAddressSpaceCodeGenTypeCache118   LangAS getASTAllocaAddressSpace() const { return ASTAllocaAddressSpace; }
119 };
120 
121 }  // end namespace CodeGen
122 }  // end namespace clang
123 
124 #endif
125