1 // Copyright 2019 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_OBJECTS_OSR_OPTIMIZED_CODE_CACHE_H_ 6 #define V8_OBJECTS_OSR_OPTIMIZED_CODE_CACHE_H_ 7 8 #include "src/objects/fixed-array.h" 9 // Has to be the last include (doesn't have include guards): 10 #include "src/objects/object-macros.h" 11 12 namespace v8 { 13 namespace internal { 14 15 class V8_EXPORT OSROptimizedCodeCache : public WeakFixedArray { 16 public: 17 DECL_CAST(OSROptimizedCodeCache) 18 19 enum OSRCodeCacheConstants { 20 kSharedOffset, 21 kCachedCodeOffset, 22 kOsrIdOffset, 23 kEntryLength 24 }; 25 26 static const int kInitialLength = OSRCodeCacheConstants::kEntryLength * 4; 27 static const int kMaxLength = OSRCodeCacheConstants::kEntryLength * 1024; 28 29 // Caches the optimized code |code| corresponding to the shared function 30 // |shared| and bailout id |osr_offset| in the OSROptimized code cache. 31 // If the OSR code cache wasn't created before it creates a code cache with 32 // kOSRCodeCacheInitialLength entries. 33 static void AddOptimizedCode(Handle<NativeContext> context, 34 Handle<SharedFunctionInfo> shared, 35 Handle<Code> code, BailoutId osr_offset); 36 // Reduces the size of the OSR code cache if the number of valid entries are 37 // less than the current capacity of the cache. 38 static void Compact(Handle<NativeContext> context); 39 // Sets the OSR optimized code cache to an empty array. 40 static void Clear(NativeContext context); 41 42 // Returns the code corresponding to the shared function |shared| and 43 // BailoutId |offset| if an entry exists in the cache. Returns an empty 44 // object otherwise. 45 Code GetOptimizedCode(Handle<SharedFunctionInfo> shared, BailoutId osr_offset, 46 Isolate* isolate); 47 48 // Remove all code objects marked for deoptimization from OSR code cache. 49 void EvictMarkedCode(Isolate* isolate); 50 51 private: 52 // Functions that implement heuristics on when to grow / shrink the cache. 53 static int CapacityForLength(int curr_capacity); 54 static bool NeedsTrimming(int num_valid_entries, int curr_capacity); 55 static int GrowOSRCache(Handle<NativeContext> native_context, 56 Handle<OSROptimizedCodeCache>* osr_cache); 57 58 // Helper functions to get individual items from an entry in the cache. 59 Code GetCodeFromEntry(int index); 60 SharedFunctionInfo GetSFIFromEntry(int index); 61 BailoutId GetBailoutIdFromEntry(int index); 62 63 inline int FindEntry(Handle<SharedFunctionInfo> shared, BailoutId osr_offset); 64 inline void ClearEntry(int src, Isolate* isolate); 65 inline void InitializeEntry(int entry, SharedFunctionInfo shared, Code code, 66 BailoutId osr_offset); 67 inline void MoveEntry(int src, int dst, Isolate* isolate); 68 69 OBJECT_CONSTRUCTORS(OSROptimizedCodeCache, WeakFixedArray); 70 }; 71 72 } // namespace internal 73 } // namespace v8 74 75 #include "src/objects/object-macros-undef.h" 76 77 #endif // V8_OBJECTS_OSR_OPTIMIZED_CODE_CACHE_H_ 78