19b76160eSDavid Sherwood //===- InstructionCostTest.cpp - InstructionCost tests --------------------===//
29b76160eSDavid Sherwood //
39b76160eSDavid Sherwood // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49b76160eSDavid Sherwood // See https://llvm.org/LICENSE.txt for license information.
59b76160eSDavid Sherwood // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69b76160eSDavid Sherwood //
79b76160eSDavid Sherwood //===----------------------------------------------------------------------===//
89b76160eSDavid Sherwood
99b76160eSDavid Sherwood #include "llvm/Support/InstructionCost.h"
109b76160eSDavid Sherwood #include "gtest/gtest.h"
119b76160eSDavid Sherwood
129b76160eSDavid Sherwood using namespace llvm;
139b76160eSDavid Sherwood
149b76160eSDavid Sherwood namespace {
159b76160eSDavid Sherwood
169b76160eSDavid Sherwood struct CostTest : public testing::Test {
CostTest__anona3147a460111::CostTest179b76160eSDavid Sherwood CostTest() {}
189b76160eSDavid Sherwood };
199b76160eSDavid Sherwood
209b76160eSDavid Sherwood } // namespace
219b76160eSDavid Sherwood
TEST_F(CostTest,DefaultCtor)22b8b054aaSChristopher Tetreault TEST_F(CostTest, DefaultCtor) {
23b8b054aaSChristopher Tetreault InstructionCost DefaultCost;
24b8b054aaSChristopher Tetreault
25b8b054aaSChristopher Tetreault ASSERT_TRUE(DefaultCost.isValid());
26b8b054aaSChristopher Tetreault EXPECT_EQ(*(DefaultCost.getValue()), 0);
27b8b054aaSChristopher Tetreault }
28b8b054aaSChristopher Tetreault
TEST_F(CostTest,Operators)299b76160eSDavid Sherwood TEST_F(CostTest, Operators) {
30b8b054aaSChristopher Tetreault
319b76160eSDavid Sherwood InstructionCost VThree = 3;
329b76160eSDavid Sherwood InstructionCost VNegTwo = -2;
339b76160eSDavid Sherwood InstructionCost VSix = 6;
349b76160eSDavid Sherwood InstructionCost IThreeA = InstructionCost::getInvalid(3);
359b76160eSDavid Sherwood InstructionCost IThreeB = InstructionCost::getInvalid(3);
36b8b054aaSChristopher Tetreault InstructionCost ITwo = InstructionCost::getInvalid(2);
379b76160eSDavid Sherwood InstructionCost TmpCost;
389b76160eSDavid Sherwood
399b76160eSDavid Sherwood EXPECT_NE(VThree, VNegTwo);
409b76160eSDavid Sherwood EXPECT_GT(VThree, VNegTwo);
419b76160eSDavid Sherwood EXPECT_NE(VThree, IThreeA);
429b76160eSDavid Sherwood EXPECT_EQ(IThreeA, IThreeB);
439b76160eSDavid Sherwood EXPECT_GE(IThreeA, VNegTwo);
449b76160eSDavid Sherwood EXPECT_LT(VSix, IThreeA);
45b8b054aaSChristopher Tetreault EXPECT_LT(VThree, ITwo);
46b8b054aaSChristopher Tetreault EXPECT_GE(ITwo, VThree);
479b76160eSDavid Sherwood EXPECT_EQ(VSix - IThreeA, IThreeB);
489b76160eSDavid Sherwood EXPECT_EQ(VThree - VNegTwo, 5);
499b76160eSDavid Sherwood EXPECT_EQ(VThree * VNegTwo, -6);
509b76160eSDavid Sherwood EXPECT_EQ(VSix / VThree, 2);
51b8b054aaSChristopher Tetreault EXPECT_NE(IThreeA, ITwo);
52b8b054aaSChristopher Tetreault EXPECT_LT(ITwo, IThreeA);
53b8b054aaSChristopher Tetreault EXPECT_GT(IThreeA, ITwo);
549b76160eSDavid Sherwood
559b76160eSDavid Sherwood EXPECT_FALSE(IThreeA.isValid());
569b76160eSDavid Sherwood EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid);
579b76160eSDavid Sherwood
589b76160eSDavid Sherwood TmpCost = VThree + IThreeA;
599b76160eSDavid Sherwood EXPECT_FALSE(TmpCost.isValid());
609b76160eSDavid Sherwood
619b76160eSDavid Sherwood // Test increments, decrements
629b76160eSDavid Sherwood EXPECT_EQ(++VThree, 4);
639b76160eSDavid Sherwood EXPECT_EQ(VThree++, 4);
649b76160eSDavid Sherwood EXPECT_EQ(VThree, 5);
659b76160eSDavid Sherwood EXPECT_EQ(--VThree, 4);
669b76160eSDavid Sherwood EXPECT_EQ(VThree--, 4);
679b76160eSDavid Sherwood EXPECT_EQ(VThree, 3);
689b76160eSDavid Sherwood
699b76160eSDavid Sherwood TmpCost = VThree * IThreeA;
709b76160eSDavid Sherwood EXPECT_FALSE(TmpCost.isValid());
719b76160eSDavid Sherwood
729b76160eSDavid Sherwood // Test value extraction
739b76160eSDavid Sherwood EXPECT_EQ(*(VThree.getValue()), 3);
74*b6a01caaSKazu Hirata EXPECT_EQ(IThreeA.getValue(), std::nullopt);
759b76160eSDavid Sherwood
76b7ccaca5SDavid Sherwood EXPECT_EQ(std::min(VThree, VNegTwo), -2);
77b7ccaca5SDavid Sherwood EXPECT_EQ(std::max(VThree, VSix), 6);
7841b60576SSander de Smalen
7941b60576SSander de Smalen // Test saturation
8041b60576SSander de Smalen auto Max = InstructionCost::getMax();
8141b60576SSander de Smalen auto Min = InstructionCost::getMin();
8241b60576SSander de Smalen auto MinusOne = InstructionCost(-1);
8341b60576SSander de Smalen auto MinusTwo = InstructionCost(-2);
8441b60576SSander de Smalen auto One = InstructionCost(1);
8541b60576SSander de Smalen auto Two = InstructionCost(2);
8641b60576SSander de Smalen EXPECT_EQ(Max + One, Max);
8741b60576SSander de Smalen EXPECT_EQ(Min + MinusOne, Min);
8841b60576SSander de Smalen EXPECT_EQ(Min - One, Min);
8941b60576SSander de Smalen EXPECT_EQ(Max - MinusOne, Max);
9041b60576SSander de Smalen EXPECT_EQ(Max * Two, Max);
9141b60576SSander de Smalen EXPECT_EQ(Min * Two, Min);
9241b60576SSander de Smalen EXPECT_EQ(Max * MinusTwo, Min);
9341b60576SSander de Smalen EXPECT_EQ(Min * MinusTwo, Max);
949b76160eSDavid Sherwood }
95