5 #include "src/compiler/backend/move-optimizer.h" 7 #include "src/register-configuration.h" 16 InstructionOperand source;
17 InstructionOperand destination;
20 struct MoveKeyCompare {
21 bool operator()(
const MoveKey& a,
const MoveKey& b)
const {
22 if (a.source.EqualsCanonicalized(b.source)) {
23 return a.destination.CompareCanonicalized(b.destination);
25 return a.source.CompareCanonicalized(b.source);
29 typedef ZoneMap<MoveKey, unsigned, MoveKeyCompare> MoveMap;
33 explicit OperandSet(ZoneVector<InstructionOperand>* buffer)
34 : set_(buffer), fp_reps_(0) {
38 void InsertOp(
const InstructionOperand& op) {
41 if (!kSimpleFPAliasing && op.IsFPRegister())
42 fp_reps_ |= RepresentationBit(LocationOperand::cast(op).representation());
45 bool Contains(
const InstructionOperand& op)
const {
46 for (
const InstructionOperand& elem : *set_) {
47 if (elem.EqualsCanonicalized(op))
return true;
52 bool ContainsOpOrAlias(
const InstructionOperand& op)
const {
53 if (Contains(op))
return true;
55 if (!kSimpleFPAliasing && op.IsFPRegister()) {
57 const LocationOperand& loc = LocationOperand::cast(op);
58 MachineRepresentation rep = loc.representation();
60 if (!HasMixedFPReps(fp_reps_ | RepresentationBit(rep)))
return false;
63 MachineRepresentation other_rep1, other_rep2;
65 case MachineRepresentation::kFloat32:
66 other_rep1 = MachineRepresentation::kFloat64;
67 other_rep2 = MachineRepresentation::kSimd128;
69 case MachineRepresentation::kFloat64:
70 other_rep1 = MachineRepresentation::kFloat32;
71 other_rep2 = MachineRepresentation::kSimd128;
73 case MachineRepresentation::kSimd128:
74 other_rep1 = MachineRepresentation::kFloat32;
75 other_rep2 = MachineRepresentation::kFloat64;
81 const RegisterConfiguration* config = RegisterConfiguration::Default();
84 config->GetAliases(rep, loc.register_code(), other_rep1, &base);
85 DCHECK(aliases > 0 || (aliases == 0 && base == -1));
87 if (Contains(AllocatedOperand(LocationOperand::REGISTER, other_rep1,
92 aliases = config->GetAliases(rep, loc.register_code(), other_rep2, &base);
93 DCHECK(aliases > 0 || (aliases == 0 && base == -1));
95 if (Contains(AllocatedOperand(LocationOperand::REGISTER, other_rep2,
105 static bool HasMixedFPReps(
int reps) {
106 return reps && !base::bits::IsPowerOfTwo(reps);
109 ZoneVector<InstructionOperand>* set_;
113 int FindFirstNonEmptySlot(
const Instruction* instr) {
114 int i = Instruction::FIRST_GAP_POSITION;
115 for (;
i <= Instruction::LAST_GAP_POSITION;
i++) {
116 ParallelMove* moves = instr->parallel_moves()[
i];
117 if (moves ==
nullptr)
continue;
118 for (MoveOperands* move : *moves) {
119 if (!move->IsRedundant())
return i;
129 MoveOptimizer::MoveOptimizer(Zone* local_zone, InstructionSequence* code)
130 : local_zone_(local_zone),
132 local_vector_(local_zone),
133 operand_buffer1(local_zone),
134 operand_buffer2(local_zone) {}
136 void MoveOptimizer::Run() {
137 for (Instruction* instruction : code()->instructions()) {
138 CompressGaps(instruction);
140 for (InstructionBlock* block : code()->instruction_blocks()) {
141 CompressBlock(block);
143 for (InstructionBlock* block : code()->instruction_blocks()) {
144 if (block->PredecessorCount() <= 1)
continue;
145 if (!block->IsDeferred()) {
146 bool has_only_deferred =
true;
147 for (RpoNumber& pred_id : block->predecessors()) {
148 if (!code()->InstructionBlockAt(pred_id)->IsDeferred()) {
149 has_only_deferred =
false;
157 if (has_only_deferred)
continue;
159 OptimizeMerge(block);
161 for (Instruction* gap : code()->instructions()) {
166 void MoveOptimizer::RemoveClobberedDestinations(Instruction* instruction) {
167 if (instruction->IsCall())
return;
168 ParallelMove* moves = instruction->parallel_moves()[0];
169 if (moves ==
nullptr)
return;
171 DCHECK(instruction->parallel_moves()[1] ==
nullptr ||
172 instruction->parallel_moves()[1]->empty());
174 OperandSet outputs(&operand_buffer1);
175 OperandSet inputs(&operand_buffer2);
179 for (
size_t i = 0;
i < instruction->OutputCount(); ++
i) {
180 outputs.InsertOp(*instruction->OutputAt(
i));
182 for (
size_t i = 0;
i < instruction->TempCount(); ++
i) {
183 outputs.InsertOp(*instruction->TempAt(
i));
187 for (
size_t i = 0;
i < instruction->InputCount(); ++
i) {
188 inputs.InsertOp(*instruction->InputAt(
i));
192 for (MoveOperands* move : *moves) {
193 if (outputs.ContainsOpOrAlias(move->destination()) &&
194 !inputs.ContainsOpOrAlias(move->destination())) {
201 if (instruction->IsRet() || instruction->IsTailCall()) {
202 for (MoveOperands* move : *moves) {
203 if (!inputs.ContainsOpOrAlias(move->destination())) {
210 void MoveOptimizer::MigrateMoves(Instruction* to, Instruction* from) {
211 if (from->IsCall())
return;
213 ParallelMove* from_moves = from->parallel_moves()[0];
214 if (from_moves ==
nullptr || from_moves->empty())
return;
216 OperandSet dst_cant_be(&operand_buffer1);
217 OperandSet src_cant_be(&operand_buffer2);
221 for (
size_t i = 0;
i < from->InputCount(); ++
i) {
222 dst_cant_be.InsertOp(*from->InputAt(
i));
230 for (
size_t i = 0;
i < from->OutputCount(); ++
i) {
231 src_cant_be.InsertOp(*from->OutputAt(
i));
233 for (
size_t i = 0;
i < from->TempCount(); ++
i) {
234 src_cant_be.InsertOp(*from->TempAt(
i));
236 for (MoveOperands* move : *from_moves) {
237 if (move->IsRedundant())
continue;
242 src_cant_be.InsertOp(move->destination());
245 ZoneSet<MoveKey, MoveKeyCompare> move_candidates(local_zone());
248 for (MoveOperands* move : *from_moves) {
249 if (move->IsRedundant())
continue;
250 if (!dst_cant_be.ContainsOpOrAlias(move->destination())) {
251 MoveKey key = {move->source(), move->destination()};
252 move_candidates.insert(key);
255 if (move_candidates.empty())
return;
258 bool changed =
false;
261 for (
auto iter = move_candidates.begin(); iter != move_candidates.end();) {
264 InstructionOperand src = current->source;
265 if (src_cant_be.ContainsOpOrAlias(src)) {
266 src_cant_be.InsertOp(current->destination);
267 move_candidates.erase(current);
273 ParallelMove to_move(local_zone());
274 for (MoveOperands* move : *from_moves) {
275 if (move->IsRedundant())
continue;
276 MoveKey key = {move->source(), move->destination()};
277 if (move_candidates.find(key) != move_candidates.end()) {
278 to_move.AddMove(move->source(), move->destination(), code_zone());
282 if (to_move.empty())
return;
285 to->GetOrCreateParallelMove(Instruction::GapPosition::START, code_zone());
287 CompressMoves(&to_move, dest);
288 DCHECK(dest->empty());
289 for (MoveOperands* m : to_move) {
294 void MoveOptimizer::CompressMoves(ParallelMove* left, MoveOpVector* right) {
295 if (right ==
nullptr)
return;
297 MoveOpVector& eliminated = local_vector();
298 DCHECK(eliminated.empty());
300 if (!left->empty()) {
303 for (MoveOperands* move : *right) {
304 if (move->IsRedundant())
continue;
305 left->PrepareInsertAfter(move, &eliminated);
308 for (MoveOperands* to_eliminate : eliminated) {
309 to_eliminate->Eliminate();
314 for (MoveOperands* move : *right) {
315 if (move->IsRedundant())
continue;
316 left->push_back(move);
320 DCHECK(eliminated.empty());
323 void MoveOptimizer::CompressGaps(Instruction* instruction) {
324 int i = FindFirstNonEmptySlot(instruction);
325 bool has_moves =
i <= Instruction::LAST_GAP_POSITION;
328 if (
i == Instruction::LAST_GAP_POSITION) {
329 std::swap(instruction->parallel_moves()[Instruction::FIRST_GAP_POSITION],
330 instruction->parallel_moves()[Instruction::LAST_GAP_POSITION]);
331 }
else if (
i == Instruction::FIRST_GAP_POSITION) {
333 instruction->parallel_moves()[Instruction::FIRST_GAP_POSITION],
334 instruction->parallel_moves()[Instruction::LAST_GAP_POSITION]);
339 ParallelMove* first =
340 instruction->parallel_moves()[Instruction::FIRST_GAP_POSITION];
342 instruction->parallel_moves()[Instruction::LAST_GAP_POSITION];
347 (first !=
nullptr && (last ==
nullptr || last->empty())));
350 void MoveOptimizer::CompressBlock(InstructionBlock* block) {
351 int first_instr_index = block->first_instruction_index();
352 int last_instr_index = block->last_instruction_index();
356 Instruction* prev_instr = code()->instructions()[first_instr_index];
357 RemoveClobberedDestinations(prev_instr);
359 for (
int index = first_instr_index + 1; index <= last_instr_index; ++index) {
360 Instruction* instr = code()->instructions()[index];
362 MigrateMoves(instr, prev_instr);
364 RemoveClobberedDestinations(instr);
369 const Instruction* MoveOptimizer::LastInstruction(
370 const InstructionBlock* block)
const {
371 return code()->instructions()[block->last_instruction_index()];
374 void MoveOptimizer::OptimizeMerge(InstructionBlock* block) {
375 DCHECK_LT(1, block->PredecessorCount());
378 for (RpoNumber& pred_index : block->predecessors()) {
379 const InstructionBlock* pred = code()->InstructionBlockAt(pred_index);
384 if (pred->SuccessorCount() > 1)
return;
386 const Instruction* last_instr =
387 code()->instructions()[pred->last_instruction_index()];
388 if (last_instr->IsCall())
return;
389 if (last_instr->TempCount() != 0)
return;
390 if (last_instr->OutputCount() != 0)
return;
391 for (
size_t i = 0;
i < last_instr->InputCount(); ++
i) {
392 const InstructionOperand* op = last_instr->InputAt(
i);
393 if (!op->IsConstant() && !op->IsImmediate())
return;
397 MoveMap move_map(local_zone());
398 size_t correct_counts = 0;
400 for (RpoNumber& pred_index : block->predecessors()) {
401 const InstructionBlock* pred = code()->InstructionBlockAt(pred_index);
402 const Instruction* instr = LastInstruction(pred);
403 if (instr->parallel_moves()[0] ==
nullptr ||
404 instr->parallel_moves()[0]->empty()) {
407 for (
const MoveOperands* move : *instr->parallel_moves()[0]) {
408 if (move->IsRedundant())
continue;
409 InstructionOperand src = move->source();
410 InstructionOperand dst = move->destination();
411 MoveKey key = {src, dst};
412 auto res = move_map.insert(std::make_pair(key, 1));
415 if (res.first->second == block->PredecessorCount()) {
421 if (move_map.empty() || correct_counts == 0)
return;
424 Instruction* instr = code()->instructions()[block->first_instruction_index()];
426 if (correct_counts != move_map.size()) {
429 OperandSet conflicting_srcs(&operand_buffer1);
430 for (
auto iter = move_map.begin(), end = move_map.end(); iter != end;) {
433 if (current->second != block->PredecessorCount()) {
434 InstructionOperand dest = current->first.destination;
439 conflicting_srcs.InsertOp(dest);
440 move_map.erase(current);
444 bool changed =
false;
449 for (
auto iter = move_map.begin(), end = move_map.end(); iter != end;) {
452 DCHECK_EQ(block->PredecessorCount(), current->second);
453 if (conflicting_srcs.ContainsOpOrAlias(current->first.source)) {
454 conflicting_srcs.InsertOp(current->first.destination);
455 move_map.erase(current);
462 if (move_map.empty())
return;
464 DCHECK_NOT_NULL(instr);
465 bool gap_initialized =
true;
466 if (instr->parallel_moves()[0] !=
nullptr &&
467 !instr->parallel_moves()[0]->empty()) {
469 gap_initialized =
false;
470 std::swap(instr->parallel_moves()[0], instr->parallel_moves()[1]);
472 ParallelMove* moves = instr->GetOrCreateParallelMove(
473 static_cast<Instruction::GapPosition>(0), code_zone());
475 bool first_iteration =
true;
476 for (RpoNumber& pred_index : block->predecessors()) {
477 const InstructionBlock* pred = code()->InstructionBlockAt(pred_index);
478 for (MoveOperands* move : *LastInstruction(pred)->parallel_moves()[0]) {
479 if (move->IsRedundant())
continue;
480 MoveKey key = {move->source(), move->destination()};
481 auto it = move_map.find(key);
482 if (it != move_map.end()) {
483 if (first_iteration) {
484 moves->AddMove(move->source(), move->destination());
489 first_iteration =
false;
492 if (!gap_initialized) {
493 CompressMoves(instr->parallel_moves()[0], instr->parallel_moves()[1]);
495 CompressBlock(block);
500 bool IsSlot(
const InstructionOperand& op) {
501 return op.IsStackSlot() || op.IsFPStackSlot();
504 bool LoadCompare(
const MoveOperands* a,
const MoveOperands* b) {
505 if (!a->source().EqualsCanonicalized(b->source())) {
506 return a->source().CompareCanonicalized(b->source());
508 if (IsSlot(a->destination()) && !IsSlot(b->destination()))
return false;
509 if (!IsSlot(a->destination()) && IsSlot(b->destination()))
return true;
510 return a->destination().CompareCanonicalized(b->destination());
517 void MoveOptimizer::FinalizeMoves(Instruction* instr) {
518 MoveOpVector& loads = local_vector();
519 DCHECK(loads.empty());
521 ParallelMove* parallel_moves = instr->parallel_moves()[0];
522 if (parallel_moves ==
nullptr)
return;
524 for (MoveOperands* move : *parallel_moves) {
525 if (move->IsRedundant())
continue;
526 if (move->source().IsConstant() || IsSlot(move->source())) {
527 loads.push_back(move);
530 if (loads.empty())
return;
533 std::sort(loads.begin(), loads.end(), LoadCompare);
534 MoveOperands* group_begin =
nullptr;
535 for (MoveOperands* load : loads) {
537 if (group_begin ==
nullptr ||
538 !load->source().EqualsCanonicalized(group_begin->source())) {
543 if (IsSlot(group_begin->destination()))
continue;
545 ParallelMove* slot_1 = instr->GetOrCreateParallelMove(
546 static_cast<Instruction::GapPosition>(1), code_zone());
547 slot_1->AddMove(group_begin->destination(), load->destination());