| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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_TOOLS_DEXANALYZE_DEXANALYZE_BYTECODE_H_ |
| 18 | #define ART_TOOLS_DEXANALYZE_DEXANALYZE_BYTECODE_H_ |
| 19 | |
| Mathieu Chartier | 8a0ede9 | 2018-07-12 10:24:04 -0700 | [diff] [blame] | 20 | #include <array> |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | #include <map> |
| 23 | |
| 24 | #include "base/safe_map.h" |
| 25 | #include "dexanalyze_experiments.h" |
| 26 | #include "dex/code_item_accessors.h" |
| Andreas Gampe | 7458a7a | 2019-01-02 10:32:11 -0800 | [diff] [blame] | 27 | #include "dex/dex_file_types.h" |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | namespace dexanalyze { |
| 31 | |
| 32 | enum BytecodeExperiment { |
| 33 | kExperimentInvoke, |
| 34 | kExperimentInstanceField, |
| 35 | kExperimentInstanceFieldSelf, |
| 36 | kExperimentStaticField, |
| 37 | kExperimentLocalType, |
| 38 | kExperimentReturn, |
| 39 | kExperimentSmallIf, |
| 40 | kExperimentString, |
| 41 | kExperimentSingleGetSet, |
| 42 | }; |
| 43 | |
| 44 | // Maps from global index to local index. |
| 45 | struct TypeLinkage { |
| 46 | // Referenced types. |
| 47 | SafeMap<size_t, size_t> types_; |
| 48 | // Owned fields. |
| 49 | SafeMap<size_t, size_t> fields_; |
| 50 | // Owned methods. |
| 51 | SafeMap<size_t, size_t> methods_; |
| 52 | // Referenced strings. |
| 53 | SafeMap<size_t, size_t> strings_; |
| 54 | }; |
| 55 | |
| Mathieu Chartier | fbc8003 | 2018-07-11 11:20:08 -0700 | [diff] [blame] | 56 | class NewRegisterInstructions : public Experiment { |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 57 | public: |
| Mathieu Chartier | cd2edfc | 2018-08-19 15:38:32 -0700 | [diff] [blame] | 58 | explicit NewRegisterInstructions(uint64_t experiments) |
| 59 | : experiments_(experiments), |
| 60 | move_result_reg_(256), |
| 61 | first_arg_reg_count_(256), |
| 62 | opcode_count_(256) {} |
| Mathieu Chartier | fbc8003 | 2018-07-11 11:20:08 -0700 | [diff] [blame] | 63 | void ProcessDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files); |
| 64 | void Dump(std::ostream& os, uint64_t total_size) const; |
| 65 | |
| 66 | void ProcessCodeItem(const DexFile& dex_file, |
| 67 | const CodeItemDataAccessor& code_item, |
| 68 | dex::TypeIndex current_class_type, |
| 69 | bool count_types, |
| 70 | std::map<size_t, TypeLinkage>& types); |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 71 | void Add(Instruction::Code opcode, const Instruction& inst); |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 72 | bool InstNibbles(uint8_t opcode, const std::vector<uint32_t>& args); |
| Mathieu Chartier | cd2edfc | 2018-08-19 15:38:32 -0700 | [diff] [blame] | 73 | bool ExtendPrefix(uint32_t* value1, uint32_t* value2); |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 74 | bool Enabled(BytecodeExperiment experiment) const { |
| 75 | return experiments_ & (1u << static_cast<uint64_t>(experiment)); |
| 76 | } |
| 77 | |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 78 | private: |
| Mathieu Chartier | fbc8003 | 2018-07-11 11:20:08 -0700 | [diff] [blame] | 79 | size_t alignment_ = 1u; |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 80 | uint64_t output_size_ = 0u; |
| 81 | uint64_t deduped_size_ = 0u; |
| 82 | uint64_t dex_code_bytes_ = 0u; |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 83 | uint64_t experiments_ = std::numeric_limits<uint64_t>::max(); |
| Mathieu Chartier | cd2edfc | 2018-08-19 15:38:32 -0700 | [diff] [blame] | 84 | uint64_t extended_field_ = 0u; |
| 85 | uint64_t extended_method_ = 0u; |
| 86 | std::vector<size_t> move_result_reg_; |
| 87 | std::vector<size_t> first_arg_reg_count_; |
| 88 | std::vector<size_t> opcode_count_; |
| 89 | std::map<std::pair<uint32_t, uint32_t>, size_t> method_linkage_counts_; |
| 90 | std::map<std::pair<uint32_t, uint32_t>, size_t> field_linkage_counts_; |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 91 | std::map<std::vector<uint8_t>, size_t> instruction_freq_; |
| Mathieu Chartier | fbc8003 | 2018-07-11 11:20:08 -0700 | [diff] [blame] | 92 | // Output instruction buffer. |
| 93 | std::vector<uint8_t> buffer_; |
| Mathieu Chartier | 12dd8a9 | 2018-06-12 11:17:22 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace dexanalyze |
| 97 | } // namespace art |
| 98 | |
| 99 | #endif // ART_TOOLS_DEXANALYZE_DEXANALYZE_BYTECODE_H_ |