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 TmpCost; 29 30 EXPECT_NE(VThree, VNegTwo); 31 EXPECT_GT(VThree, VNegTwo); 32 EXPECT_NE(VThree, IThreeA); 33 EXPECT_EQ(IThreeA, IThreeB); 34 EXPECT_GE(IThreeA, VNegTwo); 35 EXPECT_LT(VSix, IThreeA); 36 EXPECT_EQ(VSix - IThreeA, IThreeB); 37 EXPECT_EQ(VThree - VNegTwo, 5); 38 EXPECT_EQ(VThree * VNegTwo, -6); 39 EXPECT_EQ(VSix / VThree, 2); 40 41 EXPECT_FALSE(IThreeA.isValid()); 42 EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid); 43 44 TmpCost = VThree + IThreeA; 45 EXPECT_FALSE(TmpCost.isValid()); 46 47 // Test increments, decrements 48 EXPECT_EQ(++VThree, 4); 49 EXPECT_EQ(VThree++, 4); 50 EXPECT_EQ(VThree, 5); 51 EXPECT_EQ(--VThree, 4); 52 EXPECT_EQ(VThree--, 4); 53 EXPECT_EQ(VThree, 3); 54 55 TmpCost = VThree * IThreeA; 56 EXPECT_FALSE(TmpCost.isValid()); 57 58 // Test value extraction 59 EXPECT_EQ(*(VThree.getValue()), 3); 60 EXPECT_EQ(IThreeA.getValue(), None); 61 62 EXPECT_EQ(std::min(VThree, VNegTwo), -2); 63 EXPECT_EQ(std::max(VThree, VSix), 6); 64 } 65