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"
17
18using namespace llvm::abi;
19
21 assert(ABIType && "convertType requires a non-null ABI type");
22
23 auto It = TypeCache.find(ABIType);
24 if (It != TypeCache.end())
25 return It->second;
26
27 llvm::Type *Result = nullptr;
28
29 switch (ABIType->getKind()) {
31 Result = llvm::Type::getVoidTy(Context);
32 break;
34 const auto *IT = cast<abi::IntegerType>(ABIType);
35 Result =
36 llvm::IntegerType::get(Context, IT->getSizeInBits().getFixedValue());
37 break;
38 }
40 const llvm::fltSemantics *Semantics =
41 cast<abi::FloatType>(ABIType)->getSemantics();
42 Result = llvm::Type::getFloatingPointTy(Context, *Semantics);
43 break;
44 }
47 Context, cast<abi::PointerType>(ABIType)->getAddrSpace());
48 break;
50 Result = convertArrayType(cast<abi::ArrayType>(ABIType));
51 break;
53 Result = convertVectorType(cast<abi::VectorType>(ABIType));
54 break;
56 Result = convertRecordType(cast<abi::RecordType>(ABIType));
57 break;
59 Result = convertComplexType(cast<abi::ComplexType>(ABIType));
60 break;
62 Result = convertMemberPointerType(cast<abi::MemberPointerType>(ABIType));
63 break;
64 }
65
66 TypeCache[ABIType] = Result;
67 return Result;
68}
69
70llvm::Type *IRTypeMapper::convertArrayType(const abi::ArrayType *AT) {
71 llvm::Type *ElementType = convertType(AT->getElementType());
72 uint64_t NumElements = AT->getNumElements();
73 if (AT->isMatrixType())
74 return llvm::VectorType::get(ElementType,
75 ElementCount::getFixed(NumElements));
76 return llvm::ArrayType::get(ElementType, NumElements);
77}
78
79llvm::Type *IRTypeMapper::convertVectorType(const abi::VectorType *VT) {
80 llvm::Type *ElementType = convertType(VT->getElementType());
81 return llvm::VectorType::get(ElementType, VT->getNumElements());
82}
83
84llvm::Type *IRTypeMapper::convertRecordType(const abi::RecordType *RT) {
85 return createStructFromFields(RT->getFields(), RT->getSizeInBits(),
86 RT->getAlignment(), RT->isUnion());
87}
88
89llvm::Type *IRTypeMapper::convertComplexType(const abi::ComplexType *CT) {
90 llvm::Type *ElementType = convertType(CT->getElementType());
91 llvm::Type *Fields[] = {ElementType, ElementType};
92 return llvm::StructType::get(Context, Fields, /*isPacked=*/false);
93}
94
95llvm::Type *
96IRTypeMapper::convertMemberPointerType(const abi::MemberPointerType *MPT) {
97 llvm::Type *IntPtrTy = DL.getIntPtrType(Context);
98 if (MPT->isFunctionPointer()) {
99 llvm::Type *Fields[] = {IntPtrTy, IntPtrTy};
100 return llvm::StructType::get(Context, Fields, /*isPacked=*/false);
101 }
102 return IntPtrTy;
103}
104
105llvm::Type *IRTypeMapper::createPaddingType(uint64_t PaddingBits) {
106 if (PaddingBits == 0)
107 return nullptr;
108 assert(PaddingBits % 8 == 0 &&
109 "sub-byte padding cannot be expressed as an llvm::Type");
111 PaddingBits / 8);
112}
113
114llvm::StructType *
115IRTypeMapper::createStructFromFields(ArrayRef<abi::FieldInfo> Fields,
116 TypeSize Size, Align Alignment,
117 bool IsUnion) {
118 SmallVector<llvm::Type *, 16> FieldTypes;
119
120 if (IsUnion) {
121 llvm::Type *LargestFieldType = nullptr;
122 uint64_t LargestFieldSize = 0;
123 for (const auto &Field : Fields) {
124 llvm::Type *FieldType = convertType(Field.FieldType);
125 uint64_t FieldSize = Field.FieldType->getSizeInBits().getFixedValue();
126 if (FieldSize > LargestFieldSize) {
127 LargestFieldSize = FieldSize;
128 LargestFieldType = FieldType;
129 }
130 }
131 if (LargestFieldType) {
132 FieldTypes.push_back(LargestFieldType);
133 uint64_t UnionSizeBits = Size.getFixedValue();
134 if (LargestFieldSize < UnionSizeBits) {
135 if (llvm::Type *PaddingType =
136 createPaddingType(UnionSizeBits - LargestFieldSize))
137 FieldTypes.push_back(PaddingType);
138 }
139 }
140 } else {
141 uint64_t CurrentOffset = 0;
142 for (const auto &Field : Fields) {
143 assert(!Field.IsBitField && "bitfields should not reach IR type mapping");
144 llvm::Type *FieldType = convertType(Field.FieldType);
145 if (Field.OffsetInBits > CurrentOffset) {
146 uint64_t AlignBits = DL.getABITypeAlign(FieldType).value() * 8;
147 uint64_t NaturalNextOffset =
148 AlignBits ? alignTo(CurrentOffset, AlignBits) : CurrentOffset;
149 if (NaturalNextOffset != Field.OffsetInBits) {
150 if (llvm::Type *PaddingType =
151 createPaddingType(Field.OffsetInBits - CurrentOffset))
152 FieldTypes.push_back(PaddingType);
153 }
154 CurrentOffset = Field.OffsetInBits;
155 }
156 FieldTypes.push_back(FieldType);
157 CurrentOffset += Field.FieldType->getSizeInBits().getFixedValue();
158 }
159 uint64_t TotalSizeBits = Size.getFixedValue();
160 if (CurrentOffset < TotalSizeBits) {
161 if (llvm::Type *PaddingType =
162 createPaddingType(TotalSizeBits - CurrentOffset))
163 FieldTypes.push_back(PaddingType);
164 }
165 }
166
167 return StructType::get(Context, FieldTypes, /*isPacked=*/false);
168}
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:348
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:477
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:282
static LLVM_ABI Type * getFloatingPointTy(LLVMContext &C, const fltSemantics &S)
Definition Type.cpp:125
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:205
bool isMatrixType() const
Definition Types.h:207
uint64_t getNumElements() const
Definition Types.h:206
const Type * getElementType() const
Definition Types.h:100
LLVM_ABI llvm::Type * convertType(const abi::Type *ABIType)
bool isFunctionPointer() const
Definition Types.h:184
bool isUnion() const
Definition Types.h:283
ArrayRef< FieldInfo > getFields() const
Definition Types.h:306
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:44
TypeSize getSizeInBits() const
Definition Types.h:68
Align getAlignment() const
Definition Types.h:69
ElementCount getNumElements() const
Definition Types.h:227
const Type * getElementType() const
Definition Types.h:226
This file defines the type system for the LLVMABI library, which mirrors ABI-relevant aspects of fron...
@ IsUnion
Definition Types.h:256
ElementType
The element type of an SRV or UAV resource.
Definition DXILABI.h:68
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
IntPtrTy
Definition InstrProf.h:82
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