Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 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 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 17 | #include <fstream> |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
| 20 | #include "builder.h" |
| 21 | #include "code_generator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 22 | #include "compilers.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 23 | #include "driver/compiler_driver.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 24 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 25 | #include "graph_visualizer.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 26 | #include "nodes.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 27 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 28 | #include "utils/arena_allocator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 32 | /** |
| 33 | * Used by the code generator, to allocate the code in a vector. |
| 34 | */ |
| 35 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 36 | public: |
| 37 | CodeVectorAllocator() { } |
| 38 | |
| 39 | virtual uint8_t* Allocate(size_t size) { |
| 40 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 41 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 42 | return &memory_[0]; |
| 43 | } |
| 44 | |
| 45 | size_t GetSize() const { return size_; } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 46 | const std::vector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | std::vector<uint8_t> memory_; |
| 50 | size_t size_; |
| 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 53 | }; |
| 54 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 55 | /** |
| 56 | * If set to true, generates a file suitable for the c1visualizer tool and IRHydra. |
| 57 | */ |
| 58 | static bool kIsVisualizerEnabled = false; |
| 59 | |
| 60 | /** |
| 61 | * Filter to apply to the visualizer. Methods whose name contain that filter will |
| 62 | * be in the file. |
| 63 | */ |
| 64 | static const char* kStringFilter = ""; |
| 65 | |
| 66 | OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) : QuickCompiler(driver) { |
| 67 | if (kIsVisualizerEnabled) { |
| 68 | visualizer_output_.reset(new std::ofstream("art.cfg")); |
| 69 | } |
| 70 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 71 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 72 | CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 73 | uint32_t access_flags, |
| 74 | InvokeType invoke_type, |
| 75 | uint16_t class_def_idx, |
| 76 | uint32_t method_idx, |
| 77 | jobject class_loader, |
| 78 | const DexFile& dex_file) const { |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 79 | DexCompilationUnit dex_compilation_unit( |
| 80 | nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 81 | class_def_idx, method_idx, access_flags, |
| 82 | GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx)); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 83 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 84 | // For testing purposes, we put a special marker on method names that should be compiled |
| 85 | // with this compiler. This makes sure we're not regressing. |
| 86 | bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos; |
| 87 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 88 | ArenaPool pool; |
| 89 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 90 | HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 91 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 92 | HGraph* graph = builder.BuildGraph(*code_item); |
| 93 | if (graph == nullptr) { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 94 | if (shouldCompile) { |
| 95 | LOG(FATAL) << "Could not build graph in optimizing compiler"; |
| 96 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 97 | return nullptr; |
| 98 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 99 | HGraphVisualizer visualizer(visualizer_output_.get(), graph, kStringFilter, dex_compilation_unit); |
| 100 | visualizer.DumpGraph("builder"); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 101 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 102 | InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet(); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 103 | // The optimizing compiler currently does not have a Thumb2 assembler. |
| 104 | if (instruction_set == kThumb2) { |
| 105 | instruction_set = kArm; |
| 106 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 107 | CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); |
| 108 | if (codegen == nullptr) { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 109 | if (shouldCompile) { |
| 110 | LOG(FATAL) << "Could not find code generator for optimizing compiler"; |
| 111 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 112 | return nullptr; |
| 113 | } |
| 114 | |
| 115 | CodeVectorAllocator allocator; |
| 116 | codegen->Compile(&allocator); |
| 117 | |
| 118 | std::vector<uint8_t> mapping_table; |
| 119 | codegen->BuildMappingTable(&mapping_table); |
| 120 | std::vector<uint8_t> vmap_table; |
| 121 | codegen->BuildVMapTable(&vmap_table); |
| 122 | std::vector<uint8_t> gc_map; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 123 | codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 124 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 125 | // Run these phases to get some test coverage. |
| 126 | graph->BuildDominatorTree(); |
| 127 | graph->TransformToSSA(); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 128 | visualizer.DumpGraph("ssa"); |
| 129 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 130 | graph->FindNaturalLoops(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 131 | SsaLivenessAnalysis(*graph).Analyze(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 132 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 133 | return new CompiledMethod(GetCompilerDriver(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 134 | instruction_set, |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 135 | allocator.GetMemory(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 136 | codegen->GetFrameSize(), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 137 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 138 | 0, /* FPR spill mask, unused */ |
| 139 | mapping_table, |
| 140 | vmap_table, |
| 141 | gc_map, |
| 142 | nullptr); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace art |