LLVM 23.0.0git
IRTypeMapper.cpp
Go to the documentation of this file.
1//===---- IRTypeMapper.cpp - Maps LLVM ABI Types to LLVM IR Types -------===//
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
10#include "llvm/ABI/Types.h"
11#include "llvm/ADT/APFloat.h"
13#include "llvm/IR/DataLayout.h"
15#include "llvm/IR/Type.h"
16
17using namespace llvm::abi;
18
20 assert(ABIType && "convertType requires a non-null ABI type");
21
22 auto It = TypeCache.find(ABIType);
23 if (It != TypeCache.end())
24 return It->second;
25
26 llvm::Type *Result = nullptr;
27
28 switch (ABIType->getKind()) {
30 Result = llvm::Type::getVoidTy(Context);
31 break;
33 const auto *IT = cast<abi::IntegerType>(ABIType);
34 Result =
35 llvm::IntegerType::get(Context, IT->getSizeInBits().getFixedValue());
36 break;
37 }
39 const llvm::fltSemantics *Semantics =
40 cast<abi::FloatType>(ABIType)->getSemantics();
41 Result = llvm::Type::getFloatingPointTy(Context, *Semantics);
42 break;
43 }
46 Context, cast<abi::PointerType>(ABIType)->getAddrSpace());
47 break;
49 Result = convertArrayType(cast<abi::ArrayType>(ABIType));
50 break;
52 Result = convertVectorType(cast<abi::VectorType>(ABIType));
53 break;
55 Result = convertRecordType(cast<abi::RecordType>(ABIType));
56 break;
58 Result = convertComplexType(cast<abi::ComplexType>(ABIType));
59 break;
61 Result = convertMemberPointerType(cast<abi::MemberPointerType>(ABIType));
62 break;
63 }
64
65 TypeCache[ABIType] = Result;
66 return Result;
67}
68
69llvm::Type *IRTypeMapper::convertArrayType(const abi::ArrayType *AT) {
70 llvm::Type *ElementType = convertType(AT->getElementType());
71 uint64_t NumElements = AT->getNumElements();
72 if (AT->isMatrixType())
73 return llvm::VectorType::get(ElementType,
74 ElementCount::getFixed(NumElements));
75 return llvm::ArrayType::get(ElementType, NumElements);
76}
77
78llvm::Type *IRTypeMapper::convertVectorType(const abi::VectorType *VT) {
79 llvm::Type *ElementType = convertType(VT->getElementType());
80 return llvm::VectorType::get(ElementType, VT->getNumElements());
81}
82
83llvm::Type *IRTypeMapper::convertRecordType(const abi::RecordType *RT) {
84 return createStructFromFields(RT->getFields(), RT->getSizeInBits(),
85 RT->getAlignment(), RT->isUnion());
86}
87
88llvm::Type *IRTypeMapper::convertComplexType(const abi::ComplexType *CT) {
89 llvm::Type *ElementType = convertType(CT->getElementType());
90 llvm::Type *Fields[] = {ElementType, ElementType};
91 return llvm::StructType::get(Context, Fields, /*isPacked=*/false);
92}
93
94llvm::Type *
95IRTypeMapper::convertMemberPointerType(const abi::MemberPointerType *MPT) {
96 llvm::Type *IntPtrTy = DL.getIntPtrType(Context);
97 if (MPT->isFunctionPointer()) {
98 llvm::Type *Fields[] = {IntPtrTy, IntPtrTy};
99 return llvm::StructType::get(Context, Fields, /*isPacked=*/false);
100 }
101 return IntPtrTy;
102}
103
104llvm::Type *IRTypeMapper::createPaddingType(uint64_t PaddingBits) {
105 if (PaddingBits == 0)
106 return nullptr;
107 assert(PaddingBits % 8 == 0 &&
108 "sub-byte padding cannot be expressed as an llvm::Type");
110 PaddingBits / 8);
111}
112
113llvm::StructType *
114IRTypeMapper::createStructFromFields(ArrayRef<abi::FieldInfo> Fields,
115 TypeSize Size, Align Alignment,
116 bool IsUnion) {
117 SmallVector<llvm::Type *, 16> FieldTypes;
118
119 if (IsUnion) {
120 llvm::Type *LargestFieldType = nullptr;
121 uint64_t LargestFieldSize = 0;
122 for (const auto &Field : Fields) {
123 llvm::Type *FieldType = convertType(Field.FieldType);
124 uint64_t FieldSize = Field.FieldType->getSizeInBits().getFixedValue();
125 if (FieldSize > LargestFieldSize) {
126 LargestFieldSize = FieldSize;
127 LargestFieldType = FieldType;
128 }
129 }
130 if (LargestFieldType) {
131 FieldTypes.push_back(LargestFieldType);
132 uint64_t UnionSizeBits = Size.getFixedValue();
133 if (LargestFieldSize < UnionSizeBits) {
134 if (llvm::Type *PaddingType =
135 createPaddingType(UnionSizeBits - LargestFieldSize))
136 FieldTypes.push_back(PaddingType);
137 }
138 }
139 } else {
140 uint64_t CurrentOffset = 0;
141 for (const auto &Field : Fields) {
142 if (Field.OffsetInBits > CurrentOffset) {
143 if (llvm::Type *PaddingType =
144 createPaddingType(Field.OffsetInBits - CurrentOffset))
145 FieldTypes.push_back(PaddingType);
146 CurrentOffset = Field.OffsetInBits;
147 }
148 assert(!Field.IsBitField && "bitfields should not reach IR type mapping");
149 llvm::Type *FieldType = convertType(Field.FieldType);
150 FieldTypes.push_back(FieldType);
151 CurrentOffset += Field.FieldType->getSizeInBits().getFixedValue();
152 }
153 uint64_t TotalSizeBits = Size.getFixedValue();
154 if (CurrentOffset < TotalSizeBits) {
155 if (llvm::Type *PaddingType =
156 createPaddingType(TotalSizeBits - CurrentOffset))
157 FieldTypes.push_back(PaddingType);
158 }
159 }
160
161 return StructType::get(Context, FieldTypes, /*isPacked=*/false);
162}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
Maps LLVM ABI type representations back to corresponding LLVM IR types.
OptimizedStructLayoutField Field
This file defines the SmallVector class.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:354
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
void push_back(const T &Elt)
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:483
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI Type * getFloatingPointTy(LLVMContext &C, const fltSemantics &S)
Definition Type.cpp:129
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
const Type * getElementType() const
Definition Types.h:204
bool isMatrixType() const
Definition Types.h:206
uint64_t getNumElements() const
Definition Types.h:205
const Type * getElementType() const
Definition Types.h:99
llvm::Type * convertType(const abi::Type *ABIType)
bool isFunctionPointer() const
Definition Types.h:183
bool isUnion() const
Definition Types.h:282
ArrayRef< FieldInfo > getFields() const
Definition Types.h:305
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:43
TypeSize getSizeInBits() const
Definition Types.h:67
Align getAlignment() const
Definition Types.h:68
ElementCount getNumElements() const
Definition Types.h:226
const Type * getElementType() const
Definition Types.h:225
This file defines the type system for the LLVMABI library, which mirrors ABI-relevant aspects of fron...
@ IsUnion
Definition Types.h:255
ElementType
The element type of an SRV or UAV resource.
Definition DXILABI.h:68
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559