blob: ea6f4ee8307966519968862faefa9d45c8aeb4a9 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 "src/compiler/gap-resolver.h"
6
7#include "src/base/utils/random-number-generator.h"
8#include "test/cctest/cctest.h"
9
10using namespace v8::internal;
11using namespace v8::internal::compiler;
12
13// The state of our move interpreter is the mapping of operands to values. Note
14// that the actual values don't really matter, all we care about is equality.
15class InterpreterState {
16 public:
17 typedef std::vector<MoveOperands> Moves;
18
19 void ExecuteInParallel(Moves moves) {
20 InterpreterState copy(*this);
21 for (Moves::iterator it = moves.begin(); it != moves.end(); ++it) {
22 if (!it->IsRedundant()) write(it->destination(), copy.read(it->source()));
23 }
24 }
25
26 bool operator==(const InterpreterState& other) const {
27 return values_ == other.values_;
28 }
29
30 bool operator!=(const InterpreterState& other) const {
31 return values_ != other.values_;
32 }
33
34 private:
35 // Internally, the state is a normalized permutation of (kind,index) pairs.
36 typedef std::pair<InstructionOperand::Kind, int> Key;
37 typedef Key Value;
38 typedef std::map<Key, Value> OperandMap;
39
40 Value read(const InstructionOperand* op) const {
41 OperandMap::const_iterator it = values_.find(KeyFor(op));
42 return (it == values_.end()) ? ValueFor(op) : it->second;
43 }
44
45 void write(const InstructionOperand* op, Value v) {
46 if (v == ValueFor(op)) {
47 values_.erase(KeyFor(op));
48 } else {
49 values_[KeyFor(op)] = v;
50 }
51 }
52
53 static Key KeyFor(const InstructionOperand* op) {
54 return Key(op->kind(), op->index());
55 }
56
57 static Value ValueFor(const InstructionOperand* op) {
58 return Value(op->kind(), op->index());
59 }
60
Emily Bernierd0a1eb72015-03-24 16:35:39 -040061 friend std::ostream& operator<<(std::ostream& os,
62 const InterpreterState& is) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 for (OperandMap::const_iterator it = is.values_.begin();
64 it != is.values_.end(); ++it) {
65 if (it != is.values_.begin()) os << " ";
66 InstructionOperand source(it->first.first, it->first.second);
67 InstructionOperand destination(it->second.first, it->second.second);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040068 MoveOperands mo(&source, &destination);
69 PrintableMoveOperands pmo = {RegisterConfiguration::ArchDefault(), &mo};
70 os << pmo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 }
72 return os;
73 }
74
75 OperandMap values_;
76};
77
78
79// An abstract interpreter for moves, swaps and parallel moves.
80class MoveInterpreter : public GapResolver::Assembler {
81 public:
82 virtual void AssembleMove(InstructionOperand* source,
83 InstructionOperand* destination) OVERRIDE {
84 InterpreterState::Moves moves;
85 moves.push_back(MoveOperands(source, destination));
86 state_.ExecuteInParallel(moves);
87 }
88
89 virtual void AssembleSwap(InstructionOperand* source,
90 InstructionOperand* destination) OVERRIDE {
91 InterpreterState::Moves moves;
92 moves.push_back(MoveOperands(source, destination));
93 moves.push_back(MoveOperands(destination, source));
94 state_.ExecuteInParallel(moves);
95 }
96
97 void AssembleParallelMove(const ParallelMove* pm) {
98 InterpreterState::Moves moves(pm->move_operands()->begin(),
99 pm->move_operands()->end());
100 state_.ExecuteInParallel(moves);
101 }
102
103 InterpreterState state() const { return state_; }
104
105 private:
106 InterpreterState state_;
107};
108
109
110class ParallelMoveCreator : public HandleAndZoneScope {
111 public:
112 ParallelMoveCreator() : rng_(CcTest::random_number_generator()) {}
113
114 ParallelMove* Create(int size) {
115 ParallelMove* parallel_move = new (main_zone()) ParallelMove(main_zone());
116 std::set<InstructionOperand*, InstructionOperandComparator> seen;
117 for (int i = 0; i < size; ++i) {
118 MoveOperands mo(CreateRandomOperand(), CreateRandomOperand());
119 if (!mo.IsRedundant() && seen.find(mo.destination()) == seen.end()) {
120 parallel_move->AddMove(mo.source(), mo.destination(), main_zone());
121 seen.insert(mo.destination());
122 }
123 }
124 return parallel_move;
125 }
126
127 private:
128 struct InstructionOperandComparator {
129 bool operator()(const InstructionOperand* x,
130 const InstructionOperand* y) const {
131 return (x->kind() < y->kind()) ||
132 (x->kind() == y->kind() && x->index() < y->index());
133 }
134 };
135
136 InstructionOperand* CreateRandomOperand() {
137 int index = rng_->NextInt(6);
138 switch (rng_->NextInt(5)) {
139 case 0:
140 return ConstantOperand::Create(index, main_zone());
141 case 1:
142 return StackSlotOperand::Create(index, main_zone());
143 case 2:
144 return DoubleStackSlotOperand::Create(index, main_zone());
145 case 3:
146 return RegisterOperand::Create(index, main_zone());
147 case 4:
148 return DoubleRegisterOperand::Create(index, main_zone());
149 }
150 UNREACHABLE();
151 return NULL;
152 }
153
154 private:
155 v8::base::RandomNumberGenerator* rng_;
156};
157
158
159TEST(FuzzResolver) {
160 ParallelMoveCreator pmc;
161 for (int size = 0; size < 20; ++size) {
162 for (int repeat = 0; repeat < 50; ++repeat) {
163 ParallelMove* pm = pmc.Create(size);
164
165 // Note: The gap resolver modifies the ParallelMove, so interpret first.
166 MoveInterpreter mi1;
167 mi1.AssembleParallelMove(pm);
168
169 MoveInterpreter mi2;
170 GapResolver resolver(&mi2);
171 resolver.Resolve(pm);
172
173 CHECK(mi1.state() == mi2.state());
174 }
175 }
176}