forked from stichnot/subzero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIceDefs.h
200 lines (173 loc) · 5.87 KB
/
IceDefs.h
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
// -*- Mode: c++ -*-
/* Copyright 2014 The Native Client Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can
* be found in the LICENSE file.
*/
#ifndef _IceDefs_h
#define _IceDefs_h
#include <assert.h>
#include <stdint.h>
#include <stdio.h> // sprintf
#include <list>
#include <map>
#include <ostream>
#include <set>
#include <string>
#include <vector>
// See http://llvm.org/docs/ProgrammersManual.html#isa
#include "llvm/Support/Casting.h"
#include "llvm/Support/Timer.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/SmallBitVector.h"
class IceCfg;
class IceCfgNode;
class IceInst;
class IceInstPhi;
class IceInstTarget;
class IceLiveness;
class IceLiveRange;
class IceOperand;
class IceVariable;
class IceConstant;
class IceTargetLowering;
// typedefs of containers
// TODO: Switch over to LLVM's ADT container classes.
// http://llvm.org/docs/ProgrammersManual.html#picking-the-right-data-structure-for-a-task
typedef std::string IceString;
typedef std::list<IceInst *> IceInstList;
typedef std::list<IceInstPhi *> IcePhiList;
typedef std::vector<IceOperand *> IceOpList;
typedef std::vector<IceVariable *> IceVarList;
typedef std::vector<IceCfgNode *> IceNodeList;
// The IceOstream class wraps a std::ostream and an IceCfg pointer, so
// that dump routines have access to the IceCfg object and can print
// labels and variable names.
enum IceVerbose {
IceV_None = 0,
IceV_Instructions = 1 << 0,
IceV_Deleted = 1 << 1,
IceV_InstNumbers = 1 << 2,
IceV_Preds = 1 << 3,
IceV_Succs = 1 << 4,
IceV_Liveness = 1 << 5,
IceV_RegManager = 1 << 6,
IceV_RegOrigins = 1 << 7,
IceV_LinearScan = 1 << 8,
IceV_Frame = 1 << 9,
IceV_Timing = 1 << 10,
IceV_All = ~IceV_None
};
typedef uint32_t IceVerboseMask;
enum IceLivenessMode {
// Lightweight version of live-range-end calculation. Marks the
// last use of variables whose definition and uses are completely
// within a single block.
IceLiveness_LREndLightweight,
// Full version of live-range-end calculation. Marks the last uses
// of variables based on dataflow analysis. Records the set of
// live-in and live-out variables for each block. Identifies and
// deletes dead instructions (primarily stores).
IceLiveness_LREndFull,
// In addition to IceLiveness_LREndFull, also calculate the complete
// live range for each variable in a form suitable for interference
// calculation and register allocation.
IceLiveness_RangesFull
};
// This is a convenience templated class that provides a mapping
// between a parameterized type and small unsigned integers. The
// small integer is meant to be the index for an IceCfgNode or
// IceVariable. If the ICE is generated directly from the bitcode,
// this won't be necessary, but it is helpful for manual generation or
// generation from a different IR such as translating from LLVM.
template <typename T> class IceValueTranslation {
public:
typedef typename std::map<const T, uint32_t> ContainerType;
IceValueTranslation(void) {}
void clear(void) { Entries.clear(); }
uint32_t translate(const T &Value) {
typename ContainerType::const_iterator Iter = Entries.find(Value);
if (Iter != Entries.end())
return Iter->second;
uint32_t Index = Entries.size();
Index *= 2; // TODO: this adds holes to the index space for testing; remove.
Entries[Value] = Index;
return Index;
}
private:
ContainerType Entries;
};
class IceOstream {
public:
IceOstream(std::ostream &Stream, IceCfg *Cfg)
: Stream(&Stream), Cfg(Cfg), Verbose(IceV_Instructions | IceV_Preds),
CurrentNode(NULL) {}
bool isVerbose(IceVerboseMask Mask = (IceV_All & ~IceV_Timing)) {
return Verbose & Mask;
}
void setVerbose(IceVerboseMask Mask) { Verbose = Mask; }
void addVerbose(IceVerboseMask Mask) { Verbose |= Mask; }
void subVerbose(IceVerboseMask Mask) { Verbose &= ~Mask; }
void setCurrentNode(const IceCfgNode *Node) { CurrentNode = Node; }
const IceCfgNode *getCurrentNode(void) const { return CurrentNode; }
// TODO: Use LLVM's raw_ostream instead.
// http://llvm.org/docs/CodingStandards.html#use-raw-ostream
std::ostream *Stream;
IceCfg *const Cfg;
private:
IceVerboseMask Verbose;
// CurrentNode is maintained during dumping/emitting just for
// validating IceVariable::DefOrUseNode.
const IceCfgNode *CurrentNode;
};
inline IceOstream &operator<<(IceOstream &Str, const char *S) {
*(Str.Stream) << S;
return Str;
}
inline IceOstream &operator<<(IceOstream &Str, const IceString &S) {
*(Str.Stream) << S;
return Str;
}
inline IceOstream &operator<<(IceOstream &Str, uint32_t U) {
*(Str.Stream) << U;
return Str;
}
inline IceOstream &operator<<(IceOstream &Str, int32_t I) {
*(Str.Stream) << I;
return Str;
}
inline IceOstream &operator<<(IceOstream &Str, uint64_t U) {
*(Str.Stream) << U;
return Str;
}
inline IceOstream &operator<<(IceOstream &Str, int64_t I) {
*(Str.Stream) << I;
return Str;
}
inline IceOstream &operator<<(IceOstream &Str, double D) {
*(Str.Stream) << D;
return Str;
}
// GlobalStr is just for debugging, in situations where the
// IceCfg/IceOstream objects aren't otherwise available. Not
// thread-safe.
extern IceOstream *GlobalStr;
class IceTimer {
public:
IceTimer(void) : Start(llvm::TimeRecord::getCurrentTime(false)) {}
uint64_t getElapsedNs(void) const {
return getElapsedSec() * 1000 * 1000 * 1000;
}
uint64_t getElapsedUs(void) const { return getElapsedSec() * 1000 * 1000; }
uint64_t getElapsedMs(void) const { return getElapsedSec() * 1000; }
double getElapsedSec(void) const {
llvm::TimeRecord End = llvm::TimeRecord::getCurrentTime(false);
return End.getWallTime() - Start.getWallTime();
}
void printElapsedUs(IceOstream &Str, const IceString &Tag) const {
if (Str.isVerbose(IceV_Timing))
Str << "# " << getElapsedUs() << " usec " << Tag << "\n";
}
private:
const llvm::TimeRecord Start;
};
#endif // _IceDefs_h