xref: /llvm-project/llvm/unittests/Support/InstructionCostTest.cpp (revision 073cc40a343bdb87b43a45598c947de52f6f2de5)
1 //===- InstructionCostTest.cpp - InstructionCost 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 "llvm/Support/InstructionCost.h"
10 #include "gtest/gtest.h"
11 
12 using namespace llvm;
13 
14 namespace {
15 
16 struct CostTest : public testing::Test {
CostTest__anona3147a460111::CostTest17   CostTest() {}
18 };
19 
20 } // namespace
21 
TEST_F(CostTest,DefaultCtor)22 TEST_F(CostTest, DefaultCtor) {
23   InstructionCost DefaultCost;
24 
25   ASSERT_TRUE(DefaultCost.isValid());
26   EXPECT_EQ(*(DefaultCost.getValue()), 0);
27 }
28 
TEST_F(CostTest,Operators)29 TEST_F(CostTest, Operators) {
30 
31   InstructionCost VThree = 3;
32   InstructionCost VNegTwo = -2;
33   InstructionCost VSix = 6;
34   InstructionCost IThreeA = InstructionCost::getInvalid(3);
35   InstructionCost IThreeB = InstructionCost::getInvalid(3);
36   InstructionCost ITwo = InstructionCost::getInvalid(2);
37   InstructionCost TmpCost;
38 
39   EXPECT_NE(VThree, VNegTwo);
40   EXPECT_GT(VThree, VNegTwo);
41   EXPECT_NE(VThree, IThreeA);
42   EXPECT_EQ(IThreeA, IThreeB);
43   EXPECT_GE(IThreeA, VNegTwo);
44   EXPECT_LT(VSix, IThreeA);
45   EXPECT_LT(VThree, ITwo);
46   EXPECT_GE(ITwo, VThree);
47   EXPECT_EQ(VSix - IThreeA, IThreeB);
48   EXPECT_EQ(VThree - VNegTwo, 5);
49   EXPECT_EQ(VThree * VNegTwo, -6);
50   EXPECT_EQ(VSix / VThree, 2);
51   EXPECT_NE(IThreeA, ITwo);
52   EXPECT_LT(ITwo, IThreeA);
53   EXPECT_GT(IThreeA, ITwo);
54 
55   EXPECT_FALSE(IThreeA.isValid());
56   EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid);
57 
58   TmpCost = VThree + IThreeA;
59   EXPECT_FALSE(TmpCost.isValid());
60 
61   // Test increments, decrements
62   EXPECT_EQ(++VThree, 4);
63   EXPECT_EQ(VThree++, 4);
64   EXPECT_EQ(VThree, 5);
65   EXPECT_EQ(--VThree, 4);
66   EXPECT_EQ(VThree--, 4);
67   EXPECT_EQ(VThree, 3);
68 
69   TmpCost = VThree * IThreeA;
70   EXPECT_FALSE(TmpCost.isValid());
71 
72   // Test value extraction
73   EXPECT_EQ(*(VThree.getValue()), 3);
74   EXPECT_EQ(IThreeA.getValue(), std::nullopt);
75 
76   EXPECT_EQ(std::min(VThree, VNegTwo), -2);
77   EXPECT_EQ(std::max(VThree, VSix), 6);
78 
79   // Test saturation
80   auto Max = InstructionCost::getMax();
81   auto Min = InstructionCost::getMin();
82   auto MinusOne = InstructionCost(-1);
83   auto MinusTwo = InstructionCost(-2);
84   auto One = InstructionCost(1);
85   auto Two = InstructionCost(2);
86   EXPECT_EQ(Max + One, Max);
87   EXPECT_EQ(Min + MinusOne, Min);
88   EXPECT_EQ(Min - One, Min);
89   EXPECT_EQ(Max - MinusOne, Max);
90   EXPECT_EQ(Max * Two, Max);
91   EXPECT_EQ(Min * Two, Min);
92   EXPECT_EQ(Max * MinusTwo, Min);
93   EXPECT_EQ(Min * MinusTwo, Max);
94 }
95