1/*
2 * Copyright (C) 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 "AirBasicBlock.h"
31#include "AirCode.h"
32#include "AirInst.h"
33#include "AirLiveness.h"
34#include "RegisterSet.h"
35#include <wtf/IndexMap.h>
36
37namespace JSC { namespace B3 { namespace Air {
38
39// Although we could trivially adapt Air::Liveness<> to work with Reg, this would not be so
40// efficient. There is a small number of registers, so it's much better to use bitvectors for
41// register liveness. This is a specialization of Liveness<> that uses bitvectors directly.
42// This makes the code sufficiently different that it didn't make sense to try to share code.
43class RegLiveness {
44 struct Actions {
45 Actions() { }
46
47 RegisterSet use;
48 RegisterSet def;
49 };
50
51 typedef Vector<Actions, 0, UnsafeVectorOverflow> ActionsForBoundary;
52
53public:
54 typedef Reg Thing;
55
56 RegLiveness(Code& code);
57 ~RegLiveness();
58
59 class LocalCalcBase {
60 public:
61 LocalCalcBase(BasicBlock* block)
62 : m_block(block)
63 {
64 }
65
66 const RegisterSet& live() const
67 {
68 return m_workset;
69 }
70
71 bool isLive(Reg reg) const
72 {
73 return m_workset.contains(reg);
74 }
75
76 protected:
77 BasicBlock* m_block;
78 RegisterSet m_workset;
79 };
80
81 // This calculator has to be run in reverse.
82 class LocalCalc : public LocalCalcBase {
83 public:
84 LocalCalc(RegLiveness& liveness, BasicBlock* block)
85 : LocalCalcBase(block)
86 , m_actions(liveness.m_actions[block])
87 {
88 m_workset = liveness.m_liveAtTail[block];
89 }
90
91 void execute(unsigned instIndex)
92 {
93 m_workset.exclude(m_actions[instIndex + 1].def);
94 m_workset.merge(m_actions[instIndex].use);
95 }
96
97 private:
98 friend class RegLiveness;
99
100 ActionsForBoundary& m_actions;
101 };
102
103 class LocalCalcForUnifiedTmpLiveness : public LocalCalcBase {
104 public:
105 LocalCalcForUnifiedTmpLiveness(UnifiedTmpLiveness& liveness, BasicBlock* block);
106
107 void execute(unsigned instIndex);
108
109 private:
110 Code& m_code;
111 UnifiedTmpLiveness::ActionsForBoundary& m_actions;
112 };
113
114 const RegisterSet& liveAtHead(BasicBlock* block) const
115 {
116 return m_liveAtHead[block];
117 }
118
119 const RegisterSet& liveAtTail(BasicBlock* block) const
120 {
121 return m_liveAtTail[block];
122 }
123
124private:
125 IndexMap<BasicBlock*, RegisterSet> m_liveAtHead;
126 IndexMap<BasicBlock*, RegisterSet> m_liveAtTail;
127 IndexMap<BasicBlock*, ActionsForBoundary> m_actions;
128};
129
130} } } // namespace JSC::B3::Air
131
132#endif // ENABLE(B3_JIT)
133
134