Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 "src/compiler/gap-resolver.h" |
| 6 | |
| 7 | #include "src/base/utils/random-number-generator.h" |
| 8 | #include "test/cctest/cctest.h" |
| 9 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | namespace compiler { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13 | |
| 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. |
| 16 | class InterpreterState { |
| 17 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18 | void ExecuteInParallel(const ParallelMove* moves) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19 | InterpreterState copy(*this); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 20 | for (const auto m : *moves) { |
| 21 | if (!m->IsRedundant()) write(m->destination(), copy.read(m->source())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 22 | } |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | // Internally, the state is a normalized permutation of (kind,index) pairs. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | typedef Key Value; |
| 61 | typedef std::map<Key, Value> OperandMap; |
| 62 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 63 | Value read(const InstructionOperand& op) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | OperandMap::const_iterator it = values_.find(KeyFor(op)); |
| 65 | return (it == values_.end()) ? ValueFor(op) : it->second; |
| 66 | } |
| 67 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 68 | void write(const InstructionOperand& op, Value v) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | if (v == ValueFor(op)) { |
| 70 | values_.erase(KeyFor(op)); |
| 71 | } else { |
| 72 | values_[KeyFor(op)] = v; |
| 73 | } |
| 74 | } |
| 75 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 84 | } else if (op.IsFPRegister()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 111 | friend std::ostream& operator<<(std::ostream& os, |
| 112 | const InterpreterState& is) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 113 | for (OperandMap::const_iterator it = is.values_.begin(); |
| 114 | it != is.values_.end(); ++it) { |
| 115 | if (it != is.values_.begin()) os << " "; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 116 | InstructionOperand source = FromKey(it->first); |
| 117 | InstructionOperand destination = FromKey(it->second); |
| 118 | MoveOperands mo(source, destination); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 119 | PrintableMoveOperands pmo = {RegisterConfiguration::Turbofan(), &mo}; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 120 | os << pmo; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | } |
| 122 | return os; |
| 123 | } |
| 124 | |
| 125 | OperandMap values_; |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | // An abstract interpreter for moves, swaps and parallel moves. |
| 130 | class MoveInterpreter : public GapResolver::Assembler { |
| 131 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 132 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 138 | state_.ExecuteInParallel(moves); |
| 139 | } |
| 140 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 146 | state_.ExecuteInParallel(moves); |
| 147 | } |
| 148 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 149 | void AssembleParallelMove(const ParallelMove* moves) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | state_.ExecuteInParallel(moves); |
| 151 | } |
| 152 | |
| 153 | InterpreterState state() const { return state_; } |
| 154 | |
| 155 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 156 | Zone* const zone_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 157 | InterpreterState state_; |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 167 | std::set<InstructionOperand, CompareOperandModuloType> seen; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 168 | MachineRepresentation rep = RandomRepresentation(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 169 | for (int i = 0; i < size; ++i) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 170 | MoveOperands mo(CreateRandomOperand(true, rep), |
| 171 | CreateRandomOperand(false, rep)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 172 | if (!mo.IsRedundant() && seen.find(mo.destination()) == seen.end()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 173 | parallel_move->AddMove(mo.source(), mo.destination()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 174 | seen.insert(mo.destination()); |
| 175 | } |
| 176 | } |
| 177 | return parallel_move; |
| 178 | } |
| 179 | |
| 180 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | MachineRepresentation RandomRepresentation() { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 182 | int index = rng_->NextInt(5); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 183 | switch (index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 184 | case 0: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 185 | return MachineRepresentation::kWord32; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 186 | case 1: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 187 | return MachineRepresentation::kWord64; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 188 | case 2: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 189 | return MachineRepresentation::kFloat32; |
| 190 | case 3: |
| 191 | return MachineRepresentation::kFloat64; |
| 192 | case 4: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 193 | return MachineRepresentation::kTagged; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 194 | } |
| 195 | UNREACHABLE(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 196 | return MachineRepresentation::kNone; |
| 197 | } |
| 198 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 199 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 208 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 209 | default: |
| 210 | return conf->RegisterConfiguration::GetAllocatableGeneralCode(index); |
| 211 | break; |
| 212 | } |
| 213 | UNREACHABLE(); |
| 214 | return static_cast<int>(Register::kCode_no_reg); |
| 215 | }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 216 | int index = rng_->NextInt(7); |
| 217 | // destination can't be Constant. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 218 | switch (rng_->NextInt(is_source ? 5 : 4)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 219 | case 0: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 220 | return AllocatedOperand(LocationOperand::STACK_SLOT, rep, index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 221 | case 1: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 222 | return AllocatedOperand(LocationOperand::REGISTER, rep, index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 223 | case 2: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 224 | return ExplicitOperand(LocationOperand::REGISTER, rep, |
| 225 | GetRegisterCode(rep, 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 226 | case 3: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 227 | return ExplicitOperand(LocationOperand::STACK_SLOT, rep, |
| 228 | GetRegisterCode(rep, index)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 229 | case 4: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 230 | return ConstantOperand(index); |
| 231 | } |
| 232 | UNREACHABLE(); |
| 233 | return InstructionOperand(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | private: |
| 237 | v8::base::RandomNumberGenerator* rng_; |
| 238 | }; |
| 239 | |
| 240 | |
| 241 | TEST(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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 248 | MoveInterpreter mi1(pmc.main_zone()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 249 | mi1.AssembleParallelMove(pm); |
| 250 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 251 | MoveInterpreter mi2(pmc.main_zone()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 252 | GapResolver resolver(&mi2); |
| 253 | resolver.Resolve(pm); |
| 254 | |
| 255 | CHECK(mi1.state() == mi2.state()); |
| 256 | } |
| 257 | } |
| 258 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 259 | |
| 260 | } // namespace compiler |
| 261 | } // namespace internal |
| 262 | } // namespace v8 |