1 //===- llvm/unittest/CodeGen/AllocationOrderTest.cpp - AllocationOrder tests =//
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 #include "../lib/CodeGen/AllocationOrder.h"
10 #include "gtest/gtest.h"
11 
12 using namespace llvm;
13 
14 namespace {
loadOrder(const AllocationOrder & O,unsigned Limit=0)15 std::vector<MCPhysReg> loadOrder(const AllocationOrder &O, unsigned Limit = 0) {
16   std::vector<MCPhysReg> Ret;
17   if (Limit == 0)
18     for (auto R : O)
19       Ret.push_back(R);
20   else
21     for (auto I = O.begin(), E = O.getOrderLimitEnd(Limit); I != E; ++I)
22       Ret.push_back(*I);
23   return Ret;
24 }
25 } // namespace
26 
TEST(AllocationOrderTest,Basic)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));
32 }
33 
TEST(AllocationOrderTest,Duplicates)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));
39 }
40 
TEST(AllocationOrderTest,HardHints)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));
46 }
47 
TEST(AllocationOrderTest,LimitsBasic)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));
55 }
56 
TEST(AllocationOrderTest,LimitsDuplicates)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));
65 }
66 
TEST(AllocationOrderTest,LimitsHardHints)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));
72 }
73 
TEST(AllocationOrderTest,DuplicateIsFirst)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));
79 }
80 
TEST(AllocationOrderTest,DuplicateIsFirstWithLimits)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));
88 }
89 
TEST(AllocationOrderTest,NoHints)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));
97 }
98 
TEST(AllocationOrderTest,IsHintTest)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);
103   auto I = O.begin();
104   auto V = *I;
105   EXPECT_TRUE(I.isHint());
106   EXPECT_EQ(V, 1U);
107   ++I;
108   EXPECT_TRUE(I.isHint());
109   ++I;
110   EXPECT_TRUE(I.isHint());
111   V = *(++I);
112   EXPECT_FALSE(I.isHint());
113   EXPECT_EQ(V, 4U);
114   V = *(++I);
115   EXPECT_TRUE(O.isHint(1));
116   EXPECT_FALSE(I.isHint());
117   EXPECT_EQ(V, 5U);
118 }
119