Debian Codegraph @

js/src/jit/EffectiveAddressAnalysis.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "jit/EffectiveAddressAnalysis.h"
8
9 #include "jit/IonAnalysis.h"
10 #include "jit/MIR-wasm.h"
11 #include "jit/MIR.h"
12 #include "jit/MIRGenerator.h"
13 #include "jit/MIRGraph.h"
14 #include "util/CheckedArithmetic.h"
15
16 using namespace js;
17 using namespace jit;
18
19 static void AnalyzeLsh(TempAllocator& alloc, MLsh* lsh) {
20 if (lsh->type() != MIRType::Int32) {
21 return;
22 }
23
24 if (lsh->isRecoveredOnBailout()) {
25 return;
26 }
27
28 MDefinition* index = lsh->lhs();
29 MOZ_ASSERT(index->type() == MIRType::Int32);
30
31 MConstant* shiftValue = lsh->rhs()->maybeConstantValue();
32 if (!shiftValue) {
33 return;
34 }
35
36 if (shiftValue->type() != MIRType::Int32 ||
37 !IsShiftInScaleRange(shiftValue->toInt32())) {
38 return;
39 }
40
41 Scale scale = ShiftToScale(shiftValue->toInt32());
42
43 int32_t displacement = 0;
44 MInstruction* last = lsh;
45 MDefinition* base = nullptr;
46 while (true) {
47 if (!last->hasOneUse()) {
48 break;
49 }
50
51 MUseIterator use = last->usesBegin();
52 if (!use->consumer()->isDefinition() ||
53 !use->consumer()->toDefinition()->isAdd()) {
54 break;
55 }
56
57 MAdd* add = use->consumer()->toDefinition()->toAdd();
58 if (add->type() != MIRType::Int32 || !add->isTruncated()) {
59 break;
60 }
61
62 MDefinition* other = add->getOperand(1 - add->indexOf(*use));
63
64 if (MConstant* otherConst = other->maybeConstantValue()) {
65 displacement += otherConst->toInt32();
66 } else {
67 if (base) {
68 break;
69 }
70 base = other;
71 }
72
73 last = add;
74 if (last->isRecoveredOnBailout()) {
75 return;
76 }
77 }
78
79 if (!base) {
80 uint32_t elemSize = 1 << ScaleToShift(scale);
81 if (displacement % elemSize != 0) {
82 return;
83 }
84
85 if (!last->hasOneUse()) {
86 return;
87 }
88
89 MUseIterator use = last->usesBegin();
90 if (!use->consumer()->isDefinition() ||
91 !use->consumer()->toDefinition()->isBitAnd()) {
92 return;
93 }
94
95 MBitAnd* bitAnd = use->consumer()->toDefinition()->toBitAnd();
96 if (bitAnd->isRecoveredOnBailout()) {
97 return;
98 }
99
100 MDefinition* other = bitAnd->getOperand(1 - bitAnd->indexOf(*use));
101 MConstant* otherConst = other->maybeConstantValue();
102 if (!otherConst || otherConst->type() != MIRType::Int32) {
103 return;
104 }
105
106 uint32_t bitsClearedByShift = elemSize - 1;
107 uint32_t bitsClearedByMask = ~uint32_t(otherConst->toInt32());
108 if ((bitsClearedByShift & bitsClearedByMask) != bitsClearedByMask) {
109 return;
110 }
111
112 bitAnd->replaceAllUsesWith(last);
113 return;
114 }
115
116 if (base->isRecoveredOnBailout()) {
117 return;
118 }
119
120 MEffectiveAddress* eaddr =
121 MEffectiveAddress::New(alloc, base, index, scale, displacement);
122 last->replaceAllUsesWith(eaddr);
123 last->block()->insertAfter(last, eaddr);
124 }
125
126 // Transform:
127 //
128 // [AddI]
129 // addl $9, %esi
130 // [LoadUnboxedScalar]
131 // movsd 0x0(%rbx,%rsi,8), %xmm4
132 //
133 // into:
134 //
135 // [LoadUnboxedScalar]
136 // movsd 0x48(%rbx,%rsi,8), %xmm4
137 //
138 // This is possible when the AddI is only used by the LoadUnboxedScalar opcode.
139 static void AnalyzeLoadUnboxedScalar(MLoadUnboxedScalar* load) {
140 if (load->isRecoveredOnBailout()) {
141 return;
142 }
143
144 if (!load->getOperand(1)->isAdd()) {
145 return;
146 }
147
148 JitSpew(JitSpew_EAA, "analyze: %s%u", load->opName(), load->id());
149
150 MAdd* add = load->getOperand(1)->toAdd();
151
152 if (add->type() != MIRType::Int32 || !add->hasUses() ||
153 add->truncateKind() != TruncateKind::Truncate) {
154 return;
155 }
156
157 MDefinition* lhs = add->lhs();
158 MDefinition* rhs = add->rhs();
159 MDefinition* constant = nullptr;
160 MDefinition* node = nullptr;
161
162 if (lhs->isConstant()) {
163 constant = lhs;
164 node = rhs;
165 } else if (rhs->isConstant()) {
166 constant = rhs;
167 node = lhs;
168 } else
169 return;
170
171 MOZ_ASSERT(constant->type() == MIRType::Int32);
172
173 size_t storageSize = Scalar::byteSize(load->storageType());
174 int32_t c1 = load->offsetAdjustment();
175 int32_t c2 = 0;
176 if (!SafeMul(constant->maybeConstantValue()->toInt32(), storageSize, &c2)) {
177 return;
178 }
179
180 int32_t offset = 0;
181 if (!SafeAdd(c1, c2, &offset)) {
182 return;
183 }
184
185 JitSpew(JitSpew_EAA, "set offset: %d + %d = %d on: %s%u", c1, c2, offset,
186 load->opName(), load->id());
187 load->setOffsetAdjustment(offset);
188 load->replaceOperand(1, node);
189
190 if (!add->hasLiveDefUses() && DeadIfUnused(add) &&
191 add->canRecoverOnBailout()) {
192 JitSpew(JitSpew_EAA, "mark as recovered on bailout: %s%u", add->opName(),
193 add->id());
194 add->setRecoveredOnBailoutUnchecked();
195 }
196 }
197
198 template <typename AsmJSMemoryAccess>
199 void EffectiveAddressAnalysis::analyzeAsmJSHeapAccess(AsmJSMemoryAccess* ins) {
200 MDefinition* base = ins->base();
201
202 if (base->isConstant()) {
203 // If the index is within the minimum heap length, we can optimize away the
204 // bounds check. Asm.js accesses always have an int32 base, the memory is
205 // always a memory32.
206 int32_t imm = base->toConstant()->toInt32();
207 if (imm >= 0) {
208 int32_t end = (uint32_t)imm + ins->byteSize();
209 if (end >= imm && (uint32_t)end <= mir_->minWasmMemory0Length()) {
210 ins->removeBoundsCheck();
211 }
212 }
213 }
214 }
215
216 // This analysis converts patterns of the form:
217 // truncate(x + (y << {0,1,2,3}))
218 // truncate(x + (y << {0,1,2,3}) + imm32)
219 // into a single lea instruction, and patterns of the form:
220 // asmload(x + imm32)
221 // asmload(x << {0,1,2,3})
222 // asmload((x << {0,1,2,3}) + imm32)
223 // asmload((x << {0,1,2,3}) & mask) (where mask is redundant
224 // with shift)
225 // asmload(((x << {0,1,2,3}) + imm32) & mask) (where mask is redundant
226 // with shift + imm32)
227 // into a single asmload instruction (and for asmstore too).
228 //
229 // Additionally, we should consider the general forms:
230 // truncate(x + y + imm32)
231 // truncate((y << {0,1,2,3}) + imm32)
232 bool EffectiveAddressAnalysis::analyze() {
233 JitSpew(JitSpew_EAA, "Begin");
234 for (ReversePostorderIterator block(graph_.rpoBegin());
235 block != graph_.rpoEnd(); block++) {
236 for (MInstructionIterator i = block->begin(); i != block->end(); i++) {
237 if (!graph_.alloc().ensureBallast()) {
238 return false;
239 }
240
241 // Note that we don't check for MWasmCompareExchangeHeap
242 // or MWasmAtomicBinopHeap, because the backend and the OOB
243 // mechanism don't support non-zero offsets for them yet
244 // (TODO bug 1254935).
245 if (i->isLsh()) {
246 AnalyzeLsh(graph_.alloc(), i->toLsh());
247 } else if (i->isLoadUnboxedScalar()) {
248 AnalyzeLoadUnboxedScalar(i->toLoadUnboxedScalar());
249 } else if (i->isAsmJSLoadHeap()) {
250 analyzeAsmJSHeapAccess(i->toAsmJSLoadHeap());
251 } else if (i->isAsmJSStoreHeap()) {
252 analyzeAsmJSHeapAccess(i->toAsmJSStoreHeap());
253 }
254 }
255 }
256 return true;
257 }