1321369Sdim//===-- llvm/CodeGen/LowLevelType.cpp -------------------------------------===//
2311116Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6311116Sdim//
7311116Sdim//===----------------------------------------------------------------------===//
8311116Sdim//
9311116Sdim/// \file This file implements the more header-heavy bits of the LLT class to
10311116Sdim/// avoid polluting users' namespaces.
11311116Sdim//
12311116Sdim//===----------------------------------------------------------------------===//
13311116Sdim
14311116Sdim#include "llvm/CodeGen/LowLevelType.h"
15311116Sdim#include "llvm/IR/DataLayout.h"
16311116Sdim#include "llvm/IR/DerivedTypes.h"
17311116Sdim#include "llvm/Support/raw_ostream.h"
18311116Sdimusing namespace llvm;
19311116Sdim
20321369SdimLLT llvm::getLLTForType(Type &Ty, const DataLayout &DL) {
21311116Sdim  if (auto VTy = dyn_cast<VectorType>(&Ty)) {
22321369Sdim    auto NumElements = VTy->getNumElements();
23321369Sdim    LLT ScalarTy = getLLTForType(*VTy->getElementType(), DL);
24321369Sdim    if (NumElements == 1)
25321369Sdim      return ScalarTy;
26321369Sdim    return LLT::vector(NumElements, ScalarTy);
27360784Sdim  }
28360784Sdim
29360784Sdim  if (auto PTy = dyn_cast<PointerType>(&Ty)) {
30360784Sdim    unsigned AddrSpace = PTy->getAddressSpace();
31360784Sdim    return LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace));
32360784Sdim  }
33360784Sdim
34360784Sdim  if (Ty.isSized()) {
35311116Sdim    // Aggregates are no different from real scalars as far as GlobalISel is
36311116Sdim    // concerned.
37321369Sdim    auto SizeInBits = DL.getTypeSizeInBits(&Ty);
38311116Sdim    assert(SizeInBits != 0 && "invalid zero-sized type");
39321369Sdim    return LLT::scalar(SizeInBits);
40311116Sdim  }
41360784Sdim
42321369Sdim  return LLT();
43311116Sdim}
44360784Sdim
45360784SdimMVT llvm::getMVTForLLT(LLT Ty) {
46360784Sdim  if (!Ty.isVector())
47360784Sdim    return MVT::getIntegerVT(Ty.getSizeInBits());
48360784Sdim
49360784Sdim  return MVT::getVectorVT(
50360784Sdim      MVT::getIntegerVT(Ty.getElementType().getSizeInBits()),
51360784Sdim      Ty.getNumElements());
52360784Sdim}
53360784Sdim
54360784SdimLLT llvm::getLLTForMVT(MVT Ty) {
55360784Sdim  if (!Ty.isVector())
56360784Sdim    return LLT::scalar(Ty.getSizeInBits());
57360784Sdim
58360784Sdim  return LLT::vector(Ty.getVectorNumElements(),
59360784Sdim                     Ty.getVectorElementType().getSizeInBits());
60360784Sdim}
61