Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include "pc_relative_fixups_mips.h" |
| 18 | #include "code_generator_mips.h" |
| 19 | #include "intrinsics_mips.h" |
| 20 | |
| 21 | namespace art { |
| 22 | namespace mips { |
| 23 | |
| 24 | /** |
| 25 | * Finds instructions that need the constant area base as an input. |
| 26 | */ |
| 27 | class PCRelativeHandlerVisitor : public HGraphVisitor { |
| 28 | public: |
| 29 | PCRelativeHandlerVisitor(HGraph* graph, CodeGenerator* codegen) |
| 30 | : HGraphVisitor(graph), |
| 31 | codegen_(down_cast<CodeGeneratorMIPS*>(codegen)), |
| 32 | base_(nullptr) {} |
| 33 | |
| 34 | void MoveBaseIfNeeded() { |
| 35 | if (base_ != nullptr) { |
| 36 | // Bring the base closer to the first use (previously, it was in the |
| 37 | // entry block) and relieve some pressure on the register allocator |
| 38 | // while avoiding recalculation of the base in a loop. |
| 39 | base_->MoveBeforeFirstUserAndOutOfLoops(); |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 40 | // Computing the base for PC-relative literals will clobber RA with |
| 41 | // the NAL instruction on R2. Take a note of this before generating |
| 42 | // the method entry. |
| 43 | codegen_->ClobberRA(); |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { |
| 49 | HandleInvoke(invoke); |
| 50 | } |
| 51 | |
| 52 | void InitializePCRelativeBasePointer() { |
| 53 | // Ensure we only initialize the pointer once. |
| 54 | if (base_ != nullptr) { |
| 55 | return; |
| 56 | } |
| 57 | // Insert the base at the start of the entry block, move it to a better |
| 58 | // position later in MoveBaseIfNeeded(). |
| 59 | base_ = new (GetGraph()->GetArena()) HMipsComputeBaseMethodAddress(); |
| 60 | HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); |
| 61 | entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction()); |
| 62 | DCHECK(base_ != nullptr); |
| 63 | } |
| 64 | |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 65 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE { |
| 66 | HLoadClass::LoadKind load_kind = load_class->GetLoadKind(); |
| 67 | switch (load_kind) { |
| 68 | case HLoadClass::LoadKind::kBootImageLinkTimeAddress: |
| 69 | case HLoadClass::LoadKind::kBootImageAddress: |
| 70 | case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: |
| 71 | // Add a base register for PC-relative literals on R2. |
| 72 | InitializePCRelativeBasePointer(); |
| 73 | load_class->AddSpecialInput(base_); |
| 74 | break; |
| 75 | default: |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void VisitLoadString(HLoadString* load_string) OVERRIDE { |
| 81 | HLoadString::LoadKind load_kind = load_string->GetLoadKind(); |
| 82 | switch (load_kind) { |
| 83 | case HLoadString::LoadKind::kBootImageLinkTimeAddress: |
| 84 | case HLoadString::LoadKind::kBootImageAddress: |
| 85 | case HLoadString::LoadKind::kBootImageLinkTimePcRelative: |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 86 | case HLoadString::LoadKind::kBssEntry: |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 87 | // Add a base register for PC-relative literals on R2. |
| 88 | InitializePCRelativeBasePointer(); |
| 89 | load_string->AddSpecialInput(base_); |
| 90 | break; |
| 91 | default: |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
Alexey Frunze | 96b6682 | 2016-09-10 02:32:44 -0700 | [diff] [blame] | 96 | void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE { |
| 97 | if (switch_insn->GetNumEntries() <= |
| 98 | InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) { |
| 99 | return; |
| 100 | } |
| 101 | // We need to replace the HPackedSwitch with a HMipsPackedSwitch in order to |
| 102 | // address the constant area. |
| 103 | InitializePCRelativeBasePointer(); |
| 104 | HGraph* graph = GetGraph(); |
| 105 | HBasicBlock* block = switch_insn->GetBlock(); |
| 106 | HMipsPackedSwitch* mips_switch = new (graph->GetArena()) HMipsPackedSwitch( |
| 107 | switch_insn->GetStartValue(), |
| 108 | switch_insn->GetNumEntries(), |
| 109 | switch_insn->InputAt(0), |
| 110 | base_, |
| 111 | switch_insn->GetDexPc()); |
| 112 | block->ReplaceAndRemoveInstructionWith(switch_insn, mips_switch); |
| 113 | } |
| 114 | |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 115 | void HandleInvoke(HInvoke* invoke) { |
| 116 | // If this is an invoke-static/-direct with PC-relative dex cache array |
| 117 | // addressing, we need the PC-relative address base. |
| 118 | HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect(); |
| 119 | if (invoke_static_or_direct != nullptr) { |
| 120 | HInvokeStaticOrDirect::MethodLoadKind method_load_kind = |
| 121 | invoke_static_or_direct->GetMethodLoadKind(); |
| 122 | HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = |
| 123 | invoke_static_or_direct->GetCodePtrLocation(); |
| 124 | |
| 125 | bool has_extra_input = |
| 126 | (method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) || |
| 127 | (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup); |
| 128 | |
| 129 | // We can't add a pointer to the constant area if we already have a current |
| 130 | // method pointer. This may arise when sharpening doesn't remove the current |
| 131 | // method pointer from the invoke. |
| 132 | if (invoke_static_or_direct->HasCurrentMethodInput()) { |
| 133 | DCHECK(!invoke_static_or_direct->HasPcRelativeDexCache()); |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 134 | CHECK(!has_extra_input); |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | |
Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 138 | if (has_extra_input && |
| 139 | !IsCallFreeIntrinsic<IntrinsicLocationsBuilderMIPS>(invoke, codegen_)) { |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 140 | InitializePCRelativeBasePointer(); |
| 141 | // Add the extra parameter base_. |
| 142 | invoke_static_or_direct->AddSpecialInput(base_); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 147 | CodeGeneratorMIPS* codegen_; |
| 148 | |
| 149 | // The generated HMipsComputeBaseMethodAddress in the entry block needed as an |
| 150 | // input to the HMipsLoadFromConstantTable instructions. |
| 151 | HMipsComputeBaseMethodAddress* base_; |
| 152 | }; |
| 153 | |
| 154 | void PcRelativeFixups::Run() { |
| 155 | CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen_); |
| 156 | if (mips_codegen->GetInstructionSetFeatures().IsR6()) { |
| 157 | // Do nothing for R6 because it has PC-relative addressing. |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | if (graph_->HasIrreducibleLoops()) { |
| 161 | // Do not run this optimization, as irreducible loops do not work with an instruction |
| 162 | // that can be live-in at the irreducible loop header. |
| 163 | return; |
| 164 | } |
| 165 | PCRelativeHandlerVisitor visitor(graph_, codegen_); |
| 166 | visitor.VisitInsertionOrder(); |
| 167 | visitor.MoveBaseIfNeeded(); |
| 168 | } |
| 169 | |
| 170 | } // namespace mips |
| 171 | } // namespace art |