summaryrefslogtreecommitdiff
path: root/js/src/jit/shared/Lowering-shared.cpp
blob: 0acf34dfb41cb6d738aee72f6540d61863bd3f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/shared/Lowering-shared-inl.h"

#include "jit/LIR.h"
#include "jit/MIR.h"

#include "vm/Symbol.h"

using namespace js;
using namespace jit;

bool
LIRGeneratorShared::ShouldReorderCommutative(MDefinition* lhs, MDefinition* rhs, MInstruction* ins)
{
    // lhs and rhs are used by the commutative operator.
    MOZ_ASSERT(lhs->hasDefUses());
    MOZ_ASSERT(rhs->hasDefUses());

    // Ensure that if there is a constant, then it is in rhs.
    if (rhs->isConstant())
        return false;
    if (lhs->isConstant())
        return true;

    // Since clobbering binary operations clobber the left operand, prefer a
    // non-constant lhs operand with no further uses. To be fully precise, we
    // should check whether this is the *last* use, but checking hasOneDefUse()
    // is a decent approximation which doesn't require any extra analysis.
    bool rhsSingleUse = rhs->hasOneDefUse();
    bool lhsSingleUse = lhs->hasOneDefUse();
    if (rhsSingleUse) {
        if (!lhsSingleUse)
            return true;
    } else {
        if (lhsSingleUse)
            return false;
    }

    // If this is a reduction-style computation, such as
    //
    //   sum = 0;
    //   for (...)
    //      sum += ...;
    //
    // put the phi on the left to promote coalescing. This is fairly specific.
    if (rhsSingleUse &&
        rhs->isPhi() &&
        rhs->block()->isLoopHeader() &&
        ins == rhs->toPhi()->getLoopBackedgeOperand())
    {
        return true;
    }

    return false;
}

void
LIRGeneratorShared::ReorderCommutative(MDefinition** lhsp, MDefinition** rhsp, MInstruction* ins)
{
    MDefinition* lhs = *lhsp;
    MDefinition* rhs = *rhsp;

    if (ShouldReorderCommutative(lhs, rhs, ins)) {
        *rhsp = lhs;
        *lhsp = rhs;
    }
}

void
LIRGeneratorShared::visitConstant(MConstant* ins)
{
    if (!IsFloatingPointType(ins->type()) && ins->canEmitAtUses()) {
        emitAtUses(ins);
        return;
    }

    switch (ins->type()) {
      case MIRType::Double:
        define(new(alloc()) LDouble(ins->toRawF64()), ins);
        break;
      case MIRType::Float32:
        define(new(alloc()) LFloat32(ins->toRawF32()), ins);
        break;
      case MIRType::Boolean:
        define(new(alloc()) LInteger(ins->toBoolean()), ins);
        break;
      case MIRType::Int32:
        define(new(alloc()) LInteger(ins->toInt32()), ins);
        break;
      case MIRType::Int64:
        defineInt64(new(alloc()) LInteger64(ins->toInt64()), ins);
        break;
      case MIRType::String:
        define(new(alloc()) LPointer(ins->toString()), ins);
        break;
      case MIRType::Symbol:
        define(new(alloc()) LPointer(ins->toSymbol()), ins);
        break;
      case MIRType::Object:
        define(new(alloc()) LPointer(&ins->toObject()), ins);
        break;
      default:
        // Constants of special types (undefined, null) should never flow into
        // here directly. Operations blindly consuming them require a Box.
        MOZ_CRASH("unexpected constant type");
    }
}

void
LIRGeneratorShared::defineTypedPhi(MPhi* phi, size_t lirIndex)
{
    LPhi* lir = current->getPhi(lirIndex);

    uint32_t vreg = getVirtualRegister();

    phi->setVirtualRegister(vreg);
    lir->setDef(0, LDefinition(vreg, LDefinition::TypeFrom(phi->type())));
    annotate(lir);
}

void
LIRGeneratorShared::lowerTypedPhiInput(MPhi* phi, uint32_t inputPosition, LBlock* block, size_t lirIndex)
{
    MDefinition* operand = phi->getOperand(inputPosition);
    LPhi* lir = block->getPhi(lirIndex);
    lir->setOperand(inputPosition, LUse(operand->virtualRegister(), LUse::ANY));
}

LRecoverInfo*
LIRGeneratorShared::getRecoverInfo(MResumePoint* rp)
{
    if (cachedRecoverInfo_ && cachedRecoverInfo_->mir() == rp)
        return cachedRecoverInfo_;

    LRecoverInfo* recoverInfo = LRecoverInfo::New(gen, rp);
    if (!recoverInfo)
        return nullptr;

    cachedRecoverInfo_ = recoverInfo;
    return recoverInfo;
}

#ifdef DEBUG
bool
LRecoverInfo::OperandIter::canOptimizeOutIfUnused()
{
    MDefinition* ins = **this;

    // We check ins->type() in addition to ins->isUnused() because
    // EliminateDeadResumePointOperands may replace nodes with the constant
    // MagicValue(JS_OPTIMIZED_OUT).
    if ((ins->isUnused() || ins->type() == MIRType::MagicOptimizedOut) &&
        (*it_)->isResumePoint())
    {
        return !(*it_)->toResumePoint()->isObservableOperand(op_);
    }

    return true;
}
#endif

