src/share/vm/opto/superword.cpp

Fri, 26 Oct 2012 11:48:04 -0700

author
kvn
date
Fri, 26 Oct 2012 11:48:04 -0700
changeset 4207
410afdc6a07c
parent 4204
b2c669fd8114
child 4620
ad736b4683b4
permissions
-rw-r--r--

8001635: assert(in_bb(n)) failed: must be
Summary: Added missed check that Load node is in processed loop block.
Reviewed-by: twisti

     1 /*
     2  * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 #include "precompiled.hpp"
    25 #include "compiler/compileLog.hpp"
    26 #include "libadt/vectset.hpp"
    27 #include "memory/allocation.inline.hpp"
    28 #include "opto/addnode.hpp"
    29 #include "opto/callnode.hpp"
    30 #include "opto/divnode.hpp"
    31 #include "opto/matcher.hpp"
    32 #include "opto/memnode.hpp"
    33 #include "opto/mulnode.hpp"
    34 #include "opto/opcodes.hpp"
    35 #include "opto/superword.hpp"
    36 #include "opto/vectornode.hpp"
    38 //
    39 //                  S U P E R W O R D   T R A N S F O R M
    40 //=============================================================================
    42 //------------------------------SuperWord---------------------------
    43 SuperWord::SuperWord(PhaseIdealLoop* phase) :
    44   _phase(phase),
    45   _igvn(phase->_igvn),
    46   _arena(phase->C->comp_arena()),
    47   _packset(arena(), 8,  0, NULL),         // packs for the current block
    48   _bb_idx(arena(), (int)(1.10 * phase->C->unique()), 0, 0), // node idx to index in bb
    49   _block(arena(), 8,  0, NULL),           // nodes in current block
    50   _data_entry(arena(), 8,  0, NULL),      // nodes with all inputs from outside
    51   _mem_slice_head(arena(), 8,  0, NULL),  // memory slice heads
    52   _mem_slice_tail(arena(), 8,  0, NULL),  // memory slice tails
    53   _node_info(arena(), 8,  0, SWNodeInfo::initial), // info needed per node
    54   _align_to_ref(NULL),                    // memory reference to align vectors to
    55   _disjoint_ptrs(arena(), 8,  0, OrderedPair::initial), // runtime disambiguated pointer pairs
    56   _dg(_arena),                            // dependence graph
    57   _visited(arena()),                      // visited node set
    58   _post_visited(arena()),                 // post visited node set
    59   _n_idx_list(arena(), 8),                // scratch list of (node,index) pairs
    60   _stk(arena(), 8, 0, NULL),              // scratch stack of nodes
    61   _nlist(arena(), 8, 0, NULL),            // scratch list of nodes
    62   _lpt(NULL),                             // loop tree node
    63   _lp(NULL),                              // LoopNode
    64   _bb(NULL),                              // basic block
    65   _iv(NULL)                               // induction var
    66 {}
    68 //------------------------------transform_loop---------------------------
    69 void SuperWord::transform_loop(IdealLoopTree* lpt) {
    70   assert(UseSuperWord, "should be");
    71   // Do vectors exist on this architecture?
    72   if (Matcher::vector_width_in_bytes(T_BYTE) < 2) return;
    74   assert(lpt->_head->is_CountedLoop(), "must be");
    75   CountedLoopNode *cl = lpt->_head->as_CountedLoop();
    77   if (!cl->is_valid_counted_loop()) return; // skip malformed counted loop
    79   if (!cl->is_main_loop() ) return; // skip normal, pre, and post loops
    81   // Check for no control flow in body (other than exit)
    82   Node *cl_exit = cl->loopexit();
    83   if (cl_exit->in(0) != lpt->_head) return;
    85   // Make sure the are no extra control users of the loop backedge
    86   if (cl->back_control()->outcnt() != 1) {
    87     return;
    88   }
    90   // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit))))
    91   CountedLoopEndNode* pre_end = get_pre_loop_end(cl);
    92   if (pre_end == NULL) return;
    93   Node *pre_opaq1 = pre_end->limit();
    94   if (pre_opaq1->Opcode() != Op_Opaque1) return;
    96   init(); // initialize data structures
    98   set_lpt(lpt);
    99   set_lp(cl);
   101   // For now, define one block which is the entire loop body
   102   set_bb(cl);
   104   assert(_packset.length() == 0, "packset must be empty");
   105   SLP_extract();
   106 }
   108 //------------------------------SLP_extract---------------------------
   109 // Extract the superword level parallelism
   110 //
   111 // 1) A reverse post-order of nodes in the block is constructed.  By scanning
   112 //    this list from first to last, all definitions are visited before their uses.
   113 //
   114 // 2) A point-to-point dependence graph is constructed between memory references.
   115 //    This simplies the upcoming "independence" checker.
   116 //
   117 // 3) The maximum depth in the node graph from the beginning of the block
   118 //    to each node is computed.  This is used to prune the graph search
   119 //    in the independence checker.
   120 //
   121 // 4) For integer types, the necessary bit width is propagated backwards
   122 //    from stores to allow packed operations on byte, char, and short
   123 //    integers.  This reverses the promotion to type "int" that javac
   124 //    did for operations like: char c1,c2,c3;  c1 = c2 + c3.
   125 //
   126 // 5) One of the memory references is picked to be an aligned vector reference.
   127 //    The pre-loop trip count is adjusted to align this reference in the
   128 //    unrolled body.
   129 //
   130 // 6) The initial set of pack pairs is seeded with memory references.
   131 //
   132 // 7) The set of pack pairs is extended by following use->def and def->use links.
   133 //
   134 // 8) The pairs are combined into vector sized packs.
   135 //
   136 // 9) Reorder the memory slices to co-locate members of the memory packs.
   137 //
   138 // 10) Generate ideal vector nodes for the final set of packs and where necessary,
   139 //    inserting scalar promotion, vector creation from multiple scalars, and
   140 //    extraction of scalar values from vectors.
   141 //
   142 void SuperWord::SLP_extract() {
   144   // Ready the block
   146   construct_bb();
   148   dependence_graph();
   150   compute_max_depth();
   152   compute_vector_element_type();
   154   // Attempt vectorization
   156   find_adjacent_refs();
   158   extend_packlist();
   160   combine_packs();
   162   construct_my_pack_map();
   164   filter_packs();
   166   schedule();
   168   output();
   169 }
   171 //------------------------------find_adjacent_refs---------------------------
   172 // Find the adjacent memory references and create pack pairs for them.
   173 // This is the initial set of packs that will then be extended by
   174 // following use->def and def->use links.  The align positions are
   175 // assigned relative to the reference "align_to_ref"
   176 void SuperWord::find_adjacent_refs() {
   177   // Get list of memory operations
   178   Node_List memops;
   179   for (int i = 0; i < _block.length(); i++) {
   180     Node* n = _block.at(i);
   181     if (n->is_Mem() && !n->is_LoadStore() && in_bb(n) &&
   182         is_java_primitive(n->as_Mem()->memory_type())) {
   183       int align = memory_alignment(n->as_Mem(), 0);
   184       if (align != bottom_align) {
   185         memops.push(n);
   186       }
   187     }
   188   }
   190   Node_List align_to_refs;
   191   int best_iv_adjustment = 0;
   192   MemNode* best_align_to_mem_ref = NULL;
   194   while (memops.size() != 0) {
   195     // Find a memory reference to align to.
   196     MemNode* mem_ref = find_align_to_ref(memops);
   197     if (mem_ref == NULL) break;
   198     align_to_refs.push(mem_ref);
   199     int iv_adjustment = get_iv_adjustment(mem_ref);
   201     if (best_align_to_mem_ref == NULL) {
   202       // Set memory reference which is the best from all memory operations
   203       // to be used for alignment. The pre-loop trip count is modified to align
   204       // this reference to a vector-aligned address.
   205       best_align_to_mem_ref = mem_ref;
   206       best_iv_adjustment = iv_adjustment;
   207     }
   209     SWPointer align_to_ref_p(mem_ref, this);
   210     // Set alignment relative to "align_to_ref" for all related memory operations.
   211     for (int i = memops.size() - 1; i >= 0; i--) {
   212       MemNode* s = memops.at(i)->as_Mem();
   213       if (isomorphic(s, mem_ref)) {
   214         SWPointer p2(s, this);
   215         if (p2.comparable(align_to_ref_p)) {
   216           int align = memory_alignment(s, iv_adjustment);
   217           set_alignment(s, align);
   218         }
   219       }
   220     }
   222     // Create initial pack pairs of memory operations for which
   223     // alignment is set and vectors will be aligned.
   224     bool create_pack = true;
   225     if (memory_alignment(mem_ref, best_iv_adjustment) == 0) {
   226       if (!Matcher::misaligned_vectors_ok()) {
   227         int vw = vector_width(mem_ref);
   228         int vw_best = vector_width(best_align_to_mem_ref);
   229         if (vw > vw_best) {
   230           // Do not vectorize a memory access with more elements per vector
   231           // if unaligned memory access is not allowed because number of
   232           // iterations in pre-loop will be not enough to align it.
   233           create_pack = false;
   234         }
   235       }
   236     } else {
   237       if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
   238         // Can't allow vectorization of unaligned memory accesses with the
   239         // same type since it could be overlapped accesses to the same array.
   240         create_pack = false;
   241       } else {
   242         // Allow independent (different type) unaligned memory operations
   243         // if HW supports them.
   244         if (!Matcher::misaligned_vectors_ok()) {
   245           create_pack = false;
   246         } else {
   247           // Check if packs of the same memory type but
   248           // with a different alignment were created before.
   249           for (uint i = 0; i < align_to_refs.size(); i++) {
   250             MemNode* mr = align_to_refs.at(i)->as_Mem();
   251             if (same_velt_type(mr, mem_ref) &&
   252                 memory_alignment(mr, iv_adjustment) != 0)
   253               create_pack = false;
   254           }
   255         }
   256       }
   257     }
   258     if (create_pack) {
   259       for (uint i = 0; i < memops.size(); i++) {
   260         Node* s1 = memops.at(i);
   261         int align = alignment(s1);
   262         if (align == top_align) continue;
   263         for (uint j = 0; j < memops.size(); j++) {
   264           Node* s2 = memops.at(j);
   265           if (alignment(s2) == top_align) continue;
   266           if (s1 != s2 && are_adjacent_refs(s1, s2)) {
   267             if (stmts_can_pack(s1, s2, align)) {
   268               Node_List* pair = new Node_List();
   269               pair->push(s1);
   270               pair->push(s2);
   271               _packset.append(pair);
   272             }
   273           }
   274         }
   275       }
   276     } else { // Don't create unaligned pack
   277       // First, remove remaining memory ops of the same type from the list.
   278       for (int i = memops.size() - 1; i >= 0; i--) {
   279         MemNode* s = memops.at(i)->as_Mem();
   280         if (same_velt_type(s, mem_ref)) {
   281           memops.remove(i);
   282         }
   283       }
   285       // Second, remove already constructed packs of the same type.
   286       for (int i = _packset.length() - 1; i >= 0; i--) {
   287         Node_List* p = _packset.at(i);
   288         MemNode* s = p->at(0)->as_Mem();
   289         if (same_velt_type(s, mem_ref)) {
   290           remove_pack_at(i);
   291         }
   292       }
   294       // If needed find the best memory reference for loop alignment again.
   295       if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
   296         // Put memory ops from remaining packs back on memops list for
   297         // the best alignment search.
   298         uint orig_msize = memops.size();
   299         for (int i = 0; i < _packset.length(); i++) {
   300           Node_List* p = _packset.at(i);
   301           MemNode* s = p->at(0)->as_Mem();
   302           assert(!same_velt_type(s, mem_ref), "sanity");
   303           memops.push(s);
   304         }
   305         MemNode* best_align_to_mem_ref = find_align_to_ref(memops);
   306         if (best_align_to_mem_ref == NULL) break;
   307         best_iv_adjustment = get_iv_adjustment(best_align_to_mem_ref);
   308         // Restore list.
   309         while (memops.size() > orig_msize)
   310           (void)memops.pop();
   311       }
   312     } // unaligned memory accesses
   314     // Remove used mem nodes.
   315     for (int i = memops.size() - 1; i >= 0; i--) {
   316       MemNode* m = memops.at(i)->as_Mem();
   317       if (alignment(m) != top_align) {
   318         memops.remove(i);
   319       }
   320     }
   322   } // while (memops.size() != 0
   323   set_align_to_ref(best_align_to_mem_ref);
   325 #ifndef PRODUCT
   326   if (TraceSuperWord) {
   327     tty->print_cr("\nAfter find_adjacent_refs");
   328     print_packset();
   329   }
   330 #endif
   331 }
   333 //------------------------------find_align_to_ref---------------------------
   334 // Find a memory reference to align the loop induction variable to.
   335 // Looks first at stores then at loads, looking for a memory reference
   336 // with the largest number of references similar to it.
   337 MemNode* SuperWord::find_align_to_ref(Node_List &memops) {
   338   GrowableArray<int> cmp_ct(arena(), memops.size(), memops.size(), 0);
   340   // Count number of comparable memory ops
   341   for (uint i = 0; i < memops.size(); i++) {
   342     MemNode* s1 = memops.at(i)->as_Mem();
   343     SWPointer p1(s1, this);
   344     // Discard if pre loop can't align this reference
   345     if (!ref_is_alignable(p1)) {
   346       *cmp_ct.adr_at(i) = 0;
   347       continue;
   348     }
   349     for (uint j = i+1; j < memops.size(); j++) {
   350       MemNode* s2 = memops.at(j)->as_Mem();
   351       if (isomorphic(s1, s2)) {
   352         SWPointer p2(s2, this);
   353         if (p1.comparable(p2)) {
   354           (*cmp_ct.adr_at(i))++;
   355           (*cmp_ct.adr_at(j))++;
   356         }
   357       }
   358     }
   359   }
   361   // Find Store (or Load) with the greatest number of "comparable" references,
   362   // biggest vector size, smallest data size and smallest iv offset.
   363   int max_ct        = 0;
   364   int max_vw        = 0;
   365   int max_idx       = -1;
   366   int min_size      = max_jint;
   367   int min_iv_offset = max_jint;
   368   for (uint j = 0; j < memops.size(); j++) {
   369     MemNode* s = memops.at(j)->as_Mem();
   370     if (s->is_Store()) {
   371       int vw = vector_width_in_bytes(s);
   372       assert(vw > 1, "sanity");
   373       SWPointer p(s, this);
   374       if (cmp_ct.at(j) >  max_ct ||
   375           cmp_ct.at(j) == max_ct &&
   376             (vw >  max_vw ||
   377              vw == max_vw &&
   378               (data_size(s) <  min_size ||
   379                data_size(s) == min_size &&
   380                  (p.offset_in_bytes() < min_iv_offset)))) {
   381         max_ct = cmp_ct.at(j);
   382         max_vw = vw;
   383         max_idx = j;
   384         min_size = data_size(s);
   385         min_iv_offset = p.offset_in_bytes();
   386       }
   387     }
   388   }
   389   // If no stores, look at loads
   390   if (max_ct == 0) {
   391     for (uint j = 0; j < memops.size(); j++) {
   392       MemNode* s = memops.at(j)->as_Mem();
   393       if (s->is_Load()) {
   394         int vw = vector_width_in_bytes(s);
   395         assert(vw > 1, "sanity");
   396         SWPointer p(s, this);
   397         if (cmp_ct.at(j) >  max_ct ||
   398             cmp_ct.at(j) == max_ct &&
   399               (vw >  max_vw ||
   400                vw == max_vw &&
   401                 (data_size(s) <  min_size ||
   402                  data_size(s) == min_size &&
   403                    (p.offset_in_bytes() < min_iv_offset)))) {
   404           max_ct = cmp_ct.at(j);
   405           max_vw = vw;
   406           max_idx = j;
   407           min_size = data_size(s);
   408           min_iv_offset = p.offset_in_bytes();
   409         }
   410       }
   411     }
   412   }
   414 #ifdef ASSERT
   415   if (TraceSuperWord && Verbose) {
   416     tty->print_cr("\nVector memops after find_align_to_refs");
   417     for (uint i = 0; i < memops.size(); i++) {
   418       MemNode* s = memops.at(i)->as_Mem();
   419       s->dump();
   420     }
   421   }
   422 #endif
   424   if (max_ct > 0) {
   425 #ifdef ASSERT
   426     if (TraceSuperWord) {
   427       tty->print("\nVector align to node: ");
   428       memops.at(max_idx)->as_Mem()->dump();
   429     }
   430 #endif
   431     return memops.at(max_idx)->as_Mem();
   432   }
   433   return NULL;
   434 }
   436 //------------------------------ref_is_alignable---------------------------
   437 // Can the preloop align the reference to position zero in the vector?
   438 bool SuperWord::ref_is_alignable(SWPointer& p) {
   439   if (!p.has_iv()) {
   440     return true;   // no induction variable
   441   }
   442   CountedLoopEndNode* pre_end = get_pre_loop_end(lp()->as_CountedLoop());
   443   assert(pre_end->stride_is_con(), "pre loop stride is constant");
   444   int preloop_stride = pre_end->stride_con();
   446   int span = preloop_stride * p.scale_in_bytes();
   448   // Stride one accesses are alignable.
   449   if (ABS(span) == p.memory_size())
   450     return true;
   452   // If initial offset from start of object is computable,
   453   // compute alignment within the vector.
   454   int vw = vector_width_in_bytes(p.mem());
   455   assert(vw > 1, "sanity");
   456   if (vw % span == 0) {
   457     Node* init_nd = pre_end->init_trip();
   458     if (init_nd->is_Con() && p.invar() == NULL) {
   459       int init = init_nd->bottom_type()->is_int()->get_con();
   461       int init_offset = init * p.scale_in_bytes() + p.offset_in_bytes();
   462       assert(init_offset >= 0, "positive offset from object start");
   464       if (span > 0) {
   465         return (vw - (init_offset % vw)) % span == 0;
   466       } else {
   467         assert(span < 0, "nonzero stride * scale");
   468         return (init_offset % vw) % -span == 0;
   469       }
   470     }
   471   }
   472   return false;
   473 }
   475 //---------------------------get_iv_adjustment---------------------------
   476 // Calculate loop's iv adjustment for this memory ops.
   477 int SuperWord::get_iv_adjustment(MemNode* mem_ref) {
   478   SWPointer align_to_ref_p(mem_ref, this);
   479   int offset = align_to_ref_p.offset_in_bytes();
   480   int scale  = align_to_ref_p.scale_in_bytes();
   481   int vw       = vector_width_in_bytes(mem_ref);
   482   assert(vw > 1, "sanity");
   483   int stride_sign   = (scale * iv_stride()) > 0 ? 1 : -1;
   484   // At least one iteration is executed in pre-loop by default. As result
   485   // several iterations are needed to align memory operations in main-loop even
   486   // if offset is 0.
   487   int iv_adjustment_in_bytes = (stride_sign * vw - (offset % vw));
   488   int elt_size = align_to_ref_p.memory_size();
   489   assert(((ABS(iv_adjustment_in_bytes) % elt_size) == 0),
   490          err_msg_res("(%d) should be divisible by (%d)", iv_adjustment_in_bytes, elt_size));
   491   int iv_adjustment = iv_adjustment_in_bytes/elt_size;
   493 #ifndef PRODUCT
   494   if (TraceSuperWord)
   495     tty->print_cr("\noffset = %d iv_adjust = %d elt_size = %d scale = %d iv_stride = %d vect_size %d",
   496                   offset, iv_adjustment, elt_size, scale, iv_stride(), vw);
   497 #endif
   498   return iv_adjustment;
   499 }
   501 //---------------------------dependence_graph---------------------------
   502 // Construct dependency graph.
   503 // Add dependence edges to load/store nodes for memory dependence
   504 //    A.out()->DependNode.in(1) and DependNode.out()->B.prec(x)
   505 void SuperWord::dependence_graph() {
   506   // First, assign a dependence node to each memory node
   507   for (int i = 0; i < _block.length(); i++ ) {
   508     Node *n = _block.at(i);
   509     if (n->is_Mem() || n->is_Phi() && n->bottom_type() == Type::MEMORY) {
   510       _dg.make_node(n);
   511     }
   512   }
   514   // For each memory slice, create the dependences
   515   for (int i = 0; i < _mem_slice_head.length(); i++) {
   516     Node* n      = _mem_slice_head.at(i);
   517     Node* n_tail = _mem_slice_tail.at(i);
   519     // Get slice in predecessor order (last is first)
   520     mem_slice_preds(n_tail, n, _nlist);
   522     // Make the slice dependent on the root
   523     DepMem* slice = _dg.dep(n);
   524     _dg.make_edge(_dg.root(), slice);
   526     // Create a sink for the slice
   527     DepMem* slice_sink = _dg.make_node(NULL);
   528     _dg.make_edge(slice_sink, _dg.tail());
   530     // Now visit each pair of memory ops, creating the edges
   531     for (int j = _nlist.length() - 1; j >= 0 ; j--) {
   532       Node* s1 = _nlist.at(j);
   534       // If no dependency yet, use slice
   535       if (_dg.dep(s1)->in_cnt() == 0) {
   536         _dg.make_edge(slice, s1);
   537       }
   538       SWPointer p1(s1->as_Mem(), this);
   539       bool sink_dependent = true;
   540       for (int k = j - 1; k >= 0; k--) {
   541         Node* s2 = _nlist.at(k);
   542         if (s1->is_Load() && s2->is_Load())
   543           continue;
   544         SWPointer p2(s2->as_Mem(), this);
   546         int cmp = p1.cmp(p2);
   547         if (SuperWordRTDepCheck &&
   548             p1.base() != p2.base() && p1.valid() && p2.valid()) {
   549           // Create a runtime check to disambiguate
   550           OrderedPair pp(p1.base(), p2.base());
   551           _disjoint_ptrs.append_if_missing(pp);
   552         } else if (!SWPointer::not_equal(cmp)) {
   553           // Possibly same address
   554           _dg.make_edge(s1, s2);
   555           sink_dependent = false;
   556         }
   557       }
   558       if (sink_dependent) {
   559         _dg.make_edge(s1, slice_sink);
   560       }
   561     }
   562 #ifndef PRODUCT
   563     if (TraceSuperWord) {
   564       tty->print_cr("\nDependence graph for slice: %d", n->_idx);
   565       for (int q = 0; q < _nlist.length(); q++) {
   566         _dg.print(_nlist.at(q));
   567       }
   568       tty->cr();
   569     }
   570 #endif
   571     _nlist.clear();
   572   }
   574 #ifndef PRODUCT
   575   if (TraceSuperWord) {
   576     tty->print_cr("\ndisjoint_ptrs: %s", _disjoint_ptrs.length() > 0 ? "" : "NONE");
   577     for (int r = 0; r < _disjoint_ptrs.length(); r++) {
   578       _disjoint_ptrs.at(r).print();
   579       tty->cr();
   580     }
   581     tty->cr();
   582   }
   583 #endif
   584 }
   586 //---------------------------mem_slice_preds---------------------------
   587 // Return a memory slice (node list) in predecessor order starting at "start"
   588 void SuperWord::mem_slice_preds(Node* start, Node* stop, GrowableArray<Node*> &preds) {
   589   assert(preds.length() == 0, "start empty");
   590   Node* n = start;
   591   Node* prev = NULL;
   592   while (true) {
   593     assert(in_bb(n), "must be in block");
   594     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
   595       Node* out = n->fast_out(i);
   596       if (out->is_Load()) {
   597         if (in_bb(out)) {
   598           preds.push(out);
   599         }
   600       } else {
   601         // FIXME
   602         if (out->is_MergeMem() && !in_bb(out)) {
   603           // Either unrolling is causing a memory edge not to disappear,
   604           // or need to run igvn.optimize() again before SLP
   605         } else if (out->is_Phi() && out->bottom_type() == Type::MEMORY && !in_bb(out)) {
   606           // Ditto.  Not sure what else to check further.
   607         } else if (out->Opcode() == Op_StoreCM && out->in(MemNode::OopStore) == n) {
   608           // StoreCM has an input edge used as a precedence edge.
   609           // Maybe an issue when oop stores are vectorized.
   610         } else {
   611           assert(out == prev || prev == NULL, "no branches off of store slice");
   612         }
   613       }
   614     }
   615     if (n == stop) break;
   616     preds.push(n);
   617     prev = n;
   618     n = n->in(MemNode::Memory);
   619   }
   620 }
   622 //------------------------------stmts_can_pack---------------------------
   623 // Can s1 and s2 be in a pack with s1 immediately preceding s2 and
   624 // s1 aligned at "align"
   625 bool SuperWord::stmts_can_pack(Node* s1, Node* s2, int align) {
   627   // Do not use superword for non-primitives
   628   BasicType bt1 = velt_basic_type(s1);
   629   BasicType bt2 = velt_basic_type(s2);
   630   if(!is_java_primitive(bt1) || !is_java_primitive(bt2))
   631     return false;
   632   if (Matcher::max_vector_size(bt1) < 2) {
   633     return false; // No vectors for this type
   634   }
   636   if (isomorphic(s1, s2)) {
   637     if (independent(s1, s2)) {
   638       if (!exists_at(s1, 0) && !exists_at(s2, 1)) {
   639         if (!s1->is_Mem() || are_adjacent_refs(s1, s2)) {
   640           int s1_align = alignment(s1);
   641           int s2_align = alignment(s2);
   642           if (s1_align == top_align || s1_align == align) {
   643             if (s2_align == top_align || s2_align == align + data_size(s1)) {
   644               return true;
   645             }
   646           }
   647         }
   648       }
   649     }
   650   }
   651   return false;
   652 }
   654 //------------------------------exists_at---------------------------
   655 // Does s exist in a pack at position pos?
   656 bool SuperWord::exists_at(Node* s, uint pos) {
   657   for (int i = 0; i < _packset.length(); i++) {
   658     Node_List* p = _packset.at(i);
   659     if (p->at(pos) == s) {
   660       return true;
   661     }
   662   }
   663   return false;
   664 }
   666 //------------------------------are_adjacent_refs---------------------------
   667 // Is s1 immediately before s2 in memory?
   668 bool SuperWord::are_adjacent_refs(Node* s1, Node* s2) {
   669   if (!s1->is_Mem() || !s2->is_Mem()) return false;
   670   if (!in_bb(s1)    || !in_bb(s2))    return false;
   672   // Do not use superword for non-primitives
   673   if (!is_java_primitive(s1->as_Mem()->memory_type()) ||
   674       !is_java_primitive(s2->as_Mem()->memory_type())) {
   675     return false;
   676   }
   678   // FIXME - co_locate_pack fails on Stores in different mem-slices, so
   679   // only pack memops that are in the same alias set until that's fixed.
   680   if (_phase->C->get_alias_index(s1->as_Mem()->adr_type()) !=
   681       _phase->C->get_alias_index(s2->as_Mem()->adr_type()))
   682     return false;
   683   SWPointer p1(s1->as_Mem(), this);
   684   SWPointer p2(s2->as_Mem(), this);
   685   if (p1.base() != p2.base() || !p1.comparable(p2)) return false;
   686   int diff = p2.offset_in_bytes() - p1.offset_in_bytes();
   687   return diff == data_size(s1);
   688 }
   690 //------------------------------isomorphic---------------------------
   691 // Are s1 and s2 similar?
   692 bool SuperWord::isomorphic(Node* s1, Node* s2) {
   693   if (s1->Opcode() != s2->Opcode()) return false;
   694   if (s1->req() != s2->req()) return false;
   695   if (s1->in(0) != s2->in(0)) return false;
   696   if (!same_velt_type(s1, s2)) return false;
   697   return true;
   698 }
   700 //------------------------------independent---------------------------
   701 // Is there no data path from s1 to s2 or s2 to s1?
   702 bool SuperWord::independent(Node* s1, Node* s2) {
   703   //  assert(s1->Opcode() == s2->Opcode(), "check isomorphic first");
   704   int d1 = depth(s1);
   705   int d2 = depth(s2);
   706   if (d1 == d2) return s1 != s2;
   707   Node* deep    = d1 > d2 ? s1 : s2;
   708   Node* shallow = d1 > d2 ? s2 : s1;
   710   visited_clear();
   712   return independent_path(shallow, deep);
   713 }
   715 //------------------------------independent_path------------------------------
   716 // Helper for independent
   717 bool SuperWord::independent_path(Node* shallow, Node* deep, uint dp) {
   718   if (dp >= 1000) return false; // stop deep recursion
   719   visited_set(deep);
   720   int shal_depth = depth(shallow);
   721   assert(shal_depth <= depth(deep), "must be");
   722   for (DepPreds preds(deep, _dg); !preds.done(); preds.next()) {
   723     Node* pred = preds.current();
   724     if (in_bb(pred) && !visited_test(pred)) {
   725       if (shallow == pred) {
   726         return false;
   727       }
   728       if (shal_depth < depth(pred) && !independent_path(shallow, pred, dp+1)) {
   729         return false;
   730       }
   731     }
   732   }
   733   return true;
   734 }
   736 //------------------------------set_alignment---------------------------
   737 void SuperWord::set_alignment(Node* s1, Node* s2, int align) {
   738   set_alignment(s1, align);
   739   if (align == top_align || align == bottom_align) {
   740     set_alignment(s2, align);
   741   } else {
   742     set_alignment(s2, align + data_size(s1));
   743   }
   744 }
   746 //------------------------------data_size---------------------------
   747 int SuperWord::data_size(Node* s) {
   748   int bsize = type2aelembytes(velt_basic_type(s));
   749   assert(bsize != 0, "valid size");
   750   return bsize;
   751 }
   753 //------------------------------extend_packlist---------------------------
   754 // Extend packset by following use->def and def->use links from pack members.
   755 void SuperWord::extend_packlist() {
   756   bool changed;
   757   do {
   758     changed = false;
   759     for (int i = 0; i < _packset.length(); i++) {
   760       Node_List* p = _packset.at(i);
   761       changed |= follow_use_defs(p);
   762       changed |= follow_def_uses(p);
   763     }
   764   } while (changed);
   766 #ifndef PRODUCT
   767   if (TraceSuperWord) {
   768     tty->print_cr("\nAfter extend_packlist");
   769     print_packset();
   770   }
   771 #endif
   772 }
   774 //------------------------------follow_use_defs---------------------------
   775 // Extend the packset by visiting operand definitions of nodes in pack p
   776 bool SuperWord::follow_use_defs(Node_List* p) {
   777   assert(p->size() == 2, "just checking");
   778   Node* s1 = p->at(0);
   779   Node* s2 = p->at(1);
   780   assert(s1->req() == s2->req(), "just checking");
   781   assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
   783   if (s1->is_Load()) return false;
   785   int align = alignment(s1);
   786   bool changed = false;
   787   int start = s1->is_Store() ? MemNode::ValueIn   : 1;
   788   int end   = s1->is_Store() ? MemNode::ValueIn+1 : s1->req();
   789   for (int j = start; j < end; j++) {
   790     Node* t1 = s1->in(j);
   791     Node* t2 = s2->in(j);
   792     if (!in_bb(t1) || !in_bb(t2))
   793       continue;
   794     if (stmts_can_pack(t1, t2, align)) {
   795       if (est_savings(t1, t2) >= 0) {
   796         Node_List* pair = new Node_List();
   797         pair->push(t1);
   798         pair->push(t2);
   799         _packset.append(pair);
   800         set_alignment(t1, t2, align);
   801         changed = true;
   802       }
   803     }
   804   }
   805   return changed;
   806 }
   808 //------------------------------follow_def_uses---------------------------
   809 // Extend the packset by visiting uses of nodes in pack p
   810 bool SuperWord::follow_def_uses(Node_List* p) {
   811   bool changed = false;
   812   Node* s1 = p->at(0);
   813   Node* s2 = p->at(1);
   814   assert(p->size() == 2, "just checking");
   815   assert(s1->req() == s2->req(), "just checking");
   816   assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
   818   if (s1->is_Store()) return false;
   820   int align = alignment(s1);
   821   int savings = -1;
   822   Node* u1 = NULL;
   823   Node* u2 = NULL;
   824   for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
   825     Node* t1 = s1->fast_out(i);
   826     if (!in_bb(t1)) continue;
   827     for (DUIterator_Fast jmax, j = s2->fast_outs(jmax); j < jmax; j++) {
   828       Node* t2 = s2->fast_out(j);
   829       if (!in_bb(t2)) continue;
   830       if (!opnd_positions_match(s1, t1, s2, t2))
   831         continue;
   832       if (stmts_can_pack(t1, t2, align)) {
   833         int my_savings = est_savings(t1, t2);
   834         if (my_savings > savings) {
   835           savings = my_savings;
   836           u1 = t1;
   837           u2 = t2;
   838         }
   839       }
   840     }
   841   }
   842   if (savings >= 0) {
   843     Node_List* pair = new Node_List();
   844     pair->push(u1);
   845     pair->push(u2);
   846     _packset.append(pair);
   847     set_alignment(u1, u2, align);
   848     changed = true;
   849   }
   850   return changed;
   851 }
   853 //---------------------------opnd_positions_match-------------------------
   854 // Is the use of d1 in u1 at the same operand position as d2 in u2?
   855 bool SuperWord::opnd_positions_match(Node* d1, Node* u1, Node* d2, Node* u2) {
   856   uint ct = u1->req();
   857   if (ct != u2->req()) return false;
   858   uint i1 = 0;
   859   uint i2 = 0;
   860   do {
   861     for (i1++; i1 < ct; i1++) if (u1->in(i1) == d1) break;
   862     for (i2++; i2 < ct; i2++) if (u2->in(i2) == d2) break;
   863     if (i1 != i2) {
   864       if ((i1 == (3-i2)) && (u2->is_Add() || u2->is_Mul())) {
   865         // Further analysis relies on operands position matching.
   866         u2->swap_edges(i1, i2);
   867       } else {
   868         return false;
   869       }
   870     }
   871   } while (i1 < ct);
   872   return true;
   873 }
   875 //------------------------------est_savings---------------------------
   876 // Estimate the savings from executing s1 and s2 as a pack
   877 int SuperWord::est_savings(Node* s1, Node* s2) {
   878   int save_in = 2 - 1; // 2 operations per instruction in packed form
   880   // inputs
   881   for (uint i = 1; i < s1->req(); i++) {
   882     Node* x1 = s1->in(i);
   883     Node* x2 = s2->in(i);
   884     if (x1 != x2) {
   885       if (are_adjacent_refs(x1, x2)) {
   886         save_in += adjacent_profit(x1, x2);
   887       } else if (!in_packset(x1, x2)) {
   888         save_in -= pack_cost(2);
   889       } else {
   890         save_in += unpack_cost(2);
   891       }
   892     }
   893   }
   895   // uses of result
   896   uint ct = 0;
   897   int save_use = 0;
   898   for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
   899     Node* s1_use = s1->fast_out(i);
   900     for (int j = 0; j < _packset.length(); j++) {
   901       Node_List* p = _packset.at(j);
   902       if (p->at(0) == s1_use) {
   903         for (DUIterator_Fast kmax, k = s2->fast_outs(kmax); k < kmax; k++) {
   904           Node* s2_use = s2->fast_out(k);
   905           if (p->at(p->size()-1) == s2_use) {
   906             ct++;
   907             if (are_adjacent_refs(s1_use, s2_use)) {
   908               save_use += adjacent_profit(s1_use, s2_use);
   909             }
   910           }
   911         }
   912       }
   913     }
   914   }
   916   if (ct < s1->outcnt()) save_use += unpack_cost(1);
   917   if (ct < s2->outcnt()) save_use += unpack_cost(1);
   919   return MAX2(save_in, save_use);
   920 }
   922 //------------------------------costs---------------------------
   923 int SuperWord::adjacent_profit(Node* s1, Node* s2) { return 2; }
   924 int SuperWord::pack_cost(int ct)   { return ct; }
   925 int SuperWord::unpack_cost(int ct) { return ct; }
   927 //------------------------------combine_packs---------------------------
   928 // Combine packs A and B with A.last == B.first into A.first..,A.last,B.second,..B.last
   929 void SuperWord::combine_packs() {
   930   bool changed = true;
   931   // Combine packs regardless max vector size.
   932   while (changed) {
   933     changed = false;
   934     for (int i = 0; i < _packset.length(); i++) {
   935       Node_List* p1 = _packset.at(i);
   936       if (p1 == NULL) continue;
   937       for (int j = 0; j < _packset.length(); j++) {
   938         Node_List* p2 = _packset.at(j);
   939         if (p2 == NULL) continue;
   940         if (i == j) continue;
   941         if (p1->at(p1->size()-1) == p2->at(0)) {
   942           for (uint k = 1; k < p2->size(); k++) {
   943             p1->push(p2->at(k));
   944           }
   945           _packset.at_put(j, NULL);
   946           changed = true;
   947         }
   948       }
   949     }
   950   }
   952   // Split packs which have size greater then max vector size.
   953   for (int i = 0; i < _packset.length(); i++) {
   954     Node_List* p1 = _packset.at(i);
   955     if (p1 != NULL) {
   956       BasicType bt = velt_basic_type(p1->at(0));
   957       uint max_vlen = Matcher::max_vector_size(bt); // Max elements in vector
   958       assert(is_power_of_2(max_vlen), "sanity");
   959       uint psize = p1->size();
   960       if (!is_power_of_2(psize)) {
   961         // Skip pack which can't be vector.
   962         // case1: for(...) { a[i] = i; }    elements values are different (i+x)
   963         // case2: for(...) { a[i] = b[i+1]; }  can't align both, load and store
   964         _packset.at_put(i, NULL);
   965         continue;
   966       }
   967       if (psize > max_vlen) {
   968         Node_List* pack = new Node_List();
   969         for (uint j = 0; j < psize; j++) {
   970           pack->push(p1->at(j));
   971           if (pack->size() >= max_vlen) {
   972             assert(is_power_of_2(pack->size()), "sanity");
   973             _packset.append(pack);
   974             pack = new Node_List();
   975           }
   976         }
   977         _packset.at_put(i, NULL);
   978       }
   979     }
   980   }
   982   // Compress list.
   983   for (int i = _packset.length() - 1; i >= 0; i--) {
   984     Node_List* p1 = _packset.at(i);
   985     if (p1 == NULL) {
   986       _packset.remove_at(i);
   987     }
   988   }
   990 #ifndef PRODUCT
   991   if (TraceSuperWord) {
   992     tty->print_cr("\nAfter combine_packs");
   993     print_packset();
   994   }
   995 #endif
   996 }
   998 //-----------------------------construct_my_pack_map--------------------------
   999 // Construct the map from nodes to packs.  Only valid after the
  1000 // point where a node is only in one pack (after combine_packs).
  1001 void SuperWord::construct_my_pack_map() {
  1002   Node_List* rslt = NULL;
  1003   for (int i = 0; i < _packset.length(); i++) {
  1004     Node_List* p = _packset.at(i);
  1005     for (uint j = 0; j < p->size(); j++) {
  1006       Node* s = p->at(j);
  1007       assert(my_pack(s) == NULL, "only in one pack");
  1008       set_my_pack(s, p);
  1013 //------------------------------filter_packs---------------------------
  1014 // Remove packs that are not implemented or not profitable.
  1015 void SuperWord::filter_packs() {
  1017   // Remove packs that are not implemented
  1018   for (int i = _packset.length() - 1; i >= 0; i--) {
  1019     Node_List* pk = _packset.at(i);
  1020     bool impl = implemented(pk);
  1021     if (!impl) {
  1022 #ifndef PRODUCT
  1023       if (TraceSuperWord && Verbose) {
  1024         tty->print_cr("Unimplemented");
  1025         pk->at(0)->dump();
  1027 #endif
  1028       remove_pack_at(i);
  1032   // Remove packs that are not profitable
  1033   bool changed;
  1034   do {
  1035     changed = false;
  1036     for (int i = _packset.length() - 1; i >= 0; i--) {
  1037       Node_List* pk = _packset.at(i);
  1038       bool prof = profitable(pk);
  1039       if (!prof) {
  1040 #ifndef PRODUCT
  1041         if (TraceSuperWord && Verbose) {
  1042           tty->print_cr("Unprofitable");
  1043           pk->at(0)->dump();
  1045 #endif
  1046         remove_pack_at(i);
  1047         changed = true;
  1050   } while (changed);
  1052 #ifndef PRODUCT
  1053   if (TraceSuperWord) {
  1054     tty->print_cr("\nAfter filter_packs");
  1055     print_packset();
  1056     tty->cr();
  1058 #endif
  1061 //------------------------------implemented---------------------------
  1062 // Can code be generated for pack p?
  1063 bool SuperWord::implemented(Node_List* p) {
  1064   Node* p0 = p->at(0);
  1065   return VectorNode::implemented(p0->Opcode(), p->size(), velt_basic_type(p0));
  1068 //------------------------------same_inputs--------------------------
  1069 // For pack p, are all idx operands the same?
  1070 static bool same_inputs(Node_List* p, int idx) {
  1071   Node* p0 = p->at(0);
  1072   uint vlen = p->size();
  1073   Node* p0_def = p0->in(idx);
  1074   for (uint i = 1; i < vlen; i++) {
  1075     Node* pi = p->at(i);
  1076     Node* pi_def = pi->in(idx);
  1077     if (p0_def != pi_def)
  1078       return false;
  1080   return true;
  1083 //------------------------------profitable---------------------------
  1084 // For pack p, are all operands and all uses (with in the block) vector?
  1085 bool SuperWord::profitable(Node_List* p) {
  1086   Node* p0 = p->at(0);
  1087   uint start, end;
  1088   VectorNode::vector_operands(p0, &start, &end);
  1090   // Return false if some inputs are not vectors or vectors with different
  1091   // size or alignment.
  1092   // Also, for now, return false if not scalar promotion case when inputs are
  1093   // the same. Later, implement PackNode and allow differing, non-vector inputs
  1094   // (maybe just the ones from outside the block.)
  1095   for (uint i = start; i < end; i++) {
  1096     if (!is_vector_use(p0, i))
  1097       return false;
  1099   if (VectorNode::is_shift(p0)) {
  1100     // For now, return false if shift count is vector or not scalar promotion
  1101     // case (different shift counts) because it is not supported yet.
  1102     Node* cnt = p0->in(2);
  1103     Node_List* cnt_pk = my_pack(cnt);
  1104     if (cnt_pk != NULL)
  1105       return false;
  1106     if (!same_inputs(p, 2))
  1107       return false;
  1109   if (!p0->is_Store()) {
  1110     // For now, return false if not all uses are vector.
  1111     // Later, implement ExtractNode and allow non-vector uses (maybe
  1112     // just the ones outside the block.)
  1113     for (uint i = 0; i < p->size(); i++) {
  1114       Node* def = p->at(i);
  1115       for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
  1116         Node* use = def->fast_out(j);
  1117         for (uint k = 0; k < use->req(); k++) {
  1118           Node* n = use->in(k);
  1119           if (def == n) {
  1120             if (!is_vector_use(use, k)) {
  1121               return false;
  1128   return true;
  1131 //------------------------------schedule---------------------------
  1132 // Adjust the memory graph for the packed operations
  1133 void SuperWord::schedule() {
  1135   // Co-locate in the memory graph the members of each memory pack
  1136   for (int i = 0; i < _packset.length(); i++) {
  1137     co_locate_pack(_packset.at(i));
  1141 //-------------------------------remove_and_insert-------------------
  1142 // Remove "current" from its current position in the memory graph and insert
  1143 // it after the appropriate insertion point (lip or uip).
  1144 void SuperWord::remove_and_insert(MemNode *current, MemNode *prev, MemNode *lip,
  1145                                   Node *uip, Unique_Node_List &sched_before) {
  1146   Node* my_mem = current->in(MemNode::Memory);
  1147   bool sched_up = sched_before.member(current);
  1149   // remove current_store from its current position in the memmory graph
  1150   for (DUIterator i = current->outs(); current->has_out(i); i++) {
  1151     Node* use = current->out(i);
  1152     if (use->is_Mem()) {
  1153       assert(use->in(MemNode::Memory) == current, "must be");
  1154       if (use == prev) { // connect prev to my_mem
  1155           _igvn.replace_input_of(use, MemNode::Memory, my_mem);
  1156           --i; //deleted this edge; rescan position
  1157       } else if (sched_before.member(use)) {
  1158         if (!sched_up) { // Will be moved together with current
  1159           _igvn.replace_input_of(use, MemNode::Memory, uip);
  1160           --i; //deleted this edge; rescan position
  1162       } else {
  1163         if (sched_up) { // Will be moved together with current
  1164           _igvn.replace_input_of(use, MemNode::Memory, lip);
  1165           --i; //deleted this edge; rescan position
  1171   Node *insert_pt =  sched_up ?  uip : lip;
  1173   // all uses of insert_pt's memory state should use current's instead
  1174   for (DUIterator i = insert_pt->outs(); insert_pt->has_out(i); i++) {
  1175     Node* use = insert_pt->out(i);
  1176     if (use->is_Mem()) {
  1177       assert(use->in(MemNode::Memory) == insert_pt, "must be");
  1178       _igvn.replace_input_of(use, MemNode::Memory, current);
  1179       --i; //deleted this edge; rescan position
  1180     } else if (!sched_up && use->is_Phi() && use->bottom_type() == Type::MEMORY) {
  1181       uint pos; //lip (lower insert point) must be the last one in the memory slice
  1182       for (pos=1; pos < use->req(); pos++) {
  1183         if (use->in(pos) == insert_pt) break;
  1185       _igvn.replace_input_of(use, pos, current);
  1186       --i;
  1190   //connect current to insert_pt
  1191   _igvn.replace_input_of(current, MemNode::Memory, insert_pt);
  1194 //------------------------------co_locate_pack----------------------------------
  1195 // To schedule a store pack, we need to move any sandwiched memory ops either before
  1196 // or after the pack, based upon dependence information:
  1197 // (1) If any store in the pack depends on the sandwiched memory op, the
  1198 //     sandwiched memory op must be scheduled BEFORE the pack;
  1199 // (2) If a sandwiched memory op depends on any store in the pack, the
  1200 //     sandwiched memory op must be scheduled AFTER the pack;
  1201 // (3) If a sandwiched memory op (say, memA) depends on another sandwiched
  1202 //     memory op (say memB), memB must be scheduled before memA. So, if memA is
  1203 //     scheduled before the pack, memB must also be scheduled before the pack;
  1204 // (4) If there is no dependence restriction for a sandwiched memory op, we simply
  1205 //     schedule this store AFTER the pack
  1206 // (5) We know there is no dependence cycle, so there in no other case;
  1207 // (6) Finally, all memory ops in another single pack should be moved in the same direction.
  1208 //
  1209 // To schedule a load pack, we use the memory state of either the first or the last load in
  1210 // the pack, based on the dependence constraint.
  1211 void SuperWord::co_locate_pack(Node_List* pk) {
  1212   if (pk->at(0)->is_Store()) {
  1213     MemNode* first     = executed_first(pk)->as_Mem();
  1214     MemNode* last      = executed_last(pk)->as_Mem();
  1215     Unique_Node_List schedule_before_pack;
  1216     Unique_Node_List memops;
  1218     MemNode* current   = last->in(MemNode::Memory)->as_Mem();
  1219     MemNode* previous  = last;
  1220     while (true) {
  1221       assert(in_bb(current), "stay in block");
  1222       memops.push(previous);
  1223       for (DUIterator i = current->outs(); current->has_out(i); i++) {
  1224         Node* use = current->out(i);
  1225         if (use->is_Mem() && use != previous)
  1226           memops.push(use);
  1228       if (current == first) break;
  1229       previous = current;
  1230       current  = current->in(MemNode::Memory)->as_Mem();
  1233     // determine which memory operations should be scheduled before the pack
  1234     for (uint i = 1; i < memops.size(); i++) {
  1235       Node *s1 = memops.at(i);
  1236       if (!in_pack(s1, pk) && !schedule_before_pack.member(s1)) {
  1237         for (uint j = 0; j< i; j++) {
  1238           Node *s2 = memops.at(j);
  1239           if (!independent(s1, s2)) {
  1240             if (in_pack(s2, pk) || schedule_before_pack.member(s2)) {
  1241               schedule_before_pack.push(s1); // s1 must be scheduled before
  1242               Node_List* mem_pk = my_pack(s1);
  1243               if (mem_pk != NULL) {
  1244                 for (uint ii = 0; ii < mem_pk->size(); ii++) {
  1245                   Node* s = mem_pk->at(ii);  // follow partner
  1246                   if (memops.member(s) && !schedule_before_pack.member(s))
  1247                     schedule_before_pack.push(s);
  1250               break;
  1257     Node*    upper_insert_pt = first->in(MemNode::Memory);
  1258     // Following code moves loads connected to upper_insert_pt below aliased stores.
  1259     // Collect such loads here and reconnect them back to upper_insert_pt later.
  1260     memops.clear();
  1261     for (DUIterator i = upper_insert_pt->outs(); upper_insert_pt->has_out(i); i++) {
  1262       Node* use = upper_insert_pt->out(i);
  1263       if (!use->is_Store())
  1264         memops.push(use);
  1267     MemNode* lower_insert_pt = last;
  1268     previous                 = last; //previous store in pk
  1269     current                  = last->in(MemNode::Memory)->as_Mem();
  1271     // start scheduling from "last" to "first"
  1272     while (true) {
  1273       assert(in_bb(current), "stay in block");
  1274       assert(in_pack(previous, pk), "previous stays in pack");
  1275       Node* my_mem = current->in(MemNode::Memory);
  1277       if (in_pack(current, pk)) {
  1278         // Forward users of my memory state (except "previous) to my input memory state
  1279         for (DUIterator i = current->outs(); current->has_out(i); i++) {
  1280           Node* use = current->out(i);
  1281           if (use->is_Mem() && use != previous) {
  1282             assert(use->in(MemNode::Memory) == current, "must be");
  1283             if (schedule_before_pack.member(use)) {
  1284               _igvn.replace_input_of(use, MemNode::Memory, upper_insert_pt);
  1285             } else {
  1286               _igvn.replace_input_of(use, MemNode::Memory, lower_insert_pt);
  1288             --i; // deleted this edge; rescan position
  1291         previous = current;
  1292       } else { // !in_pack(current, pk) ==> a sandwiched store
  1293         remove_and_insert(current, previous, lower_insert_pt, upper_insert_pt, schedule_before_pack);
  1296       if (current == first) break;
  1297       current = my_mem->as_Mem();
  1298     } // end while
  1300     // Reconnect loads back to upper_insert_pt.
  1301     for (uint i = 0; i < memops.size(); i++) {
  1302       Node *ld = memops.at(i);
  1303       if (ld->in(MemNode::Memory) != upper_insert_pt) {
  1304         _igvn.replace_input_of(ld, MemNode::Memory, upper_insert_pt);
  1307   } else if (pk->at(0)->is_Load()) { //load
  1308     // all loads in the pack should have the same memory state. By default,
  1309     // we use the memory state of the last load. However, if any load could
  1310     // not be moved down due to the dependence constraint, we use the memory
  1311     // state of the first load.
  1312     Node* last_mem  = executed_last(pk)->in(MemNode::Memory);
  1313     Node* first_mem = executed_first(pk)->in(MemNode::Memory);
  1314     bool schedule_last = true;
  1315     for (uint i = 0; i < pk->size(); i++) {
  1316       Node* ld = pk->at(i);
  1317       for (Node* current = last_mem; current != ld->in(MemNode::Memory);
  1318            current=current->in(MemNode::Memory)) {
  1319         assert(current != first_mem, "corrupted memory graph");
  1320         if(current->is_Mem() && !independent(current, ld)){
  1321           schedule_last = false; // a later store depends on this load
  1322           break;
  1327     Node* mem_input = schedule_last ? last_mem : first_mem;
  1328     _igvn.hash_delete(mem_input);
  1329     // Give each load the same memory state
  1330     for (uint i = 0; i < pk->size(); i++) {
  1331       LoadNode* ld = pk->at(i)->as_Load();
  1332       _igvn.replace_input_of(ld, MemNode::Memory, mem_input);
  1337 //------------------------------output---------------------------
  1338 // Convert packs into vector node operations
  1339 void SuperWord::output() {
  1340   if (_packset.length() == 0) return;
  1342 #ifndef PRODUCT
  1343   if (TraceLoopOpts) {
  1344     tty->print("SuperWord    ");
  1345     lpt()->dump_head();
  1347 #endif
  1349   // MUST ENSURE main loop's initial value is properly aligned:
  1350   //  (iv_initial_value + min_iv_offset) % vector_width_in_bytes() == 0
  1352   align_initial_loop_index(align_to_ref());
  1354   // Insert extract (unpack) operations for scalar uses
  1355   for (int i = 0; i < _packset.length(); i++) {
  1356     insert_extracts(_packset.at(i));
  1359   Compile* C = _phase->C;
  1360   uint max_vlen_in_bytes = 0;
  1361   for (int i = 0; i < _block.length(); i++) {
  1362     Node* n = _block.at(i);
  1363     Node_List* p = my_pack(n);
  1364     if (p && n == executed_last(p)) {
  1365       uint vlen = p->size();
  1366       uint vlen_in_bytes = 0;
  1367       Node* vn = NULL;
  1368       Node* low_adr = p->at(0);
  1369       Node* first   = executed_first(p);
  1370       int   opc = n->Opcode();
  1371       if (n->is_Load()) {
  1372         Node* ctl = n->in(MemNode::Control);
  1373         Node* mem = first->in(MemNode::Memory);
  1374         Node* adr = low_adr->in(MemNode::Address);
  1375         const TypePtr* atyp = n->adr_type();
  1376         vn = LoadVectorNode::make(C, opc, ctl, mem, adr, atyp, vlen, velt_basic_type(n));
  1377         vlen_in_bytes = vn->as_LoadVector()->memory_size();
  1378       } else if (n->is_Store()) {
  1379         // Promote value to be stored to vector
  1380         Node* val = vector_opd(p, MemNode::ValueIn);
  1381         Node* ctl = n->in(MemNode::Control);
  1382         Node* mem = first->in(MemNode::Memory);
  1383         Node* adr = low_adr->in(MemNode::Address);
  1384         const TypePtr* atyp = n->adr_type();
  1385         vn = StoreVectorNode::make(C, opc, ctl, mem, adr, atyp, val, vlen);
  1386         vlen_in_bytes = vn->as_StoreVector()->memory_size();
  1387       } else if (n->req() == 3) {
  1388         // Promote operands to vector
  1389         Node* in1 = vector_opd(p, 1);
  1390         Node* in2 = vector_opd(p, 2);
  1391         if (VectorNode::is_invariant_vector(in1) && (n->is_Add() || n->is_Mul())) {
  1392           // Move invariant vector input into second position to avoid register spilling.
  1393           Node* tmp = in1;
  1394           in1 = in2;
  1395           in2 = tmp;
  1397         vn = VectorNode::make(C, opc, in1, in2, vlen, velt_basic_type(n));
  1398         vlen_in_bytes = vn->as_Vector()->length_in_bytes();
  1399       } else {
  1400         ShouldNotReachHere();
  1402       assert(vn != NULL, "sanity");
  1403       _igvn.register_new_node_with_optimizer(vn);
  1404       _phase->set_ctrl(vn, _phase->get_ctrl(p->at(0)));
  1405       for (uint j = 0; j < p->size(); j++) {
  1406         Node* pm = p->at(j);
  1407         _igvn.replace_node(pm, vn);
  1409       _igvn._worklist.push(vn);
  1411       if (vlen_in_bytes > max_vlen_in_bytes) {
  1412         max_vlen_in_bytes = vlen_in_bytes;
  1414 #ifdef ASSERT
  1415       if (TraceNewVectors) {
  1416         tty->print("new Vector node: ");
  1417         vn->dump();
  1419 #endif
  1422   C->set_max_vector_size(max_vlen_in_bytes);
  1425 //------------------------------vector_opd---------------------------
  1426 // Create a vector operand for the nodes in pack p for operand: in(opd_idx)
  1427 Node* SuperWord::vector_opd(Node_List* p, int opd_idx) {
  1428   Node* p0 = p->at(0);
  1429   uint vlen = p->size();
  1430   Node* opd = p0->in(opd_idx);
  1432   if (same_inputs(p, opd_idx)) {
  1433     if (opd->is_Vector() || opd->is_LoadVector()) {
  1434       assert(((opd_idx != 2) || !VectorNode::is_shift(p0)), "shift's count can't be vector");
  1435       return opd; // input is matching vector
  1437     if ((opd_idx == 2) && VectorNode::is_shift(p0)) {
  1438       Compile* C = _phase->C;
  1439       Node* cnt = opd;
  1440       // Vector instructions do not mask shift count, do it here.
  1441       juint mask = (p0->bottom_type() == TypeInt::INT) ? (BitsPerInt - 1) : (BitsPerLong - 1);
  1442       const TypeInt* t = opd->find_int_type();
  1443       if (t != NULL && t->is_con()) {
  1444         juint shift = t->get_con();
  1445         if (shift > mask) { // Unsigned cmp
  1446           cnt = ConNode::make(C, TypeInt::make(shift & mask));
  1448       } else {
  1449         if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {
  1450           cnt = ConNode::make(C, TypeInt::make(mask));
  1451           _igvn.register_new_node_with_optimizer(cnt);
  1452           cnt = new (C) AndINode(opd, cnt);
  1453           _igvn.register_new_node_with_optimizer(cnt);
  1454           _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
  1456         assert(opd->bottom_type()->isa_int(), "int type only");
  1457         // Move non constant shift count into vector register.
  1458         cnt = VectorNode::shift_count(C, p0, cnt, vlen, velt_basic_type(p0));
  1460       if (cnt != opd) {
  1461         _igvn.register_new_node_with_optimizer(cnt);
  1462         _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
  1464       return cnt;
  1466     assert(!opd->is_StoreVector(), "such vector is not expected here");
  1467     // Convert scalar input to vector with the same number of elements as
  1468     // p0's vector. Use p0's type because size of operand's container in
  1469     // vector should match p0's size regardless operand's size.
  1470     const Type* p0_t = velt_type(p0);
  1471     VectorNode* vn = VectorNode::scalar2vector(_phase->C, opd, vlen, p0_t);
  1473     _igvn.register_new_node_with_optimizer(vn);
  1474     _phase->set_ctrl(vn, _phase->get_ctrl(opd));
  1475 #ifdef ASSERT
  1476     if (TraceNewVectors) {
  1477       tty->print("new Vector node: ");
  1478       vn->dump();
  1480 #endif
  1481     return vn;
  1484   // Insert pack operation
  1485   BasicType bt = velt_basic_type(p0);
  1486   PackNode* pk = PackNode::make(_phase->C, opd, vlen, bt);
  1487   DEBUG_ONLY( const BasicType opd_bt = opd->bottom_type()->basic_type(); )
  1489   for (uint i = 1; i < vlen; i++) {
  1490     Node* pi = p->at(i);
  1491     Node* in = pi->in(opd_idx);
  1492     assert(my_pack(in) == NULL, "Should already have been unpacked");
  1493     assert(opd_bt == in->bottom_type()->basic_type(), "all same type");
  1494     pk->add_opd(in);
  1496   _igvn.register_new_node_with_optimizer(pk);
  1497   _phase->set_ctrl(pk, _phase->get_ctrl(opd));
  1498 #ifdef ASSERT
  1499   if (TraceNewVectors) {
  1500     tty->print("new Vector node: ");
  1501     pk->dump();
  1503 #endif
  1504   return pk;
  1507 //------------------------------insert_extracts---------------------------
  1508 // If a use of pack p is not a vector use, then replace the
  1509 // use with an extract operation.
  1510 void SuperWord::insert_extracts(Node_List* p) {
  1511   if (p->at(0)->is_Store()) return;
  1512   assert(_n_idx_list.is_empty(), "empty (node,index) list");
  1514   // Inspect each use of each pack member.  For each use that is
  1515   // not a vector use, replace the use with an extract operation.
  1517   for (uint i = 0; i < p->size(); i++) {
  1518     Node* def = p->at(i);
  1519     for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
  1520       Node* use = def->fast_out(j);
  1521       for (uint k = 0; k < use->req(); k++) {
  1522         Node* n = use->in(k);
  1523         if (def == n) {
  1524           if (!is_vector_use(use, k)) {
  1525             _n_idx_list.push(use, k);
  1532   while (_n_idx_list.is_nonempty()) {
  1533     Node* use = _n_idx_list.node();
  1534     int   idx = _n_idx_list.index();
  1535     _n_idx_list.pop();
  1536     Node* def = use->in(idx);
  1538     // Insert extract operation
  1539     _igvn.hash_delete(def);
  1540     int def_pos = alignment(def) / data_size(def);
  1542     Node* ex = ExtractNode::make(_phase->C, def, def_pos, velt_basic_type(def));
  1543     _igvn.register_new_node_with_optimizer(ex);
  1544     _phase->set_ctrl(ex, _phase->get_ctrl(def));
  1545     _igvn.replace_input_of(use, idx, ex);
  1546     _igvn._worklist.push(def);
  1548     bb_insert_after(ex, bb_idx(def));
  1549     set_velt_type(ex, velt_type(def));
  1553 //------------------------------is_vector_use---------------------------
  1554 // Is use->in(u_idx) a vector use?
  1555 bool SuperWord::is_vector_use(Node* use, int u_idx) {
  1556   Node_List* u_pk = my_pack(use);
  1557   if (u_pk == NULL) return false;
  1558   Node* def = use->in(u_idx);
  1559   Node_List* d_pk = my_pack(def);
  1560   if (d_pk == NULL) {
  1561     // check for scalar promotion
  1562     Node* n = u_pk->at(0)->in(u_idx);
  1563     for (uint i = 1; i < u_pk->size(); i++) {
  1564       if (u_pk->at(i)->in(u_idx) != n) return false;
  1566     return true;
  1568   if (u_pk->size() != d_pk->size())
  1569     return false;
  1570   for (uint i = 0; i < u_pk->size(); i++) {
  1571     Node* ui = u_pk->at(i);
  1572     Node* di = d_pk->at(i);
  1573     if (ui->in(u_idx) != di || alignment(ui) != alignment(di))
  1574       return false;
  1576   return true;
  1579 //------------------------------construct_bb---------------------------
  1580 // Construct reverse postorder list of block members
  1581 void SuperWord::construct_bb() {
  1582   Node* entry = bb();
  1584   assert(_stk.length() == 0,            "stk is empty");
  1585   assert(_block.length() == 0,          "block is empty");
  1586   assert(_data_entry.length() == 0,     "data_entry is empty");
  1587   assert(_mem_slice_head.length() == 0, "mem_slice_head is empty");
  1588   assert(_mem_slice_tail.length() == 0, "mem_slice_tail is empty");
  1590   // Find non-control nodes with no inputs from within block,
  1591   // create a temporary map from node _idx to bb_idx for use
  1592   // by the visited and post_visited sets,
  1593   // and count number of nodes in block.
  1594   int bb_ct = 0;
  1595   for (uint i = 0; i < lpt()->_body.size(); i++ ) {
  1596     Node *n = lpt()->_body.at(i);
  1597     set_bb_idx(n, i); // Create a temporary map
  1598     if (in_bb(n)) {
  1599       bb_ct++;
  1600       if (!n->is_CFG()) {
  1601         bool found = false;
  1602         for (uint j = 0; j < n->req(); j++) {
  1603           Node* def = n->in(j);
  1604           if (def && in_bb(def)) {
  1605             found = true;
  1606             break;
  1609         if (!found) {
  1610           assert(n != entry, "can't be entry");
  1611           _data_entry.push(n);
  1617   // Find memory slices (head and tail)
  1618   for (DUIterator_Fast imax, i = lp()->fast_outs(imax); i < imax; i++) {
  1619     Node *n = lp()->fast_out(i);
  1620     if (in_bb(n) && (n->is_Phi() && n->bottom_type() == Type::MEMORY)) {
  1621       Node* n_tail  = n->in(LoopNode::LoopBackControl);
  1622       if (n_tail != n->in(LoopNode::EntryControl)) {
  1623         _mem_slice_head.push(n);
  1624         _mem_slice_tail.push(n_tail);
  1629   // Create an RPO list of nodes in block
  1631   visited_clear();
  1632   post_visited_clear();
  1634   // Push all non-control nodes with no inputs from within block, then control entry
  1635   for (int j = 0; j < _data_entry.length(); j++) {
  1636     Node* n = _data_entry.at(j);
  1637     visited_set(n);
  1638     _stk.push(n);
  1640   visited_set(entry);
  1641   _stk.push(entry);
  1643   // Do a depth first walk over out edges
  1644   int rpo_idx = bb_ct - 1;
  1645   int size;
  1646   while ((size = _stk.length()) > 0) {
  1647     Node* n = _stk.top(); // Leave node on stack
  1648     if (!visited_test_set(n)) {
  1649       // forward arc in graph
  1650     } else if (!post_visited_test(n)) {
  1651       // cross or back arc
  1652       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1653         Node *use = n->fast_out(i);
  1654         if (in_bb(use) && !visited_test(use) &&
  1655             // Don't go around backedge
  1656             (!use->is_Phi() || n == entry)) {
  1657           _stk.push(use);
  1660       if (_stk.length() == size) {
  1661         // There were no additional uses, post visit node now
  1662         _stk.pop(); // Remove node from stack
  1663         assert(rpo_idx >= 0, "");
  1664         _block.at_put_grow(rpo_idx, n);
  1665         rpo_idx--;
  1666         post_visited_set(n);
  1667         assert(rpo_idx >= 0 || _stk.is_empty(), "");
  1669     } else {
  1670       _stk.pop(); // Remove post-visited node from stack
  1674   // Create real map of block indices for nodes
  1675   for (int j = 0; j < _block.length(); j++) {
  1676     Node* n = _block.at(j);
  1677     set_bb_idx(n, j);
  1680   initialize_bb(); // Ensure extra info is allocated.
  1682 #ifndef PRODUCT
  1683   if (TraceSuperWord) {
  1684     print_bb();
  1685     tty->print_cr("\ndata entry nodes: %s", _data_entry.length() > 0 ? "" : "NONE");
  1686     for (int m = 0; m < _data_entry.length(); m++) {
  1687       tty->print("%3d ", m);
  1688       _data_entry.at(m)->dump();
  1690     tty->print_cr("\nmemory slices: %s", _mem_slice_head.length() > 0 ? "" : "NONE");
  1691     for (int m = 0; m < _mem_slice_head.length(); m++) {
  1692       tty->print("%3d ", m); _mem_slice_head.at(m)->dump();
  1693       tty->print("    ");    _mem_slice_tail.at(m)->dump();
  1696 #endif
  1697   assert(rpo_idx == -1 && bb_ct == _block.length(), "all block members found");
  1700 //------------------------------initialize_bb---------------------------
  1701 // Initialize per node info
  1702 void SuperWord::initialize_bb() {
  1703   Node* last = _block.at(_block.length() - 1);
  1704   grow_node_info(bb_idx(last));
  1707 //------------------------------bb_insert_after---------------------------
  1708 // Insert n into block after pos
  1709 void SuperWord::bb_insert_after(Node* n, int pos) {
  1710   int n_pos = pos + 1;
  1711   // Make room
  1712   for (int i = _block.length() - 1; i >= n_pos; i--) {
  1713     _block.at_put_grow(i+1, _block.at(i));
  1715   for (int j = _node_info.length() - 1; j >= n_pos; j--) {
  1716     _node_info.at_put_grow(j+1, _node_info.at(j));
  1718   // Set value
  1719   _block.at_put_grow(n_pos, n);
  1720   _node_info.at_put_grow(n_pos, SWNodeInfo::initial);
  1721   // Adjust map from node->_idx to _block index
  1722   for (int i = n_pos; i < _block.length(); i++) {
  1723     set_bb_idx(_block.at(i), i);
  1727 //------------------------------compute_max_depth---------------------------
  1728 // Compute max depth for expressions from beginning of block
  1729 // Use to prune search paths during test for independence.
  1730 void SuperWord::compute_max_depth() {
  1731   int ct = 0;
  1732   bool again;
  1733   do {
  1734     again = false;
  1735     for (int i = 0; i < _block.length(); i++) {
  1736       Node* n = _block.at(i);
  1737       if (!n->is_Phi()) {
  1738         int d_orig = depth(n);
  1739         int d_in   = 0;
  1740         for (DepPreds preds(n, _dg); !preds.done(); preds.next()) {
  1741           Node* pred = preds.current();
  1742           if (in_bb(pred)) {
  1743             d_in = MAX2(d_in, depth(pred));
  1746         if (d_in + 1 != d_orig) {
  1747           set_depth(n, d_in + 1);
  1748           again = true;
  1752     ct++;
  1753   } while (again);
  1754 #ifndef PRODUCT
  1755   if (TraceSuperWord && Verbose)
  1756     tty->print_cr("compute_max_depth iterated: %d times", ct);
  1757 #endif
  1760 //-------------------------compute_vector_element_type-----------------------
  1761 // Compute necessary vector element type for expressions
  1762 // This propagates backwards a narrower integer type when the
  1763 // upper bits of the value are not needed.
  1764 // Example:  char a,b,c;  a = b + c;
  1765 // Normally the type of the add is integer, but for packed character
  1766 // operations the type of the add needs to be char.
  1767 void SuperWord::compute_vector_element_type() {
  1768 #ifndef PRODUCT
  1769   if (TraceSuperWord && Verbose)
  1770     tty->print_cr("\ncompute_velt_type:");
  1771 #endif
  1773   // Initial type
  1774   for (int i = 0; i < _block.length(); i++) {
  1775     Node* n = _block.at(i);
  1776     set_velt_type(n, container_type(n));
  1779   // Propagate integer narrowed type backwards through operations
  1780   // that don't depend on higher order bits
  1781   for (int i = _block.length() - 1; i >= 0; i--) {
  1782     Node* n = _block.at(i);
  1783     // Only integer types need be examined
  1784     const Type* vtn = velt_type(n);
  1785     if (vtn->basic_type() == T_INT) {
  1786       uint start, end;
  1787       VectorNode::vector_operands(n, &start, &end);
  1789       for (uint j = start; j < end; j++) {
  1790         Node* in  = n->in(j);
  1791         // Don't propagate through a memory
  1792         if (!in->is_Mem() && in_bb(in) && velt_type(in)->basic_type() == T_INT &&
  1793             data_size(n) < data_size(in)) {
  1794           bool same_type = true;
  1795           for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) {
  1796             Node *use = in->fast_out(k);
  1797             if (!in_bb(use) || !same_velt_type(use, n)) {
  1798               same_type = false;
  1799               break;
  1802           if (same_type) {
  1803             // For right shifts of small integer types (bool, byte, char, short)
  1804             // we need precise information about sign-ness. Only Load nodes have
  1805             // this information because Store nodes are the same for signed and
  1806             // unsigned values. And any arithmetic operation after a load may
  1807             // expand a value to signed Int so such right shifts can't be used
  1808             // because vector elements do not have upper bits of Int.
  1809             const Type* vt = vtn;
  1810             if (VectorNode::is_shift(in)) {
  1811               Node* load = in->in(1);
  1812               if (load->is_Load() && in_bb(load) && (velt_type(load)->basic_type() == T_INT)) {
  1813                 vt = velt_type(load);
  1814               } else if (in->Opcode() != Op_LShiftI) {
  1815                 // Widen type to Int to avoid creation of right shift vector
  1816                 // (align + data_size(s1) check in stmts_can_pack() will fail).
  1817                 // Note, left shifts work regardless type.
  1818                 vt = TypeInt::INT;
  1821             set_velt_type(in, vt);
  1827 #ifndef PRODUCT
  1828   if (TraceSuperWord && Verbose) {
  1829     for (int i = 0; i < _block.length(); i++) {
  1830       Node* n = _block.at(i);
  1831       velt_type(n)->dump();
  1832       tty->print("\t");
  1833       n->dump();
  1836 #endif
  1839 //------------------------------memory_alignment---------------------------
  1840 // Alignment within a vector memory reference
  1841 int SuperWord::memory_alignment(MemNode* s, int iv_adjust) {
  1842   SWPointer p(s, this);
  1843   if (!p.valid()) {
  1844     return bottom_align;
  1846   int vw = vector_width_in_bytes(s);
  1847   if (vw < 2) {
  1848     return bottom_align; // No vectors for this type
  1850   int offset  = p.offset_in_bytes();
  1851   offset     += iv_adjust*p.memory_size();
  1852   int off_rem = offset % vw;
  1853   int off_mod = off_rem >= 0 ? off_rem : off_rem + vw;
  1854   return off_mod;
  1857 //---------------------------container_type---------------------------
  1858 // Smallest type containing range of values
  1859 const Type* SuperWord::container_type(Node* n) {
  1860   if (n->is_Mem()) {
  1861     BasicType bt = n->as_Mem()->memory_type();
  1862     if (n->is_Store() && (bt == T_CHAR)) {
  1863       // Use T_SHORT type instead of T_CHAR for stored values because any
  1864       // preceding arithmetic operation extends values to signed Int.
  1865       bt = T_SHORT;
  1867     if (n->Opcode() == Op_LoadUB) {
  1868       // Adjust type for unsigned byte loads, it is important for right shifts.
  1869       // T_BOOLEAN is used because there is no basic type representing type
  1870       // TypeInt::UBYTE. Use of T_BOOLEAN for vectors is fine because only
  1871       // size (one byte) and sign is important.
  1872       bt = T_BOOLEAN;
  1874     return Type::get_const_basic_type(bt);
  1876   const Type* t = _igvn.type(n);
  1877   if (t->basic_type() == T_INT) {
  1878     // A narrow type of arithmetic operations will be determined by
  1879     // propagating the type of memory operations.
  1880     return TypeInt::INT;
  1882   return t;
  1885 bool SuperWord::same_velt_type(Node* n1, Node* n2) {
  1886   const Type* vt1 = velt_type(n1);
  1887   const Type* vt2 = velt_type(n2);
  1888   if (vt1->basic_type() == T_INT && vt2->basic_type() == T_INT) {
  1889     // Compare vectors element sizes for integer types.
  1890     return data_size(n1) == data_size(n2);
  1892   return vt1 == vt2;
  1895 //------------------------------in_packset---------------------------
  1896 // Are s1 and s2 in a pack pair and ordered as s1,s2?
  1897 bool SuperWord::in_packset(Node* s1, Node* s2) {
  1898   for (int i = 0; i < _packset.length(); i++) {
  1899     Node_List* p = _packset.at(i);
  1900     assert(p->size() == 2, "must be");
  1901     if (p->at(0) == s1 && p->at(p->size()-1) == s2) {
  1902       return true;
  1905   return false;
  1908 //------------------------------in_pack---------------------------
  1909 // Is s in pack p?
  1910 Node_List* SuperWord::in_pack(Node* s, Node_List* p) {
  1911   for (uint i = 0; i < p->size(); i++) {
  1912     if (p->at(i) == s) {
  1913       return p;
  1916   return NULL;
  1919 //------------------------------remove_pack_at---------------------------
  1920 // Remove the pack at position pos in the packset
  1921 void SuperWord::remove_pack_at(int pos) {
  1922   Node_List* p = _packset.at(pos);
  1923   for (uint i = 0; i < p->size(); i++) {
  1924     Node* s = p->at(i);
  1925     set_my_pack(s, NULL);
  1927   _packset.remove_at(pos);
  1930 //------------------------------executed_first---------------------------
  1931 // Return the node executed first in pack p.  Uses the RPO block list
  1932 // to determine order.
  1933 Node* SuperWord::executed_first(Node_List* p) {
  1934   Node* n = p->at(0);
  1935   int n_rpo = bb_idx(n);
  1936   for (uint i = 1; i < p->size(); i++) {
  1937     Node* s = p->at(i);
  1938     int s_rpo = bb_idx(s);
  1939     if (s_rpo < n_rpo) {
  1940       n = s;
  1941       n_rpo = s_rpo;
  1944   return n;
  1947 //------------------------------executed_last---------------------------
  1948 // Return the node executed last in pack p.
  1949 Node* SuperWord::executed_last(Node_List* p) {
  1950   Node* n = p->at(0);
  1951   int n_rpo = bb_idx(n);
  1952   for (uint i = 1; i < p->size(); i++) {
  1953     Node* s = p->at(i);
  1954     int s_rpo = bb_idx(s);
  1955     if (s_rpo > n_rpo) {
  1956       n = s;
  1957       n_rpo = s_rpo;
  1960   return n;
  1963 //----------------------------align_initial_loop_index---------------------------
  1964 // Adjust pre-loop limit so that in main loop, a load/store reference
  1965 // to align_to_ref will be a position zero in the vector.
  1966 //   (iv + k) mod vector_align == 0
  1967 void SuperWord::align_initial_loop_index(MemNode* align_to_ref) {
  1968   CountedLoopNode *main_head = lp()->as_CountedLoop();
  1969   assert(main_head->is_main_loop(), "");
  1970   CountedLoopEndNode* pre_end = get_pre_loop_end(main_head);
  1971   assert(pre_end != NULL, "");
  1972   Node *pre_opaq1 = pre_end->limit();
  1973   assert(pre_opaq1->Opcode() == Op_Opaque1, "");
  1974   Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
  1975   Node *lim0 = pre_opaq->in(1);
  1977   // Where we put new limit calculations
  1978   Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
  1980   // Ensure the original loop limit is available from the
  1981   // pre-loop Opaque1 node.
  1982   Node *orig_limit = pre_opaq->original_loop_limit();
  1983   assert(orig_limit != NULL && _igvn.type(orig_limit) != Type::TOP, "");
  1985   SWPointer align_to_ref_p(align_to_ref, this);
  1986   assert(align_to_ref_p.valid(), "sanity");
  1988   // Given:
  1989   //     lim0 == original pre loop limit
  1990   //     V == v_align (power of 2)
  1991   //     invar == extra invariant piece of the address expression
  1992   //     e == offset [ +/- invar ]
  1993   //
  1994   // When reassociating expressions involving '%' the basic rules are:
  1995   //     (a - b) % k == 0   =>  a % k == b % k
  1996   // and:
  1997   //     (a + b) % k == 0   =>  a % k == (k - b) % k
  1998   //
  1999   // For stride > 0 && scale > 0,
  2000   //   Derive the new pre-loop limit "lim" such that the two constraints:
  2001   //     (1) lim = lim0 + N           (where N is some positive integer < V)
  2002   //     (2) (e + lim) % V == 0
  2003   //   are true.
  2004   //
  2005   //   Substituting (1) into (2),
  2006   //     (e + lim0 + N) % V == 0
  2007   //   solve for N:
  2008   //     N = (V - (e + lim0)) % V
  2009   //   substitute back into (1), so that new limit
  2010   //     lim = lim0 + (V - (e + lim0)) % V
  2011   //
  2012   // For stride > 0 && scale < 0
  2013   //   Constraints:
  2014   //     lim = lim0 + N
  2015   //     (e - lim) % V == 0
  2016   //   Solving for lim:
  2017   //     (e - lim0 - N) % V == 0
  2018   //     N = (e - lim0) % V
  2019   //     lim = lim0 + (e - lim0) % V
  2020   //
  2021   // For stride < 0 && scale > 0
  2022   //   Constraints:
  2023   //     lim = lim0 - N
  2024   //     (e + lim) % V == 0
  2025   //   Solving for lim:
  2026   //     (e + lim0 - N) % V == 0
  2027   //     N = (e + lim0) % V
  2028   //     lim = lim0 - (e + lim0) % V
  2029   //
  2030   // For stride < 0 && scale < 0
  2031   //   Constraints:
  2032   //     lim = lim0 - N
  2033   //     (e - lim) % V == 0
  2034   //   Solving for lim:
  2035   //     (e - lim0 + N) % V == 0
  2036   //     N = (V - (e - lim0)) % V
  2037   //     lim = lim0 - (V - (e - lim0)) % V
  2039   int vw = vector_width_in_bytes(align_to_ref);
  2040   int stride   = iv_stride();
  2041   int scale    = align_to_ref_p.scale_in_bytes();
  2042   int elt_size = align_to_ref_p.memory_size();
  2043   int v_align  = vw / elt_size;
  2044   assert(v_align > 1, "sanity");
  2045   int offset   = align_to_ref_p.offset_in_bytes() / elt_size;
  2046   Node *offsn  = _igvn.intcon(offset);
  2048   Node *e = offsn;
  2049   if (align_to_ref_p.invar() != NULL) {
  2050     // incorporate any extra invariant piece producing (offset +/- invar) >>> log2(elt)
  2051     Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
  2052     Node* aref     = new (_phase->C) URShiftINode(align_to_ref_p.invar(), log2_elt);
  2053     _igvn.register_new_node_with_optimizer(aref);
  2054     _phase->set_ctrl(aref, pre_ctrl);
  2055     if (align_to_ref_p.negate_invar()) {
  2056       e = new (_phase->C) SubINode(e, aref);
  2057     } else {
  2058       e = new (_phase->C) AddINode(e, aref);
  2060     _igvn.register_new_node_with_optimizer(e);
  2061     _phase->set_ctrl(e, pre_ctrl);
  2063   if (vw > ObjectAlignmentInBytes) {
  2064     // incorporate base e +/- base && Mask >>> log2(elt)
  2065     Node* xbase = new(_phase->C) CastP2XNode(NULL, align_to_ref_p.base());
  2066     _igvn.register_new_node_with_optimizer(xbase);
  2067 #ifdef _LP64
  2068     xbase  = new (_phase->C) ConvL2INode(xbase);
  2069     _igvn.register_new_node_with_optimizer(xbase);
  2070 #endif
  2071     Node* mask = _igvn.intcon(vw-1);
  2072     Node* masked_xbase  = new (_phase->C) AndINode(xbase, mask);
  2073     _igvn.register_new_node_with_optimizer(masked_xbase);
  2074     Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
  2075     Node* bref     = new (_phase->C) URShiftINode(masked_xbase, log2_elt);
  2076     _igvn.register_new_node_with_optimizer(bref);
  2077     _phase->set_ctrl(bref, pre_ctrl);
  2078     e = new (_phase->C) AddINode(e, bref);
  2079     _igvn.register_new_node_with_optimizer(e);
  2080     _phase->set_ctrl(e, pre_ctrl);
  2083   // compute e +/- lim0
  2084   if (scale < 0) {
  2085     e = new (_phase->C) SubINode(e, lim0);
  2086   } else {
  2087     e = new (_phase->C) AddINode(e, lim0);
  2089   _igvn.register_new_node_with_optimizer(e);
  2090   _phase->set_ctrl(e, pre_ctrl);
  2092   if (stride * scale > 0) {
  2093     // compute V - (e +/- lim0)
  2094     Node* va  = _igvn.intcon(v_align);
  2095     e = new (_phase->C) SubINode(va, e);
  2096     _igvn.register_new_node_with_optimizer(e);
  2097     _phase->set_ctrl(e, pre_ctrl);
  2099   // compute N = (exp) % V
  2100   Node* va_msk = _igvn.intcon(v_align - 1);
  2101   Node* N = new (_phase->C) AndINode(e, va_msk);
  2102   _igvn.register_new_node_with_optimizer(N);
  2103   _phase->set_ctrl(N, pre_ctrl);
  2105   //   substitute back into (1), so that new limit
  2106   //     lim = lim0 + N
  2107   Node* lim;
  2108   if (stride < 0) {
  2109     lim = new (_phase->C) SubINode(lim0, N);
  2110   } else {
  2111     lim = new (_phase->C) AddINode(lim0, N);
  2113   _igvn.register_new_node_with_optimizer(lim);
  2114   _phase->set_ctrl(lim, pre_ctrl);
  2115   Node* constrained =
  2116     (stride > 0) ? (Node*) new (_phase->C) MinINode(lim, orig_limit)
  2117                  : (Node*) new (_phase->C) MaxINode(lim, orig_limit);
  2118   _igvn.register_new_node_with_optimizer(constrained);
  2119   _phase->set_ctrl(constrained, pre_ctrl);
  2120   _igvn.hash_delete(pre_opaq);
  2121   pre_opaq->set_req(1, constrained);
  2124 //----------------------------get_pre_loop_end---------------------------
  2125 // Find pre loop end from main loop.  Returns null if none.
  2126 CountedLoopEndNode* SuperWord::get_pre_loop_end(CountedLoopNode *cl) {
  2127   Node *ctrl = cl->in(LoopNode::EntryControl);
  2128   if (!ctrl->is_IfTrue() && !ctrl->is_IfFalse()) return NULL;
  2129   Node *iffm = ctrl->in(0);
  2130   if (!iffm->is_If()) return NULL;
  2131   Node *p_f = iffm->in(0);
  2132   if (!p_f->is_IfFalse()) return NULL;
  2133   if (!p_f->in(0)->is_CountedLoopEnd()) return NULL;
  2134   CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
  2135   if (!pre_end->loopnode()->is_pre_loop()) return NULL;
  2136   return pre_end;
  2140 //------------------------------init---------------------------
  2141 void SuperWord::init() {
  2142   _dg.init();
  2143   _packset.clear();
  2144   _disjoint_ptrs.clear();
  2145   _block.clear();
  2146   _data_entry.clear();
  2147   _mem_slice_head.clear();
  2148   _mem_slice_tail.clear();
  2149   _node_info.clear();
  2150   _align_to_ref = NULL;
  2151   _lpt = NULL;
  2152   _lp = NULL;
  2153   _bb = NULL;
  2154   _iv = NULL;
  2157 //------------------------------print_packset---------------------------
  2158 void SuperWord::print_packset() {
  2159 #ifndef PRODUCT
  2160   tty->print_cr("packset");
  2161   for (int i = 0; i < _packset.length(); i++) {
  2162     tty->print_cr("Pack: %d", i);
  2163     Node_List* p = _packset.at(i);
  2164     print_pack(p);
  2166 #endif
  2169 //------------------------------print_pack---------------------------
  2170 void SuperWord::print_pack(Node_List* p) {
  2171   for (uint i = 0; i < p->size(); i++) {
  2172     print_stmt(p->at(i));
  2176 //------------------------------print_bb---------------------------
  2177 void SuperWord::print_bb() {
  2178 #ifndef PRODUCT
  2179   tty->print_cr("\nBlock");
  2180   for (int i = 0; i < _block.length(); i++) {
  2181     Node* n = _block.at(i);
  2182     tty->print("%d ", i);
  2183     if (n) {
  2184       n->dump();
  2187 #endif
  2190 //------------------------------print_stmt---------------------------
  2191 void SuperWord::print_stmt(Node* s) {
  2192 #ifndef PRODUCT
  2193   tty->print(" align: %d \t", alignment(s));
  2194   s->dump();
  2195 #endif
  2198 //------------------------------blank---------------------------
  2199 char* SuperWord::blank(uint depth) {
  2200   static char blanks[101];
  2201   assert(depth < 101, "too deep");
  2202   for (uint i = 0; i < depth; i++) blanks[i] = ' ';
  2203   blanks[depth] = '\0';
  2204   return blanks;
  2208 //==============================SWPointer===========================
  2210 //----------------------------SWPointer------------------------
  2211 SWPointer::SWPointer(MemNode* mem, SuperWord* slp) :
  2212   _mem(mem), _slp(slp),  _base(NULL),  _adr(NULL),
  2213   _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {
  2215   Node* adr = mem->in(MemNode::Address);
  2216   if (!adr->is_AddP()) {
  2217     assert(!valid(), "too complex");
  2218     return;
  2220   // Match AddP(base, AddP(ptr, k*iv [+ invariant]), constant)
  2221   Node* base = adr->in(AddPNode::Base);
  2222   //unsafe reference could not be aligned appropriately without runtime checking
  2223   if (base == NULL || base->bottom_type() == Type::TOP) {
  2224     assert(!valid(), "unsafe access");
  2225     return;
  2227   for (int i = 0; i < 3; i++) {
  2228     if (!scaled_iv_plus_offset(adr->in(AddPNode::Offset))) {
  2229       assert(!valid(), "too complex");
  2230       return;
  2232     adr = adr->in(AddPNode::Address);
  2233     if (base == adr || !adr->is_AddP()) {
  2234       break; // stop looking at addp's
  2237   _base = base;
  2238   _adr  = adr;
  2239   assert(valid(), "Usable");
  2242 // Following is used to create a temporary object during
  2243 // the pattern match of an address expression.
  2244 SWPointer::SWPointer(SWPointer* p) :
  2245   _mem(p->_mem), _slp(p->_slp),  _base(NULL),  _adr(NULL),
  2246   _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {}
  2248 //------------------------scaled_iv_plus_offset--------------------
  2249 // Match: k*iv + offset
  2250 // where: k is a constant that maybe zero, and
  2251 //        offset is (k2 [+/- invariant]) where k2 maybe zero and invariant is optional
  2252 bool SWPointer::scaled_iv_plus_offset(Node* n) {
  2253   if (scaled_iv(n)) {
  2254     return true;
  2256   if (offset_plus_k(n)) {
  2257     return true;
  2259   int opc = n->Opcode();
  2260   if (opc == Op_AddI) {
  2261     if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2))) {
  2262       return true;
  2264     if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
  2265       return true;
  2267   } else if (opc == Op_SubI) {
  2268     if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2), true)) {
  2269       return true;
  2271     if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
  2272       _scale *= -1;
  2273       return true;
  2276   return false;
  2279 //----------------------------scaled_iv------------------------
  2280 // Match: k*iv where k is a constant that's not zero
  2281 bool SWPointer::scaled_iv(Node* n) {
  2282   if (_scale != 0) {
  2283     return false;  // already found a scale
  2285   if (n == iv()) {
  2286     _scale = 1;
  2287     return true;
  2289   int opc = n->Opcode();
  2290   if (opc == Op_MulI) {
  2291     if (n->in(1) == iv() && n->in(2)->is_Con()) {
  2292       _scale = n->in(2)->get_int();
  2293       return true;
  2294     } else if (n->in(2) == iv() && n->in(1)->is_Con()) {
  2295       _scale = n->in(1)->get_int();
  2296       return true;
  2298   } else if (opc == Op_LShiftI) {
  2299     if (n->in(1) == iv() && n->in(2)->is_Con()) {
  2300       _scale = 1 << n->in(2)->get_int();
  2301       return true;
  2303   } else if (opc == Op_ConvI2L) {
  2304     if (scaled_iv_plus_offset(n->in(1))) {
  2305       return true;
  2307   } else if (opc == Op_LShiftL) {
  2308     if (!has_iv() && _invar == NULL) {
  2309       // Need to preserve the current _offset value, so
  2310       // create a temporary object for this expression subtree.
  2311       // Hacky, so should re-engineer the address pattern match.
  2312       SWPointer tmp(this);
  2313       if (tmp.scaled_iv_plus_offset(n->in(1))) {
  2314         if (tmp._invar == NULL) {
  2315           int mult = 1 << n->in(2)->get_int();
  2316           _scale   = tmp._scale  * mult;
  2317           _offset += tmp._offset * mult;
  2318           return true;
  2323   return false;
  2326 //----------------------------offset_plus_k------------------------
  2327 // Match: offset is (k [+/- invariant])
  2328 // where k maybe zero and invariant is optional, but not both.
  2329 bool SWPointer::offset_plus_k(Node* n, bool negate) {
  2330   int opc = n->Opcode();
  2331   if (opc == Op_ConI) {
  2332     _offset += negate ? -(n->get_int()) : n->get_int();
  2333     return true;
  2334   } else if (opc == Op_ConL) {
  2335     // Okay if value fits into an int
  2336     const TypeLong* t = n->find_long_type();
  2337     if (t->higher_equal(TypeLong::INT)) {
  2338       jlong loff = n->get_long();
  2339       jint  off  = (jint)loff;
  2340       _offset += negate ? -off : loff;
  2341       return true;
  2343     return false;
  2345   if (_invar != NULL) return false; // already have an invariant
  2346   if (opc == Op_AddI) {
  2347     if (n->in(2)->is_Con() && invariant(n->in(1))) {
  2348       _negate_invar = negate;
  2349       _invar = n->in(1);
  2350       _offset += negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
  2351       return true;
  2352     } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
  2353       _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
  2354       _negate_invar = negate;
  2355       _invar = n->in(2);
  2356       return true;
  2359   if (opc == Op_SubI) {
  2360     if (n->in(2)->is_Con() && invariant(n->in(1))) {
  2361       _negate_invar = negate;
  2362       _invar = n->in(1);
  2363       _offset += !negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
  2364       return true;
  2365     } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
  2366       _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
  2367       _negate_invar = !negate;
  2368       _invar = n->in(2);
  2369       return true;
  2372   if (invariant(n)) {
  2373     _negate_invar = negate;
  2374     _invar = n;
  2375     return true;
  2377   return false;
  2380 //----------------------------print------------------------
  2381 void SWPointer::print() {
  2382 #ifndef PRODUCT
  2383   tty->print("base: %d  adr: %d  scale: %d  offset: %d  invar: %c%d\n",
  2384              _base != NULL ? _base->_idx : 0,
  2385              _adr  != NULL ? _adr->_idx  : 0,
  2386              _scale, _offset,
  2387              _negate_invar?'-':'+',
  2388              _invar != NULL ? _invar->_idx : 0);
  2389 #endif
  2392 // ========================= OrderedPair =====================
  2394 const OrderedPair OrderedPair::initial;
  2396 // ========================= SWNodeInfo =====================
  2398 const SWNodeInfo SWNodeInfo::initial;
  2401 // ============================ DepGraph ===========================
  2403 //------------------------------make_node---------------------------
  2404 // Make a new dependence graph node for an ideal node.
  2405 DepMem* DepGraph::make_node(Node* node) {
  2406   DepMem* m = new (_arena) DepMem(node);
  2407   if (node != NULL) {
  2408     assert(_map.at_grow(node->_idx) == NULL, "one init only");
  2409     _map.at_put_grow(node->_idx, m);
  2411   return m;
  2414 //------------------------------make_edge---------------------------
  2415 // Make a new dependence graph edge from dpred -> dsucc
  2416 DepEdge* DepGraph::make_edge(DepMem* dpred, DepMem* dsucc) {
  2417   DepEdge* e = new (_arena) DepEdge(dpred, dsucc, dsucc->in_head(), dpred->out_head());
  2418   dpred->set_out_head(e);
  2419   dsucc->set_in_head(e);
  2420   return e;
  2423 // ========================== DepMem ========================
  2425 //------------------------------in_cnt---------------------------
  2426 int DepMem::in_cnt() {
  2427   int ct = 0;
  2428   for (DepEdge* e = _in_head; e != NULL; e = e->next_in()) ct++;
  2429   return ct;
  2432 //------------------------------out_cnt---------------------------
  2433 int DepMem::out_cnt() {
  2434   int ct = 0;
  2435   for (DepEdge* e = _out_head; e != NULL; e = e->next_out()) ct++;
  2436   return ct;
  2439 //------------------------------print-----------------------------
  2440 void DepMem::print() {
  2441 #ifndef PRODUCT
  2442   tty->print("  DepNode %d (", _node->_idx);
  2443   for (DepEdge* p = _in_head; p != NULL; p = p->next_in()) {
  2444     Node* pred = p->pred()->node();
  2445     tty->print(" %d", pred != NULL ? pred->_idx : 0);
  2447   tty->print(") [");
  2448   for (DepEdge* s = _out_head; s != NULL; s = s->next_out()) {
  2449     Node* succ = s->succ()->node();
  2450     tty->print(" %d", succ != NULL ? succ->_idx : 0);
  2452   tty->print_cr(" ]");
  2453 #endif
  2456 // =========================== DepEdge =========================
  2458 //------------------------------DepPreds---------------------------
  2459 void DepEdge::print() {
  2460 #ifndef PRODUCT
  2461   tty->print_cr("DepEdge: %d [ %d ]", _pred->node()->_idx, _succ->node()->_idx);
  2462 #endif
  2465 // =========================== DepPreds =========================
  2466 // Iterator over predecessor edges in the dependence graph.
  2468 //------------------------------DepPreds---------------------------
  2469 DepPreds::DepPreds(Node* n, DepGraph& dg) {
  2470   _n = n;
  2471   _done = false;
  2472   if (_n->is_Store() || _n->is_Load()) {
  2473     _next_idx = MemNode::Address;
  2474     _end_idx  = n->req();
  2475     _dep_next = dg.dep(_n)->in_head();
  2476   } else if (_n->is_Mem()) {
  2477     _next_idx = 0;
  2478     _end_idx  = 0;
  2479     _dep_next = dg.dep(_n)->in_head();
  2480   } else {
  2481     _next_idx = 1;
  2482     _end_idx  = _n->req();
  2483     _dep_next = NULL;
  2485   next();
  2488 //------------------------------next---------------------------
  2489 void DepPreds::next() {
  2490   if (_dep_next != NULL) {
  2491     _current  = _dep_next->pred()->node();
  2492     _dep_next = _dep_next->next_in();
  2493   } else if (_next_idx < _end_idx) {
  2494     _current  = _n->in(_next_idx++);
  2495   } else {
  2496     _done = true;
  2500 // =========================== DepSuccs =========================
  2501 // Iterator over successor edges in the dependence graph.
  2503 //------------------------------DepSuccs---------------------------
  2504 DepSuccs::DepSuccs(Node* n, DepGraph& dg) {
  2505   _n = n;
  2506   _done = false;
  2507   if (_n->is_Load()) {
  2508     _next_idx = 0;
  2509     _end_idx  = _n->outcnt();
  2510     _dep_next = dg.dep(_n)->out_head();
  2511   } else if (_n->is_Mem() || _n->is_Phi() && _n->bottom_type() == Type::MEMORY) {
  2512     _next_idx = 0;
  2513     _end_idx  = 0;
  2514     _dep_next = dg.dep(_n)->out_head();
  2515   } else {
  2516     _next_idx = 0;
  2517     _end_idx  = _n->outcnt();
  2518     _dep_next = NULL;
  2520   next();
  2523 //-------------------------------next---------------------------
  2524 void DepSuccs::next() {
  2525   if (_dep_next != NULL) {
  2526     _current  = _dep_next->succ()->node();
  2527     _dep_next = _dep_next->next_out();
  2528   } else if (_next_idx < _end_idx) {
  2529     _current  = _n->raw_out(_next_idx++);
  2530   } else {
  2531     _done = true;

mercurial