1 //===-- llvm/CodeGen/LowLevelType.cpp -------------------------------------===//
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 /// \file This file implements the more header-heavy bits of the LLT class to
10 /// avoid polluting users' namespaces.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/LowLevelType.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm;
20 
21 LLT llvm::getLLTForType(Type &Ty, const DataLayout &DL) {
22   if (auto VTy = dyn_cast<VectorType>(&Ty)) {
23     auto NumElements = cast<FixedVectorType>(VTy)->getNumElements();
24     LLT ScalarTy = getLLTForType(*VTy->getElementType(), DL);
25     if (NumElements == 1)
26       return ScalarTy;
27     return LLT::vector(NumElements, ScalarTy);
28   }
29 
30   if (auto PTy = dyn_cast<PointerType>(&Ty)) {
31     unsigned AddrSpace = PTy->getAddressSpace();
32     return LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace));
33   }
34 
35   if (Ty.isSized()) {
36     // Aggregates are no different from real scalars as far as GlobalISel is
37     // concerned.
38     auto SizeInBits = DL.getTypeSizeInBits(&Ty);
39     assert(SizeInBits != 0 && "invalid zero-sized type");
40     return LLT::scalar(SizeInBits);
41   }
42 
43   return LLT();
44 }
45 
46 MVT llvm::getMVTForLLT(LLT Ty) {
47   if (!Ty.isVector())
48     return MVT::getIntegerVT(Ty.getSizeInBits());
49 
50   return MVT::getVectorVT(
51       MVT::getIntegerVT(Ty.getElementType().getSizeInBits()),
52       Ty.getNumElements());
53 }
54 
55 LLT llvm::getLLTForMVT(MVT Ty) {
56   if (!Ty.isVector())
57     return LLT::scalar(Ty.getSizeInBits());
58 
59   return LLT::vector(Ty.getVectorNumElements(),
60                      Ty.getVectorElementType().getSizeInBits());
61 }
62 
63 const llvm::fltSemantics &llvm::getFltSemanticForLLT(LLT Ty) {
64   assert(Ty.isScalar() && "Expected a scalar type.");
65   switch (Ty.getSizeInBits()) {
66   case 16:
67     return APFloat::IEEEhalf();
68   case 32:
69     return APFloat::IEEEsingle();
70   case 64:
71     return APFloat::IEEEdouble();
72   case 128:
73     return APFloat::IEEEquad();
74   }
75   llvm_unreachable("Invalid FP type size.");
76 }
77