1 /* 2 * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact 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 "c1/c1_Compilation.hpp" 26 #include "c1/c1_Compiler.hpp" 27 #include "c1/c1_FrameMap.hpp" 28 #include "c1/c1_GraphBuilder.hpp" 29 #include "c1/c1_LinearScan.hpp" 30 #include "c1/c1_MacroAssembler.hpp" 31 #include "c1/c1_Runtime1.hpp" 32 #include "c1/c1_ValueType.hpp" 33 #include "code/SCCache.hpp" 34 #include "compiler/compileBroker.hpp" 35 #include "compiler/compilerDirectives.hpp" 36 #include "interpreter/linkResolver.hpp" 37 #include "jfr/support/jfrIntrinsics.hpp" 38 #include "memory/allocation.hpp" 39 #include "memory/allocation.inline.hpp" 40 #include "memory/resourceArea.hpp" 41 #include "runtime/interfaceSupport.inline.hpp" 42 #include "runtime/sharedRuntime.hpp" 43 #include "runtime/vm_version.hpp" 44 #include "utilities/bitMap.inline.hpp" 45 #include "utilities/macros.hpp" 46 47 48 Compiler::Compiler() : AbstractCompiler(compiler_c1) { 49 } 50 51 void Compiler::init_c1_runtime() { 52 BufferBlob* buffer_blob = CompilerThread::current()->get_buffer_blob(); 53 Runtime1::initialize(buffer_blob); 54 SCCache::init_c1_table(); 55 FrameMap::initialize(); 56 // initialize data structures 57 ValueType::initialize(); 58 GraphBuilder::initialize(); 59 // note: to use more than one instance of LinearScan at a time this function call has to 60 // be moved somewhere outside of this constructor: 61 Interval::initialize(); 62 } 63 64 65 void Compiler::initialize() { 66 // Buffer blob must be allocated per C1 compiler thread at startup 67 BufferBlob* buffer_blob = init_buffer_blob(); 68 69 if (should_perform_init()) { 70 if (buffer_blob == nullptr) { 71 // When we come here we are in state 'initializing'; entire C1 compilation 72 // can be shut down. 73 set_state(failed); 74 } else { 75 init_c1_runtime(); 76 set_state(initialized); 77 } 78 } 79 } 80 81 uint Compiler::code_buffer_size() { 82 return Compilation::desired_max_code_buffer_size() + Compilation::desired_max_constant_size(); 83 } 84 85 BufferBlob* Compiler::init_buffer_blob() { 86 // Allocate buffer blob once at startup since allocation for each 87 // compilation seems to be too expensive (at least on Intel win32). 88 assert (CompilerThread::current()->get_buffer_blob() == nullptr, "Should initialize only once"); 89 90 // setup CodeBuffer. Preallocate a BufferBlob of size 91 // NMethodSizeLimit plus some extra space for constants. 92 BufferBlob* buffer_blob = BufferBlob::create("C1 temporary CodeBuffer", code_buffer_size()); 93 if (buffer_blob != nullptr) { 94 CompilerThread::current()->set_buffer_blob(buffer_blob); 95 } 96 97 return buffer_blob; 98 } 99 100 bool Compiler::is_intrinsic_supported(const methodHandle& method) { 101 vmIntrinsics::ID id = method->intrinsic_id(); 102 assert(id != vmIntrinsics::_none, "must be a VM intrinsic"); 103 104 if (method->is_synchronized()) { 105 // C1 does not support intrinsification of synchronized methods. 106 return false; 107 } 108 return Compiler::is_intrinsic_supported(id); 109 } 110 111 bool Compiler::is_intrinsic_supported(vmIntrinsics::ID id) { 112 switch (id) { 113 case vmIntrinsics::_compareAndSetLong: 114 break; 115 case vmIntrinsics::_getAndAddInt: 116 if (!VM_Version::supports_atomic_getadd4()) return false; 117 break; 118 case vmIntrinsics::_getAndAddLong: 119 if (!VM_Version::supports_atomic_getadd8()) return false; 120 break; 121 case vmIntrinsics::_getAndSetInt: 122 if (!VM_Version::supports_atomic_getset4()) return false; 123 break; 124 case vmIntrinsics::_getAndSetLong: 125 if (!VM_Version::supports_atomic_getset8()) return false; 126 break; 127 case vmIntrinsics::_getAndSetReference: 128 #ifdef _LP64 129 if (!UseCompressedOops && !VM_Version::supports_atomic_getset8()) return false; 130 if (UseCompressedOops && !VM_Version::supports_atomic_getset4()) return false; 131 #else 132 if (!VM_Version::supports_atomic_getset4()) return false; 133 #endif 134 break; 135 case vmIntrinsics::_onSpinWait: 136 if (!VM_Version::supports_on_spin_wait()) return false; 137 break; 138 case vmIntrinsics::_floatToFloat16: 139 case vmIntrinsics::_float16ToFloat: 140 if (!VM_Version::supports_float16()) return false; 141 break; 142 case vmIntrinsics::_arraycopy: 143 case vmIntrinsics::_currentTimeMillis: 144 case vmIntrinsics::_nanoTime: 145 case vmIntrinsics::_Reference_get: 146 // Use the intrinsic version of Reference.get() so that the value in 147 // the referent field can be registered by the G1 pre-barrier code. 148 // Also to prevent commoning reads from this field across safepoint 149 // since GC can change its value. 150 case vmIntrinsics::_loadFence: 151 case vmIntrinsics::_storeFence: 152 case vmIntrinsics::_storeStoreFence: 153 case vmIntrinsics::_fullFence: 154 case vmIntrinsics::_floatToRawIntBits: 155 case vmIntrinsics::_intBitsToFloat: 156 case vmIntrinsics::_doubleToRawLongBits: 157 case vmIntrinsics::_longBitsToDouble: 158 case vmIntrinsics::_getClass: 159 case vmIntrinsics::_isInstance: 160 case vmIntrinsics::_isPrimitive: 161 case vmIntrinsics::_getModifiers: 162 case vmIntrinsics::_currentCarrierThread: 163 case vmIntrinsics::_currentThread: 164 case vmIntrinsics::_scopedValueCache: 165 case vmIntrinsics::_dabs: 166 case vmIntrinsics::_dsqrt: 167 case vmIntrinsics::_dsqrt_strict: 168 case vmIntrinsics::_dsin: 169 case vmIntrinsics::_dcos: 170 case vmIntrinsics::_dtan: 171 #if defined(AMD64) 172 case vmIntrinsics::_dtanh: 173 #endif 174 case vmIntrinsics::_dlog: 175 case vmIntrinsics::_dlog10: 176 case vmIntrinsics::_dexp: 177 case vmIntrinsics::_dpow: 178 case vmIntrinsics::_fmaD: 179 case vmIntrinsics::_fmaF: 180 case vmIntrinsics::_getReference: 181 case vmIntrinsics::_getBoolean: 182 case vmIntrinsics::_getByte: 183 case vmIntrinsics::_getShort: 184 case vmIntrinsics::_getChar: 185 case vmIntrinsics::_getInt: 186 case vmIntrinsics::_getLong: 187 case vmIntrinsics::_getFloat: 188 case vmIntrinsics::_getDouble: 189 case vmIntrinsics::_putReference: 190 case vmIntrinsics::_putBoolean: 191 case vmIntrinsics::_putByte: 192 case vmIntrinsics::_putShort: 193 case vmIntrinsics::_putChar: 194 case vmIntrinsics::_putInt: 195 case vmIntrinsics::_putLong: 196 case vmIntrinsics::_putFloat: 197 case vmIntrinsics::_putDouble: 198 case vmIntrinsics::_getReferenceVolatile: 199 case vmIntrinsics::_getBooleanVolatile: 200 case vmIntrinsics::_getByteVolatile: 201 case vmIntrinsics::_getShortVolatile: 202 case vmIntrinsics::_getCharVolatile: 203 case vmIntrinsics::_getIntVolatile: 204 case vmIntrinsics::_getLongVolatile: 205 case vmIntrinsics::_getFloatVolatile: 206 case vmIntrinsics::_getDoubleVolatile: 207 case vmIntrinsics::_putReferenceVolatile: 208 case vmIntrinsics::_putBooleanVolatile: 209 case vmIntrinsics::_putByteVolatile: 210 case vmIntrinsics::_putShortVolatile: 211 case vmIntrinsics::_putCharVolatile: 212 case vmIntrinsics::_putIntVolatile: 213 case vmIntrinsics::_putLongVolatile: 214 case vmIntrinsics::_putFloatVolatile: 215 case vmIntrinsics::_putDoubleVolatile: 216 case vmIntrinsics::_getShortUnaligned: 217 case vmIntrinsics::_getCharUnaligned: 218 case vmIntrinsics::_getIntUnaligned: 219 case vmIntrinsics::_getLongUnaligned: 220 case vmIntrinsics::_putShortUnaligned: 221 case vmIntrinsics::_putCharUnaligned: 222 case vmIntrinsics::_putIntUnaligned: 223 case vmIntrinsics::_putLongUnaligned: 224 case vmIntrinsics::_Preconditions_checkIndex: 225 case vmIntrinsics::_Preconditions_checkLongIndex: 226 case vmIntrinsics::_updateCRC32: 227 case vmIntrinsics::_updateBytesCRC32: 228 case vmIntrinsics::_updateByteBufferCRC32: 229 #if defined(S390) || defined(PPC64) || defined(AARCH64) 230 case vmIntrinsics::_updateBytesCRC32C: 231 case vmIntrinsics::_updateDirectByteBufferCRC32C: 232 #endif 233 case vmIntrinsics::_vectorizedMismatch: 234 case vmIntrinsics::_compareAndSetInt: 235 case vmIntrinsics::_compareAndSetReference: 236 case vmIntrinsics::_getCharStringU: 237 case vmIntrinsics::_putCharStringU: 238 #ifdef JFR_HAVE_INTRINSICS 239 case vmIntrinsics::_counterTime: 240 #endif 241 case vmIntrinsics::_getObjectSize: 242 #if defined(X86) || defined(AARCH64) || defined(S390) || defined(RISCV) || defined(PPC64) 243 case vmIntrinsics::_clone: 244 #endif 245 break; 246 case vmIntrinsics::_blackhole: 247 break; 248 default: 249 return false; // Intrinsics not on the previous list are not available. 250 } 251 252 return true; 253 } 254 255 void Compiler::compile_method(ciEnv* env, ciMethod* method, int entry_bci, bool install_code, DirectiveSet* directive) { 256 CompileTask* task = env->task(); 257 if (install_code && task->is_scc()) { 258 assert(!task->preload(), "Pre-loading cached code is not implemeted for C1 code"); 259 bool success = SCCache::load_nmethod(env, method, entry_bci, this, CompLevel(task->comp_level())); 260 if (success) { 261 assert(task->is_success(), "sanity"); 262 return; 263 } 264 SCCache::invalidate(task->scc_entry()); // mark scc_entry as not entrant 265 if (SCCache::is_code_load_thread_on() && !StoreCachedCode) { 266 // Bail out if failed to load cached code in SC thread 267 // unless the code is updating. 268 env->record_failure("Failed to load cached code"); 269 return; 270 } 271 task->clear_scc(); 272 } 273 BufferBlob* buffer_blob = CompilerThread::current()->get_buffer_blob(); 274 assert(buffer_blob != nullptr, "Must exist"); 275 // invoke compilation 276 { 277 // We are nested here because we need for the destructor 278 // of Compilation to occur before we release the any 279 // competing compiler thread 280 ResourceMark rm; 281 Compilation c(this, env, method, entry_bci, buffer_blob, install_code, directive); 282 } 283 } 284 285 286 void Compiler::print_timers() { 287 Compilation::print_timers(); 288 }