code_generator.h revision 988939683c26c0b1c8808fc206add6337319509a
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ 18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ 19 20#include "arch/instruction_set.h" 21#include "arch/instruction_set_features.h" 22#include "base/bit_field.h" 23#include "driver/compiler_options.h" 24#include "globals.h" 25#include "locations.h" 26#include "memory_region.h" 27#include "nodes.h" 28#include "stack_map_stream.h" 29 30namespace art { 31 32static size_t constexpr kVRegSize = 4; 33static size_t constexpr kUninitializedFrameSize = 0; 34 35// Binary encoding of 2^32 for type double. 36static int64_t constexpr k2Pow32EncodingForDouble = INT64_C(0x41F0000000000000); 37// Binary encoding of 2^31 for type double. 38static int64_t constexpr k2Pow31EncodingForDouble = INT64_C(0x41E0000000000000); 39 40// Maximum value for a primitive integer. 41static int32_t constexpr kPrimIntMax = 0x7fffffff; 42// Maximum value for a primitive long. 43static int64_t constexpr kPrimLongMax = 0x7fffffffffffffff; 44 45class Assembler; 46class CodeGenerator; 47class DexCompilationUnit; 48class ParallelMoveResolver; 49class SrcMapElem; 50template <class Alloc> 51class SrcMap; 52using DefaultSrcMap = SrcMap<std::allocator<SrcMapElem>>; 53 54class CodeAllocator { 55 public: 56 CodeAllocator() {} 57 virtual ~CodeAllocator() {} 58 59 virtual uint8_t* Allocate(size_t size) = 0; 60 61 private: 62 DISALLOW_COPY_AND_ASSIGN(CodeAllocator); 63}; 64 65struct PcInfo { 66 uint32_t dex_pc; 67 uintptr_t native_pc; 68}; 69 70class SlowPathCode : public ArenaObject<kArenaAllocSlowPaths> { 71 public: 72 SlowPathCode() {} 73 virtual ~SlowPathCode() {} 74 75 virtual void EmitNativeCode(CodeGenerator* codegen) = 0; 76 77 private: 78 DISALLOW_COPY_AND_ASSIGN(SlowPathCode); 79}; 80 81class CodeGenerator { 82 public: 83 // Compiles the graph to executable instructions. Returns whether the compilation 84 // succeeded. 85 void CompileBaseline(CodeAllocator* allocator, bool is_leaf = false); 86 void CompileOptimized(CodeAllocator* allocator); 87 static CodeGenerator* Create(HGraph* graph, 88 InstructionSet instruction_set, 89 const InstructionSetFeatures& isa_features, 90 const CompilerOptions& compiler_options); 91 virtual ~CodeGenerator() {} 92 93 HGraph* GetGraph() const { return graph_; } 94 95 bool GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const; 96 97 size_t GetStackSlotOfParameter(HParameterValue* parameter) const { 98 // Note that this follows the current calling convention. 99 return GetFrameSize() 100 + kVRegSize // Art method 101 + parameter->GetIndex() * kVRegSize; 102 } 103 104 virtual void Initialize() = 0; 105 virtual void Finalize(CodeAllocator* allocator); 106 virtual void GenerateFrameEntry() = 0; 107 virtual void GenerateFrameExit() = 0; 108 virtual void Bind(HBasicBlock* block) = 0; 109 virtual void Move(HInstruction* instruction, Location location, HInstruction* move_for) = 0; 110 virtual HGraphVisitor* GetLocationBuilder() = 0; 111 virtual HGraphVisitor* GetInstructionVisitor() = 0; 112 virtual Assembler* GetAssembler() = 0; 113 virtual size_t GetWordSize() const = 0; 114 virtual size_t GetFloatingPointSpillSlotSize() const = 0; 115 virtual uintptr_t GetAddressOf(HBasicBlock* block) const = 0; 116 void ComputeFrameSize(size_t number_of_spill_slots, 117 size_t maximum_number_of_live_core_registers, 118 size_t maximum_number_of_live_fp_registers, 119 size_t number_of_out_slots); 120 virtual size_t FrameEntrySpillSize() const = 0; 121 int32_t GetStackSlot(HLocal* local) const; 122 Location GetTemporaryLocation(HTemporary* temp) const; 123 124 uint32_t GetFrameSize() const { return frame_size_; } 125 void SetFrameSize(uint32_t size) { frame_size_ = size; } 126 uint32_t GetCoreSpillMask() const { return core_spill_mask_; } 127 128 size_t GetNumberOfCoreRegisters() const { return number_of_core_registers_; } 129 size_t GetNumberOfFloatingPointRegisters() const { return number_of_fpu_registers_; } 130 virtual void SetupBlockedRegisters(bool is_baseline) const = 0; 131 132 virtual void DumpCoreRegister(std::ostream& stream, int reg) const = 0; 133 virtual void DumpFloatingPointRegister(std::ostream& stream, int reg) const = 0; 134 virtual InstructionSet GetInstructionSet() const = 0; 135 136 const CompilerOptions& GetCompilerOptions() const { return compiler_options_; } 137 138 // Saves the register in the stack. Returns the size taken on stack. 139 virtual size_t SaveCoreRegister(size_t stack_index, uint32_t reg_id) = 0; 140 // Restores the register from the stack. Returns the size taken on stack. 141 virtual size_t RestoreCoreRegister(size_t stack_index, uint32_t reg_id) = 0; 142 virtual size_t SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) { 143 UNUSED(stack_index, reg_id); 144 UNIMPLEMENTED(FATAL); 145 UNREACHABLE(); 146 } 147 virtual size_t RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) { 148 UNUSED(stack_index, reg_id); 149 UNIMPLEMENTED(FATAL); 150 UNREACHABLE(); 151 } 152 virtual bool NeedsTwoRegisters(Primitive::Type type) const = 0; 153 154 bool IsCoreCalleeSaveRegister(int reg) const { 155 return (core_callee_save_mask_ & (1 << reg)) != 0; 156 } 157 158 bool IsFloatingPointCalleeSaveRegister(int reg) const { 159 return (fpu_callee_save_mask_ & (1 << reg)) != 0; 160 } 161 162 void RecordPcInfo(HInstruction* instruction, uint32_t dex_pc); 163 bool CanMoveNullCheckToUser(HNullCheck* null_check); 164 void MaybeRecordImplicitNullCheck(HInstruction* instruction); 165 166 void AddSlowPath(SlowPathCode* slow_path) { 167 slow_paths_.Add(slow_path); 168 } 169 170 void GenerateSlowPaths(); 171 172 void BuildMappingTable(std::vector<uint8_t>* vector, DefaultSrcMap* src_map) const; 173 void BuildVMapTable(std::vector<uint8_t>* vector) const; 174 void BuildNativeGCMap( 175 std::vector<uint8_t>* vector, const DexCompilationUnit& dex_compilation_unit) const; 176 void BuildStackMaps(std::vector<uint8_t>* vector); 177 void SaveLiveRegisters(LocationSummary* locations); 178 void RestoreLiveRegisters(LocationSummary* locations); 179 180 bool IsLeafMethod() const { 181 return is_leaf_; 182 } 183 184 void MarkNotLeaf() { 185 is_leaf_ = false; 186 } 187 188 // Clears the spill slots taken by loop phis in the `LocationSummary` of the 189 // suspend check. This is called when the code generator generates code 190 // for the suspend check at the back edge (instead of where the suspend check 191 // is, which is the loop entry). At this point, the spill slots for the phis 192 // have not been written to. 193 void ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const; 194 195 bool* GetBlockedCoreRegisters() const { return blocked_core_registers_; } 196 bool* GetBlockedFloatingPointRegisters() const { return blocked_fpu_registers_; } 197 198 // Helper that returns the pointer offset of an index in an object array. 199 // Note: this method assumes we always have the same pointer size, regardless 200 // of the architecture. 201 static size_t GetCacheOffset(uint32_t index); 202 203 void EmitParallelMoves(Location from1, Location to1, Location from2, Location to2); 204 205 static bool StoreNeedsWriteBarrier(Primitive::Type type, HInstruction* value) { 206 if (kIsDebugBuild) { 207 if (type == Primitive::kPrimNot && value->IsIntConstant()) { 208 CHECK_EQ(value->AsIntConstant()->GetValue(), 0); 209 } 210 } 211 return type == Primitive::kPrimNot && !value->IsIntConstant(); 212 } 213 214 void AddAllocatedRegister(Location location) { 215 allocated_registers_.Add(location); 216 } 217 218 protected: 219 CodeGenerator(HGraph* graph, 220 size_t number_of_core_registers, 221 size_t number_of_fpu_registers, 222 size_t number_of_register_pairs, 223 uint32_t core_callee_save_mask, 224 uint32_t fpu_callee_save_mask, 225 const CompilerOptions& compiler_options) 226 : frame_size_(kUninitializedFrameSize), 227 core_spill_mask_(0), 228 first_register_slot_in_slow_path_(0), 229 blocked_core_registers_(graph->GetArena()->AllocArray<bool>(number_of_core_registers)), 230 blocked_fpu_registers_(graph->GetArena()->AllocArray<bool>(number_of_fpu_registers)), 231 blocked_register_pairs_(graph->GetArena()->AllocArray<bool>(number_of_register_pairs)), 232 number_of_core_registers_(number_of_core_registers), 233 number_of_fpu_registers_(number_of_fpu_registers), 234 number_of_register_pairs_(number_of_register_pairs), 235 core_callee_save_mask_(core_callee_save_mask), 236 fpu_callee_save_mask_(fpu_callee_save_mask), 237 graph_(graph), 238 compiler_options_(compiler_options), 239 pc_infos_(graph->GetArena(), 32), 240 slow_paths_(graph->GetArena(), 8), 241 is_leaf_(true), 242 stack_map_stream_(graph->GetArena()) {} 243 244 // Register allocation logic. 245 void AllocateRegistersLocally(HInstruction* instruction) const; 246 247 // Backend specific implementation for allocating a register. 248 virtual Location AllocateFreeRegister(Primitive::Type type) const = 0; 249 250 static size_t FindFreeEntry(bool* array, size_t length); 251 static size_t FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length); 252 253 virtual Location GetStackLocation(HLoadLocal* load) const = 0; 254 255 virtual ParallelMoveResolver* GetMoveResolver() = 0; 256 257 // Frame size required for this method. 258 uint32_t frame_size_; 259 uint32_t core_spill_mask_; 260 uint32_t first_register_slot_in_slow_path_; 261 262 // Registers that were allocated during linear scan. 263 RegisterSet allocated_registers_; 264 265 // Arrays used when doing register allocation to know which 266 // registers we can allocate. `SetupBlockedRegisters` updates the 267 // arrays. 268 bool* const blocked_core_registers_; 269 bool* const blocked_fpu_registers_; 270 bool* const blocked_register_pairs_; 271 size_t number_of_core_registers_; 272 size_t number_of_fpu_registers_; 273 size_t number_of_register_pairs_; 274 const uint32_t core_callee_save_mask_; 275 const uint32_t fpu_callee_save_mask_; 276 277 private: 278 void InitLocations(HInstruction* instruction); 279 size_t GetStackOffsetOfSavedRegister(size_t index); 280 281 HGraph* const graph_; 282 const CompilerOptions& compiler_options_; 283 284 GrowableArray<PcInfo> pc_infos_; 285 GrowableArray<SlowPathCode*> slow_paths_; 286 287 bool is_leaf_; 288 289 StackMapStream stack_map_stream_; 290 291 DISALLOW_COPY_AND_ASSIGN(CodeGenerator); 292}; 293 294template <typename C, typename F> 295class CallingConvention { 296 public: 297 CallingConvention(const C* registers, 298 size_t number_of_registers, 299 const F* fpu_registers, 300 size_t number_of_fpu_registers) 301 : registers_(registers), 302 number_of_registers_(number_of_registers), 303 fpu_registers_(fpu_registers), 304 number_of_fpu_registers_(number_of_fpu_registers) {} 305 306 size_t GetNumberOfRegisters() const { return number_of_registers_; } 307 size_t GetNumberOfFpuRegisters() const { return number_of_fpu_registers_; } 308 309 C GetRegisterAt(size_t index) const { 310 DCHECK_LT(index, number_of_registers_); 311 return registers_[index]; 312 } 313 314 F GetFpuRegisterAt(size_t index) const { 315 DCHECK_LT(index, number_of_fpu_registers_); 316 return fpu_registers_[index]; 317 } 318 319 size_t GetStackOffsetOf(size_t index) const { 320 // We still reserve the space for parameters passed by registers. 321 // Add one for the method pointer. 322 return (index + 1) * kVRegSize; 323 } 324 325 private: 326 const C* registers_; 327 const size_t number_of_registers_; 328 const F* fpu_registers_; 329 const size_t number_of_fpu_registers_; 330 331 DISALLOW_COPY_AND_ASSIGN(CallingConvention); 332}; 333 334} // namespace art 335 336#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ 337