blob: 8d0ca8b8dd666263a016a23fa3d03aec225cadcf [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);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100119 PrintableMoveOperands pmo = {RegisterConfiguration::Turbofan(), &mo};
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400120 os << pmo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 }
122 return os;
123 }
124
125 OperandMap values_;
126};
127
128
129// An abstract interpreter for moves, swaps and parallel moves.
130class MoveInterpreter : public GapResolver::Assembler {
131 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 explicit MoveInterpreter(Zone* zone) : zone_(zone) {}
133
134 void AssembleMove(InstructionOperand* source,
135 InstructionOperand* destination) override {
136 ParallelMove* moves = new (zone_) ParallelMove(zone_);
137 moves->AddMove(*source, *destination);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 state_.ExecuteInParallel(moves);
139 }
140
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 void AssembleSwap(InstructionOperand* source,
142 InstructionOperand* destination) override {
143 ParallelMove* moves = new (zone_) ParallelMove(zone_);
144 moves->AddMove(*source, *destination);
145 moves->AddMove(*destination, *source);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 state_.ExecuteInParallel(moves);
147 }
148
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 void AssembleParallelMove(const ParallelMove* moves) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 state_.ExecuteInParallel(moves);
151 }
152
153 InterpreterState state() const { return state_; }
154
155 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000156 Zone* const zone_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 InterpreterState state_;
158};
159
160
161class ParallelMoveCreator : public HandleAndZoneScope {
162 public:
163 ParallelMoveCreator() : rng_(CcTest::random_number_generator()) {}
164
165 ParallelMove* Create(int size) {
166 ParallelMove* parallel_move = new (main_zone()) ParallelMove(main_zone());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167 std::set<InstructionOperand, CompareOperandModuloType> seen;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100168 MachineRepresentation rep = RandomRepresentation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 for (int i = 0; i < size; ++i) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100170 MoveOperands mo(CreateRandomOperand(true, rep),
171 CreateRandomOperand(false, rep));
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() {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100182 int index = rng_->NextInt(5);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183 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 Murdoch61f157c2016-09-16 13:49:30 +0100189 return MachineRepresentation::kFloat32;
190 case 3:
191 return MachineRepresentation::kFloat64;
192 case 4:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 return MachineRepresentation::kTagged;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 }
195 UNREACHABLE();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000196 return MachineRepresentation::kNone;
197 }
198
Ben Murdoch61f157c2016-09-16 13:49:30 +0100199 InstructionOperand CreateRandomOperand(bool is_source,
200 MachineRepresentation rep) {
201 auto conf = RegisterConfiguration::Turbofan();
202 auto GetRegisterCode = [&conf](MachineRepresentation rep, int index) {
203 switch (rep) {
204 case MachineRepresentation::kFloat32:
205 case MachineRepresentation::kFloat64:
206 return conf->RegisterConfiguration::GetAllocatableDoubleCode(index);
207 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208
Ben Murdoch61f157c2016-09-16 13:49:30 +0100209 default:
210 return conf->RegisterConfiguration::GetAllocatableGeneralCode(index);
211 break;
212 }
213 UNREACHABLE();
214 return static_cast<int>(Register::kCode_no_reg);
215 };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216 int index = rng_->NextInt(7);
217 // destination can't be Constant.
Ben Murdoch61f157c2016-09-16 13:49:30 +0100218 switch (rng_->NextInt(is_source ? 5 : 4)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000219 case 0:
Ben Murdoch61f157c2016-09-16 13:49:30 +0100220 return AllocatedOperand(LocationOperand::STACK_SLOT, rep, index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000221 case 1:
Ben Murdoch61f157c2016-09-16 13:49:30 +0100222 return AllocatedOperand(LocationOperand::REGISTER, rep, index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000223 case 2:
Ben Murdoch61f157c2016-09-16 13:49:30 +0100224 return ExplicitOperand(LocationOperand::REGISTER, rep,
225 GetRegisterCode(rep, 1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226 case 3:
Ben Murdoch61f157c2016-09-16 13:49:30 +0100227 return ExplicitOperand(LocationOperand::STACK_SLOT, rep,
228 GetRegisterCode(rep, index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000229 case 4:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000230 return ConstantOperand(index);
231 }
232 UNREACHABLE();
233 return InstructionOperand();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234 }
235
236 private:
237 v8::base::RandomNumberGenerator* rng_;
238};
239
240
241TEST(FuzzResolver) {
242 ParallelMoveCreator pmc;
243 for (int size = 0; size < 20; ++size) {
244 for (int repeat = 0; repeat < 50; ++repeat) {
245 ParallelMove* pm = pmc.Create(size);
246
247 // Note: The gap resolver modifies the ParallelMove, so interpret first.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000248 MoveInterpreter mi1(pmc.main_zone());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 mi1.AssembleParallelMove(pm);
250
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000251 MoveInterpreter mi2(pmc.main_zone());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252 GapResolver resolver(&mi2);
253 resolver.Resolve(pm);
254
255 CHECK(mi1.state() == mi2.state());
256 }
257 }
258}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259
260} // namespace compiler
261} // namespace internal
262} // namespace v8