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,136 @@ 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 DEF_INSTR (ID, OPC, CLASS ) OPC
216
+ #include " llvm/SandboxIR/SandboxIRValues.def"
217
+ };
218
+
219
+ Instruction (ClassID ID, Opcode Opc, llvm::Instruction *I,
220
+ sandboxir::Context &SBCtx)
221
+ : sandboxir::User(ID, I, SBCtx), Opc(Opc) {}
222
+
223
+ protected:
224
+ Opcode Opc;
225
+
226
+ public:
227
+ static const char *getOpcodeName (Opcode Opc);
228
+ #ifndef NDEBUG
229
+ friend raw_ostream &operator <<(raw_ostream &OS, Opcode Opc) {
230
+ OS << getOpcodeName (Opc);
231
+ return OS;
232
+ }
233
+ #endif
234
+ // / For isa/dyn_cast.
235
+ static bool classof (const sandboxir::Value *From);
236
+
237
+ #ifndef NDEBUG
238
+ friend raw_ostream &operator <<(raw_ostream &OS,
239
+ const sandboxir::Instruction &SBI) {
240
+ SBI.dump (OS);
241
+ return OS;
242
+ }
243
+ void dump (raw_ostream &OS) const override ;
244
+ LLVM_DUMP_METHOD void dump () const override ;
245
+ #endif
246
+ };
247
+
248
+ // / An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
249
+ // / an OpaqueInstr.
250
+ class OpaqueInst : public sandboxir ::Instruction {
251
+ public:
252
+ OpaqueInst (llvm::Instruction *I, sandboxir::Context &Ctx)
253
+ : sandboxir::Instruction(ClassID::Opaque, Opcode::Opaque, I, Ctx) {}
254
+ OpaqueInst (ClassID SubclassID, llvm::Instruction *I, sandboxir::Context &Ctx)
255
+ : sandboxir::Instruction(SubclassID, Opcode::Opaque, I, Ctx) {}
256
+ static bool classof (const sandboxir::Value *From) {
257
+ return From->getSubclassID () == ClassID::Opaque;
258
+ }
259
+ #ifndef NDEBUG
260
+ void verify () const final {
261
+ // Nothing to do
262
+ }
263
+ friend raw_ostream &operator <<(raw_ostream &OS,
264
+ const sandboxir::OpaqueInst &OI) {
265
+ OI.dump (OS);
266
+ return OS;
267
+ }
268
+ void dump (raw_ostream &OS) const override ;
269
+ LLVM_DUMP_METHOD void dump () const override ;
145
270
#endif
146
271
};
147
272
148
273
class Context {
149
274
protected:
150
275
LLVMContext &LLVMCtx;
276
+ // / Maps LLVM Value to the corresponding sandboxir::Value. Owns all
277
+ // / SandboxIR objects.
278
+ DenseMap<llvm::Value *, std::unique_ptr<sandboxir::Value>>
279
+ LLVMValueToValueMap;
151
280
152
281
public:
153
282
Context (LLVMContext &LLVMCtx) : LLVMCtx(LLVMCtx) {}
283
+ sandboxir::Value *getValue (llvm::Value *V) const ;
284
+ };
285
+
286
+ class Function : public sandboxir ::Value {
287
+ public:
288
+ Function (llvm::Function *F, sandboxir::Context &Ctx)
289
+ : sandboxir::Value(ClassID::Function, F, Ctx) {}
290
+ // / For isa/dyn_cast.
291
+ static bool classof (const sandboxir::Value *From) {
292
+ return From->getSubclassID () == ClassID::Function;
293
+ }
294
+
295
+ #ifndef NDEBUG
296
+ void verify () const final {
297
+ assert (isa<llvm::Function>(Val) && " Expected Function!" );
298
+ }
299
+ void dumpNameAndArgs (raw_ostream &OS) const ;
300
+ void dump (raw_ostream &OS) const final ;
301
+ LLVM_DUMP_METHOD void dump () const final ;
302
+ #endif
154
303
};
304
+
155
305
} // namespace sandboxir
156
306
} // namespace llvm
157
307
0 commit comments