Wed, 07 May 2008 08:06:46 -0700
6603011: RFE: Optimize long division
Summary: Transform long division by constant into multiply
Reviewed-by: never, kvn
1 /*
2 * Copyright 1997-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 // Portions of code courtesy of Clifford Click
27 class MultiNode;
28 class PhaseCCP;
29 class PhaseTransform;
31 //------------------------------MemNode----------------------------------------
32 // Load or Store, possibly throwing a NULL pointer exception
33 class MemNode : public Node {
34 protected:
35 #ifdef ASSERT
36 const TypePtr* _adr_type; // What kind of memory is being addressed?
37 #endif
38 virtual uint size_of() const; // Size is bigger (ASSERT only)
39 public:
40 enum { Control, // When is it safe to do this load?
41 Memory, // Chunk of memory is being loaded from
42 Address, // Actually address, derived from base
43 ValueIn, // Value to store
44 OopStore // Preceeding oop store, only in StoreCM
45 };
46 protected:
47 MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at )
48 : Node(c0,c1,c2 ) {
49 init_class_id(Class_Mem);
50 debug_only(_adr_type=at; adr_type();)
51 }
52 MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at, Node *c3 )
53 : Node(c0,c1,c2,c3) {
54 init_class_id(Class_Mem);
55 debug_only(_adr_type=at; adr_type();)
56 }
57 MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at, Node *c3, Node *c4)
58 : Node(c0,c1,c2,c3,c4) {
59 init_class_id(Class_Mem);
60 debug_only(_adr_type=at; adr_type();)
61 }
63 public:
64 // Helpers for the optimizer. Documented in memnode.cpp.
65 static bool detect_ptr_independence(Node* p1, AllocateNode* a1,
66 Node* p2, AllocateNode* a2,
67 PhaseTransform* phase);
68 static bool adr_phi_is_loop_invariant(Node* adr_phi, Node* cast);
70 static Node *optimize_simple_memory_chain(Node *mchain, const TypePtr *t_adr, PhaseGVN *phase);
71 static Node *optimize_memory_chain(Node *mchain, const TypePtr *t_adr, PhaseGVN *phase);
72 // This one should probably be a phase-specific function:
73 static bool all_controls_dominate(Node* dom, Node* sub);
75 // Is this Node a MemNode or some descendent? Default is YES.
76 virtual Node *Ideal_DU_postCCP( PhaseCCP *ccp );
78 virtual const class TypePtr *adr_type() const; // returns bottom_type of address
80 // Shared code for Ideal methods:
81 Node *Ideal_common(PhaseGVN *phase, bool can_reshape); // Return -1 for short-circuit NULL.
83 // Helper function for adr_type() implementations.
84 static const TypePtr* calculate_adr_type(const Type* t, const TypePtr* cross_check = NULL);
86 // Raw access function, to allow copying of adr_type efficiently in
87 // product builds and retain the debug info for debug builds.
88 const TypePtr *raw_adr_type() const {
89 #ifdef ASSERT
90 return _adr_type;
91 #else
92 return 0;
93 #endif
94 }
96 // Map a load or store opcode to its corresponding store opcode.
97 // (Return -1 if unknown.)
98 virtual int store_Opcode() const { return -1; }
100 // What is the type of the value in memory? (T_VOID mean "unspecified".)
101 virtual BasicType memory_type() const = 0;
102 virtual int memory_size() const {
103 #ifdef ASSERT
104 return type2aelembytes(memory_type(), true);
105 #else
106 return type2aelembytes(memory_type());
107 #endif
108 }
110 // Search through memory states which precede this node (load or store).
111 // Look for an exact match for the address, with no intervening
112 // aliased stores.
113 Node* find_previous_store(PhaseTransform* phase);
115 // Can this node (load or store) accurately see a stored value in
116 // the given memory state? (The state may or may not be in(Memory).)
117 Node* can_see_stored_value(Node* st, PhaseTransform* phase) const;
119 #ifndef PRODUCT
120 static void dump_adr_type(const Node* mem, const TypePtr* adr_type, outputStream *st);
121 virtual void dump_spec(outputStream *st) const;
122 #endif
123 };
125 //------------------------------LoadNode---------------------------------------
126 // Load value; requires Memory and Address
127 class LoadNode : public MemNode {
128 protected:
129 virtual uint cmp( const Node &n ) const;
130 virtual uint size_of() const; // Size is bigger
131 const Type* const _type; // What kind of value is loaded?
132 public:
134 LoadNode( Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *rt )
135 : MemNode(c,mem,adr,at), _type(rt) {
136 init_class_id(Class_Load);
137 }
139 // Polymorphic factory method:
140 static Node* make( PhaseGVN& gvn, Node *c, Node *mem, Node *adr,
141 const TypePtr* at, const Type *rt, BasicType bt );
143 virtual uint hash() const; // Check the type
145 // Handle algebraic identities here. If we have an identity, return the Node
146 // we are equivalent to. We look for Load of a Store.
147 virtual Node *Identity( PhaseTransform *phase );
149 // If the load is from Field memory and the pointer is non-null, we can
150 // zero out the control input.
151 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
153 // Recover original value from boxed values
154 Node *eliminate_autobox(PhaseGVN *phase);
156 // Compute a new Type for this node. Basically we just do the pre-check,
157 // then call the virtual add() to set the type.
158 virtual const Type *Value( PhaseTransform *phase ) const;
160 virtual uint ideal_reg() const;
161 virtual const Type *bottom_type() const;
162 // Following method is copied from TypeNode:
163 void set_type(const Type* t) {
164 assert(t != NULL, "sanity");
165 debug_only(uint check_hash = (VerifyHashTableKeys && _hash_lock) ? hash() : NO_HASH);
166 *(const Type**)&_type = t; // cast away const-ness
167 // If this node is in the hash table, make sure it doesn't need a rehash.
168 assert(check_hash == NO_HASH || check_hash == hash(), "type change must preserve hash code");
169 }
170 const Type* type() const { assert(_type != NULL, "sanity"); return _type; };
172 // Do not match memory edge
173 virtual uint match_edge(uint idx) const;
175 // Map a load opcode to its corresponding store opcode.
176 virtual int store_Opcode() const = 0;
178 // Check if the load's memory input is a Phi node with the same control.
179 bool is_instance_field_load_with_local_phi(Node* ctrl);
181 #ifndef PRODUCT
182 virtual void dump_spec(outputStream *st) const;
183 #endif
184 protected:
185 const Type* load_array_final_field(const TypeKlassPtr *tkls,
186 ciKlass* klass) const;
187 };
189 //------------------------------LoadBNode--------------------------------------
190 // Load a byte (8bits signed) from memory
191 class LoadBNode : public LoadNode {
192 public:
193 LoadBNode( Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti = TypeInt::BYTE )
194 : LoadNode(c,mem,adr,at,ti) {}
195 virtual int Opcode() const;
196 virtual uint ideal_reg() const { return Op_RegI; }
197 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
198 virtual int store_Opcode() const { return Op_StoreB; }
199 virtual BasicType memory_type() const { return T_BYTE; }
200 };
202 //------------------------------LoadCNode--------------------------------------
203 // Load a char (16bits unsigned) from memory
204 class LoadCNode : public LoadNode {
205 public:
206 LoadCNode( Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti = TypeInt::CHAR )
207 : LoadNode(c,mem,adr,at,ti) {}
208 virtual int Opcode() const;
209 virtual uint ideal_reg() const { return Op_RegI; }
210 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
211 virtual int store_Opcode() const { return Op_StoreC; }
212 virtual BasicType memory_type() const { return T_CHAR; }
213 };
215 //------------------------------LoadINode--------------------------------------
216 // Load an integer from memory
217 class LoadINode : public LoadNode {
218 public:
219 LoadINode( Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti = TypeInt::INT )
220 : LoadNode(c,mem,adr,at,ti) {}
221 virtual int Opcode() const;
222 virtual uint ideal_reg() const { return Op_RegI; }
223 virtual int store_Opcode() const { return Op_StoreI; }
224 virtual BasicType memory_type() const { return T_INT; }
225 };
227 //------------------------------LoadRangeNode----------------------------------
228 // Load an array length from the array
229 class LoadRangeNode : public LoadINode {
230 public:
231 LoadRangeNode( Node *c, Node *mem, Node *adr, const TypeInt *ti = TypeInt::POS )
232 : LoadINode(c,mem,adr,TypeAryPtr::RANGE,ti) {}
233 virtual int Opcode() const;
234 virtual const Type *Value( PhaseTransform *phase ) const;
235 virtual Node *Identity( PhaseTransform *phase );
236 };
238 //------------------------------LoadLNode--------------------------------------
239 // Load a long from memory
240 class LoadLNode : public LoadNode {
241 virtual uint hash() const { return LoadNode::hash() + _require_atomic_access; }
242 virtual uint cmp( const Node &n ) const {
243 return _require_atomic_access == ((LoadLNode&)n)._require_atomic_access
244 && LoadNode::cmp(n);
245 }
246 virtual uint size_of() const { return sizeof(*this); }
247 const bool _require_atomic_access; // is piecewise load forbidden?
249 public:
250 LoadLNode( Node *c, Node *mem, Node *adr, const TypePtr* at,
251 const TypeLong *tl = TypeLong::LONG,
252 bool require_atomic_access = false )
253 : LoadNode(c,mem,adr,at,tl)
254 , _require_atomic_access(require_atomic_access)
255 {}
256 virtual int Opcode() const;
257 virtual uint ideal_reg() const { return Op_RegL; }
258 virtual int store_Opcode() const { return Op_StoreL; }
259 virtual BasicType memory_type() const { return T_LONG; }
260 bool require_atomic_access() { return _require_atomic_access; }
261 static LoadLNode* make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt);
262 #ifndef PRODUCT
263 virtual void dump_spec(outputStream *st) const {
264 LoadNode::dump_spec(st);
265 if (_require_atomic_access) st->print(" Atomic!");
266 }
267 #endif
268 };
270 //------------------------------LoadL_unalignedNode----------------------------
271 // Load a long from unaligned memory
272 class LoadL_unalignedNode : public LoadLNode {
273 public:
274 LoadL_unalignedNode( Node *c, Node *mem, Node *adr, const TypePtr* at )
275 : LoadLNode(c,mem,adr,at) {}
276 virtual int Opcode() const;
277 };
279 //------------------------------LoadFNode--------------------------------------
280 // Load a float (64 bits) from memory
281 class LoadFNode : public LoadNode {
282 public:
283 LoadFNode( Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *t = Type::FLOAT )
284 : LoadNode(c,mem,adr,at,t) {}
285 virtual int Opcode() const;
286 virtual uint ideal_reg() const { return Op_RegF; }
287 virtual int store_Opcode() const { return Op_StoreF; }
288 virtual BasicType memory_type() const { return T_FLOAT; }
289 };
291 //------------------------------LoadDNode--------------------------------------
292 // Load a double (64 bits) from memory
293 class LoadDNode : public LoadNode {
294 public:
295 LoadDNode( Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *t = Type::DOUBLE )
296 : LoadNode(c,mem,adr,at,t) {}
297 virtual int Opcode() const;
298 virtual uint ideal_reg() const { return Op_RegD; }
299 virtual int store_Opcode() const { return Op_StoreD; }
300 virtual BasicType memory_type() const { return T_DOUBLE; }
301 };
303 //------------------------------LoadD_unalignedNode----------------------------
304 // Load a double from unaligned memory
305 class LoadD_unalignedNode : public LoadDNode {
306 public:
307 LoadD_unalignedNode( Node *c, Node *mem, Node *adr, const TypePtr* at )
308 : LoadDNode(c,mem,adr,at) {}
309 virtual int Opcode() const;
310 };
312 //------------------------------LoadPNode--------------------------------------
313 // Load a pointer from memory (either object or array)
314 class LoadPNode : public LoadNode {
315 public:
316 LoadPNode( Node *c, Node *mem, Node *adr, const TypePtr *at, const TypePtr* t )
317 : LoadNode(c,mem,adr,at,t) {}
318 virtual int Opcode() const;
319 virtual uint ideal_reg() const { return Op_RegP; }
320 virtual int store_Opcode() const { return Op_StoreP; }
321 virtual BasicType memory_type() const { return T_ADDRESS; }
322 // depends_only_on_test is almost always true, and needs to be almost always
323 // true to enable key hoisting & commoning optimizations. However, for the
324 // special case of RawPtr loads from TLS top & end, the control edge carries
325 // the dependence preventing hoisting past a Safepoint instead of the memory
326 // edge. (An unfortunate consequence of having Safepoints not set Raw
327 // Memory; itself an unfortunate consequence of having Nodes which produce
328 // results (new raw memory state) inside of loops preventing all manner of
329 // other optimizations). Basically, it's ugly but so is the alternative.
330 // See comment in macro.cpp, around line 125 expand_allocate_common().
331 virtual bool depends_only_on_test() const { return adr_type() != TypeRawPtr::BOTTOM; }
332 };
335 //------------------------------LoadNNode--------------------------------------
336 // Load a narrow oop from memory (either object or array)
337 class LoadNNode : public LoadNode {
338 public:
339 LoadNNode( Node *c, Node *mem, Node *adr, const TypePtr *at, const Type* t )
340 : LoadNode(c,mem,adr,at,t) {}
341 virtual int Opcode() const;
342 virtual uint ideal_reg() const { return Op_RegN; }
343 virtual int store_Opcode() const { return Op_StoreN; }
344 virtual BasicType memory_type() const { return T_NARROWOOP; }
345 // depends_only_on_test is almost always true, and needs to be almost always
346 // true to enable key hoisting & commoning optimizations. However, for the
347 // special case of RawPtr loads from TLS top & end, the control edge carries
348 // the dependence preventing hoisting past a Safepoint instead of the memory
349 // edge. (An unfortunate consequence of having Safepoints not set Raw
350 // Memory; itself an unfortunate consequence of having Nodes which produce
351 // results (new raw memory state) inside of loops preventing all manner of
352 // other optimizations). Basically, it's ugly but so is the alternative.
353 // See comment in macro.cpp, around line 125 expand_allocate_common().
354 virtual bool depends_only_on_test() const { return adr_type() != TypeRawPtr::BOTTOM; }
355 };
357 //------------------------------LoadKlassNode----------------------------------
358 // Load a Klass from an object
359 class LoadKlassNode : public LoadPNode {
360 public:
361 LoadKlassNode( Node *c, Node *mem, Node *adr, const TypePtr *at, const TypeKlassPtr *tk = TypeKlassPtr::OBJECT )
362 : LoadPNode(c,mem,adr,at,tk) {}
363 virtual int Opcode() const;
364 virtual const Type *Value( PhaseTransform *phase ) const;
365 virtual Node *Identity( PhaseTransform *phase );
366 virtual bool depends_only_on_test() const { return true; }
367 };
369 //------------------------------LoadSNode--------------------------------------
370 // Load a short (16bits signed) from memory
371 class LoadSNode : public LoadNode {
372 public:
373 LoadSNode( Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti = TypeInt::SHORT )
374 : LoadNode(c,mem,adr,at,ti) {}
375 virtual int Opcode() const;
376 virtual uint ideal_reg() const { return Op_RegI; }
377 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
378 virtual int store_Opcode() const { return Op_StoreC; }
379 virtual BasicType memory_type() const { return T_SHORT; }
380 };
382 //------------------------------StoreNode--------------------------------------
383 // Store value; requires Store, Address and Value
384 class StoreNode : public MemNode {
385 protected:
386 virtual uint cmp( const Node &n ) const;
387 virtual bool depends_only_on_test() const { return false; }
389 Node *Ideal_masked_input (PhaseGVN *phase, uint mask);
390 Node *Ideal_sign_extended_input(PhaseGVN *phase, int num_bits);
392 public:
393 StoreNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val )
394 : MemNode(c,mem,adr,at,val) {
395 init_class_id(Class_Store);
396 }
397 StoreNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, Node *oop_store )
398 : MemNode(c,mem,adr,at,val,oop_store) {
399 init_class_id(Class_Store);
400 }
402 // Polymorphic factory method:
403 static StoreNode* make( PhaseGVN& gvn, Node *c, Node *mem, Node *adr,
404 const TypePtr* at, Node *val, BasicType bt );
406 virtual uint hash() const; // Check the type
408 // If the store is to Field memory and the pointer is non-null, we can
409 // zero out the control input.
410 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
412 // Compute a new Type for this node. Basically we just do the pre-check,
413 // then call the virtual add() to set the type.
414 virtual const Type *Value( PhaseTransform *phase ) const;
416 // Check for identity function on memory (Load then Store at same address)
417 virtual Node *Identity( PhaseTransform *phase );
419 // Do not match memory edge
420 virtual uint match_edge(uint idx) const;
422 virtual const Type *bottom_type() const; // returns Type::MEMORY
424 // Map a store opcode to its corresponding own opcode, trivially.
425 virtual int store_Opcode() const { return Opcode(); }
427 // have all possible loads of the value stored been optimized away?
428 bool value_never_loaded(PhaseTransform *phase) const;
429 };
431 //------------------------------StoreBNode-------------------------------------
432 // Store byte to memory
433 class StoreBNode : public StoreNode {
434 public:
435 StoreBNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val ) : StoreNode(c,mem,adr,at,val) {}
436 virtual int Opcode() const;
437 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
438 virtual BasicType memory_type() const { return T_BYTE; }
439 };
441 //------------------------------StoreCNode-------------------------------------
442 // Store char/short to memory
443 class StoreCNode : public StoreNode {
444 public:
445 StoreCNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val ) : StoreNode(c,mem,adr,at,val) {}
446 virtual int Opcode() const;
447 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
448 virtual BasicType memory_type() const { return T_CHAR; }
449 };
451 //------------------------------StoreINode-------------------------------------
452 // Store int to memory
453 class StoreINode : public StoreNode {
454 public:
455 StoreINode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val ) : StoreNode(c,mem,adr,at,val) {}
456 virtual int Opcode() const;
457 virtual BasicType memory_type() const { return T_INT; }
458 };
460 //------------------------------StoreLNode-------------------------------------
461 // Store long to memory
462 class StoreLNode : public StoreNode {
463 virtual uint hash() const { return StoreNode::hash() + _require_atomic_access; }
464 virtual uint cmp( const Node &n ) const {
465 return _require_atomic_access == ((StoreLNode&)n)._require_atomic_access
466 && StoreNode::cmp(n);
467 }
468 virtual uint size_of() const { return sizeof(*this); }
469 const bool _require_atomic_access; // is piecewise store forbidden?
471 public:
472 StoreLNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val,
473 bool require_atomic_access = false )
474 : StoreNode(c,mem,adr,at,val)
475 , _require_atomic_access(require_atomic_access)
476 {}
477 virtual int Opcode() const;
478 virtual BasicType memory_type() const { return T_LONG; }
479 bool require_atomic_access() { return _require_atomic_access; }
480 static StoreLNode* make_atomic(Compile *C, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val);
481 #ifndef PRODUCT
482 virtual void dump_spec(outputStream *st) const {
483 StoreNode::dump_spec(st);
484 if (_require_atomic_access) st->print(" Atomic!");
485 }
486 #endif
487 };
489 //------------------------------StoreFNode-------------------------------------
490 // Store float to memory
491 class StoreFNode : public StoreNode {
492 public:
493 StoreFNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val ) : StoreNode(c,mem,adr,at,val) {}
494 virtual int Opcode() const;
495 virtual BasicType memory_type() const { return T_FLOAT; }
496 };
498 //------------------------------StoreDNode-------------------------------------
499 // Store double to memory
500 class StoreDNode : public StoreNode {
501 public:
502 StoreDNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val ) : StoreNode(c,mem,adr,at,val) {}
503 virtual int Opcode() const;
504 virtual BasicType memory_type() const { return T_DOUBLE; }
505 };
507 //------------------------------StorePNode-------------------------------------
508 // Store pointer to memory
509 class StorePNode : public StoreNode {
510 public:
511 StorePNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val ) : StoreNode(c,mem,adr,at,val) {}
512 virtual int Opcode() const;
513 virtual BasicType memory_type() const { return T_ADDRESS; }
514 };
516 //------------------------------StoreNNode-------------------------------------
517 // Store narrow oop to memory
518 class StoreNNode : public StoreNode {
519 public:
520 StoreNNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val ) : StoreNode(c,mem,adr,at,val) {}
521 virtual int Opcode() const;
522 virtual BasicType memory_type() const { return T_NARROWOOP; }
523 };
525 //------------------------------StoreCMNode-----------------------------------
526 // Store card-mark byte to memory for CM
527 // The last StoreCM before a SafePoint must be preserved and occur after its "oop" store
528 // Preceeding equivalent StoreCMs may be eliminated.
529 class StoreCMNode : public StoreNode {
530 public:
531 StoreCMNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, Node *oop_store ) : StoreNode(c,mem,adr,at,val,oop_store) {}
532 virtual int Opcode() const;
533 virtual Node *Identity( PhaseTransform *phase );
534 virtual const Type *Value( PhaseTransform *phase ) const;
535 virtual BasicType memory_type() const { return T_VOID; } // unspecific
536 };
538 //------------------------------LoadPLockedNode---------------------------------
539 // Load-locked a pointer from memory (either object or array).
540 // On Sparc & Intel this is implemented as a normal pointer load.
541 // On PowerPC and friends it's a real load-locked.
542 class LoadPLockedNode : public LoadPNode {
543 public:
544 LoadPLockedNode( Node *c, Node *mem, Node *adr )
545 : LoadPNode(c,mem,adr,TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM) {}
546 virtual int Opcode() const;
547 virtual int store_Opcode() const { return Op_StorePConditional; }
548 virtual bool depends_only_on_test() const { return true; }
549 };
551 //------------------------------LoadLLockedNode---------------------------------
552 // Load-locked a pointer from memory (either object or array).
553 // On Sparc & Intel this is implemented as a normal long load.
554 class LoadLLockedNode : public LoadLNode {
555 public:
556 LoadLLockedNode( Node *c, Node *mem, Node *adr )
557 : LoadLNode(c,mem,adr,TypeRawPtr::BOTTOM, TypeLong::LONG) {}
558 virtual int Opcode() const;
559 virtual int store_Opcode() const { return Op_StoreLConditional; }
560 };
562 //------------------------------SCMemProjNode---------------------------------------
563 // This class defines a projection of the memory state of a store conditional node.
564 // These nodes return a value, but also update memory.
565 class SCMemProjNode : public ProjNode {
566 public:
567 enum {SCMEMPROJCON = (uint)-2};
568 SCMemProjNode( Node *src) : ProjNode( src, SCMEMPROJCON) { }
569 virtual int Opcode() const;
570 virtual bool is_CFG() const { return false; }
571 virtual const Type *bottom_type() const {return Type::MEMORY;}
572 virtual const TypePtr *adr_type() const { return in(0)->in(MemNode::Memory)->adr_type();}
573 virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
574 virtual const Type *Value( PhaseTransform *phase ) const;
575 #ifndef PRODUCT
576 virtual void dump_spec(outputStream *st) const {};
577 #endif
578 };
580 //------------------------------LoadStoreNode---------------------------
581 class LoadStoreNode : public Node {
582 public:
583 enum {
584 ExpectedIn = MemNode::ValueIn+1 // One more input than MemNode
585 };
586 LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex);
587 virtual bool depends_only_on_test() const { return false; }
588 virtual const Type *bottom_type() const { return TypeInt::BOOL; }
589 virtual uint ideal_reg() const { return Op_RegI; }
590 virtual uint match_edge(uint idx) const { return idx == MemNode::Address || idx == MemNode::ValueIn; }
591 };
593 //------------------------------StorePConditionalNode---------------------------
594 // Conditionally store pointer to memory, if no change since prior
595 // load-locked. Sets flags for success or failure of the store.
596 class StorePConditionalNode : public LoadStoreNode {
597 public:
598 StorePConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ll ) : LoadStoreNode(c, mem, adr, val, ll) { }
599 virtual int Opcode() const;
600 // Produces flags
601 virtual uint ideal_reg() const { return Op_RegFlags; }
602 };
604 //------------------------------StoreLConditionalNode---------------------------
605 // Conditionally store long to memory, if no change since prior
606 // load-locked. Sets flags for success or failure of the store.
607 class StoreLConditionalNode : public LoadStoreNode {
608 public:
609 StoreLConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ll ) : LoadStoreNode(c, mem, adr, val, ll) { }
610 virtual int Opcode() const;
611 };
614 //------------------------------CompareAndSwapLNode---------------------------
615 class CompareAndSwapLNode : public LoadStoreNode {
616 public:
617 CompareAndSwapLNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex) : LoadStoreNode(c, mem, adr, val, ex) { }
618 virtual int Opcode() const;
619 };
622 //------------------------------CompareAndSwapINode---------------------------
623 class CompareAndSwapINode : public LoadStoreNode {
624 public:
625 CompareAndSwapINode( Node *c, Node *mem, Node *adr, Node *val, Node *ex) : LoadStoreNode(c, mem, adr, val, ex) { }
626 virtual int Opcode() const;
627 };
630 //------------------------------CompareAndSwapPNode---------------------------
631 class CompareAndSwapPNode : public LoadStoreNode {
632 public:
633 CompareAndSwapPNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex) : LoadStoreNode(c, mem, adr, val, ex) { }
634 virtual int Opcode() const;
635 };
637 //------------------------------CompareAndSwapNNode---------------------------
638 class CompareAndSwapNNode : public LoadStoreNode {
639 public:
640 CompareAndSwapNNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex) : LoadStoreNode(c, mem, adr, val, ex) { }
641 virtual int Opcode() const;
642 };
644 //------------------------------ClearArray-------------------------------------
645 class ClearArrayNode: public Node {
646 public:
647 ClearArrayNode( Node *ctrl, Node *arymem, Node *word_cnt, Node *base ) : Node(ctrl,arymem,word_cnt,base) {}
648 virtual int Opcode() const;
649 virtual const Type *bottom_type() const { return Type::MEMORY; }
650 // ClearArray modifies array elements, and so affects only the
651 // array memory addressed by the bottom_type of its base address.
652 virtual const class TypePtr *adr_type() const;
653 virtual Node *Identity( PhaseTransform *phase );
654 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
655 virtual uint match_edge(uint idx) const;
657 // Clear the given area of an object or array.
658 // The start offset must always be aligned mod BytesPerInt.
659 // The end offset must always be aligned mod BytesPerLong.
660 // Return the new memory.
661 static Node* clear_memory(Node* control, Node* mem, Node* dest,
662 intptr_t start_offset,
663 intptr_t end_offset,
664 PhaseGVN* phase);
665 static Node* clear_memory(Node* control, Node* mem, Node* dest,
666 intptr_t start_offset,
667 Node* end_offset,
668 PhaseGVN* phase);
669 static Node* clear_memory(Node* control, Node* mem, Node* dest,
670 Node* start_offset,
671 Node* end_offset,
672 PhaseGVN* phase);
673 };
675 //------------------------------StrComp-------------------------------------
676 class StrCompNode: public Node {
677 public:
678 StrCompNode(Node *control,
679 Node* char_array_mem,
680 Node* value_mem,
681 Node* count_mem,
682 Node* offset_mem,
683 Node* s1, Node* s2): Node(control,
684 char_array_mem,
685 value_mem,
686 count_mem,
687 offset_mem,
688 s1, s2) {};
689 virtual int Opcode() const;
690 virtual bool depends_only_on_test() const { return false; }
691 virtual const Type* bottom_type() const { return TypeInt::INT; }
692 // a StrCompNode (conservatively) aliases with everything:
693 virtual const TypePtr* adr_type() const { return TypePtr::BOTTOM; }
694 virtual uint match_edge(uint idx) const;
695 virtual uint ideal_reg() const { return Op_RegI; }
696 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
697 };
699 //------------------------------MemBar-----------------------------------------
700 // There are different flavors of Memory Barriers to match the Java Memory
701 // Model. Monitor-enter and volatile-load act as Aquires: no following ref
702 // can be moved to before them. We insert a MemBar-Acquire after a FastLock or
703 // volatile-load. Monitor-exit and volatile-store act as Release: no
704 // preceeding ref can be moved to after them. We insert a MemBar-Release
705 // before a FastUnlock or volatile-store. All volatiles need to be
706 // serialized, so we follow all volatile-stores with a MemBar-Volatile to
707 // seperate it from any following volatile-load.
708 class MemBarNode: public MultiNode {
709 virtual uint hash() const ; // { return NO_HASH; }
710 virtual uint cmp( const Node &n ) const ; // Always fail, except on self
712 virtual uint size_of() const { return sizeof(*this); }
713 // Memory type this node is serializing. Usually either rawptr or bottom.
714 const TypePtr* _adr_type;
716 public:
717 enum {
718 Precedent = TypeFunc::Parms // optional edge to force precedence
719 };
720 MemBarNode(Compile* C, int alias_idx, Node* precedent);
721 virtual int Opcode() const = 0;
722 virtual const class TypePtr *adr_type() const { return _adr_type; }
723 virtual const Type *Value( PhaseTransform *phase ) const;
724 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
725 virtual uint match_edge(uint idx) const { return 0; }
726 virtual const Type *bottom_type() const { return TypeTuple::MEMBAR; }
727 virtual Node *match( const ProjNode *proj, const Matcher *m );
728 // Factory method. Builds a wide or narrow membar.
729 // Optional 'precedent' becomes an extra edge if not null.
730 static MemBarNode* make(Compile* C, int opcode,
731 int alias_idx = Compile::AliasIdxBot,
732 Node* precedent = NULL);
733 };
735 // "Acquire" - no following ref can move before (but earlier refs can
736 // follow, like an early Load stalled in cache). Requires multi-cpu
737 // visibility. Inserted after a volatile load or FastLock.
738 class MemBarAcquireNode: public MemBarNode {
739 public:
740 MemBarAcquireNode(Compile* C, int alias_idx, Node* precedent)
741 : MemBarNode(C, alias_idx, precedent) {}
742 virtual int Opcode() const;
743 };
745 // "Release" - no earlier ref can move after (but later refs can move
746 // up, like a speculative pipelined cache-hitting Load). Requires
747 // multi-cpu visibility. Inserted before a volatile store or FastUnLock.
748 class MemBarReleaseNode: public MemBarNode {
749 public:
750 MemBarReleaseNode(Compile* C, int alias_idx, Node* precedent)
751 : MemBarNode(C, alias_idx, precedent) {}
752 virtual int Opcode() const;
753 };
755 // Ordering between a volatile store and a following volatile load.
756 // Requires multi-CPU visibility?
757 class MemBarVolatileNode: public MemBarNode {
758 public:
759 MemBarVolatileNode(Compile* C, int alias_idx, Node* precedent)
760 : MemBarNode(C, alias_idx, precedent) {}
761 virtual int Opcode() const;
762 };
764 // Ordering within the same CPU. Used to order unsafe memory references
765 // inside the compiler when we lack alias info. Not needed "outside" the
766 // compiler because the CPU does all the ordering for us.
767 class MemBarCPUOrderNode: public MemBarNode {
768 public:
769 MemBarCPUOrderNode(Compile* C, int alias_idx, Node* precedent)
770 : MemBarNode(C, alias_idx, precedent) {}
771 virtual int Opcode() const;
772 virtual uint ideal_reg() const { return 0; } // not matched in the AD file
773 };
775 // Isolation of object setup after an AllocateNode and before next safepoint.
776 // (See comment in memnode.cpp near InitializeNode::InitializeNode for semantics.)
777 class InitializeNode: public MemBarNode {
778 friend class AllocateNode;
780 bool _is_complete;
782 public:
783 enum {
784 Control = TypeFunc::Control,
785 Memory = TypeFunc::Memory, // MergeMem for states affected by this op
786 RawAddress = TypeFunc::Parms+0, // the newly-allocated raw address
787 RawStores = TypeFunc::Parms+1 // zero or more stores (or TOP)
788 };
790 InitializeNode(Compile* C, int adr_type, Node* rawoop);
791 virtual int Opcode() const;
792 virtual uint size_of() const { return sizeof(*this); }
793 virtual uint ideal_reg() const { return 0; } // not matched in the AD file
794 virtual const RegMask &in_RegMask(uint) const; // mask for RawAddress
796 // Manage incoming memory edges via a MergeMem on in(Memory):
797 Node* memory(uint alias_idx);
799 // The raw memory edge coming directly from the Allocation.
800 // The contents of this memory are *always* all-zero-bits.
801 Node* zero_memory() { return memory(Compile::AliasIdxRaw); }
803 // Return the corresponding allocation for this initialization (or null if none).
804 // (Note: Both InitializeNode::allocation and AllocateNode::initialization
805 // are defined in graphKit.cpp, which sets up the bidirectional relation.)
806 AllocateNode* allocation();
808 // Anything other than zeroing in this init?
809 bool is_non_zero();
811 // An InitializeNode must completed before macro expansion is done.
812 // Completion requires that the AllocateNode must be followed by
813 // initialization of the new memory to zero, then to any initializers.
814 bool is_complete() { return _is_complete; }
816 // Mark complete. (Must not yet be complete.)
817 void set_complete(PhaseGVN* phase);
819 #ifdef ASSERT
820 // ensure all non-degenerate stores are ordered and non-overlapping
821 bool stores_are_sane(PhaseTransform* phase);
822 #endif //ASSERT
824 // See if this store can be captured; return offset where it initializes.
825 // Return 0 if the store cannot be moved (any sort of problem).
826 intptr_t can_capture_store(StoreNode* st, PhaseTransform* phase);
828 // Capture another store; reformat it to write my internal raw memory.
829 // Return the captured copy, else NULL if there is some sort of problem.
830 Node* capture_store(StoreNode* st, intptr_t start, PhaseTransform* phase);
832 // Find captured store which corresponds to the range [start..start+size).
833 // Return my own memory projection (meaning the initial zero bits)
834 // if there is no such store. Return NULL if there is a problem.
835 Node* find_captured_store(intptr_t start, int size_in_bytes, PhaseTransform* phase);
837 // Called when the associated AllocateNode is expanded into CFG.
838 Node* complete_stores(Node* rawctl, Node* rawmem, Node* rawptr,
839 intptr_t header_size, Node* size_in_bytes,
840 PhaseGVN* phase);
842 private:
843 void remove_extra_zeroes();
845 // Find out where a captured store should be placed (or already is placed).
846 int captured_store_insertion_point(intptr_t start, int size_in_bytes,
847 PhaseTransform* phase);
849 static intptr_t get_store_offset(Node* st, PhaseTransform* phase);
851 Node* make_raw_address(intptr_t offset, PhaseTransform* phase);
853 bool detect_init_independence(Node* n, bool st_is_pinned, int& count);
855 void coalesce_subword_stores(intptr_t header_size, Node* size_in_bytes,
856 PhaseGVN* phase);
858 intptr_t find_next_fullword_store(uint i, PhaseGVN* phase);
859 };
861 //------------------------------MergeMem---------------------------------------
862 // (See comment in memnode.cpp near MergeMemNode::MergeMemNode for semantics.)
863 class MergeMemNode: public Node {
864 virtual uint hash() const ; // { return NO_HASH; }
865 virtual uint cmp( const Node &n ) const ; // Always fail, except on self
866 friend class MergeMemStream;
867 MergeMemNode(Node* def); // clients use MergeMemNode::make
869 public:
870 // If the input is a whole memory state, clone it with all its slices intact.
871 // Otherwise, make a new memory state with just that base memory input.
872 // In either case, the result is a newly created MergeMem.
873 static MergeMemNode* make(Compile* C, Node* base_memory);
875 virtual int Opcode() const;
876 virtual Node *Identity( PhaseTransform *phase );
877 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
878 virtual uint ideal_reg() const { return NotAMachineReg; }
879 virtual uint match_edge(uint idx) const { return 0; }
880 virtual const RegMask &out_RegMask() const;
881 virtual const Type *bottom_type() const { return Type::MEMORY; }
882 virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
883 // sparse accessors
884 // Fetch the previously stored "set_memory_at", or else the base memory.
885 // (Caller should clone it if it is a phi-nest.)
886 Node* memory_at(uint alias_idx) const;
887 // set the memory, regardless of its previous value
888 void set_memory_at(uint alias_idx, Node* n);
889 // the "base" is the memory that provides the non-finite support
890 Node* base_memory() const { return in(Compile::AliasIdxBot); }
891 // warning: setting the base can implicitly set any of the other slices too
892 void set_base_memory(Node* def);
893 // sentinel value which denotes a copy of the base memory:
894 Node* empty_memory() const { return in(Compile::AliasIdxTop); }
895 static Node* make_empty_memory(); // where the sentinel comes from
896 bool is_empty_memory(Node* n) const { assert((n == empty_memory()) == n->is_top(), "sanity"); return n->is_top(); }
897 // hook for the iterator, to perform any necessary setup
898 void iteration_setup(const MergeMemNode* other = NULL);
899 // push sentinels until I am at least as long as the other (semantic no-op)
900 void grow_to_match(const MergeMemNode* other);
901 bool verify_sparse() const PRODUCT_RETURN0;
902 #ifndef PRODUCT
903 virtual void dump_spec(outputStream *st) const;
904 #endif
905 };
907 class MergeMemStream : public StackObj {
908 private:
909 MergeMemNode* _mm;
910 const MergeMemNode* _mm2; // optional second guy, contributes non-empty iterations
911 Node* _mm_base; // loop-invariant base memory of _mm
912 int _idx;
913 int _cnt;
914 Node* _mem;
915 Node* _mem2;
916 int _cnt2;
918 void init(MergeMemNode* mm, const MergeMemNode* mm2 = NULL) {
919 // subsume_node will break sparseness at times, whenever a memory slice
920 // folds down to a copy of the base ("fat") memory. In such a case,
921 // the raw edge will update to base, although it should be top.
922 // This iterator will recognize either top or base_memory as an
923 // "empty" slice. See is_empty, is_empty2, and next below.
924 //
925 // The sparseness property is repaired in MergeMemNode::Ideal.
926 // As long as access to a MergeMem goes through this iterator
927 // or the memory_at accessor, flaws in the sparseness will
928 // never be observed.
929 //
930 // Also, iteration_setup repairs sparseness.
931 assert(mm->verify_sparse(), "please, no dups of base");
932 assert(mm2==NULL || mm2->verify_sparse(), "please, no dups of base");
934 _mm = mm;
935 _mm_base = mm->base_memory();
936 _mm2 = mm2;
937 _cnt = mm->req();
938 _idx = Compile::AliasIdxBot-1; // start at the base memory
939 _mem = NULL;
940 _mem2 = NULL;
941 }
943 #ifdef ASSERT
944 Node* check_memory() const {
945 if (at_base_memory())
946 return _mm->base_memory();
947 else if ((uint)_idx < _mm->req() && !_mm->in(_idx)->is_top())
948 return _mm->memory_at(_idx);
949 else
950 return _mm_base;
951 }
952 Node* check_memory2() const {
953 return at_base_memory()? _mm2->base_memory(): _mm2->memory_at(_idx);
954 }
955 #endif
957 static bool match_memory(Node* mem, const MergeMemNode* mm, int idx) PRODUCT_RETURN0;
958 void assert_synch() const {
959 assert(!_mem || _idx >= _cnt || match_memory(_mem, _mm, _idx),
960 "no side-effects except through the stream");
961 }
963 public:
965 // expected usages:
966 // for (MergeMemStream mms(mem->is_MergeMem()); next_non_empty(); ) { ... }
967 // for (MergeMemStream mms(mem1, mem2); next_non_empty2(); ) { ... }
969 // iterate over one merge
970 MergeMemStream(MergeMemNode* mm) {
971 mm->iteration_setup();
972 init(mm);
973 debug_only(_cnt2 = 999);
974 }
975 // iterate in parallel over two merges
976 // only iterates through non-empty elements of mm2
977 MergeMemStream(MergeMemNode* mm, const MergeMemNode* mm2) {
978 assert(mm2, "second argument must be a MergeMem also");
979 ((MergeMemNode*)mm2)->iteration_setup(); // update hidden state
980 mm->iteration_setup(mm2);
981 init(mm, mm2);
982 _cnt2 = mm2->req();
983 }
984 #ifdef ASSERT
985 ~MergeMemStream() {
986 assert_synch();
987 }
988 #endif
990 MergeMemNode* all_memory() const {
991 return _mm;
992 }
993 Node* base_memory() const {
994 assert(_mm_base == _mm->base_memory(), "no update to base memory, please");
995 return _mm_base;
996 }
997 const MergeMemNode* all_memory2() const {
998 assert(_mm2 != NULL, "");
999 return _mm2;
1000 }
1001 bool at_base_memory() const {
1002 return _idx == Compile::AliasIdxBot;
1003 }
1004 int alias_idx() const {
1005 assert(_mem, "must call next 1st");
1006 return _idx;
1007 }
1009 const TypePtr* adr_type() const {
1010 return Compile::current()->get_adr_type(alias_idx());
1011 }
1013 const TypePtr* adr_type(Compile* C) const {
1014 return C->get_adr_type(alias_idx());
1015 }
1016 bool is_empty() const {
1017 assert(_mem, "must call next 1st");
1018 assert(_mem->is_top() == (_mem==_mm->empty_memory()), "correct sentinel");
1019 return _mem->is_top();
1020 }
1021 bool is_empty2() const {
1022 assert(_mem2, "must call next 1st");
1023 assert(_mem2->is_top() == (_mem2==_mm2->empty_memory()), "correct sentinel");
1024 return _mem2->is_top();
1025 }
1026 Node* memory() const {
1027 assert(!is_empty(), "must not be empty");
1028 assert_synch();
1029 return _mem;
1030 }
1031 // get the current memory, regardless of empty or non-empty status
1032 Node* force_memory() const {
1033 assert(!is_empty() || !at_base_memory(), "");
1034 // Use _mm_base to defend against updates to _mem->base_memory().
1035 Node *mem = _mem->is_top() ? _mm_base : _mem;
1036 assert(mem == check_memory(), "");
1037 return mem;
1038 }
1039 Node* memory2() const {
1040 assert(_mem2 == check_memory2(), "");
1041 return _mem2;
1042 }
1043 void set_memory(Node* mem) {
1044 if (at_base_memory()) {
1045 // Note that this does not change the invariant _mm_base.
1046 _mm->set_base_memory(mem);
1047 } else {
1048 _mm->set_memory_at(_idx, mem);
1049 }
1050 _mem = mem;
1051 assert_synch();
1052 }
1054 // Recover from a side effect to the MergeMemNode.
1055 void set_memory() {
1056 _mem = _mm->in(_idx);
1057 }
1059 bool next() { return next(false); }
1060 bool next2() { return next(true); }
1062 bool next_non_empty() { return next_non_empty(false); }
1063 bool next_non_empty2() { return next_non_empty(true); }
1064 // next_non_empty2 can yield states where is_empty() is true
1066 private:
1067 // find the next item, which might be empty
1068 bool next(bool have_mm2) {
1069 assert((_mm2 != NULL) == have_mm2, "use other next");
1070 assert_synch();
1071 if (++_idx < _cnt) {
1072 // Note: This iterator allows _mm to be non-sparse.
1073 // It behaves the same whether _mem is top or base_memory.
1074 _mem = _mm->in(_idx);
1075 if (have_mm2)
1076 _mem2 = _mm2->in((_idx < _cnt2) ? _idx : Compile::AliasIdxTop);
1077 return true;
1078 }
1079 return false;
1080 }
1082 // find the next non-empty item
1083 bool next_non_empty(bool have_mm2) {
1084 while (next(have_mm2)) {
1085 if (!is_empty()) {
1086 // make sure _mem2 is filled in sensibly
1087 if (have_mm2 && _mem2->is_top()) _mem2 = _mm2->base_memory();
1088 return true;
1089 } else if (have_mm2 && !is_empty2()) {
1090 return true; // is_empty() == true
1091 }
1092 }
1093 return false;
1094 }
1095 };
1097 //------------------------------Prefetch---------------------------------------
1099 // Non-faulting prefetch load. Prefetch for many reads.
1100 class PrefetchReadNode : public Node {
1101 public:
1102 PrefetchReadNode(Node *abio, Node *adr) : Node(0,abio,adr) {}
1103 virtual int Opcode() const;
1104 virtual uint ideal_reg() const { return NotAMachineReg; }
1105 virtual uint match_edge(uint idx) const { return idx==2; }
1106 virtual const Type *bottom_type() const { return Type::ABIO; }
1107 };
1109 // Non-faulting prefetch load. Prefetch for many reads & many writes.
1110 class PrefetchWriteNode : public Node {
1111 public:
1112 PrefetchWriteNode(Node *abio, Node *adr) : Node(0,abio,adr) {}
1113 virtual int Opcode() const;
1114 virtual uint ideal_reg() const { return NotAMachineReg; }
1115 virtual uint match_edge(uint idx) const { return idx==2; }
1116 virtual const Type *bottom_type() const { return Type::ABIO; }
1117 };