blob: c3470002c53384a8399709b1009f18e88cee7134 [file] [log] [blame]
Mark Mendell94991072015-10-06 14:58:32 -04001/*
2 * Copyright (C) 2015 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 "constant_area_fixups_x86.h"
18
19namespace art {
20namespace x86 {
21
22/**
23 * Finds instructions that need the constant area base as an input.
24 */
25class ConstantHandlerVisitor : public HGraphVisitor {
26 public:
27 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
28
29 private:
30 void VisitAdd(HAdd* add) OVERRIDE {
31 BinaryFP(add);
32 }
33
34 void VisitSub(HSub* sub) OVERRIDE {
35 BinaryFP(sub);
36 }
37
38 void VisitMul(HMul* mul) OVERRIDE {
39 BinaryFP(mul);
40 }
41
42 void VisitDiv(HDiv* div) OVERRIDE {
43 BinaryFP(div);
44 }
45
46 void VisitReturn(HReturn* ret) OVERRIDE {
47 HConstant* value = ret->InputAt(0)->AsConstant();
48 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
49 ReplaceInput(ret, value, 0, true);
50 }
51 }
52
53 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
54 HandleInvoke(invoke);
55 }
56
57 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
58 HandleInvoke(invoke);
59 }
60
61 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
62 HandleInvoke(invoke);
63 }
64
65 void BinaryFP(HBinaryOperation* bin) {
66 HConstant* rhs = bin->InputAt(1)->AsConstant();
67 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
68 ReplaceInput(bin, rhs, 1, false);
69 }
70 }
71
72 void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
73 // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
74 // address the constant area.
75 InitializeConstantAreaPointer(switch_insn);
76 HGraph* graph = GetGraph();
77 HBasicBlock* block = switch_insn->GetBlock();
78 HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch(
79 switch_insn->GetStartValue(),
80 switch_insn->GetNumEntries(),
81 switch_insn->InputAt(0),
82 base_,
83 switch_insn->GetDexPc());
84 block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
85 }
86
87 void InitializeConstantAreaPointer(HInstruction* user) {
88 // Ensure we only initialize the pointer once.
89 if (base_ != nullptr) {
90 return;
91 }
92
93 HGraph* graph = GetGraph();
94 HBasicBlock* entry = graph->GetEntryBlock();
95 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
96 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
97 entry->InsertInstructionBefore(base_, insert_pos);
98 DCHECK(base_ != nullptr);
99 }
100
101 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
102 InitializeConstantAreaPointer(insn);
103 HGraph* graph = GetGraph();
104 HBasicBlock* block = insn->GetBlock();
105 HX86LoadFromConstantTable* load_constant =
106 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
107 block->InsertInstructionBefore(load_constant, insn);
108 insn->ReplaceInput(load_constant, input_index);
109 }
110
111 void HandleInvoke(HInvoke* invoke) {
112 // Ensure that we can load FP arguments from the constant area.
113 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
114 HConstant* input = invoke->InputAt(i)->AsConstant();
115 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
116 ReplaceInput(invoke, input, i, true);
117 }
118 }
119 }
120
121 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
122 // input to the HX86LoadFromConstantTable instructions.
123 HX86ComputeBaseMethodAddress* base_;
124};
125
126void ConstantAreaFixups::Run() {
127 ConstantHandlerVisitor visitor(graph_);
128 visitor.VisitInsertionOrder();
129}
130
131} // namespace x86
132} // namespace art