blob: bce54bf49a4090da7f481fdba2e0115ee2c73f53 [file] [log] [blame]
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001/*
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
21namespace art {
22namespace mips {
23
24/**
25 * Finds instructions that need the constant area base as an input.
26 */
27class 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 Frunze06a46c42016-07-19 15:00:40 -070040 // 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 Frunzee3fb2452016-05-10 16:08:05 -070044 }
45 }
46
47 private:
Alexey Frunzee3fb2452016-05-10 16:08:05 -070048 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().
55 base_ = new (GetGraph()->GetArena()) HMipsComputeBaseMethodAddress();
56 HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
57 entry_block->InsertInstructionBefore(base_, entry_block->GetFirstInstruction());
58 DCHECK(base_ != nullptr);
59 }
60
Vladimir Marko65979462017-05-19 17:25:12 +010061 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
62 // If this is an invoke with PC-relative pointer to a method,
63 // we need to add the base as the special input.
64 if (invoke->GetMethodLoadKind() ==
65 HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative &&
66 !IsCallFreeIntrinsic<IntrinsicLocationsBuilderMIPS>(invoke, codegen_)) {
67 InitializePCRelativeBasePointer();
68 // Add the special argument base to the method.
69 DCHECK(!invoke->HasCurrentMethodInput());
70 invoke->AddSpecialInput(base_);
71 }
72 }
73
Alexey Frunze06a46c42016-07-19 15:00:40 -070074 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
75 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
76 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -070077 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +000078 case HLoadClass::LoadKind::kBootImageAddress:
79 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -070080 // 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 Frunze06a46c42016-07-19 15:00:40 -070092 case HLoadString::LoadKind::kBootImageAddress:
93 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +000094 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -070095 // Add a base register for PC-relative literals on R2.
96 InitializePCRelativeBasePointer();
97 load_string->AddSpecialInput(base_);
98 break;
99 default:
100 break;
101 }
102 }
103
Alexey Frunze96b66822016-09-10 02:32:44 -0700104 void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
105 if (switch_insn->GetNumEntries() <=
106 InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
107 return;
108 }
109 // We need to replace the HPackedSwitch with a HMipsPackedSwitch in order to
110 // address the constant area.
111 InitializePCRelativeBasePointer();
112 HGraph* graph = GetGraph();
113 HBasicBlock* block = switch_insn->GetBlock();
114 HMipsPackedSwitch* mips_switch = new (graph->GetArena()) HMipsPackedSwitch(
115 switch_insn->GetStartValue(),
116 switch_insn->GetNumEntries(),
117 switch_insn->InputAt(0),
118 base_,
119 switch_insn->GetDexPc());
120 block->ReplaceAndRemoveInstructionWith(switch_insn, mips_switch);
121 }
122
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700123 CodeGeneratorMIPS* codegen_;
124
125 // The generated HMipsComputeBaseMethodAddress in the entry block needed as an
126 // input to the HMipsLoadFromConstantTable instructions.
127 HMipsComputeBaseMethodAddress* base_;
128};
129
130void PcRelativeFixups::Run() {
131 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen_);
132 if (mips_codegen->GetInstructionSetFeatures().IsR6()) {
133 // Do nothing for R6 because it has PC-relative addressing.
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700134 return;
135 }
136 if (graph_->HasIrreducibleLoops()) {
137 // Do not run this optimization, as irreducible loops do not work with an instruction
138 // that can be live-in at the irreducible loop header.
139 return;
140 }
141 PCRelativeHandlerVisitor visitor(graph_, codegen_);
142 visitor.VisitInsertionOrder();
143 visitor.MoveBaseIfNeeded();
144}
145
146} // namespace mips
147} // namespace art