Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / Support / InstructionCostTest.cpp
blob4e2842d8ada9744a3bf046c60b236e5a76456b1d
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"
12 using namespace llvm;
14 namespace {
16 struct CostTest : public testing::Test {
17 CostTest() {}
20 } // namespace
22 TEST_F(CostTest, DefaultCtor) {
23 InstructionCost DefaultCost;
25 ASSERT_TRUE(DefaultCost.isValid());
26 EXPECT_EQ(*(DefaultCost.getValue()), 0);
29 TEST_F(CostTest, Operators) {
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;
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);
55 EXPECT_FALSE(IThreeA.isValid());
56 EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid);
58 TmpCost = VThree + IThreeA;
59 EXPECT_FALSE(TmpCost.isValid());
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);
69 TmpCost = VThree * IThreeA;
70 EXPECT_FALSE(TmpCost.isValid());
72 // Test value extraction
73 EXPECT_EQ(*(VThree.getValue()), 3);
74 EXPECT_EQ(IThreeA.getValue(), std::nullopt);
76 EXPECT_EQ(std::min(VThree, VNegTwo), -2);
77 EXPECT_EQ(std::max(VThree, VSix), 6);
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);