1 //===- llvm/unittest/CodeGen/AllocationOrderTest.cpp - AllocationOrder 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 "../lib/CodeGen/AllocationOrder.h"
10 #include "gtest/gtest.h"
15 std::vector
<MCPhysReg
> loadOrder(const AllocationOrder
&O
, unsigned Limit
= 0) {
16 std::vector
<MCPhysReg
> Ret
;
21 for (auto I
= O
.begin(), E
= O
.getOrderLimitEnd(Limit
); I
!= E
; ++I
)
27 TEST(AllocationOrderTest
, Basic
) {
28 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
29 SmallVector
<MCPhysReg
, 16> Order
= {4, 5, 6, 7};
30 AllocationOrder
O(std::move(Hints
), Order
, false);
31 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4, 5, 6, 7}), loadOrder(O
));
34 TEST(AllocationOrderTest
, Duplicates
) {
35 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
36 SmallVector
<MCPhysReg
, 16> Order
= {4, 1, 5, 6};
37 AllocationOrder
O(std::move(Hints
), Order
, false);
38 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4, 5, 6}), loadOrder(O
));
41 TEST(AllocationOrderTest
, HardHints
) {
42 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
43 SmallVector
<MCPhysReg
, 16> Order
= {4, 5, 6, 7};
44 AllocationOrder
O(std::move(Hints
), Order
, true);
45 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3}), loadOrder(O
));
48 TEST(AllocationOrderTest
, LimitsBasic
) {
49 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
50 SmallVector
<MCPhysReg
, 16> Order
= {4, 5, 6, 7};
51 AllocationOrder
O(std::move(Hints
), Order
, false);
52 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4, 5, 6, 7}), loadOrder(O
, 0));
53 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4}), loadOrder(O
, 1));
54 EXPECT_EQ(O
.end(), O
.getOrderLimitEnd(0));
57 TEST(AllocationOrderTest
, LimitsDuplicates
) {
58 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
59 SmallVector
<MCPhysReg
, 16> Order
= {4, 1, 5, 6};
60 AllocationOrder
O(std::move(Hints
), Order
, false);
61 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4}), loadOrder(O
, 1));
62 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4}), loadOrder(O
, 2));
63 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4, 5}), loadOrder(O
, 3));
64 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4, 5, 6}), loadOrder(O
, 4));
67 TEST(AllocationOrderTest
, LimitsHardHints
) {
68 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
69 SmallVector
<MCPhysReg
, 16> Order
= {4, 1, 5, 6};
70 AllocationOrder
O(std::move(Hints
), Order
, true);
71 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3}), loadOrder(O
, 1));
74 TEST(AllocationOrderTest
, DuplicateIsFirst
) {
75 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
76 SmallVector
<MCPhysReg
, 16> Order
= {1, 4, 5, 6};
77 AllocationOrder
O(std::move(Hints
), Order
, false);
78 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4, 5, 6}), loadOrder(O
));
81 TEST(AllocationOrderTest
, DuplicateIsFirstWithLimits
) {
82 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
83 SmallVector
<MCPhysReg
, 16> Order
= {1, 4, 5, 6};
84 AllocationOrder
O(std::move(Hints
), Order
, false);
85 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3}), loadOrder(O
, 1));
86 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4}), loadOrder(O
, 2));
87 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4, 5}), loadOrder(O
, 3));
90 TEST(AllocationOrderTest
, NoHints
) {
91 SmallVector
<MCPhysReg
, 16> Hints
;
92 SmallVector
<MCPhysReg
, 16> Order
= {1, 2, 3, 4};
93 AllocationOrder
O(std::move(Hints
), Order
, false);
94 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3, 4}), loadOrder(O
));
95 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2}), loadOrder(O
, 2));
96 EXPECT_EQ((std::vector
<MCPhysReg
>{1, 2, 3}), loadOrder(O
, 3));
99 TEST(AllocationOrderTest
, IsHintTest
) {
100 SmallVector
<MCPhysReg
, 16> Hints
= {1, 2, 3};
101 SmallVector
<MCPhysReg
, 16> Order
= {4, 1, 5, 6};
102 AllocationOrder
O(std::move(Hints
), Order
, false);
105 EXPECT_TRUE(I
.isHint());
108 EXPECT_TRUE(I
.isHint());
110 EXPECT_TRUE(I
.isHint());
112 EXPECT_FALSE(I
.isHint());
115 EXPECT_TRUE(O
.isHint(1));
116 EXPECT_FALSE(I
.isHint());