void Canonicalizer::do_LoadIndexed (LoadIndexed* x) { StableArrayConstant* array = x->array()->type()->as_StableArrayConstant(); IntConstant* index = x->index()->type()->as_IntConstant(); assert(array == NULL || FoldStableValues, "not enabled"); // Constant fold loads from stable arrays. if (!x->mismatched() && array != NULL && index != NULL) { jint idx = index->value(); if (idx < 0 || idx >= array->value()->length()) { // Leave the load as is. The range check will handle it. return; } ciConstant field_val = array->value()->element_value(idx); if (!field_val.is_null_or_zero()) { jint dimension = array->dimension(); assert(dimension <= array->value()->array_type()->dimension(), "inconsistent info"); ValueType* value = NULL; if (dimension > 1) { // Preserve information about the dimension for the element. assert(field_val.as_object()->is_array(), "not an array"); value = new StableArrayConstant(field_val.as_object()->as_array(), dimension - 1); } else { assert(dimension == 1, "sanity"); value = as_ValueType(field_val); } set_canonical(new Constant(value)); } } }
int main(int argc, char *argv[]) { struct cli_command *c; struct cli_def *cli; cli = cli_init(); cli_set_banner(cli, "libcli test environment"); cli_set_hostname(cli, "router"); cli_set_newline(cli, "\n"); cli_register_command(cli, NULL, "test", cmd_test, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); cli_register_command(cli, NULL, "sex", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); cli_register_command(cli, NULL, "simple", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); cli_register_command(cli, NULL, "simon", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL); c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); cli_register_command(cli, c, "counters", cmd_test, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the counters that the system uses"); cli_register_command(cli, c, "junk", cmd_test, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); cli_register_command(cli, NULL, "interface", cmd_config_int, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Configure an interface"); cli_register_command(cli, NULL, "exit", cmd_config_int_exit, PRIVILEGE_PRIVILEGED, MODE_CONFIG_INT, "Exit from interface configuration"); cli_register_command(cli, NULL, "address", cmd_test, PRIVILEGE_PRIVILEGED, MODE_CONFIG_INT, "Set IP address"); //cli_set_auth_callback(cli, check_auth); //cli_set_enable_callback(cli, check_enable); // Test reading from a file { FILE *fh; if ((fh = fopen("clitest.txt", "r"))) { // This sets a callback which just displays the cli_print() text to stdout cli_print_callback(cli, pc); cli_file(cli, fh, PRIVILEGE_UNPRIVILEGED, MODE_EXEC); cli_print_callback(cli, NULL); fclose(fh); } } set_canonical(0); cli_loop(cli, fileno(stdin), fileno(stdout)); set_canonical(1); cli_done(cli); return 0; }
void Canonicalizer::do_ArrayLength (ArrayLength* x) { NewArray* array = x->array()->as_NewArray(); if (array != NULL && array->length() != NULL) { Constant* length = array->length()->as_Constant(); if (length != NULL) { set_canonical(length); } } }
void Canonicalizer::do_LookupSwitch(LookupSwitch* x) { if (x->number_of_sux() == 1) { // NOTE: Code permanently disabled for now since the switch statement's // tag expression may produce side-effects in which case it must // be executed. return; // simplify to Goto set_canonical(new Goto(x->default_sux(), x->is_safepoint())); } else if (x->number_of_sux() == 2) { // NOTE: Code permanently disabled for now since it produces two new nodes // (Constant & If) and the Canonicalizer cannot return them correctly // yet. For now we copied the corresponding code directly into the // GraphBuilder (i.e., we should never reach here). return; // simplify to If assert(x->length() == 1, "length must be the same"); Constant* key = new Constant(new IntConstant(x->key_at(0))); set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint())); } }
void Canonicalizer::do_ShiftOp (ShiftOp* x) { ValueType* t = x->x()->type(); if (t->is_constant()) { switch (t->tag()) { case intTag : if (t->as_IntConstant()->value() == 0) set_constant(0); return; case longTag : if (t->as_LongConstant()->value() == (jlong)0) set_constant(jlong_cast(0)); return; default : ShouldNotReachHere(); } } ValueType* t2 = x->y()->type(); if (t2->is_constant()) { switch (t2->tag()) { case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return; default : ShouldNotReachHere(); } } }
void Canonicalizer::do_StoreField (StoreField* x) { // If a value is going to be stored into a field or array some of // the conversions emitted by javac are unneeded because the fields // are packed to their natural size. Convert* conv = x->value()->as_Convert(); if (conv) { Value value = NULL; BasicType type = x->field()->type()->basic_type(); switch (conv->op()) { case Bytecodes::_i2b: if (type == T_BYTE) value = conv->value(); break; case Bytecodes::_i2s: if (type == T_SHORT || type == T_BYTE) value = conv->value(); break; case Bytecodes::_i2c: if (type == T_CHAR || type == T_BYTE) value = conv->value(); break; } // limit this optimization to current block if (value != NULL && in_current_block(conv)) { set_canonical(new StoreField(x->obj(), x->offset(), x->field(), value, x->is_static(), x->state_before(), x->needs_patching())); return; } } }
void Canonicalizer::do_Op2(Op2* x) { if (x->x() == x->y()) { switch (x->op()) { case Bytecodes::_isub: set_constant(0); return; case Bytecodes::_lsub: set_constant(jlong_cast(0)); return; case Bytecodes::_iand: // fall through case Bytecodes::_land: // fall through case Bytecodes::_ior: // fall through case Bytecodes::_lor : set_canonical(x->x()); return; case Bytecodes::_ixor: set_constant(0); return; case Bytecodes::_lxor: set_constant(jlong_cast(0)); return; } } if (x->x()->type()->is_constant() && x->y()->type()->is_constant()) { // do constant folding for selected operations switch (x->type()->tag()) { case intTag: { jint a = x->x()->type()->as_IntConstant()->value(); jint b = x->y()->type()->as_IntConstant()->value(); switch (x->op()) { case Bytecodes::_iadd: set_constant(a + b); return; case Bytecodes::_isub: set_constant(a - b); return; case Bytecodes::_imul: set_constant(a * b); return; case Bytecodes::_idiv: if (b != 0) { if (a == min_jint && b == -1) { set_constant(min_jint); } else { set_constant(a / b); } return; } break; case Bytecodes::_irem: if (b != 0) { if (a == min_jint && b == -1) { set_constant(0); } else { set_constant(a % b); } return; } break; case Bytecodes::_iand: set_constant(a & b); return; case Bytecodes::_ior : set_constant(a | b); return; case Bytecodes::_ixor: set_constant(a ^ b); return; } } break; case longTag: { jlong a = x->x()->type()->as_LongConstant()->value(); jlong b = x->y()->type()->as_LongConstant()->value(); switch (x->op()) { case Bytecodes::_ladd: set_constant(a + b); return; case Bytecodes::_lsub: set_constant(a - b); return; case Bytecodes::_lmul: set_constant(a * b); return; case Bytecodes::_ldiv: if (b != 0) { set_constant(SharedRuntime::ldiv(b, a)); return; } break; case Bytecodes::_lrem: if (b != 0) { set_constant(SharedRuntime::lrem(b, a)); return; } break; case Bytecodes::_land: set_constant(a & b); return; case Bytecodes::_lor : set_constant(a | b); return; case Bytecodes::_lxor: set_constant(a ^ b); return; } } break; // other cases not implemented (must be extremely careful with floats & doubles!) } } // make sure constant is on the right side, if any move_const_to_right(x); if (x->y()->type()->is_constant()) { // do constant folding for selected operations switch (x->type()->tag()) { case intTag: if (x->y()->type()->as_IntConstant()->value() == 0) { switch (x->op()) { case Bytecodes::_iadd: set_canonical(x->x()); return; case Bytecodes::_isub: set_canonical(x->x()); return; case Bytecodes::_imul: set_constant(0); return; // Note: for div and rem, make sure that C semantics // corresponds to Java semantics! case Bytecodes::_iand: set_constant(0); return; case Bytecodes::_ior : set_canonical(x->x()); return; } } break; case longTag: if (x->y()->type()->as_LongConstant()->value() == (jlong)0) { switch (x->op()) { case Bytecodes::_ladd: set_canonical(x->x()); return; case Bytecodes::_lsub: set_canonical(x->x()); return; case Bytecodes::_lmul: set_constant((jlong)0); return; // Note: for div and rem, make sure that C semantics // corresponds to Java semantics! case Bytecodes::_land: set_constant((jlong)0); return; case Bytecodes::_lor : set_canonical(x->x()); return; } } break; } } }
void set_constant(jdouble x) { set_canonical(new Constant(new DoubleConstant(x))); }
void set_constant(jfloat x) { set_canonical(new Constant(new FloatConstant(x))); }
void set_constant(jlong x) { set_canonical(new Constant(new LongConstant(x))); }
void set_constant(jint x) { set_canonical(new Constant(new IntConstant(x))); }
void Canonicalizer::do_If(If* x) { // move const to right if (x->x()->type()->is_constant()) x->swap_operands(); // simplify const Value l = x->x(); ValueType* lt = l->type(); const Value r = x->y(); ValueType* rt = r->type(); if (lt->is_constant() && rt->is_constant()) { // pattern: If (lc cond rc) => simplify to: Goto Goto* g = NULL; switch (lt->tag()) { case intTag: g = new Goto(x->sux_for(is_true(lt->as_IntConstant ()->value(), x->cond(), rt->as_IntConstant ()->value())), x->is_safepoint()); break; case longTag: g = new Goto(x->sux_for(is_true(lt->as_LongConstant()->value(), x->cond(), rt->as_LongConstant()->value())), x->is_safepoint()); break; // other cases not implemented (must be extremely careful with floats & doubles!) } if (g != NULL) { // If this If is a safepoint then the debug information should come from the state_before of the If. g->set_state_before(x->state_before()); set_canonical(g); } } else if (rt->as_IntConstant() != NULL) { // pattern: If (l cond rc) => investigate further const jint rc = rt->as_IntConstant()->value(); if (l->as_CompareOp() != NULL) { // pattern: If ((a cmp b) cond rc) => simplify to: If (x cond y) or: Goto CompareOp* cmp = l->as_CompareOp(); bool unordered_is_less = cmp->op() == Bytecodes::_fcmpl || cmp->op() == Bytecodes::_dcmpl; BlockBegin* lss_sux = x->sux_for(is_true(-1, x->cond(), rc)); // successor for a < b BlockBegin* eql_sux = x->sux_for(is_true( 0, x->cond(), rc)); // successor for a = b BlockBegin* gtr_sux = x->sux_for(is_true(+1, x->cond(), rc)); // successor for a > b BlockBegin* nan_sux = unordered_is_less ? lss_sux : gtr_sux ; // successor for unordered // Note: At this point all successors (lss_sux, eql_sux, gtr_sux, nan_sux) are // equal to x->tsux() or x->fsux(). Furthermore, nan_sux equals either // lss_sux or gtr_sux. if (lss_sux == eql_sux && eql_sux == gtr_sux) { // all successors identical => simplify to: Goto set_canonical(new Goto(lss_sux, x->is_safepoint())); } else { // two successors differ and two successors are the same => simplify to: If (x cmp y) // determine new condition & successors If::Condition cond; BlockBegin* tsux = NULL; BlockBegin* fsux = NULL; if (lss_sux == eql_sux) { cond = If::leq; tsux = lss_sux; fsux = gtr_sux; } else if (lss_sux == gtr_sux) { cond = If::neq; tsux = lss_sux; fsux = eql_sux; } else if (eql_sux == gtr_sux) { cond = If::geq; tsux = eql_sux; fsux = lss_sux; } else { ShouldNotReachHere(); } set_canonical(new If(cmp->x(), cond, nan_sux == tsux, cmp->y(), tsux, fsux, cmp->state_before(), x->is_safepoint())); set_bci(cmp->bci()); } } else if (l->as_InstanceOf() != NULL) { // NOTE: Code permanently disabled for now since it leaves the old InstanceOf // instruction in the graph (it is pinned). Need to fix this at some point. return; // pattern: If ((obj instanceof klass) cond rc) => simplify to: IfInstanceOf or: Goto InstanceOf* inst = l->as_InstanceOf(); BlockBegin* is_inst_sux = x->sux_for(is_true(1, x->cond(), rc)); // successor for instanceof == 1 BlockBegin* no_inst_sux = x->sux_for(is_true(0, x->cond(), rc)); // successor for instanceof == 0 if (is_inst_sux == no_inst_sux && inst->is_loaded()) { // both successors identical and klass is loaded => simplify to: Goto set_canonical(new Goto(is_inst_sux, x->is_safepoint())); } else { // successors differ => simplify to: IfInstanceOf set_canonical(new IfInstanceOf(inst->klass(), inst->obj(), true, inst->bci(), is_inst_sux, no_inst_sux)); } } } }