1 /*
  2  * Copyright (c) 1997, 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_CFGNODE_HPP
 26 #define SHARE_OPTO_CFGNODE_HPP
 27 
 28 #include "opto/multnode.hpp"
 29 #include "opto/node.hpp"
 30 #include "opto/opcodes.hpp"
 31 #include "opto/type.hpp"
 32 
 33 // Portions of code courtesy of Clifford Click
 34 
 35 // Optimization - Graph Style
 36 
 37 class Matcher;
 38 class Node;
 39 class   RegionNode;
 40 class   TypeNode;
 41 class     PhiNode;
 42 class   GotoNode;
 43 class   MultiNode;
 44 class     MultiBranchNode;
 45 class       IfNode;
 46 class       PCTableNode;
 47 class         JumpNode;
 48 class         CatchNode;
 49 class       NeverBranchNode;
 50 class     BlackholeNode;
 51 class   ProjNode;
 52 class     CProjNode;
 53 class       IfTrueNode;
 54 class       IfFalseNode;
 55 class       CatchProjNode;
 56 class     JProjNode;
 57 class       JumpProjNode;
 58 class     SCMemProjNode;
 59 class PhaseIdealLoop;
 60 
 61 // The success projection of a Parse Predicate is always an IfTrueNode and the uncommon projection an IfFalseNode
 62 typedef IfTrueNode ParsePredicateSuccessProj;
 63 typedef IfFalseNode ParsePredicateUncommonProj;
 64 
 65 //------------------------------RegionNode-------------------------------------
 66 // The class of RegionNodes, which can be mapped to basic blocks in the
 67 // program.  Their inputs point to Control sources.  PhiNodes (described
 68 // below) have an input point to a RegionNode.  Merged data inputs to PhiNodes
 69 // correspond 1-to-1 with RegionNode inputs.  The zero input of a PhiNode is
 70 // the RegionNode, and the zero input of the RegionNode is itself.
 71 class RegionNode : public Node {
 72 public:
 73   enum LoopStatus {
 74     // No guarantee: the region may be an irreducible loop entry, thus we have to
 75     // be careful when removing entry control to it.
 76     MaybeIrreducibleEntry,
 77     // Limited guarantee: this region may be (nested) inside an irreducible loop,
 78     // but it will never be an irreducible loop entry.
 79     NeverIrreducibleEntry,
 80     // Strong guarantee: this region is not (nested) inside an irreducible loop.
 81     Reducible,
 82   };
 83 
 84 private:
 85   bool _is_unreachable_region;
 86   LoopStatus _loop_status;
 87 
 88   bool is_possible_unsafe_loop(const PhaseGVN* phase) const;
 89   bool is_unreachable_from_root(const PhaseGVN* phase) const;
 90 public:
 91   // Node layout (parallels PhiNode):
 92   enum { Region,                // Generally points to self.
 93          Control                // Control arcs are [1..len)
 94   };
 95 
 96   RegionNode(uint required)
 97     : Node(required),
 98       _is_unreachable_region(false),
 99       _loop_status(LoopStatus::NeverIrreducibleEntry)
100   {
101     init_class_id(Class_Region);
102     init_req(0, this);
103   }
104 
105   Node* is_copy() const {
106     const Node* r = _in[Region];
107     if (r == nullptr)
108       return nonnull_req();
109     return nullptr;  // not a copy!
110   }
111   PhiNode* has_phi() const;        // returns an arbitrary phi user, or null
112   PhiNode* has_unique_phi() const; // returns the unique phi user, or null
113   // Is this region node unreachable from root?
114   bool is_unreachable_region(const PhaseGVN* phase);
115 #ifdef ASSERT
116   bool is_in_infinite_subgraph();
117   static bool are_all_nodes_in_infinite_subgraph(Unique_Node_List& worklist);
118 #endif //ASSERT
119   LoopStatus loop_status() const { return _loop_status; };
120   void set_loop_status(LoopStatus status);
121   DEBUG_ONLY(void verify_can_be_irreducible_entry() const;)
122 
123   virtual int Opcode() const;
124   virtual uint size_of() const { return sizeof(*this); }
125   virtual bool pinned() const { return (const Node*)in(0) == this; }
126   virtual bool is_CFG() const { return true; }
127   virtual uint hash() const { return NO_HASH; } // CFG nodes do not hash
128   virtual bool depends_only_on_test() const { return false; }
129   virtual const Type* bottom_type() const { return Type::CONTROL; }
130   virtual const Type* Value(PhaseGVN* phase) const;
131   virtual Node* Identity(PhaseGVN* phase);
132   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
133   void remove_unreachable_subgraph(PhaseIterGVN* igvn);
134   virtual const RegMask &out_RegMask() const;
135   bool is_diamond() const;
136   void try_clean_mem_phis(PhaseIterGVN* phase);
137   bool optimize_trichotomy(PhaseIterGVN* igvn);
138   NOT_PRODUCT(virtual void dump_spec(outputStream* st) const;)
139 };
140 
141 //------------------------------JProjNode--------------------------------------
142 // jump projection for node that produces multiple control-flow paths
143 class JProjNode : public ProjNode {
144  public:
145   JProjNode( Node* ctrl, uint idx ) : ProjNode(ctrl,idx) {}
146   virtual int Opcode() const;
147   virtual bool  is_CFG() const { return true; }
148   virtual uint  hash() const { return NO_HASH; }  // CFG nodes do not hash
149   virtual const Node* is_block_proj() const { return in(0); }
150   virtual const RegMask& out_RegMask() const;
151   virtual uint  ideal_reg() const { return 0; }
152 };
153 
154 //------------------------------PhiNode----------------------------------------
155 // PhiNodes merge values from different Control paths.  Slot 0 points to the
156 // controlling RegionNode.  Other slots map 1-for-1 with incoming control flow
157 // paths to the RegionNode.
158 class PhiNode : public TypeNode {
159   friend class PhaseRenumberLive;
160 
161   const TypePtr* const _adr_type; // non-null only for Type::MEMORY nodes.
162   // The following fields are only used for data PhiNodes to indicate
163   // that the PhiNode represents the value of a known instance field.
164         int _inst_mem_id; // Instance memory id (node index of the memory Phi)
165         int _inst_id;     // Instance id of the memory slice.
166   const int _inst_index;  // Alias index of the instance memory slice.
167   // Array elements references have the same alias_idx but different offset.
168   const int _inst_offset; // Offset of the instance memory slice.
169   // Size is bigger to hold the _adr_type field.
170   virtual uint hash() const;    // Check the type
171   virtual bool cmp( const Node &n ) const;
172   virtual uint size_of() const { return sizeof(*this); }
173 
174   // Determine if CMoveNode::is_cmove_id can be used at this join point.
175   Node* is_cmove_id(PhaseTransform* phase, int true_path);
176   bool wait_for_region_igvn(PhaseGVN* phase);
177   bool is_data_loop(RegionNode* r, Node* uin, const PhaseGVN* phase);
178 
179   static Node* clone_through_phi(Node* root_phi, const Type* t, uint c, PhaseIterGVN* igvn);
180   static Node* merge_through_phi(Node* root_phi, PhaseIterGVN* igvn);
181 
182   bool must_wait_for_region_in_irreducible_loop(PhaseGVN* phase) const;
183 
184   bool can_push_inline_types_down(PhaseGVN* phase, bool can_reshape, ciInlineKlass*& inline_klass);
185   InlineTypeNode* push_inline_types_down(PhaseGVN* phase, bool can_reshape, ciInlineKlass* inline_klass);
186 
187 public:
188   // Node layout (parallels RegionNode):
189   enum { Region,                // Control input is the Phi's region.
190          Input                  // Input values are [1..len)
191   };
192 
193   PhiNode( Node *r, const Type *t, const TypePtr* at = nullptr,
194            const int imid = -1,
195            const int iid = TypeOopPtr::InstanceTop,
196            const int iidx = Compile::AliasIdxTop,
197            const int ioffs = Type::OffsetTop )
198     : TypeNode(t,r->req()),
199       _adr_type(at),
200       _inst_mem_id(imid),
201       _inst_id(iid),
202       _inst_index(iidx),
203       _inst_offset(ioffs)
204   {
205     init_class_id(Class_Phi);
206     init_req(0, r);
207     verify_adr_type();
208   }
209   // create a new phi with in edges matching r and set (initially) to x
210   static PhiNode* make( Node* r, Node* x );
211   // extra type arguments override the new phi's bottom_type and adr_type
212   static PhiNode* make( Node* r, Node* x, const Type *t, const TypePtr* at = nullptr );
213   // create a new phi with narrowed memory type
214   PhiNode* slice_memory(const TypePtr* adr_type) const;
215   PhiNode* split_out_instance(const TypePtr* at, PhaseIterGVN *igvn) const;
216   // like make(r, x), but does not initialize the in edges to x
217   static PhiNode* make_blank( Node* r, Node* x );
218 
219   // Accessors
220   RegionNode* region() const { Node* r = in(Region); assert(!r || r->is_Region(), ""); return (RegionNode*)r; }
221 
222   bool is_tripcount(BasicType bt) const;
223 
224   // Determine a unique non-trivial input, if any.
225   // Ignore casts if it helps.  Return null on failure.
226   Node* unique_input(PhaseValues* phase, bool uncast);
227   Node* unique_input(PhaseValues* phase) {
228     Node* uin = unique_input(phase, false);
229     if (uin == nullptr) {
230       uin = unique_input(phase, true);
231     }
232     return uin;
233   }
234 
235   // Check for a simple dead loop.
236   enum LoopSafety { Safe = 0, Unsafe, UnsafeLoop };
237   LoopSafety simple_data_loop_check(Node *in) const;
238   // Is it unsafe data loop? It becomes a dead loop if this phi node removed.
239   bool is_unsafe_data_reference(Node *in) const;
240   int is_diamond_phi() const;
241   bool try_clean_memory_phi(PhaseIterGVN* igvn);
242   virtual int Opcode() const;
243   virtual bool pinned() const { return in(0) != 0; }
244   virtual const TypePtr *adr_type() const { verify_adr_type(true); return _adr_type; }
245 
246   void  set_inst_mem_id(int inst_mem_id) { _inst_mem_id = inst_mem_id; }
247   int inst_mem_id() const { return _inst_mem_id; }
248   int inst_id()     const { return _inst_id; }
249   int inst_index()  const { return _inst_index; }
250   int inst_offset() const { return _inst_offset; }
251   bool is_same_inst_field(const Type* tp, int mem_id, int id, int index, int offset) {
252     return type()->basic_type() == tp->basic_type() &&
253            inst_mem_id() == mem_id &&
254            inst_id()     == id     &&
255            inst_index()  == index  &&
256            inst_offset() == offset &&
257            type()->higher_equal(tp);
258   }
259 
260   bool can_be_inline_type() const {
261     return EnableValhalla && _type->isa_instptr() && _type->is_instptr()->can_be_inline_type();
262   }
263 
264   Node* try_push_inline_types_down(PhaseGVN* phase, bool can_reshape);
265 
266   virtual const Type* Value(PhaseGVN* phase) const;
267   virtual Node* Identity(PhaseGVN* phase);
268   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
269   virtual const RegMask &out_RegMask() const;
270   virtual const RegMask &in_RegMask(uint) const;
271 #ifndef PRODUCT
272   virtual void dump_spec(outputStream *st) const;
273 #endif
274 #ifdef ASSERT
275   void verify_adr_type(VectorSet& visited, const TypePtr* at) const;
276   void verify_adr_type(bool recursive = false) const;
277 #else //ASSERT
278   void verify_adr_type(bool recursive = false) const {}
279 #endif //ASSERT
280 
281   const TypeTuple* collect_types(PhaseGVN* phase) const;
282 };
283 
284 //------------------------------GotoNode---------------------------------------
285 // GotoNodes perform direct branches.
286 class GotoNode : public Node {
287 public:
288   GotoNode( Node *control ) : Node(control) {}
289   virtual int Opcode() const;
290   virtual bool pinned() const { return true; }
291   virtual bool  is_CFG() const { return true; }
292   virtual uint hash() const { return NO_HASH; }  // CFG nodes do not hash
293   virtual const Node *is_block_proj() const { return this; }
294   virtual bool depends_only_on_test() const { return false; }
295   virtual const Type *bottom_type() const { return Type::CONTROL; }
296   virtual const Type* Value(PhaseGVN* phase) const;
297   virtual Node* Identity(PhaseGVN* phase);
298   virtual const RegMask &out_RegMask() const;
299 };
300 
301 //------------------------------CProjNode--------------------------------------
302 // control projection for node that produces multiple control-flow paths
303 class CProjNode : public ProjNode {
304 public:
305   CProjNode( Node *ctrl, uint idx ) : ProjNode(ctrl,idx) {}
306   virtual int Opcode() const;
307   virtual bool  is_CFG() const { return true; }
308   virtual uint hash() const { return NO_HASH; }  // CFG nodes do not hash
309   virtual const Node *is_block_proj() const { return in(0); }
310   virtual const RegMask &out_RegMask() const;
311   virtual uint ideal_reg() const { return 0; }
312 };
313 
314 //---------------------------MultiBranchNode-----------------------------------
315 // This class defines a MultiBranchNode, a MultiNode which yields multiple
316 // control values. These are distinguished from other types of MultiNodes
317 // which yield multiple values, but control is always and only projection #0.
318 class MultiBranchNode : public MultiNode {
319 public:
320   MultiBranchNode( uint required ) : MultiNode(required) {
321     init_class_id(Class_MultiBranch);
322   }
323   // returns required number of users to be well formed.
324   virtual int required_outcnt() const = 0;
325 };
326 
327 //------------------------------IfNode-----------------------------------------
328 // Output selected Control, based on a boolean test
329 class IfNode : public MultiBranchNode {
330   // Size is bigger to hold the probability field.  However, _prob does not
331   // change the semantics so it does not appear in the hash & cmp functions.
332   virtual uint size_of() const { return sizeof(*this); }
333 
334 private:
335   // Helper methods for fold_compares
336   bool cmpi_folds(PhaseIterGVN* igvn, bool fold_ne = false);
337   bool is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn);
338   bool has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail);
339   bool has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn);
340   Node* merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn);
341   static void improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn);
342   bool is_cmp_with_loadrange(ProjNode* proj);
343   bool is_null_check(ProjNode* proj, PhaseIterGVN* igvn);
344   bool is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn);
345   void reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn);
346   ProjNode* uncommon_trap_proj(CallStaticJavaNode*& call) const;
347   bool fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn);
348   static bool is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc);
349 
350 protected:
351   ProjNode* range_check_trap_proj(int& flip, Node*& l, Node*& r);
352   Node* Ideal_common(PhaseGVN *phase, bool can_reshape);
353   Node* search_identical(int dist, PhaseIterGVN* igvn);
354 
355   Node* simple_subsuming(PhaseIterGVN* igvn);
356 
357 public:
358 
359   // Degrees of branch prediction probability by order of magnitude:
360   // PROB_UNLIKELY_1e(N) is a 1 in 1eN chance.
361   // PROB_LIKELY_1e(N) is a 1 - PROB_UNLIKELY_1e(N)
362 #define PROB_UNLIKELY_MAG(N)    (1e- ## N ## f)
363 #define PROB_LIKELY_MAG(N)      (1.0f-PROB_UNLIKELY_MAG(N))
364 
365   // Maximum and minimum branch prediction probabilties
366   // 1 in 1,000,000 (magnitude 6)
367   //
368   // Although PROB_NEVER == PROB_MIN and PROB_ALWAYS == PROB_MAX
369   // they are used to distinguish different situations:
370   //
371   // The name PROB_MAX (PROB_MIN) is for probabilities which correspond to
372   // very likely (unlikely) but with a concrete possibility of a rare
373   // contrary case.  These constants would be used for pinning
374   // measurements, and as measures for assertions that have high
375   // confidence, but some evidence of occasional failure.
376   //
377   // The name PROB_ALWAYS (PROB_NEVER) is to stand for situations for which
378   // there is no evidence at all that the contrary case has ever occurred.
379 
380 #define PROB_NEVER              PROB_UNLIKELY_MAG(6)
381 #define PROB_ALWAYS             PROB_LIKELY_MAG(6)
382 
383 #define PROB_MIN                PROB_UNLIKELY_MAG(6)
384 #define PROB_MAX                PROB_LIKELY_MAG(6)
385 
386   // Static branch prediction probabilities
387   // 1 in 10 (magnitude 1)
388 #define PROB_STATIC_INFREQUENT  PROB_UNLIKELY_MAG(1)
389 #define PROB_STATIC_FREQUENT    PROB_LIKELY_MAG(1)
390 
391   // Fair probability 50/50
392 #define PROB_FAIR               (0.5f)
393 
394   // Unknown probability sentinel
395 #define PROB_UNKNOWN            (-1.0f)
396 
397   // Probability "constructors", to distinguish as a probability any manifest
398   // constant without a names
399 #define PROB_LIKELY(x)          ((float) (x))
400 #define PROB_UNLIKELY(x)        (1.0f - (float)(x))
401 
402   // Other probabilities in use, but without a unique name, are documented
403   // here for lack of a better place:
404   //
405   // 1 in 1000 probabilities (magnitude 3):
406   //     threshold for converting to conditional move
407   //     likelihood of null check failure if a null HAS been seen before
408   //     likelihood of slow path taken in library calls
409   //
410   // 1 in 10,000 probabilities (magnitude 4):
411   //     threshold for making an uncommon trap probability more extreme
412   //     threshold for for making a null check implicit
413   //     likelihood of needing a gc if eden top moves during an allocation
414   //     likelihood of a predicted call failure
415   //
416   // 1 in 100,000 probabilities (magnitude 5):
417   //     threshold for ignoring counts when estimating path frequency
418   //     likelihood of FP clipping failure
419   //     likelihood of catching an exception from a try block
420   //     likelihood of null check failure if a null has NOT been seen before
421   //
422   // Magic manifest probabilities such as 0.83, 0.7, ... can be found in
423   // gen_subtype_check() and catch_inline_exceptions().
424 
425   float _prob;                  // Probability of true path being taken.
426   float _fcnt;                  // Frequency counter
427   IfNode( Node *control, Node *b, float p, float fcnt )
428     : MultiBranchNode(2), _prob(p), _fcnt(fcnt) {
429     init_class_id(Class_If);
430     init_req(0,control);
431     init_req(1,b);
432   }
433   virtual int Opcode() const;
434   virtual bool pinned() const { return true; }
435   virtual const Type *bottom_type() const { return TypeTuple::IFBOTH; }
436   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
437   virtual const Type* Value(PhaseGVN* phase) const;
438   virtual int required_outcnt() const { return 2; }
439   virtual const RegMask &out_RegMask() const;
440   Node* fold_compares(PhaseIterGVN* phase);
441   static Node* up_one_dom(Node* curr, bool linear_only = false);
442   Node* dominated_by(Node* prev_dom, PhaseIterGVN* igvn);
443   bool is_zero_trip_guard() const;
444 
445   // Takes the type of val and filters it through the test represented
446   // by if_proj and returns a more refined type if one is produced.
447   // Returns null is it couldn't improve the type.
448   static const TypeInt* filtered_int_type(PhaseGVN* phase, Node* val, Node* if_proj);
449 
450   bool is_flat_array_check(PhaseTransform* phase, Node** array = nullptr);
451 
452 #ifndef PRODUCT
453   virtual void dump_spec(outputStream *st) const;
454 #endif
455 
456   bool same_condition(const Node* dom, PhaseIterGVN* igvn) const;
457 };
458 
459 class RangeCheckNode : public IfNode {
460 private:
461   int is_range_check(Node* &range, Node* &index, jint &offset);
462 
463 public:
464   RangeCheckNode(Node* control, Node *b, float p, float fcnt)
465     : IfNode(control, b, p, fcnt) {
466     init_class_id(Class_RangeCheck);
467   }
468 
469   virtual int Opcode() const;
470   virtual Node* Ideal(PhaseGVN *phase, bool can_reshape);
471 };
472 
473 // Special node that denotes a Parse Predicate added during parsing. A Parse Predicate serves as placeholder to later
474 // create Regular Predicates (Runtime Predicates with possible Assertion Predicates) above it. Together they form a
475 // Predicate Block. The Parse Predicate and Regular Predicates share the same uncommon trap.
476 // There are three kinds of Parse Predicates:
477 // Loop Parse Predicate, Profiled Loop Parse Predicate (both used by Loop Predication), and Loop Limit Check Parse
478 // Predicate (used for integer overflow checks when creating a counted loop).
479 // More information about predicates can be found in loopPredicate.cpp.
480 class ParsePredicateNode : public IfNode {
481   Deoptimization::DeoptReason _deopt_reason;
482   bool _useless; // If the associated loop dies, this parse predicate becomes useless and can be cleaned up by Value().
483  public:
484   ParsePredicateNode(Node* control, Deoptimization::DeoptReason deopt_reason, PhaseGVN* gvn);
485   virtual int Opcode() const;
486   virtual uint size_of() const { return sizeof(*this); }
487 
488   Deoptimization::DeoptReason deopt_reason() const {
489     return _deopt_reason;
490   }
491 
492   bool is_useless() const {
493     return _useless;
494   }
495 
496   void mark_useless() {
497     _useless = true;
498   }
499 
500   void mark_useful() {
501     _useless = false;
502   }
503 
504   Node* uncommon_trap() const;
505 
506   Node* Ideal(PhaseGVN* phase, bool can_reshape) {
507     return nullptr; // Don't optimize
508   }
509 
510   const Type* Value(PhaseGVN* phase) const;
511   NOT_PRODUCT(void dump_spec(outputStream* st) const;)
512 };
513 
514 class IfProjNode : public CProjNode {
515 public:
516   IfProjNode(IfNode *ifnode, uint idx) : CProjNode(ifnode,idx) {}
517   virtual Node* Identity(PhaseGVN* phase);
518 
519 protected:
520   // Type of If input when this branch is always taken
521   virtual bool always_taken(const TypeTuple* t) const = 0;
522 };
523 
524 class IfTrueNode : public IfProjNode {
525 public:
526   IfTrueNode( IfNode *ifnode ) : IfProjNode(ifnode,1) {
527     init_class_id(Class_IfTrue);
528   }
529   virtual int Opcode() const;
530 
531 protected:
532   virtual bool always_taken(const TypeTuple* t) const { return t == TypeTuple::IFTRUE; }
533 };
534 
535 class IfFalseNode : public IfProjNode {
536 public:
537   IfFalseNode( IfNode *ifnode ) : IfProjNode(ifnode,0) {
538     init_class_id(Class_IfFalse);
539   }
540   virtual int Opcode() const;
541 
542 protected:
543   virtual bool always_taken(const TypeTuple* t) const { return t == TypeTuple::IFFALSE; }
544 };
545 
546 
547 //------------------------------PCTableNode------------------------------------
548 // Build an indirect branch table.  Given a control and a table index,
549 // control is passed to the Projection matching the table index.  Used to
550 // implement switch statements and exception-handling capabilities.
551 // Undefined behavior if passed-in index is not inside the table.
552 class PCTableNode : public MultiBranchNode {
553   virtual uint hash() const;    // Target count; table size
554   virtual bool cmp( const Node &n ) const;
555   virtual uint size_of() const { return sizeof(*this); }
556 
557 public:
558   const uint _size;             // Number of targets
559 
560   PCTableNode( Node *ctrl, Node *idx, uint size ) : MultiBranchNode(2), _size(size) {
561     init_class_id(Class_PCTable);
562     init_req(0, ctrl);
563     init_req(1, idx);
564   }
565   virtual int Opcode() const;
566   virtual const Type* Value(PhaseGVN* phase) const;
567   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
568   virtual const Type *bottom_type() const;
569   virtual bool pinned() const { return true; }
570   virtual int required_outcnt() const { return _size; }
571 };
572 
573 //------------------------------JumpNode---------------------------------------
574 // Indirect branch.  Uses PCTable above to implement a switch statement.
575 // It emits as a table load and local branch.
576 class JumpNode : public PCTableNode {
577   virtual uint size_of() const { return sizeof(*this); }
578 public:
579   float* _probs; // probability of each projection
580   float _fcnt;   // total number of times this Jump was executed
581   JumpNode( Node* control, Node* switch_val, uint size, float* probs, float cnt)
582     : PCTableNode(control, switch_val, size),
583       _probs(probs), _fcnt(cnt) {
584     init_class_id(Class_Jump);
585   }
586   virtual int   Opcode() const;
587   virtual const RegMask& out_RegMask() const;
588   virtual const Node* is_block_proj() const { return this; }
589 };
590 
591 class JumpProjNode : public JProjNode {
592   virtual uint hash() const;
593   virtual bool cmp( const Node &n ) const;
594   virtual uint size_of() const { return sizeof(*this); }
595 
596  private:
597   const int  _dest_bci;
598   const uint _proj_no;
599   const int  _switch_val;
600  public:
601   JumpProjNode(Node* jumpnode, uint proj_no, int dest_bci, int switch_val)
602     : JProjNode(jumpnode, proj_no), _dest_bci(dest_bci), _proj_no(proj_no), _switch_val(switch_val) {
603     init_class_id(Class_JumpProj);
604   }
605 
606   virtual int Opcode() const;
607   virtual const Type* bottom_type() const { return Type::CONTROL; }
608   int  dest_bci()    const { return _dest_bci; }
609   int  switch_val()  const { return _switch_val; }
610   uint proj_no()     const { return _proj_no; }
611 #ifndef PRODUCT
612   virtual void dump_spec(outputStream *st) const;
613   virtual void dump_compact_spec(outputStream *st) const;
614 #endif
615 };
616 
617 //------------------------------CatchNode--------------------------------------
618 // Helper node to fork exceptions.  "Catch" catches any exceptions thrown by
619 // a just-prior call.  Looks like a PCTableNode but emits no code - just the
620 // table.  The table lookup and branch is implemented by RethrowNode.
621 class CatchNode : public PCTableNode {
622 public:
623   CatchNode( Node *ctrl, Node *idx, uint size ) : PCTableNode(ctrl,idx,size){
624     init_class_id(Class_Catch);
625   }
626   virtual int Opcode() const;
627   virtual const Type* Value(PhaseGVN* phase) const;
628 };
629 
630 // CatchProjNode controls which exception handler is targeted after a call.
631 // It is passed in the bci of the target handler, or no_handler_bci in case
632 // the projection doesn't lead to an exception handler.
633 class CatchProjNode : public CProjNode {
634   virtual uint hash() const;
635   virtual bool cmp( const Node &n ) const;
636   virtual uint size_of() const { return sizeof(*this); }
637 
638 private:
639   const int _handler_bci;
640 
641 public:
642   enum {
643     fall_through_index =  0,      // the fall through projection index
644     catch_all_index    =  1,      // the projection index for catch-alls
645     no_handler_bci     = -1       // the bci for fall through or catch-all projs
646   };
647 
648   CatchProjNode(Node* catchnode, uint proj_no, int handler_bci)
649     : CProjNode(catchnode, proj_no), _handler_bci(handler_bci) {
650     init_class_id(Class_CatchProj);
651     assert(proj_no != fall_through_index || handler_bci < 0, "fall through case must have bci < 0");
652   }
653 
654   virtual int Opcode() const;
655   virtual Node* Identity(PhaseGVN* phase);
656   virtual const Type *bottom_type() const { return Type::CONTROL; }
657   int  handler_bci() const        { return _handler_bci; }
658   bool is_handler_proj() const    { return _handler_bci >= 0; }
659 #ifndef PRODUCT
660   virtual void dump_spec(outputStream *st) const;
661 #endif
662 };
663 
664 
665 //---------------------------------CreateExNode--------------------------------
666 // Helper node to create the exception coming back from a call
667 class CreateExNode : public TypeNode {
668 public:
669   CreateExNode(const Type* t, Node* control, Node* i_o) : TypeNode(t, 2) {
670     init_req(0, control);
671     init_req(1, i_o);
672   }
673   virtual int Opcode() const;
674   virtual Node* Identity(PhaseGVN* phase);
675   virtual bool pinned() const { return true; }
676   uint match_edge(uint idx) const { return 0; }
677   virtual uint ideal_reg() const { return Op_RegP; }
678 };
679 
680 //------------------------------NeverBranchNode-------------------------------
681 // The never-taken branch.  Used to give the appearance of exiting infinite
682 // loops to those algorithms that like all paths to be reachable.  Encodes
683 // empty.
684 class NeverBranchNode : public MultiBranchNode {
685 public:
686   NeverBranchNode(Node* ctrl) : MultiBranchNode(1) {
687     init_req(0, ctrl);
688     init_class_id(Class_NeverBranch);
689   }
690   virtual int Opcode() const;
691   virtual bool pinned() const { return true; };
692   virtual const Type *bottom_type() const { return TypeTuple::IFBOTH; }
693   virtual const Type* Value(PhaseGVN* phase) const;
694   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
695   virtual int required_outcnt() const { return 2; }
696   virtual void emit(CodeBuffer &cbuf, PhaseRegAlloc *ra_) const { }
697   virtual uint size(PhaseRegAlloc *ra_) const { return 0; }
698 #ifndef PRODUCT
699   virtual void format( PhaseRegAlloc *, outputStream *st ) const;
700 #endif
701 };
702 
703 //------------------------------BlackholeNode----------------------------
704 // Blackhole all arguments. This node would survive through the compiler
705 // the effects on its arguments, and would be finally matched to nothing.
706 class BlackholeNode : public MultiNode {
707 public:
708   BlackholeNode(Node* ctrl) : MultiNode(1) {
709     init_req(TypeFunc::Control, ctrl);
710     init_class_id(Class_Blackhole);
711   }
712   virtual int   Opcode() const;
713   virtual uint ideal_reg() const { return 0; } // not matched in the AD file
714   virtual const Type* bottom_type() const { return TypeTuple::MEMBAR; }
715 
716   const RegMask &in_RegMask(uint idx) const {
717     // Fake the incoming arguments mask for blackholes: accept all registers
718     // and all stack slots. This would avoid any redundant register moves
719     // for blackhole inputs.
720     return RegMask::All;
721   }
722 #ifndef PRODUCT
723   virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
724 #endif
725 };
726 
727 
728 #endif // SHARE_OPTO_CFGNODE_HPP