src/share/vm/opto/superword.cpp

Thu, 14 Feb 2019 14:31:32 +0100

author
neliasso
date
Thu, 14 Feb 2019 14:31:32 +0100
changeset 9741
7e0a4478e80f
parent 9740
b290489738b8
child 9756
2be326848943
child 9768
eaae2ae06faf
permissions
-rw-r--r--

8087128: C2: Disallow definition split on MachCopySpill nodes
Reviewed-by: kvn

     1 /*
     2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 #include "precompiled.hpp"
    25 #include "compiler/compileLog.hpp"
    26 #include "libadt/vectset.hpp"
    27 #include "memory/allocation.inline.hpp"
    28 #include "opto/addnode.hpp"
    29 #include "opto/callnode.hpp"
    30 #include "opto/divnode.hpp"
    31 #include "opto/matcher.hpp"
    32 #include "opto/memnode.hpp"
    33 #include "opto/mulnode.hpp"
    34 #include "opto/opcodes.hpp"
    35 #include "opto/superword.hpp"
    36 #include "opto/vectornode.hpp"
    38 //
    39 //                  S U P E R W O R D   T R A N S F O R M
    40 //=============================================================================
    42 //------------------------------SuperWord---------------------------
    43 SuperWord::SuperWord(PhaseIdealLoop* phase) :
    44   _phase(phase),
    45   _igvn(phase->_igvn),
    46   _arena(phase->C->comp_arena()),
    47   _packset(arena(), 8,  0, NULL),         // packs for the current block
    48   _bb_idx(arena(), (int)(1.10 * phase->C->unique()), 0, 0), // node idx to index in bb
    49   _block(arena(), 8,  0, NULL),           // nodes in current block
    50   _data_entry(arena(), 8,  0, NULL),      // nodes with all inputs from outside
    51   _mem_slice_head(arena(), 8,  0, NULL),  // memory slice heads
    52   _mem_slice_tail(arena(), 8,  0, NULL),  // memory slice tails
    53   _node_info(arena(), 8,  0, SWNodeInfo::initial), // info needed per node
    54   _align_to_ref(NULL),                    // memory reference to align vectors to
    55   _disjoint_ptrs(arena(), 8,  0, OrderedPair::initial), // runtime disambiguated pointer pairs
    56   _dg(_arena),                            // dependence graph
    57   _visited(arena()),                      // visited node set
    58   _post_visited(arena()),                 // post visited node set
    59   _n_idx_list(arena(), 8),                // scratch list of (node,index) pairs
    60   _stk(arena(), 8, 0, NULL),              // scratch stack of nodes
    61   _nlist(arena(), 8, 0, NULL),            // scratch list of nodes
    62   _lpt(NULL),                             // loop tree node
    63   _lp(NULL),                              // LoopNode
    64   _bb(NULL),                              // basic block
    65   _iv(NULL)                               // induction var
    66 {}
    68 //------------------------------transform_loop---------------------------
    69 void SuperWord::transform_loop(IdealLoopTree* lpt) {
    70   assert(UseSuperWord, "should be");
    71   // Do vectors exist on this architecture?
    72   if (Matcher::vector_width_in_bytes(T_BYTE) < 2) return;
    74   assert(lpt->_head->is_CountedLoop(), "must be");
    75   CountedLoopNode *cl = lpt->_head->as_CountedLoop();
    77   if (!cl->is_valid_counted_loop()) return; // skip malformed counted loop
    79   if (!cl->is_main_loop() ) return; // skip normal, pre, and post loops
    81   // Check for no control flow in body (other than exit)
    82   Node *cl_exit = cl->loopexit();
    83   if (cl_exit->in(0) != lpt->_head) return;
    85   // Make sure the are no extra control users of the loop backedge
    86   if (cl->back_control()->outcnt() != 1) {
    87     return;
    88   }
    90   // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit))))
    91   CountedLoopEndNode* pre_end = get_pre_loop_end(cl);
    92   if (pre_end == NULL) return;
    93   Node *pre_opaq1 = pre_end->limit();
    94   if (pre_opaq1->Opcode() != Op_Opaque1) return;
    96   init(); // initialize data structures
    98   set_lpt(lpt);
    99   set_lp(cl);
   101   // For now, define one block which is the entire loop body
   102   set_bb(cl);
   104   assert(_packset.length() == 0, "packset must be empty");
   105   SLP_extract();
   106 }
   108 //------------------------------SLP_extract---------------------------
   109 // Extract the superword level parallelism
   110 //
   111 // 1) A reverse post-order of nodes in the block is constructed.  By scanning
   112 //    this list from first to last, all definitions are visited before their uses.
   113 //
   114 // 2) A point-to-point dependence graph is constructed between memory references.
   115 //    This simplies the upcoming "independence" checker.
   116 //
   117 // 3) The maximum depth in the node graph from the beginning of the block
   118 //    to each node is computed.  This is used to prune the graph search
   119 //    in the independence checker.
   120 //
   121 // 4) For integer types, the necessary bit width is propagated backwards
   122 //    from stores to allow packed operations on byte, char, and short
   123 //    integers.  This reverses the promotion to type "int" that javac
   124 //    did for operations like: char c1,c2,c3;  c1 = c2 + c3.
   125 //
   126 // 5) One of the memory references is picked to be an aligned vector reference.
   127 //    The pre-loop trip count is adjusted to align this reference in the
   128 //    unrolled body.
   129 //
   130 // 6) The initial set of pack pairs is seeded with memory references.
   131 //
   132 // 7) The set of pack pairs is extended by following use->def and def->use links.
   133 //
   134 // 8) The pairs are combined into vector sized packs.
   135 //
   136 // 9) Reorder the memory slices to co-locate members of the memory packs.
   137 //
   138 // 10) Generate ideal vector nodes for the final set of packs and where necessary,
   139 //    inserting scalar promotion, vector creation from multiple scalars, and
   140 //    extraction of scalar values from vectors.
   141 //
   142 void SuperWord::SLP_extract() {
   144   // Ready the block
   146   if (!construct_bb())
   147     return; // Exit if no interesting nodes or complex graph.
   149   dependence_graph();
   151   compute_max_depth();
   153   compute_vector_element_type();
   155   // Attempt vectorization
   157   find_adjacent_refs();
   159   extend_packlist();
   161   combine_packs();
   163   construct_my_pack_map();
   165   filter_packs();
   167   schedule();
   169   output();
   170 }
   172 //------------------------------find_adjacent_refs---------------------------
   173 // Find the adjacent memory references and create pack pairs for them.
   174 // This is the initial set of packs that will then be extended by
   175 // following use->def and def->use links.  The align positions are
   176 // assigned relative to the reference "align_to_ref"
   177 void SuperWord::find_adjacent_refs() {
   178   // Get list of memory operations
   179   Node_List memops;
   180   for (int i = 0; i < _block.length(); i++) {
   181     Node* n = _block.at(i);
   182     if (n->is_Mem() && !n->is_LoadStore() && in_bb(n) &&
   183         is_java_primitive(n->as_Mem()->memory_type())) {
   184       int align = memory_alignment(n->as_Mem(), 0);
   185       if (align != bottom_align) {
   186         memops.push(n);
   187       }
   188     }
   189   }
   191   Node_List align_to_refs;
   192   int best_iv_adjustment = 0;
   193   MemNode* best_align_to_mem_ref = NULL;
   195   while (memops.size() != 0) {
   196     // Find a memory reference to align to.
   197     MemNode* mem_ref = find_align_to_ref(memops);
   198     if (mem_ref == NULL) break;
   199     align_to_refs.push(mem_ref);
   200     int iv_adjustment = get_iv_adjustment(mem_ref);
   202     if (best_align_to_mem_ref == NULL) {
   203       // Set memory reference which is the best from all memory operations
   204       // to be used for alignment. The pre-loop trip count is modified to align
   205       // this reference to a vector-aligned address.
   206       best_align_to_mem_ref = mem_ref;
   207       best_iv_adjustment = iv_adjustment;
   208     }
   210     SWPointer align_to_ref_p(mem_ref, this);
   211     // Set alignment relative to "align_to_ref" for all related memory operations.
   212     for (int i = memops.size() - 1; i >= 0; i--) {
   213       MemNode* s = memops.at(i)->as_Mem();
   214       if (isomorphic(s, mem_ref)) {
   215         SWPointer p2(s, this);
   216         if (p2.comparable(align_to_ref_p)) {
   217           int align = memory_alignment(s, iv_adjustment);
   218           set_alignment(s, align);
   219         }
   220       }
   221     }
   223     // Create initial pack pairs of memory operations for which
   224     // alignment is set and vectors will be aligned.
   225     bool create_pack = true;
   226     if (memory_alignment(mem_ref, best_iv_adjustment) == 0) {
   227       if (!Matcher::misaligned_vectors_ok()) {
   228         int vw = vector_width(mem_ref);
   229         int vw_best = vector_width(best_align_to_mem_ref);
   230         if (vw > vw_best) {
   231           // Do not vectorize a memory access with more elements per vector
   232           // if unaligned memory access is not allowed because number of
   233           // iterations in pre-loop will be not enough to align it.
   234           create_pack = false;
   235         } else {
   236           SWPointer p2(best_align_to_mem_ref, this);
   237           if (align_to_ref_p.invar() != p2.invar()) {
   238             // Do not vectorize memory accesses with different invariants
   239             // if unaligned memory accesses are not allowed.
   240             create_pack = false;
   241           }
   242         }
   243       }
   244     } else {
   245       if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
   246         // Can't allow vectorization of unaligned memory accesses with the
   247         // same type since it could be overlapped accesses to the same array.
   248         create_pack = false;
   249       } else {
   250         // Allow independent (different type) unaligned memory operations
   251         // if HW supports them.
   252         if (!Matcher::misaligned_vectors_ok()) {
   253           create_pack = false;
   254         } else {
   255           // Check if packs of the same memory type but
   256           // with a different alignment were created before.
   257           for (uint i = 0; i < align_to_refs.size(); i++) {
   258             MemNode* mr = align_to_refs.at(i)->as_Mem();
   259             if (same_velt_type(mr, mem_ref) &&
   260                 memory_alignment(mr, iv_adjustment) != 0)
   261               create_pack = false;
   262           }
   263         }
   264       }
   265     }
   266     if (create_pack) {
   267       for (uint i = 0; i < memops.size(); i++) {
   268         Node* s1 = memops.at(i);
   269         int align = alignment(s1);
   270         if (align == top_align) continue;
   271         for (uint j = 0; j < memops.size(); j++) {
   272           Node* s2 = memops.at(j);
   273           if (alignment(s2) == top_align) continue;
   274           if (s1 != s2 && are_adjacent_refs(s1, s2)) {
   275             if (stmts_can_pack(s1, s2, align)) {
   276               Node_List* pair = new Node_List();
   277               pair->push(s1);
   278               pair->push(s2);
   279               _packset.append(pair);
   280             }
   281           }
   282         }
   283       }
   284     } else { // Don't create unaligned pack
   285       // First, remove remaining memory ops of the same type from the list.
   286       for (int i = memops.size() - 1; i >= 0; i--) {
   287         MemNode* s = memops.at(i)->as_Mem();
   288         if (same_velt_type(s, mem_ref)) {
   289           memops.remove(i);
   290         }
   291       }
   293       // Second, remove already constructed packs of the same type.
   294       for (int i = _packset.length() - 1; i >= 0; i--) {
   295         Node_List* p = _packset.at(i);
   296         MemNode* s = p->at(0)->as_Mem();
   297         if (same_velt_type(s, mem_ref)) {
   298           remove_pack_at(i);
   299         }
   300       }
   302       // If needed find the best memory reference for loop alignment again.
   303       if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
   304         // Put memory ops from remaining packs back on memops list for
   305         // the best alignment search.
   306         uint orig_msize = memops.size();
   307         for (int i = 0; i < _packset.length(); i++) {
   308           Node_List* p = _packset.at(i);
   309           MemNode* s = p->at(0)->as_Mem();
   310           assert(!same_velt_type(s, mem_ref), "sanity");
   311           memops.push(s);
   312         }
   313         MemNode* best_align_to_mem_ref = find_align_to_ref(memops);
   314         if (best_align_to_mem_ref == NULL) break;
   315         best_iv_adjustment = get_iv_adjustment(best_align_to_mem_ref);
   316         // Restore list.
   317         while (memops.size() > orig_msize)
   318           (void)memops.pop();
   319       }
   320     } // unaligned memory accesses
   322     // Remove used mem nodes.
   323     for (int i = memops.size() - 1; i >= 0; i--) {
   324       MemNode* m = memops.at(i)->as_Mem();
   325       if (alignment(m) != top_align) {
   326         memops.remove(i);
   327       }
   328     }
   330   } // while (memops.size() != 0
   331   set_align_to_ref(best_align_to_mem_ref);
   333 #ifndef PRODUCT
   334   if (TraceSuperWord) {
   335     tty->print_cr("\nAfter find_adjacent_refs");
   336     print_packset();
   337   }
   338 #endif
   339 }
   341 //------------------------------find_align_to_ref---------------------------
   342 // Find a memory reference to align the loop induction variable to.
   343 // Looks first at stores then at loads, looking for a memory reference
   344 // with the largest number of references similar to it.
   345 MemNode* SuperWord::find_align_to_ref(Node_List &memops) {
   346   GrowableArray<int> cmp_ct(arena(), memops.size(), memops.size(), 0);
   348   // Count number of comparable memory ops
   349   for (uint i = 0; i < memops.size(); i++) {
   350     MemNode* s1 = memops.at(i)->as_Mem();
   351     SWPointer p1(s1, this);
   352     // Discard if pre loop can't align this reference
   353     if (!ref_is_alignable(p1)) {
   354       *cmp_ct.adr_at(i) = 0;
   355       continue;
   356     }
   357     for (uint j = i+1; j < memops.size(); j++) {
   358       MemNode* s2 = memops.at(j)->as_Mem();
   359       if (isomorphic(s1, s2)) {
   360         SWPointer p2(s2, this);
   361         if (p1.comparable(p2)) {
   362           (*cmp_ct.adr_at(i))++;
   363           (*cmp_ct.adr_at(j))++;
   364         }
   365       }
   366     }
   367   }
   369   // Find Store (or Load) with the greatest number of "comparable" references,
   370   // biggest vector size, smallest data size and smallest iv offset.
   371   int max_ct        = 0;
   372   int max_vw        = 0;
   373   int max_idx       = -1;
   374   int min_size      = max_jint;
   375   int min_iv_offset = max_jint;
   376   for (uint j = 0; j < memops.size(); j++) {
   377     MemNode* s = memops.at(j)->as_Mem();
   378     if (s->is_Store()) {
   379       int vw = vector_width_in_bytes(s);
   380       assert(vw > 1, "sanity");
   381       SWPointer p(s, this);
   382       if (cmp_ct.at(j) >  max_ct ||
   383           cmp_ct.at(j) == max_ct &&
   384             (vw >  max_vw ||
   385              vw == max_vw &&
   386               (data_size(s) <  min_size ||
   387                data_size(s) == min_size &&
   388                  (p.offset_in_bytes() < min_iv_offset)))) {
   389         max_ct = cmp_ct.at(j);
   390         max_vw = vw;
   391         max_idx = j;
   392         min_size = data_size(s);
   393         min_iv_offset = p.offset_in_bytes();
   394       }
   395     }
   396   }
   397   // If no stores, look at loads
   398   if (max_ct == 0) {
   399     for (uint j = 0; j < memops.size(); j++) {
   400       MemNode* s = memops.at(j)->as_Mem();
   401       if (s->is_Load()) {
   402         int vw = vector_width_in_bytes(s);
   403         assert(vw > 1, "sanity");
   404         SWPointer p(s, this);
   405         if (cmp_ct.at(j) >  max_ct ||
   406             cmp_ct.at(j) == max_ct &&
   407               (vw >  max_vw ||
   408                vw == max_vw &&
   409                 (data_size(s) <  min_size ||
   410                  data_size(s) == min_size &&
   411                    (p.offset_in_bytes() < min_iv_offset)))) {
   412           max_ct = cmp_ct.at(j);
   413           max_vw = vw;
   414           max_idx = j;
   415           min_size = data_size(s);
   416           min_iv_offset = p.offset_in_bytes();
   417         }
   418       }
   419     }
   420   }
   422 #ifdef ASSERT
   423   if (TraceSuperWord && Verbose) {
   424     tty->print_cr("\nVector memops after find_align_to_refs");
   425     for (uint i = 0; i < memops.size(); i++) {
   426       MemNode* s = memops.at(i)->as_Mem();
   427       s->dump();
   428     }
   429   }
   430 #endif
   432   if (max_ct > 0) {
   433 #ifdef ASSERT
   434     if (TraceSuperWord) {
   435       tty->print("\nVector align to node: ");
   436       memops.at(max_idx)->as_Mem()->dump();
   437     }
   438 #endif
   439     return memops.at(max_idx)->as_Mem();
   440   }
   441   return NULL;
   442 }
   444 //------------------------------ref_is_alignable---------------------------
   445 // Can the preloop align the reference to position zero in the vector?
   446 bool SuperWord::ref_is_alignable(SWPointer& p) {
   447   if (!p.has_iv()) {
   448     return true;   // no induction variable
   449   }
   450   CountedLoopEndNode* pre_end = get_pre_loop_end(lp()->as_CountedLoop());
   451   assert(pre_end->stride_is_con(), "pre loop stride is constant");
   452   int preloop_stride = pre_end->stride_con();
   454   int span = preloop_stride * p.scale_in_bytes();
   455   int mem_size = p.memory_size();
   456   int offset   = p.offset_in_bytes();
   457   // Stride one accesses are alignable if offset is aligned to memory operation size.
   458   // Offset can be unaligned when UseUnalignedAccesses is used.
   459   if (ABS(span) == mem_size && (ABS(offset) % mem_size) == 0) {
   460     return true;
   461   }
   462   // If the initial offset from start of the object is computable,
   463   // check if the pre-loop can align the final offset accordingly.
   464   //
   465   // In other words: Can we find an i such that the offset
   466   // after i pre-loop iterations is aligned to vw?
   467   //   (init_offset + pre_loop) % vw == 0              (1)
   468   // where
   469   //   pre_loop = i * span
   470   // is the number of bytes added to the offset by i pre-loop iterations.
   471   //
   472   // For this to hold we need pre_loop to increase init_offset by
   473   //   pre_loop = vw - (init_offset % vw)
   474   //
   475   // This is only possible if pre_loop is divisible by span because each
   476   // pre-loop iteration increases the initial offset by 'span' bytes:
   477   //   (vw - (init_offset % vw)) % span == 0
   478   //
   479   int vw = vector_width_in_bytes(p.mem());
   480   assert(vw > 1, "sanity");
   481   Node* init_nd = pre_end->init_trip();
   482   if (init_nd->is_Con() && p.invar() == NULL) {
   483     int init = init_nd->bottom_type()->is_int()->get_con();
   484     int init_offset = init * p.scale_in_bytes() + offset;
   485     if (init_offset < 0) { // negative offset from object start?
   486       return false;        // may happen in dead loop
   487     }
   488     if (vw % span == 0) {
   489       // If vm is a multiple of span, we use formula (1).
   490       if (span > 0) {
   491         return (vw - (init_offset % vw)) % span == 0;
   492       } else {
   493         assert(span < 0, "nonzero stride * scale");
   494         return (init_offset % vw) % -span == 0;
   495       }
   496     } else if (span % vw == 0) {
   497       // If span is a multiple of vw, we can simplify formula (1) to:
   498       //   (init_offset + i * span) % vw == 0
   499       //     =>
   500       //   (init_offset % vw) + ((i * span) % vw) == 0
   501       //     =>
   502       //   init_offset % vw == 0
   503       //
   504       // Because we add a multiple of vw to the initial offset, the final
   505       // offset is a multiple of vw if and only if init_offset is a multiple.
   506       //
   507       return (init_offset % vw) == 0;
   508     }
   509   }
   510   return false;
   511 }
   513 //---------------------------get_iv_adjustment---------------------------
   514 // Calculate loop's iv adjustment for this memory ops.
   515 int SuperWord::get_iv_adjustment(MemNode* mem_ref) {
   516   SWPointer align_to_ref_p(mem_ref, this);
   517   int offset = align_to_ref_p.offset_in_bytes();
   518   int scale  = align_to_ref_p.scale_in_bytes();
   519   int elt_size = align_to_ref_p.memory_size();
   520   int vw       = vector_width_in_bytes(mem_ref);
   521   assert(vw > 1, "sanity");
   522   int iv_adjustment;
   523   if (scale != 0) {
   524     int stride_sign = (scale * iv_stride()) > 0 ? 1 : -1;
   525     // At least one iteration is executed in pre-loop by default. As result
   526     // several iterations are needed to align memory operations in main-loop even
   527     // if offset is 0.
   528     int iv_adjustment_in_bytes = (stride_sign * vw - (offset % vw));
   529     assert(((ABS(iv_adjustment_in_bytes) % elt_size) == 0),
   530            err_msg_res("(%d) should be divisible by (%d)", iv_adjustment_in_bytes, elt_size));
   531     iv_adjustment = iv_adjustment_in_bytes/elt_size;
   532   } else {
   533     // This memory op is not dependent on iv (scale == 0)
   534     iv_adjustment = 0;
   535   }
   537 #ifndef PRODUCT
   538   if (TraceSuperWord)
   539     tty->print_cr("\noffset = %d iv_adjust = %d elt_size = %d scale = %d iv_stride = %d vect_size %d",
   540                   offset, iv_adjustment, elt_size, scale, iv_stride(), vw);
   541 #endif
   542   return iv_adjustment;
   543 }
   545 //---------------------------dependence_graph---------------------------
   546 // Construct dependency graph.
   547 // Add dependence edges to load/store nodes for memory dependence
   548 //    A.out()->DependNode.in(1) and DependNode.out()->B.prec(x)
   549 void SuperWord::dependence_graph() {
   550   // First, assign a dependence node to each memory node
   551   for (int i = 0; i < _block.length(); i++ ) {
   552     Node *n = _block.at(i);
   553     if (n->is_Mem() || n->is_Phi() && n->bottom_type() == Type::MEMORY) {
   554       _dg.make_node(n);
   555     }
   556   }
   558   // For each memory slice, create the dependences
   559   for (int i = 0; i < _mem_slice_head.length(); i++) {
   560     Node* n      = _mem_slice_head.at(i);
   561     Node* n_tail = _mem_slice_tail.at(i);
   563     // Get slice in predecessor order (last is first)
   564     mem_slice_preds(n_tail, n, _nlist);
   566     // Make the slice dependent on the root
   567     DepMem* slice = _dg.dep(n);
   568     _dg.make_edge(_dg.root(), slice);
   570     // Create a sink for the slice
   571     DepMem* slice_sink = _dg.make_node(NULL);
   572     _dg.make_edge(slice_sink, _dg.tail());
   574     // Now visit each pair of memory ops, creating the edges
   575     for (int j = _nlist.length() - 1; j >= 0 ; j--) {
   576       Node* s1 = _nlist.at(j);
   578       // If no dependency yet, use slice
   579       if (_dg.dep(s1)->in_cnt() == 0) {
   580         _dg.make_edge(slice, s1);
   581       }
   582       SWPointer p1(s1->as_Mem(), this);
   583       bool sink_dependent = true;
   584       for (int k = j - 1; k >= 0; k--) {
   585         Node* s2 = _nlist.at(k);
   586         if (s1->is_Load() && s2->is_Load())
   587           continue;
   588         SWPointer p2(s2->as_Mem(), this);
   590         int cmp = p1.cmp(p2);
   591         if (SuperWordRTDepCheck &&
   592             p1.base() != p2.base() && p1.valid() && p2.valid()) {
   593           // Create a runtime check to disambiguate
   594           OrderedPair pp(p1.base(), p2.base());
   595           _disjoint_ptrs.append_if_missing(pp);
   596         } else if (!SWPointer::not_equal(cmp)) {
   597           // Possibly same address
   598           _dg.make_edge(s1, s2);
   599           sink_dependent = false;
   600         }
   601       }
   602       if (sink_dependent) {
   603         _dg.make_edge(s1, slice_sink);
   604       }
   605     }
   606 #ifndef PRODUCT
   607     if (TraceSuperWord) {
   608       tty->print_cr("\nDependence graph for slice: %d", n->_idx);
   609       for (int q = 0; q < _nlist.length(); q++) {
   610         _dg.print(_nlist.at(q));
   611       }
   612       tty->cr();
   613     }
   614 #endif
   615     _nlist.clear();
   616   }
   618 #ifndef PRODUCT
   619   if (TraceSuperWord) {
   620     tty->print_cr("\ndisjoint_ptrs: %s", _disjoint_ptrs.length() > 0 ? "" : "NONE");
   621     for (int r = 0; r < _disjoint_ptrs.length(); r++) {
   622       _disjoint_ptrs.at(r).print();
   623       tty->cr();
   624     }
   625     tty->cr();
   626   }
   627 #endif
   628 }
   630 //---------------------------mem_slice_preds---------------------------
   631 // Return a memory slice (node list) in predecessor order starting at "start"
   632 void SuperWord::mem_slice_preds(Node* start, Node* stop, GrowableArray<Node*> &preds) {
   633   assert(preds.length() == 0, "start empty");
   634   Node* n = start;
   635   Node* prev = NULL;
   636   while (true) {
   637     assert(in_bb(n), "must be in block");
   638     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
   639       Node* out = n->fast_out(i);
   640       if (out->is_Load()) {
   641         if (in_bb(out)) {
   642           preds.push(out);
   643         }
   644       } else {
   645         // FIXME
   646         if (out->is_MergeMem() && !in_bb(out)) {
   647           // Either unrolling is causing a memory edge not to disappear,
   648           // or need to run igvn.optimize() again before SLP
   649         } else if (out->is_Phi() && out->bottom_type() == Type::MEMORY && !in_bb(out)) {
   650           // Ditto.  Not sure what else to check further.
   651         } else if (out->Opcode() == Op_StoreCM && out->in(MemNode::OopStore) == n) {
   652           // StoreCM has an input edge used as a precedence edge.
   653           // Maybe an issue when oop stores are vectorized.
   654         } else {
   655           assert(out == prev || prev == NULL, "no branches off of store slice");
   656         }
   657       }
   658     }
   659     if (n == stop) break;
   660     preds.push(n);
   661     prev = n;
   662     assert(n->is_Mem(), err_msg_res("unexpected node %s", n->Name()));
   663     n = n->in(MemNode::Memory);
   664   }
   665 }
   667 //------------------------------stmts_can_pack---------------------------
   668 // Can s1 and s2 be in a pack with s1 immediately preceding s2 and
   669 // s1 aligned at "align"
   670 bool SuperWord::stmts_can_pack(Node* s1, Node* s2, int align) {
   672   // Do not use superword for non-primitives
   673   BasicType bt1 = velt_basic_type(s1);
   674   BasicType bt2 = velt_basic_type(s2);
   675   if(!is_java_primitive(bt1) || !is_java_primitive(bt2))
   676     return false;
   677   if (Matcher::max_vector_size(bt1) < 2) {
   678     return false; // No vectors for this type
   679   }
   681   if (isomorphic(s1, s2)) {
   682     if (independent(s1, s2)) {
   683       if (!exists_at(s1, 0) && !exists_at(s2, 1)) {
   684         if (!s1->is_Mem() || are_adjacent_refs(s1, s2)) {
   685           int s1_align = alignment(s1);
   686           int s2_align = alignment(s2);
   687           if (s1_align == top_align || s1_align == align) {
   688             if (s2_align == top_align || s2_align == align + data_size(s1)) {
   689               return true;
   690             }
   691           }
   692         }
   693       }
   694     }
   695   }
   696   return false;
   697 }
   699 //------------------------------exists_at---------------------------
   700 // Does s exist in a pack at position pos?
   701 bool SuperWord::exists_at(Node* s, uint pos) {
   702   for (int i = 0; i < _packset.length(); i++) {
   703     Node_List* p = _packset.at(i);
   704     if (p->at(pos) == s) {
   705       return true;
   706     }
   707   }
   708   return false;
   709 }
   711 //------------------------------are_adjacent_refs---------------------------
   712 // Is s1 immediately before s2 in memory?
   713 bool SuperWord::are_adjacent_refs(Node* s1, Node* s2) {
   714   if (!s1->is_Mem() || !s2->is_Mem()) return false;
   715   if (!in_bb(s1)    || !in_bb(s2))    return false;
   717   // Do not use superword for non-primitives
   718   if (!is_java_primitive(s1->as_Mem()->memory_type()) ||
   719       !is_java_primitive(s2->as_Mem()->memory_type())) {
   720     return false;
   721   }
   723   // FIXME - co_locate_pack fails on Stores in different mem-slices, so
   724   // only pack memops that are in the same alias set until that's fixed.
   725   if (_phase->C->get_alias_index(s1->as_Mem()->adr_type()) !=
   726       _phase->C->get_alias_index(s2->as_Mem()->adr_type()))
   727     return false;
   728   SWPointer p1(s1->as_Mem(), this);
   729   SWPointer p2(s2->as_Mem(), this);
   730   if (p1.base() != p2.base() || !p1.comparable(p2)) return false;
   731   int diff = p2.offset_in_bytes() - p1.offset_in_bytes();
   732   return diff == data_size(s1);
   733 }
   735 //------------------------------isomorphic---------------------------
   736 // Are s1 and s2 similar?
   737 bool SuperWord::isomorphic(Node* s1, Node* s2) {
   738   if (s1->Opcode() != s2->Opcode()) return false;
   739   if (s1->req() != s2->req()) return false;
   740   if (s1->in(0) != s2->in(0)) return false;
   741   if (!same_velt_type(s1, s2)) return false;
   742   return true;
   743 }
   745 //------------------------------independent---------------------------
   746 // Is there no data path from s1 to s2 or s2 to s1?
   747 bool SuperWord::independent(Node* s1, Node* s2) {
   748   //  assert(s1->Opcode() == s2->Opcode(), "check isomorphic first");
   749   int d1 = depth(s1);
   750   int d2 = depth(s2);
   751   if (d1 == d2) return s1 != s2;
   752   Node* deep    = d1 > d2 ? s1 : s2;
   753   Node* shallow = d1 > d2 ? s2 : s1;
   755   visited_clear();
   757   return independent_path(shallow, deep);
   758 }
   760 //------------------------------independent_path------------------------------
   761 // Helper for independent
   762 bool SuperWord::independent_path(Node* shallow, Node* deep, uint dp) {
   763   if (dp >= 1000) return false; // stop deep recursion
   764   visited_set(deep);
   765   int shal_depth = depth(shallow);
   766   assert(shal_depth <= depth(deep), "must be");
   767   for (DepPreds preds(deep, _dg); !preds.done(); preds.next()) {
   768     Node* pred = preds.current();
   769     if (in_bb(pred) && !visited_test(pred)) {
   770       if (shallow == pred) {
   771         return false;
   772       }
   773       if (shal_depth < depth(pred) && !independent_path(shallow, pred, dp+1)) {
   774         return false;
   775       }
   776     }
   777   }
   778   return true;
   779 }
   781 //------------------------------set_alignment---------------------------
   782 void SuperWord::set_alignment(Node* s1, Node* s2, int align) {
   783   set_alignment(s1, align);
   784   if (align == top_align || align == bottom_align) {
   785     set_alignment(s2, align);
   786   } else {
   787     set_alignment(s2, align + data_size(s1));
   788   }
   789 }
   791 //------------------------------data_size---------------------------
   792 int SuperWord::data_size(Node* s) {
   793   int bsize = type2aelembytes(velt_basic_type(s));
   794   assert(bsize != 0, "valid size");
   795   return bsize;
   796 }
   798 //------------------------------extend_packlist---------------------------
   799 // Extend packset by following use->def and def->use links from pack members.
   800 void SuperWord::extend_packlist() {
   801   bool changed;
   802   do {
   803     changed = false;
   804     for (int i = 0; i < _packset.length(); i++) {
   805       Node_List* p = _packset.at(i);
   806       changed |= follow_use_defs(p);
   807       changed |= follow_def_uses(p);
   808     }
   809   } while (changed);
   811 #ifndef PRODUCT
   812   if (TraceSuperWord) {
   813     tty->print_cr("\nAfter extend_packlist");
   814     print_packset();
   815   }
   816 #endif
   817 }
   819 //------------------------------follow_use_defs---------------------------
   820 // Extend the packset by visiting operand definitions of nodes in pack p
   821 bool SuperWord::follow_use_defs(Node_List* p) {
   822   assert(p->size() == 2, "just checking");
   823   Node* s1 = p->at(0);
   824   Node* s2 = p->at(1);
   825   assert(s1->req() == s2->req(), "just checking");
   826   assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
   828   if (s1->is_Load()) return false;
   830   int align = alignment(s1);
   831   bool changed = false;
   832   int start = s1->is_Store() ? MemNode::ValueIn   : 1;
   833   int end   = s1->is_Store() ? MemNode::ValueIn+1 : s1->req();
   834   for (int j = start; j < end; j++) {
   835     Node* t1 = s1->in(j);
   836     Node* t2 = s2->in(j);
   837     if (!in_bb(t1) || !in_bb(t2))
   838       continue;
   839     if (stmts_can_pack(t1, t2, align)) {
   840       if (est_savings(t1, t2) >= 0) {
   841         Node_List* pair = new Node_List();
   842         pair->push(t1);
   843         pair->push(t2);
   844         _packset.append(pair);
   845         set_alignment(t1, t2, align);
   846         changed = true;
   847       }
   848     }
   849   }
   850   return changed;
   851 }
   853 //------------------------------follow_def_uses---------------------------
   854 // Extend the packset by visiting uses of nodes in pack p
   855 bool SuperWord::follow_def_uses(Node_List* p) {
   856   bool changed = false;
   857   Node* s1 = p->at(0);
   858   Node* s2 = p->at(1);
   859   assert(p->size() == 2, "just checking");
   860   assert(s1->req() == s2->req(), "just checking");
   861   assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
   863   if (s1->is_Store()) return false;
   865   int align = alignment(s1);
   866   int savings = -1;
   867   Node* u1 = NULL;
   868   Node* u2 = NULL;
   869   for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
   870     Node* t1 = s1->fast_out(i);
   871     if (!in_bb(t1)) continue;
   872     for (DUIterator_Fast jmax, j = s2->fast_outs(jmax); j < jmax; j++) {
   873       Node* t2 = s2->fast_out(j);
   874       if (!in_bb(t2)) continue;
   875       if (!opnd_positions_match(s1, t1, s2, t2))
   876         continue;
   877       if (stmts_can_pack(t1, t2, align)) {
   878         int my_savings = est_savings(t1, t2);
   879         if (my_savings > savings) {
   880           savings = my_savings;
   881           u1 = t1;
   882           u2 = t2;
   883         }
   884       }
   885     }
   886   }
   887   if (savings >= 0) {
   888     Node_List* pair = new Node_List();
   889     pair->push(u1);
   890     pair->push(u2);
   891     _packset.append(pair);
   892     set_alignment(u1, u2, align);
   893     changed = true;
   894   }
   895   return changed;
   896 }
   898 //---------------------------opnd_positions_match-------------------------
   899 // Is the use of d1 in u1 at the same operand position as d2 in u2?
   900 bool SuperWord::opnd_positions_match(Node* d1, Node* u1, Node* d2, Node* u2) {
   901   uint ct = u1->req();
   902   if (ct != u2->req()) return false;
   903   uint i1 = 0;
   904   uint i2 = 0;
   905   do {
   906     for (i1++; i1 < ct; i1++) if (u1->in(i1) == d1) break;
   907     for (i2++; i2 < ct; i2++) if (u2->in(i2) == d2) break;
   908     if (i1 != i2) {
   909       if ((i1 == (3-i2)) && (u2->is_Add() || u2->is_Mul())) {
   910         // Further analysis relies on operands position matching.
   911         u2->swap_edges(i1, i2);
   912       } else {
   913         return false;
   914       }
   915     }
   916   } while (i1 < ct);
   917   return true;
   918 }
   920 //------------------------------est_savings---------------------------
   921 // Estimate the savings from executing s1 and s2 as a pack
   922 int SuperWord::est_savings(Node* s1, Node* s2) {
   923   int save_in = 2 - 1; // 2 operations per instruction in packed form
   925   // inputs
   926   for (uint i = 1; i < s1->req(); i++) {
   927     Node* x1 = s1->in(i);
   928     Node* x2 = s2->in(i);
   929     if (x1 != x2) {
   930       if (are_adjacent_refs(x1, x2)) {
   931         save_in += adjacent_profit(x1, x2);
   932       } else if (!in_packset(x1, x2)) {
   933         save_in -= pack_cost(2);
   934       } else {
   935         save_in += unpack_cost(2);
   936       }
   937     }
   938   }
   940   // uses of result
   941   uint ct = 0;
   942   int save_use = 0;
   943   for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
   944     Node* s1_use = s1->fast_out(i);
   945     for (int j = 0; j < _packset.length(); j++) {
   946       Node_List* p = _packset.at(j);
   947       if (p->at(0) == s1_use) {
   948         for (DUIterator_Fast kmax, k = s2->fast_outs(kmax); k < kmax; k++) {
   949           Node* s2_use = s2->fast_out(k);
   950           if (p->at(p->size()-1) == s2_use) {
   951             ct++;
   952             if (are_adjacent_refs(s1_use, s2_use)) {
   953               save_use += adjacent_profit(s1_use, s2_use);
   954             }
   955           }
   956         }
   957       }
   958     }
   959   }
   961   if (ct < s1->outcnt()) save_use += unpack_cost(1);
   962   if (ct < s2->outcnt()) save_use += unpack_cost(1);
   964   return MAX2(save_in, save_use);
   965 }
   967 //------------------------------costs---------------------------
   968 int SuperWord::adjacent_profit(Node* s1, Node* s2) { return 2; }
   969 int SuperWord::pack_cost(int ct)   { return ct; }
   970 int SuperWord::unpack_cost(int ct) { return ct; }
   972 //------------------------------combine_packs---------------------------
   973 // Combine packs A and B with A.last == B.first into A.first..,A.last,B.second,..B.last
   974 void SuperWord::combine_packs() {
   975   bool changed = true;
   976   // Combine packs regardless max vector size.
   977   while (changed) {
   978     changed = false;
   979     for (int i = 0; i < _packset.length(); i++) {
   980       Node_List* p1 = _packset.at(i);
   981       if (p1 == NULL) continue;
   982       for (int j = 0; j < _packset.length(); j++) {
   983         Node_List* p2 = _packset.at(j);
   984         if (p2 == NULL) continue;
   985         if (i == j) continue;
   986         if (p1->at(p1->size()-1) == p2->at(0)) {
   987           for (uint k = 1; k < p2->size(); k++) {
   988             p1->push(p2->at(k));
   989           }
   990           _packset.at_put(j, NULL);
   991           changed = true;
   992         }
   993       }
   994     }
   995   }
   997   // Split packs which have size greater then max vector size.
   998   for (int i = 0; i < _packset.length(); i++) {
   999     Node_List* p1 = _packset.at(i);
  1000     if (p1 != NULL) {
  1001       BasicType bt = velt_basic_type(p1->at(0));
  1002       uint max_vlen = Matcher::max_vector_size(bt); // Max elements in vector
  1003       assert(is_power_of_2(max_vlen), "sanity");
  1004       uint psize = p1->size();
  1005       if (!is_power_of_2(psize)) {
  1006         // Skip pack which can't be vector.
  1007         // case1: for(...) { a[i] = i; }    elements values are different (i+x)
  1008         // case2: for(...) { a[i] = b[i+1]; }  can't align both, load and store
  1009         _packset.at_put(i, NULL);
  1010         continue;
  1012       if (psize > max_vlen) {
  1013         Node_List* pack = new Node_List();
  1014         for (uint j = 0; j < psize; j++) {
  1015           pack->push(p1->at(j));
  1016           if (pack->size() >= max_vlen) {
  1017             assert(is_power_of_2(pack->size()), "sanity");
  1018             _packset.append(pack);
  1019             pack = new Node_List();
  1022         _packset.at_put(i, NULL);
  1027   // Compress list.
  1028   for (int i = _packset.length() - 1; i >= 0; i--) {
  1029     Node_List* p1 = _packset.at(i);
  1030     if (p1 == NULL) {
  1031       _packset.remove_at(i);
  1035 #ifndef PRODUCT
  1036   if (TraceSuperWord) {
  1037     tty->print_cr("\nAfter combine_packs");
  1038     print_packset();
  1040 #endif
  1043 //-----------------------------construct_my_pack_map--------------------------
  1044 // Construct the map from nodes to packs.  Only valid after the
  1045 // point where a node is only in one pack (after combine_packs).
  1046 void SuperWord::construct_my_pack_map() {
  1047   Node_List* rslt = NULL;
  1048   for (int i = 0; i < _packset.length(); i++) {
  1049     Node_List* p = _packset.at(i);
  1050     for (uint j = 0; j < p->size(); j++) {
  1051       Node* s = p->at(j);
  1052       assert(my_pack(s) == NULL, "only in one pack");
  1053       set_my_pack(s, p);
  1058 //------------------------------filter_packs---------------------------
  1059 // Remove packs that are not implemented or not profitable.
  1060 void SuperWord::filter_packs() {
  1062   // Remove packs that are not implemented
  1063   for (int i = _packset.length() - 1; i >= 0; i--) {
  1064     Node_List* pk = _packset.at(i);
  1065     bool impl = implemented(pk);
  1066     if (!impl) {
  1067 #ifndef PRODUCT
  1068       if (TraceSuperWord && Verbose) {
  1069         tty->print_cr("Unimplemented");
  1070         pk->at(0)->dump();
  1072 #endif
  1073       remove_pack_at(i);
  1077   // Remove packs that are not profitable
  1078   bool changed;
  1079   do {
  1080     changed = false;
  1081     for (int i = _packset.length() - 1; i >= 0; i--) {
  1082       Node_List* pk = _packset.at(i);
  1083       bool prof = profitable(pk);
  1084       if (!prof) {
  1085 #ifndef PRODUCT
  1086         if (TraceSuperWord && Verbose) {
  1087           tty->print_cr("Unprofitable");
  1088           pk->at(0)->dump();
  1090 #endif
  1091         remove_pack_at(i);
  1092         changed = true;
  1095   } while (changed);
  1097 #ifndef PRODUCT
  1098   if (TraceSuperWord) {
  1099     tty->print_cr("\nAfter filter_packs");
  1100     print_packset();
  1101     tty->cr();
  1103 #endif
  1106 //------------------------------implemented---------------------------
  1107 // Can code be generated for pack p?
  1108 bool SuperWord::implemented(Node_List* p) {
  1109   Node* p0 = p->at(0);
  1110   return VectorNode::implemented(p0->Opcode(), p->size(), velt_basic_type(p0));
  1113 //------------------------------same_inputs--------------------------
  1114 // For pack p, are all idx operands the same?
  1115 static bool same_inputs(Node_List* p, int idx) {
  1116   Node* p0 = p->at(0);
  1117   uint vlen = p->size();
  1118   Node* p0_def = p0->in(idx);
  1119   for (uint i = 1; i < vlen; i++) {
  1120     Node* pi = p->at(i);
  1121     Node* pi_def = pi->in(idx);
  1122     if (p0_def != pi_def)
  1123       return false;
  1125   return true;
  1128 //------------------------------profitable---------------------------
  1129 // For pack p, are all operands and all uses (with in the block) vector?
  1130 bool SuperWord::profitable(Node_List* p) {
  1131   Node* p0 = p->at(0);
  1132   uint start, end;
  1133   VectorNode::vector_operands(p0, &start, &end);
  1135   // Return false if some inputs are not vectors or vectors with different
  1136   // size or alignment.
  1137   // Also, for now, return false if not scalar promotion case when inputs are
  1138   // the same. Later, implement PackNode and allow differing, non-vector inputs
  1139   // (maybe just the ones from outside the block.)
  1140   for (uint i = start; i < end; i++) {
  1141     if (!is_vector_use(p0, i))
  1142       return false;
  1144   if (VectorNode::is_shift(p0)) {
  1145     // For now, return false if shift count is vector or not scalar promotion
  1146     // case (different shift counts) because it is not supported yet.
  1147     Node* cnt = p0->in(2);
  1148     Node_List* cnt_pk = my_pack(cnt);
  1149     if (cnt_pk != NULL)
  1150       return false;
  1151     if (!same_inputs(p, 2))
  1152       return false;
  1154   if (!p0->is_Store()) {
  1155     // For now, return false if not all uses are vector.
  1156     // Later, implement ExtractNode and allow non-vector uses (maybe
  1157     // just the ones outside the block.)
  1158     for (uint i = 0; i < p->size(); i++) {
  1159       Node* def = p->at(i);
  1160       for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
  1161         Node* use = def->fast_out(j);
  1162         for (uint k = 0; k < use->req(); k++) {
  1163           Node* n = use->in(k);
  1164           if (def == n) {
  1165             if (!is_vector_use(use, k)) {
  1166               return false;
  1173   return true;
  1176 //------------------------------schedule---------------------------
  1177 // Adjust the memory graph for the packed operations
  1178 void SuperWord::schedule() {
  1180   // Co-locate in the memory graph the members of each memory pack
  1181   for (int i = 0; i < _packset.length(); i++) {
  1182     co_locate_pack(_packset.at(i));
  1186 //-------------------------------remove_and_insert-------------------
  1187 // Remove "current" from its current position in the memory graph and insert
  1188 // it after the appropriate insertion point (lip or uip).
  1189 void SuperWord::remove_and_insert(MemNode *current, MemNode *prev, MemNode *lip,
  1190                                   Node *uip, Unique_Node_List &sched_before) {
  1191   Node* my_mem = current->in(MemNode::Memory);
  1192   bool sched_up = sched_before.member(current);
  1194   // remove current_store from its current position in the memmory graph
  1195   for (DUIterator i = current->outs(); current->has_out(i); i++) {
  1196     Node* use = current->out(i);
  1197     if (use->is_Mem()) {
  1198       assert(use->in(MemNode::Memory) == current, "must be");
  1199       if (use == prev) { // connect prev to my_mem
  1200           _igvn.replace_input_of(use, MemNode::Memory, my_mem);
  1201           --i; //deleted this edge; rescan position
  1202       } else if (sched_before.member(use)) {
  1203         if (!sched_up) { // Will be moved together with current
  1204           _igvn.replace_input_of(use, MemNode::Memory, uip);
  1205           --i; //deleted this edge; rescan position
  1207       } else {
  1208         if (sched_up) { // Will be moved together with current
  1209           _igvn.replace_input_of(use, MemNode::Memory, lip);
  1210           --i; //deleted this edge; rescan position
  1216   Node *insert_pt =  sched_up ?  uip : lip;
  1218   // all uses of insert_pt's memory state should use current's instead
  1219   for (DUIterator i = insert_pt->outs(); insert_pt->has_out(i); i++) {
  1220     Node* use = insert_pt->out(i);
  1221     if (use->is_Mem()) {
  1222       assert(use->in(MemNode::Memory) == insert_pt, "must be");
  1223       _igvn.replace_input_of(use, MemNode::Memory, current);
  1224       --i; //deleted this edge; rescan position
  1225     } else if (!sched_up && use->is_Phi() && use->bottom_type() == Type::MEMORY) {
  1226       uint pos; //lip (lower insert point) must be the last one in the memory slice
  1227       for (pos=1; pos < use->req(); pos++) {
  1228         if (use->in(pos) == insert_pt) break;
  1230       _igvn.replace_input_of(use, pos, current);
  1231       --i;
  1235   //connect current to insert_pt
  1236   _igvn.replace_input_of(current, MemNode::Memory, insert_pt);
  1239 //------------------------------co_locate_pack----------------------------------
  1240 // To schedule a store pack, we need to move any sandwiched memory ops either before
  1241 // or after the pack, based upon dependence information:
  1242 // (1) If any store in the pack depends on the sandwiched memory op, the
  1243 //     sandwiched memory op must be scheduled BEFORE the pack;
  1244 // (2) If a sandwiched memory op depends on any store in the pack, the
  1245 //     sandwiched memory op must be scheduled AFTER the pack;
  1246 // (3) If a sandwiched memory op (say, memA) depends on another sandwiched
  1247 //     memory op (say memB), memB must be scheduled before memA. So, if memA is
  1248 //     scheduled before the pack, memB must also be scheduled before the pack;
  1249 // (4) If there is no dependence restriction for a sandwiched memory op, we simply
  1250 //     schedule this store AFTER the pack
  1251 // (5) We know there is no dependence cycle, so there in no other case;
  1252 // (6) Finally, all memory ops in another single pack should be moved in the same direction.
  1253 //
  1254 // To schedule a load pack, we use the memory state of either the first or the last load in
  1255 // the pack, based on the dependence constraint.
  1256 void SuperWord::co_locate_pack(Node_List* pk) {
  1257   if (pk->at(0)->is_Store()) {
  1258     MemNode* first     = executed_first(pk)->as_Mem();
  1259     MemNode* last      = executed_last(pk)->as_Mem();
  1260     Unique_Node_List schedule_before_pack;
  1261     Unique_Node_List memops;
  1263     MemNode* current   = last->in(MemNode::Memory)->as_Mem();
  1264     MemNode* previous  = last;
  1265     while (true) {
  1266       assert(in_bb(current), "stay in block");
  1267       memops.push(previous);
  1268       for (DUIterator i = current->outs(); current->has_out(i); i++) {
  1269         Node* use = current->out(i);
  1270         if (use->is_Mem() && use != previous)
  1271           memops.push(use);
  1273       if (current == first) break;
  1274       previous = current;
  1275       current  = current->in(MemNode::Memory)->as_Mem();
  1278     // determine which memory operations should be scheduled before the pack
  1279     for (uint i = 1; i < memops.size(); i++) {
  1280       Node *s1 = memops.at(i);
  1281       if (!in_pack(s1, pk) && !schedule_before_pack.member(s1)) {
  1282         for (uint j = 0; j< i; j++) {
  1283           Node *s2 = memops.at(j);
  1284           if (!independent(s1, s2)) {
  1285             if (in_pack(s2, pk) || schedule_before_pack.member(s2)) {
  1286               schedule_before_pack.push(s1); // s1 must be scheduled before
  1287               Node_List* mem_pk = my_pack(s1);
  1288               if (mem_pk != NULL) {
  1289                 for (uint ii = 0; ii < mem_pk->size(); ii++) {
  1290                   Node* s = mem_pk->at(ii);  // follow partner
  1291                   if (memops.member(s) && !schedule_before_pack.member(s))
  1292                     schedule_before_pack.push(s);
  1295               break;
  1302     Node*    upper_insert_pt = first->in(MemNode::Memory);
  1303     // Following code moves loads connected to upper_insert_pt below aliased stores.
  1304     // Collect such loads here and reconnect them back to upper_insert_pt later.
  1305     memops.clear();
  1306     for (DUIterator i = upper_insert_pt->outs(); upper_insert_pt->has_out(i); i++) {
  1307       Node* use = upper_insert_pt->out(i);
  1308       if (use->is_Mem() && !use->is_Store()) {
  1309         memops.push(use);
  1313     MemNode* lower_insert_pt = last;
  1314     previous                 = last; //previous store in pk
  1315     current                  = last->in(MemNode::Memory)->as_Mem();
  1317     // start scheduling from "last" to "first"
  1318     while (true) {
  1319       assert(in_bb(current), "stay in block");
  1320       assert(in_pack(previous, pk), "previous stays in pack");
  1321       Node* my_mem = current->in(MemNode::Memory);
  1323       if (in_pack(current, pk)) {
  1324         // Forward users of my memory state (except "previous) to my input memory state
  1325         for (DUIterator i = current->outs(); current->has_out(i); i++) {
  1326           Node* use = current->out(i);
  1327           if (use->is_Mem() && use != previous) {
  1328             assert(use->in(MemNode::Memory) == current, "must be");
  1329             if (schedule_before_pack.member(use)) {
  1330               _igvn.replace_input_of(use, MemNode::Memory, upper_insert_pt);
  1331             } else {
  1332               _igvn.replace_input_of(use, MemNode::Memory, lower_insert_pt);
  1334             --i; // deleted this edge; rescan position
  1337         previous = current;
  1338       } else { // !in_pack(current, pk) ==> a sandwiched store
  1339         remove_and_insert(current, previous, lower_insert_pt, upper_insert_pt, schedule_before_pack);
  1342       if (current == first) break;
  1343       current = my_mem->as_Mem();
  1344     } // end while
  1346     // Reconnect loads back to upper_insert_pt.
  1347     for (uint i = 0; i < memops.size(); i++) {
  1348       Node *ld = memops.at(i);
  1349       if (ld->in(MemNode::Memory) != upper_insert_pt) {
  1350         _igvn.replace_input_of(ld, MemNode::Memory, upper_insert_pt);
  1353   } else if (pk->at(0)->is_Load()) { //load
  1354     // all loads in the pack should have the same memory state. By default,
  1355     // we use the memory state of the last load. However, if any load could
  1356     // not be moved down due to the dependence constraint, we use the memory
  1357     // state of the first load.
  1358     Node* last_mem  = executed_last(pk)->in(MemNode::Memory);
  1359     Node* first_mem = executed_first(pk)->in(MemNode::Memory);
  1360     bool schedule_last = true;
  1361     for (uint i = 0; i < pk->size(); i++) {
  1362       Node* ld = pk->at(i);
  1363       for (Node* current = last_mem; current != ld->in(MemNode::Memory);
  1364            current=current->in(MemNode::Memory)) {
  1365         assert(current != first_mem, "corrupted memory graph");
  1366         if(current->is_Mem() && !independent(current, ld)){
  1367           schedule_last = false; // a later store depends on this load
  1368           break;
  1373     Node* mem_input = schedule_last ? last_mem : first_mem;
  1374     _igvn.hash_delete(mem_input);
  1375     // Give each load the same memory state
  1376     for (uint i = 0; i < pk->size(); i++) {
  1377       LoadNode* ld = pk->at(i)->as_Load();
  1378       _igvn.replace_input_of(ld, MemNode::Memory, mem_input);
  1383 //------------------------------output---------------------------
  1384 // Convert packs into vector node operations
  1385 void SuperWord::output() {
  1386   if (_packset.length() == 0) return;
  1388 #ifndef PRODUCT
  1389   if (TraceLoopOpts) {
  1390     tty->print("SuperWord    ");
  1391     lpt()->dump_head();
  1393 #endif
  1395   // MUST ENSURE main loop's initial value is properly aligned:
  1396   //  (iv_initial_value + min_iv_offset) % vector_width_in_bytes() == 0
  1398   align_initial_loop_index(align_to_ref());
  1400   // Insert extract (unpack) operations for scalar uses
  1401   for (int i = 0; i < _packset.length(); i++) {
  1402     insert_extracts(_packset.at(i));
  1405   Compile* C = _phase->C;
  1406   uint max_vlen_in_bytes = 0;
  1407   for (int i = 0; i < _block.length(); i++) {
  1408     Node* n = _block.at(i);
  1409     Node_List* p = my_pack(n);
  1410     if (p && n == executed_last(p)) {
  1411       uint vlen = p->size();
  1412       uint vlen_in_bytes = 0;
  1413       Node* vn = NULL;
  1414       Node* low_adr = p->at(0);
  1415       Node* first   = executed_first(p);
  1416       int   opc = n->Opcode();
  1417       if (n->is_Load()) {
  1418         Node* ctl = n->in(MemNode::Control);
  1419         Node* mem = first->in(MemNode::Memory);
  1420         SWPointer p1(n->as_Mem(), this);
  1421         // Identify the memory dependency for the new loadVector node by
  1422         // walking up through memory chain.
  1423         // This is done to give flexibility to the new loadVector node so that
  1424         // it can move above independent storeVector nodes.
  1425         while (mem->is_StoreVector()) {
  1426           SWPointer p2(mem->as_Mem(), this);
  1427           int cmp = p1.cmp(p2);
  1428           if (SWPointer::not_equal(cmp) || !SWPointer::comparable(cmp)) {
  1429             mem = mem->in(MemNode::Memory);
  1430           } else {
  1431             break; // dependent memory
  1434         Node* adr = low_adr->in(MemNode::Address);
  1435         const TypePtr* atyp = n->adr_type();
  1436         vn = LoadVectorNode::make(C, opc, ctl, mem, adr, atyp, vlen, velt_basic_type(n), control_dependency(p));
  1437         vlen_in_bytes = vn->as_LoadVector()->memory_size();
  1438       } else if (n->is_Store()) {
  1439         // Promote value to be stored to vector
  1440         Node* val = vector_opd(p, MemNode::ValueIn);
  1441         Node* ctl = n->in(MemNode::Control);
  1442         Node* mem = first->in(MemNode::Memory);
  1443         Node* adr = low_adr->in(MemNode::Address);
  1444         const TypePtr* atyp = n->adr_type();
  1445         vn = StoreVectorNode::make(C, opc, ctl, mem, adr, atyp, val, vlen);
  1446         vlen_in_bytes = vn->as_StoreVector()->memory_size();
  1447       } else if (n->req() == 3) {
  1448         // Promote operands to vector
  1449         Node* in1 = vector_opd(p, 1);
  1450         Node* in2 = vector_opd(p, 2);
  1451         if (VectorNode::is_invariant_vector(in1) && (n->is_Add() || n->is_Mul())) {
  1452           // Move invariant vector input into second position to avoid register spilling.
  1453           Node* tmp = in1;
  1454           in1 = in2;
  1455           in2 = tmp;
  1457         vn = VectorNode::make(C, opc, in1, in2, vlen, velt_basic_type(n));
  1458         vlen_in_bytes = vn->as_Vector()->length_in_bytes();
  1459       } else {
  1460         ShouldNotReachHere();
  1462       assert(vn != NULL, "sanity");
  1463       _igvn.register_new_node_with_optimizer(vn);
  1464       _phase->set_ctrl(vn, _phase->get_ctrl(p->at(0)));
  1465       for (uint j = 0; j < p->size(); j++) {
  1466         Node* pm = p->at(j);
  1467         _igvn.replace_node(pm, vn);
  1469       _igvn._worklist.push(vn);
  1471       if (vlen_in_bytes > max_vlen_in_bytes) {
  1472         max_vlen_in_bytes = vlen_in_bytes;
  1474 #ifdef ASSERT
  1475       if (TraceNewVectors) {
  1476         tty->print("new Vector node: ");
  1477         vn->dump();
  1479 #endif
  1482   C->set_max_vector_size(max_vlen_in_bytes);
  1485 //------------------------------vector_opd---------------------------
  1486 // Create a vector operand for the nodes in pack p for operand: in(opd_idx)
  1487 Node* SuperWord::vector_opd(Node_List* p, int opd_idx) {
  1488   Node* p0 = p->at(0);
  1489   uint vlen = p->size();
  1490   Node* opd = p0->in(opd_idx);
  1492   if (same_inputs(p, opd_idx)) {
  1493     if (opd->is_Vector() || opd->is_LoadVector()) {
  1494       assert(((opd_idx != 2) || !VectorNode::is_shift(p0)), "shift's count can't be vector");
  1495       return opd; // input is matching vector
  1497     if ((opd_idx == 2) && VectorNode::is_shift(p0)) {
  1498       Compile* C = _phase->C;
  1499       Node* cnt = opd;
  1500       // Vector instructions do not mask shift count, do it here.
  1501       juint mask = (p0->bottom_type() == TypeInt::INT) ? (BitsPerInt - 1) : (BitsPerLong - 1);
  1502       const TypeInt* t = opd->find_int_type();
  1503       if (t != NULL && t->is_con()) {
  1504         juint shift = t->get_con();
  1505         if (shift > mask) { // Unsigned cmp
  1506           cnt = ConNode::make(C, TypeInt::make(shift & mask));
  1508       } else {
  1509         if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {
  1510           cnt = ConNode::make(C, TypeInt::make(mask));
  1511           _igvn.register_new_node_with_optimizer(cnt);
  1512           cnt = new (C) AndINode(opd, cnt);
  1513           _igvn.register_new_node_with_optimizer(cnt);
  1514           _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
  1516         assert(opd->bottom_type()->isa_int(), "int type only");
  1517         // Move non constant shift count into vector register.
  1518         cnt = VectorNode::shift_count(C, p0, cnt, vlen, velt_basic_type(p0));
  1520       if (cnt != opd) {
  1521         _igvn.register_new_node_with_optimizer(cnt);
  1522         _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
  1524       return cnt;
  1526     assert(!opd->is_StoreVector(), "such vector is not expected here");
  1527     // Convert scalar input to vector with the same number of elements as
  1528     // p0's vector. Use p0's type because size of operand's container in
  1529     // vector should match p0's size regardless operand's size.
  1530     const Type* p0_t = velt_type(p0);
  1531     VectorNode* vn = VectorNode::scalar2vector(_phase->C, opd, vlen, p0_t);
  1533     _igvn.register_new_node_with_optimizer(vn);
  1534     _phase->set_ctrl(vn, _phase->get_ctrl(opd));
  1535 #ifdef ASSERT
  1536     if (TraceNewVectors) {
  1537       tty->print("new Vector node: ");
  1538       vn->dump();
  1540 #endif
  1541     return vn;
  1544   // Insert pack operation
  1545   BasicType bt = velt_basic_type(p0);
  1546   PackNode* pk = PackNode::make(_phase->C, opd, vlen, bt);
  1547   DEBUG_ONLY( const BasicType opd_bt = opd->bottom_type()->basic_type(); )
  1549   for (uint i = 1; i < vlen; i++) {
  1550     Node* pi = p->at(i);
  1551     Node* in = pi->in(opd_idx);
  1552     assert(my_pack(in) == NULL, "Should already have been unpacked");
  1553     assert(opd_bt == in->bottom_type()->basic_type(), "all same type");
  1554     pk->add_opd(in);
  1556   _igvn.register_new_node_with_optimizer(pk);
  1557   _phase->set_ctrl(pk, _phase->get_ctrl(opd));
  1558 #ifdef ASSERT
  1559   if (TraceNewVectors) {
  1560     tty->print("new Vector node: ");
  1561     pk->dump();
  1563 #endif
  1564   return pk;
  1567 //------------------------------insert_extracts---------------------------
  1568 // If a use of pack p is not a vector use, then replace the
  1569 // use with an extract operation.
  1570 void SuperWord::insert_extracts(Node_List* p) {
  1571   if (p->at(0)->is_Store()) return;
  1572   assert(_n_idx_list.is_empty(), "empty (node,index) list");
  1574   // Inspect each use of each pack member.  For each use that is
  1575   // not a vector use, replace the use with an extract operation.
  1577   for (uint i = 0; i < p->size(); i++) {
  1578     Node* def = p->at(i);
  1579     for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
  1580       Node* use = def->fast_out(j);
  1581       for (uint k = 0; k < use->req(); k++) {
  1582         Node* n = use->in(k);
  1583         if (def == n) {
  1584           if (!is_vector_use(use, k)) {
  1585             _n_idx_list.push(use, k);
  1592   while (_n_idx_list.is_nonempty()) {
  1593     Node* use = _n_idx_list.node();
  1594     int   idx = _n_idx_list.index();
  1595     _n_idx_list.pop();
  1596     Node* def = use->in(idx);
  1598     // Insert extract operation
  1599     _igvn.hash_delete(def);
  1600     int def_pos = alignment(def) / data_size(def);
  1602     Node* ex = ExtractNode::make(_phase->C, def, def_pos, velt_basic_type(def));
  1603     _igvn.register_new_node_with_optimizer(ex);
  1604     _phase->set_ctrl(ex, _phase->get_ctrl(def));
  1605     _igvn.replace_input_of(use, idx, ex);
  1606     _igvn._worklist.push(def);
  1608     bb_insert_after(ex, bb_idx(def));
  1609     set_velt_type(ex, velt_type(def));
  1613 //------------------------------is_vector_use---------------------------
  1614 // Is use->in(u_idx) a vector use?
  1615 bool SuperWord::is_vector_use(Node* use, int u_idx) {
  1616   Node_List* u_pk = my_pack(use);
  1617   if (u_pk == NULL) return false;
  1618   Node* def = use->in(u_idx);
  1619   Node_List* d_pk = my_pack(def);
  1620   if (d_pk == NULL) {
  1621     // check for scalar promotion
  1622     Node* n = u_pk->at(0)->in(u_idx);
  1623     for (uint i = 1; i < u_pk->size(); i++) {
  1624       if (u_pk->at(i)->in(u_idx) != n) return false;
  1626     return true;
  1628   if (u_pk->size() != d_pk->size())
  1629     return false;
  1630   for (uint i = 0; i < u_pk->size(); i++) {
  1631     Node* ui = u_pk->at(i);
  1632     Node* di = d_pk->at(i);
  1633     if (ui->in(u_idx) != di || alignment(ui) != alignment(di))
  1634       return false;
  1636   return true;
  1639 //------------------------------construct_bb---------------------------
  1640 // Construct reverse postorder list of block members
  1641 bool SuperWord::construct_bb() {
  1642   Node* entry = bb();
  1644   assert(_stk.length() == 0,            "stk is empty");
  1645   assert(_block.length() == 0,          "block is empty");
  1646   assert(_data_entry.length() == 0,     "data_entry is empty");
  1647   assert(_mem_slice_head.length() == 0, "mem_slice_head is empty");
  1648   assert(_mem_slice_tail.length() == 0, "mem_slice_tail is empty");
  1650   // Find non-control nodes with no inputs from within block,
  1651   // create a temporary map from node _idx to bb_idx for use
  1652   // by the visited and post_visited sets,
  1653   // and count number of nodes in block.
  1654   int bb_ct = 0;
  1655   for (uint i = 0; i < lpt()->_body.size(); i++ ) {
  1656     Node *n = lpt()->_body.at(i);
  1657     set_bb_idx(n, i); // Create a temporary map
  1658     if (in_bb(n)) {
  1659       if (n->is_LoadStore() || n->is_MergeMem() ||
  1660           (n->is_Proj() && !n->as_Proj()->is_CFG())) {
  1661         // Bailout if the loop has LoadStore, MergeMem or data Proj
  1662         // nodes. Superword optimization does not work with them.
  1663         return false;
  1665       bb_ct++;
  1666       if (!n->is_CFG()) {
  1667         bool found = false;
  1668         for (uint j = 0; j < n->req(); j++) {
  1669           Node* def = n->in(j);
  1670           if (def && in_bb(def)) {
  1671             found = true;
  1672             break;
  1675         if (!found) {
  1676           assert(n != entry, "can't be entry");
  1677           _data_entry.push(n);
  1683   // Find memory slices (head and tail)
  1684   for (DUIterator_Fast imax, i = lp()->fast_outs(imax); i < imax; i++) {
  1685     Node *n = lp()->fast_out(i);
  1686     if (in_bb(n) && (n->is_Phi() && n->bottom_type() == Type::MEMORY)) {
  1687       Node* n_tail  = n->in(LoopNode::LoopBackControl);
  1688       if (n_tail != n->in(LoopNode::EntryControl)) {
  1689         if (!n_tail->is_Mem()) {
  1690           assert(n_tail->is_Mem(), err_msg_res("unexpected node for memory slice: %s", n_tail->Name()));
  1691           return false; // Bailout
  1693         _mem_slice_head.push(n);
  1694         _mem_slice_tail.push(n_tail);
  1699   // Create an RPO list of nodes in block
  1701   visited_clear();
  1702   post_visited_clear();
  1704   // Push all non-control nodes with no inputs from within block, then control entry
  1705   for (int j = 0; j < _data_entry.length(); j++) {
  1706     Node* n = _data_entry.at(j);
  1707     visited_set(n);
  1708     _stk.push(n);
  1710   visited_set(entry);
  1711   _stk.push(entry);
  1713   // Do a depth first walk over out edges
  1714   int rpo_idx = bb_ct - 1;
  1715   int size;
  1716   while ((size = _stk.length()) > 0) {
  1717     Node* n = _stk.top(); // Leave node on stack
  1718     if (!visited_test_set(n)) {
  1719       // forward arc in graph
  1720     } else if (!post_visited_test(n)) {
  1721       // cross or back arc
  1722       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1723         Node *use = n->fast_out(i);
  1724         if (in_bb(use) && !visited_test(use) &&
  1725             // Don't go around backedge
  1726             (!use->is_Phi() || n == entry)) {
  1727           _stk.push(use);
  1730       if (_stk.length() == size) {
  1731         // There were no additional uses, post visit node now
  1732         _stk.pop(); // Remove node from stack
  1733         assert(rpo_idx >= 0, "");
  1734         _block.at_put_grow(rpo_idx, n);
  1735         rpo_idx--;
  1736         post_visited_set(n);
  1737         assert(rpo_idx >= 0 || _stk.is_empty(), "");
  1739     } else {
  1740       _stk.pop(); // Remove post-visited node from stack
  1744   // Create real map of block indices for nodes
  1745   for (int j = 0; j < _block.length(); j++) {
  1746     Node* n = _block.at(j);
  1747     set_bb_idx(n, j);
  1750   initialize_bb(); // Ensure extra info is allocated.
  1752 #ifndef PRODUCT
  1753   if (TraceSuperWord) {
  1754     print_bb();
  1755     tty->print_cr("\ndata entry nodes: %s", _data_entry.length() > 0 ? "" : "NONE");
  1756     for (int m = 0; m < _data_entry.length(); m++) {
  1757       tty->print("%3d ", m);
  1758       _data_entry.at(m)->dump();
  1760     tty->print_cr("\nmemory slices: %s", _mem_slice_head.length() > 0 ? "" : "NONE");
  1761     for (int m = 0; m < _mem_slice_head.length(); m++) {
  1762       tty->print("%3d ", m); _mem_slice_head.at(m)->dump();
  1763       tty->print("    ");    _mem_slice_tail.at(m)->dump();
  1766 #endif
  1767   assert(rpo_idx == -1 && bb_ct == _block.length(), "all block members found");
  1768   return (_mem_slice_head.length() > 0) || (_data_entry.length() > 0);
  1771 //------------------------------initialize_bb---------------------------
  1772 // Initialize per node info
  1773 void SuperWord::initialize_bb() {
  1774   Node* last = _block.at(_block.length() - 1);
  1775   grow_node_info(bb_idx(last));
  1778 //------------------------------bb_insert_after---------------------------
  1779 // Insert n into block after pos
  1780 void SuperWord::bb_insert_after(Node* n, int pos) {
  1781   int n_pos = pos + 1;
  1782   // Make room
  1783   for (int i = _block.length() - 1; i >= n_pos; i--) {
  1784     _block.at_put_grow(i+1, _block.at(i));
  1786   for (int j = _node_info.length() - 1; j >= n_pos; j--) {
  1787     _node_info.at_put_grow(j+1, _node_info.at(j));
  1789   // Set value
  1790   _block.at_put_grow(n_pos, n);
  1791   _node_info.at_put_grow(n_pos, SWNodeInfo::initial);
  1792   // Adjust map from node->_idx to _block index
  1793   for (int i = n_pos; i < _block.length(); i++) {
  1794     set_bb_idx(_block.at(i), i);
  1798 //------------------------------compute_max_depth---------------------------
  1799 // Compute max depth for expressions from beginning of block
  1800 // Use to prune search paths during test for independence.
  1801 void SuperWord::compute_max_depth() {
  1802   int ct = 0;
  1803   bool again;
  1804   do {
  1805     again = false;
  1806     for (int i = 0; i < _block.length(); i++) {
  1807       Node* n = _block.at(i);
  1808       if (!n->is_Phi()) {
  1809         int d_orig = depth(n);
  1810         int d_in   = 0;
  1811         for (DepPreds preds(n, _dg); !preds.done(); preds.next()) {
  1812           Node* pred = preds.current();
  1813           if (in_bb(pred)) {
  1814             d_in = MAX2(d_in, depth(pred));
  1817         if (d_in + 1 != d_orig) {
  1818           set_depth(n, d_in + 1);
  1819           again = true;
  1823     ct++;
  1824   } while (again);
  1825 #ifndef PRODUCT
  1826   if (TraceSuperWord && Verbose)
  1827     tty->print_cr("compute_max_depth iterated: %d times", ct);
  1828 #endif
  1831 //-------------------------compute_vector_element_type-----------------------
  1832 // Compute necessary vector element type for expressions
  1833 // This propagates backwards a narrower integer type when the
  1834 // upper bits of the value are not needed.
  1835 // Example:  char a,b,c;  a = b + c;
  1836 // Normally the type of the add is integer, but for packed character
  1837 // operations the type of the add needs to be char.
  1838 void SuperWord::compute_vector_element_type() {
  1839 #ifndef PRODUCT
  1840   if (TraceSuperWord && Verbose)
  1841     tty->print_cr("\ncompute_velt_type:");
  1842 #endif
  1844   // Initial type
  1845   for (int i = 0; i < _block.length(); i++) {
  1846     Node* n = _block.at(i);
  1847     set_velt_type(n, container_type(n));
  1850   // Propagate integer narrowed type backwards through operations
  1851   // that don't depend on higher order bits
  1852   for (int i = _block.length() - 1; i >= 0; i--) {
  1853     Node* n = _block.at(i);
  1854     // Only integer types need be examined
  1855     const Type* vtn = velt_type(n);
  1856     if (vtn->basic_type() == T_INT) {
  1857       uint start, end;
  1858       VectorNode::vector_operands(n, &start, &end);
  1860       for (uint j = start; j < end; j++) {
  1861         Node* in  = n->in(j);
  1862         // Don't propagate through a memory
  1863         if (!in->is_Mem() && in_bb(in) && velt_type(in)->basic_type() == T_INT &&
  1864             data_size(n) < data_size(in)) {
  1865           bool same_type = true;
  1866           for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) {
  1867             Node *use = in->fast_out(k);
  1868             if (!in_bb(use) || !same_velt_type(use, n)) {
  1869               same_type = false;
  1870               break;
  1873           if (same_type) {
  1874             // For right shifts of small integer types (bool, byte, char, short)
  1875             // we need precise information about sign-ness. Only Load nodes have
  1876             // this information because Store nodes are the same for signed and
  1877             // unsigned values. And any arithmetic operation after a load may
  1878             // expand a value to signed Int so such right shifts can't be used
  1879             // because vector elements do not have upper bits of Int.
  1880             const Type* vt = vtn;
  1881             if (VectorNode::is_shift(in)) {
  1882               Node* load = in->in(1);
  1883               if (load->is_Load() && in_bb(load) && (velt_type(load)->basic_type() == T_INT)) {
  1884                 vt = velt_type(load);
  1885               } else if (in->Opcode() != Op_LShiftI) {
  1886                 // Widen type to Int to avoid creation of right shift vector
  1887                 // (align + data_size(s1) check in stmts_can_pack() will fail).
  1888                 // Note, left shifts work regardless type.
  1889                 vt = TypeInt::INT;
  1892             set_velt_type(in, vt);
  1898 #ifndef PRODUCT
  1899   if (TraceSuperWord && Verbose) {
  1900     for (int i = 0; i < _block.length(); i++) {
  1901       Node* n = _block.at(i);
  1902       velt_type(n)->dump();
  1903       tty->print("\t");
  1904       n->dump();
  1907 #endif
  1910 //------------------------------memory_alignment---------------------------
  1911 // Alignment within a vector memory reference
  1912 int SuperWord::memory_alignment(MemNode* s, int iv_adjust) {
  1913   SWPointer p(s, this);
  1914   if (!p.valid()) {
  1915     return bottom_align;
  1917   int vw = vector_width_in_bytes(s);
  1918   if (vw < 2) {
  1919     return bottom_align; // No vectors for this type
  1921   int offset  = p.offset_in_bytes();
  1922   offset     += iv_adjust*p.memory_size();
  1923   int off_rem = offset % vw;
  1924   int off_mod = off_rem >= 0 ? off_rem : off_rem + vw;
  1925   return off_mod;
  1928 //---------------------------container_type---------------------------
  1929 // Smallest type containing range of values
  1930 const Type* SuperWord::container_type(Node* n) {
  1931   if (n->is_Mem()) {
  1932     BasicType bt = n->as_Mem()->memory_type();
  1933     if (n->is_Store() && (bt == T_CHAR)) {
  1934       // Use T_SHORT type instead of T_CHAR for stored values because any
  1935       // preceding arithmetic operation extends values to signed Int.
  1936       bt = T_SHORT;
  1938     if (n->Opcode() == Op_LoadUB) {
  1939       // Adjust type for unsigned byte loads, it is important for right shifts.
  1940       // T_BOOLEAN is used because there is no basic type representing type
  1941       // TypeInt::UBYTE. Use of T_BOOLEAN for vectors is fine because only
  1942       // size (one byte) and sign is important.
  1943       bt = T_BOOLEAN;
  1945     return Type::get_const_basic_type(bt);
  1947   const Type* t = _igvn.type(n);
  1948   if (t->basic_type() == T_INT) {
  1949     // A narrow type of arithmetic operations will be determined by
  1950     // propagating the type of memory operations.
  1951     return TypeInt::INT;
  1953   return t;
  1956 bool SuperWord::same_velt_type(Node* n1, Node* n2) {
  1957   const Type* vt1 = velt_type(n1);
  1958   const Type* vt2 = velt_type(n2);
  1959   if (vt1->basic_type() == T_INT && vt2->basic_type() == T_INT) {
  1960     // Compare vectors element sizes for integer types.
  1961     return data_size(n1) == data_size(n2);
  1963   return vt1 == vt2;
  1966 //------------------------------in_packset---------------------------
  1967 // Are s1 and s2 in a pack pair and ordered as s1,s2?
  1968 bool SuperWord::in_packset(Node* s1, Node* s2) {
  1969   for (int i = 0; i < _packset.length(); i++) {
  1970     Node_List* p = _packset.at(i);
  1971     assert(p->size() == 2, "must be");
  1972     if (p->at(0) == s1 && p->at(p->size()-1) == s2) {
  1973       return true;
  1976   return false;
  1979 //------------------------------in_pack---------------------------
  1980 // Is s in pack p?
  1981 Node_List* SuperWord::in_pack(Node* s, Node_List* p) {
  1982   for (uint i = 0; i < p->size(); i++) {
  1983     if (p->at(i) == s) {
  1984       return p;
  1987   return NULL;
  1990 //------------------------------remove_pack_at---------------------------
  1991 // Remove the pack at position pos in the packset
  1992 void SuperWord::remove_pack_at(int pos) {
  1993   Node_List* p = _packset.at(pos);
  1994   for (uint i = 0; i < p->size(); i++) {
  1995     Node* s = p->at(i);
  1996     set_my_pack(s, NULL);
  1998   _packset.remove_at(pos);
  2001 //------------------------------executed_first---------------------------
  2002 // Return the node executed first in pack p.  Uses the RPO block list
  2003 // to determine order.
  2004 Node* SuperWord::executed_first(Node_List* p) {
  2005   Node* n = p->at(0);
  2006   int n_rpo = bb_idx(n);
  2007   for (uint i = 1; i < p->size(); i++) {
  2008     Node* s = p->at(i);
  2009     int s_rpo = bb_idx(s);
  2010     if (s_rpo < n_rpo) {
  2011       n = s;
  2012       n_rpo = s_rpo;
  2015   return n;
  2018 //------------------------------executed_last---------------------------
  2019 // Return the node executed last in pack p.
  2020 Node* SuperWord::executed_last(Node_List* p) {
  2021   Node* n = p->at(0);
  2022   int n_rpo = bb_idx(n);
  2023   for (uint i = 1; i < p->size(); i++) {
  2024     Node* s = p->at(i);
  2025     int s_rpo = bb_idx(s);
  2026     if (s_rpo > n_rpo) {
  2027       n = s;
  2028       n_rpo = s_rpo;
  2031   return n;
  2034 LoadNode::ControlDependency SuperWord::control_dependency(Node_List* p) {
  2035   LoadNode::ControlDependency dep = LoadNode::DependsOnlyOnTest;
  2036   for (uint i = 0; i < p->size(); i++) {
  2037     Node* n = p->at(i);
  2038     assert(n->is_Load(), "only meaningful for loads");
  2039     if (!n->depends_only_on_test()) {
  2040       dep = LoadNode::Pinned;
  2043   return dep;
  2047 //----------------------------align_initial_loop_index---------------------------
  2048 // Adjust pre-loop limit so that in main loop, a load/store reference
  2049 // to align_to_ref will be a position zero in the vector.
  2050 //   (iv + k) mod vector_align == 0
  2051 void SuperWord::align_initial_loop_index(MemNode* align_to_ref) {
  2052   CountedLoopNode *main_head = lp()->as_CountedLoop();
  2053   assert(main_head->is_main_loop(), "");
  2054   CountedLoopEndNode* pre_end = get_pre_loop_end(main_head);
  2055   assert(pre_end != NULL, "");
  2056   Node *pre_opaq1 = pre_end->limit();
  2057   assert(pre_opaq1->Opcode() == Op_Opaque1, "");
  2058   Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
  2059   Node *lim0 = pre_opaq->in(1);
  2061   // Where we put new limit calculations
  2062   Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
  2064   // Ensure the original loop limit is available from the
  2065   // pre-loop Opaque1 node.
  2066   Node *orig_limit = pre_opaq->original_loop_limit();
  2067   assert(orig_limit != NULL && _igvn.type(orig_limit) != Type::TOP, "");
  2069   SWPointer align_to_ref_p(align_to_ref, this);
  2070   assert(align_to_ref_p.valid(), "sanity");
  2072   // Given:
  2073   //     lim0 == original pre loop limit
  2074   //     V == v_align (power of 2)
  2075   //     invar == extra invariant piece of the address expression
  2076   //     e == offset [ +/- invar ]
  2077   //
  2078   // When reassociating expressions involving '%' the basic rules are:
  2079   //     (a - b) % k == 0   =>  a % k == b % k
  2080   // and:
  2081   //     (a + b) % k == 0   =>  a % k == (k - b) % k
  2082   //
  2083   // For stride > 0 && scale > 0,
  2084   //   Derive the new pre-loop limit "lim" such that the two constraints:
  2085   //     (1) lim = lim0 + N           (where N is some positive integer < V)
  2086   //     (2) (e + lim) % V == 0
  2087   //   are true.
  2088   //
  2089   //   Substituting (1) into (2),
  2090   //     (e + lim0 + N) % V == 0
  2091   //   solve for N:
  2092   //     N = (V - (e + lim0)) % V
  2093   //   substitute back into (1), so that new limit
  2094   //     lim = lim0 + (V - (e + lim0)) % V
  2095   //
  2096   // For stride > 0 && scale < 0
  2097   //   Constraints:
  2098   //     lim = lim0 + N
  2099   //     (e - lim) % V == 0
  2100   //   Solving for lim:
  2101   //     (e - lim0 - N) % V == 0
  2102   //     N = (e - lim0) % V
  2103   //     lim = lim0 + (e - lim0) % V
  2104   //
  2105   // For stride < 0 && scale > 0
  2106   //   Constraints:
  2107   //     lim = lim0 - N
  2108   //     (e + lim) % V == 0
  2109   //   Solving for lim:
  2110   //     (e + lim0 - N) % V == 0
  2111   //     N = (e + lim0) % V
  2112   //     lim = lim0 - (e + lim0) % V
  2113   //
  2114   // For stride < 0 && scale < 0
  2115   //   Constraints:
  2116   //     lim = lim0 - N
  2117   //     (e - lim) % V == 0
  2118   //   Solving for lim:
  2119   //     (e - lim0 + N) % V == 0
  2120   //     N = (V - (e - lim0)) % V
  2121   //     lim = lim0 - (V - (e - lim0)) % V
  2123   int vw = vector_width_in_bytes(align_to_ref);
  2124   int stride   = iv_stride();
  2125   int scale    = align_to_ref_p.scale_in_bytes();
  2126   int elt_size = align_to_ref_p.memory_size();
  2127   int v_align  = vw / elt_size;
  2128   assert(v_align > 1, "sanity");
  2129   int offset   = align_to_ref_p.offset_in_bytes() / elt_size;
  2130   Node *offsn  = _igvn.intcon(offset);
  2132   Node *e = offsn;
  2133   if (align_to_ref_p.invar() != NULL) {
  2134     // incorporate any extra invariant piece producing (offset +/- invar) >>> log2(elt)
  2135     Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
  2136     Node* aref     = new (_phase->C) URShiftINode(align_to_ref_p.invar(), log2_elt);
  2137     _igvn.register_new_node_with_optimizer(aref);
  2138     _phase->set_ctrl(aref, pre_ctrl);
  2139     if (align_to_ref_p.negate_invar()) {
  2140       e = new (_phase->C) SubINode(e, aref);
  2141     } else {
  2142       e = new (_phase->C) AddINode(e, aref);
  2144     _igvn.register_new_node_with_optimizer(e);
  2145     _phase->set_ctrl(e, pre_ctrl);
  2147   if (vw > ObjectAlignmentInBytes) {
  2148     // incorporate base e +/- base && Mask >>> log2(elt)
  2149     Node* xbase = new(_phase->C) CastP2XNode(NULL, align_to_ref_p.base());
  2150     _igvn.register_new_node_with_optimizer(xbase);
  2151 #ifdef _LP64
  2152     xbase  = new (_phase->C) ConvL2INode(xbase);
  2153     _igvn.register_new_node_with_optimizer(xbase);
  2154 #endif
  2155     Node* mask = _igvn.intcon(vw-1);
  2156     Node* masked_xbase  = new (_phase->C) AndINode(xbase, mask);
  2157     _igvn.register_new_node_with_optimizer(masked_xbase);
  2158     Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
  2159     Node* bref     = new (_phase->C) URShiftINode(masked_xbase, log2_elt);
  2160     _igvn.register_new_node_with_optimizer(bref);
  2161     _phase->set_ctrl(bref, pre_ctrl);
  2162     e = new (_phase->C) AddINode(e, bref);
  2163     _igvn.register_new_node_with_optimizer(e);
  2164     _phase->set_ctrl(e, pre_ctrl);
  2167   // compute e +/- lim0
  2168   if (scale < 0) {
  2169     e = new (_phase->C) SubINode(e, lim0);
  2170   } else {
  2171     e = new (_phase->C) AddINode(e, lim0);
  2173   _igvn.register_new_node_with_optimizer(e);
  2174   _phase->set_ctrl(e, pre_ctrl);
  2176   if (stride * scale > 0) {
  2177     // compute V - (e +/- lim0)
  2178     Node* va  = _igvn.intcon(v_align);
  2179     e = new (_phase->C) SubINode(va, e);
  2180     _igvn.register_new_node_with_optimizer(e);
  2181     _phase->set_ctrl(e, pre_ctrl);
  2183   // compute N = (exp) % V
  2184   Node* va_msk = _igvn.intcon(v_align - 1);
  2185   Node* N = new (_phase->C) AndINode(e, va_msk);
  2186   _igvn.register_new_node_with_optimizer(N);
  2187   _phase->set_ctrl(N, pre_ctrl);
  2189   //   substitute back into (1), so that new limit
  2190   //     lim = lim0 + N
  2191   Node* lim;
  2192   if (stride < 0) {
  2193     lim = new (_phase->C) SubINode(lim0, N);
  2194   } else {
  2195     lim = new (_phase->C) AddINode(lim0, N);
  2197   _igvn.register_new_node_with_optimizer(lim);
  2198   _phase->set_ctrl(lim, pre_ctrl);
  2199   Node* constrained =
  2200     (stride > 0) ? (Node*) new (_phase->C) MinINode(lim, orig_limit)
  2201                  : (Node*) new (_phase->C) MaxINode(lim, orig_limit);
  2202   _igvn.register_new_node_with_optimizer(constrained);
  2203   _phase->set_ctrl(constrained, pre_ctrl);
  2204   _igvn.hash_delete(pre_opaq);
  2205   pre_opaq->set_req(1, constrained);
  2208 //----------------------------get_pre_loop_end---------------------------
  2209 // Find pre loop end from main loop.  Returns null if none.
  2210 CountedLoopEndNode* SuperWord::get_pre_loop_end(CountedLoopNode *cl) {
  2211   Node *ctrl = cl->in(LoopNode::EntryControl);
  2212   if (!ctrl->is_IfTrue() && !ctrl->is_IfFalse()) return NULL;
  2213   Node *iffm = ctrl->in(0);
  2214   if (!iffm->is_If()) return NULL;
  2215   Node *p_f = iffm->in(0);
  2216   if (!p_f->is_IfFalse()) return NULL;
  2217   if (!p_f->in(0)->is_CountedLoopEnd()) return NULL;
  2218   CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
  2219   if (!pre_end->loopnode()->is_pre_loop()) return NULL;
  2220   return pre_end;
  2224 //------------------------------init---------------------------
  2225 void SuperWord::init() {
  2226   _dg.init();
  2227   _packset.clear();
  2228   _disjoint_ptrs.clear();
  2229   _block.clear();
  2230   _data_entry.clear();
  2231   _mem_slice_head.clear();
  2232   _mem_slice_tail.clear();
  2233   _node_info.clear();
  2234   _align_to_ref = NULL;
  2235   _lpt = NULL;
  2236   _lp = NULL;
  2237   _bb = NULL;
  2238   _iv = NULL;
  2241 //------------------------------print_packset---------------------------
  2242 void SuperWord::print_packset() {
  2243 #ifndef PRODUCT
  2244   tty->print_cr("packset");
  2245   for (int i = 0; i < _packset.length(); i++) {
  2246     tty->print_cr("Pack: %d", i);
  2247     Node_List* p = _packset.at(i);
  2248     print_pack(p);
  2250 #endif
  2253 //------------------------------print_pack---------------------------
  2254 void SuperWord::print_pack(Node_List* p) {
  2255   for (uint i = 0; i < p->size(); i++) {
  2256     print_stmt(p->at(i));
  2260 //------------------------------print_bb---------------------------
  2261 void SuperWord::print_bb() {
  2262 #ifndef PRODUCT
  2263   tty->print_cr("\nBlock");
  2264   for (int i = 0; i < _block.length(); i++) {
  2265     Node* n = _block.at(i);
  2266     tty->print("%d ", i);
  2267     if (n) {
  2268       n->dump();
  2271 #endif
  2274 //------------------------------print_stmt---------------------------
  2275 void SuperWord::print_stmt(Node* s) {
  2276 #ifndef PRODUCT
  2277   tty->print(" align: %d \t", alignment(s));
  2278   s->dump();
  2279 #endif
  2282 //------------------------------blank---------------------------
  2283 char* SuperWord::blank(uint depth) {
  2284   static char blanks[101];
  2285   assert(depth < 101, "too deep");
  2286   for (uint i = 0; i < depth; i++) blanks[i] = ' ';
  2287   blanks[depth] = '\0';
  2288   return blanks;
  2292 //==============================SWPointer===========================
  2294 //----------------------------SWPointer------------------------
  2295 SWPointer::SWPointer(MemNode* mem, SuperWord* slp) :
  2296   _mem(mem), _slp(slp),  _base(NULL),  _adr(NULL),
  2297   _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {
  2299   Node* adr = mem->in(MemNode::Address);
  2300   if (!adr->is_AddP()) {
  2301     assert(!valid(), "too complex");
  2302     return;
  2304   // Match AddP(base, AddP(ptr, k*iv [+ invariant]), constant)
  2305   Node* base = adr->in(AddPNode::Base);
  2306   // The base address should be loop invariant
  2307   if (!invariant(base)) {
  2308     assert(!valid(), "base address is loop variant");
  2309     return;
  2311   //unsafe reference could not be aligned appropriately without runtime checking
  2312   if (base == NULL || base->bottom_type() == Type::TOP) {
  2313     assert(!valid(), "unsafe access");
  2314     return;
  2316   for (int i = 0; i < 3; i++) {
  2317     if (!scaled_iv_plus_offset(adr->in(AddPNode::Offset))) {
  2318       assert(!valid(), "too complex");
  2319       return;
  2321     adr = adr->in(AddPNode::Address);
  2322     if (base == adr || !adr->is_AddP()) {
  2323       break; // stop looking at addp's
  2326   _base = base;
  2327   _adr  = adr;
  2328   assert(valid(), "Usable");
  2331 // Following is used to create a temporary object during
  2332 // the pattern match of an address expression.
  2333 SWPointer::SWPointer(SWPointer* p) :
  2334   _mem(p->_mem), _slp(p->_slp),  _base(NULL),  _adr(NULL),
  2335   _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {}
  2337 //------------------------scaled_iv_plus_offset--------------------
  2338 // Match: k*iv + offset
  2339 // where: k is a constant that maybe zero, and
  2340 //        offset is (k2 [+/- invariant]) where k2 maybe zero and invariant is optional
  2341 bool SWPointer::scaled_iv_plus_offset(Node* n) {
  2342   if (scaled_iv(n)) {
  2343     return true;
  2345   if (offset_plus_k(n)) {
  2346     return true;
  2348   int opc = n->Opcode();
  2349   if (opc == Op_AddI) {
  2350     if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2))) {
  2351       return true;
  2353     if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
  2354       return true;
  2356   } else if (opc == Op_SubI) {
  2357     if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2), true)) {
  2358       return true;
  2360     if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
  2361       _scale *= -1;
  2362       return true;
  2365   return false;
  2368 //----------------------------scaled_iv------------------------
  2369 // Match: k*iv where k is a constant that's not zero
  2370 bool SWPointer::scaled_iv(Node* n) {
  2371   if (_scale != 0) {
  2372     return false;  // already found a scale
  2374   if (n == iv()) {
  2375     _scale = 1;
  2376     return true;
  2378   int opc = n->Opcode();
  2379   if (opc == Op_MulI) {
  2380     if (n->in(1) == iv() && n->in(2)->is_Con()) {
  2381       _scale = n->in(2)->get_int();
  2382       return true;
  2383     } else if (n->in(2) == iv() && n->in(1)->is_Con()) {
  2384       _scale = n->in(1)->get_int();
  2385       return true;
  2387   } else if (opc == Op_LShiftI) {
  2388     if (n->in(1) == iv() && n->in(2)->is_Con()) {
  2389       _scale = 1 << n->in(2)->get_int();
  2390       return true;
  2392   } else if (opc == Op_ConvI2L) {
  2393     if (n->in(1)->Opcode() == Op_CastII &&
  2394         n->in(1)->as_CastII()->has_range_check()) {
  2395       // Skip range check dependent CastII nodes
  2396       n = n->in(1);
  2398     if (scaled_iv_plus_offset(n->in(1))) {
  2399       return true;
  2401   } else if (opc == Op_LShiftL) {
  2402     if (!has_iv() && _invar == NULL) {
  2403       // Need to preserve the current _offset value, so
  2404       // create a temporary object for this expression subtree.
  2405       // Hacky, so should re-engineer the address pattern match.
  2406       SWPointer tmp(this);
  2407       if (tmp.scaled_iv_plus_offset(n->in(1))) {
  2408         if (tmp._invar == NULL) {
  2409           int mult = 1 << n->in(2)->get_int();
  2410           _scale   = tmp._scale  * mult;
  2411           _offset += tmp._offset * mult;
  2412           return true;
  2417   return false;
  2420 //----------------------------offset_plus_k------------------------
  2421 // Match: offset is (k [+/- invariant])
  2422 // where k maybe zero and invariant is optional, but not both.
  2423 bool SWPointer::offset_plus_k(Node* n, bool negate) {
  2424   int opc = n->Opcode();
  2425   if (opc == Op_ConI) {
  2426     _offset += negate ? -(n->get_int()) : n->get_int();
  2427     return true;
  2428   } else if (opc == Op_ConL) {
  2429     // Okay if value fits into an int
  2430     const TypeLong* t = n->find_long_type();
  2431     if (t->higher_equal(TypeLong::INT)) {
  2432       jlong loff = n->get_long();
  2433       jint  off  = (jint)loff;
  2434       _offset += negate ? -off : loff;
  2435       return true;
  2437     return false;
  2439   if (_invar != NULL) return false; // already have an invariant
  2440   if (opc == Op_AddI) {
  2441     if (n->in(2)->is_Con() && invariant(n->in(1))) {
  2442       _negate_invar = negate;
  2443       _invar = n->in(1);
  2444       _offset += negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
  2445       return true;
  2446     } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
  2447       _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
  2448       _negate_invar = negate;
  2449       _invar = n->in(2);
  2450       return true;
  2453   if (opc == Op_SubI) {
  2454     if (n->in(2)->is_Con() && invariant(n->in(1))) {
  2455       _negate_invar = negate;
  2456       _invar = n->in(1);
  2457       _offset += !negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
  2458       return true;
  2459     } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
  2460       _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
  2461       _negate_invar = !negate;
  2462       _invar = n->in(2);
  2463       return true;
  2466   if (invariant(n)) {
  2467     _negate_invar = negate;
  2468     _invar = n;
  2469     return true;
  2471   return false;
  2474 //----------------------------print------------------------
  2475 void SWPointer::print() {
  2476 #ifndef PRODUCT
  2477   tty->print("base: %d  adr: %d  scale: %d  offset: %d  invar: %c%d\n",
  2478              _base != NULL ? _base->_idx : 0,
  2479              _adr  != NULL ? _adr->_idx  : 0,
  2480              _scale, _offset,
  2481              _negate_invar?'-':'+',
  2482              _invar != NULL ? _invar->_idx : 0);
  2483 #endif
  2486 // ========================= OrderedPair =====================
  2488 const OrderedPair OrderedPair::initial;
  2490 // ========================= SWNodeInfo =====================
  2492 const SWNodeInfo SWNodeInfo::initial;
  2495 // ============================ DepGraph ===========================
  2497 //------------------------------make_node---------------------------
  2498 // Make a new dependence graph node for an ideal node.
  2499 DepMem* DepGraph::make_node(Node* node) {
  2500   DepMem* m = new (_arena) DepMem(node);
  2501   if (node != NULL) {
  2502     assert(_map.at_grow(node->_idx) == NULL, "one init only");
  2503     _map.at_put_grow(node->_idx, m);
  2505   return m;
  2508 //------------------------------make_edge---------------------------
  2509 // Make a new dependence graph edge from dpred -> dsucc
  2510 DepEdge* DepGraph::make_edge(DepMem* dpred, DepMem* dsucc) {
  2511   DepEdge* e = new (_arena) DepEdge(dpred, dsucc, dsucc->in_head(), dpred->out_head());
  2512   dpred->set_out_head(e);
  2513   dsucc->set_in_head(e);
  2514   return e;
  2517 // ========================== DepMem ========================
  2519 //------------------------------in_cnt---------------------------
  2520 int DepMem::in_cnt() {
  2521   int ct = 0;
  2522   for (DepEdge* e = _in_head; e != NULL; e = e->next_in()) ct++;
  2523   return ct;
  2526 //------------------------------out_cnt---------------------------
  2527 int DepMem::out_cnt() {
  2528   int ct = 0;
  2529   for (DepEdge* e = _out_head; e != NULL; e = e->next_out()) ct++;
  2530   return ct;
  2533 //------------------------------print-----------------------------
  2534 void DepMem::print() {
  2535 #ifndef PRODUCT
  2536   tty->print("  DepNode %d (", _node->_idx);
  2537   for (DepEdge* p = _in_head; p != NULL; p = p->next_in()) {
  2538     Node* pred = p->pred()->node();
  2539     tty->print(" %d", pred != NULL ? pred->_idx : 0);
  2541   tty->print(") [");
  2542   for (DepEdge* s = _out_head; s != NULL; s = s->next_out()) {
  2543     Node* succ = s->succ()->node();
  2544     tty->print(" %d", succ != NULL ? succ->_idx : 0);
  2546   tty->print_cr(" ]");
  2547 #endif
  2550 // =========================== DepEdge =========================
  2552 //------------------------------DepPreds---------------------------
  2553 void DepEdge::print() {
  2554 #ifndef PRODUCT
  2555   tty->print_cr("DepEdge: %d [ %d ]", _pred->node()->_idx, _succ->node()->_idx);
  2556 #endif
  2559 // =========================== DepPreds =========================
  2560 // Iterator over predecessor edges in the dependence graph.
  2562 //------------------------------DepPreds---------------------------
  2563 DepPreds::DepPreds(Node* n, DepGraph& dg) {
  2564   _n = n;
  2565   _done = false;
  2566   if (_n->is_Store() || _n->is_Load()) {
  2567     _next_idx = MemNode::Address;
  2568     _end_idx  = n->req();
  2569     _dep_next = dg.dep(_n)->in_head();
  2570   } else if (_n->is_Mem()) {
  2571     _next_idx = 0;
  2572     _end_idx  = 0;
  2573     _dep_next = dg.dep(_n)->in_head();
  2574   } else {
  2575     _next_idx = 1;
  2576     _end_idx  = _n->req();
  2577     _dep_next = NULL;
  2579   next();
  2582 //------------------------------next---------------------------
  2583 void DepPreds::next() {
  2584   if (_dep_next != NULL) {
  2585     _current  = _dep_next->pred()->node();
  2586     _dep_next = _dep_next->next_in();
  2587   } else if (_next_idx < _end_idx) {
  2588     _current  = _n->in(_next_idx++);
  2589   } else {
  2590     _done = true;
  2594 // =========================== DepSuccs =========================
  2595 // Iterator over successor edges in the dependence graph.
  2597 //------------------------------DepSuccs---------------------------
  2598 DepSuccs::DepSuccs(Node* n, DepGraph& dg) {
  2599   _n = n;
  2600   _done = false;
  2601   if (_n->is_Load()) {
  2602     _next_idx = 0;
  2603     _end_idx  = _n->outcnt();
  2604     _dep_next = dg.dep(_n)->out_head();
  2605   } else if (_n->is_Mem() || _n->is_Phi() && _n->bottom_type() == Type::MEMORY) {
  2606     _next_idx = 0;
  2607     _end_idx  = 0;
  2608     _dep_next = dg.dep(_n)->out_head();
  2609   } else {
  2610     _next_idx = 0;
  2611     _end_idx  = _n->outcnt();
  2612     _dep_next = NULL;
  2614   next();
  2617 //-------------------------------next---------------------------
  2618 void DepSuccs::next() {
  2619   if (_dep_next != NULL) {
  2620     _current  = _dep_next->succ()->node();
  2621     _dep_next = _dep_next->next_out();
  2622   } else if (_next_idx < _end_idx) {
  2623     _current  = _n->raw_out(_next_idx++);
  2624   } else {
  2625     _done = true;

mercurial