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