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: |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 48 | void InitializePCRelativeBasePointer() { |
| 49 | // Ensure we only initialize the pointer once. |
| 50 | if (base_ != nullptr) { |
| 51 | return; |
| 52 | } |
| 53 | // Insert the base at the start of the entry block, move it to a better |
| 54 | // position later in MoveBaseIfNeeded(). |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 55 | base_ = new (GetGraph()->GetAllocator()) HMipsComputeBaseMethodAddress(); |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 56 | HBasicBlock* entry_block = GetGraph()->GetEntryBlock(); |
| 57 | entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction()); |
| 58 | DCHECK(base_ != nullptr); |
| 59 | } |
| 60 | |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 61 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 62 | // If this is an invoke with PC-relative load kind, |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 63 | // we need to add the base as the special input. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 64 | if (invoke->HasPcRelativeMethodLoadKind() && |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 65 | !IsCallFreeIntrinsic<IntrinsicLocationsBuilderMIPS>(invoke, codegen_)) { |
| 66 | InitializePCRelativeBasePointer(); |
| 67 | // Add the special argument base to the method. |
| 68 | DCHECK(!invoke->HasCurrentMethodInput()); |
| 69 | invoke->AddSpecialInput(base_); |
| 70 | } |
| 71 | } |
| 72 | |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 73 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE { |
| 74 | HLoadClass::LoadKind load_kind = load_class->GetLoadKind(); |
| 75 | switch (load_kind) { |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 76 | case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 77 | case HLoadClass::LoadKind::kBootImageAddress: |
Vladimir Marko | e47f60c | 2018-02-21 13:43:28 +0000 | [diff] [blame] | 78 | case HLoadClass::LoadKind::kBootImageRelRo: |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 79 | case HLoadClass::LoadKind::kBssEntry: |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 80 | // Add a base register for PC-relative literals on R2. |
| 81 | InitializePCRelativeBasePointer(); |
| 82 | load_class->AddSpecialInput(base_); |
| 83 | break; |
| 84 | default: |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void VisitLoadString(HLoadString* load_string) OVERRIDE { |
| 90 | HLoadString::LoadKind load_kind = load_string->GetLoadKind(); |
| 91 | switch (load_kind) { |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 92 | case HLoadString::LoadKind::kBootImageLinkTimePcRelative: |
Vladimir Marko | 6cfbdbc | 2017-07-25 13:26:39 +0100 | [diff] [blame] | 93 | case HLoadString::LoadKind::kBootImageAddress: |
Vladimir Marko | e47f60c | 2018-02-21 13:43:28 +0000 | [diff] [blame] | 94 | case HLoadString::LoadKind::kBootImageRelRo: |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 95 | case HLoadString::LoadKind::kBssEntry: |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 96 | // Add a base register for PC-relative literals on R2. |
| 97 | InitializePCRelativeBasePointer(); |
| 98 | load_string->AddSpecialInput(base_); |
| 99 | break; |
| 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
Alexey Frunze | 96b6682 | 2016-09-10 02:32:44 -0700 | [diff] [blame] | 105 | void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE { |
| 106 | if (switch_insn->GetNumEntries() <= |
| 107 | InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) { |
| 108 | return; |
| 109 | } |
| 110 | // We need to replace the HPackedSwitch with a HMipsPackedSwitch in order to |
| 111 | // address the constant area. |
| 112 | InitializePCRelativeBasePointer(); |
| 113 | HGraph* graph = GetGraph(); |
| 114 | HBasicBlock* block = switch_insn->GetBlock(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 115 | HMipsPackedSwitch* mips_switch = new (graph->GetAllocator()) HMipsPackedSwitch( |
Alexey Frunze | 96b6682 | 2016-09-10 02:32:44 -0700 | [diff] [blame] | 116 | switch_insn->GetStartValue(), |
| 117 | switch_insn->GetNumEntries(), |
| 118 | switch_insn->InputAt(0), |
| 119 | base_, |
| 120 | switch_insn->GetDexPc()); |
| 121 | block->ReplaceAndRemoveInstructionWith(switch_insn, mips_switch); |
| 122 | } |
| 123 | |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 124 | CodeGeneratorMIPS* codegen_; |
| 125 | |
| 126 | // The generated HMipsComputeBaseMethodAddress in the entry block needed as an |
| 127 | // input to the HMipsLoadFromConstantTable instructions. |
| 128 | HMipsComputeBaseMethodAddress* base_; |
| 129 | }; |
| 130 | |
| 131 | void PcRelativeFixups::Run() { |
| 132 | CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen_); |
| 133 | if (mips_codegen->GetInstructionSetFeatures().IsR6()) { |
| 134 | // Do nothing for R6 because it has PC-relative addressing. |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | if (graph_->HasIrreducibleLoops()) { |
| 138 | // Do not run this optimization, as irreducible loops do not work with an instruction |
| 139 | // that can be live-in at the irreducible loop header. |
| 140 | return; |
| 141 | } |
| 142 | PCRelativeHandlerVisitor visitor(graph_, codegen_); |
| 143 | visitor.VisitInsertionOrder(); |
| 144 | visitor.MoveBaseIfNeeded(); |
| 145 | } |
| 146 | |
| 147 | } // namespace mips |
| 148 | } // namespace art |