Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/shark/sharkInliner.cpp
32285 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* Copyright 2009 Red Hat, Inc.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "ci/ciField.hpp"27#include "ci/ciMethod.hpp"28#include "ci/ciStreams.hpp"29#include "interpreter/bytecodes.hpp"30#include "memory/allocation.hpp"31#include "shark/sharkBlock.hpp"32#include "shark/sharkConstant.hpp"33#include "shark/sharkInliner.hpp"34#include "shark/sharkIntrinsics.hpp"35#include "shark/sharkState.hpp"36#include "shark/sharkValue.hpp"37#include "shark/shark_globals.hpp"3839using namespace llvm;4041class SharkInlineBlock : public SharkBlock {42public:43SharkInlineBlock(ciMethod* target, SharkState* state)44: SharkBlock(state, target),45_outer_state(state),46_entry_state(new SharkState(this)) {47for (int i = target->max_locals() - 1; i >= 0; i--) {48SharkValue *value = NULL;49if (i < target->arg_size())50value = outer_state()->pop();51entry_state()->set_local(i, value);52}53}5455private:56SharkState* _outer_state;57SharkState* _entry_state;5859private:60SharkState* outer_state() {61return _outer_state;62}63SharkState* entry_state() {64return _entry_state;65}6667public:68void emit_IR() {69parse_bytecode(0, target()->code_size());70}7172private:73void do_return(BasicType type) {74if (type != T_VOID) {75SharkValue *result = pop_result(type);76outer_state()->push(result);77if (result->is_two_word())78outer_state()->push(NULL);79}80}81};8283class SharkInlinerHelper : public StackObj {84public:85SharkInlinerHelper(ciMethod* target, SharkState* entry_state)86: _target(target),87_entry_state(entry_state),88_iter(target) {}8990private:91ciBytecodeStream _iter;92SharkState* _entry_state;93ciMethod* _target;9495public:96ciBytecodeStream* iter() {97return &_iter;98}99SharkState* entry_state() const {100return _entry_state;101}102ciMethod* target() const {103return _target;104}105106public:107Bytecodes::Code bc() {108return iter()->cur_bc();109}110int max_locals() const {111return target()->max_locals();112}113int max_stack() const {114return target()->max_stack();115}116117// Inlinability check118public:119bool is_inlinable();120121private:122void initialize_for_check();123124bool do_getstatic() {125return do_field_access(true, false);126}127bool do_getfield() {128return do_field_access(true, true);129}130bool do_putfield() {131return do_field_access(false, true);132}133bool do_field_access(bool is_get, bool is_field);134135// Local variables for inlinability check136private:137bool* _locals;138139public:140bool* local_addr(int index) const {141assert(index >= 0 && index < max_locals(), "bad local variable index");142return &_locals[index];143}144bool local(int index) const {145return *local_addr(index);146}147void set_local(int index, bool value) {148*local_addr(index) = value;149}150151// Expression stack for inlinability check152private:153bool* _stack;154bool* _sp;155156public:157int stack_depth() const {158return _sp - _stack;159}160bool* stack_addr(int slot) const {161assert(slot >= 0 && slot < stack_depth(), "bad stack slot");162return &_sp[-(slot + 1)];163}164void push(bool value) {165assert(stack_depth() < max_stack(), "stack overrun");166*(_sp++) = value;167}168bool pop() {169assert(stack_depth() > 0, "stack underrun");170return *(--_sp);171}172173// Methods for two-word locals174public:175void push_pair_local(int index) {176push(local(index));177push(local(index + 1));178}179void pop_pair_local(int index) {180set_local(index + 1, pop());181set_local(index, pop());182}183184// Code generation185public:186void do_inline() {187(new SharkInlineBlock(target(), entry_state()))->emit_IR();188}189};190191// Quick checks so we can bail out before doing too much192bool SharkInliner::may_be_inlinable(ciMethod *target) {193// We can't inline native methods194if (target->is_native())195return false;196197// Not much point inlining abstract ones, and in any198// case we'd need a stack frame to throw the exception199if (target->is_abstract())200return false;201202// Don't inline anything huge203if (target->code_size() > SharkMaxInlineSize)204return false;205206// Monitors aren't allowed without a frame to put them in207if (target->is_synchronized() || target->has_monitor_bytecodes())208return false;209210// We don't do control flow211if (target->has_exception_handlers() || target->has_jsrs())212return false;213214// Don't try to inline constructors, as they must215// eventually call Object.<init> which we can't inline.216// Note that this catches <clinit> too, but why would217// we be compiling that?218if (target->is_initializer())219return false;220221// Mustn't inline Object.<init>222// Should be caught by the above, but just in case...223if (target->intrinsic_id() == vmIntrinsics::_Object_init)224return false;225226return true;227}228229// Full-on detailed check, for methods that pass the quick checks230// Inlined methods have no stack frame, so we can't do anything231// that would require one. This means no safepoints (and hence232// no loops) and no VM calls. No VM calls means, amongst other233// things, that no exceptions can be created, which means no null234// checks or divide-by-zero checks are allowed. The lack of null235// checks in particular would eliminate practically everything,236// but we can get around that restriction by relying on the zero-237// check eliminator to strip the checks. To do that, we need to238// walk through the method, tracking which values are and are not239// zero-checked.240bool SharkInlinerHelper::is_inlinable() {241ResourceMark rm;242initialize_for_check();243244SharkConstant *sc;245bool a, b, c, d;246247iter()->reset_to_bci(0);248while (iter()->next() != ciBytecodeStream::EOBC()) {249switch (bc()) {250case Bytecodes::_nop:251break;252253case Bytecodes::_aconst_null:254push(false);255break;256257case Bytecodes::_iconst_0:258push(false);259break;260case Bytecodes::_iconst_m1:261case Bytecodes::_iconst_1:262case Bytecodes::_iconst_2:263case Bytecodes::_iconst_3:264case Bytecodes::_iconst_4:265case Bytecodes::_iconst_5:266push(true);267break;268269case Bytecodes::_lconst_0:270push(false);271push(false);272break;273case Bytecodes::_lconst_1:274push(true);275push(false);276break;277278case Bytecodes::_fconst_0:279case Bytecodes::_fconst_1:280case Bytecodes::_fconst_2:281push(false);282break;283284case Bytecodes::_dconst_0:285case Bytecodes::_dconst_1:286push(false);287push(false);288break;289290case Bytecodes::_bipush:291push(iter()->get_constant_u1() != 0);292break;293case Bytecodes::_sipush:294push(iter()->get_constant_u2() != 0);295break;296297case Bytecodes::_ldc:298case Bytecodes::_ldc_w:299case Bytecodes::_ldc2_w:300sc = SharkConstant::for_ldc(iter());301if (!sc->is_loaded())302return false;303push(sc->is_nonzero());304if (sc->is_two_word())305push(false);306break;307308case Bytecodes::_iload_0:309case Bytecodes::_fload_0:310case Bytecodes::_aload_0:311push(local(0));312break;313case Bytecodes::_lload_0:314case Bytecodes::_dload_0:315push_pair_local(0);316break;317318case Bytecodes::_iload_1:319case Bytecodes::_fload_1:320case Bytecodes::_aload_1:321push(local(1));322break;323case Bytecodes::_lload_1:324case Bytecodes::_dload_1:325push_pair_local(1);326break;327328case Bytecodes::_iload_2:329case Bytecodes::_fload_2:330case Bytecodes::_aload_2:331push(local(2));332break;333case Bytecodes::_lload_2:334case Bytecodes::_dload_2:335push_pair_local(2);336break;337338case Bytecodes::_iload_3:339case Bytecodes::_fload_3:340case Bytecodes::_aload_3:341push(local(3));342break;343case Bytecodes::_lload_3:344case Bytecodes::_dload_3:345push_pair_local(3);346break;347348case Bytecodes::_iload:349case Bytecodes::_fload:350case Bytecodes::_aload:351push(local(iter()->get_index()));352break;353case Bytecodes::_lload:354case Bytecodes::_dload:355push_pair_local(iter()->get_index());356break;357358case Bytecodes::_istore_0:359case Bytecodes::_fstore_0:360case Bytecodes::_astore_0:361set_local(0, pop());362break;363case Bytecodes::_lstore_0:364case Bytecodes::_dstore_0:365pop_pair_local(0);366break;367368case Bytecodes::_istore_1:369case Bytecodes::_fstore_1:370case Bytecodes::_astore_1:371set_local(1, pop());372break;373case Bytecodes::_lstore_1:374case Bytecodes::_dstore_1:375pop_pair_local(1);376break;377378case Bytecodes::_istore_2:379case Bytecodes::_fstore_2:380case Bytecodes::_astore_2:381set_local(2, pop());382break;383case Bytecodes::_lstore_2:384case Bytecodes::_dstore_2:385pop_pair_local(2);386break;387388case Bytecodes::_istore_3:389case Bytecodes::_fstore_3:390case Bytecodes::_astore_3:391set_local(3, pop());392break;393case Bytecodes::_lstore_3:394case Bytecodes::_dstore_3:395pop_pair_local(3);396break;397398case Bytecodes::_istore:399case Bytecodes::_fstore:400case Bytecodes::_astore:401set_local(iter()->get_index(), pop());402break;403case Bytecodes::_lstore:404case Bytecodes::_dstore:405pop_pair_local(iter()->get_index());406break;407408case Bytecodes::_pop:409pop();410break;411case Bytecodes::_pop2:412pop();413pop();414break;415case Bytecodes::_swap:416a = pop();417b = pop();418push(a);419push(b);420break;421case Bytecodes::_dup:422a = pop();423push(a);424push(a);425break;426case Bytecodes::_dup_x1:427a = pop();428b = pop();429push(a);430push(b);431push(a);432break;433case Bytecodes::_dup_x2:434a = pop();435b = pop();436c = pop();437push(a);438push(c);439push(b);440push(a);441break;442case Bytecodes::_dup2:443a = pop();444b = pop();445push(b);446push(a);447push(b);448push(a);449break;450case Bytecodes::_dup2_x1:451a = pop();452b = pop();453c = pop();454push(b);455push(a);456push(c);457push(b);458push(a);459break;460case Bytecodes::_dup2_x2:461a = pop();462b = pop();463c = pop();464d = pop();465push(b);466push(a);467push(d);468push(c);469push(b);470push(a);471break;472473case Bytecodes::_getfield:474if (!do_getfield())475return false;476break;477case Bytecodes::_getstatic:478if (!do_getstatic())479return false;480break;481case Bytecodes::_putfield:482if (!do_putfield())483return false;484break;485486case Bytecodes::_iadd:487case Bytecodes::_isub:488case Bytecodes::_imul:489case Bytecodes::_iand:490case Bytecodes::_ixor:491case Bytecodes::_ishl:492case Bytecodes::_ishr:493case Bytecodes::_iushr:494pop();495pop();496push(false);497break;498case Bytecodes::_ior:499a = pop();500b = pop();501push(a && b);502break;503case Bytecodes::_idiv:504case Bytecodes::_irem:505if (!pop())506return false;507pop();508push(false);509break;510case Bytecodes::_ineg:511break;512513case Bytecodes::_ladd:514case Bytecodes::_lsub:515case Bytecodes::_lmul:516case Bytecodes::_land:517case Bytecodes::_lxor:518pop();519pop();520pop();521pop();522push(false);523push(false);524break;525case Bytecodes::_lor:526a = pop();527b = pop();528push(a && b);529break;530case Bytecodes::_ldiv:531case Bytecodes::_lrem:532pop();533if (!pop())534return false;535pop();536pop();537push(false);538push(false);539break;540case Bytecodes::_lneg:541break;542case Bytecodes::_lshl:543case Bytecodes::_lshr:544case Bytecodes::_lushr:545pop();546pop();547pop();548push(false);549push(false);550break;551552case Bytecodes::_fadd:553case Bytecodes::_fsub:554case Bytecodes::_fmul:555case Bytecodes::_fdiv:556case Bytecodes::_frem:557pop();558pop();559push(false);560break;561case Bytecodes::_fneg:562break;563564case Bytecodes::_dadd:565case Bytecodes::_dsub:566case Bytecodes::_dmul:567case Bytecodes::_ddiv:568case Bytecodes::_drem:569pop();570pop();571pop();572pop();573push(false);574push(false);575break;576case Bytecodes::_dneg:577break;578579case Bytecodes::_iinc:580set_local(iter()->get_index(), false);581break;582583case Bytecodes::_lcmp:584pop();585pop();586pop();587pop();588push(false);589break;590591case Bytecodes::_fcmpl:592case Bytecodes::_fcmpg:593pop();594pop();595push(false);596break;597598case Bytecodes::_dcmpl:599case Bytecodes::_dcmpg:600pop();601pop();602pop();603pop();604push(false);605break;606607case Bytecodes::_i2l:608push(false);609break;610case Bytecodes::_i2f:611pop();612push(false);613break;614case Bytecodes::_i2d:615pop();616push(false);617push(false);618break;619620case Bytecodes::_l2i:621case Bytecodes::_l2f:622pop();623pop();624push(false);625break;626case Bytecodes::_l2d:627pop();628pop();629push(false);630push(false);631break;632633case Bytecodes::_f2i:634pop();635push(false);636break;637case Bytecodes::_f2l:638case Bytecodes::_f2d:639pop();640push(false);641push(false);642break;643644case Bytecodes::_d2i:645case Bytecodes::_d2f:646pop();647pop();648push(false);649break;650case Bytecodes::_d2l:651pop();652pop();653push(false);654push(false);655break;656657case Bytecodes::_i2b:658case Bytecodes::_i2c:659case Bytecodes::_i2s:660pop();661push(false);662break;663664case Bytecodes::_return:665case Bytecodes::_ireturn:666case Bytecodes::_lreturn:667case Bytecodes::_freturn:668case Bytecodes::_dreturn:669case Bytecodes::_areturn:670break;671672default:673return false;674}675}676677return true;678}679680void SharkInlinerHelper::initialize_for_check() {681_locals = NEW_RESOURCE_ARRAY(bool, max_locals());682_stack = NEW_RESOURCE_ARRAY(bool, max_stack());683684memset(_locals, 0, max_locals() * sizeof(bool));685for (int i = 0; i < target()->arg_size(); i++) {686SharkValue *arg = entry_state()->stack(target()->arg_size() - 1 - i);687if (arg && arg->zero_checked())688set_local(i, true);689}690691_sp = _stack;692}693694bool SharkInlinerHelper::do_field_access(bool is_get, bool is_field) {695assert(is_get || is_field, "can't inline putstatic");696697// If the holder isn't linked then there isn't a lot we can do698if (!target()->holder()->is_linked())699return false;700701// Get the field702bool will_link;703ciField *field = iter()->get_field(will_link);704if (!will_link)705return false;706707// If the field is mismatched then an exception needs throwing708if (is_field == field->is_static())709return false;710711// Pop the value off the stack if necessary712if (!is_get) {713pop();714if (field->type()->is_two_word())715pop();716}717718// Pop and null-check the receiver if necessary719if (is_field) {720if (!pop())721return false;722}723724// Push the result if necessary725if (is_get) {726bool result_pushed = false;727if (field->is_constant() && field->is_static()) {728SharkConstant *sc = SharkConstant::for_field(iter());729if (sc->is_loaded()) {730push(sc->is_nonzero());731result_pushed = true;732}733}734735if (!result_pushed)736push(false);737738if (field->type()->is_two_word())739push(false);740}741742return true;743}744745bool SharkInliner::attempt_inline(ciMethod *target, SharkState *state) {746if (SharkIntrinsics::is_intrinsic(target)) {747SharkIntrinsics::inline_intrinsic(target, state);748return true;749}750751if (may_be_inlinable(target)) {752SharkInlinerHelper inliner(target, state);753if (inliner.is_inlinable()) {754inliner.do_inline();755return true;756}757}758return false;759}760761762