blob: 9781aeb5cdb41de4c82303f0f6fc5d48c878af91 [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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010namespace v8 {
11namespace internal {
12namespace compiler {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013
14// The state of our move interpreter is the mapping of operands to values. Note
15// that the actual values don't really matter, all we care about is equality.
16class InterpreterState {
17 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018 void ExecuteInParallel(const ParallelMove* moves) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019 InterpreterState copy(*this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020 for (const auto m : *moves) {
21 if (!m->IsRedundant()) write(m->destination(), copy.read(m->source()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 }
23 }
24
25 bool operator==(const InterpreterState& other) const {
26 return values_ == other.values_;
27 }
28
29 bool operator!=(const InterpreterState& other) const {
30 return values_ != other.values_;
31 }
32
33 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034 struct Key {
35 bool is_constant;
36 bool is_float;
37 LocationOperand::LocationKind kind;
38 int index;
39
40 bool operator<(const Key& other) const {
41 if (this->is_constant != other.is_constant) {
42 return this->is_constant;
43 }
44 if (this->is_float != other.is_float) {
45 return this->is_float;
46 }
47 if (this->kind != other.kind) {
48 return this->kind < other.kind;
49 }
50 return this->index < other.index;
51 }
52
53 bool operator==(const Key& other) const {
54 return this->is_constant == other.is_constant &&
55 this->kind == other.kind && this->index == other.index;
56 }
57 };
58
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 // Internally, the state is a normalized permutation of (kind,index) pairs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 typedef Key Value;
61 typedef std::map<Key, Value> OperandMap;
62
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063 Value read(const InstructionOperand& op) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 OperandMap::const_iterator it = values_.find(KeyFor(op));
65 return (it == values_.end()) ? ValueFor(op) : it->second;
66 }
67
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 void write(const InstructionOperand& op, Value v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 if (v == ValueFor(op)) {
70 values_.erase(KeyFor(op));
71 } else {
72 values_[KeyFor(op)] = v;
73 }
74 }
75
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076 static Key KeyFor(const InstructionOperand& op) {
77 bool is_constant = op.IsConstant();
78 bool is_float = false;
79 LocationOperand::LocationKind kind;
80 int index;
81 if (!is_constant) {
82 if (op.IsRegister()) {
83 index = LocationOperand::cast(op).GetRegister().code();
Ben Murdochc5610432016-08-08 18:44:38 +010084 } else if (op.IsFPRegister()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 index = LocationOperand::cast(op).GetDoubleRegister().code();
86 } else {
87 index = LocationOperand::cast(op).index();
88 }
89 is_float = IsFloatingPoint(LocationOperand::cast(op).representation());
90 kind = LocationOperand::cast(op).location_kind();
91 } else {
92 index = ConstantOperand::cast(op).virtual_register();
93 kind = LocationOperand::REGISTER;
94 }
95 Key key = {is_constant, is_float, kind, index};
96 return key;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 }
98
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 static Value ValueFor(const InstructionOperand& op) { return KeyFor(op); }
100
101 static InstructionOperand FromKey(Key key) {
102 if (key.is_constant) {
103 return ConstantOperand(key.index);
104 }
105 return AllocatedOperand(
106 key.kind,
107 v8::internal::compiler::InstructionSequence::DefaultRepresentation(),
108 key.index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 }
110
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400111 friend std::ostream& operator<<(std::ostream& os,
112 const InterpreterState& is) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 for (OperandMap::const_iterator it = is.values_.begin();
114 it != is.values_.end(); ++it) {
115 if (it != is.values_.begin()) os << " ";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 InstructionOperand source = FromKey(it->first);
117 InstructionOperand destination = FromKey(it->second);
118 MoveOperands mo(source, destination);
119 PrintableMoveOperands pmo = {
120 RegisterConfiguration::ArchDefault(RegisterConfiguration::TURBOFAN),
121 &mo};
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400122 os << pmo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 }
124 return os;
125 }
126
127 OperandMap values_;
128};
129
130
131// An abstract interpreter for moves, swaps and parallel moves.
132class MoveInterpreter : public GapResolver::Assembler {
133 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 explicit MoveInterpreter(Zone* zone) : zone_(zone) {}
135
136 void AssembleMove(InstructionOperand* source,
137 InstructionOperand* destination) override {
138 ParallelMove* moves = new (zone_) ParallelMove(zone_);
139 moves->AddMove(*source, *destination);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 state_.ExecuteInParallel(moves);
141 }
142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143 void AssembleSwap(InstructionOperand* source,
144 InstructionOperand* destination) override {
145 ParallelMove* moves = new (zone_) ParallelMove(zone_);
146 moves->AddMove(*source, *destination);
147 moves->AddMove(*destination, *source);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 state_.ExecuteInParallel(moves);
149 }
150
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000151 void AssembleParallelMove(const ParallelMove* moves) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 state_.ExecuteInParallel(moves);
153 }
154
155 InterpreterState state() const { return state_; }
156
157 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158 Zone* const zone_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 InterpreterState state_;
160};
161
162
163class ParallelMoveCreator : public HandleAndZoneScope {
164 public:
165 ParallelMoveCreator() : rng_(CcTest::random_number_generator()) {}
166
167 ParallelMove* Create(int size) {
168 ParallelMove* parallel_move = new (main_zone()) ParallelMove(main_zone());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169 std::set<InstructionOperand, CompareOperandModuloType> seen;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 for (int i = 0; i < size; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 MoveOperands mo(CreateRandomOperand(true), CreateRandomOperand(false));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 if (!mo.IsRedundant() && seen.find(mo.destination()) == seen.end()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 parallel_move->AddMove(mo.source(), mo.destination());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 seen.insert(mo.destination());
175 }
176 }
177 return parallel_move;
178 }
179
180 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181 MachineRepresentation RandomRepresentation() {
182 int index = rng_->NextInt(3);
183 switch (index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 case 0:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000185 return MachineRepresentation::kWord32;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 case 1:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000187 return MachineRepresentation::kWord64;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 case 2:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 return MachineRepresentation::kTagged;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 }
191 UNREACHABLE();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 return MachineRepresentation::kNone;
193 }
194
195 MachineRepresentation RandomDoubleRepresentation() {
196 int index = rng_->NextInt(2);
197 if (index == 0) return MachineRepresentation::kFloat64;
198 return MachineRepresentation::kFloat32;
199 }
200
201 InstructionOperand CreateRandomOperand(bool is_source) {
202 int index = rng_->NextInt(7);
203 // destination can't be Constant.
204 switch (rng_->NextInt(is_source ? 7 : 6)) {
205 case 0:
206 return AllocatedOperand(LocationOperand::STACK_SLOT,
207 RandomRepresentation(), index);
208 case 1:
209 return AllocatedOperand(LocationOperand::STACK_SLOT,
210 RandomDoubleRepresentation(), index);
211 case 2:
212 return AllocatedOperand(LocationOperand::REGISTER,
213 RandomRepresentation(), index);
214 case 3:
215 return AllocatedOperand(LocationOperand::REGISTER,
216 RandomDoubleRepresentation(), index);
217 case 4:
218 return ExplicitOperand(
219 LocationOperand::REGISTER, RandomRepresentation(),
220 RegisterConfiguration::ArchDefault(RegisterConfiguration::TURBOFAN)
221 ->GetAllocatableGeneralCode(1));
222 case 5:
223 return ExplicitOperand(
224 LocationOperand::STACK_SLOT, RandomRepresentation(),
225 RegisterConfiguration::ArchDefault(RegisterConfiguration::TURBOFAN)
226 ->GetAllocatableGeneralCode(index));
227 case 6:
228 return ConstantOperand(index);
229 }
230 UNREACHABLE();
231 return InstructionOperand();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232 }
233
234 private:
235 v8::base::RandomNumberGenerator* rng_;
236};
237
238
239TEST(FuzzResolver) {
240 ParallelMoveCreator pmc;
241 for (int size = 0; size < 20; ++size) {
242 for (int repeat = 0; repeat < 50; ++repeat) {
243 ParallelMove* pm = pmc.Create(size);
244
245 // Note: The gap resolver modifies the ParallelMove, so interpret first.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000246 MoveInterpreter mi1(pmc.main_zone());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 mi1.AssembleParallelMove(pm);
248
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 MoveInterpreter mi2(pmc.main_zone());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 GapResolver resolver(&mi2);
251 resolver.Resolve(pm);
252
253 CHECK(mi1.state() == mi2.state());
254 }
255 }
256}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257
258} // namespace compiler
259} // namespace internal
260} // namespace v8