1 //===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/DataLayout.h"
10 #include "llvm/IR/GlobalVariable.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/IR/Type.h"
14 #include "llvm/TargetParser/Triple.h"
15 #include "llvm/Testing/Support/Error.h"
16 #include "gtest/gtest.h"
22 TEST(DataLayoutTest
, FunctionPtrAlign
) {
23 EXPECT_EQ(MaybeAlign(0), DataLayout("").getFunctionPtrAlign());
24 EXPECT_EQ(MaybeAlign(1), DataLayout("Fi8").getFunctionPtrAlign());
25 EXPECT_EQ(MaybeAlign(2), DataLayout("Fi16").getFunctionPtrAlign());
26 EXPECT_EQ(MaybeAlign(4), DataLayout("Fi32").getFunctionPtrAlign());
27 EXPECT_EQ(MaybeAlign(8), DataLayout("Fi64").getFunctionPtrAlign());
28 EXPECT_EQ(MaybeAlign(1), DataLayout("Fn8").getFunctionPtrAlign());
29 EXPECT_EQ(MaybeAlign(2), DataLayout("Fn16").getFunctionPtrAlign());
30 EXPECT_EQ(MaybeAlign(4), DataLayout("Fn32").getFunctionPtrAlign());
31 EXPECT_EQ(MaybeAlign(8), DataLayout("Fn64").getFunctionPtrAlign());
32 EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent
, \
33 DataLayout("").getFunctionPtrAlignType());
34 EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent
, \
35 DataLayout("Fi8").getFunctionPtrAlignType());
36 EXPECT_EQ(DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign
, \
37 DataLayout("Fn8").getFunctionPtrAlignType());
38 EXPECT_EQ(DataLayout("Fi8"), DataLayout("Fi8"));
39 EXPECT_NE(DataLayout("Fi8"), DataLayout("Fi16"));
40 EXPECT_NE(DataLayout("Fi8"), DataLayout("Fn8"));
42 DataLayout
a(""), b("Fi8"), c("Fn8");
53 TEST(DataLayoutTest
, ValueOrABITypeAlignment
) {
54 const DataLayout
DL("Fi8");
56 Type
*const FourByteAlignType
= Type::getInt32Ty(Context
);
58 DL
.getValueOrABITypeAlignment(MaybeAlign(16), FourByteAlignType
));
60 DL
.getValueOrABITypeAlignment(MaybeAlign(), FourByteAlignType
));
63 TEST(DataLayoutTest
, GlobalsAddressSpace
) {
64 // When not explicitly defined the globals address space should be zero:
65 EXPECT_EQ(DataLayout("").getDefaultGlobalsAddressSpace(), 0u);
66 EXPECT_EQ(DataLayout("P1-A2").getDefaultGlobalsAddressSpace(), 0u);
67 EXPECT_EQ(DataLayout("G2").getDefaultGlobalsAddressSpace(), 2u);
68 // Check that creating a GlobalVariable without an explicit address space
69 // in a module with a default globals address space respects that default:
71 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
72 // Default is globals in address space zero:
73 auto *Int32
= Type::getInt32Ty(Context
);
74 auto *DefaultGlobal1
= new GlobalVariable(
75 *M
, Int32
, false, GlobalValue::ExternalLinkage
, nullptr);
76 EXPECT_EQ(DefaultGlobal1
->getAddressSpace(), 0u);
77 auto *ExplicitGlobal1
= new GlobalVariable(
78 *M
, Int32
, false, GlobalValue::ExternalLinkage
, nullptr, "", nullptr,
79 GlobalValue::NotThreadLocal
, 123);
80 EXPECT_EQ(ExplicitGlobal1
->getAddressSpace(), 123u);
82 // When using a datalayout with the global address space set to 200, global
83 // variables should default to 200
84 M
->setDataLayout("G200");
85 auto *DefaultGlobal2
= new GlobalVariable(
86 *M
, Int32
, false, GlobalValue::ExternalLinkage
, nullptr);
87 EXPECT_EQ(DefaultGlobal2
->getAddressSpace(), 200u);
88 auto *ExplicitGlobal2
= new GlobalVariable(
89 *M
, Int32
, false, GlobalValue::ExternalLinkage
, nullptr, "", nullptr,
90 GlobalValue::NotThreadLocal
, 123);
91 EXPECT_EQ(ExplicitGlobal2
->getAddressSpace(), 123u);
94 TEST(DataLayoutTest
, VectorAlign
) {
95 Expected
<DataLayout
> DL
= DataLayout::parse("v64:64");
96 EXPECT_THAT_EXPECTED(DL
, Succeeded());
99 Type
*const FloatTy
= Type::getFloatTy(Context
);
100 Type
*const V8F32Ty
= FixedVectorType::get(FloatTy
, 8);
102 // The alignment for a vector type larger than any specified vector type uses
103 // the natural alignment as a fallback.
104 EXPECT_EQ(Align(4 * 8), DL
->getABITypeAlign(V8F32Ty
));
105 EXPECT_EQ(Align(4 * 8), DL
->getPrefTypeAlign(V8F32Ty
));
108 TEST(DataLayoutTest
, UEFI
) {
109 Triple TT
= Triple("x86_64-unknown-uefi");
111 // Test UEFI X86_64 Mangling Component.
112 EXPECT_STREQ(DataLayout::getManglingComponent(TT
), "-m:w");
115 } // anonymous namespace