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"
11*41b60576SSander de Smalen #include <limits>
129b76160eSDavid Sherwood 
139b76160eSDavid Sherwood using namespace llvm;
149b76160eSDavid Sherwood 
159b76160eSDavid Sherwood namespace {
169b76160eSDavid Sherwood 
179b76160eSDavid Sherwood struct CostTest : public testing::Test {
CostTest__anonf9942ce30111::CostTest189b76160eSDavid Sherwood   CostTest() {}
199b76160eSDavid Sherwood };
209b76160eSDavid Sherwood 
219b76160eSDavid Sherwood } // namespace
229b76160eSDavid Sherwood 
TEST_F(CostTest,DefaultCtor)23b8b054aaSChristopher Tetreault TEST_F(CostTest, DefaultCtor) {
24b8b054aaSChristopher Tetreault   InstructionCost DefaultCost;
25b8b054aaSChristopher Tetreault 
26b8b054aaSChristopher Tetreault   ASSERT_TRUE(DefaultCost.isValid());
27b8b054aaSChristopher Tetreault   EXPECT_EQ(*(DefaultCost.getValue()), 0);
28b8b054aaSChristopher Tetreault }
29b8b054aaSChristopher Tetreault 
TEST_F(CostTest,Operators)309b76160eSDavid Sherwood TEST_F(CostTest, Operators) {
31b8b054aaSChristopher Tetreault 
329b76160eSDavid Sherwood   InstructionCost VThree = 3;
339b76160eSDavid Sherwood   InstructionCost VNegTwo = -2;
349b76160eSDavid Sherwood   InstructionCost VSix = 6;
359b76160eSDavid Sherwood   InstructionCost IThreeA = InstructionCost::getInvalid(3);
369b76160eSDavid Sherwood   InstructionCost IThreeB = InstructionCost::getInvalid(3);
37b8b054aaSChristopher Tetreault   InstructionCost ITwo = InstructionCost::getInvalid(2);
389b76160eSDavid Sherwood   InstructionCost TmpCost;
399b76160eSDavid Sherwood 
409b76160eSDavid Sherwood   EXPECT_NE(VThree, VNegTwo);
419b76160eSDavid Sherwood   EXPECT_GT(VThree, VNegTwo);
429b76160eSDavid Sherwood   EXPECT_NE(VThree, IThreeA);
439b76160eSDavid Sherwood   EXPECT_EQ(IThreeA, IThreeB);
449b76160eSDavid Sherwood   EXPECT_GE(IThreeA, VNegTwo);
459b76160eSDavid Sherwood   EXPECT_LT(VSix, IThreeA);
46b8b054aaSChristopher Tetreault   EXPECT_LT(VThree, ITwo);
47b8b054aaSChristopher Tetreault   EXPECT_GE(ITwo, VThree);
489b76160eSDavid Sherwood   EXPECT_EQ(VSix - IThreeA, IThreeB);
499b76160eSDavid Sherwood   EXPECT_EQ(VThree - VNegTwo, 5);
509b76160eSDavid Sherwood   EXPECT_EQ(VThree * VNegTwo, -6);
519b76160eSDavid Sherwood   EXPECT_EQ(VSix / VThree, 2);
52b8b054aaSChristopher Tetreault   EXPECT_NE(IThreeA, ITwo);
53b8b054aaSChristopher Tetreault   EXPECT_LT(ITwo, IThreeA);
54b8b054aaSChristopher Tetreault   EXPECT_GT(IThreeA, ITwo);
559b76160eSDavid Sherwood 
569b76160eSDavid Sherwood   EXPECT_FALSE(IThreeA.isValid());
579b76160eSDavid Sherwood   EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid);
589b76160eSDavid Sherwood 
599b76160eSDavid Sherwood   TmpCost = VThree + IThreeA;
609b76160eSDavid Sherwood   EXPECT_FALSE(TmpCost.isValid());
619b76160eSDavid Sherwood 
629b76160eSDavid Sherwood   // Test increments, decrements
639b76160eSDavid Sherwood   EXPECT_EQ(++VThree, 4);
649b76160eSDavid Sherwood   EXPECT_EQ(VThree++, 4);
659b76160eSDavid Sherwood   EXPECT_EQ(VThree, 5);
669b76160eSDavid Sherwood   EXPECT_EQ(--VThree, 4);
679b76160eSDavid Sherwood   EXPECT_EQ(VThree--, 4);
689b76160eSDavid Sherwood   EXPECT_EQ(VThree, 3);
699b76160eSDavid Sherwood 
709b76160eSDavid Sherwood   TmpCost = VThree * IThreeA;
719b76160eSDavid Sherwood   EXPECT_FALSE(TmpCost.isValid());
729b76160eSDavid Sherwood 
739b76160eSDavid Sherwood   // Test value extraction
749b76160eSDavid Sherwood   EXPECT_EQ(*(VThree.getValue()), 3);
759b76160eSDavid Sherwood   EXPECT_EQ(IThreeA.getValue(), None);
769b76160eSDavid Sherwood 
77b7ccaca5SDavid Sherwood   EXPECT_EQ(std::min(VThree, VNegTwo), -2);
78b7ccaca5SDavid Sherwood   EXPECT_EQ(std::max(VThree, VSix), 6);
79*41b60576SSander de Smalen 
80*41b60576SSander de Smalen   // Test saturation
81*41b60576SSander de Smalen   auto Max = InstructionCost::getMax();
82*41b60576SSander de Smalen   auto Min = InstructionCost::getMin();
83*41b60576SSander de Smalen   auto MinusOne = InstructionCost(-1);
84*41b60576SSander de Smalen   auto MinusTwo = InstructionCost(-2);
85*41b60576SSander de Smalen   auto One = InstructionCost(1);
86*41b60576SSander de Smalen   auto Two = InstructionCost(2);
87*41b60576SSander de Smalen   EXPECT_EQ(Max + One, Max);
88*41b60576SSander de Smalen   EXPECT_EQ(Min + MinusOne, Min);
89*41b60576SSander de Smalen   EXPECT_EQ(Min - One, Min);
90*41b60576SSander de Smalen   EXPECT_EQ(Max - MinusOne, Max);
91*41b60576SSander de Smalen   EXPECT_EQ(Max * Two, Max);
92*41b60576SSander de Smalen   EXPECT_EQ(Min * Two, Min);
93*41b60576SSander de Smalen   EXPECT_EQ(Max * MinusTwo, Min);
94*41b60576SSander de Smalen   EXPECT_EQ(Min * MinusTwo, Max);
959b76160eSDavid Sherwood }
96