1 /*
  2  * Copyright (c) 2000, 2023, 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 #ifndef SHARE_OPTO_OUTPUT_HPP
 26 #define SHARE_OPTO_OUTPUT_HPP
 27 
 28 #include "code/debugInfo.hpp"
 29 #include "code/exceptionHandlerTable.hpp"
 30 #include "metaprogramming/enableIf.hpp"
 31 #include "opto/ad.hpp"
 32 #include "opto/c2_CodeStubs.hpp"
 33 #include "opto/constantTable.hpp"
 34 #include "opto/phase.hpp"
 35 #include "runtime/vm_version.hpp"
 36 #include "utilities/globalDefinitions.hpp"
 37 #include "utilities/macros.hpp"
 38 
 39 class AbstractCompiler;
 40 class Arena;
 41 class Bundle;
 42 class Block;
 43 class Block_Array;
 44 class ciMethod;
 45 class Compile;
 46 class MachNode;
 47 class MachSafePointNode;
 48 class Node;
 49 class PhaseCFG;
 50 #ifndef PRODUCT
 51 #define DEBUG_ARG(x) , x
 52 #else
 53 #define DEBUG_ARG(x)
 54 #endif
 55 
 56 // Define the initial sizes for allocation of the resizable code buffer
 57 enum {
 58   initial_const_capacity =   4 * 1024
 59 };
 60 
 61 class BufferSizingData {
 62 public:
 63   int _stub;
 64   int _code;
 65   int _const;
 66   int _reloc;
 67 
 68   BufferSizingData() :
 69     _stub(0),
 70     _code(0),
 71     _const(0),
 72     _reloc(0)
 73   { };
 74 };
 75 
 76 class PhaseOutput : public Phase {
 77 private:
 78   // Instruction bits passed off to the VM
 79   int                    _method_size;           // Size of nmethod code segment in bytes
 80   CodeBuffer             _code_buffer;           // Where the code is assembled
 81   int                    _first_block_size;      // Size of unvalidated entry point code / OSR poison code
 82   ExceptionHandlerTable  _handler_table;         // Table of native-code exception handlers
 83   ImplicitExceptionTable _inc_table;             // Table of implicit null checks in native code
 84   C2CodeStubList         _stub_list;             // List of code stubs
 85   OopMapSet*             _oop_map_set;           // Table of oop maps (one for each safepoint location)
 86   BufferBlob*            _scratch_buffer_blob;   // For temporary code buffers.
 87   relocInfo*             _scratch_locs_memory;   // For temporary code buffers.
 88   int                    _scratch_const_size;    // For temporary code buffers.
 89   bool                   _in_scratch_emit_size;  // true when in scratch_emit_size.
 90 
 91   int                    _frame_slots;           // Size of total frame in stack slots
 92   CodeOffsets            _code_offsets;          // Offsets into the code for various interesting entries
 93 
 94   uint                   _node_bundling_limit;
 95   Bundle*                _node_bundling_base;    // Information for instruction bundling
 96 
 97   // For deopt
 98   int                    _orig_pc_slot;
 99   int                    _orig_pc_slot_offset_in_bytes;
100 
101   ConstantTable          _constant_table;        // The constant table for this compilation unit.
102 
103   BufferSizingData       _buf_sizes;
104   Block*                 _block;
105   uint                   _index;
106 
107   void perform_mach_node_analysis();
108   void pd_perform_mach_node_analysis();
109 
110 public:
111   PhaseOutput();
112   ~PhaseOutput();
113 
114   // Convert Nodes to instruction bits and pass off to the VM
115   void Output();
116   bool need_stack_bang(int frame_size_in_bytes) const;
117   bool need_register_stack_bang() const;
118   void compute_loop_first_inst_sizes();
119 
120   void install_code(ciMethod*         target,
121                     int               entry_bci,
122                     AbstractCompiler* compiler,
123                     bool              has_unsafe_access,
124                     bool              has_wide_vectors,
125                     RTMState          rtm_state);
126 
127   void install_stub(const char* stub_name);
128 
129   // Constant table
130   ConstantTable& constant_table() { return _constant_table; }
131 
132   // Code stubs list
133   void add_stub(C2CodeStub* stub) { _stub_list.add_stub(stub); }
134 
135   // Code emission iterator
136   Block* block()   { return _block; }
137   int index()      { return _index; }
138 
139   // The architecture description provides short branch variants for some long
140   // branch instructions. Replace eligible long branches with short branches.
141   void shorten_branches(uint* blk_starts);
142   // If "objs" contains an ObjectValue whose id is "id", returns it, else null.
143   static ObjectValue* sv_for_node_id(GrowableArray<ScopeValue*> *objs, int id);
144   static void set_sv_for_object_node(GrowableArray<ScopeValue*> *objs, ObjectValue* sv);
145   void FillLocArray( int idx, MachSafePointNode* sfpt, Node *local,
146                      GrowableArray<ScopeValue*> *array,
147                      GrowableArray<ScopeValue*> *objs );
148 
149   void Process_OopMap_Node(MachNode *mach, int current_offset);
150 
151   // Initialize code buffer
152   void estimate_buffer_size(int& const_req);
153   CodeBuffer* init_buffer();
154 
155   // Write out basic block data to code buffer
156   void fill_buffer(CodeBuffer* cb, uint* blk_starts);
157 
158   // Compute the information for the exception tables
159   void FillExceptionTables(uint cnt, uint *call_returns, uint *inct_starts, Label *blk_labels);
160 
161   // Perform instruction scheduling and bundling over the sequence of
162   // instructions in backwards order.
163   void ScheduleAndBundle();
164 
165   void install();
166 
167   // Instruction bits passed off to the VM
168   int               code_size()                 { return _method_size; }
169   CodeBuffer*       code_buffer()               { return &_code_buffer; }
170   int               first_block_size()          { return _first_block_size; }
171   void              set_frame_complete(int off) { if (!in_scratch_emit_size()) { _code_offsets.set_value(CodeOffsets::Frame_Complete, off); } }
172   ExceptionHandlerTable*  handler_table()       { return &_handler_table; }
173   ImplicitExceptionTable* inc_table()           { return &_inc_table; }
174   OopMapSet*        oop_map_set()               { return _oop_map_set; }
175 
176   // Scratch buffer
177   BufferBlob*       scratch_buffer_blob()       { return _scratch_buffer_blob; }
178   void         init_scratch_buffer_blob(int const_size);
179   void        clear_scratch_buffer_blob();
180   void          set_scratch_buffer_blob(BufferBlob* b) { _scratch_buffer_blob = b; }
181   relocInfo*        scratch_locs_memory()       { return _scratch_locs_memory; }
182   void          set_scratch_locs_memory(relocInfo* b)  { _scratch_locs_memory = b; }
183   int               scratch_buffer_code_size()  { return (address)scratch_locs_memory() - _scratch_buffer_blob->content_begin(); }
184 
185   // emit to scratch blob, report resulting size
186   uint              scratch_emit_size(const Node* n);
187   void       set_in_scratch_emit_size(bool x)   {        _in_scratch_emit_size = x; }
188   bool           in_scratch_emit_size() const   { return _in_scratch_emit_size;     }
189 
190   enum ScratchBufferBlob {
191     MAX_inst_size       = 2048,
192     MAX_locs_size       = 128, // number of relocInfo elements
193     MAX_const_size      = 128,
194     MAX_stubs_size      = 128
195   };
196 
197   int               frame_slots() const         { return _frame_slots; }
198   int               frame_size_in_words() const; // frame_slots in units of the polymorphic 'words'
199   int               frame_size_in_bytes() const { return _frame_slots << LogBytesPerInt; }
200 
201   int               bang_size_in_bytes() const;
202 
203   uint              node_bundling_limit();
204   Bundle*           node_bundling_base();
205   void          set_node_bundling_limit(uint n) { _node_bundling_limit = n; }
206   void          set_node_bundling_base(Bundle* b) { _node_bundling_base = b; }
207 
208   Bundle* node_bundling(const Node *n);
209   bool valid_bundle_info(const Node *n);
210 
211   bool starts_bundle(const Node *n) const;
212 
213   // Dump formatted assembly
214 #if defined(SUPPORT_OPTO_ASSEMBLY)
215   void dump_asm_on(outputStream* ost, int* pcs, uint pc_limit);
216   void dump_asm(int* pcs = NULL, uint pc_limit = 0) { dump_asm_on(tty, pcs, pc_limit); }
217 #else
218   void dump_asm_on(outputStream* ost, int* pcs, uint pc_limit) { return; }
219   void dump_asm(int* pcs = NULL, uint pc_limit = 0) { return; }
220 #endif
221 
222   // Build OopMaps for each GC point
223   void BuildOopMaps();
224 
225 #ifndef PRODUCT
226   static void print_statistics();
227 #endif
228 };
229 
230 #endif // SHARE_OPTO_OUTPUT_HPP