< prev index next >

src/hotspot/share/opto/arraycopynode.cpp

Print this page




  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/barrierSet.hpp"
  27 #include "gc/shared/c2/barrierSetC2.hpp"
  28 #include "gc/shared/c2/cardTableBarrierSetC2.hpp"
  29 #include "opto/arraycopynode.hpp"

  30 #include "opto/graphKit.hpp"
  31 #include "runtime/sharedRuntime.hpp"
  32 #include "utilities/macros.hpp"



  33 
  34 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
  35   : CallNode(arraycopy_type(), NULL, TypeRawPtr::BOTTOM),
  36     _alloc_tightly_coupled(alloc_tightly_coupled),
  37     _has_negative_length_guard(has_negative_length_guard),
  38     _kind(None),
  39     _arguments_validated(false),
  40     _src_type(TypeOopPtr::BOTTOM),
  41     _dest_type(TypeOopPtr::BOTTOM) {
  42   init_class_id(Class_ArrayCopy);
  43   init_flags(Flag_is_macro);
  44   C->add_macro_node(this);
  45 }
  46 
  47 uint ArrayCopyNode::size_of() const { return sizeof(*this); }
  48 
  49 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw,
  50                                    Node* src, Node* src_offset,
  51                                    Node* dest, Node* dest_offset,
  52                                    Node* length,


 131       const TypeAryPtr* ary_src = src_type->isa_aryptr();
 132       assert (ary_src != NULL, "not an array or instance?");
 133       // clone passes a length as a rounded number of longs. If we're
 134       // cloning an array we'll do it element by element. If the
 135       // length input to ArrayCopyNode is constant, length of input
 136       // array must be too.
 137 
 138       assert((get_length_if_constant(phase) == -1) == !ary_src->size()->is_con() ||
 139              phase->is_IterGVN(), "inconsistent");
 140 
 141       if (ary_src->size()->is_con()) {
 142         return ary_src->size()->get_con();
 143       }
 144       return -1;
 145     }
 146   }
 147 
 148   return get_length_if_constant(phase);
 149 }
 150 














































 151 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {
 152   if (!is_clonebasic()) {
 153     return NULL;
 154   }
 155 
 156   Node* src = in(ArrayCopyNode::Src);
 157   Node* dest = in(ArrayCopyNode::Dest);
 158   Node* ctl = in(TypeFunc::Control);
 159   Node* in_mem = in(TypeFunc::Memory);
 160 
 161   const Type* src_type = phase->type(src);
 162 
 163   assert(src->is_AddP(), "should be base + off");
 164   assert(dest->is_AddP(), "should be base + off");
 165   Node* base_src = src->in(AddPNode::Base);
 166   Node* base_dest = dest->in(AddPNode::Base);
 167 
 168   MergeMemNode* mem = MergeMemNode::make(in_mem);
 169 
 170   const TypeInstPtr* inst_src = src_type->isa_instptr();


 188     const TypePtr* adr_type = phase->C->alias_type(field)->adr_type();
 189     Node* off = phase->MakeConX(field->offset());
 190     Node* next_src = phase->transform(new AddPNode(base_src,base_src,off));
 191     Node* next_dest = phase->transform(new AddPNode(base_dest,base_dest,off));
 192     BasicType bt = field->layout_type();
 193 
 194     const Type *type;
 195     if (bt == T_OBJECT) {
 196       if (!field->type()->is_loaded()) {
 197         type = TypeInstPtr::BOTTOM;
 198       } else {
 199         ciType* field_klass = field->type();
 200         type = TypeOopPtr::make_from_klass(field_klass->as_klass());
 201       }
 202     } else {
 203       type = Type::get_const_basic_type(bt);
 204     }
 205 
 206     Node* v = LoadNode::make(*phase, ctl, mem->memory_at(fieldidx), next_src, adr_type, type, bt, MemNode::unordered);
 207     v = phase->transform(v);





 208     Node* s = StoreNode::make(*phase, ctl, mem->memory_at(fieldidx), next_dest, adr_type, v, bt, MemNode::unordered);
 209     s = phase->transform(s);
 210     mem->set_memory_at(fieldidx, s);
 211   }
 212 
 213   if (!finish_transform(phase, can_reshape, ctl, mem)) {
 214     // Return NodeSentinel to indicate that the transform failed
 215     return NodeSentinel;
 216   }
 217 
 218   return mem;
 219 }
 220 
 221 bool ArrayCopyNode::prepare_array_copy(PhaseGVN *phase, bool can_reshape,
 222                                        Node*& adr_src,
 223                                        Node*& base_src,
 224                                        Node*& adr_dest,
 225                                        Node*& base_dest,
 226                                        BasicType& copy_type,
 227                                        const Type*& value_type,


 241     disjoint_bases = is_alloc_tightly_coupled();
 242 
 243     if (ary_src  == NULL || ary_src->klass()  == NULL ||
 244         ary_dest == NULL || ary_dest->klass() == NULL) {
 245       // We don't know if arguments are arrays
 246       return false;
 247     }
 248 
 249     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 250     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
 251     if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
 252     if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
 253 
 254     if (src_elem != dest_elem || dest_elem == T_VOID) {
 255       // We don't know if arguments are arrays of the same type
 256       return false;
 257     }
 258 
 259     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 260     if (dest_elem == T_OBJECT && (!is_alloc_tightly_coupled() ||
 261                                   bs->array_copy_requires_gc_barriers(T_OBJECT))) {
 262       // It's an object array copy but we can't emit the card marking
 263       // that is needed
 264       return false;
 265     }
 266 
 267     value_type = ary_src->elem();
 268 
 269     base_src = src;
 270     base_dest = dest;
 271 
 272     uint shift  = exact_log2(type2aelembytes(dest_elem));
 273     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
 274 
 275     adr_src = src;
 276     adr_dest = dest;
 277 
 278     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
 279     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
 280 
 281     Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));


 333   Node* ctl = in(TypeFunc::Control);
 334   if (!disjoint_bases && count > 1) {
 335     Node* src_offset = in(ArrayCopyNode::SrcPos);
 336     Node* dest_offset = in(ArrayCopyNode::DestPos);
 337     assert(src_offset != NULL && dest_offset != NULL, "should be");
 338     Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
 339     Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
 340     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
 341 
 342     phase->transform(iff);
 343 
 344     forward_ctl = phase->transform(new IfFalseNode(iff));
 345     backward_ctl = phase->transform(new IfTrueNode(iff));
 346   } else {
 347     forward_ctl = ctl;
 348   }
 349 }
 350 
 351 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
 352                                         bool can_reshape,
 353                                         Node* forward_ctl,
 354                                         Node* start_mem_src,
 355                                         Node* start_mem_dest,
 356                                         const TypePtr* atp_src,
 357                                         const TypePtr* atp_dest,
 358                                         Node* adr_src,
 359                                         Node* base_src,
 360                                         Node* adr_dest,
 361                                         Node* base_dest,
 362                                         BasicType copy_type,
 363                                         const Type* value_type,
 364                                         int count) {
 365   Node* mem = phase->C->top();
 366   if (!forward_ctl->is_top()) {
 367     // copy forward
 368     mem = start_mem_dest;
 369     uint alias_idx_src = phase->C->get_alias_index(atp_src);
 370     uint alias_idx_dest = phase->C->get_alias_index(atp_dest);



 371     bool same_alias = (alias_idx_src == alias_idx_dest);
 372 
 373     if (count > 0) {
 374       Node* v = LoadNode::make(*phase, forward_ctl, start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
 375       v = phase->transform(v);





 376       mem = StoreNode::make(*phase, forward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
 377       mem = phase->transform(mem);
 378       for (int i = 1; i < count; i++) {
 379         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 380         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 381         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 382         v = LoadNode::make(*phase, forward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered);
 383         v = phase->transform(v);





 384         mem = StoreNode::make(*phase, forward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
 385         mem = phase->transform(mem);
 386       }

 387     } else if(can_reshape) {
 388       PhaseIterGVN* igvn = phase->is_IterGVN();
 389       igvn->_worklist.push(adr_src);
 390       igvn->_worklist.push(adr_dest);
 391     }

 392   }
 393   return mem;
 394 }
 395 
 396 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
 397                                          bool can_reshape,
 398                                          Node* backward_ctl,
 399                                          Node* start_mem_src,
 400                                          Node* start_mem_dest,
 401                                          const TypePtr* atp_src,
 402                                          const TypePtr* atp_dest,
 403                                          Node* adr_src,
 404                                          Node* base_src,
 405                                          Node* adr_dest,
 406                                          Node* base_dest,
 407                                          BasicType copy_type,
 408                                          const Type* value_type,
 409                                          int count) {
 410   Node* mem = phase->C->top();
 411   if (!backward_ctl->is_top()) {
 412     // copy backward
 413     mem = start_mem_dest;
 414     uint alias_idx_src = phase->C->get_alias_index(atp_src);
 415     uint alias_idx_dest = phase->C->get_alias_index(atp_dest);





 416     bool same_alias = (alias_idx_src == alias_idx_dest);
 417 
 418     if (count > 0) {
 419       for (int i = count-1; i >= 1; i--) {
 420         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 421         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 422         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 423         Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered);
 424         v = phase->transform(v);





 425         mem = StoreNode::make(*phase, backward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
 426         mem = phase->transform(mem);
 427       }
 428       Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
 429       v = phase->transform(v);





 430       mem = StoreNode::make(*phase, backward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
 431       mem = phase->transform(mem);

 432     } else if(can_reshape) {
 433       PhaseIterGVN* igvn = phase->is_IterGVN();
 434       igvn->_worklist.push(adr_src);
 435       igvn->_worklist.push(adr_dest);
 436     }

 437   }
 438   return mem;
 439 }
 440 
 441 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
 442                                      Node* ctl, Node *mem) {
 443   if (can_reshape) {
 444     PhaseIterGVN* igvn = phase->is_IterGVN();
 445     igvn->set_delay_transform(false);
 446     if (is_clonebasic()) {
 447       Node* out_mem = proj_out(TypeFunc::Memory);
 448 
 449       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 450       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
 451           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
 452         assert(bs->array_copy_requires_gc_barriers(T_OBJECT), "can only happen with card marking");
 453         return false;
 454       }
 455 
 456       igvn->replace_node(out_mem->raw_out(0), mem);
 457 
 458       Node* out_ctl = proj_out(TypeFunc::Control);


 468       }
 469       if (callprojs.fallthrough_memproj != NULL) {
 470         igvn->replace_node(callprojs.fallthrough_memproj, mem);
 471       }
 472       if (callprojs.fallthrough_catchproj != NULL) {
 473         igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
 474       }
 475 
 476       // The ArrayCopyNode is not disconnected. It still has the
 477       // projections for the exception case. Replace current
 478       // ArrayCopyNode with a dummy new one with a top() control so
 479       // that this part of the graph stays consistent but is
 480       // eventually removed.
 481 
 482       set_req(0, phase->C->top());
 483       remove_dead_region(phase, can_reshape);
 484     }
 485   } else {
 486     if (in(TypeFunc::Control) != ctl) {
 487       // we can't return new memory and control from Ideal at parse time
 488       assert(!is_clonebasic(), "added control for clone?");

 489       return false;
 490     }
 491   }
 492   return true;
 493 }
 494 
 495 
 496 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 497   if (remove_dead_region(phase, can_reshape))  return this;
 498 
 499   if (StressArrayCopyMacroNode && !can_reshape) {
 500     phase->record_for_igvn(this);
 501     return NULL;
 502   }
 503 
 504   // See if it's a small array copy and we can inline it as
 505   // loads/stores
 506   // Here we can only do:
 507   // - arraycopy if all arguments were validated before and we don't
 508   // need card marking


 542   }
 543 
 544   Node* adr_src = NULL;
 545   Node* base_src = NULL;
 546   Node* adr_dest = NULL;
 547   Node* base_dest = NULL;
 548   BasicType copy_type = T_ILLEGAL;
 549   const Type* value_type = NULL;
 550   bool disjoint_bases = false;
 551 
 552   if (!prepare_array_copy(phase, can_reshape,
 553                           adr_src, base_src, adr_dest, base_dest,
 554                           copy_type, value_type, disjoint_bases)) {
 555     return NULL;
 556   }
 557 
 558   Node* src = in(ArrayCopyNode::Src);
 559   Node* dest = in(ArrayCopyNode::Dest);
 560   const TypePtr* atp_src = get_address_type(phase, src);
 561   const TypePtr* atp_dest = get_address_type(phase, dest);
 562   uint alias_idx_src = phase->C->get_alias_index(atp_src);
 563   uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
 564 
 565   Node *in_mem = in(TypeFunc::Memory);
 566   Node *start_mem_src = in_mem;
 567   Node *start_mem_dest = in_mem;
 568   if (in_mem->is_MergeMem()) {
 569     start_mem_src = in_mem->as_MergeMem()->memory_at(alias_idx_src);
 570     start_mem_dest = in_mem->as_MergeMem()->memory_at(alias_idx_dest);
 571   }
 572 
 573 
 574   if (can_reshape) {
 575     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
 576     phase->is_IterGVN()->set_delay_transform(true);
 577   }
 578 
 579   Node* backward_ctl = phase->C->top();
 580   Node* forward_ctl = phase->C->top();
 581   array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
 582 
 583   Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
 584                                          start_mem_src, start_mem_dest,
 585                                          atp_src, atp_dest,
 586                                          adr_src, base_src, adr_dest, base_dest,
 587                                          copy_type, value_type, count);
 588 
 589   Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
 590                                            start_mem_src, start_mem_dest,
 591                                            atp_src, atp_dest,
 592                                            adr_src, base_src, adr_dest, base_dest,
 593                                            copy_type, value_type, count);
 594 
 595   Node* ctl = NULL;
 596   if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
 597     ctl = new RegionNode(3);
 598     mem = new PhiNode(ctl, Type::MEMORY, atp_dest);
 599     ctl->init_req(1, forward_ctl);
 600     mem->init_req(1, forward_mem);
 601     ctl->init_req(2, backward_ctl);
 602     mem->init_req(2, backward_mem);
 603     ctl = phase->transform(ctl);
 604     mem = phase->transform(mem);











 605   } else if (!forward_ctl->is_top()) {
 606     ctl = forward_ctl;
 607     mem = forward_mem;
 608   } else {
 609     assert(!backward_ctl->is_top(), "no copy?");
 610     ctl = backward_ctl;
 611     mem = backward_mem;
 612   }
 613 
 614   if (can_reshape) {
 615     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
 616     phase->is_IterGVN()->set_delay_transform(false);
 617   }
 618 
 619   MergeMemNode* out_mem = MergeMemNode::make(in_mem);
 620   out_mem->set_memory_at(alias_idx_dest, mem);
 621   mem = out_mem;
 622 
 623   if (!finish_transform(phase, can_reshape, ctl, mem)) {
 624     return NULL;
 625   }
 626 
 627   return mem;
 628 }
 629 
 630 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
 631   Node* dest = in(ArrayCopyNode::Dest);
 632   if (dest->is_top()) {
 633     return false;
 634   }
 635   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
 636   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
 637   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
 638          _src_type->is_known_instance(), "result of EA not recorded");
 639 
 640   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
 641     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");




  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/barrierSet.hpp"
  27 #include "gc/shared/c2/barrierSetC2.hpp"
  28 #include "gc/shared/c2/cardTableBarrierSetC2.hpp"
  29 #include "opto/arraycopynode.hpp"
  30 #include "opto/castnode.hpp"
  31 #include "opto/graphKit.hpp"
  32 #include "runtime/sharedRuntime.hpp"
  33 #include "utilities/macros.hpp"
  34 #if INCLUDE_SHENANDOAHGC
  35 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  36 #endif
  37 
  38 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
  39   : CallNode(arraycopy_type(), NULL, TypeRawPtr::BOTTOM),
  40     _alloc_tightly_coupled(alloc_tightly_coupled),
  41     _has_negative_length_guard(has_negative_length_guard),
  42     _kind(None),
  43     _arguments_validated(false),
  44     _src_type(TypeOopPtr::BOTTOM),
  45     _dest_type(TypeOopPtr::BOTTOM) {
  46   init_class_id(Class_ArrayCopy);
  47   init_flags(Flag_is_macro);
  48   C->add_macro_node(this);
  49 }
  50 
  51 uint ArrayCopyNode::size_of() const { return sizeof(*this); }
  52 
  53 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw,
  54                                    Node* src, Node* src_offset,
  55                                    Node* dest, Node* dest_offset,
  56                                    Node* length,


 135       const TypeAryPtr* ary_src = src_type->isa_aryptr();
 136       assert (ary_src != NULL, "not an array or instance?");
 137       // clone passes a length as a rounded number of longs. If we're
 138       // cloning an array we'll do it element by element. If the
 139       // length input to ArrayCopyNode is constant, length of input
 140       // array must be too.
 141 
 142       assert((get_length_if_constant(phase) == -1) == !ary_src->size()->is_con() ||
 143              phase->is_IterGVN(), "inconsistent");
 144 
 145       if (ary_src->size()->is_con()) {
 146         return ary_src->size()->get_con();
 147       }
 148       return -1;
 149     }
 150   }
 151 
 152   return get_length_if_constant(phase);
 153 }
 154 
 155 #if INCLUDE_SHENANDOAHGC
 156 Node* ArrayCopyNode::shenandoah_add_storeval_barrier(PhaseGVN *phase, bool can_reshape, Node* v, MergeMemNode* mem, Node*& ctl) {
 157   if (ShenandoahStoreValReadBarrier) {
 158     RegionNode* region = new RegionNode(3);
 159     const Type* v_t = phase->type(v);
 160     Node* phi = new PhiNode(region, v_t->isa_oopptr() ? v_t->is_oopptr()->cast_to_nonconst() : v_t);
 161     Node* cmp = phase->transform(new CmpPNode(v, phase->zerocon(T_OBJECT)));
 162     Node* bol = phase->transform(new BoolNode(cmp, BoolTest::ne));
 163     IfNode* iff = new IfNode(ctl, bol, PROB_LIKELY_MAG(3), COUNT_UNKNOWN);
 164 
 165     phase->transform(iff);
 166     if (can_reshape) {
 167       phase->is_IterGVN()->_worklist.push(iff);
 168     } else {
 169       phase->record_for_igvn(iff);
 170     }
 171 
 172     Node* null_true = phase->transform(new IfFalseNode(iff));
 173     Node* null_false = phase->transform(new IfTrueNode(iff));
 174     region->init_req(1, null_true);
 175     region->init_req(2, null_false);
 176     phi->init_req(1, phase->zerocon(T_OBJECT));
 177     Node* cast = new CastPPNode(v, phase->type(v)->join_speculative(TypePtr::NOTNULL));
 178     cast->set_req(0, null_false);
 179     cast = phase->transform(cast);
 180     Node* rb = phase->transform(new ShenandoahReadBarrierNode(null_false, phase->C->immutable_memory(), cast, false));
 181     phi->init_req(2, rb);
 182     ctl = phase->transform(region);
 183     return phase->transform(phi);
 184   }
 185   if (ShenandoahStoreValEnqueueBarrier) {
 186     const TypePtr* adr_type = ShenandoahBarrierNode::brooks_pointer_type(phase->type(v));
 187     int alias = phase->C->get_alias_index(adr_type);
 188     Node* wb = new ShenandoahWriteBarrierNode(phase->C, ctl, mem->memory_at(alias), v);
 189     Node* wb_transformed = phase->transform(wb);
 190     Node* enqueue = phase->transform(new ShenandoahEnqueueBarrierNode(wb_transformed));
 191     if (wb_transformed == wb) {
 192       Node* proj = phase->transform(new ShenandoahWBMemProjNode(wb));
 193       mem->set_memory_at(alias, proj);
 194     }
 195     return enqueue;
 196   }
 197   return v;
 198 }
 199 #endif
 200 
 201 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {
 202   if (!is_clonebasic()) {
 203     return NULL;
 204   }
 205 
 206   Node* src = in(ArrayCopyNode::Src);
 207   Node* dest = in(ArrayCopyNode::Dest);
 208   Node* ctl = in(TypeFunc::Control);
 209   Node* in_mem = in(TypeFunc::Memory);
 210 
 211   const Type* src_type = phase->type(src);
 212 
 213   assert(src->is_AddP(), "should be base + off");
 214   assert(dest->is_AddP(), "should be base + off");
 215   Node* base_src = src->in(AddPNode::Base);
 216   Node* base_dest = dest->in(AddPNode::Base);
 217 
 218   MergeMemNode* mem = MergeMemNode::make(in_mem);
 219 
 220   const TypeInstPtr* inst_src = src_type->isa_instptr();


 238     const TypePtr* adr_type = phase->C->alias_type(field)->adr_type();
 239     Node* off = phase->MakeConX(field->offset());
 240     Node* next_src = phase->transform(new AddPNode(base_src,base_src,off));
 241     Node* next_dest = phase->transform(new AddPNode(base_dest,base_dest,off));
 242     BasicType bt = field->layout_type();
 243 
 244     const Type *type;
 245     if (bt == T_OBJECT) {
 246       if (!field->type()->is_loaded()) {
 247         type = TypeInstPtr::BOTTOM;
 248       } else {
 249         ciType* field_klass = field->type();
 250         type = TypeOopPtr::make_from_klass(field_klass->as_klass());
 251       }
 252     } else {
 253       type = Type::get_const_basic_type(bt);
 254     }
 255 
 256     Node* v = LoadNode::make(*phase, ctl, mem->memory_at(fieldidx), next_src, adr_type, type, bt, MemNode::unordered);
 257     v = phase->transform(v);
 258 #if INCLUDE_SHENANDOAHGC
 259     if (UseShenandoahGC && bt == T_OBJECT) {
 260       v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mem, ctl);
 261     }
 262 #endif
 263     Node* s = StoreNode::make(*phase, ctl, mem->memory_at(fieldidx), next_dest, adr_type, v, bt, MemNode::unordered);
 264     s = phase->transform(s);
 265     mem->set_memory_at(fieldidx, s);
 266   }
 267 
 268   if (!finish_transform(phase, can_reshape, ctl, mem)) {
 269     // Return NodeSentinel to indicate that the transform failed
 270     return NodeSentinel;
 271   }
 272 
 273   return mem;
 274 }
 275 
 276 bool ArrayCopyNode::prepare_array_copy(PhaseGVN *phase, bool can_reshape,
 277                                        Node*& adr_src,
 278                                        Node*& base_src,
 279                                        Node*& adr_dest,
 280                                        Node*& base_dest,
 281                                        BasicType& copy_type,
 282                                        const Type*& value_type,


 296     disjoint_bases = is_alloc_tightly_coupled();
 297 
 298     if (ary_src  == NULL || ary_src->klass()  == NULL ||
 299         ary_dest == NULL || ary_dest->klass() == NULL) {
 300       // We don't know if arguments are arrays
 301       return false;
 302     }
 303 
 304     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 305     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
 306     if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
 307     if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
 308 
 309     if (src_elem != dest_elem || dest_elem == T_VOID) {
 310       // We don't know if arguments are arrays of the same type
 311       return false;
 312     }
 313 
 314     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 315     if (dest_elem == T_OBJECT && (!is_alloc_tightly_coupled() ||
 316          (bs->array_copy_requires_gc_barriers(T_OBJECT) SHENANDOAHGC_ONLY(&& !ShenandoahStoreValEnqueueBarrier)))) {
 317       // It's an object array copy but we can't emit the card marking
 318       // that is needed
 319       return false;
 320     }
 321 
 322     value_type = ary_src->elem();
 323 
 324     base_src = src;
 325     base_dest = dest;
 326 
 327     uint shift  = exact_log2(type2aelembytes(dest_elem));
 328     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
 329 
 330     adr_src = src;
 331     adr_dest = dest;
 332 
 333     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
 334     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
 335 
 336     Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));


 388   Node* ctl = in(TypeFunc::Control);
 389   if (!disjoint_bases && count > 1) {
 390     Node* src_offset = in(ArrayCopyNode::SrcPos);
 391     Node* dest_offset = in(ArrayCopyNode::DestPos);
 392     assert(src_offset != NULL && dest_offset != NULL, "should be");
 393     Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
 394     Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
 395     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
 396 
 397     phase->transform(iff);
 398 
 399     forward_ctl = phase->transform(new IfFalseNode(iff));
 400     backward_ctl = phase->transform(new IfTrueNode(iff));
 401   } else {
 402     forward_ctl = ctl;
 403   }
 404 }
 405 
 406 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
 407                                         bool can_reshape,
 408                                         Node*& forward_ctl,
 409                                         MergeMemNode* mm,

 410                                         const TypePtr* atp_src,
 411                                         const TypePtr* atp_dest,
 412                                         Node* adr_src,
 413                                         Node* base_src,
 414                                         Node* adr_dest,
 415                                         Node* base_dest,
 416                                         BasicType copy_type,
 417                                         const Type* value_type,
 418                                         int count) {

 419   if (!forward_ctl->is_top()) {
 420     // copy forward
 421     mm = mm->clone()->as_MergeMem();
 422     uint alias_idx_src = phase->C->get_alias_index(atp_src);
 423     uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
 424     Node *start_mem_src = mm->memory_at(alias_idx_src);
 425     Node *start_mem_dest = mm->memory_at(alias_idx_dest);
 426     Node* mem = start_mem_dest;
 427     bool same_alias = (alias_idx_src == alias_idx_dest);
 428 
 429     if (count > 0) {
 430       Node* v = LoadNode::make(*phase, forward_ctl, start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
 431       v = phase->transform(v);
 432 #if INCLUDE_SHENANDOAHGC
 433       if (UseShenandoahGC && copy_type == T_OBJECT) {
 434         v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mm, forward_ctl);
 435       }
 436 #endif
 437       mem = StoreNode::make(*phase, forward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
 438       mem = phase->transform(mem);
 439       for (int i = 1; i < count; i++) {
 440         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 441         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 442         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 443         v = LoadNode::make(*phase, forward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered);
 444         v = phase->transform(v);
 445 #if INCLUDE_SHENANDOAHGC
 446         if (UseShenandoahGC && copy_type == T_OBJECT) {
 447           v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mm, forward_ctl);
 448         }
 449 #endif
 450         mem = StoreNode::make(*phase, forward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
 451         mem = phase->transform(mem);
 452       }
 453       mm->set_memory_at(alias_idx_dest, mem);
 454     } else if(can_reshape) {
 455       PhaseIterGVN* igvn = phase->is_IterGVN();
 456       igvn->_worklist.push(adr_src);
 457       igvn->_worklist.push(adr_dest);
 458     }
 459     return mm;
 460   }
 461   return phase->C->top();
 462 }
 463 
 464 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
 465                                          bool can_reshape,
 466                                          Node*& backward_ctl,
 467                                          MergeMemNode* mm,

 468                                          const TypePtr* atp_src,
 469                                          const TypePtr* atp_dest,
 470                                          Node* adr_src,
 471                                          Node* base_src,
 472                                          Node* adr_dest,
 473                                          Node* base_dest,
 474                                          BasicType copy_type,
 475                                          const Type* value_type,
 476                                          int count) {

 477   if (!backward_ctl->is_top()) {
 478     // copy backward
 479     mm = mm->clone()->as_MergeMem();
 480     uint alias_idx_src = phase->C->get_alias_index(atp_src);
 481     uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
 482     Node *start_mem_src = mm->memory_at(alias_idx_src);
 483     Node *start_mem_dest = mm->memory_at(alias_idx_dest);
 484     Node* mem = start_mem_dest;
 485 
 486     assert(copy_type != T_OBJECT SHENANDOAHGC_ONLY(|| ShenandoahStoreValEnqueueBarrier), "only tightly coupled allocations for object arrays");
 487     bool same_alias = (alias_idx_src == alias_idx_dest);
 488 
 489     if (count > 0) {
 490       for (int i = count-1; i >= 1; i--) {
 491         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 492         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 493         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 494         Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered);
 495         v = phase->transform(v);
 496 #if INCLUDE_SHENANDOAHGC
 497         if (UseShenandoahGC && copy_type == T_OBJECT) {
 498           v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mm, backward_ctl);
 499         }
 500 #endif
 501         mem = StoreNode::make(*phase, backward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
 502         mem = phase->transform(mem);
 503       }
 504       Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
 505       v = phase->transform(v);
 506 #if INCLUDE_SHENANDOAHGC
 507       if (UseShenandoahGC && copy_type == T_OBJECT) {
 508         v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mm, backward_ctl);
 509       }
 510 #endif
 511       mem = StoreNode::make(*phase, backward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
 512       mem = phase->transform(mem);
 513       mm->set_memory_at(alias_idx_dest, mem);
 514     } else if(can_reshape) {
 515       PhaseIterGVN* igvn = phase->is_IterGVN();
 516       igvn->_worklist.push(adr_src);
 517       igvn->_worklist.push(adr_dest);
 518     }
 519     return phase->transform(mm);
 520   }
 521   return phase->C->top();
 522 }
 523 
 524 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
 525                                      Node* ctl, Node *mem) {
 526   if (can_reshape) {
 527     PhaseIterGVN* igvn = phase->is_IterGVN();
 528     igvn->set_delay_transform(false);
 529     if (is_clonebasic()) {
 530       Node* out_mem = proj_out(TypeFunc::Memory);
 531 
 532       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 533       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
 534           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
 535         assert(bs->array_copy_requires_gc_barriers(T_OBJECT), "can only happen with card marking");
 536         return false;
 537       }
 538 
 539       igvn->replace_node(out_mem->raw_out(0), mem);
 540 
 541       Node* out_ctl = proj_out(TypeFunc::Control);


 551       }
 552       if (callprojs.fallthrough_memproj != NULL) {
 553         igvn->replace_node(callprojs.fallthrough_memproj, mem);
 554       }
 555       if (callprojs.fallthrough_catchproj != NULL) {
 556         igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
 557       }
 558 
 559       // The ArrayCopyNode is not disconnected. It still has the
 560       // projections for the exception case. Replace current
 561       // ArrayCopyNode with a dummy new one with a top() control so
 562       // that this part of the graph stays consistent but is
 563       // eventually removed.
 564 
 565       set_req(0, phase->C->top());
 566       remove_dead_region(phase, can_reshape);
 567     }
 568   } else {
 569     if (in(TypeFunc::Control) != ctl) {
 570       // we can't return new memory and control from Ideal at parse time
 571       assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
 572       phase->record_for_igvn(this);
 573       return false;
 574     }
 575   }
 576   return true;
 577 }
 578 
 579 
 580 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 581   if (remove_dead_region(phase, can_reshape))  return this;
 582 
 583   if (StressArrayCopyMacroNode && !can_reshape) {
 584     phase->record_for_igvn(this);
 585     return NULL;
 586   }
 587 
 588   // See if it's a small array copy and we can inline it as
 589   // loads/stores
 590   // Here we can only do:
 591   // - arraycopy if all arguments were validated before and we don't
 592   // need card marking


 626   }
 627 
 628   Node* adr_src = NULL;
 629   Node* base_src = NULL;
 630   Node* adr_dest = NULL;
 631   Node* base_dest = NULL;
 632   BasicType copy_type = T_ILLEGAL;
 633   const Type* value_type = NULL;
 634   bool disjoint_bases = false;
 635 
 636   if (!prepare_array_copy(phase, can_reshape,
 637                           adr_src, base_src, adr_dest, base_dest,
 638                           copy_type, value_type, disjoint_bases)) {
 639     return NULL;
 640   }
 641 
 642   Node* src = in(ArrayCopyNode::Src);
 643   Node* dest = in(ArrayCopyNode::Dest);
 644   const TypePtr* atp_src = get_address_type(phase, src);
 645   const TypePtr* atp_dest = get_address_type(phase, dest);


 646 
 647   Node *in_mem = in(TypeFunc::Memory);
 648   if (!in_mem->is_MergeMem()) {
 649     in_mem = MergeMemNode::make(in_mem);



 650   }
 651 
 652 
 653   if (can_reshape) {
 654     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
 655     phase->is_IterGVN()->set_delay_transform(true);
 656   }
 657 
 658   Node* backward_ctl = phase->C->top();
 659   Node* forward_ctl = phase->C->top();
 660   array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
 661 
 662   Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
 663                                          in_mem->as_MergeMem(),
 664                                          atp_src, atp_dest,
 665                                          adr_src, base_src, adr_dest, base_dest,
 666                                          copy_type, value_type, count);
 667 
 668   Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
 669                                            in_mem->as_MergeMem(),
 670                                            atp_src, atp_dest,
 671                                            adr_src, base_src, adr_dest, base_dest,
 672                                            copy_type, value_type, count);
 673 
 674   Node* ctl = NULL;
 675   if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
 676     ctl = new RegionNode(3);

 677     ctl->init_req(1, forward_ctl);

 678     ctl->init_req(2, backward_ctl);

 679     ctl = phase->transform(ctl);
 680     MergeMemNode* forward_mm = forward_mem->as_MergeMem();
 681     MergeMemNode* backward_mm = backward_mem->as_MergeMem();
 682     for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {
 683       if (mms.memory() != mms.memory2()) {
 684         Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));
 685         phi->init_req(1, mms.memory());
 686         phi->init_req(2, mms.memory2());
 687         phi = phase->transform(phi);
 688         mms.set_memory(phi);
 689       }
 690     }
 691     mem = forward_mem;
 692   } else if (!forward_ctl->is_top()) {
 693     ctl = forward_ctl;
 694     mem = forward_mem;
 695   } else {
 696     assert(!backward_ctl->is_top(), "no copy?");
 697     ctl = backward_ctl;
 698     mem = backward_mem;
 699   }
 700 
 701   if (can_reshape) {
 702     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
 703     phase->is_IterGVN()->set_delay_transform(false);
 704   }




 705 
 706   if (!finish_transform(phase, can_reshape, ctl, mem)) {
 707     return NULL;
 708   }
 709 
 710   return mem;
 711 }
 712 
 713 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
 714   Node* dest = in(ArrayCopyNode::Dest);
 715   if (dest->is_top()) {
 716     return false;
 717   }
 718   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
 719   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
 720   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
 721          _src_type->is_known_instance(), "result of EA not recorded");
 722 
 723   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
 724     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");


< prev index next >