310 case Bytecodes::_ishl: set_constant(value << shift); return;
311 case Bytecodes::_ishr: set_constant(value >> shift); return;
312 case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
313 }
314 } else if (t->tag() == longTag) {
315 jlong value = t->as_LongConstant()->value();
316 int shift = t2->as_IntConstant()->value() & 63;
317 jlong mask = ~(~jlong_cast(0) << (64 - shift));
318 if (shift == 0) mask = ~jlong_cast(0);
319 switch (x->op()) {
320 case Bytecodes::_lshl: set_constant(value << shift); return;
321 case Bytecodes::_lshr: set_constant(value >> shift); return;
322 case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
323 }
324 }
325 }
326 }
327 if (t2->is_constant()) {
328 switch (t2->tag()) {
329 case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;
330 case longTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;
331 default : ShouldNotReachHere();
332 }
333 }
334 }
335
336
337 void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); }
338 void Canonicalizer::do_CompareOp (CompareOp* x) {
339 if (x->x() == x->y()) {
340 switch (x->x()->type()->tag()) {
341 case longTag: set_constant(0); break;
342 case floatTag: {
343 FloatConstant* fc = x->x()->type()->as_FloatConstant();
344 if (fc) {
345 if (g_isnan(fc->value())) {
346 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
347 } else {
348 set_constant(0);
349 }
350 }
791 // (Constant & If) and the Canonicalizer cannot return them correctly
792 // yet. For now we copied the corresponding code directly into the
793 // GraphBuilder (i.e., we should never reach here).
794 return;
795 // simplify to If
796 assert(x->length() == 1, "length must be the same");
797 Constant* key = new Constant(new IntConstant(x->key_at(0)));
798 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
799 }
800 }
801
802
803 void Canonicalizer::do_Return (Return* x) {}
804 void Canonicalizer::do_Throw (Throw* x) {}
805 void Canonicalizer::do_Base (Base* x) {}
806 void Canonicalizer::do_OsrEntry (OsrEntry* x) {}
807 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
808
809 static bool match_index_and_scale(Instruction* instr,
810 Instruction** index,
811 int* log2_scale,
812 Instruction** instr_to_unpin) {
813 *instr_to_unpin = NULL;
814
815 // Skip conversion ops
816 Convert* convert = instr->as_Convert();
817 if (convert != NULL) {
818 instr = convert->value();
819 }
820
821 ShiftOp* shift = instr->as_ShiftOp();
822 if (shift != NULL) {
823 if (shift->is_pinned()) {
824 *instr_to_unpin = shift;
825 }
826 // Constant shift value?
827 Constant* con = shift->y()->as_Constant();
828 if (con == NULL) return false;
829 // Well-known type and value?
830 IntConstant* val = con->type()->as_IntConstant();
831 if (val == NULL) return false;
832 if (shift->x()->type() != intType) return false;
833 *index = shift->x();
834 int tmp_scale = val->value();
835 if (tmp_scale >= 0 && tmp_scale < 4) {
836 *log2_scale = tmp_scale;
837 return true;
838 } else {
839 return false;
840 }
841 }
842
843 ArithmeticOp* arith = instr->as_ArithmeticOp();
844 if (arith != NULL) {
845 if (arith->is_pinned()) {
846 *instr_to_unpin = arith;
847 }
848 // Check for integer multiply
849 if (arith->op() == Bytecodes::_imul) {
850 // See if either arg is a known constant
851 Constant* con = arith->x()->as_Constant();
852 if (con != NULL) {
853 *index = arith->y();
854 } else {
855 con = arith->y()->as_Constant();
856 if (con == NULL) return false;
857 *index = arith->x();
858 }
859 if ((*index)->type() != intType) return false;
860 // Well-known type and value?
861 IntConstant* val = con->type()->as_IntConstant();
862 if (val == NULL) return false;
863 switch (val->value()) {
864 case 1: *log2_scale = 0; return true;
865 case 2: *log2_scale = 1; return true;
866 case 4: *log2_scale = 2; return true;
867 case 8: *log2_scale = 3; return true;
868 default: return false;
869 }
870 }
871 }
872
873 // Unknown instruction sequence; don't touch it
874 return false;
875 }
876
877
878 static bool match(UnsafeRawOp* x,
879 Instruction** base,
880 Instruction** index,
881 int* log2_scale) {
882 Instruction* instr_to_unpin = NULL;
883 ArithmeticOp* root = x->base()->as_ArithmeticOp();
884 if (root == NULL) return false;
885 // Limit ourselves to addition for now
886 if (root->op() != Bytecodes::_ladd) return false;
887 // Try to find shift or scale op
888 if (match_index_and_scale(root->y(), index, log2_scale, &instr_to_unpin)) {
889 *base = root->x();
890 } else if (match_index_and_scale(root->x(), index, log2_scale, &instr_to_unpin)) {
891 *base = root->y();
892 } else if (root->y()->as_Convert() != NULL) {
893 Convert* convert = root->y()->as_Convert();
894 if (convert->op() == Bytecodes::_i2l && convert->value()->type() == intType) {
895 // pick base and index, setting scale at 1
896 *base = root->x();
897 *index = convert->value();
898 *log2_scale = 0;
899 } else {
900 return false;
901 }
902 } else {
903 // doesn't match any expected sequences
904 return false;
905 }
906
907 // If the value is pinned then it will be always be computed so
908 // there's no profit to reshaping the expression.
909 return !root->is_pinned();
910 }
911
912
913 void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {
914 Instruction* base = NULL;
915 Instruction* index = NULL;
916 int log2_scale;
917
918 if (match(x, &base, &index, &log2_scale)) {
919 x->set_base(base);
920 x->set_index(index);
921 x->set_log2_scale(log2_scale);
922 if (PrintUnsafeOptimization) {
923 tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",
924 x->id(), x->base()->id(), x->index()->id(), x->log2_scale());
|
310 case Bytecodes::_ishl: set_constant(value << shift); return;
311 case Bytecodes::_ishr: set_constant(value >> shift); return;
312 case Bytecodes::_iushr: set_constant((value >> shift) & mask); return;
313 }
314 } else if (t->tag() == longTag) {
315 jlong value = t->as_LongConstant()->value();
316 int shift = t2->as_IntConstant()->value() & 63;
317 jlong mask = ~(~jlong_cast(0) << (64 - shift));
318 if (shift == 0) mask = ~jlong_cast(0);
319 switch (x->op()) {
320 case Bytecodes::_lshl: set_constant(value << shift); return;
321 case Bytecodes::_lshr: set_constant(value >> shift); return;
322 case Bytecodes::_lushr: set_constant((value >> shift) & mask); return;
323 }
324 }
325 }
326 }
327 if (t2->is_constant()) {
328 switch (t2->tag()) {
329 case intTag : if (t2->as_IntConstant()->value() == 0) set_canonical(x->x()); return;
330 case longTag : if (t2->as_LongConstant()->value() == (jlong)0) set_canonical(x->x()); return;
331 default : ShouldNotReachHere();
332 }
333 }
334 }
335
336
337 void Canonicalizer::do_LogicOp (LogicOp* x) { do_Op2(x); }
338 void Canonicalizer::do_CompareOp (CompareOp* x) {
339 if (x->x() == x->y()) {
340 switch (x->x()->type()->tag()) {
341 case longTag: set_constant(0); break;
342 case floatTag: {
343 FloatConstant* fc = x->x()->type()->as_FloatConstant();
344 if (fc) {
345 if (g_isnan(fc->value())) {
346 set_constant(x->op() == Bytecodes::_fcmpl ? -1 : 1);
347 } else {
348 set_constant(0);
349 }
350 }
791 // (Constant & If) and the Canonicalizer cannot return them correctly
792 // yet. For now we copied the corresponding code directly into the
793 // GraphBuilder (i.e., we should never reach here).
794 return;
795 // simplify to If
796 assert(x->length() == 1, "length must be the same");
797 Constant* key = new Constant(new IntConstant(x->key_at(0)));
798 set_canonical(new If(x->tag(), If::eql, true, key, x->sux_at(0), x->default_sux(), x->state_before(), x->is_safepoint()));
799 }
800 }
801
802
803 void Canonicalizer::do_Return (Return* x) {}
804 void Canonicalizer::do_Throw (Throw* x) {}
805 void Canonicalizer::do_Base (Base* x) {}
806 void Canonicalizer::do_OsrEntry (OsrEntry* x) {}
807 void Canonicalizer::do_ExceptionObject(ExceptionObject* x) {}
808
809 static bool match_index_and_scale(Instruction* instr,
810 Instruction** index,
811 int* log2_scale) {
812 // Skip conversion ops. This works only on 32bit because of the implicit l2i that the
813 // unsafe performs.
814 #ifndef _LP64
815 Convert* convert = instr->as_Convert();
816 if (convert != NULL && convert->op() == Bytecodes::_i2l) {
817 assert(convert->value()->type() == intType, "invalid input type");
818 instr = convert->value();
819 }
820 #endif
821
822 ShiftOp* shift = instr->as_ShiftOp();
823 if (shift != NULL) {
824 if (shift->op() == Bytecodes::_lshl) {
825 assert(shift->x()->type() == longType, "invalid input type");
826 } else {
827 #ifndef _LP64
828 if (shift->op() == Bytecodes::_ishl) {
829 assert(shift->x()->type() == intType, "invalid input type");
830 } else {
831 return false;
832 }
833 #else
834 return false;
835 #endif
836 }
837
838
839 // Constant shift value?
840 Constant* con = shift->y()->as_Constant();
841 if (con == NULL) return false;
842 // Well-known type and value?
843 IntConstant* val = con->type()->as_IntConstant();
844 assert(val != NULL, "Should be an int constant");
845
846 *index = shift->x();
847 int tmp_scale = val->value();
848 if (tmp_scale >= 0 && tmp_scale < 4) {
849 *log2_scale = tmp_scale;
850 return true;
851 } else {
852 return false;
853 }
854 }
855
856 ArithmeticOp* arith = instr->as_ArithmeticOp();
857 if (arith != NULL) {
858 // See if either arg is a known constant
859 Constant* con = arith->x()->as_Constant();
860 if (con != NULL) {
861 *index = arith->y();
862 } else {
863 con = arith->y()->as_Constant();
864 if (con == NULL) return false;
865 *index = arith->x();
866 }
867 long const_value;
868 // Check for integer multiply
869 if (arith->op() == Bytecodes::_lmul) {
870 assert((*index)->type() == longType, "invalid input type");
871 LongConstant* val = con->type()->as_LongConstant();
872 assert(val != NULL, "expecting a long constant");
873 const_value = val->value();
874 } else {
875 #ifndef _LP64
876 if (arith->op() == Bytecodes::_imul) {
877 assert((*index)->type() == intType, "invalid input type");
878 IntConstant* val = con->type()->as_IntConstant();
879 assert(val != NULL, "expecting an int constant");
880 const_value = val->value();
881 } else {
882 return false;
883 }
884 #else
885 return false;
886 #endif
887 }
888 switch (const_value) {
889 case 1: *log2_scale = 0; return true;
890 case 2: *log2_scale = 1; return true;
891 case 4: *log2_scale = 2; return true;
892 case 8: *log2_scale = 3; return true;
893 default: return false;
894 }
895 }
896
897 // Unknown instruction sequence; don't touch it
898 return false;
899 }
900
901
902 static bool match(UnsafeRawOp* x,
903 Instruction** base,
904 Instruction** index,
905 int* log2_scale) {
906 ArithmeticOp* root = x->base()->as_ArithmeticOp();
907 if (root == NULL) return false;
908 // Limit ourselves to addition for now
909 if (root->op() != Bytecodes::_ladd) return false;
910
911 bool match_found = false;
912 // Try to find shift or scale op
913 if (match_index_and_scale(root->y(), index, log2_scale)) {
914 *base = root->x();
915 match_found = true;
916 } else if (match_index_and_scale(root->x(), index, log2_scale)) {
917 *base = root->y();
918 match_found = true;
919 } else if (NOT_LP64(root->y()->as_Convert() != NULL) LP64_ONLY(false)) {
920 // Skipping i2l works only on 32bit because of the implicit l2i that the unsafe performs.
921 // 64bit needs a real sign-extending conversion.
922 Convert* convert = root->y()->as_Convert();
923 if (convert->op() == Bytecodes::_i2l) {
924 assert(convert->value()->type() == intType, "should be an int");
925 // pick base and index, setting scale at 1
926 *base = root->x();
927 *index = convert->value();
928 *log2_scale = 0;
929 match_found = true;
930 }
931 }
932 // The default solution
933 if (!match_found) {
934 *base = root->x();
935 *index = root->y();
936 *log2_scale = 0;
937 }
938
939 // If the value is pinned then it will be always be computed so
940 // there's no profit to reshaping the expression.
941 return !root->is_pinned();
942 }
943
944
945 void Canonicalizer::do_UnsafeRawOp(UnsafeRawOp* x) {
946 Instruction* base = NULL;
947 Instruction* index = NULL;
948 int log2_scale;
949
950 if (match(x, &base, &index, &log2_scale)) {
951 x->set_base(base);
952 x->set_index(index);
953 x->set_log2_scale(log2_scale);
954 if (PrintUnsafeOptimization) {
955 tty->print_cr("Canonicalizer: UnsafeRawOp id %d: base = id %d, index = id %d, log2_scale = %d",
956 x->id(), x->base()->id(), x->index()->id(), x->log2_scale());
|