1 /*
   2  * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2009 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciField.hpp"
  28 #include "ci/ciMethod.hpp"
  29 #include "ci/ciStreams.hpp"
  30 #include "interpreter/bytecodes.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "shark/sharkBlock.hpp"
  34 #include "shark/sharkConstant.hpp"
  35 #include "shark/sharkInliner.hpp"
  36 #include "shark/sharkIntrinsics.hpp"
  37 #include "shark/sharkState.hpp"
  38 #include "shark/sharkValue.hpp"
  39 #include "shark/shark_globals.hpp"
  40 
  41 using namespace llvm;
  42 
  43 class SharkInlineBlock : public SharkBlock {
  44  public:
  45   SharkInlineBlock(ciMethod* target, SharkState* state)
  46     : SharkBlock(state, target),
  47       _outer_state(state),
  48       _entry_state(new SharkState(this)) {
  49     for (int i = target->max_locals() - 1; i >= 0; i--) {
  50       SharkValue *value = NULL;
  51       if (i < target->arg_size())
  52         value = outer_state()->pop();
  53       entry_state()->set_local(i, value);
  54     }
  55   }
  56 
  57  private:
  58   SharkState* _outer_state;
  59   SharkState* _entry_state;
  60 
  61  private:
  62   SharkState* outer_state() {
  63     return _outer_state;
  64   }
  65   SharkState* entry_state() {
  66     return _entry_state;
  67   }
  68 
  69  public:
  70   void emit_IR() {
  71     parse_bytecode(0, target()->code_size());
  72   }
  73 
  74  private:
  75   void do_return(BasicType type) {
  76     if (type != T_VOID) {
  77       SharkValue *result = pop_result(type);
  78       outer_state()->push(result);
  79       if (result->is_two_word())
  80         outer_state()->push(NULL);
  81     }
  82   }
  83 };
  84 
  85 class SharkInlinerHelper : public StackObj {
  86  public:
  87   SharkInlinerHelper(ciMethod* target, SharkState* entry_state)
  88     : _target(target),
  89       _entry_state(entry_state),
  90       _iter(target) {}
  91 
  92  private:
  93   ciBytecodeStream _iter;
  94   SharkState*      _entry_state;
  95   ciMethod*        _target;
  96 
  97  public:
  98   ciBytecodeStream* iter() {
  99     return &_iter;
 100   }
 101   SharkState* entry_state() const {
 102     return _entry_state;
 103   }
 104   ciMethod* target() const {
 105     return _target;
 106   }
 107 
 108  public:
 109   Bytecodes::Code bc() {
 110     return iter()->cur_bc();
 111   }
 112   int max_locals() const {
 113     return target()->max_locals();
 114   }
 115   int max_stack() const {
 116     return target()->max_stack();
 117   }
 118 
 119   // Inlinability check
 120  public:
 121   bool is_inlinable();
 122 
 123  private:
 124   void initialize_for_check();
 125 
 126   bool do_getstatic() {
 127     return do_field_access(true, false);
 128   }
 129   bool do_getfield() {
 130     return do_field_access(true, true);
 131   }
 132   bool do_putfield() {
 133     return do_field_access(false, true);
 134   }
 135   bool do_field_access(bool is_get, bool is_field);
 136 
 137   // Local variables for inlinability check
 138  private:
 139   bool* _locals;
 140 
 141  public:
 142   bool* local_addr(int index) const {
 143     assert(index >= 0 && index < max_locals(), "bad local variable index");
 144     return &_locals[index];
 145   }
 146   bool local(int index) const {
 147     return *local_addr(index);
 148   }
 149   void set_local(int index, bool value) {
 150     *local_addr(index) = value;
 151   }
 152 
 153   // Expression stack for inlinability check
 154  private:
 155   bool* _stack;
 156   bool* _sp;
 157 
 158  public:
 159   int stack_depth() const {
 160     return _sp - _stack;
 161   }
 162   bool* stack_addr(int slot) const {
 163     assert(slot >= 0 && slot < stack_depth(), "bad stack slot");
 164     return &_sp[-(slot + 1)];
 165   }
 166   void push(bool value) {
 167     assert(stack_depth() < max_stack(), "stack overrun");
 168     *(_sp++) = value;
 169   }
 170   bool pop() {
 171     assert(stack_depth() > 0, "stack underrun");
 172     return *(--_sp);
 173   }
 174 
 175   // Methods for two-word locals
 176  public:
 177   void push_pair_local(int index) {
 178     push(local(index));
 179     push(local(index + 1));
 180   }
 181   void pop_pair_local(int index) {
 182     set_local(index + 1, pop());
 183     set_local(index, pop());
 184   }
 185 
 186   // Code generation
 187  public:
 188   void do_inline() {
 189     (new SharkInlineBlock(target(), entry_state()))->emit_IR();
 190   }
 191 };
 192 
 193 // Quick checks so we can bail out before doing too much
 194 bool SharkInliner::may_be_inlinable(ciMethod *target) {
 195   // We can't inline native methods
 196   if (target->is_native())
 197     return false;
 198 
 199   // Not much point inlining abstract ones, and in any
 200   // case we'd need a stack frame to throw the exception
 201   if (target->is_abstract())
 202     return false;
 203 
 204   // Don't inline anything huge
 205   if (target->code_size() > SharkMaxInlineSize)
 206     return false;
 207 
 208   // Monitors aren't allowed without a frame to put them in
 209   if (target->is_synchronized() || target->has_monitor_bytecodes())
 210     return false;
 211 
 212   // We don't do control flow
 213   if (target->has_exception_handlers() || target->has_jsrs())
 214     return false;
 215 
 216   // Don't try to inline constructors, as they must
 217   // eventually call Object.<init> which we can't inline.
 218   // Note that this catches <clinit> too, but why would
 219   // we be compiling that?
 220   if (target->is_initializer())
 221     return false;
 222 
 223   // Mustn't inline Object.<init>
 224   // Should be caught by the above, but just in case...
 225   if (target->intrinsic_id() == vmIntrinsics::_Object_init)
 226     return false;
 227 
 228   return true;
 229 }
 230 
 231 // Full-on detailed check, for methods that pass the quick checks
 232 // Inlined methods have no stack frame, so we can't do anything
 233 // that would require one.  This means no safepoints (and hence
 234 // no loops) and no VM calls.  No VM calls means, amongst other
 235 // things, that no exceptions can be created, which means no null
 236 // checks or divide-by-zero checks are allowed.  The lack of null
 237 // checks in particular would eliminate practically everything,
 238 // but we can get around that restriction by relying on the zero-
 239 // check eliminator to strip the checks.  To do that, we need to
 240 // walk through the method, tracking which values are and are not
 241 // zero-checked.
 242 bool SharkInlinerHelper::is_inlinable() {
 243   ResourceMark rm;
 244   initialize_for_check();
 245 
 246   SharkConstant *sc;
 247   bool a, b, c, d;
 248 
 249   iter()->reset_to_bci(0);
 250   while (iter()->next() != ciBytecodeStream::EOBC()) {
 251     switch (bc()) {
 252     case Bytecodes::_nop:
 253       break;
 254 
 255     case Bytecodes::_aconst_null:
 256       push(false);
 257       break;
 258 
 259     case Bytecodes::_iconst_0:
 260       push(false);
 261       break;
 262     case Bytecodes::_iconst_m1:
 263     case Bytecodes::_iconst_1:
 264     case Bytecodes::_iconst_2:
 265     case Bytecodes::_iconst_3:
 266     case Bytecodes::_iconst_4:
 267     case Bytecodes::_iconst_5:
 268       push(true);
 269       break;
 270 
 271     case Bytecodes::_lconst_0:
 272       push(false);
 273       push(false);
 274       break;
 275     case Bytecodes::_lconst_1:
 276       push(true);
 277       push(false);
 278       break;
 279 
 280     case Bytecodes::_fconst_0:
 281     case Bytecodes::_fconst_1:
 282     case Bytecodes::_fconst_2:
 283       push(false);
 284       break;
 285 
 286     case Bytecodes::_dconst_0:
 287     case Bytecodes::_dconst_1:
 288       push(false);
 289       push(false);
 290       break;
 291 
 292     case Bytecodes::_bipush:
 293       push(iter()->get_constant_u1() != 0);
 294       break;
 295     case Bytecodes::_sipush:
 296       push(iter()->get_constant_u2() != 0);
 297       break;
 298 
 299     case Bytecodes::_ldc:
 300     case Bytecodes::_ldc_w:
 301     case Bytecodes::_ldc2_w:
 302       sc = SharkConstant::for_ldc(iter());
 303       if (!sc->is_loaded())
 304         return false;
 305       push(sc->is_nonzero());
 306       if (sc->is_two_word())
 307         push(false);
 308       break;
 309 
 310     case Bytecodes::_iload_0:
 311     case Bytecodes::_fload_0:
 312     case Bytecodes::_aload_0:
 313       push(local(0));
 314       break;
 315     case Bytecodes::_lload_0:
 316     case Bytecodes::_dload_0:
 317       push_pair_local(0);
 318       break;
 319 
 320     case Bytecodes::_iload_1:
 321     case Bytecodes::_fload_1:
 322     case Bytecodes::_aload_1:
 323       push(local(1));
 324       break;
 325     case Bytecodes::_lload_1:
 326     case Bytecodes::_dload_1:
 327       push_pair_local(1);
 328       break;
 329 
 330     case Bytecodes::_iload_2:
 331     case Bytecodes::_fload_2:
 332     case Bytecodes::_aload_2:
 333       push(local(2));
 334       break;
 335     case Bytecodes::_lload_2:
 336     case Bytecodes::_dload_2:
 337       push_pair_local(2);
 338       break;
 339 
 340     case Bytecodes::_iload_3:
 341     case Bytecodes::_fload_3:
 342     case Bytecodes::_aload_3:
 343       push(local(3));
 344       break;
 345     case Bytecodes::_lload_3:
 346     case Bytecodes::_dload_3:
 347       push_pair_local(3);
 348       break;
 349 
 350     case Bytecodes::_iload:
 351     case Bytecodes::_fload:
 352     case Bytecodes::_aload:
 353       push(local(iter()->get_index()));
 354       break;
 355     case Bytecodes::_lload:
 356     case Bytecodes::_dload:
 357       push_pair_local(iter()->get_index());
 358       break;
 359 
 360     case Bytecodes::_istore_0:
 361     case Bytecodes::_fstore_0:
 362     case Bytecodes::_astore_0:
 363       set_local(0, pop());
 364       break;
 365     case Bytecodes::_lstore_0:
 366     case Bytecodes::_dstore_0:
 367       pop_pair_local(0);
 368       break;
 369 
 370     case Bytecodes::_istore_1:
 371     case Bytecodes::_fstore_1:
 372     case Bytecodes::_astore_1:
 373       set_local(1, pop());
 374       break;
 375     case Bytecodes::_lstore_1:
 376     case Bytecodes::_dstore_1:
 377       pop_pair_local(1);
 378       break;
 379 
 380     case Bytecodes::_istore_2:
 381     case Bytecodes::_fstore_2:
 382     case Bytecodes::_astore_2:
 383       set_local(2, pop());
 384       break;
 385     case Bytecodes::_lstore_2:
 386     case Bytecodes::_dstore_2:
 387       pop_pair_local(2);
 388       break;
 389 
 390     case Bytecodes::_istore_3:
 391     case Bytecodes::_fstore_3:
 392     case Bytecodes::_astore_3:
 393       set_local(3, pop());
 394       break;
 395     case Bytecodes::_lstore_3:
 396     case Bytecodes::_dstore_3:
 397       pop_pair_local(3);
 398       break;
 399 
 400     case Bytecodes::_istore:
 401     case Bytecodes::_fstore:
 402     case Bytecodes::_astore:
 403       set_local(iter()->get_index(), pop());
 404       break;
 405     case Bytecodes::_lstore:
 406     case Bytecodes::_dstore:
 407       pop_pair_local(iter()->get_index());
 408       break;
 409 
 410     case Bytecodes::_pop:
 411       pop();
 412       break;
 413     case Bytecodes::_pop2:
 414       pop();
 415       pop();
 416       break;
 417     case Bytecodes::_swap:
 418       a = pop();
 419       b = pop();
 420       push(a);
 421       push(b);
 422       break;
 423     case Bytecodes::_dup:
 424       a = pop();
 425       push(a);
 426       push(a);
 427       break;
 428     case Bytecodes::_dup_x1:
 429       a = pop();
 430       b = pop();
 431       push(a);
 432       push(b);
 433       push(a);
 434       break;
 435     case Bytecodes::_dup_x2:
 436       a = pop();
 437       b = pop();
 438       c = pop();
 439       push(a);
 440       push(c);
 441       push(b);
 442       push(a);
 443       break;
 444     case Bytecodes::_dup2:
 445       a = pop();
 446       b = pop();
 447       push(b);
 448       push(a);
 449       push(b);
 450       push(a);
 451       break;
 452     case Bytecodes::_dup2_x1:
 453       a = pop();
 454       b = pop();
 455       c = pop();
 456       push(b);
 457       push(a);
 458       push(c);
 459       push(b);
 460       push(a);
 461       break;
 462     case Bytecodes::_dup2_x2:
 463       a = pop();
 464       b = pop();
 465       c = pop();
 466       d = pop();
 467       push(b);
 468       push(a);
 469       push(d);
 470       push(c);
 471       push(b);
 472       push(a);
 473       break;
 474 
 475     case Bytecodes::_getfield:
 476       if (!do_getfield())
 477         return false;
 478       break;
 479     case Bytecodes::_getstatic:
 480       if (!do_getstatic())
 481         return false;
 482       break;
 483     case Bytecodes::_putfield:
 484       if (!do_putfield())
 485         return false;
 486       break;
 487 
 488     case Bytecodes::_iadd:
 489     case Bytecodes::_isub:
 490     case Bytecodes::_imul:
 491     case Bytecodes::_iand:
 492     case Bytecodes::_ixor:
 493     case Bytecodes::_ishl:
 494     case Bytecodes::_ishr:
 495     case Bytecodes::_iushr:
 496       pop();
 497       pop();
 498       push(false);
 499       break;
 500     case Bytecodes::_ior:
 501       a = pop();
 502       b = pop();
 503       push(a && b);
 504       break;
 505     case Bytecodes::_idiv:
 506     case Bytecodes::_irem:
 507       if (!pop())
 508         return false;
 509       pop();
 510       push(false);
 511       break;
 512     case Bytecodes::_ineg:
 513       break;
 514 
 515     case Bytecodes::_ladd:
 516     case Bytecodes::_lsub:
 517     case Bytecodes::_lmul:
 518     case Bytecodes::_land:
 519     case Bytecodes::_lxor:
 520       pop();
 521       pop();
 522       pop();
 523       pop();
 524       push(false);
 525       push(false);
 526       break;
 527     case Bytecodes::_lor:
 528       a = pop();
 529       b = pop();
 530       push(a && b);
 531       break;
 532     case Bytecodes::_ldiv:
 533     case Bytecodes::_lrem:
 534       pop();
 535       if (!pop())
 536         return false;
 537       pop();
 538       pop();
 539       push(false);
 540       push(false);
 541       break;
 542     case Bytecodes::_lneg:
 543       break;
 544     case Bytecodes::_lshl:
 545     case Bytecodes::_lshr:
 546     case Bytecodes::_lushr:
 547       pop();
 548       pop();
 549       pop();
 550       push(false);
 551       push(false);
 552       break;
 553 
 554     case Bytecodes::_fadd:
 555     case Bytecodes::_fsub:
 556     case Bytecodes::_fmul:
 557     case Bytecodes::_fdiv:
 558     case Bytecodes::_frem:
 559       pop();
 560       pop();
 561       push(false);
 562       break;
 563     case Bytecodes::_fneg:
 564       break;
 565 
 566     case Bytecodes::_dadd:
 567     case Bytecodes::_dsub:
 568     case Bytecodes::_dmul:
 569     case Bytecodes::_ddiv:
 570     case Bytecodes::_drem:
 571       pop();
 572       pop();
 573       pop();
 574       pop();
 575       push(false);
 576       push(false);
 577       break;
 578     case Bytecodes::_dneg:
 579       break;
 580 
 581     case Bytecodes::_iinc:
 582       set_local(iter()->get_index(), false);
 583       break;
 584 
 585     case Bytecodes::_lcmp:
 586       pop();
 587       pop();
 588       pop();
 589       pop();
 590       push(false);
 591       break;
 592 
 593     case Bytecodes::_fcmpl:
 594     case Bytecodes::_fcmpg:
 595       pop();
 596       pop();
 597       push(false);
 598       break;
 599 
 600     case Bytecodes::_dcmpl:
 601     case Bytecodes::_dcmpg:
 602       pop();
 603       pop();
 604       pop();
 605       pop();
 606       push(false);
 607       break;
 608 
 609     case Bytecodes::_i2l:
 610       push(false);
 611       break;
 612     case Bytecodes::_i2f:
 613       pop();
 614       push(false);
 615       break;
 616     case Bytecodes::_i2d:
 617       pop();
 618       push(false);
 619       push(false);
 620       break;
 621 
 622     case Bytecodes::_l2i:
 623     case Bytecodes::_l2f:
 624       pop();
 625       pop();
 626       push(false);
 627       break;
 628     case Bytecodes::_l2d:
 629       pop();
 630       pop();
 631       push(false);
 632       push(false);
 633       break;
 634 
 635     case Bytecodes::_f2i:
 636       pop();
 637       push(false);
 638       break;
 639     case Bytecodes::_f2l:
 640     case Bytecodes::_f2d:
 641       pop();
 642       push(false);
 643       push(false);
 644       break;
 645 
 646     case Bytecodes::_d2i:
 647     case Bytecodes::_d2f:
 648       pop();
 649       pop();
 650       push(false);
 651       break;
 652     case Bytecodes::_d2l:
 653       pop();
 654       pop();
 655       push(false);
 656       push(false);
 657       break;
 658 
 659     case Bytecodes::_i2b:
 660     case Bytecodes::_i2c:
 661     case Bytecodes::_i2s:
 662       pop();
 663       push(false);
 664       break;
 665 
 666     case Bytecodes::_return:
 667     case Bytecodes::_ireturn:
 668     case Bytecodes::_lreturn:
 669     case Bytecodes::_freturn:
 670     case Bytecodes::_dreturn:
 671     case Bytecodes::_areturn:
 672       break;
 673 
 674     default:
 675       return false;
 676     }
 677   }
 678 
 679   return true;
 680 }
 681 
 682 void SharkInlinerHelper::initialize_for_check() {
 683   _locals = NEW_RESOURCE_ARRAY(bool, max_locals());
 684   _stack = NEW_RESOURCE_ARRAY(bool, max_stack());
 685 
 686   memset(_locals, 0, max_locals() * sizeof(bool));
 687   for (int i = 0; i < target()->arg_size(); i++) {
 688     SharkValue *arg = entry_state()->stack(target()->arg_size() - 1 - i);
 689     if (arg && arg->zero_checked())
 690       set_local(i, true);
 691   }
 692 
 693   _sp = _stack;
 694 }
 695 
 696 bool SharkInlinerHelper::do_field_access(bool is_get, bool is_field) {
 697   assert(is_get || is_field, "can't inline putstatic");
 698 
 699   // If the holder isn't linked then there isn't a lot we can do
 700   if (!target()->holder()->is_linked())
 701     return false;
 702 
 703   // Get the field
 704   bool will_link;
 705   ciField *field = iter()->get_field(will_link);
 706   if (!will_link)
 707     return false;
 708 
 709   // If the field is mismatched then an exception needs throwing
 710   if (is_field == field->is_static())
 711     return false;
 712 
 713   // Pop the value off the stack if necessary
 714   if (!is_get) {
 715     pop();
 716     if (field->type()->is_two_word())
 717       pop();
 718   }
 719 
 720   // Pop and null-check the receiver if necessary
 721   if (is_field) {
 722     if (!pop())
 723       return false;
 724   }
 725 
 726   // Push the result if necessary
 727   if (is_get) {
 728     bool result_pushed = false;
 729     if (field->is_constant() && field->is_static()) {
 730       SharkConstant *sc = SharkConstant::for_field(iter());
 731       if (sc->is_loaded()) {
 732         push(sc->is_nonzero());
 733         result_pushed = true;
 734       }
 735     }
 736 
 737     if (!result_pushed)
 738       push(false);
 739 
 740     if (field->type()->is_two_word())
 741       push(false);
 742   }
 743 
 744   return true;
 745 }
 746 
 747 bool SharkInliner::attempt_inline(ciMethod *target, SharkState *state) {
 748   if (!Inline) {
 749     return false;
 750   }
 751 
 752   if (SharkIntrinsics::is_intrinsic(target)) {
 753     SharkIntrinsics::inline_intrinsic(target, state);
 754     return true;
 755   }
 756 
 757   if (may_be_inlinable(target)) {
 758     SharkInlinerHelper inliner(target, state);
 759     if (inliner.is_inlinable()) {
 760       inliner.do_inline();
 761       return true;
 762     }
 763   }
 764   return false;
 765 }