<commit_before>// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/change-lowering.h"
#include "src/compiler/compiler-test-utils.h"
#include "src/compiler/graph-unittest.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/node-properties-inl.h"
#include "src/compiler/simplified-operator.h"
#include "src/compiler/typer.h"
#include "testing/gmock-support.h"

using testing::_;
using testing::AllOf;
using testing::Capture;
using testing::CaptureEq;

namespace v8 {
namespace internal {
namespace compiler {

// TODO(bmeurer): Find a new home for these functions.
inline std::ostream& operator<<(std::ostream& os, const MachineType& type) {
  OStringStream ost;
  ost << type;
  return os << ost.c_str();
}


class ChangeLoweringTest : public GraphTest {
 public:
  ChangeLoweringTest() : simplified_(zone()) {}
  virtual ~ChangeLoweringTest() {}

  virtual MachineType WordRepresentation() const = 0;

 protected:
  int HeapNumberValueOffset() const {
    STATIC_ASSERT(HeapNumber::kValueOffset % kApiPointerSize == 0);
    return (HeapNumber::kValueOffset / kApiPointerSize) * PointerSize() -
           kHeapObjectTag;
  }
  bool Is32() const { return WordRepresentation() == kRepWord32; }
  int PointerSize() const {
    switch (WordRepresentation()) {
      case kRepWord32:
        return 4;
      case kRepWord64:
        return 8;
      default:
        break;
    }
    UNREACHABLE();
    return 0;
  }
  int SmiMaxValue() const { return -(SmiMinValue() + 1); }
  int SmiMinValue() const {
    return static_cast<int>(0xffffffffu << (SmiValueSize() - 1));
  }
  int SmiShiftAmount() const { return kSmiTagSize + SmiShiftSize(); }
  int SmiShiftSize() const {
    return Is32() ? SmiTagging<4>::SmiShiftSize()
                  : SmiTagging<8>::SmiShiftSize();
  }
  int SmiValueSize() const {
    return Is32() ? SmiTagging<4>::SmiValueSize()
                  : SmiTagging<8>::SmiValueSize();
  }

  Node* Parameter(int32_t index = 0) {
    return graph()->NewNode(common()->Parameter(index), graph()->start());
  }

  Reduction Reduce(Node* node) {
    Typer typer(zone());
    JSGraph jsgraph(graph(), common(), &typer);
    CompilationInfo info(isolate(), zone());
    Linkage linkage(&info);
    MachineOperatorBuilder machine(zone(), WordRepresentation());
    ChangeLowering reducer(&jsgraph, &linkage, &machine);
    return reducer.Reduce(node);
  }

  SimplifiedOperatorBuilder* simplified() { return &simplified_; }

  Matcher<Node*> IsAllocateHeapNumber(const Matcher<Node*>& effect_matcher,
                                      const Matcher<Node*>& control_matcher) {
    return IsCall(
        _, IsHeapConstant(Unique<HeapObject>::CreateImmovable(
               CEntryStub(isolate(), 1).GetCode())),
        IsExternalConstant(ExternalReference(
            Runtime::FunctionForId(Runtime::kAllocateHeapNumber), isolate())),
        IsInt32Constant(0), IsNumberConstant(0.0), effect_matcher,
        control_matcher);
  }
  Matcher<Node*> IsWordEqual(const Matcher<Node*>& lhs_matcher,
                             const Matcher<Node*>& rhs_matcher) {
    return Is32() ? IsWord32Equal(lhs_matcher, rhs_matcher)
                  : IsWord64Equal(lhs_matcher, rhs_matcher);
  }

