xref: /llvm-project/llvm/unittests/Support/InstructionCostTest.cpp (revision b481cd519e07b3ad2bd3e81c89b0dd8efd68d6bc)
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, Operators) {
23   InstructionCost VThree = 3;
24   InstructionCost VNegTwo = -2;
25   InstructionCost VSix = 6;
26   InstructionCost IThreeA = InstructionCost::getInvalid(3);
27   InstructionCost IThreeB = InstructionCost::getInvalid(3);
28   InstructionCost ITwo = InstructionCost::getInvalid(2);
29   InstructionCost TmpCost;
30 
31   EXPECT_NE(VThree, VNegTwo);
32   EXPECT_GT(VThree, VNegTwo);
33   EXPECT_NE(VThree, IThreeA);
34   EXPECT_EQ(IThreeA, IThreeB);
35   EXPECT_GE(IThreeA, VNegTwo);
36   EXPECT_LT(VSix, IThreeA);
37   EXPECT_EQ(VSix - IThreeA, IThreeB);
38   EXPECT_EQ(VThree - VNegTwo, 5);
39   EXPECT_EQ(VThree * VNegTwo, -6);
40   EXPECT_EQ(VSix / VThree, 2);
41   EXPECT_NE(IThreeA, ITwo);
42   EXPECT_LT(ITwo, IThreeA);
43   EXPECT_GT(IThreeA, ITwo);
44 
45   EXPECT_FALSE(IThreeA.isValid());
46   EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid);
47 
48   TmpCost = VThree + IThreeA;
49   EXPECT_FALSE(TmpCost.isValid());
50 
51   // Test increments, decrements
52   EXPECT_EQ(++VThree, 4);
53   EXPECT_EQ(VThree++, 4);
54   EXPECT_EQ(VThree, 5);
55   EXPECT_EQ(--VThree, 4);
56   EXPECT_EQ(VThree--, 4);
57   EXPECT_EQ(VThree, 3);
58 
59   TmpCost = VThree * IThreeA;
60   EXPECT_FALSE(TmpCost.isValid());
61 
62   // Test value extraction
63   EXPECT_EQ(*(VThree.getValue()), 3);
64   EXPECT_EQ(IThreeA.getValue(), None);
65 
66   EXPECT_EQ(std::min(VThree, VNegTwo), -2);
67   EXPECT_EQ(std::max(VThree, VSix), 6);
68 }
69