Path: blob/master/src/hotspot/share/opto/castnode.cpp
40930 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "opto/addnode.hpp"26#include "opto/callnode.hpp"27#include "opto/castnode.hpp"28#include "opto/connode.hpp"29#include "opto/matcher.hpp"30#include "opto/phaseX.hpp"31#include "opto/subnode.hpp"32#include "opto/type.hpp"3334//=============================================================================35// If input is already higher or equal to cast type, then this is an identity.36Node* ConstraintCastNode::Identity(PhaseGVN* phase) {37Node* dom = dominating_cast(phase, phase);38if (dom != NULL) {39return dom;40}41if (_carry_dependency) {42return this;43}44return phase->type(in(1))->higher_equal_speculative(_type) ? in(1) : this;45}4647//------------------------------Value------------------------------------------48// Take 'join' of input and cast-up type49const Type* ConstraintCastNode::Value(PhaseGVN* phase) const {50if (in(0) && phase->type(in(0)) == Type::TOP) return Type::TOP;51const Type* ft = phase->type(in(1))->filter_speculative(_type);5253#ifdef ASSERT54// Previous versions of this function had some special case logic,55// which is no longer necessary. Make sure of the required effects.56switch (Opcode()) {57case Op_CastII:58{59const Type* t1 = phase->type(in(1));60if( t1 == Type::TOP ) assert(ft == Type::TOP, "special case #1");61const Type* rt = t1->join_speculative(_type);62if (rt->empty()) assert(ft == Type::TOP, "special case #2");63break;64}65case Op_CastPP:66if (phase->type(in(1)) == TypePtr::NULL_PTR &&67_type->isa_ptr() && _type->is_ptr()->_ptr == TypePtr::NotNull)68assert(ft == Type::TOP, "special case #3");69break;70}71#endif //ASSERT7273return ft;74}7576//------------------------------Ideal------------------------------------------77// Return a node which is more "ideal" than the current node. Strip out78// control copies79Node *ConstraintCastNode::Ideal(PhaseGVN *phase, bool can_reshape) {80return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;81}8283bool ConstraintCastNode::cmp(const Node &n) const {84return TypeNode::cmp(n) && ((ConstraintCastNode&)n)._carry_dependency == _carry_dependency;85}8687uint ConstraintCastNode::size_of() const {88return sizeof(*this);89}9091Node* ConstraintCastNode::make_cast(int opcode, Node* c, Node *n, const Type *t, bool carry_dependency) {92switch(opcode) {93case Op_CastII: {94Node* cast = new CastIINode(n, t, carry_dependency);95cast->set_req(0, c);96return cast;97}98case Op_CastLL: {99Node* cast = new CastLLNode(n, t, carry_dependency);100cast->set_req(0, c);101return cast;102}103case Op_CastPP: {104Node* cast = new CastPPNode(n, t, carry_dependency);105cast->set_req(0, c);106return cast;107}108case Op_CheckCastPP: return new CheckCastPPNode(c, n, t, carry_dependency);109default:110fatal("Bad opcode %d", opcode);111}112return NULL;113}114115Node* ConstraintCastNode::make(Node* c, Node *n, const Type *t, BasicType bt) {116switch(bt) {117case T_INT: {118return make_cast(Op_CastII, c, n, t, false);119}120case T_LONG: {121return make_cast(Op_CastLL, c, n, t, false);122}123default:124fatal("Bad basic type %s", type2name(bt));125}126return NULL;127}128129TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {130Node* val = in(1);131Node* ctl = in(0);132int opc = Opcode();133if (ctl == NULL) {134return NULL;135}136// Range check CastIIs may all end up under a single range check and137// in that case only the narrower CastII would be kept by the code138// below which would be incorrect.139if (is_CastII() && as_CastII()->has_range_check()) {140return NULL;141}142if (type()->isa_rawptr() && (gvn->type_or_null(val) == NULL || gvn->type(val)->isa_oopptr())) {143return NULL;144}145for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {146Node* u = val->fast_out(i);147if (u != this &&148u->outcnt() > 0 &&149u->Opcode() == opc &&150u->in(0) != NULL &&151u->bottom_type()->higher_equal(type())) {152if (pt->is_dominator(u->in(0), ctl)) {153return u->as_Type();154}155if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&156u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&157u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {158// CheckCastPP following an allocation always dominates all159// use of the allocation result160return u->as_Type();161}162}163}164return NULL;165}166167#ifndef PRODUCT168void ConstraintCastNode::dump_spec(outputStream *st) const {169TypeNode::dump_spec(st);170if (_carry_dependency) {171st->print(" carry dependency");172}173}174#endif175176const Type* CastIINode::Value(PhaseGVN* phase) const {177const Type *res = ConstraintCastNode::Value(phase);178179// Try to improve the type of the CastII if we recognize a CmpI/If180// pattern.181if (_carry_dependency) {182if (in(0) != NULL && in(0)->in(0) != NULL && in(0)->in(0)->is_If()) {183assert(in(0)->is_IfFalse() || in(0)->is_IfTrue(), "should be If proj");184Node* proj = in(0);185if (proj->in(0)->in(1)->is_Bool()) {186Node* b = proj->in(0)->in(1);187if (b->in(1)->Opcode() == Op_CmpI) {188Node* cmp = b->in(1);189if (cmp->in(1) == in(1) && phase->type(cmp->in(2))->isa_int()) {190const TypeInt* in2_t = phase->type(cmp->in(2))->is_int();191const Type* t = TypeInt::INT;192BoolTest test = b->as_Bool()->_test;193if (proj->is_IfFalse()) {194test = test.negate();195}196BoolTest::mask m = test._test;197jlong lo_long = min_jint;198jlong hi_long = max_jint;199if (m == BoolTest::le || m == BoolTest::lt) {200hi_long = in2_t->_hi;201if (m == BoolTest::lt) {202hi_long -= 1;203}204} else if (m == BoolTest::ge || m == BoolTest::gt) {205lo_long = in2_t->_lo;206if (m == BoolTest::gt) {207lo_long += 1;208}209} else if (m == BoolTest::eq) {210lo_long = in2_t->_lo;211hi_long = in2_t->_hi;212} else if (m == BoolTest::ne) {213// can't do any better214} else {215stringStream ss;216test.dump_on(&ss);217fatal("unexpected comparison %s", ss.as_string());218}219int lo_int = (int)lo_long;220int hi_int = (int)hi_long;221222if (lo_long != (jlong)lo_int) {223lo_int = min_jint;224}225if (hi_long != (jlong)hi_int) {226hi_int = max_jint;227}228229t = TypeInt::make(lo_int, hi_int, Type::WidenMax);230231res = res->filter_speculative(t);232233return res;234}235}236}237}238}239return res;240}241242static Node* find_or_make_CastII(PhaseIterGVN* igvn, Node* parent, Node* control, const TypeInt* type, bool carry_dependency) {243Node* n = new CastIINode(parent, type, carry_dependency);244n->set_req(0, control);245Node* existing = igvn->hash_find_insert(n);246if (existing != NULL) {247n->destruct(igvn);248return existing;249}250return igvn->register_new_node_with_optimizer(n);251}252253Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {254Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);255if (progress != NULL) {256return progress;257}258259PhaseIterGVN *igvn = phase->is_IterGVN();260const TypeInt* this_type = this->type()->is_int();261Node* z = in(1);262const TypeInteger* rx = NULL;263const TypeInteger* ry = NULL;264// Similar to ConvI2LNode::Ideal() for the same reasons265if (!_range_check_dependency && Compile::push_thru_add(phase, z, this_type, rx, ry, T_INT)) {266if (igvn == NULL) {267// Postpone this optimization to iterative GVN, where we can handle deep268// AddI chains without an exponential number of recursive Ideal() calls.269phase->record_for_igvn(this);270return NULL;271}272int op = z->Opcode();273Node* x = z->in(1);274Node* y = z->in(2);275276Node* cx = find_or_make_CastII(igvn, x, in(0), rx->is_int(), _carry_dependency);277Node* cy = find_or_make_CastII(igvn, y, in(0), ry->is_int(), _carry_dependency);278switch (op) {279case Op_AddI: return new AddINode(cx, cy);280case Op_SubI: return new SubINode(cx, cy);281default: ShouldNotReachHere();282}283}284285// Similar to ConvI2LNode::Ideal() for the same reasons286// Do not narrow the type of range check dependent CastIINodes to287// avoid corruption of the graph if a CastII is replaced by TOP but288// the corresponding range check is not removed.289if (can_reshape && !_range_check_dependency) {290if (phase->C->post_loop_opts_phase()) {291const TypeInt* this_type = this->type()->is_int();292const TypeInt* in_type = phase->type(in(1))->isa_int();293if (in_type != NULL && this_type != NULL &&294(in_type->_lo != this_type->_lo ||295in_type->_hi != this_type->_hi)) {296jint lo1 = this_type->_lo;297jint hi1 = this_type->_hi;298int w1 = this_type->_widen;299300if (lo1 >= 0) {301// Keep a range assertion of >=0.302lo1 = 0; hi1 = max_jint;303} else if (hi1 < 0) {304// Keep a range assertion of <0.305lo1 = min_jint; hi1 = -1;306} else {307lo1 = min_jint; hi1 = max_jint;308}309const TypeInt* wtype = TypeInt::make(MAX2(in_type->_lo, lo1),310MIN2(in_type->_hi, hi1),311MAX2((int)in_type->_widen, w1));312if (wtype != type()) {313set_type(wtype);314return this;315}316}317} else {318phase->C->record_for_post_loop_opts_igvn(this);319}320}321return NULL;322}323324Node* CastIINode::Identity(PhaseGVN* phase) {325Node* progress = ConstraintCastNode::Identity(phase);326if (progress != this) {327return progress;328}329if (_range_check_dependency) {330if (phase->C->post_loop_opts_phase()) {331return this->in(1);332} else {333phase->C->record_for_post_loop_opts_igvn(this);334}335}336return this;337}338339bool CastIINode::cmp(const Node &n) const {340return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;341}342343uint CastIINode::size_of() const {344return sizeof(*this);345}346347#ifndef PRODUCT348void CastIINode::dump_spec(outputStream* st) const {349ConstraintCastNode::dump_spec(st);350if (_range_check_dependency) {351st->print(" range check dependency");352}353}354#endif355356//=============================================================================357//------------------------------Identity---------------------------------------358// If input is already higher or equal to cast type, then this is an identity.359Node* CheckCastPPNode::Identity(PhaseGVN* phase) {360Node* dom = dominating_cast(phase, phase);361if (dom != NULL) {362return dom;363}364if (_carry_dependency) {365return this;366}367const Type* t = phase->type(in(1));368if (EnableVectorReboxing && in(1)->Opcode() == Op_VectorBox) {369if (t->higher_equal_speculative(phase->type(this))) {370return in(1);371}372} else if (t == phase->type(this)) {373// Toned down to rescue meeting at a Phi 3 different oops all implementing374// the same interface.375return in(1);376}377return this;378}379380//------------------------------Value------------------------------------------381// Take 'join' of input and cast-up type, unless working with an Interface382const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {383if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;384385const Type *inn = phase->type(in(1));386if( inn == Type::TOP ) return Type::TOP; // No information yet387388const TypePtr *in_type = inn->isa_ptr();389const TypePtr *my_type = _type->isa_ptr();390const Type *result = _type;391if( in_type != NULL && my_type != NULL ) {392TypePtr::PTR in_ptr = in_type->ptr();393if (in_ptr == TypePtr::Null) {394result = in_type;395} else if (in_ptr == TypePtr::Constant) {396if (my_type->isa_rawptr()) {397result = my_type;398} else {399const TypeOopPtr *jptr = my_type->isa_oopptr();400assert(jptr, "");401result = !in_type->higher_equal(_type)402? my_type->cast_to_ptr_type(TypePtr::NotNull)403: in_type;404}405} else {406result = my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) );407}408}409410// This is the code from TypePtr::xmeet() that prevents us from411// having 2 ways to represent the same type. We have to replicate it412// here because we don't go through meet/join.413if (result->remove_speculative() == result->speculative()) {414result = result->remove_speculative();415}416417// Same as above: because we don't go through meet/join, remove the418// speculative type if we know we won't use it.419return result->cleanup_speculative();420421// JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES.422// FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR!423424//425// Remove this code after overnight run indicates no performance426// loss from not performing JOIN at CheckCastPPNode427//428// const TypeInstPtr *in_oop = in->isa_instptr();429// const TypeInstPtr *my_oop = _type->isa_instptr();430// // If either input is an 'interface', return destination type431// assert (in_oop == NULL || in_oop->klass() != NULL, "");432// assert (my_oop == NULL || my_oop->klass() != NULL, "");433// if( (in_oop && in_oop->klass()->is_interface())434// ||(my_oop && my_oop->klass()->is_interface()) ) {435// TypePtr::PTR in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR;436// // Preserve cast away nullness for interfaces437// if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) {438// return my_oop->cast_to_ptr_type(TypePtr::NotNull);439// }440// return _type;441// }442//443// // Neither the input nor the destination type is an interface,444//445// // history: JOIN used to cause weird corner case bugs446// // return (in == TypeOopPtr::NULL_PTR) ? in : _type;447// // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops.448// // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr449// const Type *join = in->join(_type);450// // Check if join preserved NotNull'ness for pointers451// if( join->isa_ptr() && _type->isa_ptr() ) {452// TypePtr::PTR join_ptr = join->is_ptr()->_ptr;453// TypePtr::PTR type_ptr = _type->is_ptr()->_ptr;454// // If there isn't any NotNull'ness to preserve455// // OR if join preserved NotNull'ness then return it456// if( type_ptr == TypePtr::BotPTR || type_ptr == TypePtr::Null ||457// join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) {458// return join;459// }460// // ELSE return same old type as before461// return _type;462// }463// // Not joining two pointers464// return join;465}466467//=============================================================================468//------------------------------Value------------------------------------------469const Type* CastX2PNode::Value(PhaseGVN* phase) const {470const Type* t = phase->type(in(1));471if (t == Type::TOP) return Type::TOP;472if (t->base() == Type_X && t->singleton()) {473uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();474if (bits == 0) return TypePtr::NULL_PTR;475return TypeRawPtr::make((address) bits);476}477return CastX2PNode::bottom_type();478}479480//------------------------------Idealize---------------------------------------481static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {482if (t == Type::TOP) return false;483const TypeX* tl = t->is_intptr_t();484jint lo = min_jint;485jint hi = max_jint;486if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow487return (tl->_lo >= lo) && (tl->_hi <= hi);488}489490static inline Node* addP_of_X2P(PhaseGVN *phase,491Node* base,492Node* dispX,493bool negate = false) {494if (negate) {495dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));496}497return new AddPNode(phase->C->top(),498phase->transform(new CastX2PNode(base)),499dispX);500}501502Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {503// convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int504int op = in(1)->Opcode();505Node* x;506Node* y;507switch (op) {508case Op_SubX:509x = in(1)->in(1);510// Avoid ideal transformations ping-pong between this and AddP for raw pointers.511if (phase->find_intptr_t_con(x, -1) == 0)512break;513y = in(1)->in(2);514if (fits_in_int(phase->type(y), true)) {515return addP_of_X2P(phase, x, y, true);516}517break;518case Op_AddX:519x = in(1)->in(1);520y = in(1)->in(2);521if (fits_in_int(phase->type(y))) {522return addP_of_X2P(phase, x, y);523}524if (fits_in_int(phase->type(x))) {525return addP_of_X2P(phase, y, x);526}527break;528}529return NULL;530}531532//------------------------------Identity---------------------------------------533Node* CastX2PNode::Identity(PhaseGVN* phase) {534if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1);535return this;536}537538//=============================================================================539//------------------------------Value------------------------------------------540const Type* CastP2XNode::Value(PhaseGVN* phase) const {541const Type* t = phase->type(in(1));542if (t == Type::TOP) return Type::TOP;543if (t->base() == Type::RawPtr && t->singleton()) {544uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();545return TypeX::make(bits);546}547return CastP2XNode::bottom_type();548}549550Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {551return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;552}553554//------------------------------Identity---------------------------------------555Node* CastP2XNode::Identity(PhaseGVN* phase) {556if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1);557return this;558}559560561