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