/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "c1/c1_Canonicalizer.hpp"
#include "c1/c1_Optimizer.hpp"
#include "c1/c1_ValueMap.hpp"
#include "c1/c1_ValueSet.hpp"
#include "c1/c1_ValueStack.hpp"
#include "utilities/bitMap.inline.hpp"
#include "compiler/compileLog.hpp"
}
private:
int _has_substitution;
public:
_has_substitution = false;
_hir->iterate_preorder(this);
if (_has_substitution) {
}
}
~CE_Eliminator() {
}
int e = sux->number_of_exception_handlers();
for (int i = 0; i < e; i++) {
if (sux->number_of_preds() == 0) {
// sux is disconnected from graph so disconnect from exception handlers
}
}
}
}
private:
};
// 1) find conditional expression
// check if block ends with an If
// check if If works on int or object types
// (we cannot handle If's working on long, float or doubles yet,
// since IfOp doesn't support them - these If's show up if cmp
// operations followed by If's are eliminated)
// one Constant may be present between BlockBegin and BlockEnd
}
}
// check if both branches end with a goto
// check if both gotos merge into the same block
// check if at least one word was pushed on sux_state
// inlining depths must match
}
}
}
// check if phi function is present at end of successor stack and that
// only this phi was pushed on the stack
// get the values that were pushed in the true- and false-branch
// backend does not support floats
// check that successor has no other phi functions but sux_phi
// this can happen when t_block or f_block contained additonal stores to local variables
// that are no longer represented by explicit instructions
);
// true and false blocks can't have phis
// 2) substitute conditional expression
// with an IfOp followed by a Goto
// cut if_ away and get node before
// append constants of true- and false-block if necessary
// clone constants because original block must not be destroyed
}
}
}
// append Goto to successor
Goto* goto_ = new Goto(sux, state_before, if_->is_safepoint() || t_goto->is_safepoint() || f_goto->is_safepoint());
// prepare state for Goto
// Adjust control flow graph
if (t_block->number_of_preds() == 0) {
}
if (f_block->number_of_preds() == 0) {
}
// update block end
// substitute the phi if possible
_has_substitution = true;
}
// 3) successfully eliminated a conditional expression
_cee_count++;
if (PrintCEE) {
tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id());
}
}
Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval) {
if (!OptimizeIfOps) {
}
_ifop_count++;
return tval;
}
x = x->subst();
y = y->subst();
// not_comparable here is a valid return in case we're comparing unloaded oop constants
_ifop_count++;
return new_tval;
} else {
}
}
}
} else {
// not_comparable here is a valid return in case we're comparing unloaded oop constants
_ifop_count++;
}
}
}
}
}
// find conditional expressions & replace them with IfOps
}
private:
public:
, _merge_count(0)
{
_hir->iterate_preorder(this);
}
~BlockMerger() {
}
// Note: It would be sufficient to check for the number of successors (= 1)
// in order to decide if this block can be merged potentially. That
// would then also include switch statements w/ only a default case.
// However, in that case we would need to make sure the switch tag
// expression is executed if it can produce observable side effects.
// We should probably have the canonicalizer simplifying such switch
// statements and then we are sure we don't miss these merge opportunities
// here (was bug - gri 7/7/99).
// merge the two blocks
#ifdef ASSERT
// verify that state at the end of block and at the beginning of sux are equal
// no phi functions must be present at beginning of sux
int index;
}
}
#endif
// find instruction before end & append first instruction of sux block
// add exception handlers of deleted block, if any
for (int k = 0; k < sux->number_of_exception_handlers(); k++) {
// also substitute predecessor of exception handler
}
}
// debugging output
_merge_count++;
if (PrintBlockElimination) {
}
if (if_) {
bool swapped = false;
swapped = true;
}
// Find the instruction before if_, starting with ifop.
// When if_ and ifop are not in the same block, prev
// becomes NULL In such (rare) cases it is not
// profitable to perform the optimization.
}
if (swapped) {
}
_merge_count++;
if (PrintBlockElimination) {
tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
}
}
}
}
}
}
return true;
}
}
return false;
}
// repeat since the same block may merge again
}
}
};
// merge blocks if possible
}
class NullCheckEliminator;
private:
public:
NullCheckVisitor() {}
void do_Constant (Constant* x);
void do_LoadField (LoadField* x);
void do_StoreField (StoreField* x);
void do_ArrayLength (ArrayLength* x);
void do_LoadIndexed (LoadIndexed* x);
void do_StoreIndexed (StoreIndexed* x);
void do_NegateOp (NegateOp* x);
void do_ArithmeticOp (ArithmeticOp* x);
void do_ShiftOp (ShiftOp* x);
void do_LogicOp (LogicOp* x);
void do_CompareOp (CompareOp* x);
void do_Convert (Convert* x);
void do_NullCheck (NullCheck* x);
void do_TypeCast (TypeCast* x);
void do_NewInstance (NewInstance* x);
void do_NewTypeArray (NewTypeArray* x);
void do_NewObjectArray (NewObjectArray* x);
void do_NewMultiArray (NewMultiArray* x);
void do_CheckCast (CheckCast* x);
void do_InstanceOf (InstanceOf* x);
void do_MonitorEnter (MonitorEnter* x);
void do_MonitorExit (MonitorExit* x);
void do_Intrinsic (Intrinsic* x);
void do_BlockBegin (BlockBegin* x);
void do_IfInstanceOf (IfInstanceOf* x);
void do_TableSwitch (TableSwitch* x);
void do_LookupSwitch (LookupSwitch* x);
void do_OsrEntry (OsrEntry* x);
void do_ExceptionObject(ExceptionObject* x);
void do_RoundFP (RoundFP* x);
void do_UnsafeGetRaw (UnsafeGetRaw* x);
void do_UnsafePutRaw (UnsafePutRaw* x);
void do_UnsafeGetObject(UnsafeGetObject* x);
void do_UnsafePutObject(UnsafePutObject* x);
void do_UnsafePrefetchRead (UnsafePrefetchRead* x);
void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x);
void do_ProfileCall (ProfileCall* x);
void do_ProfileInvoke (ProfileInvoke* x);
void do_RuntimeCall (RuntimeCall* x);
};
// Because of a static contained within (for the purpose of iteration
// over instructions), it is only valid to have one of these active at
// a time
private:
return _visitable_instructions->contains(x);
}
}
}
void clear_visitable_state() {
}
void iterate_all();
void set_state_for (BlockBegin* block, ValueSet* stack) { _block_states[block->block_id()] = stack; }
// Returns true if caused a change in the block's state.
public:
// constructor
, _work_list(new BlockList()) {
_visitable_instructions = new ValueSet();
_visitor.set_eliminator(this);
}
~NullCheckEliminator() {
}
// Process a graph
// In some situations (like NullCheck(x); getfield(x)) the debug
// information from the explicit NullCheck can be used to populate
// the getfield, even if the two instructions are in different
// scopes; this allows implicit null checks to be used but the
// correct exception information to be generated. We must clear the
// last-traversed NullCheck when we reach a potentially-exception-
// throwing instruction, as well as in some other cases.
: NULL); }
_last_explicit_null_check->set_can_trap(false);
return _last_explicit_null_check;
}
// Handlers for relevant instructions
// (separated out from NullCheckVisitor for clarity)
// The basic contract is that these must leave the instruction in
// the desired state; must not assume anything about the state of
// the instruction. We make multiple passes over some basic blocks
// and the last pass is the only one whose result is valid.
void handle_AccessField (AccessField* x);
void handle_ArrayLength (ArrayLength* x);
void handle_LoadIndexed (LoadIndexed* x);
void handle_StoreIndexed (StoreIndexed* x);
void handle_NullCheck (NullCheck* x);
void handle_Invoke (Invoke* x);
void handle_NewInstance (NewInstance* x);
void handle_NewArray (NewArray* x);
void handle_AccessMonitor (AccessMonitor* x);
void handle_Intrinsic (Intrinsic* x);
void handle_ExceptionObject (ExceptionObject* x);
void handle_Phi (Phi* x);
};
// NEEDS_CLEANUP
// There may be other instructions which need to clear the last
// explicit null check. Anything across which we can not hoist the
// debug information for a NullCheck instruction must clear it. It
// might be safer to pattern match "NullCheck ; {AccessField,
// ArrayLength, LoadIndexed}" but it is more easily structured this way.
// Should test to see performance hit of clearing it for all handlers
// with empty bodies below. If it is negligible then we should leave
// that in for safety, otherwise should think more about it.
void NullCheckVisitor::do_ArithmeticOp (ArithmeticOp* x) { if (x->can_trap()) nce()->clear_last_explicit_null_check(); }
if (visitable(*p)) {
mark_visited(*p);
}
}
return true;
} else {
if (PrintNullCheckElimination && changed) {
}
return changed;
}
}
}
}
// clear out an old explicit null checks
if (PrintNullCheckElimination) {
}
// Create new state if none present (only happens at root)
// Initial state is that local 0 (receiver) is non-null for
// non-static methods
// Local 0 is used in this scope
if (PrintNullCheckElimination) {
}
}
}
}
// Must copy block's state to avoid mutating it during iteration
// through the block -- otherwise "not-null" states can accidentally
// propagate "up" through the block during processing of backward
// branches and algorithm is incorrect (and does not converge)
// allow visiting of Phis belonging to this block
);
int i;
// Propagate the state before this block into the exception
// handlers. They aren't true successors since we aren't guaranteed
// to execute the whole block before executing them. Also putting
// them on first seems to help reduce the amount of iteration to
// reach a fixed point.
for (i = 0; i < block->number_of_exception_handlers(); i++) {
}
}
}
// Iterate through block, updating state.
// Mark instructions in this block as visitable as they are seen
// in the instruction list. This keeps the iteration from
// visiting instructions which are references in other blocks or
// visiting instructions more than once.
instr->input_values_do(this);
}
}
// Propagate state to successors if necessary
for (i = 0; i < e->number_of_sux(); i++) {
}
}
}
}
iterate_all();
}
if (x->is_static()) {
if (x->as_LoadField() != NULL) {
// If the field is a non-null static final object field (as is
// often the case for sun.misc.Unsafe), put this LoadField into
// the non-null map
if (field->is_constant()) {
if (!obj_val->is_null_object()) {
if (PrintNullCheckElimination) {
x->id());
}
set_put(x);
}
}
}
}
// Be conservative
return;
}
if (set_contains(obj)) {
// Value is non-null => update AccessField
x->set_needs_null_check(true);
if (PrintNullCheckElimination) {
}
} else {
x->set_needs_null_check(false);
if (PrintNullCheckElimination) {
}
}
} else {
if (PrintNullCheckElimination) {
}
// Ensure previous passes do not cause wrong state
x->set_needs_null_check(true);
}
}
if (set_contains(array)) {
// Value is non-null => update AccessArray
if (last_explicit_null_check_obj() == array) {
x->set_needs_null_check(true);
if (PrintNullCheckElimination) {
}
} else {
x->set_needs_null_check(false);
if (PrintNullCheckElimination) {
}
}
} else {
if (PrintNullCheckElimination) {
}
// Ensure previous passes do not cause wrong state
x->set_needs_null_check(true);
}
}
if (set_contains(array)) {
// Value is non-null => update AccessArray
if (last_explicit_null_check_obj() == array) {
x->set_needs_null_check(true);
if (PrintNullCheckElimination) {
}
} else {
x->set_needs_null_check(false);
if (PrintNullCheckElimination) {
}
}
} else {
if (PrintNullCheckElimination) {
}
// Ensure previous passes do not cause wrong state
x->set_needs_null_check(true);
}
}
if (set_contains(array)) {
// Value is non-null => update AccessArray
if (PrintNullCheckElimination) {
}
x->set_needs_null_check(false);
} else {
if (PrintNullCheckElimination) {
}
// Ensure previous passes do not cause wrong state
x->set_needs_null_check(true);
}
}
if (set_contains(obj)) {
// Already proven to be non-null => this NullCheck is useless
if (PrintNullCheckElimination) {
}
// Don't unpin since that may shrink obj's live range and make it unavailable for debug info.
// The code generator won't emit LIR for a NullCheck that cannot trap.
x->set_can_trap(false);
} else {
// May be null => add to map and set last explicit NullCheck
x->set_can_trap(true);
// make sure it's pinned if it can trap
if (PrintNullCheckElimination) {
}
}
}
if (!x->has_receiver()) {
// Be conservative
return;
}
if (!set_contains(recv)) {
if (PrintNullCheckElimination) {
}
}
}
set_put(x);
if (PrintNullCheckElimination) {
}
}
set_put(x);
if (PrintNullCheckElimination) {
}
}
set_put(x);
if (PrintNullCheckElimination) {
}
}
if (set_contains(obj)) {
// Value is non-null => update AccessMonitor
if (PrintNullCheckElimination) {
}
x->set_needs_null_check(false);
} else {
if (PrintNullCheckElimination) {
}
// Ensure previous passes do not cause wrong state
x->set_needs_null_check(true);
}
}
if (!x->has_receiver()) {
for (int i = 0; i < x->number_of_arguments(); i++) {
}
}
// Be conservative
return;
}
if (set_contains(recv)) {
// Value is non-null => update Intrinsic
if (PrintNullCheckElimination) {
}
x->set_needs_null_check(false);
} else {
if (PrintNullCheckElimination) {
}
// Ensure previous passes do not cause wrong state
x->set_needs_null_check(true);
}
}
int i;
bool all_non_null = true;
if (x->is_illegal()) {
all_non_null = false;
} else {
for (i = 0; i < x->operand_count(); i++) {
if (!set_contains(input)) {
all_non_null = false;
}
}
}
if (all_non_null) {
// Value is non-null => update Phi
if (PrintNullCheckElimination) {
tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
}
x->set_needs_null_check(false);
} else if (set_contains(x)) {
set_remove(x);
}
}
NullCheckEliminator nce(this);
if (PrintNullCheckElimination) {
}
// Apply to graph
// walk over the graph looking for exception
// handlers and iterate over them as well
BlockBegin* b = blocks[i];
// exception handlers need to be treated as additional roots
for (int e = b->number_of_exception_handlers(); e-- > 0; ) {
if (!visited_block[id]) {
visited_block[id] = true;
}
}
// traverse successors
for (int s = end->number_of_sux(); s-- > 0; ) {
if (!visited_block[id]) {
visited_block[id] = true;
}
}
}
if (PrintNullCheckElimination) {
}
}