1/*
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 "pc_relative_fixups_x86.h"
18#include "code_generator_x86.h"
19#include "intrinsics_x86.h"
20
21namespace art {
22namespace x86 {
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<CodeGeneratorX86*>(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 VisitAdd(HAdd* add) OVERRIDE {
45    BinaryFP(add);
46  }
47
48  void VisitSub(HSub* sub) OVERRIDE {
49    BinaryFP(sub);
50  }
51
52  void VisitMul(HMul* mul) OVERRIDE {
53    BinaryFP(mul);
54  }
55
56  void VisitDiv(HDiv* div) OVERRIDE {
57    BinaryFP(div);
58  }
59
60  void VisitCompare(HCompare* compare) OVERRIDE {
61    BinaryFP(compare);
62  }
63
64  void VisitReturn(HReturn* ret) OVERRIDE {
65    HConstant* value = ret->InputAt(0)->AsConstant();
66    if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
67      ReplaceInput(ret, value, 0, true);
68    }
69  }
70
71  void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
72    HandleInvoke(invoke);
73  }
74
75  void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
76    HandleInvoke(invoke);
77  }
78
79  void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
80    HandleInvoke(invoke);
81  }
82
83  void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
84    HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
85    if (load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
86        load_kind == HLoadClass::LoadKind::kBssEntry) {
87      HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_class);
88      load_class->AddSpecialInput(method_address);
89    }
90  }
91
92  void VisitLoadString(HLoadString* load_string) OVERRIDE {
93    HLoadString::LoadKind load_kind = load_string->GetLoadKind();
94    if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
95        load_kind == HLoadString::LoadKind::kBssEntry) {
96      HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(load_string);
97      load_string->AddSpecialInput(method_address);
98    }
99  }
100
101  void BinaryFP(HBinaryOperation* bin) {
102    HConstant* rhs = bin->InputAt(1)->AsConstant();
103    if (rhs != nullptr && Primitive::IsFloatingPointType(rhs->GetType())) {
104      ReplaceInput(bin, rhs, 1, false);
105    }
106  }
107
108  void VisitEqual(HEqual* cond) OVERRIDE {
109    BinaryFP(cond);
110  }
111
112  void VisitNotEqual(HNotEqual* cond) OVERRIDE {
113    BinaryFP(cond);
114  }
115
116  void VisitLessThan(HLessThan* cond) OVERRIDE {
117    BinaryFP(cond);
118  }
119
120  void VisitLessThanOrEqual(HLessThanOrEqual* cond) OVERRIDE {
121    BinaryFP(cond);
122  }
123
124  void VisitGreaterThan(HGreaterThan* cond) OVERRIDE {
125    BinaryFP(cond);
126  }
127
128  void VisitGreaterThanOrEqual(HGreaterThanOrEqual* cond) OVERRIDE {
129    BinaryFP(cond);
130  }
131
132  void VisitNeg(HNeg* neg) OVERRIDE {
133    if (Primitive::IsFloatingPointType(neg->GetType())) {
134      // We need to replace the HNeg with a HX86FPNeg in order to address the constant area.
135      HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(neg);
136      HGraph* graph = GetGraph();
137      HBasicBlock* block = neg->GetBlock();
138      HX86FPNeg* x86_fp_neg = new (graph->GetArena()) HX86FPNeg(
139          neg->GetType(),
140          neg->InputAt(0),
141          method_address,
142          neg->GetDexPc());
143      block->ReplaceAndRemoveInstructionWith(neg, x86_fp_neg);
144    }
145  }
146
147  void VisitPackedSwitch(HPackedSwitch* switch_insn) OVERRIDE {
148    if (switch_insn->GetNumEntries() <=
149        InstructionCodeGeneratorX86::kPackedSwitchJumpTableThreshold) {
150      return;
151    }
152    // We need to replace the HPackedSwitch with a HX86PackedSwitch in order to
153    // address the constant area.
154    HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(switch_insn);
155    HGraph* graph = GetGraph();
156    HBasicBlock* block = switch_insn->GetBlock();
157    HX86PackedSwitch* x86_switch = new (graph->GetArena()) HX86PackedSwitch(
158        switch_insn->GetStartValue(),
159        switch_insn->GetNumEntries(),
160        switch_insn->InputAt(0),
161        method_address,
162        switch_insn->GetDexPc());
163    block->ReplaceAndRemoveInstructionWith(switch_insn, x86_switch);
164  }
165
166  HX86ComputeBaseMethodAddress* GetPCRelativeBasePointer(HInstruction* cursor) {
167    bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
168    if (!has_irreducible_loops) {
169      // Ensure we only initialize the pointer once.
170      if (base_ != nullptr) {
171        return base_;
172      }
173    }
174    // Insert the base at the start of the entry block, move it to a better
175    // position later in MoveBaseIfNeeded().
176    HX86ComputeBaseMethodAddress* method_address =
177        new (GetGraph()->GetArena()) HX86ComputeBaseMethodAddress();
178    if (has_irreducible_loops) {
179      cursor->GetBlock()->InsertInstructionBefore(method_address, cursor);
180    } else {
181      HBasicBlock* entry_block = GetGraph()->GetEntryBlock();
182      entry_block->InsertInstructionBefore(method_address, entry_block->GetFirstInstruction());
183      base_ = method_address;
184    }
185    return method_address;
186  }
187
188  void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
189    HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(insn);
190    HX86LoadFromConstantTable* load_constant =
191        new (GetGraph()->GetArena()) HX86LoadFromConstantTable(method_address, value);
192    if (!materialize) {
193      load_constant->MarkEmittedAtUseSite();
194    }
195    insn->GetBlock()->InsertInstructionBefore(load_constant, insn);
196    insn->ReplaceInput(load_constant, input_index);
197  }
198
199  void HandleInvoke(HInvoke* invoke) {
200    // If this is an invoke-static/-direct with PC-relative dex cache array
201    // addressing, we need the PC-relative address base.
202    HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
203    // We can't add a pointer to the constant area if we already have a current
204    // method pointer. This may arise when sharpening doesn't remove the current
205    // method pointer from the invoke.
206    if (invoke_static_or_direct != nullptr &&
207        invoke_static_or_direct->HasCurrentMethodInput()) {
208      DCHECK(!invoke_static_or_direct->HasPcRelativeDexCache());
209      return;
210    }
211
212    bool base_added = false;
213    if (invoke_static_or_direct != nullptr &&
214        invoke_static_or_direct->HasPcRelativeDexCache() &&
215        !IsCallFreeIntrinsic<IntrinsicLocationsBuilderX86>(invoke, codegen_)) {
216      HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
217      // Add the extra parameter.
218      invoke_static_or_direct->AddSpecialInput(method_address);
219      base_added = true;
220    }
221
222    // Ensure that we can load FP arguments from the constant area.
223    HInputsRef inputs = invoke->GetInputs();
224    for (size_t i = 0; i < inputs.size(); i++) {
225      HConstant* input = inputs[i]->AsConstant();
226      if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
227        ReplaceInput(invoke, input, i, true);
228      }
229    }
230
231    // These intrinsics need the constant area.
232    switch (invoke->GetIntrinsic()) {
233      case Intrinsics::kMathAbsDouble:
234      case Intrinsics::kMathAbsFloat:
235      case Intrinsics::kMathMaxDoubleDouble:
236      case Intrinsics::kMathMaxFloatFloat:
237      case Intrinsics::kMathMinDoubleDouble:
238      case Intrinsics::kMathMinFloatFloat:
239      case Intrinsics::kMathRoundFloat:
240        if (!base_added) {
241          DCHECK(invoke_static_or_direct != nullptr);
242          DCHECK(!invoke_static_or_direct->HasCurrentMethodInput());
243          HX86ComputeBaseMethodAddress* method_address = GetPCRelativeBasePointer(invoke);
244          invoke_static_or_direct->AddSpecialInput(method_address);
245        }
246        break;
247      default:
248        break;
249    }
250  }
251
252  CodeGeneratorX86* codegen_;
253
254  // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
255  // input to the HX86LoadFromConstantTable instructions. Only set for
256  // graphs with reducible loops.
257  HX86ComputeBaseMethodAddress* base_;
258};
259
260void PcRelativeFixups::Run() {
261  PCRelativeHandlerVisitor visitor(graph_, codegen_);
262  visitor.VisitInsertionOrder();
263  visitor.MoveBaseIfNeeded();
264}
265
266}  // namespace x86
267}  // namespace art
268