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