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

rbackman@5791 1 /*
rbackman@5791 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
rbackman@5791 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
rbackman@5791 4 *
rbackman@5791 5 * This code is free software; you can redistribute it and/or modify it
rbackman@5791 6 * under the terms of the GNU General Public License version 2 only, as
rbackman@5791 7 * published by the Free Software Foundation.
rbackman@5791 8 *
rbackman@5791 9 * This code is distributed in the hope that it will be useful, but WITHOUT
rbackman@5791 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
rbackman@5791 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
rbackman@5791 12 * version 2 for more details (a copy is included in the LICENSE file that
rbackman@5791 13 * accompanied this code).
rbackman@5791 14 *
rbackman@5791 15 * You should have received a copy of the GNU General Public License version
rbackman@5791 16 * 2 along with this work; if not, write to the Free Software Foundation,
rbackman@5791 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
rbackman@5791 18 *
rbackman@5791 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
rbackman@5791 20 * or visit www.oracle.com if you need additional information or have any
rbackman@5791 21 * questions.
rbackman@5791 22 *
rbackman@5791 23 */
rbackman@5791 24
rbackman@5791 25 #include "precompiled.hpp"
rbackman@5791 26 #include "memory/allocation.inline.hpp"
rbackman@5791 27 #include "opto/addnode.hpp"
rbackman@5791 28 #include "opto/machnode.hpp"
rbackman@5791 29 #include "opto/mathexactnode.hpp"
rbackman@5791 30 #include "opto/matcher.hpp"
rbackman@5791 31 #include "opto/subnode.hpp"
rbackman@5791 32
rbackman@5791 33 MathExactNode::MathExactNode(Node* ctrl, Node* n1, Node* n2) : MultiNode(3) {
rbackman@5791 34 init_req(0, ctrl);
rbackman@5791 35 init_req(1, n1);
rbackman@5791 36 init_req(2, n2);
rbackman@5791 37 }
rbackman@5791 38
rbackman@5791 39 Node* AddExactINode::match(const ProjNode* proj, const Matcher* m) {
rbackman@5791 40 uint ideal_reg = proj->ideal_reg();
rbackman@5791 41 RegMask rm;
rbackman@5791 42 if (proj->_con == result_proj_node) {
rbackman@5791 43 rm = m->mathExactI_result_proj_mask();
rbackman@5791 44 } else {
rbackman@5791 45 assert(proj->_con == flags_proj_node, "must be result or flags");
rbackman@5791 46 assert(ideal_reg == Op_RegFlags, "sanity");
rbackman@5791 47 rm = m->mathExactI_flags_proj_mask();
rbackman@5791 48 }
rbackman@5791 49 return new (m->C) MachProjNode(this, proj->_con, rm, ideal_reg);
rbackman@5791 50 }
rbackman@5791 51
rbackman@5791 52 // If the MathExactNode won't overflow we have to replace the
rbackman@5791 53 // FlagsProjNode and ProjNode that is generated by the MathExactNode
rbackman@5791 54 Node* MathExactNode::no_overflow(PhaseGVN *phase, Node* new_result) {
rbackman@5791 55 PhaseIterGVN *igvn = phase->is_IterGVN();
rbackman@5791 56 if (igvn) {
rbackman@5791 57 ProjNode* result = result_node();
rbackman@5791 58 ProjNode* flags = flags_node();
rbackman@5791 59
rbackman@5791 60 if (result != NULL) {
rbackman@5791 61 igvn->replace_node(result, new_result);
rbackman@5791 62 }
rbackman@5791 63
rbackman@5791 64 if (flags != NULL) {
rbackman@5791 65 BoolNode* bolnode = (BoolNode *) flags->unique_out();
rbackman@5791 66 switch (bolnode->_test._test) {
rbackman@5791 67 case BoolTest::overflow:
rbackman@5791 68 // if the check is for overflow - never taken
rbackman@5791 69 igvn->replace_node(bolnode, phase->intcon(0));
rbackman@5791 70 break;
rbackman@5791 71 case BoolTest::no_overflow:
rbackman@5791 72 // if the check is for no overflow - always taken
rbackman@5791 73 igvn->replace_node(bolnode, phase->intcon(1));
rbackman@5791 74 break;
rbackman@5791 75 default:
rbackman@5791 76 fatal("Unexpected value of BoolTest");
rbackman@5791 77 break;
rbackman@5791 78 }
rbackman@5791 79 flags->del_req(0);
rbackman@5791 80 }
rbackman@5791 81 }
rbackman@5791 82 return new_result;
rbackman@5791 83 }
rbackman@5791 84
rbackman@5791 85 Node *AddExactINode::Ideal(PhaseGVN *phase, bool can_reshape) {
rbackman@5791 86 Node *arg1 = in(1);
rbackman@5791 87 Node *arg2 = in(2);
rbackman@5791 88
rbackman@5791 89 const Type* type1 = phase->type(arg1);
rbackman@5791 90 const Type* type2 = phase->type(arg2);
rbackman@5791 91
rbackman@5791 92 if (type1 != Type::TOP && type1->singleton() &&
rbackman@5791 93 type2 != Type::TOP && type2->singleton()) {
rbackman@5791 94 jint val1 = arg1->get_int();
rbackman@5791 95 jint val2 = arg2->get_int();
rbackman@5791 96 jint result = val1 + val2;
rbackman@5791 97 // Hacker's Delight 2-12 Overflow if both arguments have the opposite sign of the result
rbackman@5791 98 if ( (((val1 ^ result) & (val2 ^ result)) >= 0)) {
rbackman@5791 99 Node* con_result = ConINode::make(phase->C, result);
rbackman@5791 100 return no_overflow(phase, con_result);
rbackman@5791 101 }
rbackman@5791 102 return NULL;
rbackman@5791 103 }
rbackman@5791 104
rbackman@5791 105 if (type1 == TypeInt::ZERO) { // (Add 0 x) == x
rbackman@5791 106 Node* add_result = new (phase->C) AddINode(arg1, arg2);
rbackman@5791 107 return no_overflow(phase, add_result);
rbackman@5791 108 }
rbackman@5791 109
rbackman@5791 110 if (type2 == TypeInt::ZERO) { // (Add x 0) == x
rbackman@5791 111 Node* add_result = new (phase->C) AddINode(arg1, arg2);
rbackman@5791 112 return no_overflow(phase, add_result);
rbackman@5791 113 }
rbackman@5791 114
rbackman@5791 115 if (type2->singleton()) {
rbackman@5791 116 return NULL; // no change - keep constant on the right
rbackman@5791 117 }
rbackman@5791 118
rbackman@5791 119 if (type1->singleton()) {
rbackman@5791 120 // Make it x + Constant - move constant to the right
rbackman@5791 121 swap_edges(1, 2);
rbackman@5791 122 return this;
rbackman@5791 123 }
rbackman@5791 124
rbackman@5791 125 if (arg2->is_Load()) {
rbackman@5791 126 return NULL; // no change - keep load on the right
rbackman@5791 127 }
rbackman@5791 128
rbackman@5791 129 if (arg1->is_Load()) {
rbackman@5791 130 // Make it x + Load - move load to the right
rbackman@5791 131 swap_edges(1, 2);
rbackman@5791 132 return this;
rbackman@5791 133 }
rbackman@5791 134
rbackman@5791 135 if (arg1->_idx > arg2->_idx) {
rbackman@5791 136 // Sort the edges
rbackman@5791 137 swap_edges(1, 2);
rbackman@5791 138 return this;
rbackman@5791 139 }
rbackman@5791 140
rbackman@5791 141 return NULL;
rbackman@5791 142 }
rbackman@5791 143

mercurial