58
58
#ifndef LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
59
59
#define LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
60
60
61
+ #include " llvm/IR/Function.h"
61
62
#include " llvm/IR/User.h"
62
63
#include " llvm/IR/Value.h"
63
64
#include " llvm/Support/raw_ostream.h"
@@ -129,6 +130,35 @@ class Value {
129
130
void dumpCommonPrefix (raw_ostream &OS) const ;
130
131
void dumpCommonSuffix (raw_ostream &OS) const ;
131
132
void printAsOperandCommon (raw_ostream &OS) const ;
133
+ friend raw_ostream &operator <<(raw_ostream &OS, const sandboxir::Value &V) {
134
+ V.dump (OS);
135
+ return OS;
136
+ }
137
+ virtual void dump (raw_ostream &OS) const = 0;
138
+ LLVM_DUMP_METHOD virtual void dump () const = 0;
139
+ #endif
140
+ };
141
+
142
+ // / Argument of a sandboxir::Function.
143
+ class Argument : public sandboxir ::Value {
144
+ public:
145
+ Argument (llvm::Argument *Arg, sandboxir::Context &Ctx)
146
+ : sandboxir::Value(ClassID::Argument, Arg, Ctx) {}
147
+ static bool classof (const sandboxir::Value *From) {
148
+ return From->getSubclassID () == ClassID::Argument;
149
+ }
150
+ #ifndef NDEBUG
151
+ void verify () const final {
152
+ assert (isa<llvm::Argument>(Val) && " Expected Argument!" );
153
+ }
154
+ friend raw_ostream &operator <<(raw_ostream &OS,
155
+ const sandboxir::Argument &TArg) {
156
+ TArg.dump (OS);
157
+ return OS;
158
+ }
159
+ void printAsOperand (raw_ostream &OS) const ;
160
+ void dump (raw_ostream &OS) const final ;
161
+ LLVM_DUMP_METHOD void dump () const final ;
132
162
#endif
133
163
};
134
164
@@ -142,16 +172,137 @@ class User : public Value {
142
172
assert (isa<llvm::User>(Val) && " Expected User!" );
143
173
}
144
174
void dumpCommonHeader (raw_ostream &OS) const final ;
175
+ void dump (raw_ostream &OS) const override {
176
+ // TODO: Remove this tmp implementation once we get the Instruction classes.
177
+ }
178
+ LLVM_DUMP_METHOD void dump () const override {
179
+ // TODO: Remove this tmp implementation once we get the Instruction classes.
180
+ }
181
+ #endif
182
+ };
183
+
184
+ class Constant : public sandboxir ::User {
185
+ public:
186
+ Constant (llvm::Constant *C, sandboxir::Context &SBCtx)
187
+ : sandboxir::User(ClassID::Constant, C, SBCtx) {}
188
+ // / For isa/dyn_cast.
189
+ static bool classof (const sandboxir::Value *From) {
190
+ return From->getSubclassID () == ClassID::Constant ||
191
+ From->getSubclassID () == ClassID::Function;
192
+ }
193
+ sandboxir::Context &getParent () const { return getContext (); }
194
+ #ifndef NDEBUG
195
+ void verify () const final {
196
+ assert (isa<llvm::Constant>(Val) && " Expected Constant!" );
197
+ }
198
+ friend raw_ostream &operator <<(raw_ostream &OS,
199
+ const sandboxir::Constant &SBC) {
200
+ SBC.dump (OS);
201
+ return OS;
202
+ }
203
+ void dump (raw_ostream &OS) const override ;
204
+ LLVM_DUMP_METHOD void dump () const override ;
205
+ #endif
206
+ };
207
+
208
+ // / A sandboxir::User with operands and opcode.
209
+ class Instruction : public sandboxir ::User {
210
+ public:
211
+ enum class Opcode {
212
+ #define DEF_VALUE (ID, CLASS )
213
+ #define DEF_USER (ID, CLASS )
214
+ #define OP (OPC ) OPC,
215
+ #define OPCODES (...) __VA_ARGS__
216
+ #define DEF_INSTR (ID, OPC, CLASS ) OPC
217
+ #include " llvm/SandboxIR/SandboxIRValues.def"
218
+ };
219
+
220
+ Instruction (ClassID ID, Opcode Opc, llvm::Instruction *I,
221
+ sandboxir::Context &SBCtx)
222
+ : sandboxir::User(ID, I, SBCtx), Opc(Opc) {}
223
+
224
+ protected:
225
+ Opcode Opc;
226
+
227
+ public:
228
+ static const char *getOpcodeName (Opcode Opc);
229
+ #ifndef NDEBUG
230
+ friend raw_ostream &operator <<(raw_ostream &OS, Opcode Opc) {
231
+ OS << getOpcodeName (Opc);
232
+ return OS;
233
+ }
234
+ #endif
235
+ // / For isa/dyn_cast.
236
+ static bool classof (const sandboxir::Value *From);
237
+
238
+ #ifndef NDEBUG
239
+ friend raw_ostream &operator <<(raw_ostream &OS,
240
+ const sandboxir::Instruction &SBI) {
241
+ SBI.dump (OS);
242
+ return OS;
243
+ }
244
+ void dump (raw_ostream &OS) const override ;
245
+ LLVM_DUMP_METHOD void dump () const override ;
246
+ #endif
247
+ };
248
+
249
+ // / An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
250
+ // / an OpaqueInstr.
251
+ class OpaqueInst : public sandboxir ::Instruction {
252
+ public:
253
+ OpaqueInst (llvm::Instruction *I, sandboxir::Context &Ctx)
254
+ : sandboxir::Instruction(ClassID::Opaque, Opcode::Opaque, I, Ctx) {}
255
+ OpaqueInst (ClassID SubclassID, llvm::Instruction *I, sandboxir::Context &Ctx)
256
+ : sandboxir::Instruction(SubclassID, Opcode::Opaque, I, Ctx) {}
257
+ static bool classof (const sandboxir::Value *From) {
258
+ return From->getSubclassID () == ClassID::Opaque;
259
+ }
260
+ #ifndef NDEBUG
261
+ void verify () const final {
262
+ // Nothing to do
263
+ }
264
+ friend raw_ostream &operator <<(raw_ostream &OS,
265
+ const sandboxir::OpaqueInst &OI) {
266
+ OI.dump (OS);
267
+ return OS;
268
+ }
269
+ void dump (raw_ostream &OS) const override ;
270
+ LLVM_DUMP_METHOD void dump () const override ;
145
271
#endif
146
272
};
147
273
148
274
class Context {
149
275
protected:
150
276
LLVMContext &LLVMCtx;
277
+ // / Maps LLVM Value to the corresponding sandboxir::Value. Owns all
278
+ // / SandboxIR objects.
279
+ DenseMap<llvm::Value *, std::unique_ptr<sandboxir::Value>>
280
+ LLVMValueToSBValueMap;
151
281
152
282
public:
153
283
Context (LLVMContext &LLVMCtx) : LLVMCtx(LLVMCtx) {}
284
+ sandboxir::Value *getValue (llvm::Value *V) const ;
285
+ };
286
+
287
+ class Function : public sandboxir ::Value {
288
+ public:
289
+ Function (llvm::Function *F, sandboxir::Context &Ctx)
290
+ : sandboxir::Value(ClassID::Function, F, Ctx) {}
291
+ // / For isa/dyn_cast.
292
+ static bool classof (const sandboxir::Value *From) {
293
+ return From->getSubclassID () == ClassID::Function;
294
+ }
295
+
296
+ #ifndef NDEBUG
297
+ void verify () const final {
298
+ assert (isa<llvm::Function>(Val) && " Expected Function!" );
299
+ }
300
+ void dumpNameAndArgs (raw_ostream &OS) const ;
301
+ void dump (raw_ostream &OS) const final ;
302
+ LLVM_DUMP_METHOD void dump () const final ;
303
+ #endif
154
304
};
305
+
155
306
} // namespace sandboxir
156
307
} // namespace llvm
157
308
0 commit comments