src/share/vm/opto/mathexactnode.cpp

Fri, 27 Sep 2013 08:39:19 +0200

author
rbackman
date
Fri, 27 Sep 2013 08:39:19 +0200
changeset 5791
c9ccd7b85f20
child 5927
4a2acfb16e97
permissions
-rw-r--r--

8024924: Intrinsify java.lang.Math.addExact
Reviewed-by: kvn, twisti

     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     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 it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    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  */
    25 #include "precompiled.hpp"
    26 #include "memory/allocation.inline.hpp"
    27 #include "opto/addnode.hpp"
    28 #include "opto/machnode.hpp"
    29 #include "opto/mathexactnode.hpp"
    30 #include "opto/matcher.hpp"
    31 #include "opto/subnode.hpp"
    33 MathExactNode::MathExactNode(Node* ctrl, Node* n1, Node* n2) : MultiNode(3) {
    34   init_req(0, ctrl);
    35   init_req(1, n1);
    36   init_req(2, n2);
    37 }
    39 Node* AddExactINode::match(const ProjNode* proj, const Matcher* m) {
    40   uint ideal_reg = proj->ideal_reg();
    41   RegMask rm;
    42   if (proj->_con == result_proj_node) {
    43     rm = m->mathExactI_result_proj_mask();
    44   } else {
    45     assert(proj->_con == flags_proj_node, "must be result or flags");
    46     assert(ideal_reg == Op_RegFlags, "sanity");
    47     rm = m->mathExactI_flags_proj_mask();
    48   }
    49   return new (m->C) MachProjNode(this, proj->_con, rm, ideal_reg);
    50 }
    52 // If the MathExactNode won't overflow we have to replace the
    53 // FlagsProjNode and ProjNode that is generated by the MathExactNode
    54 Node* MathExactNode::no_overflow(PhaseGVN *phase, Node* new_result) {
    55   PhaseIterGVN *igvn = phase->is_IterGVN();
    56   if (igvn) {
    57     ProjNode* result = result_node();
    58     ProjNode* flags = flags_node();
    60     if (result != NULL) {
    61       igvn->replace_node(result, new_result);
    62     }
    64     if (flags != NULL) {
    65       BoolNode* bolnode = (BoolNode *) flags->unique_out();
    66       switch (bolnode->_test._test) {
    67         case BoolTest::overflow:
    68           // if the check is for overflow - never taken
    69           igvn->replace_node(bolnode, phase->intcon(0));
    70           break;
    71         case BoolTest::no_overflow:
    72           // if the check is for no overflow - always taken
    73           igvn->replace_node(bolnode, phase->intcon(1));
    74           break;
    75         default:
    76           fatal("Unexpected value of BoolTest");
    77           break;
    78       }
    79       flags->del_req(0);
    80     }
    81   }
    82   return new_result;
    83 }
    85 Node *AddExactINode::Ideal(PhaseGVN *phase, bool can_reshape) {
    86   Node *arg1 = in(1);
    87   Node *arg2 = in(2);
    89   const Type* type1 = phase->type(arg1);
    90   const Type* type2 = phase->type(arg2);
    92   if (type1 != Type::TOP && type1->singleton() &&
    93       type2 != Type::TOP && type2->singleton()) {
    94     jint val1 = arg1->get_int();
    95     jint val2 = arg2->get_int();
    96     jint result = val1 + val2;
    97     // Hacker's Delight 2-12 Overflow if both arguments have the opposite sign of the result
    98     if ( (((val1 ^ result) & (val2 ^ result)) >= 0)) {
    99       Node* con_result = ConINode::make(phase->C, result);
   100       return no_overflow(phase, con_result);
   101     }
   102     return NULL;
   103   }
   105   if (type1 == TypeInt::ZERO) { // (Add 0 x) == x
   106     Node* add_result = new (phase->C) AddINode(arg1, arg2);
   107     return no_overflow(phase, add_result);
   108   }
   110   if (type2 == TypeInt::ZERO) { // (Add x 0) == x
   111     Node* add_result = new (phase->C) AddINode(arg1, arg2);
   112     return no_overflow(phase, add_result);
   113   }
   115   if (type2->singleton()) {
   116     return NULL; // no change - keep constant on the right
   117   }
   119   if (type1->singleton()) {
   120     // Make it x + Constant - move constant to the right
   121     swap_edges(1, 2);
   122     return this;
   123   }
   125   if (arg2->is_Load()) {
   126     return NULL; // no change - keep load on the right
   127   }
   129   if (arg1->is_Load()) {
   130     // Make it x + Load - move load to the right
   131     swap_edges(1, 2);
   132     return this;
   133   }
   135   if (arg1->_idx > arg2->_idx) {
   136     // Sort the edges
   137     swap_edges(1, 2);
   138     return this;
   139   }
   141   return NULL;
   142 }

mercurial