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