Path: blob/master/src/hotspot/share/opto/castnode.cpp
64441 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 (_dependency != RegularDependency) {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)._dependency == _dependency;85}8687uint ConstraintCastNode::size_of() const {88return sizeof(*this);89}9091Node* ConstraintCastNode::make_cast(int opcode, Node* c, Node *n, const Type *t, DependencyType dependency) {92switch(opcode) {93case Op_CastII: {94Node* cast = new CastIINode(n, t, dependency);95cast->set_req(0, c);96return cast;97}98case Op_CastLL: {99Node* cast = new CastLLNode(n, t, dependency);100cast->set_req(0, c);101return cast;102}103case Op_CastPP: {104Node* cast = new CastPPNode(n, t, dependency);105cast->set_req(0, c);106return cast;107}108case Op_CastFF: {109Node* cast = new CastFFNode(n, t, dependency);110cast->set_req(0, c);111return cast;112}113case Op_CastDD: {114Node* cast = new CastDDNode(n, t, dependency);115cast->set_req(0, c);116return cast;117}118case Op_CastVV: {119Node* cast = new CastVVNode(n, t, dependency);120cast->set_req(0, c);121return cast;122}123case Op_CheckCastPP: return new CheckCastPPNode(c, n, t, dependency);124default:125fatal("Bad opcode %d", opcode);126}127return NULL;128}129130Node* ConstraintCastNode::make(Node* c, Node *n, const Type *t, BasicType bt) {131switch(bt) {132case T_INT: {133return make_cast(Op_CastII, c, n, t, RegularDependency);134}135case T_LONG: {136return make_cast(Op_CastLL, c, n, t, RegularDependency);137}138default:139fatal("Bad basic type %s", type2name(bt));140}141return NULL;142}143144TypeNode* ConstraintCastNode::dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const {145if (_dependency == UnconditionalDependency) {146return NULL;147}148Node* val = in(1);149Node* ctl = in(0);150int opc = Opcode();151if (ctl == NULL) {152return NULL;153}154// Range check CastIIs may all end up under a single range check and155// in that case only the narrower CastII would be kept by the code156// below which would be incorrect.157if (is_CastII() && as_CastII()->has_range_check()) {158return NULL;159}160if (type()->isa_rawptr() && (gvn->type_or_null(val) == NULL || gvn->type(val)->isa_oopptr())) {161return NULL;162}163for (DUIterator_Fast imax, i = val->fast_outs(imax); i < imax; i++) {164Node* u = val->fast_out(i);165if (u != this &&166u->outcnt() > 0 &&167u->Opcode() == opc &&168u->in(0) != NULL &&169u->bottom_type()->higher_equal(type())) {170if (pt->is_dominator(u->in(0), ctl)) {171return u->as_Type();172}173if (is_CheckCastPP() && u->in(1)->is_Proj() && u->in(1)->in(0)->is_Allocate() &&174u->in(0)->is_Proj() && u->in(0)->in(0)->is_Initialize() &&175u->in(1)->in(0)->as_Allocate()->initialization() == u->in(0)->in(0)) {176// CheckCastPP following an allocation always dominates all177// use of the allocation result178return u->as_Type();179}180}181}182return NULL;183}184185#ifndef PRODUCT186void ConstraintCastNode::dump_spec(outputStream *st) const {187TypeNode::dump_spec(st);188if (_dependency != RegularDependency) {189st->print(" %s dependency", _dependency == StrongDependency ? "strong" : "unconditional");190}191}192#endif193194const Type* CastIINode::Value(PhaseGVN* phase) const {195const Type *res = ConstraintCastNode::Value(phase);196197// Try to improve the type of the CastII if we recognize a CmpI/If198// pattern.199if (_dependency != RegularDependency) {200if (in(0) != NULL && in(0)->in(0) != NULL && in(0)->in(0)->is_If()) {201assert(in(0)->is_IfFalse() || in(0)->is_IfTrue(), "should be If proj");202Node* proj = in(0);203if (proj->in(0)->in(1)->is_Bool()) {204Node* b = proj->in(0)->in(1);205if (b->in(1)->Opcode() == Op_CmpI) {206Node* cmp = b->in(1);207if (cmp->in(1) == in(1) && phase->type(cmp->in(2))->isa_int()) {208const TypeInt* in2_t = phase->type(cmp->in(2))->is_int();209const Type* t = TypeInt::INT;210BoolTest test = b->as_Bool()->_test;211if (proj->is_IfFalse()) {212test = test.negate();213}214BoolTest::mask m = test._test;215jlong lo_long = min_jint;216jlong hi_long = max_jint;217if (m == BoolTest::le || m == BoolTest::lt) {218hi_long = in2_t->_hi;219if (m == BoolTest::lt) {220hi_long -= 1;221}222} else if (m == BoolTest::ge || m == BoolTest::gt) {223lo_long = in2_t->_lo;224if (m == BoolTest::gt) {225lo_long += 1;226}227} else if (m == BoolTest::eq) {228lo_long = in2_t->_lo;229hi_long = in2_t->_hi;230} else if (m == BoolTest::ne) {231// can't do any better232} else {233stringStream ss;234test.dump_on(&ss);235fatal("unexpected comparison %s", ss.as_string());236}237int lo_int = (int)lo_long;238int hi_int = (int)hi_long;239240if (lo_long != (jlong)lo_int) {241lo_int = min_jint;242}243if (hi_long != (jlong)hi_int) {244hi_int = max_jint;245}246247t = TypeInt::make(lo_int, hi_int, Type::WidenMax);248249res = res->filter_speculative(t);250251return res;252}253}254}255}256}257return res;258}259260static Node* find_or_make_CastII(PhaseIterGVN* igvn, Node* parent, Node* control, const TypeInt* type, ConstraintCastNode::DependencyType dependency) {261Node* n = new CastIINode(parent, type, dependency);262n->set_req(0, control);263Node* existing = igvn->hash_find_insert(n);264if (existing != NULL) {265n->destruct(igvn);266return existing;267}268return igvn->register_new_node_with_optimizer(n);269}270271Node *CastIINode::Ideal(PhaseGVN *phase, bool can_reshape) {272Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);273if (progress != NULL) {274return progress;275}276277PhaseIterGVN *igvn = phase->is_IterGVN();278const TypeInt* this_type = this->type()->is_int();279Node* z = in(1);280const TypeInteger* rx = NULL;281const TypeInteger* ry = NULL;282// Similar to ConvI2LNode::Ideal() for the same reasons283if (!_range_check_dependency && Compile::push_thru_add(phase, z, this_type, rx, ry, T_INT)) {284if (igvn == NULL) {285// Postpone this optimization to iterative GVN, where we can handle deep286// AddI chains without an exponential number of recursive Ideal() calls.287phase->record_for_igvn(this);288return NULL;289}290int op = z->Opcode();291Node* x = z->in(1);292Node* y = z->in(2);293294Node* cx = find_or_make_CastII(igvn, x, in(0), rx->is_int(), _dependency);295Node* cy = find_or_make_CastII(igvn, y, in(0), ry->is_int(), _dependency);296switch (op) {297case Op_AddI: return new AddINode(cx, cy);298case Op_SubI: return new SubINode(cx, cy);299default: ShouldNotReachHere();300}301}302303// Similar to ConvI2LNode::Ideal() for the same reasons304// Do not narrow the type of range check dependent CastIINodes to305// avoid corruption of the graph if a CastII is replaced by TOP but306// the corresponding range check is not removed.307if (can_reshape && !_range_check_dependency) {308if (phase->C->post_loop_opts_phase()) {309const TypeInt* this_type = this->type()->is_int();310const TypeInt* in_type = phase->type(in(1))->isa_int();311if (in_type != NULL && this_type != NULL &&312(in_type->_lo != this_type->_lo ||313in_type->_hi != this_type->_hi)) {314jint lo1 = this_type->_lo;315jint hi1 = this_type->_hi;316int w1 = this_type->_widen;317318if (lo1 >= 0) {319// Keep a range assertion of >=0.320lo1 = 0; hi1 = max_jint;321} else if (hi1 < 0) {322// Keep a range assertion of <0.323lo1 = min_jint; hi1 = -1;324} else {325lo1 = min_jint; hi1 = max_jint;326}327const TypeInt* wtype = TypeInt::make(MAX2(in_type->_lo, lo1),328MIN2(in_type->_hi, hi1),329MAX2((int)in_type->_widen, w1));330if (wtype != type()) {331set_type(wtype);332return this;333}334}335} else {336phase->C->record_for_post_loop_opts_igvn(this);337}338}339return NULL;340}341342Node* CastIINode::Identity(PhaseGVN* phase) {343Node* progress = ConstraintCastNode::Identity(phase);344if (progress != this) {345return progress;346}347if (_range_check_dependency) {348if (phase->C->post_loop_opts_phase()) {349return this->in(1);350} else {351phase->C->record_for_post_loop_opts_igvn(this);352}353}354return this;355}356357bool CastIINode::cmp(const Node &n) const {358return ConstraintCastNode::cmp(n) && ((CastIINode&)n)._range_check_dependency == _range_check_dependency;359}360361uint CastIINode::size_of() const {362return sizeof(*this);363}364365#ifndef PRODUCT366void CastIINode::dump_spec(outputStream* st) const {367ConstraintCastNode::dump_spec(st);368if (_range_check_dependency) {369st->print(" range check dependency");370}371}372#endif373374//=============================================================================375//------------------------------Identity---------------------------------------376// If input is already higher or equal to cast type, then this is an identity.377Node* CheckCastPPNode::Identity(PhaseGVN* phase) {378Node* dom = dominating_cast(phase, phase);379if (dom != NULL) {380return dom;381}382if (_dependency != RegularDependency) {383return this;384}385const Type* t = phase->type(in(1));386if (EnableVectorReboxing && in(1)->Opcode() == Op_VectorBox) {387if (t->higher_equal_speculative(phase->type(this))) {388return in(1);389}390} else if (t == phase->type(this)) {391// Toned down to rescue meeting at a Phi 3 different oops all implementing392// the same interface.393return in(1);394}395return this;396}397398//------------------------------Value------------------------------------------399// Take 'join' of input and cast-up type, unless working with an Interface400const Type* CheckCastPPNode::Value(PhaseGVN* phase) const {401if( in(0) && phase->type(in(0)) == Type::TOP ) return Type::TOP;402403const Type *inn = phase->type(in(1));404if( inn == Type::TOP ) return Type::TOP; // No information yet405406const TypePtr *in_type = inn->isa_ptr();407const TypePtr *my_type = _type->isa_ptr();408const Type *result = _type;409if( in_type != NULL && my_type != NULL ) {410TypePtr::PTR in_ptr = in_type->ptr();411if (in_ptr == TypePtr::Null) {412result = in_type;413} else if (in_ptr == TypePtr::Constant) {414if (my_type->isa_rawptr()) {415result = my_type;416} else {417const TypeOopPtr *jptr = my_type->isa_oopptr();418assert(jptr, "");419result = !in_type->higher_equal(_type)420? my_type->cast_to_ptr_type(TypePtr::NotNull)421: in_type;422}423} else {424result = my_type->cast_to_ptr_type( my_type->join_ptr(in_ptr) );425}426}427428// This is the code from TypePtr::xmeet() that prevents us from429// having 2 ways to represent the same type. We have to replicate it430// here because we don't go through meet/join.431if (result->remove_speculative() == result->speculative()) {432result = result->remove_speculative();433}434435// Same as above: because we don't go through meet/join, remove the436// speculative type if we know we won't use it.437return result->cleanup_speculative();438439// JOIN NOT DONE HERE BECAUSE OF INTERFACE ISSUES.440// FIX THIS (DO THE JOIN) WHEN UNION TYPES APPEAR!441442//443// Remove this code after overnight run indicates no performance444// loss from not performing JOIN at CheckCastPPNode445//446// const TypeInstPtr *in_oop = in->isa_instptr();447// const TypeInstPtr *my_oop = _type->isa_instptr();448// // If either input is an 'interface', return destination type449// assert (in_oop == NULL || in_oop->klass() != NULL, "");450// assert (my_oop == NULL || my_oop->klass() != NULL, "");451// if( (in_oop && in_oop->klass()->is_interface())452// ||(my_oop && my_oop->klass()->is_interface()) ) {453// TypePtr::PTR in_ptr = in->isa_ptr() ? in->is_ptr()->_ptr : TypePtr::BotPTR;454// // Preserve cast away nullness for interfaces455// if( in_ptr == TypePtr::NotNull && my_oop && my_oop->_ptr == TypePtr::BotPTR ) {456// return my_oop->cast_to_ptr_type(TypePtr::NotNull);457// }458// return _type;459// }460//461// // Neither the input nor the destination type is an interface,462//463// // history: JOIN used to cause weird corner case bugs464// // return (in == TypeOopPtr::NULL_PTR) ? in : _type;465// // JOIN picks up NotNull in common instance-of/check-cast idioms, both oops.466// // JOIN does not preserve NotNull in other cases, e.g. RawPtr vs InstPtr467// const Type *join = in->join(_type);468// // Check if join preserved NotNull'ness for pointers469// if( join->isa_ptr() && _type->isa_ptr() ) {470// TypePtr::PTR join_ptr = join->is_ptr()->_ptr;471// TypePtr::PTR type_ptr = _type->is_ptr()->_ptr;472// // If there isn't any NotNull'ness to preserve473// // OR if join preserved NotNull'ness then return it474// if( type_ptr == TypePtr::BotPTR || type_ptr == TypePtr::Null ||475// join_ptr == TypePtr::NotNull || join_ptr == TypePtr::Constant ) {476// return join;477// }478// // ELSE return same old type as before479// return _type;480// }481// // Not joining two pointers482// return join;483}484485//=============================================================================486//------------------------------Value------------------------------------------487const Type* CastX2PNode::Value(PhaseGVN* phase) const {488const Type* t = phase->type(in(1));489if (t == Type::TOP) return Type::TOP;490if (t->base() == Type_X && t->singleton()) {491uintptr_t bits = (uintptr_t) t->is_intptr_t()->get_con();492if (bits == 0) return TypePtr::NULL_PTR;493return TypeRawPtr::make((address) bits);494}495return CastX2PNode::bottom_type();496}497498//------------------------------Idealize---------------------------------------499static inline bool fits_in_int(const Type* t, bool but_not_min_int = false) {500if (t == Type::TOP) return false;501const TypeX* tl = t->is_intptr_t();502jint lo = min_jint;503jint hi = max_jint;504if (but_not_min_int) ++lo; // caller wants to negate the value w/o overflow505return (tl->_lo >= lo) && (tl->_hi <= hi);506}507508static inline Node* addP_of_X2P(PhaseGVN *phase,509Node* base,510Node* dispX,511bool negate = false) {512if (negate) {513dispX = phase->transform(new SubXNode(phase->MakeConX(0), dispX));514}515return new AddPNode(phase->C->top(),516phase->transform(new CastX2PNode(base)),517dispX);518}519520Node *CastX2PNode::Ideal(PhaseGVN *phase, bool can_reshape) {521// convert CastX2P(AddX(x, y)) to AddP(CastX2P(x), y) if y fits in an int522int op = in(1)->Opcode();523Node* x;524Node* y;525switch (op) {526case Op_SubX:527x = in(1)->in(1);528// Avoid ideal transformations ping-pong between this and AddP for raw pointers.529if (phase->find_intptr_t_con(x, -1) == 0)530break;531y = in(1)->in(2);532if (fits_in_int(phase->type(y), true)) {533return addP_of_X2P(phase, x, y, true);534}535break;536case Op_AddX:537x = in(1)->in(1);538y = in(1)->in(2);539if (fits_in_int(phase->type(y))) {540return addP_of_X2P(phase, x, y);541}542if (fits_in_int(phase->type(x))) {543return addP_of_X2P(phase, y, x);544}545break;546}547return NULL;548}549550//------------------------------Identity---------------------------------------551Node* CastX2PNode::Identity(PhaseGVN* phase) {552if (in(1)->Opcode() == Op_CastP2X) return in(1)->in(1);553return this;554}555556//=============================================================================557//------------------------------Value------------------------------------------558const Type* CastP2XNode::Value(PhaseGVN* phase) const {559const Type* t = phase->type(in(1));560if (t == Type::TOP) return Type::TOP;561if (t->base() == Type::RawPtr && t->singleton()) {562uintptr_t bits = (uintptr_t) t->is_rawptr()->get_con();563return TypeX::make(bits);564}565return CastP2XNode::bottom_type();566}567568Node *CastP2XNode::Ideal(PhaseGVN *phase, bool can_reshape) {569return (in(0) && remove_dead_region(phase, can_reshape)) ? this : NULL;570}571572//------------------------------Identity---------------------------------------573Node* CastP2XNode::Identity(PhaseGVN* phase) {574if (in(1)->Opcode() == Op_CastX2P) return in(1)->in(1);575return this;576}577578Node* ConstraintCastNode::make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency) {579Node* cast= NULL;580if (type->isa_int()) {581cast = make_cast(Op_CastII, c, in, type, dependency);582} else if (type->isa_long()) {583cast = make_cast(Op_CastLL, c, in, type, dependency);584} else if (type->isa_float()) {585cast = make_cast(Op_CastFF, c, in, type, dependency);586} else if (type->isa_double()) {587cast = make_cast(Op_CastDD, c, in, type, dependency);588} else if (type->isa_vect()) {589cast = make_cast(Op_CastVV, c, in, type, dependency);590} else if (type->isa_ptr()) {591cast = make_cast(Op_CastPP, c, in, type, dependency);592}593return cast;594}595596597