 private:
  SimplifiedOperatorBuilder simplified_;
};


// -----------------------------------------------------------------------------
// Common.


class ChangeLoweringCommonTest
    : public ChangeLoweringTest,
      public ::testing::WithParamInterface<MachineType> {
 public:
  virtual ~ChangeLoweringCommonTest() {}

  virtual MachineType WordRepresentation() const FINAL OVERRIDE {
    return GetParam();
  }
};


TARGET_TEST_P(ChangeLoweringCommonTest, ChangeBitToBool) {
  Node* val = Parameter(0);
  Node* node = graph()->NewNode(simplified()->ChangeBitToBool(), val);
  Reduction reduction = Reduce(node);
  ASSERT_TRUE(reduction.Changed());

  Node* phi = reduction.replacement();
  Capture<Node*> branch;
  EXPECT_THAT(phi,
              IsPhi(kMachAnyTagged, IsTrueConstant(), IsFalseConstant(),
                    IsMerge(IsIfTrue(AllOf(CaptureEq(&branch),
                                           IsBranch(val, graph()->start()))),<commit_msg>[turbofan] Missing fix from last commit.<commit_after>// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/change-lowering.h"
#include "src/compiler/compiler-test-utils.h"
#include "src/compiler/graph-unittest.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/node-properties-inl.h"
#include "src/compiler/simplified-operator.h"
#include "src/compiler/typer.h"
#include "testing/gmock-support.h"

using testing::_;
using testing::AllOf;
using testing::Capture;
using testing::CaptureEq;

namespace v8 {
namespace internal {
namespace compiler {

// TODO(bmeurer): Find a new home for these functions.
inline std::ostream& operator<<(std::ostream& os, const MachineType& type) {
  OStringStream ost;
  ost << type;
  return os << ost.c_str();
}


class ChangeLoweringTest : public GraphTest {
 public:
  ChangeLoweringTest() : simplified_(zone()) {}
  virtual ~ChangeLoweringTest() {}

  virtual MachineType WordRepresentation() const = 0;

 protected:
  int HeapNumberValueOffset() const {
    STATIC_ASSERT(HeapNumber::kValueOffset % kApiPointerSize == 0);
    return (HeapNumber::kValueOffset / kApiPointerSize) * PointerSize() -
           kHeapObjectTag;
  }
  bool Is32() const { return WordRepresentation() == kRepWord32; }
  int PointerSize() const {
    switch (WordRepresentation()) {
      case kRepWord32:
        return 4;
      case kRepWord64:
        return 8;
      default:
        break;
    }
    UNREACHABLE();
    return 0;
  }
  int SmiMaxValue() const { return -(SmiMinValue() + 1); }
  int SmiMinValue() const {
    return static_cast<int>(0xffffffffu << (SmiValueSize() - 1));
  }
  int SmiShiftAmount() const { return kSmiTagSize + SmiShiftSize(); }
  int SmiShiftSize() const {
    return Is32() ? SmiTagging<4>::SmiShiftSize()
                  : SmiTagging<8>::SmiShiftSize();
  }
  int SmiValueSize() const {
    return Is32() ? SmiTagging<4>::SmiValueSize()
                  : SmiTagging<8>::SmiValueSize();
  }

  Node* Parameter(int32_t index = 0) {
    return graph()->NewNode(common()->Parameter(index), graph()->start());
  }

  Reduction Reduce(Node* node) {
    Typer typer(zone());
    JSGraph jsgraph(graph(), common(), &typer);
    CompilationInfo info(isolate(), zone());
    Linkage linkage(&info);
    MachineOperatorBuilder machine(zone(), WordRepresentation());
    ChangeLowering reducer(&jsgraph, &linkage, &machine);
    return reducer.Reduce(node);
  }

  SimplifiedOperatorBuilder* simplified() { return &simplified_; }

  Matcher<Node*> IsAllocateHeapNumber(const Matcher<Node*>& effect_matcher,
                                      const Matcher<Node*>& control_matcher) {
    return IsCall(
        _, IsHeapConstant(Unique<HeapObject>::CreateImmovable(
               CEntryStub(isolate(), 1).GetCode())),
        IsExternalConstant(ExternalReference(
            Runtime::FunctionForId(Runtime::kAllocateHeapNumber), isolate())),
        IsInt32Constant(0), IsNumberConstant(0.0), effect_matcher,
        control_matcher);
  }
  Matcher<Node*> IsWordEqual(const Matcher<Node*>& lhs_matcher,
                             const Matcher<Node*>& rhs_matcher) {
    return Is32() ? IsWord32Equal(lhs_matcher, rhs_matcher)
                  : IsWord64Equal(lhs_matcher, rhs_matcher);
  }

 private:
  SimplifiedOperatorBuilder simplified_;
};


// -----------------------------------------------------------------------------
// Common.


class ChangeLoweringCommonTest
    : public ChangeLoweringTest,
      public ::testing::WithParamInterface<MachineType> {
 public:
  virtual ~ChangeLoweringCommonTest() {}

  virtual MachineType WordRepresentation() const FINAL OVERRIDE {
    return GetParam();
  }
};


TARGET_TEST_P(ChangeLoweringCommonTest, ChangeBitToBool) {
  Node* val = Parameter(0);
  Node* node = graph()->NewNode(simplified()->ChangeBitToBool(), val);
  Reduction reduction = Reduce(node);
  ASSERT_TRUE(reduction.Changed());

  Node* phi = reduction.replacement();
  Capture<Node*> branch;
  EXPECT_THAT(phi,
              IsPhi(static_cast<MachineType>(kTypeBool | kRepTagged),
                    IsTrueConstant(), IsFalseConstant(),
                    IsMerge(IsIfTrue(AllOf(CaptureEq(&branch),
                                           IsBranch(val, graph()->start()))),
{'fraction_non_alphanumeric': 0.14310325025841278, 'fraction_numerical': 0.01056621109452165, 'mean_word_length': 3.300246913580247, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5513376', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2578, 'n_words': 557}