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