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 {
17   CostTest() {}
18 };
19 
20 } // namespace
21 
22 TEST_F(CostTest, DefaultCtor) {
23   InstructionCost DefaultCost;
24 
25   ASSERT_TRUE(DefaultCost.isValid());
26   EXPECT_EQ(*(DefaultCost.getValue()), 0);
27 }
28 
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(), None);
75 
76   EXPECT_EQ(std::min(VThree, VNegTwo), -2);
77   EXPECT_EQ(std::max(VThree, VSix), 6);
78 }
79