59
59
#define LLVM_SANDBOXIR_SANDBOXIR_H
60
60
61
61
#include " llvm/IR/Function.h"
62
+ #include " llvm/IR/IRBuilder.h"
62
63
#include " llvm/IR/User.h"
63
64
#include " llvm/IR/Value.h"
64
65
#include " llvm/SandboxIR/Tracker.h"
@@ -74,6 +75,7 @@ class BasicBlock;
74
75
class Context ;
75
76
class Function ;
76
77
class Instruction ;
78
+ class LoadInst ;
77
79
class User ;
78
80
class Value ;
79
81
@@ -170,9 +172,10 @@ class Value {
170
172
// / order.
171
173
llvm::Value *Val = nullptr ;
172
174
173
- friend class Context ; // For getting `Val`.
174
- friend class User ; // For getting `Val`.
175
- friend class Use ; // For getting `Val`.
175
+ friend class Context ; // For getting `Val`.
176
+ friend class User ; // For getting `Val`.
177
+ friend class Use ; // For getting `Val`.
178
+ friend class LoadInst ; // For getting `Val`.
176
179
177
180
// / All values point to the context.
178
181
Context &Ctx;
@@ -262,11 +265,14 @@ class Value {
262
265
llvm::function_ref<bool (const Use &)> ShouldReplace);
263
266
void replaceAllUsesWith (Value *Other);
264
267
268
+ // / \Returns the LLVM IR name of the bottom-most LLVM value.
269
+ StringRef getName () const { return Val->getName (); }
270
+
265
271
#ifndef NDEBUG
266
272
// / Should crash if there is something wrong with the instruction.
267
273
virtual void verify () const = 0;
268
- // / Returns the name in the form 'SB<number>.' like 'SB1.'
269
- std::string getName () const ;
274
+ // / Returns the unique id in the form 'SB<number>.' like 'SB1.'
275
+ std::string getUid () const ;
270
276
virtual void dumpCommonHeader (raw_ostream &OS) const ;
271
277
void dumpCommonFooter (raw_ostream &OS) const ;
272
278
void dumpCommonPrefix (raw_ostream &OS) const ;
@@ -489,6 +495,7 @@ class Instruction : public sandboxir::User {
489
495
// / A SandboxIR Instruction may map to multiple LLVM IR Instruction. This
490
496
// / returns its topmost LLVM IR instruction.
491
497
llvm::Instruction *getTopmostLLVMInstruction () const ;
498
+ friend class LoadInst ; // For getTopmostLLVMInstruction().
492
499
493
500
// / \Returns the LLVM IR Instructions that this SandboxIR maps to in program
494
501
// / order.
@@ -553,6 +560,45 @@ class Instruction : public sandboxir::User {
553
560
#endif
554
561
};
555
562
563
+ class LoadInst final : public Instruction {
564
+ // / Use LoadInst::create() instead of calling the constructor.
565
+ LoadInst (llvm::LoadInst *LI, Context &Ctx)
566
+ : Instruction(ClassID::Load, Opcode::Load, LI, Ctx) {}
567
+ friend Context; // for LoadInst()
568
+ Use getOperandUseInternal (unsigned OpIdx, bool Verify) const final {
569
+ return getOperandUseDefault (OpIdx, Verify);
570
+ }
571
+ SmallVector<llvm::Instruction *, 1 > getLLVMInstrs () const final {
572
+ return {cast<llvm::Instruction>(Val)};
573
+ }
574
+
575
+ public:
576
+ unsigned getUseOperandNo (const Use &Use) const final {
577
+ return getUseOperandNoDefault (Use);
578
+ }
579
+
580
+ unsigned getNumOfIRInstrs () const final { return 1u ; }
581
+ static LoadInst *create (Type *Ty, Value *Ptr, MaybeAlign Align,
582
+ Instruction *InsertBefore, Context &Ctx,
583
+ const Twine &Name = " " );
584
+ static LoadInst *create (Type *Ty, Value *Ptr, MaybeAlign Align,
585
+ BasicBlock *InsertAtEnd, Context &Ctx,
586
+ const Twine &Name = " " );
587
+ // / For isa/dyn_cast.
588
+ static bool classof (const Value *From);
589
+ Value *getPointerOperand () const ;
590
+ Align getAlign () const { return cast<llvm::LoadInst>(Val)->getAlign (); }
591
+ bool isUnordered () const { return cast<llvm::LoadInst>(Val)->isUnordered (); }
592
+ bool isSimple () const { return cast<llvm::LoadInst>(Val)->isSimple (); }
593
+ #ifndef NDEBUG
594
+ void verify () const final {
595
+ assert (isa<llvm::LoadInst>(Val) && " Expected LoadInst!" );
596
+ }
597
+ void dump (raw_ostream &OS) const override ;
598
+ LLVM_DUMP_METHOD void dump () const override ;
599
+ #endif
600
+ };
601
+
556
602
// / An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
557
603
// / an OpaqueInstr.
558
604
class OpaqueInst : public sandboxir ::Instruction {
@@ -683,8 +729,16 @@ class Context {
683
729
684
730
friend class BasicBlock ; // For getOrCreateValue().
685
731
732
+ IRBuilder<ConstantFolder> LLVMIRBuilder;
733
+ auto &getLLVMIRBuilder () { return LLVMIRBuilder; }
734
+
735
+ LoadInst *createLoadInst (llvm::LoadInst *LI);
736
+ friend LoadInst; // For createLoadInst()
737
+
686
738
public:
687
- Context (LLVMContext &LLVMCtx) : LLVMCtx(LLVMCtx), IRTracker(*this ) {}
739
+ Context (LLVMContext &LLVMCtx)
740
+ : LLVMCtx(LLVMCtx), IRTracker(*this ),
741
+ LLVMIRBuilder (LLVMCtx, ConstantFolder()) {}
688
742
689
743
Tracker &getTracker () { return IRTracker; }
690
744
// / Convenience function for `getTracker().save()`
0 commit comments