1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/opto/mathexactnode.cpp Fri Sep 27 08:39:19 2013 +0200 1.3 @@ -0,0 +1,143 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "memory/allocation.inline.hpp" 1.30 +#include "opto/addnode.hpp" 1.31 +#include "opto/machnode.hpp" 1.32 +#include "opto/mathexactnode.hpp" 1.33 +#include "opto/matcher.hpp" 1.34 +#include "opto/subnode.hpp" 1.35 + 1.36 +MathExactNode::MathExactNode(Node* ctrl, Node* n1, Node* n2) : MultiNode(3) { 1.37 + init_req(0, ctrl); 1.38 + init_req(1, n1); 1.39 + init_req(2, n2); 1.40 +} 1.41 + 1.42 +Node* AddExactINode::match(const ProjNode* proj, const Matcher* m) { 1.43 + uint ideal_reg = proj->ideal_reg(); 1.44 + RegMask rm; 1.45 + if (proj->_con == result_proj_node) { 1.46 + rm = m->mathExactI_result_proj_mask(); 1.47 + } else { 1.48 + assert(proj->_con == flags_proj_node, "must be result or flags"); 1.49 + assert(ideal_reg == Op_RegFlags, "sanity"); 1.50 + rm = m->mathExactI_flags_proj_mask(); 1.51 + } 1.52 + return new (m->C) MachProjNode(this, proj->_con, rm, ideal_reg); 1.53 +} 1.54 + 1.55 +// If the MathExactNode won't overflow we have to replace the 1.56 +// FlagsProjNode and ProjNode that is generated by the MathExactNode 1.57 +Node* MathExactNode::no_overflow(PhaseGVN *phase, Node* new_result) { 1.58 + PhaseIterGVN *igvn = phase->is_IterGVN(); 1.59 + if (igvn) { 1.60 + ProjNode* result = result_node(); 1.61 + ProjNode* flags = flags_node(); 1.62 + 1.63 + if (result != NULL) { 1.64 + igvn->replace_node(result, new_result); 1.65 + } 1.66 + 1.67 + if (flags != NULL) { 1.68 + BoolNode* bolnode = (BoolNode *) flags->unique_out(); 1.69 + switch (bolnode->_test._test) { 1.70 + case BoolTest::overflow: 1.71 + // if the check is for overflow - never taken 1.72 + igvn->replace_node(bolnode, phase->intcon(0)); 1.73 + break; 1.74 + case BoolTest::no_overflow: 1.75 + // if the check is for no overflow - always taken 1.76 + igvn->replace_node(bolnode, phase->intcon(1)); 1.77 + break; 1.78 + default: 1.79 + fatal("Unexpected value of BoolTest"); 1.80 + break; 1.81 + } 1.82 + flags->del_req(0); 1.83 + } 1.84 + } 1.85 + return new_result; 1.86 +} 1.87 + 1.88 +Node *AddExactINode::Ideal(PhaseGVN *phase, bool can_reshape) { 1.89 + Node *arg1 = in(1); 1.90 + Node *arg2 = in(2); 1.91 + 1.92 + const Type* type1 = phase->type(arg1); 1.93 + const Type* type2 = phase->type(arg2); 1.94 + 1.95 + if (type1 != Type::TOP && type1->singleton() && 1.96 + type2 != Type::TOP && type2->singleton()) { 1.97 + jint val1 = arg1->get_int(); 1.98 + jint val2 = arg2->get_int(); 1.99 + jint result = val1 + val2; 1.100 + // Hacker's Delight 2-12 Overflow if both arguments have the opposite sign of the result 1.101 + if ( (((val1 ^ result) & (val2 ^ result)) >= 0)) { 1.102 + Node* con_result = ConINode::make(phase->C, result); 1.103 + return no_overflow(phase, con_result); 1.104 + } 1.105 + return NULL; 1.106 + } 1.107 + 1.108 + if (type1 == TypeInt::ZERO) { // (Add 0 x) == x 1.109 + Node* add_result = new (phase->C) AddINode(arg1, arg2); 1.110 + return no_overflow(phase, add_result); 1.111 + } 1.112 + 1.113 + if (type2 == TypeInt::ZERO) { // (Add x 0) == x 1.114 + Node* add_result = new (phase->C) AddINode(arg1, arg2); 1.115 + return no_overflow(phase, add_result); 1.116 + } 1.117 + 1.118 + if (type2->singleton()) { 1.119 + return NULL; // no change - keep constant on the right 1.120 + } 1.121 + 1.122 + if (type1->singleton()) { 1.123 + // Make it x + Constant - move constant to the right 1.124 + swap_edges(1, 2); 1.125 + return this; 1.126 + } 1.127 + 1.128 + if (arg2->is_Load()) { 1.129 + return NULL; // no change - keep load on the right 1.130 + } 1.131 + 1.132 + if (arg1->is_Load()) { 1.133 + // Make it x + Load - move load to the right 1.134 + swap_edges(1, 2); 1.135 + return this; 1.136 + } 1.137 + 1.138 + if (arg1->_idx > arg2->_idx) { 1.139 + // Sort the edges 1.140 + swap_edges(1, 2); 1.141 + return this; 1.142 + } 1.143 + 1.144 + return NULL; 1.145 +} 1.146 +