29
int RegisterForm::_reg_ctr = 0;
33
RegisterForm::RegisterForm()
34
: _current_ac(nullptr),
35
_regDef(cmpstr,hashstr, Form::arena),
36
_regClass(cmpstr,hashstr, Form::arena),
37
_allocClass(cmpstr,hashstr, Form::arena) {
39
RegisterForm::~RegisterForm() {
43
void RegisterForm::addRegDef(char *name, char *callingConv, char *c_conv,
44
char *idealtype, char *encoding, char* concrete) {
45
RegDef *regDef = new RegDef(name, callingConv, c_conv, idealtype, encoding, concrete);
47
_regDef.Insert(name,regDef);
52
T* RegisterForm::addRegClass(const char* className) {
53
T* regClass = new T(className);
54
_rclasses.addName(className);
55
_regClass.Insert(className, regClass);
60
template RegClass* RegisterForm::addRegClass<RegClass>(const char* className);
61
template CodeSnippetRegClass* RegisterForm::addRegClass<CodeSnippetRegClass>(const char* className);
62
template ConditionalRegClass* RegisterForm::addRegClass<ConditionalRegClass>(const char* className);
65
AllocClass *RegisterForm::addAllocClass(char *className) {
66
AllocClass *allocClass = new AllocClass(className);
67
_aclasses.addName(className);
68
_allocClass.Insert(className,allocClass);
74
void RegisterForm::addSpillRegClass() {
76
_reg_ctr = (_reg_ctr+7) & ~7;
77
const char *rc_name = "stack_slots";
78
RegClass* reg_class = new RegClass(rc_name);
79
reg_class->set_stack_version(true);
80
_rclasses.addName(rc_name);
81
_regClass.Insert(rc_name,reg_class);
86
void RegisterForm::addDynamicRegClass() {
87
const char *rc_name = "dynamic";
88
RegClass* reg_class = new RegClass(rc_name);
89
reg_class->set_stack_version(false);
90
_rclasses.addName(rc_name);
91
_regClass.Insert(rc_name,reg_class);
96
void RegisterForm::reset_RegDefs() {
97
_current_ac = nullptr;
101
RegDef *RegisterForm::iter_RegDefs() {
103
if ( _current_ac == nullptr ) {
104
const char *ac_name = _aclasses.iter();
105
if( ac_name == nullptr ) return nullptr;
106
_current_ac = (AllocClass*)_allocClass[ac_name];
107
_current_ac->_regDefs.reset();
108
assert( _current_ac != nullptr, "Name must match an allocation class");
111
const char *rd_name = _current_ac->_regDefs.iter();
112
if( rd_name == nullptr ) {
114
_current_ac = nullptr;
115
return iter_RegDefs();
117
RegDef *reg_def = (RegDef*)_current_ac->_regDef[rd_name];
118
assert( reg_def != nullptr, "Name must match a register definition");
123
RegDef *RegisterForm::getRegDef(const char *regName) {
124
RegDef *regDef = (RegDef*)_regDef[regName];
129
RegClass *RegisterForm::getRegClass(const char *className) {
130
RegClass *regClass = (RegClass*)_regClass[className];
136
bool RegisterForm::verify() {
141
const char *rc_name = nullptr;
143
while ( (rc_name = _rclasses.iter()) != nullptr ) {
145
RegClass *reg_class = getRegClass(rc_name);
146
assert( reg_class != nullptr, "InternalError() no matching register class");
150
RegDef *reg_def = nullptr;
152
uint num_register_zero = 0;
153
while ( (reg_def = iter_RegDefs()) != nullptr ) {
154
if( reg_def->register_num() == 0 ) ++num_register_zero;
156
if( num_register_zero > 1 ) {
158
"ERROR: More than one register has been assigned register-number 0.\n"
159
"Probably because a register has not been entered into an allocation class.\n");
166
int RegisterForm::RegMask_Size() {
168
int words_for_regs = (_reg_ctr + 31)>>5;
176
return (words_for_regs + 3 + 1) & ~1;
179
void RegisterForm::dump() {
183
void RegisterForm::output(FILE *fp) {
186
fprintf(fp,"-------------------- Dump RegisterForm --------------------\n");
187
for(_rdefs.reset(); (name = _rdefs.iter()) != nullptr;) {
188
((RegDef*)_regDef[name])->output(fp);
191
for (_rclasses.reset(); (name = _rclasses.iter()) != nullptr;) {
192
((RegClass*)_regClass[name])->output(fp);
195
for (_aclasses.reset(); (name = _aclasses.iter()) != nullptr;) {
196
((AllocClass*)_allocClass[name])->output(fp);
198
fprintf(fp,"-------------------- end RegisterForm --------------------\n");
201
void RegisterForm::forms_do(FormClosure *f) {
202
const char *name = nullptr;
203
if (_current_ac) f->do_form(_current_ac);
204
for(_rdefs.reset(); (name = _rdefs.iter()) != nullptr;) {
205
f->do_form((RegDef*)_regDef[name]);
207
for (_rclasses.reset(); (name = _rclasses.iter()) != nullptr;) {
208
f->do_form((RegClass*)_regClass[name]);
210
for (_aclasses.reset(); (name = _aclasses.iter()) != nullptr;) {
211
f->do_form((AllocClass*)_allocClass[name]);
217
RegDef::RegDef(char *regname, char *callconv, char *c_conv, char * idealtype, char * encode, char * concrete)
218
: _regname(regname), _callconv(callconv), _c_conv(c_conv),
219
_idealtype(idealtype),
220
_register_encode(encode),
230
void RegDef::set_register_num(uint32 register_num) {
231
_register_num = register_num;
235
const char* RegDef::register_encode() const {
236
return _register_encode;
240
uint32 RegDef::register_num() const {
241
return _register_num;
248
void RegDef::output(FILE *fp) {
249
fprintf(fp,"RegDef: %s (%s) encode as %s using number %d\n",
250
_regname, (_callconv?_callconv:""), _register_encode, _register_num);
257
RegClass::RegClass(const char* classid) : _stack_or_reg(false), _classid(classid), _regDef(cmpstr, hashstr, Form::arena) {
260
RegClass::~RegClass() {
264
void RegClass::addReg(RegDef *regDef) {
265
_regDefs.addName(regDef->_regname);
266
_regDef.Insert((void*)regDef->_regname, regDef);
270
uint RegClass::size() const {
271
return _regDef.Size();
274
const RegDef *RegClass::get_RegDef(const char *rd_name) const {
275
return (const RegDef*)_regDef[rd_name];
278
void RegClass::reset() {
282
const char *RegClass::rd_name_iter() {
283
return _regDefs.iter();
286
RegDef *RegClass::RegDef_iter() {
287
const char *rd_name = rd_name_iter();
288
RegDef *reg_def = rd_name ? (RegDef*)_regDef[rd_name] : nullptr;
292
const RegDef* RegClass::find_first_elem() {
293
const RegDef* first = nullptr;
294
const RegDef* def = nullptr;
297
while ((def = RegDef_iter()) != nullptr) {
298
if (first == nullptr || def->register_num() < first->register_num()) {
303
assert(first != nullptr, "empty mask?");
308
int RegClass::regs_in_word( int wordnum, bool stack_also ) {
311
for(_regDefs.reset(); (name = _regDefs.iter()) != nullptr;) {
312
int rnum = ((RegDef*)_regDef[name])->register_num();
313
if( (rnum >> 5) == wordnum )
314
word |= (1 << (rnum & 31));
318
for( int i = 0; i < 32; i++ )
319
if( wordnum*32+i >= RegisterForm::_reg_ctr )
326
void RegClass::dump() {
330
void RegClass::output(FILE *fp) {
331
fprintf(fp,"RegClass: %s\n",_classid);
333
for(_regDefs.reset(); (name = _regDefs.iter()) != nullptr;) {
334
((RegDef*)_regDef[name])->output(fp);
336
fprintf(fp,"--- done with entries for reg_class %s\n\n",_classid);
339
void RegClass::forms_do(FormClosure *f) {
340
const char *name = nullptr;
341
for( _regDefs.reset(); (name = _regDefs.iter()) != nullptr; ) {
342
f->do_form((RegDef*)_regDef[name]);
346
void RegClass::declare_register_masks(FILE* fp) {
347
const char* prefix = "";
348
const char* rc_name_to_upper = toUpper(_classid);
349
fprintf(fp, "extern const RegMask _%s%s_mask;\n", prefix, rc_name_to_upper);
350
fprintf(fp, "inline const RegMask &%s%s_mask() { return _%s%s_mask; }\n", prefix, rc_name_to_upper, prefix, rc_name_to_upper);
352
fprintf(fp, "extern const RegMask _%sSTACK_OR_%s_mask;\n", prefix, rc_name_to_upper);
353
fprintf(fp, "inline const RegMask &%sSTACK_OR_%s_mask() { return _%sSTACK_OR_%s_mask; }\n", prefix, rc_name_to_upper, prefix, rc_name_to_upper);
355
delete[] rc_name_to_upper;
358
void RegClass::build_register_masks(FILE* fp) {
359
int len = RegisterForm::RegMask_Size();
360
const char *prefix = "";
361
const char* rc_name_to_upper = toUpper(_classid);
362
fprintf(fp, "const RegMask _%s%s_mask(", prefix, rc_name_to_upper);
365
for(i = 0; i < len - 1; i++) {
366
fprintf(fp," 0x%x,", regs_in_word(i, false));
368
fprintf(fp," 0x%x );\n", regs_in_word(i, false));
371
fprintf(fp, "const RegMask _%sSTACK_OR_%s_mask(", prefix, rc_name_to_upper);
372
for(i = 0; i < len - 1; i++) {
373
fprintf(fp," 0x%x,", regs_in_word(i, true));
375
fprintf(fp," 0x%x );\n", regs_in_word(i, true));
377
delete[] rc_name_to_upper;
381
CodeSnippetRegClass::CodeSnippetRegClass(const char* classid) : RegClass(classid), _code_snippet(nullptr) {
384
CodeSnippetRegClass::~CodeSnippetRegClass() {
385
delete _code_snippet;
388
void CodeSnippetRegClass::declare_register_masks(FILE* fp) {
389
const char* prefix = "";
390
const char* rc_name_to_upper = toUpper(_classid);
391
fprintf(fp, "inline const RegMask &%s%s_mask() { %s }\n", prefix, rc_name_to_upper, _code_snippet);
392
delete[] rc_name_to_upper;
396
ConditionalRegClass::ConditionalRegClass(const char *classid) : RegClass(classid), _condition_code(nullptr) {
397
_rclasses[0] = nullptr;
398
_rclasses[1] = nullptr;
401
ConditionalRegClass::~ConditionalRegClass() {
402
delete _condition_code;
405
void ConditionalRegClass::declare_register_masks(FILE* fp) {
406
const char* prefix = "";
407
const char* rc_name_to_upper = toUpper(_classid);
408
const char* rclass_0_to_upper = toUpper(_rclasses[0]->_classid);
409
const char* rclass_1_to_upper = toUpper(_rclasses[1]->_classid);
410
fprintf(fp, "inline const RegMask &%s%s_mask() {"
414
prefix, rc_name_to_upper,
416
prefix, rclass_0_to_upper,
417
prefix, rclass_1_to_upper);
419
fprintf(fp, "inline const RegMask &%sSTACK_OR_%s_mask() {"
421
" %sSTACK_OR_%s_mask() :"
422
" %sSTACK_OR_%s_mask(); }\n",
423
prefix, rc_name_to_upper,
425
prefix, rclass_0_to_upper,
426
prefix, rclass_1_to_upper);
428
delete[] rc_name_to_upper;
429
delete[] rclass_0_to_upper;
430
delete[] rclass_1_to_upper;
435
AllocClass::AllocClass(char *classid) : _classid(classid), _regDef(cmpstr,hashstr, Form::arena) {
439
void AllocClass::addReg(RegDef *regDef) {
440
assert( regDef != nullptr, "Can not add a null to an allocation class");
441
regDef->set_register_num( RegisterForm::_reg_ctr++ );
443
_regDefs.addName(regDef->_regname);
444
_regDef.Insert((void*)regDef->_regname, regDef);
447
void AllocClass::dump() {
451
void AllocClass::output(FILE *fp) {
452
fprintf(fp,"AllocClass: %s \n",_classid);
454
for(_regDefs.reset(); (name = _regDefs.iter()) != nullptr;) {
455
((RegDef*)_regDef[name])->output(fp);
457
fprintf(fp,"--- done with entries for alloc_class %s\n\n",_classid);
460
void AllocClass::forms_do(FormClosure* f) {
462
for(_regDefs.reset(); (name = _regDefs.iter()) != nullptr;) {
463
f->do_form((RegDef*)_regDef[name]);
470
FrameForm::FrameForm() {
471
_sync_stack_slots = nullptr;
472
_inline_cache_reg = nullptr;
473
_interpreter_frame_pointer_reg = nullptr;
474
_cisc_spilling_operand_name = nullptr;
475
_frame_pointer = nullptr;
476
_c_frame_pointer = nullptr;
477
_alignment = nullptr;
478
_return_addr_loc = false;
479
_c_return_addr_loc = false;
480
_return_addr = nullptr;
481
_c_return_addr = nullptr;
482
_varargs_C_out_slots_killed = nullptr;
483
_return_value = nullptr;
484
_c_return_value = nullptr;
487
FrameForm::~FrameForm() {
490
void FrameForm::dump() {
494
void FrameForm::output(FILE *fp) {
495
fprintf(fp,"\nFrame:\n");
500
PipelineForm::PipelineForm()
502
, _resdict (cmpstr, hashstr, Form::arena)
503
, _classdict (cmpstr, hashstr, Form::arena)
512
, _variableSizeInstrs (false)
513
, _branchHasDelaySlot (false)
514
, _maxInstrsPerBundle (0)
515
, _maxBundlesPerCycle (1)
517
, _bundleUnitSize (0)
518
, _instrFetchUnitSize (0)
519
, _instrFetchUnits (0) {
521
PipelineForm::~PipelineForm() {
524
void PipelineForm::dump() {
528
void PipelineForm::output(FILE *fp) {
535
fprintf(fp,"\nPipeline:");
536
if (_variableSizeInstrs)
537
if (_instrUnitSize > 0)
538
fprintf(fp," variable-sized instructions in %d byte units", _instrUnitSize);
540
fprintf(fp," variable-sized instructions");
542
if (_instrUnitSize > 0)
543
fprintf(fp," fixed-sized instructions of %d bytes", _instrUnitSize);
544
else if (_bundleUnitSize > 0)
545
fprintf(fp," fixed-sized bundles of %d bytes", _bundleUnitSize);
547
fprintf(fp," fixed-sized instructions");
548
if (_branchHasDelaySlot)
549
fprintf(fp,", branch has delay slot");
550
if (_maxInstrsPerBundle > 0)
551
fprintf(fp,", max of %d instruction%s in parallel",
552
_maxInstrsPerBundle, _maxInstrsPerBundle > 1 ? "s" : "");
553
if (_maxBundlesPerCycle > 0)
554
fprintf(fp,", max of %d bundle%s in parallel",
555
_maxBundlesPerCycle, _maxBundlesPerCycle > 1 ? "s" : "");
556
if (_instrFetchUnitSize > 0 && _instrFetchUnits)
557
fprintf(fp, ", fetch %d x % d bytes per cycle", _instrFetchUnits, _instrFetchUnitSize);
559
fprintf(fp,"\nResource:");
560
for ( _reslist.reset(); (res = _reslist.iter()) != nullptr; )
561
fprintf(fp," %s(0x%08x)", res, _resdict[res]->is_resource()->mask());
564
fprintf(fp,"\nDescription:\n");
565
for ( _stages.reset(); (stage = _stages.iter()) != nullptr; )
566
fprintf(fp," %s(%d)", stage, count++);
569
fprintf(fp,"\nClasses:\n");
570
for ( _classlist.reset(); (cls = _classlist.iter()) != nullptr; )
571
_classdict[cls]->is_pipeclass()->output(fp);
573
fprintf(fp,"\nNop Instructions:");
574
for ( _noplist.reset(); (nop = _noplist.iter()) != nullptr; )
575
fprintf(fp, " \"%s\"", nop);
581
ResourceForm::ResourceForm(unsigned resmask)
584
ResourceForm::~ResourceForm() {
587
ResourceForm *ResourceForm::is_resource() const {
588
return (ResourceForm *)(this);
591
void ResourceForm::dump() {
595
void ResourceForm::output(FILE *fp) {
596
fprintf(fp, "resource: 0x%08x;\n", mask());
602
void PipeClassOperandForm::dump() {
606
void PipeClassOperandForm::output(FILE *fp) {
607
fprintf(stderr,"PipeClassOperandForm: %s", _stage);
609
if (_more_instrs > 0)
610
fprintf(stderr,"+%d", _more_instrs);
611
fprintf(stderr," (%s)\n", _iswrite ? "write" : "read");
613
fprintf(fp,"PipeClassOperandForm: %s", _stage);
614
if (_more_instrs > 0)
615
fprintf(fp,"+%d", _more_instrs);
616
fprintf(fp," (%s)\n", _iswrite ? "write" : "read");
622
void PipeClassResourceForm::dump() {
626
void PipeClassResourceForm::output(FILE *fp) {
627
fprintf(fp,"PipeClassResourceForm: %s at stage %s for %d cycles\n",
628
_resource, _stage, _cycles);
633
PipeClassForm::PipeClassForm(const char *id, int num)
636
, _localNames(cmpstr, hashstr, Form::arena)
637
, _localUsage(cmpstr, hashstr, Form::arena)
638
, _has_fixed_latency(0)
640
, _instruction_count(0)
641
, _has_multiple_bundles(false)
642
, _has_branch_delay_slot(false)
643
, _force_serialization(false)
644
, _may_have_no_code(false) {
647
PipeClassForm::~PipeClassForm() {
650
PipeClassForm *PipeClassForm::is_pipeclass() const {
651
return (PipeClassForm *)(this);
654
void PipeClassForm::dump() {
658
void PipeClassForm::output(FILE *fp) {
659
fprintf(fp,"PipeClassForm: #%03d", _num);
661
fprintf(fp," \"%s\":", _ident);
662
if (_has_fixed_latency)
663
fprintf(fp," latency %d", _fixed_latency);
664
if (_force_serialization)
665
fprintf(fp, ", force serialization");
666
if (_may_have_no_code)
667
fprintf(fp, ", may have no code");
668
fprintf(fp, ", %d instruction%s\n", InstructionCount(), InstructionCount() != 1 ? "s" : "");
673
int Peephole::_peephole_counter = 0;
675
Peephole::Peephole() : _predicate(nullptr), _match(nullptr), _procedure(nullptr),
676
_constraint(nullptr), _replace(nullptr), _next(nullptr) {
677
_peephole_number = _peephole_counter++;
679
Peephole::~Peephole() {
683
void Peephole::append_peephole(Peephole *next_peephole) {
684
if( _next == nullptr ) {
685
_next = next_peephole;
687
_next->append_peephole( next_peephole );
692
void Peephole::add_predicate(PeepPredicate* predicate) {
693
assert( _predicate == nullptr, "fatal()" );
694
_predicate = predicate;
698
void Peephole::add_match(PeepMatch *match) {
699
assert( _match == nullptr, "fatal()" );
704
void Peephole::add_procedure(PeepProcedure* procedure) {
705
assert( _procedure == nullptr, "fatal()" );
706
_procedure = procedure;
709
void Peephole::append_constraint(PeepConstraint *next_constraint) {
710
if( _constraint == nullptr ) {
711
_constraint = next_constraint;
713
_constraint->append( next_constraint );
717
void Peephole::add_replace(PeepReplace *replace) {
718
assert( _replace == nullptr, "fatal()" );
725
void Peephole::dump() {
729
void Peephole::output(FILE *fp) {
730
fprintf(fp,"Peephole:\n");
731
if( _match != nullptr ) _match->output(fp);
732
if( _constraint != nullptr ) _constraint->output(fp);
733
if( _replace != nullptr ) _replace->output(fp);
735
if( _next ) _next->output(fp);
738
void Peephole::forms_do(FormClosure *f) {
739
if (_predicate) f->do_form(_predicate);
740
if (_match) f->do_form(_match);
741
if (_procedure) f->do_form(_procedure);
742
if (_constraint) f->do_form(_constraint);
743
if (_replace) f->do_form(_replace);
748
PeepPredicate::PeepPredicate(const char* rule) : _rule(rule) {
750
PeepPredicate::~PeepPredicate() {
753
const char* PeepPredicate::rule() const {
757
void PeepPredicate::dump() {
761
void PeepPredicate::output(FILE* fp) {
762
fprintf(fp, "PeepPredicate\n");
766
PeepMatch::PeepMatch(char *rule) : _max_position(0), _rule(rule) {
768
PeepMatch::~PeepMatch() {
772
void PeepMatch::add_instruction(int parent, int position, const char *name,
774
if( position > _max_position ) _max_position = position;
776
_parent.addName((char*) (intptr_t) parent);
777
_position.addName((char*) (intptr_t) position);
778
_instrs.addName(name);
779
_input.addName((char*) (intptr_t) input);
783
int PeepMatch::max_position() {
784
return _max_position;
787
const char *PeepMatch::instruction_name(int position) {
788
return _instrs.name(position);
792
void PeepMatch::reset() {
799
void PeepMatch::next_instruction(int &parent, int &position, const char* &name, int &input) {
800
parent = (int) (intptr_t) _parent.iter();
801
position = (int) (intptr_t) _position.iter();
802
name = _instrs.iter();
803
input = (int) (intptr_t) _input.iter();
807
bool PeepMatch::is_placeholder() {
808
return _instrs.current_is_signal();
812
void PeepMatch::dump() {
816
void PeepMatch::output(FILE *fp) {
817
fprintf(fp,"PeepMatch:\n");
821
PeepProcedure::PeepProcedure(const char* name) : _name(name) {
823
PeepProcedure::~PeepProcedure() {
826
const char* PeepProcedure::name() const {
830
void PeepProcedure::dump() {
834
void PeepProcedure::output(FILE* fp) {
835
fprintf(fp, "PeepProcedure\n");
839
PeepConstraint::PeepConstraint(int left_inst, char* left_op, char* relation,
840
int right_inst, char* right_op)
841
: _left_inst(left_inst), _left_op(left_op), _relation(relation),
842
_right_inst(right_inst), _right_op(right_op), _next(nullptr) {}
843
PeepConstraint::~PeepConstraint() {
847
bool PeepConstraint::constrains_instruction(int position) {
849
if( _left_inst == position ) return true;
850
if( _right_inst == position ) return true;
853
if( _next == nullptr ) return false;
854
else return _next->constrains_instruction(position);
858
void PeepConstraint::append(PeepConstraint *next_constraint) {
859
if( _next == nullptr ) {
860
_next = next_constraint;
862
_next->append( next_constraint );
867
PeepConstraint *PeepConstraint::next() {
872
void PeepConstraint::dump() {
876
void PeepConstraint::output(FILE *fp) {
877
fprintf(fp,"PeepConstraint:\n");
881
PeepReplace::PeepReplace(char *rule) : _rule(rule) {
883
PeepReplace::~PeepReplace() {
887
void PeepReplace::add_instruction(char *root) {
888
_instruction.addName(root);
889
_operand_inst_num.add_signal();
890
_operand_op_name.add_signal();
892
void PeepReplace::add_operand( int inst_num, char *inst_operand ) {
893
_instruction.add_signal();
894
_operand_inst_num.addName((char*) (intptr_t) inst_num);
895
_operand_op_name.addName(inst_operand);
899
void PeepReplace::reset() {
900
_instruction.reset();
901
_operand_inst_num.reset();
902
_operand_op_name.reset();
904
void PeepReplace::next_instruction(const char* &inst){
905
inst = _instruction.iter();
906
int inst_num = (int) (intptr_t) _operand_inst_num.iter();
907
const char* inst_operand = _operand_op_name.iter();
909
void PeepReplace::next_operand(int &inst_num, const char* &inst_operand) {
910
const char* inst = _instruction.iter();
911
inst_num = (int) (intptr_t) _operand_inst_num.iter();
912
inst_operand = _operand_op_name.iter();
917
void PeepReplace::dump() {
921
void PeepReplace::output(FILE *fp) {
922
fprintf(fp,"PeepReplace:\n");