< prev index next >

src/hotspot/share/opto/parseHelper.cpp

Print this page

  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 

 25 #include "ci/ciSymbols.hpp"
 26 #include "compiler/compileLog.hpp"

 27 #include "oops/objArrayKlass.hpp"
 28 #include "opto/addnode.hpp"


 29 #include "opto/memnode.hpp"
 30 #include "opto/mulnode.hpp"
 31 #include "opto/parse.hpp"
 32 #include "opto/rootnode.hpp"
 33 #include "opto/runtime.hpp"
 34 #include "runtime/sharedRuntime.hpp"
 35 
 36 //------------------------------make_dtrace_method_entry_exit ----------------
 37 // Dtrace -- record entry or exit of a method if compiled with dtrace support
 38 void GraphKit::make_dtrace_method_entry_exit(ciMethod* method, bool is_entry) {
 39   const TypeFunc *call_type    = OptoRuntime::dtrace_method_entry_exit_Type();
 40   address         call_address = is_entry ? CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry) :
 41                                             CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit);
 42   const char     *call_name    = is_entry ? "dtrace_method_entry" : "dtrace_method_exit";
 43 
 44   // Get base of thread-local storage area
 45   Node* thread = _gvn.transform( new ThreadLocalNode() );
 46 
 47   // Get method
 48   const TypePtr* method_type = TypeMetadataPtr::make(method);
 49   Node *method_node = _gvn.transform(ConNode::make(method_type));
 50 
 51   kill_dead_locals();
 52 
 53   // For some reason, this call reads only raw memory.
 54   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
 55   make_runtime_call(RC_LEAF | RC_NARROW_MEM,
 56                     call_type, call_address,
 57                     call_name, raw_adr_type,
 58                     thread, method_node);
 59 }
 60 
 61 
 62 //=============================================================================
 63 //------------------------------do_checkcast-----------------------------------
 64 void Parse::do_checkcast() {
 65   bool will_link;
 66   ciKlass* klass = iter().get_klass(will_link);
 67 
 68   Node *obj = peek();
 69 
 70   // Throw uncommon trap if class is not loaded or the value we are casting
 71   // _from_ is not loaded, and value is not null.  If the value _is_ null,
 72   // then the checkcast does nothing.
 73   const TypeOopPtr *tp = _gvn.type(obj)->isa_oopptr();
 74   if (!will_link || (tp && !tp->is_loaded())) {
 75     if (C->log() != nullptr) {
 76       if (!will_link) {
 77         C->log()->elem("assert_null reason='checkcast' klass='%d'",
 78                        C->log()->identify(klass));
 79       }
 80       if (tp && !tp->is_loaded()) {
 81         // %%% Cannot happen?
 82         ciKlass* klass = tp->unloaded_klass();
 83         C->log()->elem("assert_null reason='checkcast source' klass='%d'",
 84                        C->log()->identify(klass));
 85       }
 86     }
 87     null_assert(obj);

120     assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
121     if (!stopped()) {
122       // The object is now known to be null.
123       // Shortcut the effect of gen_instanceof and return "false" directly.
124       pop();                   // pop the null
125       push(_gvn.intcon(0));    // push false answer
126     }
127     return;
128   }
129 
130   // Push the bool result back on stack
131   Node* res = gen_instanceof(peek(), makecon(TypeKlassPtr::make(klass, Type::trust_interfaces)), true);
132 
133   // Pop from stack AFTER gen_instanceof because it can uncommon trap.
134   pop();
135   push(res);
136 }
137 
138 //------------------------------array_store_check------------------------------
139 // pull array from stack and check that the store is valid
140 void Parse::array_store_check() {
141 
142   // Shorthand access to array store elements without popping them.
143   Node *obj = peek(0);
144   Node *idx = peek(1);
145   Node *ary = peek(2);
146 
147   if (_gvn.type(obj) == TypePtr::NULL_PTR) {
148     // There's never a type check on null values.
149     // This cutout lets us avoid the uncommon_trap(Reason_array_check)
150     // below, which turns into a performance liability if the
151     // gen_checkcast folds up completely.
152     return;



153   }
154 
155   // Extract the array klass type
156   int klass_offset = oopDesc::klass_offset_in_bytes();
157   Node* p = basic_plus_adr( ary, ary, klass_offset );
158   // p's type is array-of-OOPS plus klass_offset
159   Node* array_klass = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p, TypeInstPtr::KLASS));
160   // Get the array klass
161   const TypeKlassPtr *tak = _gvn.type(array_klass)->is_klassptr();
162 
163   // The type of array_klass is usually INexact array-of-oop.  Heroically
164   // cast array_klass to EXACT array and uncommon-trap if the cast fails.
165   // Make constant out of the inexact array klass, but use it only if the cast
166   // succeeds.
167   if (MonomorphicArrayCheck &&
168       !too_many_traps(Deoptimization::Reason_array_check) &&
169       !tak->klass_is_exact() &&
170       tak->isa_aryklassptr()) {
171       // Regarding the fourth condition in the if-statement from above:
172       //




















173       // If the compiler has determined that the type of array 'ary' (represented
174       // by 'array_klass') is java/lang/Object, the compiler must not assume that
175       // the array 'ary' is monomorphic.
176       //
177       // If 'ary' were of type java/lang/Object, this arraystore would have to fail,
178       // because it is not possible to perform a arraystore into an object that is not
179       // a "proper" array.
180       //
181       // Therefore, let's obtain at runtime the type of 'ary' and check if we can still
182       // successfully perform the store.
183       //
184       // The implementation reasons for the condition are the following:
185       //
186       // java/lang/Object is the superclass of all arrays, but it is represented by the VM
187       // as an InstanceKlass. The checks generated by gen_checkcast() (see below) expect
188       // 'array_klass' to be ObjArrayKlass, which can result in invalid memory accesses.
189       //
190       // See issue JDK-8057622 for details.
191 
192     // Make a constant out of the exact array klass
193     const TypeAryKlassPtr* extak = tak->cast_to_exactness(true)->is_aryklassptr();
194     if (extak->exact_klass(true) != nullptr) {
195       Node* con = makecon(extak);
196       Node* cmp = _gvn.transform(new CmpPNode(array_klass, con));
197       Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
198       Node* ctrl= control();
199       { BuildCutout unless(this, bol, PROB_MAX);
200         uncommon_trap(Deoptimization::Reason_array_check,
201                       Deoptimization::Action_maybe_recompile,
202                       extak->exact_klass());
203       }
204       if (stopped()) {          // MUST uncommon-trap?
205         set_control(ctrl);      // Then Don't Do It, just fall into the normal checking
206       } else {                  // Cast array klass to exactness:
207         // Use the exact constant value we know it is.
208         replace_in_map(array_klass, con);










209         CompileLog* log = C->log();
210         if (log != nullptr) {
211           log->elem("cast_up reason='monomorphic_array' from='%d' to='(exact)'",
212                     log->identify(extak->exact_klass()));
213         }
214         array_klass = con;      // Use cast value moving forward
215       }
216     }
217   }
218 
219   // Come here for polymorphic array klasses
220 
221   // Extract the array element class
222   int element_klass_offset = in_bytes(ObjArrayKlass::element_klass_offset());
223   Node* p2 = basic_plus_adr(array_klass, array_klass, element_klass_offset);
224   Node* a_e_klass = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p2, tak));
225   assert(array_klass->is_Con() == a_e_klass->is_Con() || StressReflectiveCode, "a constant array type must come with a constant element type");


































