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