LCOV - code coverage report
Current view: top level - test/unittests/compiler - common-operator-unittest.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 220 225 97.8 %
Date: 2019-01-20 Functions: 42 62 67.7 %

          Line data    Source code
       1             : // Copyright 2014 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include <limits>
       6             : 
       7             : #include "src/compiler/common-operator.h"
       8             : #include "src/compiler/opcodes.h"
       9             : #include "src/compiler/operator.h"
      10             : #include "src/compiler/operator-properties.h"
      11             : #include "test/unittests/test-utils.h"
      12             : 
      13             : namespace v8 {
      14             : namespace internal {
      15             : namespace compiler {
      16             : namespace common_operator_unittest {
      17             : 
      18             : // -----------------------------------------------------------------------------
      19             : // Shared operators.
      20             : 
      21             : 
      22             : namespace {
      23             : 
      24             : struct SharedOperator {
      25             :   const Operator* (CommonOperatorBuilder::*constructor)();
      26             :   IrOpcode::Value opcode;
      27             :   Operator::Properties properties;
      28             :   int value_input_count;
      29             :   int effect_input_count;
      30             :   int control_input_count;
      31             :   int value_output_count;
      32             :   int effect_output_count;
      33             :   int control_output_count;
      34             : };
      35             : 
      36             : 
      37       84700 : std::ostream& operator<<(std::ostream& os, const SharedOperator& fop) {
      38       84700 :   return os << IrOpcode::Mnemonic(fop.opcode);
      39             : }
      40             : 
      41             : const SharedOperator kSharedOperators[] = {
      42             : #define SHARED(Name, properties, value_input_count, effect_input_count,      \
      43             :                control_input_count, value_output_count, effect_output_count, \
      44             :                control_output_count)                                         \
      45             :   {                                                                          \
      46             :     &CommonOperatorBuilder::Name, IrOpcode::k##Name, properties,             \
      47             :         value_input_count, effect_input_count, control_input_count,          \
      48             :         value_output_count, effect_output_count, control_output_count        \
      49             :   }
      50             :     SHARED(Dead, Operator::kFoldable, 0, 0, 0, 1, 1, 1),
      51             :     SHARED(IfTrue, Operator::kKontrol, 0, 0, 1, 0, 0, 1),
      52             :     SHARED(IfFalse, Operator::kKontrol, 0, 0, 1, 0, 0, 1),
      53             :     SHARED(IfSuccess, Operator::kKontrol, 0, 0, 1, 0, 0, 1),
      54             :     SHARED(IfException, Operator::kKontrol, 0, 1, 1, 1, 1, 1),
      55             :     SHARED(Throw, Operator::kKontrol, 0, 1, 1, 0, 0, 1),
      56             :     SHARED(Terminate, Operator::kKontrol, 0, 1, 1, 0, 0, 1)
      57             : #undef SHARED
      58             : };
      59             : 
      60             : 
      61         112 : class CommonSharedOperatorTest
      62             :     : public TestWithZone,
      63             :       public ::testing::WithParamInterface<SharedOperator> {};
      64             : 
      65             : }  // namespace
      66             : 
      67             : 
      68       18178 : TEST_P(CommonSharedOperatorTest, InstancesAreGloballyShared) {
      69           7 :   const SharedOperator& sop = GetParam();
      70           7 :   CommonOperatorBuilder common1(zone());
      71           7 :   CommonOperatorBuilder common2(zone());
      72          14 :   EXPECT_EQ((common1.*sop.constructor)(), (common2.*sop.constructor)());
      73           7 : }
      74             : 
      75             : 
      76       18178 : TEST_P(CommonSharedOperatorTest, NumberOfInputsAndOutputs) {
      77           7 :   CommonOperatorBuilder common(zone());
      78           7 :   const SharedOperator& sop = GetParam();
      79          42 :   const Operator* op = (common.*sop.constructor)();
      80             : 
      81          14 :   EXPECT_EQ(sop.value_input_count, op->ValueInputCount());
      82          14 :   EXPECT_EQ(sop.effect_input_count, op->EffectInputCount());
      83          14 :   EXPECT_EQ(sop.control_input_count, op->ControlInputCount());
      84          14 :   EXPECT_EQ(
      85             :       sop.value_input_count + sop.effect_input_count + sop.control_input_count,
      86           0 :       OperatorProperties::GetTotalInputCount(op));
      87             : 
      88          14 :   EXPECT_EQ(sop.value_output_count, op->ValueOutputCount());
      89          14 :   EXPECT_EQ(sop.effect_output_count, op->EffectOutputCount());
      90          14 :   EXPECT_EQ(sop.control_output_count, op->ControlOutputCount());
      91           7 : }
      92             : 
      93             : 
      94       18178 : TEST_P(CommonSharedOperatorTest, OpcodeIsCorrect) {
      95           7 :   CommonOperatorBuilder common(zone());
      96           7 :   const SharedOperator& sop = GetParam();
      97           7 :   const Operator* op = (common.*sop.constructor)();
      98          14 :   EXPECT_EQ(sop.opcode, op->opcode());
      99           7 : }
     100             : 
     101             : 
     102       18178 : TEST_P(CommonSharedOperatorTest, Properties) {
     103           7 :   CommonOperatorBuilder common(zone());
     104           7 :   const SharedOperator& sop = GetParam();
     105           7 :   const Operator* op = (common.*sop.constructor)();
     106          14 :   EXPECT_EQ(sop.properties, op->properties());
     107           7 : }
     108             : 
     109             : 
     110      124025 : INSTANTIATE_TEST_CASE_P(CommonOperatorTest, CommonSharedOperatorTest,
     111             :                         ::testing::ValuesIn(kSharedOperators));
     112             : 
     113             : 
     114             : // -----------------------------------------------------------------------------
     115             : // Other operators.
     116             : 
     117             : 
     118             : namespace {
     119             : 
     120             : class CommonOperatorTest : public TestWithZone {
     121             :  public:
     122          12 :   CommonOperatorTest() : common_(zone()) {}
     123          12 :   ~CommonOperatorTest() override = default;
     124             : 
     125             :   CommonOperatorBuilder* common() { return &common_; }
     126             : 
     127             :  private:
     128             :   CommonOperatorBuilder common_;
     129             : };
     130             : 
     131             : 
     132             : const int kArguments[] = {1, 5, 6, 42, 100, 10000, 65000};
     133             : 
     134             : 
     135             : const size_t kCases[] = {3, 4, 100, 255, 1024, 65000};
     136             : 
     137             : 
     138             : const float kFloatValues[] = {-std::numeric_limits<float>::infinity(),
     139             :                               std::numeric_limits<float>::min(),
     140             :                               -1.0f,
     141             :                               -0.0f,
     142             :                               0.0f,
     143             :                               1.0f,
     144             :                               std::numeric_limits<float>::max(),
     145             :                               std::numeric_limits<float>::infinity(),
     146             :                               std::numeric_limits<float>::quiet_NaN(),
     147             :                               std::numeric_limits<float>::signaling_NaN()};
     148             : 
     149             : 
     150             : const size_t kInputCounts[] = {3, 4, 100, 255, 1024, 65000};
     151             : 
     152             : 
     153             : const int32_t kInt32Values[] = {
     154             :     std::numeric_limits<int32_t>::min(), -1914954528, -1698749618, -1578693386,
     155             :     -1577976073, -1573998034, -1529085059, -1499540537, -1299205097,
     156             :     -1090814845, -938186388, -806828902, -750927650, -520676892, -513661538,
     157             :     -453036354, -433622833, -282638793, -28375, -27788, -22770, -18806, -14173,
     158             :     -11956, -11200, -10212, -8160, -3751, -2758, -1522, -121, -120, -118, -117,
     159             :     -106, -84, -80, -74, -59, -52, -48, -39, -35, -17, -11, -10, -9, -7, -5, 0,
     160             :     9, 12, 17, 23, 29, 31, 33, 35, 40, 47, 55, 56, 62, 64, 67, 68, 69, 74, 79,
     161             :     84, 89, 90, 97, 104, 118, 124, 126, 127, 7278, 17787, 24136, 24202, 25570,
     162             :     26680, 30242, 32399, 420886487, 642166225, 821912648, 822577803, 851385718,
     163             :     1212241078, 1411419304, 1589626102, 1596437184, 1876245816, 1954730266,
     164             :     2008792749, 2045320228, std::numeric_limits<int32_t>::max()};
     165             : 
     166             : 
     167             : const BranchHint kBranchHints[] = {BranchHint::kNone, BranchHint::kTrue,
     168             :                                    BranchHint::kFalse};
     169             : 
     170             : }  // namespace
     171             : 
     172             : 
     173       15129 : TEST_F(CommonOperatorTest, End) {
     174          43 :   TRACED_FOREACH(size_t, input_count, kInputCounts) {
     175          42 :     const Operator* const op = common()->End(input_count);
     176          12 :     EXPECT_EQ(IrOpcode::kEnd, op->opcode());
     177          12 :     EXPECT_EQ(Operator::kKontrol, op->properties());
     178          12 :     EXPECT_EQ(0, op->ValueInputCount());
     179          12 :     EXPECT_EQ(0, op->EffectInputCount());
     180          12 :     EXPECT_EQ(input_count, static_cast<uint32_t>(op->ControlInputCount()));
     181          12 :     EXPECT_EQ(input_count, static_cast<uint32_t>(
     182           0 :                                OperatorProperties::GetTotalInputCount(op)));
     183          12 :     EXPECT_EQ(0, op->ValueOutputCount());
     184          12 :     EXPECT_EQ(0, op->EffectOutputCount());
     185          12 :     EXPECT_EQ(0, op->ControlOutputCount());
     186           6 :   }
     187           1 : }
     188             : 
     189             : 
     190       15129 : TEST_F(CommonOperatorTest, Return) {
     191          43 :   TRACED_FOREACH(int, input_count, kArguments) {
     192          49 :     const Operator* const op = common()->Return(input_count);
     193          14 :     EXPECT_EQ(IrOpcode::kReturn, op->opcode());
     194          14 :     EXPECT_EQ(Operator::kNoThrow, op->properties());
     195          14 :     EXPECT_EQ(input_count + 1, op->ValueInputCount());
     196          14 :     EXPECT_EQ(1, op->EffectInputCount());
     197          14 :     EXPECT_EQ(1, op->ControlInputCount());
     198          14 :     EXPECT_EQ(3 + input_count, OperatorProperties::GetTotalInputCount(op));
     199          14 :     EXPECT_EQ(0, op->ValueOutputCount());
     200          14 :     EXPECT_EQ(0, op->EffectOutputCount());
     201          14 :     EXPECT_EQ(1, op->ControlOutputCount());
     202           7 :   }
     203           1 : }
     204             : 
     205             : 
     206       15129 : TEST_F(CommonOperatorTest, Branch) {
     207          22 :   TRACED_FOREACH(BranchHint, hint, kBranchHints) {
     208          21 :     const Operator* const op = common()->Branch(hint);
     209           6 :     EXPECT_EQ(IrOpcode::kBranch, op->opcode());
     210           6 :     EXPECT_EQ(Operator::kKontrol, op->properties());
     211           6 :     EXPECT_EQ(hint, BranchHintOf(op));
     212           6 :     EXPECT_EQ(1, op->ValueInputCount());
     213           6 :     EXPECT_EQ(0, op->EffectInputCount());
     214           6 :     EXPECT_EQ(1, op->ControlInputCount());
     215           6 :     EXPECT_EQ(2, OperatorProperties::GetTotalInputCount(op));
     216           6 :     EXPECT_EQ(0, op->ValueOutputCount());
     217           6 :     EXPECT_EQ(0, op->EffectOutputCount());
     218           6 :     EXPECT_EQ(2, op->ControlOutputCount());
     219           3 :   }
     220           1 : }
     221             : 
     222             : 
     223       15129 : TEST_F(CommonOperatorTest, Switch) {
     224          37 :   TRACED_FOREACH(size_t, cases, kCases) {
     225          42 :     const Operator* const op = common()->Switch(cases);
     226          12 :     EXPECT_EQ(IrOpcode::kSwitch, op->opcode());
     227          12 :     EXPECT_EQ(Operator::kKontrol, op->properties());
     228          12 :     EXPECT_EQ(1, op->ValueInputCount());
     229          12 :     EXPECT_EQ(0, op->EffectInputCount());
     230          12 :     EXPECT_EQ(1, op->ControlInputCount());
     231          12 :     EXPECT_EQ(2, OperatorProperties::GetTotalInputCount(op));
     232          12 :     EXPECT_EQ(0, op->ValueOutputCount());
     233          12 :     EXPECT_EQ(0, op->EffectOutputCount());
     234          12 :     EXPECT_EQ(static_cast<int>(cases), op->ControlOutputCount());
     235           6 :   }
     236           1 : }
     237             : 
     238             : 
     239       15129 : TEST_F(CommonOperatorTest, IfValue) {
     240         601 :   TRACED_FOREACH(int32_t, value, kInt32Values) {
     241       70000 :     TRACED_FOREACH(int32_t, order, kInt32Values) {
     242       70000 :       const Operator* const op = common()->IfValue(value, order);
     243       20000 :       EXPECT_EQ(IrOpcode::kIfValue, op->opcode());
     244       20000 :       EXPECT_EQ(Operator::kKontrol, op->properties());
     245       20000 :       EXPECT_EQ(IfValueParameters(value, order), IfValueParametersOf(op));
     246       20000 :       EXPECT_EQ(0, op->ValueInputCount());
     247       20000 :       EXPECT_EQ(0, op->EffectInputCount());
     248       20000 :       EXPECT_EQ(1, op->ControlInputCount());
     249       20000 :       EXPECT_EQ(1, OperatorProperties::GetTotalInputCount(op));
     250       20000 :       EXPECT_EQ(0, op->ValueOutputCount());
     251       20000 :       EXPECT_EQ(0, op->EffectOutputCount());
     252       20000 :       EXPECT_EQ(1, op->ControlOutputCount());
     253       10000 :     }
     254         100 :   }
     255           1 : }
     256             : 
     257             : 
     258       15129 : TEST_F(CommonOperatorTest, Select) {
     259             :   static const MachineRepresentation kMachineRepresentations[] = {
     260             :       MachineRepresentation::kBit,     MachineRepresentation::kWord8,
     261             :       MachineRepresentation::kWord16,  MachineRepresentation::kWord32,
     262             :       MachineRepresentation::kWord64,  MachineRepresentation::kFloat32,
     263             :       MachineRepresentation::kFloat64, MachineRepresentation::kTagged};
     264             : 
     265             : 
     266          57 :   TRACED_FOREACH(MachineRepresentation, rep, kMachineRepresentations) {
     267         192 :     TRACED_FOREACH(BranchHint, hint, kBranchHints) {
     268         168 :       const Operator* const op = common()->Select(rep, hint);
     269          48 :       EXPECT_EQ(IrOpcode::kSelect, op->opcode());
     270          48 :       EXPECT_EQ(Operator::kPure, op->properties());
     271          48 :       EXPECT_EQ(rep, SelectParametersOf(op).representation());
     272          48 :       EXPECT_EQ(hint, SelectParametersOf(op).hint());
     273          48 :       EXPECT_EQ(3, op->ValueInputCount());
     274          48 :       EXPECT_EQ(0, op->EffectInputCount());
     275          48 :       EXPECT_EQ(0, op->ControlInputCount());
     276          48 :       EXPECT_EQ(3, OperatorProperties::GetTotalInputCount(op));
     277          48 :       EXPECT_EQ(1, op->ValueOutputCount());
     278          48 :       EXPECT_EQ(0, op->EffectOutputCount());
     279          48 :       EXPECT_EQ(0, op->ControlOutputCount());
     280          24 :     }
     281           8 :   }
     282           1 : }
     283             : 
     284             : 
     285       15129 : TEST_F(CommonOperatorTest, Float32Constant) {
     286          71 :   TRACED_FOREACH(float, value, kFloatValues) {
     287          50 :     const Operator* op = common()->Float32Constant(value);
     288          10 :     EXPECT_PRED2(base::bit_equal_to<float>(), value, OpParameter<float>(op));
     289          20 :     EXPECT_EQ(0, op->ValueInputCount());
     290          20 :     EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
     291          20 :     EXPECT_EQ(0, op->ControlOutputCount());
     292          20 :     EXPECT_EQ(0, op->EffectOutputCount());
     293          20 :     EXPECT_EQ(1, op->ValueOutputCount());
     294          10 :   }
     295          61 :   TRACED_FOREACH(float, v1, kFloatValues) {
     296         700 :     TRACED_FOREACH(float, v2, kFloatValues) {
     297         100 :       const Operator* op1 = common()->Float32Constant(v1);
     298         100 :       const Operator* op2 = common()->Float32Constant(v2);
     299         300 :       EXPECT_EQ(bit_cast<uint32_t>(v1) == bit_cast<uint32_t>(v2),
     300           0 :                 op1->Equals(op2));
     301         100 :     }
     302          10 :   }
     303           1 : }
     304             : 
     305             : 
     306       15129 : TEST_F(CommonOperatorTest, Float64Constant) {
     307          71 :   TRACED_FOREACH(double, value, kFloatValues) {
     308          50 :     const Operator* op = common()->Float64Constant(value);
     309          10 :     EXPECT_PRED2(base::bit_equal_to<double>(), value, OpParameter<double>(op));
     310          20 :     EXPECT_EQ(0, op->ValueInputCount());
     311          20 :     EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
     312          20 :     EXPECT_EQ(0, op->ControlOutputCount());
     313          20 :     EXPECT_EQ(0, op->EffectOutputCount());
     314          20 :     EXPECT_EQ(1, op->ValueOutputCount());
     315          10 :   }
     316          61 :   TRACED_FOREACH(double, v1, kFloatValues) {
     317         700 :     TRACED_FOREACH(double, v2, kFloatValues) {
     318         100 :       const Operator* op1 = common()->Float64Constant(v1);
     319         100 :       const Operator* op2 = common()->Float64Constant(v2);
     320         300 :       EXPECT_EQ(bit_cast<uint64_t>(v1) == bit_cast<uint64_t>(v2),
     321           0 :                 op1->Equals(op2));
     322         100 :     }
     323          10 :   }
     324           1 : }
     325             : 
     326             : 
     327       15129 : TEST_F(CommonOperatorTest, NumberConstant) {
     328          71 :   TRACED_FOREACH(double, value, kFloatValues) {
     329          50 :     const Operator* op = common()->NumberConstant(value);
     330          10 :     EXPECT_PRED2(base::bit_equal_to<double>(), value, OpParameter<double>(op));
     331          20 :     EXPECT_EQ(0, op->ValueInputCount());
     332          20 :     EXPECT_EQ(0, OperatorProperties::GetTotalInputCount(op));
     333          20 :     EXPECT_EQ(0, op->ControlOutputCount());
     334          20 :     EXPECT_EQ(0, op->EffectOutputCount());
     335          20 :     EXPECT_EQ(1, op->ValueOutputCount());
     336          10 :   }
     337          61 :   TRACED_FOREACH(double, v1, kFloatValues) {
     338         700 :     TRACED_FOREACH(double, v2, kFloatValues) {
     339         100 :       const Operator* op1 = common()->NumberConstant(v1);
     340         100 :       const Operator* op2 = common()->NumberConstant(v2);
     341         300 :       EXPECT_EQ(bit_cast<uint64_t>(v1) == bit_cast<uint64_t>(v2),
     342           0 :                 op1->Equals(op2));
     343         100 :     }
     344          10 :   }
     345           1 : }
     346             : 
     347             : 
     348       15129 : TEST_F(CommonOperatorTest, BeginRegion) {
     349             :   {
     350           4 :     const Operator* op =
     351           1 :         common()->BeginRegion(RegionObservability::kObservable);
     352           2 :     EXPECT_EQ(1, op->EffectInputCount());
     353           2 :     EXPECT_EQ(1, OperatorProperties::GetTotalInputCount(op));
     354           2 :     EXPECT_EQ(0, op->ControlOutputCount());
     355           2 :     EXPECT_EQ(1, op->EffectOutputCount());
     356           2 :     EXPECT_EQ(0, op->ValueOutputCount());
     357             :   }
     358             :   {
     359           4 :     const Operator* op =
     360           1 :         common()->BeginRegion(RegionObservability::kNotObservable);
     361           2 :     EXPECT_EQ(1, op->EffectInputCount());
     362           2 :     EXPECT_EQ(1, OperatorProperties::GetTotalInputCount(op));
     363           2 :     EXPECT_EQ(0, op->ControlOutputCount());
     364           2 :     EXPECT_EQ(1, op->EffectOutputCount());
     365           2 :     EXPECT_EQ(0, op->ValueOutputCount());
     366             :   }
     367           1 : }
     368             : 
     369       15129 : TEST_F(CommonOperatorTest, FinishRegion) {
     370           5 :   const Operator* op = common()->FinishRegion();
     371           2 :   EXPECT_EQ(1, op->ValueInputCount());
     372           2 :   EXPECT_EQ(1, op->EffectInputCount());
     373           2 :   EXPECT_EQ(2, OperatorProperties::GetTotalInputCount(op));
     374           2 :   EXPECT_EQ(0, op->ControlOutputCount());
     375           2 :   EXPECT_EQ(1, op->EffectOutputCount());
     376           2 :   EXPECT_EQ(1, op->ValueOutputCount());
     377           1 : }
     378             : 
     379       15129 : TEST_F(CommonOperatorTest, Projection) {
     380          29 :   TRACED_FORRANGE(size_t, index, 0, 3) {
     381          24 :     const Operator* op = common()->Projection(index);
     382           8 :     EXPECT_EQ(index, ProjectionIndexOf(op));
     383           8 :     EXPECT_EQ(1, op->ValueInputCount());
     384           8 :     EXPECT_EQ(1, op->ControlInputCount());
     385           8 :     EXPECT_EQ(2, OperatorProperties::GetTotalInputCount(op));
     386           8 :     EXPECT_EQ(0, op->ControlOutputCount());
     387           8 :     EXPECT_EQ(0, op->EffectOutputCount());
     388           8 :     EXPECT_EQ(1, op->ValueOutputCount());
     389           4 :   }
     390           1 : }
     391             : 
     392             : }  // namespace common_operator_unittest
     393             : }  // namespace compiler
     394             : }  // namespace internal
     395        9075 : }  // namespace v8

Generated by: LCOV version 1.10