optimizing_compiler.cc revision 8ccc3f5d06fd217cdaabd37e743adab2031d3720
1b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray/* 2b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project 3b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 4b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License"); 5b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * you may not use this file except in compliance with the License. 6b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * You may obtain a copy of the License at 7b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 8b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * http://www.apache.org/licenses/LICENSE-2.0 9b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 10b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software 11b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS, 12b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * See the License for the specific language governing permissions and 14b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * limitations under the License. 15b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray */ 16b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 17787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include <stdint.h> 18787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 19787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h" 20787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h" 21b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray#include "compilers.h" 22787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h" 2392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h" 24787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h" 25787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "utils/arena_allocator.h" 26b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 27b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 28b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 29787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/** 30787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector. 31787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */ 32787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator { 33787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public: 34787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeVectorAllocator() { } 35787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 36787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 37787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_ = size; 3892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray memory_.resize(size); 39787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return &memory_[0]; 40787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 41787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 42787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t GetSize() const { return size_; } 4392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray const std::vector<uint8_t>& GetMemory() const { return memory_; } 44787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 45787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private: 46787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> memory_; 47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t size_; 48787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 49787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 50787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray}; 51787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 52787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 53b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas GeoffrayCompiledMethod* OptimizingCompiler::TryCompile(CompilerDriver& driver, 54b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const DexFile::CodeItem* code_item, 55b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t access_flags, 56b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray InvokeType invoke_type, 57b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint16_t class_def_idx, 58b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t method_idx, 59b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray jobject class_loader, 60b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const DexFile& dex_file) const { 6192cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 6292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, 6392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray class_def_idx, method_idx, access_flags, driver.GetVerifiedMethod(&dex_file, method_idx)); 6492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 658ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // For testing purposes, we put a special marker on method names that should be compiled 668ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // with this compiler. This makes sure we're not regressing. 678ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos; 688ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray 69787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray ArenaPool pool; 70787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray ArenaAllocator arena(&pool); 718ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file); 72787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray HGraph* graph = builder.BuildGraph(*code_item); 73787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray if (graph == nullptr) { 748ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray if (shouldCompile) { 758ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray LOG(FATAL) << "Could not build graph in optimizing compiler"; 768ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray } 77787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 78787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 79787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 80787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray InstructionSet instruction_set = driver.GetInstructionSet(); 818ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // The optimizing compiler currently does not have a Thumb2 assembler. 828ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray if (instruction_set == kThumb2) { 838ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray instruction_set = kArm; 848ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray } 85787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); 86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray if (codegen == nullptr) { 878ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray if (shouldCompile) { 888ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray LOG(FATAL) << "Could not find code generator for optimizing compiler"; 898ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray } 90787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 92787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 93787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeVectorAllocator allocator; 94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray codegen->Compile(&allocator); 95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 96787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> mapping_table; 97787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray codegen->BuildMappingTable(&mapping_table); 98787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> vmap_table; 99787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray codegen->BuildVMapTable(&vmap_table); 100787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> gc_map; 10192cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); 102787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 103787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return new CompiledMethod(driver, 104787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray instruction_set, 10592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray allocator.GetMemory(), 106787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray codegen->GetFrameSize(), 1078ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray codegen->GetCoreSpillMask(), 108787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 0, /* FPR spill mask, unused */ 109787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray mapping_table, 110787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray vmap_table, 111787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray gc_map, 112787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray nullptr); 113b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 114b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 115b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 116