20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "ci/ciMethodData.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "compiler/compileLog.hpp"
30 #include "interpreter/linkResolver.hpp"
31 #include "memory/universe.inline.hpp"
32 #include "opto/addnode.hpp"
33 #include "opto/castnode.hpp"
34 #include "opto/convertnode.hpp"
35 #include "opto/divnode.hpp"
36 #include "opto/idealGraphPrinter.hpp"
37 #include "opto/matcher.hpp"
38 #include "opto/memnode.hpp"
39 #include "opto/mulnode.hpp"
40 #include "opto/parse.hpp"
41 #include "opto/runtime.hpp"
42 #include "runtime/deoptimization.hpp"
43 #include "runtime/sharedRuntime.hpp"
44
45 extern int explicit_null_checks_inserted,
46 explicit_null_checks_elided;
47
48 //---------------------------------array_load----------------------------------
49 void Parse::array_load(BasicType elem_type) {
50 const Type* elem = Type::TOP;
51 Node* adr = array_addressing(elem_type, 0, &elem);
52 if (stopped()) return; // guaranteed null or range check
53 dec_sp(2); // Pop array and index
54 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(elem_type);
55 Node* ld = make_load(control(), adr, elem, elem_type, adr_type, MemNode::unordered);
56 push(ld);
57 }
58
59
746 assert(ret_addr->singleton(), "must be a constant (cloned jsr body)");
747
748 // Effect on jsr on stack
749 push(_gvn.makecon(ret_addr));
750
751 // Flow to the jsr.
752 merge(jsr_bci);
753 }
754
755 // Handle ret bytecode
756 void Parse::do_ret() {
757 // Find to whom we return.
758 assert(block()->num_successors() == 1, "a ret can only go one place now");
759 Block* target = block()->successor_at(0);
760 assert(!target->is_ready(), "our arrival must be expected");
761 profile_ret(target->flow()->start());
762 int pnum = target->next_path_num();
763 merge_common(target, pnum);
764 }
765
766 //--------------------------dynamic_branch_prediction--------------------------
767 // Try to gather dynamic branch prediction behavior. Return a probability
768 // of the branch being taken and set the "cnt" field. Returns a -1.0
769 // if we need to use static prediction for some reason.
770 float Parse::dynamic_branch_prediction(float &cnt) {
771 ResourceMark rm;
772
773 cnt = COUNT_UNKNOWN;
774
775 // Use MethodData information if it is available
776 // FIXME: free the ProfileData structure
777 ciMethodData* methodData = method()->method_data();
778 if (!methodData->is_mature()) return PROB_UNKNOWN;
779 ciProfileData* data = methodData->bci_to_data(bci());
780 if (!data->is_JumpData()) return PROB_UNKNOWN;
781
782 // get taken and not taken values
783 int taken = data->as_JumpData()->taken();
784 int not_taken = 0;
785 if (data->is_BranchData()) {
786 not_taken = data->as_BranchData()->not_taken();
787 }
788
789 // scale the counts to be commensurate with invocation counts:
790 taken = method()->scale_count(taken);
791 not_taken = method()->scale_count(not_taken);
792
793 // Give up if too few (or too many, in which case the sum will overflow) counts to be meaningful.
794 // We also check that individual counters are positive first, overwise the sum can become positive.
795 if (taken < 0 || not_taken < 0 || taken + not_taken < 40) {
796 if (C->log() != NULL) {
797 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d'", iter().get_dest(), taken, not_taken);
798 }
799 return PROB_UNKNOWN;
800 }
801
802 // Compute frequency that we arrive here
803 float sum = taken + not_taken;
804 // Adjust, if this block is a cloned private block but the
805 // Jump counts are shared. Taken the private counts for
806 // just this path instead of the shared counts.
807 if( block()->count() > 0 )
808 sum = block()->count();
809 cnt = sum / FreqCountInvocations;
810
811 // Pin probability to sane limits
812 float prob;
813 if( !taken )
814 prob = (0+PROB_MIN) / 2;
824 "Bad frequency assignment in if");
825
826 if (C->log() != NULL) {
827 const char* prob_str = NULL;
828 if (prob >= PROB_MAX) prob_str = (prob == PROB_MAX) ? "max" : "always";
829 if (prob <= PROB_MIN) prob_str = (prob == PROB_MIN) ? "min" : "never";
830 char prob_str_buf[30];
831 if (prob_str == NULL) {
832 sprintf(prob_str_buf, "%g", prob);
833 prob_str = prob_str_buf;
834 }
835 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d' cnt='%g' prob='%s'",
836 iter().get_dest(), taken, not_taken, cnt, prob_str);
837 }
838 return prob;
839 }
840
841 //-----------------------------branch_prediction-------------------------------
842 float Parse::branch_prediction(float& cnt,
843 BoolTest::mask btest,
844 int target_bci) {
845 float prob = dynamic_branch_prediction(cnt);
846 // If prob is unknown, switch to static prediction
847 if (prob != PROB_UNKNOWN) return prob;
848
849 prob = PROB_FAIR; // Set default value
850 if (btest == BoolTest::eq) // Exactly equal test?
851 prob = PROB_STATIC_INFREQUENT; // Assume its relatively infrequent
852 else if (btest == BoolTest::ne)
853 prob = PROB_STATIC_FREQUENT; // Assume its relatively frequent
854
855 // If this is a conditional test guarding a backwards branch,
856 // assume its a loop-back edge. Make it a likely taken branch.
857 if (target_bci < bci()) {
858 if (is_osr_parse()) { // Could be a hot OSR'd loop; force deopt
859 // Since it's an OSR, we probably have profile data, but since
860 // branch_prediction returned PROB_UNKNOWN, the counts are too small.
861 // Let's make a special check here for completely zero counts.
862 ciMethodData* methodData = method()->method_data();
863 if (!methodData->is_empty()) {
864 ciProfileData* data = methodData->bci_to_data(bci());
865 // Only stop for truly zero counts, which mean an unknown part
915 method()->print_name(); tty->cr();
916 }
917 #endif
918 int bc_depth = - Bytecodes::depth(iter().cur_bc());
919 assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches");
920 DEBUG_ONLY(sync_jvms()); // argument(n) requires a synced jvms
921 assert(argument(0) != NULL, "must exist");
922 assert(bc_depth == 1 || argument(1) != NULL, "two must exist");
923 inc_sp(bc_depth);
924 return bc_depth;
925 }
926
927 //----------------------------------do_ifnull----------------------------------
928 void Parse::do_ifnull(BoolTest::mask btest, Node *c) {
929 int target_bci = iter().get_dest();
930
931 Block* branch_block = successor_for_bci(target_bci);
932 Block* next_block = successor_for_bci(iter().next_bci());
933
934 float cnt;
935 float prob = branch_prediction(cnt, btest, target_bci);
936 if (prob == PROB_UNKNOWN) {
937 // (An earlier version of do_ifnull omitted this trap for OSR methods.)
938 #ifndef PRODUCT
939 if (PrintOpto && Verbose)
940 tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
941 #endif
942 repush_if_args(); // to gather stats on loop
943 // We need to mark this branch as taken so that if we recompile we will
944 // see that it is possible. In the tiered system the interpreter doesn't
945 // do profiling and by the time we get to the lower tier from the interpreter
946 // the path may be cold again. Make sure it doesn't look untaken
947 profile_taken_branch(target_bci, !ProfileInterpreter);
948 uncommon_trap(Deoptimization::Reason_unreached,
949 Deoptimization::Action_reinterpret,
950 NULL, "cold");
951 if (C->eliminate_boxing()) {
952 // Mark the successor blocks as parsed
953 branch_block->next_path_num();
954 next_block->next_path_num();
955 }
996 if (C->eliminate_boxing()) {
997 // Mark the successor block as parsed
998 next_block->next_path_num();
999 }
1000 } else { // Path is live.
1001 // Update method data
1002 profile_not_taken_branch();
1003 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob,
1004 next_block, branch_block);
1005 }
1006 }
1007
1008 //------------------------------------do_if------------------------------------
1009 void Parse::do_if(BoolTest::mask btest, Node* c) {
1010 int target_bci = iter().get_dest();
1011
1012 Block* branch_block = successor_for_bci(target_bci);
1013 Block* next_block = successor_for_bci(iter().next_bci());
1014
1015 float cnt;
1016 float prob = branch_prediction(cnt, btest, target_bci);
1017 float untaken_prob = 1.0 - prob;
1018
1019 if (prob == PROB_UNKNOWN) {
1020 #ifndef PRODUCT
1021 if (PrintOpto && Verbose)
1022 tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
1023 #endif
1024 repush_if_args(); // to gather stats on loop
1025 // We need to mark this branch as taken so that if we recompile we will
1026 // see that it is possible. In the tiered system the interpreter doesn't
1027 // do profiling and by the time we get to the lower tier from the interpreter
1028 // the path may be cold again. Make sure it doesn't look untaken
1029 profile_taken_branch(target_bci, !ProfileInterpreter);
1030 uncommon_trap(Deoptimization::Reason_unreached,
1031 Deoptimization::Action_reinterpret,
1032 NULL, "cold");
1033 if (C->eliminate_boxing()) {
1034 // Mark the successor blocks as parsed
1035 branch_block->next_path_num();
1036 next_block->next_path_num();
|
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "ci/ciMethodData.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "compiler/compileLog.hpp"
30 #include "interpreter/linkResolver.hpp"
31 #include "memory/universe.inline.hpp"
32 #include "opto/addnode.hpp"
33 #include "opto/castnode.hpp"
34 #include "opto/convertnode.hpp"
35 #include "opto/divnode.hpp"
36 #include "opto/idealGraphPrinter.hpp"
37 #include "opto/matcher.hpp"
38 #include "opto/memnode.hpp"
39 #include "opto/mulnode.hpp"
40 #include "opto/opaquenode.hpp"
41 #include "opto/parse.hpp"
42 #include "opto/runtime.hpp"
43 #include "runtime/deoptimization.hpp"
44 #include "runtime/sharedRuntime.hpp"
45
46 extern int explicit_null_checks_inserted,
47 explicit_null_checks_elided;
48
49 //---------------------------------array_load----------------------------------
50 void Parse::array_load(BasicType elem_type) {
51 const Type* elem = Type::TOP;
52 Node* adr = array_addressing(elem_type, 0, &elem);
53 if (stopped()) return; // guaranteed null or range check
54 dec_sp(2); // Pop array and index
55 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(elem_type);
56 Node* ld = make_load(control(), adr, elem, elem_type, adr_type, MemNode::unordered);
57 push(ld);
58 }
59
60
747 assert(ret_addr->singleton(), "must be a constant (cloned jsr body)");
748
749 // Effect on jsr on stack
750 push(_gvn.makecon(ret_addr));
751
752 // Flow to the jsr.
753 merge(jsr_bci);
754 }
755
756 // Handle ret bytecode
757 void Parse::do_ret() {
758 // Find to whom we return.
759 assert(block()->num_successors() == 1, "a ret can only go one place now");
760 Block* target = block()->successor_at(0);
761 assert(!target->is_ready(), "our arrival must be expected");
762 profile_ret(target->flow()->start());
763 int pnum = target->next_path_num();
764 merge_common(target, pnum);
765 }
766
767 static bool has_injected_profile(BoolTest::mask btest, Node* test, int& taken, int& not_taken) {
768 ProfileBooleanNode* profile = NULL;
769
770 if (btest != BoolTest::eq && btest != BoolTest::ne) {
771 // Only ::eq and ::ne are supported for profile injection.
772 return false;
773 }
774
775 if (test->is_Cmp()) {
776 Node* n = test->in(1);
777 switch (n->Opcode()) {
778 case Op_ProfileBoolean:
779 profile = (ProfileBooleanNode*) n;
780 break;
781 case Op_AndI:
782 // Look for the following shape: AndI (ProfileBoolean) (ConI 1))
783 const TypeInt* t = NULL;
784 if (n->in(1)->Opcode() == Op_ProfileBoolean &&
785 n->in(2)->is_Con() &&
786 (t = n->in(2)->bottom_type()->isa_int()) != NULL &&
787 t->get_con() == 1) {
788 profile = (ProfileBooleanNode*) n->in(1);
789 }
790 break;
791 }
792 }
793 if (profile != NULL) {
794 int false_cnt = profile->false_count();
795 int true_cnt = profile->true_count();
796
797 // Counts matching depends on the actual test operation (::eq or ::ne).
798 taken = (btest == BoolTest::eq) ? false_cnt : true_cnt;
799 not_taken = (btest == BoolTest::eq) ? true_cnt : false_cnt;
800
801 profile->consume();
802 return true;
803 }
804 return false;
805 }
806 //--------------------------dynamic_branch_prediction--------------------------
807 // Try to gather dynamic branch prediction behavior. Return a probability
808 // of the branch being taken and set the "cnt" field. Returns a -1.0
809 // if we need to use static prediction for some reason.
810 float Parse::dynamic_branch_prediction(float &cnt, BoolTest::mask btest, Node* test) {
811 ResourceMark rm;
812
813 cnt = COUNT_UNKNOWN;
814
815 int taken = 0;
816 int not_taken = 0;
817
818 bool use_mdo = !has_injected_profile(btest, test, taken, not_taken);
819
820 if (use_mdo) {
821 // Use MethodData information if it is available
822 // FIXME: free the ProfileData structure
823 ciMethodData* methodData = method()->method_data();
824 if (!methodData->is_mature()) return PROB_UNKNOWN;
825 ciProfileData* data = methodData->bci_to_data(bci());
826 if (!data->is_JumpData()) return PROB_UNKNOWN;
827
828 // get taken and not taken values
829 taken = data->as_JumpData()->taken();
830 not_taken = 0;
831 if (data->is_BranchData()) {
832 not_taken = data->as_BranchData()->not_taken();
833 }
834
835 // scale the counts to be commensurate with invocation counts:
836 taken = method()->scale_count(taken);
837 not_taken = method()->scale_count(not_taken);
838 }
839
840 // Give up if too few (or too many, in which case the sum will overflow) counts to be meaningful.
841 // We also check that individual counters are positive first, otherwise the sum can become positive.
842 if (taken < 0 || not_taken < 0 || taken + not_taken < 40) {
843 if (C->log() != NULL) {
844 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d'", iter().get_dest(), taken, not_taken);
845 }
846 return PROB_UNKNOWN;
847 }
848
849 // Compute frequency that we arrive here
850 float sum = taken + not_taken;
851 // Adjust, if this block is a cloned private block but the
852 // Jump counts are shared. Taken the private counts for
853 // just this path instead of the shared counts.
854 if( block()->count() > 0 )
855 sum = block()->count();
856 cnt = sum / FreqCountInvocations;
857
858 // Pin probability to sane limits
859 float prob;
860 if( !taken )
861 prob = (0+PROB_MIN) / 2;
871 "Bad frequency assignment in if");
872
873 if (C->log() != NULL) {
874 const char* prob_str = NULL;
875 if (prob >= PROB_MAX) prob_str = (prob == PROB_MAX) ? "max" : "always";
876 if (prob <= PROB_MIN) prob_str = (prob == PROB_MIN) ? "min" : "never";
877 char prob_str_buf[30];
878 if (prob_str == NULL) {
879 sprintf(prob_str_buf, "%g", prob);
880 prob_str = prob_str_buf;
881 }
882 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d' cnt='%g' prob='%s'",
883 iter().get_dest(), taken, not_taken, cnt, prob_str);
884 }
885 return prob;
886 }
887
888 //-----------------------------branch_prediction-------------------------------
889 float Parse::branch_prediction(float& cnt,
890 BoolTest::mask btest,
891 int target_bci,
892 Node* test) {
893 float prob = dynamic_branch_prediction(cnt, btest, test);
894 // If prob is unknown, switch to static prediction
895 if (prob != PROB_UNKNOWN) return prob;
896
897 prob = PROB_FAIR; // Set default value
898 if (btest == BoolTest::eq) // Exactly equal test?
899 prob = PROB_STATIC_INFREQUENT; // Assume its relatively infrequent
900 else if (btest == BoolTest::ne)
901 prob = PROB_STATIC_FREQUENT; // Assume its relatively frequent
902
903 // If this is a conditional test guarding a backwards branch,
904 // assume its a loop-back edge. Make it a likely taken branch.
905 if (target_bci < bci()) {
906 if (is_osr_parse()) { // Could be a hot OSR'd loop; force deopt
907 // Since it's an OSR, we probably have profile data, but since
908 // branch_prediction returned PROB_UNKNOWN, the counts are too small.
909 // Let's make a special check here for completely zero counts.
910 ciMethodData* methodData = method()->method_data();
911 if (!methodData->is_empty()) {
912 ciProfileData* data = methodData->bci_to_data(bci());
913 // Only stop for truly zero counts, which mean an unknown part
963 method()->print_name(); tty->cr();
964 }
965 #endif
966 int bc_depth = - Bytecodes::depth(iter().cur_bc());
967 assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches");
968 DEBUG_ONLY(sync_jvms()); // argument(n) requires a synced jvms
969 assert(argument(0) != NULL, "must exist");
970 assert(bc_depth == 1 || argument(1) != NULL, "two must exist");
971 inc_sp(bc_depth);
972 return bc_depth;
973 }
974
975 //----------------------------------do_ifnull----------------------------------
976 void Parse::do_ifnull(BoolTest::mask btest, Node *c) {
977 int target_bci = iter().get_dest();
978
979 Block* branch_block = successor_for_bci(target_bci);
980 Block* next_block = successor_for_bci(iter().next_bci());
981
982 float cnt;
983 float prob = branch_prediction(cnt, btest, target_bci, c);
984 if (prob == PROB_UNKNOWN) {
985 // (An earlier version of do_ifnull omitted this trap for OSR methods.)
986 #ifndef PRODUCT
987 if (PrintOpto && Verbose)
988 tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
989 #endif
990 repush_if_args(); // to gather stats on loop
991 // We need to mark this branch as taken so that if we recompile we will
992 // see that it is possible. In the tiered system the interpreter doesn't
993 // do profiling and by the time we get to the lower tier from the interpreter
994 // the path may be cold again. Make sure it doesn't look untaken
995 profile_taken_branch(target_bci, !ProfileInterpreter);
996 uncommon_trap(Deoptimization::Reason_unreached,
997 Deoptimization::Action_reinterpret,
998 NULL, "cold");
999 if (C->eliminate_boxing()) {
1000 // Mark the successor blocks as parsed
1001 branch_block->next_path_num();
1002 next_block->next_path_num();
1003 }
1044 if (C->eliminate_boxing()) {
1045 // Mark the successor block as parsed
1046 next_block->next_path_num();
1047 }
1048 } else { // Path is live.
1049 // Update method data
1050 profile_not_taken_branch();
1051 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob,
1052 next_block, branch_block);
1053 }
1054 }
1055
1056 //------------------------------------do_if------------------------------------
1057 void Parse::do_if(BoolTest::mask btest, Node* c) {
1058 int target_bci = iter().get_dest();
1059
1060 Block* branch_block = successor_for_bci(target_bci);
1061 Block* next_block = successor_for_bci(iter().next_bci());
1062
1063 float cnt;
1064 float prob = branch_prediction(cnt, btest, target_bci, c);
1065 float untaken_prob = 1.0 - prob;
1066
1067 if (prob == PROB_UNKNOWN) {
1068 #ifndef PRODUCT
1069 if (PrintOpto && Verbose)
1070 tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
1071 #endif
1072 repush_if_args(); // to gather stats on loop
1073 // We need to mark this branch as taken so that if we recompile we will
1074 // see that it is possible. In the tiered system the interpreter doesn't
1075 // do profiling and by the time we get to the lower tier from the interpreter
1076 // the path may be cold again. Make sure it doesn't look untaken
1077 profile_taken_branch(target_bci, !ProfileInterpreter);
1078 uncommon_trap(Deoptimization::Reason_unreached,
1079 Deoptimization::Action_reinterpret,
1080 NULL, "cold");
1081 if (C->eliminate_boxing()) {
1082 // Mark the successor blocks as parsed
1083 branch_block->next_path_num();
1084 next_block->next_path_num();
|