buzbee | 2502e00 | 2012-12-31 16:05:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
buzbee | 395116c | 2013-02-27 14:30:25 -0800 | [diff] [blame] | 17 | #ifndef ART_SRC_COMPILER_DEX_BBOPT_H_ |
| 18 | #define ART_SRC_COMPILER_DEX_BBOPT_H_ |
buzbee | 2502e00 | 2012-12-31 16:05:53 -0800 | [diff] [blame] | 19 | |
| 20 | #include "compiler_internals.h" |
| 21 | |
| 22 | #define NO_VALUE 0xffff |
| 23 | #define ARRAY_REF 0xfffe |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // Key is s_reg, value is value name. |
| 28 | typedef SafeMap<uint16_t, uint16_t> SregValueMap; |
| 29 | // Key is concatenation of quad, value is value name. |
| 30 | typedef SafeMap<uint64_t, uint16_t> ValueMap; |
| 31 | // Key represents a memory address, value is generation. |
| 32 | typedef SafeMap<uint32_t, uint16_t> MemoryVersionMap; |
| 33 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 34 | class LocalValueNumbering { |
buzbee | 2502e00 | 2012-12-31 16:05:53 -0800 | [diff] [blame] | 35 | public: |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame^] | 36 | LocalValueNumbering(CompilationUnit* cu) : cu_(cu) {}; |
buzbee | 2502e00 | 2012-12-31 16:05:53 -0800 | [diff] [blame] | 37 | |
| 38 | uint64_t BuildKey(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) |
| 39 | { |
| 40 | return (static_cast<uint64_t>(op) << 48 | static_cast<uint64_t>(operand1) << 32 | |
| 41 | static_cast<uint64_t>(operand2) << 16 | static_cast<uint64_t>(modifier)); |
| 42 | }; |
| 43 | |
| 44 | uint16_t LookupValue(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) |
| 45 | { |
| 46 | uint16_t res; |
| 47 | uint64_t key = BuildKey(op, operand1, operand2, modifier); |
| 48 | ValueMap::iterator it = value_map_.find(key); |
| 49 | if (it != value_map_.end()) { |
| 50 | res = it->second; |
| 51 | } else { |
| 52 | res = value_map_.size() + 1; |
| 53 | value_map_.Put(key, res); |
| 54 | } |
| 55 | return res; |
| 56 | }; |
| 57 | |
| 58 | bool ValueExists(uint16_t op, uint16_t operand1, uint16_t operand2, uint16_t modifier) |
| 59 | { |
| 60 | uint64_t key = BuildKey(op, operand1, operand2, modifier); |
| 61 | ValueMap::iterator it = value_map_.find(key); |
| 62 | return (it != value_map_.end()); |
| 63 | }; |
| 64 | |
| 65 | uint16_t GetMemoryVersion(uint16_t base, uint16_t field) |
| 66 | { |
| 67 | uint32_t key = (base << 16) | field; |
| 68 | uint16_t res; |
| 69 | MemoryVersionMap::iterator it = memory_version_map_.find(key); |
| 70 | if (it == memory_version_map_.end()) { |
| 71 | res = 0; |
| 72 | memory_version_map_.Put(key, res); |
| 73 | } else { |
| 74 | res = it->second; |
| 75 | } |
| 76 | return res; |
| 77 | }; |
| 78 | |
| 79 | void AdvanceMemoryVersion(uint16_t base, uint16_t field) |
| 80 | { |
| 81 | uint32_t key = (base << 16) | field; |
| 82 | MemoryVersionMap::iterator it = memory_version_map_.find(key); |
| 83 | if (it == memory_version_map_.end()) { |
| 84 | memory_version_map_.Put(key, 0); |
| 85 | } else { |
| 86 | it->second++; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | void SetOperandValue(uint16_t s_reg, uint16_t value) |
| 91 | { |
| 92 | SregValueMap::iterator it = sreg_value_map_.find(s_reg); |
| 93 | if (it != sreg_value_map_.end()) { |
| 94 | DCHECK_EQ(it->second, value); |
| 95 | } else { |
| 96 | sreg_value_map_.Put(s_reg, value); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | uint16_t GetOperandValue(int s_reg) |
| 101 | { |
| 102 | uint16_t res = NO_VALUE; |
| 103 | SregValueMap::iterator it = sreg_value_map_.find(s_reg); |
| 104 | if (it != sreg_value_map_.end()) { |
| 105 | res = it->second; |
| 106 | } else { |
| 107 | // First use |
| 108 | res = LookupValue(NO_VALUE, s_reg, NO_VALUE, NO_VALUE); |
| 109 | sreg_value_map_.Put(s_reg, res); |
| 110 | } |
| 111 | return res; |
| 112 | }; |
| 113 | |
| 114 | void SetOperandValueWide(uint16_t s_reg, uint16_t value) |
| 115 | { |
| 116 | SregValueMap::iterator it = sreg_wide_value_map_.find(s_reg); |
| 117 | if (it != sreg_wide_value_map_.end()) { |
| 118 | DCHECK_EQ(it->second, value); |
| 119 | } else { |
| 120 | sreg_wide_value_map_.Put(s_reg, value); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | uint16_t GetOperandValueWide(int s_reg) |
| 125 | { |
| 126 | uint16_t res = NO_VALUE; |
| 127 | SregValueMap::iterator it = sreg_wide_value_map_.find(s_reg); |
| 128 | if (it != sreg_wide_value_map_.end()) { |
| 129 | res = it->second; |
| 130 | } else { |
| 131 | // First use |
| 132 | res = LookupValue(NO_VALUE, s_reg, NO_VALUE, NO_VALUE); |
| 133 | sreg_wide_value_map_.Put(s_reg, res); |
| 134 | } |
| 135 | return res; |
| 136 | }; |
| 137 | |
| 138 | uint16_t GetValueNumber(MIR* mir); |
| 139 | |
| 140 | private: |
| 141 | CompilationUnit* cu_; |
| 142 | SregValueMap sreg_value_map_; |
| 143 | SregValueMap sreg_wide_value_map_; |
| 144 | ValueMap value_map_; |
| 145 | MemoryVersionMap memory_version_map_; |
| 146 | std::set<uint16_t> null_checked_; |
| 147 | |
| 148 | }; |
| 149 | |
| 150 | } // namespace art |
| 151 | |
buzbee | 395116c | 2013-02-27 14:30:25 -0800 | [diff] [blame] | 152 | #endif // ART_SRC_COMPILER_DEX_BBOPT_H_ |