38 // definitions. During propagation, split the live range around regions of
39 // High Register Pressure (HRP). If a Def is in a region of Low Register
40 // Pressure (LRP), it will not get spilled until we encounter a region of
41 // HRP between it and one of its uses. We will spill at the transition
42 // point between LRP and HRP. Uses in the HRP region will use the spilled
43 // Def. The first Use outside the HRP region will generate a SpillCopy to
44 // hoist the live range back up into a register, and all subsequent uses
45 // will use that new Def until another HRP region is encountered. Defs in
46 // HRP regions will get trailing SpillCopies to push the LRG down into the
47 // stack immediately.
48 //
49 // As a side effect, unlink from (hence make dead) coalesced copies.
50 //
51
52 static const char out_of_nodes[] = "out of nodes during split";
53
54 //------------------------------get_spillcopy_wide-----------------------------
55 // Get a SpillCopy node with wide-enough masks. Use the 'wide-mask', the
56 // wide ideal-register spill-mask if possible. If the 'wide-mask' does
57 // not cover the input (or output), use the input (or output) mask instead.
58 Node *PhaseChaitin::get_spillcopy_wide(MachSpillCopyNode::SpillType spill_type, Node *def, Node *use, uint uidx ) {
59 // If ideal reg doesn't exist we've got a bad schedule happening
60 // that is forcing us to spill something that isn't spillable.
61 // Bail rather than abort
62 int ireg = def->ideal_reg();
63 if( ireg == 0 || ireg == Op_RegFlags ) {
64 assert(false, "attempted to spill a non-spillable item");
65 C->record_method_not_compilable("attempted to spill a non-spillable item");
66 return NULL;
67 }
68 if (C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {
69 return NULL;
70 }
71 const RegMask *i_mask = &def->out_RegMask();
72 const RegMask *w_mask = C->matcher()->idealreg2spillmask[ireg];
73 const RegMask *o_mask = use ? &use->in_RegMask(uidx) : w_mask;
74 const RegMask *w_i_mask = w_mask->overlap( *i_mask ) ? w_mask : i_mask;
75 const RegMask *w_o_mask;
76
77 int num_regs = RegMask::num_registers(ireg);
78 bool is_vect = RegMask::is_vector(ireg);
79 if( w_mask->overlap( *o_mask ) && // Overlap AND
80 ((num_regs == 1) // Single use or aligned
81 || is_vect // or vector
82 || !is_vect && o_mask->is_aligned_pairs()) ) {
83 assert(!is_vect || o_mask->is_aligned_sets(num_regs), "vectors are aligned");
84 // Don't come here for mis-aligned doubles
291 tty->print_cr("into block:");
292 b->dump();
293 }
294 #endif
295 if (C->subsume_loads() == true && !C->failing()) {
296 // Retry with subsume_loads == false
297 // If this is the first failure, the sentinel string will "stick"
298 // to the Compile object, and the C2Compiler will see it and retry.
299 C->record_failure(C2Compiler::retry_no_subsuming_loads());
300 } else {
301 // Bailout without retry
302 C->record_method_not_compilable("RA Split failed: attempt to clone node with anti_dependence");
303 }
304 return 0;
305 }
306 return def->clone();
307 }
308
309 //------------------------------split_Rematerialize----------------------------
310 // Clone a local copy of the def.
311 Node *PhaseChaitin::split_Rematerialize( Node *def, Block *b, uint insidx, uint &maxlrg, GrowableArray<uint> splits, int slidx, uint *lrg2reach, Node **Reachblock, bool walkThru ) {
312 // The input live ranges will be stretched to the site of the new
313 // instruction. They might be stretched past a def and will thus
314 // have the old and new values of the same live range alive at the
315 // same time - a definite no-no. Split out private copies of
316 // the inputs.
317 if( def->req() > 1 ) {
318 for( uint i = 1; i < def->req(); i++ ) {
319 Node *in = def->in(i);
320 uint lidx = _lrg_map.live_range_id(in);
321 // We do not need this for live ranges that are only defined once.
322 // However, this is not true for spill copies that are added in this
323 // Split() pass, since they might get coalesced later on in this pass.
324 if (lidx < _lrg_map.max_lrg_id() && lrgs(lidx).is_singledef()) {
325 continue;
326 }
327
328 Block *b_def = _cfg.get_block_for_node(def);
329 int idx_def = b_def->find_node(def);
330 Node *in_spill = get_spillcopy_wide(MachSpillCopyNode::InputToRematerialization, in, def, i );
331 if( !in_spill ) return 0; // Bailed out
332 insert_proj(b_def,idx_def,in_spill,maxlrg++);
333 if( b_def == b )
334 insidx++;
335 def->set_req(i,in_spill);
336 }
337 }
338
339 Node *spill = clone_node(def, b, C);
340 if (spill == NULL || C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {
341 // Check when generating nodes
342 return 0;
343 }
344
345 // See if any inputs are currently being spilled, and take the
346 // latest copy of spilled inputs.
347 if( spill->req() > 1 ) {
348 for( uint i = 1; i < spill->req(); i++ ) {
349 Node *in = spill->in(i);
350 uint lidx = _lrg_map.find_id(in);
351
352 // Walk backwards thru spill copy node intermediates
353 if (walkThru) {
354 while (in->is_SpillCopy() && lidx >= _lrg_map.max_lrg_id()) {
355 in = in->in(1);
|
38 // definitions. During propagation, split the live range around regions of
39 // High Register Pressure (HRP). If a Def is in a region of Low Register
40 // Pressure (LRP), it will not get spilled until we encounter a region of
41 // HRP between it and one of its uses. We will spill at the transition
42 // point between LRP and HRP. Uses in the HRP region will use the spilled
43 // Def. The first Use outside the HRP region will generate a SpillCopy to
44 // hoist the live range back up into a register, and all subsequent uses
45 // will use that new Def until another HRP region is encountered. Defs in
46 // HRP regions will get trailing SpillCopies to push the LRG down into the
47 // stack immediately.
48 //
49 // As a side effect, unlink from (hence make dead) coalesced copies.
50 //
51
52 static const char out_of_nodes[] = "out of nodes during split";
53
54 //------------------------------get_spillcopy_wide-----------------------------
55 // Get a SpillCopy node with wide-enough masks. Use the 'wide-mask', the
56 // wide ideal-register spill-mask if possible. If the 'wide-mask' does
57 // not cover the input (or output), use the input (or output) mask instead.
58 Node *PhaseChaitin::get_spillcopy_wide(MachSpillCopyNode::SpillType spill_type, Node *def, Node *use, uint uidx) {
59 // If ideal reg doesn't exist we've got a bad schedule happening
60 // that is forcing us to spill something that isn't spillable.
61 // Bail rather than abort
62 int ireg = def->ideal_reg();
63 if (ireg == 0 || ireg == Op_RegFlags) {
64 assert(false, "attempted to spill a non-spillable item: %d: %s <- %d: %s, ireg = %d, spill_type: %s",
65 def->_idx, def->Name(), use->_idx, use->Name(), ireg,
66 MachSpillCopyNode::spill_type(spill_type));
67 C->record_method_not_compilable("attempted to spill a non-spillable item");
68 return NULL;
69 }
70 if (C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {
71 return NULL;
72 }
73 const RegMask *i_mask = &def->out_RegMask();
74 const RegMask *w_mask = C->matcher()->idealreg2spillmask[ireg];
75 const RegMask *o_mask = use ? &use->in_RegMask(uidx) : w_mask;
76 const RegMask *w_i_mask = w_mask->overlap( *i_mask ) ? w_mask : i_mask;
77 const RegMask *w_o_mask;
78
79 int num_regs = RegMask::num_registers(ireg);
80 bool is_vect = RegMask::is_vector(ireg);
81 if( w_mask->overlap( *o_mask ) && // Overlap AND
82 ((num_regs == 1) // Single use or aligned
83 || is_vect // or vector
84 || !is_vect && o_mask->is_aligned_pairs()) ) {
85 assert(!is_vect || o_mask->is_aligned_sets(num_regs), "vectors are aligned");
86 // Don't come here for mis-aligned doubles
293 tty->print_cr("into block:");
294 b->dump();
295 }
296 #endif
297 if (C->subsume_loads() == true && !C->failing()) {
298 // Retry with subsume_loads == false
299 // If this is the first failure, the sentinel string will "stick"
300 // to the Compile object, and the C2Compiler will see it and retry.
301 C->record_failure(C2Compiler::retry_no_subsuming_loads());
302 } else {
303 // Bailout without retry
304 C->record_method_not_compilable("RA Split failed: attempt to clone node with anti_dependence");
305 }
306 return 0;
307 }
308 return def->clone();
309 }
310
311 //------------------------------split_Rematerialize----------------------------
312 // Clone a local copy of the def.
313 Node *PhaseChaitin::split_Rematerialize(Node *def, Block *b, uint insidx, uint &maxlrg,
314 GrowableArray<uint> splits, int slidx, uint *lrg2reach,
315 Node **Reachblock, bool walkThru) {
316 // The input live ranges will be stretched to the site of the new
317 // instruction. They might be stretched past a def and will thus
318 // have the old and new values of the same live range alive at the
319 // same time - a definite no-no. Split out private copies of
320 // the inputs.
321 if (def->req() > 1) {
322 for (uint i = 1; i < def->req(); i++) {
323 Node *in = def->in(i);
324 uint lidx = _lrg_map.live_range_id(in);
325 // We do not need this for live ranges that are only defined once.
326 // However, this is not true for spill copies that are added in this
327 // Split() pass, since they might get coalesced later on in this pass.
328 if (lidx < _lrg_map.max_lrg_id() && lrgs(lidx).is_singledef()) {
329 continue;
330 }
331
332 Block *b_def = _cfg.get_block_for_node(def);
333 int idx_def = b_def->find_node(def);
334 // Cannot spill Op_RegFlags.
335 Node *in_spill;
336 if (in->ideal_reg() != Op_RegFlags) {
337 in_spill = get_spillcopy_wide(MachSpillCopyNode::InputToRematerialization, in, def, i);
338 if (!in_spill) { return 0; } // Bailed out
339 insert_proj(b_def, idx_def, in_spill, maxlrg++);
340 if (b_def == b) {
341 insidx++;
342 }
343 def->set_req(i, in_spill);
344 } else {
345 // The 'in' defines a flag register. Flag registers can not be spilled.
346 // Register allocation handles live ranges with flag registers
347 // by rematerializing the def (in this case 'in'). Thus, this is not
348 // critical if the input can be rematerialized, too.
349 if (!in->rematerialize()) {
350 assert(false, "Can not rematerialize %d: %s. Prolongs RegFlags live"
351 " range and defining node %d: %s may not be rematerialized.",
352 def->_idx, def->Name(), in->_idx, in->Name());
353 C->record_method_not_compilable("attempted to spill a non-spillable item with RegFlags input");
354 return 0; // Bailed out
355 }
356 }
357 }
358 }
359
360 Node *spill = clone_node(def, b, C);
361 if (spill == NULL || C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {
362 // Check when generating nodes
363 return 0;
364 }
365
366 // See if any inputs are currently being spilled, and take the
367 // latest copy of spilled inputs.
368 if( spill->req() > 1 ) {
369 for( uint i = 1; i < spill->req(); i++ ) {
370 Node *in = spill->in(i);
371 uint lidx = _lrg_map.find_id(in);
372
373 // Walk backwards thru spill copy node intermediates
374 if (walkThru) {
375 while (in->is_SpillCopy() && lidx >= _lrg_map.max_lrg_id()) {
376 in = in->in(1);
|