summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/move-optimizer.cc
blob: 2c4c720d263b50bd909acd5800b2eccd8df6bc48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/move-optimizer.h"

namespace v8 {
namespace internal {
namespace compiler {

namespace {

typedef std::pair<InstructionOperand, InstructionOperand> MoveKey;
typedef ZoneMap<MoveKey, unsigned> MoveMap;
typedef ZoneSet<InstructionOperand> OperandSet;


bool GapsCanMoveOver(Instruction* instr) {
  DCHECK(!instr->IsGapMoves());
  return instr->IsSourcePosition() || instr->IsNop();
}


int FindFirstNonEmptySlot(GapInstruction* gap) {
  int i = GapInstruction::FIRST_INNER_POSITION;
  for (; i <= GapInstruction::LAST_INNER_POSITION; i++) {
    auto move = gap->parallel_moves()[i];
    if (move == nullptr) continue;
    auto move_ops = move->move_operands();
    auto op = move_ops->begin();
    for (; op != move_ops->end(); ++op) {
      if (!op->IsRedundant()) break;
      op->Eliminate();
    }
    if (op != move_ops->end()) break;  // Found non-redundant move.
    move_ops->Rewind(0);               // Clear this redundant move.
  }
  return i;
}

}  // namepace


MoveOptimizer::MoveOptimizer(Zone* local_zone, InstructionSequence* code)
    : local_zone_(local_zone),
      code_(code),
      to_finalize_(local_zone),
      temp_vector_0_(local_zone),
      temp_vector_1_(local_zone) {}


void MoveOptimizer::Run() {
  for (auto* block : code()->instruction_blocks()) {
    CompressBlock(block);
  }
  for (auto block : code()->instruction_blocks()) {
    if (block->PredecessorCount() <= 1) continue;
    OptimizeMerge(block);
  }
  for (auto gap : to_finalize_) {
    FinalizeMoves(gap);
  }
}


void MoveOptimizer::CompressMoves(MoveOpVector* eliminated, ParallelMove* left,
                                  ParallelMove* right) {
  DCHECK(eliminated->empty());
  auto move_ops = right->move_operands();
  if (!left->move_operands()->is_empty()) {
    // Modify the right moves in place and collect moves that will be killed by
    // merging the two gaps.
    for (auto op = move_ops->begin(); op != move_ops->end(); ++op) {
      if (op->IsRedundant()) continue;
      auto to_eliminate = left->PrepareInsertAfter(op);
      if (to_eliminate != nullptr) eliminated->push_back(to_eliminate);
    }
    // Eliminate dead moves.  Must happen before insertion of new moves as the
    // contents of eliminated are pointers into a list.
    for (auto to_eliminate : *eliminated) {
      to_eliminate->Eliminate();
    }
    eliminated->clear();
  }
  // Add all possibly modified moves from right side.
  for (auto op = move_ops->begin(); op != move_ops->end(); ++op) {
    if (op->IsRedundant()) continue;
    left->move_operands()->Add(*op, code_zone());
  }
  // Nuke right.
  move_ops->Rewind(0);
}


// Smash all consecutive moves into the left most move slot and accumulate them
// as much as possible across instructions.
void MoveOptimizer::CompressBlock(InstructionBlock* block) {
  auto temp_vector = temp_vector_0();
  DCHECK(temp_vector.empty());
  GapInstruction* prev_gap = nullptr;
  for (int index = block->code_start(); index < block->code_end(); ++index) {
    auto instr = code()->instructions()[index];
    if (!instr->IsGapMoves()) {
      if (GapsCanMoveOver(instr)) continue;
      if (prev_gap != nullptr) to_finalize_.push_back(prev_gap);
      prev_gap = nullptr;
      continue;
    }
    auto gap = GapInstruction::cast(instr);
    int i = FindFirstNonEmptySlot(gap);
    // Nothing to do here.
    if (i == GapInstruction::LAST_INNER_POSITION + 1) {
      if (prev_gap != nullptr) {
        // Slide prev_gap down so we always know where to look for it.
        std::swap(prev_gap->parallel_moves()[0], gap->parallel_moves()[0]);
        prev_gap = gap;
      }
      continue;
    }
    // Move the first non-empty gap to position 0.
    std::swap(gap->parallel_moves()[0], gap->parallel_moves()[i]);
    auto left = gap->parallel_moves()[0];
    // Compress everything into position 0.
    for (++i; i <= GapInstruction::LAST_INNER_POSITION; ++i) {
      auto move = gap->parallel_moves()[i];
      if (move == nullptr) continue;
      CompressMoves(&temp_vector, left, move);
    }
    if (prev_gap != nullptr) {
      // Smash left into prev_gap, killing left.
      auto pred_moves = prev_gap->parallel_moves()[0];
      CompressMoves(&temp_vector, pred_moves, left);
      // Slide prev_gap down so we always know where to look for it.
      std::swap(prev_gap->parallel_moves()[0], gap->parallel_moves()[0]);
    }
    prev_gap = gap;
  }
  if (prev_gap != nullptr) to_finalize_.push_back(prev_gap);
}


GapInstruction* MoveOptimizer::LastGap(InstructionBlock* block) {
  int gap_index = block->last_instruction_index() - 1;
  auto instr = code()->instructions()[gap_index];
  return GapInstruction::cast(instr);
}


void MoveOptimizer::OptimizeMerge(InstructionBlock* block) {
  DCHECK(block->PredecessorCount() > 1);
  // Ensure that the last instruction in all incoming blocks don't contain
  // things that would prevent moving gap moves across them.
  for (auto pred_index : block->predecessors()) {
    auto pred = code()->InstructionBlockAt(pred_index);
    auto last_instr = code()->instructions()[pred->last_instruction_index()];
    DCHECK(!last_instr->IsGapMoves());
    if (last_instr->IsSourcePosition()) continue;
    if (last_instr->IsCall()) return;
    if (last_instr->TempCount() != 0) return;
    if (last_instr->OutputCount() != 0) return;
    for (size_t i = 0; i < last_instr->InputCount(); ++i) {
      auto op = last_instr->InputAt(i);
      if (!op->IsConstant() && !op->IsImmediate()) return;
    }
  }
  // TODO(dcarney): pass a ZonePool down for this?
  MoveMap move_map(local_zone());
  size_t correct_counts = 0;
  // Accumulate set of shared moves.
  for (auto pred_index : block->predecessors()) {
    auto pred = code()->InstructionBlockAt(pred_index);
    auto gap = LastGap(pred);
    if (gap->parallel_moves()[0] == nullptr ||
        gap->parallel_moves()[0]->move_operands()->is_empty()) {
      return;
    }
    auto move_ops = gap->parallel_moves()[0]->move_operands();
    for (auto op = move_ops->begin(); op != move_ops->end(); ++op) {
      if (op->IsRedundant()) continue;
      auto src = *op->source();
      auto dst = *op->destination();
      MoveKey key = {src, dst};
      auto res = move_map.insert(std::make_pair(key, 1));
      if (!res.second) {
        res.first->second++;
        if (res.first->second == block->PredecessorCount()) {
          correct_counts++;
        }
      }
    }
  }
  if (move_map.empty() || correct_counts != move_map.size()) return;
  // Find insertion point.
  GapInstruction* gap = nullptr;
  for (int i = block->first_instruction_index();
       i <= block->last_instruction_index(); ++i) {
    auto instr = code()->instructions()[i];
    if (instr->IsGapMoves()) {
      gap = GapInstruction::cast(instr);
      continue;
    }
    if (!GapsCanMoveOver(instr)) break;
  }
  DCHECK(gap != nullptr);
  bool gap_initialized = true;
  if (gap->parallel_moves()[0] == nullptr ||
      gap->parallel_moves()[0]->move_operands()->is_empty()) {
    to_finalize_.push_back(gap);
  } else {
    // Will compress after insertion.
    gap_initialized = false;
    std::swap(gap->parallel_moves()[0], gap->parallel_moves()[1]);
  }
  auto move = gap->GetOrCreateParallelMove(
      static_cast<GapInstruction::InnerPosition>(0), code_zone());
  // Delete relevant entries in predecessors and move everything to block.
  bool first_iteration = true;
  for (auto pred_index : block->predecessors()) {
    auto pred = code()->InstructionBlockAt(pred_index);
    auto gap = LastGap(pred);
    auto move_ops = gap->parallel_moves()[0]->move_operands();
    for (auto op = move_ops->begin(); op != move_ops->end(); ++op) {
      if (op->IsRedundant()) continue;
      MoveKey key = {*op->source(), *op->destination()};
      auto it = move_map.find(key);
      USE(it);
      DCHECK(it != move_map.end());
      if (first_iteration) {
        move->AddMove(op->source(), op->destination(), code_zone());
      }
      op->Eliminate();
    }
    first_iteration = false;
  }
  // Compress.
  if (!gap_initialized) {
    CompressMoves(&temp_vector_0(), gap->parallel_moves()[0],
                  gap->parallel_moves()[1]);
  }
}


// Split multiple loads of the same constant or stack slot off into the second
// slot and keep remaining moves in the first slot.
void MoveOptimizer::FinalizeMoves(GapInstruction* gap) {
  auto loads = temp_vector_0();
  DCHECK(loads.empty());
  auto new_moves = temp_vector_1();
  DCHECK(new_moves.empty());
  auto move_ops = gap->parallel_moves()[0]->move_operands();
  for (auto move = move_ops->begin(); move != move_ops->end(); ++move) {
    if (move->IsRedundant()) {
      move->Eliminate();
      continue;
    }
    if (!(move->source()->IsConstant() || move->source()->IsStackSlot() ||
          move->source()->IsDoubleStackSlot()))
      continue;
    // Search for existing move to this slot.
    MoveOperands* found = nullptr;
    for (auto load : loads) {
      if (load->source()->Equals(move->source())) {
        found = load;
        break;
      }
    }
    // Not found so insert.
    if (found == nullptr) {
      loads.push_back(move);
      // Replace source with copy for later use.
      auto dest = move->destination();
      move->set_destination(
          InstructionOperand::New(code_zone(), dest->kind(), dest->index()));
      continue;
    }
    if ((found->destination()->IsStackSlot() ||
         found->destination()->IsDoubleStackSlot()) &&
        !(move->destination()->IsStackSlot() ||
          move->destination()->IsDoubleStackSlot())) {
      // Found a better source for this load.  Smash it in place to affect other
      // loads that have already been split.
      InstructionOperand::Kind found_kind = found->destination()->kind();
      int found_index = found->destination()->index();
      auto next_dest =
          InstructionOperand::New(code_zone(), found_kind, found_index);
      auto dest = move->destination();
      found->destination()->ConvertTo(dest->kind(), dest->index());
      move->set_destination(next_dest);
    }
    // move from load destination.
    move->set_source(found->destination());
    new_moves.push_back(move);
  }
  loads.clear();
  if (new_moves.empty()) return;
  // Insert all new moves into slot 1.
  auto slot_1 = gap->GetOrCreateParallelMove(
      static_cast<GapInstruction::InnerPosition>(1), code_zone());
  DCHECK(slot_1->move_operands()->is_empty());
  slot_1->move_operands()->AddBlock(MoveOperands(nullptr, nullptr),
                                    static_cast<int>(new_moves.size()),
                                    code_zone());
  auto it = slot_1->move_operands()->begin();
  for (auto new_move : new_moves) {
    std::swap(*new_move, *it);
    ++it;
  }
  DCHECK_EQ(it, slot_1->move_operands()->end());
  new_moves.clear();
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8