5959#define LLVM_SANDBOXIR_SANDBOXIR_H
6060
6161#include " llvm/IR/Function.h"
62+ #include " llvm/IR/IRBuilder.h"
6263#include " llvm/IR/User.h"
6364#include " llvm/IR/Value.h"
6465#include " llvm/SandboxIR/Tracker.h"
@@ -74,6 +75,7 @@ class BasicBlock;
7475class Context ;
7576class Function ;
7677class Instruction ;
78+ class LoadInst ;
7779class User ;
7880class Value ;
7981
@@ -170,9 +172,10 @@ class Value {
170172 // / order.
171173 llvm::Value *Val = nullptr ;
172174
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`.
176179
177180 // / All values point to the context.
178181 Context &Ctx;
@@ -262,11 +265,14 @@ class Value {
262265 llvm::function_ref<bool (const Use &)> ShouldReplace);
263266 void replaceAllUsesWith (Value *Other);
264267
268+ // / \Returns the LLVM IR name of the bottom-most LLVM value.
269+ StringRef getName () const { return Val->getName (); }
270+
265271#ifndef NDEBUG
266272 // / Should crash if there is something wrong with the instruction.
267273 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 ;
270276 virtual void dumpCommonHeader (raw_ostream &OS) const ;
271277 void dumpCommonFooter (raw_ostream &OS) const ;
272278 void dumpCommonPrefix (raw_ostream &OS) const ;
@@ -489,6 +495,7 @@ class Instruction : public sandboxir::User {
489495 // / A SandboxIR Instruction may map to multiple LLVM IR Instruction. This
490496 // / returns its topmost LLVM IR instruction.
491497 llvm::Instruction *getTopmostLLVMInstruction () const ;
498+ friend class LoadInst ; // For getTopmostLLVMInstruction().
492499
493500 // / \Returns the LLVM IR Instructions that this SandboxIR maps to in program
494501 // / order.
@@ -553,6 +560,45 @@ class Instruction : public sandboxir::User {
553560#endif
554561};
555562
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+
556602// / An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
557603// / an OpaqueInstr.
558604class OpaqueInst : public sandboxir ::Instruction {
@@ -683,8 +729,16 @@ class Context {
683729
684730 friend class BasicBlock ; // For getOrCreateValue().
685731
732+ IRBuilder<ConstantFolder> LLVMIRBuilder;
733+ auto &getLLVMIRBuilder () { return LLVMIRBuilder; }
734+
735+ LoadInst *createLoadInst (llvm::LoadInst *LI);
736+ friend LoadInst; // For createLoadInst()
737+
686738public:
687- Context (LLVMContext &LLVMCtx) : LLVMCtx(LLVMCtx), IRTracker(*this ) {}
739+ Context (LLVMContext &LLVMCtx)
740+ : LLVMCtx(LLVMCtx), IRTracker(*this ),
741+ LLVMIRBuilder (LLVMCtx, ConstantFolder()) {}
688742
689743 Tracker &getTracker () { return IRTracker; }
690744 // / Convenience function for `getTracker().save()`
0 commit comments