code_generator.h revision 77520bca97ec44e3758510cebd0f20e3bb4584ea
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() 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 void RecordPcInfo(HInstruction* instruction, uint32_t dex_pc); 155 bool CanMoveNullCheckToUser(HNullCheck* null_check); 156 void MaybeRecordImplicitNullCheck(HInstruction* instruction); 157 158 void AddSlowPath(SlowPathCode* slow_path) { 159 slow_paths_.Add(slow_path); 160 } 161 162 void GenerateSlowPaths(); 163 164 void BuildMappingTable(std::vector<uint8_t>* vector, DefaultSrcMap* src_map) const; 165 void BuildVMapTable(std::vector<uint8_t>* vector) const; 166 void BuildNativeGCMap( 167 std::vector<uint8_t>* vector, const DexCompilationUnit& dex_compilation_unit) const; 168 void BuildStackMaps(std::vector<uint8_t>* vector); 169 void SaveLiveRegisters(LocationSummary* locations); 170 void RestoreLiveRegisters(LocationSummary* locations); 171 172 bool IsLeafMethod() const { 173 return is_leaf_; 174 } 175 176 void MarkNotLeaf() { 177 is_leaf_ = false; 178 } 179 180 // Clears the spill slots taken by loop phis in the `LocationSummary` of the 181 // suspend check. This is called when the code generator generates code 182 // for the suspend check at the back edge (instead of where the suspend check 183 // is, which is the loop entry). At this point, the spill slots for the phis 184 // have not been written to. 185 void ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const; 186 187 bool* GetBlockedCoreRegisters() const { return blocked_core_registers_; } 188 bool* GetBlockedFloatingPointRegisters() const { return blocked_fpu_registers_; } 189 190 // Helper that returns the pointer offset of an index in an object array. 191 // Note: this method assumes we always have the same pointer size, regardless 192 // of the architecture. 193 static size_t GetCacheOffset(uint32_t index); 194 195 void EmitParallelMoves(Location from1, Location to1, Location from2, Location to2); 196 197 static bool StoreNeedsWriteBarrier(Primitive::Type type, HInstruction* value) { 198 if (kIsDebugBuild) { 199 if (type == Primitive::kPrimNot && value->IsIntConstant()) { 200 CHECK_EQ(value->AsIntConstant()->GetValue(), 0); 201 } 202 } 203 return type == Primitive::kPrimNot && !value->IsIntConstant(); 204 } 205 206 protected: 207 CodeGenerator(HGraph* graph, 208 size_t number_of_core_registers, 209 size_t number_of_fpu_registers, 210 size_t number_of_register_pairs, 211 const CompilerOptions& compiler_options) 212 : frame_size_(kUninitializedFrameSize), 213 core_spill_mask_(0), 214 first_register_slot_in_slow_path_(0), 215 blocked_core_registers_(graph->GetArena()->AllocArray<bool>(number_of_core_registers)), 216 blocked_fpu_registers_(graph->GetArena()->AllocArray<bool>(number_of_fpu_registers)), 217 blocked_register_pairs_(graph->GetArena()->AllocArray<bool>(number_of_register_pairs)), 218 number_of_core_registers_(number_of_core_registers), 219 number_of_fpu_registers_(number_of_fpu_registers), 220 number_of_register_pairs_(number_of_register_pairs), 221 graph_(graph), 222 compiler_options_(compiler_options), 223 pc_infos_(graph->GetArena(), 32), 224 slow_paths_(graph->GetArena(), 8), 225 is_leaf_(true), 226 stack_map_stream_(graph->GetArena()) {} 227 228 // Register allocation logic. 229 void AllocateRegistersLocally(HInstruction* instruction) const; 230 231 // Backend specific implementation for allocating a register. 232 virtual Location AllocateFreeRegister(Primitive::Type type) const = 0; 233 234 static size_t FindFreeEntry(bool* array, size_t length); 235 static size_t FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length); 236 237 virtual Location GetStackLocation(HLoadLocal* load) const = 0; 238 239 virtual ParallelMoveResolver* GetMoveResolver() = 0; 240 241 // Frame size required for this method. 242 uint32_t frame_size_; 243 uint32_t core_spill_mask_; 244 uint32_t first_register_slot_in_slow_path_; 245 246 // Arrays used when doing register allocation to know which 247 // registers we can allocate. `SetupBlockedRegisters` updates the 248 // arrays. 249 bool* const blocked_core_registers_; 250 bool* const blocked_fpu_registers_; 251 bool* const blocked_register_pairs_; 252 size_t number_of_core_registers_; 253 size_t number_of_fpu_registers_; 254 size_t number_of_register_pairs_; 255 256 private: 257 void InitLocations(HInstruction* instruction); 258 size_t GetStackOffsetOfSavedRegister(size_t index); 259 260 HGraph* const graph_; 261 const CompilerOptions& compiler_options_; 262 263 GrowableArray<PcInfo> pc_infos_; 264 GrowableArray<SlowPathCode*> slow_paths_; 265 266 bool is_leaf_; 267 268 StackMapStream stack_map_stream_; 269 270 DISALLOW_COPY_AND_ASSIGN(CodeGenerator); 271}; 272 273template <typename C, typename F> 274class CallingConvention { 275 public: 276 CallingConvention(const C* registers, 277 size_t number_of_registers, 278 const F* fpu_registers, 279 size_t number_of_fpu_registers) 280 : registers_(registers), 281 number_of_registers_(number_of_registers), 282 fpu_registers_(fpu_registers), 283 number_of_fpu_registers_(number_of_fpu_registers) {} 284 285 size_t GetNumberOfRegisters() const { return number_of_registers_; } 286 size_t GetNumberOfFpuRegisters() const { return number_of_fpu_registers_; } 287 288 C GetRegisterAt(size_t index) const { 289 DCHECK_LT(index, number_of_registers_); 290 return registers_[index]; 291 } 292 293 F GetFpuRegisterAt(size_t index) const { 294 DCHECK_LT(index, number_of_fpu_registers_); 295 return fpu_registers_[index]; 296 } 297 298 size_t GetStackOffsetOf(size_t index) const { 299 // We still reserve the space for parameters passed by registers. 300 // Add one for the method pointer. 301 return (index + 1) * kVRegSize; 302 } 303 304 private: 305 const C* registers_; 306 const size_t number_of_registers_; 307 const F* fpu_registers_; 308 const size_t number_of_fpu_registers_; 309 310 DISALLOW_COPY_AND_ASSIGN(CallingConvention); 311}; 312 313} // namespace art 314 315#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ 316