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 #include <limits> 12 13 using namespace llvm; 14 15 namespace { 16 17 struct CostTest : public testing::Test { 18 CostTest() {} 19 }; 20 21 } // namespace 22 23 TEST_F(CostTest, DefaultCtor) { 24 InstructionCost DefaultCost; 25 26 ASSERT_TRUE(DefaultCost.isValid()); 27 EXPECT_EQ(*(DefaultCost.getValue()), 0); 28 } 29 30 TEST_F(CostTest, Operators) { 31 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; 39 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); 55 56 EXPECT_FALSE(IThreeA.isValid()); 57 EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid); 58 59 TmpCost = VThree + IThreeA; 60 EXPECT_FALSE(TmpCost.isValid()); 61 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); 69 70 TmpCost = VThree * IThreeA; 71 EXPECT_FALSE(TmpCost.isValid()); 72 73 // Test value extraction 74 EXPECT_EQ(*(VThree.getValue()), 3); 75 EXPECT_EQ(IThreeA.getValue(), std::nullopt); 76 77 EXPECT_EQ(std::min(VThree, VNegTwo), -2); 78 EXPECT_EQ(std::max(VThree, VSix), 6); 79 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); 95 } 96