[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / InstructionCostTest.cpp
blobe31bf34233a2360b60229a0a4a686ea00c85d662
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/InstructionCost.h"
10 #include "gtest/gtest.h"
11 #include <limits>
13 using namespace llvm;
15 namespace {
17 struct CostTest : public testing::Test {
18 CostTest() {}
21 } // namespace
23 TEST_F(CostTest, DefaultCtor) {
24 InstructionCost DefaultCost;
26 ASSERT_TRUE(DefaultCost.isValid());
27 EXPECT_EQ(*(DefaultCost.getValue()), 0);
30 TEST_F(CostTest, Operators) {
32 InstructionCost VThree = 3;
33 InstructionCost VNegTwo = -2;
34 InstructionCost VSix = 6;
35 InstructionCost IThreeA = InstructionCost::getInvalid(3);
36 InstructionCost IThreeB = InstructionCost::getInvalid(3);
37 InstructionCost ITwo = InstructionCost::getInvalid(2);
38 InstructionCost TmpCost;
40 EXPECT_NE(VThree, VNegTwo);
41 EXPECT_GT(VThree, VNegTwo);
42 EXPECT_NE(VThree, IThreeA);
43 EXPECT_EQ(IThreeA, IThreeB);
44 EXPECT_GE(IThreeA, VNegTwo);
45 EXPECT_LT(VSix, IThreeA);
46 EXPECT_LT(VThree, ITwo);
47 EXPECT_GE(ITwo, VThree);
48 EXPECT_EQ(VSix - IThreeA, IThreeB);
49 EXPECT_EQ(VThree - VNegTwo, 5);
50 EXPECT_EQ(VThree * VNegTwo, -6);
51 EXPECT_EQ(VSix / VThree, 2);
52 EXPECT_NE(IThreeA, ITwo);
53 EXPECT_LT(ITwo, IThreeA);
54 EXPECT_GT(IThreeA, ITwo);
56 EXPECT_FALSE(IThreeA.isValid());
57 EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid);
59 TmpCost = VThree + IThreeA;
60 EXPECT_FALSE(TmpCost.isValid());
62 // Test increments, decrements
63 EXPECT_EQ(++VThree, 4);
64 EXPECT_EQ(VThree++, 4);
65 EXPECT_EQ(VThree, 5);
66 EXPECT_EQ(--VThree, 4);
67 EXPECT_EQ(VThree--, 4);
68 EXPECT_EQ(VThree, 3);
70 TmpCost = VThree * IThreeA;
71 EXPECT_FALSE(TmpCost.isValid());
73 // Test value extraction
74 EXPECT_EQ(*(VThree.getValue()), 3);
75 EXPECT_EQ(IThreeA.getValue(), None);
77 EXPECT_EQ(std::min(VThree, VNegTwo), -2);
78 EXPECT_EQ(std::max(VThree, VSix), 6);
80 // Test saturation
81 auto Max = InstructionCost::getMax();
82 auto Min = InstructionCost::getMin();
83 auto MinusOne = InstructionCost(-1);
84 auto MinusTwo = InstructionCost(-2);
85 auto One = InstructionCost(1);
86 auto Two = InstructionCost(2);
87 EXPECT_EQ(Max + One, Max);
88 EXPECT_EQ(Min + MinusOne, Min);
89 EXPECT_EQ(Min - One, Min);
90 EXPECT_EQ(Max - MinusOne, Max);
91 EXPECT_EQ(Max * Two, Max);
92 EXPECT_EQ(Min * Two, Min);
93 EXPECT_EQ(Max * MinusTwo, Min);
94 EXPECT_EQ(Min * MinusTwo, Max);