hotspot/src/share/vm/opto/superword.cpp
changeset 1 489c9b5090e2
child 202 dc13bf0e5d5d
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 #include "incls/_precompiled.incl"
       
    25 #include "incls/_superword.cpp.incl"
       
    26 
       
    27 //
       
    28 //                  S U P E R W O R D   T R A N S F O R M
       
    29 //=============================================================================
       
    30 
       
    31 //------------------------------SuperWord---------------------------
       
    32 SuperWord::SuperWord(PhaseIdealLoop* phase) :
       
    33   _phase(phase),
       
    34   _igvn(phase->_igvn),
       
    35   _arena(phase->C->comp_arena()),
       
    36   _packset(arena(), 8,  0, NULL),         // packs for the current block
       
    37   _bb_idx(arena(), (int)(1.10 * phase->C->unique()), 0, 0), // node idx to index in bb
       
    38   _block(arena(), 8,  0, NULL),           // nodes in current block
       
    39   _data_entry(arena(), 8,  0, NULL),      // nodes with all inputs from outside
       
    40   _mem_slice_head(arena(), 8,  0, NULL),  // memory slice heads
       
    41   _mem_slice_tail(arena(), 8,  0, NULL),  // memory slice tails
       
    42   _node_info(arena(), 8,  0, SWNodeInfo::initial), // info needed per node
       
    43   _align_to_ref(NULL),                    // memory reference to align vectors to
       
    44   _disjoint_ptrs(arena(), 8,  0, OrderedPair::initial), // runtime disambiguated pointer pairs
       
    45   _dg(_arena),                            // dependence graph
       
    46   _visited(arena()),                      // visited node set
       
    47   _post_visited(arena()),                 // post visited node set
       
    48   _n_idx_list(arena(), 8),                // scratch list of (node,index) pairs
       
    49   _stk(arena(), 8, 0, NULL),              // scratch stack of nodes
       
    50   _nlist(arena(), 8, 0, NULL),            // scratch list of nodes
       
    51   _lpt(NULL),                             // loop tree node
       
    52   _lp(NULL),                              // LoopNode
       
    53   _bb(NULL),                              // basic block
       
    54   _iv(NULL)                               // induction var
       
    55 {}
       
    56 
       
    57 //------------------------------transform_loop---------------------------
       
    58 void SuperWord::transform_loop(IdealLoopTree* lpt) {
       
    59   assert(lpt->_head->is_CountedLoop(), "must be");
       
    60   CountedLoopNode *cl = lpt->_head->as_CountedLoop();
       
    61 
       
    62   if (!cl->is_main_loop() ) return; // skip normal, pre, and post loops
       
    63 
       
    64   // Check for no control flow in body (other than exit)
       
    65   Node *cl_exit = cl->loopexit();
       
    66   if (cl_exit->in(0) != lpt->_head) return;
       
    67 
       
    68   // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit))))
       
    69   CountedLoopEndNode* pre_end = get_pre_loop_end(cl);
       
    70   if (pre_end == NULL) return;
       
    71   Node *pre_opaq1 = pre_end->limit();
       
    72   if (pre_opaq1->Opcode() != Op_Opaque1) return;
       
    73 
       
    74   // Do vectors exist on this architecture?
       
    75   if (vector_width_in_bytes() == 0) return;
       
    76 
       
    77   init(); // initialize data structures
       
    78 
       
    79   set_lpt(lpt);
       
    80   set_lp(cl);
       
    81 
       
    82  // For now, define one block which is the entire loop body
       
    83   set_bb(cl);
       
    84 
       
    85   assert(_packset.length() == 0, "packset must be empty");
       
    86   SLP_extract();
       
    87 }
       
    88 
       
    89 //------------------------------SLP_extract---------------------------
       
    90 // Extract the superword level parallelism
       
    91 //
       
    92 // 1) A reverse post-order of nodes in the block is constructed.  By scanning
       
    93 //    this list from first to last, all definitions are visited before their uses.
       
    94 //
       
    95 // 2) A point-to-point dependence graph is constructed between memory references.
       
    96 //    This simplies the upcoming "independence" checker.
       
    97 //
       
    98 // 3) The maximum depth in the node graph from the beginning of the block
       
    99 //    to each node is computed.  This is used to prune the graph search
       
   100 //    in the independence checker.
       
   101 //
       
   102 // 4) For integer types, the necessary bit width is propagated backwards
       
   103 //    from stores to allow packed operations on byte, char, and short
       
   104 //    integers.  This reverses the promotion to type "int" that javac
       
   105 //    did for operations like: char c1,c2,c3;  c1 = c2 + c3.
       
   106 //
       
   107 // 5) One of the memory references is picked to be an aligned vector reference.
       
   108 //    The pre-loop trip count is adjusted to align this reference in the
       
   109 //    unrolled body.
       
   110 //
       
   111 // 6) The initial set of pack pairs is seeded with memory references.
       
   112 //
       
   113 // 7) The set of pack pairs is extended by following use->def and def->use links.
       
   114 //
       
   115 // 8) The pairs are combined into vector sized packs.
       
   116 //
       
   117 // 9) Reorder the memory slices to co-locate members of the memory packs.
       
   118 //
       
   119 // 10) Generate ideal vector nodes for the final set of packs and where necessary,
       
   120 //    inserting scalar promotion, vector creation from multiple scalars, and
       
   121 //    extraction of scalar values from vectors.
       
   122 //
       
   123 void SuperWord::SLP_extract() {
       
   124 
       
   125   // Ready the block
       
   126 
       
   127   construct_bb();
       
   128 
       
   129   dependence_graph();
       
   130 
       
   131   compute_max_depth();
       
   132 
       
   133   compute_vector_element_type();
       
   134 
       
   135   // Attempt vectorization
       
   136 
       
   137   find_adjacent_refs();
       
   138 
       
   139   extend_packlist();
       
   140 
       
   141   combine_packs();
       
   142 
       
   143   construct_my_pack_map();
       
   144 
       
   145   filter_packs();
       
   146 
       
   147   schedule();
       
   148 
       
   149   output();
       
   150 }
       
   151 
       
   152 //------------------------------find_adjacent_refs---------------------------
       
   153 // Find the adjacent memory references and create pack pairs for them.
       
   154 // This is the initial set of packs that will then be extended by
       
   155 // following use->def and def->use links.  The align positions are
       
   156 // assigned relative to the reference "align_to_ref"
       
   157 void SuperWord::find_adjacent_refs() {
       
   158   // Get list of memory operations
       
   159   Node_List memops;
       
   160   for (int i = 0; i < _block.length(); i++) {
       
   161     Node* n = _block.at(i);
       
   162     if (n->is_Mem() && in_bb(n)) {
       
   163       int align = memory_alignment(n->as_Mem(), 0);
       
   164       if (align != bottom_align) {
       
   165         memops.push(n);
       
   166       }
       
   167     }
       
   168   }
       
   169   if (memops.size() == 0) return;
       
   170 
       
   171   // Find a memory reference to align to.  The pre-loop trip count
       
   172   // is modified to align this reference to a vector-aligned address
       
   173   find_align_to_ref(memops);
       
   174   if (align_to_ref() == NULL) return;
       
   175 
       
   176   SWPointer align_to_ref_p(align_to_ref(), this);
       
   177   int offset = align_to_ref_p.offset_in_bytes();
       
   178   int scale  = align_to_ref_p.scale_in_bytes();
       
   179   int vw              = vector_width_in_bytes();
       
   180   int stride_sign     = (scale * iv_stride()) > 0 ? 1 : -1;
       
   181   int iv_adjustment   = (stride_sign * vw - (offset % vw)) % vw;
       
   182 
       
   183 #ifndef PRODUCT
       
   184   if (TraceSuperWord)
       
   185     tty->print_cr("\noffset = %d iv_adjustment = %d  elt_align = %d",
       
   186                   offset, iv_adjustment, align_to_ref_p.memory_size());
       
   187 #endif
       
   188 
       
   189   // Set alignment relative to "align_to_ref"
       
   190   for (int i = memops.size() - 1; i >= 0; i--) {
       
   191     MemNode* s = memops.at(i)->as_Mem();
       
   192     SWPointer p2(s, this);
       
   193     if (p2.comparable(align_to_ref_p)) {
       
   194       int align = memory_alignment(s, iv_adjustment);
       
   195       set_alignment(s, align);
       
   196     } else {
       
   197       memops.remove(i);
       
   198     }
       
   199   }
       
   200 
       
   201   // Create initial pack pairs of memory operations
       
   202   for (uint i = 0; i < memops.size(); i++) {
       
   203     Node* s1 = memops.at(i);
       
   204     for (uint j = 0; j < memops.size(); j++) {
       
   205       Node* s2 = memops.at(j);
       
   206       if (s1 != s2 && are_adjacent_refs(s1, s2)) {
       
   207         int align = alignment(s1);
       
   208         if (stmts_can_pack(s1, s2, align)) {
       
   209           Node_List* pair = new Node_List();
       
   210           pair->push(s1);
       
   211           pair->push(s2);
       
   212           _packset.append(pair);
       
   213         }
       
   214       }
       
   215     }
       
   216   }
       
   217 
       
   218 #ifndef PRODUCT
       
   219   if (TraceSuperWord) {
       
   220     tty->print_cr("\nAfter find_adjacent_refs");
       
   221     print_packset();
       
   222   }
       
   223 #endif
       
   224 }
       
   225 
       
   226 //------------------------------find_align_to_ref---------------------------
       
   227 // Find a memory reference to align the loop induction variable to.
       
   228 // Looks first at stores then at loads, looking for a memory reference
       
   229 // with the largest number of references similar to it.
       
   230 void SuperWord::find_align_to_ref(Node_List &memops) {
       
   231   GrowableArray<int> cmp_ct(arena(), memops.size(), memops.size(), 0);
       
   232 
       
   233   // Count number of comparable memory ops
       
   234   for (uint i = 0; i < memops.size(); i++) {
       
   235     MemNode* s1 = memops.at(i)->as_Mem();
       
   236     SWPointer p1(s1, this);
       
   237     // Discard if pre loop can't align this reference
       
   238     if (!ref_is_alignable(p1)) {
       
   239       *cmp_ct.adr_at(i) = 0;
       
   240       continue;
       
   241     }
       
   242     for (uint j = i+1; j < memops.size(); j++) {
       
   243       MemNode* s2 = memops.at(j)->as_Mem();
       
   244       if (isomorphic(s1, s2)) {
       
   245         SWPointer p2(s2, this);
       
   246         if (p1.comparable(p2)) {
       
   247           (*cmp_ct.adr_at(i))++;
       
   248           (*cmp_ct.adr_at(j))++;
       
   249         }
       
   250       }
       
   251     }
       
   252   }
       
   253 
       
   254   // Find Store (or Load) with the greatest number of "comparable" references
       
   255   int max_ct        = 0;
       
   256   int max_idx       = -1;
       
   257   int min_size      = max_jint;
       
   258   int min_iv_offset = max_jint;
       
   259   for (uint j = 0; j < memops.size(); j++) {
       
   260     MemNode* s = memops.at(j)->as_Mem();
       
   261     if (s->is_Store()) {
       
   262       SWPointer p(s, this);
       
   263       if (cmp_ct.at(j) > max_ct ||
       
   264           cmp_ct.at(j) == max_ct && (data_size(s) < min_size ||
       
   265                                      data_size(s) == min_size &&
       
   266                                         p.offset_in_bytes() < min_iv_offset)) {
       
   267         max_ct = cmp_ct.at(j);
       
   268         max_idx = j;
       
   269         min_size = data_size(s);
       
   270         min_iv_offset = p.offset_in_bytes();
       
   271       }
       
   272     }
       
   273   }
       
   274   // If no stores, look at loads
       
   275   if (max_ct == 0) {
       
   276     for (uint j = 0; j < memops.size(); j++) {
       
   277       MemNode* s = memops.at(j)->as_Mem();
       
   278       if (s->is_Load()) {
       
   279         SWPointer p(s, this);
       
   280         if (cmp_ct.at(j) > max_ct ||
       
   281             cmp_ct.at(j) == max_ct && (data_size(s) < min_size ||
       
   282                                        data_size(s) == min_size &&
       
   283                                           p.offset_in_bytes() < min_iv_offset)) {
       
   284           max_ct = cmp_ct.at(j);
       
   285           max_idx = j;
       
   286           min_size = data_size(s);
       
   287           min_iv_offset = p.offset_in_bytes();
       
   288         }
       
   289       }
       
   290     }
       
   291   }
       
   292 
       
   293   if (max_ct > 0)
       
   294     set_align_to_ref(memops.at(max_idx)->as_Mem());
       
   295 
       
   296 #ifndef PRODUCT
       
   297   if (TraceSuperWord && Verbose) {
       
   298     tty->print_cr("\nVector memops after find_align_to_refs");
       
   299     for (uint i = 0; i < memops.size(); i++) {
       
   300       MemNode* s = memops.at(i)->as_Mem();
       
   301       s->dump();
       
   302     }
       
   303   }
       
   304 #endif
       
   305 }
       
   306 
       
   307 //------------------------------ref_is_alignable---------------------------
       
   308 // Can the preloop align the reference to position zero in the vector?
       
   309 bool SuperWord::ref_is_alignable(SWPointer& p) {
       
   310   if (!p.has_iv()) {
       
   311     return true;   // no induction variable
       
   312   }
       
   313   CountedLoopEndNode* pre_end = get_pre_loop_end(lp()->as_CountedLoop());
       
   314   assert(pre_end->stride_is_con(), "pre loop stride is constant");
       
   315   int preloop_stride = pre_end->stride_con();
       
   316 
       
   317   int span = preloop_stride * p.scale_in_bytes();
       
   318 
       
   319   // Stride one accesses are alignable.
       
   320   if (ABS(span) == p.memory_size())
       
   321     return true;
       
   322 
       
   323   // If initial offset from start of object is computable,
       
   324   // compute alignment within the vector.
       
   325   int vw = vector_width_in_bytes();
       
   326   if (vw % span == 0) {
       
   327     Node* init_nd = pre_end->init_trip();
       
   328     if (init_nd->is_Con() && p.invar() == NULL) {
       
   329       int init = init_nd->bottom_type()->is_int()->get_con();
       
   330 
       
   331       int init_offset = init * p.scale_in_bytes() + p.offset_in_bytes();
       
   332       assert(init_offset >= 0, "positive offset from object start");
       
   333 
       
   334       if (span > 0) {
       
   335         return (vw - (init_offset % vw)) % span == 0;
       
   336       } else {
       
   337         assert(span < 0, "nonzero stride * scale");
       
   338         return (init_offset % vw) % -span == 0;
       
   339       }
       
   340     }
       
   341   }
       
   342   return false;
       
   343 }
       
   344 
       
   345 //---------------------------dependence_graph---------------------------
       
   346 // Construct dependency graph.
       
   347 // Add dependence edges to load/store nodes for memory dependence
       
   348 //    A.out()->DependNode.in(1) and DependNode.out()->B.prec(x)
       
   349 void SuperWord::dependence_graph() {
       
   350   // First, assign a dependence node to each memory node
       
   351   for (int i = 0; i < _block.length(); i++ ) {
       
   352     Node *n = _block.at(i);
       
   353     if (n->is_Mem() || n->is_Phi() && n->bottom_type() == Type::MEMORY) {
       
   354       _dg.make_node(n);
       
   355     }
       
   356   }
       
   357 
       
   358   // For each memory slice, create the dependences
       
   359   for (int i = 0; i < _mem_slice_head.length(); i++) {
       
   360     Node* n      = _mem_slice_head.at(i);
       
   361     Node* n_tail = _mem_slice_tail.at(i);
       
   362 
       
   363     // Get slice in predecessor order (last is first)
       
   364     mem_slice_preds(n_tail, n, _nlist);
       
   365 
       
   366     // Make the slice dependent on the root
       
   367     DepMem* slice = _dg.dep(n);
       
   368     _dg.make_edge(_dg.root(), slice);
       
   369 
       
   370     // Create a sink for the slice
       
   371     DepMem* slice_sink = _dg.make_node(NULL);
       
   372     _dg.make_edge(slice_sink, _dg.tail());
       
   373 
       
   374     // Now visit each pair of memory ops, creating the edges
       
   375     for (int j = _nlist.length() - 1; j >= 0 ; j--) {
       
   376       Node* s1 = _nlist.at(j);
       
   377 
       
   378       // If no dependency yet, use slice
       
   379       if (_dg.dep(s1)->in_cnt() == 0) {
       
   380         _dg.make_edge(slice, s1);
       
   381       }
       
   382       SWPointer p1(s1->as_Mem(), this);
       
   383       bool sink_dependent = true;
       
   384       for (int k = j - 1; k >= 0; k--) {
       
   385         Node* s2 = _nlist.at(k);
       
   386         if (s1->is_Load() && s2->is_Load())
       
   387           continue;
       
   388         SWPointer p2(s2->as_Mem(), this);
       
   389 
       
   390         int cmp = p1.cmp(p2);
       
   391         if (SuperWordRTDepCheck &&
       
   392             p1.base() != p2.base() && p1.valid() && p2.valid()) {
       
   393           // Create a runtime check to disambiguate
       
   394           OrderedPair pp(p1.base(), p2.base());
       
   395           _disjoint_ptrs.append_if_missing(pp);
       
   396         } else if (!SWPointer::not_equal(cmp)) {
       
   397           // Possibly same address
       
   398           _dg.make_edge(s1, s2);
       
   399           sink_dependent = false;
       
   400         }
       
   401       }
       
   402       if (sink_dependent) {
       
   403         _dg.make_edge(s1, slice_sink);
       
   404       }
       
   405     }
       
   406 #ifndef PRODUCT
       
   407     if (TraceSuperWord) {
       
   408       tty->print_cr("\nDependence graph for slice: %d", n->_idx);
       
   409       for (int q = 0; q < _nlist.length(); q++) {
       
   410         _dg.print(_nlist.at(q));
       
   411       }
       
   412       tty->cr();
       
   413     }
       
   414 #endif
       
   415     _nlist.clear();
       
   416   }
       
   417 
       
   418 #ifndef PRODUCT
       
   419   if (TraceSuperWord) {
       
   420     tty->print_cr("\ndisjoint_ptrs: %s", _disjoint_ptrs.length() > 0 ? "" : "NONE");
       
   421     for (int r = 0; r < _disjoint_ptrs.length(); r++) {
       
   422       _disjoint_ptrs.at(r).print();
       
   423       tty->cr();
       
   424     }
       
   425     tty->cr();
       
   426   }
       
   427 #endif
       
   428 }
       
   429 
       
   430 //---------------------------mem_slice_preds---------------------------
       
   431 // Return a memory slice (node list) in predecessor order starting at "start"
       
   432 void SuperWord::mem_slice_preds(Node* start, Node* stop, GrowableArray<Node*> &preds) {
       
   433   assert(preds.length() == 0, "start empty");
       
   434   Node* n = start;
       
   435   Node* prev = NULL;
       
   436   while (true) {
       
   437     assert(in_bb(n), "must be in block");
       
   438     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
       
   439       Node* out = n->fast_out(i);
       
   440       if (out->is_Load()) {
       
   441         if (in_bb(out)) {
       
   442           preds.push(out);
       
   443         }
       
   444       } else {
       
   445         // FIXME
       
   446         if (out->is_MergeMem() && !in_bb(out)) {
       
   447           // Either unrolling is causing a memory edge not to disappear,
       
   448           // or need to run igvn.optimize() again before SLP
       
   449         } else if (out->is_Phi() && out->bottom_type() == Type::MEMORY && !in_bb(out)) {
       
   450           // Ditto.  Not sure what else to check further.
       
   451         } else if (out->Opcode() == Op_StoreCM && out->in(4) == n) {
       
   452           // StoreCM has an input edge used as a precedence edge.
       
   453           // Maybe an issue when oop stores are vectorized.
       
   454         } else {
       
   455           assert(out == prev || prev == NULL, "no branches off of store slice");
       
   456         }
       
   457       }
       
   458     }
       
   459     if (n == stop) break;
       
   460     preds.push(n);
       
   461     prev = n;
       
   462     n = n->in(MemNode::Memory);
       
   463   }
       
   464 }
       
   465 
       
   466 //------------------------------stmts_can_pack---------------------------
       
   467 // Can s1 and s2 be in a pack with s1 immediately preceeding s2 and
       
   468 // s1 aligned at "align"
       
   469 bool SuperWord::stmts_can_pack(Node* s1, Node* s2, int align) {
       
   470   if (isomorphic(s1, s2)) {
       
   471     if (independent(s1, s2)) {
       
   472       if (!exists_at(s1, 0) && !exists_at(s2, 1)) {
       
   473         if (!s1->is_Mem() || are_adjacent_refs(s1, s2)) {
       
   474           int s1_align = alignment(s1);
       
   475           int s2_align = alignment(s2);
       
   476           if (s1_align == top_align || s1_align == align) {
       
   477             if (s2_align == top_align || s2_align == align + data_size(s1)) {
       
   478               return true;
       
   479             }
       
   480           }
       
   481         }
       
   482       }
       
   483     }
       
   484   }
       
   485   return false;
       
   486 }
       
   487 
       
   488 //------------------------------exists_at---------------------------
       
   489 // Does s exist in a pack at position pos?
       
   490 bool SuperWord::exists_at(Node* s, uint pos) {
       
   491   for (int i = 0; i < _packset.length(); i++) {
       
   492     Node_List* p = _packset.at(i);
       
   493     if (p->at(pos) == s) {
       
   494       return true;
       
   495     }
       
   496   }
       
   497   return false;
       
   498 }
       
   499 
       
   500 //------------------------------are_adjacent_refs---------------------------
       
   501 // Is s1 immediately before s2 in memory?
       
   502 bool SuperWord::are_adjacent_refs(Node* s1, Node* s2) {
       
   503   if (!s1->is_Mem() || !s2->is_Mem()) return false;
       
   504   if (!in_bb(s1)    || !in_bb(s2))    return false;
       
   505   // FIXME - co_locate_pack fails on Stores in different mem-slices, so
       
   506   // only pack memops that are in the same alias set until that's fixed.
       
   507   if (_phase->C->get_alias_index(s1->as_Mem()->adr_type()) !=
       
   508       _phase->C->get_alias_index(s2->as_Mem()->adr_type()))
       
   509     return false;
       
   510   SWPointer p1(s1->as_Mem(), this);
       
   511   SWPointer p2(s2->as_Mem(), this);
       
   512   if (p1.base() != p2.base() || !p1.comparable(p2)) return false;
       
   513   int diff = p2.offset_in_bytes() - p1.offset_in_bytes();
       
   514   return diff == data_size(s1);
       
   515 }
       
   516 
       
   517 //------------------------------isomorphic---------------------------
       
   518 // Are s1 and s2 similar?
       
   519 bool SuperWord::isomorphic(Node* s1, Node* s2) {
       
   520   if (s1->Opcode() != s2->Opcode()) return false;
       
   521   if (s1->req() != s2->req()) return false;
       
   522   if (s1->in(0) != s2->in(0)) return false;
       
   523   if (velt_type(s1) != velt_type(s2)) return false;
       
   524   return true;
       
   525 }
       
   526 
       
   527 //------------------------------independent---------------------------
       
   528 // Is there no data path from s1 to s2 or s2 to s1?
       
   529 bool SuperWord::independent(Node* s1, Node* s2) {
       
   530   //  assert(s1->Opcode() == s2->Opcode(), "check isomorphic first");
       
   531   int d1 = depth(s1);
       
   532   int d2 = depth(s2);
       
   533   if (d1 == d2) return s1 != s2;
       
   534   Node* deep    = d1 > d2 ? s1 : s2;
       
   535   Node* shallow = d1 > d2 ? s2 : s1;
       
   536 
       
   537   visited_clear();
       
   538 
       
   539   return independent_path(shallow, deep);
       
   540 }
       
   541 
       
   542 //------------------------------independent_path------------------------------
       
   543 // Helper for independent
       
   544 bool SuperWord::independent_path(Node* shallow, Node* deep, uint dp) {
       
   545   if (dp >= 1000) return false; // stop deep recursion
       
   546   visited_set(deep);
       
   547   int shal_depth = depth(shallow);
       
   548   assert(shal_depth <= depth(deep), "must be");
       
   549   for (DepPreds preds(deep, _dg); !preds.done(); preds.next()) {
       
   550     Node* pred = preds.current();
       
   551     if (in_bb(pred) && !visited_test(pred)) {
       
   552       if (shallow == pred) {
       
   553         return false;
       
   554       }
       
   555       if (shal_depth < depth(pred) && !independent_path(shallow, pred, dp+1)) {
       
   556         return false;
       
   557       }
       
   558     }
       
   559   }
       
   560   return true;
       
   561 }
       
   562 
       
   563 //------------------------------set_alignment---------------------------
       
   564 void SuperWord::set_alignment(Node* s1, Node* s2, int align) {
       
   565   set_alignment(s1, align);
       
   566   set_alignment(s2, align + data_size(s1));
       
   567 }
       
   568 
       
   569 //------------------------------data_size---------------------------
       
   570 int SuperWord::data_size(Node* s) {
       
   571   const Type* t = velt_type(s);
       
   572   BasicType  bt = t->array_element_basic_type();
       
   573   int bsize = type2aelembytes[bt];
       
   574   assert(bsize != 0, "valid size");
       
   575   return bsize;
       
   576 }
       
   577 
       
   578 //------------------------------extend_packlist---------------------------
       
   579 // Extend packset by following use->def and def->use links from pack members.
       
   580 void SuperWord::extend_packlist() {
       
   581   bool changed;
       
   582   do {
       
   583     changed = false;
       
   584     for (int i = 0; i < _packset.length(); i++) {
       
   585       Node_List* p = _packset.at(i);
       
   586       changed |= follow_use_defs(p);
       
   587       changed |= follow_def_uses(p);
       
   588     }
       
   589   } while (changed);
       
   590 
       
   591 #ifndef PRODUCT
       
   592   if (TraceSuperWord) {
       
   593     tty->print_cr("\nAfter extend_packlist");
       
   594     print_packset();
       
   595   }
       
   596 #endif
       
   597 }
       
   598 
       
   599 //------------------------------follow_use_defs---------------------------
       
   600 // Extend the packset by visiting operand definitions of nodes in pack p
       
   601 bool SuperWord::follow_use_defs(Node_List* p) {
       
   602   Node* s1 = p->at(0);
       
   603   Node* s2 = p->at(1);
       
   604   assert(p->size() == 2, "just checking");
       
   605   assert(s1->req() == s2->req(), "just checking");
       
   606   assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
       
   607 
       
   608   if (s1->is_Load()) return false;
       
   609 
       
   610   int align = alignment(s1);
       
   611   bool changed = false;
       
   612   int start = s1->is_Store() ? MemNode::ValueIn   : 1;
       
   613   int end   = s1->is_Store() ? MemNode::ValueIn+1 : s1->req();
       
   614   for (int j = start; j < end; j++) {
       
   615     Node* t1 = s1->in(j);
       
   616     Node* t2 = s2->in(j);
       
   617     if (!in_bb(t1) || !in_bb(t2))
       
   618       continue;
       
   619     if (stmts_can_pack(t1, t2, align)) {
       
   620       if (est_savings(t1, t2) >= 0) {
       
   621         Node_List* pair = new Node_List();
       
   622         pair->push(t1);
       
   623         pair->push(t2);
       
   624         _packset.append(pair);
       
   625         set_alignment(t1, t2, align);
       
   626         changed = true;
       
   627       }
       
   628     }
       
   629   }
       
   630   return changed;
       
   631 }
       
   632 
       
   633 //------------------------------follow_def_uses---------------------------
       
   634 // Extend the packset by visiting uses of nodes in pack p
       
   635 bool SuperWord::follow_def_uses(Node_List* p) {
       
   636   bool changed = false;
       
   637   Node* s1 = p->at(0);
       
   638   Node* s2 = p->at(1);
       
   639   assert(p->size() == 2, "just checking");
       
   640   assert(s1->req() == s2->req(), "just checking");
       
   641   assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
       
   642 
       
   643   if (s1->is_Store()) return false;
       
   644 
       
   645   int align = alignment(s1);
       
   646   int savings = -1;
       
   647   Node* u1 = NULL;
       
   648   Node* u2 = NULL;
       
   649   for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
       
   650     Node* t1 = s1->fast_out(i);
       
   651     if (!in_bb(t1)) continue;
       
   652     for (DUIterator_Fast jmax, j = s2->fast_outs(jmax); j < jmax; j++) {
       
   653       Node* t2 = s2->fast_out(j);
       
   654       if (!in_bb(t2)) continue;
       
   655       if (!opnd_positions_match(s1, t1, s2, t2))
       
   656         continue;
       
   657       if (stmts_can_pack(t1, t2, align)) {
       
   658         int my_savings = est_savings(t1, t2);
       
   659         if (my_savings > savings) {
       
   660           savings = my_savings;
       
   661           u1 = t1;
       
   662           u2 = t2;
       
   663         }
       
   664       }
       
   665     }
       
   666   }
       
   667   if (savings >= 0) {
       
   668     Node_List* pair = new Node_List();
       
   669     pair->push(u1);
       
   670     pair->push(u2);
       
   671     _packset.append(pair);
       
   672     set_alignment(u1, u2, align);
       
   673     changed = true;
       
   674   }
       
   675   return changed;
       
   676 }
       
   677 
       
   678 //---------------------------opnd_positions_match-------------------------
       
   679 // Is the use of d1 in u1 at the same operand position as d2 in u2?
       
   680 bool SuperWord::opnd_positions_match(Node* d1, Node* u1, Node* d2, Node* u2) {
       
   681   uint ct = u1->req();
       
   682   if (ct != u2->req()) return false;
       
   683   uint i1 = 0;
       
   684   uint i2 = 0;
       
   685   do {
       
   686     for (i1++; i1 < ct; i1++) if (u1->in(i1) == d1) break;
       
   687     for (i2++; i2 < ct; i2++) if (u2->in(i2) == d2) break;
       
   688     if (i1 != i2) {
       
   689       return false;
       
   690     }
       
   691   } while (i1 < ct);
       
   692   return true;
       
   693 }
       
   694 
       
   695 //------------------------------est_savings---------------------------
       
   696 // Estimate the savings from executing s1 and s2 as a pack
       
   697 int SuperWord::est_savings(Node* s1, Node* s2) {
       
   698   int save = 2 - 1; // 2 operations per instruction in packed form
       
   699 
       
   700   // inputs
       
   701   for (uint i = 1; i < s1->req(); i++) {
       
   702     Node* x1 = s1->in(i);
       
   703     Node* x2 = s2->in(i);
       
   704     if (x1 != x2) {
       
   705       if (are_adjacent_refs(x1, x2)) {
       
   706         save += adjacent_profit(x1, x2);
       
   707       } else if (!in_packset(x1, x2)) {
       
   708         save -= pack_cost(2);
       
   709       } else {
       
   710         save += unpack_cost(2);
       
   711       }
       
   712     }
       
   713   }
       
   714 
       
   715   // uses of result
       
   716   uint ct = 0;
       
   717   for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
       
   718     Node* s1_use = s1->fast_out(i);
       
   719     for (int j = 0; j < _packset.length(); j++) {
       
   720       Node_List* p = _packset.at(j);
       
   721       if (p->at(0) == s1_use) {
       
   722         for (DUIterator_Fast kmax, k = s2->fast_outs(kmax); k < kmax; k++) {
       
   723           Node* s2_use = s2->fast_out(k);
       
   724           if (p->at(p->size()-1) == s2_use) {
       
   725             ct++;
       
   726             if (are_adjacent_refs(s1_use, s2_use)) {
       
   727               save += adjacent_profit(s1_use, s2_use);
       
   728             }
       
   729           }
       
   730         }
       
   731       }
       
   732     }
       
   733   }
       
   734 
       
   735   if (ct < s1->outcnt()) save += unpack_cost(1);
       
   736   if (ct < s2->outcnt()) save += unpack_cost(1);
       
   737 
       
   738   return save;
       
   739 }
       
   740 
       
   741 //------------------------------costs---------------------------
       
   742 int SuperWord::adjacent_profit(Node* s1, Node* s2) { return 2; }
       
   743 int SuperWord::pack_cost(int ct)   { return ct; }
       
   744 int SuperWord::unpack_cost(int ct) { return ct; }
       
   745 
       
   746 //------------------------------combine_packs---------------------------
       
   747 // Combine packs A and B with A.last == B.first into A.first..,A.last,B.second,..B.last
       
   748 void SuperWord::combine_packs() {
       
   749   bool changed;
       
   750   do {
       
   751     changed = false;
       
   752     for (int i = 0; i < _packset.length(); i++) {
       
   753       Node_List* p1 = _packset.at(i);
       
   754       if (p1 == NULL) continue;
       
   755       for (int j = 0; j < _packset.length(); j++) {
       
   756         Node_List* p2 = _packset.at(j);
       
   757         if (p2 == NULL) continue;
       
   758         if (p1->at(p1->size()-1) == p2->at(0)) {
       
   759           for (uint k = 1; k < p2->size(); k++) {
       
   760             p1->push(p2->at(k));
       
   761           }
       
   762           _packset.at_put(j, NULL);
       
   763           changed = true;
       
   764         }
       
   765       }
       
   766     }
       
   767   } while (changed);
       
   768 
       
   769   for (int i = _packset.length() - 1; i >= 0; i--) {
       
   770     Node_List* p1 = _packset.at(i);
       
   771     if (p1 == NULL) {
       
   772       _packset.remove_at(i);
       
   773     }
       
   774   }
       
   775 
       
   776 #ifndef PRODUCT
       
   777   if (TraceSuperWord) {
       
   778     tty->print_cr("\nAfter combine_packs");
       
   779     print_packset();
       
   780   }
       
   781 #endif
       
   782 }
       
   783 
       
   784 //-----------------------------construct_my_pack_map--------------------------
       
   785 // Construct the map from nodes to packs.  Only valid after the
       
   786 // point where a node is only in one pack (after combine_packs).
       
   787 void SuperWord::construct_my_pack_map() {
       
   788   Node_List* rslt = NULL;
       
   789   for (int i = 0; i < _packset.length(); i++) {
       
   790     Node_List* p = _packset.at(i);
       
   791     for (uint j = 0; j < p->size(); j++) {
       
   792       Node* s = p->at(j);
       
   793       assert(my_pack(s) == NULL, "only in one pack");
       
   794       set_my_pack(s, p);
       
   795     }
       
   796   }
       
   797 }
       
   798 
       
   799 //------------------------------filter_packs---------------------------
       
   800 // Remove packs that are not implemented or not profitable.
       
   801 void SuperWord::filter_packs() {
       
   802 
       
   803   // Remove packs that are not implemented
       
   804   for (int i = _packset.length() - 1; i >= 0; i--) {
       
   805     Node_List* pk = _packset.at(i);
       
   806     bool impl = implemented(pk);
       
   807     if (!impl) {
       
   808 #ifndef PRODUCT
       
   809       if (TraceSuperWord && Verbose) {
       
   810         tty->print_cr("Unimplemented");
       
   811         pk->at(0)->dump();
       
   812       }
       
   813 #endif
       
   814       remove_pack_at(i);
       
   815     }
       
   816   }
       
   817 
       
   818   // Remove packs that are not profitable
       
   819   bool changed;
       
   820   do {
       
   821     changed = false;
       
   822     for (int i = _packset.length() - 1; i >= 0; i--) {
       
   823       Node_List* pk = _packset.at(i);
       
   824       bool prof = profitable(pk);
       
   825       if (!prof) {
       
   826 #ifndef PRODUCT
       
   827         if (TraceSuperWord && Verbose) {
       
   828           tty->print_cr("Unprofitable");
       
   829           pk->at(0)->dump();
       
   830         }
       
   831 #endif
       
   832         remove_pack_at(i);
       
   833         changed = true;
       
   834       }
       
   835     }
       
   836   } while (changed);
       
   837 
       
   838 #ifndef PRODUCT
       
   839   if (TraceSuperWord) {
       
   840     tty->print_cr("\nAfter filter_packs");
       
   841     print_packset();
       
   842     tty->cr();
       
   843   }
       
   844 #endif
       
   845 }
       
   846 
       
   847 //------------------------------implemented---------------------------
       
   848 // Can code be generated for pack p?
       
   849 bool SuperWord::implemented(Node_List* p) {
       
   850   Node* p0 = p->at(0);
       
   851   int vopc = VectorNode::opcode(p0->Opcode(), p->size(), velt_type(p0));
       
   852   return vopc > 0 && Matcher::has_match_rule(vopc);
       
   853 }
       
   854 
       
   855 //------------------------------profitable---------------------------
       
   856 // For pack p, are all operands and all uses (with in the block) vector?
       
   857 bool SuperWord::profitable(Node_List* p) {
       
   858   Node* p0 = p->at(0);
       
   859   uint start, end;
       
   860   vector_opd_range(p0, &start, &end);
       
   861 
       
   862   // Return false if some input is not vector and inside block
       
   863   for (uint i = start; i < end; i++) {
       
   864     if (!is_vector_use(p0, i)) {
       
   865       // For now, return false if not scalar promotion case (inputs are the same.)
       
   866       // Later, implement PackNode and allow differring, non-vector inputs
       
   867       // (maybe just the ones from outside the block.)
       
   868       Node* p0_def = p0->in(i);
       
   869       for (uint j = 1; j < p->size(); j++) {
       
   870         Node* use = p->at(j);
       
   871         Node* def = use->in(i);
       
   872         if (p0_def != def)
       
   873           return false;
       
   874       }
       
   875     }
       
   876   }
       
   877   if (!p0->is_Store()) {
       
   878     // For now, return false if not all uses are vector.
       
   879     // Later, implement ExtractNode and allow non-vector uses (maybe
       
   880     // just the ones outside the block.)
       
   881     for (uint i = 0; i < p->size(); i++) {
       
   882       Node* def = p->at(i);
       
   883       for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
       
   884         Node* use = def->fast_out(j);
       
   885         for (uint k = 0; k < use->req(); k++) {
       
   886           Node* n = use->in(k);
       
   887           if (def == n) {
       
   888             if (!is_vector_use(use, k)) {
       
   889               return false;
       
   890             }
       
   891           }
       
   892         }
       
   893       }
       
   894     }
       
   895   }
       
   896   return true;
       
   897 }
       
   898 
       
   899 //------------------------------schedule---------------------------
       
   900 // Adjust the memory graph for the packed operations
       
   901 void SuperWord::schedule() {
       
   902 
       
   903   // Co-locate in the memory graph the members of each memory pack
       
   904   for (int i = 0; i < _packset.length(); i++) {
       
   905     co_locate_pack(_packset.at(i));
       
   906   }
       
   907 }
       
   908 
       
   909 //------------------------------co_locate_pack---------------------------
       
   910 // Within a pack, move stores down to the last executed store,
       
   911 // and move loads up to the first executed load.
       
   912 void SuperWord::co_locate_pack(Node_List* pk) {
       
   913   if (pk->at(0)->is_Store()) {
       
   914     // Push Stores down towards last executed pack member
       
   915     MemNode* first     = executed_first(pk)->as_Mem();
       
   916     MemNode* last      = executed_last(pk)->as_Mem();
       
   917     MemNode* insert_pt = last;
       
   918     MemNode* current   = last->in(MemNode::Memory)->as_Mem();
       
   919     while (true) {
       
   920       assert(in_bb(current), "stay in block");
       
   921       Node* my_mem = current->in(MemNode::Memory);
       
   922       if (in_pack(current, pk)) {
       
   923         // Forward users of my memory state to my input memory state
       
   924         _igvn.hash_delete(current);
       
   925         _igvn.hash_delete(my_mem);
       
   926         for (DUIterator i = current->outs(); current->has_out(i); i++) {
       
   927           Node* use = current->out(i);
       
   928           if (use->is_Mem()) {
       
   929             assert(use->in(MemNode::Memory) == current, "must be");
       
   930             _igvn.hash_delete(use);
       
   931             use->set_req(MemNode::Memory, my_mem);
       
   932             _igvn._worklist.push(use);
       
   933             --i; // deleted this edge; rescan position
       
   934           }
       
   935         }
       
   936         // put current immediately before insert_pt
       
   937         current->set_req(MemNode::Memory, insert_pt->in(MemNode::Memory));
       
   938         _igvn.hash_delete(insert_pt);
       
   939         insert_pt->set_req(MemNode::Memory, current);
       
   940         _igvn._worklist.push(insert_pt);
       
   941         _igvn._worklist.push(current);
       
   942         insert_pt = current;
       
   943       }
       
   944       if (current == first) break;
       
   945       current = my_mem->as_Mem();
       
   946     }
       
   947   } else if (pk->at(0)->is_Load()) {
       
   948     // Pull Loads up towards first executed pack member
       
   949     LoadNode* first = executed_first(pk)->as_Load();
       
   950     Node* first_mem = first->in(MemNode::Memory);
       
   951     _igvn.hash_delete(first_mem);
       
   952     // Give each load same memory state as first
       
   953     for (uint i = 0; i < pk->size(); i++) {
       
   954       LoadNode* ld = pk->at(i)->as_Load();
       
   955       _igvn.hash_delete(ld);
       
   956       ld->set_req(MemNode::Memory, first_mem);
       
   957       _igvn._worklist.push(ld);
       
   958     }
       
   959   }
       
   960 }
       
   961 
       
   962 //------------------------------output---------------------------
       
   963 // Convert packs into vector node operations
       
   964 void SuperWord::output() {
       
   965   if (_packset.length() == 0) return;
       
   966 
       
   967   // MUST ENSURE main loop's initial value is properly aligned:
       
   968   //  (iv_initial_value + min_iv_offset) % vector_width_in_bytes() == 0
       
   969 
       
   970   align_initial_loop_index(align_to_ref());
       
   971 
       
   972   // Insert extract (unpack) operations for scalar uses
       
   973   for (int i = 0; i < _packset.length(); i++) {
       
   974     insert_extracts(_packset.at(i));
       
   975   }
       
   976 
       
   977   for (int i = 0; i < _block.length(); i++) {
       
   978     Node* n = _block.at(i);
       
   979     Node_List* p = my_pack(n);
       
   980     if (p && n == executed_last(p)) {
       
   981       uint vlen = p->size();
       
   982       Node* vn = NULL;
       
   983       Node* low_adr = p->at(0);
       
   984       Node* first   = executed_first(p);
       
   985       if (n->is_Load()) {
       
   986         int   opc = n->Opcode();
       
   987         Node* ctl = n->in(MemNode::Control);
       
   988         Node* mem = first->in(MemNode::Memory);
       
   989         Node* adr = low_adr->in(MemNode::Address);
       
   990         const TypePtr* atyp = n->adr_type();
       
   991         vn = VectorLoadNode::make(_phase->C, opc, ctl, mem, adr, atyp, vlen);
       
   992 
       
   993       } else if (n->is_Store()) {
       
   994         // Promote value to be stored to vector
       
   995         VectorNode* val = vector_opd(p, MemNode::ValueIn);
       
   996 
       
   997         int   opc = n->Opcode();
       
   998         Node* ctl = n->in(MemNode::Control);
       
   999         Node* mem = first->in(MemNode::Memory);
       
  1000         Node* adr = low_adr->in(MemNode::Address);
       
  1001         const TypePtr* atyp = n->adr_type();
       
  1002         vn = VectorStoreNode::make(_phase->C, opc, ctl, mem, adr, atyp, val, vlen);
       
  1003 
       
  1004       } else if (n->req() == 3) {
       
  1005         // Promote operands to vector
       
  1006         Node* in1 = vector_opd(p, 1);
       
  1007         Node* in2 = vector_opd(p, 2);
       
  1008         vn = VectorNode::make(_phase->C, n->Opcode(), in1, in2, vlen, velt_type(n));
       
  1009 
       
  1010       } else {
       
  1011         ShouldNotReachHere();
       
  1012       }
       
  1013 
       
  1014       _phase->_igvn.register_new_node_with_optimizer(vn);
       
  1015       _phase->set_ctrl(vn, _phase->get_ctrl(p->at(0)));
       
  1016       for (uint j = 0; j < p->size(); j++) {
       
  1017         Node* pm = p->at(j);
       
  1018         _igvn.hash_delete(pm);
       
  1019         _igvn.subsume_node(pm, vn);
       
  1020       }
       
  1021       _igvn._worklist.push(vn);
       
  1022     }
       
  1023   }
       
  1024 }
       
  1025 
       
  1026 //------------------------------vector_opd---------------------------
       
  1027 // Create a vector operand for the nodes in pack p for operand: in(opd_idx)
       
  1028 VectorNode* SuperWord::vector_opd(Node_List* p, int opd_idx) {
       
  1029   Node* p0 = p->at(0);
       
  1030   uint vlen = p->size();
       
  1031   Node* opd = p0->in(opd_idx);
       
  1032 
       
  1033   bool same_opd = true;
       
  1034   for (uint i = 1; i < vlen; i++) {
       
  1035     Node* pi = p->at(i);
       
  1036     Node* in = pi->in(opd_idx);
       
  1037     if (opd != in) {
       
  1038       same_opd = false;
       
  1039       break;
       
  1040     }
       
  1041   }
       
  1042 
       
  1043   if (same_opd) {
       
  1044     if (opd->is_Vector()) {
       
  1045       return (VectorNode*)opd; // input is matching vector
       
  1046     }
       
  1047     // Convert scalar input to vector. Use p0's type because it's container
       
  1048     // maybe smaller than the operand's container.
       
  1049     const Type* opd_t = velt_type(!in_bb(opd) ? p0 : opd);
       
  1050     const Type* p0_t  = velt_type(p0);
       
  1051     if (p0_t->higher_equal(opd_t)) opd_t = p0_t;
       
  1052     VectorNode* vn    = VectorNode::scalar2vector(_phase->C, opd, vlen, opd_t);
       
  1053 
       
  1054     _phase->_igvn.register_new_node_with_optimizer(vn);
       
  1055     _phase->set_ctrl(vn, _phase->get_ctrl(opd));
       
  1056     return vn;
       
  1057   }
       
  1058 
       
  1059   // Insert pack operation
       
  1060   const Type* opd_t = velt_type(!in_bb(opd) ? p0 : opd);
       
  1061   PackNode* pk = PackNode::make(_phase->C, opd, opd_t);
       
  1062 
       
  1063   for (uint i = 1; i < vlen; i++) {
       
  1064     Node* pi = p->at(i);
       
  1065     Node* in = pi->in(opd_idx);
       
  1066     assert(my_pack(in) == NULL, "Should already have been unpacked");
       
  1067     assert(opd_t == velt_type(!in_bb(in) ? pi : in), "all same type");
       
  1068     pk->add_opd(in);
       
  1069   }
       
  1070   _phase->_igvn.register_new_node_with_optimizer(pk);
       
  1071   _phase->set_ctrl(pk, _phase->get_ctrl(opd));
       
  1072   return pk;
       
  1073 }
       
  1074 
       
  1075 //------------------------------insert_extracts---------------------------
       
  1076 // If a use of pack p is not a vector use, then replace the
       
  1077 // use with an extract operation.
       
  1078 void SuperWord::insert_extracts(Node_List* p) {
       
  1079   if (p->at(0)->is_Store()) return;
       
  1080   assert(_n_idx_list.is_empty(), "empty (node,index) list");
       
  1081 
       
  1082   // Inspect each use of each pack member.  For each use that is
       
  1083   // not a vector use, replace the use with an extract operation.
       
  1084 
       
  1085   for (uint i = 0; i < p->size(); i++) {
       
  1086     Node* def = p->at(i);
       
  1087     for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
       
  1088       Node* use = def->fast_out(j);
       
  1089       for (uint k = 0; k < use->req(); k++) {
       
  1090         Node* n = use->in(k);
       
  1091         if (def == n) {
       
  1092           if (!is_vector_use(use, k)) {
       
  1093             _n_idx_list.push(use, k);
       
  1094           }
       
  1095         }
       
  1096       }
       
  1097     }
       
  1098   }
       
  1099 
       
  1100   while (_n_idx_list.is_nonempty()) {
       
  1101     Node* use = _n_idx_list.node();
       
  1102     int   idx = _n_idx_list.index();
       
  1103     _n_idx_list.pop();
       
  1104     Node* def = use->in(idx);
       
  1105 
       
  1106     // Insert extract operation
       
  1107     _igvn.hash_delete(def);
       
  1108     _igvn.hash_delete(use);
       
  1109     int def_pos = alignment(def) / data_size(def);
       
  1110     const Type* def_t = velt_type(def);
       
  1111 
       
  1112     Node* ex = ExtractNode::make(_phase->C, def, def_pos, def_t);
       
  1113     _phase->_igvn.register_new_node_with_optimizer(ex);
       
  1114     _phase->set_ctrl(ex, _phase->get_ctrl(def));
       
  1115     use->set_req(idx, ex);
       
  1116     _igvn._worklist.push(def);
       
  1117     _igvn._worklist.push(use);
       
  1118 
       
  1119     bb_insert_after(ex, bb_idx(def));
       
  1120     set_velt_type(ex, def_t);
       
  1121   }
       
  1122 }
       
  1123 
       
  1124 //------------------------------is_vector_use---------------------------
       
  1125 // Is use->in(u_idx) a vector use?
       
  1126 bool SuperWord::is_vector_use(Node* use, int u_idx) {
       
  1127   Node_List* u_pk = my_pack(use);
       
  1128   if (u_pk == NULL) return false;
       
  1129   Node* def = use->in(u_idx);
       
  1130   Node_List* d_pk = my_pack(def);
       
  1131   if (d_pk == NULL) {
       
  1132     // check for scalar promotion
       
  1133     Node* n = u_pk->at(0)->in(u_idx);
       
  1134     for (uint i = 1; i < u_pk->size(); i++) {
       
  1135       if (u_pk->at(i)->in(u_idx) != n) return false;
       
  1136     }
       
  1137     return true;
       
  1138   }
       
  1139   if (u_pk->size() != d_pk->size())
       
  1140     return false;
       
  1141   for (uint i = 0; i < u_pk->size(); i++) {
       
  1142     Node* ui = u_pk->at(i);
       
  1143     Node* di = d_pk->at(i);
       
  1144     if (ui->in(u_idx) != di || alignment(ui) != alignment(di))
       
  1145       return false;
       
  1146   }
       
  1147   return true;
       
  1148 }
       
  1149 
       
  1150 //------------------------------construct_bb---------------------------
       
  1151 // Construct reverse postorder list of block members
       
  1152 void SuperWord::construct_bb() {
       
  1153   Node* entry = bb();
       
  1154 
       
  1155   assert(_stk.length() == 0,            "stk is empty");
       
  1156   assert(_block.length() == 0,          "block is empty");
       
  1157   assert(_data_entry.length() == 0,     "data_entry is empty");
       
  1158   assert(_mem_slice_head.length() == 0, "mem_slice_head is empty");
       
  1159   assert(_mem_slice_tail.length() == 0, "mem_slice_tail is empty");
       
  1160 
       
  1161   // Find non-control nodes with no inputs from within block,
       
  1162   // create a temporary map from node _idx to bb_idx for use
       
  1163   // by the visited and post_visited sets,
       
  1164   // and count number of nodes in block.
       
  1165   int bb_ct = 0;
       
  1166   for (uint i = 0; i < lpt()->_body.size(); i++ ) {
       
  1167     Node *n = lpt()->_body.at(i);
       
  1168     set_bb_idx(n, i); // Create a temporary map
       
  1169     if (in_bb(n)) {
       
  1170       bb_ct++;
       
  1171       if (!n->is_CFG()) {
       
  1172         bool found = false;
       
  1173         for (uint j = 0; j < n->req(); j++) {
       
  1174           Node* def = n->in(j);
       
  1175           if (def && in_bb(def)) {
       
  1176             found = true;
       
  1177             break;
       
  1178           }
       
  1179         }
       
  1180         if (!found) {
       
  1181           assert(n != entry, "can't be entry");
       
  1182           _data_entry.push(n);
       
  1183         }
       
  1184       }
       
  1185     }
       
  1186   }
       
  1187 
       
  1188   // Find memory slices (head and tail)
       
  1189   for (DUIterator_Fast imax, i = lp()->fast_outs(imax); i < imax; i++) {
       
  1190     Node *n = lp()->fast_out(i);
       
  1191     if (in_bb(n) && (n->is_Phi() && n->bottom_type() == Type::MEMORY)) {
       
  1192       Node* n_tail  = n->in(LoopNode::LoopBackControl);
       
  1193       _mem_slice_head.push(n);
       
  1194       _mem_slice_tail.push(n_tail);
       
  1195     }
       
  1196   }
       
  1197 
       
  1198   // Create an RPO list of nodes in block
       
  1199 
       
  1200   visited_clear();
       
  1201   post_visited_clear();
       
  1202 
       
  1203   // Push all non-control nodes with no inputs from within block, then control entry
       
  1204   for (int j = 0; j < _data_entry.length(); j++) {
       
  1205     Node* n = _data_entry.at(j);
       
  1206     visited_set(n);
       
  1207     _stk.push(n);
       
  1208   }
       
  1209   visited_set(entry);
       
  1210   _stk.push(entry);
       
  1211 
       
  1212   // Do a depth first walk over out edges
       
  1213   int rpo_idx = bb_ct - 1;
       
  1214   int size;
       
  1215   while ((size = _stk.length()) > 0) {
       
  1216     Node* n = _stk.top(); // Leave node on stack
       
  1217     if (!visited_test_set(n)) {
       
  1218       // forward arc in graph
       
  1219     } else if (!post_visited_test(n)) {
       
  1220       // cross or back arc
       
  1221       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
       
  1222         Node *use = n->fast_out(i);
       
  1223         if (in_bb(use) && !visited_test(use) &&
       
  1224             // Don't go around backedge
       
  1225             (!use->is_Phi() || n == entry)) {
       
  1226           _stk.push(use);
       
  1227         }
       
  1228       }
       
  1229       if (_stk.length() == size) {
       
  1230         // There were no additional uses, post visit node now
       
  1231         _stk.pop(); // Remove node from stack
       
  1232         assert(rpo_idx >= 0, "");
       
  1233         _block.at_put_grow(rpo_idx, n);
       
  1234         rpo_idx--;
       
  1235         post_visited_set(n);
       
  1236         assert(rpo_idx >= 0 || _stk.is_empty(), "");
       
  1237       }
       
  1238     } else {
       
  1239       _stk.pop(); // Remove post-visited node from stack
       
  1240     }
       
  1241   }
       
  1242 
       
  1243   // Create real map of block indices for nodes
       
  1244   for (int j = 0; j < _block.length(); j++) {
       
  1245     Node* n = _block.at(j);
       
  1246     set_bb_idx(n, j);
       
  1247   }
       
  1248 
       
  1249   initialize_bb(); // Ensure extra info is allocated.
       
  1250 
       
  1251 #ifndef PRODUCT
       
  1252   if (TraceSuperWord) {
       
  1253     print_bb();
       
  1254     tty->print_cr("\ndata entry nodes: %s", _data_entry.length() > 0 ? "" : "NONE");
       
  1255     for (int m = 0; m < _data_entry.length(); m++) {
       
  1256       tty->print("%3d ", m);
       
  1257       _data_entry.at(m)->dump();
       
  1258     }
       
  1259     tty->print_cr("\nmemory slices: %s", _mem_slice_head.length() > 0 ? "" : "NONE");
       
  1260     for (int m = 0; m < _mem_slice_head.length(); m++) {
       
  1261       tty->print("%3d ", m); _mem_slice_head.at(m)->dump();
       
  1262       tty->print("    ");    _mem_slice_tail.at(m)->dump();
       
  1263     }
       
  1264   }
       
  1265 #endif
       
  1266   assert(rpo_idx == -1 && bb_ct == _block.length(), "all block members found");
       
  1267 }
       
  1268 
       
  1269 //------------------------------initialize_bb---------------------------
       
  1270 // Initialize per node info
       
  1271 void SuperWord::initialize_bb() {
       
  1272   Node* last = _block.at(_block.length() - 1);
       
  1273   grow_node_info(bb_idx(last));
       
  1274 }
       
  1275 
       
  1276 //------------------------------bb_insert_after---------------------------
       
  1277 // Insert n into block after pos
       
  1278 void SuperWord::bb_insert_after(Node* n, int pos) {
       
  1279   int n_pos = pos + 1;
       
  1280   // Make room
       
  1281   for (int i = _block.length() - 1; i >= n_pos; i--) {
       
  1282     _block.at_put_grow(i+1, _block.at(i));
       
  1283   }
       
  1284   for (int j = _node_info.length() - 1; j >= n_pos; j--) {
       
  1285     _node_info.at_put_grow(j+1, _node_info.at(j));
       
  1286   }
       
  1287   // Set value
       
  1288   _block.at_put_grow(n_pos, n);
       
  1289   _node_info.at_put_grow(n_pos, SWNodeInfo::initial);
       
  1290   // Adjust map from node->_idx to _block index
       
  1291   for (int i = n_pos; i < _block.length(); i++) {
       
  1292     set_bb_idx(_block.at(i), i);
       
  1293   }
       
  1294 }
       
  1295 
       
  1296 //------------------------------compute_max_depth---------------------------
       
  1297 // Compute max depth for expressions from beginning of block
       
  1298 // Use to prune search paths during test for independence.
       
  1299 void SuperWord::compute_max_depth() {
       
  1300   int ct = 0;
       
  1301   bool again;
       
  1302   do {
       
  1303     again = false;
       
  1304     for (int i = 0; i < _block.length(); i++) {
       
  1305       Node* n = _block.at(i);
       
  1306       if (!n->is_Phi()) {
       
  1307         int d_orig = depth(n);
       
  1308         int d_in   = 0;
       
  1309         for (DepPreds preds(n, _dg); !preds.done(); preds.next()) {
       
  1310           Node* pred = preds.current();
       
  1311           if (in_bb(pred)) {
       
  1312             d_in = MAX2(d_in, depth(pred));
       
  1313           }
       
  1314         }
       
  1315         if (d_in + 1 != d_orig) {
       
  1316           set_depth(n, d_in + 1);
       
  1317           again = true;
       
  1318         }
       
  1319       }
       
  1320     }
       
  1321     ct++;
       
  1322   } while (again);
       
  1323 #ifndef PRODUCT
       
  1324   if (TraceSuperWord && Verbose)
       
  1325     tty->print_cr("compute_max_depth iterated: %d times", ct);
       
  1326 #endif
       
  1327 }
       
  1328 
       
  1329 //-------------------------compute_vector_element_type-----------------------
       
  1330 // Compute necessary vector element type for expressions
       
  1331 // This propagates backwards a narrower integer type when the
       
  1332 // upper bits of the value are not needed.
       
  1333 // Example:  char a,b,c;  a = b + c;
       
  1334 // Normally the type of the add is integer, but for packed character
       
  1335 // operations the type of the add needs to be char.
       
  1336 void SuperWord::compute_vector_element_type() {
       
  1337 #ifndef PRODUCT
       
  1338   if (TraceSuperWord && Verbose)
       
  1339     tty->print_cr("\ncompute_velt_type:");
       
  1340 #endif
       
  1341 
       
  1342   // Initial type
       
  1343   for (int i = 0; i < _block.length(); i++) {
       
  1344     Node* n = _block.at(i);
       
  1345     const Type* t  = n->is_Mem() ? Type::get_const_basic_type(n->as_Mem()->memory_type())
       
  1346                                  : _igvn.type(n);
       
  1347     const Type* vt = container_type(t);
       
  1348     set_velt_type(n, vt);
       
  1349   }
       
  1350 
       
  1351   // Propagate narrowed type backwards through operations
       
  1352   // that don't depend on higher order bits
       
  1353   for (int i = _block.length() - 1; i >= 0; i--) {
       
  1354     Node* n = _block.at(i);
       
  1355     // Only integer types need be examined
       
  1356     if (n->bottom_type()->isa_int()) {
       
  1357       uint start, end;
       
  1358       vector_opd_range(n, &start, &end);
       
  1359       const Type* vt = velt_type(n);
       
  1360 
       
  1361       for (uint j = start; j < end; j++) {
       
  1362         Node* in  = n->in(j);
       
  1363         // Don't propagate through a type conversion
       
  1364         if (n->bottom_type() != in->bottom_type())
       
  1365           continue;
       
  1366         switch(in->Opcode()) {
       
  1367         case Op_AddI:    case Op_AddL:
       
  1368         case Op_SubI:    case Op_SubL:
       
  1369         case Op_MulI:    case Op_MulL:
       
  1370         case Op_AndI:    case Op_AndL:
       
  1371         case Op_OrI:     case Op_OrL:
       
  1372         case Op_XorI:    case Op_XorL:
       
  1373         case Op_LShiftI: case Op_LShiftL:
       
  1374         case Op_CMoveI:  case Op_CMoveL:
       
  1375           if (in_bb(in)) {
       
  1376             bool same_type = true;
       
  1377             for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) {
       
  1378               Node *use = in->fast_out(k);
       
  1379               if (!in_bb(use) || velt_type(use) != vt) {
       
  1380                 same_type = false;
       
  1381                 break;
       
  1382               }
       
  1383             }
       
  1384             if (same_type) {
       
  1385               set_velt_type(in, vt);
       
  1386             }
       
  1387           }
       
  1388         }
       
  1389       }
       
  1390     }
       
  1391   }
       
  1392 #ifndef PRODUCT
       
  1393   if (TraceSuperWord && Verbose) {
       
  1394     for (int i = 0; i < _block.length(); i++) {
       
  1395       Node* n = _block.at(i);
       
  1396       velt_type(n)->dump();
       
  1397       tty->print("\t");
       
  1398       n->dump();
       
  1399     }
       
  1400   }
       
  1401 #endif
       
  1402 }
       
  1403 
       
  1404 //------------------------------memory_alignment---------------------------
       
  1405 // Alignment within a vector memory reference
       
  1406 int SuperWord::memory_alignment(MemNode* s, int iv_adjust_in_bytes) {
       
  1407   SWPointer p(s, this);
       
  1408   if (!p.valid()) {
       
  1409     return bottom_align;
       
  1410   }
       
  1411   int offset  = p.offset_in_bytes();
       
  1412   offset     += iv_adjust_in_bytes;
       
  1413   int off_rem = offset % vector_width_in_bytes();
       
  1414   int off_mod = off_rem >= 0 ? off_rem : off_rem + vector_width_in_bytes();
       
  1415   return off_mod;
       
  1416 }
       
  1417 
       
  1418 //---------------------------container_type---------------------------
       
  1419 // Smallest type containing range of values
       
  1420 const Type* SuperWord::container_type(const Type* t) {
       
  1421   if (t->isa_aryptr()) {
       
  1422     t = t->is_aryptr()->elem();
       
  1423   }
       
  1424   if (t->basic_type() == T_INT) {
       
  1425     if (t->higher_equal(TypeInt::BOOL))  return TypeInt::BOOL;
       
  1426     if (t->higher_equal(TypeInt::BYTE))  return TypeInt::BYTE;
       
  1427     if (t->higher_equal(TypeInt::CHAR))  return TypeInt::CHAR;
       
  1428     if (t->higher_equal(TypeInt::SHORT)) return TypeInt::SHORT;
       
  1429     return TypeInt::INT;
       
  1430   }
       
  1431   return t;
       
  1432 }
       
  1433 
       
  1434 //-------------------------vector_opd_range-----------------------
       
  1435 // (Start, end] half-open range defining which operands are vector
       
  1436 void SuperWord::vector_opd_range(Node* n, uint* start, uint* end) {
       
  1437   switch (n->Opcode()) {
       
  1438   case Op_LoadB:   case Op_LoadC:
       
  1439   case Op_LoadI:   case Op_LoadL:
       
  1440   case Op_LoadF:   case Op_LoadD:
       
  1441   case Op_LoadP:
       
  1442     *start = 0;
       
  1443     *end   = 0;
       
  1444     return;
       
  1445   case Op_StoreB:  case Op_StoreC:
       
  1446   case Op_StoreI:  case Op_StoreL:
       
  1447   case Op_StoreF:  case Op_StoreD:
       
  1448   case Op_StoreP:
       
  1449     *start = MemNode::ValueIn;
       
  1450     *end   = *start + 1;
       
  1451     return;
       
  1452   case Op_LShiftI: case Op_LShiftL:
       
  1453     *start = 1;
       
  1454     *end   = 2;
       
  1455     return;
       
  1456   case Op_CMoveI:  case Op_CMoveL:  case Op_CMoveF:  case Op_CMoveD:
       
  1457     *start = 2;
       
  1458     *end   = n->req();
       
  1459     return;
       
  1460   }
       
  1461   *start = 1;
       
  1462   *end   = n->req(); // default is all operands
       
  1463 }
       
  1464 
       
  1465 //------------------------------in_packset---------------------------
       
  1466 // Are s1 and s2 in a pack pair and ordered as s1,s2?
       
  1467 bool SuperWord::in_packset(Node* s1, Node* s2) {
       
  1468   for (int i = 0; i < _packset.length(); i++) {
       
  1469     Node_List* p = _packset.at(i);
       
  1470     assert(p->size() == 2, "must be");
       
  1471     if (p->at(0) == s1 && p->at(p->size()-1) == s2) {
       
  1472       return true;
       
  1473     }
       
  1474   }
       
  1475   return false;
       
  1476 }
       
  1477 
       
  1478 //------------------------------in_pack---------------------------
       
  1479 // Is s in pack p?
       
  1480 Node_List* SuperWord::in_pack(Node* s, Node_List* p) {
       
  1481   for (uint i = 0; i < p->size(); i++) {
       
  1482     if (p->at(i) == s) {
       
  1483       return p;
       
  1484     }
       
  1485   }
       
  1486   return NULL;
       
  1487 }
       
  1488 
       
  1489 //------------------------------remove_pack_at---------------------------
       
  1490 // Remove the pack at position pos in the packset
       
  1491 void SuperWord::remove_pack_at(int pos) {
       
  1492   Node_List* p = _packset.at(pos);
       
  1493   for (uint i = 0; i < p->size(); i++) {
       
  1494     Node* s = p->at(i);
       
  1495     set_my_pack(s, NULL);
       
  1496   }
       
  1497   _packset.remove_at(pos);
       
  1498 }
       
  1499 
       
  1500 //------------------------------executed_first---------------------------
       
  1501 // Return the node executed first in pack p.  Uses the RPO block list
       
  1502 // to determine order.
       
  1503 Node* SuperWord::executed_first(Node_List* p) {
       
  1504   Node* n = p->at(0);
       
  1505   int n_rpo = bb_idx(n);
       
  1506   for (uint i = 1; i < p->size(); i++) {
       
  1507     Node* s = p->at(i);
       
  1508     int s_rpo = bb_idx(s);
       
  1509     if (s_rpo < n_rpo) {
       
  1510       n = s;
       
  1511       n_rpo = s_rpo;
       
  1512     }
       
  1513   }
       
  1514   return n;
       
  1515 }
       
  1516 
       
  1517 //------------------------------executed_last---------------------------
       
  1518 // Return the node executed last in pack p.
       
  1519 Node* SuperWord::executed_last(Node_List* p) {
       
  1520   Node* n = p->at(0);
       
  1521   int n_rpo = bb_idx(n);
       
  1522   for (uint i = 1; i < p->size(); i++) {
       
  1523     Node* s = p->at(i);
       
  1524     int s_rpo = bb_idx(s);
       
  1525     if (s_rpo > n_rpo) {
       
  1526       n = s;
       
  1527       n_rpo = s_rpo;
       
  1528     }
       
  1529   }
       
  1530   return n;
       
  1531 }
       
  1532 
       
  1533 //----------------------------align_initial_loop_index---------------------------
       
  1534 // Adjust pre-loop limit so that in main loop, a load/store reference
       
  1535 // to align_to_ref will be a position zero in the vector.
       
  1536 //   (iv + k) mod vector_align == 0
       
  1537 void SuperWord::align_initial_loop_index(MemNode* align_to_ref) {
       
  1538   CountedLoopNode *main_head = lp()->as_CountedLoop();
       
  1539   assert(main_head->is_main_loop(), "");
       
  1540   CountedLoopEndNode* pre_end = get_pre_loop_end(main_head);
       
  1541   assert(pre_end != NULL, "");
       
  1542   Node *pre_opaq1 = pre_end->limit();
       
  1543   assert(pre_opaq1->Opcode() == Op_Opaque1, "");
       
  1544   Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
       
  1545   Node *pre_limit = pre_opaq->in(1);
       
  1546 
       
  1547   // Where we put new limit calculations
       
  1548   Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
       
  1549 
       
  1550   // Ensure the original loop limit is available from the
       
  1551   // pre-loop Opaque1 node.
       
  1552   Node *orig_limit = pre_opaq->original_loop_limit();
       
  1553   assert(orig_limit != NULL && _igvn.type(orig_limit) != Type::TOP, "");
       
  1554 
       
  1555   SWPointer align_to_ref_p(align_to_ref, this);
       
  1556 
       
  1557   // Let l0 == original pre_limit, l == new pre_limit, V == v_align
       
  1558   //
       
  1559   // For stride > 0
       
  1560   //   Need l such that l > l0 && (l+k)%V == 0
       
  1561   //   Find n such that l = (l0 + n)
       
  1562   //   (l0 + n + k) % V == 0
       
  1563   //   n = [V - (l0 + k)%V]%V
       
  1564   //   new limit = l0 + [V - (l0 + k)%V]%V
       
  1565   // For stride < 0
       
  1566   //   Need l such that l < l0 && (l+k)%V == 0
       
  1567   //   Find n such that l = (l0 - n)
       
  1568   //   (l0 - n + k) % V == 0
       
  1569   //   n = (l0 + k)%V
       
  1570   //   new limit = l0 - (l0 + k)%V
       
  1571 
       
  1572   int elt_size = align_to_ref_p.memory_size();
       
  1573   int v_align  = vector_width_in_bytes() / elt_size;
       
  1574   int k        = align_to_ref_p.offset_in_bytes() / elt_size;
       
  1575 
       
  1576   Node *kn   = _igvn.intcon(k);
       
  1577   Node *limk = new (_phase->C, 3) AddINode(pre_limit, kn);
       
  1578   _phase->_igvn.register_new_node_with_optimizer(limk);
       
  1579   _phase->set_ctrl(limk, pre_ctrl);
       
  1580   if (align_to_ref_p.invar() != NULL) {
       
  1581     Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
       
  1582     Node* aref     = new (_phase->C, 3) URShiftINode(align_to_ref_p.invar(), log2_elt);
       
  1583     _phase->_igvn.register_new_node_with_optimizer(aref);
       
  1584     _phase->set_ctrl(aref, pre_ctrl);
       
  1585     if (!align_to_ref_p.negate_invar()) {
       
  1586       limk = new (_phase->C, 3) AddINode(limk, aref);
       
  1587     } else {
       
  1588       limk = new (_phase->C, 3) SubINode(limk, aref);
       
  1589     }
       
  1590     _phase->_igvn.register_new_node_with_optimizer(limk);
       
  1591     _phase->set_ctrl(limk, pre_ctrl);
       
  1592   }
       
  1593   Node* va_msk = _igvn.intcon(v_align - 1);
       
  1594   Node* n      = new (_phase->C, 3) AndINode(limk, va_msk);
       
  1595   _phase->_igvn.register_new_node_with_optimizer(n);
       
  1596   _phase->set_ctrl(n, pre_ctrl);
       
  1597   Node* newlim;
       
  1598   if (iv_stride() > 0) {
       
  1599     Node* va  = _igvn.intcon(v_align);
       
  1600     Node* adj = new (_phase->C, 3) SubINode(va, n);
       
  1601     _phase->_igvn.register_new_node_with_optimizer(adj);
       
  1602     _phase->set_ctrl(adj, pre_ctrl);
       
  1603     Node* adj2 = new (_phase->C, 3) AndINode(adj, va_msk);
       
  1604     _phase->_igvn.register_new_node_with_optimizer(adj2);
       
  1605     _phase->set_ctrl(adj2, pre_ctrl);
       
  1606     newlim = new (_phase->C, 3) AddINode(pre_limit, adj2);
       
  1607   } else {
       
  1608     newlim = new (_phase->C, 3) SubINode(pre_limit, n);
       
  1609   }
       
  1610   _phase->_igvn.register_new_node_with_optimizer(newlim);
       
  1611   _phase->set_ctrl(newlim, pre_ctrl);
       
  1612   Node* constrained =
       
  1613     (iv_stride() > 0) ? (Node*) new (_phase->C,3) MinINode(newlim, orig_limit)
       
  1614                       : (Node*) new (_phase->C,3) MaxINode(newlim, orig_limit);
       
  1615   _phase->_igvn.register_new_node_with_optimizer(constrained);
       
  1616   _phase->set_ctrl(constrained, pre_ctrl);
       
  1617   _igvn.hash_delete(pre_opaq);
       
  1618   pre_opaq->set_req(1, constrained);
       
  1619 }
       
  1620 
       
  1621 //----------------------------get_pre_loop_end---------------------------
       
  1622 // Find pre loop end from main loop.  Returns null if none.
       
  1623 CountedLoopEndNode* SuperWord::get_pre_loop_end(CountedLoopNode *cl) {
       
  1624   Node *ctrl = cl->in(LoopNode::EntryControl);
       
  1625   if (!ctrl->is_IfTrue() && !ctrl->is_IfFalse()) return NULL;
       
  1626   Node *iffm = ctrl->in(0);
       
  1627   if (!iffm->is_If()) return NULL;
       
  1628   Node *p_f = iffm->in(0);
       
  1629   if (!p_f->is_IfFalse()) return NULL;
       
  1630   if (!p_f->in(0)->is_CountedLoopEnd()) return NULL;
       
  1631   CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
       
  1632   if (!pre_end->loopnode()->is_pre_loop()) return NULL;
       
  1633   return pre_end;
       
  1634 }
       
  1635 
       
  1636 
       
  1637 //------------------------------init---------------------------
       
  1638 void SuperWord::init() {
       
  1639   _dg.init();
       
  1640   _packset.clear();
       
  1641   _disjoint_ptrs.clear();
       
  1642   _block.clear();
       
  1643   _data_entry.clear();
       
  1644   _mem_slice_head.clear();
       
  1645   _mem_slice_tail.clear();
       
  1646   _node_info.clear();
       
  1647   _align_to_ref = NULL;
       
  1648   _lpt = NULL;
       
  1649   _lp = NULL;
       
  1650   _bb = NULL;
       
  1651   _iv = NULL;
       
  1652 }
       
  1653 
       
  1654 //------------------------------print_packset---------------------------
       
  1655 void SuperWord::print_packset() {
       
  1656 #ifndef PRODUCT
       
  1657   tty->print_cr("packset");
       
  1658   for (int i = 0; i < _packset.length(); i++) {
       
  1659     tty->print_cr("Pack: %d", i);
       
  1660     Node_List* p = _packset.at(i);
       
  1661     print_pack(p);
       
  1662   }
       
  1663 #endif
       
  1664 }
       
  1665 
       
  1666 //------------------------------print_pack---------------------------
       
  1667 void SuperWord::print_pack(Node_List* p) {
       
  1668   for (uint i = 0; i < p->size(); i++) {
       
  1669     print_stmt(p->at(i));
       
  1670   }
       
  1671 }
       
  1672 
       
  1673 //------------------------------print_bb---------------------------
       
  1674 void SuperWord::print_bb() {
       
  1675 #ifndef PRODUCT
       
  1676   tty->print_cr("\nBlock");
       
  1677   for (int i = 0; i < _block.length(); i++) {
       
  1678     Node* n = _block.at(i);
       
  1679     tty->print("%d ", i);
       
  1680     if (n) {
       
  1681       n->dump();
       
  1682     }
       
  1683   }
       
  1684 #endif
       
  1685 }
       
  1686 
       
  1687 //------------------------------print_stmt---------------------------
       
  1688 void SuperWord::print_stmt(Node* s) {
       
  1689 #ifndef PRODUCT
       
  1690   tty->print(" align: %d \t", alignment(s));
       
  1691   s->dump();
       
  1692 #endif
       
  1693 }
       
  1694 
       
  1695 //------------------------------blank---------------------------
       
  1696 char* SuperWord::blank(uint depth) {
       
  1697   static char blanks[101];
       
  1698   assert(depth < 101, "too deep");
       
  1699   for (uint i = 0; i < depth; i++) blanks[i] = ' ';
       
  1700   blanks[depth] = '\0';
       
  1701   return blanks;
       
  1702 }
       
  1703 
       
  1704 
       
  1705 //==============================SWPointer===========================
       
  1706 
       
  1707 //----------------------------SWPointer------------------------
       
  1708 SWPointer::SWPointer(MemNode* mem, SuperWord* slp) :
       
  1709   _mem(mem), _slp(slp),  _base(NULL),  _adr(NULL),
       
  1710   _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {
       
  1711 
       
  1712   Node* adr = mem->in(MemNode::Address);
       
  1713   if (!adr->is_AddP()) {
       
  1714     assert(!valid(), "too complex");
       
  1715     return;
       
  1716   }
       
  1717   // Match AddP(base, AddP(ptr, k*iv [+ invariant]), constant)
       
  1718   Node* base = adr->in(AddPNode::Base);
       
  1719   for (int i = 0; i < 3; i++) {
       
  1720     if (!scaled_iv_plus_offset(adr->in(AddPNode::Offset))) {
       
  1721       assert(!valid(), "too complex");
       
  1722       return;
       
  1723     }
       
  1724     adr = adr->in(AddPNode::Address);
       
  1725     if (base == adr || !adr->is_AddP()) {
       
  1726       break; // stop looking at addp's
       
  1727     }
       
  1728   }
       
  1729   _base = base;
       
  1730   _adr  = adr;
       
  1731   assert(valid(), "Usable");
       
  1732 }
       
  1733 
       
  1734 // Following is used to create a temporary object during
       
  1735 // the pattern match of an address expression.
       
  1736 SWPointer::SWPointer(SWPointer* p) :
       
  1737   _mem(p->_mem), _slp(p->_slp),  _base(NULL),  _adr(NULL),
       
  1738   _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {}
       
  1739 
       
  1740 //------------------------scaled_iv_plus_offset--------------------
       
  1741 // Match: k*iv + offset
       
  1742 // where: k is a constant that maybe zero, and
       
  1743 //        offset is (k2 [+/- invariant]) where k2 maybe zero and invariant is optional
       
  1744 bool SWPointer::scaled_iv_plus_offset(Node* n) {
       
  1745   if (scaled_iv(n)) {
       
  1746     return true;
       
  1747   }
       
  1748   if (offset_plus_k(n)) {
       
  1749     return true;
       
  1750   }
       
  1751   int opc = n->Opcode();
       
  1752   if (opc == Op_AddI) {
       
  1753     if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2))) {
       
  1754       return true;
       
  1755     }
       
  1756     if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
       
  1757       return true;
       
  1758     }
       
  1759   } else if (opc == Op_SubI) {
       
  1760     if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2), true)) {
       
  1761       return true;
       
  1762     }
       
  1763     if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
       
  1764       _scale *= -1;
       
  1765       return true;
       
  1766     }
       
  1767   }
       
  1768   return false;
       
  1769 }
       
  1770 
       
  1771 //----------------------------scaled_iv------------------------
       
  1772 // Match: k*iv where k is a constant that's not zero
       
  1773 bool SWPointer::scaled_iv(Node* n) {
       
  1774   if (_scale != 0) {
       
  1775     return false;  // already found a scale
       
  1776   }
       
  1777   if (n == iv()) {
       
  1778     _scale = 1;
       
  1779     return true;
       
  1780   }
       
  1781   int opc = n->Opcode();
       
  1782   if (opc == Op_MulI) {
       
  1783     if (n->in(1) == iv() && n->in(2)->is_Con()) {
       
  1784       _scale = n->in(2)->get_int();
       
  1785       return true;
       
  1786     } else if (n->in(2) == iv() && n->in(1)->is_Con()) {
       
  1787       _scale = n->in(1)->get_int();
       
  1788       return true;
       
  1789     }
       
  1790   } else if (opc == Op_LShiftI) {
       
  1791     if (n->in(1) == iv() && n->in(2)->is_Con()) {
       
  1792       _scale = 1 << n->in(2)->get_int();
       
  1793       return true;
       
  1794     }
       
  1795   } else if (opc == Op_ConvI2L) {
       
  1796     if (scaled_iv_plus_offset(n->in(1))) {
       
  1797       return true;
       
  1798     }
       
  1799   } else if (opc == Op_LShiftL) {
       
  1800     if (!has_iv() && _invar == NULL) {
       
  1801       // Need to preserve the current _offset value, so
       
  1802       // create a temporary object for this expression subtree.
       
  1803       // Hacky, so should re-engineer the address pattern match.
       
  1804       SWPointer tmp(this);
       
  1805       if (tmp.scaled_iv_plus_offset(n->in(1))) {
       
  1806         if (tmp._invar == NULL) {
       
  1807           int mult = 1 << n->in(2)->get_int();
       
  1808           _scale   = tmp._scale  * mult;
       
  1809           _offset += tmp._offset * mult;
       
  1810           return true;
       
  1811         }
       
  1812       }
       
  1813     }
       
  1814   }
       
  1815   return false;
       
  1816 }
       
  1817 
       
  1818 //----------------------------offset_plus_k------------------------
       
  1819 // Match: offset is (k [+/- invariant])
       
  1820 // where k maybe zero and invariant is optional, but not both.
       
  1821 bool SWPointer::offset_plus_k(Node* n, bool negate) {
       
  1822   int opc = n->Opcode();
       
  1823   if (opc == Op_ConI) {
       
  1824     _offset += negate ? -(n->get_int()) : n->get_int();
       
  1825     return true;
       
  1826   } else if (opc == Op_ConL) {
       
  1827     // Okay if value fits into an int
       
  1828     const TypeLong* t = n->find_long_type();
       
  1829     if (t->higher_equal(TypeLong::INT)) {
       
  1830       jlong loff = n->get_long();
       
  1831       jint  off  = (jint)loff;
       
  1832       _offset += negate ? -off : loff;
       
  1833       return true;
       
  1834     }
       
  1835     return false;
       
  1836   }
       
  1837   if (_invar != NULL) return false; // already have an invariant
       
  1838   if (opc == Op_AddI) {
       
  1839     if (n->in(2)->is_Con() && invariant(n->in(1))) {
       
  1840       _negate_invar = negate;
       
  1841       _invar = n->in(1);
       
  1842       _offset += negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
       
  1843       return true;
       
  1844     } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
       
  1845       _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
       
  1846       _negate_invar = negate;
       
  1847       _invar = n->in(2);
       
  1848       return true;
       
  1849     }
       
  1850   }
       
  1851   if (opc == Op_SubI) {
       
  1852     if (n->in(2)->is_Con() && invariant(n->in(1))) {
       
  1853       _negate_invar = negate;
       
  1854       _invar = n->in(1);
       
  1855       _offset += !negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
       
  1856       return true;
       
  1857     } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
       
  1858       _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
       
  1859       _negate_invar = !negate;
       
  1860       _invar = n->in(2);
       
  1861       return true;
       
  1862     }
       
  1863   }
       
  1864   if (invariant(n)) {
       
  1865     _negate_invar = negate;
       
  1866     _invar = n;
       
  1867     return true;
       
  1868   }
       
  1869   return false;
       
  1870 }
       
  1871 
       
  1872 //----------------------------print------------------------
       
  1873 void SWPointer::print() {
       
  1874 #ifndef PRODUCT
       
  1875   tty->print("base: %d  adr: %d  scale: %d  offset: %d  invar: %c%d\n",
       
  1876              _base != NULL ? _base->_idx : 0,
       
  1877              _adr  != NULL ? _adr->_idx  : 0,
       
  1878              _scale, _offset,
       
  1879              _negate_invar?'-':'+',
       
  1880              _invar != NULL ? _invar->_idx : 0);
       
  1881 #endif
       
  1882 }
       
  1883 
       
  1884 // ========================= OrderedPair =====================
       
  1885 
       
  1886 const OrderedPair OrderedPair::initial;
       
  1887 
       
  1888 // ========================= SWNodeInfo =====================
       
  1889 
       
  1890 const SWNodeInfo SWNodeInfo::initial;
       
  1891 
       
  1892 
       
  1893 // ============================ DepGraph ===========================
       
  1894 
       
  1895 //------------------------------make_node---------------------------
       
  1896 // Make a new dependence graph node for an ideal node.
       
  1897 DepMem* DepGraph::make_node(Node* node) {
       
  1898   DepMem* m = new (_arena) DepMem(node);
       
  1899   if (node != NULL) {
       
  1900     assert(_map.at_grow(node->_idx) == NULL, "one init only");
       
  1901     _map.at_put_grow(node->_idx, m);
       
  1902   }
       
  1903   return m;
       
  1904 }
       
  1905 
       
  1906 //------------------------------make_edge---------------------------
       
  1907 // Make a new dependence graph edge from dpred -> dsucc
       
  1908 DepEdge* DepGraph::make_edge(DepMem* dpred, DepMem* dsucc) {
       
  1909   DepEdge* e = new (_arena) DepEdge(dpred, dsucc, dsucc->in_head(), dpred->out_head());
       
  1910   dpred->set_out_head(e);
       
  1911   dsucc->set_in_head(e);
       
  1912   return e;
       
  1913 }
       
  1914 
       
  1915 // ========================== DepMem ========================
       
  1916 
       
  1917 //------------------------------in_cnt---------------------------
       
  1918 int DepMem::in_cnt() {
       
  1919   int ct = 0;
       
  1920   for (DepEdge* e = _in_head; e != NULL; e = e->next_in()) ct++;
       
  1921   return ct;
       
  1922 }
       
  1923 
       
  1924 //------------------------------out_cnt---------------------------
       
  1925 int DepMem::out_cnt() {
       
  1926   int ct = 0;
       
  1927   for (DepEdge* e = _out_head; e != NULL; e = e->next_out()) ct++;
       
  1928   return ct;
       
  1929 }
       
  1930 
       
  1931 //------------------------------print-----------------------------
       
  1932 void DepMem::print() {
       
  1933 #ifndef PRODUCT
       
  1934   tty->print("  DepNode %d (", _node->_idx);
       
  1935   for (DepEdge* p = _in_head; p != NULL; p = p->next_in()) {
       
  1936     Node* pred = p->pred()->node();
       
  1937     tty->print(" %d", pred != NULL ? pred->_idx : 0);
       
  1938   }
       
  1939   tty->print(") [");
       
  1940   for (DepEdge* s = _out_head; s != NULL; s = s->next_out()) {
       
  1941     Node* succ = s->succ()->node();
       
  1942     tty->print(" %d", succ != NULL ? succ->_idx : 0);
       
  1943   }
       
  1944   tty->print_cr(" ]");
       
  1945 #endif
       
  1946 }
       
  1947 
       
  1948 // =========================== DepEdge =========================
       
  1949 
       
  1950 //------------------------------DepPreds---------------------------
       
  1951 void DepEdge::print() {
       
  1952 #ifndef PRODUCT
       
  1953   tty->print_cr("DepEdge: %d [ %d ]", _pred->node()->_idx, _succ->node()->_idx);
       
  1954 #endif
       
  1955 }
       
  1956 
       
  1957 // =========================== DepPreds =========================
       
  1958 // Iterator over predecessor edges in the dependence graph.
       
  1959 
       
  1960 //------------------------------DepPreds---------------------------
       
  1961 DepPreds::DepPreds(Node* n, DepGraph& dg) {
       
  1962   _n = n;
       
  1963   _done = false;
       
  1964   if (_n->is_Store() || _n->is_Load()) {
       
  1965     _next_idx = MemNode::Address;
       
  1966     _end_idx  = n->req();
       
  1967     _dep_next = dg.dep(_n)->in_head();
       
  1968   } else if (_n->is_Mem()) {
       
  1969     _next_idx = 0;
       
  1970     _end_idx  = 0;
       
  1971     _dep_next = dg.dep(_n)->in_head();
       
  1972   } else {
       
  1973     _next_idx = 1;
       
  1974     _end_idx  = _n->req();
       
  1975     _dep_next = NULL;
       
  1976   }
       
  1977   next();
       
  1978 }
       
  1979 
       
  1980 //------------------------------next---------------------------
       
  1981 void DepPreds::next() {
       
  1982   if (_dep_next != NULL) {
       
  1983     _current  = _dep_next->pred()->node();
       
  1984     _dep_next = _dep_next->next_in();
       
  1985   } else if (_next_idx < _end_idx) {
       
  1986     _current  = _n->in(_next_idx++);
       
  1987   } else {
       
  1988     _done = true;
       
  1989   }
       
  1990 }
       
  1991 
       
  1992 // =========================== DepSuccs =========================
       
  1993 // Iterator over successor edges in the dependence graph.
       
  1994 
       
  1995 //------------------------------DepSuccs---------------------------
       
  1996 DepSuccs::DepSuccs(Node* n, DepGraph& dg) {
       
  1997   _n = n;
       
  1998   _done = false;
       
  1999   if (_n->is_Load()) {
       
  2000     _next_idx = 0;
       
  2001     _end_idx  = _n->outcnt();
       
  2002     _dep_next = dg.dep(_n)->out_head();
       
  2003   } else if (_n->is_Mem() || _n->is_Phi() && _n->bottom_type() == Type::MEMORY) {
       
  2004     _next_idx = 0;
       
  2005     _end_idx  = 0;
       
  2006     _dep_next = dg.dep(_n)->out_head();
       
  2007   } else {
       
  2008     _next_idx = 0;
       
  2009     _end_idx  = _n->outcnt();
       
  2010     _dep_next = NULL;
       
  2011   }
       
  2012   next();
       
  2013 }
       
  2014 
       
  2015 //-------------------------------next---------------------------
       
  2016 void DepSuccs::next() {
       
  2017   if (_dep_next != NULL) {
       
  2018     _current  = _dep_next->succ()->node();
       
  2019     _dep_next = _dep_next->next_out();
       
  2020   } else if (_next_idx < _end_idx) {
       
  2021     _current  = _n->raw_out(_next_idx++);
       
  2022   } else {
       
  2023     _done = true;
       
  2024   }
       
  2025 }