1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
46 #include "opto/loopnode.hpp"
47 #include "opto/machnode.hpp"
48 #include "opto/macro.hpp"
49 #include "opto/matcher.hpp"
50 #include "opto/memnode.hpp"
51 #include "opto/mulnode.hpp"
52 #include "opto/node.hpp"
53 #include "opto/opcodes.hpp"
54 #include "opto/output.hpp"
55 #include "opto/parse.hpp"
56 #include "opto/phaseX.hpp"
57 #include "opto/rootnode.hpp"
58 #include "opto/runtime.hpp"
59 #include "opto/stringopts.hpp"
60 #include "opto/type.hpp"
61 #include "opto/vectornode.hpp"
62 #include "runtime/arguments.hpp"
63 #include "runtime/signature.hpp"
64 #include "runtime/stubRoutines.hpp"
65 #include "runtime/timer.hpp"
66 #include "utilities/copy.hpp"
67 #ifdef TARGET_ARCH_MODEL_x86_32
68 # include "adfiles/ad_x86_32.hpp"
69 #endif
70 #ifdef TARGET_ARCH_MODEL_x86_64
71 # include "adfiles/ad_x86_64.hpp"
72 #endif
73 #ifdef TARGET_ARCH_MODEL_sparc
74 # include "adfiles/ad_sparc.hpp"
75 #endif
76 #ifdef TARGET_ARCH_MODEL_zero
77 # include "adfiles/ad_zero.hpp"
78 #endif
79 #ifdef TARGET_ARCH_MODEL_arm
80 # include "adfiles/ad_arm.hpp"
81 #endif
82 #ifdef TARGET_ARCH_MODEL_ppc
83 # include "adfiles/ad_ppc.hpp"
84 #endif
85
769 // Accept return values, and transfer control we know not where.
770 // This is done by a special, unique ReturnNode bound to root.
771 return_values(kit.jvms());
772 }
773
774 if (kit.has_exceptions()) {
775 // Any exceptions that escape from this call must be rethrown
776 // to whatever caller is dynamically above us on the stack.
777 // This is done by a special, unique RethrowNode bound to root.
778 rethrow_exceptions(kit.transfer_exceptions_into_jvms());
779 }
780
781 assert(IncrementalInline || (_late_inlines.length() == 0 && !has_mh_late_inlines()), "incremental inlining is off");
782
783 if (_late_inlines.length() == 0 && !has_mh_late_inlines() && !failing() && has_stringbuilder()) {
784 inline_string_calls(true);
785 }
786
787 if (failing()) return;
788
789 print_method("Before RemoveUseless", 3);
790
791 // Remove clutter produced by parsing.
792 if (!failing()) {
793 ResourceMark rm;
794 PhaseRemoveUseless pru(initial_gvn(), &for_igvn);
795 }
796 }
797
798 // Note: Large methods are capped off in do_one_bytecode().
799 if (failing()) return;
800
801 // After parsing, node notes are no longer automagic.
802 // They must be propagated by register_new_node_with_optimizer(),
803 // clone(), or the like.
804 set_default_node_notes(NULL);
805
806 for (;;) {
807 int successes = Inline_Warm();
808 if (failing()) return;
809 if (successes == 0) break;
1784 void Compile::cleanup_loop_predicates(PhaseIterGVN &igvn) {
1785 if (predicate_count()==0) return;
1786 for (int i = predicate_count(); i > 0; i--) {
1787 Node * n = predicate_opaque1_node(i-1);
1788 assert(n->Opcode() == Op_Opaque1, "must be");
1789 igvn.replace_node(n, n->in(1));
1790 }
1791 assert(predicate_count()==0, "should be clean!");
1792 }
1793
1794 // StringOpts and late inlining of string methods
1795 void Compile::inline_string_calls(bool parse_time) {
1796 {
1797 // remove useless nodes to make the usage analysis simpler
1798 ResourceMark rm;
1799 PhaseRemoveUseless pru(initial_gvn(), for_igvn());
1800 }
1801
1802 {
1803 ResourceMark rm;
1804 print_method("Before StringOpts", 3);
1805 PhaseStringOpts pso(initial_gvn(), for_igvn());
1806 print_method("After StringOpts", 3);
1807 }
1808
1809 // now inline anything that we skipped the first time around
1810 if (!parse_time) {
1811 _late_inlines_pos = _late_inlines.length();
1812 }
1813
1814 while (_string_late_inlines.length() > 0) {
1815 CallGenerator* cg = _string_late_inlines.pop();
1816 cg->do_late_inline();
1817 if (failing()) return;
1818 }
1819 _string_late_inlines.trunc_to(0);
1820 }
1821
1822 // Late inlining of boxing methods
1823 void Compile::inline_boxing_calls(PhaseIterGVN& igvn) {
1824 if (_boxing_late_inlines.length() > 0) {
1825 assert(has_boxed_value(), "inconsistent");
1826
1941 }
1942
1943
1944 //------------------------------Optimize---------------------------------------
1945 // Given a graph, optimize it.
1946 void Compile::Optimize() {
1947 TracePhase t1("optimizer", &_t_optimizer, true);
1948
1949 #ifndef PRODUCT
1950 if (env()->break_at_compile()) {
1951 BREAKPOINT;
1952 }
1953
1954 #endif
1955
1956 ResourceMark rm;
1957 int loop_opts_cnt;
1958
1959 NOT_PRODUCT( verify_graph_edges(); )
1960
1961 print_method("After Parsing");
1962
1963 {
1964 // Iterative Global Value Numbering, including ideal transforms
1965 // Initialize IterGVN with types and values from parse-time GVN
1966 PhaseIterGVN igvn(initial_gvn());
1967 {
1968 NOT_PRODUCT( TracePhase t2("iterGVN", &_t_iterGVN, TimeCompiler); )
1969 igvn.optimize();
1970 }
1971
1972 print_method("Iter GVN 1", 2);
1973
1974 if (failing()) return;
1975
1976 {
1977 NOT_PRODUCT( TracePhase t2("incrementalInline", &_t_incrInline, TimeCompiler); )
1978 inline_incrementally(igvn);
1979 }
1980
1981 print_method("Incremental Inline", 2);
1982
1983 if (failing()) return;
1984
1985 if (eliminate_boxing()) {
1986 NOT_PRODUCT( TracePhase t2("incrementalInline", &_t_incrInline, TimeCompiler); )
1987 // Inline valueOf() methods now.
1988 inline_boxing_calls(igvn);
1989
1990 print_method("Incremental Boxing Inline", 2);
1991
1992 if (failing()) return;
1993 }
1994
1995 // No more new expensive nodes will be added to the list from here
1996 // so keep only the actual candidates for optimizations.
1997 cleanup_expensive_nodes(igvn);
1998
1999 // Perform escape analysis
2000 if (_do_escape_analysis && ConnectionGraph::has_candidates(this)) {
2001 if (has_loops()) {
2002 // Cleanup graph (remove dead nodes).
2003 TracePhase t2("idealLoop", &_t_idealLoop, true);
2004 PhaseIdealLoop ideal_loop( igvn, false, true );
2005 if (major_progress()) print_method("PhaseIdealLoop before EA", 2);
2006 if (failing()) return;
2007 }
2008 ConnectionGraph::do_analysis(this, &igvn);
2009
2010 if (failing()) return;
2011
2012 // Optimize out fields loads from scalar replaceable allocations.
2013 igvn.optimize();
2014 print_method("Iter GVN after EA", 2);
2015
2016 if (failing()) return;
2017
2018 if (congraph() != NULL && macro_count() > 0) {
2019 NOT_PRODUCT( TracePhase t2("macroEliminate", &_t_macroEliminate, TimeCompiler); )
2020 PhaseMacroExpand mexp(igvn);
2021 mexp.eliminate_macro_nodes();
2022 igvn.set_delay_transform(false);
2023
2024 igvn.optimize();
2025 print_method("Iter GVN after eliminating allocations and locks", 2);
2026
2027 if (failing()) return;
2028 }
2029 }
2030
2031 // Loop transforms on the ideal graph. Range Check Elimination,
2032 // peeling, unrolling, etc.
2033
2034 // Set loop opts counter
2035 loop_opts_cnt = num_loop_opts();
2036 if((loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
2037 {
2038 TracePhase t2("idealLoop", &_t_idealLoop, true);
2039 PhaseIdealLoop ideal_loop( igvn, true );
2040 loop_opts_cnt--;
2041 if (major_progress()) print_method("PhaseIdealLoop 1", 2);
2042 if (failing()) return;
2043 }
2044 // Loop opts pass if partial peeling occurred in previous pass
2045 if(PartialPeelLoop && major_progress() && (loop_opts_cnt > 0)) {
2046 TracePhase t3("idealLoop", &_t_idealLoop, true);
2047 PhaseIdealLoop ideal_loop( igvn, false );
2048 loop_opts_cnt--;
2049 if (major_progress()) print_method("PhaseIdealLoop 2", 2);
2050 if (failing()) return;
2051 }
2052 // Loop opts pass for loop-unrolling before CCP
2053 if(major_progress() && (loop_opts_cnt > 0)) {
2054 TracePhase t4("idealLoop", &_t_idealLoop, true);
2055 PhaseIdealLoop ideal_loop( igvn, false );
2056 loop_opts_cnt--;
2057 if (major_progress()) print_method("PhaseIdealLoop 3", 2);
2058 }
2059 if (!failing()) {
2060 // Verify that last round of loop opts produced a valid graph
2061 NOT_PRODUCT( TracePhase t2("idealLoopVerify", &_t_idealLoopVerify, TimeCompiler); )
2062 PhaseIdealLoop::verify(igvn);
2063 }
2064 }
2065 if (failing()) return;
2066
2067 // Conditional Constant Propagation;
2068 PhaseCCP ccp( &igvn );
2069 assert( true, "Break here to ccp.dump_nodes_and_types(_root,999,1)");
2070 {
2071 TracePhase t2("ccp", &_t_ccp, true);
2072 ccp.do_transform();
2073 }
2074 print_method("PhaseCPP 1", 2);
2075
2076 assert( true, "Break here to ccp.dump_old2new_map()");
2077
2078 // Iterative Global Value Numbering, including ideal transforms
2079 {
2080 NOT_PRODUCT( TracePhase t2("iterGVN2", &_t_iterGVN2, TimeCompiler); )
2081 igvn = ccp;
2082 igvn.optimize();
2083 }
2084
2085 print_method("Iter GVN 2", 2);
2086
2087 if (failing()) return;
2088
2089 // Loop transforms on the ideal graph. Range Check Elimination,
2090 // peeling, unrolling, etc.
2091 if(loop_opts_cnt > 0) {
2092 debug_only( int cnt = 0; );
2093 while(major_progress() && (loop_opts_cnt > 0)) {
2094 TracePhase t2("idealLoop", &_t_idealLoop, true);
2095 assert( cnt++ < 40, "infinite cycle in loop optimization" );
2096 PhaseIdealLoop ideal_loop( igvn, true);
2097 loop_opts_cnt--;
2098 if (major_progress()) print_method("PhaseIdealLoop iterations", 2);
2099 if (failing()) return;
2100 }
2101 }
2102
2103 {
2104 // Verify that all previous optimizations produced a valid graph
2105 // at least to this point, even if no loop optimizations were done.
2106 NOT_PRODUCT( TracePhase t2("idealLoopVerify", &_t_idealLoopVerify, TimeCompiler); )
2107 PhaseIdealLoop::verify(igvn);
2108 }
2109
2110 {
2111 NOT_PRODUCT( TracePhase t2("macroExpand", &_t_macroExpand, TimeCompiler); )
2112 PhaseMacroExpand mex(igvn);
2113 if (mex.expand_macro_nodes()) {
2114 assert(failing(), "must bail out w/ explicit message");
2115 return;
2116 }
2117 }
2118
2119 } // (End scope of igvn; run destructor if necessary for asserts.)
2120
2121 dump_inlining();
2122 // A method with only infinite loops has no edges entering loops from root
2123 {
2124 NOT_PRODUCT( TracePhase t2("graphReshape", &_t_graphReshaping, TimeCompiler); )
2125 if (final_graph_reshaping()) {
2126 assert(failing(), "must bail out w/ explicit message");
2127 return;
2128 }
2129 }
2130
2131 print_method("Optimize finished", 2);
2132 }
2133
2134
2135 //------------------------------Code_Gen---------------------------------------
2136 // Given a graph, generate code for it
2137 void Compile::Code_Gen() {
2138 if (failing()) return;
2139
2140 // Perform instruction selection. You might think we could reclaim Matcher
2141 // memory PDQ, but actually the Matcher is used in generating spill code.
2142 // Internals of the Matcher (including some VectorSets) must remain live
2143 // for awhile - thus I cannot reclaim Matcher memory lest a VectorSet usage
2144 // set a bit in reclaimed memory.
2145
2146 // In debug mode can dump m._nodes.dump() for mapping of ideal to machine
2147 // nodes. Mapping is only valid at the root of each matched subtree.
2148 NOT_PRODUCT( verify_graph_edges(); )
2149
2150 Node_List proj_list;
2151 Matcher m(proj_list);
2159 NOT_PRODUCT( verify_graph_edges(); )
2160
2161 // If you have too many nodes, or if matching has failed, bail out
2162 check_node_count(0, "out of nodes matching instructions");
2163 if (failing()) return;
2164
2165 // Build a proper-looking CFG
2166 PhaseCFG cfg(node_arena(), root(), m);
2167 _cfg = &cfg;
2168 {
2169 NOT_PRODUCT( TracePhase t2("scheduler", &_t_scheduler, TimeCompiler); )
2170 cfg.Dominators();
2171 if (failing()) return;
2172
2173 NOT_PRODUCT( verify_graph_edges(); )
2174
2175 cfg.Estimate_Block_Frequency();
2176 cfg.GlobalCodeMotion(m,unique(),proj_list);
2177 if (failing()) return;
2178
2179 print_method("Global code motion", 2);
2180
2181 NOT_PRODUCT( verify_graph_edges(); )
2182
2183 debug_only( cfg.verify(); )
2184 }
2185 NOT_PRODUCT( verify_graph_edges(); )
2186
2187 PhaseChaitin regalloc(unique(), cfg, m);
2188 _regalloc = ®alloc;
2189 {
2190 TracePhase t2("regalloc", &_t_registerAllocation, true);
2191 // Perform register allocation. After Chaitin, use-def chains are
2192 // no longer accurate (at spill code) and so must be ignored.
2193 // Node->LRG->reg mappings are still accurate.
2194 _regalloc->Register_Allocate();
2195
2196 // Bail out if the allocator builds too many nodes
2197 if (failing()) {
2198 return;
2199 }
2212 cfg.set_loop_alignment();
2213 }
2214 cfg.fixup_flow();
2215 }
2216
2217 // Apply peephole optimizations
2218 if( OptoPeephole ) {
2219 NOT_PRODUCT( TracePhase t2("peephole", &_t_peephole, TimeCompiler); )
2220 PhasePeephole peep( _regalloc, cfg);
2221 peep.do_transform();
2222 }
2223
2224 // Convert Nodes to instruction bits in a buffer
2225 {
2226 // %%%% workspace merge brought two timers together for one job
2227 TracePhase t2a("output", &_t_output, true);
2228 NOT_PRODUCT( TraceTime t2b(NULL, &_t_codeGeneration, TimeCompiler, false); )
2229 Output();
2230 }
2231
2232 print_method("Final Code");
2233
2234 // He's dead, Jim.
2235 _cfg = (PhaseCFG*)0xdeadbeef;
2236 _regalloc = (PhaseChaitin*)0xdeadbeef;
2237 }
2238
2239
2240 //------------------------------dump_asm---------------------------------------
2241 // Dump formatted assembly
2242 #ifndef PRODUCT
2243 void Compile::dump_asm(int *pcs, uint pc_limit) {
2244 bool cut_short = false;
2245 tty->print_cr("#");
2246 tty->print("# "); _tf->dump(); tty->cr();
2247 tty->print_cr("#");
2248
2249 // For all blocks
2250 int pc = 0x0; // Program counter
2251 char starts_bundle = ' ';
2252 _regalloc->dump_frame();
3299 }
3300 }
3301 }
3302 #endif
3303
3304 // The Compile object keeps track of failure reasons separately from the ciEnv.
3305 // This is required because there is not quite a 1-1 relation between the
3306 // ciEnv and its compilation task and the Compile object. Note that one
3307 // ciEnv might use two Compile objects, if C2Compiler::compile_method decides
3308 // to backtrack and retry without subsuming loads. Other than this backtracking
3309 // behavior, the Compile's failure reason is quietly copied up to the ciEnv
3310 // by the logic in C2Compiler.
3311 void Compile::record_failure(const char* reason) {
3312 if (log() != NULL) {
3313 log()->elem("failure reason='%s' phase='compile'", reason);
3314 }
3315 if (_failure_reason == NULL) {
3316 // Record the first failure reason.
3317 _failure_reason = reason;
3318 }
3319 if (!C->failure_reason_is(C2Compiler::retry_no_subsuming_loads())) {
3320 C->print_method(_failure_reason);
3321 }
3322 _root = NULL; // flush the graph, too
3323 }
3324
3325 Compile::TracePhase::TracePhase(const char* name, elapsedTimer* accumulator, bool dolog)
3326 : TraceTime(NULL, accumulator, false NOT_PRODUCT( || TimeCompiler ), false),
3327 _phase_name(name), _dolog(dolog)
3328 {
3329 if (dolog) {
3330 C = Compile::current();
3331 _log = C->log();
3332 } else {
3333 C = NULL;
3334 _log = NULL;
3335 }
3336 if (_log != NULL) {
3337 _log->begin_head("phase name='%s' nodes='%d' live='%d'", _phase_name, C->unique(), C->live_nodes());
3338 _log->stamp();
3339 _log->end_head();
3340 }
|
1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
46 #include "opto/loopnode.hpp"
47 #include "opto/machnode.hpp"
48 #include "opto/macro.hpp"
49 #include "opto/matcher.hpp"
50 #include "opto/memnode.hpp"
51 #include "opto/mulnode.hpp"
52 #include "opto/node.hpp"
53 #include "opto/opcodes.hpp"
54 #include "opto/output.hpp"
55 #include "opto/parse.hpp"
56 #include "opto/phaseX.hpp"
57 #include "opto/rootnode.hpp"
58 #include "opto/runtime.hpp"
59 #include "opto/stringopts.hpp"
60 #include "opto/type.hpp"
61 #include "opto/vectornode.hpp"
62 #include "runtime/arguments.hpp"
63 #include "runtime/signature.hpp"
64 #include "runtime/stubRoutines.hpp"
65 #include "runtime/timer.hpp"
66 #include "trace/tracing.hpp"
67 #include "utilities/copy.hpp"
68 #ifdef TARGET_ARCH_MODEL_x86_32
69 # include "adfiles/ad_x86_32.hpp"
70 #endif
71 #ifdef TARGET_ARCH_MODEL_x86_64
72 # include "adfiles/ad_x86_64.hpp"
73 #endif
74 #ifdef TARGET_ARCH_MODEL_sparc
75 # include "adfiles/ad_sparc.hpp"
76 #endif
77 #ifdef TARGET_ARCH_MODEL_zero
78 # include "adfiles/ad_zero.hpp"
79 #endif
80 #ifdef TARGET_ARCH_MODEL_arm
81 # include "adfiles/ad_arm.hpp"
82 #endif
83 #ifdef TARGET_ARCH_MODEL_ppc
84 # include "adfiles/ad_ppc.hpp"
85 #endif
86
770 // Accept return values, and transfer control we know not where.
771 // This is done by a special, unique ReturnNode bound to root.
772 return_values(kit.jvms());
773 }
774
775 if (kit.has_exceptions()) {
776 // Any exceptions that escape from this call must be rethrown
777 // to whatever caller is dynamically above us on the stack.
778 // This is done by a special, unique RethrowNode bound to root.
779 rethrow_exceptions(kit.transfer_exceptions_into_jvms());
780 }
781
782 assert(IncrementalInline || (_late_inlines.length() == 0 && !has_mh_late_inlines()), "incremental inlining is off");
783
784 if (_late_inlines.length() == 0 && !has_mh_late_inlines() && !failing() && has_stringbuilder()) {
785 inline_string_calls(true);
786 }
787
788 if (failing()) return;
789
790 print_method(PHASE_BEFORE_REMOVEUSELESS, 3);
791
792 // Remove clutter produced by parsing.
793 if (!failing()) {
794 ResourceMark rm;
795 PhaseRemoveUseless pru(initial_gvn(), &for_igvn);
796 }
797 }
798
799 // Note: Large methods are capped off in do_one_bytecode().
800 if (failing()) return;
801
802 // After parsing, node notes are no longer automagic.
803 // They must be propagated by register_new_node_with_optimizer(),
804 // clone(), or the like.
805 set_default_node_notes(NULL);
806
807 for (;;) {
808 int successes = Inline_Warm();
809 if (failing()) return;
810 if (successes == 0) break;
1785 void Compile::cleanup_loop_predicates(PhaseIterGVN &igvn) {
1786 if (predicate_count()==0) return;
1787 for (int i = predicate_count(); i > 0; i--) {
1788 Node * n = predicate_opaque1_node(i-1);
1789 assert(n->Opcode() == Op_Opaque1, "must be");
1790 igvn.replace_node(n, n->in(1));
1791 }
1792 assert(predicate_count()==0, "should be clean!");
1793 }
1794
1795 // StringOpts and late inlining of string methods
1796 void Compile::inline_string_calls(bool parse_time) {
1797 {
1798 // remove useless nodes to make the usage analysis simpler
1799 ResourceMark rm;
1800 PhaseRemoveUseless pru(initial_gvn(), for_igvn());
1801 }
1802
1803 {
1804 ResourceMark rm;
1805 print_method(PHASE_BEFORE_STRINGOPTS, 3);
1806 PhaseStringOpts pso(initial_gvn(), for_igvn());
1807 print_method(PHASE_AFTER_STRINGOPTS, 3);
1808 }
1809
1810 // now inline anything that we skipped the first time around
1811 if (!parse_time) {
1812 _late_inlines_pos = _late_inlines.length();
1813 }
1814
1815 while (_string_late_inlines.length() > 0) {
1816 CallGenerator* cg = _string_late_inlines.pop();
1817 cg->do_late_inline();
1818 if (failing()) return;
1819 }
1820 _string_late_inlines.trunc_to(0);
1821 }
1822
1823 // Late inlining of boxing methods
1824 void Compile::inline_boxing_calls(PhaseIterGVN& igvn) {
1825 if (_boxing_late_inlines.length() > 0) {
1826 assert(has_boxed_value(), "inconsistent");
1827
1942 }
1943
1944
1945 //------------------------------Optimize---------------------------------------
1946 // Given a graph, optimize it.
1947 void Compile::Optimize() {
1948 TracePhase t1("optimizer", &_t_optimizer, true);
1949
1950 #ifndef PRODUCT
1951 if (env()->break_at_compile()) {
1952 BREAKPOINT;
1953 }
1954
1955 #endif
1956
1957 ResourceMark rm;
1958 int loop_opts_cnt;
1959
1960 NOT_PRODUCT( verify_graph_edges(); )
1961
1962 print_method(PHASE_AFTER_PARSING);
1963
1964 {
1965 // Iterative Global Value Numbering, including ideal transforms
1966 // Initialize IterGVN with types and values from parse-time GVN
1967 PhaseIterGVN igvn(initial_gvn());
1968 {
1969 NOT_PRODUCT( TracePhase t2("iterGVN", &_t_iterGVN, TimeCompiler); )
1970 igvn.optimize();
1971 }
1972
1973 print_method(PHASE_ITER_GVN1, 2);
1974
1975 if (failing()) return;
1976
1977 {
1978 NOT_PRODUCT( TracePhase t2("incrementalInline", &_t_incrInline, TimeCompiler); )
1979 inline_incrementally(igvn);
1980 }
1981
1982 print_method(PHASE_INCREMENTAL_INLINE, 2);
1983
1984 if (failing()) return;
1985
1986 if (eliminate_boxing()) {
1987 NOT_PRODUCT( TracePhase t2("incrementalInline", &_t_incrInline, TimeCompiler); )
1988 // Inline valueOf() methods now.
1989 inline_boxing_calls(igvn);
1990
1991 print_method(PHASE_INCREMENTAL_BOXING_INLINE, 2);
1992
1993 if (failing()) return;
1994 }
1995
1996 // No more new expensive nodes will be added to the list from here
1997 // so keep only the actual candidates for optimizations.
1998 cleanup_expensive_nodes(igvn);
1999
2000 // Perform escape analysis
2001 if (_do_escape_analysis && ConnectionGraph::has_candidates(this)) {
2002 if (has_loops()) {
2003 // Cleanup graph (remove dead nodes).
2004 TracePhase t2("idealLoop", &_t_idealLoop, true);
2005 PhaseIdealLoop ideal_loop( igvn, false, true );
2006 if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2007 if (failing()) return;
2008 }
2009 ConnectionGraph::do_analysis(this, &igvn);
2010
2011 if (failing()) return;
2012
2013 // Optimize out fields loads from scalar replaceable allocations.
2014 igvn.optimize();
2015 print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2016
2017 if (failing()) return;
2018
2019 if (congraph() != NULL && macro_count() > 0) {
2020 NOT_PRODUCT( TracePhase t2("macroEliminate", &_t_macroEliminate, TimeCompiler); )
2021 PhaseMacroExpand mexp(igvn);
2022 mexp.eliminate_macro_nodes();
2023 igvn.set_delay_transform(false);
2024
2025 igvn.optimize();
2026 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
2027
2028 if (failing()) return;
2029 }
2030 }
2031
2032 // Loop transforms on the ideal graph. Range Check Elimination,
2033 // peeling, unrolling, etc.
2034
2035 // Set loop opts counter
2036 loop_opts_cnt = num_loop_opts();
2037 if((loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
2038 {
2039 TracePhase t2("idealLoop", &_t_idealLoop, true);
2040 PhaseIdealLoop ideal_loop( igvn, true );
2041 loop_opts_cnt--;
2042 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP1, 2);
2043 if (failing()) return;
2044 }
2045 // Loop opts pass if partial peeling occurred in previous pass
2046 if(PartialPeelLoop && major_progress() && (loop_opts_cnt > 0)) {
2047 TracePhase t3("idealLoop", &_t_idealLoop, true);
2048 PhaseIdealLoop ideal_loop( igvn, false );
2049 loop_opts_cnt--;
2050 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP2, 2);
2051 if (failing()) return;
2052 }
2053 // Loop opts pass for loop-unrolling before CCP
2054 if(major_progress() && (loop_opts_cnt > 0)) {
2055 TracePhase t4("idealLoop", &_t_idealLoop, true);
2056 PhaseIdealLoop ideal_loop( igvn, false );
2057 loop_opts_cnt--;
2058 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP3, 2);
2059 }
2060 if (!failing()) {
2061 // Verify that last round of loop opts produced a valid graph
2062 NOT_PRODUCT( TracePhase t2("idealLoopVerify", &_t_idealLoopVerify, TimeCompiler); )
2063 PhaseIdealLoop::verify(igvn);
2064 }
2065 }
2066 if (failing()) return;
2067
2068 // Conditional Constant Propagation;
2069 PhaseCCP ccp( &igvn );
2070 assert( true, "Break here to ccp.dump_nodes_and_types(_root,999,1)");
2071 {
2072 TracePhase t2("ccp", &_t_ccp, true);
2073 ccp.do_transform();
2074 }
2075 print_method(PHASE_CPP1, 2);
2076
2077 assert( true, "Break here to ccp.dump_old2new_map()");
2078
2079 // Iterative Global Value Numbering, including ideal transforms
2080 {
2081 NOT_PRODUCT( TracePhase t2("iterGVN2", &_t_iterGVN2, TimeCompiler); )
2082 igvn = ccp;
2083 igvn.optimize();
2084 }
2085
2086 print_method(PHASE_ITER_GVN2, 2);
2087
2088 if (failing()) return;
2089
2090 // Loop transforms on the ideal graph. Range Check Elimination,
2091 // peeling, unrolling, etc.
2092 if(loop_opts_cnt > 0) {
2093 debug_only( int cnt = 0; );
2094 while(major_progress() && (loop_opts_cnt > 0)) {
2095 TracePhase t2("idealLoop", &_t_idealLoop, true);
2096 assert( cnt++ < 40, "infinite cycle in loop optimization" );
2097 PhaseIdealLoop ideal_loop( igvn, true);
2098 loop_opts_cnt--;
2099 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2100 if (failing()) return;
2101 }
2102 }
2103
2104 {
2105 // Verify that all previous optimizations produced a valid graph
2106 // at least to this point, even if no loop optimizations were done.
2107 NOT_PRODUCT( TracePhase t2("idealLoopVerify", &_t_idealLoopVerify, TimeCompiler); )
2108 PhaseIdealLoop::verify(igvn);
2109 }
2110
2111 {
2112 NOT_PRODUCT( TracePhase t2("macroExpand", &_t_macroExpand, TimeCompiler); )
2113 PhaseMacroExpand mex(igvn);
2114 if (mex.expand_macro_nodes()) {
2115 assert(failing(), "must bail out w/ explicit message");
2116 return;
2117 }
2118 }
2119
2120 } // (End scope of igvn; run destructor if necessary for asserts.)
2121
2122 dump_inlining();
2123 // A method with only infinite loops has no edges entering loops from root
2124 {
2125 NOT_PRODUCT( TracePhase t2("graphReshape", &_t_graphReshaping, TimeCompiler); )
2126 if (final_graph_reshaping()) {
2127 assert(failing(), "must bail out w/ explicit message");
2128 return;
2129 }
2130 }
2131
2132 print_method(PHASE_OPTIMIZE_FINISHED, 2);
2133 }
2134
2135
2136 //------------------------------Code_Gen---------------------------------------
2137 // Given a graph, generate code for it
2138 void Compile::Code_Gen() {
2139 if (failing()) return;
2140
2141 // Perform instruction selection. You might think we could reclaim Matcher
2142 // memory PDQ, but actually the Matcher is used in generating spill code.
2143 // Internals of the Matcher (including some VectorSets) must remain live
2144 // for awhile - thus I cannot reclaim Matcher memory lest a VectorSet usage
2145 // set a bit in reclaimed memory.
2146
2147 // In debug mode can dump m._nodes.dump() for mapping of ideal to machine
2148 // nodes. Mapping is only valid at the root of each matched subtree.
2149 NOT_PRODUCT( verify_graph_edges(); )
2150
2151 Node_List proj_list;
2152 Matcher m(proj_list);
2160 NOT_PRODUCT( verify_graph_edges(); )
2161
2162 // If you have too many nodes, or if matching has failed, bail out
2163 check_node_count(0, "out of nodes matching instructions");
2164 if (failing()) return;
2165
2166 // Build a proper-looking CFG
2167 PhaseCFG cfg(node_arena(), root(), m);
2168 _cfg = &cfg;
2169 {
2170 NOT_PRODUCT( TracePhase t2("scheduler", &_t_scheduler, TimeCompiler); )
2171 cfg.Dominators();
2172 if (failing()) return;
2173
2174 NOT_PRODUCT( verify_graph_edges(); )
2175
2176 cfg.Estimate_Block_Frequency();
2177 cfg.GlobalCodeMotion(m,unique(),proj_list);
2178 if (failing()) return;
2179
2180 print_method(PHASE_GLOBAL_CODE_MOTION, 2);
2181
2182 NOT_PRODUCT( verify_graph_edges(); )
2183
2184 debug_only( cfg.verify(); )
2185 }
2186 NOT_PRODUCT( verify_graph_edges(); )
2187
2188 PhaseChaitin regalloc(unique(), cfg, m);
2189 _regalloc = ®alloc;
2190 {
2191 TracePhase t2("regalloc", &_t_registerAllocation, true);
2192 // Perform register allocation. After Chaitin, use-def chains are
2193 // no longer accurate (at spill code) and so must be ignored.
2194 // Node->LRG->reg mappings are still accurate.
2195 _regalloc->Register_Allocate();
2196
2197 // Bail out if the allocator builds too many nodes
2198 if (failing()) {
2199 return;
2200 }
2213 cfg.set_loop_alignment();
2214 }
2215 cfg.fixup_flow();
2216 }
2217
2218 // Apply peephole optimizations
2219 if( OptoPeephole ) {
2220 NOT_PRODUCT( TracePhase t2("peephole", &_t_peephole, TimeCompiler); )
2221 PhasePeephole peep( _regalloc, cfg);
2222 peep.do_transform();
2223 }
2224
2225 // Convert Nodes to instruction bits in a buffer
2226 {
2227 // %%%% workspace merge brought two timers together for one job
2228 TracePhase t2a("output", &_t_output, true);
2229 NOT_PRODUCT( TraceTime t2b(NULL, &_t_codeGeneration, TimeCompiler, false); )
2230 Output();
2231 }
2232
2233 print_method(PHASE_FINAL_CODE);
2234
2235 // He's dead, Jim.
2236 _cfg = (PhaseCFG*)0xdeadbeef;
2237 _regalloc = (PhaseChaitin*)0xdeadbeef;
2238 }
2239
2240
2241 //------------------------------dump_asm---------------------------------------
2242 // Dump formatted assembly
2243 #ifndef PRODUCT
2244 void Compile::dump_asm(int *pcs, uint pc_limit) {
2245 bool cut_short = false;
2246 tty->print_cr("#");
2247 tty->print("# "); _tf->dump(); tty->cr();
2248 tty->print_cr("#");
2249
2250 // For all blocks
2251 int pc = 0x0; // Program counter
2252 char starts_bundle = ' ';
2253 _regalloc->dump_frame();
3300 }
3301 }
3302 }
3303 #endif
3304
3305 // The Compile object keeps track of failure reasons separately from the ciEnv.
3306 // This is required because there is not quite a 1-1 relation between the
3307 // ciEnv and its compilation task and the Compile object. Note that one
3308 // ciEnv might use two Compile objects, if C2Compiler::compile_method decides
3309 // to backtrack and retry without subsuming loads. Other than this backtracking
3310 // behavior, the Compile's failure reason is quietly copied up to the ciEnv
3311 // by the logic in C2Compiler.
3312 void Compile::record_failure(const char* reason) {
3313 if (log() != NULL) {
3314 log()->elem("failure reason='%s' phase='compile'", reason);
3315 }
3316 if (_failure_reason == NULL) {
3317 // Record the first failure reason.
3318 _failure_reason = reason;
3319 }
3320
3321 EventCompilerFailure event;
3322 if (event.should_commit()) {
3323 event.set_compileID(Compile::compile_id());
3324 event.set_failure(reason);
3325 event.commit();
3326 }
3327
3328 if (!C->failure_reason_is(C2Compiler::retry_no_subsuming_loads())) {
3329 C->print_method(PHASE_FAILURE);
3330 }
3331 _root = NULL; // flush the graph, too
3332 }
3333
3334 Compile::TracePhase::TracePhase(const char* name, elapsedTimer* accumulator, bool dolog)
3335 : TraceTime(NULL, accumulator, false NOT_PRODUCT( || TimeCompiler ), false),
3336 _phase_name(name), _dolog(dolog)
3337 {
3338 if (dolog) {
3339 C = Compile::current();
3340 _log = C->log();
3341 } else {
3342 C = NULL;
3343 _log = NULL;
3344 }
3345 if (_log != NULL) {
3346 _log->begin_head("phase name='%s' nodes='%d' live='%d'", _phase_name, C->unique(), C->live_nodes());
3347 _log->stamp();
3348 _log->end_head();
3349 }
|