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  */
24 
25 // FORMS.CPP - Definitions for ADL Parser Forms Classes
26 #include "adlc.hpp"
27 
28 //==============================Register Allocation============================
29 int RegisterForm::_reg_ctr = 0;
30 
31 //------------------------------RegisterForm-----------------------------------
32 // Constructor
RegisterForm()33 RegisterForm::RegisterForm()
34   : _regDef(cmpstr,hashstr, Form::arena),
35     _regClass(cmpstr,hashstr, Form::arena),
36     _allocClass(cmpstr,hashstr, Form::arena) {
37 }
~RegisterForm()38 RegisterForm::~RegisterForm() {
39 }
40 
41 // record a new register definition
addRegDef(char * name,char * callingConv,char * c_conv,char * idealtype,char * encoding,char * concrete)42 void RegisterForm::addRegDef(char *name, char *callingConv, char *c_conv,
43                              char *idealtype, char *encoding, char* concrete) {
44   RegDef *regDef = new RegDef(name, callingConv, c_conv, idealtype, encoding, concrete);
45   _rdefs.addName(name);
46   _regDef.Insert(name,regDef);
47 }
48 
49 // record a new register class
50 template <typename T>
addRegClass(const char * className)51 T* RegisterForm::addRegClass(const char* className) {
52   T* regClass = new T(className);
53   _rclasses.addName(className);
54   _regClass.Insert(className, regClass);
55   return regClass;
56 }
57 
58 // Explicit instantiation for all supported register classes.
59 template RegClass* RegisterForm::addRegClass<RegClass>(const char* className);
60 template CodeSnippetRegClass* RegisterForm::addRegClass<CodeSnippetRegClass>(const char* className);
61 template ConditionalRegClass* RegisterForm::addRegClass<ConditionalRegClass>(const char* className);
62 
63 // record a new register class
addAllocClass(char * className)64 AllocClass *RegisterForm::addAllocClass(char *className) {
65   AllocClass *allocClass = new AllocClass(className);
66   _aclasses.addName(className);
67   _allocClass.Insert(className,allocClass);
68   return allocClass;
69 }
70 
71 // Called after parsing the Register block.  Record the register class
72 // for spill-slots/regs.
addSpillRegClass()73 void RegisterForm::addSpillRegClass() {
74   // Stack slots start at the next available even register number.
75   _reg_ctr = (_reg_ctr+7) & ~7;
76   const char *rc_name = "stack_slots";
77   RegClass* reg_class = new RegClass(rc_name);
78   reg_class->set_stack_version(true);
79   _rclasses.addName(rc_name);
80   _regClass.Insert(rc_name,reg_class);
81 }
82 
83 
84 // Provide iteration over all register definitions
85 // in the order used by the register allocator
reset_RegDefs()86 void        RegisterForm::reset_RegDefs() {
87   _current_ac = NULL;
88   _aclasses.reset();
89 }
90 
iter_RegDefs()91 RegDef     *RegisterForm::iter_RegDefs() {
92   // Check if we need to get the next AllocClass
93   if ( _current_ac == NULL ) {
94     const char *ac_name = _aclasses.iter();
95     if( ac_name == NULL )   return NULL;   // No more allocation classes
96     _current_ac = (AllocClass*)_allocClass[ac_name];
97     _current_ac->_regDefs.reset();
98     assert( _current_ac != NULL, "Name must match an allocation class");
99   }
100 
101   const char *rd_name = _current_ac->_regDefs.iter();
102   if( rd_name == NULL ) {
103     // At end of this allocation class, check the next
104     _current_ac = NULL;
105     return iter_RegDefs();
106   }
107   RegDef *reg_def = (RegDef*)_current_ac->_regDef[rd_name];
108   assert( reg_def != NULL, "Name must match a register definition");
109   return reg_def;
110 }
111 
112 // return the register definition with name 'regName'
getRegDef(const char * regName)113 RegDef *RegisterForm::getRegDef(const char *regName) {
114   RegDef *regDef = (RegDef*)_regDef[regName];
115   return  regDef;
116 }
117 
118 // return the register class with name 'className'
getRegClass(const char * className)119 RegClass *RegisterForm::getRegClass(const char *className) {
120   RegClass *regClass = (RegClass*)_regClass[className];
121   return    regClass;
122 }
123 
124 
125 // Check that register classes are compatible with chunks
verify()126 bool   RegisterForm::verify() {
127   bool valid = true;
128 
129   // Verify Register Classes
130   // check that each register class contains registers from one chunk
131   const char *rc_name = NULL;
132   _rclasses.reset();
133   while ( (rc_name = _rclasses.iter()) != NULL ) {
134     // Check the chunk value for all registers in this class
135     RegClass *reg_class = getRegClass(rc_name);
136     assert( reg_class != NULL, "InternalError() no matching register class");
137   } // end of RegClasses
138 
139   // Verify that every register has been placed into an allocation class
140   RegDef *reg_def = NULL;
141   reset_RegDefs();
142   uint  num_register_zero = 0;
143   while ( (reg_def = iter_RegDefs()) != NULL ) {
144     if( reg_def->register_num() == 0 )  ++num_register_zero;
145   }
146   if( num_register_zero > 1 ) {
147     fprintf(stderr,
148             "ERROR: More than one register has been assigned register-number 0.\n"
149             "Probably because a register has not been entered into an allocation class.\n");
150   }
151 
152   return  valid;
153 }
154 
155 // Compute RegMask size
RegMask_Size()156 int RegisterForm::RegMask_Size() {
157   // Need at least this many words
158   int words_for_regs = (_reg_ctr + 31)>>5;
159   // The array of Register Mask bits should be large enough to cover
160   // all the machine registers and all parameters that need to be passed
161   // on the stack (stack registers) up to some interesting limit.  Methods
162   // that need more parameters will NOT be compiled.  On Intel, the limit
163   // is something like 90+ parameters.
164   // Add a few (3 words == 96 bits) for incoming & outgoing arguments to calls.
165   // Round up to the next doubleword size.
166   return (words_for_regs + 3 + 1) & ~1;
167 }
168 
dump()169 void RegisterForm::dump() {                  // Debug printer
170   output(stderr);
171 }
172 
output(FILE * fp)173 void RegisterForm::output(FILE *fp) {          // Write info to output files
174   const char *name;
175   fprintf(fp,"\n");
176   fprintf(fp,"-------------------- Dump RegisterForm --------------------\n");
177   for(_rdefs.reset(); (name = _rdefs.iter()) != NULL;) {
178     ((RegDef*)_regDef[name])->output(fp);
179   }
180   fprintf(fp,"\n");
181   for (_rclasses.reset(); (name = _rclasses.iter()) != NULL;) {
182     ((RegClass*)_regClass[name])->output(fp);
183   }
184   fprintf(fp,"\n");
185   for (_aclasses.reset(); (name = _aclasses.iter()) != NULL;) {
186     ((AllocClass*)_allocClass[name])->output(fp);
187   }
188   fprintf(fp,"-------------------- end  RegisterForm --------------------\n");
189 }
190 
191 //------------------------------RegDef-----------------------------------------
192 // Constructor
RegDef(char * regname,char * callconv,char * c_conv,char * idealtype,char * encode,char * concrete)193 RegDef::RegDef(char *regname, char *callconv, char *c_conv, char * idealtype, char * encode, char * concrete)
194   : _regname(regname), _callconv(callconv), _c_conv(c_conv),
195     _idealtype(idealtype),
196     _register_encode(encode),
197     _concrete(concrete),
198     _register_num(0) {
199 
200   // Chunk and register mask are determined by the register number
201   // _register_num is set when registers are added to an allocation class
202 }
~RegDef()203 RegDef::~RegDef() {                      // Destructor
204 }
205 
set_register_num(uint32 register_num)206 void RegDef::set_register_num(uint32 register_num) {
207   _register_num      = register_num;
208 }
209 
210 // Bit pattern used for generating machine code
register_encode() const211 const char* RegDef::register_encode() const {
212   return _register_encode;
213 }
214 
215 // Register number used in machine-independent code
register_num() const216 uint32 RegDef::register_num()    const {
217   return _register_num;
218 }
219 
dump()220 void RegDef::dump() {
221   output(stderr);
222 }
223 
output(FILE * fp)224 void RegDef::output(FILE *fp) {         // Write info to output files
225   fprintf(fp,"RegDef: %s (%s) encode as %s  using number %d\n",
226           _regname, (_callconv?_callconv:""), _register_encode, _register_num);
227   fprintf(fp,"\n");
228 }
229 
230 
231 //------------------------------RegClass---------------------------------------
232 // Construct a register class into which registers will be inserted
RegClass(const char * classid)233 RegClass::RegClass(const char* classid) : _stack_or_reg(false), _classid(classid), _regDef(cmpstr, hashstr, Form::arena) {
234 }
235 
~RegClass()236 RegClass::~RegClass() {
237   delete _classid;
238 }
239 
240 // record a register in this class
addReg(RegDef * regDef)241 void RegClass::addReg(RegDef *regDef) {
242   _regDefs.addName(regDef->_regname);
243   _regDef.Insert((void*)regDef->_regname, regDef);
244 }
245 
246 // Number of registers in class
size() const247 uint RegClass::size() const {
248   return _regDef.Size();
249 }
250 
get_RegDef(const char * rd_name) const251 const RegDef *RegClass::get_RegDef(const char *rd_name) const {
252   return  (const RegDef*)_regDef[rd_name];
253 }
254 
reset()255 void RegClass::reset() {
256   _regDefs.reset();
257 }
258 
rd_name_iter()259 const char *RegClass::rd_name_iter() {
260   return _regDefs.iter();
261 }
262 
RegDef_iter()263 RegDef *RegClass::RegDef_iter() {
264   const char *rd_name  = rd_name_iter();
265   RegDef     *reg_def  = rd_name ? (RegDef*)_regDef[rd_name] : NULL;
266   return      reg_def;
267 }
268 
find_first_elem()269 const RegDef* RegClass::find_first_elem() {
270   const RegDef* first = NULL;
271   const RegDef* def = NULL;
272 
273   reset();
274   while ((def = RegDef_iter()) != NULL) {
275     if (first == NULL || def->register_num() < first->register_num()) {
276       first = def;
277     }
278   }
279 
280   assert(first != NULL, "empty mask?");
281   return first;;
282 }
283 
284 // Collect all the registers in this register-word.  One bit per register.
regs_in_word(int wordnum,bool stack_also)285 int RegClass::regs_in_word( int wordnum, bool stack_also ) {
286   int         word = 0;
287   const char *name;
288   for(_regDefs.reset(); (name = _regDefs.iter()) != NULL;) {
289     int rnum = ((RegDef*)_regDef[name])->register_num();
290     if( (rnum >> 5) == wordnum )
291       word |= (1 << (rnum & 31));
292   }
293   if( stack_also ) {
294     // Now also collect stack bits
295     for( int i = 0; i < 32; i++ )
296       if( wordnum*32+i >= RegisterForm::_reg_ctr )
297         word |= (1 << i);
298   }
299 
300   return word;
301 }
302 
dump()303 void RegClass::dump() {
304   output(stderr);
305 }
306 
output(FILE * fp)307 void RegClass::output(FILE *fp) {           // Write info to output files
308   fprintf(fp,"RegClass: %s\n",_classid);
309   const char *name;
310   for(_regDefs.reset(); (name = _regDefs.iter()) != NULL;) {
311     ((RegDef*)_regDef[name])->output(fp);
312   }
313   fprintf(fp,"--- done with entries for reg_class %s\n\n",_classid);
314 }
315 
declare_register_masks(FILE * fp)316 void RegClass::declare_register_masks(FILE* fp) {
317   const char* prefix = "";
318   const char* rc_name_to_upper = toUpper(_classid);
319   fprintf(fp, "extern const RegMask _%s%s_mask;\n", prefix,  rc_name_to_upper);
320   fprintf(fp, "inline const RegMask &%s%s_mask() { return _%s%s_mask; }\n", prefix, rc_name_to_upper, prefix, rc_name_to_upper);
321   if (_stack_or_reg) {
322     fprintf(fp, "extern const RegMask _%sSTACK_OR_%s_mask;\n", prefix, rc_name_to_upper);
323     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);
324   }
325   delete[] rc_name_to_upper;
326 }
327 
build_register_masks(FILE * fp)328 void RegClass::build_register_masks(FILE* fp) {
329   int len = RegisterForm::RegMask_Size();
330   const char *prefix = "";
331   const char* rc_name_to_upper = toUpper(_classid);
332   fprintf(fp, "const RegMask _%s%s_mask(", prefix, rc_name_to_upper);
333 
334   int i;
335   for(i = 0; i < len - 1; i++) {
336     fprintf(fp," 0x%x,", regs_in_word(i, false));
337   }
338   fprintf(fp," 0x%x );\n", regs_in_word(i, false));
339 
340   if (_stack_or_reg) {
341     fprintf(fp, "const RegMask _%sSTACK_OR_%s_mask(", prefix, rc_name_to_upper);
342     for(i = 0; i < len - 1; i++) {
343       fprintf(fp," 0x%x,", regs_in_word(i, true));
344     }
345     fprintf(fp," 0x%x );\n", regs_in_word(i, true));
346   }
347   delete[] rc_name_to_upper;
348 }
349 
350 //------------------------------CodeSnippetRegClass---------------------------
CodeSnippetRegClass(const char * classid)351 CodeSnippetRegClass::CodeSnippetRegClass(const char* classid) : RegClass(classid), _code_snippet(NULL) {
352 }
353 
~CodeSnippetRegClass()354 CodeSnippetRegClass::~CodeSnippetRegClass() {
355   delete _code_snippet;
356 }
357 
declare_register_masks(FILE * fp)358 void CodeSnippetRegClass::declare_register_masks(FILE* fp) {
359   const char* prefix = "";
360   const char* rc_name_to_upper = toUpper(_classid);
361   fprintf(fp, "inline const RegMask &%s%s_mask() { %s }\n", prefix, rc_name_to_upper, _code_snippet);
362   delete[] rc_name_to_upper;
363 }
364 
365 //------------------------------ConditionalRegClass---------------------------
ConditionalRegClass(const char * classid)366 ConditionalRegClass::ConditionalRegClass(const char *classid) : RegClass(classid), _condition_code(NULL) {
367 }
368 
~ConditionalRegClass()369 ConditionalRegClass::~ConditionalRegClass() {
370   delete _condition_code;
371 }
372 
declare_register_masks(FILE * fp)373 void ConditionalRegClass::declare_register_masks(FILE* fp) {
374   const char* prefix = "";
375   const char* rc_name_to_upper = toUpper(_classid);
376   const char* rclass_0_to_upper = toUpper(_rclasses[0]->_classid);
377   const char* rclass_1_to_upper = toUpper(_rclasses[1]->_classid);
378   fprintf(fp, "inline const RegMask &%s%s_mask() {"
379               " return (%s) ?"
380               " %s%s_mask() :"
381               " %s%s_mask(); }\n",
382               prefix, rc_name_to_upper,
383               _condition_code,
384               prefix, rclass_0_to_upper,
385               prefix, rclass_1_to_upper);
386   if (_stack_or_reg) {
387     fprintf(fp, "inline const RegMask &%sSTACK_OR_%s_mask() {"
388                   " return (%s) ?"
389                   " %sSTACK_OR_%s_mask() :"
390                   " %sSTACK_OR_%s_mask(); }\n",
391                   prefix, rc_name_to_upper,
392                   _condition_code,
393                   prefix, rclass_0_to_upper,
394                   prefix, rclass_1_to_upper);
395   }
396   delete[] rc_name_to_upper;
397   delete[] rclass_0_to_upper;
398   delete[] rclass_1_to_upper;
399   return;
400 }
401 
402 //------------------------------AllocClass-------------------------------------
AllocClass(char * classid)403 AllocClass::AllocClass(char *classid) : _classid(classid), _regDef(cmpstr,hashstr, Form::arena) {
404 }
405 
406 // record a register in this class
addReg(RegDef * regDef)407 void AllocClass::addReg(RegDef *regDef) {
408   assert( regDef != NULL, "Can not add a NULL to an allocation class");
409   regDef->set_register_num( RegisterForm::_reg_ctr++ );
410   // Add regDef to this allocation class
411   _regDefs.addName(regDef->_regname);
412   _regDef.Insert((void*)regDef->_regname, regDef);
413 }
414 
dump()415 void AllocClass::dump() {
416   output(stderr);
417 }
418 
output(FILE * fp)419 void AllocClass::output(FILE *fp) {       // Write info to output files
420   fprintf(fp,"AllocClass: %s \n",_classid);
421   const char *name;
422   for(_regDefs.reset(); (name = _regDefs.iter()) != NULL;) {
423     ((RegDef*)_regDef[name])->output(fp);
424   }
425   fprintf(fp,"--- done with entries for alloc_class %s\n\n",_classid);
426 }
427 
428 //==============================Frame Handling=================================
429 //------------------------------FrameForm--------------------------------------
FrameForm()430 FrameForm::FrameForm() {
431   _direction = false;
432   _sync_stack_slots = NULL;
433   _inline_cache_reg = NULL;
434   _interpreter_method_oop_reg = NULL;
435   _interpreter_frame_pointer_reg = NULL;
436   _cisc_spilling_operand_name = NULL;
437   _frame_pointer = NULL;
438   _c_frame_pointer = NULL;
439   _alignment = NULL;
440   _return_addr_loc = false;
441   _c_return_addr_loc = false;
442   _return_addr = NULL;
443   _c_return_addr = NULL;
444   _in_preserve_slots = NULL;
445   _varargs_C_out_slots_killed = NULL;
446   _calling_convention = NULL;
447   _c_calling_convention = NULL;
448   _return_value = NULL;
449   _c_return_value = NULL;
450 }
451 
~FrameForm()452 FrameForm::~FrameForm() {
453 }
454 
dump()455 void FrameForm::dump() {
456   output(stderr);
457 }
458 
output(FILE * fp)459 void FrameForm::output(FILE *fp) {           // Write info to output files
460   fprintf(fp,"\nFrame:\n");
461 }
462 
463 //==============================Scheduling=====================================
464 //------------------------------PipelineForm-----------------------------------
PipelineForm()465 PipelineForm::PipelineForm()
466   :  _reslist               ()
467   ,  _resdict               (cmpstr, hashstr, Form::arena)
468   ,  _classdict             (cmpstr, hashstr, Form::arena)
469   ,  _rescount              (0)
470   ,  _maxcycleused          (0)
471   ,  _stages                ()
472   ,  _stagecnt              (0)
473   ,  _classlist             ()
474   ,  _classcnt              (0)
475   ,  _noplist               ()
476   ,  _nopcnt                (0)
477   ,  _variableSizeInstrs    (false)
478   ,  _branchHasDelaySlot    (false)
479   ,  _maxInstrsPerBundle    (0)
480   ,  _maxBundlesPerCycle    (1)
481   ,  _instrUnitSize         (0)
482   ,  _bundleUnitSize        (0)
483   ,  _instrFetchUnitSize    (0)
484   ,  _instrFetchUnits       (0) {
485 }
~PipelineForm()486 PipelineForm::~PipelineForm() {
487 }
488 
dump()489 void PipelineForm::dump() {
490   output(stderr);
491 }
492 
output(FILE * fp)493 void PipelineForm::output(FILE *fp) {           // Write info to output files
494   const char *res;
495   const char *stage;
496   const char *cls;
497   const char *nop;
498   int count = 0;
499 
500   fprintf(fp,"\nPipeline:");
501   if (_variableSizeInstrs)
502     if (_instrUnitSize > 0)
503       fprintf(fp," variable-sized instructions in %d byte units", _instrUnitSize);
504     else
505       fprintf(fp," variable-sized instructions");
506   else
507     if (_instrUnitSize > 0)
508       fprintf(fp," fixed-sized instructions of %d bytes", _instrUnitSize);
509     else if (_bundleUnitSize > 0)
510       fprintf(fp," fixed-sized bundles of %d bytes", _bundleUnitSize);
511     else
512       fprintf(fp," fixed-sized instructions");
513   if (_branchHasDelaySlot)
514     fprintf(fp,", branch has delay slot");
515   if (_maxInstrsPerBundle > 0)
516     fprintf(fp,", max of %d instruction%s in parallel",
517       _maxInstrsPerBundle, _maxInstrsPerBundle > 1 ? "s" : "");
518   if (_maxBundlesPerCycle > 0)
519     fprintf(fp,", max of %d bundle%s in parallel",
520       _maxBundlesPerCycle, _maxBundlesPerCycle > 1 ? "s" : "");
521   if (_instrFetchUnitSize > 0 && _instrFetchUnits)
522     fprintf(fp, ", fetch %d x % d bytes per cycle", _instrFetchUnits, _instrFetchUnitSize);
523 
524   fprintf(fp,"\nResource:");
525   for ( _reslist.reset(); (res = _reslist.iter()) != NULL; )
526     fprintf(fp," %s(0x%08x)", res, _resdict[res]->is_resource()->mask());
527   fprintf(fp,"\n");
528 
529   fprintf(fp,"\nDescription:\n");
530   for ( _stages.reset(); (stage = _stages.iter()) != NULL; )
531     fprintf(fp," %s(%d)", stage, count++);
532   fprintf(fp,"\n");
533 
534   fprintf(fp,"\nClasses:\n");
535   for ( _classlist.reset(); (cls = _classlist.iter()) != NULL; )
536     _classdict[cls]->is_pipeclass()->output(fp);
537 
538   fprintf(fp,"\nNop Instructions:");
539   for ( _noplist.reset(); (nop = _noplist.iter()) != NULL; )
540     fprintf(fp, " \"%s\"", nop);
541   fprintf(fp,"\n");
542 }
543 
544 
545 //------------------------------ResourceForm-----------------------------------
ResourceForm(unsigned resmask)546 ResourceForm::ResourceForm(unsigned resmask)
547 : _resmask(resmask) {
548 }
~ResourceForm()549 ResourceForm::~ResourceForm() {
550 }
551 
is_resource() const552 ResourceForm  *ResourceForm::is_resource() const {
553   return (ResourceForm *)(this);
554 }
555 
dump()556 void ResourceForm::dump() {
557   output(stderr);
558 }
559 
output(FILE * fp)560 void ResourceForm::output(FILE *fp) {          // Write info to output files
561   fprintf(fp, "resource: 0x%08x;\n", mask());
562 }
563 
564 
565 //------------------------------PipeClassOperandForm----------------------------------
566 
dump()567 void PipeClassOperandForm::dump() {
568   output(stderr);
569 }
570 
output(FILE * fp)571 void PipeClassOperandForm::output(FILE *fp) {         // Write info to output files
572   fprintf(stderr,"PipeClassOperandForm: %s", _stage);
573   fflush(stderr);
574   if (_more_instrs > 0)
575     fprintf(stderr,"+%d", _more_instrs);
576   fprintf(stderr," (%s)\n", _iswrite ? "write" : "read");
577   fflush(stderr);
578   fprintf(fp,"PipeClassOperandForm: %s", _stage);
579   if (_more_instrs > 0)
580     fprintf(fp,"+%d", _more_instrs);
581   fprintf(fp," (%s)\n", _iswrite ? "write" : "read");
582 }
583 
584 
585 //------------------------------PipeClassResourceForm----------------------------------
586 
dump()587 void PipeClassResourceForm::dump() {
588   output(stderr);
589 }
590 
output(FILE * fp)591 void PipeClassResourceForm::output(FILE *fp) {         // Write info to output files
592   fprintf(fp,"PipeClassResourceForm: %s at stage %s for %d cycles\n",
593      _resource, _stage, _cycles);
594 }
595 
596 
597 //------------------------------PipeClassForm----------------------------------
PipeClassForm(const char * id,int num)598 PipeClassForm::PipeClassForm(const char *id, int num)
599   : _ident(id)
600   , _num(num)
601   , _localNames(cmpstr, hashstr, Form::arena)
602   , _localUsage(cmpstr, hashstr, Form::arena)
603   , _has_fixed_latency(0)
604   , _fixed_latency(0)
605   , _instruction_count(0)
606   , _has_multiple_bundles(false)
607   , _has_branch_delay_slot(false)
608   , _force_serialization(false)
609   , _may_have_no_code(false) {
610 }
611 
~PipeClassForm()612 PipeClassForm::~PipeClassForm() {
613 }
614 
is_pipeclass() const615 PipeClassForm  *PipeClassForm::is_pipeclass() const {
616   return (PipeClassForm *)(this);
617 }
618 
dump()619 void PipeClassForm::dump() {
620   output(stderr);
621 }
622 
output(FILE * fp)623 void PipeClassForm::output(FILE *fp) {         // Write info to output files
624   fprintf(fp,"PipeClassForm: #%03d", _num);
625   if (_ident)
626      fprintf(fp," \"%s\":", _ident);
627   if (_has_fixed_latency)
628      fprintf(fp," latency %d", _fixed_latency);
629   if (_force_serialization)
630      fprintf(fp, ", force serialization");
631   if (_may_have_no_code)
632      fprintf(fp, ", may have no code");
633   fprintf(fp, ", %d instruction%s\n", InstructionCount(), InstructionCount() != 1 ? "s" : "");
634 }
635 
636 
637 //==============================Peephole Optimization==========================
638 int Peephole::_peephole_counter = 0;
639 //------------------------------Peephole---------------------------------------
Peephole()640 Peephole::Peephole() : _match(NULL), _constraint(NULL), _replace(NULL), _next(NULL) {
641   _peephole_number = _peephole_counter++;
642 }
~Peephole()643 Peephole::~Peephole() {
644 }
645 
646 // Append a peephole rule with the same root instruction
append_peephole(Peephole * next_peephole)647 void Peephole::append_peephole(Peephole *next_peephole) {
648   if( _next == NULL ) {
649     _next = next_peephole;
650   } else {
651     _next->append_peephole( next_peephole );
652   }
653 }
654 
655 // Store the components of this peephole rule
add_match(PeepMatch * match)656 void Peephole::add_match(PeepMatch *match) {
657   assert( _match == NULL, "fatal()" );
658   _match = match;
659 }
660 
append_constraint(PeepConstraint * next_constraint)661 void Peephole::append_constraint(PeepConstraint *next_constraint) {
662   if( _constraint == NULL ) {
663     _constraint = next_constraint;
664   } else {
665     _constraint->append( next_constraint );
666   }
667 }
668 
add_replace(PeepReplace * replace)669 void Peephole::add_replace(PeepReplace *replace) {
670   assert( _replace == NULL, "fatal()" );
671   _replace = replace;
672 }
673 
674 // class Peephole accessor methods are in the declaration.
675 
676 
dump()677 void Peephole::dump() {
678   output(stderr);
679 }
680 
output(FILE * fp)681 void Peephole::output(FILE *fp) {         // Write info to output files
682   fprintf(fp,"Peephole:\n");
683   if( _match != NULL )       _match->output(fp);
684   if( _constraint != NULL )  _constraint->output(fp);
685   if( _replace != NULL )     _replace->output(fp);
686   // Output the next entry
687   if( _next ) _next->output(fp);
688 }
689 
690 //------------------------------PeepMatch--------------------------------------
PeepMatch(char * rule)691 PeepMatch::PeepMatch(char *rule) : _max_position(0), _rule(rule) {
692 }
~PeepMatch()693 PeepMatch::~PeepMatch() {
694 }
695 
696 
697 // Insert info into the match-rule
add_instruction(int parent,int position,const char * name,int input)698 void  PeepMatch::add_instruction(int parent, int position, const char *name,
699                                  int input) {
700   if( position > _max_position ) _max_position = position;
701 
702   _parent.addName((char*) (intptr_t) parent);
703   _position.addName((char*) (intptr_t) position);
704   _instrs.addName(name);
705   _input.addName((char*) (intptr_t) input);
706 }
707 
708 // Access info about instructions in the peep-match rule
max_position()709 int   PeepMatch::max_position() {
710   return _max_position;
711 }
712 
instruction_name(int position)713 const char *PeepMatch::instruction_name(int position) {
714   return _instrs.name(position);
715 }
716 
717 // Iterate through all info on matched instructions
reset()718 void  PeepMatch::reset() {
719   _parent.reset();
720   _position.reset();
721   _instrs.reset();
722   _input.reset();
723 }
724 
next_instruction(int & parent,int & position,const char * & name,int & input)725 void  PeepMatch::next_instruction(int &parent, int &position, const char* &name, int &input) {
726   parent   = (int) (intptr_t) _parent.iter();
727   position = (int) (intptr_t) _position.iter();
728   name     = _instrs.iter();
729   input    = (int) (intptr_t) _input.iter();
730 }
731 
732 // 'true' if current position in iteration is a placeholder, not matched.
is_placeholder()733 bool  PeepMatch::is_placeholder() {
734   return _instrs.current_is_signal();
735 }
736 
737 
dump()738 void PeepMatch::dump() {
739   output(stderr);
740 }
741 
output(FILE * fp)742 void PeepMatch::output(FILE *fp) {        // Write info to output files
743   fprintf(fp,"PeepMatch:\n");
744 }
745 
746 //------------------------------PeepConstraint---------------------------------
PeepConstraint(int left_inst,char * left_op,char * relation,int right_inst,char * right_op)747 PeepConstraint::PeepConstraint(int left_inst,  char* left_op, char* relation,
748                                int right_inst, char* right_op)
749   : _left_inst(left_inst), _left_op(left_op), _relation(relation),
750     _right_inst(right_inst), _right_op(right_op), _next(NULL) {}
~PeepConstraint()751 PeepConstraint::~PeepConstraint() {
752 }
753 
754 // Check if constraints use instruction at position
constrains_instruction(int position)755 bool PeepConstraint::constrains_instruction(int position) {
756   // Check local instruction constraints
757   if( _left_inst  == position ) return true;
758   if( _right_inst == position ) return true;
759 
760   // Check remaining constraints in list
761   if( _next == NULL )  return false;
762   else                 return _next->constrains_instruction(position);
763 }
764 
765 // Add another constraint
append(PeepConstraint * next_constraint)766 void PeepConstraint::append(PeepConstraint *next_constraint) {
767   if( _next == NULL ) {
768     _next = next_constraint;
769   } else {
770     _next->append( next_constraint );
771   }
772 }
773 
774 // Access the next constraint in the list
next()775 PeepConstraint *PeepConstraint::next() {
776   return _next;
777 }
778 
779 
dump()780 void PeepConstraint::dump() {
781   output(stderr);
782 }
783 
output(FILE * fp)784 void PeepConstraint::output(FILE *fp) {   // Write info to output files
785   fprintf(fp,"PeepConstraint:\n");
786 }
787 
788 //------------------------------PeepReplace------------------------------------
PeepReplace(char * rule)789 PeepReplace::PeepReplace(char *rule) : _rule(rule) {
790 }
~PeepReplace()791 PeepReplace::~PeepReplace() {
792 }
793 
794 // Add contents of peepreplace
add_instruction(char * root)795 void  PeepReplace::add_instruction(char *root) {
796   _instruction.addName(root);
797   _operand_inst_num.add_signal();
798   _operand_op_name.add_signal();
799 }
add_operand(int inst_num,char * inst_operand)800 void  PeepReplace::add_operand( int inst_num, char *inst_operand ) {
801   _instruction.add_signal();
802   _operand_inst_num.addName((char*) (intptr_t) inst_num);
803   _operand_op_name.addName(inst_operand);
804 }
805 
806 // Access contents of peepreplace
reset()807 void  PeepReplace::reset() {
808   _instruction.reset();
809   _operand_inst_num.reset();
810   _operand_op_name.reset();
811 }
next_instruction(const char * & inst)812 void  PeepReplace::next_instruction(const char* &inst){
813   inst                     = _instruction.iter();
814   int         inst_num     = (int) (intptr_t) _operand_inst_num.iter();
815   const char* inst_operand = _operand_op_name.iter();
816 }
next_operand(int & inst_num,const char * & inst_operand)817 void  PeepReplace::next_operand(int &inst_num, const char* &inst_operand) {
818   const char* inst = _instruction.iter();
819   inst_num         = (int) (intptr_t) _operand_inst_num.iter();
820   inst_operand     = _operand_op_name.iter();
821 }
822 
823 
824 
dump()825 void PeepReplace::dump() {
826   output(stderr);
827 }
828 
output(FILE * fp)829 void PeepReplace::output(FILE *fp) {      // Write info to output files
830   fprintf(fp,"PeepReplace:\n");
831 }
832