1 /*
2 * Copyright (c) 1997, 2025, 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 #include "classfile/javaClasses.hpp"
27 #include "compiler/compileLog.hpp"
28 #include "gc/shared/barrierSet.hpp"
29 #include "gc/shared/c2/barrierSetC2.hpp"
30 #include "gc/shared/tlab_globals.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "oops/objArrayKlass.hpp"
34 #include "opto/addnode.hpp"
35 #include "opto/arraycopynode.hpp"
36 #include "opto/cfgnode.hpp"
37 #include "opto/compile.hpp"
38 #include "opto/connode.hpp"
39 #include "opto/convertnode.hpp"
40 #include "opto/loopnode.hpp"
41 #include "opto/machnode.hpp"
42 #include "opto/matcher.hpp"
43 #include "opto/memnode.hpp"
44 #include "opto/mempointer.hpp"
45 #include "opto/mulnode.hpp"
46 #include "opto/narrowptrnode.hpp"
47 #include "opto/phaseX.hpp"
48 #include "opto/regalloc.hpp"
49 #include "opto/regmask.hpp"
50 #include "opto/rootnode.hpp"
51 #include "opto/traceMergeStoresTag.hpp"
52 #include "opto/vectornode.hpp"
53 #include "utilities/align.hpp"
54 #include "utilities/copy.hpp"
55 #include "utilities/macros.hpp"
56 #include "utilities/powerOfTwo.hpp"
57 #include "utilities/vmError.hpp"
58
59 // Portions of code courtesy of Clifford Click
60
61 // Optimization - Graph Style
62
63 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st);
64
65 //=============================================================================
66 uint MemNode::size_of() const { return sizeof(*this); }
67
68 const TypePtr *MemNode::adr_type() const {
69 Node* adr = in(Address);
70 if (adr == nullptr) return nullptr; // node is dead
71 const TypePtr* cross_check = nullptr;
72 DEBUG_ONLY(cross_check = _adr_type);
73 return calculate_adr_type(adr->bottom_type(), cross_check);
74 }
123 st->print(", idx=Bot;");
124 else if (atp->index() == Compile::AliasIdxTop)
125 st->print(", idx=Top;");
126 else if (atp->index() == Compile::AliasIdxRaw)
127 st->print(", idx=Raw;");
128 else {
129 ciField* field = atp->field();
130 if (field) {
131 st->print(", name=");
132 field->print_name_on(st);
133 }
134 st->print(", idx=%d;", atp->index());
135 }
136 }
137 }
138
139 extern void print_alias_types();
140
141 #endif
142
143 Node *MemNode::optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase) {
144 assert((t_oop != nullptr), "sanity");
145 bool is_instance = t_oop->is_known_instance_field();
146 bool is_boxed_value_load = t_oop->is_ptr_to_boxed_value() &&
147 (load != nullptr) && load->is_Load() &&
148 (phase->is_IterGVN() != nullptr);
149 if (!(is_instance || is_boxed_value_load))
150 return mchain; // don't try to optimize non-instance types
151 uint instance_id = t_oop->instance_id();
152 Node *start_mem = phase->C->start()->proj_out_or_null(TypeFunc::Memory);
153 Node *prev = nullptr;
154 Node *result = mchain;
155 while (prev != result) {
156 prev = result;
157 if (result == start_mem)
158 break; // hit one of our sentinels
159 // skip over a call which does not affect this memory slice
160 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
161 Node *proj_in = result->in(0);
162 if (proj_in->is_Allocate() && proj_in->_idx == instance_id) {
163 break; // hit one of our sentinels
164 } else if (proj_in->is_Call()) {
165 // ArrayCopyNodes processed here as well
166 CallNode *call = proj_in->as_Call();
167 if (!call->may_modify(t_oop, phase)) { // returns false for instances
168 result = call->in(TypeFunc::Memory);
169 }
170 } else if (proj_in->is_Initialize()) {
171 AllocateNode* alloc = proj_in->as_Initialize()->allocation();
172 // Stop if this is the initialization for the object instance which
173 // contains this memory slice, otherwise skip over it.
174 if ((alloc == nullptr) || (alloc->_idx == instance_id)) {
175 break;
176 }
177 if (is_instance) {
178 result = proj_in->in(TypeFunc::Memory);
179 } else if (is_boxed_value_load) {
180 Node* klass = alloc->in(AllocateNode::KlassNode);
181 const TypeKlassPtr* tklass = phase->type(klass)->is_klassptr();
182 if (tklass->klass_is_exact() && !tklass->exact_klass()->equals(t_oop->is_instptr()->exact_klass())) {
183 result = proj_in->in(TypeFunc::Memory); // not related allocation
184 }
185 }
186 } else if (proj_in->is_MemBar()) {
187 ArrayCopyNode* ac = nullptr;
188 if (ArrayCopyNode::may_modify(t_oop, proj_in->as_MemBar(), phase, ac)) {
189 break;
190 }
191 result = proj_in->in(TypeFunc::Memory);
192 } else if (proj_in->is_top()) {
193 break; // dead code
194 } else {
195 assert(false, "unexpected projection");
196 }
197 } else if (result->is_ClearArray()) {
198 if (!is_instance || !ClearArrayNode::step_through(&result, instance_id, phase)) {
199 // Can not bypass initialization of the instance
200 // we are looking for.
201 break;
202 }
203 // Otherwise skip it (the call updated 'result' value).
204 } else if (result->is_MergeMem()) {
205 result = step_through_mergemem(phase, result->as_MergeMem(), t_oop, nullptr, tty);
206 }
207 }
208 return result;
209 }
210
211 Node *MemNode::optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase) {
212 const TypeOopPtr* t_oop = t_adr->isa_oopptr();
213 if (t_oop == nullptr)
214 return mchain; // don't try to optimize non-oop types
215 Node* result = optimize_simple_memory_chain(mchain, t_oop, load, phase);
216 bool is_instance = t_oop->is_known_instance_field();
217 PhaseIterGVN *igvn = phase->is_IterGVN();
218 if (is_instance && igvn != nullptr && result->is_Phi()) {
219 PhiNode *mphi = result->as_Phi();
220 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
221 const TypePtr *t = mphi->adr_type();
222 bool do_split = false;
223 // In the following cases, Load memory input can be further optimized based on
224 // its precise address type
225 if (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ) {
226 do_split = true;
227 } else if (t->isa_oopptr() && !t->is_oopptr()->is_known_instance()) {
228 const TypeOopPtr* mem_t =
229 t->is_oopptr()->cast_to_exactness(true)
230 ->is_oopptr()->cast_to_ptr_type(t_oop->ptr())
231 ->is_oopptr()->cast_to_instance_id(t_oop->instance_id());
232 if (t_oop->isa_aryptr()) {
233 mem_t = mem_t->is_aryptr()
234 ->cast_to_stable(t_oop->is_aryptr()->is_stable())
235 ->cast_to_size(t_oop->is_aryptr()->size())
236 ->with_offset(t_oop->is_aryptr()->offset())
237 ->is_aryptr();
238 }
239 do_split = mem_t == t_oop;
240 }
241 if (do_split) {
242 // clone the Phi with our address type
243 result = mphi->split_out_instance(t_adr, igvn);
244 } else {
245 assert(phase->C->get_alias_index(t) == phase->C->get_alias_index(t_adr), "correct memory chain");
246 }
247 }
248 return result;
249 }
250
251 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st) {
252 uint alias_idx = phase->C->get_alias_index(tp);
253 Node *mem = mmem;
254 #ifdef ASSERT
255 {
256 // Check that current type is consistent with the alias index used during graph construction
257 assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx");
258 bool consistent = adr_check == nullptr || adr_check->empty() ||
259 phase->C->must_alias(adr_check, alias_idx );
260 // Sometimes dead array references collapse to a[-1], a[-2], or a[-3]
261 if( !consistent && adr_check != nullptr && !adr_check->empty() &&
262 tp->isa_aryptr() && tp->offset() == Type::OffsetBot &&
263 adr_check->isa_aryptr() && adr_check->offset() != Type::OffsetBot &&
264 ( adr_check->offset() == arrayOopDesc::length_offset_in_bytes() ||
265 adr_check->offset() == oopDesc::klass_offset_in_bytes() ||
266 adr_check->offset() == oopDesc::mark_offset_in_bytes() ) ) {
267 // don't assert if it is dead code.
268 consistent = true;
269 }
270 if( !consistent ) {
271 st->print("alias_idx==%d, adr_check==", alias_idx);
272 if( adr_check == nullptr ) {
273 st->print("null");
274 } else {
275 adr_check->dump();
276 }
277 st->cr();
278 print_alias_types();
279 assert(consistent, "adr_check must match alias idx");
280 }
281 }
282 #endif
954 "use LoadKlassNode instead");
955 assert(!(adr_type->isa_aryptr() &&
956 adr_type->offset() == arrayOopDesc::length_offset_in_bytes()),
957 "use LoadRangeNode instead");
958 // Check control edge of raw loads
959 assert( ctl != nullptr || C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
960 // oop will be recorded in oop map if load crosses safepoint
961 rt->isa_oopptr() || is_immutable_value(adr),
962 "raw memory operations should have control edge");
963 LoadNode* load = nullptr;
964 switch (bt) {
965 case T_BOOLEAN: load = new LoadUBNode(ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
966 case T_BYTE: load = new LoadBNode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
967 case T_INT: load = new LoadINode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
968 case T_CHAR: load = new LoadUSNode(ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
969 case T_SHORT: load = new LoadSNode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
970 case T_LONG: load = new LoadLNode (ctl, mem, adr, adr_type, rt->is_long(), mo, control_dependency, require_atomic_access); break;
971 case T_FLOAT: load = new LoadFNode (ctl, mem, adr, adr_type, rt, mo, control_dependency); break;
972 case T_DOUBLE: load = new LoadDNode (ctl, mem, adr, adr_type, rt, mo, control_dependency, require_atomic_access); break;
973 case T_ADDRESS: load = new LoadPNode (ctl, mem, adr, adr_type, rt->is_ptr(), mo, control_dependency); break;
974 case T_OBJECT:
975 case T_NARROWOOP:
976 #ifdef _LP64
977 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
978 load = new LoadNNode(ctl, mem, adr, adr_type, rt->make_narrowoop(), mo, control_dependency);
979 } else
980 #endif
981 {
982 assert(!adr->bottom_type()->is_ptr_to_narrowoop() && !adr->bottom_type()->is_ptr_to_narrowklass(), "should have got back a narrow oop");
983 load = new LoadPNode(ctl, mem, adr, adr_type, rt->is_ptr(), mo, control_dependency);
984 }
985 break;
986 default:
987 ShouldNotReachHere();
988 break;
989 }
990 assert(load != nullptr, "LoadNode should have been created");
991 if (unaligned) {
992 load->set_unaligned_access();
993 }
994 if (mismatched) {
995 load->set_mismatched_access();
996 }
997 if (unsafe) {
998 load->set_unsafe_access();
999 }
1000 load->set_barrier_data(barrier_data);
1001 if (load->Opcode() == Op_LoadN) {
1002 Node* ld = gvn.transform(load);
1003 return new DecodeNNode(ld, ld->bottom_type()->make_ptr());
1004 }
1005
1006 return load;
1007 }
1008
1009 //------------------------------hash-------------------------------------------
1010 uint LoadNode::hash() const {
1011 // unroll addition of interesting fields
1012 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
1013 }
1014
1015 static bool skip_through_membars(Compile::AliasType* atp, const TypeInstPtr* tp, bool eliminate_boxing) {
1016 if ((atp != nullptr) && (atp->index() >= Compile::AliasIdxRaw)) {
1017 bool non_volatile = (atp->field() != nullptr) && !atp->field()->is_volatile();
1018 bool is_stable_ary = FoldStableValues &&
1019 (tp != nullptr) && (tp->isa_aryptr() != nullptr) &&
1020 tp->isa_aryptr()->is_stable();
1021
1022 return (eliminate_boxing && non_volatile) || is_stable_ary;
1023 }
1024
1025 return false;
1026 }
1027
1028 LoadNode* LoadNode::pin_array_access_node() const {
1029 const TypePtr* adr_type = this->adr_type();
1030 if (adr_type != nullptr && adr_type->isa_aryptr()) {
1031 return clone_pinned();
1032 }
1033 return nullptr;
1034 }
1035
1036 // Is the value loaded previously stored by an arraycopy? If so return
1037 // a load node that reads from the source array so we may be able to
1038 // optimize out the ArrayCopy node later.
1039 Node* LoadNode::can_see_arraycopy_value(Node* st, PhaseGVN* phase) const {
1040 Node* ld_adr = in(MemNode::Address);
1041 intptr_t ld_off = 0;
1042 AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off);
1058 if (ac->as_ArrayCopy()->is_clonebasic()) {
1059 assert(ld_alloc != nullptr, "need an alloc");
1060 assert(addp->is_AddP(), "address must be addp");
1061 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1062 assert(bs->step_over_gc_barrier(addp->in(AddPNode::Base)) == bs->step_over_gc_barrier(ac->in(ArrayCopyNode::Dest)), "strange pattern");
1063 assert(bs->step_over_gc_barrier(addp->in(AddPNode::Address)) == bs->step_over_gc_barrier(ac->in(ArrayCopyNode::Dest)), "strange pattern");
1064 addp->set_req(AddPNode::Base, src);
1065 addp->set_req(AddPNode::Address, src);
1066 } else {
1067 assert(ac->as_ArrayCopy()->is_arraycopy_validated() ||
1068 ac->as_ArrayCopy()->is_copyof_validated() ||
1069 ac->as_ArrayCopy()->is_copyofrange_validated(), "only supported cases");
1070 assert(addp->in(AddPNode::Base) == addp->in(AddPNode::Address), "should be");
1071 addp->set_req(AddPNode::Base, src);
1072 addp->set_req(AddPNode::Address, src);
1073
1074 const TypeAryPtr* ary_t = phase->type(in(MemNode::Address))->isa_aryptr();
1075 BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type();
1076 if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT;
1077
1078 uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
1079 uint shift = exact_log2(type2aelembytes(ary_elem));
1080
1081 Node* diff = phase->transform(new SubINode(ac->in(ArrayCopyNode::SrcPos), ac->in(ArrayCopyNode::DestPos)));
1082 #ifdef _LP64
1083 diff = phase->transform(new ConvI2LNode(diff));
1084 #endif
1085 diff = phase->transform(new LShiftXNode(diff, phase->intcon(shift)));
1086
1087 Node* offset = phase->transform(new AddXNode(addp->in(AddPNode::Offset), diff));
1088 addp->set_req(AddPNode::Offset, offset);
1089 }
1090 addp = phase->transform(addp);
1091 #ifdef ASSERT
1092 const TypePtr* adr_type = phase->type(addp)->is_ptr();
1093 ld->_adr_type = adr_type;
1094 #endif
1095 ld->set_req(MemNode::Address, addp);
1096 ld->set_req(0, ctl);
1097 ld->set_req(MemNode::Memory, mem);
1098 return ld;
1099 }
1100 return nullptr;
1101 }
1102
1103
1104 //---------------------------can_see_stored_value------------------------------
1105 // This routine exists to make sure this set of tests is done the same
1106 // everywhere. We need to make a coordinated change: first LoadNode::Ideal
1107 // will change the graph shape in a way which makes memory alive twice at the
1108 // same time (uses the Oracle model of aliasing), then some
1109 // LoadXNode::Identity will fold things back to the equivalence-class model
1110 // of aliasing.
1111 Node* MemNode::can_see_stored_value(Node* st, PhaseValues* phase) const {
1112 Node* ld_adr = in(MemNode::Address);
1113 intptr_t ld_off = 0;
1114 Node* ld_base = AddPNode::Ideal_base_and_offset(ld_adr, phase, ld_off);
1115 Node* ld_alloc = AllocateNode::Ideal_allocation(ld_base);
1116 const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr();
1117 Compile::AliasType* atp = (tp != nullptr) ? phase->C->alias_type(tp) : nullptr;
1118 // This is more general than load from boxing objects.
1119 if (skip_through_membars(atp, tp, phase->C->eliminate_boxing())) {
1120 uint alias_idx = atp->index();
1121 Node* result = nullptr;
1122 Node* current = st;
1123 // Skip through chains of MemBarNodes checking the MergeMems for
1124 // new states for the slice of this load. Stop once any other
1125 // kind of node is encountered. Loads from final memory can skip
1126 // through any kind of MemBar but normal loads shouldn't skip
1127 // through MemBarAcquire since the could allow them to move out of
1128 // a synchronized region. It is not safe to step over MemBarCPUOrder,
1129 // because alias info above them may be inaccurate (e.g., due to
1130 // mixed/mismatched unsafe accesses).
1131 bool is_final_mem = !atp->is_rewritable();
1132 while (current->is_Proj()) {
1133 int opc = current->in(0)->Opcode();
1134 if ((is_final_mem && (opc == Op_MemBarAcquire ||
1178 // Same base, same offset.
1179 // Possible improvement for arrays: check index value instead of absolute offset.
1180
1181 // At this point we have proven something like this setup:
1182 // B = << base >>
1183 // L = LoadQ(AddP(Check/CastPP(B), #Off))
1184 // S = StoreQ(AddP( B , #Off), V)
1185 // (Actually, we haven't yet proven the Q's are the same.)
1186 // In other words, we are loading from a casted version of
1187 // the same pointer-and-offset that we stored to.
1188 // Casted version may carry a dependency and it is respected.
1189 // Thus, we are able to replace L by V.
1190 }
1191 // Now prove that we have a LoadQ matched to a StoreQ, for some Q.
1192 if (store_Opcode() != st->Opcode()) {
1193 return nullptr;
1194 }
1195 // LoadVector/StoreVector needs additional check to ensure the types match.
1196 if (st->is_StoreVector()) {
1197 const TypeVect* in_vt = st->as_StoreVector()->vect_type();
1198 const TypeVect* out_vt = as_LoadVector()->vect_type();
1199 if (in_vt != out_vt) {
1200 return nullptr;
1201 }
1202 }
1203 return st->in(MemNode::ValueIn);
1204 }
1205
1206 // A load from a freshly-created object always returns zero.
1207 // (This can happen after LoadNode::Ideal resets the load's memory input
1208 // to find_captured_store, which returned InitializeNode::zero_memory.)
1209 if (st->is_Proj() && st->in(0)->is_Allocate() &&
1210 (st->in(0) == ld_alloc) &&
1211 (ld_off >= st->in(0)->as_Allocate()->minimum_header_size())) {
1212 // return a zero value for the load's basic type
1213 // (This is one of the few places where a generic PhaseTransform
1214 // can create new nodes. Think of it as lazily manifesting
1215 // virtually pre-existing constants.)
1216 if (value_basic_type() != T_VOID) {
1217 if (ReduceBulkZeroing || find_array_copy_clone(ld_alloc, in(MemNode::Memory)) == nullptr) {
1218 // If ReduceBulkZeroing is disabled, we need to check if the allocation does not belong to an
1219 // ArrayCopyNode clone. If it does, then we cannot assume zero since the initialization is done
1220 // by the ArrayCopyNode.
1221 return phase->zerocon(value_basic_type());
1222 }
1223 } else {
1224 // TODO: materialize all-zero vector constant
1225 assert(!isa_Load() || as_Load()->type()->isa_vect(), "");
1226 }
1227 }
1228
1229 // A load from an initialization barrier can match a captured store.
1230 if (st->is_Proj() && st->in(0)->is_Initialize()) {
1231 InitializeNode* init = st->in(0)->as_Initialize();
1232 AllocateNode* alloc = init->allocation();
1233 if ((alloc != nullptr) && (alloc == ld_alloc)) {
1234 // examine a captured store value
1235 st = init->find_captured_store(ld_off, memory_size(), phase);
1248 base = bs->step_over_gc_barrier(base);
1249 if (base != nullptr && base->is_Proj() &&
1250 base->as_Proj()->_con == TypeFunc::Parms &&
1251 base->in(0)->is_CallStaticJava() &&
1252 base->in(0)->as_CallStaticJava()->is_boxing_method()) {
1253 return base->in(0)->in(TypeFunc::Parms);
1254 }
1255 }
1256
1257 break;
1258 }
1259
1260 return nullptr;
1261 }
1262
1263 //----------------------is_instance_field_load_with_local_phi------------------
1264 bool LoadNode::is_instance_field_load_with_local_phi(Node* ctrl) {
1265 if( in(Memory)->is_Phi() && in(Memory)->in(0) == ctrl &&
1266 in(Address)->is_AddP() ) {
1267 const TypeOopPtr* t_oop = in(Address)->bottom_type()->isa_oopptr();
1268 // Only instances and boxed values.
1269 if( t_oop != nullptr &&
1270 (t_oop->is_ptr_to_boxed_value() ||
1271 t_oop->is_known_instance_field()) &&
1272 t_oop->offset() != Type::OffsetBot &&
1273 t_oop->offset() != Type::OffsetTop) {
1274 return true;
1275 }
1276 }
1277 return false;
1278 }
1279
1280 //------------------------------Identity---------------------------------------
1281 // Loads are identity if previous store is to same address
1282 Node* LoadNode::Identity(PhaseGVN* phase) {
1283 // If the previous store-maker is the right kind of Store, and the store is
1284 // to the same address, then we are equal to the value stored.
1285 Node* mem = in(Memory);
1286 Node* value = can_see_stored_value(mem, phase);
1287 if( value ) {
1288 // byte, short & char stores truncate naturally.
1289 // A load has to load the truncated value which requires
1290 // some sort of masking operation and that requires an
1291 // Ideal call instead of an Identity call.
1292 if (memory_size() < BytesPerInt) {
1293 // If the input to the store does not fit with the load's result type,
1294 // it must be truncated via an Ideal call.
1295 if (!phase->type(value)->higher_equal(phase->type(this)))
1296 return this;
1297 }
1298 // (This works even when value is a Con, but LoadNode::Value
1299 // usually runs first, producing the singleton type of the Con.)
1300 if (!has_pinned_control_dependency() || value->is_Con()) {
1301 return value;
1302 } else {
1303 return this;
1304 }
1305 }
1306
1307 if (has_pinned_control_dependency()) {
1308 return this;
1309 }
1310 // Search for an existing data phi which was generated before for the same
1311 // instance's field to avoid infinite generation of phis in a loop.
1312 Node *region = mem->in(0);
1313 if (is_instance_field_load_with_local_phi(region)) {
1314 const TypeOopPtr *addr_t = in(Address)->bottom_type()->isa_oopptr();
1315 int this_index = phase->C->get_alias_index(addr_t);
1316 int this_offset = addr_t->offset();
1317 int this_iid = addr_t->instance_id();
1318 if (!addr_t->is_known_instance() &&
1319 addr_t->is_ptr_to_boxed_value()) {
1320 // Use _idx of address base (could be Phi node) for boxed values.
1321 intptr_t ignore = 0;
1322 Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore);
1323 if (base == nullptr) {
1324 return this;
1325 }
1326 this_iid = base->_idx;
1327 }
1328 const Type* this_type = bottom_type();
1329 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
1330 Node* phi = region->fast_out(i);
1331 if (phi->is_Phi() && phi != mem &&
1332 phi->as_Phi()->is_same_inst_field(this_type, (int)mem->_idx, this_iid, this_index, this_offset)) {
1333 return phi;
1334 }
1335 }
1336 }
1337
1338 return this;
1339 }
1340
1856 bool addr_mark = ((phase->type(address)->isa_oopptr() || phase->type(address)->isa_narrowoop()) &&
1857 phase->type(address)->is_ptr()->offset() == oopDesc::mark_offset_in_bytes());
1858
1859 // Skip up past a SafePoint control. Cannot do this for Stores because
1860 // pointer stores & cardmarks must stay on the same side of a SafePoint.
1861 if( ctrl != nullptr && ctrl->Opcode() == Op_SafePoint &&
1862 phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw &&
1863 !addr_mark &&
1864 (depends_only_on_test() || has_unknown_control_dependency())) {
1865 ctrl = ctrl->in(0);
1866 set_req(MemNode::Control,ctrl);
1867 progress = true;
1868 }
1869
1870 intptr_t ignore = 0;
1871 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
1872 if (base != nullptr
1873 && phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw) {
1874 // Check for useless control edge in some common special cases
1875 if (in(MemNode::Control) != nullptr
1876 && can_remove_control()
1877 && phase->type(base)->higher_equal(TypePtr::NOTNULL)
1878 && all_controls_dominate(base, phase->C->start())) {
1879 // A method-invariant, non-null address (constant or 'this' argument).
1880 set_req(MemNode::Control, nullptr);
1881 progress = true;
1882 }
1883 }
1884
1885 Node* mem = in(MemNode::Memory);
1886 const TypePtr *addr_t = phase->type(address)->isa_ptr();
1887
1888 if (can_reshape && (addr_t != nullptr)) {
1889 // try to optimize our memory input
1890 Node* opt_mem = MemNode::optimize_memory_chain(mem, addr_t, this, phase);
1891 if (opt_mem != mem) {
1892 set_req_X(MemNode::Memory, opt_mem, phase);
1893 if (phase->type( opt_mem ) == Type::TOP) return nullptr;
1894 return this;
1895 }
1952 // fold up, do so.
1953 Node* prev_mem = find_previous_store(phase);
1954 if (prev_mem != nullptr) {
1955 Node* value = can_see_arraycopy_value(prev_mem, phase);
1956 if (value != nullptr) {
1957 return value;
1958 }
1959 }
1960 // Steps (a), (b): Walk past independent stores to find an exact match.
1961 if (prev_mem != nullptr && prev_mem != in(MemNode::Memory)) {
1962 // (c) See if we can fold up on the spot, but don't fold up here.
1963 // Fold-up might require truncation (for LoadB/LoadS/LoadUS) or
1964 // just return a prior value, which is done by Identity calls.
1965 if (can_see_stored_value(prev_mem, phase)) {
1966 // Make ready for step (d):
1967 set_req_X(MemNode::Memory, prev_mem, phase);
1968 return this;
1969 }
1970 }
1971
1972 return progress ? this : nullptr;
1973 }
1974
1975 // Helper to recognize certain Klass fields which are invariant across
1976 // some group of array types (e.g., int[] or all T[] where T < Object).
1977 const Type*
1978 LoadNode::load_array_final_field(const TypeKlassPtr *tkls,
1979 ciKlass* klass) const {
1980 assert(!UseCompactObjectHeaders || tkls->offset() != in_bytes(Klass::prototype_header_offset()),
1981 "must not happen");
1982
1983 if (tkls->isa_instklassptr() && tkls->offset() == in_bytes(InstanceKlass::access_flags_offset())) {
1984 // The field is InstanceKlass::_access_flags. Return its (constant) value.
1985 assert(Opcode() == Op_LoadUS, "must load an unsigned short from _access_flags");
1986 ciInstanceKlass* iklass = tkls->is_instklassptr()->instance_klass();
1987 return TypeInt::make(iklass->access_flags());
1988 }
1989 if (tkls->offset() == in_bytes(Klass::misc_flags_offset())) {
1990 // The field is Klass::_misc_flags. Return its (constant) value.
1991 assert(Opcode() == Op_LoadUB, "must load an unsigned byte from _misc_flags");
1992 return TypeInt::make(klass->misc_flags());
2000 // No match.
2001 return nullptr;
2002 }
2003
2004 //------------------------------Value-----------------------------------------
2005 const Type* LoadNode::Value(PhaseGVN* phase) const {
2006 // Either input is TOP ==> the result is TOP
2007 Node* mem = in(MemNode::Memory);
2008 const Type *t1 = phase->type(mem);
2009 if (t1 == Type::TOP) return Type::TOP;
2010 Node* adr = in(MemNode::Address);
2011 const TypePtr* tp = phase->type(adr)->isa_ptr();
2012 if (tp == nullptr || tp->empty()) return Type::TOP;
2013 int off = tp->offset();
2014 assert(off != Type::OffsetTop, "case covered by TypePtr::empty");
2015 Compile* C = phase->C;
2016
2017 // If load can see a previous constant store, use that.
2018 Node* value = can_see_stored_value(mem, phase);
2019 if (value != nullptr && value->is_Con()) {
2020 assert(value->bottom_type()->higher_equal(_type), "sanity");
2021 return value->bottom_type();
2022 }
2023
2024 // Try to guess loaded type from pointer type
2025 if (tp->isa_aryptr()) {
2026 const TypeAryPtr* ary = tp->is_aryptr();
2027 const Type* t = ary->elem();
2028
2029 // Determine whether the reference is beyond the header or not, by comparing
2030 // the offset against the offset of the start of the array's data.
2031 // Different array types begin at slightly different offsets (12 vs. 16).
2032 // We choose T_BYTE as an example base type that is least restrictive
2033 // as to alignment, which will therefore produce the smallest
2034 // possible base offset.
2035 const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE);
2036 const bool off_beyond_header = (off >= min_base_off);
2037
2038 // Try to constant-fold a stable array element.
2039 if (FoldStableValues && !is_mismatched_access() && ary->is_stable()) {
2040 // Make sure the reference is not into the header and the offset is constant
2041 ciObject* aobj = ary->const_oop();
2042 if (aobj != nullptr && off_beyond_header && adr->is_AddP() && off != Type::OffsetBot) {
2043 int stable_dimension = (ary->stable_dimension() > 0 ? ary->stable_dimension() - 1 : 0);
2044 const Type* con_type = Type::make_constant_from_array_element(aobj->as_array(), off,
2045 stable_dimension,
2046 value_basic_type(), is_unsigned());
2047 if (con_type != nullptr) {
2048 return con_type;
2049 }
2050 }
2051 }
2052
2053 // Don't do this for integer types. There is only potential profit if
2054 // the element type t is lower than _type; that is, for int types, if _type is
2055 // more restrictive than t. This only happens here if one is short and the other
2056 // char (both 16 bits), and in those cases we've made an intentional decision
2057 // to use one kind of load over the other. See AndINode::Ideal and 4965907.
2058 // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
2059 //
2060 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
2061 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier
2062 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
2063 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed,
2064 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
2065 // In fact, that could have been the original type of p1, and p1 could have
2066 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
2067 // expression (LShiftL quux 3) independently optimized to the constant 8.
2068 if ((t->isa_int() == nullptr) && (t->isa_long() == nullptr)
2069 && (_type->isa_vect() == nullptr)
2070 && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) {
2071 // t might actually be lower than _type, if _type is a unique
2072 // concrete subclass of abstract class t.
2073 if (off_beyond_header || off == Type::OffsetBot) { // is the offset beyond the header?
2074 const Type* jt = t->join_speculative(_type);
2075 // In any case, do not allow the join, per se, to empty out the type.
2076 if (jt->empty() && !t->empty()) {
2077 // This can happen if a interface-typed array narrows to a class type.
2078 jt = _type;
2079 }
2080 #ifdef ASSERT
2081 if (phase->C->eliminate_boxing() && adr->is_AddP()) {
2082 // The pointers in the autobox arrays are always non-null
2083 Node* base = adr->in(AddPNode::Base);
2084 if ((base != nullptr) && base->is_DecodeN()) {
2085 // Get LoadN node which loads IntegerCache.cache field
2086 base = base->in(1);
2087 }
2088 if ((base != nullptr) && base->is_Con()) {
2089 const TypeAryPtr* base_type = base->bottom_type()->isa_aryptr();
2090 if ((base_type != nullptr) && base_type->is_autobox_cache()) {
2091 // It could be narrow oop
2092 assert(jt->make_ptr()->ptr() == TypePtr::NotNull,"sanity");
2093 }
2094 }
2095 }
2096 #endif
2097 return jt;
2098 }
2099 }
2100 } else if (tp->base() == Type::InstPtr) {
2101 assert( off != Type::OffsetBot ||
2102 // arrays can be cast to Objects
2103 !tp->isa_instptr() ||
2104 tp->is_instptr()->instance_klass()->is_java_lang_Object() ||
2105 // unsafe field access may not have a constant offset
2106 C->has_unsafe_access(),
2107 "Field accesses must be precise" );
2108 // For oop loads, we expect the _type to be precise.
2109
2110 // Optimize loads from constant fields.
2111 const TypeInstPtr* tinst = tp->is_instptr();
2112 ciObject* const_oop = tinst->const_oop();
2113 if (!is_mismatched_access() && off != Type::OffsetBot && const_oop != nullptr && const_oop->is_instance()) {
2114 const Type* con_type = Type::make_constant_from_field(const_oop->as_instance(), off, is_unsigned(), value_basic_type());
2115 if (con_type != nullptr) {
2116 return con_type;
2117 }
2118 }
2119 } else if (tp->base() == Type::KlassPtr || tp->base() == Type::InstKlassPtr || tp->base() == Type::AryKlassPtr) {
2120 assert(off != Type::OffsetBot ||
2121 !tp->isa_instklassptr() ||
2122 // arrays can be cast to Objects
2123 tp->isa_instklassptr()->instance_klass()->is_java_lang_Object() ||
2124 // also allow array-loading from the primary supertype
2125 // array during subtype checks
2126 Opcode() == Op_LoadKlass,
2127 "Field accesses must be precise");
2128 // For klass/static loads, we expect the _type to be precise
2129 } else if (tp->base() == Type::RawPtr && adr->is_Load() && off == 0) {
2130 /* With mirrors being an indirect in the Klass*
2131 * the VM is now using two loads. LoadKlass(LoadP(LoadP(Klass, mirror_offset), zero_offset))
2132 * The LoadP from the Klass has a RawPtr type (see LibraryCallKit::load_mirror_from_klass).
2133 *
2134 * So check the type and klass of the node before the LoadP.
2141 assert(adr->Opcode() == Op_LoadP, "must load an oop from _java_mirror");
2142 assert(Opcode() == Op_LoadP, "must load an oop from _java_mirror");
2143 return TypeInstPtr::make(klass->java_mirror());
2144 }
2145 }
2146 }
2147
2148 const TypeKlassPtr *tkls = tp->isa_klassptr();
2149 if (tkls != nullptr) {
2150 if (tkls->is_loaded() && tkls->klass_is_exact()) {
2151 ciKlass* klass = tkls->exact_klass();
2152 // We are loading a field from a Klass metaobject whose identity
2153 // is known at compile time (the type is "exact" or "precise").
2154 // Check for fields we know are maintained as constants by the VM.
2155 if (tkls->offset() == in_bytes(Klass::super_check_offset_offset())) {
2156 // The field is Klass::_super_check_offset. Return its (constant) value.
2157 // (Folds up type checking code.)
2158 assert(Opcode() == Op_LoadI, "must load an int from _super_check_offset");
2159 return TypeInt::make(klass->super_check_offset());
2160 }
2161 if (UseCompactObjectHeaders) {
2162 if (tkls->offset() == in_bytes(Klass::prototype_header_offset())) {
2163 // The field is Klass::_prototype_header. Return its (constant) value.
2164 assert(this->Opcode() == Op_LoadX, "must load a proper type from _prototype_header");
2165 return TypeX::make(klass->prototype_header());
2166 }
2167 }
2168 // Compute index into primary_supers array
2169 juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*);
2170 // Check for overflowing; use unsigned compare to handle the negative case.
2171 if( depth < ciKlass::primary_super_limit() ) {
2172 // The field is an element of Klass::_primary_supers. Return its (constant) value.
2173 // (Folds up type checking code.)
2174 assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers");
2175 ciKlass *ss = klass->super_of_depth(depth);
2176 return ss ? TypeKlassPtr::make(ss, Type::trust_interfaces) : TypePtr::NULL_PTR;
2177 }
2178 const Type* aift = load_array_final_field(tkls, klass);
2179 if (aift != nullptr) return aift;
2180 }
2181
2182 // We can still check if we are loading from the primary_supers array at a
2183 // shallow enough depth. Even though the klass is not exact, entries less
2184 // than or equal to its super depth are correct.
2185 if (tkls->is_loaded()) {
2186 ciKlass* klass = nullptr;
2220 jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false);
2221 // The key property of this type is that it folds up tests
2222 // for array-ness, since it proves that the layout_helper is positive.
2223 // Thus, a generic value like the basic object layout helper works fine.
2224 return TypeInt::make(min_size, max_jint, Type::WidenMin);
2225 }
2226 }
2227
2228 // If we are loading from a freshly-allocated object/array, produce a zero.
2229 // Things to check:
2230 // 1. Load is beyond the header: headers are not guaranteed to be zero
2231 // 2. Load is not vectorized: vectors have no zero constant
2232 // 3. Load has no matching store, i.e. the input is the initial memory state
2233 const TypeOopPtr* tinst = tp->isa_oopptr();
2234 bool is_not_header = (tinst != nullptr) && tinst->is_known_instance_field();
2235 bool is_not_vect = (_type->isa_vect() == nullptr);
2236 if (is_not_header && is_not_vect) {
2237 Node* mem = in(MemNode::Memory);
2238 if (mem->is_Parm() && mem->in(0)->is_Start()) {
2239 assert(mem->as_Parm()->_con == TypeFunc::Memory, "must be memory Parm");
2240 return Type::get_zero_type(_type->basic_type());
2241 }
2242 }
2243
2244 if (!UseCompactObjectHeaders) {
2245 Node* alloc = is_new_object_mark_load();
2246 if (alloc != nullptr) {
2247 return TypeX::make(markWord::prototype().value());
2248 }
2249 }
2250
2251 return _type;
2252 }
2253
2254 //------------------------------match_edge-------------------------------------
2255 // Do we Match on this edge index or not? Match only the address.
2256 uint LoadNode::match_edge(uint idx) const {
2257 return idx == MemNode::Address;
2258 }
2259
2260 //--------------------------LoadBNode::Ideal--------------------------------------
2261 //
2262 // If the previous store is to the same address as this load,
2263 // and the value stored was larger than a byte, replace this load
2264 // with the value stored truncated to a byte. If no truncation is
2265 // needed, the replacement is done in LoadNode::Identity().
2266 //
2267 Node* LoadBNode::Ideal(PhaseGVN* phase, bool can_reshape) {
2376 }
2377 }
2378 // Identity call will handle the case where truncation is not needed.
2379 return LoadNode::Ideal(phase, can_reshape);
2380 }
2381
2382 const Type* LoadSNode::Value(PhaseGVN* phase) const {
2383 Node* mem = in(MemNode::Memory);
2384 Node* value = can_see_stored_value(mem,phase);
2385 if (value != nullptr && value->is_Con() &&
2386 !value->bottom_type()->higher_equal(_type)) {
2387 // If the input to the store does not fit with the load's result type,
2388 // it must be truncated. We can't delay until Ideal call since
2389 // a singleton Value is needed for split_thru_phi optimization.
2390 int con = value->get_int();
2391 return TypeInt::make((con << 16) >> 16);
2392 }
2393 return LoadNode::Value(phase);
2394 }
2395
2396 //=============================================================================
2397 //----------------------------LoadKlassNode::make------------------------------
2398 // Polymorphic factory method:
2399 Node* LoadKlassNode::make(PhaseGVN& gvn, Node* mem, Node* adr, const TypePtr* at, const TypeKlassPtr* tk) {
2400 // sanity check the alias category against the created node type
2401 const TypePtr* adr_type = adr->bottom_type()->isa_ptr();
2402 assert(adr_type != nullptr, "expecting TypeKlassPtr");
2403 #ifdef _LP64
2404 if (adr_type->is_ptr_to_narrowklass()) {
2405 assert(UseCompressedClassPointers, "no compressed klasses");
2406 Node* load_klass = gvn.transform(new LoadNKlassNode(mem, adr, at, tk->make_narrowklass(), MemNode::unordered));
2407 return new DecodeNKlassNode(load_klass, load_klass->bottom_type()->make_ptr());
2408 }
2409 #endif
2410 assert(!adr_type->is_ptr_to_narrowklass() && !adr_type->is_ptr_to_narrowoop(), "should have got back a narrow oop");
2411 return new LoadKlassNode(mem, adr, at, tk, MemNode::unordered);
2412 }
2413
2414 //------------------------------Value------------------------------------------
2415 const Type* LoadKlassNode::Value(PhaseGVN* phase) const {
2449 }
2450 return TypeKlassPtr::make(ciArrayKlass::make(t), Type::trust_interfaces);
2451 }
2452 if (!t->is_klass()) {
2453 // a primitive Class (e.g., int.class) has null for a klass field
2454 return TypePtr::NULL_PTR;
2455 }
2456 // Fold up the load of the hidden field
2457 return TypeKlassPtr::make(t->as_klass(), Type::trust_interfaces);
2458 }
2459 // non-constant mirror, so we can't tell what's going on
2460 }
2461 if (!tinst->is_loaded())
2462 return _type; // Bail out if not loaded
2463 if (offset == oopDesc::klass_offset_in_bytes()) {
2464 return tinst->as_klass_type(true);
2465 }
2466 }
2467
2468 // Check for loading klass from an array
2469 const TypeAryPtr *tary = tp->isa_aryptr();
2470 if (tary != nullptr &&
2471 tary->offset() == oopDesc::klass_offset_in_bytes()) {
2472 return tary->as_klass_type(true);
2473 }
2474
2475 // Check for loading klass from an array klass
2476 const TypeKlassPtr *tkls = tp->isa_klassptr();
2477 if (tkls != nullptr && !StressReflectiveCode) {
2478 if (!tkls->is_loaded())
2479 return _type; // Bail out if not loaded
2480 if (tkls->isa_aryklassptr() && tkls->is_aryklassptr()->elem()->isa_klassptr() &&
2481 tkls->offset() == in_bytes(ObjArrayKlass::element_klass_offset())) {
2482 // // Always returning precise element type is incorrect,
2483 // // e.g., element type could be object and array may contain strings
2484 // return TypeKlassPtr::make(TypePtr::Constant, elem, 0);
2485
2486 // The array's TypeKlassPtr was declared 'precise' or 'not precise'
2487 // according to the element type's subclassing.
2488 return tkls->is_aryklassptr()->elem()->isa_klassptr()->cast_to_exactness(tkls->klass_is_exact());
2489 }
2490 if (tkls->isa_instklassptr() != nullptr && tkls->klass_is_exact() &&
2491 tkls->offset() == in_bytes(Klass::super_offset())) {
2492 ciKlass* sup = tkls->is_instklassptr()->instance_klass()->super();
2493 // The field is Klass::_super. Return its (constant) value.
2494 // (Folds up the 2nd indirection in aClassConstant.getSuperClass().)
2495 return sup ? TypeKlassPtr::make(sup, Type::trust_interfaces) : TypePtr::NULL_PTR;
2496 }
2497 }
2498
2499 if (tkls != nullptr && !UseSecondarySupersCache
2500 && tkls->offset() == in_bytes(Klass::secondary_super_cache_offset())) {
2501 // Treat Klass::_secondary_super_cache as a constant when the cache is disabled.
2502 return TypePtr::NULL_PTR;
2503 }
2504
2505 // Bailout case
2506 return LoadNode::Value(phase);
2507 }
2508
2509 //------------------------------Identity---------------------------------------
2532 base = bs->step_over_gc_barrier(base);
2533 }
2534
2535 // We can fetch the klass directly through an AllocateNode.
2536 // This works even if the klass is not constant (clone or newArray).
2537 if (offset == oopDesc::klass_offset_in_bytes()) {
2538 Node* allocated_klass = AllocateNode::Ideal_klass(base, phase);
2539 if (allocated_klass != nullptr) {
2540 return allocated_klass;
2541 }
2542 }
2543
2544 // Simplify k.java_mirror.as_klass to plain k, where k is a Klass*.
2545 // See inline_native_Class_query for occurrences of these patterns.
2546 // Java Example: x.getClass().isAssignableFrom(y)
2547 //
2548 // This improves reflective code, often making the Class
2549 // mirror go completely dead. (Current exception: Class
2550 // mirrors may appear in debug info, but we could clean them out by
2551 // introducing a new debug info operator for Klass.java_mirror).
2552
2553 if (toop->isa_instptr() && toop->is_instptr()->instance_klass() == phase->C->env()->Class_klass()
2554 && offset == java_lang_Class::klass_offset()) {
2555 if (base->is_Load()) {
2556 Node* base2 = base->in(MemNode::Address);
2557 if (base2->is_Load()) { /* direct load of a load which is the OopHandle */
2558 Node* adr2 = base2->in(MemNode::Address);
2559 const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr();
2560 if (tkls != nullptr && !tkls->empty()
2561 && (tkls->isa_instklassptr() || tkls->isa_aryklassptr())
2562 && adr2->is_AddP()
2563 ) {
2564 int mirror_field = in_bytes(Klass::java_mirror_offset());
2565 if (tkls->offset() == mirror_field) {
2566 return adr2->in(AddPNode::Base);
2567 }
2568 }
2569 }
2570 }
2571 }
2572
2573 return this;
2574 }
2575
2576 LoadNode* LoadNode::clone_pinned() const {
2577 LoadNode* ld = clone()->as_Load();
2578 ld->_control_dependency = UnknownControl;
2579 return ld;
2580 }
2581
2582
2583 //------------------------------Value------------------------------------------
2688 //---------------------------StoreNode::make-----------------------------------
2689 // Polymorphic factory method:
2690 StoreNode* StoreNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt, MemOrd mo, bool require_atomic_access) {
2691 assert((mo == unordered || mo == release), "unexpected");
2692 Compile* C = gvn.C;
2693 assert(C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
2694 ctl != nullptr, "raw memory operations should have control edge");
2695
2696 switch (bt) {
2697 case T_BOOLEAN: val = gvn.transform(new AndINode(val, gvn.intcon(0x1))); // Fall through to T_BYTE case
2698 case T_BYTE: return new StoreBNode(ctl, mem, adr, adr_type, val, mo);
2699 case T_INT: return new StoreINode(ctl, mem, adr, adr_type, val, mo);
2700 case T_CHAR:
2701 case T_SHORT: return new StoreCNode(ctl, mem, adr, adr_type, val, mo);
2702 case T_LONG: return new StoreLNode(ctl, mem, adr, adr_type, val, mo, require_atomic_access);
2703 case T_FLOAT: return new StoreFNode(ctl, mem, adr, adr_type, val, mo);
2704 case T_DOUBLE: return new StoreDNode(ctl, mem, adr, adr_type, val, mo, require_atomic_access);
2705 case T_METADATA:
2706 case T_ADDRESS:
2707 case T_OBJECT:
2708 #ifdef _LP64
2709 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
2710 val = gvn.transform(new EncodePNode(val, val->bottom_type()->make_narrowoop()));
2711 return new StoreNNode(ctl, mem, adr, adr_type, val, mo);
2712 } else if (adr->bottom_type()->is_ptr_to_narrowklass() ||
2713 (UseCompressedClassPointers && val->bottom_type()->isa_klassptr() &&
2714 adr->bottom_type()->isa_rawptr())) {
2715 val = gvn.transform(new EncodePKlassNode(val, val->bottom_type()->make_narrowklass()));
2716 return new StoreNKlassNode(ctl, mem, adr, adr_type, val, mo);
2717 }
2718 #endif
2719 {
2720 return new StorePNode(ctl, mem, adr, adr_type, val, mo);
2721 }
2722 default:
2723 ShouldNotReachHere();
2724 return (StoreNode*)nullptr;
2725 }
2726 }
2727
2728 //--------------------------bottom_type----------------------------------------
2729 const Type *StoreNode::bottom_type() const {
2730 return Type::MEMORY;
2731 }
2732
2733 //------------------------------hash-------------------------------------------
2734 uint StoreNode::hash() const {
2735 // unroll addition of interesting fields
2736 //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn);
2737
2738 // Since they are not commoned, do not hash them:
2739 return NO_HASH;
2740 }
2741
2742 // Link together multiple stores (B/S/C/I) into a longer one.
2743 //
3365 }
3366 ss.print_cr("[TraceMergeStores]: with");
3367 merged_input_value->dump("\n", false, &ss);
3368 merged_store->dump("\n", false, &ss);
3369 tty->print("%s", ss.as_string());
3370 }
3371 #endif
3372
3373 //------------------------------Ideal------------------------------------------
3374 // Change back-to-back Store(, p, x) -> Store(m, p, y) to Store(m, p, x).
3375 // When a store immediately follows a relevant allocation/initialization,
3376 // try to capture it into the initialization, or hoist it above.
3377 Node *StoreNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3378 Node* p = MemNode::Ideal_common(phase, can_reshape);
3379 if (p) return (p == NodeSentinel) ? nullptr : p;
3380
3381 Node* mem = in(MemNode::Memory);
3382 Node* address = in(MemNode::Address);
3383 Node* value = in(MemNode::ValueIn);
3384 // Back-to-back stores to same address? Fold em up. Generally
3385 // unsafe if I have intervening uses.
3386 {
3387 Node* st = mem;
3388 // If Store 'st' has more than one use, we cannot fold 'st' away.
3389 // For example, 'st' might be the final state at a conditional
3390 // return. Or, 'st' might be used by some node which is live at
3391 // the same time 'st' is live, which might be unschedulable. So,
3392 // require exactly ONE user until such time as we clone 'mem' for
3393 // each of 'mem's uses (thus making the exactly-1-user-rule hold
3394 // true).
3395 while (st->is_Store() && st->outcnt() == 1) {
3396 // Looking at a dead closed cycle of memory?
3397 assert(st != st->in(MemNode::Memory), "dead loop in StoreNode::Ideal");
3398 assert(Opcode() == st->Opcode() ||
3399 st->Opcode() == Op_StoreVector ||
3400 Opcode() == Op_StoreVector ||
3401 st->Opcode() == Op_StoreVectorScatter ||
3402 Opcode() == Op_StoreVectorScatter ||
3403 phase->C->get_alias_index(adr_type()) == Compile::AliasIdxRaw ||
3404 (Opcode() == Op_StoreL && st->Opcode() == Op_StoreI) || // expanded ClearArrayNode
3405 (Opcode() == Op_StoreI && st->Opcode() == Op_StoreL) || // initialization by arraycopy
3406 (is_mismatched_access() || st->as_Store()->is_mismatched_access()),
3407 "no mismatched stores, except on raw memory: %s %s", NodeClassNames[Opcode()], NodeClassNames[st->Opcode()]);
3408
3409 if (st->in(MemNode::Address)->eqv_uncast(address) &&
3410 st->as_Store()->memory_size() <= this->memory_size()) {
3411 Node* use = st->raw_out(0);
3412 if (phase->is_IterGVN()) {
3413 phase->is_IterGVN()->rehash_node_delayed(use);
3414 }
3415 // It's OK to do this in the parser, since DU info is always accurate,
3416 // and the parser always refers to nodes via SafePointNode maps.
3417 use->set_req_X(MemNode::Memory, st->in(MemNode::Memory), phase);
3418 return this;
3419 }
3420 st = st->in(MemNode::Memory);
3421 }
3422 }
3423
3424
3425 // Capture an unaliased, unconditional, simple store into an initializer.
3523 const StoreVectorNode* store_vector = as_StoreVector();
3524 const StoreVectorNode* mem_vector = mem->as_StoreVector();
3525 const Node* store_indices = store_vector->indices();
3526 const Node* mem_indices = mem_vector->indices();
3527 const Node* store_mask = store_vector->mask();
3528 const Node* mem_mask = mem_vector->mask();
3529 // Ensure types, indices, and masks match
3530 if (store_vector->vect_type() == mem_vector->vect_type() &&
3531 ((store_indices == nullptr) == (mem_indices == nullptr) &&
3532 (store_indices == nullptr || store_indices->eqv_uncast(mem_indices))) &&
3533 ((store_mask == nullptr) == (mem_mask == nullptr) &&
3534 (store_mask == nullptr || store_mask->eqv_uncast(mem_mask)))) {
3535 result = mem;
3536 }
3537 }
3538 }
3539
3540 // Store of zero anywhere into a freshly-allocated object?
3541 // Then the store is useless.
3542 // (It must already have been captured by the InitializeNode.)
3543 if (result == this &&
3544 ReduceFieldZeroing && phase->type(val)->is_zero_type()) {
3545 // a newly allocated object is already all-zeroes everywhere
3546 if (mem->is_Proj() && mem->in(0)->is_Allocate()) {
3547 result = mem;
3548 }
3549
3550 if (result == this) {
3551 // the store may also apply to zero-bits in an earlier object
3552 Node* prev_mem = find_previous_store(phase);
3553 // Steps (a), (b): Walk past independent stores to find an exact match.
3554 if (prev_mem != nullptr) {
3555 Node* prev_val = can_see_stored_value(prev_mem, phase);
3556 if (prev_val != nullptr && prev_val == val) {
3557 // prev_val and val might differ by a cast; it would be good
3558 // to keep the more informative of the two.
3559 result = mem;
3560 }
3561 }
3562 }
3563 }
3564
3565 PhaseIterGVN* igvn = phase->is_IterGVN();
3566 if (result != this && igvn != nullptr) {
3567 MemBarNode* trailing = trailing_membar();
3568 if (trailing != nullptr) {
3569 #ifdef ASSERT
3570 const TypeOopPtr* t_oop = phase->type(in(Address))->isa_oopptr();
4039 // Clearing a short array is faster with stores
4040 Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) {
4041 // Already know this is a large node, do not try to ideal it
4042 if (_is_large) return nullptr;
4043
4044 const int unit = BytesPerLong;
4045 const TypeX* t = phase->type(in(2))->isa_intptr_t();
4046 if (!t) return nullptr;
4047 if (!t->is_con()) return nullptr;
4048 intptr_t raw_count = t->get_con();
4049 intptr_t size = raw_count;
4050 if (!Matcher::init_array_count_is_in_bytes) size *= unit;
4051 // Clearing nothing uses the Identity call.
4052 // Negative clears are possible on dead ClearArrays
4053 // (see jck test stmt114.stmt11402.val).
4054 if (size <= 0 || size % unit != 0) return nullptr;
4055 intptr_t count = size / unit;
4056 // Length too long; communicate this to matchers and assemblers.
4057 // Assemblers are responsible to produce fast hardware clears for it.
4058 if (size > InitArrayShortSize) {
4059 return new ClearArrayNode(in(0), in(1), in(2), in(3), true);
4060 } else if (size > 2 && Matcher::match_rule_supported_vector(Op_ClearArray, 4, T_LONG)) {
4061 return nullptr;
4062 }
4063 if (!IdealizeClearArrayNode) return nullptr;
4064 Node *mem = in(1);
4065 if( phase->type(mem)==Type::TOP ) return nullptr;
4066 Node *adr = in(3);
4067 const Type* at = phase->type(adr);
4068 if( at==Type::TOP ) return nullptr;
4069 const TypePtr* atp = at->isa_ptr();
4070 // adjust atp to be the correct array element address type
4071 if (atp == nullptr) atp = TypePtr::BOTTOM;
4072 else atp = atp->add_offset(Type::OffsetBot);
4073 // Get base for derived pointer purposes
4074 if( adr->Opcode() != Op_AddP ) Unimplemented();
4075 Node *base = adr->in(1);
4076
4077 Node *zero = phase->makecon(TypeLong::ZERO);
4078 Node *off = phase->MakeConX(BytesPerLong);
4079 mem = new StoreLNode(in(0),mem,adr,atp,zero,MemNode::unordered,false);
4080 count--;
4081 while( count-- ) {
4082 mem = phase->transform(mem);
4083 adr = phase->transform(new AddPNode(base,adr,off));
4084 mem = new StoreLNode(in(0),mem,adr,atp,zero,MemNode::unordered,false);
4085 }
4086 return mem;
4087 }
4088
4089 //----------------------------step_through----------------------------------
4090 // Return allocation input memory edge if it is different instance
4091 // or itself if it is the one we are looking for.
4092 bool ClearArrayNode::step_through(Node** np, uint instance_id, PhaseValues* phase) {
4093 Node* n = *np;
4094 assert(n->is_ClearArray(), "sanity");
4095 intptr_t offset;
4096 AllocateNode* alloc = AllocateNode::Ideal_allocation(n->in(3), phase, offset);
4097 // This method is called only before Allocate nodes are expanded
4098 // during macro nodes expansion. Before that ClearArray nodes are
4099 // only generated in PhaseMacroExpand::generate_arraycopy() (before
4100 // Allocate nodes are expanded) which follows allocations.
4101 assert(alloc != nullptr, "should have allocation");
4102 if (alloc->_idx == instance_id) {
4103 // Can not bypass initialization of the instance we are looking for.
4104 return false;
4105 }
4106 // Otherwise skip it.
4107 InitializeNode* init = alloc->initialization();
4108 if (init != nullptr)
4109 *np = init->in(TypeFunc::Memory);
4110 else
4111 *np = alloc->in(TypeFunc::Memory);
4112 return true;
4113 }
4114
4115 //----------------------------clear_memory-------------------------------------
4116 // Generate code to initialize object storage to zero.
4117 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
4118 intptr_t start_offset,
4119 Node* end_offset,
4120 PhaseGVN* phase) {
4121 intptr_t offset = start_offset;
4122
4123 int unit = BytesPerLong;
4124 if ((offset % unit) != 0) {
4125 Node* adr = new AddPNode(dest, dest, phase->MakeConX(offset));
4126 adr = phase->transform(adr);
4127 const TypePtr* atp = TypeRawPtr::BOTTOM;
4128 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered);
4129 mem = phase->transform(mem);
4130 offset += BytesPerInt;
4131 }
4132 assert((offset % unit) == 0, "");
4133
4134 // Initialize the remaining stuff, if any, with a ClearArray.
4135 return clear_memory(ctl, mem, dest, phase->MakeConX(offset), end_offset, phase);
4136 }
4137
4138 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
4139 Node* start_offset,
4140 Node* end_offset,
4141 PhaseGVN* phase) {
4142 if (start_offset == end_offset) {
4143 // nothing to do
4144 return mem;
4145 }
4146
4147 int unit = BytesPerLong;
4148 Node* zbase = start_offset;
4149 Node* zend = end_offset;
4150
4151 // Scale to the unit required by the CPU:
4152 if (!Matcher::init_array_count_is_in_bytes) {
4153 Node* shift = phase->intcon(exact_log2(unit));
4154 zbase = phase->transform(new URShiftXNode(zbase, shift) );
4155 zend = phase->transform(new URShiftXNode(zend, shift) );
4156 }
4157
4158 // Bulk clear double-words
4159 Node* zsize = phase->transform(new SubXNode(zend, zbase) );
4160 Node* adr = phase->transform(new AddPNode(dest, dest, start_offset) );
4161 mem = new ClearArrayNode(ctl, mem, zsize, adr, false);
4162 return phase->transform(mem);
4163 }
4164
4165 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
4166 intptr_t start_offset,
4167 intptr_t end_offset,
4168 PhaseGVN* phase) {
4169 if (start_offset == end_offset) {
4170 // nothing to do
4171 return mem;
4172 }
4173
4174 assert((end_offset % BytesPerInt) == 0, "odd end offset");
4175 intptr_t done_offset = end_offset;
4176 if ((done_offset % BytesPerLong) != 0) {
4177 done_offset -= BytesPerInt;
4178 }
4179 if (done_offset > start_offset) {
4180 mem = clear_memory(ctl, mem, dest,
4181 start_offset, phase->MakeConX(done_offset), phase);
4182 }
4183 if (done_offset < end_offset) { // emit the final 32-bit store
4184 Node* adr = new AddPNode(dest, dest, phase->MakeConX(done_offset));
4185 adr = phase->transform(adr);
4186 const TypePtr* atp = TypeRawPtr::BOTTOM;
4187 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered);
4188 mem = phase->transform(mem);
4189 done_offset += BytesPerInt;
4190 }
4191 assert(done_offset == end_offset, "");
4192 return mem;
4193 }
4194
4195 //=============================================================================
4196 MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent)
4197 : MultiNode(TypeFunc::Parms + (precedent == nullptr? 0: 1)),
4198 _adr_type(C->get_adr_type(alias_idx)), _kind(Standalone)
4199 #ifdef ASSERT
4200 , _pair_idx(0)
4201 #endif
4202 {
4203 init_class_id(Class_MemBar);
4204 Node* top = C->top();
4205 init_req(TypeFunc::I_O,top);
4206 init_req(TypeFunc::FramePtr,top);
4207 init_req(TypeFunc::ReturnAdr,top);
4314 PhaseIterGVN* igvn = phase->is_IterGVN();
4315 remove(igvn);
4316 // Must return either the original node (now dead) or a new node
4317 // (Do not return a top here, since that would break the uniqueness of top.)
4318 return new ConINode(TypeInt::ZERO);
4319 }
4320 }
4321 return progress ? this : nullptr;
4322 }
4323
4324 //------------------------------Value------------------------------------------
4325 const Type* MemBarNode::Value(PhaseGVN* phase) const {
4326 if( !in(0) ) return Type::TOP;
4327 if( phase->type(in(0)) == Type::TOP )
4328 return Type::TOP;
4329 return TypeTuple::MEMBAR;
4330 }
4331
4332 //------------------------------match------------------------------------------
4333 // Construct projections for memory.
4334 Node *MemBarNode::match( const ProjNode *proj, const Matcher *m ) {
4335 switch (proj->_con) {
4336 case TypeFunc::Control:
4337 case TypeFunc::Memory:
4338 return new MachProjNode(this, proj->_con, RegMask::EMPTY, MachProjNode::unmatched_proj);
4339 }
4340 ShouldNotReachHere();
4341 return nullptr;
4342 }
4343
4344 void MemBarNode::set_store_pair(MemBarNode* leading, MemBarNode* trailing) {
4345 trailing->_kind = TrailingStore;
4346 leading->_kind = LeadingStore;
4347 #ifdef ASSERT
4348 trailing->_pair_idx = leading->_idx;
4349 leading->_pair_idx = leading->_idx;
4350 #endif
4351 }
4352
4353 void MemBarNode::set_load_store_pair(MemBarNode* leading, MemBarNode* trailing) {
4354 trailing->_kind = TrailingLoadStore;
4601 return (req() > RawStores);
4602 }
4603
4604 void InitializeNode::set_complete(PhaseGVN* phase) {
4605 assert(!is_complete(), "caller responsibility");
4606 _is_complete = Complete;
4607
4608 // After this node is complete, it contains a bunch of
4609 // raw-memory initializations. There is no need for
4610 // it to have anything to do with non-raw memory effects.
4611 // Therefore, tell all non-raw users to re-optimize themselves,
4612 // after skipping the memory effects of this initialization.
4613 PhaseIterGVN* igvn = phase->is_IterGVN();
4614 if (igvn) igvn->add_users_to_worklist(this);
4615 }
4616
4617 // convenience function
4618 // return false if the init contains any stores already
4619 bool AllocateNode::maybe_set_complete(PhaseGVN* phase) {
4620 InitializeNode* init = initialization();
4621 if (init == nullptr || init->is_complete()) return false;
4622 init->remove_extra_zeroes();
4623 // for now, if this allocation has already collected any inits, bail:
4624 if (init->is_non_zero()) return false;
4625 init->set_complete(phase);
4626 return true;
4627 }
4628
4629 void InitializeNode::remove_extra_zeroes() {
4630 if (req() == RawStores) return;
4631 Node* zmem = zero_memory();
4632 uint fill = RawStores;
4633 for (uint i = fill; i < req(); i++) {
4634 Node* n = in(i);
4635 if (n->is_top() || n == zmem) continue; // skip
4636 if (fill < i) set_req(fill, n); // compact
4637 ++fill;
4638 }
4639 // delete any empty spaces created:
4640 while (fill < req()) {
4641 del_req(fill);
4785 // store node that we'd like to capture. We need to check
4786 // the uses of the MergeMemNode.
4787 mems.push(n);
4788 }
4789 } else if (n->is_Mem()) {
4790 Node* other_adr = n->in(MemNode::Address);
4791 if (other_adr == adr) {
4792 failed = true;
4793 break;
4794 } else {
4795 const TypePtr* other_t_adr = phase->type(other_adr)->isa_ptr();
4796 if (other_t_adr != nullptr) {
4797 int other_alias_idx = phase->C->get_alias_index(other_t_adr);
4798 if (other_alias_idx == alias_idx) {
4799 // A load from the same memory slice as the store right
4800 // after the InitializeNode. We check the control of the
4801 // object/array that is loaded from. If it's the same as
4802 // the store control then we cannot capture the store.
4803 assert(!n->is_Store(), "2 stores to same slice on same control?");
4804 Node* base = other_adr;
4805 assert(base->is_AddP(), "should be addp but is %s", base->Name());
4806 base = base->in(AddPNode::Base);
4807 if (base != nullptr) {
4808 base = base->uncast();
4809 if (base->is_Proj() && base->in(0) == alloc) {
4810 failed = true;
4811 break;
4812 }
4813 }
4814 }
4815 }
4816 }
4817 } else {
4818 failed = true;
4819 break;
4820 }
4821 }
4822 }
4823 }
4824 if (failed) {
5371 // z's_done 12 16 16 16 12 16 12
5372 // z's_needed 12 16 16 16 16 16 16
5373 // zsize 0 0 0 0 4 0 4
5374 if (next_full_store < 0) {
5375 // Conservative tack: Zero to end of current word.
5376 zeroes_needed = align_up(zeroes_needed, BytesPerInt);
5377 } else {
5378 // Zero to beginning of next fully initialized word.
5379 // Or, don't zero at all, if we are already in that word.
5380 assert(next_full_store >= zeroes_needed, "must go forward");
5381 assert((next_full_store & (BytesPerInt-1)) == 0, "even boundary");
5382 zeroes_needed = next_full_store;
5383 }
5384 }
5385
5386 if (zeroes_needed > zeroes_done) {
5387 intptr_t zsize = zeroes_needed - zeroes_done;
5388 // Do some incremental zeroing on rawmem, in parallel with inits.
5389 zeroes_done = align_down(zeroes_done, BytesPerInt);
5390 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
5391 zeroes_done, zeroes_needed,
5392 phase);
5393 zeroes_done = zeroes_needed;
5394 if (zsize > InitArrayShortSize && ++big_init_gaps > 2)
5395 do_zeroing = false; // leave the hole, next time
5396 }
5397 }
5398
5399 // Collect the store and move on:
5400 phase->replace_input_of(st, MemNode::Memory, inits);
5401 inits = st; // put it on the linearized chain
5402 set_req(i, zmem); // unhook from previous position
5403
5404 if (zeroes_done == st_off)
5405 zeroes_done = next_init_off;
5406
5407 assert(!do_zeroing || zeroes_done >= next_init_off, "don't miss any");
5408
5409 #ifdef ASSERT
5410 // Various order invariants. Weaker than stores_are_sane because
5430 remove_extra_zeroes(); // clear out all the zmems left over
5431 add_req(inits);
5432
5433 if (!(UseTLAB && ZeroTLAB)) {
5434 // If anything remains to be zeroed, zero it all now.
5435 zeroes_done = align_down(zeroes_done, BytesPerInt);
5436 // if it is the last unused 4 bytes of an instance, forget about it
5437 intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, max_jint);
5438 if (zeroes_done + BytesPerLong >= size_limit) {
5439 AllocateNode* alloc = allocation();
5440 assert(alloc != nullptr, "must be present");
5441 if (alloc != nullptr && alloc->Opcode() == Op_Allocate) {
5442 Node* klass_node = alloc->in(AllocateNode::KlassNode);
5443 ciKlass* k = phase->type(klass_node)->is_instklassptr()->instance_klass();
5444 if (zeroes_done == k->layout_helper())
5445 zeroes_done = size_limit;
5446 }
5447 }
5448 if (zeroes_done < size_limit) {
5449 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
5450 zeroes_done, size_in_bytes, phase);
5451 }
5452 }
5453
5454 set_complete(phase);
5455 return rawmem;
5456 }
5457
5458 void InitializeNode::replace_mem_projs_by(Node* mem, Compile* C) {
5459 auto replace_proj = [&](ProjNode* proj) {
5460 C->gvn_replace_by(proj, mem);
5461 return CONTINUE;
5462 };
5463 apply_to_projs(replace_proj, TypeFunc::Memory);
5464 }
5465
5466 void InitializeNode::replace_mem_projs_by(Node* mem, PhaseIterGVN* igvn) {
5467 DUIterator_Fast imax, i = fast_outs(imax);
5468 auto replace_proj = [&](ProjNode* proj) {
5469 igvn->replace_node(proj, mem);
|
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 #include "ci/ciFlatArrayKlass.hpp"
27 #include "ci/ciInlineKlass.hpp"
28 #include "classfile/javaClasses.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "compiler/compileLog.hpp"
31 #include "gc/shared/barrierSet.hpp"
32 #include "gc/shared/c2/barrierSetC2.hpp"
33 #include "gc/shared/tlab_globals.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include "memory/resourceArea.hpp"
36 #include "oops/flatArrayKlass.hpp"
37 #include "oops/objArrayKlass.hpp"
38 #include "opto/addnode.hpp"
39 #include "opto/arraycopynode.hpp"
40 #include "opto/callnode.hpp"
41 #include "opto/cfgnode.hpp"
42 #include "opto/compile.hpp"
43 #include "opto/connode.hpp"
44 #include "opto/convertnode.hpp"
45 #include "opto/inlinetypenode.hpp"
46 #include "opto/loopnode.hpp"
47 #include "opto/machnode.hpp"
48 #include "opto/matcher.hpp"
49 #include "opto/memnode.hpp"
50 #include "opto/mempointer.hpp"
51 #include "opto/mulnode.hpp"
52 #include "opto/narrowptrnode.hpp"
53 #include "opto/phaseX.hpp"
54 #include "opto/regalloc.hpp"
55 #include "opto/regmask.hpp"
56 #include "opto/rootnode.hpp"
57 #include "opto/traceMergeStoresTag.hpp"
58 #include "opto/vectornode.hpp"
59 #include "runtime/arguments.hpp"
60 #include "utilities/align.hpp"
61 #include "utilities/copy.hpp"
62 #include "utilities/globalDefinitions.hpp"
63 #include "utilities/macros.hpp"
64 #include "utilities/powerOfTwo.hpp"
65 #include "utilities/vmError.hpp"
66
67 // Portions of code courtesy of Clifford Click
68
69 // Optimization - Graph Style
70
71 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st);
72
73 //=============================================================================
74 uint MemNode::size_of() const { return sizeof(*this); }
75
76 const TypePtr *MemNode::adr_type() const {
77 Node* adr = in(Address);
78 if (adr == nullptr) return nullptr; // node is dead
79 const TypePtr* cross_check = nullptr;
80 DEBUG_ONLY(cross_check = _adr_type);
81 return calculate_adr_type(adr->bottom_type(), cross_check);
82 }
131 st->print(", idx=Bot;");
132 else if (atp->index() == Compile::AliasIdxTop)
133 st->print(", idx=Top;");
134 else if (atp->index() == Compile::AliasIdxRaw)
135 st->print(", idx=Raw;");
136 else {
137 ciField* field = atp->field();
138 if (field) {
139 st->print(", name=");
140 field->print_name_on(st);
141 }
142 st->print(", idx=%d;", atp->index());
143 }
144 }
145 }
146
147 extern void print_alias_types();
148
149 #endif
150
151 // Find the memory output corresponding to the fall-through path of a call
152 static Node* find_call_fallthrough_mem_output(CallNode* call) {
153 ResourceMark rm;
154 CallProjections* projs = call->extract_projections(false, false);
155 Node* res = projs->fallthrough_memproj;
156 assert(res != nullptr, "must have a fallthrough mem output");
157 return res;
158 }
159
160 // Try to find a better memory input for a load from a strict final field
161 static Node* try_optimize_strict_final_load_memory(PhaseGVN* phase, Node* adr, ProjNode*& base_local) {
162 intptr_t offset = 0;
163 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
164 if (base == nullptr) {
165 return nullptr;
166 }
167
168 Node* base_uncasted = base->uncast();
169 if (base_uncasted->is_Proj()) {
170 Node* multi = base_uncasted->in(0);
171 if (multi->is_top()) {
172 // The pointer dies, make the memory die, too
173 return multi;
174 } else if (multi->is_Allocate()) {
175 base_local = base_uncasted->as_Proj();
176 return nullptr;
177 } else if (multi->is_Call()) {
178 // The oop is returned from a call, the memory can be the fallthrough output of the call
179 return find_call_fallthrough_mem_output(multi->as_Call());
180 } else if (multi->is_Start()) {
181 // The oop is a parameter
182 if (phase->C->method()->is_object_constructor() && base_uncasted->as_Proj()->_con == TypeFunc::Parms) {
183 // The receiver of a constructor is similar to the result of an AllocateNode
184 base_local = base_uncasted->as_Proj();
185 return nullptr;
186 } else {
187 // Use the start memory otherwise
188 return multi->as_Start()->proj_out(TypeFunc::Memory);
189 }
190 }
191 }
192
193 return nullptr;
194 }
195
196 // Whether a call can modify a strict final field, given that the object is allocated inside the
197 // current compilation unit, or is the first parameter when the compilation root is a constructor.
198 // This is equivalent to asking whether 'call' is a constructor invocation and the class declaring
199 // the target method is a subclass of the class declaring 'field'.
200 static bool call_can_modify_local_object(ciField* field, CallNode* call) {
201 if (!call->is_CallJava()) {
202 return false;
203 }
204
205 ciMethod* target = call->as_CallJava()->method();
206 if (target == nullptr || !target->is_object_constructor()) {
207 return false;
208 }
209
210 // If 'field' is declared in a class that is a subclass of the one declaring the constructor,
211 // then the field is set inside the constructor, else the field must be set before the
212 // constructor invocation. E.g. A field Super.x will be set during the execution of Sub::<init>,
213 // while a field Sub.y must be set before Super::<init> is invoked.
214 // We can try to be more heroic and decide if the receiver of the constructor invocation is the
215 // object from which we are loading from. This, however, may be problematic as deciding if 2
216 // nodes are definitely different may not be trivial, especially if the graph is not canonical.
217 // As a result, it is made more conservative for now.
218 assert(call->req() > TypeFunc::Parms, "constructor must have at least 1 argument");
219 return target->holder()->is_subclass_of(field->holder());
220 }
221
222 Node* MemNode::optimize_simple_memory_chain(Node* mchain, const TypeOopPtr* t_oop, Node* load, PhaseGVN* phase) {
223 assert(t_oop != nullptr, "sanity");
224 bool is_known_instance = t_oop->is_known_instance_field();
225 bool is_strict_final_load = false;
226
227 // After macro expansion, an allocation may become a call, changing the memory input to the
228 // memory output of that call would be illegal. As a result, disallow this transformation after
229 // macro expansion.
230 if (phase->is_IterGVN() && phase->C->allow_macro_nodes() && load != nullptr && load->is_Load() && !load->as_Load()->is_mismatched_access()) {
231 is_strict_final_load = t_oop->is_ptr_to_strict_final_field();
232 #ifdef ASSERT
233 if ((t_oop->is_inlinetypeptr() && t_oop->inline_klass()->contains_field_offset(t_oop->offset())) || t_oop->is_ptr_to_boxed_value()) {
234 assert(is_strict_final_load, "sanity check for basic cases");
235 }
236 #endif // ASSERT
237 }
238
239 if (!is_known_instance && !is_strict_final_load) {
240 return mchain;
241 }
242
243 Node* result = mchain;
244 ProjNode* base_local = nullptr;
245
246 ciField* field = nullptr;
247 if (is_strict_final_load) {
248 field = phase->C->alias_type(t_oop)->field();
249 assert(field != nullptr, "must point to a field");
250
251 Node* adr = load->in(MemNode::Address);
252 assert(phase->type(adr) == t_oop, "inconsistent type");
253 Node* tmp = try_optimize_strict_final_load_memory(phase, adr, base_local);
254 if (tmp != nullptr) {
255 result = tmp;
256 }
257 }
258
259 uint instance_id = t_oop->instance_id();
260 Node* start_mem = phase->C->start()->proj_out_or_null(TypeFunc::Memory);
261 Node* prev = nullptr;
262 while (prev != result) {
263 prev = result;
264 if (result == start_mem) {
265 // start_mem is the earliest memory possible
266 break;
267 }
268
269 // skip over a call which does not affect this memory slice
270 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
271 Node* proj_in = result->in(0);
272 if (proj_in->is_Allocate() && proj_in->_idx == instance_id) {
273 // This is the allocation that creates the object from which we are loading from
274 break;
275 } else if (proj_in->is_Call()) {
276 // ArrayCopyNodes processed here as well
277 CallNode* call = proj_in->as_Call();
278 if (!call->may_modify(t_oop, phase)) {
279 result = call->in(TypeFunc::Memory);
280 } else if (is_strict_final_load && base_local != nullptr && !call_can_modify_local_object(field, call)) {
281 result = call->in(TypeFunc::Memory);
282 }
283 } else if (proj_in->Opcode() == Op_Tuple) {
284 // The call will be folded, skip over it.
285 break;
286 } else if (proj_in->is_Initialize()) {
287 AllocateNode* alloc = proj_in->as_Initialize()->allocation();
288 // Stop if this is the initialization for the object instance which
289 // contains this memory slice, otherwise skip over it.
290 if ((alloc == nullptr) || (alloc->_idx == instance_id)) {
291 break;
292 }
293 if (is_known_instance) {
294 result = proj_in->in(TypeFunc::Memory);
295 } else if (is_strict_final_load) {
296 Node* klass = alloc->in(AllocateNode::KlassNode);
297 const TypeKlassPtr* tklass = phase->type(klass)->is_klassptr();
298 if (tklass->klass_is_exact() && !tklass->exact_klass()->equals(t_oop->is_instptr()->exact_klass())) {
299 // Allocation of another type, must be another object
300 result = proj_in->in(TypeFunc::Memory);
301 } else if (base_local != nullptr && (base_local->is_Parm() || base_local->in(0) != alloc)) {
302 // Allocation of another object
303 result = proj_in->in(TypeFunc::Memory);
304 }
305 }
306 } else if (proj_in->is_MemBar()) {
307 ArrayCopyNode* ac = nullptr;
308 if (ArrayCopyNode::may_modify(t_oop, proj_in->as_MemBar(), phase, ac)) {
309 break;
310 }
311 result = proj_in->in(TypeFunc::Memory);
312 } else if (proj_in->is_LoadFlat() || proj_in->is_StoreFlat()) {
313 if (is_strict_final_load) {
314 // LoadFlat and StoreFlat cannot happen to strict final fields
315 result = proj_in->in(TypeFunc::Memory);
316 }
317 } else if (proj_in->is_top()) {
318 break; // dead code
319 } else {
320 assert(false, "unexpected projection of %s", proj_in->Name());
321 }
322 } else if (result->is_ClearArray()) {
323 if (!is_known_instance || !ClearArrayNode::step_through(&result, instance_id, phase)) {
324 // Can not bypass initialization of the instance
325 // we are looking for.
326 break;
327 }
328 // Otherwise skip it (the call updated 'result' value).
329 } else if (result->is_MergeMem()) {
330 result = step_through_mergemem(phase, result->as_MergeMem(), t_oop, nullptr, tty);
331 }
332 }
333 return result;
334 }
335
336 Node *MemNode::optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase) {
337 const TypeOopPtr* t_oop = t_adr->isa_oopptr();
338 if (t_oop == nullptr)
339 return mchain; // don't try to optimize non-oop types
340 Node* result = optimize_simple_memory_chain(mchain, t_oop, load, phase);
341 bool is_instance = t_oop->is_known_instance_field();
342 PhaseIterGVN *igvn = phase->is_IterGVN();
343 if (is_instance && igvn != nullptr && result->is_Phi()) {
344 PhiNode *mphi = result->as_Phi();
345 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
346 const TypePtr *t = mphi->adr_type();
347 bool do_split = false;
348 // In the following cases, Load memory input can be further optimized based on
349 // its precise address type
350 if (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ) {
351 do_split = true;
352 } else if (t->isa_oopptr() && !t->is_oopptr()->is_known_instance()) {
353 const TypeOopPtr* mem_t =
354 t->is_oopptr()->cast_to_exactness(true)
355 ->is_oopptr()->cast_to_ptr_type(t_oop->ptr())
356 ->is_oopptr()->cast_to_instance_id(t_oop->instance_id());
357 if (t_oop->isa_aryptr()) {
358 mem_t = mem_t->is_aryptr()
359 ->cast_to_stable(t_oop->is_aryptr()->is_stable())
360 ->cast_to_size(t_oop->is_aryptr()->size())
361 ->cast_to_not_flat(t_oop->is_aryptr()->is_not_flat())
362 ->cast_to_not_null_free(t_oop->is_aryptr()->is_not_null_free())
363 ->with_offset(t_oop->is_aryptr()->offset())
364 ->is_aryptr();
365 }
366 do_split = mem_t == t_oop;
367 }
368 if (do_split) {
369 // clone the Phi with our address type
370 result = mphi->split_out_instance(t_adr, igvn);
371 } else {
372 assert(phase->C->get_alias_index(t) == phase->C->get_alias_index(t_adr), "correct memory chain");
373 }
374 }
375 return result;
376 }
377
378 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem, const TypePtr *tp, const TypePtr *adr_check, outputStream *st) {
379 uint alias_idx = phase->C->get_alias_index(tp);
380 Node *mem = mmem;
381 #ifdef ASSERT
382 {
383 // Check that current type is consistent with the alias index used during graph construction
384 assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx");
385 bool consistent = adr_check == nullptr || adr_check->empty() ||
386 phase->C->must_alias(adr_check, alias_idx );
387 // Sometimes dead array references collapse to a[-1], a[-2], or a[-3]
388 if( !consistent && adr_check != nullptr && !adr_check->empty() &&
389 tp->isa_aryptr() && tp->offset() == Type::OffsetBot &&
390 adr_check->isa_aryptr() && adr_check->offset() != Type::OffsetBot &&
391 ( adr_check->offset() == arrayOopDesc::length_offset_in_bytes() ||
392 adr_check->offset() == oopDesc::klass_offset_in_bytes() ||
393 adr_check->offset() == oopDesc::mark_offset_in_bytes() ) ) {
394 // don't assert if it is dead code.
395 consistent = true;
396 }
397 if( !consistent ) {
398 st->print("alias_idx==%d, adr_check==", alias_idx);
399 if( adr_check == nullptr ) {
400 st->print("null");
401 } else {
402 adr_check->dump();
403 }
404 st->cr();
405 print_alias_types();
406 assert(consistent, "adr_check must match alias idx");
407 }
408 }
409 #endif
1081 "use LoadKlassNode instead");
1082 assert(!(adr_type->isa_aryptr() &&
1083 adr_type->offset() == arrayOopDesc::length_offset_in_bytes()),
1084 "use LoadRangeNode instead");
1085 // Check control edge of raw loads
1086 assert( ctl != nullptr || C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
1087 // oop will be recorded in oop map if load crosses safepoint
1088 rt->isa_oopptr() || is_immutable_value(adr),
1089 "raw memory operations should have control edge");
1090 LoadNode* load = nullptr;
1091 switch (bt) {
1092 case T_BOOLEAN: load = new LoadUBNode(ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
1093 case T_BYTE: load = new LoadBNode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
1094 case T_INT: load = new LoadINode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
1095 case T_CHAR: load = new LoadUSNode(ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
1096 case T_SHORT: load = new LoadSNode (ctl, mem, adr, adr_type, rt->is_int(), mo, control_dependency); break;
1097 case T_LONG: load = new LoadLNode (ctl, mem, adr, adr_type, rt->is_long(), mo, control_dependency, require_atomic_access); break;
1098 case T_FLOAT: load = new LoadFNode (ctl, mem, adr, adr_type, rt, mo, control_dependency); break;
1099 case T_DOUBLE: load = new LoadDNode (ctl, mem, adr, adr_type, rt, mo, control_dependency, require_atomic_access); break;
1100 case T_ADDRESS: load = new LoadPNode (ctl, mem, adr, adr_type, rt->is_ptr(), mo, control_dependency); break;
1101 case T_ARRAY:
1102 case T_OBJECT:
1103 case T_NARROWOOP:
1104 #ifdef _LP64
1105 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
1106 load = new LoadNNode(ctl, mem, adr, adr_type, rt->make_narrowoop(), mo, control_dependency);
1107 } else
1108 #endif
1109 {
1110 assert(!adr->bottom_type()->is_ptr_to_narrowoop() && !adr->bottom_type()->is_ptr_to_narrowklass(), "should have got back a narrow oop");
1111 load = new LoadPNode(ctl, mem, adr, adr_type, rt->is_ptr(), mo, control_dependency);
1112 }
1113 break;
1114 default:
1115 assert(false, "unexpected basic type %s", type2name(bt));
1116 break;
1117 }
1118 assert(load != nullptr, "LoadNode should have been created");
1119 if (unaligned) {
1120 load->set_unaligned_access();
1121 }
1122 if (mismatched) {
1123 load->set_mismatched_access();
1124 }
1125 if (unsafe) {
1126 load->set_unsafe_access();
1127 }
1128 load->set_barrier_data(barrier_data);
1129 if (load->Opcode() == Op_LoadN) {
1130 Node* ld = gvn.transform(load);
1131 return new DecodeNNode(ld, ld->bottom_type()->make_ptr());
1132 }
1133
1134 return load;
1135 }
1136
1137 //------------------------------hash-------------------------------------------
1138 uint LoadNode::hash() const {
1139 // unroll addition of interesting fields
1140 return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
1141 }
1142
1143 static bool skip_through_membars(Compile::AliasType* atp, const TypeInstPtr* tp, bool eliminate_boxing) {
1144 if ((atp != nullptr) && (atp->index() >= Compile::AliasIdxRaw)) {
1145 bool non_volatile = (atp->field() != nullptr) && !atp->field()->is_volatile();
1146 bool is_stable_ary = FoldStableValues &&
1147 (tp != nullptr) && (tp->isa_aryptr() != nullptr) &&
1148 tp->isa_aryptr()->is_stable();
1149
1150 return (eliminate_boxing && non_volatile) || is_stable_ary || tp->is_inlinetypeptr();
1151 }
1152
1153 return false;
1154 }
1155
1156 LoadNode* LoadNode::pin_array_access_node() const {
1157 const TypePtr* adr_type = this->adr_type();
1158 if (adr_type != nullptr && adr_type->isa_aryptr()) {
1159 return clone_pinned();
1160 }
1161 return nullptr;
1162 }
1163
1164 // Is the value loaded previously stored by an arraycopy? If so return
1165 // a load node that reads from the source array so we may be able to
1166 // optimize out the ArrayCopy node later.
1167 Node* LoadNode::can_see_arraycopy_value(Node* st, PhaseGVN* phase) const {
1168 Node* ld_adr = in(MemNode::Address);
1169 intptr_t ld_off = 0;
1170 AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off);
1186 if (ac->as_ArrayCopy()->is_clonebasic()) {
1187 assert(ld_alloc != nullptr, "need an alloc");
1188 assert(addp->is_AddP(), "address must be addp");
1189 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1190 assert(bs->step_over_gc_barrier(addp->in(AddPNode::Base)) == bs->step_over_gc_barrier(ac->in(ArrayCopyNode::Dest)), "strange pattern");
1191 assert(bs->step_over_gc_barrier(addp->in(AddPNode::Address)) == bs->step_over_gc_barrier(ac->in(ArrayCopyNode::Dest)), "strange pattern");
1192 addp->set_req(AddPNode::Base, src);
1193 addp->set_req(AddPNode::Address, src);
1194 } else {
1195 assert(ac->as_ArrayCopy()->is_arraycopy_validated() ||
1196 ac->as_ArrayCopy()->is_copyof_validated() ||
1197 ac->as_ArrayCopy()->is_copyofrange_validated(), "only supported cases");
1198 assert(addp->in(AddPNode::Base) == addp->in(AddPNode::Address), "should be");
1199 addp->set_req(AddPNode::Base, src);
1200 addp->set_req(AddPNode::Address, src);
1201
1202 const TypeAryPtr* ary_t = phase->type(in(MemNode::Address))->isa_aryptr();
1203 BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type();
1204 if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT;
1205
1206 uint shift = ary_t->is_flat() ? ary_t->flat_log_elem_size() : exact_log2(type2aelembytes(ary_elem));
1207
1208 Node* diff = phase->transform(new SubINode(ac->in(ArrayCopyNode::SrcPos), ac->in(ArrayCopyNode::DestPos)));
1209 #ifdef _LP64
1210 diff = phase->transform(new ConvI2LNode(diff));
1211 #endif
1212 diff = phase->transform(new LShiftXNode(diff, phase->intcon(shift)));
1213
1214 Node* offset = phase->transform(new AddXNode(addp->in(AddPNode::Offset), diff));
1215 addp->set_req(AddPNode::Offset, offset);
1216 }
1217 addp = phase->transform(addp);
1218 #ifdef ASSERT
1219 const TypePtr* adr_type = phase->type(addp)->is_ptr();
1220 ld->_adr_type = adr_type;
1221 #endif
1222 ld->set_req(MemNode::Address, addp);
1223 ld->set_req(0, ctl);
1224 ld->set_req(MemNode::Memory, mem);
1225 return ld;
1226 }
1227 return nullptr;
1228 }
1229
1230 static Node* see_through_inline_type(PhaseValues* phase, const MemNode* load, Node* base, int offset) {
1231 if (!load->is_mismatched_access() && base != nullptr && base->is_InlineType() && offset > oopDesc::klass_offset_in_bytes()) {
1232 InlineTypeNode* vt = base->as_InlineType();
1233 Node* value = vt->field_value_by_offset(offset, true);
1234 assert(value != nullptr, "must see some value");
1235 return value;
1236 }
1237
1238 return nullptr;
1239 }
1240
1241 //---------------------------can_see_stored_value------------------------------
1242 // This routine exists to make sure this set of tests is done the same
1243 // everywhere. We need to make a coordinated change: first LoadNode::Ideal
1244 // will change the graph shape in a way which makes memory alive twice at the
1245 // same time (uses the Oracle model of aliasing), then some
1246 // LoadXNode::Identity will fold things back to the equivalence-class model
1247 // of aliasing.
1248 // This method may find an unencoded node instead of the corresponding encoded one.
1249 Node* MemNode::can_see_stored_value(Node* st, PhaseValues* phase) const {
1250 Node* ld_adr = in(MemNode::Address);
1251 intptr_t ld_off = 0;
1252 Node* ld_base = AddPNode::Ideal_base_and_offset(ld_adr, phase, ld_off);
1253 // Try to see through an InlineTypeNode
1254 // LoadN is special because the input is not compressed
1255 if (Opcode() != Op_LoadN) {
1256 Node* value = see_through_inline_type(phase, this, ld_base, ld_off);
1257 if (value != nullptr) {
1258 return value;
1259 }
1260 }
1261
1262 Node* ld_alloc = AllocateNode::Ideal_allocation(ld_base);
1263 const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr();
1264 Compile::AliasType* atp = (tp != nullptr) ? phase->C->alias_type(tp) : nullptr;
1265 // This is more general than load from boxing objects.
1266 if (skip_through_membars(atp, tp, phase->C->eliminate_boxing())) {
1267 uint alias_idx = atp->index();
1268 Node* result = nullptr;
1269 Node* current = st;
1270 // Skip through chains of MemBarNodes checking the MergeMems for
1271 // new states for the slice of this load. Stop once any other
1272 // kind of node is encountered. Loads from final memory can skip
1273 // through any kind of MemBar but normal loads shouldn't skip
1274 // through MemBarAcquire since the could allow them to move out of
1275 // a synchronized region. It is not safe to step over MemBarCPUOrder,
1276 // because alias info above them may be inaccurate (e.g., due to
1277 // mixed/mismatched unsafe accesses).
1278 bool is_final_mem = !atp->is_rewritable();
1279 while (current->is_Proj()) {
1280 int opc = current->in(0)->Opcode();
1281 if ((is_final_mem && (opc == Op_MemBarAcquire ||
1325 // Same base, same offset.
1326 // Possible improvement for arrays: check index value instead of absolute offset.
1327
1328 // At this point we have proven something like this setup:
1329 // B = << base >>
1330 // L = LoadQ(AddP(Check/CastPP(B), #Off))
1331 // S = StoreQ(AddP( B , #Off), V)
1332 // (Actually, we haven't yet proven the Q's are the same.)
1333 // In other words, we are loading from a casted version of
1334 // the same pointer-and-offset that we stored to.
1335 // Casted version may carry a dependency and it is respected.
1336 // Thus, we are able to replace L by V.
1337 }
1338 // Now prove that we have a LoadQ matched to a StoreQ, for some Q.
1339 if (store_Opcode() != st->Opcode()) {
1340 return nullptr;
1341 }
1342 // LoadVector/StoreVector needs additional check to ensure the types match.
1343 if (st->is_StoreVector()) {
1344 const TypeVect* in_vt = st->as_StoreVector()->vect_type();
1345 const TypeVect* out_vt = is_Load() ? as_LoadVector()->vect_type() : as_StoreVector()->vect_type();
1346 if (in_vt != out_vt) {
1347 return nullptr;
1348 }
1349 }
1350 return st->in(MemNode::ValueIn);
1351 }
1352
1353 // A load from a freshly-created object always returns zero.
1354 // (This can happen after LoadNode::Ideal resets the load's memory input
1355 // to find_captured_store, which returned InitializeNode::zero_memory.)
1356 if (st->is_Proj() && st->in(0)->is_Allocate() &&
1357 (st->in(0) == ld_alloc) &&
1358 (ld_off >= st->in(0)->as_Allocate()->minimum_header_size())) {
1359 // return a zero value for the load's basic type
1360 // (This is one of the few places where a generic PhaseTransform
1361 // can create new nodes. Think of it as lazily manifesting
1362 // virtually pre-existing constants.)
1363 Node* init_value = ld_alloc->in(AllocateNode::InitValue);
1364 if (init_value != nullptr) {
1365 const TypeAryPtr* ld_adr_type = phase->type(ld_adr)->isa_aryptr();
1366 if (ld_adr_type == nullptr) {
1367 return nullptr;
1368 }
1369
1370 // We know that this is not a flat array, the load should return the whole oop
1371 if (ld_adr_type->is_not_flat()) {
1372 return init_value;
1373 }
1374
1375 // If this is a flat array, try to see through init_value
1376 if (init_value->is_EncodeP()) {
1377 init_value = init_value->in(1);
1378 }
1379 if (!init_value->is_InlineType() || ld_adr_type->field_offset() == Type::Offset::bottom) {
1380 return nullptr;
1381 }
1382
1383 ciInlineKlass* vk = phase->type(init_value)->inline_klass();
1384 int field_offset_in_payload = ld_adr_type->field_offset().get();
1385 if (field_offset_in_payload == vk->null_marker_offset_in_payload()) {
1386 return init_value->as_InlineType()->get_null_marker();
1387 } else {
1388 return init_value->as_InlineType()->field_value_by_offset(field_offset_in_payload + vk->payload_offset(), true);
1389 }
1390 }
1391 assert(ld_alloc->in(AllocateNode::RawInitValue) == nullptr, "init value may not be null");
1392 if (value_basic_type() != T_VOID) {
1393 if (ReduceBulkZeroing || find_array_copy_clone(ld_alloc, in(MemNode::Memory)) == nullptr) {
1394 // If ReduceBulkZeroing is disabled, we need to check if the allocation does not belong to an
1395 // ArrayCopyNode clone. If it does, then we cannot assume zero since the initialization is done
1396 // by the ArrayCopyNode.
1397 return phase->zerocon(value_basic_type());
1398 }
1399 } else {
1400 // TODO: materialize all-zero vector constant
1401 assert(!isa_Load() || as_Load()->type()->isa_vect(), "");
1402 }
1403 }
1404
1405 // A load from an initialization barrier can match a captured store.
1406 if (st->is_Proj() && st->in(0)->is_Initialize()) {
1407 InitializeNode* init = st->in(0)->as_Initialize();
1408 AllocateNode* alloc = init->allocation();
1409 if ((alloc != nullptr) && (alloc == ld_alloc)) {
1410 // examine a captured store value
1411 st = init->find_captured_store(ld_off, memory_size(), phase);
1424 base = bs->step_over_gc_barrier(base);
1425 if (base != nullptr && base->is_Proj() &&
1426 base->as_Proj()->_con == TypeFunc::Parms &&
1427 base->in(0)->is_CallStaticJava() &&
1428 base->in(0)->as_CallStaticJava()->is_boxing_method()) {
1429 return base->in(0)->in(TypeFunc::Parms);
1430 }
1431 }
1432
1433 break;
1434 }
1435
1436 return nullptr;
1437 }
1438
1439 //----------------------is_instance_field_load_with_local_phi------------------
1440 bool LoadNode::is_instance_field_load_with_local_phi(Node* ctrl) {
1441 if( in(Memory)->is_Phi() && in(Memory)->in(0) == ctrl &&
1442 in(Address)->is_AddP() ) {
1443 const TypeOopPtr* t_oop = in(Address)->bottom_type()->isa_oopptr();
1444 // Only known instances and immutable fields
1445 if( t_oop != nullptr &&
1446 (t_oop->is_ptr_to_strict_final_field() ||
1447 t_oop->is_known_instance_field()) &&
1448 t_oop->offset() != Type::OffsetBot &&
1449 t_oop->offset() != Type::OffsetTop) {
1450 return true;
1451 }
1452 }
1453 return false;
1454 }
1455
1456 //------------------------------Identity---------------------------------------
1457 // Loads are identity if previous store is to same address
1458 Node* LoadNode::Identity(PhaseGVN* phase) {
1459 // If the previous store-maker is the right kind of Store, and the store is
1460 // to the same address, then we are equal to the value stored.
1461 Node* mem = in(Memory);
1462 Node* value = can_see_stored_value(mem, phase);
1463 if( value ) {
1464 // byte, short & char stores truncate naturally.
1465 // A load has to load the truncated value which requires
1466 // some sort of masking operation and that requires an
1467 // Ideal call instead of an Identity call.
1468 if (memory_size() < BytesPerInt) {
1469 // If the input to the store does not fit with the load's result type,
1470 // it must be truncated via an Ideal call.
1471 if (!phase->type(value)->higher_equal(phase->type(this)))
1472 return this;
1473 }
1474
1475 if (phase->type(value)->isa_ptr() && phase->type(this)->isa_narrowoop()) {
1476 return this;
1477 }
1478 // (This works even when value is a Con, but LoadNode::Value
1479 // usually runs first, producing the singleton type of the Con.)
1480 if (!has_pinned_control_dependency() || value->is_Con()) {
1481 return value;
1482 } else {
1483 return this;
1484 }
1485 }
1486
1487 if (has_pinned_control_dependency()) {
1488 return this;
1489 }
1490 // Search for an existing data phi which was generated before for the same
1491 // instance's field to avoid infinite generation of phis in a loop.
1492 Node *region = mem->in(0);
1493 if (is_instance_field_load_with_local_phi(region)) {
1494 const TypeOopPtr *addr_t = in(Address)->bottom_type()->isa_oopptr();
1495 int this_index = phase->C->get_alias_index(addr_t);
1496 int this_offset = addr_t->offset();
1497 int this_iid = addr_t->instance_id();
1498 if (!addr_t->is_known_instance() &&
1499 addr_t->is_ptr_to_strict_final_field()) {
1500 // Use _idx of address base (could be Phi node) for immutable fields in unknown instances
1501 intptr_t ignore = 0;
1502 Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore);
1503 if (base == nullptr) {
1504 return this;
1505 }
1506 this_iid = base->_idx;
1507 }
1508 const Type* this_type = bottom_type();
1509 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
1510 Node* phi = region->fast_out(i);
1511 if (phi->is_Phi() && phi != mem &&
1512 phi->as_Phi()->is_same_inst_field(this_type, (int)mem->_idx, this_iid, this_index, this_offset)) {
1513 return phi;
1514 }
1515 }
1516 }
1517
1518 return this;
1519 }
1520
2036 bool addr_mark = ((phase->type(address)->isa_oopptr() || phase->type(address)->isa_narrowoop()) &&
2037 phase->type(address)->is_ptr()->offset() == oopDesc::mark_offset_in_bytes());
2038
2039 // Skip up past a SafePoint control. Cannot do this for Stores because
2040 // pointer stores & cardmarks must stay on the same side of a SafePoint.
2041 if( ctrl != nullptr && ctrl->Opcode() == Op_SafePoint &&
2042 phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw &&
2043 !addr_mark &&
2044 (depends_only_on_test() || has_unknown_control_dependency())) {
2045 ctrl = ctrl->in(0);
2046 set_req(MemNode::Control,ctrl);
2047 progress = true;
2048 }
2049
2050 intptr_t ignore = 0;
2051 Node* base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
2052 if (base != nullptr
2053 && phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw) {
2054 // Check for useless control edge in some common special cases
2055 if (in(MemNode::Control) != nullptr
2056 // TODO 8350865 Can we re-enable this?
2057 && !(phase->type(address)->is_inlinetypeptr() && is_mismatched_access())
2058 && can_remove_control()
2059 && phase->type(base)->higher_equal(TypePtr::NOTNULL)
2060 && all_controls_dominate(base, phase->C->start())) {
2061 // A method-invariant, non-null address (constant or 'this' argument).
2062 set_req(MemNode::Control, nullptr);
2063 progress = true;
2064 }
2065 }
2066
2067 Node* mem = in(MemNode::Memory);
2068 const TypePtr *addr_t = phase->type(address)->isa_ptr();
2069
2070 if (can_reshape && (addr_t != nullptr)) {
2071 // try to optimize our memory input
2072 Node* opt_mem = MemNode::optimize_memory_chain(mem, addr_t, this, phase);
2073 if (opt_mem != mem) {
2074 set_req_X(MemNode::Memory, opt_mem, phase);
2075 if (phase->type( opt_mem ) == Type::TOP) return nullptr;
2076 return this;
2077 }
2134 // fold up, do so.
2135 Node* prev_mem = find_previous_store(phase);
2136 if (prev_mem != nullptr) {
2137 Node* value = can_see_arraycopy_value(prev_mem, phase);
2138 if (value != nullptr) {
2139 return value;
2140 }
2141 }
2142 // Steps (a), (b): Walk past independent stores to find an exact match.
2143 if (prev_mem != nullptr && prev_mem != in(MemNode::Memory)) {
2144 // (c) See if we can fold up on the spot, but don't fold up here.
2145 // Fold-up might require truncation (for LoadB/LoadS/LoadUS) or
2146 // just return a prior value, which is done by Identity calls.
2147 if (can_see_stored_value(prev_mem, phase)) {
2148 // Make ready for step (d):
2149 set_req_X(MemNode::Memory, prev_mem, phase);
2150 return this;
2151 }
2152 }
2153
2154 if (progress) {
2155 return this;
2156 }
2157
2158 if (!can_reshape) {
2159 phase->record_for_igvn(this);
2160 }
2161 return nullptr;
2162 }
2163
2164 // Helper to recognize certain Klass fields which are invariant across
2165 // some group of array types (e.g., int[] or all T[] where T < Object).
2166 const Type*
2167 LoadNode::load_array_final_field(const TypeKlassPtr *tkls,
2168 ciKlass* klass) const {
2169 assert(!UseCompactObjectHeaders || tkls->offset() != in_bytes(Klass::prototype_header_offset()),
2170 "must not happen");
2171
2172 if (tkls->isa_instklassptr() && tkls->offset() == in_bytes(InstanceKlass::access_flags_offset())) {
2173 // The field is InstanceKlass::_access_flags. Return its (constant) value.
2174 assert(Opcode() == Op_LoadUS, "must load an unsigned short from _access_flags");
2175 ciInstanceKlass* iklass = tkls->is_instklassptr()->instance_klass();
2176 return TypeInt::make(iklass->access_flags());
2177 }
2178 if (tkls->offset() == in_bytes(Klass::misc_flags_offset())) {
2179 // The field is Klass::_misc_flags. Return its (constant) value.
2180 assert(Opcode() == Op_LoadUB, "must load an unsigned byte from _misc_flags");
2181 return TypeInt::make(klass->misc_flags());
2189 // No match.
2190 return nullptr;
2191 }
2192
2193 //------------------------------Value-----------------------------------------
2194 const Type* LoadNode::Value(PhaseGVN* phase) const {
2195 // Either input is TOP ==> the result is TOP
2196 Node* mem = in(MemNode::Memory);
2197 const Type *t1 = phase->type(mem);
2198 if (t1 == Type::TOP) return Type::TOP;
2199 Node* adr = in(MemNode::Address);
2200 const TypePtr* tp = phase->type(adr)->isa_ptr();
2201 if (tp == nullptr || tp->empty()) return Type::TOP;
2202 int off = tp->offset();
2203 assert(off != Type::OffsetTop, "case covered by TypePtr::empty");
2204 Compile* C = phase->C;
2205
2206 // If load can see a previous constant store, use that.
2207 Node* value = can_see_stored_value(mem, phase);
2208 if (value != nullptr && value->is_Con()) {
2209 if (phase->type(value)->isa_ptr() && _type->isa_narrowoop()) {
2210 return phase->type(value)->make_narrowoop();
2211 } else {
2212 assert(value->bottom_type()->higher_equal(_type), "sanity");
2213 return phase->type(value);
2214 }
2215 }
2216 // Try to guess loaded type from pointer type
2217 if (tp->isa_aryptr()) {
2218 const TypeAryPtr* ary = tp->is_aryptr();
2219 const Type* t = ary->elem();
2220
2221 // Determine whether the reference is beyond the header or not, by comparing
2222 // the offset against the offset of the start of the array's data.
2223 // Different array types begin at slightly different offsets (12 vs. 16).
2224 // We choose T_BYTE as an example base type that is least restrictive
2225 // as to alignment, which will therefore produce the smallest
2226 // possible base offset.
2227 const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE);
2228 const bool off_beyond_header = (off >= min_base_off);
2229
2230 // Try to constant-fold a stable array element.
2231 if (FoldStableValues && !is_mismatched_access() && ary->is_stable()) {
2232 // Make sure the reference is not into the header and the offset is constant
2233 ciObject* aobj = ary->const_oop();
2234 if (aobj != nullptr && off_beyond_header && adr->is_AddP() && off != Type::OffsetBot) {
2235 int stable_dimension = (ary->stable_dimension() > 0 ? ary->stable_dimension() - 1 : 0);
2236 const Type* con_type = Type::make_constant_from_array_element(aobj->as_array(), off, ary->field_offset().get(),
2237 stable_dimension,
2238 value_basic_type(), is_unsigned());
2239 if (con_type != nullptr) {
2240 return con_type;
2241 }
2242 }
2243 }
2244
2245 // Don't do this for integer types. There is only potential profit if
2246 // the element type t is lower than _type; that is, for int types, if _type is
2247 // more restrictive than t. This only happens here if one is short and the other
2248 // char (both 16 bits), and in those cases we've made an intentional decision
2249 // to use one kind of load over the other. See AndINode::Ideal and 4965907.
2250 // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
2251 //
2252 // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
2253 // where the _gvn.type of the AddP is wider than 8. This occurs when an earlier
2254 // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
2255 // subsumed by p1. If p1 is on the worklist but has not yet been re-transformed,
2256 // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
2257 // In fact, that could have been the original type of p1, and p1 could have
2258 // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
2259 // expression (LShiftL quux 3) independently optimized to the constant 8.
2260 if ((t->isa_int() == nullptr) && (t->isa_long() == nullptr)
2261 && (_type->isa_vect() == nullptr)
2262 && !ary->is_flat()
2263 && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) {
2264 // t might actually be lower than _type, if _type is a unique
2265 // concrete subclass of abstract class t.
2266 if (off_beyond_header || off == Type::OffsetBot) { // is the offset beyond the header?
2267 const Type* jt = t->join_speculative(_type);
2268 // In any case, do not allow the join, per se, to empty out the type.
2269 if (jt->empty() && !t->empty()) {
2270 // This can happen if a interface-typed array narrows to a class type.
2271 jt = _type;
2272 }
2273 #ifdef ASSERT
2274 if (phase->C->eliminate_boxing() && adr->is_AddP()) {
2275 // The pointers in the autobox arrays are always non-null
2276 Node* base = adr->in(AddPNode::Base);
2277 if ((base != nullptr) && base->is_DecodeN()) {
2278 // Get LoadN node which loads IntegerCache.cache field
2279 base = base->in(1);
2280 }
2281 if ((base != nullptr) && base->is_Con()) {
2282 const TypeAryPtr* base_type = base->bottom_type()->isa_aryptr();
2283 if ((base_type != nullptr) && base_type->is_autobox_cache()) {
2284 // It could be narrow oop
2285 assert(jt->make_ptr()->ptr() == TypePtr::NotNull,"sanity");
2286 }
2287 }
2288 }
2289 #endif
2290 return jt;
2291 }
2292 }
2293 } else if (tp->base() == Type::InstPtr) {
2294 assert( off != Type::OffsetBot ||
2295 // arrays can be cast to Objects
2296 !tp->isa_instptr() ||
2297 tp->is_instptr()->instance_klass()->is_java_lang_Object() ||
2298 // Default value load
2299 tp->is_instptr()->instance_klass() == ciEnv::current()->Class_klass() ||
2300 // unsafe field access may not have a constant offset
2301 C->has_unsafe_access(),
2302 "Field accesses must be precise" );
2303 // For oop loads, we expect the _type to be precise.
2304
2305 const TypeInstPtr* tinst = tp->is_instptr();
2306 BasicType bt = value_basic_type();
2307
2308 // Fold loads of the field map
2309 if (tinst != nullptr) {
2310 ciInstanceKlass* ik = tinst->instance_klass();
2311 int offset = tinst->offset();
2312 if (ik == phase->C->env()->Class_klass()) {
2313 ciType* t = tinst->java_mirror_type();
2314 if (t != nullptr && t->is_inlinetype() && offset == t->as_inline_klass()->field_map_offset()) {
2315 ciConstant map = t->as_inline_klass()->get_field_map();
2316 bool is_narrow_oop = (bt == T_NARROWOOP);
2317 return Type::make_from_constant(map, true, 1, is_narrow_oop);
2318 }
2319 }
2320 }
2321
2322 // Optimize loads from constant fields.
2323 ciObject* const_oop = tinst->const_oop();
2324 if (!is_mismatched_access() && off != Type::OffsetBot && const_oop != nullptr && const_oop->is_instance()) {
2325 const Type* con_type = Type::make_constant_from_field(const_oop->as_instance(), off, is_unsigned(), bt);
2326 if (con_type != nullptr) {
2327 return con_type;
2328 }
2329 }
2330 } else if (tp->base() == Type::KlassPtr || tp->base() == Type::InstKlassPtr || tp->base() == Type::AryKlassPtr) {
2331 assert(off != Type::OffsetBot ||
2332 !tp->isa_instklassptr() ||
2333 // arrays can be cast to Objects
2334 tp->isa_instklassptr()->instance_klass()->is_java_lang_Object() ||
2335 // also allow array-loading from the primary supertype
2336 // array during subtype checks
2337 Opcode() == Op_LoadKlass,
2338 "Field accesses must be precise");
2339 // For klass/static loads, we expect the _type to be precise
2340 } else if (tp->base() == Type::RawPtr && adr->is_Load() && off == 0) {
2341 /* With mirrors being an indirect in the Klass*
2342 * the VM is now using two loads. LoadKlass(LoadP(LoadP(Klass, mirror_offset), zero_offset))
2343 * The LoadP from the Klass has a RawPtr type (see LibraryCallKit::load_mirror_from_klass).
2344 *
2345 * So check the type and klass of the node before the LoadP.
2352 assert(adr->Opcode() == Op_LoadP, "must load an oop from _java_mirror");
2353 assert(Opcode() == Op_LoadP, "must load an oop from _java_mirror");
2354 return TypeInstPtr::make(klass->java_mirror());
2355 }
2356 }
2357 }
2358
2359 const TypeKlassPtr *tkls = tp->isa_klassptr();
2360 if (tkls != nullptr) {
2361 if (tkls->is_loaded() && tkls->klass_is_exact()) {
2362 ciKlass* klass = tkls->exact_klass();
2363 // We are loading a field from a Klass metaobject whose identity
2364 // is known at compile time (the type is "exact" or "precise").
2365 // Check for fields we know are maintained as constants by the VM.
2366 if (tkls->offset() == in_bytes(Klass::super_check_offset_offset())) {
2367 // The field is Klass::_super_check_offset. Return its (constant) value.
2368 // (Folds up type checking code.)
2369 assert(Opcode() == Op_LoadI, "must load an int from _super_check_offset");
2370 return TypeInt::make(klass->super_check_offset());
2371 }
2372 if (klass->is_inlinetype() && tkls->offset() == in_bytes(InstanceKlass::acmp_maps_offset_offset())) {
2373 return TypeInt::make(klass->as_inline_klass()->field_map_offset());
2374 }
2375 if (klass->is_obj_array_klass() && tkls->offset() == in_bytes(ObjArrayKlass::next_refined_array_klass_offset())) {
2376 // Fold loads from LibraryCallKit::load_default_refined_array_klass
2377 return tkls->is_aryklassptr()->cast_to_refined_array_klass_ptr();
2378 }
2379 if (klass->is_array_klass() && tkls->offset() == in_bytes(ObjArrayKlass::properties_offset())) {
2380 assert(klass->is_type_array_klass() || tkls->is_aryklassptr()->is_refined_type(), "Must be a refined array klass pointer");
2381 return TypeInt::make(klass->as_array_klass()->properties());
2382 }
2383 if (klass->is_flat_array_klass() && tkls->offset() == in_bytes(FlatArrayKlass::layout_kind_offset())) {
2384 assert(Opcode() == Op_LoadI, "must load an int from _layout_kind");
2385 return TypeInt::make(static_cast<jint>(klass->as_flat_array_klass()->layout_kind()));
2386 }
2387 if (UseCompactObjectHeaders && tkls->offset() == in_bytes(Klass::prototype_header_offset())) {
2388 // The field is Klass::_prototype_header. Return its (constant) value.
2389 assert(this->Opcode() == Op_LoadX, "must load a proper type from _prototype_header");
2390 return TypeX::make(klass->prototype_header());
2391 }
2392 // Compute index into primary_supers array
2393 juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*);
2394 // Check for overflowing; use unsigned compare to handle the negative case.
2395 if( depth < ciKlass::primary_super_limit() ) {
2396 // The field is an element of Klass::_primary_supers. Return its (constant) value.
2397 // (Folds up type checking code.)
2398 assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers");
2399 ciKlass *ss = klass->super_of_depth(depth);
2400 return ss ? TypeKlassPtr::make(ss, Type::trust_interfaces) : TypePtr::NULL_PTR;
2401 }
2402 const Type* aift = load_array_final_field(tkls, klass);
2403 if (aift != nullptr) return aift;
2404 }
2405
2406 // We can still check if we are loading from the primary_supers array at a
2407 // shallow enough depth. Even though the klass is not exact, entries less
2408 // than or equal to its super depth are correct.
2409 if (tkls->is_loaded()) {
2410 ciKlass* klass = nullptr;
2444 jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false);
2445 // The key property of this type is that it folds up tests
2446 // for array-ness, since it proves that the layout_helper is positive.
2447 // Thus, a generic value like the basic object layout helper works fine.
2448 return TypeInt::make(min_size, max_jint, Type::WidenMin);
2449 }
2450 }
2451
2452 // If we are loading from a freshly-allocated object/array, produce a zero.
2453 // Things to check:
2454 // 1. Load is beyond the header: headers are not guaranteed to be zero
2455 // 2. Load is not vectorized: vectors have no zero constant
2456 // 3. Load has no matching store, i.e. the input is the initial memory state
2457 const TypeOopPtr* tinst = tp->isa_oopptr();
2458 bool is_not_header = (tinst != nullptr) && tinst->is_known_instance_field();
2459 bool is_not_vect = (_type->isa_vect() == nullptr);
2460 if (is_not_header && is_not_vect) {
2461 Node* mem = in(MemNode::Memory);
2462 if (mem->is_Parm() && mem->in(0)->is_Start()) {
2463 assert(mem->as_Parm()->_con == TypeFunc::Memory, "must be memory Parm");
2464 // TODO 8350865 Scalar replacement does not work well for flat arrays.
2465 // Escape Analysis assumes that arrays are always zeroed during allocation which is not true for null-free arrays
2466 // ConnectionGraph::split_unique_types will re-wire the memory of loads from such arrays around the allocation
2467 // TestArrays::test6 and test152 and TestBasicFunctionality::test20 are affected by this.
2468 if (tp->isa_aryptr() && tp->is_aryptr()->is_flat() && tp->is_aryptr()->is_null_free()) {
2469 intptr_t offset = 0;
2470 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
2471 AllocateNode* alloc = AllocateNode::Ideal_allocation(base);
2472 if (alloc != nullptr && alloc->is_AllocateArray() && alloc->in(AllocateNode::InitValue) != nullptr) {
2473 return _type;
2474 }
2475 }
2476 return Type::get_zero_type(_type->basic_type());
2477 }
2478 }
2479 if (!UseCompactObjectHeaders) {
2480 Node* alloc = is_new_object_mark_load();
2481 if (alloc != nullptr) {
2482 if (Arguments::is_valhalla_enabled()) {
2483 // The mark word may contain property bits (inline, flat, null-free)
2484 Node* klass_node = alloc->in(AllocateNode::KlassNode);
2485 const TypeKlassPtr* tkls = phase->type(klass_node)->isa_klassptr();
2486 if (tkls != nullptr && tkls->is_loaded() && tkls->klass_is_exact()) {
2487 return TypeX::make(tkls->exact_klass()->prototype_header());
2488 }
2489 } else {
2490 return TypeX::make(markWord::prototype().value());
2491 }
2492 }
2493 }
2494
2495 return _type;
2496 }
2497
2498 //------------------------------match_edge-------------------------------------
2499 // Do we Match on this edge index or not? Match only the address.
2500 uint LoadNode::match_edge(uint idx) const {
2501 return idx == MemNode::Address;
2502 }
2503
2504 //--------------------------LoadBNode::Ideal--------------------------------------
2505 //
2506 // If the previous store is to the same address as this load,
2507 // and the value stored was larger than a byte, replace this load
2508 // with the value stored truncated to a byte. If no truncation is
2509 // needed, the replacement is done in LoadNode::Identity().
2510 //
2511 Node* LoadBNode::Ideal(PhaseGVN* phase, bool can_reshape) {
2620 }
2621 }
2622 // Identity call will handle the case where truncation is not needed.
2623 return LoadNode::Ideal(phase, can_reshape);
2624 }
2625
2626 const Type* LoadSNode::Value(PhaseGVN* phase) const {
2627 Node* mem = in(MemNode::Memory);
2628 Node* value = can_see_stored_value(mem,phase);
2629 if (value != nullptr && value->is_Con() &&
2630 !value->bottom_type()->higher_equal(_type)) {
2631 // If the input to the store does not fit with the load's result type,
2632 // it must be truncated. We can't delay until Ideal call since
2633 // a singleton Value is needed for split_thru_phi optimization.
2634 int con = value->get_int();
2635 return TypeInt::make((con << 16) >> 16);
2636 }
2637 return LoadNode::Value(phase);
2638 }
2639
2640 Node* LoadNNode::Ideal(PhaseGVN* phase, bool can_reshape) {
2641 // Loading from an InlineType, find the input and make an EncodeP
2642 Node* addr = in(Address);
2643 intptr_t offset;
2644 Node* base = AddPNode::Ideal_base_and_offset(addr, phase, offset);
2645 Node* value = see_through_inline_type(phase, this, base, offset);
2646 if (value != nullptr) {
2647 return new EncodePNode(value, type());
2648 }
2649
2650 // Can see the corresponding value, may need to add an EncodeP
2651 value = can_see_stored_value(in(Memory), phase);
2652 if (value != nullptr && phase->type(value)->isa_ptr() && type()->isa_narrowoop()) {
2653 return new EncodePNode(value, type());
2654 }
2655
2656 // Identity call will handle the case where EncodeP is unnecessary
2657 return LoadNode::Ideal(phase, can_reshape);
2658 }
2659
2660 //=============================================================================
2661 //----------------------------LoadKlassNode::make------------------------------
2662 // Polymorphic factory method:
2663 Node* LoadKlassNode::make(PhaseGVN& gvn, Node* mem, Node* adr, const TypePtr* at, const TypeKlassPtr* tk) {
2664 // sanity check the alias category against the created node type
2665 const TypePtr* adr_type = adr->bottom_type()->isa_ptr();
2666 assert(adr_type != nullptr, "expecting TypeKlassPtr");
2667 #ifdef _LP64
2668 if (adr_type->is_ptr_to_narrowklass()) {
2669 assert(UseCompressedClassPointers, "no compressed klasses");
2670 Node* load_klass = gvn.transform(new LoadNKlassNode(mem, adr, at, tk->make_narrowklass(), MemNode::unordered));
2671 return new DecodeNKlassNode(load_klass, load_klass->bottom_type()->make_ptr());
2672 }
2673 #endif
2674 assert(!adr_type->is_ptr_to_narrowklass() && !adr_type->is_ptr_to_narrowoop(), "should have got back a narrow oop");
2675 return new LoadKlassNode(mem, adr, at, tk, MemNode::unordered);
2676 }
2677
2678 //------------------------------Value------------------------------------------
2679 const Type* LoadKlassNode::Value(PhaseGVN* phase) const {
2713 }
2714 return TypeKlassPtr::make(ciArrayKlass::make(t), Type::trust_interfaces);
2715 }
2716 if (!t->is_klass()) {
2717 // a primitive Class (e.g., int.class) has null for a klass field
2718 return TypePtr::NULL_PTR;
2719 }
2720 // Fold up the load of the hidden field
2721 return TypeKlassPtr::make(t->as_klass(), Type::trust_interfaces);
2722 }
2723 // non-constant mirror, so we can't tell what's going on
2724 }
2725 if (!tinst->is_loaded())
2726 return _type; // Bail out if not loaded
2727 if (offset == oopDesc::klass_offset_in_bytes()) {
2728 return tinst->as_klass_type(true);
2729 }
2730 }
2731
2732 // Check for loading klass from an array
2733 const TypeAryPtr* tary = tp->isa_aryptr();
2734 if (tary != nullptr &&
2735 tary->offset() == oopDesc::klass_offset_in_bytes()) {
2736 return tary->as_klass_type(true)->is_aryklassptr();
2737 }
2738
2739 // Check for loading klass from an array klass
2740 const TypeKlassPtr *tkls = tp->isa_klassptr();
2741 if (tkls != nullptr && !StressReflectiveCode) {
2742 if (!tkls->is_loaded())
2743 return _type; // Bail out if not loaded
2744 if (tkls->isa_aryklassptr() && tkls->is_aryklassptr()->elem()->isa_klassptr() &&
2745 tkls->offset() == in_bytes(ObjArrayKlass::element_klass_offset())) {
2746 // // Always returning precise element type is incorrect,
2747 // // e.g., element type could be object and array may contain strings
2748 // return TypeKlassPtr::make(TypePtr::Constant, elem, 0);
2749
2750 // The array's TypeKlassPtr was declared 'precise' or 'not precise'
2751 // according to the element type's subclassing.
2752 return tkls->is_aryklassptr()->elem()->isa_klassptr()->cast_to_exactness(tkls->klass_is_exact());
2753 }
2754 if (tkls->isa_aryklassptr() != nullptr && tkls->klass_is_exact() &&
2755 !tkls->exact_klass()->is_type_array_klass() &&
2756 tkls->offset() == in_bytes(Klass::super_offset())) {
2757 // We are loading the super klass of a refined array klass, return the non-refined klass pointer
2758 assert(tkls->is_aryklassptr()->is_refined_type(), "Must be a refined array klass pointer");
2759 return tkls->is_aryklassptr()->with_offset(0)->cast_to_non_refined();
2760 }
2761 if (tkls->isa_instklassptr() != nullptr && tkls->klass_is_exact() &&
2762 tkls->offset() == in_bytes(Klass::super_offset())) {
2763 ciKlass* sup = tkls->is_instklassptr()->instance_klass()->super();
2764 // The field is Klass::_super. Return its (constant) value.
2765 // (Folds up the 2nd indirection in aClassConstant.getSuperClass().)
2766 return sup ? TypeKlassPtr::make(sup, Type::trust_interfaces) : TypePtr::NULL_PTR;
2767 }
2768 }
2769
2770 if (tkls != nullptr && !UseSecondarySupersCache
2771 && tkls->offset() == in_bytes(Klass::secondary_super_cache_offset())) {
2772 // Treat Klass::_secondary_super_cache as a constant when the cache is disabled.
2773 return TypePtr::NULL_PTR;
2774 }
2775
2776 // Bailout case
2777 return LoadNode::Value(phase);
2778 }
2779
2780 //------------------------------Identity---------------------------------------
2803 base = bs->step_over_gc_barrier(base);
2804 }
2805
2806 // We can fetch the klass directly through an AllocateNode.
2807 // This works even if the klass is not constant (clone or newArray).
2808 if (offset == oopDesc::klass_offset_in_bytes()) {
2809 Node* allocated_klass = AllocateNode::Ideal_klass(base, phase);
2810 if (allocated_klass != nullptr) {
2811 return allocated_klass;
2812 }
2813 }
2814
2815 // Simplify k.java_mirror.as_klass to plain k, where k is a Klass*.
2816 // See inline_native_Class_query for occurrences of these patterns.
2817 // Java Example: x.getClass().isAssignableFrom(y)
2818 //
2819 // This improves reflective code, often making the Class
2820 // mirror go completely dead. (Current exception: Class
2821 // mirrors may appear in debug info, but we could clean them out by
2822 // introducing a new debug info operator for Klass.java_mirror).
2823 //
2824 // This optimization does not apply to arrays because if k is not a
2825 // constant, it was obtained via load_klass which returns the VM type
2826 // and '.java_mirror.as_klass' should return the Java type instead.
2827
2828 if (toop->isa_instptr() && toop->is_instptr()->instance_klass() == phase->C->env()->Class_klass()
2829 && offset == java_lang_Class::klass_offset()) {
2830 if (base->is_Load()) {
2831 Node* base2 = base->in(MemNode::Address);
2832 if (base2->is_Load()) { /* direct load of a load which is the OopHandle */
2833 Node* adr2 = base2->in(MemNode::Address);
2834 const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr();
2835 if (tkls != nullptr && !tkls->empty()
2836 && ((tkls->isa_instklassptr() && !tkls->is_instklassptr()->might_be_an_array()))
2837 && adr2->is_AddP()) {
2838 int mirror_field = in_bytes(Klass::java_mirror_offset());
2839 if (tkls->offset() == mirror_field) {
2840 return adr2->in(AddPNode::Base);
2841 }
2842 }
2843 }
2844 }
2845 }
2846
2847 return this;
2848 }
2849
2850 LoadNode* LoadNode::clone_pinned() const {
2851 LoadNode* ld = clone()->as_Load();
2852 ld->_control_dependency = UnknownControl;
2853 return ld;
2854 }
2855
2856
2857 //------------------------------Value------------------------------------------
2962 //---------------------------StoreNode::make-----------------------------------
2963 // Polymorphic factory method:
2964 StoreNode* StoreNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt, MemOrd mo, bool require_atomic_access) {
2965 assert((mo == unordered || mo == release), "unexpected");
2966 Compile* C = gvn.C;
2967 assert(C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
2968 ctl != nullptr, "raw memory operations should have control edge");
2969
2970 switch (bt) {
2971 case T_BOOLEAN: val = gvn.transform(new AndINode(val, gvn.intcon(0x1))); // Fall through to T_BYTE case
2972 case T_BYTE: return new StoreBNode(ctl, mem, adr, adr_type, val, mo);
2973 case T_INT: return new StoreINode(ctl, mem, adr, adr_type, val, mo);
2974 case T_CHAR:
2975 case T_SHORT: return new StoreCNode(ctl, mem, adr, adr_type, val, mo);
2976 case T_LONG: return new StoreLNode(ctl, mem, adr, adr_type, val, mo, require_atomic_access);
2977 case T_FLOAT: return new StoreFNode(ctl, mem, adr, adr_type, val, mo);
2978 case T_DOUBLE: return new StoreDNode(ctl, mem, adr, adr_type, val, mo, require_atomic_access);
2979 case T_METADATA:
2980 case T_ADDRESS:
2981 case T_OBJECT:
2982 case T_ARRAY:
2983 #ifdef _LP64
2984 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
2985 val = gvn.transform(new EncodePNode(val, val->bottom_type()->make_narrowoop()));
2986 return new StoreNNode(ctl, mem, adr, adr_type, val, mo);
2987 } else if (adr->bottom_type()->is_ptr_to_narrowklass() ||
2988 (UseCompressedClassPointers && val->bottom_type()->isa_klassptr() &&
2989 adr->bottom_type()->isa_rawptr())) {
2990 val = gvn.transform(new EncodePKlassNode(val, val->bottom_type()->make_narrowklass()));
2991 return new StoreNKlassNode(ctl, mem, adr, adr_type, val, mo);
2992 }
2993 #endif
2994 {
2995 return new StorePNode(ctl, mem, adr, adr_type, val, mo);
2996 }
2997 default:
2998 assert(false, "unexpected basic type %s", type2name(bt));
2999 return (StoreNode*)nullptr;
3000 }
3001 }
3002
3003 //--------------------------bottom_type----------------------------------------
3004 const Type *StoreNode::bottom_type() const {
3005 return Type::MEMORY;
3006 }
3007
3008 //------------------------------hash-------------------------------------------
3009 uint StoreNode::hash() const {
3010 // unroll addition of interesting fields
3011 //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn);
3012
3013 // Since they are not commoned, do not hash them:
3014 return NO_HASH;
3015 }
3016
3017 // Link together multiple stores (B/S/C/I) into a longer one.
3018 //
3640 }
3641 ss.print_cr("[TraceMergeStores]: with");
3642 merged_input_value->dump("\n", false, &ss);
3643 merged_store->dump("\n", false, &ss);
3644 tty->print("%s", ss.as_string());
3645 }
3646 #endif
3647
3648 //------------------------------Ideal------------------------------------------
3649 // Change back-to-back Store(, p, x) -> Store(m, p, y) to Store(m, p, x).
3650 // When a store immediately follows a relevant allocation/initialization,
3651 // try to capture it into the initialization, or hoist it above.
3652 Node *StoreNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3653 Node* p = MemNode::Ideal_common(phase, can_reshape);
3654 if (p) return (p == NodeSentinel) ? nullptr : p;
3655
3656 Node* mem = in(MemNode::Memory);
3657 Node* address = in(MemNode::Address);
3658 Node* value = in(MemNode::ValueIn);
3659 // Back-to-back stores to same address? Fold em up. Generally
3660 // unsafe if I have intervening uses...
3661 if (phase->C->get_adr_type(phase->C->get_alias_index(adr_type())) != TypeAryPtr::INLINES) {
3662 Node* st = mem;
3663 // If Store 'st' has more than one use, we cannot fold 'st' away.
3664 // For example, 'st' might be the final state at a conditional
3665 // return. Or, 'st' might be used by some node which is live at
3666 // the same time 'st' is live, which might be unschedulable. So,
3667 // require exactly ONE user until such time as we clone 'mem' for
3668 // each of 'mem's uses (thus making the exactly-1-user-rule hold
3669 // true).
3670 while (st->is_Store() && st->outcnt() == 1) {
3671 // Looking at a dead closed cycle of memory?
3672 assert(st != st->in(MemNode::Memory), "dead loop in StoreNode::Ideal");
3673 assert(Opcode() == st->Opcode() ||
3674 st->Opcode() == Op_StoreVector ||
3675 Opcode() == Op_StoreVector ||
3676 st->Opcode() == Op_StoreVectorScatter ||
3677 Opcode() == Op_StoreVectorScatter ||
3678 phase->C->get_alias_index(adr_type()) == Compile::AliasIdxRaw ||
3679 (Opcode() == Op_StoreL && st->Opcode() == Op_StoreI) || // expanded ClearArrayNode
3680 (Opcode() == Op_StoreI && st->Opcode() == Op_StoreL) || // initialization by arraycopy
3681 (Opcode() == Op_StoreL && st->Opcode() == Op_StoreN) ||
3682 (st->adr_type()->isa_aryptr() && st->adr_type()->is_aryptr()->is_flat()) || // TODO 8343835
3683 (is_mismatched_access() || st->as_Store()->is_mismatched_access()),
3684 "no mismatched stores, except on raw memory: %s %s", NodeClassNames[Opcode()], NodeClassNames[st->Opcode()]);
3685
3686 if (st->in(MemNode::Address)->eqv_uncast(address) &&
3687 st->as_Store()->memory_size() <= this->memory_size()) {
3688 Node* use = st->raw_out(0);
3689 if (phase->is_IterGVN()) {
3690 phase->is_IterGVN()->rehash_node_delayed(use);
3691 }
3692 // It's OK to do this in the parser, since DU info is always accurate,
3693 // and the parser always refers to nodes via SafePointNode maps.
3694 use->set_req_X(MemNode::Memory, st->in(MemNode::Memory), phase);
3695 return this;
3696 }
3697 st = st->in(MemNode::Memory);
3698 }
3699 }
3700
3701
3702 // Capture an unaliased, unconditional, simple store into an initializer.
3800 const StoreVectorNode* store_vector = as_StoreVector();
3801 const StoreVectorNode* mem_vector = mem->as_StoreVector();
3802 const Node* store_indices = store_vector->indices();
3803 const Node* mem_indices = mem_vector->indices();
3804 const Node* store_mask = store_vector->mask();
3805 const Node* mem_mask = mem_vector->mask();
3806 // Ensure types, indices, and masks match
3807 if (store_vector->vect_type() == mem_vector->vect_type() &&
3808 ((store_indices == nullptr) == (mem_indices == nullptr) &&
3809 (store_indices == nullptr || store_indices->eqv_uncast(mem_indices))) &&
3810 ((store_mask == nullptr) == (mem_mask == nullptr) &&
3811 (store_mask == nullptr || store_mask->eqv_uncast(mem_mask)))) {
3812 result = mem;
3813 }
3814 }
3815 }
3816
3817 // Store of zero anywhere into a freshly-allocated object?
3818 // Then the store is useless.
3819 // (It must already have been captured by the InitializeNode.)
3820 if (result == this && ReduceFieldZeroing) {
3821 // a newly allocated object is already all-zeroes everywhere
3822 if (mem->is_Proj() && mem->in(0)->is_Allocate() &&
3823 (phase->type(val)->is_zero_type() || mem->in(0)->in(AllocateNode::InitValue) == val)) {
3824 result = mem;
3825 }
3826
3827 if (result == this && phase->type(val)->is_zero_type()) {
3828 // the store may also apply to zero-bits in an earlier object
3829 Node* prev_mem = find_previous_store(phase);
3830 // Steps (a), (b): Walk past independent stores to find an exact match.
3831 if (prev_mem != nullptr) {
3832 Node* prev_val = can_see_stored_value(prev_mem, phase);
3833 if (prev_val != nullptr && prev_val == val) {
3834 // prev_val and val might differ by a cast; it would be good
3835 // to keep the more informative of the two.
3836 result = mem;
3837 }
3838 }
3839 }
3840 }
3841
3842 PhaseIterGVN* igvn = phase->is_IterGVN();
3843 if (result != this && igvn != nullptr) {
3844 MemBarNode* trailing = trailing_membar();
3845 if (trailing != nullptr) {
3846 #ifdef ASSERT
3847 const TypeOopPtr* t_oop = phase->type(in(Address))->isa_oopptr();
4316 // Clearing a short array is faster with stores
4317 Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape) {
4318 // Already know this is a large node, do not try to ideal it
4319 if (_is_large) return nullptr;
4320
4321 const int unit = BytesPerLong;
4322 const TypeX* t = phase->type(in(2))->isa_intptr_t();
4323 if (!t) return nullptr;
4324 if (!t->is_con()) return nullptr;
4325 intptr_t raw_count = t->get_con();
4326 intptr_t size = raw_count;
4327 if (!Matcher::init_array_count_is_in_bytes) size *= unit;
4328 // Clearing nothing uses the Identity call.
4329 // Negative clears are possible on dead ClearArrays
4330 // (see jck test stmt114.stmt11402.val).
4331 if (size <= 0 || size % unit != 0) return nullptr;
4332 intptr_t count = size / unit;
4333 // Length too long; communicate this to matchers and assemblers.
4334 // Assemblers are responsible to produce fast hardware clears for it.
4335 if (size > InitArrayShortSize) {
4336 return new ClearArrayNode(in(0), in(1), in(2), in(3), in(4), true);
4337 } else if (size > 2 && Matcher::match_rule_supported_vector(Op_ClearArray, 4, T_LONG)) {
4338 return nullptr;
4339 }
4340 if (!IdealizeClearArrayNode) return nullptr;
4341 Node *mem = in(1);
4342 if( phase->type(mem)==Type::TOP ) return nullptr;
4343 Node *adr = in(3);
4344 const Type* at = phase->type(adr);
4345 if( at==Type::TOP ) return nullptr;
4346 const TypePtr* atp = at->isa_ptr();
4347 // adjust atp to be the correct array element address type
4348 if (atp == nullptr) atp = TypePtr::BOTTOM;
4349 else atp = atp->add_offset(Type::OffsetBot);
4350 // Get base for derived pointer purposes
4351 if( adr->Opcode() != Op_AddP ) Unimplemented();
4352 Node *base = adr->in(1);
4353
4354 Node *val = in(4);
4355 Node *off = phase->MakeConX(BytesPerLong);
4356 mem = new StoreLNode(in(0), mem, adr, atp, val, MemNode::unordered, false);
4357 count--;
4358 while( count-- ) {
4359 mem = phase->transform(mem);
4360 adr = phase->transform(new AddPNode(base,adr,off));
4361 mem = new StoreLNode(in(0), mem, adr, atp, val, MemNode::unordered, false);
4362 }
4363 return mem;
4364 }
4365
4366 //----------------------------step_through----------------------------------
4367 // Return allocation input memory edge if it is different instance
4368 // or itself if it is the one we are looking for.
4369 bool ClearArrayNode::step_through(Node** np, uint instance_id, PhaseValues* phase) {
4370 Node* n = *np;
4371 assert(n->is_ClearArray(), "sanity");
4372 intptr_t offset;
4373 AllocateNode* alloc = AllocateNode::Ideal_allocation(n->in(3), phase, offset);
4374 // This method is called only before Allocate nodes are expanded
4375 // during macro nodes expansion. Before that ClearArray nodes are
4376 // only generated in PhaseMacroExpand::generate_arraycopy() (before
4377 // Allocate nodes are expanded) which follows allocations.
4378 assert(alloc != nullptr, "should have allocation");
4379 if (alloc->_idx == instance_id) {
4380 // Can not bypass initialization of the instance we are looking for.
4381 return false;
4382 }
4383 // Otherwise skip it.
4384 InitializeNode* init = alloc->initialization();
4385 if (init != nullptr)
4386 *np = init->in(TypeFunc::Memory);
4387 else
4388 *np = alloc->in(TypeFunc::Memory);
4389 return true;
4390 }
4391
4392 //----------------------------clear_memory-------------------------------------
4393 // Generate code to initialize object storage to zero.
4394 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
4395 Node* val,
4396 Node* raw_val,
4397 intptr_t start_offset,
4398 Node* end_offset,
4399 PhaseGVN* phase) {
4400 intptr_t offset = start_offset;
4401
4402 int unit = BytesPerLong;
4403 if ((offset % unit) != 0) {
4404 Node* adr = new AddPNode(dest, dest, phase->MakeConX(offset));
4405 adr = phase->transform(adr);
4406 const TypePtr* atp = TypeRawPtr::BOTTOM;
4407 if (val != nullptr) {
4408 assert(phase->type(val)->isa_narrowoop(), "should be narrow oop");
4409 mem = new StoreNNode(ctl, mem, adr, atp, val, MemNode::unordered);
4410 } else {
4411 assert(raw_val == nullptr, "val may not be null");
4412 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered);
4413 }
4414 mem = phase->transform(mem);
4415 offset += BytesPerInt;
4416 }
4417 assert((offset % unit) == 0, "");
4418
4419 // Initialize the remaining stuff, if any, with a ClearArray.
4420 return clear_memory(ctl, mem, dest, raw_val, phase->MakeConX(offset), end_offset, phase);
4421 }
4422
4423 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
4424 Node* raw_val,
4425 Node* start_offset,
4426 Node* end_offset,
4427 PhaseGVN* phase) {
4428 if (start_offset == end_offset) {
4429 // nothing to do
4430 return mem;
4431 }
4432
4433 int unit = BytesPerLong;
4434 Node* zbase = start_offset;
4435 Node* zend = end_offset;
4436
4437 // Scale to the unit required by the CPU:
4438 if (!Matcher::init_array_count_is_in_bytes) {
4439 Node* shift = phase->intcon(exact_log2(unit));
4440 zbase = phase->transform(new URShiftXNode(zbase, shift) );
4441 zend = phase->transform(new URShiftXNode(zend, shift) );
4442 }
4443
4444 // Bulk clear double-words
4445 Node* zsize = phase->transform(new SubXNode(zend, zbase) );
4446 Node* adr = phase->transform(new AddPNode(dest, dest, start_offset) );
4447 if (raw_val == nullptr) {
4448 raw_val = phase->MakeConX(0);
4449 }
4450 mem = new ClearArrayNode(ctl, mem, zsize, adr, raw_val, false);
4451 return phase->transform(mem);
4452 }
4453
4454 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
4455 Node* val,
4456 Node* raw_val,
4457 intptr_t start_offset,
4458 intptr_t end_offset,
4459 PhaseGVN* phase) {
4460 if (start_offset == end_offset) {
4461 // nothing to do
4462 return mem;
4463 }
4464
4465 assert((end_offset % BytesPerInt) == 0, "odd end offset");
4466 intptr_t done_offset = end_offset;
4467 if ((done_offset % BytesPerLong) != 0) {
4468 done_offset -= BytesPerInt;
4469 }
4470 if (done_offset > start_offset) {
4471 mem = clear_memory(ctl, mem, dest, val, raw_val,
4472 start_offset, phase->MakeConX(done_offset), phase);
4473 }
4474 if (done_offset < end_offset) { // emit the final 32-bit store
4475 Node* adr = new AddPNode(dest, dest, phase->MakeConX(done_offset));
4476 adr = phase->transform(adr);
4477 const TypePtr* atp = TypeRawPtr::BOTTOM;
4478 if (val != nullptr) {
4479 assert(phase->type(val)->isa_narrowoop(), "should be narrow oop");
4480 mem = new StoreNNode(ctl, mem, adr, atp, val, MemNode::unordered);
4481 } else {
4482 assert(raw_val == nullptr, "val may not be null");
4483 mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered);
4484 }
4485 mem = phase->transform(mem);
4486 done_offset += BytesPerInt;
4487 }
4488 assert(done_offset == end_offset, "");
4489 return mem;
4490 }
4491
4492 //=============================================================================
4493 MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent)
4494 : MultiNode(TypeFunc::Parms + (precedent == nullptr? 0: 1)),
4495 _adr_type(C->get_adr_type(alias_idx)), _kind(Standalone)
4496 #ifdef ASSERT
4497 , _pair_idx(0)
4498 #endif
4499 {
4500 init_class_id(Class_MemBar);
4501 Node* top = C->top();
4502 init_req(TypeFunc::I_O,top);
4503 init_req(TypeFunc::FramePtr,top);
4504 init_req(TypeFunc::ReturnAdr,top);
4611 PhaseIterGVN* igvn = phase->is_IterGVN();
4612 remove(igvn);
4613 // Must return either the original node (now dead) or a new node
4614 // (Do not return a top here, since that would break the uniqueness of top.)
4615 return new ConINode(TypeInt::ZERO);
4616 }
4617 }
4618 return progress ? this : nullptr;
4619 }
4620
4621 //------------------------------Value------------------------------------------
4622 const Type* MemBarNode::Value(PhaseGVN* phase) const {
4623 if( !in(0) ) return Type::TOP;
4624 if( phase->type(in(0)) == Type::TOP )
4625 return Type::TOP;
4626 return TypeTuple::MEMBAR;
4627 }
4628
4629 //------------------------------match------------------------------------------
4630 // Construct projections for memory.
4631 Node *MemBarNode::match(const ProjNode *proj, const Matcher *m, const RegMask* mask) {
4632 switch (proj->_con) {
4633 case TypeFunc::Control:
4634 case TypeFunc::Memory:
4635 return new MachProjNode(this, proj->_con, RegMask::EMPTY, MachProjNode::unmatched_proj);
4636 }
4637 ShouldNotReachHere();
4638 return nullptr;
4639 }
4640
4641 void MemBarNode::set_store_pair(MemBarNode* leading, MemBarNode* trailing) {
4642 trailing->_kind = TrailingStore;
4643 leading->_kind = LeadingStore;
4644 #ifdef ASSERT
4645 trailing->_pair_idx = leading->_idx;
4646 leading->_pair_idx = leading->_idx;
4647 #endif
4648 }
4649
4650 void MemBarNode::set_load_store_pair(MemBarNode* leading, MemBarNode* trailing) {
4651 trailing->_kind = TrailingLoadStore;
4898 return (req() > RawStores);
4899 }
4900
4901 void InitializeNode::set_complete(PhaseGVN* phase) {
4902 assert(!is_complete(), "caller responsibility");
4903 _is_complete = Complete;
4904
4905 // After this node is complete, it contains a bunch of
4906 // raw-memory initializations. There is no need for
4907 // it to have anything to do with non-raw memory effects.
4908 // Therefore, tell all non-raw users to re-optimize themselves,
4909 // after skipping the memory effects of this initialization.
4910 PhaseIterGVN* igvn = phase->is_IterGVN();
4911 if (igvn) igvn->add_users_to_worklist(this);
4912 }
4913
4914 // convenience function
4915 // return false if the init contains any stores already
4916 bool AllocateNode::maybe_set_complete(PhaseGVN* phase) {
4917 InitializeNode* init = initialization();
4918 if (init == nullptr || init->is_complete()) {
4919 return false;
4920 }
4921 init->remove_extra_zeroes();
4922 // for now, if this allocation has already collected any inits, bail:
4923 if (init->is_non_zero()) return false;
4924 init->set_complete(phase);
4925 return true;
4926 }
4927
4928 void InitializeNode::remove_extra_zeroes() {
4929 if (req() == RawStores) return;
4930 Node* zmem = zero_memory();
4931 uint fill = RawStores;
4932 for (uint i = fill; i < req(); i++) {
4933 Node* n = in(i);
4934 if (n->is_top() || n == zmem) continue; // skip
4935 if (fill < i) set_req(fill, n); // compact
4936 ++fill;
4937 }
4938 // delete any empty spaces created:
4939 while (fill < req()) {
4940 del_req(fill);
5084 // store node that we'd like to capture. We need to check
5085 // the uses of the MergeMemNode.
5086 mems.push(n);
5087 }
5088 } else if (n->is_Mem()) {
5089 Node* other_adr = n->in(MemNode::Address);
5090 if (other_adr == adr) {
5091 failed = true;
5092 break;
5093 } else {
5094 const TypePtr* other_t_adr = phase->type(other_adr)->isa_ptr();
5095 if (other_t_adr != nullptr) {
5096 int other_alias_idx = phase->C->get_alias_index(other_t_adr);
5097 if (other_alias_idx == alias_idx) {
5098 // A load from the same memory slice as the store right
5099 // after the InitializeNode. We check the control of the
5100 // object/array that is loaded from. If it's the same as
5101 // the store control then we cannot capture the store.
5102 assert(!n->is_Store(), "2 stores to same slice on same control?");
5103 Node* base = other_adr;
5104 if (base->is_Phi()) {
5105 // In rare case, base may be a PhiNode and it may read
5106 // the same memory slice between InitializeNode and store.
5107 failed = true;
5108 break;
5109 }
5110 assert(base->is_AddP(), "should be addp but is %s", base->Name());
5111 base = base->in(AddPNode::Base);
5112 if (base != nullptr) {
5113 base = base->uncast();
5114 if (base->is_Proj() && base->in(0) == alloc) {
5115 failed = true;
5116 break;
5117 }
5118 }
5119 }
5120 }
5121 }
5122 } else {
5123 failed = true;
5124 break;
5125 }
5126 }
5127 }
5128 }
5129 if (failed) {
5676 // z's_done 12 16 16 16 12 16 12
5677 // z's_needed 12 16 16 16 16 16 16
5678 // zsize 0 0 0 0 4 0 4
5679 if (next_full_store < 0) {
5680 // Conservative tack: Zero to end of current word.
5681 zeroes_needed = align_up(zeroes_needed, BytesPerInt);
5682 } else {
5683 // Zero to beginning of next fully initialized word.
5684 // Or, don't zero at all, if we are already in that word.
5685 assert(next_full_store >= zeroes_needed, "must go forward");
5686 assert((next_full_store & (BytesPerInt-1)) == 0, "even boundary");
5687 zeroes_needed = next_full_store;
5688 }
5689 }
5690
5691 if (zeroes_needed > zeroes_done) {
5692 intptr_t zsize = zeroes_needed - zeroes_done;
5693 // Do some incremental zeroing on rawmem, in parallel with inits.
5694 zeroes_done = align_down(zeroes_done, BytesPerInt);
5695 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
5696 allocation()->in(AllocateNode::InitValue),
5697 allocation()->in(AllocateNode::RawInitValue),
5698 zeroes_done, zeroes_needed,
5699 phase);
5700 zeroes_done = zeroes_needed;
5701 if (zsize > InitArrayShortSize && ++big_init_gaps > 2)
5702 do_zeroing = false; // leave the hole, next time
5703 }
5704 }
5705
5706 // Collect the store and move on:
5707 phase->replace_input_of(st, MemNode::Memory, inits);
5708 inits = st; // put it on the linearized chain
5709 set_req(i, zmem); // unhook from previous position
5710
5711 if (zeroes_done == st_off)
5712 zeroes_done = next_init_off;
5713
5714 assert(!do_zeroing || zeroes_done >= next_init_off, "don't miss any");
5715
5716 #ifdef ASSERT
5717 // Various order invariants. Weaker than stores_are_sane because
5737 remove_extra_zeroes(); // clear out all the zmems left over
5738 add_req(inits);
5739
5740 if (!(UseTLAB && ZeroTLAB)) {
5741 // If anything remains to be zeroed, zero it all now.
5742 zeroes_done = align_down(zeroes_done, BytesPerInt);
5743 // if it is the last unused 4 bytes of an instance, forget about it
5744 intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, max_jint);
5745 if (zeroes_done + BytesPerLong >= size_limit) {
5746 AllocateNode* alloc = allocation();
5747 assert(alloc != nullptr, "must be present");
5748 if (alloc != nullptr && alloc->Opcode() == Op_Allocate) {
5749 Node* klass_node = alloc->in(AllocateNode::KlassNode);
5750 ciKlass* k = phase->type(klass_node)->is_instklassptr()->instance_klass();
5751 if (zeroes_done == k->layout_helper())
5752 zeroes_done = size_limit;
5753 }
5754 }
5755 if (zeroes_done < size_limit) {
5756 rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
5757 allocation()->in(AllocateNode::InitValue),
5758 allocation()->in(AllocateNode::RawInitValue),
5759 zeroes_done, size_in_bytes, phase);
5760 }
5761 }
5762
5763 set_complete(phase);
5764 return rawmem;
5765 }
5766
5767 void InitializeNode::replace_mem_projs_by(Node* mem, Compile* C) {
5768 auto replace_proj = [&](ProjNode* proj) {
5769 C->gvn_replace_by(proj, mem);
5770 return CONTINUE;
5771 };
5772 apply_to_projs(replace_proj, TypeFunc::Memory);
5773 }
5774
5775 void InitializeNode::replace_mem_projs_by(Node* mem, PhaseIterGVN* igvn) {
5776 DUIterator_Fast imax, i = fast_outs(imax);
5777 auto replace_proj = [&](ProjNode* proj) {
5778 igvn->replace_node(proj, mem);
|