optimizing_compiler.cc revision 804d09372cc3d80d537da1489da4a45e0e19aa5d
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" 25804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h" 26787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "utils/arena_allocator.h" 27b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 28b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 29b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 30787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/** 31787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector. 32787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */ 33787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator { 34787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public: 35787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeVectorAllocator() { } 36787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 37787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 38787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_ = size; 3992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray memory_.resize(size); 40787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return &memory_[0]; 41787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 42787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 43787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t GetSize() const { return size_; } 4492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray const std::vector<uint8_t>& GetMemory() const { return memory_; } 45787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 46787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private: 47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> memory_; 48787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t size_; 49787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 50787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 51787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray}; 52787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 53787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 54b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas GeoffrayCompiledMethod* OptimizingCompiler::TryCompile(CompilerDriver& driver, 55b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const DexFile::CodeItem* code_item, 56b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t access_flags, 57b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray InvokeType invoke_type, 58b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint16_t class_def_idx, 59b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t method_idx, 60b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray jobject class_loader, 61b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const DexFile& dex_file) const { 6292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 6392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, 6492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray class_def_idx, method_idx, access_flags, driver.GetVerifiedMethod(&dex_file, method_idx)); 6592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 668ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // For testing purposes, we put a special marker on method names that should be compiled 678ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // with this compiler. This makes sure we're not regressing. 688ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos; 698ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray 70787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray ArenaPool pool; 71787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray ArenaAllocator arena(&pool); 728ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file); 73787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray HGraph* graph = builder.BuildGraph(*code_item); 74787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray if (graph == nullptr) { 758ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray if (shouldCompile) { 768ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray LOG(FATAL) << "Could not build graph in optimizing compiler"; 778ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray } 78787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 79787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 80787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 81787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray InstructionSet instruction_set = driver.GetInstructionSet(); 828ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // The optimizing compiler currently does not have a Thumb2 assembler. 838ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray if (instruction_set == kThumb2) { 848ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray instruction_set = kArm; 858ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray } 86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); 87787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray if (codegen == nullptr) { 888ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray if (shouldCompile) { 898ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray LOG(FATAL) << "Could not find code generator for optimizing compiler"; 908ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray } 91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 92787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 93787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeVectorAllocator allocator; 95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray codegen->Compile(&allocator); 96787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 97787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> mapping_table; 98787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray codegen->BuildMappingTable(&mapping_table); 99787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> vmap_table; 100787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray codegen->BuildVMapTable(&vmap_table); 101787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> gc_map; 10292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); 103787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 104c32e770f21540e4e9eda6dc7f770e745d33f1b9fNicolas Geoffray // Run these phases to get some test coverage. 105c32e770f21540e4e9eda6dc7f770e745d33f1b9fNicolas Geoffray graph->BuildDominatorTree(); 106c32e770f21540e4e9eda6dc7f770e745d33f1b9fNicolas Geoffray graph->TransformToSSA(); 107804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray SsaLivenessAnalysis(*graph).Analyze(); 108c32e770f21540e4e9eda6dc7f770e745d33f1b9fNicolas Geoffray 109787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return new CompiledMethod(driver, 110787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray instruction_set, 11192cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray allocator.GetMemory(), 112787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray codegen->GetFrameSize(), 1138ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray codegen->GetCoreSpillMask(), 114787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 0, /* FPR spill mask, unused */ 115787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray mapping_table, 116787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray vmap_table, 117787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray gc_map, 118787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray nullptr); 119b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 120b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 121b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 122