1 /*
2 * Copyright (c) 1998, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 // FORMS.CPP - Definitions for ADL Parser Forms Classes
26 #include "adlc.hpp"
27
28 #define remaining_buflen(buffer, position) (sizeof(buffer) - ((position) - (buffer)))
29
30 //==============================Instructions===================================
31 //------------------------------InstructForm-----------------------------------
32 InstructForm::InstructForm(const char *id, bool ideal_only)
33 : _ident(id), _ideal_only(ideal_only),
34 _localNames(cmpstr, hashstr, Form::arena),
35 _effects(cmpstr, hashstr, Form::arena),
36 _is_mach_constant(false),
37 _needs_constant_base(false),
38 _has_call(false)
39 {
40 _ftype = Form::INS;
41
42 _matrule = nullptr;
43 _insencode = nullptr;
44 _constant = nullptr;
45 _is_postalloc_expand = false;
46 _opcode = nullptr;
47 _size = nullptr;
48 _attribs = nullptr;
49 _predicate = nullptr;
50 _exprule = nullptr;
51 _rewrule = nullptr;
52 _format = nullptr;
53 _peephole = nullptr;
54 _ins_pipe = nullptr;
55 _flag = nullptr;
56 _uniq_idx = nullptr;
57 _num_uniq = 0;
58 _cisc_spill_operand = Not_cisc_spillable;// Which operand may cisc-spill
59 _cisc_spill_alternate = nullptr; // possible cisc replacement
60 _cisc_reg_mask_name = nullptr;
61 _is_cisc_alternate = false;
62 _is_short_branch = false;
63 _short_branch_form = nullptr;
64 _alignment = 1;
65 }
66
67 InstructForm::InstructForm(const char *id, InstructForm *instr, MatchRule *rule)
68 : _ident(id), _ideal_only(false),
69 _localNames(instr->_localNames),
70 _effects(instr->_effects),
71 _is_mach_constant(instr->_is_mach_constant),
72 _needs_constant_base(false),
73 _has_call(false)
74 {
75 _ftype = Form::INS;
76
77 _matrule = rule;
78 _insencode = instr->_insencode;
79 _constant = instr->_constant;
80 _is_postalloc_expand = instr->_is_postalloc_expand;
81 _opcode = instr->_opcode;
82 _size = instr->_size;
83 _attribs = instr->_attribs;
84 _predicate = instr->_predicate;
85 _exprule = instr->_exprule;
86 _rewrule = instr->_rewrule;
87 _format = instr->_format;
88 _peephole = instr->_peephole;
89 _ins_pipe = instr->_ins_pipe;
90 _flag = instr->_flag;
91 _uniq_idx = instr->_uniq_idx;
92 _num_uniq = instr->_num_uniq;
93 _cisc_spill_operand = Not_cisc_spillable; // Which operand may cisc-spill
94 _cisc_spill_alternate = nullptr; // possible cisc replacement
95 _cisc_reg_mask_name = nullptr;
96 _is_cisc_alternate = false;
97 _is_short_branch = false;
98 _short_branch_form = nullptr;
99 _alignment = 1;
100 // Copy parameters
101 const char *name;
102 instr->_parameters.reset();
103 for (; (name = instr->_parameters.iter()) != nullptr;)
104 _parameters.addName(name);
105 }
106
107 InstructForm::~InstructForm() {
108 }
109
110 InstructForm *InstructForm::is_instruction() const {
111 return (InstructForm*)this;
112 }
113
114 bool InstructForm::ideal_only() const {
115 return _ideal_only;
116 }
117
118 bool InstructForm::sets_result() const {
119 return (_matrule != nullptr && _matrule->sets_result());
120 }
121
122 bool InstructForm::needs_projections() {
123 _components.reset();
124 for( Component *comp; (comp = _components.iter()) != nullptr; ) {
125 if (comp->isa(Component::KILL)) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132
133 bool InstructForm::has_temps() {
134 if (_matrule) {
135 // Examine each component to see if it is a TEMP
136 _components.reset();
137 // Skip the first component, if already handled as (SET dst (...))
138 Component *comp = nullptr;
139 if (sets_result()) comp = _components.iter();
140 while ((comp = _components.iter()) != nullptr) {
141 if (comp->isa(Component::TEMP)) {
142 return true;
143 }
144 }
145 }
146
147 return false;
148 }
149
150 uint InstructForm::num_defs_or_kills() {
151 uint defs_or_kills = 0;
152
153 _components.reset();
154 for( Component *comp; (comp = _components.iter()) != nullptr; ) {
155 if( comp->isa(Component::DEF) || comp->isa(Component::KILL) ) {
156 ++defs_or_kills;
157 }
158 }
159
160 return defs_or_kills;
161 }
162
163 // This instruction has an expand rule?
164 bool InstructForm::expands() const {
165 return ( _exprule != nullptr );
166 }
167
168 // This instruction has a late expand rule?
169 bool InstructForm::postalloc_expands() const {
170 return _is_postalloc_expand;
171 }
172
173 // This instruction has a peephole rule?
174 Peephole *InstructForm::peepholes() const {
175 return _peephole;
176 }
177
178 // This instruction has a peephole rule?
179 void InstructForm::append_peephole(Peephole *peephole) {
180 if( _peephole == nullptr ) {
181 _peephole = peephole;
182 } else {
183 _peephole->append_peephole(peephole);
184 }
185 }
186
187
188 // ideal opcode enumeration
189 const char *InstructForm::ideal_Opcode( FormDict &globalNames ) const {
190 if( !_matrule ) return "Node"; // Something weird
191 // Chain rules do not really have ideal Opcodes; use their source
192 // operand ideal Opcode instead.
193 if( is_simple_chain_rule(globalNames) ) {
194 const char *src = _matrule->_rChild->_opType;
195 OperandForm *src_op = globalNames[src]->is_operand();
196 assert( src_op, "Not operand class of chain rule" );
197 if( !src_op->_matrule ) return "Node";
198 return src_op->_matrule->_opType;
199 }
200 // Operand chain rules do not really have ideal Opcodes
201 if( _matrule->is_chain_rule(globalNames) )
202 return "Node";
203 return strcmp(_matrule->_opType,"Set")
204 ? _matrule->_opType
205 : _matrule->_rChild->_opType;
206 }
207
208 // Recursive check on all operands' match rules in my match rule
209 bool InstructForm::is_pinned(FormDict &globals) {
210 if ( ! _matrule) return false;
211
212 int index = 0;
213 if (_matrule->find_type("Goto", index)) return true;
214 if (_matrule->find_type("If", index)) return true;
215 if (_matrule->find_type("CountedLoopEnd", index)) return true;
216 if (_matrule->find_type("Return", index)) return true;
217 if (_matrule->find_type("Rethrow", index)) return true;
218 if (_matrule->find_type("TailCall", index)) return true;
219 if (_matrule->find_type("TailJump", index)) return true;
220 if (_matrule->find_type("ForwardException", index)) return true;
221 if (_matrule->find_type("Halt", index)) return true;
222 if (_matrule->find_type("Jump", index)) return true;
223
224 return is_parm(globals);
225 }
226
227 // Recursive check on all operands' match rules in my match rule
228 bool InstructForm::is_projection(FormDict &globals) {
229 if ( ! _matrule) return false;
230
231 int index = 0;
232 if (_matrule->find_type("Goto", index)) return true;
233 if (_matrule->find_type("Return", index)) return true;
234 if (_matrule->find_type("Rethrow", index)) return true;
235 if (_matrule->find_type("TailCall", index)) return true;
236 if (_matrule->find_type("TailJump", index)) return true;
237 if (_matrule->find_type("ForwardException", index)) return true;
238 if (_matrule->find_type("Halt", index)) return true;
239
240 return false;
241 }
242
243 // Recursive check on all operands' match rules in my match rule
244 bool InstructForm::is_parm(FormDict &globals) {
245 if ( ! _matrule) return false;
246
247 int index = 0;
248 if (_matrule->find_type("Parm",index)) return true;
249
250 return false;
251 }
252
253 bool InstructForm::is_ideal_negD() const {
254 return (_matrule && _matrule->_rChild && strcmp(_matrule->_rChild->_opType, "NegD") == 0);
255 }
256
257 // Return 'true' if this instruction matches an ideal 'Copy*' node
258 int InstructForm::is_ideal_copy() const {
259 return _matrule ? _matrule->is_ideal_copy() : 0;
260 }
261
262 // Return 'true' if this instruction is too complex to rematerialize.
263 int InstructForm::is_expensive() const {
264 // We can prove it is cheap if it has an empty encoding.
265 // This helps with platform-specific nops like ThreadLocal and RoundFloat.
266 if (is_empty_encoding())
267 return 0;
268
269 if (is_tls_instruction())
270 return 1;
271
272 if (_matrule == nullptr) return 0;
273
274 return _matrule->is_expensive();
275 }
276
277 // Has an empty encoding if _size is a constant zero or there
278 // are no ins_encode tokens.
279 int InstructForm::is_empty_encoding() const {
280 if (_insencode != nullptr) {
281 _insencode->reset();
282 if (_insencode->encode_class_iter() == nullptr) {
283 return 1;
284 }
285 }
286 if (_size != nullptr && strcmp(_size, "0") == 0) {
287 return 1;
288 }
289 return 0;
290 }
291
292 int InstructForm::is_tls_instruction() const {
293 if (_ident != nullptr &&
294 ( ! strcmp( _ident,"tlsLoadP") ||
295 ! strncmp(_ident,"tlsLoadP_",9)) ) {
296 return 1;
297 }
298
299 if (_matrule != nullptr && _insencode != nullptr) {
300 const char* opType = _matrule->_opType;
301 if (strcmp(opType, "Set")==0)
302 opType = _matrule->_rChild->_opType;
303 if (strcmp(opType,"ThreadLocal")==0) {
304 fprintf(stderr, "Warning: ThreadLocal instruction %s should be named 'tlsLoadP_*'\n",
305 (_ident == nullptr ? "nullptr" : _ident));
306 return 1;
307 }
308 }
309
310 return 0;
311 }
312
313
314 // Return 'true' if this instruction matches an ideal 'If' node
315 bool InstructForm::is_ideal_if() const {
316 if( _matrule == nullptr ) return false;
317
318 return _matrule->is_ideal_if();
319 }
320
321 // Return 'true' if this instruction matches an ideal 'FastLock' node
322 bool InstructForm::is_ideal_fastlock() const {
323 if( _matrule == nullptr ) return false;
324
325 return _matrule->is_ideal_fastlock();
326 }
327
328 // Return 'true' if this instruction matches an ideal 'MemBarXXX' node
329 bool InstructForm::is_ideal_membar() const {
330 if( _matrule == nullptr ) return false;
331
332 return _matrule->is_ideal_membar();
333 }
334
335 // Return 'true' if this instruction matches an ideal 'LoadPC' node
336 bool InstructForm::is_ideal_loadPC() const {
337 if( _matrule == nullptr ) return false;
338
339 return _matrule->is_ideal_loadPC();
340 }
341
342 // Return 'true' if this instruction matches an ideal 'Box' node
343 bool InstructForm::is_ideal_box() const {
344 if( _matrule == nullptr ) return false;
345
346 return _matrule->is_ideal_box();
347 }
348
349 // Return 'true' if this instruction matches an ideal 'Goto' node
350 bool InstructForm::is_ideal_goto() const {
351 if( _matrule == nullptr ) return false;
352
353 return _matrule->is_ideal_goto();
354 }
355
356 // Return 'true' if this instruction matches an ideal 'Jump' node
357 bool InstructForm::is_ideal_jump() const {
358 if( _matrule == nullptr ) return false;
359
360 return _matrule->is_ideal_jump();
361 }
362
363 // Return 'true' if instruction matches ideal 'If' | 'Goto' | 'CountedLoopEnd'
364 bool InstructForm::is_ideal_branch() const {
365 if( _matrule == nullptr ) return false;
366
367 return _matrule->is_ideal_if() || _matrule->is_ideal_goto();
368 }
369
370
371 // Return 'true' if this instruction matches an ideal 'Return' node
372 bool InstructForm::is_ideal_return() const {
373 if( _matrule == nullptr ) return false;
374
375 // Check MatchRule to see if the first entry is the ideal "Return" node
376 int index = 0;
377 if (_matrule->find_type("Return",index)) return true;
378 if (_matrule->find_type("Rethrow",index)) return true;
379 if (_matrule->find_type("TailCall",index)) return true;
380 if (_matrule->find_type("TailJump",index)) return true;
381 if (_matrule->find_type("ForwardException", index)) return true;
382
383 return false;
384 }
385
386 // Return 'true' if this instruction matches an ideal 'Halt' node
387 bool InstructForm::is_ideal_halt() const {
388 int index = 0;
389 return _matrule && _matrule->find_type("Halt",index);
390 }
391
392 // Return 'true' if this instruction matches an ideal 'SafePoint' node
393 bool InstructForm::is_ideal_safepoint() const {
394 int index = 0;
395 return _matrule && _matrule->find_type("SafePoint",index);
396 }
397
398 // Return 'true' if this instruction matches an ideal 'Nop' node
399 bool InstructForm::is_ideal_nop() const {
400 return _ident && _ident[0] == 'N' && _ident[1] == 'o' && _ident[2] == 'p' && _ident[3] == '_';
401 }
402
403 bool InstructForm::is_ideal_control() const {
404 if ( ! _matrule) return false;
405
406 return is_ideal_return() || is_ideal_branch() || _matrule->is_ideal_jump() || is_ideal_halt();
407 }
408
409 // Return 'true' if this instruction matches an ideal 'Call' node
410 Form::CallType InstructForm::is_ideal_call() const {
411 if( _matrule == nullptr ) return Form::invalid_type;
412
413 // Check MatchRule to see if the first entry is the ideal "Call" node
414 int idx = 0;
415 if(_matrule->find_type("CallStaticJava",idx)) return Form::JAVA_STATIC;
416 idx = 0;
417 if(_matrule->find_type("Lock",idx)) return Form::JAVA_STATIC;
418 idx = 0;
419 if(_matrule->find_type("Unlock",idx)) return Form::JAVA_STATIC;
420 idx = 0;
421 if(_matrule->find_type("CallDynamicJava",idx)) return Form::JAVA_DYNAMIC;
422 idx = 0;
423 if(_matrule->find_type("CallRuntime",idx)) return Form::JAVA_RUNTIME;
424 idx = 0;
425 if(_matrule->find_type("CallLeaf",idx)) return Form::JAVA_LEAF;
426 idx = 0;
427 if(_matrule->find_type("CallLeafNoFP",idx)) return Form::JAVA_LEAF;
428 idx = 0;
429 if(_matrule->find_type("CallLeafVector",idx)) return Form::JAVA_LEAF;
430 idx = 0;
431
432 return Form::invalid_type;
433 }
434
435 // Return 'true' if this instruction matches an ideal 'Load?' node
436 Form::DataType InstructForm::is_ideal_load() const {
437 if( _matrule == nullptr ) return Form::none;
438
439 return _matrule->is_ideal_load();
440 }
441
442 // Return 'true' if this instruction matches an ideal 'LoadKlass' node
443 bool InstructForm::skip_antidep_check() const {
444 if( _matrule == nullptr ) return false;
445
446 return _matrule->skip_antidep_check();
447 }
448
449 // Return 'true' if this instruction matches an ideal 'Load?' node
450 Form::DataType InstructForm::is_ideal_store() const {
451 if( _matrule == nullptr ) return Form::none;
452
453 return _matrule->is_ideal_store();
454 }
455
456 // Return the input register that must match the output register
457 // If this is not required, return 0
458 uint InstructForm::two_address(FormDict &globals) {
459 uint matching_input = 0;
460 if(_components.count() == 0) return 0;
461
462 _components.reset();
463 Component *comp = _components.iter();
464 // Check if there is a DEF
465 if( comp->isa(Component::DEF) ) {
466 // Check that this is a register
467 const char *def_type = comp->_type;
468 const Form *form = globals[def_type];
469 OperandForm *op = form->is_operand();
470 if( op ) {
471 if( op->constrained_reg_class() != nullptr &&
472 op->interface_type(globals) == Form::register_interface ) {
473 // Remember the local name for equality test later
474 const char *def_name = comp->_name;
475 // Check if a component has the same name and is a USE
476 do {
477 if( comp->isa(Component::USE) && strcmp(comp->_name,def_name)==0 ) {
478 return operand_position_format(def_name);
479 }
480 } while( (comp = _components.iter()) != nullptr);
481 }
482 }
483 }
484
485 return 0;
486 }
487
488
489 // when chaining a constant to an instruction, returns 'true' and sets opType
490 Form::DataType InstructForm::is_chain_of_constant(FormDict &globals) {
491 const char *dummy = nullptr;
492 const char *dummy2 = nullptr;
493 return is_chain_of_constant(globals, dummy, dummy2);
494 }
495 Form::DataType InstructForm::is_chain_of_constant(FormDict &globals,
496 const char * &opTypeParam) {
497 const char *result = nullptr;
498
499 return is_chain_of_constant(globals, opTypeParam, result);
500 }
501
502 Form::DataType InstructForm::is_chain_of_constant(FormDict &globals,
503 const char * &opTypeParam, const char * &resultParam) {
504 Form::DataType data_type = Form::none;
505 if ( ! _matrule) return data_type;
506
507 // !!!!!
508 // The source of the chain rule is 'position = 1'
509 uint position = 1;
510 const char *result = nullptr;
511 const char *name = nullptr;
512 const char *opType = nullptr;
513 // Here base_operand is looking for an ideal type to be returned (opType).
514 if ( _matrule->is_chain_rule(globals)
515 && _matrule->base_operand(position, globals, result, name, opType) ) {
516 data_type = ideal_to_const_type(opType);
517
518 // if it isn't an ideal constant type, just return
519 if ( data_type == Form::none ) return data_type;
520
521 // Ideal constant types also adjust the opType parameter.
522 resultParam = result;
523 opTypeParam = opType;
524 return data_type;
525 }
526
527 return data_type;
528 }
529
530 // Check if a simple chain rule
531 bool InstructForm::is_simple_chain_rule(FormDict &globals) const {
532 if( _matrule && _matrule->sets_result()
533 && _matrule->_rChild->_lChild == nullptr
534 && globals[_matrule->_rChild->_opType]
535 && globals[_matrule->_rChild->_opType]->is_opclass() ) {
536 return true;
537 }
538 return false;
539 }
540
541 // check for structural rematerialization
542 bool InstructForm::rematerialize(FormDict &globals, RegisterForm *registers ) {
543 bool rematerialize = false;
544
545 Form::DataType data_type = is_chain_of_constant(globals);
546 if( data_type != Form::none )
547 rematerialize = true;
548
549 // Constants
550 if( _components.count() == 1 && _components[0]->is(Component::USE_DEF) )
551 rematerialize = true;
552
553 // Pseudo-constants (values easily available to the runtime)
554 if (is_empty_encoding() && is_tls_instruction())
555 rematerialize = true;
556
557 // 1-input, 1-output, such as copies or increments.
558 if( _components.count() == 2 &&
559 _components[0]->is(Component::DEF) &&
560 _components[1]->isa(Component::USE) )
561 rematerialize = true;
562
563 // Check for an ideal 'Load?' and eliminate rematerialize option
564 if ( is_ideal_load() != Form::none || // Ideal load? Do not rematerialize
565 is_ideal_copy() != Form::none || // Ideal copy? Do not rematerialize
566 is_expensive() != Form::none) { // Expensive? Do not rematerialize
567 rematerialize = false;
568 }
569
570 // Always rematerialize the flags. They are more expensive to save &
571 // restore than to recompute (and possibly spill the compare's inputs).
572 if( _components.count() >= 1 ) {
573 Component *c = _components[0];
574 const Form *form = globals[c->_type];
575 OperandForm *opform = form->is_operand();
576 if( opform ) {
577 // Avoid the special stack_slots register classes
578 const char *rc_name = opform->constrained_reg_class();
579 if( rc_name ) {
580 if( strcmp(rc_name,"stack_slots") ) {
581 // Check for ideal_type of RegFlags
582 const char *type = opform->ideal_type( globals, registers );
583 if( (type != nullptr) && !strcmp(type, "RegFlags") )
584 rematerialize = true;
585 } else
586 rematerialize = false; // Do not rematerialize things target stk
587 }
588 }
589 }
590
591 return rematerialize;
592 }
593
594 // loads from memory, so must check for anti-dependence
595 bool InstructForm::needs_anti_dependence_check(FormDict &globals) const {
596 if ( skip_antidep_check() ) return false;
597
598 // Machine independent loads must be checked for anti-dependences
599 if( is_ideal_load() != Form::none ) return true;
600
601 // !!!!! !!!!! !!!!!
602 // TEMPORARY
603 // if( is_simple_chain_rule(globals) ) return false;
604
605 // String.(compareTo/equals/indexOf/hashCode) and Arrays.(equals/hashCode)
606 // use many memorys edges, but writes none
607 if( _matrule && _matrule->_rChild &&
608 ( strcmp(_matrule->_rChild->_opType,"StrComp" )==0 ||
609 strcmp(_matrule->_rChild->_opType,"StrEquals" )==0 ||
610 strcmp(_matrule->_rChild->_opType,"StrIndexOf" )==0 ||
611 strcmp(_matrule->_rChild->_opType,"StrIndexOfChar" )==0 ||
612 strcmp(_matrule->_rChild->_opType,"CountPositives" )==0 ||
613 strcmp(_matrule->_rChild->_opType,"AryEq" )==0 ||
614 strcmp(_matrule->_rChild->_opType,"VectorizedHashCode")==0 ))
615 return true;
616
617 // Check if instruction has a USE of a memory operand class, but no defs
618 bool USE_of_memory = false;
619 bool DEF_of_memory = false;
620 Component *comp = nullptr;
621 ComponentList &components = (ComponentList &)_components;
622
623 components.reset();
624 while( (comp = components.iter()) != nullptr ) {
625 const Form *form = globals[comp->_type];
626 if( !form ) continue;
627 OpClassForm *op = form->is_opclass();
628 if( !op ) continue;
629 if( form->interface_type(globals) == Form::memory_interface ) {
630 if( comp->isa(Component::USE) ) USE_of_memory = true;
631 if( comp->isa(Component::DEF) ) {
632 OperandForm *oper = form->is_operand();
633 if( oper && oper->is_user_name_for_sReg() ) {
634 // Stack slots are unaliased memory handled by allocator
635 oper = oper; // debug stopping point !!!!!
636 } else {
637 DEF_of_memory = true;
638 }
639 }
640 }
641 }
642 return (USE_of_memory && !DEF_of_memory);
643 }
644
645
646 int InstructForm::memory_operand(FormDict &globals) const {
647 // Machine independent loads must be checked for anti-dependences
648 // Check if instruction has a USE of a memory operand class, or a def.
649 int USE_of_memory = 0;
650 int DEF_of_memory = 0;
651 const char* last_memory_DEF = nullptr; // to test DEF/USE pairing in asserts
652 const char* last_memory_USE = nullptr;
653 Component *unique = nullptr;
654 Component *comp = nullptr;
655 ComponentList &components = (ComponentList &)_components;
656
657 components.reset();
658 while( (comp = components.iter()) != nullptr ) {
659 const Form *form = globals[comp->_type];
660 if( !form ) continue;
661 OpClassForm *op = form->is_opclass();
662 if( !op ) continue;
663 if( op->stack_slots_only(globals) ) continue;
664 if( form->interface_type(globals) == Form::memory_interface ) {
665 if( comp->isa(Component::DEF) ) {
666 last_memory_DEF = comp->_name;
667 DEF_of_memory++;
668 unique = comp;
669 } else if( comp->isa(Component::USE) ) {
670 if( last_memory_DEF != nullptr ) {
671 assert(0 == strcmp(last_memory_DEF, comp->_name), "every memory DEF is followed by a USE of the same name");
672 last_memory_DEF = nullptr;
673 }
674 // Handles same memory being used multiple times in the case of BMI1 instructions.
675 if (last_memory_USE != nullptr) {
676 if (strcmp(comp->_name, last_memory_USE) != 0) {
677 USE_of_memory++;
678 }
679 } else {
680 USE_of_memory++;
681 }
682 last_memory_USE = comp->_name;
683
684 if (DEF_of_memory == 0) // defs take precedence
685 unique = comp;
686 } else {
687 assert(last_memory_DEF == nullptr, "unpaired memory DEF");
688 }
689 }
690 }
691 assert(last_memory_DEF == nullptr, "unpaired memory DEF");
692 assert(USE_of_memory >= DEF_of_memory, "unpaired memory DEF");
693 USE_of_memory -= DEF_of_memory; // treat paired DEF/USE as one occurrence
694 if( (USE_of_memory + DEF_of_memory) > 0 ) {
695 if( is_simple_chain_rule(globals) ) {
696 //fprintf(stderr, "Warning: chain rule is not really a memory user.\n");
697 //((InstructForm*)this)->dump();
698 // Preceding code prints nothing on sparc and these insns on intel:
699 // leaP8 leaP32 leaPIdxOff leaPIdxScale leaPIdxScaleOff leaP8 leaP32
700 // leaPIdxOff leaPIdxScale leaPIdxScaleOff
701 return NO_MEMORY_OPERAND;
702 }
703
704 if( DEF_of_memory == 1 ) {
705 assert(unique != nullptr, "");
706 if( USE_of_memory == 0 ) {
707 // unique def, no uses
708 } else {
709 // // unique def, some uses
710 // // must return bottom unless all uses match def
711 // unique = nullptr;
712 #ifdef S390
713 // This case is important for move instructions on s390x.
714 // On other platforms (e.g. x86), all uses always match the def.
715 unique = nullptr;
716 #endif
717 }
718 } else if( DEF_of_memory > 0 ) {
719 // multiple defs, don't care about uses
720 unique = nullptr;
721 } else if( USE_of_memory == 1) {
722 // unique use, no defs
723 assert(unique != nullptr, "");
724 } else if( USE_of_memory > 0 ) {
725 // multiple uses, no defs
726 unique = nullptr;
727 } else {
728 assert(false, "bad case analysis");
729 }
730 // process the unique DEF or USE, if there is one
731 if( unique == nullptr ) {
732 return MANY_MEMORY_OPERANDS;
733 } else {
734 int pos = components.operand_position(unique->_name);
735 if( unique->isa(Component::DEF) ) {
736 pos += 1; // get corresponding USE from DEF
737 }
738 assert(pos >= 1, "I was just looking at it!");
739 return pos;
740 }
741 }
742
743 // missed the memory op??
744 if( true ) { // %%% should not be necessary
745 if( is_ideal_store() != Form::none ) {
746 fprintf(stderr, "Warning: cannot find memory opnd in instr.\n");
747 ((InstructForm*)this)->dump();
748 // pretend it has multiple defs and uses
749 return MANY_MEMORY_OPERANDS;
750 }
751 if( is_ideal_load() != Form::none ) {
752 fprintf(stderr, "Warning: cannot find memory opnd in instr.\n");
753 ((InstructForm*)this)->dump();
754 // pretend it has multiple uses and no defs
755 return MANY_MEMORY_OPERANDS;
756 }
757 }
758
759 return NO_MEMORY_OPERAND;
760 }
761
762 // Access instr_cost attribute or return null.
763 const char* InstructForm::cost() {
764 for (Attribute* cur = _attribs; cur != nullptr; cur = (Attribute*)cur->_next) {
765 if( strcmp(cur->_ident,AttributeForm::_ins_cost) == 0 ) {
766 return cur->_val;
767 }
768 }
769 return nullptr;
770 }
771
772 // Return count of top-level operands.
773 uint InstructForm::num_opnds() {
774 int num_opnds = _components.num_operands();
775
776 // Need special handling for matching some ideal nodes
777 // i.e. Matching a return node
778 /*
779 if( _matrule ) {
780 if( strcmp(_matrule->_opType,"Return" )==0 ||
781 strcmp(_matrule->_opType,"Halt" )==0 )
782 return 3;
783 }
784 */
785 return num_opnds;
786 }
787
788 const char* InstructForm::opnd_ident(int idx) {
789 return _components.at(idx)->_name;
790 }
791
792 const char* InstructForm::unique_opnd_ident(uint idx) {
793 uint i;
794 for (i = 1; i < num_opnds(); ++i) {
795 if (unique_opnds_idx(i) == idx) {
796 break;
797 }
798 }
799 return (_components.at(i) != nullptr) ? _components.at(i)->_name : "";
800 }
801
802 // Return count of unmatched operands.
803 uint InstructForm::num_post_match_opnds() {
804 uint num_post_match_opnds = _components.count();
805 uint num_match_opnds = _components.match_count();
806 num_post_match_opnds = num_post_match_opnds - num_match_opnds;
807
808 return num_post_match_opnds;
809 }
810
811 // Return the number of leaves below this complex operand
812 uint InstructForm::num_consts(FormDict &globals) const {
813 if ( ! _matrule) return 0;
814
815 // This is a recursive invocation on all operands in the matchrule
816 return _matrule->num_consts(globals);
817 }
818
819 // Constants in match rule with specified type
820 uint InstructForm::num_consts(FormDict &globals, Form::DataType type) const {
821 if ( ! _matrule) return 0;
822
823 // This is a recursive invocation on all operands in the matchrule
824 return _matrule->num_consts(globals, type);
825 }
826
827
828 // Return the register class associated with 'leaf'.
829 const char *InstructForm::out_reg_class(FormDict &globals) {
830 assert( false, "InstructForm::out_reg_class(FormDict &globals); Not Implemented");
831
832 return nullptr;
833 }
834
835
836
837 // Lookup the starting position of inputs we are interested in wrt. ideal nodes
838 uint InstructForm::oper_input_base(FormDict &globals) {
839 if( !_matrule ) return 1; // Skip control for most nodes
840
841 // Need special handling for matching some ideal nodes
842 // i.e. Matching a return node
843 if( strcmp(_matrule->_opType,"Return" )==0 ||
844 strcmp(_matrule->_opType,"Rethrow" )==0 ||
845 strcmp(_matrule->_opType,"TailCall" )==0 ||
846 strcmp(_matrule->_opType,"TailJump" )==0 ||
847 strcmp(_matrule->_opType,"ForwardException")==0 ||
848 strcmp(_matrule->_opType,"SafePoint" )==0 ||
849 strcmp(_matrule->_opType,"Halt" )==0 ||
850 strcmp(_matrule->_opType,"CallLeafNoFP")==0)
851 return AdlcVMDeps::Parms; // Skip the machine-state edges
852
853 if( _matrule->_rChild &&
854 ( strcmp(_matrule->_rChild->_opType,"AryEq" )==0 ||
855 strcmp(_matrule->_rChild->_opType,"VectorizedHashCode")==0 ||
856 strcmp(_matrule->_rChild->_opType,"StrComp" )==0 ||
857 strcmp(_matrule->_rChild->_opType,"StrEquals" )==0 ||
858 strcmp(_matrule->_rChild->_opType,"StrInflatedCopy" )==0 ||
859 strcmp(_matrule->_rChild->_opType,"StrCompressedCopy" )==0 ||
860 strcmp(_matrule->_rChild->_opType,"StrIndexOf")==0 ||
861 strcmp(_matrule->_rChild->_opType,"StrIndexOfChar")==0 ||
862 strcmp(_matrule->_rChild->_opType,"CountPositives")==0 ||
863 strcmp(_matrule->_rChild->_opType,"EncodeISOArray")==0)) {
864 // String.(compareTo/equals/indexOf/hashCode) and Arrays.equals
865 // and sun.nio.cs.iso8859_1$Encoder.EncodeISOArray
866 // take 1 control and 1 memory edges.
867 // Also String.(compressedCopy/inflatedCopy).
868 return 2;
869 }
870
871 // Check for handling of 'Memory' input/edge in the ideal world.
872 // The AD file writer is shielded from knowledge of these edges.
873 int base = 1; // Skip control
874 base += _matrule->needs_ideal_memory_edge(globals);
875
876 // Also skip the base-oop value for uses of derived oops.
877 // The AD file writer is shielded from knowledge of these edges.
878 base += needs_base_oop_edge(globals);
879
880 return base;
881 }
882
883 // This function determines the order of the MachOper in _opnds[]
884 // by writing the operand names into the _components list.
885 //
886 // Implementation does not modify state of internal structures
887 void InstructForm::build_components() {
888 // Add top-level operands to the components
889 if (_matrule) _matrule->append_components(_localNames, _components);
890
891 // Add parameters that "do not appear in match rule".
892 bool has_temp = false;
893 const char *name;
894 const char *kill_name = nullptr;
895 for (_parameters.reset(); (name = _parameters.iter()) != nullptr;) {
896 OpClassForm *opForm = _localNames[name]->is_opclass();
897 assert(opForm != nullptr, "sanity");
898
899 Effect* e = nullptr;
900 {
901 const Form* form = _effects[name];
902 e = form ? form->is_effect() : nullptr;
903 }
904
905 if (e != nullptr) {
906 has_temp |= e->is(Component::TEMP);
907
908 // KILLs must be declared after any TEMPs because TEMPs are real
909 // uses so their operand numbering must directly follow the real
910 // inputs from the match rule. Fixing the numbering seems
911 // complex so simply enforce the restriction during parse.
912 if (kill_name != nullptr &&
913 e->isa(Component::TEMP) && !e->isa(Component::DEF)) {
914 OpClassForm* kill = _localNames[kill_name]->is_opclass();
915 assert(kill != nullptr, "sanity");
916 globalAD->syntax_err(_linenum, "%s: %s %s must be at the end of the argument list\n",
917 _ident, kill->_ident, kill_name);
918 } else if (e->isa(Component::KILL) && !e->isa(Component::USE)) {
919 kill_name = name;
920 }
921 }
922
923 const Component *component = _components.search(name);
924 if ( component == nullptr ) {
925 if (e) {
926 _components.insert(name, opForm->_ident, e->_use_def, false);
927 component = _components.search(name);
928 if (component->isa(Component::USE) && !component->isa(Component::TEMP) && _matrule) {
929 const Form *form = globalAD->globalNames()[component->_type];
930 assert( form, "component type must be a defined form");
931 OperandForm *op = form->is_operand();
932 if (op->_interface && op->_interface->is_RegInterface()) {
933 globalAD->syntax_err(_linenum, "%s: illegal USE of non-input: %s %s\n",
934 _ident, opForm->_ident, name);
935 }
936 }
937 } else {
938 // This would be a nice warning but it triggers in a few places in a benign way
939 // if (_matrule != nullptr && !expands()) {
940 // globalAD->syntax_err(_linenum, "%s: %s %s not mentioned in effect or match rule\n",
941 // _ident, opForm->_ident, name);
942 // }
943 _components.insert(name, opForm->_ident, Component::INVALID, false);
944 }
945 }
946 else if (e) {
947 // Component was found in the list
948 // Check if there is a new effect that requires an extra component.
949 // This happens when adding 'USE' to a component that is not yet one.
950 if ((!component->isa( Component::USE) && ((e->_use_def & Component::USE) != 0))) {
951 if (component->isa(Component::USE) && _matrule) {
952 const Form *form = globalAD->globalNames()[component->_type];
953 assert( form, "component type must be a defined form");
954 OperandForm *op = form->is_operand();
955 if (op->_interface && op->_interface->is_RegInterface()) {
956 globalAD->syntax_err(_linenum, "%s: illegal USE of non-input: %s %s\n",
957 _ident, opForm->_ident, name);
958 }
959 }
960 _components.insert(name, opForm->_ident, e->_use_def, false);
961 } else {
962 Component *comp = (Component*)component;
963 comp->promote_use_def_info(e->_use_def);
964 }
965 // Component positions are zero based.
966 int pos = _components.operand_position(name);
967 assert( ! (component->isa(Component::DEF) && (pos >= 1)),
968 "Component::DEF can only occur in the first position");
969 }
970 }
971
972 // Resolving the interactions between expand rules and TEMPs would
973 // be complex so simply disallow it.
974 if (_matrule == nullptr && has_temp) {
975 globalAD->syntax_err(_linenum, "%s: TEMPs without match rule isn't supported\n", _ident);
976 }
977
978 return;
979 }
980
981 // Return zero-based position in component list; -1 if not in list.
982 int InstructForm::operand_position(const char *name, int usedef) {
983 return unique_opnds_idx(_components.operand_position(name, usedef, this));
984 }
985
986 int InstructForm::operand_position_format(const char *name) {
987 return unique_opnds_idx(_components.operand_position_format(name, this));
988 }
989
990 // Return zero-based position in component list; -1 if not in list.
991 int InstructForm::label_position() {
992 return unique_opnds_idx(_components.label_position());
993 }
994
995 int InstructForm::method_position() {
996 return unique_opnds_idx(_components.method_position());
997 }
998
999 // Return number of relocation entries needed for this instruction.
1000 uint InstructForm::reloc(FormDict &globals) {
1001 uint reloc_entries = 0;
1002 // Check for "Call" nodes
1003 if ( is_ideal_call() ) ++reloc_entries;
1004 if ( is_ideal_return() ) ++reloc_entries;
1005 if ( is_ideal_safepoint() ) ++reloc_entries;
1006
1007
1008 // Check if operands MAYBE oop pointers, by checking for ConP elements
1009 // Proceed through the leaves of the match-tree and check for ConPs
1010 if ( _matrule != nullptr ) {
1011 uint position = 0;
1012 const char *result = nullptr;
1013 const char *name = nullptr;
1014 const char *opType = nullptr;
1015 while (_matrule->base_operand(position, globals, result, name, opType)) {
1016 if ( strcmp(opType,"ConP") == 0 ) {
1017 ++reloc_entries;
1018 }
1019 ++position;
1020 }
1021 }
1022
1023 // Above is only a conservative estimate
1024 // because it did not check contents of operand classes.
1025 // !!!!! !!!!!
1026 // Add 1 to reloc info for each operand class in the component list.
1027 Component *comp;
1028 _components.reset();
1029 while ( (comp = _components.iter()) != nullptr ) {
1030 const Form *form = globals[comp->_type];
1031 assert( form, "Did not find component's type in global names");
1032 const OpClassForm *opc = form->is_opclass();
1033 const OperandForm *oper = form->is_operand();
1034 if ( opc && (oper == nullptr) ) {
1035 ++reloc_entries;
1036 } else if ( oper ) {
1037 // floats and doubles loaded out of method's constant pool require reloc info
1038 Form::DataType type = oper->is_base_constant(globals);
1039 if ( (type == Form::idealH) || (type == Form::idealF) || (type == Form::idealD) ) {
1040 ++reloc_entries;
1041 }
1042 }
1043 }
1044
1045 // Float and Double constants may come from the CodeBuffer table
1046 // and require relocatable addresses for access
1047 // !!!!!
1048 // Check for any component being an immediate float or double.
1049 Form::DataType data_type = is_chain_of_constant(globals);
1050 if( data_type==idealH || data_type==idealD || data_type==idealF ) {
1051 reloc_entries++;
1052 }
1053
1054 return reloc_entries;
1055 }
1056
1057 // Utility function defined in archDesc.cpp
1058 extern bool is_def(int usedef);
1059
1060 // Return the result of reducing an instruction
1061 const char *InstructForm::reduce_result() {
1062 const char* result = "Universe"; // default
1063 _components.reset();
1064 Component *comp = _components.iter();
1065 if (comp != nullptr && comp->isa(Component::DEF)) {
1066 result = comp->_type;
1067 // Override this if the rule is a store operation:
1068 if (_matrule && _matrule->_rChild &&
1069 is_store_to_memory(_matrule->_rChild->_opType))
1070 result = "Universe";
1071 }
1072 return result;
1073 }
1074
1075 // Return the name of the operand on the right hand side of the binary match
1076 // Return null if there is no right hand side
1077 const char *InstructForm::reduce_right(FormDict &globals) const {
1078 if( _matrule == nullptr ) return nullptr;
1079 return _matrule->reduce_right(globals);
1080 }
1081
1082 // Similar for left
1083 const char *InstructForm::reduce_left(FormDict &globals) const {
1084 if( _matrule == nullptr ) return nullptr;
1085 return _matrule->reduce_left(globals);
1086 }
1087
1088
1089 // Base class for this instruction, MachNode except for calls
1090 const char *InstructForm::mach_base_class(FormDict &globals) const {
1091 if( is_ideal_call() == Form::JAVA_STATIC ) {
1092 return "MachCallStaticJavaNode";
1093 }
1094 else if( is_ideal_call() == Form::JAVA_DYNAMIC ) {
1095 return "MachCallDynamicJavaNode";
1096 }
1097 else if( is_ideal_call() == Form::JAVA_RUNTIME ) {
1098 return "MachCallRuntimeNode";
1099 }
1100 else if( is_ideal_call() == Form::JAVA_LEAF ) {
1101 return "MachCallLeafNode";
1102 }
1103 else if (is_ideal_return()) {
1104 return "MachReturnNode";
1105 }
1106 else if (is_ideal_halt()) {
1107 return "MachHaltNode";
1108 }
1109 else if (is_ideal_safepoint()) {
1110 return "MachSafePointNode";
1111 }
1112 else if (is_ideal_if()) {
1113 return "MachIfNode";
1114 }
1115 else if (is_ideal_goto()) {
1116 return "MachGotoNode";
1117 }
1118 else if (is_ideal_fastlock()) {
1119 return "MachFastLockNode";
1120 }
1121 else if (is_ideal_nop()) {
1122 return "MachNopNode";
1123 }
1124 else if( is_ideal_membar()) {
1125 return "MachMemBarNode";
1126 }
1127 else if (is_ideal_jump()) {
1128 return "MachJumpNode";
1129 }
1130 else if (is_mach_constant()) {
1131 return "MachConstantNode";
1132 } else {
1133 return "MachNode";
1134 }
1135 assert( false, "ShouldNotReachHere()");
1136 return nullptr;
1137 }
1138
1139 // Compare the instruction predicates for textual equality
1140 bool equivalent_predicates( const InstructForm *instr1, const InstructForm *instr2 ) {
1141 const Predicate *pred1 = instr1->_predicate;
1142 const Predicate *pred2 = instr2->_predicate;
1143 if( pred1 == nullptr && pred2 == nullptr ) {
1144 // no predicates means they are identical
1145 return true;
1146 }
1147 if( pred1 != nullptr && pred2 != nullptr ) {
1148 // compare the predicates
1149 if (ADLParser::equivalent_expressions(pred1->_pred, pred2->_pred)) {
1150 return true;
1151 }
1152 }
1153
1154 return false;
1155 }
1156
1157 // Check if this instruction can cisc-spill to 'alternate'
1158 bool InstructForm::cisc_spills_to(ArchDesc &AD, InstructForm *instr) {
1159 assert( _matrule != nullptr && instr->_matrule != nullptr, "must have match rules");
1160 // Do not replace if a cisc-version has been found.
1161 if( cisc_spill_operand() != Not_cisc_spillable ) return false;
1162
1163 int cisc_spill_operand = Maybe_cisc_spillable;
1164 char *result = nullptr;
1165 char *result2 = nullptr;
1166 const char *op_name = nullptr;
1167 const char *reg_type = nullptr;
1168 FormDict &globals = AD.globalNames();
1169 cisc_spill_operand = _matrule->matchrule_cisc_spill_match(globals, AD.get_registers(), instr->_matrule, op_name, reg_type);
1170 if( (cisc_spill_operand != Not_cisc_spillable) && (op_name != nullptr) && equivalent_predicates(this, instr) ) {
1171 cisc_spill_operand = operand_position(op_name, Component::USE);
1172 int def_oper = operand_position(op_name, Component::DEF);
1173 if( def_oper == NameList::Not_in_list && instr->num_opnds() == num_opnds()) {
1174 // Do not support cisc-spilling for destination operands and
1175 // make sure they have the same number of operands.
1176 _cisc_spill_alternate = instr;
1177 instr->set_cisc_alternate(true);
1178 if( AD._cisc_spill_debug ) {
1179 fprintf(stderr, "Instruction %s cisc-spills-to %s\n", _ident, instr->_ident);
1180 fprintf(stderr, " using operand %s %s at index %d\n", reg_type, op_name, cisc_spill_operand);
1181 }
1182 // Record that a stack-version of the reg_mask is needed
1183 // !!!!!
1184 OperandForm *oper = (OperandForm*)(globals[reg_type]->is_operand());
1185 assert( oper != nullptr, "cisc-spilling non operand");
1186 const char *reg_class_name = oper->constrained_reg_class();
1187 AD.set_stack_or_reg(reg_class_name);
1188 const char *reg_mask_name = AD.reg_mask(*oper);
1189 set_cisc_reg_mask_name(reg_mask_name);
1190 const char *stack_or_reg_mask_name = AD.stack_or_reg_mask(*oper);
1191 } else {
1192 cisc_spill_operand = Not_cisc_spillable;
1193 }
1194 } else {
1195 cisc_spill_operand = Not_cisc_spillable;
1196 }
1197
1198 set_cisc_spill_operand(cisc_spill_operand);
1199 return (cisc_spill_operand != Not_cisc_spillable);
1200 }
1201
1202 // Check to see if this instruction can be replaced with the short branch
1203 // instruction `short-branch'
1204 bool InstructForm::check_branch_variant(ArchDesc &AD, InstructForm *short_branch) {
1205 if (_matrule != nullptr &&
1206 this != short_branch && // Don't match myself
1207 !is_short_branch() && // Don't match another short branch variant
1208 reduce_result() != nullptr &&
1209 strstr(_ident, "restoreMask") == nullptr && // Don't match side effects
1210 strcmp(reduce_result(), short_branch->reduce_result()) == 0 &&
1211 _matrule->equivalent(AD.globalNames(), short_branch->_matrule)) {
1212 // The instructions are equivalent.
1213
1214 // Now verify that both instructions have the same parameters and
1215 // the same effects. Both branch forms should have the same inputs
1216 // and resulting projections to correctly replace a long branch node
1217 // with corresponding short branch node during code generation.
1218
1219 bool different = false;
1220 if (short_branch->_components.count() != _components.count()) {
1221 different = true;
1222 } else if (_components.count() > 0) {
1223 short_branch->_components.reset();
1224 _components.reset();
1225 Component *comp;
1226 while ((comp = _components.iter()) != nullptr) {
1227 Component *short_comp = short_branch->_components.iter();
1228 if (short_comp == nullptr ||
1229 short_comp->_type != comp->_type ||
1230 short_comp->_usedef != comp->_usedef) {
1231 different = true;
1232 break;
1233 }
1234 }
1235 if (short_branch->_components.iter() != nullptr)
1236 different = true;
1237 }
1238 if (different) {
1239 globalAD->syntax_err(short_branch->_linenum, "Instruction %s and its short form %s have different parameters\n", _ident, short_branch->_ident);
1240 }
1241 if (AD._adl_debug > 1 || AD._short_branch_debug) {
1242 fprintf(stderr, "Instruction %s has short form %s\n", _ident, short_branch->_ident);
1243 }
1244 _short_branch_form = short_branch;
1245 return true;
1246 }
1247 return false;
1248 }
1249
1250
1251 // --------------------------- FILE *output_routines
1252 //
1253 // Generate the format call for the replacement variable
1254 void InstructForm::rep_var_format(FILE *fp, const char *rep_var) {
1255 // Handle special constant table variables.
1256 if (strcmp(rep_var, "constanttablebase") == 0) {
1257 fprintf(fp, "char reg[128]; ra->dump_register(in(mach_constant_base_node_input()), reg, sizeof(reg));\n");
1258 fprintf(fp, " st->print(\"%%s\", reg);\n");
1259 return;
1260 }
1261 if (strcmp(rep_var, "constantoffset") == 0) {
1262 fprintf(fp, "st->print(\"#%%d\", constant_offset_unchecked());\n");
1263 return;
1264 }
1265 if (strcmp(rep_var, "constantaddress") == 0) {
1266 fprintf(fp, "st->print(\"constant table base + #%%d\", constant_offset_unchecked());\n");
1267 return;
1268 }
1269
1270 // Find replacement variable's type
1271 const Form *form = _localNames[rep_var];
1272 if (form == nullptr) {
1273 globalAD->syntax_err(_linenum, "Unknown replacement variable %s in format statement of %s.",
1274 rep_var, _ident);
1275 return;
1276 }
1277 OpClassForm *opc = form->is_opclass();
1278 assert( opc, "replacement variable was not found in local names");
1279 // Lookup the index position of the replacement variable
1280 int idx = operand_position_format(rep_var);
1281 if ( idx == -1 ) {
1282 globalAD->syntax_err(_linenum, "Could not find replacement variable %s in format statement of %s.\n",
1283 rep_var, _ident);
1284 assert(strcmp(opc->_ident, "label") == 0, "Unimplemented");
1285 return;
1286 }
1287
1288 if (is_noninput_operand(idx)) {
1289 // This component isn't in the input array. Print out the static
1290 // name of the register.
1291 OperandForm* oper = form->is_operand();
1292 if (oper != nullptr && oper->is_bound_register()) {
1293 const RegDef* first = oper->get_RegClass()->find_first_elem();
1294 fprintf(fp, " st->print_raw(\"%s\");\n", first->_regname);
1295 } else {
1296 globalAD->syntax_err(_linenum, "In %s can't find format for %s %s", _ident, opc->_ident, rep_var);
1297 }
1298 } else {
1299 // Output the format call for this operand
1300 fprintf(fp,"opnd_array(%d)->",idx);
1301 if (idx == 0)
1302 fprintf(fp,"int_format(ra, this, st); // %s\n", rep_var);
1303 else
1304 fprintf(fp,"ext_format(ra, this,idx%d, st); // %s\n", idx, rep_var );
1305 }
1306 }
1307
1308 // Search through operands to determine parameters unique positions.
1309 void InstructForm::set_unique_opnds() {
1310 uint* uniq_idx = nullptr;
1311 uint nopnds = num_opnds();
1312 uint num_uniq = nopnds;
1313 uint i;
1314 _uniq_idx_length = 0;
1315 if (nopnds > 0) {
1316 // Allocate index array. Worst case we're mapping from each
1317 // component back to an index and any DEF always goes at 0 so the
1318 // length of the array has to be the number of components + 1.
1319 _uniq_idx_length = _components.count() + 1;
1320 uniq_idx = (uint*) AdlAllocateHeap(sizeof(uint) * _uniq_idx_length);
1321 for (i = 0; i < _uniq_idx_length; i++) {
1322 uniq_idx[i] = i;
1323 }
1324 }
1325 // Do it only if there is a match rule and no expand rule. With an
1326 // expand rule it is done by creating new mach node in Expand()
1327 // method.
1328 if (nopnds > 0 && _matrule != nullptr && _exprule == nullptr) {
1329 const char *name;
1330 uint count;
1331 bool has_dupl_use = false;
1332
1333 _parameters.reset();
1334 while ((name = _parameters.iter()) != nullptr) {
1335 count = 0;
1336 uint position = 0;
1337 uint uniq_position = 0;
1338 _components.reset();
1339 Component *comp = nullptr;
1340 if (sets_result()) {
1341 comp = _components.iter();
1342 position++;
1343 }
1344 // The next code is copied from the method operand_position().
1345 for (; (comp = _components.iter()) != nullptr; ++position) {
1346 // When the first component is not a DEF,
1347 // leave space for the result operand!
1348 if (position==0 && (!comp->isa(Component::DEF))) {
1349 ++position;
1350 }
1351 if (strcmp(name, comp->_name) == 0) {
1352 if (++count > 1) {
1353 assert(position < _uniq_idx_length, "out of bounds");
1354 uniq_idx[position] = uniq_position;
1355 has_dupl_use = true;
1356 } else {
1357 uniq_position = position;
1358 }
1359 }
1360 if (comp->isa(Component::DEF) && comp->isa(Component::USE)) {
1361 ++position;
1362 if (position != 1)
1363 --position; // only use two slots for the 1st USE_DEF
1364 }
1365 }
1366 }
1367 if (has_dupl_use) {
1368 for (i = 1; i < nopnds; i++) {
1369 if (i != uniq_idx[i]) {
1370 break;
1371 }
1372 }
1373 uint j = i;
1374 for (; i < nopnds; i++) {
1375 if (i == uniq_idx[i]) {
1376 uniq_idx[i] = j++;
1377 }
1378 }
1379 num_uniq = j;
1380 }
1381 }
1382 _uniq_idx = uniq_idx;
1383 _num_uniq = num_uniq;
1384 }
1385
1386 // Generate index values needed for determining the operand position
1387 void InstructForm::index_temps(FILE *fp, FormDict &globals, const char *prefix, const char *receiver) {
1388 uint idx = 0; // position of operand in match rule
1389 int cur_num_opnds = num_opnds();
1390
1391 // Compute the index into vector of operand pointers:
1392 // idx0=0 is used to indicate that info comes from this same node, not from input edge.
1393 // idx1 starts at oper_input_base()
1394 if ( cur_num_opnds >= 1 ) {
1395 fprintf(fp," // Start at oper_input_base() and count operands\n");
1396 fprintf(fp," unsigned %sidx0 = %d;\n", prefix, oper_input_base(globals));
1397 fprintf(fp," unsigned %sidx1 = %d;", prefix, oper_input_base(globals));
1398 fprintf(fp," \t// %s\n", unique_opnd_ident(1));
1399
1400 // Generate starting points for other unique operands if they exist
1401 for ( idx = 2; idx < num_unique_opnds(); ++idx ) {
1402 if( *receiver == 0 ) {
1403 fprintf(fp," unsigned %sidx%d = %sidx%d + opnd_array(%d)->num_edges();",
1404 prefix, idx, prefix, idx-1, idx-1 );
1405 } else {
1406 fprintf(fp," unsigned %sidx%d = %sidx%d + %s_opnds[%d]->num_edges();",
1407 prefix, idx, prefix, idx-1, receiver, idx-1 );
1408 }
1409 fprintf(fp," \t// %s\n", unique_opnd_ident(idx));
1410 }
1411 }
1412 if( *receiver != 0 ) {
1413 // This value is used by generate_peepreplace when copying a node.
1414 // Don't emit it in other cases since it can hide bugs with the
1415 // use invalid idx's.
1416 fprintf(fp," unsigned %sidx%d = %sreq(); \n", prefix, idx, receiver);
1417 }
1418
1419 }
1420
1421 // ---------------------------
1422 bool InstructForm::verify() {
1423 // !!!!! !!!!!
1424 // Check that a "label" operand occurs last in the operand list, if present
1425 return true;
1426 }
1427
1428 void InstructForm::dump() {
1429 output(stderr);
1430 }
1431
1432 void InstructForm::output(FILE *fp) {
1433 fprintf(fp,"\nInstruction: %s\n", (_ident?_ident:""));
1434 if (_matrule) _matrule->output(fp);
1435 if (_insencode) _insencode->output(fp);
1436 if (_constant) _constant->output(fp);
1437 if (_opcode) _opcode->output(fp);
1438 if (_attribs) _attribs->output(fp);
1439 if (_predicate) _predicate->output(fp);
1440 if (_effects.Size()) {
1441 fprintf(fp,"Effects\n");
1442 _effects.dump();
1443 }
1444 if (_exprule) _exprule->output(fp);
1445 if (_rewrule) _rewrule->output(fp);
1446 if (_format) _format->output(fp);
1447 if (_peephole) _peephole->output(fp);
1448 }
1449
1450 void InstructForm::forms_do(FormClosure *f) {
1451 if (_cisc_spill_alternate) f->do_form(_cisc_spill_alternate);
1452 if (_short_branch_form) f->do_form(_short_branch_form);
1453 _localNames.forms_do(f);
1454 if (_matrule) f->do_form(_matrule);
1455 if (_opcode) f->do_form(_opcode);
1456 if (_insencode) f->do_form(_insencode);
1457 if (_constant) f->do_form(_constant);
1458 if (_attribs) f->do_form(_attribs);
1459 if (_predicate) f->do_form(_predicate);
1460 _effects.forms_do(f);
1461 if (_exprule) f->do_form(_exprule);
1462 if (_rewrule) f->do_form(_rewrule);
1463 if (_format) f->do_form(_format);
1464 if (_peephole) f->do_form(_peephole);
1465 assert(_components.count() == 0, "skip components");
1466 }
1467
1468 void MachNodeForm::dump() {
1469 output(stderr);
1470 }
1471
1472 void MachNodeForm::output(FILE *fp) {
1473 fprintf(fp,"\nMachNode: %s\n", (_ident?_ident:""));
1474 }
1475
1476 //------------------------------build_predicate--------------------------------
1477 // Build instruction predicates. If the user uses the same operand name
1478 // twice, we need to check that the operands are pointer-eequivalent in
1479 // the DFA during the labeling process.
1480 Predicate *InstructForm::build_predicate() {
1481 const int buflen = 1024;
1482 char buf[buflen], *s=buf;
1483 Dict names(cmpstr,hashstr,Form::arena); // Map Names to counts
1484
1485 MatchNode *mnode =
1486 strcmp(_matrule->_opType, "Set") ? _matrule : _matrule->_rChild;
1487 if (mnode != nullptr) mnode->count_instr_names(names);
1488
1489 uint first = 1;
1490 // Start with the predicate supplied in the .ad file.
1491 if (_predicate) {
1492 if (first) first = 0;
1493 strcpy(s, "("); s += strlen(s);
1494 strncpy(s, _predicate->_pred, buflen - strlen(s) - 1);
1495 s += strlen(s);
1496 strcpy(s, ")"); s += strlen(s);
1497 }
1498 for( DictI i(&names); i.test(); ++i ) {
1499 uintptr_t cnt = (uintptr_t)i._value;
1500 if( cnt > 1 ) { // Need a predicate at all?
1501 int path_bitmask = 0;
1502 assert( cnt == 2, "Unimplemented" );
1503 // Handle many pairs
1504 if( first ) first=0;
1505 else { // All tests must pass, so use '&&'
1506 strcpy(s," && ");
1507 s += strlen(s);
1508 }
1509 // Add predicate to working buffer
1510 snprintf_checked(s, remaining_buflen(buf, s), "/*%s*/(",(char*)i._key);
1511 s += strlen(s);
1512 mnode->build_instr_pred(s,(char*)i._key, 0, path_bitmask, 0);
1513 s += strlen(s);
1514 strcpy(s," == "); s += strlen(s);
1515 mnode->build_instr_pred(s,(char*)i._key, 1, path_bitmask, 0);
1516 s += strlen(s);
1517 strcpy(s,")"); s += strlen(s);
1518 }
1519 }
1520 if( s == buf ) s = nullptr;
1521 else {
1522 assert( strlen(buf) < sizeof(buf), "String buffer overflow" );
1523 s = strdup(buf);
1524 }
1525 return new Predicate(s);
1526 }
1527
1528 //------------------------------EncodeForm-------------------------------------
1529 // Constructor
1530 EncodeForm::EncodeForm()
1531 : _encClass(cmpstr,hashstr, Form::arena) {
1532 }
1533 EncodeForm::~EncodeForm() {
1534 }
1535
1536 // record a new register class
1537 EncClass *EncodeForm::add_EncClass(const char *className) {
1538 EncClass *encClass = new EncClass(className);
1539 _eclasses.addName(className);
1540 _encClass.Insert(className,encClass);
1541 return encClass;
1542 }
1543
1544 // Lookup the function body for an encoding class
1545 EncClass *EncodeForm::encClass(const char *className) {
1546 assert( className != nullptr, "Must provide a defined encoding name");
1547
1548 EncClass *encClass = (EncClass*)_encClass[className];
1549 return encClass;
1550 }
1551
1552 // Lookup the function body for an encoding class
1553 const char *EncodeForm::encClassBody(const char *className) {
1554 if( className == nullptr ) return nullptr;
1555
1556 EncClass *encClass = (EncClass*)_encClass[className];
1557 assert( encClass != nullptr, "Encode Class is missing.");
1558 encClass->_code.reset();
1559 const char *code = (const char*)encClass->_code.iter();
1560 assert( code != nullptr, "Found an empty encode class body.");
1561
1562 return code;
1563 }
1564
1565 // Lookup the function body for an encoding class
1566 const char *EncodeForm::encClassPrototype(const char *className) {
1567 assert( className != nullptr, "Encode class name must be non null.");
1568
1569 return className;
1570 }
1571
1572 void EncodeForm::dump() { // Debug printer
1573 output(stderr);
1574 }
1575
1576 void EncodeForm::output(FILE *fp) { // Write info to output files
1577 const char *name;
1578 fprintf(fp,"\n");
1579 fprintf(fp,"-------------------- Dump EncodeForm --------------------\n");
1580 for (_eclasses.reset(); (name = _eclasses.iter()) != nullptr;) {
1581 ((EncClass*)_encClass[name])->output(fp);
1582 }
1583 fprintf(fp,"-------------------- end EncodeForm --------------------\n");
1584 }
1585
1586 void EncodeForm::forms_do(FormClosure* f) {
1587 const char *name;
1588 for (_eclasses.reset(); (name = _eclasses.iter()) != nullptr;) {
1589 f->do_form((EncClass*)_encClass[name]);
1590 }
1591 }
1592
1593 //------------------------------EncClass---------------------------------------
1594 EncClass::EncClass(const char *name)
1595 : _localNames(cmpstr,hashstr, Form::arena), _name(name) {
1596 }
1597 EncClass::~EncClass() {
1598 }
1599
1600 // Add a parameter <type,name> pair
1601 void EncClass::add_parameter(const char *parameter_type, const char *parameter_name) {
1602 _parameter_type.addName( parameter_type );
1603 _parameter_name.addName( parameter_name );
1604 }
1605
1606 // Verify operand types in parameter list
1607 bool EncClass::check_parameter_types(FormDict &globals) {
1608 // !!!!!
1609 return false;
1610 }
1611
1612 // Add the decomposed "code" sections of an encoding's code-block
1613 void EncClass::add_code(const char *code) {
1614 _code.addName(code);
1615 }
1616
1617 // Add the decomposed "replacement variables" of an encoding's code-block
1618 void EncClass::add_rep_var(char *replacement_var) {
1619 _code.addName(NameList::_signal);
1620 _rep_vars.addName(replacement_var);
1621 }
1622
1623 // Lookup the function body for an encoding class
1624 int EncClass::rep_var_index(const char *rep_var) {
1625 uint position = 0;
1626 const char *name = nullptr;
1627
1628 _parameter_name.reset();
1629 while ( (name = _parameter_name.iter()) != nullptr ) {
1630 if ( strcmp(rep_var,name) == 0 ) return position;
1631 ++position;
1632 }
1633
1634 return -1;
1635 }
1636
1637 // Check after parsing
1638 bool EncClass::verify() {
1639 // 1!!!!
1640 // Check that each replacement variable, '$name' in architecture description
1641 // is actually a local variable for this encode class, or a reserved name
1642 // "primary, secondary, tertiary"
1643 return true;
1644 }
1645
1646 void EncClass::dump() {
1647 output(stderr);
1648 }
1649
1650 // Write info to output files
1651 void EncClass::output(FILE *fp) {
1652 fprintf(fp,"EncClass: %s", (_name ? _name : ""));
1653
1654 // Output the parameter list
1655 _parameter_type.reset();
1656 _parameter_name.reset();
1657 const char *type = _parameter_type.iter();
1658 const char *name = _parameter_name.iter();
1659 fprintf(fp, " ( ");
1660 for ( ; (type != nullptr) && (name != nullptr);
1661 (type = _parameter_type.iter()), (name = _parameter_name.iter()) ) {
1662 fprintf(fp, " %s %s,", type, name);
1663 }
1664 fprintf(fp, " ) ");
1665
1666 // Output the code block
1667 _code.reset();
1668 _rep_vars.reset();
1669 const char *code;
1670 while ( (code = _code.iter()) != nullptr ) {
1671 if ( _code.is_signal(code) ) {
1672 // A replacement variable
1673 const char *rep_var = _rep_vars.iter();
1674 fprintf(fp,"($%s)", rep_var);
1675 } else {
1676 // A section of code
1677 fprintf(fp,"%s", code);
1678 }
1679 }
1680
1681 }
1682
1683 void EncClass::forms_do(FormClosure *f) {
1684 _parameter_type.reset();
1685 const char *type = _parameter_type.iter();
1686 for ( ; type != nullptr ; type = _parameter_type.iter() ) {
1687 f->do_form_by_name(type);
1688 }
1689 _localNames.forms_do(f);
1690 }
1691
1692 //------------------------------Opcode-----------------------------------------
1693 Opcode::Opcode(char *primary, char *secondary, char *tertiary)
1694 : _primary(primary), _secondary(secondary), _tertiary(tertiary) {
1695 }
1696
1697 Opcode::~Opcode() {
1698 }
1699
1700 Opcode::opcode_type Opcode::as_opcode_type(const char *param) {
1701 if( strcmp(param,"primary") == 0 ) {
1702 return Opcode::PRIMARY;
1703 }
1704 else if( strcmp(param,"secondary") == 0 ) {
1705 return Opcode::SECONDARY;
1706 }
1707 else if( strcmp(param,"tertiary") == 0 ) {
1708 return Opcode::TERTIARY;
1709 }
1710 return Opcode::NOT_AN_OPCODE;
1711 }
1712
1713 bool Opcode::print_opcode(FILE *fp, Opcode::opcode_type desired_opcode) {
1714 // Default values previously provided by MachNode::primary()...
1715 const char *description = nullptr;
1716 const char *value = nullptr;
1717 // Check if user provided any opcode definitions
1718 // Update 'value' if user provided a definition in the instruction
1719 switch (desired_opcode) {
1720 case PRIMARY:
1721 description = "primary()";
1722 if( _primary != nullptr) { value = _primary; }
1723 break;
1724 case SECONDARY:
1725 description = "secondary()";
1726 if( _secondary != nullptr ) { value = _secondary; }
1727 break;
1728 case TERTIARY:
1729 description = "tertiary()";
1730 if( _tertiary != nullptr ) { value = _tertiary; }
1731 break;
1732 default:
1733 assert( false, "ShouldNotReachHere();");
1734 break;
1735 }
1736
1737 if (value != nullptr) {
1738 fprintf(fp, "(%s /*%s*/)", value, description);
1739 }
1740 return value != nullptr;
1741 }
1742
1743 void Opcode::dump() {
1744 output(stderr);
1745 }
1746
1747 // Write info to output files
1748 void Opcode::output(FILE *fp) {
1749 if (_primary != nullptr) fprintf(fp,"Primary opcode: %s\n", _primary);
1750 if (_secondary != nullptr) fprintf(fp,"Secondary opcode: %s\n", _secondary);
1751 if (_tertiary != nullptr) fprintf(fp,"Tertiary opcode: %s\n", _tertiary);
1752 }
1753
1754 //------------------------------InsEncode--------------------------------------
1755 InsEncode::InsEncode() {
1756 }
1757 InsEncode::~InsEncode() {
1758 }
1759
1760 // Add "encode class name" and its parameters
1761 NameAndList *InsEncode::add_encode(char *encoding) {
1762 assert( encoding != nullptr, "Must provide name for encoding");
1763
1764 // add_parameter(NameList::_signal);
1765 NameAndList *encode = new NameAndList(encoding);
1766 _encoding.addName((char*)encode);
1767
1768 return encode;
1769 }
1770
1771 // Access the list of encodings
1772 void InsEncode::reset() {
1773 _encoding.reset();
1774 // _parameter.reset();
1775 }
1776 const char* InsEncode::encode_class_iter() {
1777 NameAndList *encode_class = (NameAndList*)_encoding.iter();
1778 return ( encode_class != nullptr ? encode_class->name() : nullptr );
1779 }
1780 // Obtain parameter name from zero based index
1781 const char *InsEncode::rep_var_name(InstructForm &inst, uint param_no) {
1782 NameAndList *params = (NameAndList*)_encoding.current();
1783 assert( params != nullptr, "Internal Error");
1784 const char *param = (*params)[param_no];
1785
1786 // Remove '$' if parser placed it there.
1787 return ( param != nullptr && *param == '$') ? (param+1) : param;
1788 }
1789
1790 void InsEncode::dump() {
1791 output(stderr);
1792 }
1793
1794 // Write info to output files
1795 void InsEncode::output(FILE *fp) {
1796 NameAndList *encoding = nullptr;
1797 const char *parameter = nullptr;
1798
1799 fprintf(fp,"InsEncode: ");
1800 _encoding.reset();
1801
1802 while ( (encoding = (NameAndList*)_encoding.iter()) != nullptr ) {
1803 // Output the encoding being used
1804 fprintf(fp,"%s(", encoding->name() );
1805
1806 // Output its parameter list, if any
1807 bool first_param = true;
1808 encoding->reset();
1809 while ( (parameter = encoding->iter()) != nullptr ) {
1810 // Output the ',' between parameters
1811 if ( ! first_param ) fprintf(fp,", ");
1812 first_param = false;
1813 // Output the parameter
1814 fprintf(fp,"%s", parameter);
1815 } // done with parameters
1816 fprintf(fp,") ");
1817 } // done with encodings
1818
1819 fprintf(fp,"\n");
1820 }
1821
1822 void InsEncode::forms_do(FormClosure *f) {
1823 _encoding.reset();
1824 NameAndList *encoding = (NameAndList*)_encoding.iter();
1825 for( ; encoding != nullptr; encoding = (NameAndList*)_encoding.iter() ) {
1826 // just check name, other operands will be checked as instruction parameters
1827 f->do_form_by_name(encoding->name());
1828 }
1829 }
1830
1831 //------------------------------Effect-----------------------------------------
1832 static int effect_lookup(const char *name) {
1833 if (!strcmp(name, "USE")) return Component::USE;
1834 if (!strcmp(name, "DEF")) return Component::DEF;
1835 if (!strcmp(name, "USE_DEF")) return Component::USE_DEF;
1836 if (!strcmp(name, "KILL")) return Component::KILL;
1837 if (!strcmp(name, "USE_KILL")) return Component::USE_KILL;
1838 if (!strcmp(name, "TEMP")) return Component::TEMP;
1839 if (!strcmp(name, "TEMP_DEF")) return Component::TEMP_DEF;
1840 if (!strcmp(name, "INVALID")) return Component::INVALID;
1841 if (!strcmp(name, "CALL")) return Component::CALL;
1842 assert(false,"Invalid effect name specified\n");
1843 return Component::INVALID;
1844 }
1845
1846 const char *Component::getUsedefName() {
1847 switch (_usedef) {
1848 case Component::INVALID: return "INVALID"; break;
1849 case Component::USE: return "USE"; break;
1850 case Component::USE_DEF: return "USE_DEF"; break;
1851 case Component::USE_KILL: return "USE_KILL"; break;
1852 case Component::KILL: return "KILL"; break;
1853 case Component::TEMP: return "TEMP"; break;
1854 case Component::TEMP_DEF: return "TEMP_DEF"; break;
1855 case Component::DEF: return "DEF"; break;
1856 case Component::CALL: return "CALL"; break;
1857 default: assert(false, "unknown effect");
1858 }
1859 return "Undefined Use/Def info";
1860 }
1861
1862 Effect::Effect(const char *name) : _name(name), _use_def(effect_lookup(name)) {
1863 _ftype = Form::EFF;
1864 }
1865
1866 Effect::~Effect() {
1867 }
1868
1869 // Dynamic type check
1870 Effect *Effect::is_effect() const {
1871 return (Effect*)this;
1872 }
1873
1874
1875 // True if this component is equal to the parameter.
1876 bool Effect::is(int use_def_kill_enum) const {
1877 return (_use_def == use_def_kill_enum ? true : false);
1878 }
1879 // True if this component is used/def'd/kill'd as the parameter suggests.
1880 bool Effect::isa(int use_def_kill_enum) const {
1881 return (_use_def & use_def_kill_enum) == use_def_kill_enum;
1882 }
1883
1884 void Effect::dump() {
1885 output(stderr);
1886 }
1887
1888 void Effect::output(FILE *fp) { // Write info to output files
1889 fprintf(fp,"Effect: %s\n", (_name?_name:""));
1890 }
1891
1892 //---------------------------------Flag----------------------------------------
1893 Flag::Flag(const char *name) : _name(name), _next(nullptr) {
1894 _ftype = Form::FLG;
1895 }
1896
1897 Flag::~Flag() {
1898 }
1899
1900 void Flag::append_flag(Flag *next_flag) {
1901 if( _next == nullptr ) {
1902 _next = next_flag;
1903 } else {
1904 _next->append_flag( next_flag );
1905 }
1906 }
1907
1908 Flag* Flag::next() {
1909 return _next;
1910 }
1911
1912 void Flag::dump() {
1913 output(stderr);
1914 }
1915
1916 void Flag::output(FILE *fp) { // Write info to output files
1917 fprintf(fp,"Flag: %s\n", (_name?_name:""));
1918 }
1919
1920 //------------------------------ExpandRule-------------------------------------
1921 ExpandRule::ExpandRule() : _expand_instrs(),
1922 _newopconst(cmpstr, hashstr, Form::arena) {
1923 _ftype = Form::EXP;
1924 }
1925
1926 ExpandRule::~ExpandRule() { // Destructor
1927 }
1928
1929 void ExpandRule::add_instruction(NameAndList *instruction_name_and_operand_list) {
1930 _expand_instrs.addName((char*)instruction_name_and_operand_list);
1931 }
1932
1933 void ExpandRule::reset_instructions() {
1934 _expand_instrs.reset();
1935 }
1936
1937 NameAndList* ExpandRule::iter_instructions() {
1938 return (NameAndList*)_expand_instrs.iter();
1939 }
1940
1941
1942 void ExpandRule::dump() {
1943 output(stderr);
1944 }
1945
1946 void ExpandRule::output(FILE *fp) { // Write info to output files
1947 NameAndList *expand_instr = nullptr;
1948 const char *opid = nullptr;
1949
1950 fprintf(fp,"\nExpand Rule:\n");
1951
1952 // Iterate over the instructions 'node' expands into
1953 for(reset_instructions(); (expand_instr = iter_instructions()) != nullptr; ) {
1954 fprintf(fp,"%s(", expand_instr->name());
1955
1956 // iterate over the operand list
1957 for( expand_instr->reset(); (opid = expand_instr->iter()) != nullptr; ) {
1958 fprintf(fp,"%s ", opid);
1959 }
1960 fprintf(fp,");\n");
1961 }
1962 }
1963
1964 void ExpandRule::forms_do(FormClosure *f) {
1965 NameAndList *expand_instr = nullptr;
1966 // Iterate over the instructions 'node' expands into
1967 for(reset_instructions(); (expand_instr = iter_instructions()) != nullptr; ) {
1968 f->do_form_by_name(expand_instr->name());
1969 }
1970 _newopers.reset();
1971 const char* oper = _newopers.iter();
1972 for(; oper != nullptr; oper = _newopers.iter()) {
1973 f->do_form_by_name(oper);
1974 }
1975 }
1976
1977 //------------------------------RewriteRule------------------------------------
1978 RewriteRule::RewriteRule(char* params, char* block)
1979 : _tempParams(params), _tempBlock(block) { }; // Constructor
1980 RewriteRule::~RewriteRule() { // Destructor
1981 }
1982
1983 void RewriteRule::dump() {
1984 output(stderr);
1985 }
1986
1987 void RewriteRule::output(FILE *fp) { // Write info to output files
1988 fprintf(fp,"\nRewrite Rule:\n%s\n%s\n",
1989 (_tempParams?_tempParams:""),
1990 (_tempBlock?_tempBlock:""));
1991 }
1992
1993 void RewriteRule::forms_do(FormClosure *f) {
1994 if (_condition) f->do_form(_condition);
1995 if (_instrs) f->do_form(_instrs);
1996 if (_opers) f->do_form(_opers);
1997 }
1998
1999
2000 //==============================MachNodes======================================
2001 //------------------------------MachNodeForm-----------------------------------
2002 MachNodeForm::MachNodeForm(char *id)
2003 : _ident(id) {
2004 }
2005
2006 MachNodeForm::~MachNodeForm() {
2007 }
2008
2009 MachNodeForm *MachNodeForm::is_machnode() const {
2010 return (MachNodeForm*)this;
2011 }
2012
2013 //==============================Operand Classes================================
2014 //------------------------------OpClassForm------------------------------------
2015 OpClassForm::OpClassForm(const char* id) : _ident(id) {
2016 _ftype = Form::OPCLASS;
2017 }
2018
2019 OpClassForm::~OpClassForm() {
2020 }
2021
2022 bool OpClassForm::ideal_only() const { return 0; }
2023
2024 OpClassForm *OpClassForm::is_opclass() const {
2025 return (OpClassForm*)this;
2026 }
2027
2028 Form::InterfaceType OpClassForm::interface_type(FormDict &globals) const {
2029 if( _oplst.count() == 0 ) return Form::no_interface;
2030
2031 // Check that my operands have the same interface type
2032 Form::InterfaceType interface;
2033 bool first = true;
2034 NameList &op_list = (NameList &)_oplst;
2035 op_list.reset();
2036 const char *op_name;
2037 while( (op_name = op_list.iter()) != nullptr ) {
2038 const Form *form = globals[op_name];
2039 OperandForm *operand = form->is_operand();
2040 assert( operand, "Entry in operand class that is not an operand");
2041 if( first ) {
2042 first = false;
2043 interface = operand->interface_type(globals);
2044 } else {
2045 interface = (interface == operand->interface_type(globals) ? interface : Form::no_interface);
2046 }
2047 }
2048 return interface;
2049 }
2050
2051 bool OpClassForm::stack_slots_only(FormDict &globals) const {
2052 if( _oplst.count() == 0 ) return false; // how?
2053
2054 NameList &op_list = (NameList &)_oplst;
2055 op_list.reset();
2056 const char *op_name;
2057 while( (op_name = op_list.iter()) != nullptr ) {
2058 const Form *form = globals[op_name];
2059 OperandForm *operand = form->is_operand();
2060 assert( operand, "Entry in operand class that is not an operand");
2061 if( !operand->stack_slots_only(globals) ) return false;
2062 }
2063 return true;
2064 }
2065
2066
2067 void OpClassForm::dump() {
2068 output(stderr);
2069 }
2070
2071 void OpClassForm::output(FILE *fp) {
2072 const char *name;
2073 fprintf(fp,"\nOperand Class: %s\n", (_ident?_ident:""));
2074 fprintf(fp,"\nCount = %d\n", _oplst.count());
2075 for(_oplst.reset(); (name = _oplst.iter()) != nullptr;) {
2076 fprintf(fp,"%s, ",name);
2077 }
2078 fprintf(fp,"\n");
2079 }
2080
2081 void OpClassForm::forms_do(FormClosure* f) {
2082 const char *name;
2083 for(_oplst.reset(); (name = _oplst.iter()) != nullptr;) {
2084 f->do_form_by_name(name);
2085 }
2086 }
2087
2088
2089 //==============================Operands=======================================
2090 //------------------------------OperandForm------------------------------------
2091 OperandForm::OperandForm(const char* id)
2092 : OpClassForm(id), _ideal_only(false),
2093 _localNames(cmpstr, hashstr, Form::arena) {
2094 _ftype = Form::OPER;
2095
2096 _matrule = nullptr;
2097 _interface = nullptr;
2098 _attribs = nullptr;
2099 _predicate = nullptr;
2100 _constraint= nullptr;
2101 _construct = nullptr;
2102 _format = nullptr;
2103 }
2104 OperandForm::OperandForm(const char* id, bool ideal_only)
2105 : OpClassForm(id), _ideal_only(ideal_only),
2106 _localNames(cmpstr, hashstr, Form::arena) {
2107 _ftype = Form::OPER;
2108
2109 _matrule = nullptr;
2110 _interface = nullptr;
2111 _attribs = nullptr;
2112 _predicate = nullptr;
2113 _constraint= nullptr;
2114 _construct = nullptr;
2115 _format = nullptr;
2116 }
2117 OperandForm::~OperandForm() {
2118 }
2119
2120
2121 OperandForm *OperandForm::is_operand() const {
2122 return (OperandForm*)this;
2123 }
2124
2125 bool OperandForm::ideal_only() const {
2126 return _ideal_only;
2127 }
2128
2129 Form::InterfaceType OperandForm::interface_type(FormDict &globals) const {
2130 if( _interface == nullptr ) return Form::no_interface;
2131
2132 return _interface->interface_type(globals);
2133 }
2134
2135
2136 bool OperandForm::stack_slots_only(FormDict &globals) const {
2137 if( _constraint == nullptr ) return false;
2138 return _constraint->stack_slots_only();
2139 }
2140
2141
2142 // Access op_cost attribute or return null.
2143 const char* OperandForm::cost() {
2144 for (Attribute* cur = _attribs; cur != nullptr; cur = (Attribute*)cur->_next) {
2145 if( strcmp(cur->_ident,AttributeForm::_op_cost) == 0 ) {
2146 return cur->_val;
2147 }
2148 }
2149 return nullptr;
2150 }
2151
2152 // Return the number of leaves below this complex operand
2153 uint OperandForm::num_leaves() const {
2154 if ( ! _matrule) return 0;
2155
2156 int num_leaves = _matrule->_numleaves;
2157 return num_leaves;
2158 }
2159
2160 // Return the number of constants contained within this complex operand
2161 uint OperandForm::num_consts(FormDict &globals) const {
2162 if ( ! _matrule) return 0;
2163
2164 // This is a recursive invocation on all operands in the matchrule
2165 return _matrule->num_consts(globals);
2166 }
2167
2168 // Return the number of constants in match rule with specified type
2169 uint OperandForm::num_consts(FormDict &globals, Form::DataType type) const {
2170 if ( ! _matrule) return 0;
2171
2172 // This is a recursive invocation on all operands in the matchrule
2173 return _matrule->num_consts(globals, type);
2174 }
2175
2176 // Return the number of pointer constants contained within this complex operand
2177 uint OperandForm::num_const_ptrs(FormDict &globals) const {
2178 if ( ! _matrule) return 0;
2179
2180 // This is a recursive invocation on all operands in the matchrule
2181 return _matrule->num_const_ptrs(globals);
2182 }
2183
2184 uint OperandForm::num_edges(FormDict &globals) const {
2185 uint edges = 0;
2186 uint leaves = num_leaves();
2187 uint consts = num_consts(globals);
2188
2189 // If we are matching a constant directly, there are no leaves.
2190 edges = ( leaves > consts ) ? leaves - consts : 0;
2191
2192 // !!!!!
2193 // Special case operands that do not have a corresponding ideal node.
2194 if( (edges == 0) && (consts == 0) ) {
2195 if( constrained_reg_class() != nullptr ) {
2196 edges = 1;
2197 } else {
2198 if( _matrule
2199 && (_matrule->_lChild == nullptr) && (_matrule->_rChild == nullptr) ) {
2200 const Form *form = globals[_matrule->_opType];
2201 OperandForm *oper = form ? form->is_operand() : nullptr;
2202 if( oper ) {
2203 return oper->num_edges(globals);
2204 }
2205 }
2206 }
2207 }
2208
2209 return edges;
2210 }
2211
2212
2213 // Check if this operand is usable for cisc-spilling
2214 bool OperandForm::is_cisc_reg(FormDict &globals) const {
2215 const char *ideal = ideal_type(globals);
2216 bool is_cisc_reg = (ideal && (ideal_to_Reg_type(ideal) != none));
2217 return is_cisc_reg;
2218 }
2219
2220 bool OpClassForm::is_cisc_mem(FormDict &globals) const {
2221 Form::InterfaceType my_interface = interface_type(globals);
2222 return (my_interface == memory_interface);
2223 }
2224
2225
2226 // node matches ideal 'Bool'
2227 bool OperandForm::is_ideal_bool() const {
2228 if( _matrule == nullptr ) return false;
2229
2230 return _matrule->is_ideal_bool();
2231 }
2232
2233 // Require user's name for an sRegX to be stackSlotX
2234 Form::DataType OperandForm::is_user_name_for_sReg() const {
2235 DataType data_type = none;
2236 if( _ident != nullptr ) {
2237 if( strcmp(_ident,"stackSlotI") == 0 ) data_type = Form::idealI;
2238 else if( strcmp(_ident,"stackSlotP") == 0 ) data_type = Form::idealP;
2239 else if( strcmp(_ident,"stackSlotD") == 0 ) data_type = Form::idealD;
2240 else if( strcmp(_ident,"stackSlotF") == 0 ) data_type = Form::idealF;
2241 else if( strcmp(_ident,"stackSlotL") == 0 ) data_type = Form::idealL;
2242 }
2243 assert((data_type == none) || (_matrule == nullptr), "No match-rule for stackSlotX");
2244
2245 return data_type;
2246 }
2247
2248
2249 // Return ideal type, if there is a single ideal type for this operand
2250 const char *OperandForm::ideal_type(FormDict &globals, RegisterForm *registers) const {
2251 const char *type = nullptr;
2252 if (ideal_only()) type = _ident;
2253 else if( _matrule == nullptr ) {
2254 // Check for condition code register
2255 const char *rc_name = constrained_reg_class();
2256 // !!!!!
2257 if (rc_name == nullptr) return nullptr;
2258 // !!!!! !!!!!
2259 // Check constraints on result's register class
2260 if( registers ) {
2261 RegClass *reg_class = registers->getRegClass(rc_name);
2262 assert( reg_class != nullptr, "Register class is not defined");
2263
2264 // Check for ideal type of entries in register class, all are the same type
2265 reg_class->reset();
2266 RegDef *reg_def = reg_class->RegDef_iter();
2267 assert( reg_def != nullptr, "No entries in register class");
2268 assert( reg_def->_idealtype != nullptr, "Did not define ideal type for register");
2269 // Return substring that names the register's ideal type
2270 type = reg_def->_idealtype + 3;
2271 assert( *(reg_def->_idealtype + 0) == 'O', "Expect Op_ prefix");
2272 assert( *(reg_def->_idealtype + 1) == 'p', "Expect Op_ prefix");
2273 assert( *(reg_def->_idealtype + 2) == '_', "Expect Op_ prefix");
2274 }
2275 }
2276 else if( _matrule->_lChild == nullptr && _matrule->_rChild == nullptr ) {
2277 // This operand matches a single type, at the top level.
2278 // Check for ideal type
2279 type = _matrule->_opType;
2280 if( strcmp(type,"Bool") == 0 )
2281 return "Bool";
2282 // transitive lookup
2283 const Form *frm = globals[type];
2284 OperandForm *op = frm->is_operand();
2285 type = op->ideal_type(globals, registers);
2286 }
2287 return type;
2288 }
2289
2290
2291 // If there is a single ideal type for this interface field, return it.
2292 const char *OperandForm::interface_ideal_type(FormDict &globals,
2293 const char *field) const {
2294 const char *ideal_type = nullptr;
2295 const char *value = nullptr;
2296
2297 // Check if "field" is valid for this operand's interface
2298 if ( ! is_interface_field(field, value) ) return ideal_type;
2299
2300 // !!!!! !!!!! !!!!!
2301 // If a valid field has a constant value, identify "ConI" or "ConP" or ...
2302
2303 // Else, lookup type of field's replacement variable
2304
2305 return ideal_type;
2306 }
2307
2308
2309 RegClass* OperandForm::get_RegClass() const {
2310 if (_interface && !_interface->is_RegInterface()) return nullptr;
2311 return globalAD->get_registers()->getRegClass(constrained_reg_class());
2312 }
2313
2314
2315 bool OperandForm::is_bound_register() const {
2316 RegClass* reg_class = get_RegClass();
2317 if (reg_class == nullptr) {
2318 return false;
2319 }
2320
2321 const char* name = ideal_type(globalAD->globalNames());
2322 if (name == nullptr) {
2323 return false;
2324 }
2325
2326 uint size = 0;
2327 if (strcmp(name, "RegFlags") == 0) size = 1;
2328 if (strcmp(name, "RegI") == 0) size = 1;
2329 if (strcmp(name, "RegF") == 0) size = 1;
2330 if (strcmp(name, "RegD") == 0) size = 2;
2331 if (strcmp(name, "RegL") == 0) size = 2;
2332 if (strcmp(name, "RegN") == 0) size = 1;
2333 if (strcmp(name, "RegVectMask") == 0) size = globalAD->get_preproc_def("AARCH64") ? 1 : 2;
2334 if (strcmp(name, "VecX") == 0) size = 4;
2335 if (strcmp(name, "VecY") == 0) size = 8;
2336 if (strcmp(name, "VecZ") == 0) size = 16;
2337 if (strcmp(name, "RegP") == 0) size = globalAD->get_preproc_def("_LP64") ? 2 : 1;
2338 if (size == 0) {
2339 return false;
2340 }
2341 return size == reg_class->size();
2342 }
2343
2344
2345 // Check if this is a valid field for this operand,
2346 // Return 'true' if valid, and set the value to the string the user provided.
2347 bool OperandForm::is_interface_field(const char *field,
2348 const char * &value) const {
2349 return false;
2350 }
2351
2352
2353 // Return register class name if a constraint specifies the register class.
2354 const char *OperandForm::constrained_reg_class() const {
2355 const char *reg_class = nullptr;
2356 if ( _constraint ) {
2357 // !!!!!
2358 Constraint *constraint = _constraint;
2359 if ( strcmp(_constraint->_func,"ALLOC_IN_RC") == 0 ) {
2360 reg_class = _constraint->_arg;
2361 }
2362 }
2363
2364 return reg_class;
2365 }
2366
2367
2368 // Return the register class associated with 'leaf'.
2369 const char *OperandForm::in_reg_class(uint leaf, FormDict &globals) {
2370 const char* reg_class = nullptr; // "RegMask::EMPTY";
2371
2372 if((_matrule == nullptr) || (_matrule->is_chain_rule(globals))) {
2373 reg_class = constrained_reg_class();
2374 return reg_class;
2375 }
2376 const char *result = nullptr;
2377 const char *name = nullptr;
2378 const char *type = nullptr;
2379 // iterate through all base operands
2380 // until we reach the register that corresponds to "leaf"
2381 // This function is not looking for an ideal type. It needs the first
2382 // level user type associated with the leaf.
2383 for(uint idx = 0;_matrule->base_operand(idx,globals,result,name,type);++idx) {
2384 const Form *form = (_localNames[name] ? _localNames[name] : globals[result]);
2385 OperandForm *oper = form ? form->is_operand() : nullptr;
2386 if( oper ) {
2387 reg_class = oper->constrained_reg_class();
2388 if( reg_class ) {
2389 reg_class = reg_class;
2390 } else {
2391 // ShouldNotReachHere();
2392 }
2393 } else {
2394 // ShouldNotReachHere();
2395 }
2396
2397 // Increment our target leaf position if current leaf is not a candidate.
2398 if( reg_class == nullptr) ++leaf;
2399 // Exit the loop with the value of reg_class when at the correct index
2400 if( idx == leaf ) break;
2401 // May iterate through all base operands if reg_class for 'leaf' is null
2402 }
2403 return reg_class;
2404 }
2405
2406
2407 // Recursive call to construct list of top-level operands.
2408 // Implementation does not modify state of internal structures
2409 void OperandForm::build_components() {
2410 if (_matrule) _matrule->append_components(_localNames, _components);
2411
2412 // Add parameters that "do not appear in match rule".
2413 const char *name;
2414 for (_parameters.reset(); (name = _parameters.iter()) != nullptr;) {
2415 OpClassForm *opForm = _localNames[name]->is_opclass();
2416 assert(opForm != nullptr, "sanity");
2417
2418 if ( _components.operand_position(name) == -1 ) {
2419 _components.insert(name, opForm->_ident, Component::INVALID, false);
2420 }
2421 }
2422
2423 return;
2424 }
2425
2426 int OperandForm::operand_position(const char *name, int usedef) {
2427 return _components.operand_position(name, usedef, this);
2428 }
2429
2430
2431 // Return zero-based position in component list, only counting constants;
2432 // Return -1 if not in list.
2433 int OperandForm::constant_position(FormDict &globals, const Component *last) {
2434 // Iterate through components and count constants preceding 'constant'
2435 int position = 0;
2436 Component *comp;
2437 _components.reset();
2438 while( (comp = _components.iter()) != nullptr && (comp != last) ) {
2439 // Special case for operands that take a single user-defined operand
2440 // Skip the initial definition in the component list.
2441 if( strcmp(comp->_name,this->_ident) == 0 ) continue;
2442
2443 const char *type = comp->_type;
2444 // Lookup operand form for replacement variable's type
2445 const Form *form = globals[type];
2446 assert( form != nullptr, "Component's type not found");
2447 OperandForm *oper = form ? form->is_operand() : nullptr;
2448 if( oper ) {
2449 if( oper->_matrule->is_base_constant(globals) != Form::none ) {
2450 ++position;
2451 }
2452 }
2453 }
2454
2455 // Check for being passed a component that was not in the list
2456 if( comp != last ) position = -1;
2457
2458 return position;
2459 }
2460 // Provide position of constant by "name"
2461 int OperandForm::constant_position(FormDict &globals, const char *name) {
2462 const Component *comp = _components.search(name);
2463 int idx = constant_position( globals, comp );
2464
2465 return idx;
2466 }
2467
2468
2469 // Return zero-based position in component list, only counting constants;
2470 // Return -1 if not in list.
2471 int OperandForm::register_position(FormDict &globals, const char *reg_name) {
2472 // Iterate through components and count registers preceding 'last'
2473 uint position = 0;
2474 Component *comp;
2475 _components.reset();
2476 while( (comp = _components.iter()) != nullptr
2477 && (strcmp(comp->_name,reg_name) != 0) ) {
2478 // Special case for operands that take a single user-defined operand
2479 // Skip the initial definition in the component list.
2480 if( strcmp(comp->_name,this->_ident) == 0 ) continue;
2481
2482 const char *type = comp->_type;
2483 // Lookup operand form for component's type
2484 const Form *form = globals[type];
2485 assert( form != nullptr, "Component's type not found");
2486 OperandForm *oper = form ? form->is_operand() : nullptr;
2487 if( oper ) {
2488 if( oper->_matrule->is_base_register(globals) ) {
2489 ++position;
2490 }
2491 }
2492 }
2493
2494 return position;
2495 }
2496
2497
2498 const char *OperandForm::reduce_result() const {
2499 return _ident;
2500 }
2501 // Return the name of the operand on the right hand side of the binary match
2502 // Return null if there is no right hand side
2503 const char *OperandForm::reduce_right(FormDict &globals) const {
2504 return ( _matrule ? _matrule->reduce_right(globals) : nullptr );
2505 }
2506
2507 // Similar for left
2508 const char *OperandForm::reduce_left(FormDict &globals) const {
2509 return ( _matrule ? _matrule->reduce_left(globals) : nullptr );
2510 }
2511
2512
2513 // --------------------------- FILE *output_routines
2514 //
2515 // Output code for disp_is_oop, if true.
2516 void OperandForm::disp_is_oop(FILE *fp, FormDict &globals) {
2517 // Check it is a memory interface with a non-user-constant disp field
2518 if ( this->_interface == nullptr ) return;
2519 MemInterface *mem_interface = this->_interface->is_MemInterface();
2520 if ( mem_interface == nullptr ) return;
2521 const char *disp = mem_interface->_disp;
2522 if ( *disp != '$' ) return;
2523
2524 // Lookup replacement variable in operand's component list
2525 const char *rep_var = disp + 1;
2526 const Component *comp = this->_components.search(rep_var);
2527 assert( comp != nullptr, "Replacement variable not found in components");
2528 // Lookup operand form for replacement variable's type
2529 const char *type = comp->_type;
2530 Form *form = (Form*)globals[type];
2531 assert( form != nullptr, "Replacement variable's type not found");
2532 OperandForm *op = form->is_operand();
2533 assert( op, "Memory Interface 'disp' can only emit an operand form");
2534 // Check if this is a ConP, which may require relocation
2535 if ( op->is_base_constant(globals) == Form::idealP ) {
2536 // Find the constant's index: _c0, _c1, _c2, ... , _cN
2537 uint idx = op->constant_position( globals, rep_var);
2538 fprintf(fp," virtual relocInfo::relocType disp_reloc() const {");
2539 fprintf(fp, " return _c%d->reloc();", idx);
2540 fprintf(fp, " }\n");
2541 }
2542 }
2543
2544 // Generate code for internal and external format methods
2545 //
2546 // internal access to reg# node->_idx
2547 // access to subsumed constant _c0, _c1,
2548 void OperandForm::int_format(FILE *fp, FormDict &globals, uint index) {
2549 Form::DataType dtype;
2550 if (_matrule && (_matrule->is_base_register(globals) ||
2551 strcmp(ideal_type(globalAD->globalNames()), "RegFlags") == 0)) {
2552 // !!!!! !!!!!
2553 fprintf(fp," { char reg_str[128];\n");
2554 fprintf(fp," ra->dump_register(node,reg_str, sizeof(reg_str));\n");
2555 fprintf(fp," st->print(\"%cs\",reg_str);\n",'%');
2556 fprintf(fp," }\n");
2557 } else if (_matrule && (dtype = _matrule->is_base_constant(globals)) != Form::none) {
2558 format_constant( fp, index, dtype );
2559 } else if (ideal_to_sReg_type(_ident) != Form::none) {
2560 // Special format for Stack Slot Register
2561 fprintf(fp," { char reg_str[128];\n");
2562 fprintf(fp," ra->dump_register(node,reg_str, sizeof(reg_str));\n");
2563 fprintf(fp," st->print(\"%cs\",reg_str);\n",'%');
2564 fprintf(fp," }\n");
2565 } else {
2566 fprintf(fp," st->print(\"No format defined for %s\n\");\n", _ident);
2567 fflush(fp);
2568 fprintf(stderr,"No format defined for %s\n", _ident);
2569 dump();
2570 assert( false,"Internal error:\n output_internal_operand() attempting to output other than a Register or Constant");
2571 }
2572 }
2573
2574 // Similar to "int_format" but for cases where data is external to operand
2575 // external access to reg# node->in(idx)->_idx,
2576 void OperandForm::ext_format(FILE *fp, FormDict &globals, uint index) {
2577 Form::DataType dtype;
2578 if (_matrule && (_matrule->is_base_register(globals) ||
2579 strcmp(ideal_type(globalAD->globalNames()), "RegFlags") == 0)) {
2580 fprintf(fp," { char reg_str[128];\n");
2581 fprintf(fp," ra->dump_register(node->in(idx");
2582 if ( index != 0 ) fprintf(fp, "+%d",index);
2583 fprintf(fp, "),reg_str,sizeof(reg_str));\n");
2584 fprintf(fp," st->print(\"%cs\",reg_str);\n",'%');
2585 fprintf(fp," }\n");
2586 } else if (_matrule && (dtype = _matrule->is_base_constant(globals)) != Form::none) {
2587 format_constant( fp, index, dtype );
2588 } else if (ideal_to_sReg_type(_ident) != Form::none) {
2589 // Special format for Stack Slot Register
2590 fprintf(fp," { char reg_str[128];\n");
2591 fprintf(fp," ra->dump_register(node->in(idx");
2592 if ( index != 0 ) fprintf(fp, "+%d",index);
2593 fprintf(fp, "),reg_str,sizeof(reg_str));\n");
2594 fprintf(fp," st->print(\"%cs\",reg_str);\n",'%');
2595 fprintf(fp," }\n");
2596 } else {
2597 fprintf(fp," st->print(\"No format defined for %s\n\");\n", _ident);
2598 assert( false,"Internal error:\n output_external_operand() attempting to output other than a Register or Constant");
2599 }
2600 }
2601
2602 void OperandForm::format_constant(FILE *fp, uint const_index, uint const_type) {
2603 switch(const_type) {
2604 case Form::idealI: fprintf(fp," st->print(\"#%%d\", _c%d);\n", const_index); break;
2605 case Form::idealP: fprintf(fp," if (_c%d) _c%d->dump_on(st);\n", const_index, const_index); break;
2606 case Form::idealNKlass:
2607 case Form::idealN: fprintf(fp," if (_c%d) _c%d->dump_on(st);\n", const_index, const_index); break;
2608 case Form::idealL: fprintf(fp," st->print(\"#\" INT64_FORMAT, (int64_t)_c%d);\n", const_index); break;
2609 case Form::idealF: fprintf(fp," st->print(\"#%%f\", _c%d);\n", const_index); break;
2610 case Form::idealH: fprintf(fp," st->print(\"#%%d\", _c%d);\n", const_index); break;
2611 case Form::idealD: fprintf(fp," st->print(\"#%%f\", _c%d);\n", const_index); break;
2612 default:
2613 assert( false, "ShouldNotReachHere()");
2614 }
2615 }
2616
2617 // Return the operand form corresponding to the given index, else null.
2618 OperandForm *OperandForm::constant_operand(FormDict &globals,
2619 uint index) {
2620 // !!!!!
2621 // Check behavior on complex operands
2622 uint n_consts = num_consts(globals);
2623 if( n_consts > 0 ) {
2624 uint i = 0;
2625 const char *type;
2626 Component *comp;
2627 _components.reset();
2628 if ((comp = _components.iter()) == nullptr) {
2629 assert(n_consts == 1, "Bad component list detected.\n");
2630 // Current operand is THE operand
2631 if ( index == 0 ) {
2632 return this;
2633 }
2634 } // end if null
2635 else {
2636 // Skip the first component, it can not be a DEF of a constant
2637 do {
2638 type = comp->base_type(globals);
2639 // Check that "type" is a 'ConI', 'ConP', ...
2640 if ( ideal_to_const_type(type) != Form::none ) {
2641 // When at correct component, get corresponding Operand
2642 if ( index == 0 ) {
2643 return globals[comp->_type]->is_operand();
2644 }
2645 // Decrement number of constants to go
2646 --index;
2647 }
2648 } while((comp = _components.iter()) != nullptr);
2649 }
2650 }
2651
2652 // Did not find a constant for this index.
2653 return nullptr;
2654 }
2655
2656 // If this operand has a single ideal type, return its type
2657 Form::DataType OperandForm::simple_type(FormDict &globals) const {
2658 const char *type_name = ideal_type(globals);
2659 Form::DataType type = type_name ? ideal_to_const_type( type_name )
2660 : Form::none;
2661 return type;
2662 }
2663
2664 Form::DataType OperandForm::is_base_constant(FormDict &globals) const {
2665 if ( _matrule == nullptr ) return Form::none;
2666
2667 return _matrule->is_base_constant(globals);
2668 }
2669
2670 // "true" if this operand is a simple type that is swallowed
2671 bool OperandForm::swallowed(FormDict &globals) const {
2672 Form::DataType type = simple_type(globals);
2673 if( type != Form::none ) {
2674 return true;
2675 }
2676
2677 return false;
2678 }
2679
2680 // Output code to access the value of the index'th constant
2681 void OperandForm::access_constant(FILE *fp, FormDict &globals,
2682 uint const_index) {
2683 OperandForm *oper = constant_operand(globals, const_index);
2684 assert( oper, "Index exceeds number of constants in operand");
2685 Form::DataType dtype = oper->is_base_constant(globals);
2686
2687 switch(dtype) {
2688 case idealI: fprintf(fp,"_c%d", const_index); break;
2689 case idealP: fprintf(fp,"_c%d->get_con()",const_index); break;
2690 case idealL: fprintf(fp,"_c%d", const_index); break;
2691 case idealF: fprintf(fp,"_c%d", const_index); break;
2692 case idealH: fprintf(fp,"_c%d", const_index); break;
2693 case idealD: fprintf(fp,"_c%d", const_index); break;
2694 default:
2695 assert( false, "ShouldNotReachHere()");
2696 }
2697 }
2698
2699
2700 void OperandForm::dump() {
2701 output(stderr);
2702 }
2703
2704 void OperandForm::output(FILE *fp) {
2705 fprintf(fp,"\nOperand: %s\n", (_ident?_ident:""));
2706 if (_matrule) _matrule->dump();
2707 if (_interface) _interface->dump();
2708 if (_attribs) _attribs->dump();
2709 if (_predicate) _predicate->dump();
2710 if (_constraint) _constraint->dump();
2711 if (_construct) _construct->dump();
2712 if (_format) _format->dump();
2713 }
2714
2715 void OperandForm::forms_do(FormClosure* f) {
2716 if (_matrule) f->do_form(_matrule);
2717 if (_interface) f->do_form(_interface);
2718 if (_attribs) f->do_form(_attribs);
2719 if (_predicate) f->do_form(_predicate);
2720 if (_constraint) f->do_form(_constraint);
2721 if (_construct) f->do_form(_construct);
2722 if (_format) f->do_form(_format);
2723 _localNames.forms_do(f);
2724 const char* opclass = nullptr;
2725 for ( _classes.reset(); (opclass = _classes.iter()) != nullptr; ) {
2726 f->do_form_by_name(opclass);
2727 }
2728 assert(_components.count() == 0, "skip _compnets");
2729 }
2730
2731 //------------------------------Constraint-------------------------------------
2732 Constraint::Constraint(const char *func, const char *arg)
2733 : _func(func), _arg(arg) {
2734 }
2735 Constraint::~Constraint() { /* not owner of char* */
2736 }
2737
2738 bool Constraint::stack_slots_only() const {
2739 return strcmp(_func, "ALLOC_IN_RC") == 0
2740 && strcmp(_arg, "stack_slots") == 0;
2741 }
2742
2743 void Constraint::dump() {
2744 output(stderr);
2745 }
2746
2747 void Constraint::output(FILE *fp) { // Write info to output files
2748 assert((_func != nullptr && _arg != nullptr),"missing constraint function or arg");
2749 fprintf(fp,"Constraint: %s ( %s )\n", _func, _arg);
2750 }
2751
2752 void Constraint::forms_do(FormClosure *f) {
2753 f->do_form_by_name(_arg);
2754 }
2755
2756 //------------------------------Predicate--------------------------------------
2757 Predicate::Predicate(char *pr)
2758 : _pred(pr) {
2759 }
2760 Predicate::~Predicate() {
2761 }
2762
2763 void Predicate::dump() {
2764 output(stderr);
2765 }
2766
2767 void Predicate::output(FILE *fp) {
2768 fprintf(fp,"Predicate"); // Write to output files
2769 }
2770 //------------------------------Interface--------------------------------------
2771 Interface::Interface(const char *name) : _name(name) {
2772 }
2773 Interface::~Interface() {
2774 }
2775
2776 Form::InterfaceType Interface::interface_type(FormDict &globals) const {
2777 Interface *thsi = (Interface*)this;
2778 if ( thsi->is_RegInterface() ) return Form::register_interface;
2779 if ( thsi->is_MemInterface() ) return Form::memory_interface;
2780 if ( thsi->is_ConstInterface() ) return Form::constant_interface;
2781 if ( thsi->is_CondInterface() ) return Form::conditional_interface;
2782
2783 return Form::no_interface;
2784 }
2785
2786 RegInterface *Interface::is_RegInterface() {
2787 if ( strcmp(_name,"REG_INTER") != 0 )
2788 return nullptr;
2789 return (RegInterface*)this;
2790 }
2791 MemInterface *Interface::is_MemInterface() {
2792 if ( strcmp(_name,"MEMORY_INTER") != 0 ) return nullptr;
2793 return (MemInterface*)this;
2794 }
2795 ConstInterface *Interface::is_ConstInterface() {
2796 if ( strcmp(_name,"CONST_INTER") != 0 ) return nullptr;
2797 return (ConstInterface*)this;
2798 }
2799 CondInterface *Interface::is_CondInterface() {
2800 if ( strcmp(_name,"COND_INTER") != 0 ) return nullptr;
2801 return (CondInterface*)this;
2802 }
2803
2804
2805 void Interface::dump() {
2806 output(stderr);
2807 }
2808
2809 // Write info to output files
2810 void Interface::output(FILE *fp) {
2811 fprintf(fp,"Interface: %s\n", (_name ? _name : "") );
2812 }
2813
2814 //------------------------------RegInterface-----------------------------------
2815 RegInterface::RegInterface() : Interface("REG_INTER") {
2816 }
2817 RegInterface::~RegInterface() {
2818 }
2819
2820 void RegInterface::dump() {
2821 output(stderr);
2822 }
2823
2824 // Write info to output files
2825 void RegInterface::output(FILE *fp) {
2826 Interface::output(fp);
2827 }
2828
2829 //------------------------------ConstInterface---------------------------------
2830 ConstInterface::ConstInterface() : Interface("CONST_INTER") {
2831 }
2832 ConstInterface::~ConstInterface() {
2833 }
2834
2835 void ConstInterface::dump() {
2836 output(stderr);
2837 }
2838
2839 // Write info to output files
2840 void ConstInterface::output(FILE *fp) {
2841 Interface::output(fp);
2842 }
2843
2844 //------------------------------MemInterface-----------------------------------
2845 MemInterface::MemInterface(char *base, char *index, char *scale, char *disp)
2846 : Interface("MEMORY_INTER"), _base(base), _index(index), _scale(scale), _disp(disp) {
2847 }
2848 MemInterface::~MemInterface() {
2849 // not owner of any character arrays
2850 }
2851
2852 void MemInterface::dump() {
2853 output(stderr);
2854 }
2855
2856 // Write info to output files
2857 void MemInterface::output(FILE *fp) {
2858 Interface::output(fp);
2859 if ( _base != nullptr ) fprintf(fp," base == %s\n", _base);
2860 if ( _index != nullptr ) fprintf(fp," index == %s\n", _index);
2861 if ( _scale != nullptr ) fprintf(fp," scale == %s\n", _scale);
2862 if ( _disp != nullptr ) fprintf(fp," disp == %s\n", _disp);
2863 // fprintf(fp,"\n");
2864 }
2865
2866 //------------------------------CondInterface----------------------------------
2867 CondInterface::CondInterface(const char* equal, const char* equal_format,
2868 const char* not_equal, const char* not_equal_format,
2869 const char* less, const char* less_format,
2870 const char* greater_equal, const char* greater_equal_format,
2871 const char* less_equal, const char* less_equal_format,
2872 const char* greater, const char* greater_format,
2873 const char* overflow, const char* overflow_format,
2874 const char* no_overflow, const char* no_overflow_format)
2875 : Interface("COND_INTER"),
2876 _equal(equal), _equal_format(equal_format),
2877 _not_equal(not_equal), _not_equal_format(not_equal_format),
2878 _less(less), _less_format(less_format),
2879 _greater_equal(greater_equal), _greater_equal_format(greater_equal_format),
2880 _less_equal(less_equal), _less_equal_format(less_equal_format),
2881 _greater(greater), _greater_format(greater_format),
2882 _overflow(overflow), _overflow_format(overflow_format),
2883 _no_overflow(no_overflow), _no_overflow_format(no_overflow_format) {
2884 }
2885 CondInterface::~CondInterface() {
2886 // not owner of any character arrays
2887 }
2888
2889 void CondInterface::dump() {
2890 output(stderr);
2891 }
2892
2893 // Write info to output files
2894 void CondInterface::output(FILE *fp) {
2895 Interface::output(fp);
2896 if ( _equal != nullptr ) fprintf(fp," equal == %s\n", _equal);
2897 if ( _not_equal != nullptr ) fprintf(fp," not_equal == %s\n", _not_equal);
2898 if ( _less != nullptr ) fprintf(fp," less == %s\n", _less);
2899 if ( _greater_equal != nullptr ) fprintf(fp," greater_equal == %s\n", _greater_equal);
2900 if ( _less_equal != nullptr ) fprintf(fp," less_equal == %s\n", _less_equal);
2901 if ( _greater != nullptr ) fprintf(fp," greater == %s\n", _greater);
2902 if ( _overflow != nullptr ) fprintf(fp," overflow == %s\n", _overflow);
2903 if ( _no_overflow != nullptr ) fprintf(fp," no_overflow == %s\n", _no_overflow);
2904 // fprintf(fp,"\n");
2905 }
2906
2907 //------------------------------ConstructRule----------------------------------
2908 ConstructRule::ConstructRule(char *cnstr)
2909 : _construct(cnstr) {
2910 }
2911 ConstructRule::~ConstructRule() {
2912 }
2913
2914 void ConstructRule::dump() {
2915 output(stderr);
2916 }
2917
2918 void ConstructRule::output(FILE *fp) {
2919 fprintf(fp,"\nConstruct Rule\n"); // Write to output files
2920 }
2921
2922
2923 //==============================Shared Forms===================================
2924 //------------------------------AttributeForm----------------------------------
2925 int AttributeForm::_insId = 0; // start counter at 0
2926 int AttributeForm::_opId = 0; // start counter at 0
2927 const char* AttributeForm::_ins_cost = "ins_cost"; // required name
2928 const char* AttributeForm::_op_cost = "op_cost"; // required name
2929
2930 AttributeForm::AttributeForm(char *attr, int type, char *attrdef)
2931 : Form(Form::ATTR), _attrname(attr), _atype(type), _attrdef(attrdef) {
2932 if (type==OP_ATTR) {
2933 id = ++_opId;
2934 }
2935 else if (type==INS_ATTR) {
2936 id = ++_insId;
2937 }
2938 else assert( false,"");
2939 }
2940 AttributeForm::~AttributeForm() {
2941 }
2942
2943 // Dynamic type check
2944 AttributeForm *AttributeForm::is_attribute() const {
2945 return (AttributeForm*)this;
2946 }
2947
2948
2949 // inlined // int AttributeForm::type() { return id;}
2950
2951 void AttributeForm::dump() {
2952 output(stderr);
2953 }
2954
2955 void AttributeForm::output(FILE *fp) {
2956 if( _attrname && _attrdef ) {
2957 fprintf(fp,"\n// AttributeForm \nstatic const int %s = %s;\n",
2958 _attrname, _attrdef);
2959 }
2960 else {
2961 fprintf(fp,"\n// AttributeForm missing name %s or definition %s\n",
2962 (_attrname?_attrname:""), (_attrdef?_attrdef:"") );
2963 }
2964 }
2965
2966 //------------------------------Component--------------------------------------
2967 Component::Component(const char *name, const char *type, int usedef)
2968 : _name(name), _type(type), _usedef(usedef) {
2969 _ftype = Form::COMP;
2970 }
2971 Component::~Component() {
2972 }
2973
2974 // True if this component is equal to the parameter.
2975 bool Component::is(int use_def_kill_enum) const {
2976 return (_usedef == use_def_kill_enum ? true : false);
2977 }
2978 // True if this component is used/def'd/kill'd as the parameter suggests.
2979 bool Component::isa(int use_def_kill_enum) const {
2980 return (_usedef & use_def_kill_enum) == use_def_kill_enum;
2981 }
2982
2983 // Extend this component with additional use/def/kill behavior
2984 int Component::promote_use_def_info(int new_use_def) {
2985 _usedef |= new_use_def;
2986
2987 return _usedef;
2988 }
2989
2990 // Check the base type of this component, if it has one
2991 const char *Component::base_type(FormDict &globals) {
2992 const Form *frm = globals[_type];
2993 if (frm == nullptr) return nullptr;
2994 OperandForm *op = frm->is_operand();
2995 if (op == nullptr) return nullptr;
2996 if (op->ideal_only()) return op->_ident;
2997 return (char *)op->ideal_type(globals);
2998 }
2999
3000 void Component::dump() {
3001 output(stderr);
3002 }
3003
3004 void Component::output(FILE *fp) {
3005 fprintf(fp,"Component:"); // Write to output files
3006 fprintf(fp, " name = %s", _name);
3007 fprintf(fp, ", type = %s", _type);
3008 assert(_usedef != 0, "unknown effect");
3009 fprintf(fp, ", use/def = %s\n", getUsedefName());
3010 }
3011
3012
3013 //------------------------------ComponentList---------------------------------
3014 ComponentList::ComponentList() : NameList(), _matchcnt(0) {
3015 }
3016 ComponentList::~ComponentList() {
3017 // // This list may not own its elements if copied via assignment
3018 // Component *component;
3019 // for (reset(); (component = iter()) != nullptr;) {
3020 // delete component;
3021 // }
3022 }
3023
3024 void ComponentList::insert(Component *component, bool mflag) {
3025 NameList::addName((char *)component);
3026 if(mflag) _matchcnt++;
3027 }
3028 void ComponentList::insert(const char *name, const char *opType, int usedef,
3029 bool mflag) {
3030 Component * component = new Component(name, opType, usedef);
3031 insert(component, mflag);
3032 }
3033 Component *ComponentList::current() { return (Component*)NameList::current(); }
3034 Component *ComponentList::iter() { return (Component*)NameList::iter(); }
3035 Component *ComponentList::match_iter() {
3036 if(_iter < _matchcnt) return (Component*)NameList::iter();
3037 return nullptr;
3038 }
3039 Component *ComponentList::post_match_iter() {
3040 Component *comp = iter();
3041 // At end of list?
3042 if ( comp == nullptr ) {
3043 return comp;
3044 }
3045 // In post-match components?
3046 if (_iter > match_count()-1) {
3047 return comp;
3048 }
3049
3050 return post_match_iter();
3051 }
3052
3053 void ComponentList::reset() { NameList::reset(); }
3054 int ComponentList::count() { return NameList::count(); }
3055
3056 Component *ComponentList::operator[](int position) {
3057 // Shortcut complete iteration if there are not enough entries
3058 if (position >= count()) return nullptr;
3059
3060 int index = 0;
3061 Component *component = nullptr;
3062 for (reset(); (component = iter()) != nullptr;) {
3063 if (index == position) {
3064 return component;
3065 }
3066 ++index;
3067 }
3068
3069 return nullptr;
3070 }
3071
3072 const Component *ComponentList::search(const char *name) {
3073 PreserveIter pi(this);
3074 reset();
3075 for( Component *comp = nullptr; ((comp = iter()) != nullptr); ) {
3076 if( strcmp(comp->_name,name) == 0 ) return comp;
3077 }
3078
3079 return nullptr;
3080 }
3081
3082 // Return number of USEs + number of DEFs
3083 // When there are no components, or the first component is a USE,
3084 // then we add '1' to hold a space for the 'result' operand.
3085 int ComponentList::num_operands() {
3086 PreserveIter pi(this);
3087 uint count = 1; // result operand
3088 uint position = 0;
3089
3090 Component *component = nullptr;
3091 for( reset(); (component = iter()) != nullptr; ++position ) {
3092 if( component->isa(Component::USE) ||
3093 ( position == 0 && (! component->isa(Component::DEF))) ) {
3094 ++count;
3095 }
3096 }
3097
3098 return count;
3099 }
3100
3101 // Return zero-based position of operand 'name' in list; -1 if not in list.
3102 // if parameter 'usedef' is ::USE, it will match USE, USE_DEF, ...
3103 int ComponentList::operand_position(const char *name, int usedef, Form *fm) {
3104 PreserveIter pi(this);
3105 int position = 0;
3106 int num_opnds = num_operands();
3107 Component *component;
3108 Component* preceding_non_use = nullptr;
3109 Component* first_def = nullptr;
3110 for (reset(); (component = iter()) != nullptr; ++position) {
3111 // When the first component is not a DEF,
3112 // leave space for the result operand!
3113 if ( position==0 && (! component->isa(Component::DEF)) ) {
3114 ++position;
3115 ++num_opnds;
3116 }
3117 if (strcmp(name, component->_name)==0 && (component->isa(usedef))) {
3118 // When the first entry in the component list is a DEF and a USE
3119 // Treat them as being separate, a DEF first, then a USE
3120 if( position==0
3121 && usedef==Component::USE && component->isa(Component::DEF) ) {
3122 assert(position+1 < num_opnds, "advertised index in bounds");
3123 return position+1;
3124 } else {
3125 if( preceding_non_use && strcmp(component->_name, preceding_non_use->_name) ) {
3126 fprintf(stderr, "the name '%s(%s)' should not precede the name '%s(%s)'",
3127 preceding_non_use->_name, preceding_non_use->getUsedefName(),
3128 name, component->getUsedefName());
3129 if (fm && fm->is_instruction()) fprintf(stderr, "in form '%s'", fm->is_instruction()->_ident);
3130 if (fm && fm->is_operand()) fprintf(stderr, "in form '%s'", fm->is_operand()->_ident);
3131 fprintf(stderr, "\n");
3132 }
3133 if( position >= num_opnds ) {
3134 fprintf(stderr, "the name '%s' is too late in its name list", name);
3135 if (fm && fm->is_instruction()) fprintf(stderr, "in form '%s'", fm->is_instruction()->_ident);
3136 if (fm && fm->is_operand()) fprintf(stderr, "in form '%s'", fm->is_operand()->_ident);
3137 fprintf(stderr, "\n");
3138 }
3139 assert(position < num_opnds, "advertised index in bounds");
3140 return position;
3141 }
3142 }
3143 if( component->isa(Component::DEF)
3144 && component->isa(Component::USE) ) {
3145 ++position;
3146 if( position != 1 ) --position; // only use two slots for the 1st USE_DEF
3147 }
3148 if( component->isa(Component::DEF) && !first_def ) {
3149 first_def = component;
3150 }
3151 if( !component->isa(Component::USE) && component != first_def ) {
3152 preceding_non_use = component;
3153 } else if( preceding_non_use && !strcmp(component->_name, preceding_non_use->_name) ) {
3154 preceding_non_use = nullptr;
3155 }
3156 }
3157 return Not_in_list;
3158 }
3159
3160 // Find position for this name, regardless of use/def information
3161 int ComponentList::operand_position(const char *name) {
3162 PreserveIter pi(this);
3163 int position = 0;
3164 Component *component;
3165 for (reset(); (component = iter()) != nullptr; ++position) {
3166 // When the first component is not a DEF,
3167 // leave space for the result operand!
3168 if ( position==0 && (! component->isa(Component::DEF)) ) {
3169 ++position;
3170 }
3171 if (strcmp(name, component->_name)==0) {
3172 return position;
3173 }
3174 if( component->isa(Component::DEF)
3175 && component->isa(Component::USE) ) {
3176 ++position;
3177 if( position != 1 ) --position; // only use two slots for the 1st USE_DEF
3178 }
3179 }
3180 return Not_in_list;
3181 }
3182
3183 int ComponentList::operand_position_format(const char *name, Form *fm) {
3184 PreserveIter pi(this);
3185 int first_position = operand_position(name);
3186 int use_position = operand_position(name, Component::USE, fm);
3187
3188 return ((first_position < use_position) ? use_position : first_position);
3189 }
3190
3191 int ComponentList::label_position() {
3192 PreserveIter pi(this);
3193 int position = 0;
3194 reset();
3195 for( Component *comp; (comp = iter()) != nullptr; ++position) {
3196 // When the first component is not a DEF,
3197 // leave space for the result operand!
3198 if ( position==0 && (! comp->isa(Component::DEF)) ) {
3199 ++position;
3200 }
3201 if (strcmp(comp->_type, "label")==0) {
3202 return position;
3203 }
3204 if( comp->isa(Component::DEF)
3205 && comp->isa(Component::USE) ) {
3206 ++position;
3207 if( position != 1 ) --position; // only use two slots for the 1st USE_DEF
3208 }
3209 }
3210
3211 return -1;
3212 }
3213
3214 int ComponentList::method_position() {
3215 PreserveIter pi(this);
3216 int position = 0;
3217 reset();
3218 for( Component *comp; (comp = iter()) != nullptr; ++position) {
3219 // When the first component is not a DEF,
3220 // leave space for the result operand!
3221 if ( position==0 && (! comp->isa(Component::DEF)) ) {
3222 ++position;
3223 }
3224 if (strcmp(comp->_type, "method")==0) {
3225 return position;
3226 }
3227 if( comp->isa(Component::DEF)
3228 && comp->isa(Component::USE) ) {
3229 ++position;
3230 if( position != 1 ) --position; // only use two slots for the 1st USE_DEF
3231 }
3232 }
3233
3234 return -1;
3235 }
3236
3237 void ComponentList::dump() { output(stderr); }
3238
3239 void ComponentList::output(FILE *fp) {
3240 PreserveIter pi(this);
3241 fprintf(fp, "\n");
3242 Component *component;
3243 for (reset(); (component = iter()) != nullptr;) {
3244 component->output(fp);
3245 }
3246 fprintf(fp, "\n");
3247 }
3248
3249 //------------------------------MatchNode--------------------------------------
3250 MatchNode::MatchNode(ArchDesc &ad, const char *result, const char *mexpr,
3251 const char *opType, MatchNode *lChild, MatchNode *rChild)
3252 : _AD(ad), _result(result), _name(mexpr), _opType(opType),
3253 _lChild(lChild), _rChild(rChild), _internalop(nullptr), _numleaves(0),
3254 _commutative_id(0) {
3255 _numleaves = (lChild ? lChild->_numleaves : 0)
3256 + (rChild ? rChild->_numleaves : 0);
3257 }
3258
3259 MatchNode::MatchNode(ArchDesc &ad, MatchNode& mnode)
3260 : _AD(ad), _result(mnode._result), _name(mnode._name),
3261 _opType(mnode._opType), _lChild(mnode._lChild), _rChild(mnode._rChild),
3262 _internalop(nullptr), _numleaves(mnode._numleaves),
3263 _commutative_id(mnode._commutative_id) {
3264 }
3265
3266 MatchNode::MatchNode(ArchDesc &ad, MatchNode& mnode, int clone)
3267 : _AD(ad), _result(mnode._result), _name(mnode._name),
3268 _opType(mnode._opType),
3269 _internalop(nullptr), _numleaves(mnode._numleaves),
3270 _commutative_id(mnode._commutative_id) {
3271 if (mnode._lChild) {
3272 _lChild = new MatchNode(ad, *mnode._lChild, clone);
3273 } else {
3274 _lChild = nullptr;
3275 }
3276 if (mnode._rChild) {
3277 _rChild = new MatchNode(ad, *mnode._rChild, clone);
3278 } else {
3279 _rChild = nullptr;
3280 }
3281 }
3282
3283 MatchNode::~MatchNode() {
3284 // // This node may not own its children if copied via assignment
3285 // if( _lChild ) delete _lChild;
3286 // if( _rChild ) delete _rChild;
3287 }
3288
3289 bool MatchNode::find_type(const char *type, int &position) const {
3290 if ( (_lChild != nullptr) && (_lChild->find_type(type, position)) ) return true;
3291 if ( (_rChild != nullptr) && (_rChild->find_type(type, position)) ) return true;
3292
3293 if (strcmp(type,_opType)==0) {
3294 return true;
3295 } else {
3296 ++position;
3297 }
3298 return false;
3299 }
3300
3301 // Recursive call collecting info on top-level operands, not transitive.
3302 // Implementation does not modify state of internal structures.
3303 void MatchNode::append_components(FormDict& locals, ComponentList& components,
3304 bool def_flag) const {
3305 int usedef = def_flag ? Component::DEF : Component::USE;
3306 FormDict &globals = _AD.globalNames();
3307
3308 assert (_name != nullptr, "MatchNode::build_components encountered empty node\n");
3309 // Base case
3310 if (_lChild==nullptr && _rChild==nullptr) {
3311 // If _opType is not an operation, do not build a component for it #####
3312 const Form *f = globals[_opType];
3313 if( f != nullptr ) {
3314 // Add non-ideals that are operands, operand-classes,
3315 if( ! f->ideal_only()
3316 && (f->is_opclass() || f->is_operand()) ) {
3317 components.insert(_name, _opType, usedef, true);
3318 }
3319 }
3320 return;
3321 }
3322 // Promote results of "Set" to DEF
3323 bool tmpdef_flag = (!strcmp(_opType, "Set")) ? true : false;
3324 if (_lChild) _lChild->append_components(locals, components, tmpdef_flag);
3325 tmpdef_flag = false; // only applies to component immediately following 'Set'
3326 if (_rChild) _rChild->append_components(locals, components, tmpdef_flag);
3327 }
3328
3329 // Find the n'th base-operand in the match node,
3330 // recursively investigates match rules of user-defined operands.
3331 //
3332 // Implementation does not modify state of internal structures since they
3333 // can be shared.
3334 bool MatchNode::base_operand(uint &position, FormDict &globals,
3335 const char * &result, const char * &name,
3336 const char * &opType) const {
3337 assert (_name != nullptr, "MatchNode::base_operand encountered empty node\n");
3338 // Base case
3339 if (_lChild==nullptr && _rChild==nullptr) {
3340 // Check for special case: "Universe", "label"
3341 if (strcmp(_opType,"Universe") == 0 || strcmp(_opType,"label")==0 ) {
3342 if (position == 0) {
3343 result = _result;
3344 name = _name;
3345 opType = _opType;
3346 return 1;
3347 } else {
3348 -- position;
3349 return 0;
3350 }
3351 }
3352
3353 const Form *form = globals[_opType];
3354 MatchNode *matchNode = nullptr;
3355 // Check for user-defined type
3356 if (form) {
3357 // User operand or instruction?
3358 OperandForm *opForm = form->is_operand();
3359 InstructForm *inForm = form->is_instruction();
3360 if ( opForm ) {
3361 matchNode = (MatchNode*)opForm->_matrule;
3362 } else if ( inForm ) {
3363 matchNode = (MatchNode*)inForm->_matrule;
3364 }
3365 }
3366 // if this is user-defined, recurse on match rule
3367 // User-defined operand and instruction forms have a match-rule.
3368 if (matchNode) {
3369 return (matchNode->base_operand(position,globals,result,name,opType));
3370 } else {
3371 // Either not a form, or a system-defined form (no match rule).
3372 if (position==0) {
3373 result = _result;
3374 name = _name;
3375 opType = _opType;
3376 return 1;
3377 } else {
3378 --position;
3379 return 0;
3380 }
3381 }
3382
3383 } else {
3384 // Examine the left child and right child as well
3385 if (_lChild) {
3386 if (_lChild->base_operand(position, globals, result, name, opType))
3387 return 1;
3388 }
3389
3390 if (_rChild) {
3391 if (_rChild->base_operand(position, globals, result, name, opType))
3392 return 1;
3393 }
3394 }
3395
3396 return 0;
3397 }
3398
3399 // Recursive call on all operands' match rules in my match rule.
3400 uint MatchNode::num_consts(FormDict &globals) const {
3401 uint index = 0;
3402 uint num_consts = 0;
3403 const char *result;
3404 const char *name;
3405 const char *opType;
3406
3407 for (uint position = index;
3408 base_operand(position,globals,result,name,opType); position = index) {
3409 ++index;
3410 if( ideal_to_const_type(opType) ) num_consts++;
3411 }
3412
3413 return num_consts;
3414 }
3415
3416 // Recursive call on all operands' match rules in my match rule.
3417 // Constants in match rule subtree with specified type
3418 uint MatchNode::num_consts(FormDict &globals, Form::DataType type) const {
3419 uint index = 0;
3420 uint num_consts = 0;
3421 const char *result;
3422 const char *name;
3423 const char *opType;
3424
3425 for (uint position = index;
3426 base_operand(position,globals,result,name,opType); position = index) {
3427 ++index;
3428 if( ideal_to_const_type(opType) == type ) num_consts++;
3429 }
3430
3431 return num_consts;
3432 }
3433
3434 // Recursive call on all operands' match rules in my match rule.
3435 uint MatchNode::num_const_ptrs(FormDict &globals) const {
3436 return num_consts( globals, Form::idealP );
3437 }
3438
3439 bool MatchNode::sets_result() const {
3440 return ( (strcmp(_name,"Set") == 0) ? true : false );
3441 }
3442
3443 const char *MatchNode::reduce_right(FormDict &globals) const {
3444 // If there is no right reduction, return null.
3445 const char *rightStr = nullptr;
3446
3447 // If we are a "Set", start from the right child.
3448 const MatchNode *const mnode = sets_result() ?
3449 (const MatchNode *)this->_rChild :
3450 (const MatchNode *)this;
3451
3452 // If our right child exists, it is the right reduction
3453 if ( mnode->_rChild ) {
3454 rightStr = mnode->_rChild->_internalop ? mnode->_rChild->_internalop
3455 : mnode->_rChild->_opType;
3456 }
3457 // Else, May be simple chain rule: (Set dst operand_form), rightStr=nullptr;
3458 return rightStr;
3459 }
3460
3461 const char *MatchNode::reduce_left(FormDict &globals) const {
3462 // If there is no left reduction, return null.
3463 const char *leftStr = nullptr;
3464
3465 // If we are a "Set", start from the right child.
3466 const MatchNode *const mnode = sets_result() ?
3467 (const MatchNode *)this->_rChild :
3468 (const MatchNode *)this;
3469
3470 // If our left child exists, it is the left reduction
3471 if ( mnode->_lChild ) {
3472 leftStr = mnode->_lChild->_internalop ? mnode->_lChild->_internalop
3473 : mnode->_lChild->_opType;
3474 } else {
3475 // May be simple chain rule: (Set dst operand_form_source)
3476 if ( sets_result() ) {
3477 OperandForm *oper = globals[mnode->_opType]->is_operand();
3478 if( oper ) {
3479 leftStr = mnode->_opType;
3480 }
3481 }
3482 }
3483 return leftStr;
3484 }
3485
3486 //------------------------------count_instr_names------------------------------
3487 // Count occurrences of operands names in the leaves of the instruction
3488 // match rule.
3489 void MatchNode::count_instr_names( Dict &names ) {
3490 if( _lChild ) _lChild->count_instr_names(names);
3491 if( _rChild ) _rChild->count_instr_names(names);
3492 if( !_lChild && !_rChild ) {
3493 uintptr_t cnt = (uintptr_t)names[_name];
3494 cnt++; // One more name found
3495 names.Insert(_name,(void*)cnt);
3496 }
3497 }
3498
3499 //------------------------------build_instr_pred-------------------------------
3500 // Build a path to 'name' in buf. Actually only build if cnt is zero, so we
3501 // can skip some leading instances of 'name'.
3502 int MatchNode::build_instr_pred( char *buf, const char *name, int cnt, int path_bitmask, int level) {
3503 if( _lChild ) {
3504 cnt = _lChild->build_instr_pred(buf, name, cnt, path_bitmask, level+1);
3505 if( cnt < 0 ) {
3506 return cnt; // Found it, all done
3507 }
3508 }
3509 if( _rChild ) {
3510 path_bitmask |= 1 << level;
3511 cnt = _rChild->build_instr_pred( buf, name, cnt, path_bitmask, level+1);
3512 if( cnt < 0 ) {
3513 return cnt; // Found it, all done
3514 }
3515 }
3516 if( !_lChild && !_rChild ) { // Found a leaf
3517 // Wrong name? Give up...
3518 if( strcmp(name,_name) ) return cnt;
3519 if( !cnt ) {
3520 for(int i = 0; i < level; i++) {
3521 int kid = path_bitmask & (1 << i);
3522 if (0 == kid) {
3523 strcpy( buf, "_kids[0]->" );
3524 } else {
3525 strcpy( buf, "_kids[1]->" );
3526 }
3527 buf += 10;
3528 }
3529 strcpy( buf, "_leaf" );
3530 }
3531 return cnt-1;
3532 }
3533 return cnt;
3534 }
3535
3536
3537 //------------------------------build_internalop-------------------------------
3538 // Build string representation of subtree
3539 void MatchNode::build_internalop( ) {
3540 char *iop, *subtree;
3541 const char *lstr, *rstr;
3542 // Build string representation of subtree
3543 // Operation lchildType rchildType
3544 int len = (int)strlen(_opType) + 4;
3545 lstr = (_lChild) ? ((_lChild->_internalop) ?
3546 _lChild->_internalop : _lChild->_opType) : "";
3547 rstr = (_rChild) ? ((_rChild->_internalop) ?
3548 _rChild->_internalop : _rChild->_opType) : "";
3549 len += (int)strlen(lstr) + (int)strlen(rstr);
3550 subtree = (char *)AdlAllocateHeap(len);
3551 snprintf_checked(subtree, len, "_%s_%s_%s", _opType, lstr, rstr);
3552 // Hash the subtree string in _internalOps; if a name exists, use it
3553 iop = (char *)_AD._internalOps[subtree];
3554 // Else create a unique name, and add it to the hash table
3555 if (iop == nullptr) {
3556 iop = subtree;
3557 _AD._internalOps.Insert(subtree, iop);
3558 _AD._internalOpNames.addName(iop);
3559 _AD._internalMatch.Insert(iop, this);
3560 }
3561 // Add the internal operand name to the MatchNode
3562 _internalop = iop;
3563 _result = iop;
3564 }
3565
3566
3567 void MatchNode::dump() {
3568 output(stderr);
3569 }
3570
3571 void MatchNode::output(FILE *fp) {
3572 if (_lChild==nullptr && _rChild==nullptr) {
3573 fprintf(fp," %s",_name); // operand
3574 }
3575 else {
3576 fprintf(fp," (%s ",_name); // " (opcodeName "
3577 if(_lChild) _lChild->output(fp); // left operand
3578 if(_rChild) _rChild->output(fp); // right operand
3579 fprintf(fp,")"); // ")"
3580 }
3581 }
3582
3583 void MatchNode::forms_do(FormClosure *f) {
3584 f->do_form_by_name(_name);
3585 if (_lChild) f->do_form(_lChild);
3586 if (_rChild) f->do_form(_rChild);
3587 }
3588
3589 int MatchNode::needs_ideal_memory_edge(FormDict &globals) const {
3590 static const char *needs_ideal_memory_list[] = {
3591 "StoreI","StoreL","StoreLSpecial","StoreP","StoreN","StoreNKlass","StoreD","StoreF" ,
3592 "StoreB","StoreC","Store" ,"StoreFP",
3593 "LoadI", "LoadL", "LoadP" ,"LoadN", "LoadD" ,"LoadF" ,
3594 "LoadB" , "LoadUB", "LoadUS" ,"LoadS" ,"Load" ,
3595 "StoreVector", "LoadVector", "LoadVectorMasked", "StoreVectorMasked",
3596 "LoadVectorGather", "StoreVectorScatter", "LoadVectorGatherMasked", "StoreVectorScatterMasked",
3597 "LoadRange", "LoadKlass", "LoadNKlass", "LoadL_unaligned", "LoadD_unaligned",
3598 "CompareAndSwapB", "CompareAndSwapS", "CompareAndSwapI", "CompareAndSwapL", "CompareAndSwapP", "CompareAndSwapN",
3599 "WeakCompareAndSwapB", "WeakCompareAndSwapS", "WeakCompareAndSwapI", "WeakCompareAndSwapL", "WeakCompareAndSwapP", "WeakCompareAndSwapN",
3600 "CompareAndExchangeB", "CompareAndExchangeS", "CompareAndExchangeI", "CompareAndExchangeL", "CompareAndExchangeP", "CompareAndExchangeN",
3601 "GetAndSetB", "GetAndSetS", "GetAndAddI", "GetAndSetI", "GetAndSetP",
3602 "GetAndAddB", "GetAndAddS", "GetAndAddL", "GetAndSetL", "GetAndSetN",
3603 "ClearArray"
3604 };
3605 int cnt = sizeof(needs_ideal_memory_list)/sizeof(char*);
3606 if( strcmp(_opType,"PrefetchAllocation")==0 )
3607 return 1;
3608 if( strcmp(_opType,"CacheWB")==0 )
3609 return 1;
3610 if( strcmp(_opType,"CacheWBPreSync")==0 )
3611 return 1;
3612 if( strcmp(_opType,"CacheWBPostSync")==0 )
3613 return 1;
3614 if( _lChild ) {
3615 const char *opType = _lChild->_opType;
3616 for( int i=0; i<cnt; i++ )
3617 if( strcmp(opType,needs_ideal_memory_list[i]) == 0 )
3618 return 1;
3619 if( _lChild->needs_ideal_memory_edge(globals) )
3620 return 1;
3621 }
3622 if( _rChild ) {
3623 const char *opType = _rChild->_opType;
3624 for( int i=0; i<cnt; i++ )
3625 if( strcmp(opType,needs_ideal_memory_list[i]) == 0 )
3626 return 1;
3627 if( _rChild->needs_ideal_memory_edge(globals) )
3628 return 1;
3629 }
3630
3631 return 0;
3632 }
3633
3634 // TRUE if defines a derived oop, and so needs a base oop edge present
3635 // post-matching.
3636 int MatchNode::needs_base_oop_edge() const {
3637 if( !strcmp(_opType,"AddP") ) return 1;
3638 if( strcmp(_opType,"Set") ) return 0;
3639 return !strcmp(_rChild->_opType,"AddP");
3640 }
3641
3642 int InstructForm::needs_base_oop_edge(FormDict &globals) const {
3643 if( is_simple_chain_rule(globals) ) {
3644 const char *src = _matrule->_rChild->_opType;
3645 OperandForm *src_op = globals[src]->is_operand();
3646 assert( src_op, "Not operand class of chain rule" );
3647 return src_op->_matrule ? src_op->_matrule->needs_base_oop_edge() : 0;
3648 } // Else check instruction
3649
3650 return _matrule ? _matrule->needs_base_oop_edge() : 0;
3651 }
3652
3653
3654
3655 //-------------------------cisc spilling methods-------------------------------
3656 // helper routines and methods for detecting cisc-spilling instructions
3657 //-------------------------cisc_spill_merge------------------------------------
3658 int MatchNode::cisc_spill_merge(int left_spillable, int right_spillable) {
3659 int cisc_spillable = Maybe_cisc_spillable;
3660
3661 // Combine results of left and right checks
3662 if( (left_spillable == Maybe_cisc_spillable) && (right_spillable == Maybe_cisc_spillable) ) {
3663 // neither side is spillable, nor prevents cisc spilling
3664 cisc_spillable = Maybe_cisc_spillable;
3665 }
3666 else if( (left_spillable == Maybe_cisc_spillable) && (right_spillable > Maybe_cisc_spillable) ) {
3667 // right side is spillable
3668 cisc_spillable = right_spillable;
3669 }
3670 else if( (right_spillable == Maybe_cisc_spillable) && (left_spillable > Maybe_cisc_spillable) ) {
3671 // left side is spillable
3672 cisc_spillable = left_spillable;
3673 }
3674 else if( (left_spillable == Not_cisc_spillable) || (right_spillable == Not_cisc_spillable) ) {
3675 // left or right prevents cisc spilling this instruction
3676 cisc_spillable = Not_cisc_spillable;
3677 }
3678 else {
3679 // Only allow one to spill
3680 cisc_spillable = Not_cisc_spillable;
3681 }
3682
3683 return cisc_spillable;
3684 }
3685
3686 //-------------------------root_ops_match--------------------------------------
3687 bool static root_ops_match(FormDict &globals, const char *op1, const char *op2) {
3688 // Base Case: check that the current operands/operations match
3689 assert( op1, "Must have op's name");
3690 assert( op2, "Must have op's name");
3691 const Form *form1 = globals[op1];
3692 const Form *form2 = globals[op2];
3693
3694 return (form1 == form2);
3695 }
3696
3697 //-------------------------cisc_spill_match_node-------------------------------
3698 // Recursively check two MatchRules for legal conversion via cisc-spilling
3699 int MatchNode::cisc_spill_match(FormDict& globals, RegisterForm* registers, MatchNode* mRule2, const char* &operand, const char* ®_type) {
3700 int cisc_spillable = Maybe_cisc_spillable;
3701 int left_spillable = Maybe_cisc_spillable;
3702 int right_spillable = Maybe_cisc_spillable;
3703
3704 // Check that each has same number of operands at this level
3705 if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) )
3706 return Not_cisc_spillable;
3707
3708 // Base Case: check that the current operands/operations match
3709 // or are CISC spillable
3710 assert( _opType, "Must have _opType");
3711 assert( mRule2->_opType, "Must have _opType");
3712 const Form *form = globals[_opType];
3713 const Form *form2 = globals[mRule2->_opType];
3714 if( form == form2 ) {
3715 cisc_spillable = Maybe_cisc_spillable;
3716 } else {
3717 const InstructForm *form2_inst = form2 ? form2->is_instruction() : nullptr;
3718 const char *name_left = mRule2->_lChild ? mRule2->_lChild->_opType : nullptr;
3719 const char *name_right = mRule2->_rChild ? mRule2->_rChild->_opType : nullptr;
3720 DataType data_type = Form::none;
3721 if (form->is_operand()) {
3722 // Make sure the loadX matches the type of the reg
3723 data_type = form->ideal_to_Reg_type(form->is_operand()->ideal_type(globals));
3724 }
3725 // Detect reg vs (loadX memory)
3726 if( form->is_cisc_reg(globals)
3727 && form2_inst
3728 && data_type != Form::none
3729 && (is_load_from_memory(mRule2->_opType) == data_type) // reg vs. (load memory)
3730 && (name_left != nullptr) // NOT (load)
3731 && (name_right == nullptr) ) { // NOT (load memory foo)
3732 const Form *form2_left = globals[name_left];
3733 if( form2_left && form2_left->is_cisc_mem(globals) ) {
3734 cisc_spillable = Is_cisc_spillable;
3735 operand = _name;
3736 reg_type = _result;
3737 return Is_cisc_spillable;
3738 } else {
3739 cisc_spillable = Not_cisc_spillable;
3740 }
3741 }
3742 // Detect reg vs memory
3743 else if (form->is_cisc_reg(globals) && form2 != nullptr && form2->is_cisc_mem(globals)) {
3744 cisc_spillable = Is_cisc_spillable;
3745 operand = _name;
3746 reg_type = _result;
3747 return Is_cisc_spillable;
3748 } else {
3749 cisc_spillable = Not_cisc_spillable;
3750 }
3751 }
3752
3753 // If cisc is still possible, check rest of tree
3754 if( cisc_spillable == Maybe_cisc_spillable ) {
3755 // Check that each has same number of operands at this level
3756 if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) ) return Not_cisc_spillable;
3757
3758 // Check left operands
3759 if( (_lChild == nullptr) && (mRule2->_lChild == nullptr) ) {
3760 left_spillable = Maybe_cisc_spillable;
3761 } else if (_lChild != nullptr) {
3762 left_spillable = _lChild->cisc_spill_match(globals, registers, mRule2->_lChild, operand, reg_type);
3763 }
3764
3765 // Check right operands
3766 if( (_rChild == nullptr) && (mRule2->_rChild == nullptr) ) {
3767 right_spillable = Maybe_cisc_spillable;
3768 } else if (_rChild != nullptr) {
3769 right_spillable = _rChild->cisc_spill_match(globals, registers, mRule2->_rChild, operand, reg_type);
3770 }
3771
3772 // Combine results of left and right checks
3773 cisc_spillable = cisc_spill_merge(left_spillable, right_spillable);
3774 }
3775
3776 return cisc_spillable;
3777 }
3778
3779 //---------------------------cisc_spill_match_rule------------------------------
3780 // Recursively check two MatchRules for legal conversion via cisc-spilling
3781 // This method handles the root of Match tree,
3782 // general recursive checks done in MatchNode
3783 int MatchRule::matchrule_cisc_spill_match(FormDict& globals, RegisterForm* registers,
3784 MatchRule* mRule2, const char* &operand,
3785 const char* ®_type) {
3786 int cisc_spillable = Maybe_cisc_spillable;
3787 int left_spillable = Maybe_cisc_spillable;
3788 int right_spillable = Maybe_cisc_spillable;
3789
3790 // Check that each sets a result
3791 if( !(sets_result() && mRule2->sets_result()) ) return Not_cisc_spillable;
3792 // Check that each has same number of operands at this level
3793 if( (_lChild && !(mRule2->_lChild)) || (_rChild && !(mRule2->_rChild)) ) return Not_cisc_spillable;
3794
3795 // Check left operands: at root, must be target of 'Set'
3796 if( (_lChild == nullptr) || (mRule2->_lChild == nullptr) ) {
3797 left_spillable = Not_cisc_spillable;
3798 } else {
3799 // Do not support cisc-spilling instruction's target location
3800 if( root_ops_match(globals, _lChild->_opType, mRule2->_lChild->_opType) ) {
3801 left_spillable = Maybe_cisc_spillable;
3802 } else {
3803 left_spillable = Not_cisc_spillable;
3804 }
3805 }
3806
3807 // Check right operands: recursive walk to identify reg->mem operand
3808 if (_rChild == nullptr) {
3809 if (mRule2->_rChild == nullptr) {
3810 right_spillable = Maybe_cisc_spillable;
3811 } else {
3812 assert(0, "_rChild should not be null");
3813 }
3814 } else {
3815 right_spillable = _rChild->cisc_spill_match(globals, registers, mRule2->_rChild, operand, reg_type);
3816 }
3817
3818 // Combine results of left and right checks
3819 cisc_spillable = cisc_spill_merge(left_spillable, right_spillable);
3820
3821 return cisc_spillable;
3822 }
3823
3824 //----------------------------- equivalent ------------------------------------
3825 // Recursively check to see if two match rules are equivalent.
3826 // This rule handles the root.
3827 bool MatchRule::equivalent(FormDict &globals, MatchNode *mRule2) {
3828 // Check that each sets a result
3829 if (sets_result() != mRule2->sets_result()) {
3830 return false;
3831 }
3832
3833 // Check that the current operands/operations match
3834 assert( _opType, "Must have _opType");
3835 assert( mRule2->_opType, "Must have _opType");
3836 const Form *form = globals[_opType];
3837 const Form *form2 = globals[mRule2->_opType];
3838 if( form != form2 ) {
3839 return false;
3840 }
3841
3842 if (_lChild ) {
3843 if( !_lChild->equivalent(globals, mRule2->_lChild) )
3844 return false;
3845 } else if (mRule2->_lChild) {
3846 return false; // I have null left child, mRule2 has non-null left child.
3847 }
3848
3849 if (_rChild ) {
3850 if( !_rChild->equivalent(globals, mRule2->_rChild) )
3851 return false;
3852 } else if (mRule2->_rChild) {
3853 return false; // I have null right child, mRule2 has non-null right child.
3854 }
3855
3856 // We've made it through the gauntlet.
3857 return true;
3858 }
3859
3860 //----------------------------- equivalent ------------------------------------
3861 // Recursively check to see if two match rules are equivalent.
3862 // This rule handles the operands.
3863 bool MatchNode::equivalent(FormDict &globals, MatchNode *mNode2) {
3864 if( !mNode2 )
3865 return false;
3866
3867 // Check that the current operands/operations match
3868 assert( _opType, "Must have _opType");
3869 assert( mNode2->_opType, "Must have _opType");
3870 const Form *form = globals[_opType];
3871 const Form *form2 = globals[mNode2->_opType];
3872 if( form != form2 ) {
3873 return false;
3874 }
3875
3876 // Check that their children also match
3877 if (_lChild ) {
3878 if( !_lChild->equivalent(globals, mNode2->_lChild) )
3879 return false;
3880 } else if (mNode2->_lChild) {
3881 return false; // I have null left child, mNode2 has non-null left child.
3882 }
3883
3884 if (_rChild ) {
3885 if( !_rChild->equivalent(globals, mNode2->_rChild) )
3886 return false;
3887 } else if (mNode2->_rChild) {
3888 return false; // I have null right child, mNode2 has non-null right child.
3889 }
3890
3891 // We've made it through the gauntlet.
3892 return true;
3893 }
3894
3895 //-------------------------- count_commutative_op -------------------------------
3896 // Recursively check for commutative operations with subtree operands
3897 // which could be swapped.
3898 void MatchNode::count_commutative_op(int& count) {
3899 static const char *commut_op_list[] = {
3900 "AddI","AddL","AddHF","AddF","AddD",
3901 "AndI","AndL",
3902 "MaxI","MinI","MaxHF","MinHF","MaxF","MinF","MaxD","MinD",
3903 "MulI","MulL","MulHF","MulF","MulD",
3904 "OrI","OrL",
3905 "XorI","XorL"
3906 "UMax","UMin"
3907 };
3908
3909 static const char *commut_vector_op_list[] = {
3910 "AddVB", "AddVS", "AddVI", "AddVL", "AddVHF", "AddVF", "AddVD",
3911 "MulVB", "MulVS", "MulVI", "MulVL", "MulVHF", "MulVF", "MulVD",
3912 "AndV", "OrV", "XorV", "AndVMask", "OrVMask", "XorVMask",
3913 "MaxVHF", "MinVHF", "MaxV", "MinV", "UMax","UMin"
3914 };
3915
3916 if (_lChild && _rChild && (_lChild->_lChild || _rChild->_lChild)) {
3917 // Don't swap if right operand is an immediate constant.
3918 bool is_const = false;
3919 if (_rChild->_lChild == nullptr && _rChild->_rChild == nullptr) {
3920 FormDict &globals = _AD.globalNames();
3921 const Form *form = globals[_rChild->_opType];
3922 if (form) {
3923 OperandForm *oper = form->is_operand();
3924 if (oper && oper->interface_type(globals) == Form::constant_interface)
3925 is_const = true;
3926 }
3927 }
3928
3929 if (!is_const) {
3930 int scalar_cnt = sizeof(commut_op_list)/sizeof(char*);
3931 int vector_cnt = sizeof(commut_vector_op_list)/sizeof(char*);
3932 bool matched = false;
3933
3934 // Check the commutative vector op first. It's noncommutative if
3935 // the current node is a masked vector op, since a mask value
3936 // is added to the original vector node's input list and the original
3937 // first two inputs are packed into one BinaryNode. So don't swap
3938 // if one of the operands is a BinaryNode.
3939 for (int i = 0; i < vector_cnt; i++) {
3940 if (strcmp(_opType, commut_vector_op_list[i]) == 0) {
3941 if (strcmp(_lChild->_opType, "Binary") != 0 &&
3942 strcmp(_rChild->_opType, "Binary") != 0) {
3943 count++;
3944 _commutative_id = count; // id should be > 0
3945 }
3946 matched = true;
3947 break;
3948 }
3949 }
3950
3951 // Then check the scalar op if the current op is not in
3952 // the commut_vector_op_list.
3953 if (!matched) {
3954 for (int i = 0; i < scalar_cnt; i++) {
3955 if (strcmp(_opType, commut_op_list[i]) == 0) {
3956 count++;
3957 _commutative_id = count; // id should be > 0
3958 break;
3959 }
3960 }
3961 }
3962 }
3963 }
3964 if (_lChild)
3965 _lChild->count_commutative_op(count);
3966 if (_rChild)
3967 _rChild->count_commutative_op(count);
3968 }
3969
3970 //-------------------------- swap_commutative_op ------------------------------
3971 // Recursively swap specified commutative operation with subtree operands.
3972 void MatchNode::swap_commutative_op(bool atroot, int id) {
3973 if( _commutative_id == id ) { // id should be > 0
3974 assert(_lChild && _rChild && (_lChild->_lChild || _rChild->_lChild ),
3975 "not swappable operation");
3976 MatchNode* tmp = _lChild;
3977 _lChild = _rChild;
3978 _rChild = tmp;
3979 // Don't exit here since we need to build internalop.
3980 }
3981
3982 bool is_set = ( strcmp(_opType, "Set") == 0 );
3983 if( _lChild )
3984 _lChild->swap_commutative_op(is_set, id);
3985 if( _rChild )
3986 _rChild->swap_commutative_op(is_set, id);
3987
3988 // If not the root, reduce this subtree to an internal operand
3989 if( !atroot && (_lChild || _rChild) ) {
3990 build_internalop();
3991 }
3992 }
3993
3994 //-------------------------- swap_commutative_op ------------------------------
3995 // Recursively swap specified commutative operation with subtree operands.
3996 void MatchRule::matchrule_swap_commutative_op(const char* instr_ident, int count, int& match_rules_cnt) {
3997 assert(match_rules_cnt < 100," too many match rule clones");
3998 // Clone
3999 MatchRule* clone = new MatchRule(_AD, this);
4000 // Swap operands of commutative operation
4001 ((MatchNode*)clone)->swap_commutative_op(true, count);
4002 const size_t buf_size = strlen(instr_ident) + 4;
4003 char* buf = (char*) AdlAllocateHeap(buf_size);
4004 snprintf_checked(buf, buf_size, "%s_%d", instr_ident, match_rules_cnt++);
4005 clone->_result = buf;
4006
4007 clone->_next = this->_next;
4008 this-> _next = clone;
4009 if( (--count) > 0 ) {
4010 this-> matchrule_swap_commutative_op(instr_ident, count, match_rules_cnt);
4011 clone->matchrule_swap_commutative_op(instr_ident, count, match_rules_cnt);
4012 }
4013 }
4014
4015 //------------------------------MatchRule--------------------------------------
4016 MatchRule::MatchRule(ArchDesc &ad)
4017 : MatchNode(ad), _depth(0), _construct(nullptr), _numchilds(0) {
4018 _next = nullptr;
4019 }
4020
4021 MatchRule::MatchRule(ArchDesc &ad, MatchRule* mRule)
4022 : MatchNode(ad, *mRule, 0), _depth(mRule->_depth),
4023 _construct(mRule->_construct), _numchilds(mRule->_numchilds) {
4024 _next = nullptr;
4025 }
4026
4027 MatchRule::MatchRule(ArchDesc &ad, MatchNode* mroot, int depth, char *cnstr,
4028 int numleaves)
4029 : MatchNode(ad,*mroot), _depth(depth), _construct(cnstr),
4030 _numchilds(0) {
4031 _next = nullptr;
4032 mroot->_lChild = nullptr;
4033 mroot->_rChild = nullptr;
4034 delete mroot;
4035 _numleaves = numleaves;
4036 _numchilds = (_lChild ? 1 : 0) + (_rChild ? 1 : 0);
4037 }
4038 MatchRule::~MatchRule() {
4039 }
4040
4041 // Recursive call collecting info on top-level operands, not transitive.
4042 // Implementation does not modify state of internal structures.
4043 void MatchRule::append_components(FormDict& locals, ComponentList& components, bool def_flag) const {
4044 assert (_name != nullptr, "MatchNode::build_components encountered empty node\n");
4045
4046 MatchNode::append_components(locals, components,
4047 false /* not necessarily a def */);
4048 }
4049
4050 // Recursive call on all operands' match rules in my match rule.
4051 // Implementation does not modify state of internal structures since they
4052 // can be shared.
4053 // The MatchNode that is called first treats its
4054 bool MatchRule::base_operand(uint &position0, FormDict &globals,
4055 const char *&result, const char * &name,
4056 const char * &opType)const{
4057 uint position = position0;
4058
4059 return (MatchNode::base_operand( position, globals, result, name, opType));
4060 }
4061
4062
4063 bool MatchRule::is_base_register(FormDict &globals) const {
4064 uint position = 1;
4065 const char *result = nullptr;
4066 const char *name = nullptr;
4067 const char *opType = nullptr;
4068 if (!base_operand(position, globals, result, name, opType)) {
4069 position = 0;
4070 if( base_operand(position, globals, result, name, opType) &&
4071 (strcmp(opType,"RegI")==0 ||
4072 strcmp(opType,"RegP")==0 ||
4073 strcmp(opType,"RegN")==0 ||
4074 strcmp(opType,"RegL")==0 ||
4075 strcmp(opType,"RegF")==0 ||
4076 strcmp(opType,"RegD")==0 ||
4077 strcmp(opType,"RegVectMask")==0 ||
4078 strcmp(opType,"VecA")==0 ||
4079 strcmp(opType,"VecS")==0 ||
4080 strcmp(opType,"VecD")==0 ||
4081 strcmp(opType,"VecX")==0 ||
4082 strcmp(opType,"VecY")==0 ||
4083 strcmp(opType,"VecZ")==0 ||
4084 strcmp(opType,"Reg" )==0) ) {
4085 return 1;
4086 }
4087 }
4088 return 0;
4089 }
4090
4091 Form::DataType MatchRule::is_base_constant(FormDict &globals) const {
4092 uint position = 1;
4093 const char *result = nullptr;
4094 const char *name = nullptr;
4095 const char *opType = nullptr;
4096 if (!base_operand(position, globals, result, name, opType)) {
4097 position = 0;
4098 if (base_operand(position, globals, result, name, opType)) {
4099 return ideal_to_const_type(opType);
4100 }
4101 }
4102 return Form::none;
4103 }
4104
4105 bool MatchRule::is_chain_rule(FormDict &globals) const {
4106
4107 // Check for chain rule, and do not generate a match list for it
4108 if ((_lChild == nullptr) && (_rChild == nullptr) ) {
4109 const Form *form = globals[_opType];
4110 // If this is ideal, then it is a base match, not a chain rule.
4111 if ( form && form->is_operand() && (!form->ideal_only())) {
4112 return true;
4113 }
4114 }
4115 // Check for "Set" form of chain rule, and do not generate a match list
4116 if (_rChild) {
4117 const char *rch = _rChild->_opType;
4118 const Form *form = globals[rch];
4119 if ((!strcmp(_opType,"Set") &&
4120 ((form) && form->is_operand()))) {
4121 return true;
4122 }
4123 }
4124 return false;
4125 }
4126
4127 int MatchRule::is_ideal_copy() const {
4128 if (is_chain_rule(_AD.globalNames()) &&
4129 _lChild && strncmp(_lChild->_opType, "stackSlot", 9) == 0) {
4130 return 1;
4131 }
4132 return 0;
4133 }
4134
4135 int MatchRule::is_expensive() const {
4136 if( _rChild ) {
4137 const char *opType = _rChild->_opType;
4138 if( strcmp(opType,"AtanD")==0 ||
4139 strcmp(opType,"DivD")==0 ||
4140 strcmp(opType,"DivF")==0 ||
4141 strcmp(opType,"DivHF")==0 ||
4142 strcmp(opType,"DivI")==0 ||
4143 strcmp(opType,"Log10D")==0 ||
4144 strcmp(opType,"ModD")==0 ||
4145 strcmp(opType,"ModF")==0 ||
4146 strcmp(opType,"ModI")==0 ||
4147 strcmp(opType,"SqrtD")==0 ||
4148 strcmp(opType,"SqrtF")==0 ||
4149 strcmp(opType,"SqrtHF")==0 ||
4150 strcmp(opType,"TanD")==0 ||
4151 strcmp(opType,"ConvD2F")==0 ||
4152 strcmp(opType,"ConvD2I")==0 ||
4153 strcmp(opType,"ConvD2L")==0 ||
4154 strcmp(opType,"ConvF2D")==0 ||
4155 strcmp(opType,"ConvF2I")==0 ||
4156 strcmp(opType,"ConvF2L")==0 ||
4157 strcmp(opType,"ConvI2D")==0 ||
4158 strcmp(opType,"ConvI2F")==0 ||
4159 strcmp(opType,"ConvI2L")==0 ||
4160 strcmp(opType,"ConvL2D")==0 ||
4161 strcmp(opType,"ConvL2F")==0 ||
4162 strcmp(opType,"ConvL2I")==0 ||
4163 strcmp(opType,"DecodeN")==0 ||
4164 strcmp(opType,"EncodeP")==0 ||
4165 strcmp(opType,"EncodePKlass")==0 ||
4166 strcmp(opType,"DecodeNKlass")==0 ||
4167 strcmp(opType,"FmaD") == 0 ||
4168 strcmp(opType,"FmaF") == 0 ||
4169 strcmp(opType,"FmaHF") == 0 ||
4170 strcmp(opType,"RoundDoubleMode")==0 ||
4171 strcmp(opType,"ReverseBytesI")==0 ||
4172 strcmp(opType,"ReverseBytesL")==0 ||
4173 strcmp(opType,"ReverseBytesUS")==0 ||
4174 strcmp(opType,"ReverseBytesS")==0 ||
4175 strcmp(opType,"PopulateIndex")==0 ||
4176 strcmp(opType,"AddReductionVI")==0 ||
4177 strcmp(opType,"AddReductionVL")==0 ||
4178 strcmp(opType,"AddReductionVHF")==0 ||
4179 strcmp(opType,"AddReductionVF")==0 ||
4180 strcmp(opType,"AddReductionVD")==0 ||
4181 strcmp(opType,"MulReductionVI")==0 ||
4182 strcmp(opType,"MulReductionVL")==0 ||
4183 strcmp(opType,"MulReductionVF")==0 ||
4184 strcmp(opType,"MulReductionVHF")==0 ||
4185 strcmp(opType,"MulReductionVD")==0 ||
4186 strcmp(opType,"MinReductionV")==0 ||
4187 strcmp(opType,"MaxReductionV")==0 ||
4188 strcmp(opType,"AndReductionV")==0 ||
4189 strcmp(opType,"OrReductionV")==0 ||
4190 strcmp(opType,"XorReductionV")==0 ||
4191 strcmp(opType,"MaskAll")==0 ||
4192 0 /* 0 to line up columns nicely */ ) {
4193 return 1;
4194 }
4195 }
4196 return 0;
4197 }
4198
4199 bool MatchRule::is_ideal_if() const {
4200 if( !_opType ) return false;
4201 return
4202 !strcmp(_opType,"If" ) ||
4203 !strcmp(_opType,"CountedLoopEnd");
4204 }
4205
4206 bool MatchRule::is_ideal_fastlock() const {
4207 if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
4208 return (strcmp(_rChild->_opType,"FastLock") == 0);
4209 }
4210 return false;
4211 }
4212
4213 bool MatchRule::is_ideal_membar() const {
4214 if( !_opType ) return false;
4215 return
4216 !strcmp(_opType,"MemBarAcquire") ||
4217 !strcmp(_opType,"MemBarRelease") ||
4218 !strcmp(_opType,"MemBarAcquireLock") ||
4219 !strcmp(_opType,"MemBarReleaseLock") ||
4220 !strcmp(_opType,"LoadFence" ) ||
4221 !strcmp(_opType,"StoreFence") ||
4222 !strcmp(_opType,"StoreStoreFence") ||
4223 !strcmp(_opType,"MemBarStoreLoad") ||
4224 !strcmp(_opType,"MemBarVolatile") ||
4225 !strcmp(_opType,"MemBarFull") ||
4226 !strcmp(_opType,"MemBarCPUOrder") ||
4227 !strcmp(_opType,"MemBarStoreStore") ||
4228 !strcmp(_opType,"OnSpinWait");
4229 }
4230
4231 bool MatchRule::is_ideal_loadPC() const {
4232 if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
4233 return (strcmp(_rChild->_opType,"LoadPC") == 0);
4234 }
4235 return false;
4236 }
4237
4238 bool MatchRule::is_ideal_box() const {
4239 if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
4240 return (strcmp(_rChild->_opType,"Box") == 0);
4241 }
4242 return false;
4243 }
4244
4245 bool MatchRule::is_ideal_goto() const {
4246 bool ideal_goto = false;
4247
4248 if( _opType && (strcmp(_opType,"Goto") == 0) ) {
4249 ideal_goto = true;
4250 }
4251 return ideal_goto;
4252 }
4253
4254 bool MatchRule::is_ideal_jump() const {
4255 if( _opType ) {
4256 if( !strcmp(_opType,"Jump") )
4257 return true;
4258 }
4259 return false;
4260 }
4261
4262 bool MatchRule::is_ideal_bool() const {
4263 if( _opType ) {
4264 if( !strcmp(_opType,"Bool") )
4265 return true;
4266 }
4267 return false;
4268 }
4269
4270
4271 Form::DataType MatchRule::is_ideal_load() const {
4272 Form::DataType ideal_load = Form::none;
4273
4274 if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
4275 const char *opType = _rChild->_opType;
4276 ideal_load = is_load_from_memory(opType);
4277 }
4278
4279 return ideal_load;
4280 }
4281
4282 bool MatchRule::skip_antidep_check() const {
4283 // Some loads operate on what is effectively immutable memory so we
4284 // should skip the anti dep computations. For some of these nodes
4285 // the rewritable field keeps the anti dep logic from triggering but
4286 // for certain kinds of LoadKlass it does not since they are
4287 // actually reading memory which could be rewritten by the runtime,
4288 // though never by generated code. This disables it uniformly for
4289 // the nodes that behave like this: LoadKlass, LoadNKlass and
4290 // LoadRange.
4291 if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
4292 const char *opType = _rChild->_opType;
4293 if (strcmp("LoadKlass", opType) == 0 ||
4294 strcmp("LoadNKlass", opType) == 0 ||
4295 strcmp("LoadRange", opType) == 0) {
4296 return true;
4297 }
4298 }
4299
4300 return false;
4301 }
4302
4303
4304 Form::DataType MatchRule::is_ideal_store() const {
4305 Form::DataType ideal_store = Form::none;
4306
4307 if ( _opType && (strcmp(_opType,"Set") == 0) && _rChild ) {
4308 const char *opType = _rChild->_opType;
4309 ideal_store = is_store_to_memory(opType);
4310 }
4311
4312 return ideal_store;
4313 }
4314
4315
4316 void MatchRule::dump() {
4317 output(stderr);
4318 }
4319
4320 // Write just one line.
4321 void MatchRule::output_short(FILE *fp) {
4322 fprintf(fp,"MatchRule: ( %s",_name);
4323 if (_lChild) _lChild->output(fp);
4324 if (_rChild) _rChild->output(fp);
4325 fprintf(fp," )");
4326 }
4327
4328 void MatchRule::output(FILE *fp) {
4329 output_short(fp);
4330 fprintf(fp,"\n nesting depth = %d\n", _depth);
4331 if (_result) fprintf(fp," Result Type = %s", _result);
4332 fprintf(fp,"\n");
4333 }
4334
4335 void MatchRule::forms_do(FormClosure* f) {
4336 // keep sync with MatchNode::forms_do
4337 f->do_form_by_name(_name);
4338 if (_lChild) f->do_form(_lChild);
4339 if (_rChild) f->do_form(_rChild);
4340
4341 // handle next rule
4342 if (_next) {
4343 f->do_form(_next);
4344 }
4345 }
4346
4347 //------------------------------Attribute--------------------------------------
4348 Attribute::Attribute(char *id, char* val, int type)
4349 : _ident(id), _val(val), _atype(type) {
4350 }
4351 Attribute::~Attribute() {
4352 }
4353
4354 int Attribute::int_val(ArchDesc &ad) {
4355 // Make sure it is an integer constant:
4356 int result = 0;
4357 if (!_val || !ADLParser::is_int_token(_val, result)) {
4358 ad.syntax_err(0, "Attribute %s must have an integer value: %s",
4359 _ident, _val ? _val : "");
4360 }
4361 return result;
4362 }
4363
4364 void Attribute::dump() {
4365 output(stderr);
4366 } // Debug printer
4367
4368 // Write to output files
4369 void Attribute::output(FILE *fp) {
4370 fprintf(fp,"Attribute: %s %s\n", (_ident?_ident:""), (_val?_val:""));
4371 }
4372
4373 //------------------------------FormatRule----------------------------------
4374 FormatRule::FormatRule(char *temp)
4375 : _temp(temp) {
4376 }
4377 FormatRule::~FormatRule() {
4378 }
4379
4380 void FormatRule::dump() {
4381 output(stderr);
4382 }
4383
4384 // Write to output files
4385 void FormatRule::output(FILE *fp) {
4386 fprintf(fp,"\nFormat Rule: \n%s", (_temp?_temp:""));
4387 fprintf(fp,"\n");
4388 }