1/*
2 * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "AirLowerMacros.h"
28
29#if ENABLE(B3_JIT)
30
31#include "AirCCallingConvention.h"
32#include "AirCode.h"
33#include "AirEmitShuffle.h"
34#include "AirInsertionSet.h"
35#include "AirInstInlines.h"
36#include "AirPhaseScope.h"
37#include "B3CCallValue.h"
38#include "B3ValueInlines.h"
39
40namespace JSC { namespace B3 { namespace Air {
41
42void lowerMacros(Code& code)
43{
44 PhaseScope phaseScope(code, "Air::lowerMacros");
45
46 InsertionSet insertionSet(code);
47 for (BasicBlock* block : code) {
48 for (unsigned instIndex = 0; instIndex < block->size(); ++instIndex) {
49 Inst& inst = block->at(instIndex);
50
51 auto handleCall = [&] () {
52 CCallValue* value = inst.origin->as<CCallValue>();
53 Kind oldKind = inst.kind;
54
55 Vector<Arg> destinations = computeCCallingConvention(code, value);
56
57 unsigned offset = value->type() == Void ? 0 : 1;
58 Vector<ShufflePair, 16> shufflePairs;
59 bool hasRegisterSource = false;
60 for (unsigned i = 1; i < destinations.size(); ++i) {
61 Value* child = value->child(i);
62 ShufflePair pair(inst.args[offset + i], destinations[i], widthForType(child->type()));
63 shufflePairs.append(pair);
64 hasRegisterSource |= pair.src().isReg();
65 }
66
67 if (UNLIKELY(hasRegisterSource))
68 insertionSet.insertInst(instIndex, createShuffle(inst.origin, Vector<ShufflePair>(shufflePairs)));
69 else {
70 // If none of the inputs are registers, then we can efficiently lower this
71 // shuffle before register allocation. First we lower all of the moves to
72 // memory, in the hopes that this is the last use of the operands. This
73 // avoids creating interference between argument registers and arguments
74 // that don't go into argument registers.
75 for (ShufflePair& pair : shufflePairs) {
76 if (pair.dst().isMemory())
77 insertionSet.insertInsts(instIndex, pair.insts(code, inst.origin));
78 }
79
80 // Fill the argument registers by starting with the first one. This avoids
81 // creating interference between things passed to low-numbered argument
82 // registers and high-numbered argument registers. The assumption here is
83 // that lower-numbered argument registers are more likely to be
84 // incidentally clobbered.
85 for (ShufflePair& pair : shufflePairs) {
86 if (!pair.dst().isMemory())
87 insertionSet.insertInsts(instIndex, pair.insts(code, inst.origin));
88 }
89 }
90
91 // Indicate that we're using our original callee argument.
92 destinations[0] = inst.args[0];
93
94 // Save where the original instruction put its result.
95 Arg resultDst = value->type() == Void ? Arg() : inst.args[1];
96
97 inst = buildCCall(code, inst.origin, destinations);
98 if (oldKind.effects)
99 inst.kind.effects = true;
100
101 Tmp result = cCallResult(value->type());
102 switch (value->type().kind()) {
103 case Void:
104 case Tuple:
105 break;
106 case Float:
107 insertionSet.insert(instIndex + 1, MoveFloat, value, result, resultDst);
108 break;
109 case Double:
110 insertionSet.insert(instIndex + 1, MoveDouble, value, result, resultDst);
111 break;
112 case Int32:
113 insertionSet.insert(instIndex + 1, Move32, value, result, resultDst);
114 break;
115 case Int64:
116 insertionSet.insert(instIndex + 1, Move, value, result, resultDst);
117 break;
118 }
119 };
120
121 switch (inst.kind.opcode) {
122 case ColdCCall:
123 if (code.optLevel() < 2)
124 handleCall();
125 break;
126
127 case CCall:
128 handleCall();
129 break;
130
131 default:
132 break;
133 }
134 }
135 insertionSet.execute(block);
136 }
137}
138
139} } } // namespace JSC::B3::Air
140
141#endif // ENABLE(B3_JIT)
142
143