src/share/vm/opto/superword.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7025
b1bc1af04c6e
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial