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