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 <algorithm>
8 #include <set>
9 
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13 
14 namespace {
15 
16 #define REP_BIT(rep) (1 << static_cast<int>(rep))
17 
18 const int kFloat32Bit = REP_BIT(MachineRepresentation::kFloat32);
19 const int kFloat64Bit = REP_BIT(MachineRepresentation::kFloat64);
20 
21 // Splits a FP move between two location operands into the equivalent series of
22 // moves between smaller sub-operands, e.g. a double move to two single moves.
23 // This helps reduce the number of cycles that would normally occur under FP
24 // aliasing, and makes swaps much easier to implement.
Split(MoveOperands * move,MachineRepresentation smaller_rep,ParallelMove * moves)25 MoveOperands* Split(MoveOperands* move, MachineRepresentation smaller_rep,
26                     ParallelMove* moves) {
27   DCHECK(!kSimpleFPAliasing);
28   // Splitting is only possible when the slot size is the same as float size.
29   DCHECK_EQ(kPointerSize, kFloatSize);
30   const LocationOperand& src_loc = LocationOperand::cast(move->source());
31   const LocationOperand& dst_loc = LocationOperand::cast(move->destination());
32   MachineRepresentation dst_rep = dst_loc.representation();
33   DCHECK_NE(smaller_rep, dst_rep);
34   auto src_kind = src_loc.location_kind();
35   auto dst_kind = dst_loc.location_kind();
36 
37   int aliases =
38       1 << (ElementSizeLog2Of(dst_rep) - ElementSizeLog2Of(smaller_rep));
39   int base = -1;
40   USE(base);
41   DCHECK_EQ(aliases, RegisterConfiguration::Default()->GetAliases(
42                          dst_rep, 0, smaller_rep, &base));
43 
44   int src_index = -1;
45   int slot_size = (1 << ElementSizeLog2Of(smaller_rep)) / kPointerSize;
46   int src_step = 1;
47   if (src_kind == LocationOperand::REGISTER) {
48     src_index = src_loc.register_code() * aliases;
49   } else {
50     src_index = src_loc.index();
51     // For operands that occupy multiple slots, the index refers to the last
52     // slot. On little-endian architectures, we start at the high slot and use a
53     // negative step so that register-to-slot moves are in the correct order.
54     src_step = -slot_size;
55   }
56   int dst_index = -1;
57   int dst_step = 1;
58   if (dst_kind == LocationOperand::REGISTER) {
59     dst_index = dst_loc.register_code() * aliases;
60   } else {
61     dst_index = dst_loc.index();
62     dst_step = -slot_size;
63   }
64 
65   // Reuse 'move' for the first fragment. It is not pending.
66   move->set_source(AllocatedOperand(src_kind, smaller_rep, src_index));
67   move->set_destination(AllocatedOperand(dst_kind, smaller_rep, dst_index));
68   // Add the remaining fragment moves.
69   for (int i = 1; i < aliases; ++i) {
70     src_index += src_step;
71     dst_index += dst_step;
72     moves->AddMove(AllocatedOperand(src_kind, smaller_rep, src_index),
73                    AllocatedOperand(dst_kind, smaller_rep, dst_index));
74   }
75   // Return the first fragment.
76   return move;
77 }
78 
79 }  // namespace
80 
Resolve(ParallelMove * moves)81 void GapResolver::Resolve(ParallelMove* moves) {
82   // Clear redundant moves, and collect FP move representations if aliasing
83   // is non-simple.
84   int reps = 0;
85   for (size_t i = 0; i < moves->size();) {
86     MoveOperands* move = (*moves)[i];
87     if (move->IsRedundant()) {
88       (*moves)[i] = moves->back();
89       moves->pop_back();
90       continue;
91     }
92     i++;
93     if (!kSimpleFPAliasing && move->destination().IsFPRegister()) {
94       reps |=
95           REP_BIT(LocationOperand::cast(move->destination()).representation());
96     }
97   }
98 
99   if (!kSimpleFPAliasing) {
100     if (reps && !base::bits::IsPowerOfTwo(reps)) {
101       // Start with the smallest FP moves, so we never encounter smaller moves
102       // in the middle of a cycle of larger moves.
103       if ((reps & kFloat32Bit) != 0) {
104         split_rep_ = MachineRepresentation::kFloat32;
105         for (size_t i = 0; i < moves->size(); ++i) {
106           auto move = (*moves)[i];
107           if (!move->IsEliminated() && move->destination().IsFloatRegister())
108             PerformMove(moves, move);
109         }
110       }
111       if ((reps & kFloat64Bit) != 0) {
112         split_rep_ = MachineRepresentation::kFloat64;
113         for (size_t i = 0; i < moves->size(); ++i) {
114           auto move = (*moves)[i];
115           if (!move->IsEliminated() && move->destination().IsDoubleRegister())
116             PerformMove(moves, move);
117         }
118       }
119     }
120     split_rep_ = MachineRepresentation::kSimd128;
121   }
122 
123   for (size_t i = 0; i < moves->size(); ++i) {
124     auto move = (*moves)[i];
125     if (!move->IsEliminated()) PerformMove(moves, move);
126   }
127 }
128 
PerformMove(ParallelMove * moves,MoveOperands * move)129 void GapResolver::PerformMove(ParallelMove* moves, MoveOperands* move) {
130   // Each call to this function performs a move and deletes it from the move
131   // graph.  We first recursively perform any move blocking this one.  We mark a
132   // move as "pending" on entry to PerformMove in order to detect cycles in the
133   // move graph.  We use operand swaps to resolve cycles, which means that a
134   // call to PerformMove could change any source operand in the move graph.
135   DCHECK(!move->IsPending());
136   DCHECK(!move->IsRedundant());
137 
138   // Clear this move's destination to indicate a pending move.  The actual
139   // destination is saved on the side.
140   InstructionOperand source = move->source();
141   DCHECK(!source.IsInvalid());  // Or else it will look eliminated.
142   InstructionOperand destination = move->destination();
143   move->SetPending();
144 
145   // We may need to split moves between FP locations differently.
146   const bool is_fp_loc_move =
147       !kSimpleFPAliasing && destination.IsFPLocationOperand();
148 
149   // Perform a depth-first traversal of the move graph to resolve dependencies.
150   // Any unperformed, unpending move with a source the same as this one's
151   // destination blocks this one so recursively perform all such moves.
152   for (size_t i = 0; i < moves->size(); ++i) {
153     auto other = (*moves)[i];
154     if (other->IsEliminated()) continue;
155     if (other->IsPending()) continue;
156     if (other->source().InterferesWith(destination)) {
157       if (is_fp_loc_move &&
158           LocationOperand::cast(other->source()).representation() >
159               split_rep_) {
160         // 'other' must also be an FP location move. Break it into fragments
161         // of the same size as 'move'. 'other' is set to one of the fragments,
162         // and the rest are appended to 'moves'.
163         other = Split(other, split_rep_, moves);
164         // 'other' may not block destination now.
165         if (!other->source().InterferesWith(destination)) continue;
166       }
167       // Though PerformMove can change any source operand in the move graph,
168       // this call cannot create a blocking move via a swap (this loop does not
169       // miss any).  Assume there is a non-blocking move with source A and this
170       // move is blocked on source B and there is a swap of A and B.  Then A and
171       // B must be involved in the same cycle (or they would not be swapped).
172       // Since this move's destination is B and there is only a single incoming
173       // edge to an operand, this move must also be involved in the same cycle.
174       // In that case, the blocking move will be created but will be "pending"
175       // when we return from PerformMove.
176       PerformMove(moves, other);
177     }
178   }
179 
180   // This move's source may have changed due to swaps to resolve cycles and so
181   // it may now be the last move in the cycle.  If so remove it.
182   source = move->source();
183   if (source.EqualsCanonicalized(destination)) {
184     move->Eliminate();
185     return;
186   }
187 
188   // We are about to resolve this move and don't need it marked as pending, so
189   // restore its destination.
190   move->set_destination(destination);
191 
192   // The move may be blocked on a (at most one) pending move, in which case we
193   // have a cycle.  Search for such a blocking move and perform a swap to
194   // resolve it.
195   auto blocker =
196       std::find_if(moves->begin(), moves->end(), [&](MoveOperands* move) {
197         return !move->IsEliminated() &&
198                move->source().InterferesWith(destination);
199       });
200   if (blocker == moves->end()) {
201     // The easy case: This move is not blocked.
202     assembler_->AssembleMove(&source, &destination);
203     move->Eliminate();
204     return;
205   }
206 
207   // Ensure source is a register or both are stack slots, to limit swap cases.
208   if (source.IsStackSlot() || source.IsFPStackSlot()) {
209     std::swap(source, destination);
210   }
211   assembler_->AssembleSwap(&source, &destination);
212   move->Eliminate();
213 
214   // Update outstanding moves whose source may now have been moved.
215   if (is_fp_loc_move) {
216     // We may have to split larger moves.
217     for (size_t i = 0; i < moves->size(); ++i) {
218       auto other = (*moves)[i];
219       if (other->IsEliminated()) continue;
220       if (source.InterferesWith(other->source())) {
221         if (LocationOperand::cast(other->source()).representation() >
222             split_rep_) {
223           other = Split(other, split_rep_, moves);
224           if (!source.InterferesWith(other->source())) continue;
225         }
226         other->set_source(destination);
227       } else if (destination.InterferesWith(other->source())) {
228         if (LocationOperand::cast(other->source()).representation() >
229             split_rep_) {
230           other = Split(other, split_rep_, moves);
231           if (!destination.InterferesWith(other->source())) continue;
232         }
233         other->set_source(source);
234       }
235     }
236   } else {
237     for (auto other : *moves) {
238       if (other->IsEliminated()) continue;
239       if (source.EqualsCanonicalized(other->source())) {
240         other->set_source(destination);
241       } else if (destination.EqualsCanonicalized(other->source())) {
242         other->set_source(source);
243       }
244     }
245   }
246 }
247 }  // namespace compiler
248 }  // namespace internal
249 }  // namespace v8
250