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