blob: ba405cdb69a144b3f482cf4b5f405c0d156be684 [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();
40 }
41 }
42
43 private:
44 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
45 HandleInvoke(invoke);
46 }
47
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().
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
61 void HandleInvoke(HInvoke* invoke) {
62 // If this is an invoke-static/-direct with PC-relative dex cache array
63 // addressing, we need the PC-relative address base.
64 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
65 if (invoke_static_or_direct != nullptr) {
66 HInvokeStaticOrDirect::MethodLoadKind method_load_kind =
67 invoke_static_or_direct->GetMethodLoadKind();
68 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
69 invoke_static_or_direct->GetCodePtrLocation();
70
71 bool has_extra_input =
72 (method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
73 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup);
74
75 // We can't add a pointer to the constant area if we already have a current
76 // method pointer. This may arise when sharpening doesn't remove the current
77 // method pointer from the invoke.
78 if (invoke_static_or_direct->HasCurrentMethodInput()) {
79 DCHECK(!invoke_static_or_direct->HasPcRelativeDexCache());
80 CHECK(!has_extra_input); // TODO: review this.
81 return;
82 }
83
84 if (has_extra_input && !WillHaveCallFreeIntrinsicsCodeGen(invoke)) {
85 InitializePCRelativeBasePointer();
86 // Add the extra parameter base_.
87 invoke_static_or_direct->AddSpecialInput(base_);
88 }
89 }
90 }
91
92 bool WillHaveCallFreeIntrinsicsCodeGen(HInvoke* invoke) {
93 if (invoke->GetIntrinsic() != Intrinsics::kNone) {
94 // This invoke may have intrinsic code generation defined. However, we must
95 // now also determine if this code generation is truly there and call-free
96 // (not unimplemented, no bail on instruction features, or call on slow path).
97 // This is done by actually calling the locations builder on the instruction
98 // and clearing out the locations once result is known. We assume this
99 // call only has creating locations as side effects!
100 IntrinsicLocationsBuilderMIPS builder(codegen_);
101 bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall();
102 invoke->SetLocations(nullptr);
103 return success;
104 }
105 return false;
106 }
107
108 CodeGeneratorMIPS* codegen_;
109
110 // The generated HMipsComputeBaseMethodAddress in the entry block needed as an
111 // input to the HMipsLoadFromConstantTable instructions.
112 HMipsComputeBaseMethodAddress* base_;
113};
114
115void PcRelativeFixups::Run() {
116 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen_);
117 if (mips_codegen->GetInstructionSetFeatures().IsR6()) {
118 // Do nothing for R6 because it has PC-relative addressing.
119 // TODO: review. Move this check into RunArchOptimizations()?
120 return;
121 }
122 if (graph_->HasIrreducibleLoops()) {
123 // Do not run this optimization, as irreducible loops do not work with an instruction
124 // that can be live-in at the irreducible loop header.
125 return;
126 }
127 PCRelativeHandlerVisitor visitor(graph_, codegen_);
128 visitor.VisitInsertionOrder();
129 visitor.MoveBaseIfNeeded();
130}
131
132} // namespace mips
133} // namespace art