1 /*
2 * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2024, Alibaba Group Holding Limited. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #ifndef SHARE_OPTO_MEMNODE_HPP
27 #define SHARE_OPTO_MEMNODE_HPP
28
29 #include "memory/allocation.hpp"
30 #include "opto/multnode.hpp"
31 #include "opto/node.hpp"
32 #include "opto/opcodes.hpp"
33 #include "opto/type.hpp"
34
35 // Portions of code courtesy of Clifford Click
36
37 class MultiNode;
38 class PhaseCCP;
39 class PhaseTransform;
40
41 //------------------------------MemNode----------------------------------------
42 // Load or Store, possibly throwing a null pointer exception
43 class MemNode : public Node {
44 private:
45 bool _unaligned_access; // Unaligned access from unsafe
46 bool _mismatched_access; // Mismatched access from unsafe: byte read in integer array for instance
47 bool _unsafe_access; // Access of unsafe origin.
48 uint8_t _barrier_data; // Bit field with barrier information
49
50 friend class AccessAnalyzer;
51
52 protected:
53 #ifdef ASSERT
54 const TypePtr* _adr_type; // What kind of memory is being addressed?
55 #endif
56 virtual uint size_of() const;
57 public:
58 enum { Control, // When is it safe to do this load?
59 Memory, // Chunk of memory is being loaded from
60 Address, // Actually address, derived from base
61 ValueIn // Value to store
62 };
63 typedef enum { unordered = 0,
64 acquire, // Load has to acquire or be succeeded by MemBarAcquire.
65 release, // Store has to release or be preceded by MemBarRelease.
66 seqcst, // LoadStore has to have both acquire and release semantics.
67 unset // The memory ordering is not set (used for testing)
68 } MemOrd;
69 protected:
70 MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at ) :
71 Node(c0,c1,c2),
72 _unaligned_access(false),
73 _mismatched_access(false),
74 _unsafe_access(false),
75 _barrier_data(0) {
76 init_class_id(Class_Mem);
77 DEBUG_ONLY(_adr_type=at; adr_type();)
78 }
79 MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at, Node *c3 ) :
80 Node(c0,c1,c2,c3),
81 _unaligned_access(false),
82 _mismatched_access(false),
83 _unsafe_access(false),
84 _barrier_data(0) {
85 init_class_id(Class_Mem);
86 DEBUG_ONLY(_adr_type=at; adr_type();)
87 }
88 MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at, Node *c3, Node *c4) :
89 Node(c0,c1,c2,c3,c4),
90 _unaligned_access(false),
91 _mismatched_access(false),
92 _unsafe_access(false),
93 _barrier_data(0) {
94 init_class_id(Class_Mem);
95 DEBUG_ONLY(_adr_type=at; adr_type();)
96 }
97
98 virtual Node* find_previous_arraycopy(PhaseValues* phase, Node* ld_alloc, Node*& mem, bool can_see_stored_value) const { return nullptr; }
99 ArrayCopyNode* find_array_copy_clone(Node* ld_alloc, Node* mem) const;
100 static bool check_if_adr_maybe_raw(Node* adr);
101
102 public:
103 // Helpers for the optimizer. Documented in memnode.cpp.
104 static bool detect_ptr_independence(Node* p1, AllocateNode* a1,
105 Node* p2, AllocateNode* a2,
106 PhaseTransform* phase);
107 static bool adr_phi_is_loop_invariant(Node* adr_phi, Node* cast);
108
109 static Node *optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase);
110 static Node *optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase);
111 // The following two should probably be phase-specific functions:
112 static DomResult maybe_all_controls_dominate(Node* dom, Node* sub);
113 static bool all_controls_dominate(Node* dom, Node* sub) {
114 DomResult dom_result = maybe_all_controls_dominate(dom, sub);
115 return dom_result == DomResult::Dominate;
116 }
117
118 virtual const class TypePtr *adr_type() const; // returns bottom_type of address
119
120 // Shared code for Ideal methods:
121 Node *Ideal_common(PhaseGVN *phase, bool can_reshape); // Return -1 for short-circuit null.
122
123 // Helper function for adr_type() implementations.
124 static const TypePtr* calculate_adr_type(const Type* t, const TypePtr* cross_check = nullptr);
125
126 // Raw access function, to allow copying of adr_type efficiently in
127 // product builds and retain the debug info for debug builds.
128 const TypePtr *raw_adr_type() const {
129 return DEBUG_ONLY(_adr_type) NOT_DEBUG(nullptr);
130 }
131
132 // Return the barrier data of n, if available, or 0 otherwise.
133 static uint8_t barrier_data(const Node* n);
134
135 // Map a load or store opcode to its corresponding store opcode.
136 // (Return -1 if unknown.)
137 virtual int store_Opcode() const { return -1; }
138
139 // What is the type of the value in memory? (T_VOID mean "unspecified".)
140 // The returned type is a property of the value that is loaded/stored and
141 // not the memory that is accessed. For mismatched memory accesses
142 // they might differ. For instance, a value of type 'short' may be stored
143 // into an array of elements of type 'long'.
144 virtual BasicType value_basic_type() const = 0;
145 virtual int memory_size() const {
146 #ifdef ASSERT
147 return type2aelembytes(value_basic_type(), true);
148 #else
149 return type2aelembytes(value_basic_type());
150 #endif
151 }
152
153 uint8_t barrier_data() { return _barrier_data; }
154 void set_barrier_data(uint8_t barrier_data) { _barrier_data = barrier_data; }
155
156 // Search through memory states which precede this node (load or store).
157 // Look for an exact match for the address, with no intervening
158 // aliased stores.
159 Node* find_previous_store(PhaseValues* phase);
160
161 // Can this node (load or store) accurately see a stored value in
162 // the given memory state? (The state may or may not be in(Memory).)
163 Node* can_see_stored_value(Node* st, PhaseValues* phase) const;
164
165 void set_unaligned_access() { _unaligned_access = true; }
166 bool is_unaligned_access() const { return _unaligned_access; }
167 void set_mismatched_access() { _mismatched_access = true; }
168 bool is_mismatched_access() const { return _mismatched_access; }
169 void set_unsafe_access() { _unsafe_access = true; }
170 bool is_unsafe_access() const { return _unsafe_access; }
171
172 #ifndef PRODUCT
173 static void dump_adr_type(const TypePtr* adr_type, outputStream* st);
174 virtual void dump_spec(outputStream *st) const;
175 #endif
176
177 MemNode* clone_with_adr_type(const TypePtr* adr_type) const {
178 MemNode* new_node = clone()->as_Mem();
179 #ifdef ASSERT
180 new_node->_adr_type = adr_type;
181 #endif
182 return new_node;
183 }
184 };
185
186 // Analyze a MemNode to try to prove that it is independent from other memory accesses
187 class AccessAnalyzer : StackObj {
188 private:
189 PhaseValues* const _phase;
190 MemNode* const _n;
191 Node* _base;
192 intptr_t _offset;
193 const int _memory_size;
194 bool _maybe_raw;
195 AllocateNode* _alloc;
196 const TypePtr* _adr_type;
197 int _alias_idx;
198
199 public:
200 AccessAnalyzer(PhaseValues* phase, MemNode* n);
201
202 // The result of deciding whether a memory node 'other' writes into the memory which '_n'
203 // observes.
204 class AccessIndependence {
205 public:
206 // Whether 'other' writes into the memory which '_n' observes. This value is conservative, that
207 // is, it is only true when it is provable that the memory accessed by the nodes is
208 // non-overlapping.
209 bool independent;
210
211 // If 'independent' is true, this is the memory input of 'other' that corresponds to the memory
212 // location that '_n' observes. For example, if 'other' is a StoreNode, then 'mem' is its
213 // memory input, if 'other' is a MergeMemNode, then 'mem' is the memory input corresponding to
214 // the alias class of '_n'.
215 // If 'independent' is false,
216 // - 'mem' is non-nullptr if it seems that 'other' writes to the exact memory location '_n'
217 // observes.
218 // - 'mem' is nullptr otherwise.
219 Node* mem;
220 };
221
222 AccessIndependence detect_access_independence(Node* other) const;
223 };
224
225 //------------------------------LoadNode---------------------------------------
226 // Load value; requires Memory and Address
227 class LoadNode : public MemNode {
228 public:
229 // Some loads (from unsafe) should be pinned: they don't depend only
230 // on the dominating test. The field _control_dependency below records
231 // whether that node depends only on the dominating test.
232 // Pinned and UnknownControl are similar, but differ in that Pinned
233 // loads are not allowed to float across safepoints, whereas UnknownControl
234 // loads are allowed to do that. Therefore, Pinned is stricter.
235 enum ControlDependency {
236 Pinned,
237 UnknownControl,
238 DependsOnlyOnTest
239 };
240
241 private:
242 // LoadNode::hash() doesn't take the _control_dependency field
243 // into account: If the graph already has a non-pinned LoadNode and
244 // we add a pinned LoadNode with the same inputs, it's safe for GVN
245 // to replace the pinned LoadNode with the non-pinned LoadNode,
246 // otherwise it wouldn't be safe to have a non pinned LoadNode with
247 // those inputs in the first place. If the graph already has a
248 // pinned LoadNode and we add a non pinned LoadNode with the same
249 // inputs, it's safe (but suboptimal) for GVN to replace the
250 // non-pinned LoadNode by the pinned LoadNode.
251 ControlDependency _control_dependency;
252
253 // On platforms with weak memory ordering (e.g., PPC) we distinguish
254 // loads that can be reordered, and such requiring acquire semantics to
255 // adhere to the Java specification. The required behaviour is stored in
256 // this field.
257 const MemOrd _mo;
258
259 AllocateNode* is_new_object_mark_load() const;
260
261 protected:
262 virtual bool cmp(const Node &n) const;
263 virtual uint size_of() const; // Size is bigger
264 // Should LoadNode::Ideal() attempt to remove control edges?
265 virtual bool can_remove_control() const;
266 const Type* const _type; // What kind of value is loaded?
267
268 virtual Node* find_previous_arraycopy(PhaseValues* phase, Node* ld_alloc, Node*& mem, bool can_see_stored_value) const;
269 Node* can_see_stored_value_through_membars(Node* st, PhaseValues* phase) const;
270 public:
271
272 LoadNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *rt, MemOrd mo, ControlDependency control_dependency)
273 : MemNode(c,mem,adr,at), _control_dependency(control_dependency), _mo(mo), _type(rt) {
274 init_class_id(Class_Load);
275 }
276 inline bool is_unordered() const { return !is_acquire(); }
277 inline bool is_acquire() const {
278 assert(_mo == unordered || _mo == acquire, "unexpected");
279 return _mo == acquire;
280 }
281 inline bool is_unsigned() const {
282 int lop = Opcode();
283 return (lop == Op_LoadUB) || (lop == Op_LoadUS);
284 }
285
286 // Polymorphic factory method:
287 static Node* make(PhaseGVN& gvn, Node* c, Node* mem, Node* adr,
288 const TypePtr* at, const Type* rt, BasicType bt,
289 MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest,
290 bool require_atomic_access = false, bool unaligned = false, bool mismatched = false, bool unsafe = false,
291 uint8_t barrier_data = 0);
292
293 virtual uint hash() const; // Check the type
294
295 // Handle algebraic identities here. If we have an identity, return the Node
296 // we are equivalent to. We look for Load of a Store.
297 virtual Node* Identity(PhaseGVN* phase);
298
299 // If the load is from Field memory and the pointer is non-null, it might be possible to
300 // zero out the control input.
301 // If the offset is constant and the base is an object allocation,
302 // try to hook me up to the exact initializing store.
303 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
304
305 // Return true if it's possible to split the Load through a Phi merging the bases
306 bool can_split_through_phi_base(PhaseGVN *phase);
307
308 // Split instance field load through Phi.
309 Node* split_through_phi(PhaseGVN *phase, bool ignore_missing_instance_id = false);
310
311 // Recover original value from boxed values
312 Node *eliminate_autobox(PhaseIterGVN *igvn);
313
314 // Compute a new Type for this node. Basically we just do the pre-check,
315 // then call the virtual add() to set the type.
316 virtual const Type* Value(PhaseGVN* phase) const;
317
318 // Common methods for LoadKlass and LoadNKlass nodes.
319 const Type* klass_value_common(PhaseGVN* phase) const;
320 Node* klass_identity_common(PhaseGVN* phase);
321
322 virtual uint ideal_reg() const;
323 virtual const Type *bottom_type() const;
324 // Following method is copied from TypeNode:
325 void set_type(const Type* t) {
326 assert(t != nullptr, "sanity");
327 DEBUG_ONLY(uint check_hash = (VerifyHashTableKeys && _hash_lock) ? hash() : NO_HASH);
328 *(const Type**)&_type = t; // cast away const-ness
329 // If this node is in the hash table, make sure it doesn't need a rehash.
330 assert(check_hash == NO_HASH || check_hash == hash(), "type change must preserve hash code");
331 }
332 const Type* type() const { assert(_type != nullptr, "sanity"); return _type; };
333
334 // Do not match memory edge
335 virtual uint match_edge(uint idx) const;
336
337 // Map a load opcode to its corresponding store opcode.
338 virtual int store_Opcode() const = 0;
339
340 // Check if the load's memory input is a Phi node with the same control.
341 bool is_instance_field_load_with_local_phi(Node* ctrl);
342
343 Node* convert_to_unsigned_load(PhaseGVN& gvn);
344 Node* convert_to_signed_load(PhaseGVN& gvn);
345
346 bool has_reinterpret_variant(const Type* rt);
347 Node* convert_to_reinterpret_load(PhaseGVN& gvn, const Type* rt);
348
349 ControlDependency control_dependency() const { return _control_dependency; }
350 bool has_unknown_control_dependency() const { return _control_dependency == UnknownControl; }
351 bool has_pinned_control_dependency() const { return _control_dependency == Pinned; }
352
353 #ifndef PRODUCT
354 virtual void dump_spec(outputStream *st) const;
355 #endif
356 #ifdef ASSERT
357 // Helper function to allow a raw load without control edge for some cases
358 static bool is_immutable_value(Node* adr);
359 #endif
360 protected:
361 const Type* load_array_final_field(const TypeKlassPtr *tkls,
362 ciKlass* klass) const;
363
364 Node* can_see_arraycopy_value(Node* st, PhaseGVN* phase) const;
365
366 private:
367 // depends_only_on_test is almost always true, and needs to be almost always
368 // true to enable key hoisting & commoning optimizations. However, for the
369 // special case of RawPtr loads from TLS top & end, and other loads performed by
370 // GC barriers, the control edge carries the dependence preventing hoisting past
371 // a Safepoint instead of the memory edge. (An unfortunate consequence of having
372 // Safepoints not set Raw Memory; itself an unfortunate consequence of having Nodes
373 // which produce results (new raw memory state) inside of loops preventing all
374 // manner of other optimizations). Basically, it's ugly but so is the alternative.
375 // See comment in macro.cpp, around line 125 expand_allocate_common().
376 virtual bool depends_only_on_test_impl() const {
377 return adr_type() != TypeRawPtr::BOTTOM && _control_dependency == DependsOnlyOnTest;
378 }
379
380 LoadNode* clone_pinned() const;
381 virtual LoadNode* pin_node_under_control_impl() const;
382 };
383
384 //------------------------------LoadBNode--------------------------------------
385 // Load a byte (8bits signed) from memory
386 class LoadBNode : public LoadNode {
387 public:
388 LoadBNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
389 : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
390 virtual int Opcode() const;
391 virtual uint ideal_reg() const { return Op_RegI; }
392 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
393 virtual const Type* Value(PhaseGVN* phase) const;
394 virtual int store_Opcode() const { return Op_StoreB; }
395 virtual BasicType value_basic_type() const { return T_BYTE; }
396 };
397
398 //------------------------------LoadUBNode-------------------------------------
399 // Load a unsigned byte (8bits unsigned) from memory
400 class LoadUBNode : public LoadNode {
401 public:
402 LoadUBNode(Node* c, Node* mem, Node* adr, const TypePtr* at, const TypeInt* ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
403 : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
404 virtual int Opcode() const;
405 virtual uint ideal_reg() const { return Op_RegI; }
406 virtual Node* Ideal(PhaseGVN *phase, bool can_reshape);
407 virtual const Type* Value(PhaseGVN* phase) const;
408 virtual int store_Opcode() const { return Op_StoreB; }
409 virtual BasicType value_basic_type() const { return T_BYTE; }
410 };
411
412 //------------------------------LoadUSNode-------------------------------------
413 // Load an unsigned short/char (16bits unsigned) from memory
414 class LoadUSNode : public LoadNode {
415 public:
416 LoadUSNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
417 : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
418 virtual int Opcode() const;
419 virtual uint ideal_reg() const { return Op_RegI; }
420 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
421 virtual const Type* Value(PhaseGVN* phase) const;
422 virtual int store_Opcode() const { return Op_StoreC; }
423 virtual BasicType value_basic_type() const { return T_CHAR; }
424 };
425
426 //------------------------------LoadSNode--------------------------------------
427 // Load a short (16bits signed) from memory
428 class LoadSNode : public LoadNode {
429 public:
430 LoadSNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
431 : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
432 virtual int Opcode() const;
433 virtual uint ideal_reg() const { return Op_RegI; }
434 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
435 virtual const Type* Value(PhaseGVN* phase) const;
436 virtual int store_Opcode() const { return Op_StoreC; }
437 virtual BasicType value_basic_type() const { return T_SHORT; }
438 };
439
440 //------------------------------LoadINode--------------------------------------
441 // Load an integer from memory
442 class LoadINode : public LoadNode {
443 public:
444 LoadINode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
445 : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
446 virtual int Opcode() const;
447 virtual uint ideal_reg() const { return Op_RegI; }
448 virtual int store_Opcode() const { return Op_StoreI; }
449 virtual BasicType value_basic_type() const { return T_INT; }
450 };
451
452 //------------------------------LoadRangeNode----------------------------------
453 // Load an array length from the array
454 class LoadRangeNode : public LoadINode {
455 public:
456 LoadRangeNode(Node *c, Node *mem, Node *adr, const TypeInt *ti = TypeInt::POS)
457 : LoadINode(c, mem, adr, TypeAryPtr::RANGE, ti, MemNode::unordered) {}
458 virtual int Opcode() const;
459 virtual const Type* Value(PhaseGVN* phase) const;
460 virtual Node* Identity(PhaseGVN* phase);
461 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
462 };
463
464 //------------------------------LoadLNode--------------------------------------
465 // Load a long from memory
466 class LoadLNode : public LoadNode {
467 virtual uint hash() const { return LoadNode::hash() + _require_atomic_access; }
468 virtual bool cmp( const Node &n ) const {
469 return _require_atomic_access == ((LoadLNode&)n)._require_atomic_access
470 && LoadNode::cmp(n);
471 }
472 virtual uint size_of() const { return sizeof(*this); }
473 const bool _require_atomic_access; // is piecewise load forbidden?
474
475 public:
476 LoadLNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeLong *tl,
477 MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest, bool require_atomic_access = false)
478 : LoadNode(c, mem, adr, at, tl, mo, control_dependency), _require_atomic_access(require_atomic_access) {}
479 virtual int Opcode() const;
480 virtual uint ideal_reg() const { return Op_RegL; }
481 virtual int store_Opcode() const { return Op_StoreL; }
482 virtual BasicType value_basic_type() const { return T_LONG; }
483 bool require_atomic_access() const { return _require_atomic_access; }
484
485 #ifndef PRODUCT
486 virtual void dump_spec(outputStream *st) const {
487 LoadNode::dump_spec(st);
488 if (_require_atomic_access) st->print(" Atomic!");
489 }
490 #endif
491 };
492
493 //------------------------------LoadL_unalignedNode----------------------------
494 // Load a long from unaligned memory
495 class LoadL_unalignedNode : public LoadLNode {
496 public:
497 LoadL_unalignedNode(Node *c, Node *mem, Node *adr, const TypePtr* at, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
498 : LoadLNode(c, mem, adr, at, TypeLong::LONG, mo, control_dependency) {}
499 virtual int Opcode() const;
500 };
501
502 //------------------------------LoadFNode--------------------------------------
503 // Load a float (64 bits) from memory
504 class LoadFNode : public LoadNode {
505 public:
506 LoadFNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
507 : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
508 virtual int Opcode() const;
509 virtual uint ideal_reg() const { return Op_RegF; }
510 virtual int store_Opcode() const { return Op_StoreF; }
511 virtual BasicType value_basic_type() const { return T_FLOAT; }
512 };
513
514 //------------------------------LoadDNode--------------------------------------
515 // Load a double (64 bits) from memory
516 class LoadDNode : public LoadNode {
517 virtual uint hash() const { return LoadNode::hash() + _require_atomic_access; }
518 virtual bool cmp( const Node &n ) const {
519 return _require_atomic_access == ((LoadDNode&)n)._require_atomic_access
520 && LoadNode::cmp(n);
521 }
522 virtual uint size_of() const { return sizeof(*this); }
523 const bool _require_atomic_access; // is piecewise load forbidden?
524
525 public:
526 LoadDNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *t,
527 MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest, bool require_atomic_access = false)
528 : LoadNode(c, mem, adr, at, t, mo, control_dependency), _require_atomic_access(require_atomic_access) {}
529 virtual int Opcode() const;
530 virtual uint ideal_reg() const { return Op_RegD; }
531 virtual int store_Opcode() const { return Op_StoreD; }
532 virtual BasicType value_basic_type() const { return T_DOUBLE; }
533 bool require_atomic_access() const { return _require_atomic_access; }
534
535 #ifndef PRODUCT
536 virtual void dump_spec(outputStream *st) const {
537 LoadNode::dump_spec(st);
538 if (_require_atomic_access) st->print(" Atomic!");
539 }
540 #endif
541 };
542
543 //------------------------------LoadD_unalignedNode----------------------------
544 // Load a double from unaligned memory
545 class LoadD_unalignedNode : public LoadDNode {
546 public:
547 LoadD_unalignedNode(Node *c, Node *mem, Node *adr, const TypePtr* at, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
548 : LoadDNode(c, mem, adr, at, Type::DOUBLE, mo, control_dependency) {}
549 virtual int Opcode() const;
550 };
551
552 //------------------------------LoadPNode--------------------------------------
553 // Load a pointer from memory (either object or array)
554 class LoadPNode : public LoadNode {
555 public:
556 LoadPNode(Node *c, Node *mem, Node *adr, const TypePtr *at, const TypePtr* t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
557 : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
558 virtual int Opcode() const;
559 virtual uint ideal_reg() const { return Op_RegP; }
560 virtual int store_Opcode() const { return Op_StoreP; }
561 virtual BasicType value_basic_type() const { return T_ADDRESS; }
562 };
563
564
565 //------------------------------LoadNNode--------------------------------------
566 // Load a narrow oop from memory (either object or array)
567 class LoadNNode : public LoadNode {
568 public:
569 LoadNNode(Node *c, Node *mem, Node *adr, const TypePtr *at, const Type* t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
570 : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
571 virtual int Opcode() const;
572 virtual uint ideal_reg() const { return Op_RegN; }
573 virtual int store_Opcode() const { return Op_StoreN; }
574 virtual BasicType value_basic_type() const { return T_NARROWOOP; }
575 };
576
577 //------------------------------LoadKlassNode----------------------------------
578 // Load a Klass from an object
579 class LoadKlassNode : public LoadPNode {
580 private:
581 LoadKlassNode(Node* mem, Node* adr, const TypePtr* at, const TypeKlassPtr* tk, MemOrd mo)
582 : LoadPNode(nullptr, mem, adr, at, tk, mo) {}
583
584 public:
585 virtual int Opcode() const;
586 virtual const Type* Value(PhaseGVN* phase) const;
587 virtual Node* Identity(PhaseGVN* phase);
588
589 // Polymorphic factory method:
590 static Node* make(PhaseGVN& gvn, Node* mem, Node* adr, const TypePtr* at,
591 const TypeKlassPtr* tk = TypeInstKlassPtr::OBJECT);
592 };
593
594 //------------------------------LoadNKlassNode---------------------------------
595 // Load a narrow Klass from an object.
596 // With compact headers, the input address (adr) does not point at the exact
597 // header position where the (narrow) class pointer is located, but into the
598 // middle of the mark word (see oopDesc::klass_offset_in_bytes()). This node
599 // implicitly shifts the loaded value (markWord::klass_shift_at_offset bits) to
600 // extract the actual class pointer. C2's type system is agnostic on whether the
601 // input address directly points into the class pointer.
602 class LoadNKlassNode : public LoadNNode {
603 private:
604 friend Node* LoadKlassNode::make(PhaseGVN&, Node*, Node*, const TypePtr*, const TypeKlassPtr*);
605 LoadNKlassNode(Node* mem, Node* adr, const TypePtr* at, const TypeNarrowKlass* tk, MemOrd mo)
606 : LoadNNode(nullptr, mem, adr, at, tk, mo) {}
607
608 public:
609 virtual int Opcode() const;
610 virtual uint ideal_reg() const { return Op_RegN; }
611 virtual int store_Opcode() const { return Op_StoreNKlass; }
612 virtual BasicType value_basic_type() const { return T_NARROWKLASS; }
613
614 virtual const Type* Value(PhaseGVN* phase) const;
615 virtual Node* Identity(PhaseGVN* phase);
616 };
617
618
619 //------------------------------StoreNode--------------------------------------
620 // Store value; requires Store, Address and Value
621 class StoreNode : public MemNode {
622 private:
623 // On platforms with weak memory ordering (e.g., PPC) we distinguish
624 // stores that can be reordered, and such requiring release semantics to
625 // adhere to the Java specification. The required behaviour is stored in
626 // this field.
627 const MemOrd _mo;
628 // Needed for proper cloning.
629 virtual uint size_of() const { return sizeof(*this); }
630 protected:
631 virtual bool cmp( const Node &n ) const;
632
633 Node *Ideal_masked_input (PhaseGVN *phase, uint mask);
634 Node* Ideal_sign_extended_input(PhaseGVN* phase, int num_rejected_bits);
635
636 public:
637 // We must ensure that stores of object references will be visible
638 // only after the object's initialization. So the callers of this
639 // procedure must indicate that the store requires `release'
640 // semantics, if the stored value is an object reference that might
641 // point to a new object and may become externally visible.
642 StoreNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
643 : MemNode(c, mem, adr, at, val), _mo(mo) {
644 init_class_id(Class_Store);
645 }
646 StoreNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, Node *oop_store, MemOrd mo)
647 : MemNode(c, mem, adr, at, val, oop_store), _mo(mo) {
648 init_class_id(Class_Store);
649 }
650
651 inline bool is_unordered() const { return !is_release(); }
652 inline bool is_release() const {
653 assert((_mo == unordered || _mo == release), "unexpected");
654 return _mo == release;
655 }
656
657 // Conservatively release stores of object references in order to
658 // ensure visibility of object initialization.
659 static inline MemOrd release_if_reference(const BasicType t) {
660 #ifdef AARCH64
661 // AArch64 doesn't need a release store here because object
662 // initialization contains the necessary barriers.
663 return unordered;
664 #else
665 const MemOrd mo = (t == T_ARRAY ||
666 t == T_ADDRESS || // Might be the address of an object reference (`boxing').
667 t == T_OBJECT) ? release : unordered;
668 return mo;
669 #endif
670 }
671
672 // Polymorphic factory method
673 //
674 // We must ensure that stores of object references will be visible
675 // only after the object's initialization. So the callers of this
676 // procedure must indicate that the store requires `release'
677 // semantics, if the stored value is an object reference that might
678 // point to a new object and may become externally visible.
679 static StoreNode* make(PhaseGVN& gvn, Node* c, Node* mem, Node* adr,
680 const TypePtr* at, Node* val, BasicType bt,
681 MemOrd mo, bool require_atomic_access = false);
682
683 virtual uint hash() const; // Check the type
684
685 // If the store is to Field memory and the pointer is non-null, we can
686 // zero out the control input.
687 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
688
689 // Compute a new Type for this node. Basically we just do the pre-check,
690 // then call the virtual add() to set the type.
691 virtual const Type* Value(PhaseGVN* phase) const;
692
693 // Check for identity function on memory (Load then Store at same address)
694 virtual Node* Identity(PhaseGVN* phase);
695
696 // Do not match memory edge
697 virtual uint match_edge(uint idx) const;
698
699 virtual const Type *bottom_type() const; // returns Type::MEMORY
700
701 // Map a store opcode to its corresponding own opcode, trivially.
702 virtual int store_Opcode() const { return Opcode(); }
703
704 // have all possible loads of the value stored been optimized away?
705 bool value_never_loaded(PhaseValues* phase) const;
706
707 bool has_reinterpret_variant(const Type* vt);
708 Node* convert_to_reinterpret_store(PhaseGVN& gvn, Node* val, const Type* vt);
709
710 MemBarNode* trailing_membar() const;
711
712 private:
713 virtual bool depends_only_on_test_impl() const { return false; }
714 };
715
716 //------------------------------StoreBNode-------------------------------------
717 // Store byte to memory
718 class StoreBNode : public StoreNode {
719 public:
720 StoreBNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
721 : StoreNode(c, mem, adr, at, val, mo) {}
722 virtual int Opcode() const;
723 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
724 virtual BasicType value_basic_type() const { return T_BYTE; }
725 };
726
727 //------------------------------StoreCNode-------------------------------------
728 // Store char/short to memory
729 class StoreCNode : public StoreNode {
730 public:
731 StoreCNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
732 : StoreNode(c, mem, adr, at, val, mo) {}
733 virtual int Opcode() const;
734 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
735 virtual BasicType value_basic_type() const { return T_CHAR; }
736 };
737
738 //------------------------------StoreINode-------------------------------------
739 // Store int to memory
740 class StoreINode : public StoreNode {
741 public:
742 StoreINode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
743 : StoreNode(c, mem, adr, at, val, mo) {}
744 virtual int Opcode() const;
745 virtual BasicType value_basic_type() const { return T_INT; }
746 };
747
748 //------------------------------StoreLNode-------------------------------------
749 // Store long to memory
750 class StoreLNode : public StoreNode {
751 virtual uint hash() const { return StoreNode::hash() + _require_atomic_access; }
752 virtual bool cmp( const Node &n ) const {
753 return _require_atomic_access == ((StoreLNode&)n)._require_atomic_access
754 && StoreNode::cmp(n);
755 }
756 virtual uint size_of() const { return sizeof(*this); }
757 const bool _require_atomic_access; // is piecewise store forbidden?
758
759 public:
760 StoreLNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo, bool require_atomic_access = false)
761 : StoreNode(c, mem, adr, at, val, mo), _require_atomic_access(require_atomic_access) {}
762 virtual int Opcode() const;
763 virtual BasicType value_basic_type() const { return T_LONG; }
764 bool require_atomic_access() const { return _require_atomic_access; }
765
766 #ifndef PRODUCT
767 virtual void dump_spec(outputStream *st) const {
768 StoreNode::dump_spec(st);
769 if (_require_atomic_access) st->print(" Atomic!");
770 }
771 #endif
772 };
773
774 //------------------------------StoreFNode-------------------------------------
775 // Store float to memory
776 class StoreFNode : public StoreNode {
777 public:
778 StoreFNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
779 : StoreNode(c, mem, adr, at, val, mo) {}
780 virtual int Opcode() const;
781 virtual BasicType value_basic_type() const { return T_FLOAT; }
782 };
783
784 //------------------------------StoreDNode-------------------------------------
785 // Store double to memory
786 class StoreDNode : public StoreNode {
787 virtual uint hash() const { return StoreNode::hash() + _require_atomic_access; }
788 virtual bool cmp( const Node &n ) const {
789 return _require_atomic_access == ((StoreDNode&)n)._require_atomic_access
790 && StoreNode::cmp(n);
791 }
792 virtual uint size_of() const { return sizeof(*this); }
793 const bool _require_atomic_access; // is piecewise store forbidden?
794 public:
795 StoreDNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val,
796 MemOrd mo, bool require_atomic_access = false)
797 : StoreNode(c, mem, adr, at, val, mo), _require_atomic_access(require_atomic_access) {}
798 virtual int Opcode() const;
799 virtual BasicType value_basic_type() const { return T_DOUBLE; }
800 bool require_atomic_access() const { return _require_atomic_access; }
801
802 #ifndef PRODUCT
803 virtual void dump_spec(outputStream *st) const {
804 StoreNode::dump_spec(st);
805 if (_require_atomic_access) st->print(" Atomic!");
806 }
807 #endif
808
809 };
810
811 //------------------------------StorePNode-------------------------------------
812 // Store pointer to memory
813 class StorePNode : public StoreNode {
814 public:
815 StorePNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
816 : StoreNode(c, mem, adr, at, val, mo) {}
817 virtual int Opcode() const;
818 virtual BasicType value_basic_type() const { return T_ADDRESS; }
819 };
820
821 //------------------------------StoreNNode-------------------------------------
822 // Store narrow oop to memory
823 class StoreNNode : public StoreNode {
824 public:
825 StoreNNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
826 : StoreNode(c, mem, adr, at, val, mo) {}
827 virtual int Opcode() const;
828 virtual BasicType value_basic_type() const { return T_NARROWOOP; }
829 };
830
831 //------------------------------StoreNKlassNode--------------------------------------
832 // Store narrow klass to memory
833 class StoreNKlassNode : public StoreNNode {
834 public:
835 StoreNKlassNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
836 : StoreNNode(c, mem, adr, at, val, mo) {}
837 virtual int Opcode() const;
838 virtual BasicType value_basic_type() const { return T_NARROWKLASS; }
839 };
840
841 //------------------------------SCMemProjNode---------------------------------------
842 // This class defines a projection of the memory state of a store conditional node.
843 // These nodes return a value, but also update memory.
844 class SCMemProjNode : public ProjNode {
845 public:
846 enum {SCMEMPROJCON = (uint)-2};
847 SCMemProjNode( Node *src) : ProjNode( src, SCMEMPROJCON) { }
848 virtual int Opcode() const;
849 virtual bool is_CFG() const { return false; }
850 virtual const Type *bottom_type() const {return Type::MEMORY;}
851 virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
852 virtual const Type* Value(PhaseGVN* phase) const;
853 #ifndef PRODUCT
854 virtual void dump_spec(outputStream *st) const {};
855 #endif
856 };
857
858 //------------------------------LoadStoreNode---------------------------
859 // Note: is_Mem() method returns 'true' for this class.
860 class LoadStoreNode : public Node {
861 private:
862 const Type* const _type; // What kind of value is loaded?
863 uint8_t _barrier_data; // Bit field with barrier information
864 virtual uint size_of() const; // Size is bigger
865 #ifdef ASSERT
866 const TypePtr* _adr_type; // What kind of memory is being addressed?
867 #endif // ASSERT
868 public:
869 LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* rt, uint required );
870 virtual uint match_edge(uint idx) const { return idx == MemNode::Address || idx == MemNode::ValueIn; }
871
872 virtual const Type *bottom_type() const { return _type; }
873 virtual uint ideal_reg() const;
874 virtual const TypePtr* adr_type() const;
875 virtual const Type* Value(PhaseGVN* phase) const;
876
877 bool result_not_used() const;
878 MemBarNode* trailing_membar() const;
879
880 uint8_t barrier_data() { return _barrier_data; }
881 void set_barrier_data(uint8_t barrier_data) { _barrier_data = barrier_data; }
882
883 private:
884 virtual bool depends_only_on_test_impl() const { return false; }
885 };
886
887 class LoadStoreConditionalNode : public LoadStoreNode {
888 public:
889 enum {
890 ExpectedIn = MemNode::ValueIn+1 // One more input than MemNode
891 };
892 LoadStoreConditionalNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex);
893 virtual const Type* Value(PhaseGVN* phase) const;
894 };
895
896 class CompareAndSwapNode : public LoadStoreConditionalNode {
897 private:
898 const MemNode::MemOrd _mem_ord;
899 public:
900 CompareAndSwapNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : LoadStoreConditionalNode(c, mem, adr, val, ex), _mem_ord(mem_ord) {}
901 MemNode::MemOrd order() const {
902 return _mem_ord;
903 }
904 virtual uint size_of() const { return sizeof(*this); }
905 };
906
907 class CompareAndExchangeNode : public LoadStoreNode {
908 private:
909 const MemNode::MemOrd _mem_ord;
910 public:
911 enum {
912 ExpectedIn = MemNode::ValueIn+1 // One more input than MemNode
913 };
914 CompareAndExchangeNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord, const TypePtr* at, const Type* t) :
915 LoadStoreNode(c, mem, adr, val, at, t, 5), _mem_ord(mem_ord) {
916 init_req(ExpectedIn, ex );
917 }
918
919 MemNode::MemOrd order() const {
920 return _mem_ord;
921 }
922 virtual uint size_of() const { return sizeof(*this); }
923 };
924
925 //------------------------------CompareAndSwapBNode---------------------------
926 class CompareAndSwapBNode : public CompareAndSwapNode {
927 public:
928 CompareAndSwapBNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
929 virtual int Opcode() const;
930 };
931
932 //------------------------------CompareAndSwapSNode---------------------------
933 class CompareAndSwapSNode : public CompareAndSwapNode {
934 public:
935 CompareAndSwapSNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
936 virtual int Opcode() const;
937 };
938
939 //------------------------------CompareAndSwapINode---------------------------
940 class CompareAndSwapINode : public CompareAndSwapNode {
941 public:
942 CompareAndSwapINode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
943 virtual int Opcode() const;
944 };
945
946 //------------------------------CompareAndSwapLNode---------------------------
947 class CompareAndSwapLNode : public CompareAndSwapNode {
948 public:
949 CompareAndSwapLNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
950 virtual int Opcode() const;
951 };
952
953 //------------------------------CompareAndSwapPNode---------------------------
954 class CompareAndSwapPNode : public CompareAndSwapNode {
955 public:
956 CompareAndSwapPNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
957 virtual int Opcode() const;
958 };
959
960 //------------------------------CompareAndSwapNNode---------------------------
961 class CompareAndSwapNNode : public CompareAndSwapNode {
962 public:
963 CompareAndSwapNNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
964 virtual int Opcode() const;
965 };
966
967 //------------------------------WeakCompareAndSwapBNode---------------------------
968 class WeakCompareAndSwapBNode : public CompareAndSwapNode {
969 public:
970 WeakCompareAndSwapBNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
971 virtual int Opcode() const;
972 };
973
974 //------------------------------WeakCompareAndSwapSNode---------------------------
975 class WeakCompareAndSwapSNode : public CompareAndSwapNode {
976 public:
977 WeakCompareAndSwapSNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
978 virtual int Opcode() const;
979 };
980
981 //------------------------------WeakCompareAndSwapINode---------------------------
982 class WeakCompareAndSwapINode : public CompareAndSwapNode {
983 public:
984 WeakCompareAndSwapINode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
985 virtual int Opcode() const;
986 };
987
988 //------------------------------WeakCompareAndSwapLNode---------------------------
989 class WeakCompareAndSwapLNode : public CompareAndSwapNode {
990 public:
991 WeakCompareAndSwapLNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
992 virtual int Opcode() const;
993 };
994
995 //------------------------------WeakCompareAndSwapPNode---------------------------
996 class WeakCompareAndSwapPNode : public CompareAndSwapNode {
997 public:
998 WeakCompareAndSwapPNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
999 virtual int Opcode() const;
1000 };
1001
1002 //------------------------------WeakCompareAndSwapNNode---------------------------
1003 class WeakCompareAndSwapNNode : public CompareAndSwapNode {
1004 public:
1005 WeakCompareAndSwapNNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
1006 virtual int Opcode() const;
1007 };
1008
1009 //------------------------------CompareAndExchangeBNode---------------------------
1010 class CompareAndExchangeBNode : public CompareAndExchangeNode {
1011 public:
1012 CompareAndExchangeBNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, TypeInt::BYTE) { }
1013 virtual int Opcode() const;
1014 };
1015
1016
1017 //------------------------------CompareAndExchangeSNode---------------------------
1018 class CompareAndExchangeSNode : public CompareAndExchangeNode {
1019 public:
1020 CompareAndExchangeSNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, TypeInt::SHORT) { }
1021 virtual int Opcode() const;
1022 };
1023
1024 //------------------------------CompareAndExchangeLNode---------------------------
1025 class CompareAndExchangeLNode : public CompareAndExchangeNode {
1026 public:
1027 CompareAndExchangeLNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, TypeLong::LONG) { }
1028 virtual int Opcode() const;
1029 };
1030
1031
1032 //------------------------------CompareAndExchangeINode---------------------------
1033 class CompareAndExchangeINode : public CompareAndExchangeNode {
1034 public:
1035 CompareAndExchangeINode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, TypeInt::INT) { }
1036 virtual int Opcode() const;
1037 };
1038
1039
1040 //------------------------------CompareAndExchangePNode---------------------------
1041 class CompareAndExchangePNode : public CompareAndExchangeNode {
1042 public:
1043 CompareAndExchangePNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, t) { }
1044 virtual int Opcode() const;
1045 };
1046
1047 //------------------------------CompareAndExchangeNNode---------------------------
1048 class CompareAndExchangeNNode : public CompareAndExchangeNode {
1049 public:
1050 CompareAndExchangeNNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, t) { }
1051 virtual int Opcode() const;
1052 };
1053
1054 //------------------------------GetAndAddBNode---------------------------
1055 class GetAndAddBNode : public LoadStoreNode {
1056 public:
1057 GetAndAddBNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::BYTE, 4) { }
1058 virtual int Opcode() const;
1059 };
1060
1061 //------------------------------GetAndAddSNode---------------------------
1062 class GetAndAddSNode : public LoadStoreNode {
1063 public:
1064 GetAndAddSNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::SHORT, 4) { }
1065 virtual int Opcode() const;
1066 };
1067
1068 //------------------------------GetAndAddINode---------------------------
1069 class GetAndAddINode : public LoadStoreNode {
1070 public:
1071 GetAndAddINode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::INT, 4) { }
1072 virtual int Opcode() const;
1073 };
1074
1075 //------------------------------GetAndAddLNode---------------------------
1076 class GetAndAddLNode : public LoadStoreNode {
1077 public:
1078 GetAndAddLNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeLong::LONG, 4) { }
1079 virtual int Opcode() const;
1080 };
1081
1082 //------------------------------GetAndSetBNode---------------------------
1083 class GetAndSetBNode : public LoadStoreNode {
1084 public:
1085 GetAndSetBNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::BYTE, 4) { }
1086 virtual int Opcode() const;
1087 };
1088
1089 //------------------------------GetAndSetSNode---------------------------
1090 class GetAndSetSNode : public LoadStoreNode {
1091 public:
1092 GetAndSetSNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::SHORT, 4) { }
1093 virtual int Opcode() const;
1094 };
1095
1096 //------------------------------GetAndSetINode---------------------------
1097 class GetAndSetINode : public LoadStoreNode {
1098 public:
1099 GetAndSetINode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::INT, 4) { }
1100 virtual int Opcode() const;
1101 };
1102
1103 //------------------------------GetAndSetLNode---------------------------
1104 class GetAndSetLNode : public LoadStoreNode {
1105 public:
1106 GetAndSetLNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeLong::LONG, 4) { }
1107 virtual int Opcode() const;
1108 };
1109
1110 //------------------------------GetAndSetPNode---------------------------
1111 class GetAndSetPNode : public LoadStoreNode {
1112 public:
1113 GetAndSetPNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* t ) : LoadStoreNode(c, mem, adr, val, at, t, 4) { }
1114 virtual int Opcode() const;
1115 };
1116
1117 //------------------------------GetAndSetNNode---------------------------
1118 class GetAndSetNNode : public LoadStoreNode {
1119 public:
1120 GetAndSetNNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* t ) : LoadStoreNode(c, mem, adr, val, at, t, 4) { }
1121 virtual int Opcode() const;
1122 };
1123
1124 //------------------------------ClearArray-------------------------------------
1125 class ClearArrayNode: public Node {
1126 private:
1127 bool _is_large;
1128 static Node* make_address(Node* dest, Node* offset, bool raw_base, PhaseGVN* phase);
1129 public:
1130 ClearArrayNode( Node *ctrl, Node *arymem, Node *word_cnt, Node *base, bool is_large)
1131 : Node(ctrl,arymem,word_cnt,base), _is_large(is_large) {
1132 init_class_id(Class_ClearArray);
1133 }
1134 virtual int Opcode() const;
1135 virtual const Type *bottom_type() const { return Type::MEMORY; }
1136 // ClearArray modifies array elements, and so affects only the
1137 // array memory addressed by the bottom_type of its base address.
1138 virtual const class TypePtr *adr_type() const;
1139 virtual Node* Identity(PhaseGVN* phase);
1140 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1141 virtual uint match_edge(uint idx) const;
1142 bool is_large() const { return _is_large; }
1143 virtual uint size_of() const { return sizeof(ClearArrayNode); }
1144 virtual uint hash() const { return Node::hash() + _is_large; }
1145 virtual bool cmp(const Node& n) const {
1146 return Node::cmp(n) && _is_large == ((ClearArrayNode&)n).is_large();
1147 }
1148
1149 // Clear the given area of an object or array.
1150 // The start offset must always be aligned mod BytesPerInt.
1151 // The end offset must always be aligned mod BytesPerLong.
1152 // Return the new memory.
1153 static Node* clear_memory(Node* control, Node* mem, Node* dest,
1154 intptr_t start_offset,
1155 intptr_t end_offset,
1156 bool raw_base,
1157 PhaseGVN* phase);
1158 static Node* clear_memory(Node* control, Node* mem, Node* dest,
1159 intptr_t start_offset,
1160 Node* end_offset,
1161 bool raw_base,
1162 PhaseGVN* phase);
1163 static Node* clear_memory(Node* control, Node* mem, Node* dest,
1164 Node* start_offset,
1165 Node* end_offset,
1166 bool raw_base,
1167 PhaseGVN* phase);
1168 // Return allocation input memory edge if it is different instance
1169 // or itself if it is the one we are looking for.
1170 static bool step_through(Node** np, uint instance_id, PhaseValues* phase);
1171
1172 private:
1173 virtual bool depends_only_on_test_impl() const { return false; }
1174 };
1175
1176 //------------------------------MemBar-----------------------------------------
1177 // There are different flavors of Memory Barriers to match the Java Memory
1178 // Model. Monitor-enter and volatile-load act as Acquires: no following ref
1179 // can be moved to before them. We insert a MemBar-Acquire after a FastLock or
1180 // volatile-load. Monitor-exit and volatile-store act as Release: no
1181 // preceding ref can be moved to after them. We insert a MemBar-Release
1182 // before a FastUnlock or volatile-store. All volatiles need to be
1183 // serialized, so we follow all volatile-stores with a MemBar-Volatile to
1184 // separate it from any following volatile-load.
1185 class MemBarNode: public MultiNode {
1186 virtual uint hash() const ; // { return NO_HASH; }
1187 virtual bool cmp( const Node &n ) const ; // Always fail, except on self
1188
1189 virtual uint size_of() const { return sizeof(*this); }
1190 // Memory type this node is serializing. Usually either rawptr or bottom.
1191 const TypePtr* _adr_type;
1192
1193 // How is this membar related to a nearby memory access?
1194 enum {
1195 Standalone,
1196 TrailingLoad,
1197 TrailingStore,
1198 LeadingStore,
1199 TrailingLoadStore,
1200 LeadingLoadStore,
1201 TrailingExpandedArrayCopy
1202 } _kind;
1203
1204 #ifdef ASSERT
1205 uint _pair_idx;
1206 #endif
1207
1208 public:
1209 enum {
1210 Precedent = TypeFunc::Parms // optional edge to force precedence
1211 };
1212 MemBarNode(Compile* C, int alias_idx, Node* precedent);
1213 virtual int Opcode() const = 0;
1214 virtual const class TypePtr *adr_type() const { return _adr_type; }
1215 virtual const Type* Value(PhaseGVN* phase) const;
1216 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1217 virtual uint match_edge(uint idx) const { return 0; }
1218 virtual const Type *bottom_type() const { return TypeTuple::MEMBAR; }
1219 virtual Node *match( const ProjNode *proj, const Matcher *m );
1220 // Factory method. Builds a wide or narrow membar.
1221 // Optional 'precedent' becomes an extra edge if not null.
1222 static MemBarNode* make(Compile* C, int opcode,
1223 int alias_idx = Compile::AliasIdxBot,
1224 Node* precedent = nullptr);
1225
1226 MemBarNode* trailing_membar() const;
1227 MemBarNode* leading_membar() const;
1228
1229 void set_trailing_load() { _kind = TrailingLoad; }
1230 bool trailing_load() const { return _kind == TrailingLoad; }
1231 bool trailing_store() const { return _kind == TrailingStore; }
1232 bool leading_store() const { return _kind == LeadingStore; }
1233 bool trailing_load_store() const { return _kind == TrailingLoadStore; }
1234 bool leading_load_store() const { return _kind == LeadingLoadStore; }
1235 bool trailing() const { return _kind == TrailingLoad || _kind == TrailingStore || _kind == TrailingLoadStore; }
1236 bool leading() const { return _kind == LeadingStore || _kind == LeadingLoadStore; }
1237 bool standalone() const { return _kind == Standalone; }
1238 void set_trailing_expanded_array_copy() { _kind = TrailingExpandedArrayCopy; }
1239 bool trailing_expanded_array_copy() const { return _kind == TrailingExpandedArrayCopy; }
1240
1241 static void set_store_pair(MemBarNode* leading, MemBarNode* trailing);
1242 static void set_load_store_pair(MemBarNode* leading, MemBarNode* trailing);
1243
1244 void remove(PhaseIterGVN *igvn);
1245 };
1246
1247 // "Acquire" - no following ref can move before (but earlier refs can
1248 // follow, like an early Load stalled in cache). Requires multi-cpu
1249 // visibility. Inserted after a volatile load.
1250 class MemBarAcquireNode: public MemBarNode {
1251 public:
1252 MemBarAcquireNode(Compile* C, int alias_idx, Node* precedent)
1253 : MemBarNode(C, alias_idx, precedent) {}
1254 virtual int Opcode() const;
1255 };
1256
1257 // "Acquire" - no following ref can move before (but earlier refs can
1258 // follow, like an early Load stalled in cache). Requires multi-cpu
1259 // visibility. Inserted independent of any load, as required
1260 // for intrinsic Unsafe.loadFence().
1261 class LoadFenceNode: public MemBarNode {
1262 public:
1263 LoadFenceNode(Compile* C, int alias_idx, Node* precedent)
1264 : MemBarNode(C, alias_idx, precedent) {}
1265 virtual int Opcode() const;
1266 };
1267
1268 // "Release" - no earlier ref can move after (but later refs can move
1269 // up, like a speculative pipelined cache-hitting Load). Requires
1270 // multi-cpu visibility. Inserted before a volatile store.
1271 class MemBarReleaseNode: public MemBarNode {
1272 public:
1273 MemBarReleaseNode(Compile* C, int alias_idx, Node* precedent)
1274 : MemBarNode(C, alias_idx, precedent) {}
1275 virtual int Opcode() const;
1276 };
1277
1278 // "Release" - no earlier ref can move after (but later refs can move
1279 // up, like a speculative pipelined cache-hitting Load). Requires
1280 // multi-cpu visibility. Inserted independent of any store, as required
1281 // for intrinsic Unsafe.storeFence().
1282 class StoreFenceNode: public MemBarNode {
1283 public:
1284 StoreFenceNode(Compile* C, int alias_idx, Node* precedent)
1285 : MemBarNode(C, alias_idx, precedent) {}
1286 virtual int Opcode() const;
1287 };
1288
1289 // "Acquire" - no following ref can move before (but earlier refs can
1290 // follow, like an early Load stalled in cache). Requires multi-cpu
1291 // visibility. Inserted after a FastLock.
1292 class MemBarAcquireLockNode: public MemBarNode {
1293 public:
1294 MemBarAcquireLockNode(Compile* C, int alias_idx, Node* precedent)
1295 : MemBarNode(C, alias_idx, precedent) {}
1296 virtual int Opcode() const;
1297 };
1298
1299 // "Release" - no earlier ref can move after (but later refs can move
1300 // up, like a speculative pipelined cache-hitting Load). Requires
1301 // multi-cpu visibility. Inserted before a FastUnLock.
1302 class MemBarReleaseLockNode: public MemBarNode {
1303 public:
1304 MemBarReleaseLockNode(Compile* C, int alias_idx, Node* precedent)
1305 : MemBarNode(C, alias_idx, precedent) {}
1306 virtual int Opcode() const;
1307 };
1308
1309 class MemBarStoreStoreNode: public MemBarNode {
1310 public:
1311 MemBarStoreStoreNode(Compile* C, int alias_idx, Node* precedent)
1312 : MemBarNode(C, alias_idx, precedent) {
1313 init_class_id(Class_MemBarStoreStore);
1314 }
1315 virtual int Opcode() const;
1316 };
1317
1318 class StoreStoreFenceNode: public MemBarNode {
1319 public:
1320 StoreStoreFenceNode(Compile* C, int alias_idx, Node* precedent)
1321 : MemBarNode(C, alias_idx, precedent) {}
1322 virtual int Opcode() const;
1323 };
1324
1325 class MemBarStoreLoadNode : public MemBarNode {
1326 public:
1327 MemBarStoreLoadNode(Compile* C, int alias_idx, Node* precedent)
1328 : MemBarNode(C, alias_idx, precedent) {}
1329 virtual int Opcode() const;
1330 };
1331
1332 // Ordering between a volatile store and a following volatile load.
1333 // Requires multi-CPU visibility?
1334 class MemBarVolatileNode: public MemBarNode {
1335 public:
1336 MemBarVolatileNode(Compile* C, int alias_idx, Node* precedent)
1337 : MemBarNode(C, alias_idx, precedent) {}
1338 virtual int Opcode() const;
1339 };
1340
1341 // A full barrier blocks all loads and stores from moving across it
1342 class MemBarFullNode : public MemBarNode {
1343 public:
1344 MemBarFullNode(Compile* C, int alias_idx, Node* precedent)
1345 : MemBarNode(C, alias_idx, precedent) {}
1346 virtual int Opcode() const;
1347 };
1348
1349 // Ordering within the same CPU. Used to order unsafe memory references
1350 // inside the compiler when we lack alias info. Not needed "outside" the
1351 // compiler because the CPU does all the ordering for us.
1352 class MemBarCPUOrderNode: public MemBarNode {
1353 public:
1354 MemBarCPUOrderNode(Compile* C, int alias_idx, Node* precedent)
1355 : MemBarNode(C, alias_idx, precedent) {}
1356 virtual int Opcode() const;
1357 virtual uint ideal_reg() const { return 0; } // not matched in the AD file
1358 };
1359
1360 class OnSpinWaitNode: public MemBarNode {
1361 public:
1362 OnSpinWaitNode(Compile* C, int alias_idx, Node* precedent)
1363 : MemBarNode(C, alias_idx, precedent) {}
1364 virtual int Opcode() const;
1365 };
1366
1367 // Isolation of object setup after an AllocateNode and before next safepoint.
1368 // (See comment in memnode.cpp near InitializeNode::InitializeNode for semantics.)
1369 class InitializeNode: public MemBarNode {
1370 friend class AllocateNode;
1371
1372 enum {
1373 Incomplete = 0,
1374 Complete = 1,
1375 WithArraycopy = 2
1376 };
1377 int _is_complete;
1378
1379 bool _does_not_escape;
1380
1381 public:
1382 enum {
1383 Control = TypeFunc::Control,
1384 Memory = TypeFunc::Memory, // MergeMem for states affected by this op
1385 RawAddress = TypeFunc::Parms+0, // the newly-allocated raw address
1386 RawStores = TypeFunc::Parms+1 // zero or more stores (or TOP)
1387 };
1388
1389 InitializeNode(Compile* C, int adr_type, Node* rawoop);
1390 virtual int Opcode() const;
1391 virtual uint size_of() const { return sizeof(*this); }
1392 virtual uint ideal_reg() const { return 0; } // not matched in the AD file
1393 virtual const RegMask &in_RegMask(uint) const; // mask for RawAddress
1394
1395 // Manage incoming memory edges via a MergeMem on in(Memory):
1396 Node* memory(uint alias_idx);
1397
1398 // The raw memory edge coming directly from the Allocation.
1399 // The contents of this memory are *always* all-zero-bits.
1400 Node* zero_memory() { return memory(Compile::AliasIdxRaw); }
1401
1402 // Return the corresponding allocation for this initialization (or null if none).
1403 // (Note: Both InitializeNode::allocation and AllocateNode::initialization
1404 // are defined in graphKit.cpp, which sets up the bidirectional relation.)
1405 AllocateNode* allocation();
1406
1407 // Anything other than zeroing in this init?
1408 bool is_non_zero();
1409
1410 // An InitializeNode must completed before macro expansion is done.
1411 // Completion requires that the AllocateNode must be followed by
1412 // initialization of the new memory to zero, then to any initializers.
1413 bool is_complete() { return _is_complete != Incomplete; }
1414 bool is_complete_with_arraycopy() { return (_is_complete & WithArraycopy) != 0; }
1415
1416 // Mark complete. (Must not yet be complete.)
1417 void set_complete(PhaseGVN* phase);
1418 void set_complete_with_arraycopy() { _is_complete = Complete | WithArraycopy; }
1419
1420 bool does_not_escape() { return _does_not_escape; }
1421 void set_does_not_escape() { _does_not_escape = true; }
1422
1423 #ifdef ASSERT
1424 // ensure all non-degenerate stores are ordered and non-overlapping
1425 bool stores_are_sane(PhaseValues* phase);
1426 #endif //ASSERT
1427
1428 // See if this store can be captured; return offset where it initializes.
1429 // Return 0 if the store cannot be moved (any sort of problem).
1430 intptr_t can_capture_store(StoreNode* st, PhaseGVN* phase, bool can_reshape);
1431
1432 // Capture another store; reformat it to write my internal raw memory.
1433 // Return the captured copy, else null if there is some sort of problem.
1434 Node* capture_store(StoreNode* st, intptr_t start, PhaseGVN* phase, bool can_reshape);
1435
1436 // Find captured store which corresponds to the range [start..start+size).
1437 // Return my own memory projection (meaning the initial zero bits)
1438 // if there is no such store. Return null if there is a problem.
1439 Node* find_captured_store(intptr_t start, int size_in_bytes, PhaseValues* phase);
1440
1441 // Called when the associated AllocateNode is expanded into CFG.
1442 Node* complete_stores(Node* rawctl, Node* rawmem, Node* rawptr,
1443 intptr_t header_size, Node* size_in_bytes,
1444 PhaseIterGVN* phase);
1445
1446 // An Initialize node has multiple memory projections. Helper methods used when the node is removed.
1447 // For use at parse time
1448 void replace_mem_projs_by(Node* mem, Compile* C);
1449 // For use with IGVN
1450 void replace_mem_projs_by(Node* mem, PhaseIterGVN* igvn);
1451
1452 // Does a NarrowMemProj with this adr_type and this node as input already exist?
1453 bool already_has_narrow_mem_proj_with_adr_type(const TypePtr* adr_type) const;
1454
1455 // Used during matching: find the MachProj memory projection if there's one. Expectation is that there should be at
1456 // most one.
1457 MachProjNode* mem_mach_proj() const;
1458
1459 private:
1460 void remove_extra_zeroes();
1461
1462 // Find out where a captured store should be placed (or already is placed).
1463 int captured_store_insertion_point(intptr_t start, int size_in_bytes,
1464 PhaseValues* phase);
1465
1466 static intptr_t get_store_offset(Node* st, PhaseValues* phase);
1467
1468 Node* make_raw_address(intptr_t offset, PhaseGVN* phase);
1469
1470 bool detect_init_independence(Node* value, PhaseGVN* phase);
1471
1472 void coalesce_subword_stores(intptr_t header_size, Node* size_in_bytes,
1473 PhaseGVN* phase);
1474
1475 intptr_t find_next_fullword_store(uint i, PhaseGVN* phase);
1476
1477 // Iterate with i over all NarrowMemProj uses calling callback
1478 template <class Callback, class Iterator> NarrowMemProjNode* apply_to_narrow_mem_projs_any_iterator(Iterator i, Callback callback) const {
1479 auto filter = [&](ProjNode* proj) {
1480 if (proj->is_NarrowMemProj() && callback(proj->as_NarrowMemProj()) == BREAK_AND_RETURN_CURRENT_PROJ) {
1481 return BREAK_AND_RETURN_CURRENT_PROJ;
1482 }
1483 return CONTINUE;
1484 };
1485 ProjNode* res = apply_to_projs_any_iterator(i, filter);
1486 if (res == nullptr) {
1487 return nullptr;
1488 }
1489 return res->as_NarrowMemProj();
1490 }
1491
1492 public:
1493
1494 // callback is allowed to add new uses that will then be iterated over
1495 template <class Callback> void for_each_narrow_mem_proj_with_new_uses(Callback callback) const {
1496 auto callback_always_continue = [&](NarrowMemProjNode* proj) {
1497 callback(proj);
1498 return MultiNode::CONTINUE;
1499 };
1500 DUIterator i = outs();
1501 apply_to_narrow_mem_projs_any_iterator(UsesIterator(i, this), callback_always_continue);
1502 }
1503 };
1504
1505 //------------------------------MergeMem---------------------------------------
1506 // (See comment in memnode.cpp near MergeMemNode::MergeMemNode for semantics.)
1507 class MergeMemNode: public Node {
1508 virtual uint hash() const ; // { return NO_HASH; }
1509 virtual bool cmp( const Node &n ) const ; // Always fail, except on self
1510 friend class MergeMemStream;
1511 MergeMemNode(Node* def); // clients use MergeMemNode::make
1512
1513 public:
1514 // If the input is a whole memory state, clone it with all its slices intact.
1515 // Otherwise, make a new memory state with just that base memory input.
1516 // In either case, the result is a newly created MergeMem.
1517 static MergeMemNode* make(Node* base_memory);
1518
1519 virtual int Opcode() const;
1520 virtual Node* Identity(PhaseGVN* phase);
1521 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1522 virtual uint ideal_reg() const { return NotAMachineReg; }
1523 virtual uint match_edge(uint idx) const { return 0; }
1524 virtual const RegMask &out_RegMask() const;
1525 virtual const Type *bottom_type() const { return Type::MEMORY; }
1526 virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1527 // sparse accessors
1528 // Fetch the previously stored "set_memory_at", or else the base memory.
1529 // (Caller should clone it if it is a phi-nest.)
1530 Node* memory_at(uint alias_idx) const;
1531 // set the memory, regardless of its previous value
1532 void set_memory_at(uint alias_idx, Node* n);
1533 // the "base" is the memory that provides the non-finite support
1534 Node* base_memory() const { return in(Compile::AliasIdxBot); }
1535 // warning: setting the base can implicitly set any of the other slices too
1536 void set_base_memory(Node* def);
1537 // sentinel value which denotes a copy of the base memory:
1538 Node* empty_memory() const { return in(Compile::AliasIdxTop); }
1539 static Node* make_empty_memory(); // where the sentinel comes from
1540 bool is_empty_memory(Node* n) const { assert((n == empty_memory()) == n->is_top(), "sanity"); return n->is_top(); }
1541 // hook for the iterator, to perform any necessary setup
1542 void iteration_setup(const MergeMemNode* other = nullptr);
1543 // push sentinels until I am at least as long as the other (semantic no-op)
1544 void grow_to_match(const MergeMemNode* other);
1545 bool verify_sparse() const PRODUCT_RETURN0;
1546 #ifndef PRODUCT
1547 virtual void dump_spec(outputStream *st) const;
1548 #endif
1549 };
1550
1551 class MergeMemStream : public StackObj {
1552 private:
1553 MergeMemNode* _mm;
1554 const MergeMemNode* _mm2; // optional second guy, contributes non-empty iterations
1555 Node* _mm_base; // loop-invariant base memory of _mm
1556 int _idx;
1557 int _cnt;
1558 Node* _mem;
1559 Node* _mem2;
1560 int _cnt2;
1561
1562 void init(MergeMemNode* mm, const MergeMemNode* mm2 = nullptr) {
1563 // subsume_node will break sparseness at times, whenever a memory slice
1564 // folds down to a copy of the base ("fat") memory. In such a case,
1565 // the raw edge will update to base, although it should be top.
1566 // This iterator will recognize either top or base_memory as an
1567 // "empty" slice. See is_empty, is_empty2, and next below.
1568 //
1569 // The sparseness property is repaired in MergeMemNode::Ideal.
1570 // As long as access to a MergeMem goes through this iterator
1571 // or the memory_at accessor, flaws in the sparseness will
1572 // never be observed.
1573 //
1574 // Also, iteration_setup repairs sparseness.
1575 assert(mm->verify_sparse(), "please, no dups of base");
1576 assert(mm2==nullptr || mm2->verify_sparse(), "please, no dups of base");
1577
1578 _mm = mm;
1579 _mm_base = mm->base_memory();
1580 _mm2 = mm2;
1581 _cnt = mm->req();
1582 _idx = Compile::AliasIdxBot-1; // start at the base memory
1583 _mem = nullptr;
1584 _mem2 = nullptr;
1585 }
1586
1587 #ifdef ASSERT
1588 Node* check_memory() const {
1589 if (at_base_memory())
1590 return _mm->base_memory();
1591 else if ((uint)_idx < _mm->req() && !_mm->in(_idx)->is_top())
1592 return _mm->memory_at(_idx);
1593 else
1594 return _mm_base;
1595 }
1596 Node* check_memory2() const {
1597 return at_base_memory()? _mm2->base_memory(): _mm2->memory_at(_idx);
1598 }
1599 #endif
1600
1601 static bool match_memory(Node* mem, const MergeMemNode* mm, int idx) PRODUCT_RETURN0;
1602 void assert_synch() const {
1603 assert(!_mem || _idx >= _cnt || match_memory(_mem, _mm, _idx),
1604 "no side-effects except through the stream");
1605 }
1606
1607 public:
1608
1609 // expected usages:
1610 // for (MergeMemStream mms(mem->is_MergeMem()); next_non_empty(); ) { ... }
1611 // for (MergeMemStream mms(mem1, mem2); next_non_empty2(); ) { ... }
1612
1613 // iterate over one merge
1614 MergeMemStream(MergeMemNode* mm) {
1615 mm->iteration_setup();
1616 init(mm);
1617 DEBUG_ONLY(_cnt2 = 999);
1618 }
1619 // iterate in parallel over two merges
1620 // only iterates through non-empty elements of mm2
1621 MergeMemStream(MergeMemNode* mm, const MergeMemNode* mm2) {
1622 assert(mm2, "second argument must be a MergeMem also");
1623 ((MergeMemNode*)mm2)->iteration_setup(); // update hidden state
1624 mm->iteration_setup(mm2);
1625 init(mm, mm2);
1626 _cnt2 = mm2->req();
1627 }
1628 #ifdef ASSERT
1629 ~MergeMemStream() {
1630 assert_synch();
1631 }
1632 #endif
1633
1634 MergeMemNode* all_memory() const {
1635 return _mm;
1636 }
1637 Node* base_memory() const {
1638 assert(_mm_base == _mm->base_memory(), "no update to base memory, please");
1639 return _mm_base;
1640 }
1641 const MergeMemNode* all_memory2() const {
1642 assert(_mm2 != nullptr, "");
1643 return _mm2;
1644 }
1645 bool at_base_memory() const {
1646 return _idx == Compile::AliasIdxBot;
1647 }
1648 int alias_idx() const {
1649 assert(_mem, "must call next 1st");
1650 return _idx;
1651 }
1652
1653 const TypePtr* adr_type() const {
1654 return Compile::current()->get_adr_type(alias_idx());
1655 }
1656
1657 const TypePtr* adr_type(Compile* C) const {
1658 return C->get_adr_type(alias_idx());
1659 }
1660 bool is_empty() const {
1661 assert(_mem, "must call next 1st");
1662 assert(_mem->is_top() == (_mem==_mm->empty_memory()), "correct sentinel");
1663 return _mem->is_top();
1664 }
1665 bool is_empty2() const {
1666 assert(_mem2, "must call next 1st");
1667 assert(_mem2->is_top() == (_mem2==_mm2->empty_memory()), "correct sentinel");
1668 return _mem2->is_top();
1669 }
1670 Node* memory() const {
1671 assert(!is_empty(), "must not be empty");
1672 assert_synch();
1673 return _mem;
1674 }
1675 // get the current memory, regardless of empty or non-empty status
1676 Node* force_memory() const {
1677 assert(!is_empty() || !at_base_memory(), "");
1678 // Use _mm_base to defend against updates to _mem->base_memory().
1679 Node *mem = _mem->is_top() ? _mm_base : _mem;
1680 assert(mem == check_memory(), "");
1681 return mem;
1682 }
1683 Node* memory2() const {
1684 assert(_mem2 == check_memory2(), "");
1685 return _mem2;
1686 }
1687 void set_memory(Node* mem) {
1688 if (at_base_memory()) {
1689 // Note that this does not change the invariant _mm_base.
1690 _mm->set_base_memory(mem);
1691 } else {
1692 _mm->set_memory_at(_idx, mem);
1693 }
1694 _mem = mem;
1695 assert_synch();
1696 }
1697
1698 // Recover from a side effect to the MergeMemNode.
1699 void set_memory() {
1700 _mem = _mm->in(_idx);
1701 }
1702
1703 bool next() { return next(false); }
1704 bool next2() { return next(true); }
1705
1706 bool next_non_empty() { return next_non_empty(false); }
1707 bool next_non_empty2() { return next_non_empty(true); }
1708 // next_non_empty2 can yield states where is_empty() is true
1709
1710 private:
1711 // find the next item, which might be empty
1712 bool next(bool have_mm2) {
1713 assert((_mm2 != nullptr) == have_mm2, "use other next");
1714 assert_synch();
1715 if (++_idx < _cnt) {
1716 // Note: This iterator allows _mm to be non-sparse.
1717 // It behaves the same whether _mem is top or base_memory.
1718 _mem = _mm->in(_idx);
1719 if (have_mm2)
1720 _mem2 = _mm2->in((_idx < _cnt2) ? _idx : Compile::AliasIdxTop);
1721 return true;
1722 }
1723 return false;
1724 }
1725
1726 // find the next non-empty item
1727 bool next_non_empty(bool have_mm2) {
1728 while (next(have_mm2)) {
1729 if (!is_empty()) {
1730 // make sure _mem2 is filled in sensibly
1731 if (have_mm2 && _mem2->is_top()) _mem2 = _mm2->base_memory();
1732 return true;
1733 } else if (have_mm2 && !is_empty2()) {
1734 return true; // is_empty() == true
1735 }
1736 }
1737 return false;
1738 }
1739 };
1740
1741 // cachewb node for guaranteeing writeback of the cache line at a
1742 // given address to (non-volatile) RAM
1743 class CacheWBNode : public Node {
1744 public:
1745 CacheWBNode(Node *ctrl, Node *mem, Node *addr) : Node(ctrl, mem, addr) {}
1746 virtual int Opcode() const;
1747 virtual uint ideal_reg() const { return NotAMachineReg; }
1748 virtual uint match_edge(uint idx) const { return (idx == 2); }
1749 virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1750 virtual const Type *bottom_type() const { return Type::MEMORY; }
1751
1752 private:
1753 virtual bool depends_only_on_test_impl() const { return false; }
1754 };
1755
1756 // cachewb pre sync node for ensuring that writebacks are serialised
1757 // relative to preceding or following stores
1758 class CacheWBPreSyncNode : public Node {
1759 public:
1760 CacheWBPreSyncNode(Node *ctrl, Node *mem) : Node(ctrl, mem) {}
1761 virtual int Opcode() const;
1762 virtual uint ideal_reg() const { return NotAMachineReg; }
1763 virtual uint match_edge(uint idx) const { return false; }
1764 virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1765 virtual const Type *bottom_type() const { return Type::MEMORY; }
1766
1767 private:
1768 virtual bool depends_only_on_test_impl() const { return false; }
1769 };
1770
1771 // cachewb pre sync node for ensuring that writebacks are serialised
1772 // relative to preceding or following stores
1773 class CacheWBPostSyncNode : public Node {
1774 public:
1775 CacheWBPostSyncNode(Node *ctrl, Node *mem) : Node(ctrl, mem) {}
1776 virtual int Opcode() const;
1777 virtual uint ideal_reg() const { return NotAMachineReg; }
1778 virtual uint match_edge(uint idx) const { return false; }
1779 virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1780 virtual const Type *bottom_type() const { return Type::MEMORY; }
1781
1782 private:
1783 virtual bool depends_only_on_test_impl() const { return false; }
1784 };
1785
1786 //------------------------------Prefetch---------------------------------------
1787
1788 // Allocation prefetch which may fault, TLAB size have to be adjusted.
1789 class PrefetchAllocationNode : public Node {
1790 public:
1791 PrefetchAllocationNode(Node *mem, Node *adr) : Node(nullptr,mem,adr) {}
1792 virtual int Opcode() const;
1793 virtual uint ideal_reg() const { return NotAMachineReg; }
1794 virtual uint match_edge(uint idx) const { return idx==2; }
1795 virtual const Type *bottom_type() const { return ( AllocatePrefetchStyle == 3 ) ? Type::MEMORY : Type::ABIO; }
1796
1797 private:
1798 virtual bool depends_only_on_test_impl() const { return false; }
1799 };
1800
1801 #endif // SHARE_OPTO_MEMNODE_HPP