226 
227   // Check (the hard way) and throw if not a subklass.
228   // Result is ignored, we just need the CFG effects.
229   gen_checkcast(obj, a_e_klass);
230 }
231 
232 
233 //------------------------------do_new-----------------------------------------
234 void Parse::do_new() {
235   kill_dead_locals();
236 
237   bool will_link;
238   ciInstanceKlass* klass = iter().get_klass(will_link)->as_instance_klass();
239   assert(will_link, "_new: typeflow responsibility");
240 
241   // Should throw an InstantiationError?
242   if (klass->is_abstract() || klass->is_interface() ||
243       klass->name() == ciSymbols::java_lang_Class() ||
244       iter().is_unresolved_klass()) {
245     uncommon_trap(Deoptimization::Reason_unhandled,
246                   Deoptimization::Action_none,
247                   klass);
248     return;
249   }
250 
251   if (C->needs_clinit_barrier(klass, method())) {
252     clinit_barrier(klass, method());
253     if (stopped())  return;
254   }
255 





256   Node* kls = makecon(TypeKlassPtr::make(klass));
257   Node* obj = new_instance(kls);
258 
259   // Push resultant oop onto stack
260   push(obj);
261 
262   // Keep track of whether opportunities exist for StringBuilder
263   // optimizations.
264   if (OptimizeStringConcat &&
265       (klass == C->env()->StringBuilder_klass() ||
266        klass == C->env()->StringBuffer_klass())) {
267     C->set_has_stringbuilder(true);
268   }
269 
270   // Keep track of boxed values for EliminateAutoBox optimizations.
271   if (C->eliminate_boxing() && klass->is_box_klass()) {
272     C->set_has_boxed_value(true);
273   }
274 }
275 

  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "ci/ciInlineKlass.hpp"
 26 #include "ci/ciSymbols.hpp"
 27 #include "compiler/compileLog.hpp"
 28 #include "oops/flatArrayKlass.hpp"
 29 #include "oops/objArrayKlass.hpp"
 30 #include "opto/addnode.hpp"
 31 #include "opto/castnode.hpp"
 32 #include "opto/inlinetypenode.hpp"
 33 #include "opto/memnode.hpp"
 34 #include "opto/mulnode.hpp"
 35 #include "opto/parse.hpp"
 36 #include "opto/rootnode.hpp"
 37 #include "opto/runtime.hpp"
 38 #include "runtime/sharedRuntime.hpp"
 39 
 40 //------------------------------make_dtrace_method_entry_exit ----------------
 41 // Dtrace -- record entry or exit of a method if compiled with dtrace support
 42 void GraphKit::make_dtrace_method_entry_exit(ciMethod* method, bool is_entry) {
 43   const TypeFunc *call_type    = OptoRuntime::dtrace_method_entry_exit_Type();
 44   address         call_address = is_entry ? CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry) :
 45                                             CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit);
 46   const char     *call_name    = is_entry ? "dtrace_method_entry" : "dtrace_method_exit";
 47 
 48   // Get base of thread-local storage area
 49   Node* thread = _gvn.transform( new ThreadLocalNode() );
 50 
 51   // Get method
 52   const TypePtr* method_type = TypeMetadataPtr::make(method);
 53   Node *method_node = _gvn.transform(ConNode::make(method_type));
 54 
 55   kill_dead_locals();
 56 
 57   // For some reason, this call reads only raw memory.
 58   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
 59   make_runtime_call(RC_LEAF | RC_NARROW_MEM,
 60                     call_type, call_address,
 61                     call_name, raw_adr_type,
 62                     thread, method_node);
 63 }
 64 
 65 
 66 //=============================================================================
 67 //------------------------------do_checkcast-----------------------------------
 68 void Parse::do_checkcast() {
 69   bool will_link;
 70   ciKlass* klass = iter().get_klass(will_link);

 71   Node *obj = peek();
 72 
 73   // Throw uncommon trap if class is not loaded or the value we are casting
 74   // _from_ is not loaded, and value is not null.  If the value _is_ null,
 75   // then the checkcast does nothing.
 76   const TypeOopPtr *tp = _gvn.type(obj)->isa_oopptr();
 77   if (!will_link || (tp && !tp->is_loaded())) {
 78     if (C->log() != nullptr) {
 79       if (!will_link) {
 80         C->log()->elem("assert_null reason='checkcast' klass='%d'",
 81                        C->log()->identify(klass));
 82       }
 83       if (tp && !tp->is_loaded()) {
 84         // %%% Cannot happen?
 85         ciKlass* klass = tp->unloaded_klass();
 86         C->log()->elem("assert_null reason='checkcast source' klass='%d'",
 87                        C->log()->identify(klass));
 88       }
 89     }
 90     null_assert(obj);

123     assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
124     if (!stopped()) {
125       // The object is now known to be null.
126       // Shortcut the effect of gen_instanceof and return "false" directly.
127       pop();                   // pop the null
128       push(_gvn.intcon(0));    // push false answer
129     }
130     return;
131   }
132 
133   // Push the bool result back on stack
134   Node* res = gen_instanceof(peek(), makecon(TypeKlassPtr::make(klass, Type::trust_interfaces)), true);
135 
136   // Pop from stack AFTER gen_instanceof because it can uncommon trap.
137   pop();
138   push(res);
139 }
140 
141 //------------------------------array_store_check------------------------------
142 // pull array from stack and check that the store is valid
143 Node* Parse::array_store_check(Node*& adr, const Type*& elemtype) {

144   // Shorthand access to array store elements without popping them.
145   Node *obj = peek(0);
146   Node *idx = peek(1);
147   Node *ary = peek(2);
148 
149   if (_gvn.type(obj) == TypePtr::NULL_PTR) {
150     // There's never a type check on null values.
151     // This cutout lets us avoid the uncommon_trap(Reason_array_check)
152     // below, which turns into a performance liability if the
153     // gen_checkcast folds up completely.
154     if (_gvn.type(ary)->is_aryptr()->is_null_free()) {
155       null_check(obj);
156     }
157     return obj;
158   }
159 
160   // Extract the array klass type
161   Node* array_klass = load_object_klass(ary);



162   // Get the array klass
163   const TypeKlassPtr* tak = _gvn.type(array_klass)->is_klassptr();
164 
165   // The type of array_klass is usually INexact array-of-oop.  Heroically
166   // cast array_klass to EXACT array and uncommon-trap if the cast fails.
167   // Make constant out of the inexact array klass, but use it only if the cast
168   // succeeds.
169   if (MonomorphicArrayCheck && !tak->klass_is_exact()) {
170     // Make a constant out of the inexact array klass
171     const TypeAryKlassPtr* extak = nullptr;
172     const TypeOopPtr* ary_t = _gvn.type(ary)->is_oopptr();
173     ciKlass* ary_spec = ary_t->speculative_type();
174     Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
175     // Try to cast the array to an exact type from profile data. First
176     // check the speculative type.
177     if (ary_spec != nullptr && !too_many_traps(Deoptimization::Reason_speculate_class_check)) {
178       extak = TypeKlassPtr::make(ary_spec)->is_aryklassptr();
179       reason = Deoptimization::Reason_speculate_class_check;
180     } else if (UseArrayLoadStoreProfile) {
181       // No speculative type: check profile data at this bci.
182       reason = Deoptimization::Reason_class_check;
183       if (!too_many_traps(reason)) {
184         ciKlass* array_type = nullptr;
185         ciKlass* element_type = nullptr;
186         ProfilePtrKind element_ptr = ProfileMaybeNull;
187         bool flat_array = true;
188         bool null_free_array = true;
189         method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
190         if (array_type != nullptr) {
191           extak = TypeKlassPtr::make(array_type)->is_aryklassptr();
192         }
193       }
194     } else if (!too_many_traps(Deoptimization::Reason_array_check) && tak->isa_aryklassptr()) {
195       // If the compiler has determined that the type of array 'ary' (represented
196       // by 'array_klass') is java/lang/Object, the compiler must not assume that
197       // the array 'ary' is monomorphic.
198       //
199       // If 'ary' were of type java/lang/Object, this arraystore would have to fail,
200       // because it is not possible to perform a arraystore into an object that is not
201       // a "proper" array.
202       //
203       // Therefore, let's obtain at runtime the type of 'ary' and check if we can still
204       // successfully perform the store.
205       //
206       // The implementation reasons for the condition are the following:
207       //
208       // java/lang/Object is the superclass of all arrays, but it is represented by the VM
209       // as an InstanceKlass. The checks generated by gen_checkcast() (see below) expect
210       // 'array_klass' to be ObjArrayKlass, which can result in invalid memory accesses.
211       //
212       // See issue JDK-8057622 for details.
213       extak = tak->cast_to_exactness(true)->is_aryklassptr();
214       reason = Deoptimization::Reason_array_check;
215     }
216     if (extak != nullptr && extak->exact_klass(true) != nullptr) {
217       Node* con = makecon(extak);
218       Node* cmp = _gvn.transform(new CmpPNode(array_klass, con));
219       Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
220       // Only do it if the check does not always pass/fail
221       if (!bol->is_Con()) {
222         { BuildCutout unless(this, bol, PROB_MAX);
223           uncommon_trap(reason,
224                         Deoptimization::Action_maybe_recompile,
225                         extak->exact_klass());
226         }
227         // Cast array klass to exactness


228         replace_in_map(array_klass, con);
229         array_klass = con;
230         Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, extak->as_instance_type()));
231         replace_in_map(ary, cast);
232         ary = cast;
233 
234         // Recompute element type and address
235         const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
236         elemtype = arytype->elem();
237         adr = array_element_address(ary, idx, T_OBJECT, arytype->size(), control());
238 
239         CompileLog* log = C->log();
240         if (log != nullptr) {
241           log->elem("cast_up reason='monomorphic_array' from='%d' to='(exact)'",
242                     log->identify(extak->exact_klass()));
243         }

244       }
245     }
246   }
247 
248   // Come here for polymorphic array klasses
249 
250   // Extract the array element class
251   int element_klass_offset = in_bytes(ArrayKlass::element_klass_offset());
252   Node* p2 = basic_plus_adr(array_klass, array_klass, element_klass_offset);
253   Node* a_e_klass = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p2, tak));
254 
255   // If we statically know that this is an inline type array, use precise element klass for checkcast
256   const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
257   const TypePtr* elem_ptr = elemtype->make_ptr();
258   bool null_free = arytype->is_null_free();
259   if (elem_ptr->is_inlinetypeptr()) {
260     // We statically know that this is an inline type array, use precise klass ptr
261     a_e_klass = makecon(TypeKlassPtr::make(elemtype->inline_klass()));
262   }
263 #ifdef ASSERT
264   if (!StressReflectiveCode && array_klass->is_Con() != a_e_klass->is_Con()) {
265     // When the element type is exact, the array type also needs to be exact. There is one exception, though:
266     // Nullable arrays are not exact because the null-free array is a subtype while the element type being a
267     // concrete value class (i.e. final) is always exact.
268     assert(!array_klass->is_Con() && a_e_klass->is_Con() && elem_ptr->is_inlinetypeptr() && !null_free,
269            "a constant element type either matches a constant array type or a non-constant nullable value class array");
270   }
271 
272   // If the element type is exact, the array can be null-free (i.e. the element type is NotNull) if:
273   //   - The elements are inline types
274   //   - The array is from an autobox cache.
275   // If the element type is inexact, it could represent multiple null-free arrays. Since autobox cache arrays
276   // are local to very few cache classes and are only used in the valueOf() methods, they are always exact and are not
277   // merged or hidden behind super types. Therefore, an inexact null-free array always represents some kind of
278   // inline type array - either of an abstract value class or Object.
279   if (null_free) {
280     ciKlass* klass = elem_ptr->is_instptr()->instance_klass();
281     if (klass->exact_klass()) {
282       assert(elem_ptr->is_inlinetypeptr() || arytype->is_autobox_cache(), "elements must be inline type or autobox cache");
283     } else {
284       assert(!arytype->is_autobox_cache() && elem_ptr->can_be_inline_type() &&
285              (klass->is_java_lang_Object() || klass->is_abstract()), "cannot have inexact non-inline type elements");
286     }
287   }
288 #endif // ASSERT
289 
290   // Check (the hard way) and throw if not a subklass.
291   return gen_checkcast(obj, a_e_klass, nullptr, null_free);

