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#pragma once
27
28#if ENABLE(B3_JIT)
29
30#include "AirArg.h"
31#include "AirInst.h"
32#include <wtf/Vector.h>
33
34namespace JSC { namespace B3 {
35
36class Value;
37
38namespace Air {
39
40class Code;
41
42inline Opcode moveFor(Bank bank, Width width)
43{
44 switch (width) {
45 case Width32:
46 return bank == GP ? Move32 : MoveFloat;
47 case Width64:
48 return bank == GP ? Move : MoveDouble;
49 default:
50 RELEASE_ASSERT_NOT_REACHED();
51 return Oops;
52 }
53}
54
55class ShufflePair {
56public:
57 ShufflePair()
58 {
59 }
60
61 ShufflePair(const Arg& src, const Arg& dst, Width width)
62 : m_src(src)
63 , m_dst(dst)
64 , m_width(width)
65 {
66 }
67
68 const Arg& src() const { return m_src; }
69 const Arg& dst() const { return m_dst; }
70
71 // The width determines the kind of move we do. You can only choose Width32 or Width64 right now.
72 // For GP, it picks between Move32 and Move. For FP, it picks between MoveFloat and MoveDouble.
73 Width width() const { return m_width; }
74
75 Bank bank() const;
76
77 // Creates an instruction sequence for the move represented by this shuffle pair.
78 // You need to pass Code because we may need to create a tmp.
79 Vector<Inst, 2> insts(Code&, Value* origin) const;
80
81 void dump(PrintStream&) const;
82
83private:
84 Arg m_src;
85 Arg m_dst;
86 Width m_width { Width8 };
87};
88
89// Create a Shuffle instruction.
90Inst createShuffle(Value* origin, const Vector<ShufflePair>&);
91
92// Perform a shuffle of a given type. The scratch argument is mandatory. You should pass it as
93// follows: If you know that you have scratch registers or temporaries available - that is, they're
94// registers that are not mentioned in the shuffle, have the same type as the shuffle, and are not
95// live at the shuffle - then you can pass them. If you don't have scratch registers available or if
96// you don't feel like looking for them, you can pass memory locations. It's always safe to pass a
97// pair of memory locations, and replacing either memory location with a register can be viewed as an
98// optimization. It's a pretty important optimization. Some more notes:
99//
100// - We define scratch registers as things that are not live before the shuffle and are not one of
101// the destinations of the shuffle. Not being live before the shuffle also means that they cannot
102// be used for any of the sources of the shuffle.
103//
104// - A second scratch location is only needed when you have shuffle pairs where memory is used both
105// as source and destination.
106//
107// - You're guaranteed not to need any scratch locations if there is a Swap instruction available for
108// the type and you don't have any memory locations that are both the source and the destination of
109// some pairs. GP supports Swap on x86 while FP never supports Swap.
110//
111// - Passing memory locations as scratch if are running emitShuffle() before register allocation is
112// silly, since that will cause emitShuffle() to pick some specific registers when it does need
113// scratch. One easy way to avoid that predicament is to ensure that you call emitShuffle() after
114// register allocation. For this reason we could add a Shuffle instruction so that we can defer
115// shufflings until after regalloc.
116//
117// - Shuffles with memory=>memory pairs are not very well tuned. You should avoid them if you want
118// performance. If you need to do them, then making sure that you reserve a temporary is one way to
119// get acceptable performance.
120//
121// NOTE: Use this method (and its friend below) to emit shuffles after register allocation. Before
122// register allocation it is much better to simply use the Shuffle instruction.
123Vector<Inst> emitShuffle(
124 Code& code, Vector<ShufflePair>, std::array<Arg, 2> scratch, Bank, Value* origin);
125
126// Perform a shuffle that involves any number of types. Pass scratch registers or memory locations
127// for each type according to the rules above.
128Vector<Inst> emitShuffle(
129 Code& code, const Vector<ShufflePair>&,
130 const std::array<Arg, 2>& gpScratch, const std::array<Arg, 2>& fpScratch,
131 Value* origin);
132
133} } } // namespace JSC::B3::Air
134
135#endif // ENABLE(B3_JIT)
136