1308e82ecSMichael Platings //===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
2308e82ecSMichael Platings //
3308e82ecSMichael Platings // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4308e82ecSMichael Platings // See https://llvm.org/LICENSE.txt for license information.
5308e82ecSMichael Platings // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6308e82ecSMichael Platings //
7308e82ecSMichael Platings //===----------------------------------------------------------------------===//
8308e82ecSMichael Platings
9308e82ecSMichael Platings #include "llvm/IR/DataLayout.h"
103bc41575SAlex Richardson #include "llvm/IR/GlobalVariable.h"
11c7a76d6bSGuillaume Chatelet #include "llvm/IR/LLVMContext.h"
123bc41575SAlex Richardson #include "llvm/IR/Module.h"
13c7a76d6bSGuillaume Chatelet #include "llvm/IR/Type.h"
14*8002fa67SFraser Cormack #include "llvm/Testing/Support/Error.h"
15308e82ecSMichael Platings #include "gtest/gtest.h"
16308e82ecSMichael Platings
17308e82ecSMichael Platings using namespace llvm;
18308e82ecSMichael Platings
19308e82ecSMichael Platings namespace {
20308e82ecSMichael Platings
TEST(DataLayoutTest,FunctionPtrAlign)21308e82ecSMichael Platings TEST(DataLayoutTest, FunctionPtrAlign) {
2265e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(0), DataLayout("").getFunctionPtrAlign());
2365e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(1), DataLayout("Fi8").getFunctionPtrAlign());
2465e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(2), DataLayout("Fi16").getFunctionPtrAlign());
2565e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(4), DataLayout("Fi32").getFunctionPtrAlign());
2665e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(8), DataLayout("Fi64").getFunctionPtrAlign());
2765e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(1), DataLayout("Fn8").getFunctionPtrAlign());
2865e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(2), DataLayout("Fn16").getFunctionPtrAlign());
2965e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(4), DataLayout("Fn32").getFunctionPtrAlign());
3065e4b47aSGuillaume Chatelet EXPECT_EQ(MaybeAlign(8), DataLayout("Fn64").getFunctionPtrAlign());
31308e82ecSMichael Platings EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \
32308e82ecSMichael Platings DataLayout("").getFunctionPtrAlignType());
33308e82ecSMichael Platings EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent, \
34308e82ecSMichael Platings DataLayout("Fi8").getFunctionPtrAlignType());
35308e82ecSMichael Platings EXPECT_EQ(DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign, \
36308e82ecSMichael Platings DataLayout("Fn8").getFunctionPtrAlignType());
37308e82ecSMichael Platings EXPECT_EQ(DataLayout("Fi8"), DataLayout("Fi8"));
38308e82ecSMichael Platings EXPECT_NE(DataLayout("Fi8"), DataLayout("Fi16"));
39308e82ecSMichael Platings EXPECT_NE(DataLayout("Fi8"), DataLayout("Fn8"));
40308e82ecSMichael Platings
41308e82ecSMichael Platings DataLayout a(""), b("Fi8"), c("Fn8");
42308e82ecSMichael Platings EXPECT_NE(a, b);
43308e82ecSMichael Platings EXPECT_NE(a, c);
44308e82ecSMichael Platings EXPECT_NE(b, c);
45308e82ecSMichael Platings
46308e82ecSMichael Platings a = b;
47308e82ecSMichael Platings EXPECT_EQ(a, b);
48308e82ecSMichael Platings a = c;
49308e82ecSMichael Platings EXPECT_EQ(a, c);
50308e82ecSMichael Platings }
51308e82ecSMichael Platings
TEST(DataLayoutTest,ValueOrABITypeAlignment)52c7a76d6bSGuillaume Chatelet TEST(DataLayoutTest, ValueOrABITypeAlignment) {
53c7a76d6bSGuillaume Chatelet const DataLayout DL("Fi8");
54c7a76d6bSGuillaume Chatelet LLVMContext Context;
55c7a76d6bSGuillaume Chatelet Type *const FourByteAlignType = Type::getInt32Ty(Context);
56c7a76d6bSGuillaume Chatelet EXPECT_EQ(Align(16),
57c7a76d6bSGuillaume Chatelet DL.getValueOrABITypeAlignment(MaybeAlign(16), FourByteAlignType));
58c7a76d6bSGuillaume Chatelet EXPECT_EQ(Align(4),
59c7a76d6bSGuillaume Chatelet DL.getValueOrABITypeAlignment(MaybeAlign(), FourByteAlignType));
60c7a76d6bSGuillaume Chatelet }
61c7a76d6bSGuillaume Chatelet
TEST(DataLayoutTest,GlobalsAddressSpace)623bc41575SAlex Richardson TEST(DataLayoutTest, GlobalsAddressSpace) {
633bc41575SAlex Richardson // When not explicitly defined the globals address space should be zero:
643bc41575SAlex Richardson EXPECT_EQ(DataLayout("").getDefaultGlobalsAddressSpace(), 0u);
653bc41575SAlex Richardson EXPECT_EQ(DataLayout("P1-A2").getDefaultGlobalsAddressSpace(), 0u);
663bc41575SAlex Richardson EXPECT_EQ(DataLayout("G2").getDefaultGlobalsAddressSpace(), 2u);
673bc41575SAlex Richardson // Check that creating a GlobalVariable without an explicit address space
683bc41575SAlex Richardson // in a module with a default globals address space respects that default:
693bc41575SAlex Richardson LLVMContext Context;
703bc41575SAlex Richardson std::unique_ptr<Module> M(new Module("MyModule", Context));
713bc41575SAlex Richardson // Default is globals in address space zero:
723bc41575SAlex Richardson auto *Int32 = Type::getInt32Ty(Context);
733bc41575SAlex Richardson auto *DefaultGlobal1 = new GlobalVariable(
743bc41575SAlex Richardson *M, Int32, false, GlobalValue::ExternalLinkage, nullptr);
753bc41575SAlex Richardson EXPECT_EQ(DefaultGlobal1->getAddressSpace(), 0u);
763bc41575SAlex Richardson auto *ExplicitGlobal1 = new GlobalVariable(
773bc41575SAlex Richardson *M, Int32, false, GlobalValue::ExternalLinkage, nullptr, "", nullptr,
783bc41575SAlex Richardson GlobalValue::NotThreadLocal, 123);
793bc41575SAlex Richardson EXPECT_EQ(ExplicitGlobal1->getAddressSpace(), 123u);
803bc41575SAlex Richardson
813bc41575SAlex Richardson // When using a datalayout with the global address space set to 200, global
823bc41575SAlex Richardson // variables should default to 200
833bc41575SAlex Richardson M->setDataLayout("G200");
843bc41575SAlex Richardson auto *DefaultGlobal2 = new GlobalVariable(
853bc41575SAlex Richardson *M, Int32, false, GlobalValue::ExternalLinkage, nullptr);
863bc41575SAlex Richardson EXPECT_EQ(DefaultGlobal2->getAddressSpace(), 200u);
873bc41575SAlex Richardson auto *ExplicitGlobal2 = new GlobalVariable(
883bc41575SAlex Richardson *M, Int32, false, GlobalValue::ExternalLinkage, nullptr, "", nullptr,
893bc41575SAlex Richardson GlobalValue::NotThreadLocal, 123);
903bc41575SAlex Richardson EXPECT_EQ(ExplicitGlobal2->getAddressSpace(), 123u);
913bc41575SAlex Richardson }
923bc41575SAlex Richardson
TEST(DataLayoutTest,VectorAlign)93*8002fa67SFraser Cormack TEST(DataLayoutTest, VectorAlign) {
94*8002fa67SFraser Cormack Expected<DataLayout> DL = DataLayout::parse("v64:64");
95*8002fa67SFraser Cormack EXPECT_THAT_EXPECTED(DL, Succeeded());
96*8002fa67SFraser Cormack
97*8002fa67SFraser Cormack LLVMContext Context;
98*8002fa67SFraser Cormack Type *const FloatTy = Type::getFloatTy(Context);
99*8002fa67SFraser Cormack Type *const V8F32Ty = FixedVectorType::get(FloatTy, 8);
100*8002fa67SFraser Cormack
101*8002fa67SFraser Cormack // The alignment for a vector type larger than any specified vector type uses
102*8002fa67SFraser Cormack // the natural alignment as a fallback.
103*8002fa67SFraser Cormack EXPECT_EQ(Align(4 * 8), DL->getABITypeAlign(V8F32Ty));
104*8002fa67SFraser Cormack EXPECT_EQ(Align(4 * 8), DL->getPrefTypeAlign(V8F32Ty));
105*8002fa67SFraser Cormack }
106*8002fa67SFraser Cormack
107308e82ecSMichael Platings } // anonymous namespace
108