292 }
293 
294 
295 //------------------------------do_new-----------------------------------------
296 void Parse::do_new() {
297   kill_dead_locals();
298 
299   bool will_link;
300   ciInstanceKlass* klass = iter().get_klass(will_link)->as_instance_klass();
301   assert(will_link, "_new: typeflow responsibility");
302 
303   // Should throw an InstantiationError?
304   if (klass->is_abstract() || klass->is_interface() ||
305       klass->name() == ciSymbols::java_lang_Class() ||
306       iter().is_unresolved_klass()) {
307     uncommon_trap(Deoptimization::Reason_unhandled,
308                   Deoptimization::Action_none,
309                   klass);
310     return;
311   }
312 
313   if (C->needs_clinit_barrier(klass, method())) {
314     clinit_barrier(klass, method());
315     if (stopped())  return;
316   }
317 
318   if (klass->is_inlinetype()) {
319     push(InlineTypeNode::make_all_zero(_gvn, klass->as_inline_klass(), /* is_larval */ true));
320     return;
321   }
322 
323   Node* kls = makecon(TypeKlassPtr::make(klass));
324   Node* obj = new_instance(kls);
325 
326   // Push resultant oop onto stack
327   push(obj);
328 
329   // Keep track of whether opportunities exist for StringBuilder
330   // optimizations.
331   if (OptimizeStringConcat &&
332       (klass == C->env()->StringBuilder_klass() ||
333        klass == C->env()->StringBuffer_klass())) {
334     C->set_has_stringbuilder(true);
335   }
336 
337   // Keep track of boxed values for EliminateAutoBox optimizations.
338   if (C->eliminate_boxing() && klass->is_box_klass()) {
339     C->set_has_boxed_value(true);
340   }
341 }
342 
< prev index next >