Thu, 24 May 2018 19:26:50 +0800
#7046 C2 supports long branch
Contributed-by: fujie
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@1 | 25 | /* |
aoqi@1 | 26 | * This file has been modified by Loongson Technology in 2015. These |
aoqi@1 | 27 | * modifications are Copyright (c) 2015 Loongson Technology, and are made |
aoqi@1 | 28 | * available on the same license terms set forth above. |
aoqi@1 | 29 | */ |
aoqi@1 | 30 | |
aoqi@0 | 31 | #include "precompiled.hpp" |
aoqi@0 | 32 | #include "compiler/oopMap.hpp" |
aoqi@0 | 33 | #include "opto/addnode.hpp" |
aoqi@0 | 34 | #include "opto/callnode.hpp" |
aoqi@0 | 35 | #include "opto/compile.hpp" |
aoqi@0 | 36 | #include "opto/machnode.hpp" |
aoqi@0 | 37 | #include "opto/matcher.hpp" |
aoqi@0 | 38 | #include "opto/phase.hpp" |
aoqi@0 | 39 | #include "opto/regalloc.hpp" |
aoqi@0 | 40 | #include "opto/rootnode.hpp" |
aoqi@0 | 41 | #ifdef TARGET_ARCH_x86 |
aoqi@0 | 42 | # include "vmreg_x86.inline.hpp" |
aoqi@0 | 43 | #endif |
aoqi@0 | 44 | #ifdef TARGET_ARCH_sparc |
aoqi@0 | 45 | # include "vmreg_sparc.inline.hpp" |
aoqi@0 | 46 | #endif |
aoqi@0 | 47 | #ifdef TARGET_ARCH_zero |
aoqi@0 | 48 | # include "vmreg_zero.inline.hpp" |
aoqi@0 | 49 | #endif |
aoqi@0 | 50 | #ifdef TARGET_ARCH_arm |
aoqi@0 | 51 | # include "vmreg_arm.inline.hpp" |
aoqi@0 | 52 | #endif |
aoqi@0 | 53 | #ifdef TARGET_ARCH_ppc |
aoqi@0 | 54 | # include "vmreg_ppc.inline.hpp" |
aoqi@0 | 55 | #endif |
aoqi@1 | 56 | #ifdef TARGET_ARCH_mips |
aoqi@1 | 57 | # include "vmreg_mips.inline.hpp" |
aoqi@1 | 58 | #endif |
aoqi@0 | 59 | |
aoqi@0 | 60 | // The functions in this file builds OopMaps after all scheduling is done. |
aoqi@0 | 61 | // |
aoqi@0 | 62 | // OopMaps contain a list of all registers and stack-slots containing oops (so |
aoqi@0 | 63 | // they can be updated by GC). OopMaps also contain a list of derived-pointer |
aoqi@0 | 64 | // base-pointer pairs. When the base is moved, the derived pointer moves to |
aoqi@0 | 65 | // follow it. Finally, any registers holding callee-save values are also |
aoqi@0 | 66 | // recorded. These might contain oops, but only the caller knows. |
aoqi@0 | 67 | // |
aoqi@0 | 68 | // BuildOopMaps implements a simple forward reaching-defs solution. At each |
aoqi@0 | 69 | // GC point we'll have the reaching-def Nodes. If the reaching Nodes are |
aoqi@0 | 70 | // typed as pointers (no offset), then they are oops. Pointers+offsets are |
aoqi@0 | 71 | // derived pointers, and bases can be found from them. Finally, we'll also |
aoqi@0 | 72 | // track reaching callee-save values. Note that a copy of a callee-save value |
aoqi@0 | 73 | // "kills" it's source, so that only 1 copy of a callee-save value is alive at |
aoqi@0 | 74 | // a time. |
aoqi@0 | 75 | // |
aoqi@0 | 76 | // We run a simple bitvector liveness pass to help trim out dead oops. Due to |
aoqi@0 | 77 | // irreducible loops, we can have a reaching def of an oop that only reaches |
aoqi@0 | 78 | // along one path and no way to know if it's valid or not on the other path. |
aoqi@0 | 79 | // The bitvectors are quite dense and the liveness pass is fast. |
aoqi@0 | 80 | // |
aoqi@0 | 81 | // At GC points, we consult this information to build OopMaps. All reaching |
aoqi@0 | 82 | // defs typed as oops are added to the OopMap. Only 1 instance of a |
aoqi@0 | 83 | // callee-save register can be recorded. For derived pointers, we'll have to |
aoqi@0 | 84 | // find and record the register holding the base. |
aoqi@0 | 85 | // |
aoqi@0 | 86 | // The reaching def's is a simple 1-pass worklist approach. I tried a clever |
aoqi@0 | 87 | // breadth-first approach but it was worse (showed O(n^2) in the |
aoqi@0 | 88 | // pick-next-block code). |
aoqi@0 | 89 | // |
aoqi@0 | 90 | // The relevant data is kept in a struct of arrays (it could just as well be |
aoqi@0 | 91 | // an array of structs, but the struct-of-arrays is generally a little more |
aoqi@0 | 92 | // efficient). The arrays are indexed by register number (including |
aoqi@0 | 93 | // stack-slots as registers) and so is bounded by 200 to 300 elements in |
aoqi@0 | 94 | // practice. One array will map to a reaching def Node (or NULL for |
aoqi@0 | 95 | // conflict/dead). The other array will map to a callee-saved register or |
aoqi@0 | 96 | // OptoReg::Bad for not-callee-saved. |
aoqi@0 | 97 | |
aoqi@0 | 98 | |
aoqi@0 | 99 | // Structure to pass around |
aoqi@0 | 100 | struct OopFlow : public ResourceObj { |
aoqi@0 | 101 | short *_callees; // Array mapping register to callee-saved |
aoqi@0 | 102 | Node **_defs; // array mapping register to reaching def |
aoqi@0 | 103 | // or NULL if dead/conflict |
aoqi@0 | 104 | // OopFlow structs, when not being actively modified, describe the _end_ of |
aoqi@0 | 105 | // this block. |
aoqi@0 | 106 | Block *_b; // Block for this struct |
aoqi@0 | 107 | OopFlow *_next; // Next free OopFlow |
aoqi@0 | 108 | // or NULL if dead/conflict |
aoqi@0 | 109 | Compile* C; |
aoqi@0 | 110 | |
aoqi@0 | 111 | OopFlow( short *callees, Node **defs, Compile* c ) : _callees(callees), _defs(defs), |
aoqi@0 | 112 | _b(NULL), _next(NULL), C(c) { } |
aoqi@0 | 113 | |
aoqi@0 | 114 | // Given reaching-defs for this block start, compute it for this block end |
aoqi@0 | 115 | void compute_reach( PhaseRegAlloc *regalloc, int max_reg, Dict *safehash ); |
aoqi@0 | 116 | |
aoqi@0 | 117 | // Merge these two OopFlows into the 'this' pointer. |
aoqi@0 | 118 | void merge( OopFlow *flow, int max_reg ); |
aoqi@0 | 119 | |
aoqi@0 | 120 | // Copy a 'flow' over an existing flow |
aoqi@0 | 121 | void clone( OopFlow *flow, int max_size); |
aoqi@0 | 122 | |
aoqi@0 | 123 | // Make a new OopFlow from scratch |
aoqi@0 | 124 | static OopFlow *make( Arena *A, int max_size, Compile* C ); |
aoqi@0 | 125 | |
aoqi@0 | 126 | // Build an oopmap from the current flow info |
aoqi@0 | 127 | OopMap *build_oop_map( Node *n, int max_reg, PhaseRegAlloc *regalloc, int* live ); |
aoqi@0 | 128 | }; |
aoqi@0 | 129 | |
aoqi@0 | 130 | // Given reaching-defs for this block start, compute it for this block end |
aoqi@0 | 131 | void OopFlow::compute_reach( PhaseRegAlloc *regalloc, int max_reg, Dict *safehash ) { |
aoqi@0 | 132 | |
aoqi@0 | 133 | for( uint i=0; i<_b->number_of_nodes(); i++ ) { |
aoqi@0 | 134 | Node *n = _b->get_node(i); |
aoqi@0 | 135 | |
aoqi@0 | 136 | if( n->jvms() ) { // Build an OopMap here? |
aoqi@0 | 137 | JVMState *jvms = n->jvms(); |
aoqi@0 | 138 | // no map needed for leaf calls |
aoqi@0 | 139 | if( n->is_MachSafePoint() && !n->is_MachCallLeaf() ) { |
aoqi@0 | 140 | int *live = (int*) (*safehash)[n]; |
aoqi@0 | 141 | assert( live, "must find live" ); |
aoqi@0 | 142 | n->as_MachSafePoint()->set_oop_map( build_oop_map(n,max_reg,regalloc, live) ); |
aoqi@0 | 143 | } |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | // Assign new reaching def's. |
aoqi@0 | 147 | // Note that I padded the _defs and _callees arrays so it's legal |
aoqi@0 | 148 | // to index at _defs[OptoReg::Bad]. |
aoqi@0 | 149 | OptoReg::Name first = regalloc->get_reg_first(n); |
aoqi@0 | 150 | OptoReg::Name second = regalloc->get_reg_second(n); |
aoqi@0 | 151 | _defs[first] = n; |
aoqi@0 | 152 | _defs[second] = n; |
aoqi@0 | 153 | |
aoqi@0 | 154 | // Pass callee-save info around copies |
aoqi@0 | 155 | int idx = n->is_Copy(); |
aoqi@0 | 156 | if( idx ) { // Copies move callee-save info |
aoqi@0 | 157 | OptoReg::Name old_first = regalloc->get_reg_first(n->in(idx)); |
aoqi@0 | 158 | OptoReg::Name old_second = regalloc->get_reg_second(n->in(idx)); |
aoqi@0 | 159 | int tmp_first = _callees[old_first]; |
aoqi@0 | 160 | int tmp_second = _callees[old_second]; |
aoqi@0 | 161 | _callees[old_first] = OptoReg::Bad; // callee-save is moved, dead in old location |
aoqi@0 | 162 | _callees[old_second] = OptoReg::Bad; |
aoqi@0 | 163 | _callees[first] = tmp_first; |
aoqi@0 | 164 | _callees[second] = tmp_second; |
aoqi@0 | 165 | } else if( n->is_Phi() ) { // Phis do not mod callee-saves |
aoqi@0 | 166 | assert( _callees[first] == _callees[regalloc->get_reg_first(n->in(1))], "" ); |
aoqi@0 | 167 | assert( _callees[second] == _callees[regalloc->get_reg_second(n->in(1))], "" ); |
aoqi@0 | 168 | assert( _callees[first] == _callees[regalloc->get_reg_first(n->in(n->req()-1))], "" ); |
aoqi@0 | 169 | assert( _callees[second] == _callees[regalloc->get_reg_second(n->in(n->req()-1))], "" ); |
aoqi@0 | 170 | } else { |
aoqi@0 | 171 | _callees[first] = OptoReg::Bad; // No longer holding a callee-save value |
aoqi@0 | 172 | _callees[second] = OptoReg::Bad; |
aoqi@0 | 173 | |
aoqi@0 | 174 | // Find base case for callee saves |
aoqi@0 | 175 | if( n->is_Proj() && n->in(0)->is_Start() ) { |
aoqi@0 | 176 | if( OptoReg::is_reg(first) && |
aoqi@0 | 177 | regalloc->_matcher.is_save_on_entry(first) ) |
aoqi@0 | 178 | _callees[first] = first; |
aoqi@0 | 179 | if( OptoReg::is_reg(second) && |
aoqi@0 | 180 | regalloc->_matcher.is_save_on_entry(second) ) |
aoqi@0 | 181 | _callees[second] = second; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | } |
aoqi@0 | 184 | } |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | // Merge the given flow into the 'this' flow |
aoqi@0 | 188 | void OopFlow::merge( OopFlow *flow, int max_reg ) { |
aoqi@0 | 189 | assert( _b == NULL, "merging into a happy flow" ); |
aoqi@0 | 190 | assert( flow->_b, "this flow is still alive" ); |
aoqi@0 | 191 | assert( flow != this, "no self flow" ); |
aoqi@0 | 192 | |
aoqi@0 | 193 | // Do the merge. If there are any differences, drop to 'bottom' which |
aoqi@0 | 194 | // is OptoReg::Bad or NULL depending. |
aoqi@0 | 195 | for( int i=0; i<max_reg; i++ ) { |
aoqi@0 | 196 | // Merge the callee-save's |
aoqi@0 | 197 | if( _callees[i] != flow->_callees[i] ) |
aoqi@0 | 198 | _callees[i] = OptoReg::Bad; |
aoqi@0 | 199 | // Merge the reaching defs |
aoqi@0 | 200 | if( _defs[i] != flow->_defs[i] ) |
aoqi@0 | 201 | _defs[i] = NULL; |
aoqi@0 | 202 | } |
aoqi@0 | 203 | |
aoqi@0 | 204 | } |
aoqi@0 | 205 | |
aoqi@0 | 206 | void OopFlow::clone( OopFlow *flow, int max_size ) { |
aoqi@0 | 207 | _b = flow->_b; |
aoqi@0 | 208 | memcpy( _callees, flow->_callees, sizeof(short)*max_size); |
aoqi@0 | 209 | memcpy( _defs , flow->_defs , sizeof(Node*)*max_size); |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | OopFlow *OopFlow::make( Arena *A, int max_size, Compile* C ) { |
aoqi@0 | 213 | short *callees = NEW_ARENA_ARRAY(A,short,max_size+1); |
aoqi@0 | 214 | Node **defs = NEW_ARENA_ARRAY(A,Node*,max_size+1); |
aoqi@0 | 215 | debug_only( memset(defs,0,(max_size+1)*sizeof(Node*)) ); |
aoqi@0 | 216 | OopFlow *flow = new (A) OopFlow(callees+1, defs+1, C); |
aoqi@0 | 217 | assert( &flow->_callees[OptoReg::Bad] == callees, "Ok to index at OptoReg::Bad" ); |
aoqi@0 | 218 | assert( &flow->_defs [OptoReg::Bad] == defs , "Ok to index at OptoReg::Bad" ); |
aoqi@0 | 219 | return flow; |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | static int get_live_bit( int *live, int reg ) { |
aoqi@0 | 223 | return live[reg>>LogBitsPerInt] & (1<<(reg&(BitsPerInt-1))); } |
aoqi@0 | 224 | static void set_live_bit( int *live, int reg ) { |
aoqi@0 | 225 | live[reg>>LogBitsPerInt] |= (1<<(reg&(BitsPerInt-1))); } |
aoqi@0 | 226 | static void clr_live_bit( int *live, int reg ) { |
aoqi@0 | 227 | live[reg>>LogBitsPerInt] &= ~(1<<(reg&(BitsPerInt-1))); } |
aoqi@0 | 228 | |
aoqi@0 | 229 | // Build an oopmap from the current flow info |
aoqi@0 | 230 | OopMap *OopFlow::build_oop_map( Node *n, int max_reg, PhaseRegAlloc *regalloc, int* live ) { |
aoqi@0 | 231 | int framesize = regalloc->_framesize; |
aoqi@0 | 232 | int max_inarg_slot = OptoReg::reg2stack(regalloc->_matcher._new_SP); |
aoqi@0 | 233 | debug_only( char *dup_check = NEW_RESOURCE_ARRAY(char,OptoReg::stack0()); |
aoqi@0 | 234 | memset(dup_check,0,OptoReg::stack0()) ); |
aoqi@0 | 235 | |
aoqi@0 | 236 | OopMap *omap = new OopMap( framesize, max_inarg_slot ); |
aoqi@0 | 237 | MachCallNode *mcall = n->is_MachCall() ? n->as_MachCall() : NULL; |
aoqi@0 | 238 | JVMState* jvms = n->jvms(); |
aoqi@0 | 239 | |
aoqi@0 | 240 | // For all registers do... |
aoqi@0 | 241 | for( int reg=0; reg<max_reg; reg++ ) { |
aoqi@0 | 242 | if( get_live_bit(live,reg) == 0 ) |
aoqi@0 | 243 | continue; // Ignore if not live |
aoqi@0 | 244 | |
aoqi@0 | 245 | // %%% C2 can use 2 OptoRegs when the physical register is only one 64bit |
aoqi@0 | 246 | // register in that case we'll get an non-concrete register for the second |
aoqi@0 | 247 | // half. We only need to tell the map the register once! |
aoqi@0 | 248 | // |
aoqi@0 | 249 | // However for the moment we disable this change and leave things as they |
aoqi@0 | 250 | // were. |
aoqi@0 | 251 | |
aoqi@0 | 252 | VMReg r = OptoReg::as_VMReg(OptoReg::Name(reg), framesize, max_inarg_slot); |
aoqi@0 | 253 | |
aoqi@0 | 254 | if (false && r->is_reg() && !r->is_concrete()) { |
aoqi@0 | 255 | continue; |
aoqi@0 | 256 | } |
aoqi@0 | 257 | |
aoqi@0 | 258 | // See if dead (no reaching def). |
aoqi@0 | 259 | Node *def = _defs[reg]; // Get reaching def |
aoqi@0 | 260 | assert( def, "since live better have reaching def" ); |
aoqi@0 | 261 | |
aoqi@0 | 262 | // Classify the reaching def as oop, derived, callee-save, dead, or other |
aoqi@0 | 263 | const Type *t = def->bottom_type(); |
aoqi@0 | 264 | if( t->isa_oop_ptr() ) { // Oop or derived? |
aoqi@0 | 265 | assert( !OptoReg::is_valid(_callees[reg]), "oop can't be callee save" ); |
aoqi@0 | 266 | #ifdef _LP64 |
aoqi@0 | 267 | // 64-bit pointers record oop-ishness on 2 aligned adjacent registers. |
aoqi@0 | 268 | // Make sure both are record from the same reaching def, but do not |
aoqi@0 | 269 | // put both into the oopmap. |
aoqi@0 | 270 | if( (reg&1) == 1 ) { // High half of oop-pair? |
aoqi@0 | 271 | assert( _defs[reg-1] == _defs[reg], "both halves from same reaching def" ); |
aoqi@0 | 272 | continue; // Do not record high parts in oopmap |
aoqi@0 | 273 | } |
aoqi@0 | 274 | #endif |
aoqi@0 | 275 | |
aoqi@0 | 276 | // Check for a legal reg name in the oopMap and bailout if it is not. |
aoqi@0 | 277 | if (!omap->legal_vm_reg_name(r)) { |
aoqi@0 | 278 | regalloc->C->record_method_not_compilable("illegal oopMap register name"); |
aoqi@0 | 279 | continue; |
aoqi@0 | 280 | } |
aoqi@0 | 281 | if( t->is_ptr()->_offset == 0 ) { // Not derived? |
aoqi@0 | 282 | if( mcall ) { |
aoqi@0 | 283 | // Outgoing argument GC mask responsibility belongs to the callee, |
aoqi@0 | 284 | // not the caller. Inspect the inputs to the call, to see if |
aoqi@0 | 285 | // this live-range is one of them. |
aoqi@0 | 286 | uint cnt = mcall->tf()->domain()->cnt(); |
aoqi@0 | 287 | uint j; |
aoqi@0 | 288 | for( j = TypeFunc::Parms; j < cnt; j++) |
aoqi@0 | 289 | if( mcall->in(j) == def ) |
aoqi@0 | 290 | break; // reaching def is an argument oop |
aoqi@0 | 291 | if( j < cnt ) // arg oops dont go in GC map |
aoqi@0 | 292 | continue; // Continue on to the next register |
aoqi@0 | 293 | } |
aoqi@0 | 294 | omap->set_oop(r); |
aoqi@0 | 295 | } else { // Else it's derived. |
aoqi@0 | 296 | // Find the base of the derived value. |
aoqi@0 | 297 | uint i; |
aoqi@0 | 298 | // Fast, common case, scan |
aoqi@0 | 299 | for( i = jvms->oopoff(); i < n->req(); i+=2 ) |
aoqi@0 | 300 | if( n->in(i) == def ) break; // Common case |
aoqi@0 | 301 | if( i == n->req() ) { // Missed, try a more generous scan |
aoqi@0 | 302 | // Scan again, but this time peek through copies |
aoqi@0 | 303 | for( i = jvms->oopoff(); i < n->req(); i+=2 ) { |
aoqi@0 | 304 | Node *m = n->in(i); // Get initial derived value |
aoqi@0 | 305 | while( 1 ) { |
aoqi@0 | 306 | Node *d = def; // Get initial reaching def |
aoqi@0 | 307 | while( 1 ) { // Follow copies of reaching def to end |
aoqi@0 | 308 | if( m == d ) goto found; // breaks 3 loops |
aoqi@0 | 309 | int idx = d->is_Copy(); |
aoqi@0 | 310 | if( !idx ) break; |
aoqi@0 | 311 | d = d->in(idx); // Link through copy |
aoqi@0 | 312 | } |
aoqi@0 | 313 | int idx = m->is_Copy(); |
aoqi@0 | 314 | if( !idx ) break; |
aoqi@0 | 315 | m = m->in(idx); |
aoqi@0 | 316 | } |
aoqi@0 | 317 | } |
aoqi@0 | 318 | guarantee( 0, "must find derived/base pair" ); |
aoqi@0 | 319 | } |
aoqi@0 | 320 | found: ; |
aoqi@0 | 321 | Node *base = n->in(i+1); // Base is other half of pair |
aoqi@0 | 322 | int breg = regalloc->get_reg_first(base); |
aoqi@0 | 323 | VMReg b = OptoReg::as_VMReg(OptoReg::Name(breg), framesize, max_inarg_slot); |
aoqi@0 | 324 | |
aoqi@0 | 325 | // I record liveness at safepoints BEFORE I make the inputs |
aoqi@0 | 326 | // live. This is because argument oops are NOT live at a |
aoqi@0 | 327 | // safepoint (or at least they cannot appear in the oopmap). |
aoqi@0 | 328 | // Thus bases of base/derived pairs might not be in the |
aoqi@0 | 329 | // liveness data but they need to appear in the oopmap. |
aoqi@0 | 330 | if( get_live_bit(live,breg) == 0 ) {// Not live? |
aoqi@0 | 331 | // Flag it, so next derived pointer won't re-insert into oopmap |
aoqi@0 | 332 | set_live_bit(live,breg); |
aoqi@0 | 333 | // Already missed our turn? |
aoqi@0 | 334 | if( breg < reg ) { |
aoqi@0 | 335 | if (b->is_stack() || b->is_concrete() || true ) { |
aoqi@0 | 336 | omap->set_oop( b); |
aoqi@0 | 337 | } |
aoqi@0 | 338 | } |
aoqi@0 | 339 | } |
aoqi@0 | 340 | if (b->is_stack() || b->is_concrete() || true ) { |
aoqi@0 | 341 | omap->set_derived_oop( r, b); |
aoqi@0 | 342 | } |
aoqi@0 | 343 | } |
aoqi@0 | 344 | |
aoqi@0 | 345 | } else if( t->isa_narrowoop() ) { |
aoqi@0 | 346 | assert( !OptoReg::is_valid(_callees[reg]), "oop can't be callee save" ); |
aoqi@0 | 347 | // Check for a legal reg name in the oopMap and bailout if it is not. |
aoqi@0 | 348 | if (!omap->legal_vm_reg_name(r)) { |
aoqi@0 | 349 | regalloc->C->record_method_not_compilable("illegal oopMap register name"); |
aoqi@0 | 350 | continue; |
aoqi@0 | 351 | } |
aoqi@0 | 352 | if( mcall ) { |
aoqi@0 | 353 | // Outgoing argument GC mask responsibility belongs to the callee, |
aoqi@0 | 354 | // not the caller. Inspect the inputs to the call, to see if |
aoqi@0 | 355 | // this live-range is one of them. |
aoqi@0 | 356 | uint cnt = mcall->tf()->domain()->cnt(); |
aoqi@0 | 357 | uint j; |
aoqi@0 | 358 | for( j = TypeFunc::Parms; j < cnt; j++) |
aoqi@0 | 359 | if( mcall->in(j) == def ) |
aoqi@0 | 360 | break; // reaching def is an argument oop |
aoqi@0 | 361 | if( j < cnt ) // arg oops dont go in GC map |
aoqi@0 | 362 | continue; // Continue on to the next register |
aoqi@0 | 363 | } |
aoqi@0 | 364 | omap->set_narrowoop(r); |
aoqi@0 | 365 | } else if( OptoReg::is_valid(_callees[reg])) { // callee-save? |
aoqi@0 | 366 | // It's a callee-save value |
aoqi@0 | 367 | assert( dup_check[_callees[reg]]==0, "trying to callee save same reg twice" ); |
aoqi@0 | 368 | debug_only( dup_check[_callees[reg]]=1; ) |
aoqi@0 | 369 | VMReg callee = OptoReg::as_VMReg(OptoReg::Name(_callees[reg])); |
aoqi@0 | 370 | if ( callee->is_concrete() || true ) { |
aoqi@0 | 371 | omap->set_callee_saved( r, callee); |
aoqi@0 | 372 | } |
aoqi@0 | 373 | |
aoqi@0 | 374 | } else { |
aoqi@0 | 375 | // Other - some reaching non-oop value |
aoqi@0 | 376 | omap->set_value( r); |
aoqi@0 | 377 | #ifdef ASSERT |
aoqi@0 | 378 | if( t->isa_rawptr() && C->cfg()->_raw_oops.member(def) ) { |
aoqi@0 | 379 | def->dump(); |
aoqi@0 | 380 | n->dump(); |
aoqi@0 | 381 | assert(false, "there should be a oop in OopMap instead of a live raw oop at safepoint"); |
aoqi@0 | 382 | } |
aoqi@0 | 383 | #endif |
aoqi@0 | 384 | } |
aoqi@0 | 385 | |
aoqi@0 | 386 | } |
aoqi@0 | 387 | |
aoqi@0 | 388 | #ifdef ASSERT |
aoqi@0 | 389 | /* Nice, Intel-only assert |
aoqi@0 | 390 | int cnt_callee_saves=0; |
aoqi@0 | 391 | int reg2 = 0; |
aoqi@0 | 392 | while (OptoReg::is_reg(reg2)) { |
aoqi@0 | 393 | if( dup_check[reg2] != 0) cnt_callee_saves++; |
aoqi@0 | 394 | assert( cnt_callee_saves==3 || cnt_callee_saves==5, "missed some callee-save" ); |
aoqi@0 | 395 | reg2++; |
aoqi@0 | 396 | } |
aoqi@0 | 397 | */ |
aoqi@0 | 398 | #endif |
aoqi@0 | 399 | |
aoqi@0 | 400 | #ifdef ASSERT |
aoqi@0 | 401 | for( OopMapStream oms1(omap, OopMapValue::derived_oop_value); !oms1.is_done(); oms1.next()) { |
aoqi@0 | 402 | OopMapValue omv1 = oms1.current(); |
aoqi@0 | 403 | bool found = false; |
aoqi@0 | 404 | for( OopMapStream oms2(omap,OopMapValue::oop_value); !oms2.is_done(); oms2.next()) { |
aoqi@0 | 405 | if( omv1.content_reg() == oms2.current().reg() ) { |
aoqi@0 | 406 | found = true; |
aoqi@0 | 407 | break; |
aoqi@0 | 408 | } |
aoqi@0 | 409 | } |
aoqi@0 | 410 | assert( found, "derived with no base in oopmap" ); |
aoqi@0 | 411 | } |
aoqi@0 | 412 | #endif |
aoqi@0 | 413 | |
aoqi@0 | 414 | return omap; |
aoqi@0 | 415 | } |
aoqi@0 | 416 | |
aoqi@0 | 417 | // Compute backwards liveness on registers |
aoqi@0 | 418 | static void do_liveness(PhaseRegAlloc* regalloc, PhaseCFG* cfg, Block_List* worklist, int max_reg_ints, Arena* A, Dict* safehash) { |
aoqi@0 | 419 | int* live = NEW_ARENA_ARRAY(A, int, (cfg->number_of_blocks() + 1) * max_reg_ints); |
aoqi@0 | 420 | int* tmp_live = &live[cfg->number_of_blocks() * max_reg_ints]; |
aoqi@0 | 421 | Node* root = cfg->get_root_node(); |
aoqi@0 | 422 | // On CISC platforms, get the node representing the stack pointer that regalloc |
aoqi@0 | 423 | // used for spills |
aoqi@0 | 424 | Node *fp = NodeSentinel; |
aoqi@0 | 425 | if (UseCISCSpill && root->req() > 1) { |
aoqi@0 | 426 | fp = root->in(1)->in(TypeFunc::FramePtr); |
aoqi@0 | 427 | } |
aoqi@0 | 428 | memset(live, 0, cfg->number_of_blocks() * (max_reg_ints << LogBytesPerInt)); |
aoqi@0 | 429 | // Push preds onto worklist |
aoqi@0 | 430 | for (uint i = 1; i < root->req(); i++) { |
aoqi@0 | 431 | Block* block = cfg->get_block_for_node(root->in(i)); |
aoqi@0 | 432 | worklist->push(block); |
aoqi@0 | 433 | } |
aoqi@0 | 434 | |
aoqi@0 | 435 | // ZKM.jar includes tiny infinite loops which are unreached from below. |
aoqi@0 | 436 | // If we missed any blocks, we'll retry here after pushing all missed |
aoqi@0 | 437 | // blocks on the worklist. Normally this outer loop never trips more |
aoqi@0 | 438 | // than once. |
aoqi@0 | 439 | while (1) { |
aoqi@0 | 440 | |
aoqi@0 | 441 | while( worklist->size() ) { // Standard worklist algorithm |
aoqi@0 | 442 | Block *b = worklist->rpop(); |
aoqi@0 | 443 | |
aoqi@0 | 444 | // Copy first successor into my tmp_live space |
aoqi@0 | 445 | int s0num = b->_succs[0]->_pre_order; |
aoqi@0 | 446 | int *t = &live[s0num*max_reg_ints]; |
aoqi@0 | 447 | for( int i=0; i<max_reg_ints; i++ ) |
aoqi@0 | 448 | tmp_live[i] = t[i]; |
aoqi@0 | 449 | |
aoqi@0 | 450 | // OR in the remaining live registers |
aoqi@0 | 451 | for( uint j=1; j<b->_num_succs; j++ ) { |
aoqi@0 | 452 | uint sjnum = b->_succs[j]->_pre_order; |
aoqi@0 | 453 | int *t = &live[sjnum*max_reg_ints]; |
aoqi@0 | 454 | for( int i=0; i<max_reg_ints; i++ ) |
aoqi@0 | 455 | tmp_live[i] |= t[i]; |
aoqi@0 | 456 | } |
aoqi@0 | 457 | |
aoqi@0 | 458 | // Now walk tmp_live up the block backwards, computing live |
aoqi@0 | 459 | for( int k=b->number_of_nodes()-1; k>=0; k-- ) { |
aoqi@0 | 460 | Node *n = b->get_node(k); |
aoqi@0 | 461 | // KILL def'd bits |
aoqi@0 | 462 | int first = regalloc->get_reg_first(n); |
aoqi@0 | 463 | int second = regalloc->get_reg_second(n); |
aoqi@0 | 464 | if( OptoReg::is_valid(first) ) clr_live_bit(tmp_live,first); |
aoqi@0 | 465 | if( OptoReg::is_valid(second) ) clr_live_bit(tmp_live,second); |
aoqi@0 | 466 | |
aoqi@0 | 467 | MachNode *m = n->is_Mach() ? n->as_Mach() : NULL; |
aoqi@0 | 468 | |
aoqi@0 | 469 | // Check if m is potentially a CISC alternate instruction (i.e, possibly |
aoqi@0 | 470 | // synthesized by RegAlloc from a conventional instruction and a |
aoqi@0 | 471 | // spilled input) |
aoqi@0 | 472 | bool is_cisc_alternate = false; |
aoqi@0 | 473 | if (UseCISCSpill && m) { |
aoqi@0 | 474 | is_cisc_alternate = m->is_cisc_alternate(); |
aoqi@0 | 475 | } |
aoqi@0 | 476 | |
aoqi@0 | 477 | // GEN use'd bits |
aoqi@0 | 478 | for( uint l=1; l<n->req(); l++ ) { |
aoqi@0 | 479 | Node *def = n->in(l); |
aoqi@0 | 480 | assert(def != 0, "input edge required"); |
aoqi@0 | 481 | int first = regalloc->get_reg_first(def); |
aoqi@0 | 482 | int second = regalloc->get_reg_second(def); |
aoqi@0 | 483 | if( OptoReg::is_valid(first) ) set_live_bit(tmp_live,first); |
aoqi@0 | 484 | if( OptoReg::is_valid(second) ) set_live_bit(tmp_live,second); |
aoqi@0 | 485 | // If we use the stack pointer in a cisc-alternative instruction, |
aoqi@0 | 486 | // check for use as a memory operand. Then reconstruct the RegName |
aoqi@0 | 487 | // for this stack location, and set the appropriate bit in the |
aoqi@0 | 488 | // live vector 4987749. |
aoqi@0 | 489 | if (is_cisc_alternate && def == fp) { |
aoqi@0 | 490 | const TypePtr *adr_type = NULL; |
aoqi@0 | 491 | intptr_t offset; |
aoqi@0 | 492 | const Node* base = m->get_base_and_disp(offset, adr_type); |
aoqi@0 | 493 | if (base == NodeSentinel) { |
aoqi@0 | 494 | // Machnode has multiple memory inputs. We are unable to reason |
aoqi@0 | 495 | // with these, but are presuming (with trepidation) that not any of |
aoqi@0 | 496 | // them are oops. This can be fixed by making get_base_and_disp() |
aoqi@0 | 497 | // look at a specific input instead of all inputs. |
aoqi@0 | 498 | assert(!def->bottom_type()->isa_oop_ptr(), "expecting non-oop mem input"); |
aoqi@0 | 499 | } else if (base != fp || offset == Type::OffsetBot) { |
aoqi@0 | 500 | // Do nothing: the fp operand is either not from a memory use |
aoqi@0 | 501 | // (base == NULL) OR the fp is used in a non-memory context |
aoqi@0 | 502 | // (base is some other register) OR the offset is not constant, |
aoqi@0 | 503 | // so it is not a stack slot. |
aoqi@0 | 504 | } else { |
aoqi@0 | 505 | assert(offset >= 0, "unexpected negative offset"); |
aoqi@0 | 506 | offset -= (offset % jintSize); // count the whole word |
aoqi@0 | 507 | int stack_reg = regalloc->offset2reg(offset); |
aoqi@0 | 508 | if (OptoReg::is_stack(stack_reg)) { |
aoqi@0 | 509 | set_live_bit(tmp_live, stack_reg); |
aoqi@0 | 510 | } else { |
aoqi@0 | 511 | assert(false, "stack_reg not on stack?"); |
aoqi@0 | 512 | } |
aoqi@0 | 513 | } |
aoqi@0 | 514 | } |
aoqi@0 | 515 | } |
aoqi@0 | 516 | |
aoqi@0 | 517 | if( n->jvms() ) { // Record liveness at safepoint |
aoqi@0 | 518 | |
aoqi@0 | 519 | // This placement of this stanza means inputs to calls are |
aoqi@0 | 520 | // considered live at the callsite's OopMap. Argument oops are |
aoqi@0 | 521 | // hence live, but NOT included in the oopmap. See cutout in |
aoqi@0 | 522 | // build_oop_map. Debug oops are live (and in OopMap). |
aoqi@0 | 523 | int *n_live = NEW_ARENA_ARRAY(A, int, max_reg_ints); |
aoqi@0 | 524 | for( int l=0; l<max_reg_ints; l++ ) |
aoqi@0 | 525 | n_live[l] = tmp_live[l]; |
aoqi@0 | 526 | safehash->Insert(n,n_live); |
aoqi@0 | 527 | } |
aoqi@0 | 528 | |
aoqi@0 | 529 | } |
aoqi@0 | 530 | |
aoqi@0 | 531 | // Now at block top, see if we have any changes. If so, propagate |
aoqi@0 | 532 | // to prior blocks. |
aoqi@0 | 533 | int *old_live = &live[b->_pre_order*max_reg_ints]; |
aoqi@0 | 534 | int l; |
aoqi@0 | 535 | for( l=0; l<max_reg_ints; l++ ) |
aoqi@0 | 536 | if( tmp_live[l] != old_live[l] ) |
aoqi@0 | 537 | break; |
aoqi@0 | 538 | if( l<max_reg_ints ) { // Change! |
aoqi@0 | 539 | // Copy in new value |
aoqi@0 | 540 | for( l=0; l<max_reg_ints; l++ ) |
aoqi@0 | 541 | old_live[l] = tmp_live[l]; |
aoqi@0 | 542 | // Push preds onto worklist |
aoqi@0 | 543 | for (l = 1; l < (int)b->num_preds(); l++) { |
aoqi@0 | 544 | Block* block = cfg->get_block_for_node(b->pred(l)); |
aoqi@0 | 545 | worklist->push(block); |
aoqi@0 | 546 | } |
aoqi@0 | 547 | } |
aoqi@0 | 548 | } |
aoqi@0 | 549 | |
aoqi@0 | 550 | // Scan for any missing safepoints. Happens to infinite loops |
aoqi@0 | 551 | // ala ZKM.jar |
aoqi@0 | 552 | uint i; |
aoqi@0 | 553 | for (i = 1; i < cfg->number_of_blocks(); i++) { |
aoqi@0 | 554 | Block* block = cfg->get_block(i); |
aoqi@0 | 555 | uint j; |
aoqi@0 | 556 | for (j = 1; j < block->number_of_nodes(); j++) { |
aoqi@0 | 557 | if (block->get_node(j)->jvms() && (*safehash)[block->get_node(j)] == NULL) { |
aoqi@0 | 558 | break; |
aoqi@0 | 559 | } |
aoqi@0 | 560 | } |
aoqi@0 | 561 | if (j < block->number_of_nodes()) { |
aoqi@0 | 562 | break; |
aoqi@0 | 563 | } |
aoqi@0 | 564 | } |
aoqi@0 | 565 | if (i == cfg->number_of_blocks()) { |
aoqi@0 | 566 | break; // Got 'em all |
aoqi@0 | 567 | } |
aoqi@0 | 568 | #ifndef PRODUCT |
aoqi@0 | 569 | if( PrintOpto && Verbose ) |
aoqi@0 | 570 | tty->print_cr("retripping live calc"); |
aoqi@0 | 571 | #endif |
aoqi@0 | 572 | // Force the issue (expensively): recheck everybody |
aoqi@0 | 573 | for (i = 1; i < cfg->number_of_blocks(); i++) { |
aoqi@0 | 574 | worklist->push(cfg->get_block(i)); |
aoqi@0 | 575 | } |
aoqi@0 | 576 | } |
aoqi@0 | 577 | } |
aoqi@0 | 578 | |
aoqi@0 | 579 | // Collect GC mask info - where are all the OOPs? |
aoqi@0 | 580 | void Compile::BuildOopMaps() { |
aoqi@0 | 581 | NOT_PRODUCT( TracePhase t3("bldOopMaps", &_t_buildOopMaps, TimeCompiler); ) |
aoqi@0 | 582 | // Can't resource-mark because I need to leave all those OopMaps around, |
aoqi@0 | 583 | // or else I need to resource-mark some arena other than the default. |
aoqi@0 | 584 | // ResourceMark rm; // Reclaim all OopFlows when done |
aoqi@0 | 585 | int max_reg = _regalloc->_max_reg; // Current array extent |
aoqi@0 | 586 | |
aoqi@0 | 587 | Arena *A = Thread::current()->resource_area(); |
aoqi@0 | 588 | Block_List worklist; // Worklist of pending blocks |
aoqi@0 | 589 | |
aoqi@0 | 590 | int max_reg_ints = round_to(max_reg, BitsPerInt)>>LogBitsPerInt; |
aoqi@0 | 591 | Dict *safehash = NULL; // Used for assert only |
aoqi@0 | 592 | // Compute a backwards liveness per register. Needs a bitarray of |
aoqi@0 | 593 | // #blocks x (#registers, rounded up to ints) |
aoqi@0 | 594 | safehash = new Dict(cmpkey,hashkey,A); |
aoqi@0 | 595 | do_liveness( _regalloc, _cfg, &worklist, max_reg_ints, A, safehash ); |
aoqi@0 | 596 | OopFlow *free_list = NULL; // Free, unused |
aoqi@0 | 597 | |
aoqi@0 | 598 | // Array mapping blocks to completed oopflows |
aoqi@0 | 599 | OopFlow **flows = NEW_ARENA_ARRAY(A, OopFlow*, _cfg->number_of_blocks()); |
aoqi@0 | 600 | memset( flows, 0, _cfg->number_of_blocks() * sizeof(OopFlow*) ); |
aoqi@0 | 601 | |
aoqi@0 | 602 | |
aoqi@0 | 603 | // Do the first block 'by hand' to prime the worklist |
aoqi@0 | 604 | Block *entry = _cfg->get_block(1); |
aoqi@0 | 605 | OopFlow *rootflow = OopFlow::make(A,max_reg,this); |
aoqi@0 | 606 | // Initialize to 'bottom' (not 'top') |
aoqi@0 | 607 | memset( rootflow->_callees, OptoReg::Bad, max_reg*sizeof(short) ); |
aoqi@0 | 608 | memset( rootflow->_defs , 0, max_reg*sizeof(Node*) ); |
aoqi@0 | 609 | flows[entry->_pre_order] = rootflow; |
aoqi@0 | 610 | |
aoqi@0 | 611 | // Do the first block 'by hand' to prime the worklist |
aoqi@0 | 612 | rootflow->_b = entry; |
aoqi@0 | 613 | rootflow->compute_reach( _regalloc, max_reg, safehash ); |
aoqi@0 | 614 | for( uint i=0; i<entry->_num_succs; i++ ) |
aoqi@0 | 615 | worklist.push(entry->_succs[i]); |
aoqi@0 | 616 | |
aoqi@0 | 617 | // Now worklist contains blocks which have some, but perhaps not all, |
aoqi@0 | 618 | // predecessors visited. |
aoqi@0 | 619 | while( worklist.size() ) { |
aoqi@0 | 620 | // Scan for a block with all predecessors visited, or any randoms slob |
aoqi@0 | 621 | // otherwise. All-preds-visited order allows me to recycle OopFlow |
aoqi@0 | 622 | // structures rapidly and cut down on the memory footprint. |
aoqi@0 | 623 | // Note: not all predecessors might be visited yet (must happen for |
aoqi@0 | 624 | // irreducible loops). This is OK, since every live value must have the |
aoqi@0 | 625 | // SAME reaching def for the block, so any reaching def is OK. |
aoqi@0 | 626 | uint i; |
aoqi@0 | 627 | |
aoqi@0 | 628 | Block *b = worklist.pop(); |
aoqi@0 | 629 | // Ignore root block |
aoqi@0 | 630 | if (b == _cfg->get_root_block()) { |
aoqi@0 | 631 | continue; |
aoqi@0 | 632 | } |
aoqi@0 | 633 | // Block is already done? Happens if block has several predecessors, |
aoqi@0 | 634 | // he can get on the worklist more than once. |
aoqi@0 | 635 | if( flows[b->_pre_order] ) continue; |
aoqi@0 | 636 | |
aoqi@0 | 637 | // If this block has a visited predecessor AND that predecessor has this |
aoqi@0 | 638 | // last block as his only undone child, we can move the OopFlow from the |
aoqi@0 | 639 | // pred to this block. Otherwise we have to grab a new OopFlow. |
aoqi@0 | 640 | OopFlow *flow = NULL; // Flag for finding optimized flow |
aoqi@0 | 641 | Block *pred = (Block*)0xdeadbeef; |
aoqi@0 | 642 | // Scan this block's preds to find a done predecessor |
aoqi@0 | 643 | for (uint j = 1; j < b->num_preds(); j++) { |
aoqi@0 | 644 | Block* p = _cfg->get_block_for_node(b->pred(j)); |
aoqi@0 | 645 | OopFlow *p_flow = flows[p->_pre_order]; |
aoqi@0 | 646 | if( p_flow ) { // Predecessor is done |
aoqi@0 | 647 | assert( p_flow->_b == p, "cross check" ); |
aoqi@0 | 648 | pred = p; // Record some predecessor |
aoqi@0 | 649 | // If all successors of p are done except for 'b', then we can carry |
aoqi@0 | 650 | // p_flow forward to 'b' without copying, otherwise we have to draw |
aoqi@0 | 651 | // from the free_list and clone data. |
aoqi@0 | 652 | uint k; |
aoqi@0 | 653 | for( k=0; k<p->_num_succs; k++ ) |
aoqi@0 | 654 | if( !flows[p->_succs[k]->_pre_order] && |
aoqi@0 | 655 | p->_succs[k] != b ) |
aoqi@0 | 656 | break; |
aoqi@0 | 657 | |
aoqi@0 | 658 | // Either carry-forward the now-unused OopFlow for b's use |
aoqi@0 | 659 | // or draw a new one from the free list |
aoqi@0 | 660 | if( k==p->_num_succs ) { |
aoqi@0 | 661 | flow = p_flow; |
aoqi@0 | 662 | break; // Found an ideal pred, use him |
aoqi@0 | 663 | } |
aoqi@0 | 664 | } |
aoqi@0 | 665 | } |
aoqi@0 | 666 | |
aoqi@0 | 667 | if( flow ) { |
aoqi@0 | 668 | // We have an OopFlow that's the last-use of a predecessor. |
aoqi@0 | 669 | // Carry it forward. |
aoqi@0 | 670 | } else { // Draw a new OopFlow from the freelist |
aoqi@0 | 671 | if( !free_list ) |
aoqi@0 | 672 | free_list = OopFlow::make(A,max_reg,C); |
aoqi@0 | 673 | flow = free_list; |
aoqi@0 | 674 | assert( flow->_b == NULL, "oopFlow is not free" ); |
aoqi@0 | 675 | free_list = flow->_next; |
aoqi@0 | 676 | flow->_next = NULL; |
aoqi@0 | 677 | |
aoqi@0 | 678 | // Copy/clone over the data |
aoqi@0 | 679 | flow->clone(flows[pred->_pre_order], max_reg); |
aoqi@0 | 680 | } |
aoqi@0 | 681 | |
aoqi@0 | 682 | // Mark flow for block. Blocks can only be flowed over once, |
aoqi@0 | 683 | // because after the first time they are guarded from entering |
aoqi@0 | 684 | // this code again. |
aoqi@0 | 685 | assert( flow->_b == pred, "have some prior flow" ); |
aoqi@0 | 686 | flow->_b = NULL; |
aoqi@0 | 687 | |
aoqi@0 | 688 | // Now push flow forward |
aoqi@0 | 689 | flows[b->_pre_order] = flow;// Mark flow for this block |
aoqi@0 | 690 | flow->_b = b; |
aoqi@0 | 691 | flow->compute_reach( _regalloc, max_reg, safehash ); |
aoqi@0 | 692 | |
aoqi@0 | 693 | // Now push children onto worklist |
aoqi@0 | 694 | for( i=0; i<b->_num_succs; i++ ) |
aoqi@0 | 695 | worklist.push(b->_succs[i]); |
aoqi@0 | 696 | |
aoqi@0 | 697 | } |
aoqi@0 | 698 | } |