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 { 15 std::vector<MCPhysReg> loadOrder(AllocationOrder &O, unsigned Limit = 0) { 16 std::vector<MCPhysReg> Ret; 17 O.rewind(); 18 while (auto R = O.next(Limit)) 19 Ret.push_back(R); 20 return Ret; 21 } 22 } // namespace 23 24 TEST(AllocationOrderTest, Basic) { 25 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 26 SmallVector<MCPhysReg, 16> Order = {4, 5, 6, 7}; 27 AllocationOrder O(std::move(Hints), Order, false); 28 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4, 5, 6, 7}), loadOrder(O)); 29 } 30 31 TEST(AllocationOrderTest, Duplicates) { 32 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 33 SmallVector<MCPhysReg, 16> Order = {4, 1, 5, 6}; 34 AllocationOrder O(std::move(Hints), Order, false); 35 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4, 5, 6}), loadOrder(O)); 36 } 37 38 TEST(AllocationOrderTest, HardHints) { 39 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 40 SmallVector<MCPhysReg, 16> Order = {4, 5, 6, 7}; 41 AllocationOrder O(std::move(Hints), Order, true); 42 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3}), loadOrder(O)); 43 } 44 45 TEST(AllocationOrderTest, LimitsBasic) { 46 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 47 SmallVector<MCPhysReg, 16> Order = {4, 5, 6, 7}; 48 AllocationOrder O(std::move(Hints), Order, false); 49 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4, 5, 6, 7}), loadOrder(O, 0)); 50 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4}), loadOrder(O, 1)); 51 } 52 53 TEST(AllocationOrderTest, LimitsDuplicates) { 54 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 55 SmallVector<MCPhysReg, 16> Order = {4, 1, 5, 6}; 56 AllocationOrder O(std::move(Hints), Order, false); 57 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4}), loadOrder(O, 1)); 58 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4}), loadOrder(O, 2)); 59 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4, 5}), loadOrder(O, 3)); 60 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4, 5, 6}), loadOrder(O, 4)); 61 } 62 63 TEST(AllocationOrderTest, LimitsHardHints) { 64 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 65 SmallVector<MCPhysReg, 16> Order = {4, 1, 5, 6}; 66 AllocationOrder O(std::move(Hints), Order, true); 67 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3}), loadOrder(O, 1)); 68 } 69 70 TEST(AllocationOrderTest, DuplicateIsFirst) { 71 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 72 SmallVector<MCPhysReg, 16> Order = {1, 4, 5, 6}; 73 AllocationOrder O(std::move(Hints), Order, false); 74 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4, 5, 6}), loadOrder(O)); 75 } 76 77 TEST(AllocationOrderTest, DuplicateIsFirstWithLimits) { 78 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 79 SmallVector<MCPhysReg, 16> Order = {1, 4, 5, 6}; 80 AllocationOrder O(std::move(Hints), Order, false); 81 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3}), loadOrder(O, 1)); 82 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4}), loadOrder(O, 2)); 83 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4, 5}), loadOrder(O, 3)); 84 } 85 86 TEST(AllocationOrderTest, NoHints) { 87 SmallVector<MCPhysReg, 16> Hints; 88 SmallVector<MCPhysReg, 16> Order = {1, 2, 3, 4}; 89 AllocationOrder O(std::move(Hints), Order, false); 90 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4}), loadOrder(O)); 91 EXPECT_EQ((std::vector<MCPhysReg>{1, 2}), loadOrder(O, 2)); 92 EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3}), loadOrder(O, 3)); 93 } 94 95 TEST(AllocationOrderTest, IsHintTest) { 96 SmallVector<MCPhysReg, 16> Hints = {1, 2, 3}; 97 SmallVector<MCPhysReg, 16> Order = {4, 1, 5, 6}; 98 AllocationOrder O(std::move(Hints), Order, false); 99 O.rewind(); 100 auto V = O.next(); 101 EXPECT_TRUE(O.isHint()); 102 EXPECT_EQ(V, 1U); 103 O.next(); 104 EXPECT_TRUE(O.isHint()); 105 O.next(); 106 EXPECT_TRUE(O.isHint()); 107 V = O.next(); 108 EXPECT_FALSE(O.isHint()); 109 EXPECT_EQ(V, 4U); 110 V = O.next(); 111 EXPECT_TRUE(O.isHint(1)); 112 EXPECT_FALSE(O.isHint()); 113 EXPECT_EQ(V, 5U); 114 } 115