0N/A/*
3845N/A * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "compiler/compileLog.hpp"
1879N/A#include "libadt/vectset.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "opto/addnode.hpp"
1879N/A#include "opto/callnode.hpp"
1879N/A#include "opto/divnode.hpp"
1879N/A#include "opto/matcher.hpp"
1879N/A#include "opto/memnode.hpp"
1879N/A#include "opto/mulnode.hpp"
1879N/A#include "opto/opcodes.hpp"
1879N/A#include "opto/superword.hpp"
1879N/A#include "opto/vectornode.hpp"
0N/A
0N/A//
0N/A// S U P E R W O R D T R A N S F O R M
0N/A//=============================================================================
0N/A
0N/A//------------------------------SuperWord---------------------------
0N/ASuperWord::SuperWord(PhaseIdealLoop* phase) :
0N/A _phase(phase),
0N/A _igvn(phase->_igvn),
0N/A _arena(phase->C->comp_arena()),
0N/A _packset(arena(), 8, 0, NULL), // packs for the current block
0N/A _bb_idx(arena(), (int)(1.10 * phase->C->unique()), 0, 0), // node idx to index in bb
0N/A _block(arena(), 8, 0, NULL), // nodes in current block
0N/A _data_entry(arena(), 8, 0, NULL), // nodes with all inputs from outside
0N/A _mem_slice_head(arena(), 8, 0, NULL), // memory slice heads
0N/A _mem_slice_tail(arena(), 8, 0, NULL), // memory slice tails
0N/A _node_info(arena(), 8, 0, SWNodeInfo::initial), // info needed per node
0N/A _align_to_ref(NULL), // memory reference to align vectors to
0N/A _disjoint_ptrs(arena(), 8, 0, OrderedPair::initial), // runtime disambiguated pointer pairs
0N/A _dg(_arena), // dependence graph
0N/A _visited(arena()), // visited node set
0N/A _post_visited(arena()), // post visited node set
0N/A _n_idx_list(arena(), 8), // scratch list of (node,index) pairs
0N/A _stk(arena(), 8, 0, NULL), // scratch stack of nodes
0N/A _nlist(arena(), 8, 0, NULL), // scratch list of nodes
0N/A _lpt(NULL), // loop tree node
0N/A _lp(NULL), // LoopNode
0N/A _bb(NULL), // basic block
0N/A _iv(NULL) // induction var
0N/A{}
0N/A
0N/A//------------------------------transform_loop---------------------------
0N/Avoid SuperWord::transform_loop(IdealLoopTree* lpt) {
3845N/A assert(UseSuperWord, "should be");
3845N/A // Do vectors exist on this architecture?
3845N/A if (Matcher::vector_width_in_bytes(T_BYTE) < 2) return;
3845N/A
0N/A assert(lpt->_head->is_CountedLoop(), "must be");
0N/A CountedLoopNode *cl = lpt->_head->as_CountedLoop();
0N/A
2675N/A if (!cl->is_valid_counted_loop()) return; // skip malformed counted loop
2675N/A
0N/A if (!cl->is_main_loop() ) return; // skip normal, pre, and post loops
0N/A
0N/A // Check for no control flow in body (other than exit)
0N/A Node *cl_exit = cl->loopexit();
0N/A if (cl_exit->in(0) != lpt->_head) return;
0N/A
105N/A // Make sure the are no extra control users of the loop backedge
105N/A if (cl->back_control()->outcnt() != 1) {
105N/A return;
105N/A }
105N/A
0N/A // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit))))
0N/A CountedLoopEndNode* pre_end = get_pre_loop_end(cl);
0N/A if (pre_end == NULL) return;
0N/A Node *pre_opaq1 = pre_end->limit();
0N/A if (pre_opaq1->Opcode() != Op_Opaque1) return;
0N/A
0N/A init(); // initialize data structures
0N/A
0N/A set_lpt(lpt);
0N/A set_lp(cl);
0N/A
3845N/A // For now, define one block which is the entire loop body
0N/A set_bb(cl);
0N/A
0N/A assert(_packset.length() == 0, "packset must be empty");
0N/A SLP_extract();
0N/A}
0N/A
0N/A//------------------------------SLP_extract---------------------------
0N/A// Extract the superword level parallelism
0N/A//
0N/A// 1) A reverse post-order of nodes in the block is constructed. By scanning
0N/A// this list from first to last, all definitions are visited before their uses.
0N/A//
0N/A// 2) A point-to-point dependence graph is constructed between memory references.
0N/A// This simplies the upcoming "independence" checker.
0N/A//
0N/A// 3) The maximum depth in the node graph from the beginning of the block
0N/A// to each node is computed. This is used to prune the graph search
0N/A// in the independence checker.
0N/A//
0N/A// 4) For integer types, the necessary bit width is propagated backwards
0N/A// from stores to allow packed operations on byte, char, and short
0N/A// integers. This reverses the promotion to type "int" that javac
0N/A// did for operations like: char c1,c2,c3; c1 = c2 + c3.
0N/A//
0N/A// 5) One of the memory references is picked to be an aligned vector reference.
0N/A// The pre-loop trip count is adjusted to align this reference in the
0N/A// unrolled body.
0N/A//
0N/A// 6) The initial set of pack pairs is seeded with memory references.
0N/A//
0N/A// 7) The set of pack pairs is extended by following use->def and def->use links.
0N/A//
0N/A// 8) The pairs are combined into vector sized packs.
0N/A//
0N/A// 9) Reorder the memory slices to co-locate members of the memory packs.
0N/A//
0N/A// 10) Generate ideal vector nodes for the final set of packs and where necessary,
0N/A// inserting scalar promotion, vector creation from multiple scalars, and
0N/A// extraction of scalar values from vectors.
0N/A//
0N/Avoid SuperWord::SLP_extract() {
0N/A
0N/A // Ready the block
0N/A
4242N/A if (!construct_bb())
4242N/A return; // Exit if no interesting nodes or complex graph.
0N/A
0N/A dependence_graph();
0N/A
0N/A compute_max_depth();
0N/A
0N/A compute_vector_element_type();
0N/A
0N/A // Attempt vectorization
0N/A
0N/A find_adjacent_refs();
0N/A
0N/A extend_packlist();
0N/A
0N/A combine_packs();
0N/A
0N/A construct_my_pack_map();
0N/A
0N/A filter_packs();
0N/A
0N/A schedule();
0N/A
0N/A output();
0N/A}
0N/A
0N/A//------------------------------find_adjacent_refs---------------------------
0N/A// Find the adjacent memory references and create pack pairs for them.
0N/A// This is the initial set of packs that will then be extended by
0N/A// following use->def and def->use links. The align positions are
0N/A// assigned relative to the reference "align_to_ref"
0N/Avoid SuperWord::find_adjacent_refs() {
0N/A // Get list of memory operations
0N/A Node_List memops;
0N/A for (int i = 0; i < _block.length(); i++) {
0N/A Node* n = _block.at(i);
3845N/A if (n->is_Mem() && !n->is_LoadStore() && in_bb(n) &&
29N/A is_java_primitive(n->as_Mem()->memory_type())) {
0N/A int align = memory_alignment(n->as_Mem(), 0);
0N/A if (align != bottom_align) {
0N/A memops.push(n);
0N/A }
0N/A }
0N/A }
0N/A
3845N/A Node_List align_to_refs;
3845N/A int best_iv_adjustment = 0;
3845N/A MemNode* best_align_to_mem_ref = NULL;
0N/A
3845N/A while (memops.size() != 0) {
3845N/A // Find a memory reference to align to.
3845N/A MemNode* mem_ref = find_align_to_ref(memops);
3845N/A if (mem_ref == NULL) break;
3845N/A align_to_refs.push(mem_ref);
3845N/A int iv_adjustment = get_iv_adjustment(mem_ref);
0N/A
3845N/A if (best_align_to_mem_ref == NULL) {
3845N/A // Set memory reference which is the best from all memory operations
3845N/A // to be used for alignment. The pre-loop trip count is modified to align
3845N/A // this reference to a vector-aligned address.
3845N/A best_align_to_mem_ref = mem_ref;
3845N/A best_iv_adjustment = iv_adjustment;
3845N/A }
0N/A
3845N/A SWPointer align_to_ref_p(mem_ref, this);
3845N/A // Set alignment relative to "align_to_ref" for all related memory operations.
3845N/A for (int i = memops.size() - 1; i >= 0; i--) {
3845N/A MemNode* s = memops.at(i)->as_Mem();
3845N/A if (isomorphic(s, mem_ref)) {
3845N/A SWPointer p2(s, this);
3845N/A if (p2.comparable(align_to_ref_p)) {
3845N/A int align = memory_alignment(s, iv_adjustment);
3845N/A set_alignment(s, align);
3845N/A }
3845N/A }
0N/A }
0N/A
3845N/A // Create initial pack pairs of memory operations for which
3845N/A // alignment is set and vectors will be aligned.
3845N/A bool create_pack = true;
3849N/A if (memory_alignment(mem_ref, best_iv_adjustment) == 0) {
3849N/A if (!Matcher::misaligned_vectors_ok()) {
3849N/A int vw = vector_width(mem_ref);
3849N/A int vw_best = vector_width(best_align_to_mem_ref);
3849N/A if (vw > vw_best) {
3849N/A // Do not vectorize a memory access with more elements per vector
3849N/A // if unaligned memory access is not allowed because number of
3849N/A // iterations in pre-loop will be not enough to align it.
3849N/A create_pack = false;
3849N/A }
3849N/A }
3849N/A } else {
3845N/A if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
3845N/A // Can't allow vectorization of unaligned memory accesses with the
3845N/A // same type since it could be overlapped accesses to the same array.
3845N/A create_pack = false;
3845N/A } else {
3845N/A // Allow independent (different type) unaligned memory operations
3845N/A // if HW supports them.
3845N/A if (!Matcher::misaligned_vectors_ok()) {
3845N/A create_pack = false;
3845N/A } else {
3845N/A // Check if packs of the same memory type but
3845N/A // with a different alignment were created before.
3845N/A for (uint i = 0; i < align_to_refs.size(); i++) {
3845N/A MemNode* mr = align_to_refs.at(i)->as_Mem();
3845N/A if (same_velt_type(mr, mem_ref) &&
3845N/A memory_alignment(mr, iv_adjustment) != 0)
3845N/A create_pack = false;
3845N/A }
0N/A }
0N/A }
0N/A }
3845N/A if (create_pack) {
3845N/A for (uint i = 0; i < memops.size(); i++) {
3845N/A Node* s1 = memops.at(i);
3845N/A int align = alignment(s1);
3845N/A if (align == top_align) continue;
3845N/A for (uint j = 0; j < memops.size(); j++) {
3845N/A Node* s2 = memops.at(j);
3845N/A if (alignment(s2) == top_align) continue;
3845N/A if (s1 != s2 && are_adjacent_refs(s1, s2)) {
3845N/A if (stmts_can_pack(s1, s2, align)) {
3845N/A Node_List* pair = new Node_List();
3845N/A pair->push(s1);
3845N/A pair->push(s2);
3845N/A _packset.append(pair);
3845N/A }
3845N/A }
3845N/A }
3845N/A }
3845N/A } else { // Don't create unaligned pack
3845N/A // First, remove remaining memory ops of the same type from the list.
3845N/A for (int i = memops.size() - 1; i >= 0; i--) {
3845N/A MemNode* s = memops.at(i)->as_Mem();
3845N/A if (same_velt_type(s, mem_ref)) {
3845N/A memops.remove(i);
3845N/A }
3845N/A }
3845N/A
3845N/A // Second, remove already constructed packs of the same type.
3845N/A for (int i = _packset.length() - 1; i >= 0; i--) {
3845N/A Node_List* p = _packset.at(i);
3845N/A MemNode* s = p->at(0)->as_Mem();
3845N/A if (same_velt_type(s, mem_ref)) {
3845N/A remove_pack_at(i);
3845N/A }
3845N/A }
3845N/A
3845N/A // If needed find the best memory reference for loop alignment again.
3845N/A if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
3845N/A // Put memory ops from remaining packs back on memops list for
3845N/A // the best alignment search.
3845N/A uint orig_msize = memops.size();
3845N/A for (int i = 0; i < _packset.length(); i++) {
3845N/A Node_List* p = _packset.at(i);
3845N/A MemNode* s = p->at(0)->as_Mem();
3845N/A assert(!same_velt_type(s, mem_ref), "sanity");
3845N/A memops.push(s);
3845N/A }
3845N/A MemNode* best_align_to_mem_ref = find_align_to_ref(memops);
3845N/A if (best_align_to_mem_ref == NULL) break;
3845N/A best_iv_adjustment = get_iv_adjustment(best_align_to_mem_ref);
3845N/A // Restore list.
3845N/A while (memops.size() > orig_msize)
3845N/A (void)memops.pop();
3845N/A }
3845N/A } // unaligned memory accesses
3845N/A
3845N/A // Remove used mem nodes.
3845N/A for (int i = memops.size() - 1; i >= 0; i--) {
3845N/A MemNode* m = memops.at(i)->as_Mem();
3845N/A if (alignment(m) != top_align) {
3845N/A memops.remove(i);
3845N/A }
3845N/A }
3845N/A
3845N/A } // while (memops.size() != 0
3845N/A set_align_to_ref(best_align_to_mem_ref);
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord) {
0N/A tty->print_cr("\nAfter find_adjacent_refs");
0N/A print_packset();
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A//------------------------------find_align_to_ref---------------------------
0N/A// Find a memory reference to align the loop induction variable to.
0N/A// Looks first at stores then at loads, looking for a memory reference
0N/A// with the largest number of references similar to it.
3845N/AMemNode* SuperWord::find_align_to_ref(Node_List &memops) {
0N/A GrowableArray<int> cmp_ct(arena(), memops.size(), memops.size(), 0);
0N/A
0N/A // Count number of comparable memory ops
0N/A for (uint i = 0; i < memops.size(); i++) {
0N/A MemNode* s1 = memops.at(i)->as_Mem();
0N/A SWPointer p1(s1, this);
0N/A // Discard if pre loop can't align this reference
0N/A if (!ref_is_alignable(p1)) {
0N/A *cmp_ct.adr_at(i) = 0;
0N/A continue;
0N/A }
0N/A for (uint j = i+1; j < memops.size(); j++) {
0N/A MemNode* s2 = memops.at(j)->as_Mem();
0N/A if (isomorphic(s1, s2)) {
0N/A SWPointer p2(s2, this);
0N/A if (p1.comparable(p2)) {
0N/A (*cmp_ct.adr_at(i))++;
0N/A (*cmp_ct.adr_at(j))++;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
3845N/A // Find Store (or Load) with the greatest number of "comparable" references,
3845N/A // biggest vector size, smallest data size and smallest iv offset.
0N/A int max_ct = 0;
3845N/A int max_vw = 0;
0N/A int max_idx = -1;
0N/A int min_size = max_jint;
0N/A int min_iv_offset = max_jint;
0N/A for (uint j = 0; j < memops.size(); j++) {
0N/A MemNode* s = memops.at(j)->as_Mem();
0N/A if (s->is_Store()) {
3849N/A int vw = vector_width_in_bytes(s);
3845N/A assert(vw > 1, "sanity");
0N/A SWPointer p(s, this);
3845N/A if (cmp_ct.at(j) > max_ct ||
3845N/A cmp_ct.at(j) == max_ct &&
3845N/A (vw > max_vw ||
3845N/A vw == max_vw &&
3845N/A (data_size(s) < min_size ||
3845N/A data_size(s) == min_size &&
3845N/A (p.offset_in_bytes() < min_iv_offset)))) {
0N/A max_ct = cmp_ct.at(j);
3845N/A max_vw = vw;
0N/A max_idx = j;
0N/A min_size = data_size(s);
0N/A min_iv_offset = p.offset_in_bytes();
0N/A }
0N/A }
0N/A }
0N/A // If no stores, look at loads
0N/A if (max_ct == 0) {
0N/A for (uint j = 0; j < memops.size(); j++) {
0N/A MemNode* s = memops.at(j)->as_Mem();
0N/A if (s->is_Load()) {
3849N/A int vw = vector_width_in_bytes(s);
3845N/A assert(vw > 1, "sanity");
0N/A SWPointer p(s, this);
3845N/A if (cmp_ct.at(j) > max_ct ||
3845N/A cmp_ct.at(j) == max_ct &&
3845N/A (vw > max_vw ||
3845N/A vw == max_vw &&
3845N/A (data_size(s) < min_size ||
3845N/A data_size(s) == min_size &&
3845N/A (p.offset_in_bytes() < min_iv_offset)))) {
0N/A max_ct = cmp_ct.at(j);
3845N/A max_vw = vw;
0N/A max_idx = j;
0N/A min_size = data_size(s);
0N/A min_iv_offset = p.offset_in_bytes();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
3845N/A#ifdef ASSERT
0N/A if (TraceSuperWord && Verbose) {
0N/A tty->print_cr("\nVector memops after find_align_to_refs");
0N/A for (uint i = 0; i < memops.size(); i++) {
0N/A MemNode* s = memops.at(i)->as_Mem();
0N/A s->dump();
0N/A }
0N/A }
0N/A#endif
3845N/A
3845N/A if (max_ct > 0) {
3845N/A#ifdef ASSERT
3845N/A if (TraceSuperWord) {
3845N/A tty->print("\nVector align to node: ");
3845N/A memops.at(max_idx)->as_Mem()->dump();
3845N/A }
3845N/A#endif
3845N/A return memops.at(max_idx)->as_Mem();
3845N/A }
3845N/A return NULL;
0N/A}
0N/A
0N/A//------------------------------ref_is_alignable---------------------------
0N/A// Can the preloop align the reference to position zero in the vector?
0N/Abool SuperWord::ref_is_alignable(SWPointer& p) {
0N/A if (!p.has_iv()) {
0N/A return true; // no induction variable
0N/A }
0N/A CountedLoopEndNode* pre_end = get_pre_loop_end(lp()->as_CountedLoop());
0N/A assert(pre_end->stride_is_con(), "pre loop stride is constant");
0N/A int preloop_stride = pre_end->stride_con();
0N/A
0N/A int span = preloop_stride * p.scale_in_bytes();
0N/A
0N/A // Stride one accesses are alignable.
0N/A if (ABS(span) == p.memory_size())
0N/A return true;
0N/A
0N/A // If initial offset from start of object is computable,
0N/A // compute alignment within the vector.
3849N/A int vw = vector_width_in_bytes(p.mem());
3845N/A assert(vw > 1, "sanity");
0N/A if (vw % span == 0) {
0N/A Node* init_nd = pre_end->init_trip();
0N/A if (init_nd->is_Con() && p.invar() == NULL) {
0N/A int init = init_nd->bottom_type()->is_int()->get_con();
0N/A
0N/A int init_offset = init * p.scale_in_bytes() + p.offset_in_bytes();
0N/A assert(init_offset >= 0, "positive offset from object start");
0N/A
0N/A if (span > 0) {
0N/A return (vw - (init_offset % vw)) % span == 0;
0N/A } else {
0N/A assert(span < 0, "nonzero stride * scale");
0N/A return (init_offset % vw) % -span == 0;
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
3845N/A//---------------------------get_iv_adjustment---------------------------
3845N/A// Calculate loop's iv adjustment for this memory ops.
3845N/Aint SuperWord::get_iv_adjustment(MemNode* mem_ref) {
3845N/A SWPointer align_to_ref_p(mem_ref, this);
3845N/A int offset = align_to_ref_p.offset_in_bytes();
3845N/A int scale = align_to_ref_p.scale_in_bytes();
3849N/A int vw = vector_width_in_bytes(mem_ref);
3845N/A assert(vw > 1, "sanity");
3845N/A int stride_sign = (scale * iv_stride()) > 0 ? 1 : -1;
4014N/A // At least one iteration is executed in pre-loop by default. As result
4014N/A // several iterations are needed to align memory operations in main-loop even
4014N/A // if offset is 0.
4014N/A int iv_adjustment_in_bytes = (stride_sign * vw - (offset % vw));
4014N/A int elt_size = align_to_ref_p.memory_size();
4014N/A assert(((ABS(iv_adjustment_in_bytes) % elt_size) == 0),
4014N/A err_msg_res("(%d) should be divisible by (%d)", iv_adjustment_in_bytes, elt_size));
4014N/A int iv_adjustment = iv_adjustment_in_bytes/elt_size;
3845N/A
3845N/A#ifndef PRODUCT
3845N/A if (TraceSuperWord)
3845N/A tty->print_cr("\noffset = %d iv_adjust = %d elt_size = %d scale = %d iv_stride = %d vect_size %d",
4014N/A offset, iv_adjustment, elt_size, scale, iv_stride(), vw);
3845N/A#endif
3845N/A return iv_adjustment;
3845N/A}
3845N/A
0N/A//---------------------------dependence_graph---------------------------
0N/A// Construct dependency graph.
0N/A// Add dependence edges to load/store nodes for memory dependence
0N/A// A.out()->DependNode.in(1) and DependNode.out()->B.prec(x)
0N/Avoid SuperWord::dependence_graph() {
0N/A // First, assign a dependence node to each memory node
0N/A for (int i = 0; i < _block.length(); i++ ) {
0N/A Node *n = _block.at(i);
0N/A if (n->is_Mem() || n->is_Phi() && n->bottom_type() == Type::MEMORY) {
0N/A _dg.make_node(n);
0N/A }
0N/A }
0N/A
0N/A // For each memory slice, create the dependences
0N/A for (int i = 0; i < _mem_slice_head.length(); i++) {
0N/A Node* n = _mem_slice_head.at(i);
0N/A Node* n_tail = _mem_slice_tail.at(i);
0N/A
0N/A // Get slice in predecessor order (last is first)
0N/A mem_slice_preds(n_tail, n, _nlist);
0N/A
0N/A // Make the slice dependent on the root
0N/A DepMem* slice = _dg.dep(n);
0N/A _dg.make_edge(_dg.root(), slice);
0N/A
0N/A // Create a sink for the slice
0N/A DepMem* slice_sink = _dg.make_node(NULL);
0N/A _dg.make_edge(slice_sink, _dg.tail());
0N/A
0N/A // Now visit each pair of memory ops, creating the edges
0N/A for (int j = _nlist.length() - 1; j >= 0 ; j--) {
0N/A Node* s1 = _nlist.at(j);
0N/A
0N/A // If no dependency yet, use slice
0N/A if (_dg.dep(s1)->in_cnt() == 0) {
0N/A _dg.make_edge(slice, s1);
0N/A }
0N/A SWPointer p1(s1->as_Mem(), this);
0N/A bool sink_dependent = true;
0N/A for (int k = j - 1; k >= 0; k--) {
0N/A Node* s2 = _nlist.at(k);
0N/A if (s1->is_Load() && s2->is_Load())
0N/A continue;
0N/A SWPointer p2(s2->as_Mem(), this);
0N/A
0N/A int cmp = p1.cmp(p2);
0N/A if (SuperWordRTDepCheck &&
0N/A p1.base() != p2.base() && p1.valid() && p2.valid()) {
0N/A // Create a runtime check to disambiguate
0N/A OrderedPair pp(p1.base(), p2.base());
0N/A _disjoint_ptrs.append_if_missing(pp);
0N/A } else if (!SWPointer::not_equal(cmp)) {
0N/A // Possibly same address
0N/A _dg.make_edge(s1, s2);
0N/A sink_dependent = false;
0N/A }
0N/A }
0N/A if (sink_dependent) {
0N/A _dg.make_edge(s1, slice_sink);
0N/A }
0N/A }
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord) {
0N/A tty->print_cr("\nDependence graph for slice: %d", n->_idx);
0N/A for (int q = 0; q < _nlist.length(); q++) {
0N/A _dg.print(_nlist.at(q));
0N/A }
0N/A tty->cr();
0N/A }
0N/A#endif
0N/A _nlist.clear();
0N/A }
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord) {
0N/A tty->print_cr("\ndisjoint_ptrs: %s", _disjoint_ptrs.length() > 0 ? "" : "NONE");
0N/A for (int r = 0; r < _disjoint_ptrs.length(); r++) {
0N/A _disjoint_ptrs.at(r).print();
0N/A tty->cr();
0N/A }
0N/A tty->cr();
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A//---------------------------mem_slice_preds---------------------------
0N/A// Return a memory slice (node list) in predecessor order starting at "start"
0N/Avoid SuperWord::mem_slice_preds(Node* start, Node* stop, GrowableArray<Node*> &preds) {
0N/A assert(preds.length() == 0, "start empty");
0N/A Node* n = start;
0N/A Node* prev = NULL;
0N/A while (true) {
0N/A assert(in_bb(n), "must be in block");
0N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
0N/A Node* out = n->fast_out(i);
0N/A if (out->is_Load()) {
0N/A if (in_bb(out)) {
0N/A preds.push(out);
0N/A }
0N/A } else {
0N/A // FIXME
0N/A if (out->is_MergeMem() && !in_bb(out)) {
0N/A // Either unrolling is causing a memory edge not to disappear,
0N/A // or need to run igvn.optimize() again before SLP
0N/A } else if (out->is_Phi() && out->bottom_type() == Type::MEMORY && !in_bb(out)) {
0N/A // Ditto. Not sure what else to check further.
667N/A } else if (out->Opcode() == Op_StoreCM && out->in(MemNode::OopStore) == n) {
0N/A // StoreCM has an input edge used as a precedence edge.
0N/A // Maybe an issue when oop stores are vectorized.
0N/A } else {
0N/A assert(out == prev || prev == NULL, "no branches off of store slice");
0N/A }
0N/A }
0N/A }
0N/A if (n == stop) break;
0N/A preds.push(n);
0N/A prev = n;
4242N/A assert(n->is_Mem(), err_msg_res("unexpected node %s", n->Name()));
0N/A n = n->in(MemNode::Memory);
0N/A }
0N/A}
0N/A
0N/A//------------------------------stmts_can_pack---------------------------
605N/A// Can s1 and s2 be in a pack with s1 immediately preceding s2 and
0N/A// s1 aligned at "align"
0N/Abool SuperWord::stmts_can_pack(Node* s1, Node* s2, int align) {
987N/A
987N/A // Do not use superword for non-primitives
3845N/A BasicType bt1 = velt_basic_type(s1);
3845N/A BasicType bt2 = velt_basic_type(s2);
3845N/A if(!is_java_primitive(bt1) || !is_java_primitive(bt2))
987N/A return false;
3845N/A if (Matcher::max_vector_size(bt1) < 2) {
3845N/A return false; // No vectors for this type
3845N/A }
987N/A
0N/A if (isomorphic(s1, s2)) {
0N/A if (independent(s1, s2)) {
0N/A if (!exists_at(s1, 0) && !exists_at(s2, 1)) {
0N/A if (!s1->is_Mem() || are_adjacent_refs(s1, s2)) {
0N/A int s1_align = alignment(s1);
0N/A int s2_align = alignment(s2);
0N/A if (s1_align == top_align || s1_align == align) {
0N/A if (s2_align == top_align || s2_align == align + data_size(s1)) {
0N/A return true;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A//------------------------------exists_at---------------------------
0N/A// Does s exist in a pack at position pos?
0N/Abool SuperWord::exists_at(Node* s, uint pos) {
0N/A for (int i = 0; i < _packset.length(); i++) {
0N/A Node_List* p = _packset.at(i);
0N/A if (p->at(pos) == s) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A//------------------------------are_adjacent_refs---------------------------
0N/A// Is s1 immediately before s2 in memory?
0N/Abool SuperWord::are_adjacent_refs(Node* s1, Node* s2) {
0N/A if (!s1->is_Mem() || !s2->is_Mem()) return false;
0N/A if (!in_bb(s1) || !in_bb(s2)) return false;
1505N/A
1505N/A // Do not use superword for non-primitives
1505N/A if (!is_java_primitive(s1->as_Mem()->memory_type()) ||
1505N/A !is_java_primitive(s2->as_Mem()->memory_type())) {
1505N/A return false;
1505N/A }
1505N/A
0N/A // FIXME - co_locate_pack fails on Stores in different mem-slices, so
0N/A // only pack memops that are in the same alias set until that's fixed.
0N/A if (_phase->C->get_alias_index(s1->as_Mem()->adr_type()) !=
0N/A _phase->C->get_alias_index(s2->as_Mem()->adr_type()))
0N/A return false;
0N/A SWPointer p1(s1->as_Mem(), this);
0N/A SWPointer p2(s2->as_Mem(), this);
0N/A if (p1.base() != p2.base() || !p1.comparable(p2)) return false;
0N/A int diff = p2.offset_in_bytes() - p1.offset_in_bytes();
0N/A return diff == data_size(s1);
0N/A}
0N/A
0N/A//------------------------------isomorphic---------------------------
0N/A// Are s1 and s2 similar?
0N/Abool SuperWord::isomorphic(Node* s1, Node* s2) {
0N/A if (s1->Opcode() != s2->Opcode()) return false;
0N/A if (s1->req() != s2->req()) return false;
0N/A if (s1->in(0) != s2->in(0)) return false;
3845N/A if (!same_velt_type(s1, s2)) return false;
0N/A return true;
0N/A}
0N/A
0N/A//------------------------------independent---------------------------
0N/A// Is there no data path from s1 to s2 or s2 to s1?
0N/Abool SuperWord::independent(Node* s1, Node* s2) {
0N/A // assert(s1->Opcode() == s2->Opcode(), "check isomorphic first");
0N/A int d1 = depth(s1);
0N/A int d2 = depth(s2);
0N/A if (d1 == d2) return s1 != s2;
0N/A Node* deep = d1 > d2 ? s1 : s2;
0N/A Node* shallow = d1 > d2 ? s2 : s1;
0N/A
0N/A visited_clear();
0N/A
0N/A return independent_path(shallow, deep);
0N/A}
0N/A
0N/A//------------------------------independent_path------------------------------
0N/A// Helper for independent
0N/Abool SuperWord::independent_path(Node* shallow, Node* deep, uint dp) {
0N/A if (dp >= 1000) return false; // stop deep recursion
0N/A visited_set(deep);
0N/A int shal_depth = depth(shallow);
0N/A assert(shal_depth <= depth(deep), "must be");
0N/A for (DepPreds preds(deep, _dg); !preds.done(); preds.next()) {
0N/A Node* pred = preds.current();
0N/A if (in_bb(pred) && !visited_test(pred)) {
0N/A if (shallow == pred) {
0N/A return false;
0N/A }
0N/A if (shal_depth < depth(pred) && !independent_path(shallow, pred, dp+1)) {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A return true;
0N/A}
0N/A
0N/A//------------------------------set_alignment---------------------------
0N/Avoid SuperWord::set_alignment(Node* s1, Node* s2, int align) {
0N/A set_alignment(s1, align);
3845N/A if (align == top_align || align == bottom_align) {
3845N/A set_alignment(s2, align);
3845N/A } else {
3845N/A set_alignment(s2, align + data_size(s1));
3845N/A }
0N/A}
0N/A
0N/A//------------------------------data_size---------------------------
0N/Aint SuperWord::data_size(Node* s) {
3845N/A int bsize = type2aelembytes(velt_basic_type(s));
0N/A assert(bsize != 0, "valid size");
0N/A return bsize;
0N/A}
0N/A
0N/A//------------------------------extend_packlist---------------------------
0N/A// Extend packset by following use->def and def->use links from pack members.
0N/Avoid SuperWord::extend_packlist() {
0N/A bool changed;
0N/A do {
0N/A changed = false;
0N/A for (int i = 0; i < _packset.length(); i++) {
0N/A Node_List* p = _packset.at(i);
0N/A changed |= follow_use_defs(p);
0N/A changed |= follow_def_uses(p);
0N/A }
0N/A } while (changed);
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord) {
0N/A tty->print_cr("\nAfter extend_packlist");
0N/A print_packset();
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A//------------------------------follow_use_defs---------------------------
0N/A// Extend the packset by visiting operand definitions of nodes in pack p
0N/Abool SuperWord::follow_use_defs(Node_List* p) {
3845N/A assert(p->size() == 2, "just checking");
0N/A Node* s1 = p->at(0);
0N/A Node* s2 = p->at(1);
0N/A assert(s1->req() == s2->req(), "just checking");
0N/A assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
0N/A
0N/A if (s1->is_Load()) return false;
0N/A
0N/A int align = alignment(s1);
0N/A bool changed = false;
0N/A int start = s1->is_Store() ? MemNode::ValueIn : 1;
0N/A int end = s1->is_Store() ? MemNode::ValueIn+1 : s1->req();
0N/A for (int j = start; j < end; j++) {
0N/A Node* t1 = s1->in(j);
0N/A Node* t2 = s2->in(j);
0N/A if (!in_bb(t1) || !in_bb(t2))
0N/A continue;
0N/A if (stmts_can_pack(t1, t2, align)) {
0N/A if (est_savings(t1, t2) >= 0) {
0N/A Node_List* pair = new Node_List();
0N/A pair->push(t1);
0N/A pair->push(t2);
0N/A _packset.append(pair);
0N/A set_alignment(t1, t2, align);
0N/A changed = true;
0N/A }
0N/A }
0N/A }
0N/A return changed;
0N/A}
0N/A
0N/A//------------------------------follow_def_uses---------------------------
0N/A// Extend the packset by visiting uses of nodes in pack p
0N/Abool SuperWord::follow_def_uses(Node_List* p) {
0N/A bool changed = false;
0N/A Node* s1 = p->at(0);
0N/A Node* s2 = p->at(1);
0N/A assert(p->size() == 2, "just checking");
0N/A assert(s1->req() == s2->req(), "just checking");
0N/A assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
0N/A
0N/A if (s1->is_Store()) return false;
0N/A
0N/A int align = alignment(s1);
0N/A int savings = -1;
0N/A Node* u1 = NULL;
0N/A Node* u2 = NULL;
0N/A for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
0N/A Node* t1 = s1->fast_out(i);
0N/A if (!in_bb(t1)) continue;
0N/A for (DUIterator_Fast jmax, j = s2->fast_outs(jmax); j < jmax; j++) {
0N/A Node* t2 = s2->fast_out(j);
0N/A if (!in_bb(t2)) continue;
0N/A if (!opnd_positions_match(s1, t1, s2, t2))
0N/A continue;
0N/A if (stmts_can_pack(t1, t2, align)) {
0N/A int my_savings = est_savings(t1, t2);
0N/A if (my_savings > savings) {
0N/A savings = my_savings;
0N/A u1 = t1;
0N/A u2 = t2;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if (savings >= 0) {
0N/A Node_List* pair = new Node_List();
0N/A pair->push(u1);
0N/A pair->push(u2);
0N/A _packset.append(pair);
0N/A set_alignment(u1, u2, align);
0N/A changed = true;
0N/A }
0N/A return changed;
0N/A}
0N/A
0N/A//---------------------------opnd_positions_match-------------------------
0N/A// Is the use of d1 in u1 at the same operand position as d2 in u2?
0N/Abool SuperWord::opnd_positions_match(Node* d1, Node* u1, Node* d2, Node* u2) {
0N/A uint ct = u1->req();
0N/A if (ct != u2->req()) return false;
0N/A uint i1 = 0;
0N/A uint i2 = 0;
0N/A do {
0N/A for (i1++; i1 < ct; i1++) if (u1->in(i1) == d1) break;
0N/A for (i2++; i2 < ct; i2++) if (u2->in(i2) == d2) break;
0N/A if (i1 != i2) {
3845N/A if ((i1 == (3-i2)) && (u2->is_Add() || u2->is_Mul())) {
3845N/A // Further analysis relies on operands position matching.
3845N/A u2->swap_edges(i1, i2);
3845N/A } else {
3845N/A return false;
3845N/A }
0N/A }
0N/A } while (i1 < ct);
0N/A return true;
0N/A}
0N/A
0N/A//------------------------------est_savings---------------------------
0N/A// Estimate the savings from executing s1 and s2 as a pack
0N/Aint SuperWord::est_savings(Node* s1, Node* s2) {
3845N/A int save_in = 2 - 1; // 2 operations per instruction in packed form
0N/A
0N/A // inputs
0N/A for (uint i = 1; i < s1->req(); i++) {
0N/A Node* x1 = s1->in(i);
0N/A Node* x2 = s2->in(i);
0N/A if (x1 != x2) {
0N/A if (are_adjacent_refs(x1, x2)) {
3845N/A save_in += adjacent_profit(x1, x2);
0N/A } else if (!in_packset(x1, x2)) {
3845N/A save_in -= pack_cost(2);
0N/A } else {
3845N/A save_in += unpack_cost(2);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // uses of result
0N/A uint ct = 0;
3845N/A int save_use = 0;
0N/A for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
0N/A Node* s1_use = s1->fast_out(i);
0N/A for (int j = 0; j < _packset.length(); j++) {
0N/A Node_List* p = _packset.at(j);
0N/A if (p->at(0) == s1_use) {
0N/A for (DUIterator_Fast kmax, k = s2->fast_outs(kmax); k < kmax; k++) {
0N/A Node* s2_use = s2->fast_out(k);
0N/A if (p->at(p->size()-1) == s2_use) {
0N/A ct++;
0N/A if (are_adjacent_refs(s1_use, s2_use)) {
3845N/A save_use += adjacent_profit(s1_use, s2_use);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
3845N/A if (ct < s1->outcnt()) save_use += unpack_cost(1);
3845N/A if (ct < s2->outcnt()) save_use += unpack_cost(1);
0N/A
3845N/A return MAX2(save_in, save_use);
0N/A}
0N/A
0N/A//------------------------------costs---------------------------
0N/Aint SuperWord::adjacent_profit(Node* s1, Node* s2) { return 2; }
0N/Aint SuperWord::pack_cost(int ct) { return ct; }
0N/Aint SuperWord::unpack_cost(int ct) { return ct; }
0N/A
0N/A//------------------------------combine_packs---------------------------
0N/A// Combine packs A and B with A.last == B.first into A.first..,A.last,B.second,..B.last
0N/Avoid SuperWord::combine_packs() {
3845N/A bool changed = true;
3845N/A // Combine packs regardless max vector size.
3845N/A while (changed) {
0N/A changed = false;
0N/A for (int i = 0; i < _packset.length(); i++) {
0N/A Node_List* p1 = _packset.at(i);
0N/A if (p1 == NULL) continue;
0N/A for (int j = 0; j < _packset.length(); j++) {
0N/A Node_List* p2 = _packset.at(j);
0N/A if (p2 == NULL) continue;
3845N/A if (i == j) continue;
0N/A if (p1->at(p1->size()-1) == p2->at(0)) {
0N/A for (uint k = 1; k < p2->size(); k++) {
0N/A p1->push(p2->at(k));
0N/A }
0N/A _packset.at_put(j, NULL);
0N/A changed = true;
0N/A }
0N/A }
0N/A }
3845N/A }
0N/A
3845N/A // Split packs which have size greater then max vector size.
3845N/A for (int i = 0; i < _packset.length(); i++) {
3845N/A Node_List* p1 = _packset.at(i);
3845N/A if (p1 != NULL) {
3845N/A BasicType bt = velt_basic_type(p1->at(0));
3845N/A uint max_vlen = Matcher::max_vector_size(bt); // Max elements in vector
3845N/A assert(is_power_of_2(max_vlen), "sanity");
3845N/A uint psize = p1->size();
3845N/A if (!is_power_of_2(psize)) {
3845N/A // Skip pack which can't be vector.
3845N/A // case1: for(...) { a[i] = i; } elements values are different (i+x)
3845N/A // case2: for(...) { a[i] = b[i+1]; } can't align both, load and store
3845N/A _packset.at_put(i, NULL);
3845N/A continue;
3845N/A }
3845N/A if (psize > max_vlen) {
3845N/A Node_List* pack = new Node_List();
3845N/A for (uint j = 0; j < psize; j++) {
3845N/A pack->push(p1->at(j));
3845N/A if (pack->size() >= max_vlen) {
3845N/A assert(is_power_of_2(pack->size()), "sanity");
3845N/A _packset.append(pack);
3845N/A pack = new Node_List();
3845N/A }
3845N/A }
3845N/A _packset.at_put(i, NULL);
3845N/A }
3845N/A }
3845N/A }
3845N/A
3845N/A // Compress list.
0N/A for (int i = _packset.length() - 1; i >= 0; i--) {
0N/A Node_List* p1 = _packset.at(i);
0N/A if (p1 == NULL) {
0N/A _packset.remove_at(i);
0N/A }
0N/A }
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord) {
0N/A tty->print_cr("\nAfter combine_packs");
0N/A print_packset();
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A//-----------------------------construct_my_pack_map--------------------------
0N/A// Construct the map from nodes to packs. Only valid after the
0N/A// point where a node is only in one pack (after combine_packs).
0N/Avoid SuperWord::construct_my_pack_map() {
0N/A Node_List* rslt = NULL;
0N/A for (int i = 0; i < _packset.length(); i++) {
0N/A Node_List* p = _packset.at(i);
0N/A for (uint j = 0; j < p->size(); j++) {
0N/A Node* s = p->at(j);
0N/A assert(my_pack(s) == NULL, "only in one pack");
0N/A set_my_pack(s, p);
0N/A }
0N/A }
0N/A}
0N/A
0N/A//------------------------------filter_packs---------------------------
0N/A// Remove packs that are not implemented or not profitable.
0N/Avoid SuperWord::filter_packs() {
0N/A
0N/A // Remove packs that are not implemented
0N/A for (int i = _packset.length() - 1; i >= 0; i--) {
0N/A Node_List* pk = _packset.at(i);
0N/A bool impl = implemented(pk);
0N/A if (!impl) {
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord && Verbose) {
0N/A tty->print_cr("Unimplemented");
0N/A pk->at(0)->dump();
0N/A }
0N/A#endif
0N/A remove_pack_at(i);
0N/A }
0N/A }
0N/A
0N/A // Remove packs that are not profitable
0N/A bool changed;
0N/A do {
0N/A changed = false;
0N/A for (int i = _packset.length() - 1; i >= 0; i--) {
0N/A Node_List* pk = _packset.at(i);
0N/A bool prof = profitable(pk);
0N/A if (!prof) {
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord && Verbose) {
0N/A tty->print_cr("Unprofitable");
0N/A pk->at(0)->dump();
0N/A }
0N/A#endif
0N/A remove_pack_at(i);
0N/A changed = true;
0N/A }
0N/A }
0N/A } while (changed);
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord) {
0N/A tty->print_cr("\nAfter filter_packs");
0N/A print_packset();
0N/A tty->cr();
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A//------------------------------implemented---------------------------
0N/A// Can code be generated for pack p?
0N/Abool SuperWord::implemented(Node_List* p) {
0N/A Node* p0 = p->at(0);
3969N/A return VectorNode::implemented(p0->Opcode(), p->size(), velt_basic_type(p0));
3969N/A}
3969N/A
3969N/A//------------------------------same_inputs--------------------------
3969N/A// For pack p, are all idx operands the same?
3969N/Astatic bool same_inputs(Node_List* p, int idx) {
3969N/A Node* p0 = p->at(0);
3969N/A uint vlen = p->size();
3969N/A Node* p0_def = p0->in(idx);
3969N/A for (uint i = 1; i < vlen; i++) {
3969N/A Node* pi = p->at(i);
3969N/A Node* pi_def = pi->in(idx);
3969N/A if (p0_def != pi_def)
3969N/A return false;
3967N/A }
3969N/A return true;
0N/A}
0N/A
0N/A//------------------------------profitable---------------------------
0N/A// For pack p, are all operands and all uses (with in the block) vector?
0N/Abool SuperWord::profitable(Node_List* p) {
0N/A Node* p0 = p->at(0);
0N/A uint start, end;
3969N/A VectorNode::vector_operands(p0, &start, &end);
0N/A
4021N/A // Return false if some inputs are not vectors or vectors with different
4021N/A // size or alignment.
4021N/A // Also, for now, return false if not scalar promotion case when inputs are
4021N/A // the same. Later, implement PackNode and allow differing, non-vector inputs
4021N/A // (maybe just the ones from outside the block.)
0N/A for (uint i = start; i < end; i++) {
4021N/A if (!is_vector_use(p0, i))
4021N/A return false;
0N/A }
3969N/A if (VectorNode::is_shift(p0)) {
4021N/A // For now, return false if shift count is vector or not scalar promotion
4021N/A // case (different shift counts) because it is not supported yet.
4021N/A Node* cnt = p0->in(2);
4021N/A Node_List* cnt_pk = my_pack(cnt);
4021N/A if (cnt_pk != NULL)
3969N/A return false;
3969N/A if (!same_inputs(p, 2))
3969N/A return false;
3969N/A }
0N/A if (!p0->is_Store()) {
0N/A // For now, return false if not all uses are vector.
0N/A // Later, implement ExtractNode and allow non-vector uses (maybe
0N/A // just the ones outside the block.)
0N/A for (uint i = 0; i < p->size(); i++) {
0N/A Node* def = p->at(i);
0N/A for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
0N/A Node* use = def->fast_out(j);
0N/A for (uint k = 0; k < use->req(); k++) {
0N/A Node* n = use->in(k);
0N/A if (def == n) {
0N/A if (!is_vector_use(use, k)) {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return true;
0N/A}
0N/A
0N/A//------------------------------schedule---------------------------
0N/A// Adjust the memory graph for the packed operations
0N/Avoid SuperWord::schedule() {
0N/A
0N/A // Co-locate in the memory graph the members of each memory pack
0N/A for (int i = 0; i < _packset.length(); i++) {
0N/A co_locate_pack(_packset.at(i));
0N/A }
0N/A}
0N/A
667N/A//-------------------------------remove_and_insert-------------------
3845N/A// Remove "current" from its current position in the memory graph and insert
3845N/A// it after the appropriate insertion point (lip or uip).
667N/Avoid SuperWord::remove_and_insert(MemNode *current, MemNode *prev, MemNode *lip,
667N/A Node *uip, Unique_Node_List &sched_before) {
667N/A Node* my_mem = current->in(MemNode::Memory);
3845N/A bool sched_up = sched_before.member(current);
667N/A
3845N/A // remove current_store from its current position in the memmory graph
667N/A for (DUIterator i = current->outs(); current->has_out(i); i++) {
667N/A Node* use = current->out(i);
667N/A if (use->is_Mem()) {
667N/A assert(use->in(MemNode::Memory) == current, "must be");
667N/A if (use == prev) { // connect prev to my_mem
3845N/A _igvn.replace_input_of(use, MemNode::Memory, my_mem);
3845N/A --i; //deleted this edge; rescan position
667N/A } else if (sched_before.member(use)) {
3845N/A if (!sched_up) { // Will be moved together with current
3845N/A _igvn.replace_input_of(use, MemNode::Memory, uip);
3845N/A --i; //deleted this edge; rescan position
3845N/A }
667N/A } else {
3845N/A if (sched_up) { // Will be moved together with current
3845N/A _igvn.replace_input_of(use, MemNode::Memory, lip);
3845N/A --i; //deleted this edge; rescan position
3845N/A }
667N/A }
667N/A }
667N/A }
667N/A
667N/A Node *insert_pt = sched_up ? uip : lip;
667N/A
667N/A // all uses of insert_pt's memory state should use current's instead
667N/A for (DUIterator i = insert_pt->outs(); insert_pt->has_out(i); i++) {
667N/A Node* use = insert_pt->out(i);
667N/A if (use->is_Mem()) {
667N/A assert(use->in(MemNode::Memory) == insert_pt, "must be");
3811N/A _igvn.replace_input_of(use, MemNode::Memory, current);
667N/A --i; //deleted this edge; rescan position
667N/A } else if (!sched_up && use->is_Phi() && use->bottom_type() == Type::MEMORY) {
667N/A uint pos; //lip (lower insert point) must be the last one in the memory slice
667N/A for (pos=1; pos < use->req(); pos++) {
667N/A if (use->in(pos) == insert_pt) break;
667N/A }
3811N/A _igvn.replace_input_of(use, pos, current);
667N/A --i;
667N/A }
667N/A }
667N/A
667N/A //connect current to insert_pt
3845N/A _igvn.replace_input_of(current, MemNode::Memory, insert_pt);
667N/A}
667N/A
667N/A//------------------------------co_locate_pack----------------------------------
667N/A// To schedule a store pack, we need to move any sandwiched memory ops either before
667N/A// or after the pack, based upon dependence information:
667N/A// (1) If any store in the pack depends on the sandwiched memory op, the
667N/A// sandwiched memory op must be scheduled BEFORE the pack;
667N/A// (2) If a sandwiched memory op depends on any store in the pack, the
667N/A// sandwiched memory op must be scheduled AFTER the pack;
667N/A// (3) If a sandwiched memory op (say, memA) depends on another sandwiched
667N/A// memory op (say memB), memB must be scheduled before memA. So, if memA is
667N/A// scheduled before the pack, memB must also be scheduled before the pack;
667N/A// (4) If there is no dependence restriction for a sandwiched memory op, we simply
667N/A// schedule this store AFTER the pack
667N/A// (5) We know there is no dependence cycle, so there in no other case;
667N/A// (6) Finally, all memory ops in another single pack should be moved in the same direction.
667N/A//
952N/A// To schedule a load pack, we use the memory state of either the first or the last load in
952N/A// the pack, based on the dependence constraint.
0N/Avoid SuperWord::co_locate_pack(Node_List* pk) {
0N/A if (pk->at(0)->is_Store()) {
0N/A MemNode* first = executed_first(pk)->as_Mem();
0N/A MemNode* last = executed_last(pk)->as_Mem();
667N/A Unique_Node_List schedule_before_pack;
667N/A Unique_Node_List memops;
667N/A
0N/A MemNode* current = last->in(MemNode::Memory)->as_Mem();
667N/A MemNode* previous = last;
0N/A while (true) {
0N/A assert(in_bb(current), "stay in block");
667N/A memops.push(previous);
667N/A for (DUIterator i = current->outs(); current->has_out(i); i++) {
667N/A Node* use = current->out(i);
667N/A if (use->is_Mem() && use != previous)
667N/A memops.push(use);
667N/A }
3845N/A if (current == first) break;
667N/A previous = current;
667N/A current = current->in(MemNode::Memory)->as_Mem();
667N/A }
667N/A
667N/A // determine which memory operations should be scheduled before the pack
667N/A for (uint i = 1; i < memops.size(); i++) {
667N/A Node *s1 = memops.at(i);
667N/A if (!in_pack(s1, pk) && !schedule_before_pack.member(s1)) {
667N/A for (uint j = 0; j< i; j++) {
667N/A Node *s2 = memops.at(j);
667N/A if (!independent(s1, s2)) {
667N/A if (in_pack(s2, pk) || schedule_before_pack.member(s2)) {
3845N/A schedule_before_pack.push(s1); // s1 must be scheduled before
667N/A Node_List* mem_pk = my_pack(s1);
667N/A if (mem_pk != NULL) {
667N/A for (uint ii = 0; ii < mem_pk->size(); ii++) {
3845N/A Node* s = mem_pk->at(ii); // follow partner
667N/A if (memops.member(s) && !schedule_before_pack.member(s))
667N/A schedule_before_pack.push(s);
667N/A }
667N/A }
3845N/A break;
667N/A }
667N/A }
667N/A }
667N/A }
667N/A }
667N/A
3845N/A Node* upper_insert_pt = first->in(MemNode::Memory);
3845N/A // Following code moves loads connected to upper_insert_pt below aliased stores.
3845N/A // Collect such loads here and reconnect them back to upper_insert_pt later.
3845N/A memops.clear();
3845N/A for (DUIterator i = upper_insert_pt->outs(); upper_insert_pt->has_out(i); i++) {
3845N/A Node* use = upper_insert_pt->out(i);
3845N/A if (!use->is_Store())
3845N/A memops.push(use);
3845N/A }
3845N/A
667N/A MemNode* lower_insert_pt = last;
667N/A previous = last; //previous store in pk
667N/A current = last->in(MemNode::Memory)->as_Mem();
667N/A
3845N/A // start scheduling from "last" to "first"
667N/A while (true) {
667N/A assert(in_bb(current), "stay in block");
667N/A assert(in_pack(previous, pk), "previous stays in pack");
0N/A Node* my_mem = current->in(MemNode::Memory);
667N/A
0N/A if (in_pack(current, pk)) {
667N/A // Forward users of my memory state (except "previous) to my input memory state
0N/A for (DUIterator i = current->outs(); current->has_out(i); i++) {
0N/A Node* use = current->out(i);
667N/A if (use->is_Mem() && use != previous) {
0N/A assert(use->in(MemNode::Memory) == current, "must be");
667N/A if (schedule_before_pack.member(use)) {
3811N/A _igvn.replace_input_of(use, MemNode::Memory, upper_insert_pt);
667N/A } else {
3811N/A _igvn.replace_input_of(use, MemNode::Memory, lower_insert_pt);
667N/A }
0N/A --i; // deleted this edge; rescan position
0N/A }
0N/A }
667N/A previous = current;
667N/A } else { // !in_pack(current, pk) ==> a sandwiched store
667N/A remove_and_insert(current, previous, lower_insert_pt, upper_insert_pt, schedule_before_pack);
0N/A }
667N/A
0N/A if (current == first) break;
0N/A current = my_mem->as_Mem();
667N/A } // end while
3845N/A
3845N/A // Reconnect loads back to upper_insert_pt.
3845N/A for (uint i = 0; i < memops.size(); i++) {
3845N/A Node *ld = memops.at(i);
3845N/A if (ld->in(MemNode::Memory) != upper_insert_pt) {
3845N/A _igvn.replace_input_of(ld, MemNode::Memory, upper_insert_pt);
3845N/A }
3845N/A }
667N/A } else if (pk->at(0)->is_Load()) { //load
952N/A // all loads in the pack should have the same memory state. By default,
952N/A // we use the memory state of the last load. However, if any load could
952N/A // not be moved down due to the dependence constraint, we use the memory
952N/A // state of the first load.
952N/A Node* last_mem = executed_last(pk)->in(MemNode::Memory);
952N/A Node* first_mem = executed_first(pk)->in(MemNode::Memory);
952N/A bool schedule_last = true;
952N/A for (uint i = 0; i < pk->size(); i++) {
952N/A Node* ld = pk->at(i);
952N/A for (Node* current = last_mem; current != ld->in(MemNode::Memory);
952N/A current=current->in(MemNode::Memory)) {
952N/A assert(current != first_mem, "corrupted memory graph");
952N/A if(current->is_Mem() && !independent(current, ld)){
952N/A schedule_last = false; // a later store depends on this load
952N/A break;
952N/A }
952N/A }
952N/A }
952N/A
952N/A Node* mem_input = schedule_last ? last_mem : first_mem;
952N/A _igvn.hash_delete(mem_input);
952N/A // Give each load the same memory state
0N/A for (uint i = 0; i < pk->size(); i++) {
0N/A LoadNode* ld = pk->at(i)->as_Load();
3811N/A _igvn.replace_input_of(ld, MemNode::Memory, mem_input);
0N/A }
0N/A }
0N/A}
0N/A
0N/A//------------------------------output---------------------------
0N/A// Convert packs into vector node operations
0N/Avoid SuperWord::output() {
0N/A if (_packset.length() == 0) return;
0N/A
2292N/A#ifndef PRODUCT
2292N/A if (TraceLoopOpts) {
2292N/A tty->print("SuperWord ");
2292N/A lpt()->dump_head();
2292N/A }
2292N/A#endif
2292N/A
0N/A // MUST ENSURE main loop's initial value is properly aligned:
0N/A // (iv_initial_value + min_iv_offset) % vector_width_in_bytes() == 0
0N/A
0N/A align_initial_loop_index(align_to_ref());
0N/A
0N/A // Insert extract (unpack) operations for scalar uses
0N/A for (int i = 0; i < _packset.length(); i++) {
0N/A insert_extracts(_packset.at(i));
0N/A }
0N/A
4012N/A Compile* C = _phase->C;
4012N/A uint max_vlen_in_bytes = 0;
0N/A for (int i = 0; i < _block.length(); i++) {
0N/A Node* n = _block.at(i);
0N/A Node_List* p = my_pack(n);
0N/A if (p && n == executed_last(p)) {
0N/A uint vlen = p->size();
4012N/A uint vlen_in_bytes = 0;
0N/A Node* vn = NULL;
0N/A Node* low_adr = p->at(0);
0N/A Node* first = executed_first(p);
3845N/A int opc = n->Opcode();
0N/A if (n->is_Load()) {
0N/A Node* ctl = n->in(MemNode::Control);
0N/A Node* mem = first->in(MemNode::Memory);
0N/A Node* adr = low_adr->in(MemNode::Address);
0N/A const TypePtr* atyp = n->adr_type();
4012N/A vn = LoadVectorNode::make(C, opc, ctl, mem, adr, atyp, vlen, velt_basic_type(n));
4012N/A vlen_in_bytes = vn->as_LoadVector()->memory_size();
0N/A } else if (n->is_Store()) {
0N/A // Promote value to be stored to vector
2667N/A Node* val = vector_opd(p, MemNode::ValueIn);
0N/A Node* ctl = n->in(MemNode::Control);
0N/A Node* mem = first->in(MemNode::Memory);
0N/A Node* adr = low_adr->in(MemNode::Address);
0N/A const TypePtr* atyp = n->adr_type();
4012N/A vn = StoreVectorNode::make(C, opc, ctl, mem, adr, atyp, val, vlen);
4012N/A vlen_in_bytes = vn->as_StoreVector()->memory_size();
0N/A } else if (n->req() == 3) {
0N/A // Promote operands to vector
0N/A Node* in1 = vector_opd(p, 1);
0N/A Node* in2 = vector_opd(p, 2);
3964N/A if (VectorNode::is_invariant_vector(in1) && (n->is_Add() || n->is_Mul())) {
3964N/A // Move invariant vector input into second position to avoid register spilling.
3964N/A Node* tmp = in1;
3964N/A in1 = in2;
3964N/A in2 = tmp;
3964N/A }
4012N/A vn = VectorNode::make(C, opc, in1, in2, vlen, velt_basic_type(n));
4012N/A vlen_in_bytes = vn->as_Vector()->length_in_bytes();
0N/A } else {
0N/A ShouldNotReachHere();
0N/A }
3845N/A assert(vn != NULL, "sanity");
4021N/A _igvn.register_new_node_with_optimizer(vn);
0N/A _phase->set_ctrl(vn, _phase->get_ctrl(p->at(0)));
0N/A for (uint j = 0; j < p->size(); j++) {
0N/A Node* pm = p->at(j);
1541N/A _igvn.replace_node(pm, vn);
0N/A }
0N/A _igvn._worklist.push(vn);
4012N/A
4012N/A if (vlen_in_bytes > max_vlen_in_bytes) {
4012N/A max_vlen_in_bytes = vlen_in_bytes;
4012N/A }
3845N/A#ifdef ASSERT
3849N/A if (TraceNewVectors) {
3845N/A tty->print("new Vector node: ");
3845N/A vn->dump();
3845N/A }
3845N/A#endif
0N/A }
0N/A }
4012N/A C->set_max_vector_size(max_vlen_in_bytes);
0N/A}
0N/A
0N/A//------------------------------vector_opd---------------------------
0N/A// Create a vector operand for the nodes in pack p for operand: in(opd_idx)
2667N/ANode* SuperWord::vector_opd(Node_List* p, int opd_idx) {
0N/A Node* p0 = p->at(0);
0N/A uint vlen = p->size();
0N/A Node* opd = p0->in(opd_idx);
0N/A
3969N/A if (same_inputs(p, opd_idx)) {
3845N/A if (opd->is_Vector() || opd->is_LoadVector()) {
3967N/A assert(((opd_idx != 2) || !VectorNode::is_shift(p0)), "shift's count can't be vector");
2667N/A return opd; // input is matching vector
0N/A }
3964N/A if ((opd_idx == 2) && VectorNode::is_shift(p0)) {
3964N/A Compile* C = _phase->C;
3964N/A Node* cnt = opd;
4024N/A // Vector instructions do not mask shift count, do it here.
3964N/A juint mask = (p0->bottom_type() == TypeInt::INT) ? (BitsPerInt - 1) : (BitsPerLong - 1);
3964N/A const TypeInt* t = opd->find_int_type();
3964N/A if (t != NULL && t->is_con()) {
3964N/A juint shift = t->get_con();
3964N/A if (shift > mask) { // Unsigned cmp
3964N/A cnt = ConNode::make(C, TypeInt::make(shift & mask));
3964N/A }
3964N/A } else {
3964N/A if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {
3964N/A cnt = ConNode::make(C, TypeInt::make(mask));
4021N/A _igvn.register_new_node_with_optimizer(cnt);
4022N/A cnt = new (C) AndINode(opd, cnt);
4021N/A _igvn.register_new_node_with_optimizer(cnt);
3964N/A _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
3964N/A }
3964N/A assert(opd->bottom_type()->isa_int(), "int type only");
4024N/A // Move non constant shift count into vector register.
4024N/A cnt = VectorNode::shift_count(C, p0, cnt, vlen, velt_basic_type(p0));
3964N/A }
3964N/A if (cnt != opd) {
4021N/A _igvn.register_new_node_with_optimizer(cnt);
3964N/A _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
3964N/A }
3964N/A return cnt;
3964N/A }
3845N/A assert(!opd->is_StoreVector(), "such vector is not expected here");
3714N/A // Convert scalar input to vector with the same number of elements as
3714N/A // p0's vector. Use p0's type because size of operand's container in
3714N/A // vector should match p0's size regardless operand's size.
3714N/A const Type* p0_t = velt_type(p0);
3714N/A VectorNode* vn = VectorNode::scalar2vector(_phase->C, opd, vlen, p0_t);
0N/A
4021N/A _igvn.register_new_node_with_optimizer(vn);
0N/A _phase->set_ctrl(vn, _phase->get_ctrl(opd));
3845N/A#ifdef ASSERT
3849N/A if (TraceNewVectors) {
3845N/A tty->print("new Vector node: ");
3845N/A vn->dump();
3845N/A }
3845N/A#endif
0N/A return vn;
0N/A }
0N/A
0N/A // Insert pack operation
3845N/A BasicType bt = velt_basic_type(p0);
3845N/A PackNode* pk = PackNode::make(_phase->C, opd, vlen, bt);
3714N/A DEBUG_ONLY( const BasicType opd_bt = opd->bottom_type()->basic_type(); )
0N/A
0N/A for (uint i = 1; i < vlen; i++) {
0N/A Node* pi = p->at(i);
0N/A Node* in = pi->in(opd_idx);
0N/A assert(my_pack(in) == NULL, "Should already have been unpacked");
3714N/A assert(opd_bt == in->bottom_type()->basic_type(), "all same type");
3969N/A pk->add_opd(in);
0N/A }
4021N/A _igvn.register_new_node_with_optimizer(pk);
0N/A _phase->set_ctrl(pk, _phase->get_ctrl(opd));
3845N/A#ifdef ASSERT
4012N/A if (TraceNewVectors) {
4012N/A tty->print("new Vector node: ");
4012N/A pk->dump();
4012N/A }
3845N/A#endif
0N/A return pk;
0N/A}
0N/A
0N/A//------------------------------insert_extracts---------------------------
0N/A// If a use of pack p is not a vector use, then replace the
0N/A// use with an extract operation.
0N/Avoid SuperWord::insert_extracts(Node_List* p) {
0N/A if (p->at(0)->is_Store()) return;
0N/A assert(_n_idx_list.is_empty(), "empty (node,index) list");
0N/A
0N/A // Inspect each use of each pack member. For each use that is
0N/A // not a vector use, replace the use with an extract operation.
0N/A
0N/A for (uint i = 0; i < p->size(); i++) {
0N/A Node* def = p->at(i);
0N/A for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
0N/A Node* use = def->fast_out(j);
0N/A for (uint k = 0; k < use->req(); k++) {
0N/A Node* n = use->in(k);
0N/A if (def == n) {
0N/A if (!is_vector_use(use, k)) {
0N/A _n_idx_list.push(use, k);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A while (_n_idx_list.is_nonempty()) {
0N/A Node* use = _n_idx_list.node();
0N/A int idx = _n_idx_list.index();
0N/A _n_idx_list.pop();
0N/A Node* def = use->in(idx);
0N/A
0N/A // Insert extract operation
0N/A _igvn.hash_delete(def);
0N/A int def_pos = alignment(def) / data_size(def);
0N/A
3845N/A Node* ex = ExtractNode::make(_phase->C, def, def_pos, velt_basic_type(def));
4021N/A _igvn.register_new_node_with_optimizer(ex);
0N/A _phase->set_ctrl(ex, _phase->get_ctrl(def));
3811N/A _igvn.replace_input_of(use, idx, ex);
0N/A _igvn._worklist.push(def);
0N/A
0N/A bb_insert_after(ex, bb_idx(def));
3845N/A set_velt_type(ex, velt_type(def));
0N/A }
0N/A}
0N/A
0N/A//------------------------------is_vector_use---------------------------
0N/A// Is use->in(u_idx) a vector use?
0N/Abool SuperWord::is_vector_use(Node* use, int u_idx) {
0N/A Node_List* u_pk = my_pack(use);
0N/A if (u_pk == NULL) return false;
0N/A Node* def = use->in(u_idx);
0N/A Node_List* d_pk = my_pack(def);
0N/A if (d_pk == NULL) {
0N/A // check for scalar promotion
0N/A Node* n = u_pk->at(0)->in(u_idx);
0N/A for (uint i = 1; i < u_pk->size(); i++) {
0N/A if (u_pk->at(i)->in(u_idx) != n) return false;
0N/A }
0N/A return true;
0N/A }
0N/A if (u_pk->size() != d_pk->size())
0N/A return false;
0N/A for (uint i = 0; i < u_pk->size(); i++) {
0N/A Node* ui = u_pk->at(i);
0N/A Node* di = d_pk->at(i);
0N/A if (ui->in(u_idx) != di || alignment(ui) != alignment(di))
0N/A return false;
0N/A }
0N/A return true;
0N/A}
0N/A
0N/A//------------------------------construct_bb---------------------------
0N/A// Construct reverse postorder list of block members
4242N/Abool SuperWord::construct_bb() {
0N/A Node* entry = bb();
0N/A
0N/A assert(_stk.length() == 0, "stk is empty");
0N/A assert(_block.length() == 0, "block is empty");
0N/A assert(_data_entry.length() == 0, "data_entry is empty");
0N/A assert(_mem_slice_head.length() == 0, "mem_slice_head is empty");
0N/A assert(_mem_slice_tail.length() == 0, "mem_slice_tail is empty");
0N/A
0N/A // Find non-control nodes with no inputs from within block,
0N/A // create a temporary map from node _idx to bb_idx for use
0N/A // by the visited and post_visited sets,
0N/A // and count number of nodes in block.
0N/A int bb_ct = 0;
0N/A for (uint i = 0; i < lpt()->_body.size(); i++ ) {
0N/A Node *n = lpt()->_body.at(i);
0N/A set_bb_idx(n, i); // Create a temporary map
0N/A if (in_bb(n)) {
4242N/A if (n->is_LoadStore() || n->is_MergeMem() ||
4242N/A (n->is_Proj() && !n->as_Proj()->is_CFG())) {
4242N/A // Bailout if the loop has LoadStore, MergeMem or data Proj
4242N/A // nodes. Superword optimization does not work with them.
4242N/A return false;
4242N/A }
0N/A bb_ct++;
0N/A if (!n->is_CFG()) {
0N/A bool found = false;
0N/A for (uint j = 0; j < n->req(); j++) {
0N/A Node* def = n->in(j);
0N/A if (def && in_bb(def)) {
0N/A found = true;
0N/A break;
0N/A }
0N/A }
0N/A if (!found) {
0N/A assert(n != entry, "can't be entry");
0N/A _data_entry.push(n);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Find memory slices (head and tail)
0N/A for (DUIterator_Fast imax, i = lp()->fast_outs(imax); i < imax; i++) {
0N/A Node *n = lp()->fast_out(i);
0N/A if (in_bb(n) && (n->is_Phi() && n->bottom_type() == Type::MEMORY)) {
0N/A Node* n_tail = n->in(LoopNode::LoopBackControl);
253N/A if (n_tail != n->in(LoopNode::EntryControl)) {
4242N/A if (!n_tail->is_Mem()) {
4242N/A assert(n_tail->is_Mem(), err_msg_res("unexpected node for memory slice: %s", n_tail->Name()));
4242N/A return false; // Bailout
4242N/A }
253N/A _mem_slice_head.push(n);
253N/A _mem_slice_tail.push(n_tail);
253N/A }
0N/A }
0N/A }
0N/A
0N/A // Create an RPO list of nodes in block
0N/A
0N/A visited_clear();
0N/A post_visited_clear();
0N/A
0N/A // Push all non-control nodes with no inputs from within block, then control entry
0N/A for (int j = 0; j < _data_entry.length(); j++) {
0N/A Node* n = _data_entry.at(j);
0N/A visited_set(n);
0N/A _stk.push(n);
0N/A }
0N/A visited_set(entry);
0N/A _stk.push(entry);
0N/A
0N/A // Do a depth first walk over out edges
0N/A int rpo_idx = bb_ct - 1;
0N/A int size;
0N/A while ((size = _stk.length()) > 0) {
0N/A Node* n = _stk.top(); // Leave node on stack
0N/A if (!visited_test_set(n)) {
0N/A // forward arc in graph
0N/A } else if (!post_visited_test(n)) {
0N/A // cross or back arc
0N/A for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
0N/A Node *use = n->fast_out(i);
0N/A if (in_bb(use) && !visited_test(use) &&
0N/A // Don't go around backedge
0N/A (!use->is_Phi() || n == entry)) {
0N/A _stk.push(use);
0N/A }
0N/A }
0N/A if (_stk.length() == size) {
0N/A // There were no additional uses, post visit node now
0N/A _stk.pop(); // Remove node from stack
0N/A assert(rpo_idx >= 0, "");
0N/A _block.at_put_grow(rpo_idx, n);
0N/A rpo_idx--;
0N/A post_visited_set(n);
0N/A assert(rpo_idx >= 0 || _stk.is_empty(), "");
0N/A }
0N/A } else {
0N/A _stk.pop(); // Remove post-visited node from stack
0N/A }
0N/A }
0N/A
0N/A // Create real map of block indices for nodes
0N/A for (int j = 0; j < _block.length(); j++) {
0N/A Node* n = _block.at(j);
0N/A set_bb_idx(n, j);
0N/A }
0N/A
0N/A initialize_bb(); // Ensure extra info is allocated.
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord) {
0N/A print_bb();
0N/A tty->print_cr("\ndata entry nodes: %s", _data_entry.length() > 0 ? "" : "NONE");
0N/A for (int m = 0; m < _data_entry.length(); m++) {
0N/A tty->print("%3d ", m);
0N/A _data_entry.at(m)->dump();
0N/A }
0N/A tty->print_cr("\nmemory slices: %s", _mem_slice_head.length() > 0 ? "" : "NONE");
0N/A for (int m = 0; m < _mem_slice_head.length(); m++) {
0N/A tty->print("%3d ", m); _mem_slice_head.at(m)->dump();
0N/A tty->print(" "); _mem_slice_tail.at(m)->dump();
0N/A }
0N/A }
0N/A#endif
0N/A assert(rpo_idx == -1 && bb_ct == _block.length(), "all block members found");
4242N/A return (_mem_slice_head.length() > 0) || (_data_entry.length() > 0);
0N/A}
0N/A
0N/A//------------------------------initialize_bb---------------------------
0N/A// Initialize per node info
0N/Avoid SuperWord::initialize_bb() {
0N/A Node* last = _block.at(_block.length() - 1);
0N/A grow_node_info(bb_idx(last));
0N/A}
0N/A
0N/A//------------------------------bb_insert_after---------------------------
0N/A// Insert n into block after pos
0N/Avoid SuperWord::bb_insert_after(Node* n, int pos) {
0N/A int n_pos = pos + 1;
0N/A // Make room
0N/A for (int i = _block.length() - 1; i >= n_pos; i--) {
0N/A _block.at_put_grow(i+1, _block.at(i));
0N/A }
0N/A for (int j = _node_info.length() - 1; j >= n_pos; j--) {
0N/A _node_info.at_put_grow(j+1, _node_info.at(j));
0N/A }
0N/A // Set value
0N/A _block.at_put_grow(n_pos, n);
0N/A _node_info.at_put_grow(n_pos, SWNodeInfo::initial);
0N/A // Adjust map from node->_idx to _block index
0N/A for (int i = n_pos; i < _block.length(); i++) {
0N/A set_bb_idx(_block.at(i), i);
0N/A }
0N/A}
0N/A
0N/A//------------------------------compute_max_depth---------------------------
0N/A// Compute max depth for expressions from beginning of block
0N/A// Use to prune search paths during test for independence.
0N/Avoid SuperWord::compute_max_depth() {
0N/A int ct = 0;
0N/A bool again;
0N/A do {
0N/A again = false;
0N/A for (int i = 0; i < _block.length(); i++) {
0N/A Node* n = _block.at(i);
0N/A if (!n->is_Phi()) {
0N/A int d_orig = depth(n);
0N/A int d_in = 0;
0N/A for (DepPreds preds(n, _dg); !preds.done(); preds.next()) {
0N/A Node* pred = preds.current();
0N/A if (in_bb(pred)) {
0N/A d_in = MAX2(d_in, depth(pred));
0N/A }
0N/A }
0N/A if (d_in + 1 != d_orig) {
0N/A set_depth(n, d_in + 1);
0N/A again = true;
0N/A }
0N/A }
0N/A }
0N/A ct++;
0N/A } while (again);
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord && Verbose)
0N/A tty->print_cr("compute_max_depth iterated: %d times", ct);
0N/A#endif
0N/A}
0N/A
0N/A//-------------------------compute_vector_element_type-----------------------
0N/A// Compute necessary vector element type for expressions
0N/A// This propagates backwards a narrower integer type when the
0N/A// upper bits of the value are not needed.
0N/A// Example: char a,b,c; a = b + c;
0N/A// Normally the type of the add is integer, but for packed character
0N/A// operations the type of the add needs to be char.
0N/Avoid SuperWord::compute_vector_element_type() {
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord && Verbose)
0N/A tty->print_cr("\ncompute_velt_type:");
0N/A#endif
0N/A
0N/A // Initial type
0N/A for (int i = 0; i < _block.length(); i++) {
0N/A Node* n = _block.at(i);
3845N/A set_velt_type(n, container_type(n));
0N/A }
0N/A
4039N/A // Propagate integer narrowed type backwards through operations
0N/A // that don't depend on higher order bits
0N/A for (int i = _block.length() - 1; i >= 0; i--) {
0N/A Node* n = _block.at(i);
0N/A // Only integer types need be examined
4039N/A const Type* vtn = velt_type(n);
4039N/A if (vtn->basic_type() == T_INT) {
0N/A uint start, end;
3969N/A VectorNode::vector_operands(n, &start, &end);
0N/A
0N/A for (uint j = start; j < end; j++) {
0N/A Node* in = n->in(j);
3964N/A // Don't propagate through a memory
3964N/A if (!in->is_Mem() && in_bb(in) && velt_type(in)->basic_type() == T_INT &&
3964N/A data_size(n) < data_size(in)) {
3964N/A bool same_type = true;
3964N/A for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) {
3964N/A Node *use = in->fast_out(k);
3964N/A if (!in_bb(use) || !same_velt_type(use, n)) {
3964N/A same_type = false;
3964N/A break;
0N/A }
3964N/A }
3964N/A if (same_type) {
4039N/A // For right shifts of small integer types (bool, byte, char, short)
4039N/A // we need precise information about sign-ness. Only Load nodes have
4039N/A // this information because Store nodes are the same for signed and
4039N/A // unsigned values. And any arithmetic operation after a load may
4039N/A // expand a value to signed Int so such right shifts can't be used
4039N/A // because vector elements do not have upper bits of Int.
4039N/A const Type* vt = vtn;
4039N/A if (VectorNode::is_shift(in)) {
4039N/A Node* load = in->in(1);
4041N/A if (load->is_Load() && in_bb(load) && (velt_type(load)->basic_type() == T_INT)) {
4039N/A vt = velt_type(load);
4039N/A } else if (in->Opcode() != Op_LShiftI) {
4039N/A // Widen type to Int to avoid creation of right shift vector
4039N/A // (align + data_size(s1) check in stmts_can_pack() will fail).
4039N/A // Note, left shifts work regardless type.
4039N/A vt = TypeInt::INT;
4039N/A }
4039N/A }
3964N/A set_velt_type(in, vt);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A#ifndef PRODUCT
0N/A if (TraceSuperWord && Verbose) {
0N/A for (int i = 0; i < _block.length(); i++) {
0N/A Node* n = _block.at(i);
0N/A velt_type(n)->dump();
0N/A tty->print("\t");
0N/A n->dump();
0N/A }
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A//------------------------------memory_alignment---------------------------
0N/A// Alignment within a vector memory reference
4014N/Aint SuperWord::memory_alignment(MemNode* s, int iv_adjust) {
0N/A SWPointer p(s, this);
0N/A if (!p.valid()) {
0N/A return bottom_align;
0N/A }
3849N/A int vw = vector_width_in_bytes(s);
3845N/A if (vw < 2) {
3845N/A return bottom_align; // No vectors for this type
3845N/A }
0N/A int offset = p.offset_in_bytes();
4014N/A offset += iv_adjust*p.memory_size();
3845N/A int off_rem = offset % vw;
3845N/A int off_mod = off_rem >= 0 ? off_rem : off_rem + vw;
0N/A return off_mod;
0N/A}
0N/A
0N/A//---------------------------container_type---------------------------
0N/A// Smallest type containing range of values
3845N/Aconst Type* SuperWord::container_type(Node* n) {
3845N/A if (n->is_Mem()) {
4039N/A BasicType bt = n->as_Mem()->memory_type();
4039N/A if (n->is_Store() && (bt == T_CHAR)) {
4039N/A // Use T_SHORT type instead of T_CHAR for stored values because any
4039N/A // preceding arithmetic operation extends values to signed Int.
4039N/A bt = T_SHORT;
4039N/A }
4039N/A if (n->Opcode() == Op_LoadUB) {
4039N/A // Adjust type for unsigned byte loads, it is important for right shifts.
4039N/A // T_BOOLEAN is used because there is no basic type representing type
4039N/A // TypeInt::UBYTE. Use of T_BOOLEAN for vectors is fine because only
4039N/A // size (one byte) and sign is important.
4039N/A bt = T_BOOLEAN;
4039N/A }
4039N/A return Type::get_const_basic_type(bt);
0N/A }
3845N/A const Type* t = _igvn.type(n);
0N/A if (t->basic_type() == T_INT) {
3964N/A // A narrow type of arithmetic operations will be determined by
3964N/A // propagating the type of memory operations.
0N/A return TypeInt::INT;
0N/A }
0N/A return t;
0N/A}
0N/A
3845N/Abool SuperWord::same_velt_type(Node* n1, Node* n2) {
3845N/A const Type* vt1 = velt_type(n1);
4014N/A const Type* vt2 = velt_type(n2);
3845N/A if (vt1->basic_type() == T_INT && vt2->basic_type() == T_INT) {
3845N/A // Compare vectors element sizes for integer types.
3845N/A return data_size(n1) == data_size(n2);
3845N/A }
3845N/A return vt1 == vt2;
3845N/A}
3845N/A
0N/A//------------------------------in_packset---------------------------
0N/A// Are s1 and s2 in a pack pair and ordered as s1,s2?
0N/Abool SuperWord::in_packset(Node* s1, Node* s2) {
0N/A for (int i = 0; i < _packset.length(); i++) {
0N/A Node_List* p = _packset.at(i);
0N/A assert(p->size() == 2, "must be");
0N/A if (p->at(0) == s1 && p->at(p->size()-1) == s2) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A//------------------------------in_pack---------------------------
0N/A// Is s in pack p?
0N/ANode_List* SuperWord::in_pack(Node* s, Node_List* p) {
0N/A for (uint i = 0; i < p->size(); i++) {
0N/A if (p->at(i) == s) {
0N/A return p;
0N/A }
0N/A }
0N/A return NULL;
0N/A}
0N/A
0N/A//------------------------------remove_pack_at---------------------------
0N/A// Remove the pack at position pos in the packset
0N/Avoid SuperWord::remove_pack_at(int pos) {
0N/A Node_List* p = _packset.at(pos);
0N/A for (uint i = 0; i < p->size(); i++) {
0N/A Node* s = p->at(i);
0N/A set_my_pack(s, NULL);
0N/A }
0N/A _packset.remove_at(pos);
0N/A}
0N/A
0N/A//------------------------------executed_first---------------------------
0N/A// Return the node executed first in pack p. Uses the RPO block list
0N/A// to determine order.
0N/ANode* SuperWord::executed_first(Node_List* p) {
0N/A Node* n = p->at(0);
0N/A int n_rpo = bb_idx(n);
0N/A for (uint i = 1; i < p->size(); i++) {
0N/A Node* s = p->at(i);
0N/A int s_rpo = bb_idx(s);
0N/A if (s_rpo < n_rpo) {
0N/A n = s;
0N/A n_rpo = s_rpo;
0N/A }
0N/A }
0N/A return n;
0N/A}
0N/A
0N/A//------------------------------executed_last---------------------------
0N/A// Return the node executed last in pack p.
0N/ANode* SuperWord::executed_last(Node_List* p) {
0N/A Node* n = p->at(0);
0N/A int n_rpo = bb_idx(n);
0N/A for (uint i = 1; i < p->size(); i++) {
0N/A Node* s = p->at(i);
0N/A int s_rpo = bb_idx(s);
0N/A if (s_rpo > n_rpo) {
0N/A n = s;
0N/A n_rpo = s_rpo;
0N/A }
0N/A }
0N/A return n;
0N/A}
0N/A
0N/A//----------------------------align_initial_loop_index---------------------------
0N/A// Adjust pre-loop limit so that in main loop, a load/store reference
0N/A// to align_to_ref will be a position zero in the vector.
0N/A// (iv + k) mod vector_align == 0
0N/Avoid SuperWord::align_initial_loop_index(MemNode* align_to_ref) {
0N/A CountedLoopNode *main_head = lp()->as_CountedLoop();
0N/A assert(main_head->is_main_loop(), "");
0N/A CountedLoopEndNode* pre_end = get_pre_loop_end(main_head);
0N/A assert(pre_end != NULL, "");
0N/A Node *pre_opaq1 = pre_end->limit();
0N/A assert(pre_opaq1->Opcode() == Op_Opaque1, "");
0N/A Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
72N/A Node *lim0 = pre_opaq->in(1);
0N/A
0N/A // Where we put new limit calculations
0N/A Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
0N/A
0N/A // Ensure the original loop limit is available from the
0N/A // pre-loop Opaque1 node.
0N/A Node *orig_limit = pre_opaq->original_loop_limit();
0N/A assert(orig_limit != NULL && _igvn.type(orig_limit) != Type::TOP, "");
0N/A
0N/A SWPointer align_to_ref_p(align_to_ref, this);
3845N/A assert(align_to_ref_p.valid(), "sanity");
0N/A
72N/A // Given:
72N/A // lim0 == original pre loop limit
72N/A // V == v_align (power of 2)
72N/A // invar == extra invariant piece of the address expression
3964N/A // e == offset [ +/- invar ]
72N/A //
72N/A // When reassociating expressions involving '%' the basic rules are:
72N/A // (a - b) % k == 0 => a % k == b % k
72N/A // and:
72N/A // (a + b) % k == 0 => a % k == (k - b) % k
72N/A //
72N/A // For stride > 0 && scale > 0,
72N/A // Derive the new pre-loop limit "lim" such that the two constraints:
72N/A // (1) lim = lim0 + N (where N is some positive integer < V)
72N/A // (2) (e + lim) % V == 0
72N/A // are true.
72N/A //
72N/A // Substituting (1) into (2),
72N/A // (e + lim0 + N) % V == 0
72N/A // solve for N:
72N/A // N = (V - (e + lim0)) % V
72N/A // substitute back into (1), so that new limit
72N/A // lim = lim0 + (V - (e + lim0)) % V
0N/A //
72N/A // For stride > 0 && scale < 0
72N/A // Constraints:
72N/A // lim = lim0 + N
72N/A // (e - lim) % V == 0
72N/A // Solving for lim:
72N/A // (e - lim0 - N) % V == 0
72N/A // N = (e - lim0) % V
72N/A // lim = lim0 + (e - lim0) % V
72N/A //
72N/A // For stride < 0 && scale > 0
72N/A // Constraints:
72N/A // lim = lim0 - N
72N/A // (e + lim) % V == 0
72N/A // Solving for lim:
72N/A // (e + lim0 - N) % V == 0
72N/A // N = (e + lim0) % V
72N/A // lim = lim0 - (e + lim0) % V
72N/A //
72N/A // For stride < 0 && scale < 0
72N/A // Constraints:
72N/A // lim = lim0 - N
72N/A // (e - lim) % V == 0
72N/A // Solving for lim:
72N/A // (e - lim0 + N) % V == 0
72N/A // N = (V - (e - lim0)) % V
72N/A // lim = lim0 - (V - (e - lim0)) % V
0N/A
3849N/A int vw = vector_width_in_bytes(align_to_ref);
72N/A int stride = iv_stride();
72N/A int scale = align_to_ref_p.scale_in_bytes();
0N/A int elt_size = align_to_ref_p.memory_size();
3845N/A int v_align = vw / elt_size;
3849N/A assert(v_align > 1, "sanity");
3964N/A int offset = align_to_ref_p.offset_in_bytes() / elt_size;
3964N/A Node *offsn = _igvn.intcon(offset);
72N/A
3964N/A Node *e = offsn;
0N/A if (align_to_ref_p.invar() != NULL) {
3964N/A // incorporate any extra invariant piece producing (offset +/- invar) >>> log2(elt)
0N/A Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
4022N/A Node* aref = new (_phase->C) URShiftINode(align_to_ref_p.invar(), log2_elt);
4021N/A _igvn.register_new_node_with_optimizer(aref);
0N/A _phase->set_ctrl(aref, pre_ctrl);
72N/A if (align_to_ref_p.negate_invar()) {
4022N/A e = new (_phase->C) SubINode(e, aref);
0N/A } else {
4022N/A e = new (_phase->C) AddINode(e, aref);
0N/A }
4021N/A _igvn.register_new_node_with_optimizer(e);
72N/A _phase->set_ctrl(e, pre_ctrl);
0N/A }
3845N/A if (vw > ObjectAlignmentInBytes) {
3845N/A // incorporate base e +/- base && Mask >>> log2(elt)
4022N/A Node* xbase = new(_phase->C) CastP2XNode(NULL, align_to_ref_p.base());
4021N/A _igvn.register_new_node_with_optimizer(xbase);
3964N/A#ifdef _LP64
4022N/A xbase = new (_phase->C) ConvL2INode(xbase);
4021N/A _igvn.register_new_node_with_optimizer(xbase);
3964N/A#endif
3964N/A Node* mask = _igvn.intcon(vw-1);
4022N/A Node* masked_xbase = new (_phase->C) AndINode(xbase, mask);
4021N/A _igvn.register_new_node_with_optimizer(masked_xbase);
3845N/A Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
4022N/A Node* bref = new (_phase->C) URShiftINode(masked_xbase, log2_elt);
4021N/A _igvn.register_new_node_with_optimizer(bref);
3845N/A _phase->set_ctrl(bref, pre_ctrl);
4022N/A e = new (_phase->C) AddINode(e, bref);
4021N/A _igvn.register_new_node_with_optimizer(e);
3845N/A _phase->set_ctrl(e, pre_ctrl);
3845N/A }
72N/A
72N/A // compute e +/- lim0
72N/A if (scale < 0) {
4022N/A e = new (_phase->C) SubINode(e, lim0);
72N/A } else {
4022N/A e = new (_phase->C) AddINode(e, lim0);
72N/A }
4021N/A _igvn.register_new_node_with_optimizer(e);
72N/A _phase->set_ctrl(e, pre_ctrl);
72N/A
72N/A if (stride * scale > 0) {
72N/A // compute V - (e +/- lim0)
0N/A Node* va = _igvn.intcon(v_align);
4022N/A e = new (_phase->C) SubINode(va, e);
4021N/A _igvn.register_new_node_with_optimizer(e);
72N/A _phase->set_ctrl(e, pre_ctrl);
72N/A }
72N/A // compute N = (exp) % V
72N/A Node* va_msk = _igvn.intcon(v_align - 1);
4022N/A Node* N = new (_phase->C) AndINode(e, va_msk);
4021N/A _igvn.register_new_node_with_optimizer(N);
72N/A _phase->set_ctrl(N, pre_ctrl);
72N/A
72N/A // substitute back into (1), so that new limit
72N/A // lim = lim0 + N
72N/A Node* lim;
72N/A if (stride < 0) {
4022N/A lim = new (_phase->C) SubINode(lim0, N);
0N/A } else {
4022N/A lim = new (_phase->C) AddINode(lim0, N);
0N/A }
4021N/A _igvn.register_new_node_with_optimizer(lim);
72N/A _phase->set_ctrl(lim, pre_ctrl);
0N/A Node* constrained =
4022N/A (stride > 0) ? (Node*) new (_phase->C) MinINode(lim, orig_limit)
4022N/A : (Node*) new (_phase->C) MaxINode(lim, orig_limit);
4021N/A _igvn.register_new_node_with_optimizer(constrained);
0N/A _phase->set_ctrl(constrained, pre_ctrl);
0N/A _igvn.hash_delete(pre_opaq);
0N/A pre_opaq->set_req(1, constrained);
0N/A}
0N/A
0N/A//----------------------------get_pre_loop_end---------------------------
0N/A// Find pre loop end from main loop. Returns null if none.
0N/ACountedLoopEndNode* SuperWord::get_pre_loop_end(CountedLoopNode *cl) {
0N/A Node *ctrl = cl->in(LoopNode::EntryControl);
0N/A if (!ctrl->is_IfTrue() && !ctrl->is_IfFalse()) return NULL;
0N/A Node *iffm = ctrl->in(0);
0N/A if (!iffm->is_If()) return NULL;
0N/A Node *p_f = iffm->in(0);
0N/A if (!p_f->is_IfFalse()) return NULL;
0N/A if (!p_f->in(0)->is_CountedLoopEnd()) return NULL;
0N/A CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
0N/A if (!pre_end->loopnode()->is_pre_loop()) return NULL;
0N/A return pre_end;
0N/A}
0N/A
0N/A
0N/A//------------------------------init---------------------------
0N/Avoid SuperWord::init() {
0N/A _dg.init();
0N/A _packset.clear();
0N/A _disjoint_ptrs.clear();
0N/A _block.clear();
0N/A _data_entry.clear();
0N/A _mem_slice_head.clear();
0N/A _mem_slice_tail.clear();
0N/A _node_info.clear();
0N/A _align_to_ref = NULL;
0N/A _lpt = NULL;
0N/A _lp = NULL;
0N/A _bb = NULL;
0N/A _iv = NULL;
0N/A}
0N/A
0N/A//------------------------------print_packset---------------------------
0N/Avoid SuperWord::print_packset() {
0N/A#ifndef PRODUCT
0N/A tty->print_cr("packset");
0N/A for (int i = 0; i < _packset.length(); i++) {
0N/A tty->print_cr("Pack: %d", i);
0N/A Node_List* p = _packset.at(i);
0N/A print_pack(p);
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A//------------------------------print_pack---------------------------
0N/Avoid SuperWord::print_pack(Node_List* p) {
0N/A for (uint i = 0; i < p->size(); i++) {
0N/A print_stmt(p->at(i));
0N/A }
0N/A}
0N/A
0N/A//------------------------------print_bb---------------------------
0N/Avoid SuperWord::print_bb() {
0N/A#ifndef PRODUCT
0N/A tty->print_cr("\nBlock");
0N/A for (int i = 0; i < _block.length(); i++) {
0N/A Node* n = _block.at(i);
0N/A tty->print("%d ", i);
0N/A if (n) {
0N/A n->dump();
0N/A }
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A//------------------------------print_stmt---------------------------
0N/Avoid SuperWord::print_stmt(Node* s) {
0N/A#ifndef PRODUCT
0N/A tty->print(" align: %d \t", alignment(s));
0N/A s->dump();
0N/A#endif
0N/A}
0N/A
0N/A//------------------------------blank---------------------------
0N/Achar* SuperWord::blank(uint depth) {
0N/A static char blanks[101];
0N/A assert(depth < 101, "too deep");
0N/A for (uint i = 0; i < depth; i++) blanks[i] = ' ';
0N/A blanks[depth] = '\0';
0N/A return blanks;
0N/A}
0N/A
0N/A
0N/A//==============================SWPointer===========================
0N/A
0N/A//----------------------------SWPointer------------------------
0N/ASWPointer::SWPointer(MemNode* mem, SuperWord* slp) :
0N/A _mem(mem), _slp(slp), _base(NULL), _adr(NULL),
0N/A _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {
0N/A
0N/A Node* adr = mem->in(MemNode::Address);
0N/A if (!adr->is_AddP()) {
0N/A assert(!valid(), "too complex");
0N/A return;
0N/A }
0N/A // Match AddP(base, AddP(ptr, k*iv [+ invariant]), constant)
0N/A Node* base = adr->in(AddPNode::Base);
1058N/A //unsafe reference could not be aligned appropriately without runtime checking
1058N/A if (base == NULL || base->bottom_type() == Type::TOP) {
1058N/A assert(!valid(), "unsafe access");
1058N/A return;
1058N/A }
0N/A for (int i = 0; i < 3; i++) {
0N/A if (!scaled_iv_plus_offset(adr->in(AddPNode::Offset))) {
0N/A assert(!valid(), "too complex");
0N/A return;
0N/A }
0N/A adr = adr->in(AddPNode::Address);
0N/A if (base == adr || !adr->is_AddP()) {
0N/A break; // stop looking at addp's
0N/A }
0N/A }
0N/A _base = base;
0N/A _adr = adr;
0N/A assert(valid(), "Usable");
0N/A}
0N/A
0N/A// Following is used to create a temporary object during
0N/A// the pattern match of an address expression.
0N/ASWPointer::SWPointer(SWPointer* p) :
0N/A _mem(p->_mem), _slp(p->_slp), _base(NULL), _adr(NULL),
0N/A _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {}
0N/A
0N/A//------------------------scaled_iv_plus_offset--------------------
0N/A// Match: k*iv + offset
0N/A// where: k is a constant that maybe zero, and
0N/A// offset is (k2 [+/- invariant]) where k2 maybe zero and invariant is optional
0N/Abool SWPointer::scaled_iv_plus_offset(Node* n) {
0N/A if (scaled_iv(n)) {
0N/A return true;
0N/A }
0N/A if (offset_plus_k(n)) {
0N/A return true;
0N/A }
0N/A int opc = n->Opcode();
0N/A if (opc == Op_AddI) {
0N/A if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2))) {
0N/A return true;
0N/A }
0N/A if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
0N/A return true;
0N/A }
0N/A } else if (opc == Op_SubI) {
0N/A if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2), true)) {
0N/A return true;
0N/A }
0N/A if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
0N/A _scale *= -1;
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A//----------------------------scaled_iv------------------------
0N/A// Match: k*iv where k is a constant that's not zero
0N/Abool SWPointer::scaled_iv(Node* n) {
0N/A if (_scale != 0) {
0N/A return false; // already found a scale
0N/A }
0N/A if (n == iv()) {
0N/A _scale = 1;
0N/A return true;
0N/A }
0N/A int opc = n->Opcode();
0N/A if (opc == Op_MulI) {
0N/A if (n->in(1) == iv() && n->in(2)->is_Con()) {
0N/A _scale = n->in(2)->get_int();
0N/A return true;
0N/A } else if (n->in(2) == iv() && n->in(1)->is_Con()) {
0N/A _scale = n->in(1)->get_int();
0N/A return true;
0N/A }
0N/A } else if (opc == Op_LShiftI) {
0N/A if (n->in(1) == iv() && n->in(2)->is_Con()) {
0N/A _scale = 1 << n->in(2)->get_int();
0N/A return true;
0N/A }
0N/A } else if (opc == Op_ConvI2L) {
0N/A if (scaled_iv_plus_offset(n->in(1))) {
0N/A return true;
0N/A }
0N/A } else if (opc == Op_LShiftL) {
0N/A if (!has_iv() && _invar == NULL) {
0N/A // Need to preserve the current _offset value, so
0N/A // create a temporary object for this expression subtree.
0N/A // Hacky, so should re-engineer the address pattern match.
0N/A SWPointer tmp(this);
0N/A if (tmp.scaled_iv_plus_offset(n->in(1))) {
0N/A if (tmp._invar == NULL) {
0N/A int mult = 1 << n->in(2)->get_int();
0N/A _scale = tmp._scale * mult;
0N/A _offset += tmp._offset * mult;
0N/A return true;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A//----------------------------offset_plus_k------------------------
0N/A// Match: offset is (k [+/- invariant])
0N/A// where k maybe zero and invariant is optional, but not both.
0N/Abool SWPointer::offset_plus_k(Node* n, bool negate) {
0N/A int opc = n->Opcode();
0N/A if (opc == Op_ConI) {
0N/A _offset += negate ? -(n->get_int()) : n->get_int();
0N/A return true;
0N/A } else if (opc == Op_ConL) {
0N/A // Okay if value fits into an int
0N/A const TypeLong* t = n->find_long_type();
0N/A if (t->higher_equal(TypeLong::INT)) {
0N/A jlong loff = n->get_long();
0N/A jint off = (jint)loff;
0N/A _offset += negate ? -off : loff;
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A if (_invar != NULL) return false; // already have an invariant
0N/A if (opc == Op_AddI) {
0N/A if (n->in(2)->is_Con() && invariant(n->in(1))) {
0N/A _negate_invar = negate;
0N/A _invar = n->in(1);
0N/A _offset += negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
0N/A return true;
0N/A } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
0N/A _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
0N/A _negate_invar = negate;
0N/A _invar = n->in(2);
0N/A return true;
0N/A }
0N/A }
0N/A if (opc == Op_SubI) {
0N/A if (n->in(2)->is_Con() && invariant(n->in(1))) {
0N/A _negate_invar = negate;
0N/A _invar = n->in(1);
0N/A _offset += !negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
0N/A return true;
0N/A } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
0N/A _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
0N/A _negate_invar = !negate;
0N/A _invar = n->in(2);
0N/A return true;
0N/A }
0N/A }
0N/A if (invariant(n)) {
0N/A _negate_invar = negate;
0N/A _invar = n;
0N/A return true;
0N/A }
0N/A return false;
0N/A}
0N/A
0N/A//----------------------------print------------------------
0N/Avoid SWPointer::print() {
0N/A#ifndef PRODUCT
0N/A tty->print("base: %d adr: %d scale: %d offset: %d invar: %c%d\n",
0N/A _base != NULL ? _base->_idx : 0,
0N/A _adr != NULL ? _adr->_idx : 0,
0N/A _scale, _offset,
0N/A _negate_invar?'-':'+',
0N/A _invar != NULL ? _invar->_idx : 0);
0N/A#endif
0N/A}
0N/A
0N/A// ========================= OrderedPair =====================
0N/A
0N/Aconst OrderedPair OrderedPair::initial;
0N/A
0N/A// ========================= SWNodeInfo =====================
0N/A
0N/Aconst SWNodeInfo SWNodeInfo::initial;
0N/A
0N/A
0N/A// ============================ DepGraph ===========================
0N/A
0N/A//------------------------------make_node---------------------------
0N/A// Make a new dependence graph node for an ideal node.
0N/ADepMem* DepGraph::make_node(Node* node) {
0N/A DepMem* m = new (_arena) DepMem(node);
0N/A if (node != NULL) {
0N/A assert(_map.at_grow(node->_idx) == NULL, "one init only");
0N/A _map.at_put_grow(node->_idx, m);
0N/A }
0N/A return m;
0N/A}
0N/A
0N/A//------------------------------make_edge---------------------------
0N/A// Make a new dependence graph edge from dpred -> dsucc
0N/ADepEdge* DepGraph::make_edge(DepMem* dpred, DepMem* dsucc) {
0N/A DepEdge* e = new (_arena) DepEdge(dpred, dsucc, dsucc->in_head(), dpred->out_head());
0N/A dpred->set_out_head(e);
0N/A dsucc->set_in_head(e);
0N/A return e;
0N/A}
0N/A
0N/A// ========================== DepMem ========================
0N/A
0N/A//------------------------------in_cnt---------------------------
0N/Aint DepMem::in_cnt() {
0N/A int ct = 0;
0N/A for (DepEdge* e = _in_head; e != NULL; e = e->next_in()) ct++;
0N/A return ct;
0N/A}
0N/A
0N/A//------------------------------out_cnt---------------------------
0N/Aint DepMem::out_cnt() {
0N/A int ct = 0;
0N/A for (DepEdge* e = _out_head; e != NULL; e = e->next_out()) ct++;
0N/A return ct;
0N/A}
0N/A
0N/A//------------------------------print-----------------------------
0N/Avoid DepMem::print() {
0N/A#ifndef PRODUCT
0N/A tty->print(" DepNode %d (", _node->_idx);
0N/A for (DepEdge* p = _in_head; p != NULL; p = p->next_in()) {
0N/A Node* pred = p->pred()->node();
0N/A tty->print(" %d", pred != NULL ? pred->_idx : 0);
0N/A }
0N/A tty->print(") [");
0N/A for (DepEdge* s = _out_head; s != NULL; s = s->next_out()) {
0N/A Node* succ = s->succ()->node();
0N/A tty->print(" %d", succ != NULL ? succ->_idx : 0);
0N/A }
0N/A tty->print_cr(" ]");
0N/A#endif
0N/A}
0N/A
0N/A// =========================== DepEdge =========================
0N/A
0N/A//------------------------------DepPreds---------------------------
0N/Avoid DepEdge::print() {
0N/A#ifndef PRODUCT
0N/A tty->print_cr("DepEdge: %d [ %d ]", _pred->node()->_idx, _succ->node()->_idx);
0N/A#endif
0N/A}
0N/A
0N/A// =========================== DepPreds =========================
0N/A// Iterator over predecessor edges in the dependence graph.
0N/A
0N/A//------------------------------DepPreds---------------------------
0N/ADepPreds::DepPreds(Node* n, DepGraph& dg) {
0N/A _n = n;
0N/A _done = false;
0N/A if (_n->is_Store() || _n->is_Load()) {
0N/A _next_idx = MemNode::Address;
0N/A _end_idx = n->req();
0N/A _dep_next = dg.dep(_n)->in_head();
0N/A } else if (_n->is_Mem()) {
0N/A _next_idx = 0;
0N/A _end_idx = 0;
0N/A _dep_next = dg.dep(_n)->in_head();
0N/A } else {
0N/A _next_idx = 1;
0N/A _end_idx = _n->req();
0N/A _dep_next = NULL;
0N/A }
0N/A next();
0N/A}
0N/A
0N/A//------------------------------next---------------------------
0N/Avoid DepPreds::next() {
0N/A if (_dep_next != NULL) {
0N/A _current = _dep_next->pred()->node();
0N/A _dep_next = _dep_next->next_in();
0N/A } else if (_next_idx < _end_idx) {
0N/A _current = _n->in(_next_idx++);
0N/A } else {
0N/A _done = true;
0N/A }
0N/A}
0N/A
0N/A// =========================== DepSuccs =========================
0N/A// Iterator over successor edges in the dependence graph.
0N/A
0N/A//------------------------------DepSuccs---------------------------
0N/ADepSuccs::DepSuccs(Node* n, DepGraph& dg) {
0N/A _n = n;
0N/A _done = false;
0N/A if (_n->is_Load()) {
0N/A _next_idx = 0;
0N/A _end_idx = _n->outcnt();
0N/A _dep_next = dg.dep(_n)->out_head();
0N/A } else if (_n->is_Mem() || _n->is_Phi() && _n->bottom_type() == Type::MEMORY) {
0N/A _next_idx = 0;
0N/A _end_idx = 0;
0N/A _dep_next = dg.dep(_n)->out_head();
0N/A } else {
0N/A _next_idx = 0;
0N/A _end_idx = _n->outcnt();
0N/A _dep_next = NULL;
0N/A }
0N/A next();
0N/A}
0N/A
0N/A//-------------------------------next---------------------------
0N/Avoid DepSuccs::next() {
0N/A if (_dep_next != NULL) {
0N/A _current = _dep_next->succ()->node();
0N/A _dep_next = _dep_next->next_out();
0N/A } else if (_next_idx < _end_idx) {
0N/A _current = _n->raw_out(_next_idx++);
0N/A } else {
0N/A _done = true;
0N/A }
0N/A}