annotate src/hotspot/share/opto/superword.cpp @ 48667:e5da6c246176

8194992: Null pointer dereference in MultiNode::proj_out related to loopexit() Reviewed-by: kvn, thartmann
author dlong
date Thu, 18 Jan 2018 10:05:32 -0800
parents cfde2a53d393
children b242a1e3f9cf
rev   line source
duke@1 1 /*
dlong@48667 2 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 */
duke@1 23
stefank@7397 24 #include "precompiled.hpp"
stefank@7397 25 #include "compiler/compileLog.hpp"
stefank@7397 26 #include "libadt/vectset.hpp"
stefank@7397 27 #include "memory/allocation.inline.hpp"
jprovino@37248 28 #include "memory/resourceArea.hpp"
stefank@7397 29 #include "opto/addnode.hpp"
stefank@7397 30 #include "opto/callnode.hpp"
morris@23528 31 #include "opto/castnode.hpp"
morris@23528 32 #include "opto/convertnode.hpp"
stefank@7397 33 #include "opto/divnode.hpp"
stefank@7397 34 #include "opto/matcher.hpp"
stefank@7397 35 #include "opto/memnode.hpp"
stefank@7397 36 #include "opto/mulnode.hpp"
stefank@7397 37 #include "opto/opcodes.hpp"
morris@23528 38 #include "opto/opaquenode.hpp"
stefank@7397 39 #include "opto/superword.hpp"
stefank@7397 40 #include "opto/vectornode.hpp"
iveresov@33469 41 #include "opto/movenode.hpp"
duke@1 42
duke@1 43 //
duke@1 44 // S U P E R W O R D T R A N S F O R M
duke@1 45 //=============================================================================
duke@1 46
duke@1 47 //------------------------------SuperWord---------------------------
duke@1 48 SuperWord::SuperWord(PhaseIdealLoop* phase) :
duke@1 49 _phase(phase),
duke@1 50 _igvn(phase->_igvn),
duke@1 51 _arena(phase->C->comp_arena()),
duke@1 52 _packset(arena(), 8, 0, NULL), // packs for the current block
duke@1 53 _bb_idx(arena(), (int)(1.10 * phase->C->unique()), 0, 0), // node idx to index in bb
duke@1 54 _block(arena(), 8, 0, NULL), // nodes in current block
mcberg@38049 55 _post_block(arena(), 8, 0, NULL), // nodes common to current block which are marked as post loop vectorizable
duke@1 56 _data_entry(arena(), 8, 0, NULL), // nodes with all inputs from outside
duke@1 57 _mem_slice_head(arena(), 8, 0, NULL), // memory slice heads
duke@1 58 _mem_slice_tail(arena(), 8, 0, NULL), // memory slice tails
duke@1 59 _node_info(arena(), 8, 0, SWNodeInfo::initial), // info needed per node
kvn@30593 60 _clone_map(phase->C->clone_map()), // map of nodes created in cloning
kvn@48164 61 _cmovev_kit(_arena, this), // map to facilitate CMoveV creation
duke@1 62 _align_to_ref(NULL), // memory reference to align vectors to
duke@1 63 _disjoint_ptrs(arena(), 8, 0, OrderedPair::initial), // runtime disambiguated pointer pairs
duke@1 64 _dg(_arena), // dependence graph
duke@1 65 _visited(arena()), // visited node set
duke@1 66 _post_visited(arena()), // post visited node set
duke@1 67 _n_idx_list(arena(), 8), // scratch list of (node,index) pairs
duke@1 68 _stk(arena(), 8, 0, NULL), // scratch stack of nodes
duke@1 69 _nlist(arena(), 8, 0, NULL), // scratch list of nodes
duke@1 70 _lpt(NULL), // loop tree node
duke@1 71 _lp(NULL), // LoopNode
duke@1 72 _bb(NULL), // basic block
kvn@30211 73 _iv(NULL), // induction var
kvn@30588 74 _race_possible(false), // cases where SDMU is true
mcberg@31403 75 _early_return(true), // analysis evaluations routine
kvn@30588 76 _num_work_vecs(0), // amount of vector work we have
kvn@30593 77 _num_reductions(0), // amount of reduction work we have
kvn@30593 78 _do_vector_loop(phase->C->do_vector_loop()), // whether to do vectorization/simd style
iveresov@33469 79 _do_reserve_copy(DoReserveCopyInSuperWord),
kvn@30593 80 _ii_first(-1), // first loop generation index - only if do_vector_loop()
kvn@30593 81 _ii_last(-1), // last loop generation index - only if do_vector_loop()
kvn@31858 82 _ii_order(arena(), 8, 0, 0)
kvn@31858 83 {
kvn@31858 84 #ifndef PRODUCT
kvn@31858 85 _vector_loop_debug = 0;
kvn@31858 86 if (_phase->C->method() != NULL) {
neliasso@33451 87 _vector_loop_debug = phase->C->directive()->VectorizeDebugOption;
kvn@31858 88 }
neliasso@33451 89
kvn@31858 90 #endif
kvn@31858 91 }
duke@1 92
duke@1 93 //------------------------------transform_loop---------------------------
mcberg@31403 94 void SuperWord::transform_loop(IdealLoopTree* lpt, bool do_optimization) {
kvn@13104 95 assert(UseSuperWord, "should be");
kvn@13104 96 // Do vectors exist on this architecture?
kvn@13104 97 if (Matcher::vector_width_in_bytes(T_BYTE) < 2) return;
kvn@13104 98
duke@1 99 assert(lpt->_head->is_CountedLoop(), "must be");
duke@1 100 CountedLoopNode *cl = lpt->_head->as_CountedLoop();
duke@1 101
kvn@10263 102 if (!cl->is_valid_counted_loop()) return; // skip malformed counted loop
kvn@10263 103
mcberg@38049 104 bool post_loop_allowed = (PostLoopMultiversioning && Matcher::has_predicated_vectors() && cl->is_post_loop());
mcberg@38049 105 if (post_loop_allowed) {
mcberg@38049 106 if (cl->is_reduction_loop()) return; // no predication mapping
mcberg@38049 107 Node *limit = cl->limit();
mcberg@38049 108 if (limit->is_Con()) return; // non constant limits only
mcberg@38049 109 // Now check the limit for expressions we do not handle
mcberg@38049 110 if (limit->is_Add()) {
mcberg@38049 111 Node *in2 = limit->in(2);
mcberg@38049 112 if (in2->is_Con()) {
mcberg@38049 113 int val = in2->get_int();
mcberg@38049 114 // should not try to program these cases
mcberg@38049 115 if (val < 0) return;
mcberg@38049 116 }
mcberg@38049 117 }
mcberg@38049 118 }
mcberg@38049 119
mcberg@38049 120 // skip any loop that has not been assigned max unroll by analysis
mcberg@38049 121 if (do_optimization) {
roland@38136 122 if (SuperWordLoopUnrollAnalysis && cl->slp_max_unroll() == 0) return;
mcberg@38049 123 }
mcberg@38049 124
duke@1 125 // Check for no control flow in body (other than exit)
duke@1 126 Node *cl_exit = cl->loopexit();
mcberg@38049 127 if (cl->is_main_loop() && (cl_exit->in(0) != lpt->_head)) {
iveresov@33469 128 #ifndef PRODUCT
iveresov@33469 129 if (TraceSuperWord) {
iveresov@33469 130 tty->print_cr("SuperWord::transform_loop: loop too complicated, cl_exit->in(0) != lpt->_head");
iveresov@33469 131 tty->print("cl_exit %d", cl_exit->_idx); cl_exit->dump();
iveresov@33469 132 tty->print("cl_exit->in(0) %d", cl_exit->in(0)->_idx); cl_exit->in(0)->dump();
iveresov@33469 133 tty->print("lpt->_head %d", lpt->_head->_idx); lpt->_head->dump();
iveresov@33469 134 lpt->dump_head();
iveresov@33469 135 }
iveresov@33469 136 #endif
iveresov@33469 137 return;
iveresov@33469 138 }
duke@1 139
never@352 140 // Make sure the are no extra control users of the loop backedge
never@352 141 if (cl->back_control()->outcnt() != 1) {
never@352 142 return;
never@352 143 }
never@352 144
mcberg@38049 145 // Skip any loops already optimized by slp
mcberg@38049 146 if (cl->is_vectorized_loop()) return;
mcberg@38049 147
zyao@47380 148 if (cl->do_unroll_only()) return;
zyao@47380 149
mcberg@38049 150 if (cl->is_main_loop()) {
mcberg@38049 151 // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit))))
mcberg@38049 152 CountedLoopEndNode* pre_end = get_pre_loop_end(cl);
mcberg@38049 153 if (pre_end == NULL) return;
mcberg@38049 154 Node *pre_opaq1 = pre_end->limit();
mcberg@38049 155 if (pre_opaq1->Opcode() != Op_Opaque1) return;
mcberg@38049 156 }
duke@1 157
duke@1 158 init(); // initialize data structures
duke@1 159
duke@1 160 set_lpt(lpt);
duke@1 161 set_lp(cl);
duke@1 162
kvn@13104 163 // For now, define one block which is the entire loop body
duke@1 164 set_bb(cl);
duke@1 165
mcberg@31403 166 if (do_optimization) {
mcberg@31403 167 assert(_packset.length() == 0, "packset must be empty");
mcberg@31403 168 SLP_extract();
mcberg@38049 169 if (PostLoopMultiversioning && Matcher::has_predicated_vectors()) {
mcberg@38049 170 if (cl->is_vectorized_loop() && cl->is_main_loop() && !cl->is_reduction_loop()) {
mcberg@38049 171 IdealLoopTree *lpt_next = lpt->_next;
mcberg@38049 172 CountedLoopNode *cl_next = lpt_next->_head->as_CountedLoop();
mcberg@38049 173 _phase->has_range_checks(lpt_next);
mcberg@38049 174 if (cl_next->is_post_loop() && !cl_next->range_checks_present()) {
mcberg@38049 175 if (!cl_next->is_vectorized_loop()) {
mcberg@38049 176 int slp_max_unroll_factor = cl->slp_max_unroll();
mcberg@38049 177 cl_next->set_slp_max_unroll(slp_max_unroll_factor);
mcberg@38049 178 }
mcberg@38049 179 }
mcberg@38049 180 }
mcberg@38049 181 }
mcberg@31403 182 }
mcberg@31403 183 }
mcberg@31403 184
mcberg@31403 185 //------------------------------early unrolling analysis------------------------------
kvn@31772 186 void SuperWord::unrolling_analysis(int &local_loop_unroll_factor) {
mcberg@31403 187 bool is_slp = true;
mcberg@31403 188 ResourceMark rm;
mcberg@31403 189 size_t ignored_size = lpt()->_body.size();
mcberg@31403 190 int *ignored_loop_nodes = NEW_RESOURCE_ARRAY(int, ignored_size);
mcberg@31403 191 Node_Stack nstack((int)ignored_size);
kvn@31772 192 CountedLoopNode *cl = lpt()->_head->as_CountedLoop();
dlong@48667 193 Node *cl_exit = cl->loopexit_or_null();
mcberg@38049 194 int rpo_idx = _post_block.length();
mcberg@38049 195
mcberg@38049 196 assert(rpo_idx == 0, "post loop block is empty");
mcberg@31403 197
mcberg@31403 198 // First clear the entries
mcberg@31403 199 for (uint i = 0; i < lpt()->_body.size(); i++) {
mcberg@31403 200 ignored_loop_nodes[i] = -1;
mcberg@31403 201 }
mcberg@31403 202
roland@38235 203 int max_vector = Matcher::max_vector_size(T_BYTE);
mcberg@38049 204 bool post_loop_allowed = (PostLoopMultiversioning && Matcher::has_predicated_vectors() && cl->is_post_loop());
mcberg@31403 205
mcberg@31403 206 // Process the loop, some/all of the stack entries will not be in order, ergo
mcberg@31403 207 // need to preprocess the ignored initial state before we process the loop
mcberg@31403 208 for (uint i = 0; i < lpt()->_body.size(); i++) {
mcberg@31403 209 Node* n = lpt()->_body.at(i);
mcberg@31403 210 if (n == cl->incr() ||
mcberg@31403 211 n->is_reduction() ||
mcberg@31403 212 n->is_AddP() ||
mcberg@31403 213 n->is_Cmp() ||
mcberg@31403 214 n->is_IfTrue() ||
mcberg@31403 215 n->is_CountedLoop() ||
mcberg@31403 216 (n == cl_exit)) {
mcberg@31403 217 ignored_loop_nodes[i] = n->_idx;
mcberg@31403 218 continue;
mcberg@31403 219 }
mcberg@31403 220
mcberg@31403 221 if (n->is_If()) {
mcberg@31403 222 IfNode *iff = n->as_If();
mcberg@31403 223 if (iff->_fcnt != COUNT_UNKNOWN && iff->_prob != PROB_UNKNOWN) {
mcberg@31403 224 if (lpt()->is_loop_exit(iff)) {
mcberg@31403 225 ignored_loop_nodes[i] = n->_idx;
mcberg@31403 226 continue;
mcberg@31403 227 }
mcberg@31403 228 }
mcberg@31403 229 }
mcberg@31403 230
mcberg@31403 231 if (n->is_Phi() && (n->bottom_type() == Type::MEMORY)) {
mcberg@31403 232 Node* n_tail = n->in(LoopNode::LoopBackControl);
mcberg@31403 233 if (n_tail != n->in(LoopNode::EntryControl)) {
mcberg@31403 234 if (!n_tail->is_Mem()) {
mcberg@31403 235 is_slp = false;
mcberg@31403 236 break;
mcberg@31403 237 }
mcberg@31403 238 }
mcberg@31403 239 }
mcberg@31403 240
mcberg@31403 241 // This must happen after check of phi/if
mcberg@31403 242 if (n->is_Phi() || n->is_If()) {
mcberg@31403 243 ignored_loop_nodes[i] = n->_idx;
mcberg@31403 244 continue;
mcberg@31403 245 }
mcberg@31403 246
mcberg@31403 247 if (n->is_LoadStore() || n->is_MergeMem() ||
mcberg@31403 248 (n->is_Proj() && !n->as_Proj()->is_CFG())) {
mcberg@31403 249 is_slp = false;
mcberg@31403 250 break;
mcberg@31403 251 }
mcberg@31403 252
kvn@31520 253 // Ignore nodes with non-primitive type.
kvn@31520 254 BasicType bt;
kvn@31520 255 if (n->is_Mem()) {
kvn@31520 256 bt = n->as_Mem()->memory_type();
kvn@31520 257 } else {
kvn@31520 258 bt = n->bottom_type()->basic_type();
kvn@31520 259 }
kvn@31520 260 if (is_java_primitive(bt) == false) {
kvn@31520 261 ignored_loop_nodes[i] = n->_idx;
kvn@31520 262 continue;
kvn@31520 263 }
kvn@31520 264
mcberg@31403 265 if (n->is_Mem()) {
kvn@31405 266 MemNode* current = n->as_Mem();
mcberg@31403 267 Node* adr = n->in(MemNode::Address);
mcberg@31403 268 Node* n_ctrl = _phase->get_ctrl(adr);
mcberg@31403 269
mcberg@31403 270 // save a queue of post process nodes
mcberg@31403 271 if (n_ctrl != NULL && lpt()->is_member(_phase->get_loop(n_ctrl))) {
mcberg@31403 272 // Process the memory expression
mcberg@31403 273 int stack_idx = 0;
mcberg@31403 274 bool have_side_effects = true;
mcberg@31403 275 if (adr->is_AddP() == false) {
mcberg@31403 276 nstack.push(adr, stack_idx++);
mcberg@31403 277 } else {
mcberg@31403 278 // Mark the components of the memory operation in nstack
mcberg@31403 279 SWPointer p1(current, this, &nstack, true);
mcberg@31403 280 have_side_effects = p1.node_stack()->is_nonempty();
mcberg@31403 281 }
mcberg@31403 282
mcberg@31403 283 // Process the pointer stack
mcberg@31403 284 while (have_side_effects) {
mcberg@31403 285 Node* pointer_node = nstack.node();
mcberg@31403 286 for (uint j = 0; j < lpt()->_body.size(); j++) {
mcberg@31403 287 Node* cur_node = lpt()->_body.at(j);
mcberg@31403 288 if (cur_node == pointer_node) {
mcberg@31403 289 ignored_loop_nodes[j] = cur_node->_idx;
mcberg@31403 290 break;
mcberg@31403 291 }
mcberg@31403 292 }
mcberg@31403 293 nstack.pop();
mcberg@31403 294 have_side_effects = nstack.is_nonempty();
mcberg@31403 295 }
mcberg@31403 296 }
mcberg@31403 297 }
mcberg@31403 298 }
mcberg@31403 299
mcberg@31403 300 if (is_slp) {
mcberg@31403 301 // Now we try to find the maximum supported consistent vector which the machine
mcberg@31403 302 // description can use
mcberg@38049 303 bool small_basic_type = false;
vdeshpande@46692 304 bool flag_small_bt = false;
mcberg@31403 305 for (uint i = 0; i < lpt()->_body.size(); i++) {
mcberg@31403 306 if (ignored_loop_nodes[i] != -1) continue;
mcberg@31403 307
mcberg@31403 308 BasicType bt;
mcberg@31403 309 Node* n = lpt()->_body.at(i);
kvn@31520 310 if (n->is_Mem()) {
mcberg@31403 311 bt = n->as_Mem()->memory_type();
kvn@31405 312 } else {
mcberg@31403 313 bt = n->bottom_type()->basic_type();
mcberg@31403 314 }
mcberg@38049 315
mcberg@38049 316 if (post_loop_allowed) {
mcberg@38049 317 if (!small_basic_type) {
mcberg@38049 318 switch (bt) {
mcberg@38049 319 case T_CHAR:
mcberg@38049 320 case T_BYTE:
mcberg@38049 321 case T_SHORT:
mcberg@38049 322 small_basic_type = true;
mcberg@38049 323 break;
mcberg@38049 324
mcberg@38049 325 case T_LONG:
mcberg@38049 326 // TODO: Remove when support completed for mask context with LONG.
mcberg@38049 327 // Support needs to be augmented for logical qword operations, currently we map to dword
mcberg@38049 328 // buckets for vectors on logicals as these were legacy.
mcberg@38049 329 small_basic_type = true;
mcberg@38049 330 break;
jwilhelm@46630 331
jwilhelm@46630 332 default:
jwilhelm@46630 333 break;
mcberg@38049 334 }
mcberg@38049 335 }
mcberg@38049 336 }
mcberg@38049 337
kvn@31520 338 if (is_java_primitive(bt) == false) continue;
mcberg@31403 339
vdeshpande@46692 340 int cur_max_vector = Matcher::max_vector_size(bt);
mcberg@31403 341
mcberg@31403 342 // If a max vector exists which is not larger than _local_loop_unroll_factor
mcberg@31403 343 // stop looking, we already have the max vector to map to.
kvn@31772 344 if (cur_max_vector < local_loop_unroll_factor) {
mcberg@31403 345 is_slp = false;
twisti@34174 346 if (TraceSuperWordLoopUnrollAnalysis) {
twisti@34174 347 tty->print_cr("slp analysis fails: unroll limit greater than max vector\n");
twisti@34174 348 }
mcberg@31403 349 break;
mcberg@31403 350 }
mcberg@31403 351
mcberg@31403 352 // Map the maximal common vector
mcberg@31403 353 if (VectorNode::implemented(n->Opcode(), cur_max_vector, bt)) {
vdeshpande@46692 354 if (cur_max_vector < max_vector && !flag_small_bt) {
mcberg@31403 355 max_vector = cur_max_vector;
vdeshpande@46692 356 } else if (cur_max_vector > max_vector && UseSubwordForMaxVector) {
vdeshpande@46692 357 // Analyse subword in the loop to set maximum vector size to take advantage of full vector width for subword types.
vdeshpande@46692 358 // Here we analyze if narrowing is likely to happen and if it is we set vector size more aggressively.
vdeshpande@46692 359 // We check for possibility of narrowing by looking through chain operations using subword types.
vdeshpande@46692 360 if (is_subword_type(bt)) {
vdeshpande@46692 361 uint start, end;
vdeshpande@46692 362 VectorNode::vector_operands(n, &start, &end);
vdeshpande@46692 363
vdeshpande@46692 364 for (uint j = start; j < end; j++) {
vdeshpande@46692 365 Node* in = n->in(j);
vdeshpande@46692 366 // Don't propagate through a memory
vdeshpande@46692 367 if (!in->is_Mem() && in_bb(in) && in->bottom_type()->basic_type() == T_INT) {
vdeshpande@46692 368 bool same_type = true;
vdeshpande@46692 369 for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) {
vdeshpande@46692 370 Node *use = in->fast_out(k);
vdeshpande@46692 371 if (!in_bb(use) && use->bottom_type()->basic_type() != bt) {
vdeshpande@46692 372 same_type = false;
vdeshpande@46692 373 break;
vdeshpande@46692 374 }
vdeshpande@46692 375 }
vdeshpande@46692 376 if (same_type) {
vdeshpande@46692 377 max_vector = cur_max_vector;
vdeshpande@46692 378 flag_small_bt = true;
vdeshpande@46692 379 }
vdeshpande@46692 380 }
vdeshpande@46692 381 }
vdeshpande@46692 382 }
mcberg@31403 383 }
mcberg@38049 384 // We only process post loops on predicated targets where we want to
mcberg@38049 385 // mask map the loop to a single iteration
mcberg@38049 386 if (post_loop_allowed) {
mcberg@38049 387 _post_block.at_put_grow(rpo_idx++, n);
mcberg@38049 388 }
mcberg@31403 389 }
mcberg@31403 390 }
mcberg@31403 391 if (is_slp) {
mcberg@31403 392 local_loop_unroll_factor = max_vector;
kvn@31772 393 cl->mark_passed_slp();
mcberg@31403 394 }
kvn@31772 395 cl->mark_was_slp();
mcberg@38049 396 if (cl->is_main_loop()) {
mcberg@38049 397 cl->set_slp_max_unroll(local_loop_unroll_factor);
mcberg@38049 398 } else if (post_loop_allowed) {
mcberg@38049 399 if (!small_basic_type) {
mcberg@38049 400 // avoid replication context for small basic types in programmable masked loops
mcberg@38049 401 cl->set_slp_max_unroll(local_loop_unroll_factor);
mcberg@38049 402 }
mcberg@38049 403 }
mcberg@31403 404 }
duke@1 405 }
duke@1 406
duke@1 407 //------------------------------SLP_extract---------------------------
duke@1 408 // Extract the superword level parallelism
duke@1 409 //
duke@1 410 // 1) A reverse post-order of nodes in the block is constructed. By scanning
duke@1 411 // this list from first to last, all definitions are visited before their uses.
duke@1 412 //
duke@1 413 // 2) A point-to-point dependence graph is constructed between memory references.
duke@1 414 // This simplies the upcoming "independence" checker.
duke@1 415 //
duke@1 416 // 3) The maximum depth in the node graph from the beginning of the block
duke@1 417 // to each node is computed. This is used to prune the graph search
duke@1 418 // in the independence checker.
duke@1 419 //
duke@1 420 // 4) For integer types, the necessary bit width is propagated backwards
duke@1 421 // from stores to allow packed operations on byte, char, and short
duke@1 422 // integers. This reverses the promotion to type "int" that javac
duke@1 423 // did for operations like: char c1,c2,c3; c1 = c2 + c3.
duke@1 424 //
duke@1 425 // 5) One of the memory references is picked to be an aligned vector reference.
duke@1 426 // The pre-loop trip count is adjusted to align this reference in the
duke@1 427 // unrolled body.
duke@1 428 //
duke@1 429 // 6) The initial set of pack pairs is seeded with memory references.
duke@1 430 //
duke@1 431 // 7) The set of pack pairs is extended by following use->def and def->use links.
duke@1 432 //
duke@1 433 // 8) The pairs are combined into vector sized packs.
duke@1 434 //
duke@1 435 // 9) Reorder the memory slices to co-locate members of the memory packs.
duke@1 436 //
duke@1 437 // 10) Generate ideal vector nodes for the final set of packs and where necessary,
duke@1 438 // inserting scalar promotion, vector creation from multiple scalars, and
duke@1 439 // extraction of scalar values from vectors.
duke@1 440 //
duke@1 441 void SuperWord::SLP_extract() {
duke@1 442
kvn@30593 443 #ifndef PRODUCT
kvn@30593 444 if (_do_vector_loop && TraceSuperWord) {
kvn@30593 445 tty->print("SuperWord::SLP_extract\n");
kvn@30593 446 tty->print("input loop\n");
kvn@30593 447 _lpt->dump_head();
kvn@30593 448 _lpt->dump();
kvn@30593 449 for (uint i = 0; i < _lpt->_body.size(); i++) {
kvn@30593 450 _lpt->_body.at(i)->dump();
kvn@30593 451 }
kvn@30593 452 }
kvn@30593 453 #endif
duke@1 454 // Ready the block
kvn@30593 455 if (!construct_bb()) {
kvn@15755 456 return; // Exit if no interesting nodes or complex graph.
kvn@30593 457 }
mcberg@38049 458
kvn@30593 459 // build _dg, _disjoint_ptrs
duke@1 460 dependence_graph();
duke@1 461
kvn@30593 462 // compute function depth(Node*)
duke@1 463 compute_max_depth();
duke@1 464
mcberg@38049 465 CountedLoopNode *cl = lpt()->_head->as_CountedLoop();
mcberg@38049 466 bool post_loop_allowed = (PostLoopMultiversioning && Matcher::has_predicated_vectors() && cl->is_post_loop());
mcberg@38049 467 if (cl->is_main_loop()) {
mcberg@38049 468 if (_do_vector_loop) {
mcberg@38049 469 if (mark_generations() != -1) {
mcberg@38049 470 hoist_loads_in_graph(); // this only rebuild the graph; all basic structs need rebuild explicitly
mcberg@38049 471
mcberg@38049 472 if (!construct_bb()) {
mcberg@38049 473 return; // Exit if no interesting nodes or complex graph.
mcberg@38049 474 }
mcberg@38049 475 dependence_graph();
mcberg@38049 476 compute_max_depth();
kvn@30593 477 }
mcberg@38049 478
mcberg@38049 479 #ifndef PRODUCT
mcberg@38049 480 if (TraceSuperWord) {
mcberg@38049 481 tty->print_cr("\nSuperWord::_do_vector_loop: graph after hoist_loads_in_graph");
mcberg@38049 482 _lpt->dump_head();
mcberg@38049 483 for (int j = 0; j < _block.length(); j++) {
mcberg@38049 484 Node* n = _block.at(j);
mcberg@38049 485 int d = depth(n);
mcberg@38049 486 for (int i = 0; i < d; i++) tty->print("%s", " ");
mcberg@38049 487 tty->print("%d :", d);
mcberg@38049 488 n->dump();
mcberg@38049 489 }
mcberg@38049 490 }
mcberg@38049 491 #endif
kvn@30593 492 }
kvn@30593 493
mcberg@38049 494 compute_vector_element_type();
mcberg@38049 495
mcberg@38049 496 // Attempt vectorization
mcberg@38049 497
mcberg@38049 498 find_adjacent_refs();
mcberg@38049 499
mcberg@38049 500 extend_packlist();
mcberg@38049 501
mcberg@38049 502 if (_do_vector_loop) {
mcberg@38049 503 if (_packset.length() == 0) {
mcberg@38049 504 if (TraceSuperWord) {
mcberg@38049 505 tty->print_cr("\nSuperWord::_do_vector_loop DFA could not build packset, now trying to build anyway");
mcberg@38049 506 }
mcberg@38049 507 pack_parallel();
kvn@30593 508 }
kvn@30593 509 }
mcberg@38049 510
mcberg@38049 511 combine_packs();
mcberg@38049 512
mcberg@38049 513 construct_my_pack_map();
kvn@48164 514 if (UseVectorCmov) {
mcberg@38049 515 merge_packs_to_cmovd();
mcberg@38049 516 }
mcberg@38049 517
mcberg@38049 518 filter_packs();
mcberg@38049 519
mcberg@38049 520 schedule();
mcberg@38049 521 } else if (post_loop_allowed) {
mcberg@38049 522 int saved_mapped_unroll_factor = cl->slp_max_unroll();
mcberg@38049 523 if (saved_mapped_unroll_factor) {
mcberg@38049 524 int vector_mapped_unroll_factor = saved_mapped_unroll_factor;
mcberg@38049 525
mcberg@38049 526 // now reset the slp_unroll_factor so that we can check the analysis mapped
mcberg@38049 527 // what the vector loop was mapped to
mcberg@38049 528 cl->set_slp_max_unroll(0);
mcberg@38049 529
mcberg@38049 530 // do the analysis on the post loop
mcberg@38049 531 unrolling_analysis(vector_mapped_unroll_factor);
mcberg@38049 532
mcberg@38049 533 // if our analyzed loop is a canonical fit, start processing it
mcberg@38049 534 if (vector_mapped_unroll_factor == saved_mapped_unroll_factor) {
mcberg@38049 535 // now add the vector nodes to packsets
mcberg@38049 536 for (int i = 0; i < _post_block.length(); i++) {
mcberg@38049 537 Node* n = _post_block.at(i);
mcberg@38049 538 Node_List* singleton = new Node_List();
mcberg@38049 539 singleton->push(n);
mcberg@38049 540 _packset.append(singleton);
mcberg@38049 541 set_my_pack(n, singleton);
mcberg@38049 542 }
mcberg@38049 543
mcberg@38049 544 // map base types for vector usage
mcberg@38049 545 compute_vector_element_type();
mcberg@38049 546 } else {
mcberg@38049 547 return;
kvn@30593 548 }
mcberg@38049 549 } else {
mcberg@38049 550 // for some reason we could not map the slp analysis state of the vectorized loop
mcberg@38049 551 return;
kvn@30593 552 }
kvn@30593 553 }
kvn@30593 554
duke@1 555 output();
duke@1 556 }
duke@1 557
duke@1 558 //------------------------------find_adjacent_refs---------------------------
duke@1 559 // Find the adjacent memory references and create pack pairs for them.
duke@1 560 // This is the initial set of packs that will then be extended by
duke@1 561 // following use->def and def->use links. The align positions are
duke@1 562 // assigned relative to the reference "align_to_ref"
duke@1 563 void SuperWord::find_adjacent_refs() {
duke@1 564 // Get list of memory operations
duke@1 565 Node_List memops;
duke@1 566 for (int i = 0; i < _block.length(); i++) {
duke@1 567 Node* n = _block.at(i);
kvn@13104 568 if (n->is_Mem() && !n->is_LoadStore() && in_bb(n) &&
kvn@202 569 is_java_primitive(n->as_Mem()->memory_type())) {
duke@1 570 int align = memory_alignment(n->as_Mem(), 0);
duke@1 571 if (align != bottom_align) {
duke@1 572 memops.push(n);
duke@1 573 }
duke@1 574 }
duke@1 575 }
duke@1 576
kvn@13104 577 Node_List align_to_refs;
kvn@13104 578 int best_iv_adjustment = 0;
kvn@13104 579 MemNode* best_align_to_mem_ref = NULL;
duke@1 580
kvn@13104 581 while (memops.size() != 0) {
kvn@13104 582 // Find a memory reference to align to.
kvn@13104 583 MemNode* mem_ref = find_align_to_ref(memops);
kvn@13104 584 if (mem_ref == NULL) break;
kvn@13104 585 align_to_refs.push(mem_ref);
kvn@13104 586 int iv_adjustment = get_iv_adjustment(mem_ref);
duke@1 587
kvn@13104 588 if (best_align_to_mem_ref == NULL) {
kvn@13104 589 // Set memory reference which is the best from all memory operations
kvn@13104 590 // to be used for alignment. The pre-loop trip count is modified to align
kvn@13104 591 // this reference to a vector-aligned address.
kvn@13104 592 best_align_to_mem_ref = mem_ref;
kvn@13104 593 best_iv_adjustment = iv_adjustment;
kvn@31858 594 NOT_PRODUCT(find_adjacent_refs_trace_1(best_align_to_mem_ref, best_iv_adjustment);)
kvn@13104 595 }
duke@1 596
mcberg@31403 597 SWPointer align_to_ref_p(mem_ref, this, NULL, false);
kvn@13104 598 // Set alignment relative to "align_to_ref" for all related memory operations.
kvn@13104 599 for (int i = memops.size() - 1; i >= 0; i--) {
kvn@13104 600 MemNode* s = memops.at(i)->as_Mem();
kvn@31858 601 if (isomorphic(s, mem_ref) &&
kvn@31858 602 (!_do_vector_loop || same_origin_idx(s, mem_ref))) {
mcberg@31403 603 SWPointer p2(s, this, NULL, false);
kvn@13104 604 if (p2.comparable(align_to_ref_p)) {
kvn@13104 605 int align = memory_alignment(s, iv_adjustment);
kvn@13104 606 set_alignment(s, align);
duke@1 607 }
duke@1 608 }
duke@1 609 }
kvn@13104 610
kvn@13104 611 // Create initial pack pairs of memory operations for which
kvn@13104 612 // alignment is set and vectors will be aligned.
kvn@13104 613 bool create_pack = true;
kvn@30593 614 if (memory_alignment(mem_ref, best_iv_adjustment) == 0 || _do_vector_loop) {
kvn@13108 615 if (!Matcher::misaligned_vectors_ok()) {
kvn@13108 616 int vw = vector_width(mem_ref);
kvn@13108 617 int vw_best = vector_width(best_align_to_mem_ref);
kvn@13108 618 if (vw > vw_best) {
kvn@13108 619 // Do not vectorize a memory access with more elements per vector
kvn@13108 620 // if unaligned memory access is not allowed because number of
kvn@13108 621 // iterations in pre-loop will be not enough to align it.
kvn@13108 622 create_pack = false;
thartmann@30625 623 } else {
mcberg@31403 624 SWPointer p2(best_align_to_mem_ref, this, NULL, false);
thartmann@30625 625 if (align_to_ref_p.invar() != p2.invar()) {
thartmann@30625 626 // Do not vectorize memory accesses with different invariants
thartmann@30625 627 // if unaligned memory accesses are not allowed.
thartmann@30625 628 create_pack = false;
thartmann@30625 629 }
kvn@13108 630 }
kvn@13108 631 }
kvn@13108 632 } else {
kvn@13104 633 if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
kvn@13104 634 // Can't allow vectorization of unaligned memory accesses with the
kvn@13104 635 // same type since it could be overlapped accesses to the same array.
kvn@13104 636 create_pack = false;
kvn@13104 637 } else {
kvn@13104 638 // Allow independent (different type) unaligned memory operations
kvn@13104 639 // if HW supports them.
kvn@13104 640 if (!Matcher::misaligned_vectors_ok()) {
kvn@13104 641 create_pack = false;
kvn@13104 642 } else {
kvn@13104 643 // Check if packs of the same memory type but
kvn@13104 644 // with a different alignment were created before.
kvn@13104 645 for (uint i = 0; i < align_to_refs.size(); i++) {
kvn@13104 646 MemNode* mr = align_to_refs.at(i)->as_Mem();
kvn@13104 647 if (same_velt_type(mr, mem_ref) &&
kvn@13104 648 memory_alignment(mr, iv_adjustment) != 0)
kvn@13104 649 create_pack = false;
kvn@13104 650 }
kvn@13104 651 }
kvn@13104 652 }
kvn@13104 653 }
kvn@13104 654 if (create_pack) {
kvn@13104 655 for (uint i = 0; i < memops.size(); i++) {
kvn@13104 656 Node* s1 = memops.at(i);
kvn@13104 657 int align = alignment(s1);
kvn@13104 658 if (align == top_align) continue;
kvn@13104 659 for (uint j = 0; j < memops.size(); j++) {
kvn@13104 660 Node* s2 = memops.at(j);
kvn@13104 661 if (alignment(s2) == top_align) continue;
kvn@13104 662 if (s1 != s2 && are_adjacent_refs(s1, s2)) {
kvn@13104 663 if (stmts_can_pack(s1, s2, align)) {
kvn@13104 664 Node_List* pair = new Node_List();
kvn@13104 665 pair->push(s1);
kvn@13104 666 pair->push(s2);
kvn@31858 667 if (!_do_vector_loop || same_origin_idx(s1, s2)) {
kvn@30593 668 _packset.append(pair);
kvn@30593 669 }
kvn@13104 670 }
kvn@13104 671 }
kvn@13104 672 }
kvn@13104 673 }
kvn@13104 674 } else { // Don't create unaligned pack
kvn@13104 675 // First, remove remaining memory ops of the same type from the list.
kvn@13104 676 for (int i = memops.size() - 1; i >= 0; i--) {
kvn@13104 677 MemNode* s = memops.at(i)->as_Mem();
kvn@13104 678 if (same_velt_type(s, mem_ref)) {
kvn@13104 679 memops.remove(i);
kvn@13104 680 }
kvn@13104 681 }
kvn@13104 682
kvn@13104 683 // Second, remove already constructed packs of the same type.
kvn@13104 684 for (int i = _packset.length() - 1; i >= 0; i--) {
kvn@13104 685 Node_List* p = _packset.at(i);
kvn@13104 686 MemNode* s = p->at(0)->as_Mem();
kvn@13104 687 if (same_velt_type(s, mem_ref)) {
kvn@13104 688 remove_pack_at(i);
kvn@13104 689 }
kvn@13104 690 }
kvn@13104 691
kvn@13104 692 // If needed find the best memory reference for loop alignment again.
kvn@13104 693 if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
kvn@13104 694 // Put memory ops from remaining packs back on memops list for
kvn@13104 695 // the best alignment search.
kvn@13104 696 uint orig_msize = memops.size();
kvn@13104 697 for (int i = 0; i < _packset.length(); i++) {
kvn@13104 698 Node_List* p = _packset.at(i);
kvn@13104 699 MemNode* s = p->at(0)->as_Mem();
kvn@13104 700 assert(!same_velt_type(s, mem_ref), "sanity");
kvn@13104 701 memops.push(s);
kvn@13104 702 }
kvn@34168 703 best_align_to_mem_ref = find_align_to_ref(memops);
kvn@31858 704 if (best_align_to_mem_ref == NULL) {
twisti@34174 705 if (TraceSuperWord) {
twisti@34174 706 tty->print_cr("SuperWord::find_adjacent_refs(): best_align_to_mem_ref == NULL");
twisti@34174 707 }
kvn@31858 708 break;
kvn@31858 709 }
kvn@13104 710 best_iv_adjustment = get_iv_adjustment(best_align_to_mem_ref);
kvn@31858 711 NOT_PRODUCT(find_adjacent_refs_trace_1(best_align_to_mem_ref, best_iv_adjustment);)
kvn@13104 712 // Restore list.
kvn@13104 713 while (memops.size() > orig_msize)
kvn@13104 714 (void)memops.pop();
kvn@13104 715 }
kvn@13104 716 } // unaligned memory accesses
kvn@13104 717
kvn@13104 718 // Remove used mem nodes.
kvn@13104 719 for (int i = memops.size() - 1; i >= 0; i--) {
kvn@13104 720 MemNode* m = memops.at(i)->as_Mem();
kvn@13104 721 if (alignment(m) != top_align) {
kvn@13104 722 memops.remove(i);
kvn@13104 723 }
kvn@13104 724 }
kvn@13104 725
kvn@13104 726 } // while (memops.size() != 0
kvn@13104 727 set_align_to_ref(best_align_to_mem_ref);
duke@1 728
duke@1 729 if (TraceSuperWord) {
duke@1 730 tty->print_cr("\nAfter find_adjacent_refs");
duke@1 731 print_packset();
duke@1 732 }
duke@1 733 }
duke@1 734
kvn@31858 735 #ifndef PRODUCT
kvn@31858 736 void SuperWord::find_adjacent_refs_trace_1(Node* best_align_to_mem_ref, int best_iv_adjustment) {
kvn@31858 737 if (is_trace_adjacent()) {
kvn@31858 738 tty->print("SuperWord::find_adjacent_refs best_align_to_mem_ref = %d, best_iv_adjustment = %d",
kvn@31858 739 best_align_to_mem_ref->_idx, best_iv_adjustment);
kvn@31858 740 best_align_to_mem_ref->dump();
kvn@31858 741 }
kvn@31858 742 }
kvn@31858 743 #endif
kvn@31858 744
duke@1 745 //------------------------------find_align_to_ref---------------------------
duke@1 746 // Find a memory reference to align the loop induction variable to.
duke@1 747 // Looks first at stores then at loads, looking for a memory reference
duke@1 748 // with the largest number of references similar to it.
kvn@13104 749 MemNode* SuperWord::find_align_to_ref(Node_List &memops) {
duke@1 750 GrowableArray<int> cmp_ct(arena(), memops.size(), memops.size(), 0);
duke@1 751
duke@1 752 // Count number of comparable memory ops
duke@1 753 for (uint i = 0; i < memops.size(); i++) {
duke@1 754 MemNode* s1 = memops.at(i)->as_Mem();
mcberg@31403 755 SWPointer p1(s1, this, NULL, false);
duke@1 756 // Discard if pre loop can't align this reference
duke@1 757 if (!ref_is_alignable(p1)) {
duke@1 758 *cmp_ct.adr_at(i) = 0;
duke@1 759 continue;
duke@1 760 }
duke@1 761 for (uint j = i+1; j < memops.size(); j++) {
duke@1 762 MemNode* s2 = memops.at(j)->as_Mem();
duke@1 763 if (isomorphic(s1, s2)) {
mcberg@31403 764 SWPointer p2(s2, this, NULL, false);
duke@1 765 if (p1.comparable(p2)) {
duke@1 766 (*cmp_ct.adr_at(i))++;
duke@1 767 (*cmp_ct.adr_at(j))++;
duke@1 768 }
duke@1 769 }
duke@1 770 }
duke@1 771 }
duke@1 772
kvn@13104 773 // Find Store (or Load) with the greatest number of "comparable" references,
kvn@13104 774 // biggest vector size, smallest data size and smallest iv offset.
duke@1 775 int max_ct = 0;
kvn@13104 776 int max_vw = 0;
duke@1 777 int max_idx = -1;
duke@1 778 int min_size = max_jint;
duke@1 779 int min_iv_offset = max_jint;
duke@1 780 for (uint j = 0; j < memops.size(); j++) {
duke@1 781 MemNode* s = memops.at(j)->as_Mem();
duke@1 782 if (s->is_Store()) {
kvn@13108 783 int vw = vector_width_in_bytes(s);
kvn@13104 784 assert(vw > 1, "sanity");
mcberg@31403 785 SWPointer p(s, this, NULL, false);
jwilhelm@46630 786 if ( cmp_ct.at(j) > max_ct ||
jwilhelm@46630 787 (cmp_ct.at(j) == max_ct &&
jwilhelm@46630 788 ( vw > max_vw ||
jwilhelm@46630 789 (vw == max_vw &&
jwilhelm@46630 790 ( data_size(s) < min_size ||
jwilhelm@46630 791 (data_size(s) == min_size &&
jwilhelm@46630 792 p.offset_in_bytes() < min_iv_offset)))))) {
duke@1 793 max_ct = cmp_ct.at(j);
kvn@13104 794 max_vw = vw;
duke@1 795 max_idx = j;
duke@1 796 min_size = data_size(s);
duke@1 797 min_iv_offset = p.offset_in_bytes();
duke@1 798 }
duke@1 799 }
duke@1 800 }
duke@1 801 // If no stores, look at loads
duke@1 802 if (max_ct == 0) {
duke@1 803 for (uint j = 0; j < memops.size(); j++) {
duke@1 804 MemNode* s = memops.at(j)->as_Mem();
duke@1 805 if (s->is_Load()) {
kvn@13108 806 int vw = vector_width_in_bytes(s);
kvn@13104 807 assert(vw > 1, "sanity");
mcberg@31403 808 SWPointer p(s, this, NULL, false);
jwilhelm@46630 809 if ( cmp_ct.at(j) > max_ct ||
jwilhelm@46630 810 (cmp_ct.at(j) == max_ct &&
jwilhelm@46630 811 ( vw > max_vw ||
jwilhelm@46630 812 (vw == max_vw &&
jwilhelm@46630 813 ( data_size(s) < min_size ||
jwilhelm@46630 814 (data_size(s) == min_size &&
jwilhelm@46630 815 p.offset_in_bytes() < min_iv_offset)))))) {
duke@1 816 max_ct = cmp_ct.at(j);
kvn@13104 817 max_vw = vw;
duke@1 818 max_idx = j;
duke@1 819 min_size = data_size(s);
duke@1 820 min_iv_offset = p.offset_in_bytes();
duke@1 821 }
duke@1 822 }
duke@1 823 }
duke@1 824 }
duke@1 825
kvn@13104 826 #ifdef ASSERT
duke@1 827 if (TraceSuperWord && Verbose) {
kvn@30593 828 tty->print_cr("\nVector memops after find_align_to_ref");
duke@1 829 for (uint i = 0; i < memops.size(); i++) {
duke@1 830 MemNode* s = memops.at(i)->as_Mem();
duke@1 831 s->dump();
duke@1 832 }
duke@1 833 }
duke@1 834 #endif
kvn@13104 835
kvn@13104 836 if (max_ct > 0) {
kvn@13104 837 #ifdef ASSERT
kvn@13104 838 if (TraceSuperWord) {
kvn@13104 839 tty->print("\nVector align to node: ");
kvn@13104 840 memops.at(max_idx)->as_Mem()->dump();
kvn@13104 841 }
kvn@13104 842 #endif
kvn@13104 843 return memops.at(max_idx)->as_Mem();
kvn@13104 844 }
kvn@13104 845 return NULL;
duke@1 846 }
duke@1 847
duke@1 848 //------------------------------ref_is_alignable---------------------------
duke@1 849 // Can the preloop align the reference to position zero in the vector?
duke@1 850 bool SuperWord::ref_is_alignable(SWPointer& p) {
duke@1 851 if (!p.has_iv()) {
duke@1 852 return true; // no induction variable
duke@1 853 }
duke@1 854 CountedLoopEndNode* pre_end = get_pre_loop_end(lp()->as_CountedLoop());
adlertz@22919 855 assert(pre_end != NULL, "we must have a correct pre-loop");
duke@1 856 assert(pre_end->stride_is_con(), "pre loop stride is constant");
duke@1 857 int preloop_stride = pre_end->stride_con();
duke@1 858
duke@1 859 int span = preloop_stride * p.scale_in_bytes();
kvn@30215 860 int mem_size = p.memory_size();
kvn@30215 861 int offset = p.offset_in_bytes();
kvn@30215 862 // Stride one accesses are alignable if offset is aligned to memory operation size.
kvn@30215 863 // Offset can be unaligned when UseUnalignedAccesses is used.
kvn@30215 864 if (ABS(span) == mem_size && (ABS(offset) % mem_size) == 0) {
duke@1 865 return true;
kvn@30215 866 }
thartmann@30625 867 // If the initial offset from start of the object is computable,
thartmann@30625 868 // check if the pre-loop can align the final offset accordingly.
thartmann@30625 869 //
thartmann@30625 870 // In other words: Can we find an i such that the offset
thartmann@30625 871 // after i pre-loop iterations is aligned to vw?
thartmann@30625 872 // (init_offset + pre_loop) % vw == 0 (1)
thartmann@30625 873 // where
thartmann@30625 874 // pre_loop = i * span
thartmann@30625 875 // is the number of bytes added to the offset by i pre-loop iterations.
thartmann@30625 876 //
thartmann@30625 877 // For this to hold we need pre_loop to increase init_offset by
thartmann@30625 878 // pre_loop = vw - (init_offset % vw)
thartmann@30625 879 //
thartmann@30625 880 // This is only possible if pre_loop is divisible by span because each
thartmann@30625 881 // pre-loop iteration increases the initial offset by 'span' bytes:
thartmann@30625 882 // (vw - (init_offset % vw)) % span == 0
thartmann@30625 883 //
kvn@13108 884 int vw = vector_width_in_bytes(p.mem());
kvn@13104 885 assert(vw > 1, "sanity");
thartmann@30625 886 Node* init_nd = pre_end->init_trip();
thartmann@30625 887 if (init_nd->is_Con() && p.invar() == NULL) {
thartmann@30625 888 int init = init_nd->bottom_type()->is_int()->get_con();
thartmann@30625 889 int init_offset = init * p.scale_in_bytes() + offset;
thartmann@30625 890 assert(init_offset >= 0, "positive offset from object start");
thartmann@30625 891 if (vw % span == 0) {
thartmann@30625 892 // If vm is a multiple of span, we use formula (1).
duke@1 893 if (span > 0) {
duke@1 894 return (vw - (init_offset % vw)) % span == 0;
duke@1 895 } else {
duke@1 896 assert(span < 0, "nonzero stride * scale");
duke@1 897 return (init_offset % vw) % -span == 0;
duke@1 898 }
thartmann@30625 899 } else if (span % vw == 0) {
thartmann@30625 900 // If span is a multiple of vw, we can simplify formula (1) to:
thartmann@30625 901 // (init_offset + i * span) % vw == 0
thartmann@30625 902 // =>
thartmann@30625 903 // (init_offset % vw) + ((i * span) % vw) == 0
thartmann@30625 904 // =>
thartmann@30625 905 // init_offset % vw == 0
thartmann@30625 906 //
thartmann@30625 907 // Because we add a multiple of vw to the initial offset, the final
thartmann@30625 908 // offset is a multiple of vw if and only if init_offset is a multiple.
thartmann@30625 909 //
thartmann@30625 910 return (init_offset % vw) == 0;
duke@1 911 }
duke@1 912 }
duke@1 913 return false;
duke@1 914 }
duke@1 915
kvn@13104 916 //---------------------------get_iv_adjustment---------------------------
kvn@13104 917 // Calculate loop's iv adjustment for this memory ops.
kvn@13104 918 int SuperWord::get_iv_adjustment(MemNode* mem_ref) {
mcberg@31403 919 SWPointer align_to_ref_p(mem_ref, this, NULL, false);
kvn@13104 920 int offset = align_to_ref_p.offset_in_bytes();
kvn@13104 921 int scale = align_to_ref_p.scale_in_bytes();
thartmann@30625 922 int elt_size = align_to_ref_p.memory_size();
kvn@13108 923 int vw = vector_width_in_bytes(mem_ref);
kvn@13104 924 assert(vw > 1, "sanity");
thartmann@30625 925 int iv_adjustment;
thartmann@30625 926 if (scale != 0) {
thartmann@30625 927 int stride_sign = (scale * iv_stride()) > 0 ? 1 : -1;
thartmann@30625 928 // At least one iteration is executed in pre-loop by default. As result
thartmann@30625 929 // several iterations are needed to align memory operations in main-loop even
thartmann@30625 930 // if offset is 0.
thartmann@30625 931 int iv_adjustment_in_bytes = (stride_sign * vw - (offset % vw));
thartmann@30625 932 assert(((ABS(iv_adjustment_in_bytes) % elt_size) == 0),
david@33105 933 "(%d) should be divisible by (%d)", iv_adjustment_in_bytes, elt_size);
thartmann@30625 934 iv_adjustment = iv_adjustment_in_bytes/elt_size;
thartmann@30625 935 } else {
thartmann@30625 936 // This memory op is not dependent on iv (scale == 0)
thartmann@30625 937 iv_adjustment = 0;
thartmann@30625 938 }
kvn@13104 939
kvn@13104 940 #ifndef PRODUCT
kvn@31858 941 if (TraceSuperWord) {
kvn@31858 942 tty->print("SuperWord::get_iv_adjustment: n = %d, noffset = %d iv_adjust = %d elt_size = %d scale = %d iv_stride = %d vect_size %d: ",
kvn@31858 943 mem_ref->_idx, offset, iv_adjustment, elt_size, scale, iv_stride(), vw);
kvn@31858 944 mem_ref->dump();
kvn@31858 945 }
kvn@13104 946 #endif
kvn@13104 947 return iv_adjustment;
kvn@13104 948 }
kvn@13104 949
duke@1 950 //---------------------------dependence_graph---------------------------
duke@1 951 // Construct dependency graph.
duke@1 952 // Add dependence edges to load/store nodes for memory dependence
duke@1 953 // A.out()->DependNode.in(1) and DependNode.out()->B.prec(x)
duke@1 954 void SuperWord::dependence_graph() {
mcberg@38049 955 CountedLoopNode *cl = lpt()->_head->as_CountedLoop();
duke@1 956 // First, assign a dependence node to each memory node
duke@1 957 for (int i = 0; i < _block.length(); i++ ) {
duke@1 958 Node *n = _block.at(i);
jwilhelm@46630 959 if (n->is_Mem() || (n->is_Phi() && n->bottom_type() == Type::MEMORY)) {
duke@1 960 _dg.make_node(n);
duke@1 961 }
duke@1 962 }
duke@1 963
duke@1 964 // For each memory slice, create the dependences
duke@1 965 for (int i = 0; i < _mem_slice_head.length(); i++) {
duke@1 966 Node* n = _mem_slice_head.at(i);
duke@1 967 Node* n_tail = _mem_slice_tail.at(i);
duke@1 968
duke@1 969 // Get slice in predecessor order (last is first)
mcberg@38049 970 if (cl->is_main_loop()) {
mcberg@38049 971 mem_slice_preds(n_tail, n, _nlist);
mcberg@38049 972 }
duke@1 973
kvn@30593 974 #ifndef PRODUCT
kvn@30593 975 if(TraceSuperWord && Verbose) {
kvn@30593 976 tty->print_cr("SuperWord::dependence_graph: built a new mem slice");
kvn@30593 977 for (int j = _nlist.length() - 1; j >= 0 ; j--) {
kvn@30593 978 _nlist.at(j)->dump();
kvn@30593 979 }
kvn@30593 980 }
kvn@30593 981 #endif
duke@1 982 // Make the slice dependent on the root
duke@1 983 DepMem* slice = _dg.dep(n);
duke@1 984 _dg.make_edge(_dg.root(), slice);
duke@1 985
duke@1 986 // Create a sink for the slice
duke@1 987 DepMem* slice_sink = _dg.make_node(NULL);
duke@1 988 _dg.make_edge(slice_sink, _dg.tail());
duke@1 989
duke@1 990 // Now visit each pair of memory ops, creating the edges
duke@1 991 for (int j = _nlist.length() - 1; j >= 0 ; j--) {
duke@1 992 Node* s1 = _nlist.at(j);
duke@1 993
duke@1 994 // If no dependency yet, use slice
duke@1 995 if (_dg.dep(s1)->in_cnt() == 0) {
duke@1 996 _dg.make_edge(slice, s1);
duke@1 997 }
mcberg@31403 998 SWPointer p1(s1->as_Mem(), this, NULL, false);
duke@1 999 bool sink_dependent = true;
duke@1 1000 for (int k = j - 1; k >= 0; k--) {
duke@1 1001 Node* s2 = _nlist.at(k);
duke@1 1002 if (s1->is_Load() && s2->is_Load())
duke@1 1003 continue;
mcberg@31403 1004 SWPointer p2(s2->as_Mem(), this, NULL, false);
duke@1 1005
duke@1 1006 int cmp = p1.cmp(p2);
duke@1 1007 if (SuperWordRTDepCheck &&
duke@1 1008 p1.base() != p2.base() && p1.valid() && p2.valid()) {
duke@1 1009 // Create a runtime check to disambiguate
duke@1 1010 OrderedPair pp(p1.base(), p2.base());
duke@1 1011 _disjoint_ptrs.append_if_missing(pp);
duke@1 1012 } else if (!SWPointer::not_equal(cmp)) {
duke@1 1013 // Possibly same address
duke@1 1014 _dg.make_edge(s1, s2);
duke@1 1015 sink_dependent = false;
duke@1 1016 }
duke@1 1017 }
duke@1 1018 if (sink_dependent) {
duke@1 1019 _dg.make_edge(s1, slice_sink);
duke@1 1020 }
duke@1 1021 }
twisti@34174 1022
duke@1 1023 if (TraceSuperWord) {
duke@1 1024 tty->print_cr("\nDependence graph for slice: %d", n->_idx);
duke@1 1025 for (int q = 0; q < _nlist.length(); q++) {
duke@1 1026 _dg.print(_nlist.at(q));
duke@1 1027 }
duke@1 1028 tty->cr();
duke@1 1029 }
twisti@34174 1030
duke@1 1031 _nlist.clear();
duke@1 1032 }
duke@1 1033
duke@1 1034 if (TraceSuperWord) {
duke@1 1035 tty->print_cr("\ndisjoint_ptrs: %s", _disjoint_ptrs.length() > 0 ? "" : "NONE");
duke@1 1036 for (int r = 0; r < _disjoint_ptrs.length(); r++) {
duke@1 1037 _disjoint_ptrs.at(r).print();
duke@1 1038 tty->cr();
duke@1 1039 }
duke@1 1040 tty->cr();
duke@1 1041 }
twisti@34174 1042
duke@1 1043 }
duke@1 1044
duke@1 1045 //---------------------------mem_slice_preds---------------------------
duke@1 1046 // Return a memory slice (node list) in predecessor order starting at "start"
duke@1 1047 void SuperWord::mem_slice_preds(Node* start, Node* stop, GrowableArray<Node*> &preds) {
duke@1 1048 assert(preds.length() == 0, "start empty");
duke@1 1049 Node* n = start;
duke@1 1050 Node* prev = NULL;
duke@1 1051 while (true) {
kvn@31858 1052 NOT_PRODUCT( if(is_trace_mem_slice()) tty->print_cr("SuperWord::mem_slice_preds: n %d", n->_idx);)
duke@1 1053 assert(in_bb(n), "must be in block");
duke@1 1054 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
duke@1 1055 Node* out = n->fast_out(i);
duke@1 1056 if (out->is_Load()) {
duke@1 1057 if (in_bb(out)) {
duke@1 1058 preds.push(out);
twisti@34174 1059 if (TraceSuperWord && Verbose) {
twisti@34174 1060 tty->print_cr("SuperWord::mem_slice_preds: added pred(%d)", out->_idx);
twisti@34174 1061 }
duke@1 1062 }
duke@1 1063 } else {
duke@1 1064 // FIXME
duke@1 1065 if (out->is_MergeMem() && !in_bb(out)) {
duke@1 1066 // Either unrolling is causing a memory edge not to disappear,
duke@1 1067 // or need to run igvn.optimize() again before SLP
duke@1 1068 } else if (out->is_Phi() && out->bottom_type() == Type::MEMORY && !in_bb(out)) {
duke@1 1069 // Ditto. Not sure what else to check further.
cfang@2334 1070 } else if (out->Opcode() == Op_StoreCM && out->in(MemNode::OopStore) == n) {
duke@1 1071 // StoreCM has an input edge used as a precedence edge.
duke@1 1072 // Maybe an issue when oop stores are vectorized.
duke@1 1073 } else {
duke@1 1074 assert(out == prev || prev == NULL, "no branches off of store slice");
duke@1 1075 }
kvn@31858 1076 }//else
kvn@31858 1077 }//for
duke@1 1078 if (n == stop) break;
duke@1 1079 preds.push(n);
twisti@34174 1080 if (TraceSuperWord && Verbose) {
twisti@34174 1081 tty->print_cr("SuperWord::mem_slice_preds: added pred(%d)", n->_idx);
twisti@34174 1082 }
duke@1 1083 prev = n;
david@33105 1084 assert(n->is_Mem(), "unexpected node %s", n->Name());
duke@1 1085 n = n->in(MemNode::Memory);
duke@1 1086 }
duke@1 1087 }
duke@1 1088
duke@1 1089 //------------------------------stmts_can_pack---------------------------
twisti@2131 1090 // Can s1 and s2 be in a pack with s1 immediately preceding s2 and
duke@1 1091 // s1 aligned at "align"
duke@1 1092 bool SuperWord::stmts_can_pack(Node* s1, Node* s2, int align) {
cfang@3906 1093
cfang@3906 1094 // Do not use superword for non-primitives
kvn@13104 1095 BasicType bt1 = velt_basic_type(s1);
kvn@13104 1096 BasicType bt2 = velt_basic_type(s2);
kvn@13104 1097 if(!is_java_primitive(bt1) || !is_java_primitive(bt2))
cfang@3906 1098 return false;
kvn@13104 1099 if (Matcher::max_vector_size(bt1) < 2) {
kvn@13104 1100 return false; // No vectors for this type
kvn@13104 1101 }
cfang@3906 1102
duke@1 1103 if (isomorphic(s1, s2)) {
njian@48011 1104 if ((independent(s1, s2) && have_similar_inputs(s1, s2)) || reduction(s1, s2)) {
duke@1 1105 if (!exists_at(s1, 0) && !exists_at(s2, 1)) {
duke@1 1106 if (!s1->is_Mem() || are_adjacent_refs(s1, s2)) {
duke@1 1107 int s1_align = alignment(s1);
duke@1 1108 int s2_align = alignment(s2);
duke@1 1109 if (s1_align == top_align || s1_align == align) {
duke@1 1110 if (s2_align == top_align || s2_align == align + data_size(s1)) {
duke@1 1111 return true;
duke@1 1112 }
duke@1 1113 }
duke@1 1114 }
duke@1 1115 }
duke@1 1116 }
duke@1 1117 }
duke@1 1118 return false;
duke@1 1119 }
duke@1 1120
duke@1 1121 //------------------------------exists_at---------------------------
duke@1 1122 // Does s exist in a pack at position pos?
duke@1 1123 bool SuperWord::exists_at(Node* s, uint pos) {
duke@1 1124 for (int i = 0; i < _packset.length(); i++) {
duke@1 1125 Node_List* p = _packset.at(i);
duke@1 1126 if (p->at(pos) == s) {
duke@1 1127 return true;
duke@1 1128 }
duke@1 1129 }
duke@1 1130 return false;
duke@1 1131 }
duke@1 1132
duke@1 1133 //------------------------------are_adjacent_refs---------------------------
duke@1 1134 // Is s1 immediately before s2 in memory?
duke@1 1135 bool SuperWord::are_adjacent_refs(Node* s1, Node* s2) {
duke@1 1136 if (!s1->is_Mem() || !s2->is_Mem()) return false;
duke@1 1137 if (!in_bb(s1) || !in_bb(s2)) return false;
never@5708 1138
never@5708 1139 // Do not use superword for non-primitives
never@5708 1140 if (!is_java_primitive(s1->as_Mem()->memory_type()) ||
never@5708 1141 !is_java_primitive(s2->as_Mem()->memory_type())) {
never@5708 1142 return false;
never@5708 1143 }
never@5708 1144
duke@1 1145 // FIXME - co_locate_pack fails on Stores in different mem-slices, so
duke@1 1146 // only pack memops that are in the same alias set until that's fixed.
duke@1 1147 if (_phase->C->get_alias_index(s1->as_Mem()->adr_type()) !=
duke@1 1148 _phase->C->get_alias_index(s2->as_Mem()->adr_type()))
duke@1 1149 return false;
mcberg@31403 1150 SWPointer p1(s1->as_Mem(), this, NULL, false);
mcberg@31403 1151 SWPointer p2(s2->as_Mem(), this, NULL, false);
duke@1 1152 if (p1.base() != p2.base() || !p1.comparable(p2)) return false;
duke@1 1153 int diff = p2.offset_in_bytes() - p1.offset_in_bytes();
duke@1 1154 return diff == data_size(s1);
duke@1 1155 }
duke@1 1156
duke@1 1157 //------------------------------isomorphic---------------------------
duke@1 1158 // Are s1 and s2 similar?
duke@1 1159 bool SuperWord::isomorphic(Node* s1, Node* s2) {
duke@1 1160 if (s1->Opcode() != s2->Opcode()) return false;
duke@1 1161 if (s1->req() != s2->req()) return false;
duke@1 1162 if (s1->in(0) != s2->in(0)) return false;
kvn@13104 1163 if (!same_velt_type(s1, s2)) return false;
duke@1 1164 return true;
duke@1 1165 }
duke@1 1166
duke@1 1167 //------------------------------independent---------------------------
duke@1 1168 // Is there no data path from s1 to s2 or s2 to s1?
duke@1 1169 bool SuperWord::independent(Node* s1, Node* s2) {
duke@1 1170 // assert(s1->Opcode() == s2->Opcode(), "check isomorphic first");
duke@1 1171 int d1 = depth(s1);
duke@1 1172 int d2 = depth(s2);
duke@1 1173 if (d1 == d2) return s1 != s2;
duke@1 1174 Node* deep = d1 > d2 ? s1 : s2;
duke@1 1175 Node* shallow = d1 > d2 ? s2 : s1;
duke@1 1176
duke@1 1177 visited_clear();
duke@1 1178
duke@1 1179 return independent_path(shallow, deep);
duke@1 1180 }
duke@1 1181
njian@48011 1182 //--------------------------have_similar_inputs-----------------------
njian@48011 1183 // For a node pair (s1, s2) which is isomorphic and independent,
njian@48011 1184 // do s1 and s2 have similar input edges?
njian@48011 1185 bool SuperWord::have_similar_inputs(Node* s1, Node* s2) {
njian@48011 1186 // assert(isomorphic(s1, s2) == true, "check isomorphic");
njian@48011 1187 // assert(independent(s1, s2) == true, "check independent");
njian@48011 1188 if (s1->req() > 1 && !s1->is_Store() && !s1->is_Load()) {
njian@48011 1189 for (uint i = 1; i < s1->req(); i++) {
njian@48011 1190 if (s1->in(i)->Opcode() != s2->in(i)->Opcode()) return false;
njian@48011 1191 }
njian@48011 1192 }
njian@48011 1193 return true;
njian@48011 1194 }
njian@48011 1195
kvn@30211 1196 //------------------------------reduction---------------------------
kvn@30211 1197 // Is there a data path between s1 and s2 and the nodes reductions?
kvn@30211 1198 bool SuperWord::reduction(Node* s1, Node* s2) {
kvn@30211 1199 bool retValue = false;
kvn@30211 1200 int d1 = depth(s1);
kvn@30211 1201 int d2 = depth(s2);
kvn@30211 1202 if (d1 + 1 == d2) {
kvn@30211 1203 if (s1->is_reduction() && s2->is_reduction()) {
kvn@30211 1204 // This is an ordered set, so s1 should define s2
kvn@30211 1205 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
kvn@30211 1206 Node* t1 = s1->fast_out(i);
kvn@30211 1207 if (t1 == s2) {
kvn@30211 1208 // both nodes are reductions and connected
kvn@30211 1209 retValue = true;
kvn@30211 1210 }
kvn@30211 1211 }
kvn@30211 1212 }
kvn@30211 1213 }
kvn@30211 1214
kvn@30211 1215 return retValue;
kvn@30211 1216 }
kvn@30211 1217
duke@1 1218 //------------------------------independent_path------------------------------
duke@1 1219 // Helper for independent
duke@1 1220 bool SuperWord::independent_path(Node* shallow, Node* deep, uint dp) {
duke@1 1221 if (dp >= 1000) return false; // stop deep recursion
duke@1 1222 visited_set(deep);
duke@1 1223 int shal_depth = depth(shallow);
duke@1 1224 assert(shal_depth <= depth(deep), "must be");
duke@1 1225 for (DepPreds preds(deep, _dg); !preds.done(); preds.next()) {
duke@1 1226 Node* pred = preds.current();
duke@1 1227 if (in_bb(pred) && !visited_test(pred)) {
duke@1 1228 if (shallow == pred) {
duke@1 1229 return false;
duke@1 1230 }
duke@1 1231 if (shal_depth < depth(pred) && !independent_path(shallow, pred, dp+1)) {
duke@1 1232 return false;
duke@1 1233 }
duke@1 1234 }
duke@1 1235 }
duke@1 1236 return true;
duke@1 1237 }
duke@1 1238
duke@1 1239 //------------------------------set_alignment---------------------------
duke@1 1240 void SuperWord::set_alignment(Node* s1, Node* s2, int align) {
duke@1 1241 set_alignment(s1, align);
kvn@13104 1242 if (align == top_align || align == bottom_align) {
kvn@13104 1243 set_alignment(s2, align);
kvn@13104 1244 } else {
kvn@13104 1245 set_alignment(s2, align + data_size(s1));
kvn@13104 1246 }
duke@1 1247 }
duke@1 1248
duke@1 1249 //------------------------------data_size---------------------------
duke@1 1250 int SuperWord::data_size(Node* s) {
kvn@48164 1251 Node* use = NULL; //test if the node is a candidate for CMoveV optimization, then return the size of CMov
kvn@48164 1252 if (UseVectorCmov) {
iveresov@33469 1253 use = _cmovev_kit.is_Bool_candidate(s);
iveresov@33469 1254 if (use != NULL) {
iveresov@33469 1255 return data_size(use);
iveresov@33469 1256 }
iveresov@33469 1257 use = _cmovev_kit.is_CmpD_candidate(s);
iveresov@33469 1258 if (use != NULL) {
iveresov@33469 1259 return data_size(use);
iveresov@33469 1260 }
iveresov@33469 1261 }
kvn@48164 1262
kvn@13104 1263 int bsize = type2aelembytes(velt_basic_type(s));
duke@1 1264 assert(bsize != 0, "valid size");
duke@1 1265 return bsize;
duke@1 1266 }
duke@1 1267
duke@1 1268 //------------------------------extend_packlist---------------------------
duke@1 1269 // Extend packset by following use->def and def->use links from pack members.
duke@1 1270 void SuperWord::extend_packlist() {
duke@1 1271 bool changed;
duke@1 1272 do {
kvn@30211 1273 packset_sort(_packset.length());
duke@1 1274 changed = false;
duke@1 1275 for (int i = 0; i < _packset.length(); i++) {
duke@1 1276 Node_List* p = _packset.at(i);
duke@1 1277 changed |= follow_use_defs(p);
duke@1 1278 changed |= follow_def_uses(p);
duke@1 1279 }
duke@1 1280 } while (changed);
duke@1 1281
kvn@30211 1282 if (_race_possible) {
kvn@30211 1283 for (int i = 0; i < _packset.length(); i++) {
kvn@30211 1284 Node_List* p = _packset.at(i);
kvn@30211 1285 order_def_uses(p);
kvn@30211 1286 }
kvn@30211 1287 }
kvn@30211 1288
duke@1 1289 if (TraceSuperWord) {
duke@1 1290 tty->print_cr("\nAfter extend_packlist");
duke@1 1291 print_packset();
duke@1 1292 }
duke@1 1293 }
duke@1 1294
duke@1 1295 //------------------------------follow_use_defs---------------------------
duke@1 1296 // Extend the packset by visiting operand definitions of nodes in pack p
duke@1 1297 bool SuperWord::follow_use_defs(Node_List* p) {
kvn@13104 1298 assert(p->size() == 2, "just checking");
duke@1 1299 Node* s1 = p->at(0);
duke@1 1300 Node* s2 = p->at(1);
duke@1 1301 assert(s1->req() == s2->req(), "just checking");
duke@1 1302 assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
duke@1 1303
duke@1 1304 if (s1->is_Load()) return false;
duke@1 1305
duke@1 1306 int align = alignment(s1);
iveresov@33469 1307 NOT_PRODUCT(if(is_trace_alignment()) tty->print_cr("SuperWord::follow_use_defs: s1 %d, align %d", s1->_idx, align);)
duke@1 1308 bool changed = false;
duke@1 1309 int start = s1->is_Store() ? MemNode::ValueIn : 1;
duke@1 1310 int end = s1->is_Store() ? MemNode::ValueIn+1 : s1->req();
duke@1 1311 for (int j = start; j < end; j++) {
duke@1 1312 Node* t1 = s1->in(j);
duke@1 1313 Node* t2 = s2->in(j);
duke@1 1314 if (!in_bb(t1) || !in_bb(t2))
duke@1 1315 continue;
duke@1 1316 if (stmts_can_pack(t1, t2, align)) {
duke@1 1317 if (est_savings(t1, t2) >= 0) {
duke@1 1318 Node_List* pair = new Node_List();
duke@1 1319 pair->push(t1);
duke@1 1320 pair->push(t2);
duke@1 1321 _packset.append(pair);
iveresov@33469 1322 NOT_PRODUCT(if(is_trace_alignment()) tty->print_cr("SuperWord::follow_use_defs: set_alignment(%d, %d, %d)", t1->_idx, t2->_idx, align);)
duke@1 1323 set_alignment(t1, t2, align);
duke@1 1324 changed = true;
duke@1 1325 }
duke@1 1326 }
duke@1 1327 }
duke@1 1328 return changed;
duke@1 1329 }
duke@1 1330
duke@1 1331 //------------------------------follow_def_uses---------------------------
duke@1 1332 // Extend the packset by visiting uses of nodes in pack p
duke@1 1333 bool SuperWord::follow_def_uses(Node_List* p) {
duke@1 1334 bool changed = false;
duke@1 1335 Node* s1 = p->at(0);
duke@1 1336 Node* s2 = p->at(1);
duke@1 1337 assert(p->size() == 2, "just checking");
duke@1 1338 assert(s1->req() == s2->req(), "just checking");
duke@1 1339 assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
duke@1 1340
duke@1 1341 if (s1->is_Store()) return false;
duke@1 1342
duke@1 1343 int align = alignment(s1);
iveresov@33469 1344 NOT_PRODUCT(if(is_trace_alignment()) tty->print_cr("SuperWord::follow_def_uses: s1 %d, align %d", s1->_idx, align);)
duke@1 1345 int savings = -1;
kvn@30211 1346 int num_s1_uses = 0;
duke@1 1347 Node* u1 = NULL;
duke@1 1348 Node* u2 = NULL;
duke@1 1349 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
duke@1 1350 Node* t1 = s1->fast_out(i);
kvn@30211 1351 num_s1_uses++;
duke@1 1352 if (!in_bb(t1)) continue;
duke@1 1353 for (DUIterator_Fast jmax, j = s2->fast_outs(jmax); j < jmax; j++) {
duke@1 1354 Node* t2 = s2->fast_out(j);
duke@1 1355 if (!in_bb(t2)) continue;
roland@48020 1356 if (t2->Opcode() == Op_AddI && t2 == _lp->as_CountedLoop()->incr()) continue; // don't mess with the iv
duke@1 1357 if (!opnd_positions_match(s1, t1, s2, t2))
duke@1 1358 continue;
duke@1 1359 if (stmts_can_pack(t1, t2, align)) {
duke@1 1360 int my_savings = est_savings(t1, t2);
duke@1 1361 if (my_savings > savings) {
duke@1 1362 savings = my_savings;
duke@1 1363 u1 = t1;
duke@1 1364 u2 = t2;
duke@1 1365 }
duke@1 1366 }
duke@1 1367 }
duke@1 1368 }
kvn@30211 1369 if (num_s1_uses > 1) {
kvn@30211 1370 _race_possible = true;
kvn@30211 1371 }
duke@1 1372 if (savings >= 0) {
duke@1 1373 Node_List* pair = new Node_List();
duke@1 1374 pair->push(u1);
duke@1 1375 pair->push(u2);
duke@1 1376 _packset.append(pair);
iveresov@33469 1377 NOT_PRODUCT(if(is_trace_alignment()) tty->print_cr("SuperWord::follow_def_uses: set_alignment(%d, %d, %d)", u1->_idx, u2->_idx, align);)
duke@1 1378 set_alignment(u1, u2, align);
duke@1 1379 changed = true;
duke@1 1380 }
duke@1 1381 return changed;
duke@1 1382 }
duke@1 1383
kvn@30211 1384 //------------------------------order_def_uses---------------------------
kvn@30211 1385 // For extended packsets, ordinally arrange uses packset by major component
kvn@30211 1386 void SuperWord::order_def_uses(Node_List* p) {
kvn@30211 1387 Node* s1 = p->at(0);
kvn@30211 1388
kvn@30211 1389 if (s1->is_Store()) return;
kvn@30211 1390
kvn@30211 1391 // reductions are always managed beforehand
kvn@30211 1392 if (s1->is_reduction()) return;
kvn@30211 1393
kvn@30211 1394 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
kvn@30211 1395 Node* t1 = s1->fast_out(i);
kvn@30211 1396
kvn@30211 1397 // Only allow operand swap on commuting operations
kvn@30211 1398 if (!t1->is_Add() && !t1->is_Mul()) {
kvn@30211 1399 break;
kvn@30211 1400 }
kvn@30211 1401
kvn@30211 1402 // Now find t1's packset
kvn@30211 1403 Node_List* p2 = NULL;
kvn@30211 1404 for (int j = 0; j < _packset.length(); j++) {
kvn@30211 1405 p2 = _packset.at(j);
kvn@30211 1406 Node* first = p2->at(0);
kvn@30211 1407 if (t1 == first) {
kvn@30211 1408 break;
kvn@30211 1409 }
kvn@30211 1410 p2 = NULL;
kvn@30211 1411 }
kvn@30211 1412 // Arrange all sub components by the major component
kvn@30211 1413 if (p2 != NULL) {
kvn@30211 1414 for (uint j = 1; j < p->size(); j++) {
kvn@30211 1415 Node* d1 = p->at(j);
kvn@30211 1416 Node* u1 = p2->at(j);
kvn@30211 1417 opnd_positions_match(s1, t1, d1, u1);
kvn@30211 1418 }
kvn@30211 1419 }
kvn@30211 1420 }
kvn@30211 1421 }
kvn@30211 1422
duke@1 1423 //---------------------------opnd_positions_match-------------------------
duke@1 1424 // Is the use of d1 in u1 at the same operand position as d2 in u2?
duke@1 1425 bool SuperWord::opnd_positions_match(Node* d1, Node* u1, Node* d2, Node* u2) {
kvn@30211 1426 // check reductions to see if they are marshalled to represent the reduction
kvn@30211 1427 // operator in a specified opnd
kvn@30211 1428 if (u1->is_reduction() && u2->is_reduction()) {
kvn@30211 1429 // ensure reductions have phis and reduction definitions feeding the 1st operand
kvn@30211 1430 Node* first = u1->in(2);
kvn@30211 1431 if (first->is_Phi() || first->is_reduction()) {
kvn@30211 1432 u1->swap_edges(1, 2);
kvn@30211 1433 }
kvn@30211 1434 // ensure reductions have phis and reduction definitions feeding the 1st operand
kvn@30211 1435 first = u2->in(2);
kvn@30211 1436 if (first->is_Phi() || first->is_reduction()) {
kvn@30211 1437 u2->swap_edges(1, 2);
kvn@30211 1438 }
kvn@30211 1439 return true;
kvn@30211 1440 }
kvn@30211 1441
duke@1 1442 uint ct = u1->req();
duke@1 1443 if (ct != u2->req()) return false;
duke@1 1444 uint i1 = 0;
duke@1 1445 uint i2 = 0;
duke@1 1446 do {
duke@1 1447 for (i1++; i1 < ct; i1++) if (u1->in(i1) == d1) break;
duke@1 1448 for (i2++; i2 < ct; i2++) if (u2->in(i2) == d2) break;
duke@1 1449 if (i1 != i2) {
kvn@13104 1450 if ((i1 == (3-i2)) && (u2->is_Add() || u2->is_Mul())) {
kvn@13104 1451 // Further analysis relies on operands position matching.
kvn@13104 1452 u2->swap_edges(i1, i2);
kvn@13104 1453 } else {
kvn@13104 1454 return false;
kvn@13104 1455 }
duke@1 1456 }
duke@1 1457 } while (i1 < ct);
duke@1 1458 return true;
duke@1 1459 }
duke@1 1460
duke@1 1461 //------------------------------est_savings---------------------------
duke@1 1462 // Estimate the savings from executing s1 and s2 as a pack
duke@1 1463 int SuperWord::est_savings(Node* s1, Node* s2) {
kvn@13104 1464 int save_in = 2 - 1; // 2 operations per instruction in packed form
duke@1 1465
duke@1 1466 // inputs
duke@1 1467 for (uint i = 1; i < s1->req(); i++) {
duke@1 1468 Node* x1 = s1->in(i);
duke@1 1469 Node* x2 = s2->in(i);
duke@1 1470 if (x1 != x2) {
duke@1 1471 if (are_adjacent_refs(x1, x2)) {
kvn@13104 1472 save_in += adjacent_profit(x1, x2);
duke@1 1473 } else if (!in_packset(x1, x2)) {
kvn@13104 1474 save_in -= pack_cost(2);
duke@1 1475 } else {
kvn@13104 1476 save_in += unpack_cost(2);
duke@1 1477 }
duke@1 1478 }
duke@1 1479 }
duke@1 1480
duke@1 1481 // uses of result
duke@1 1482 uint ct = 0;
kvn@13104 1483 int save_use = 0;
duke@1 1484 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
duke@1 1485 Node* s1_use = s1->fast_out(i);
duke@1 1486 for (int j = 0; j < _packset.length(); j++) {
duke@1 1487 Node_List* p = _packset.at(j);
duke@1 1488 if (p->at(0) == s1_use) {
duke@1 1489 for (DUIterator_Fast kmax, k = s2->fast_outs(kmax); k < kmax; k++) {
duke@1 1490 Node* s2_use = s2->fast_out(k);
duke@1 1491 if (p->at(p->size()-1) == s2_use) {
duke@1 1492 ct++;
duke@1 1493 if (are_adjacent_refs(s1_use, s2_use)) {
kvn@13104 1494 save_use += adjacent_profit(s1_use, s2_use);
duke@1 1495 }
duke@1 1496 }
duke@1 1497 }
duke@1 1498 }
duke@1 1499 }
duke@1 1500 }
duke@1 1501
kvn@13104 1502 if (ct < s1->outcnt()) save_use += unpack_cost(1);
kvn@13104 1503 if (ct < s2->outcnt()) save_use += unpack_cost(1);
duke@1 1504
kvn@13104 1505 return MAX2(save_in, save_use);
duke@1 1506 }
duke@1 1507
duke@1 1508 //------------------------------costs---------------------------
duke@1 1509 int SuperWord::adjacent_profit(Node* s1, Node* s2) { return 2; }
duke@1 1510 int SuperWord::pack_cost(int ct) { return ct; }
duke@1 1511 int SuperWord::unpack_cost(int ct) { return ct; }
duke@1 1512
duke@1 1513 //------------------------------combine_packs---------------------------
duke@1 1514 // Combine packs A and B with A.last == B.first into A.first..,A.last,B.second,..B.last
duke@1 1515 void SuperWord::combine_packs() {
kvn@13104 1516 bool changed = true;
kvn@13104 1517 // Combine packs regardless max vector size.
kvn@13104 1518 while (changed) {
duke@1 1519 changed = false;
duke@1 1520 for (int i = 0; i < _packset.length(); i++) {
duke@1 1521 Node_List* p1 = _packset.at(i);
duke@1 1522 if (p1 == NULL) continue;
kvn@30211 1523 // Because of sorting we can start at i + 1
kvn@30211 1524 for (int j = i + 1; j < _packset.length(); j++) {
duke@1 1525 Node_List* p2 = _packset.at(j);
duke@1 1526 if (p2 == NULL) continue;
kvn@13104 1527 if (i == j) continue;
duke@1 1528 if (p1->at(p1->size()-1) == p2->at(0)) {
duke@1 1529 for (uint k = 1; k < p2->size(); k++) {
duke@1 1530 p1->push(p2->at(k));
duke@1 1531 }
duke@1 1532 _packset.at_put(j, NULL);
duke@1 1533 changed = true;
duke@1 1534 }
duke@1 1535 }
duke@1 1536 }
kvn@13104 1537 }
duke@1 1538
kvn@13104 1539 // Split packs which have size greater then max vector size.
kvn@13104 1540 for (int i = 0; i < _packset.length(); i++) {
kvn@13104 1541 Node_List* p1 = _packset.at(i);
kvn@13104 1542 if (p1 != NULL) {
kvn@13104 1543 BasicType bt = velt_basic_type(p1->at(0));
kvn@13104 1544 uint max_vlen = Matcher::max_vector_size(bt); // Max elements in vector
kvn@13104 1545 assert(is_power_of_2(max_vlen), "sanity");
kvn@13104 1546 uint psize = p1->size();
kvn@13104 1547 if (!is_power_of_2(psize)) {
kvn@13104 1548 // Skip pack which can't be vector.
kvn@13104 1549 // case1: for(...) { a[i] = i; } elements values are different (i+x)
kvn@13104 1550 // case2: for(...) { a[i] = b[i+1]; } can't align both, load and store
kvn@13104 1551 _packset.at_put(i, NULL);
kvn@13104 1552 continue;
kvn@13104 1553 }
kvn@13104 1554 if (psize > max_vlen) {
kvn@13104 1555 Node_List* pack = new Node_List();
kvn@13104 1556 for (uint j = 0; j < psize; j++) {
kvn@13104 1557 pack->push(p1->at(j));
kvn@13104 1558 if (pack->size() >= max_vlen) {
kvn@13104 1559 assert(is_power_of_2(pack->size()), "sanity");
kvn@13104 1560 _packset.append(pack);
kvn@13104 1561 pack = new Node_List();
kvn@13104 1562 }
kvn@13104 1563 }
kvn@13104 1564 _packset.at_put(i, NULL);
kvn@13104 1565 }
kvn@13104 1566 }
kvn@13104 1567 }
kvn@13104 1568
kvn@13104 1569 // Compress list.
duke@1 1570 for (int i = _packset.length() - 1; i >= 0; i--) {
duke@1 1571 Node_List* p1 = _packset.at(i);
duke@1 1572 if (p1 == NULL) {
duke@1 1573 _packset.remove_at(i);
duke@1 1574 }
duke@1 1575 }
duke@1 1576
duke@1 1577 if (TraceSuperWord) {
duke@1 1578 tty->print_cr("\nAfter combine_packs");
duke@1 1579 print_packset();
duke@1 1580 }
duke@1 1581 }
duke@1 1582
duke@1 1583 //-----------------------------construct_my_pack_map--------------------------
duke@1 1584 // Construct the map from nodes to packs. Only valid after the
duke@1 1585 // point where a node is only in one pack (after combine_packs).
duke@1 1586 void SuperWord::construct_my_pack_map() {
duke@1 1587 Node_List* rslt = NULL;
duke@1 1588 for (int i = 0; i < _packset.length(); i++) {
duke@1 1589 Node_List* p = _packset.at(i);
duke@1 1590 for (uint j = 0; j < p->size(); j++) {
duke@1 1591 Node* s = p->at(j);
duke@1 1592 assert(my_pack(s) == NULL, "only in one pack");
duke@1 1593 set_my_pack(s, p);
duke@1 1594 }
duke@1 1595 }
duke@1 1596 }
duke@1 1597
duke@1 1598 //------------------------------filter_packs---------------------------
duke@1 1599 // Remove packs that are not implemented or not profitable.
duke@1 1600 void SuperWord::filter_packs() {
duke@1 1601 // Remove packs that are not implemented
duke@1 1602 for (int i = _packset.length() - 1; i >= 0; i--) {
duke@1 1603 Node_List* pk = _packset.at(i);
duke@1 1604 bool impl = implemented(pk);
duke@1 1605 if (!impl) {
duke@1 1606 #ifndef PRODUCT
duke@1 1607 if (TraceSuperWord && Verbose) {
duke@1 1608 tty->print_cr("Unimplemented");
duke@1 1609 pk->at(0)->dump();
duke@1 1610 }
duke@1 1611 #endif
duke@1 1612 remove_pack_at(i);
duke@1 1613 }
kvn@30588 1614 Node *n = pk->at(0);
kvn@30588 1615 if (n->is_reduction()) {
kvn@30588 1616 _num_reductions++;
kvn@30588 1617 } else {
kvn@30588 1618 _num_work_vecs++;
kvn@30588 1619 }
duke@1 1620 }
duke@1 1621
duke@1 1622 // Remove packs that are not profitable
duke@1 1623 bool changed;
duke@1 1624 do {
duke@1 1625 changed = false;
duke@1 1626 for (int i = _packset.length() - 1; i >= 0; i--) {
duke@1 1627 Node_List* pk = _packset.at(i);
duke@1 1628 bool prof = profitable(pk);
duke@1 1629 if (!prof) {
duke@1 1630 #ifndef PRODUCT
duke@1 1631 if (TraceSuperWord && Verbose) {
duke@1 1632 tty->print_cr("Unprofitable");
duke@1 1633 pk->at(0)->dump();
duke@1 1634 }
duke@1 1635 #endif
duke@1 1636 remove_pack_at(i);
duke@1 1637 changed = true;
duke@1 1638 }
duke@1 1639 }
duke@1 1640 } while (changed);
duke@1 1641
duke@1 1642 #ifndef PRODUCT
duke@1 1643 if (TraceSuperWord) {
duke@1 1644 tty->print_cr("\nAfter filter_packs");
duke@1 1645 print_packset();
duke@1 1646 tty->cr();
duke@1 1647 }
duke@1 1648 #endif
duke@1 1649 }
duke@1 1650
iveresov@33469 1651 //------------------------------merge_packs_to_cmovd---------------------------
iveresov@33469 1652 // Merge CMoveD into new vector-nodes
iveresov@33469 1653 // We want to catch this pattern and subsume CmpD and Bool into CMoveD
iveresov@33469 1654 //
iveresov@33469 1655 // SubD ConD
iveresov@33469 1656 // / | /
iveresov@33469 1657 // / | / /
iveresov@33469 1658 // / | / /
iveresov@33469 1659 // / | / /
iveresov@33469 1660 // / / /
iveresov@33469 1661 // / / | /
iveresov@33469 1662 // v / | /
iveresov@33469 1663 // CmpD | /
iveresov@33469 1664 // | | /
iveresov@33469 1665 // v | /
iveresov@33469 1666 // Bool | /
iveresov@33469 1667 // \ | /
iveresov@33469 1668 // \ | /
iveresov@33469 1669 // \ | /
iveresov@33469 1670 // \ | /
iveresov@33469 1671 // \ v /
iveresov@33469 1672 // CMoveD
iveresov@33469 1673 //
iveresov@33469 1674
iveresov@33469 1675 void SuperWord::merge_packs_to_cmovd() {
iveresov@33469 1676 for (int i = _packset.length() - 1; i >= 0; i--) {
iveresov@33469 1677 _cmovev_kit.make_cmovevd_pack(_packset.at(i));
iveresov@33469 1678 }
iveresov@33469 1679 #ifndef PRODUCT
iveresov@33469 1680 if (TraceSuperWord) {
iveresov@33469 1681 tty->print_cr("\nSuperWord::merge_packs_to_cmovd(): After merge");
iveresov@33469 1682 print_packset();
iveresov@33469 1683 tty->cr();
iveresov@33469 1684 }
iveresov@33469 1685 #endif
iveresov@33469 1686 }
iveresov@33469 1687
iveresov@33469 1688 Node* CMoveKit::is_Bool_candidate(Node* def) const {
iveresov@33469 1689 Node* use = NULL;
iveresov@33469 1690 if (!def->is_Bool() || def->in(0) != NULL || def->outcnt() != 1) {
iveresov@33469 1691 return NULL;
iveresov@33469 1692 }
iveresov@33469 1693 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
iveresov@33469 1694 use = def->fast_out(j);
iveresov@33469 1695 if (!_sw->same_generation(def, use) || !use->is_CMove()) {
iveresov@33469 1696 return NULL;
iveresov@33469 1697 }
iveresov@33469 1698 }
iveresov@33469 1699 return use;
iveresov@33469 1700 }
iveresov@33469 1701
iveresov@33469 1702 Node* CMoveKit::is_CmpD_candidate(Node* def) const {
iveresov@33469 1703 Node* use = NULL;
iveresov@33469 1704 if (!def->is_Cmp() || def->in(0) != NULL || def->outcnt() != 1) {
iveresov@33469 1705 return NULL;
iveresov@33469 1706 }
iveresov@33469 1707 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
iveresov@33469 1708 use = def->fast_out(j);
iveresov@33469 1709 if (!_sw->same_generation(def, use) || (use = is_Bool_candidate(use)) == NULL || !_sw->same_generation(def, use)) {
iveresov@33469 1710 return NULL;
iveresov@33469 1711 }
iveresov@33469 1712 }
iveresov@33469 1713 return use;
iveresov@33469 1714 }
iveresov@33469 1715
iveresov@33469 1716 Node_List* CMoveKit::make_cmovevd_pack(Node_List* cmovd_pk) {
iveresov@33469 1717 Node *cmovd = cmovd_pk->at(0);
iveresov@33469 1718 if (!cmovd->is_CMove()) {
iveresov@33469 1719 return NULL;
iveresov@33469 1720 }
kvn@48164 1721 if (cmovd->Opcode() != Op_CMoveF && cmovd->Opcode() != Op_CMoveD) {
kvn@48164 1722 return NULL;
kvn@48164 1723 }
iveresov@33469 1724 if (pack(cmovd) != NULL) { // already in the cmov pack
iveresov@33469 1725 return NULL;
iveresov@33469 1726 }
iveresov@33469 1727 if (cmovd->in(0) != NULL) {
iveresov@33469 1728 NOT_PRODUCT(if(_sw->is_trace_cmov()) {tty->print("CMoveKit::make_cmovevd_pack: CMoveD %d has control flow, escaping...", cmovd->_idx); cmovd->dump();})
iveresov@33469 1729 return NULL;
iveresov@33469 1730 }
iveresov@33469 1731
iveresov@33469 1732 Node* bol = cmovd->as_CMove()->in(CMoveNode::Condition);
iveresov@33469 1733 if (!bol->is_Bool()
iveresov@33469 1734 || bol->outcnt() != 1
iveresov@33469 1735 || !_sw->same_generation(bol, cmovd)
iveresov@33469 1736 || bol->in(0) != NULL // BoolNode has control flow!!
iveresov@33469 1737 || _sw->my_pack(bol) == NULL) {
iveresov@33469 1738 NOT_PRODUCT(if(_sw->is_trace_cmov()) {tty->print("CMoveKit::make_cmovevd_pack: Bool %d does not fit CMoveD %d for building vector, escaping...", bol->_idx, cmovd->_idx); bol->dump();})
iveresov@33469 1739 return NULL;
iveresov@33469 1740 }
iveresov@33469 1741 Node_List* bool_pk = _sw->my_pack(bol);
iveresov@33469 1742 if (bool_pk->size() != cmovd_pk->size() ) {
iveresov@33469 1743 return NULL;
iveresov@33469 1744 }
iveresov@33469 1745
iveresov@33469 1746 Node* cmpd = bol->in(1);
iveresov@33469 1747 if (!cmpd->is_Cmp()
iveresov@33469 1748 || cmpd->outcnt() != 1
iveresov@33469 1749 || !_sw->same_generation(cmpd, cmovd)
iveresov@33469 1750 || cmpd->in(0) != NULL // CmpDNode has control flow!!
iveresov@33469 1751 || _sw->my_pack(cmpd) == NULL) {
iveresov@33469 1752 NOT_PRODUCT(if(_sw->is_trace_cmov()) {tty->print("CMoveKit::make_cmovevd_pack: CmpD %d does not fit CMoveD %d for building vector, escaping...", cmpd->_idx, cmovd->_idx); cmpd->dump();})
iveresov@33469 1753 return NULL;
iveresov@33469 1754 }
iveresov@33469 1755 Node_List* cmpd_pk = _sw->my_pack(cmpd);
iveresov@33469 1756 if (cmpd_pk->size() != cmovd_pk->size() ) {
iveresov@33469 1757 return NULL;
iveresov@33469 1758 }
iveresov@33469 1759
iveresov@33469 1760 if (!test_cmpd_pack(cmpd_pk, cmovd_pk)) {
iveresov@33469 1761 NOT_PRODUCT(if(_sw->is_trace_cmov()) {tty->print("CMoveKit::make_cmovevd_pack: cmpd pack for CmpD %d failed vectorization test", cmpd->_idx); cmpd->dump();})
iveresov@33469 1762 return NULL;
iveresov@33469 1763 }
iveresov@33469 1764
iveresov@33469 1765 Node_List* new_cmpd_pk = new Node_List();
iveresov@33469 1766 uint sz = cmovd_pk->size() - 1;
iveresov@33469 1767 for (uint i = 0; i <= sz; ++i) {
iveresov@33469 1768 Node* cmov = cmovd_pk->at(i);
iveresov@33469 1769 Node* bol = bool_pk->at(i);
iveresov@33469 1770 Node* cmp = cmpd_pk->at(i);
iveresov@33469 1771
iveresov@33469 1772 new_cmpd_pk->insert(i, cmov);
iveresov@33469 1773
iveresov@33469 1774 map(cmov, new_cmpd_pk);
iveresov@33469 1775 map(bol, new_cmpd_pk);
iveresov@33469 1776 map(cmp, new_cmpd_pk);
iveresov@33469 1777
iveresov@33469 1778 _sw->set_my_pack(cmov, new_cmpd_pk); // and keep old packs for cmp and bool
iveresov@33469 1779 }
iveresov@33469 1780 _sw->_packset.remove(cmovd_pk);
iveresov@33469 1781 _sw->_packset.remove(bool_pk);
iveresov@33469 1782 _sw->_packset.remove(cmpd_pk);
iveresov@33469 1783 _sw->_packset.append(new_cmpd_pk);
iveresov@33469 1784 NOT_PRODUCT(if(_sw->is_trace_cmov()) {tty->print_cr("CMoveKit::make_cmovevd_pack: added syntactic CMoveD pack"); _sw->print_pack(new_cmpd_pk);})
iveresov@33469 1785 return new_cmpd_pk;
iveresov@33469 1786 }
iveresov@33469 1787
iveresov@33469 1788 bool CMoveKit::test_cmpd_pack(Node_List* cmpd_pk, Node_List* cmovd_pk) {
iveresov@33469 1789 Node* cmpd0 = cmpd_pk->at(0);
iveresov@33469 1790 assert(cmpd0->is_Cmp(), "CMoveKit::test_cmpd_pack: should be CmpDNode");
iveresov@33469 1791 assert(cmovd_pk->at(0)->is_CMove(), "CMoveKit::test_cmpd_pack: should be CMoveD");
iveresov@33469 1792 assert(cmpd_pk->size() == cmovd_pk->size(), "CMoveKit::test_cmpd_pack: should be same size");
iveresov@33469 1793 Node* in1 = cmpd0->in(1);
iveresov@33469 1794 Node* in2 = cmpd0->in(2);
iveresov@33469 1795 Node_List* in1_pk = _sw->my_pack(in1);
iveresov@33469 1796 Node_List* in2_pk = _sw->my_pack(in2);
iveresov@33469 1797
jwilhelm@46630 1798 if ( (in1_pk != NULL && in1_pk->size() != cmpd_pk->size())
jwilhelm@46630 1799 || (in2_pk != NULL && in2_pk->size() != cmpd_pk->size()) ) {
iveresov@33469 1800 return false;
iveresov@33469 1801 }
iveresov@33469 1802
iveresov@33469 1803 // test if "all" in1 are in the same pack or the same node
iveresov@33469 1804 if (in1_pk == NULL) {
iveresov@33469 1805 for (uint j = 1; j < cmpd_pk->size(); j++) {
iveresov@33469 1806 if (cmpd_pk->at(j)->in(1) != in1) {
iveresov@33469 1807 return false;
iveresov@33469 1808 }
iveresov@33469 1809 }//for: in1_pk is not pack but all CmpD nodes in the pack have the same in(1)
iveresov@33469 1810 }
iveresov@33469 1811 // test if "all" in2 are in the same pack or the same node
iveresov@33469 1812 if (in2_pk == NULL) {
iveresov@33469 1813 for (uint j = 1; j < cmpd_pk->size(); j++) {
iveresov@33469 1814 if (cmpd_pk->at(j)->in(2) != in2) {
iveresov@33469 1815 return false;
iveresov@33469 1816 }
iveresov@33469 1817 }//for: in2_pk is not pack but all CmpD nodes in the pack have the same in(2)
iveresov@33469 1818 }
iveresov@33469 1819 //now check if cmpd_pk may be subsumed in vector built for cmovd_pk
iveresov@33469 1820 int cmovd_ind1, cmovd_ind2;
iveresov@33469 1821 if (cmpd_pk->at(0)->in(1) == cmovd_pk->at(0)->as_CMove()->in(CMoveNode::IfFalse)
iveresov@33469 1822 && cmpd_pk->at(0)->in(2) == cmovd_pk->at(0)->as_CMove()->in(CMoveNode::IfTrue)) {
iveresov@33469 1823 cmovd_ind1 = CMoveNode::IfFalse;
iveresov@33469 1824 cmovd_ind2 = CMoveNode::IfTrue;
iveresov@33469 1825 } else if (cmpd_pk->at(0)->in(2) == cmovd_pk->at(0)->as_CMove()->in(CMoveNode::IfFalse)
iveresov@33469 1826 && cmpd_pk->at(0)->in(1) == cmovd_pk->at(0)->as_CMove()->in(CMoveNode::IfTrue)) {
iveresov@33469 1827 cmovd_ind2 = CMoveNode::IfFalse;
iveresov@33469 1828 cmovd_ind1 = CMoveNode::IfTrue;
iveresov@33469 1829 }
iveresov@33469 1830 else {
iveresov@33469 1831 return false;
iveresov@33469 1832 }
iveresov@33469 1833
iveresov@33469 1834 for (uint j = 1; j < cmpd_pk->size(); j++) {
iveresov@33469 1835 if (cmpd_pk->at(j)->in(1) != cmovd_pk->at(j)->as_CMove()->in(cmovd_ind1)
iveresov@33469 1836 || cmpd_pk->at(j)->in(2) != cmovd_pk->at(j)->as_CMove()->in(cmovd_ind2)) {
iveresov@33469 1837 return false;
iveresov@33469 1838 }//if
iveresov@33469 1839 }
iveresov@33469 1840 NOT_PRODUCT(if(_sw->is_trace_cmov()) { tty->print("CMoveKit::test_cmpd_pack: cmpd pack for 1st CmpD %d is OK for vectorization: ", cmpd0->_idx); cmpd0->dump(); })
iveresov@33469 1841 return true;
iveresov@33469 1842 }
iveresov@33469 1843
duke@1 1844 //------------------------------implemented---------------------------
duke@1 1845 // Can code be generated for pack p?
duke@1 1846 bool SuperWord::implemented(Node_List* p) {
kvn@30211 1847 bool retValue = false;
duke@1 1848 Node* p0 = p->at(0);
kvn@30211 1849 if (p0 != NULL) {
kvn@30211 1850 int opc = p0->Opcode();
kvn@30211 1851 uint size = p->size();
kvn@30211 1852 if (p0->is_reduction()) {
kvn@30211 1853 const Type *arith_type = p0->bottom_type();
kvn@30588 1854 // Length 2 reductions of INT/LONG do not offer performance benefits
kvn@30588 1855 if (((arith_type->basic_type() == T_INT) || (arith_type->basic_type() == T_LONG)) && (size == 2)) {
kvn@30588 1856 retValue = false;
kvn@30588 1857 } else {
kvn@30588 1858 retValue = ReductionNode::implemented(opc, size, arith_type->basic_type());
kvn@30588 1859 }
kvn@30211 1860 } else {
kvn@30211 1861 retValue = VectorNode::implemented(opc, size, velt_basic_type(p0));
kvn@30211 1862 }
iveresov@33469 1863 if (!retValue) {
iveresov@33469 1864 if (is_cmov_pack(p)) {
iveresov@33469 1865 NOT_PRODUCT(if(is_trace_cmov()) {tty->print_cr("SWPointer::implemented: found cmpd pack"); print_pack(p);})
iveresov@33469 1866 return true;
iveresov@33469 1867 }
iveresov@33469 1868 }
kvn@30211 1869 }
kvn@30211 1870 return retValue;
kvn@13490 1871 }
kvn@13490 1872
iveresov@33469 1873 bool SuperWord::is_cmov_pack(Node_List* p) {
iveresov@33469 1874 return _cmovev_kit.pack(p->at(0)) != NULL;
iveresov@33469 1875 }
kvn@13490 1876 //------------------------------same_inputs--------------------------
kvn@13490 1877 // For pack p, are all idx operands the same?
iveresov@33469 1878 bool SuperWord::same_inputs(Node_List* p, int idx) {
kvn@13490 1879 Node* p0 = p->at(0);
kvn@13490 1880 uint vlen = p->size();
kvn@13490 1881 Node* p0_def = p0->in(idx);
kvn@13490 1882 for (uint i = 1; i < vlen; i++) {
kvn@13490 1883 Node* pi = p->at(i);
kvn@13490 1884 Node* pi_def = pi->in(idx);
iveresov@33469 1885 if (p0_def != pi_def) {
kvn@13490 1886 return false;
iveresov@33469 1887 }
kvn@13488 1888 }
kvn@13490 1889 return true;
duke@1 1890 }
duke@1 1891
duke@1 1892 //------------------------------profitable---------------------------
duke@1 1893 // For pack p, are all operands and all uses (with in the block) vector?
duke@1 1894 bool SuperWord::profitable(Node_List* p) {
duke@1 1895 Node* p0 = p->at(0);
duke@1 1896 uint start, end;
kvn@13490 1897 VectorNode::vector_operands(p0, &start, &end);
duke@1 1898
kvn@13894 1899 // Return false if some inputs are not vectors or vectors with different
kvn@13894 1900 // size or alignment.
kvn@13894 1901 // Also, for now, return false if not scalar promotion case when inputs are
kvn@13894 1902 // the same. Later, implement PackNode and allow differing, non-vector inputs
kvn@13894 1903 // (maybe just the ones from outside the block.)
duke@1 1904 for (uint i = start; i < end; i++) {
iveresov@33469 1905 if (!is_vector_use(p0, i)) {
kvn@13894 1906 return false;
iveresov@33469 1907 }
duke@1 1908 }
kvn@30211 1909 // Check if reductions are connected
kvn@30211 1910 if (p0->is_reduction()) {
kvn@30211 1911 Node* second_in = p0->in(2);
kvn@30211 1912 Node_List* second_pk = my_pack(second_in);
kvn@30588 1913 if ((second_pk == NULL) || (_num_work_vecs == _num_reductions)) {
kvn@30588 1914 // Remove reduction flag if no parent pack or if not enough work
kvn@30588 1915 // to cover reduction expansion overhead
kvn@30211 1916 p0->remove_flag(Node::Flag_is_reduction);
kvn@30211 1917 return false;
kvn@30211 1918 } else if (second_pk->size() != p->size()) {
kvn@30211 1919 return false;
kvn@30211 1920 }
kvn@30211 1921 }
kvn@13490 1922 if (VectorNode::is_shift(p0)) {
kvn@13894 1923 // For now, return false if shift count is vector or not scalar promotion
kvn@13894 1924 // case (different shift counts) because it is not supported yet.
kvn@13894 1925 Node* cnt = p0->in(2);
kvn@13894 1926 Node_List* cnt_pk = my_pack(cnt);
kvn@13894 1927 if (cnt_pk != NULL)
kvn@13490 1928 return false;
kvn@13490 1929 if (!same_inputs(p, 2))
kvn@13490 1930 return false;
kvn@13490 1931 }
duke@1 1932 if (!p0->is_Store()) {
duke@1 1933 // For now, return false if not all uses are vector.
duke@1 1934 // Later, implement ExtractNode and allow non-vector uses (maybe
duke@1 1935 // just the ones outside the block.)
duke@1 1936 for (uint i = 0; i < p->size(); i++) {
duke@1 1937 Node* def = p->at(i);
iveresov@33469 1938 if (is_cmov_pack_internal_node(p, def)) {
iveresov@33469 1939 continue;
iveresov@33469 1940 }
duke@1 1941 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
duke@1 1942 Node* use = def->fast_out(j);
duke@1 1943 for (uint k = 0; k < use->req(); k++) {
duke@1 1944 Node* n = use->in(k);
duke@1 1945 if (def == n) {
kvn@30211 1946 // reductions can be loop carried dependences
kvn@30211 1947 if (def->is_reduction() && use->is_Phi())
kvn@30211 1948 continue;
duke@1 1949 if (!is_vector_use(use, k)) {
duke@1 1950 return false;
duke@1 1951 }
duke@1 1952 }
duke@1 1953 }
duke@1 1954 }
duke@1 1955 }
duke@1 1956 }
duke@1 1957 return true;
duke@1 1958 }
duke@1 1959
duke@1 1960 //------------------------------schedule---------------------------
duke@1 1961 // Adjust the memory graph for the packed operations
duke@1 1962 void SuperWord::schedule() {
duke@1 1963
duke@1 1964 // Co-locate in the memory graph the members of each memory pack
duke@1 1965 for (int i = 0; i < _packset.length(); i++) {
duke@1 1966 co_locate_pack(_packset.at(i));
duke@1 1967 }
duke@1 1968 }
duke@1 1969
cfang@2334 1970 //-------------------------------remove_and_insert-------------------
kvn@13104 1971 // Remove "current" from its current position in the memory graph and insert
kvn@13104 1972 // it after the appropriate insertion point (lip or uip).
cfang@2334 1973 void SuperWord::remove_and_insert(MemNode *current, MemNode *prev, MemNode *lip,
cfang@2334 1974 Node *uip, Unique_Node_List &sched_before) {
cfang@2334 1975 Node* my_mem = current->in(MemNode::Memory);
kvn@13104 1976 bool sched_up = sched_before.member(current);
cfang@2334 1977
kvn@13104 1978 // remove current_store from its current position in the memmory graph
cfang@2334 1979 for (DUIterator i = current->outs(); current->has_out(i); i++) {
cfang@2334 1980 Node* use = current->out(i);
cfang@2334 1981 if (use->is_Mem()) {
cfang@2334 1982 assert(use->in(MemNode::Memory) == current, "must be");
cfang@2334 1983 if (use == prev) { // connect prev to my_mem
kvn@13104 1984 _igvn.replace_input_of(use, MemNode::Memory, my_mem);
kvn@13104 1985 --i; //deleted this edge; rescan position
cfang@2334 1986 } else if (sched_before.member(use)) {
kvn@13104 1987 if (!sched_up) { // Will be moved together with current
kvn@13104 1988 _igvn.replace_input_of(use, MemNode::Memory, uip);
kvn@13104 1989 --i; //deleted this edge; rescan position
kvn@13104 1990 }
cfang@2334 1991 } else {
kvn@13104 1992 if (sched_up) { // Will be moved together with current
kvn@13104 1993 _igvn.replace_input_of(use, MemNode::Memory, lip);
kvn@13104 1994 --i; //deleted this edge; rescan position
kvn@13104 1995 }
cfang@2334 1996 }
cfang@2334 1997 }
cfang@2334 1998 }
cfang@2334 1999
cfang@2334 2000 Node *insert_pt = sched_up ? uip : lip;
cfang@2334 2001
cfang@2334 2002 // all uses of insert_pt's memory state should use current's instead
cfang@2334 2003 for (DUIterator i = insert_pt->outs(); insert_pt->has_out(i); i++) {
cfang@2334 2004 Node* use = insert_pt->out(i);
cfang@2334 2005 if (use->is_Mem()) {
cfang@2334 2006 assert(use->in(MemNode::Memory) == insert_pt, "must be");
kvn@12958 2007 _igvn.replace_input_of(use, MemNode::Memory, current);
cfang@2334 2008 --i; //deleted this edge; rescan position
cfang@2334 2009 } else if (!sched_up && use->is_Phi() && use->bottom_type() == Type::MEMORY) {
cfang@2334 2010 uint pos; //lip (lower insert point) must be the last one in the memory slice
cfang@2334 2011 for (pos=1; pos < use->req(); pos++) {
cfang@2334 2012 if (use->in(pos) == insert_pt) break;
cfang@2334 2013 }
kvn@12958 2014 _igvn.replace_input_of(use, pos, current);
cfang@2334 2015 --i;
cfang@2334 2016 }
cfang@2334 2017 }
cfang@2334 2018
cfang@2334 2019 //connect current to insert_pt
kvn@13104 2020 _igvn.replace_input_of(current, MemNode::Memory, insert_pt);
cfang@2334 2021 }
cfang@2334 2022
cfang@2334 2023 //------------------------------co_locate_pack----------------------------------
cfang@2334 2024 // To schedule a store pack, we need to move any sandwiched memory ops either before
cfang@2334 2025 // or after the pack, based upon dependence information:
cfang@2334 2026 // (1) If any store in the pack depends on the sandwiched memory op, the
cfang@2334 2027 // sandwiched memory op must be scheduled BEFORE the pack;
cfang@2334 2028 // (2) If a sandwiched memory op depends on any store in the pack, the
cfang@2334 2029 // sandwiched memory op must be scheduled AFTER the pack;
cfang@2334 2030 // (3) If a sandwiched memory op (say, memA) depends on another sandwiched
cfang@2334 2031 // memory op (say memB), memB must be scheduled before memA. So, if memA is
cfang@2334 2032 // scheduled before the pack, memB must also be scheduled before the pack;
cfang@2334 2033 // (4) If there is no dependence restriction for a sandwiched memory op, we simply
cfang@2334 2034 // schedule this store AFTER the pack
cfang@2334 2035 // (5) We know there is no dependence cycle, so there in no other case;
cfang@2334 2036 // (6) Finally, all memory ops in another single pack should be moved in the same direction.
cfang@2334 2037 //
cfang@3799 2038 // To schedule a load pack, we use the memory state of either the first or the last load in
cfang@3799 2039 // the pack, based on the dependence constraint.
duke@1 2040 void SuperWord::co_locate_pack(Node_List* pk) {
duke@1 2041 if (pk->at(0)->is_Store()) {
duke@1 2042 MemNode* first = executed_first(pk)->as_Mem();
duke@1 2043 MemNode* last = executed_last(pk)->as_Mem();
cfang@2334 2044 Unique_Node_List schedule_before_pack;
cfang@2334 2045 Unique_Node_List memops;
cfang@2334 2046
duke@1 2047 MemNode* current = last->in(MemNode::Memory)->as_Mem();
cfang@2334 2048 MemNode* previous = last;
duke@1 2049 while (true) {
duke@1 2050 assert(in_bb(current), "stay in block");
cfang@2334 2051 memops.push(previous);
cfang@2334 2052 for (DUIterator i = current->outs(); current->has_out(i); i++) {
cfang@2334 2053 Node* use = current->out(i);
cfang@2334 2054 if (use->is_Mem() && use != previous)
cfang@2334 2055 memops.push(use);
cfang@2334 2056 }
kvn@13104 2057 if (current == first) break;
cfang@2334 2058 previous = current;
cfang@2334 2059 current = current->in(MemNode::Memory)->as_Mem();
cfang@2334 2060 }
cfang@2334 2061
cfang@2334 2062 // determine which memory operations should be scheduled before the pack
cfang@2334 2063 for (uint i = 1; i < memops.size(); i++) {
cfang@2334 2064 Node *s1 = memops.at(i);
cfang@2334 2065 if (!in_pack(s1, pk) && !schedule_before_pack.member(s1)) {
cfang@2334 2066 for (uint j = 0; j< i; j++) {
cfang@2334 2067 Node *s2 = memops.at(j);
cfang@2334 2068 if (!independent(s1, s2)) {
cfang@2334 2069 if (in_pack(s2, pk) || schedule_before_pack.member(s2)) {
kvn@13104 2070 schedule_before_pack.push(s1); // s1 must be scheduled before
cfang@2334 2071 Node_List* mem_pk = my_pack(s1);
cfang@2334 2072 if (mem_pk != NULL) {
cfang@2334 2073 for (uint ii = 0; ii < mem_pk->size(); ii++) {
kvn@13104 2074 Node* s = mem_pk->at(ii); // follow partner
cfang@2334 2075 if (memops.member(s) && !schedule_before_pack.member(s))
cfang@2334 2076 schedule_before_pack.push(s);
cfang@2334 2077 }
cfang@2334 2078 }
kvn@13104 2079 break;
cfang@2334 2080 }
cfang@2334 2081 }
cfang@2334 2082 }
cfang@2334 2083 }
cfang@2334 2084 }
cfang@2334 2085
kvn@13104 2086 Node* upper_insert_pt = first->in(MemNode::Memory);
kvn@13104 2087 // Following code moves loads connected to upper_insert_pt below aliased stores.
kvn@13104 2088 // Collect such loads here and reconnect them back to upper_insert_pt later.
kvn@13104 2089 memops.clear();
kvn@13104 2090 for (DUIterator i = upper_insert_pt->outs(); upper_insert_pt->has_out(i); i++) {
kvn@13104 2091 Node* use = upper_insert_pt->out(i);
kvn@24090 2092 if (use->is_Mem() && !use->is_Store()) {
kvn@13104 2093 memops.push(use);
kvn@24090 2094 }
kvn@13104 2095 }
kvn@13104 2096
cfang@2334 2097 MemNode* lower_insert_pt = last;
cfang@2334 2098 previous = last; //previous store in pk
cfang@2334 2099 current = last->in(MemNode::Memory)->as_Mem();
cfang@2334 2100
kvn@13104 2101 // start scheduling from "last" to "first"
cfang@2334 2102 while (true) {
cfang@2334 2103 assert(in_bb(current), "stay in block");
cfang@2334 2104 assert(in_pack(previous, pk), "previous stays in pack");
duke@1 2105 Node* my_mem = current->in(MemNode::Memory);
cfang@2334 2106
duke@1 2107 if (in_pack(current, pk)) {
cfang@2334 2108 // Forward users of my memory state (except "previous) to my input memory state
duke@1 2109 for (DUIterator i = current->outs(); current->has_out(i); i++) {
duke@1 2110 Node* use = current->out(i);
cfang@2334 2111 if (use->is_Mem() && use != previous) {
duke@1 2112 assert(use->in(MemNode::Memory) == current, "must be");
cfang@2334 2113 if (schedule_before_pack.member(use)) {
kvn@12958 2114 _igvn.replace_input_of(use, MemNode::Memory, upper_insert_pt);
cfang@2334 2115 } else {
kvn@12958 2116 _igvn.replace_input_of(use, MemNode::Memory, lower_insert_pt);
cfang@2334 2117 }
duke@1 2118 --i; // deleted this edge; rescan position
duke@1 2119 }
duke@1 2120 }
cfang@2334 2121 previous = current;
cfang@2334 2122 } else { // !in_pack(current, pk) ==> a sandwiched store
cfang@2334 2123 remove_and_insert(current, previous, lower_insert_pt, upper_insert_pt, schedule_before_pack);
duke@1 2124 }
cfang@2334 2125
duke@1 2126 if (current == first) break;
duke@1 2127 current = my_mem->as_Mem();
cfang@2334 2128 } // end while
kvn@13104 2129
kvn@13104 2130 // Reconnect loads back to upper_insert_pt.
kvn@13104 2131 for (uint i = 0; i < memops.size(); i++) {
kvn@13104 2132 Node *ld = memops.at(i);
kvn@13104 2133 if (ld->in(MemNode::Memory) != upper_insert_pt) {
kvn@13104 2134 _igvn.replace_input_of(ld, MemNode::Memory, upper_insert_pt);
kvn@13104 2135 }
kvn@13104 2136 }
cfang@2334 2137 } else if (pk->at(0)->is_Load()) { //load
cfang@3799 2138 // all loads in the pack should have the same memory state. By default,
cfang@3799 2139 // we use the memory state of the last load. However, if any load could
cfang@3799 2140 // not be moved down due to the dependence constraint, we use the memory
cfang@3799 2141 // state of the first load.
cfang@3799 2142 Node* last_mem = executed_last(pk)->in(MemNode::Memory);
cfang@3799 2143 Node* first_mem = executed_first(pk)->in(MemNode::Memory);
cfang@3799 2144 bool schedule_last = true;
cfang@3799 2145 for (uint i = 0; i < pk->size(); i++) {
cfang@3799 2146 Node* ld = pk->at(i);
cfang@3799 2147 for (Node* current = last_mem; current != ld->in(MemNode::Memory);
cfang@3799 2148 current=current->in(MemNode::Memory)) {
cfang@3799 2149 assert(current != first_mem, "corrupted memory graph");
cfang@3799 2150 if(current->is_Mem() && !independent(current, ld)){
cfang@3799 2151 schedule_last = false; // a later store depends on this load
cfang@3799 2152 break;
cfang@3799 2153 }
cfang@3799 2154 }
cfang@3799 2155 }
cfang@3799 2156
cfang@3799 2157 Node* mem_input = schedule_last ? last_mem : first_mem;
cfang@3799 2158 _igvn.hash_delete(mem_input);
cfang@3799 2159 // Give each load the same memory state
duke@1 2160 for (uint i = 0; i < pk->size(); i++) {
duke@1 2161 LoadNode* ld = pk->at(i)->as_Load();
kvn@12958 2162 _igvn.replace_input_of(ld, MemNode::Memory, mem_input);
duke@1 2163 }
duke@1 2164 }
duke@1 2165 }
duke@1 2166
iveresov@33166 2167 #ifndef PRODUCT
iveresov@33166 2168 void SuperWord::print_loop(bool whole) {
iveresov@33166 2169 Node_Stack stack(_arena, _phase->C->unique() >> 2);
iveresov@33166 2170 Node_List rpo_list;
iveresov@33166 2171 VectorSet visited(_arena);
iveresov@33166 2172 visited.set(lpt()->_head->_idx);
iveresov@33166 2173 _phase->rpo(lpt()->_head, stack, visited, rpo_list);
iveresov@33166 2174 _phase->dump(lpt(), rpo_list.size(), rpo_list );
iveresov@33166 2175 if(whole) {
iveresov@33166 2176 tty->print_cr("\n Whole loop tree");
iveresov@33166 2177 _phase->dump();
iveresov@33166 2178 tty->print_cr(" End of whole loop tree\n");
iveresov@33166 2179 }
iveresov@33166 2180 }
iveresov@33166 2181 #endif
iveresov@33166 2182
duke@1 2183 //------------------------------output---------------------------
duke@1 2184 // Convert packs into vector node operations
duke@1 2185 void SuperWord::output() {
zyao@47380 2186 CountedLoopNode *cl = lpt()->_head->as_CountedLoop();
zyao@47380 2187 Compile* C = _phase->C;
zyao@47380 2188 if (_packset.length() == 0) {
kvn@47585 2189 if (cl->is_main_loop()) {
kvn@47585 2190 // Instigate more unrolling for optimization when vectorization fails.
kvn@47585 2191 C->set_major_progress();
kvn@47585 2192 cl->set_notpassed_slp();
kvn@47585 2193 cl->mark_do_unroll_only();
kvn@47585 2194 }
zyao@47380 2195 return;
zyao@47380 2196 }
duke@1 2197
kvn@9101 2198 #ifndef PRODUCT
kvn@9101 2199 if (TraceLoopOpts) {
iveresov@33166 2200 tty->print("SuperWord::output ");
kvn@9101 2201 lpt()->dump_head();
kvn@9101 2202 }
kvn@9101 2203 #endif
kvn@9101 2204
mcberg@38049 2205 if (cl->is_main_loop()) {
mcberg@38049 2206 // MUST ENSURE main loop's initial value is properly aligned:
mcberg@38049 2207 // (iv_initial_value + min_iv_offset) % vector_width_in_bytes() == 0
mcberg@38049 2208
mcberg@38049 2209 align_initial_loop_index(align_to_ref());
mcberg@38049 2210
mcberg@38049 2211 // Insert extract (unpack) operations for scalar uses
mcberg@38049 2212 for (int i = 0; i < _packset.length(); i++) {
mcberg@38049 2213 insert_extracts(_packset.at(i));
mcberg@38049 2214 }
duke@1 2215 }
duke@1 2216
kvn@13883 2217 uint max_vlen_in_bytes = 0;
kvn@31772 2218 uint max_vlen = 0;
mcberg@38049 2219 bool can_process_post_loop = (PostLoopMultiversioning && Matcher::has_predicated_vectors() && cl->is_post_loop());
iveresov@33166 2220
iveresov@33469 2221 NOT_PRODUCT(if(is_trace_loop_reverse()) {tty->print_cr("SWPointer::output: print loop before create_reserve_version_of_loop"); print_loop(true);})
iveresov@33469 2222
iveresov@33469 2223 CountedLoopReserveKit make_reversable(_phase, _lpt, do_reserve_copy());
iveresov@33469 2224
iveresov@33469 2225 NOT_PRODUCT(if(is_trace_loop_reverse()) {tty->print_cr("SWPointer::output: print loop after create_reserve_version_of_loop"); print_loop(true);})
iveresov@33469 2226
iveresov@33469 2227 if (do_reserve_copy() && !make_reversable.has_reserved()) {
iveresov@33469 2228 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: loop was not reserved correctly, exiting SuperWord");})
iveresov@33166 2229 return;
iveresov@33166 2230 }
iveresov@33166 2231
duke@1 2232 for (int i = 0; i < _block.length(); i++) {
duke@1 2233 Node* n = _block.at(i);
duke@1 2234 Node_List* p = my_pack(n);
duke@1 2235 if (p && n == executed_last(p)) {
duke@1 2236 uint vlen = p->size();
kvn@13883 2237 uint vlen_in_bytes = 0;
duke@1 2238 Node* vn = NULL;
duke@1 2239 Node* low_adr = p->at(0);
duke@1 2240 Node* first = executed_first(p);
mcberg@38049 2241 if (can_process_post_loop) {
mcberg@38049 2242 // override vlen with the main loops vector length
mcberg@38049 2243 vlen = cl->slp_max_unroll();
mcberg@38049 2244 }
iveresov@33469 2245 NOT_PRODUCT(if(is_trace_cmov()) {tty->print_cr("SWPointer::output: %d executed first, %d executed last in pack", first->_idx, n->_idx); print_pack(p);})
kvn@13104 2246 int opc = n->Opcode();
duke@1 2247 if (n->is_Load()) {
duke@1 2248 Node* ctl = n->in(MemNode::Control);
duke@1 2249 Node* mem = first->in(MemNode::Memory);
mcberg@31403 2250 SWPointer p1(n->as_Mem(), this, NULL, false);
kvn@25932 2251 // Identify the memory dependency for the new loadVector node by
kvn@25932 2252 // walking up through memory chain.
kvn@25932 2253 // This is done to give flexibility to the new loadVector node so that
kvn@25932 2254 // it can move above independent storeVector nodes.
kvn@25932 2255 while (mem->is_StoreVector()) {
mcberg@31403 2256 SWPointer p2(mem->as_Mem(), this, NULL, false);
kvn@25932 2257 int cmp = p1.cmp(p2);
kvn@25932 2258 if (SWPointer::not_equal(cmp) || !SWPointer::comparable(cmp)) {
kvn@25932 2259 mem = mem->in(MemNode::Memory);
kvn@25932 2260 } else {
kvn@25932 2261 break; // dependent memory
kvn@25932 2262 }
kvn@25932 2263 }
duke@1 2264 Node* adr = low_adr->in(MemNode::Address);
duke@1 2265 const TypePtr* atyp = n->adr_type();
roland@31035 2266 vn = LoadVectorNode::make(opc, ctl, mem, adr, atyp, vlen, velt_basic_type(n), control_dependency(p));
kvn@13883 2267 vlen_in_bytes = vn->as_LoadVector()->memory_size();
duke@1 2268 } else if (n->is_Store()) {
duke@1 2269 // Promote value to be stored to vector
kvn@10255 2270 Node* val = vector_opd(p, MemNode::ValueIn);
iveresov@33469 2271 if (val == NULL) {
iveresov@33469 2272 if (do_reserve_copy()) {
iveresov@33469 2273 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: val should not be NULL, exiting SuperWord");})
iveresov@33469 2274 return; //and reverse to backup IG
iveresov@33469 2275 }
iveresov@33469 2276 ShouldNotReachHere();
iveresov@33469 2277 }
iveresov@33469 2278
duke@1 2279 Node* ctl = n->in(MemNode::Control);
duke@1 2280 Node* mem = first->in(MemNode::Memory);
duke@1 2281 Node* adr = low_adr->in(MemNode::Address);
duke@1 2282 const TypePtr* atyp = n->adr_type();
thartmann@25930 2283 vn = StoreVectorNode::make(opc, ctl, mem, adr, atyp, val, vlen);
kvn@13883 2284 vlen_in_bytes = vn->as_StoreVector()->memory_size();
iveresov@33469 2285 } else if (n->req() == 3 && !is_cmov_pack(p)) {
duke@1 2286 // Promote operands to vector
kvn@30211 2287 Node* in1 = NULL;
kvn@30211 2288 bool node_isa_reduction = n->is_reduction();
kvn@30211 2289 if (node_isa_reduction) {
kvn@30211 2290 // the input to the first reduction operation is retained
kvn@30211 2291 in1 = low_adr->in(1);
kvn@30211 2292 } else {
kvn@30211 2293 in1 = vector_opd(p, 1);
iveresov@33469 2294 if (in1 == NULL) {
iveresov@33469 2295 if (do_reserve_copy()) {
iveresov@33469 2296 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: in1 should not be NULL, exiting SuperWord");})
iveresov@33469 2297 return; //and reverse to backup IG
iveresov@33469 2298 }
iveresov@33469 2299 ShouldNotReachHere();
iveresov@33469 2300 }
kvn@30211 2301 }
duke@1 2302 Node* in2 = vector_opd(p, 2);
iveresov@33469 2303 if (in2 == NULL) {
iveresov@33469 2304 if (do_reserve_copy()) {
iveresov@33469 2305 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: in2 should not be NULL, exiting SuperWord");})
iveresov@33469 2306 return; //and reverse to backup IG
iveresov@33469 2307 }
iveresov@33469 2308 ShouldNotReachHere();
iveresov@33469 2309 }
kvn@30211 2310 if (VectorNode::is_invariant_vector(in1) && (node_isa_reduction == false) && (n->is_Add() || n->is_Mul())) {
kvn@13485 2311 // Move invariant vector input into second position to avoid register spilling.
kvn@13485 2312 Node* tmp = in1;
kvn@13485 2313 in1 = in2;
kvn@13485 2314 in2 = tmp;
kvn@13485 2315 }
kvn@30211 2316 if (node_isa_reduction) {
kvn@30211 2317 const Type *arith_type = n->bottom_type();
kvn@30211 2318 vn = ReductionNode::make(opc, NULL, in1, in2, arith_type->basic_type());
kvn@30211 2319 if (in2->is_Load()) {
kvn@30211 2320 vlen_in_bytes = in2->as_LoadVector()->memory_size();
kvn@30211 2321 } else {
kvn@30211 2322 vlen_in_bytes = in2->as_Vector()->length_in_bytes();
kvn@30211 2323 }
kvn@30211 2324 } else {
kvn@30211 2325 vn = VectorNode::make(opc, in1, in2, vlen, velt_basic_type(n));
kvn@30211 2326 vlen_in_bytes = vn->as_Vector()->length_in_bytes();
kvn@30211 2327 }
rlupusoru@47893 2328 } else if (opc == Op_SqrtF || opc == Op_SqrtD || opc == Op_AbsF || opc == Op_AbsD || opc == Op_NegF || opc == Op_NegD) {
enevill@33088 2329 // Promote operand to vector (Sqrt/Abs/Neg are 2 address instructions)
mcberg@32723 2330 Node* in = vector_opd(p, 1);
mcberg@32723 2331 vn = VectorNode::make(opc, in, NULL, vlen, velt_basic_type(n));
mcberg@32723 2332 vlen_in_bytes = vn->as_Vector()->length_in_bytes();
iveresov@33469 2333 } else if (is_cmov_pack(p)) {
mcberg@38049 2334 if (can_process_post_loop) {
mcberg@38049 2335 // do not refactor of flow in post loop context
mcberg@38049 2336 return;
mcberg@38049 2337 }
iveresov@33469 2338 if (!n->is_CMove()) {
iveresov@33469 2339 continue;
iveresov@33469 2340 }
iveresov@33469 2341 // place here CMoveVDNode
iveresov@33469 2342 NOT_PRODUCT(if(is_trace_cmov()) {tty->print_cr("SWPointer::output: print before CMove vectorization"); print_loop(false);})
iveresov@33469 2343 Node* bol = n->in(CMoveNode::Condition);
iveresov@33469 2344 if (!bol->is_Bool() && bol->Opcode() == Op_ExtractI && bol->req() > 1 ) {
iveresov@33469 2345 NOT_PRODUCT(if(is_trace_cmov()) {tty->print_cr("SWPointer::output: %d is not Bool node, trying its in(1) node %d", bol->_idx, bol->in(1)->_idx); bol->dump(); bol->in(1)->dump();})
iveresov@33469 2346 bol = bol->in(1); //may be ExtractNode
iveresov@33469 2347 }
iveresov@33469 2348
iveresov@33469 2349 assert(bol->is_Bool(), "should be BoolNode - too late to bail out!");
iveresov@33469 2350 if (!bol->is_Bool()) {
iveresov@33469 2351 if (do_reserve_copy()) {
iveresov@33469 2352 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: expected %d bool node, exiting SuperWord", bol->_idx); bol->dump();})
iveresov@33469 2353 return; //and reverse to backup IG
iveresov@33469 2354 }
iveresov@33469 2355 ShouldNotReachHere();
iveresov@33469 2356 }
iveresov@33469 2357
iveresov@33469 2358 int cond = (int)bol->as_Bool()->_test._test;
iveresov@33469 2359 Node* in_cc = _igvn.intcon(cond);
iveresov@33469 2360 NOT_PRODUCT(if(is_trace_cmov()) {tty->print("SWPointer::output: created intcon in_cc node %d", in_cc->_idx); in_cc->dump();})
iveresov@33469 2361 Node* cc = bol->clone();
iveresov@33469 2362 cc->set_req(1, in_cc);
iveresov@33469 2363 NOT_PRODUCT(if(is_trace_cmov()) {tty->print("SWPointer::output: created bool cc node %d", cc->_idx); cc->dump();})
iveresov@33469 2364
iveresov@33469 2365 Node* src1 = vector_opd(p, 2); //2=CMoveNode::IfFalse
iveresov@33469 2366 if (src1 == NULL) {
iveresov@33469 2367 if (do_reserve_copy()) {
iveresov@33469 2368 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: src1 should not be NULL, exiting SuperWord");})
iveresov@33469 2369 return; //and reverse to backup IG
iveresov@33469 2370 }
iveresov@33469 2371 ShouldNotReachHere();
iveresov@33469 2372 }
iveresov@33469 2373 Node* src2 = vector_opd(p, 3); //3=CMoveNode::IfTrue
iveresov@33469 2374 if (src2 == NULL) {
iveresov@33469 2375 if (do_reserve_copy()) {
iveresov@33469 2376 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: src2 should not be NULL, exiting SuperWord");})
iveresov@33469 2377 return; //and reverse to backup IG
iveresov@33469 2378 }
iveresov@33469 2379 ShouldNotReachHere();
iveresov@33469 2380 }
iveresov@33469 2381 BasicType bt = velt_basic_type(n);
iveresov@33469 2382 const TypeVect* vt = TypeVect::make(bt, vlen);
kvn@48164 2383 assert(bt == T_FLOAT || bt == T_DOUBLE, "Only vectorization for FP cmovs is supported");
kvn@48164 2384 if (bt == T_FLOAT) {
kvn@48164 2385 vn = new CMoveVFNode(cc, src1, src2, vt);
kvn@48164 2386 } else {
kvn@48164 2387 assert(bt == T_DOUBLE, "Expected double");
kvn@48164 2388 vn = new CMoveVDNode(cc, src1, src2, vt);
kvn@48164 2389 }
iveresov@33469 2390 NOT_PRODUCT(if(is_trace_cmov()) {tty->print("SWPointer::output: created new CMove node %d: ", vn->_idx); vn->dump();})
vdeshpande@46528 2391 } else if (opc == Op_FmaD || opc == Op_FmaF) {
vdeshpande@46528 2392 // Promote operands to vector
vdeshpande@46528 2393 Node* in1 = vector_opd(p, 1);
vdeshpande@46528 2394 Node* in2 = vector_opd(p, 2);
vdeshpande@46528 2395 Node* in3 = vector_opd(p, 3);
vdeshpande@46528 2396 vn = VectorNode::make(opc, in1, in2, in3, vlen, velt_basic_type(n));
vdeshpande@46528 2397 vlen_in_bytes = vn->as_Vector()->length_in_bytes();
duke@1 2398 } else {
iveresov@33469 2399 if (do_reserve_copy()) {
iveresov@33469 2400 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: ShouldNotReachHere, exiting SuperWord");})
iveresov@33469 2401 return; //and reverse to backup IG
iveresov@33469 2402 }
duke@1 2403 ShouldNotReachHere();
duke@1 2404 }
iveresov@33469 2405
kvn@13104 2406 assert(vn != NULL, "sanity");
iveresov@33469 2407 if (vn == NULL) {
iveresov@33469 2408 if (do_reserve_copy()){
iveresov@33469 2409 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("SWPointer::output: got NULL node, cannot proceed, exiting SuperWord");})
iveresov@33469 2410 return; //and reverse to backup IG
iveresov@33469 2411 }
iveresov@33469 2412 ShouldNotReachHere();
iveresov@33469 2413 }
iveresov@33469 2414
mcberg@38049 2415 _block.at_put(i, vn);
kvn@13894 2416 _igvn.register_new_node_with_optimizer(vn);
duke@1 2417 _phase->set_ctrl(vn, _phase->get_ctrl(p->at(0)));
duke@1 2418 for (uint j = 0; j < p->size(); j++) {
duke@1 2419 Node* pm = p->at(j);
kvn@5901 2420 _igvn.replace_node(pm, vn);
duke@1 2421 }
duke@1 2422 _igvn._worklist.push(vn);
kvn@13883 2423
mcberg@38049 2424 if (can_process_post_loop) {
mcberg@38049 2425 // first check if the vector size if the maximum vector which we can use on the machine,
mcberg@38049 2426 // other vector size have reduced values for predicated data mapping.
mcberg@38049 2427 if (vlen_in_bytes != (uint)MaxVectorSize) {
mcberg@38049 2428 return;
mcberg@38049 2429 }
mcberg@38049 2430 }
mcberg@38049 2431
vdeshpande@46692 2432 if (vlen_in_bytes >= max_vlen_in_bytes && vlen > max_vlen) {
kvn@31772 2433 max_vlen = vlen;
kvn@13883 2434 max_vlen_in_bytes = vlen_in_bytes;
kvn@13883 2435 }
kvn@13104 2436 #ifdef ASSERT
kvn@13108 2437 if (TraceNewVectors) {
kvn@13104 2438 tty->print("new Vector node: ");
kvn@13104 2439 vn->dump();
kvn@13104 2440 }
kvn@13104 2441 #endif
duke@1 2442 }
iveresov@33469 2443 }//for (int i = 0; i < _block.length(); i++)
iveresov@33469 2444
roland@48422 2445 if (max_vlen_in_bytes > C->max_vector_size()) {
roland@48422 2446 C->set_max_vector_size(max_vlen_in_bytes);
roland@48422 2447 }
kvn@47585 2448 if (max_vlen_in_bytes > 0) {
kvn@47585 2449 cl->mark_loop_vectorized();
kvn@47585 2450 }
iveresov@33166 2451
kvn@31772 2452 if (SuperWordLoopUnrollAnalysis) {
kvn@31772 2453 if (cl->has_passed_slp()) {
kvn@31772 2454 uint slp_max_unroll_factor = cl->slp_max_unroll();
kvn@31772 2455 if (slp_max_unroll_factor == max_vlen) {
twisti@34174 2456 if (TraceSuperWordLoopUnrollAnalysis) {
twisti@34174 2457 tty->print_cr("vector loop(unroll=%d, len=%d)\n", max_vlen, max_vlen_in_bytes*BitsPerByte);
twisti@34174 2458 }
mcberg@38049 2459
mcberg@38049 2460 // For atomic unrolled loops which are vector mapped, instigate more unrolling
kvn@31772 2461 cl->set_notpassed_slp();
mcberg@38049 2462 if (cl->is_main_loop()) {
mcberg@38049 2463 // if vector resources are limited, do not allow additional unrolling, also
mcberg@38049 2464 // do not unroll more on pure vector loops which were not reduced so that we can
mcberg@38049 2465 // program the post loop to single iteration execution.
mcberg@38049 2466 if (FLOATPRESSURE > 8) {
mcberg@38049 2467 C->set_major_progress();
mcberg@38049 2468 cl->mark_do_unroll_only();
mcberg@38049 2469 }
iveresov@34162 2470 }
mcberg@38049 2471
mcberg@36066 2472 if (do_reserve_copy()) {
mcberg@38049 2473 if (can_process_post_loop) {
mcberg@38049 2474 // Now create the difference of trip and limit and use it as our mask index.
mcberg@38049 2475 // Note: We limited the unroll of the vectorized loop so that
mcberg@38049 2476 // only vlen-1 size iterations can remain to be mask programmed.
mcberg@38049 2477 Node *incr = cl->incr();
mcberg@38049 2478 SubINode *index = new SubINode(cl->limit(), cl->init_trip());
mcberg@38049 2479 _igvn.register_new_node_with_optimizer(index);
mcberg@38049 2480 SetVectMaskINode *mask = new SetVectMaskINode(_phase->get_ctrl(cl->init_trip()), index);
mcberg@38049 2481 _igvn.register_new_node_with_optimizer(mask);
mcberg@38049 2482 // make this a single iteration loop
mcberg@38049 2483 AddINode *new_incr = new AddINode(incr->in(1), mask);
mcberg@38049 2484 _igvn.register_new_node_with_optimizer(new_incr);
mcberg@38049 2485 _phase->set_ctrl(new_incr, _phase->get_ctrl(incr));
mcberg@38049 2486 _igvn.replace_node(incr, new_incr);
mcberg@38049 2487 cl->mark_is_multiversioned();
mcberg@38049 2488 cl->loopexit()->add_flag(Node::Flag_has_vector_mask_set);
mcberg@38049 2489 }
mcberg@36066 2490 }
kvn@31772 2491 }
kvn@31772 2492 }
kvn@31772 2493 }
iveresov@33166 2494
iveresov@33469 2495 if (do_reserve_copy()) {
iveresov@33166 2496 make_reversable.use_new();
iveresov@33166 2497 }
iveresov@33469 2498 NOT_PRODUCT(if(is_trace_loop_reverse()) {tty->print_cr("\n Final loop after SuperWord"); print_loop(true);})
iveresov@33166 2499 return;
duke@1 2500 }
duke@1 2501
duke@1 2502 //------------------------------vector_opd---------------------------
duke@1 2503 // Create a vector operand for the nodes in pack p for operand: in(opd_idx)
kvn@10255 2504 Node* SuperWord::vector_opd(Node_List* p, int opd_idx) {
duke@1 2505 Node* p0 = p->at(0);
duke@1 2506 uint vlen = p->size();
duke@1 2507 Node* opd = p0->in(opd_idx);
mcberg@38049 2508 CountedLoopNode *cl = lpt()->_head->as_CountedLoop();
mcberg@38049 2509
mcberg@38049 2510 if (PostLoopMultiversioning && Matcher::has_predicated_vectors() && cl->is_post_loop()) {
mcberg@38049 2511 // override vlen with the main loops vector length
mcberg@38049 2512 vlen = cl->slp_max_unroll();
mcberg@38049 2513 }
duke@1 2514
kvn@13490 2515 if (same_inputs(p, opd_idx)) {
kvn@13104 2516 if (opd->is_Vector() || opd->is_LoadVector()) {
kvn@13488 2517 assert(((opd_idx != 2) || !VectorNode::is_shift(p0)), "shift's count can't be vector");
iveresov@33469 2518 if (opd_idx == 2 && VectorNode::is_shift(p0)) {
iveresov@33469 2519 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("shift's count can't be vector");})
iveresov@33469 2520 return NULL;
iveresov@33469 2521 }
kvn@10255 2522 return opd; // input is matching vector
duke@1 2523 }
kvn@13485 2524 if ((opd_idx == 2) && VectorNode::is_shift(p0)) {
kvn@13485 2525 Compile* C = _phase->C;
kvn@13485 2526 Node* cnt = opd;
kvn@13930 2527 // Vector instructions do not mask shift count, do it here.
kvn@13485 2528 juint mask = (p0->bottom_type() == TypeInt::INT) ? (BitsPerInt - 1) : (BitsPerLong - 1);
kvn@13485 2529 const TypeInt* t = opd->find_int_type();
kvn@13485 2530 if (t != NULL && t->is_con()) {
kvn@13485 2531 juint shift = t->get_con();
kvn@13485 2532 if (shift > mask) { // Unsigned cmp
thartmann@25930 2533 cnt = ConNode::make(TypeInt::make(shift & mask));
kvn@13485 2534 }
kvn@13485 2535 } else {
kvn@13485 2536 if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {
thartmann@25930 2537 cnt = ConNode::make(TypeInt::make(mask));
kvn@13894 2538 _igvn.register_new_node_with_optimizer(cnt);
thartmann@24923 2539 cnt = new AndINode(opd, cnt);
kvn@13894 2540 _igvn.register_new_node_with_optimizer(cnt);
kvn@13485 2541 _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
kvn@13485 2542 }
kvn@13485 2543 assert(opd->bottom_type()->isa_int(), "int type only");
iveresov@33469 2544 if (!opd->bottom_type()->isa_int()) {
iveresov@33469 2545 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("Should be int type only");})
iveresov@33469 2546 return NULL;
iveresov@33469 2547 }
kvn@13930 2548 // Move non constant shift count into vector register.
thartmann@25930 2549 cnt = VectorNode::shift_count(p0, cnt, vlen, velt_basic_type(p0));
kvn@13485 2550 }
kvn@13485 2551 if (cnt != opd) {
kvn@13894 2552 _igvn.register_new_node_with_optimizer(cnt);
kvn@13485 2553 _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
kvn@13485 2554 }
kvn@13485 2555 return cnt;
kvn@13485 2556 }
kvn@13104 2557 assert(!opd->is_StoreVector(), "such vector is not expected here");
iveresov@33469 2558 if (opd->is_StoreVector()) {
iveresov@33469 2559 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("StoreVector is not expected here");})
iveresov@33469 2560 return NULL;
iveresov@33469 2561 }
kvn@12594 2562 // Convert scalar input to vector with the same number of elements as
kvn@12594 2563 // p0's vector. Use p0's type because size of operand's container in
kvn@12594 2564 // vector should match p0's size regardless operand's size.
kvn@12594 2565 const Type* p0_t = velt_type(p0);
thartmann@25930 2566 VectorNode* vn = VectorNode::scalar2vector(opd, vlen, p0_t);
duke@1 2567
kvn@13894 2568 _igvn.register_new_node_with_optimizer(vn);
duke@1 2569 _phase->set_ctrl(vn, _phase->get_ctrl(opd));
kvn@13104 2570 #ifdef ASSERT
kvn@13108 2571 if (TraceNewVectors) {
kvn@13104 2572 tty->print("new Vector node: ");
kvn@13104 2573 vn->dump();
kvn@13104 2574 }
kvn@13104 2575 #endif
duke@1 2576 return vn;
duke@1 2577 }
duke@1 2578
duke@1 2579 // Insert pack operation
kvn@13104 2580 BasicType bt = velt_basic_type(p0);
thartmann@25930 2581 PackNode* pk = PackNode::make(opd, vlen, bt);
kvn@12594 2582 DEBUG_ONLY( const BasicType opd_bt = opd->bottom_type()->basic_type(); )
duke@1 2583
duke@1 2584 for (uint i = 1; i < vlen; i++) {
duke@1 2585 Node* pi = p->at(i);
duke@1 2586 Node* in = pi->in(opd_idx);
duke@1 2587 assert(my_pack(in) == NULL, "Should already have been unpacked");
iveresov@33469 2588 if (my_pack(in) != NULL) {
iveresov@33469 2589 NOT_PRODUCT(if(is_trace_loop_reverse() || TraceLoopOpts) {tty->print_cr("Should already have been unpacked");})
iveresov@33469 2590 return NULL;
iveresov@33469 2591 }
kvn@12594 2592 assert(opd_bt == in->bottom_type()->basic_type(), "all same type");
kvn@13490 2593 pk->add_opd(in);
duke@1 2594 }
kvn@13894 2595 _igvn.register_new_node_with_optimizer(pk);
duke@1 2596 _phase->set_ctrl(pk, _phase->get_ctrl(opd));
kvn@13104 2597 #ifdef ASSERT
kvn@13883 2598 if (TraceNewVectors) {
kvn@13883 2599 tty->print("new Vector node: ");
kvn@13883 2600 pk->dump();
kvn@13883 2601 }
kvn@13104 2602 #endif
duke@1 2603 return pk;
duke@1 2604 }
duke@1 2605
duke@1 2606 //------------------------------insert_extracts---------------------------
duke@1 2607 // If a use of pack p is not a vector use, then replace the
duke@1 2608 // use with an extract operation.
duke@1 2609 void SuperWord::insert_extracts(Node_List* p) {
duke@1 2610 if (p->at(0)->is_Store()) return;
duke@1 2611 assert(_n_idx_list.is_empty(), "empty (node,index) list");
duke@1 2612
duke@1 2613 // Inspect each use of each pack member. For each use that is
duke@1 2614 // not a vector use, replace the use with an extract operation.
duke@1 2615
duke@1 2616 for (uint i = 0; i < p->size(); i++) {
duke@1 2617 Node* def = p->at(i);
duke@1 2618 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
duke@1 2619 Node* use = def->fast_out(j);
duke@1 2620 for (uint k = 0; k < use->req(); k++) {
duke@1 2621 Node* n = use->in(k);
duke@1 2622 if (def == n) {
iveresov@33469 2623 Node_List* u_pk = my_pack(use);
iveresov@33469 2624 if ((u_pk == NULL || !is_cmov_pack(u_pk) || use->is_CMove()) && !is_vector_use(use, k)) {
iveresov@33469 2625 _n_idx_list.push(use, k);
duke@1 2626 }
duke@1 2627 }
duke@1 2628 }
duke@1 2629 }
duke@1 2630 }
duke@1 2631
duke@1 2632 while (_n_idx_list.is_nonempty()) {
duke@1 2633 Node* use = _n_idx_list.node();
duke@1 2634 int idx = _n_idx_list.index();
duke@1 2635 _n_idx_list.pop();
duke@1 2636 Node* def = use->in(idx);
duke@1 2637
kvn@30211 2638 if (def->is_reduction()) continue;
kvn@30211 2639
duke@1 2640 // Insert extract operation
duke@1 2641 _igvn.hash_delete(def);
duke@1 2642 int def_pos = alignment(def) / data_size(def);
duke@1 2643
thartmann@25930 2644 Node* ex = ExtractNode::make(def, def_pos, velt_basic_type(def));
kvn@13894 2645 _igvn.register_new_node_with_optimizer(ex);
duke@1 2646 _phase->set_ctrl(ex, _phase->get_ctrl(def));
kvn@12958 2647 _igvn.replace_input_of(use, idx, ex);
duke@1 2648 _igvn._worklist.push(def);
duke@1 2649
duke@1 2650 bb_insert_after(ex, bb_idx(def));
kvn@13104 2651 set_velt_type(ex, velt_type(def));
duke@1 2652 }
duke@1 2653 }
duke@1 2654
duke@1 2655 //------------------------------is_vector_use---------------------------
duke@1 2656 // Is use->in(u_idx) a vector use?
duke@1 2657 bool SuperWord::is_vector_use(Node* use, int u_idx) {
duke@1 2658 Node_List* u_pk = my_pack(use);
duke@1 2659 if (u_pk == NULL) return false;
kvn@30211 2660 if (use->is_reduction()) return true;
duke@1 2661 Node* def = use->in(u_idx);
duke@1 2662 Node_List* d_pk = my_pack(def);
duke@1 2663 if (d_pk == NULL) {
duke@1 2664 // check for scalar promotion
duke@1 2665 Node* n = u_pk->at(0)->in(u_idx);
duke@1 2666 for (uint i = 1; i < u_pk->size(); i++) {
duke@1 2667 if (u_pk->at(i)->in(u_idx) != n) return false;
duke@1 2668 }
duke@1 2669 return true;
duke@1 2670 }
duke@1 2671 if (u_pk->size() != d_pk->size())
duke@1 2672 return false;
duke@1 2673 for (uint i = 0; i < u_pk->size(); i++) {
duke@1 2674 Node* ui = u_pk->at(i);
duke@1 2675 Node* di = d_pk->at(i);
duke@1 2676 if (ui->in(u_idx) != di || alignment(ui) != alignment(di))
duke@1 2677 return false;
duke@1 2678 }
duke@1 2679 return true;
duke@1 2680 }
duke@1 2681
duke@1 2682 //------------------------------construct_bb---------------------------
duke@1 2683 // Construct reverse postorder list of block members
kvn@15755 2684 bool SuperWord::construct_bb() {
duke@1 2685 Node* entry = bb();
duke@1 2686
duke@1 2687 assert(_stk.length() == 0, "stk is empty");
duke@1 2688 assert(_block.length() == 0, "block is empty");
duke@1 2689 assert(_data_entry.length() == 0, "data_entry is empty");
duke@1 2690 assert(_mem_slice_head.length() == 0, "mem_slice_head is empty");
duke@1 2691 assert(_mem_slice_tail.length() == 0, "mem_slice_tail is empty");
duke@1 2692
duke@1 2693 // Find non-control nodes with no inputs from within block,
duke@1 2694 // create a temporary map from node _idx to bb_idx for use
duke@1 2695 // by the visited and post_visited sets,
duke@1 2696 // and count number of nodes in block.
duke@1 2697 int bb_ct = 0;
kvn@30211 2698 for (uint i = 0; i < lpt()->_body.size(); i++) {
duke@1 2699 Node *n = lpt()->_body.at(i);
duke@1 2700 set_bb_idx(n, i); // Create a temporary map
duke@1 2701 if (in_bb(n)) {
kvn@15755 2702 if (n->is_LoadStore() || n->is_MergeMem() ||
kvn@15755 2703 (n->is_Proj() && !n->as_Proj()->is_CFG())) {
kvn@15755 2704 // Bailout if the loop has LoadStore, MergeMem or data Proj
kvn@15755 2705 // nodes. Superword optimization does not work with them.
kvn@15755 2706 return false;
kvn@15755 2707 }
duke@1 2708 bb_ct++;
duke@1 2709 if (!n->is_CFG()) {
duke@1 2710 bool found = false;
duke@1 2711 for (uint j = 0; j < n->req(); j++) {
duke@1 2712 Node* def = n->in(j);
duke@1 2713 if (def && in_bb(def)) {
duke@1 2714 found = true;
duke@1 2715 break;
duke@1 2716 }
duke@1 2717 }
duke@1 2718 if (!found) {
duke@1 2719 assert(n != entry, "can't be entry");
duke@1 2720 _data_entry.push(n);
duke@1 2721 }
duke@1 2722 }
duke@1 2723 }
duke@1 2724 }
duke@1 2725
duke@1 2726 // Find memory slices (head and tail)
duke@1 2727 for (DUIterator_Fast imax, i = lp()->fast_outs(imax); i < imax; i++) {
duke@1 2728 Node *n = lp()->fast_out(i);
duke@1 2729 if (in_bb(n) && (n->is_Phi() && n->bottom_type() == Type::MEMORY)) {
duke@1 2730 Node* n_tail = n->in(LoopNode::LoopBackControl);
kvn@961 2731 if (n_tail != n->in(LoopNode::EntryControl)) {
kvn@15755 2732 if (!n_tail->is_Mem()) {
david@33105 2733 assert(n_tail->is_Mem(), "unexpected node for memory slice: %s", n_tail->Name());
kvn@15755 2734 return false; // Bailout
kvn@15755 2735 }
kvn@961 2736 _mem_slice_head.push(n);
kvn@961 2737 _mem_slice_tail.push(n_tail);
kvn@961 2738 }
duke@1 2739 }
duke@1 2740 }
duke@1 2741
duke@1 2742 // Create an RPO list of nodes in block
duke@1 2743
duke@1 2744 visited_clear();
duke@1 2745 post_visited_clear();
duke@1 2746
duke@1 2747 // Push all non-control nodes with no inputs from within block, then control entry
duke@1 2748 for (int j = 0; j < _data_entry.length(); j++) {
duke@1 2749 Node* n = _data_entry.at(j);
duke@1 2750 visited_set(n);
duke@1 2751 _stk.push(n);
duke@1 2752 }
duke@1 2753 visited_set(entry);
duke@1 2754 _stk.push(entry);
duke@1 2755
duke@1 2756 // Do a depth first walk over out edges
duke@1 2757 int rpo_idx = bb_ct - 1;
duke@1 2758 int size;
kvn@30211 2759 int reduction_uses = 0;
duke@1 2760 while ((size = _stk.length()) > 0) {
duke@1 2761 Node* n = _stk.top(); // Leave node on stack
duke@1 2762 if (!visited_test_set(n)) {
duke@1 2763 // forward arc in graph
duke@1 2764 } else if (!post_visited_test(n)) {
duke@1 2765 // cross or back arc
duke@1 2766 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
duke@1 2767 Node *use = n->fast_out(i);
duke@1 2768 if (in_bb(use) && !visited_test(use) &&
duke@1 2769 // Don't go around backedge
duke@1 2770 (!use->is_Phi() || n == entry)) {
kvn@30211 2771 if (use->is_reduction()) {
kvn@30211 2772 // First see if we can map the reduction on the given system we are on, then
kvn@30211 2773 // make a data entry operation for each reduction we see.
kvn@30211 2774 BasicType bt = use->bottom_type()->basic_type();
kvn@30211 2775 if (ReductionNode::implemented(use->Opcode(), Matcher::min_vector_size(bt), bt)) {
kvn@30211 2776 reduction_uses++;
kvn@30211 2777 }
kvn@30211 2778 }
duke@1 2779 _stk.push(use);
duke@1 2780 }
duke@1 2781 }
duke@1 2782 if (_stk.length() == size) {
duke@1 2783 // There were no additional uses, post visit node now
duke@1 2784 _stk.pop(); // Remove node from stack
duke@1 2785 assert(rpo_idx >= 0, "");
duke@1 2786 _block.at_put_grow(rpo_idx, n);
duke@1 2787 rpo_idx--;
duke@1 2788 post_visited_set(n);
duke@1 2789 assert(rpo_idx >= 0 || _stk.is_empty(), "");
duke@1 2790 }
duke@1 2791 } else {
duke@1 2792 _stk.pop(); // Remove post-visited node from stack
duke@1 2793 }
kvn@31858 2794 }//while
kvn@31858 2795
kvn@31858 2796 int ii_current = -1;
goetz@31864 2797 unsigned int load_idx = (unsigned int)-1;
kvn@31858 2798 _ii_order.clear();
duke@1 2799 // Create real map of block indices for nodes
duke@1 2800 for (int j = 0; j < _block.length(); j++) {
duke@1 2801 Node* n = _block.at(j);
duke@1 2802 set_bb_idx(n, j);
kvn@31858 2803 if (_do_vector_loop && n->is_Load()) {
kvn@31858 2804 if (ii_current == -1) {
kvn@31858 2805 ii_current = _clone_map.gen(n->_idx);
kvn@31858 2806 _ii_order.push(ii_current);
kvn@31858 2807 load_idx = _clone_map.idx(n->_idx);
kvn@31858 2808 } else if (_clone_map.idx(n->_idx) == load_idx && _clone_map.gen(n->_idx) != ii_current) {
kvn@31858 2809 ii_current = _clone_map.gen(n->_idx);
kvn@31858 2810 _ii_order.push(ii_current);
kvn@31858 2811 }
kvn@31858 2812 }
kvn@31858 2813 }//for
duke@1 2814
kvn@30211 2815 // Ensure extra info is allocated.
kvn@30211 2816 initialize_bb();
duke@1 2817
duke@1 2818 #ifndef PRODUCT
kvn@31858 2819 if (_vector_loop_debug && _ii_order.length() > 0) {
kvn@31858 2820 tty->print("SuperWord::construct_bb: List of generations: ");
kvn@31858 2821 for (int jj = 0; jj < _ii_order.length(); ++jj) {
kvn@31858 2822 tty->print(" %d:%d", jj, _ii_order.at(jj));
kvn@31858 2823 }
kvn@31858 2824 tty->print_cr(" ");
kvn@31858 2825 }
duke@1 2826 if (TraceSuperWord) {
duke@1 2827 print_bb();
duke@1 2828 tty->print_cr("\ndata entry nodes: %s", _data_entry.length() > 0 ? "" : "NONE");
duke@1 2829 for (int m = 0; m < _data_entry.length(); m++) {
duke@1 2830 tty->print("%3d ", m);
duke@1 2831 _data_entry.at(m)->dump();
duke@1 2832 }
duke@1 2833 tty->print_cr("\nmemory slices: %s", _mem_slice_head.length() > 0 ? "" : "NONE");
duke@1 2834 for (int m = 0; m < _mem_slice_head.length(); m++) {
duke@1 2835 tty->print("%3d ", m); _mem_slice_head.at(m)->dump();
duke@1 2836 tty->print(" "); _mem_slice_tail.at(m)->dump();
duke@1 2837 }
duke@1 2838 }
duke@1 2839 #endif
duke@1 2840 assert(rpo_idx == -1 && bb_ct == _block.length(), "all block members found");
kvn@30211 2841 return (_mem_slice_head.length() > 0) || (reduction_uses > 0) || (_data_entry.length() > 0);
duke@1 2842 }
duke@1 2843
duke@1 2844 //------------------------------initialize_bb---------------------------
duke@1 2845 // Initialize per node info
duke@1 2846 void SuperWord::initialize_bb() {
duke@1 2847 Node* last = _block.at(_block.length() - 1);
duke@1 2848 grow_node_info(bb_idx(last));
duke@1 2849 }
duke@1 2850
duke@1 2851 //------------------------------bb_insert_after---------------------------
duke@1 2852 // Insert n into block after pos
duke@1 2853 void SuperWord::bb_insert_after(Node* n, int pos) {
duke@1 2854 int n_pos = pos + 1;
duke@1 2855 // Make room
duke@1 2856 for (int i = _block.length() - 1; i >= n_pos; i--) {
duke@1 2857 _block.at_put_grow(i+1, _block.at(i));
duke@1 2858 }
duke@1 2859 for (int j = _node_info.length() - 1; j >= n_pos; j--) {
duke@1 2860 _node_info.at_put_grow(j+1, _node_info.at(j));
duke@1 2861 }
duke@1 2862 // Set value
duke@1 2863 _block.at_put_grow(n_pos, n);
duke@1 2864 _node_info.at_put_grow(n_pos, SWNodeInfo::initial);
duke@1 2865 // Adjust map from node->_idx to _block index
duke@1 2866 for (int i = n_pos; i < _block.length(); i++) {
duke@1 2867 set_bb_idx(_block.at(i), i);
duke@1 2868 }
duke@1 2869 }
duke@1 2870
duke@1 2871 //------------------------------compute_max_depth---------------------------
duke@1 2872 // Compute max depth for expressions from beginning of block
duke@1 2873 // Use to prune search paths during test for independence.
duke@1 2874 void SuperWord::compute_max_depth() {
duke@1 2875 int ct = 0;
duke@1 2876 bool again;
duke@1 2877 do {
duke@1 2878 again = false;
duke@1 2879 for (int i = 0; i < _block.length(); i++) {
duke@1 2880 Node* n = _block.at(i);
duke@1 2881 if (!n->is_Phi()) {
duke@1 2882 int d_orig = depth(n);
duke@1 2883 int d_in = 0;
duke@1 2884 for (DepPreds preds(n, _dg); !preds.done(); preds.next()) {
duke@1 2885 Node* pred = preds.current();
duke@1 2886 if (in_bb(pred)) {
duke@1 2887 d_in = MAX2(d_in, depth(pred));
duke@1 2888 }
duke@1 2889 }
duke@1 2890 if (d_in + 1 != d_orig) {
duke@1 2891 set_depth(n, d_in + 1);
duke@1 2892 again = true;
duke@1 2893 }
duke@1 2894 }
duke@1 2895 }
duke@1 2896 ct++;
duke@1 2897 } while (again);
twisti@34174 2898
twisti@34174 2899 if (TraceSuperWord && Verbose) {
duke@1 2900 tty->print_cr("compute_max_depth iterated: %d times", ct);
twisti@34174 2901 }
duke@1 2902 }
duke@1 2903
duke@1 2904 //-------------------------compute_vector_element_type-----------------------
duke@1 2905 // Compute necessary vector element type for expressions
duke@1 2906 // This propagates backwards a narrower integer type when the
duke@1 2907 // upper bits of the value are not needed.
duke@1 2908 // Example: char a,b,c; a = b + c;
duke@1 2909 // Normally the type of the add is integer, but for packed character
duke@1 2910 // operations the type of the add needs to be char.
duke@1 2911 void SuperWord::compute_vector_element_type() {
twisti@34174 2912 if (TraceSuperWord && Verbose) {
duke@1 2913 tty->print_cr("\ncompute_velt_type:");
twisti@34174 2914 }
duke@1 2915
duke@1 2916 // Initial type
duke@1 2917 for (int i = 0; i < _block.length(); i++) {
duke@1 2918 Node* n = _block.at(i);
kvn@13104 2919 set_velt_type(n, container_type(n));
duke@1 2920 }
duke@1 2921
kvn@14131 2922 // Propagate integer narrowed type backwards through operations
duke@1 2923 // that don't depend on higher order bits
duke@1 2924 for (int i = _block.length() - 1; i >= 0; i--) {
duke@1 2925 Node* n = _block.at(i);
duke@1 2926 // Only integer types need be examined
kvn@14131 2927 const Type* vtn = velt_type(n);
kvn@14131 2928 if (vtn->basic_type() == T_INT) {
duke@1 2929 uint start, end;
kvn@13490 2930 VectorNode::vector_operands(n, &start, &end);
duke@1 2931
duke@1 2932 for (uint j = start; j < end; j++) {
duke@1 2933 Node* in = n->in(j);
kvn@13485 2934 // Don't propagate through a memory
kvn@13485 2935 if (!in->is_Mem() && in_bb(in) && velt_type(in)->basic_type() == T_INT &&
kvn@13485 2936 data_size(n) < data_size(in)) {
kvn@13485 2937 bool same_type = true;
kvn@13485 2938 for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) {
kvn@13485 2939 Node *use = in->fast_out(k);
kvn@13485 2940 if (!in_bb(use) || !same_velt_type(use, n)) {
kvn@13485 2941 same_type = false;
kvn@13485 2942 break;
duke@1 2943 }
kvn@13485 2944 }
kvn@13485 2945 if (same_type) {
kvn@14131 2946 // For right shifts of small integer types (bool, byte, char, short)
kvn@14131 2947 // we need precise information about sign-ness. Only Load nodes have
kvn@14131 2948 // this information because Store nodes are the same for signed and
kvn@14131 2949 // unsigned values. And any arithmetic operation after a load may
kvn@14131 2950 // expand a value to signed Int so such right shifts can't be used
kvn@14131 2951 // because vector elements do not have upper bits of Int.
kvn@14131 2952 const Type* vt = vtn;
kvn@14131 2953 if (VectorNode::is_shift(in)) {
kvn@14131 2954 Node* load = in->in(1);
kvn@14134 2955 if (load->is_Load() && in_bb(load) && (velt_type(load)->basic_type() == T_INT)) {
kvn@14131 2956 vt = velt_type(load);
kvn@14131 2957 } else if (in->Opcode() != Op_LShiftI) {
kvn@14131 2958 // Widen type to Int to avoid creation of right shift vector
kvn@14131 2959 // (align + data_size(s1) check in stmts_can_pack() will fail).
kvn@14131 2960 // Note, left shifts work regardless type.
kvn@14131 2961 vt = TypeInt::INT;
kvn@14131 2962 }
kvn@14131 2963 }
kvn@13485 2964 set_velt_type(in, vt);
duke@1 2965 }
duke@1 2966 }
duke@1 2967 }
duke@1 2968 }
duke@1 2969 }
duke@1 2970 #ifndef PRODUCT
duke@1 2971 if (TraceSuperWord && Verbose) {
duke@1 2972 for (int i = 0; i < _block.length(); i++) {
duke@1 2973 Node* n = _block.at(i);
duke@1 2974 velt_type(n)->dump();
duke@1 2975 tty->print("\t");
duke@1 2976 n->dump();
duke@1 2977 }
duke@1 2978 }
duke@1 2979 #endif
duke@1 2980 }
duke@1 2981
duke@1 2982 //------------------------------memory_alignment---------------------------
duke@1 2983 // Alignment within a vector memory reference
kvn@13885 2984 int SuperWord::memory_alignment(MemNode* s, int iv_adjust) {
kvn@31858 2985 #ifndef PRODUCT
kvn@31858 2986 if(TraceSuperWord && Verbose) {
kvn@31858 2987 tty->print("SuperWord::memory_alignment within a vector memory reference for %d: ", s->_idx); s->dump();
kvn@31858 2988 }
kvn@31858 2989 #endif
kvn@31858 2990 NOT_PRODUCT(SWPointer::Tracer::Depth ddd(0);)
mcberg@31403 2991 SWPointer p(s, this, NULL, false);
duke@1 2992 if (!p.valid()) {
kvn@31858 2993 NOT_PRODUCT(if(is_trace_alignment()) tty->print("SWPointer::memory_alignment: SWPointer p invalid, return bottom_align");)
duke@1 2994 return bottom_align;
duke@1 2995 }
kvn@13108 2996 int vw = vector_width_in_bytes(s);
kvn@13104 2997 if (vw < 2) {
kvn@31858 2998 NOT_PRODUCT(if(is_trace_alignment()) tty->print_cr("SWPointer::memory_alignment: vector_width_in_bytes < 2, return bottom_align");)
kvn@13104 2999 return bottom_align; // No vectors for this type
kvn@13104 3000 }
duke@1 3001 int offset = p.offset_in_bytes();
kvn@13885 3002 offset += iv_adjust*p.memory_size();
kvn@13104 3003 int off_rem = offset % vw;
kvn@13104 3004 int off_mod = off_rem >= 0 ? off_rem : off_rem + vw;
twisti@34174 3005 if (TraceSuperWord && Verbose) {
twisti@34174 3006 tty->print_cr("SWPointer::memory_alignment: off_rem = %d, off_mod = %d", off_rem, off_mod);
twisti@34174 3007 }
duke@1 3008 return off_mod;
duke@1 3009 }
duke@1 3010
duke@1 3011 //---------------------------container_type---------------------------
duke@1 3012 // Smallest type containing range of values
kvn@13104 3013 const Type* SuperWord::container_type(Node* n) {
kvn@13104 3014 if (n->is_Mem()) {
kvn@14131 3015 BasicType bt = n->as_Mem()->memory_type();
kvn@14131 3016 if (n->is_Store() && (bt == T_CHAR)) {
kvn@14131 3017 // Use T_SHORT type instead of T_CHAR for stored values because any
kvn@14131 3018 // preceding arithmetic operation extends values to signed Int.
kvn@14131 3019 bt = T_SHORT;
kvn@14131 3020 }
kvn@14131 3021 if (n->Opcode() == Op_LoadUB) {
kvn@14131 3022 // Adjust type for unsigned byte loads, it is important for right shifts.
kvn@14131 3023 // T_BOOLEAN is used because there is no basic type representing type
kvn@14131 3024 // TypeInt::UBYTE. Use of T_BOOLEAN for vectors is fine because only
kvn@14131 3025 // size (one byte) and sign is important.
kvn@14131 3026 bt = T_BOOLEAN;
kvn@14131 3027 }
kvn@14131 3028 return Type::get_const_basic_type(bt);
duke@1 3029 }
kvn@13104 3030 const Type* t = _igvn.type(n);
duke@1 3031 if (t->basic_type() == T_INT) {
kvn@13485 3032 // A narrow type of arithmetic operations will be determined by
kvn@13485 3033 // propagating the type of memory operations.
duke@1 3034 return TypeInt::INT;
duke@1 3035 }
duke@1 3036 return t;
duke@1 3037 }
duke@1 3038
kvn@13104 3039 bool SuperWord::same_velt_type(Node* n1, Node* n2) {
kvn@13104 3040 const Type* vt1 = velt_type(n1);
kvn@13885 3041 const Type* vt2 = velt_type(n2);
kvn@13104 3042 if (vt1->basic_type() == T_INT && vt2->basic_type() == T_INT) {
kvn@13104 3043 // Compare vectors element sizes for integer types.
kvn@13104 3044 return data_size(n1) == data_size(n2);
kvn@13104 3045 }
kvn@13104 3046 return vt1 == vt2;
kvn@13104 3047 }
kvn@13104 3048
duke@1 3049 //------------------------------in_packset---------------------------
duke@1 3050 // Are s1 and s2 in a pack pair and ordered as s1,s2?
duke@1 3051 bool SuperWord::in_packset(Node* s1, Node* s2) {
duke@1 3052 for (int i = 0; i < _packset.length(); i++) {
duke@1 3053 Node_List* p = _packset.at(i);
duke@1 3054 assert(p->size() == 2, "must be");
duke@1 3055 if (p->at(0) == s1 && p->at(p->size()-1) == s2) {
duke@1 3056 return true;
duke@1 3057 }
duke@1 3058 }
duke@1 3059 return false;
duke@1 3060 }
duke@1 3061
duke@1 3062 //------------------------------in_pack---------------------------
duke@1 3063 // Is s in pack p?
duke@1 3064 Node_List* SuperWord::in_pack(Node* s, Node_List* p) {
duke@1 3065 for (uint i = 0; i < p->size(); i++) {
duke@1 3066 if (p->at(i) == s) {
duke@1 3067 return p;
duke@1 3068 }
duke@1 3069 }
duke@1 3070 return NULL;
duke@1 3071 }
duke@1 3072
duke@1 3073 //------------------------------remove_pack_at---------------------------
duke@1 3074 // Remove the pack at position pos in the packset
duke@1 3075 void SuperWord::remove_pack_at(int pos) {
duke@1 3076 Node_List* p = _packset.at(pos);
duke@1 3077 for (uint i = 0; i < p->size(); i++) {
duke@1 3078 Node* s = p->at(i);
duke@1 3079 set_my_pack(s, NULL);
duke@1 3080 }
duke@1 3081 _packset.remove_at(pos);
duke@1 3082 }
duke@1 3083
kvn@30211 3084 void SuperWord::packset_sort(int n) {
kvn@30211 3085 // simple bubble sort so that we capitalize with O(n) when its already sorted
kvn@30211 3086 while (n != 0) {
kvn@30211 3087 bool swapped = false;
kvn@30211 3088 for (int i = 1; i < n; i++) {
kvn@30211 3089 Node_List* q_low = _packset.at(i-1);
kvn@30211 3090 Node_List* q_i = _packset.at(i);
kvn@30211 3091
kvn@30211 3092 // only swap when we find something to swap
kvn@30211 3093 if (alignment(q_low->at(0)) > alignment(q_i->at(0))) {
kvn@30211 3094 Node_List* t = q_i;
kvn@30211 3095 *(_packset.adr_at(i)) = q_low;
kvn@30211 3096 *(_packset.adr_at(i-1)) = q_i;
kvn@30211 3097 swapped = true;
kvn@30211 3098 }
kvn@30211 3099 }
kvn@30211 3100 if (swapped == false) break;
kvn@30211 3101 n--;
kvn@30211 3102 }
kvn@30211 3103 }
kvn@30211 3104
duke@1 3105 //------------------------------executed_first---------------------------
duke@1 3106 // Return the node executed first in pack p. Uses the RPO block list
duke@1 3107 // to determine order.
duke@1 3108 Node* SuperWord::executed_first(Node_List* p) {
duke@1 3109 Node* n = p->at(0);
duke@1 3110 int n_rpo = bb_idx(n);
duke@1 3111 for (uint i = 1; i < p->size(); i++) {
duke@1 3112 Node* s = p->at(i);
duke@1 3113 int s_rpo = bb_idx(s);
duke@1 3114 if (s_rpo < n_rpo) {
duke@1 3115 n = s;
duke@1 3116 n_rpo = s_rpo;
duke@1 3117 }
duke@1 3118 }
duke@1 3119 return n;
duke@1 3120 }
duke@1 3121
duke@1 3122 //------------------------------executed_last---------------------------
duke@1 3123 // Return the node executed last in pack p.
duke@1 3124 Node* SuperWord::executed_last(Node_List* p) {
duke@1 3125 Node* n = p->at(0);
duke@1 3126 int n_rpo = bb_idx(n);
duke@1 3127 for (uint i = 1; i < p->size(); i++) {
duke@1 3128 Node* s = p->at(i);
duke@1 3129 int s_rpo = bb_idx(s);
duke@1 3130 if (s_rpo > n_rpo) {
duke@1 3131 n = s;
duke@1 3132 n_rpo = s_rpo;
duke@1 3133 }
duke@1 3134 }
duke@1 3135 return n;
duke@1 3136 }
duke@1 3137
roland@31035 3138 LoadNode::ControlDependency SuperWord::control_dependency(Node_List* p) {
roland@31035 3139 LoadNode::ControlDependency dep = LoadNode::DependsOnlyOnTest;
roland@31035 3140 for (uint i = 0; i < p->size(); i++) {
roland@31035 3141 Node* n = p->at(i);
roland@31035 3142 assert(n->is_Load(), "only meaningful for loads");
roland@31035 3143 if (!n->depends_only_on_test()) {
roland@31035 3144 dep = LoadNode::Pinned;
roland@31035 3145 }
roland@31035 3146 }
roland@31035 3147 return dep;
roland@31035 3148 }
roland@31035 3149
roland@31035 3150
duke@1 3151 //----------------------------align_initial_loop_index---------------------------
duke@1 3152 // Adjust pre-loop limit so that in main loop, a load/store reference
duke@1 3153 // to align_to_ref will be a position zero in the vector.
duke@1 3154 // (iv + k) mod vector_align == 0
duke@1 3155 void SuperWord::align_initial_loop_index(MemNode* align_to_ref) {
duke@1 3156 CountedLoopNode *main_head = lp()->as_CountedLoop();
duke@1 3157 assert(main_head->is_main_loop(), "");
duke@1 3158 CountedLoopEndNode* pre_end = get_pre_loop_end(main_head);
adlertz@22919 3159 assert(pre_end != NULL, "we must have a correct pre-loop");
duke@1 3160 Node *pre_opaq1 = pre_end->limit();
duke@1 3161 assert(pre_opaq1->Opcode() == Op_Opaque1, "");
duke@1 3162 Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
never@245 3163 Node *lim0 = pre_opaq->in(1);
duke@1 3164
duke@1 3165 // Where we put new limit calculations
duke@1 3166 Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
duke@1 3167
duke@1 3168 // Ensure the original loop limit is available from the
duke@1 3169 // pre-loop Opaque1 node.
duke@1 3170 Node *orig_limit = pre_opaq->original_loop_limit();
duke@1 3171 assert(orig_limit != NULL && _igvn.type(orig_limit) != Type::TOP, "");
duke@1 3172
mcberg@31403 3173 SWPointer align_to_ref_p(align_to_ref, this, NULL, false);
kvn@13104 3174 assert(align_to_ref_p.valid(), "sanity");
duke@1 3175
never@245 3176 // Given:
never@245 3177 // lim0 == original pre loop limit
never@245 3178 // V == v_align (power of 2)
never@245 3179 // invar == extra invariant piece of the address expression
kvn@13485 3180 // e == offset [ +/- invar ]
duke@1 3181 //
never@245 3182 // When reassociating expressions involving '%' the basic rules are:
never@245 3183 // (a - b) % k == 0 => a % k == b % k
never@245 3184 // and:
never@245 3185 // (a + b) % k == 0 => a % k == (k - b) % k
never@245 3186 //
never@245 3187 // For stride > 0 && scale > 0,
never@245 3188 // Derive the new pre-loop limit "lim" such that the two constraints:
never@245 3189 // (1) lim = lim0 + N (where N is some positive integer < V)
never@245 3190 // (2) (e + lim) % V == 0
never@245 3191 // are true.
never@245 3192 //
never@245 3193 // Substituting (1) into (2),
never@245 3194 // (e + lim0 + N) % V == 0
never@245 3195 // solve for N:
never@245 3196 // N = (V - (e + lim0)) % V
never@245 3197 // substitute back into (1), so that new limit
never@245 3198 // lim = lim0 + (V - (e + lim0)) % V
never@245 3199 //
never@245 3200 // For stride > 0 && scale < 0
never@245 3201 // Constraints:
never@245 3202 // lim = lim0 + N
never@245 3203 // (e - lim) % V == 0
never@245 3204 // Solving for lim:
never@245 3205 // (e - lim0 - N) % V == 0
never@245 3206 // N = (e - lim0) % V
never@245 3207 // lim = lim0 + (e - lim0) % V
never@245 3208 //
never@245 3209 // For stride < 0 && scale > 0
never@245 3210 // Constraints:
never@245 3211 // lim = lim0 - N
never@245 3212 // (e + lim) % V == 0
never@245 3213 // Solving for lim:
never@245 3214 // (e + lim0 - N) % V == 0
never@245 3215 // N = (e + lim0) % V
never@245 3216 // lim = lim0 - (e + lim0) % V
never@245 3217 //
never@245 3218 // For stride < 0 && scale < 0
never@245 3219 // Constraints:
never@245 3220 // lim = lim0 - N
never@245 3221 // (e - lim) % V == 0
never@245 3222 // Solving for lim:
never@245 3223 // (e - lim0 + N) % V == 0
never@245 3224 // N = (V - (e - lim0)) % V
never@245 3225 // lim = lim0 - (V - (e - lim0)) % V
duke@1 3226
kvn@13108 3227 int vw = vector_width_in_bytes(align_to_ref);
never@245 3228 int stride = iv_stride();
never@245 3229 int scale = align_to_ref_p.scale_in_bytes();
duke@1 3230 int elt_size = align_to_ref_p.memory_size();
kvn@13104 3231 int v_align = vw / elt_size;
kvn@13108 3232 assert(v_align > 1, "sanity");
kvn@13485 3233 int offset = align_to_ref_p.offset_in_bytes() / elt_size;
kvn@13485 3234 Node *offsn = _igvn.intcon(offset);
duke@1 3235
kvn@13485 3236 Node *e = offsn;
duke@1 3237 if (align_to_ref_p.invar() != NULL) {
kvn@13485 3238 // incorporate any extra invariant piece producing (offset +/- invar) >>> log2(elt)
duke@1 3239 Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
roland@46728 3240 Node* invar = align_to_ref_p.invar();
roland@46728 3241 if (_igvn.type(invar)->isa_long()) {
roland@46728 3242 // Computations are done % (vector width/element size) so it's
roland@46728 3243 // safe to simply convert invar to an int and loose the upper 32
roland@46728 3244 // bit half.
roland@46728 3245 invar = new ConvL2INode(invar);
roland@46728 3246 _igvn.register_new_node_with_optimizer(invar);
roland@46728 3247 }
roland@46728 3248 Node* aref = new URShiftINode(invar, log2_elt);
kvn@13894 3249 _igvn.register_new_node_with_optimizer(aref);
duke@1 3250 _phase->set_ctrl(aref, pre_ctrl);
never@245 3251 if (align_to_ref_p.negate_invar()) {
thartmann@24923 3252 e = new SubINode(e, aref);
duke@1 3253 } else {
thartmann@24923 3254 e = new AddINode(e, aref);
duke@1 3255 }
kvn@13894 3256 _igvn.register_new_node_with_optimizer(e);
never@245 3257 _phase->set_ctrl(e, pre_ctrl);
duke@1 3258 }
kvn@13104 3259 if (vw > ObjectAlignmentInBytes) {
kvn@13104 3260 // incorporate base e +/- base && Mask >>> log2(elt)
thartmann@24923 3261 Node* xbase = new CastP2XNode(NULL, align_to_ref_p.base());
kvn@13894 3262 _igvn.register_new_node_with_optimizer(xbase);
kvn@13485 3263 #ifdef _LP64
thartmann@24923 3264 xbase = new ConvL2INode(xbase);
kvn@13894 3265 _igvn.register_new_node_with_optimizer(xbase);
kvn@13485 3266 #endif
kvn@13485 3267 Node* mask = _igvn.intcon(vw-1);
thartmann@24923 3268 Node* masked_xbase = new AndINode(xbase, mask);
kvn@13894 3269 _igvn.register_new_node_with_optimizer(masked_xbase);
kvn@13104 3270 Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
thartmann@24923 3271 Node* bref = new URShiftINode(masked_xbase, log2_elt);
kvn@13894 3272 _igvn.register_new_node_with_optimizer(bref);
kvn@13104 3273 _phase->set_ctrl(bref, pre_ctrl);
thartmann@24923 3274 e = new AddINode(e, bref);
kvn@13894 3275 _igvn.register_new_node_with_optimizer(e);
kvn@13104 3276 _phase->set_ctrl(e, pre_ctrl);
kvn@13104 3277 }
never@245 3278
never@245 3279 // compute e +/- lim0
never@245 3280 if (scale < 0) {
thartmann@24923 3281 e = new SubINode(e, lim0);
never@245 3282 } else {
thartmann@24923 3283 e = new AddINode(e, lim0);
never@245 3284 }
kvn@13894 3285 _igvn.register_new_node_with_optimizer(e);
never@245 3286 _phase->set_ctrl(e, pre_ctrl);
never@245 3287
never@245 3288 if (stride * scale > 0) {
never@245 3289 // compute V - (e +/- lim0)
never@245 3290 Node* va = _igvn.intcon(v_align);
thartmann@24923 3291 e = new SubINode(va, e);
kvn@13894 3292 _igvn.register_new_node_with_optimizer(e);
never@245 3293 _phase->set_ctrl(e, pre_ctrl);
never@245 3294 }
never@245 3295 // compute N = (exp) % V
duke@1 3296 Node* va_msk = _igvn.intcon(v_align - 1);
thartmann@24923 3297 Node* N = new AndINode(e, va_msk);
kvn@13894 3298 _igvn.register_new_node_with_optimizer(N);
never@245 3299 _phase->set_ctrl(N, pre_ctrl);
never@245 3300
never@245 3301 // substitute back into (1), so that new limit
never@245 3302 // lim = lim0 + N
never@245 3303 Node* lim;
never@245 3304 if (stride < 0) {
thartmann@24923 3305 lim = new SubINode(lim0, N);
duke@1 3306 } else {
thartmann@24923 3307 lim = new AddINode(lim0, N);
duke@1 3308 }
kvn@13894 3309 _igvn.register_new_node_with_optimizer(lim);
never@245 3310 _phase->set_ctrl(lim, pre_ctrl);
duke@1 3311 Node* constrained =
thartmann@24923 3312 (stride > 0) ? (Node*) new MinINode(lim, orig_limit)
thartmann@24923 3313 : (Node*) new MaxINode(lim, orig_limit);
kvn@13894 3314 _igvn.register_new_node_with_optimizer(constrained);
duke@1 3315 _phase->set_ctrl(constrained, pre_ctrl);
thartmann@38130 3316 _igvn.replace_input_of(pre_opaq, 1, constrained);
duke@1 3317 }
duke@1 3318
duke@1 3319 //----------------------------get_pre_loop_end---------------------------
duke@1 3320 // Find pre loop end from main loop. Returns null if none.
thartmann@33067 3321 CountedLoopEndNode* SuperWord::get_pre_loop_end(CountedLoopNode* cl) {
zmajo@36809 3322 // The loop cannot be optimized if the graph shape at
zmajo@36809 3323 // the loop entry is inappropriate.
mcberg@37292 3324 if (!PhaseIdealLoop::is_canonical_loop_entry(cl)) {
thartmann@33067 3325 return NULL;
thartmann@33067 3326 }
zmajo@36809 3327
roland@48020 3328 Node* p_f = cl->skip_strip_mined()->in(LoopNode::EntryControl)->in(0)->in(0);
duke@1 3329 if (!p_f->is_IfFalse()) return NULL;
duke@1 3330 if (!p_f->in(0)->is_CountedLoopEnd()) return NULL;
thartmann@33067 3331 CountedLoopEndNode* pre_end = p_f->in(0)->as_CountedLoopEnd();
adlertz@22919 3332 CountedLoopNode* loop_node = pre_end->loopnode();
adlertz@22919 3333 if (loop_node == NULL || !loop_node->is_pre_loop()) return NULL;
duke@1 3334 return pre_end;
duke@1 3335 }
duke@1 3336
duke@1 3337 //------------------------------init---------------------------
duke@1 3338 void SuperWord::init() {
duke@1 3339 _dg.init();
duke@1 3340 _packset.clear();
duke@1 3341 _disjoint_ptrs.clear();
duke@1 3342 _block.clear();
mcberg@38049 3343 _post_block.clear();
duke@1 3344 _data_entry.clear();
duke@1 3345 _mem_slice_head.clear();
duke@1 3346 _mem_slice_tail.clear();
kvn@30593 3347 _iteration_first.clear();
kvn@30593 3348 _iteration_last.clear();
duke@1 3349 _node_info.clear();
duke@1 3350 _align_to_ref = NULL;
duke@1 3351 _lpt = NULL;
duke@1 3352 _lp = NULL;
duke@1 3353 _bb = NULL;
duke@1 3354 _iv = NULL;
kvn@30588 3355 _race_possible = 0;
mcberg@31403 3356 _early_return = false;
kvn@30588 3357 _num_work_vecs = 0;
kvn@30588 3358 _num_reductions = 0;
duke@1 3359 }
duke@1 3360
kvn@30593 3361 //------------------------------restart---------------------------
kvn@30593 3362 void SuperWord::restart() {
kvn@30593 3363 _dg.init();
kvn@30593 3364 _packset.clear();
kvn@30593 3365 _disjoint_ptrs.clear();
kvn@30593 3366 _block.clear();
mcberg@38049 3367 _post_block.clear();
kvn@30593 3368 _data_entry.clear();
kvn@30593 3369 _mem_slice_head.clear();
kvn@30593 3370 _mem_slice_tail.clear();
kvn@30593 3371 _node_info.clear();
kvn@30593 3372 }
kvn@30593 3373
duke@1 3374 //------------------------------print_packset---------------------------
duke@1 3375 void SuperWord::print_packset() {
duke@1 3376 #ifndef PRODUCT
duke@1 3377 tty->print_cr("packset");
duke@1 3378 for (int i = 0; i < _packset.length(); i++) {
duke@1 3379 tty->print_cr("Pack: %d", i);
duke@1 3380 Node_List* p = _packset.at(i);
duke@1 3381 print_pack(p);
duke@1 3382 }
duke@1 3383 #endif
duke@1 3384 }
duke@1 3385
duke@1 3386 //------------------------------print_pack---------------------------
duke@1 3387 void SuperWord::print_pack(Node_List* p) {
duke@1 3388 for (uint i = 0; i < p->size(); i++) {
duke@1 3389 print_stmt(p->at(i));
duke@1 3390 }
duke@1 3391 }
duke@1 3392
duke@1 3393 //------------------------------print_bb---------------------------
duke@1 3394 void SuperWord::print_bb() {
duke@1 3395 #ifndef PRODUCT
duke@1 3396 tty->print_cr("\nBlock");
duke@1 3397 for (int i = 0; i < _block.length(); i++) {
duke@1 3398 Node* n = _block.at(i);
duke@1 3399 tty->print("%d ", i);
duke@1 3400 if (n) {
duke@1 3401 n->dump();
duke@1 3402 }
duke@1 3403 }
duke@1 3404 #endif
duke@1 3405 }
duke@1 3406
duke@1 3407 //------------------------------print_stmt---------------------------
duke@1 3408 void SuperWord::print_stmt(Node* s) {
duke@1 3409 #ifndef PRODUCT
duke@1 3410 tty->print(" align: %d \t", alignment(s));
duke@1 3411 s->dump();
duke@1 3412 #endif
duke@1 3413 }
duke@1 3414
duke@1 3415 //------------------------------blank---------------------------
duke@1 3416 char* SuperWord::blank(uint depth) {
duke@1 3417 static char blanks[101];
duke@1 3418 assert(depth < 101, "too deep");
duke@1 3419 for (uint i = 0; i < depth; i++) blanks[i] = ' ';
duke@1 3420 blanks[depth] = '\0';
duke@1 3421 return blanks;
duke@1 3422 }
duke@1 3423
duke@1 3424
duke@1 3425 //==============================SWPointer===========================
kvn@31858 3426 #ifndef PRODUCT
kvn@31858 3427 int SWPointer::Tracer::_depth = 0;
kvn@31858 3428 #endif
duke@1 3429 //----------------------------SWPointer------------------------
mcberg@31403 3430 SWPointer::SWPointer(MemNode* mem, SuperWord* slp, Node_Stack *nstack, bool analyze_only) :
duke@1 3431 _mem(mem), _slp(slp), _base(NULL), _adr(NULL),
mcberg@31403 3432 _scale(0), _offset(0), _invar(NULL), _negate_invar(false),
mcberg@31403 3433 _nstack(nstack), _analyze_only(analyze_only),
kvn@31858 3434 _stack_idx(0)
kvn@31858 3435 #ifndef PRODUCT
kvn@31858 3436 , _tracer(slp)
kvn@31858 3437 #endif
kvn@31858