#ifdef JS_NUNBOX32
LSnapshot*
LIRGeneratorShared::buildSnapshot(LInstruction* ins, MResumePoint* rp, BailoutKind kind)
{
    LRecoverInfo* recoverInfo = getRecoverInfo(rp);
    if (!recoverInfo)
        return nullptr;

    LSnapshot* snapshot = LSnapshot::New(gen, recoverInfo, kind);
    if (!snapshot)
        return nullptr;

    size_t index = 0;
    for (LRecoverInfo::OperandIter it(recoverInfo); !it; ++it) {
        // Check that optimized out operands are in eliminable slots.
        MOZ_ASSERT(it.canOptimizeOutIfUnused());

        MDefinition* ins = *it;

        if (ins->isRecoveredOnBailout())
            continue;

        LAllocation* type = snapshot->typeOfSlot(index);
        LAllocation* payload = snapshot->payloadOfSlot(index);
        ++index;

        if (ins->isBox())
            ins = ins->toBox()->getOperand(0);

        // Guards should never be eliminated.
        MOZ_ASSERT_IF(ins->isUnused(), !ins->isGuard());

        // Snapshot operands other than constants should never be
        // emitted-at-uses. Try-catch support depends on there being no
        // code between an instruction and the LOsiPoint that follows it.
        MOZ_ASSERT_IF(!ins->isConstant(), !ins->isEmittedAtUses());

        // The register allocation will fill these fields in with actual
        // register/stack assignments. During code generation, we can restore
        // interpreter state with the given information. Note that for
        // constants, including known types, we record a dummy placeholder,
        // since we can recover the same information, much cleaner, from MIR.
        if (ins->isConstant() || ins->isUnused()) {
            *type = LAllocation();
            *payload = LAllocation();
        } else if (ins->type() != MIRType::Value) {
            *type = LAllocation();
            *payload = use(ins, LUse(LUse::KEEPALIVE));
        } else {
            *type = useType(ins, LUse::KEEPALIVE);
            *payload = usePayload(ins, LUse::KEEPALIVE);
        }
    }

    return snapshot;
}

#elif JS_PUNBOX64

LSnapshot*
LIRGeneratorShared::buildSnapshot(LInstruction* ins, MResumePoint* rp, BailoutKind kind)
{
    LRecoverInfo* recoverInfo = getRecoverInfo(rp);
    if (!recoverInfo)
        return nullptr;

    LSnapshot* snapshot = LSnapshot::New(gen, recoverInfo, kind);
    if (!snapshot)
        return nullptr;

    size_t index = 0;
    for (LRecoverInfo::OperandIter it(recoverInfo); !it; ++it) {
        // Check that optimized out operands are in eliminable slots.
        MOZ_ASSERT(it.canOptimizeOutIfUnused());

        MDefinition* def = *it;

        if (def->isRecoveredOnBailout())
            continue;

        if (def->isBox())
            def = def->toBox()->getOperand(0);

        // Guards should never be eliminated.
        MOZ_ASSERT_IF(def->isUnused(), !def->isGuard());

        // Snapshot operands other than constants should never be
        // emitted-at-uses. Try-catch support depends on there being no
        // code between an instruction and the LOsiPoint that follows it.
        MOZ_ASSERT_IF(!def->isConstant(), !def->isEmittedAtUses());

        LAllocation* a = snapshot->getEntry(index++);

        if (def->isUnused()) {
            *a = LAllocation();
            continue;
        }

        *a = useKeepaliveOrConstant(def);
    }

    return snapshot;
}
#endif

void
LIRGeneratorShared::assignSnapshot(LInstruction* ins, BailoutKind kind)
{
    // assignSnapshot must be called before define/add, since
    // it may add new instructions for emitted-at-use operands.
    MOZ_ASSERT(ins->id() == 0);

    LSnapshot* snapshot = buildSnapshot(ins, lastResumePoint_, kind);
    if (snapshot)
        ins->assignSnapshot(snapshot);
    else
        gen->abort("buildSnapshot failed");
}

void
LIRGeneratorShared::assignSafepoint(LInstruction* ins, MInstruction* mir, BailoutKind kind)
{
    MOZ_ASSERT(!osiPoint_);
    MOZ_ASSERT(!ins->safepoint());

    ins->initSafepoint(alloc());

    MResumePoint* mrp = mir->resumePoint() ? mir->resumePoint() : lastResumePoint_;
    LSnapshot* postSnapshot = buildSnapshot(ins, mrp, kind);
    if (!postSnapshot) {
        gen->abort("buildSnapshot failed");
        return;
    }

    osiPoint_ = new(alloc()) LOsiPoint(ins->safepoint(), postSnapshot);

    if (!lirGraph_.noteNeedsSafepoint(ins))
        gen->abort("noteNeedsSafepoint failed");
}