LLVM 20.0.0git
SandboxIR.h
Go to the documentation of this file.
1//===- SandboxIR.h ----------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Sandbox IR is a lightweight overlay transactional IR on top of LLVM IR.
10// Features:
11// - You can save/rollback the state of the IR at any time.
12// - Any changes made to Sandbox IR will automatically update the underlying
13// LLVM IR so both IRs are always in sync.
14// - Feels like LLVM IR, similar API.
15//
16// SandboxIR forms a class hierarchy that resembles that of LLVM IR
17// but is in the `sandboxir` namespace:
18//
19// namespace sandboxir {
20//
21// Value -+- Argument
22// |
23// +- BasicBlock
24// |
25// +- User ------+- Constant ------ Function
26// |
27// +- Instruction -+- BinaryOperator
28// |
29// +- BranchInst
30// |
31// +- CastInst --------+- AddrSpaceCastInst
32// | |
33// | +- BitCastInst
34// | |
35// | +- FPExtInst
36// | |
37// | +- FPToSIInst
38// | |
39// | +- FPToUIInst
40// | |
41// | +- FPTruncInst
42// | |
43// | +- IntToPtrInst
44// | |
45// | +- PtrToIntInst
46// | |
47// | +- SExtInst
48// | |
49// | +- SIToFPInst
50// | |
51// | +- TruncInst
52// | |
53// | +- UIToFPInst
54// | |
55// | +- ZExtInst
56// |
57// +- CallBase -----------+- CallBrInst
58// | |
59// +- CmpInst +- CallInst
60// | |
61// +- ExtractElementInst +- InvokeInst
62// |
63// +- GetElementPtrInst
64// |
65// +- InsertElementInst
66// |
67// +- OpaqueInst
68// |
69// +- PHINode
70// |
71// +- ReturnInst
72// |
73// +- SelectInst
74// |
75// +- ShuffleVectorInst
76// |
77// +- StoreInst
78// |
79// +- UnaryInstruction -+- LoadInst
80// | |
81// | +- CastInst
82// |
83// +- UnaryOperator
84// |
85// +- UnreachableInst
86//
87// Use
88//
89// } // namespace sandboxir
90//
91
92#ifndef LLVM_SANDBOXIR_SANDBOXIR_H
93#define LLVM_SANDBOXIR_SANDBOXIR_H
94
95#include "llvm/IR/Function.h"
96#include "llvm/IR/IRBuilder.h"
97#include "llvm/IR/Instruction.h"
98#include "llvm/IR/User.h"
99#include "llvm/IR/Value.h"
101#include "llvm/SandboxIR/Use.h"
103#include <iterator>
104
105namespace llvm {
106
107namespace sandboxir {
108
109class BasicBlock;
110class ConstantInt;
111class Context;
112class Function;
113class Instruction;
114class SelectInst;
117class BranchInst;
118class UnaryInstruction;
119class LoadInst;
120class ReturnInst;
121class StoreInst;
122class User;
123class UnreachableInst;
124class Value;
125class CallBase;
126class CallInst;
127class InvokeInst;
128class CallBrInst;
130class CastInst;
131class PtrToIntInst;
132class BitCastInst;
133class AllocaInst;
134class SwitchInst;
135class UnaryOperator;
136class BinaryOperator;
137class AtomicRMWInst;
139
140/// Iterator for the `Use` edges of a User's operands.
141/// \Returns the operand `Use` when dereferenced.
144 /// Don't let the user create a non-empty OperandUseIterator.
145 OperandUseIterator(const class Use &Use) : Use(Use) {}
146 friend class User; // For constructor
147#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS; // For constructor
148#include "llvm/SandboxIR/SandboxIRValues.def"
149
150public:
151 using difference_type = std::ptrdiff_t;
155 using iterator_category = std::input_iterator_tag;
156
158 value_type operator*() const;
161 auto Copy = *this;
162 this->operator++();
163 return Copy;
164 }
166 return Use == Other.Use;
167 }
169 return !(*this == Other);
170 }
171 OperandUseIterator operator+(unsigned Num) const;
172 OperandUseIterator operator-(unsigned Num) const;
173 int operator-(const OperandUseIterator &Other) const;
174};
175
176/// Iterator for the `Use` edges of a Value's users.
177/// \Returns a `Use` when dereferenced.
180 /// Don't let the user create a non-empty UserUseIterator.
181 UserUseIterator(const class Use &Use) : Use(Use) {}
182 friend class Value; // For constructor
183
184public:
185 using difference_type = std::ptrdiff_t;
189 using iterator_category = std::input_iterator_tag;
190
191 UserUseIterator() = default;
192 value_type operator*() const { return Use; }
194 bool operator==(const UserUseIterator &Other) const {
195 return Use == Other.Use;
196 }
197 bool operator!=(const UserUseIterator &Other) const {
198 return !(*this == Other);
199 }
200 const sandboxir::Use &getUse() const { return Use; }
201};
202
203/// A SandboxIR Value has users. This is the base class.
204class Value {
205public:
206 enum class ClassID : unsigned {
207#define DEF_VALUE(ID, CLASS) ID,
208#define DEF_USER(ID, CLASS) ID,
209#define DEF_INSTR(ID, OPC, CLASS) ID,
210#include "llvm/SandboxIR/SandboxIRValues.def"
211 };
212
213protected:
214 static const char *getSubclassIDStr(ClassID ID) {
215 switch (ID) {
216#define DEF_VALUE(ID, CLASS) \
217 case ClassID::ID: \
218 return #ID;
219#define DEF_USER(ID, CLASS) \
220 case ClassID::ID: \
221 return #ID;
222#define DEF_INSTR(ID, OPC, CLASS) \
223 case ClassID::ID: \
224 return #ID;
225#include "llvm/SandboxIR/SandboxIRValues.def"
226 }
227 llvm_unreachable("Unimplemented ID");
228 }
229
230 /// For isa/dyn_cast.
232#ifndef NDEBUG
233 /// A unique ID used for forming the name (used for debugging).
234 unsigned UID;
235#endif
236 /// The LLVM Value that corresponds to this SandboxIR Value.
237 /// NOTE: Some sandboxir Instructions, like Packs, may include more than one
238 /// value and in these cases `Val` points to the last instruction in program
239 /// order.
240 llvm::Value *Val = nullptr;
241
242 friend class Context; // For getting `Val`.
243 friend class User; // For getting `Val`.
244 friend class Use; // For getting `Val`.
245 friend class SelectInst; // For getting `Val`.
246 friend class ExtractElementInst; // For getting `Val`.
247 friend class InsertElementInst; // For getting `Val`.
248 friend class BranchInst; // For getting `Val`.
249 friend class LoadInst; // For getting `Val`.
250 friend class StoreInst; // For getting `Val`.
251 friend class ReturnInst; // For getting `Val`.
252 friend class CallBase; // For getting `Val`.
253 friend class CallInst; // For getting `Val`.
254 friend class InvokeInst; // For getting `Val`.
255 friend class CallBrInst; // For getting `Val`.
256 friend class GetElementPtrInst; // For getting `Val`.
257 friend class SwitchInst; // For getting `Val`.
258 friend class UnaryOperator; // For getting `Val`.
259 friend class BinaryOperator; // For getting `Val`.
260 friend class AtomicRMWInst; // For getting `Val`.
261 friend class AtomicCmpXchgInst; // For getting `Val`.
262 friend class AllocaInst; // For getting `Val`.
263 friend class CastInst; // For getting `Val`.
264 friend class PHINode; // For getting `Val`.
265 friend class UnreachableInst; // For getting `Val`.
266
267 /// All values point to the context.
269 // This is used by eraseFromParent().
270 void clearValue() { Val = nullptr; }
271 template <typename ItTy, typename SBTy> friend class LLVMOpUserItToSBTy;
272
274 /// Disable copies.
275 Value(const Value &) = delete;
276 Value &operator=(const Value &) = delete;
277
278public:
279 virtual ~Value() = default;
280 ClassID getSubclassID() const { return SubclassID; }
281
284
287 return const_cast<Value *>(this)->use_begin();
288 }
289 use_iterator use_end() { return use_iterator(Use(nullptr, nullptr, Ctx)); }
291 return const_cast<Value *>(this)->use_end();
292 }
293
295 return make_range<use_iterator>(use_begin(), use_end());
296 }
298 return make_range<const_use_iterator>(use_begin(), use_end());
299 }
300
301 /// Helper for mapped_iterator.
302 struct UseToUser {
303 User *operator()(const Use &Use) const { return &*Use.getUser(); }
304 };
305
308
311 return user_iterator(Use(nullptr, nullptr, Ctx), UseToUser());
312 }
314 return const_cast<Value *>(this)->user_begin();
315 }
317 return const_cast<Value *>(this)->user_end();
318 }
319
321 return make_range<user_iterator>(user_begin(), user_end());
322 }
324 return make_range<const_user_iterator>(user_begin(), user_end());
325 }
326 /// \Returns the number of user edges (not necessarily to unique users).
327 /// WARNING: This is a linear-time operation.
328 unsigned getNumUses() const;
329 /// Return true if this value has N uses or more.
330 /// This is logically equivalent to getNumUses() >= N.
331 /// WARNING: This can be expensive, as it is linear to the number of users.
332 bool hasNUsesOrMore(unsigned Num) const {
333 unsigned Cnt = 0;
334 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
335 if (++Cnt >= Num)
336 return true;
337 }
338 return false;
339 }
340 /// Return true if this Value has exactly N uses.
341 bool hasNUses(unsigned Num) const {
342 unsigned Cnt = 0;
343 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
344 if (++Cnt > Num)
345 return false;
346 }
347 return Cnt == Num;
348 }
349
350 Type *getType() const { return Val->getType(); }
351
352 Context &getContext() const { return Ctx; }
353
354 void replaceUsesWithIf(Value *OtherV,
355 llvm::function_ref<bool(const Use &)> ShouldReplace);
357
358 /// \Returns the LLVM IR name of the bottom-most LLVM value.
359 StringRef getName() const { return Val->getName(); }
360
361#ifndef NDEBUG
362 /// Should crash if there is something wrong with the instruction.
363 virtual void verify() const = 0;
364 /// Returns the unique id in the form 'SB<number>.' like 'SB1.'
365 std::string getUid() const;
366 virtual void dumpCommonHeader(raw_ostream &OS) const;
367 void dumpCommonFooter(raw_ostream &OS) const;
368 void dumpCommonPrefix(raw_ostream &OS) const;
369 void dumpCommonSuffix(raw_ostream &OS) const;
372 V.dumpOS(OS);
373 return OS;
374 }
375 virtual void dumpOS(raw_ostream &OS) const = 0;
376 LLVM_DUMP_METHOD void dump() const;
377#endif
378};
379
380/// Argument of a sandboxir::Function.
383 : sandboxir::Value(ClassID::Argument, Arg, Ctx) {}
384 friend class Context; // For constructor.
385
386public:
387 static bool classof(const sandboxir::Value *From) {
388 return From->getSubclassID() == ClassID::Argument;
389 }
390#ifndef NDEBUG
391 void verify() const final {
392 assert(isa<llvm::Argument>(Val) && "Expected Argument!");
393 }
394 void printAsOperand(raw_ostream &OS) const;
395 void dumpOS(raw_ostream &OS) const final;
396#endif
397};
398
399/// A sandboxir::User has operands.
400class User : public Value {
401protected:
403
404 /// \Returns the Use edge that corresponds to \p OpIdx.
405 /// Note: This is the default implementation that works for instructions that
406 /// match the underlying LLVM instruction. All others should use a different
407 /// implementation.
408 Use getOperandUseDefault(unsigned OpIdx, bool Verify) const;
409 /// \Returns the Use for the \p OpIdx'th operand. This is virtual to allow
410 /// instructions to deviate from the LLVM IR operands, which is a requirement
411 /// for sandboxir Instructions that consist of more than one LLVM Instruction.
412 virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const = 0;
413 friend class OperandUseIterator; // for getOperandUseInternal()
414
415 /// The default implementation works only for single-LLVMIR-instruction
416 /// Users and only if they match exactly the LLVM instruction.
417 unsigned getUseOperandNoDefault(const Use &Use) const {
418 return Use.LLVMUse->getOperandNo();
419 }
420 /// \Returns the operand index of \p Use.
421 virtual unsigned getUseOperandNo(const Use &Use) const = 0;
422 friend unsigned Use::getOperandNo() const; // For getUseOperandNo()
423
424 void swapOperandsInternal(unsigned OpIdxA, unsigned OpIdxB) {
425 assert(OpIdxA < getNumOperands() && "OpIdxA out of bounds!");
426 assert(OpIdxB < getNumOperands() && "OpIdxB out of bounds!");
427 auto UseA = getOperandUse(OpIdxA);
428 auto UseB = getOperandUse(OpIdxB);
429 UseA.swap(UseB);
430 }
431
432#ifndef NDEBUG
433 void verifyUserOfLLVMUse(const llvm::Use &Use) const;
434#endif // NDEBUG
435
436public:
437 /// For isa/dyn_cast.
438 static bool classof(const Value *From);
443
445 assert(isa<llvm::User>(Val) && "Expect User value!");
446 return op_iterator(getOperandUseInternal(0, /*Verify=*/false));
447 }
448 virtual op_iterator op_end() {
449 assert(isa<llvm::User>(Val) && "Expect User value!");
450 return op_iterator(
451 getOperandUseInternal(getNumOperands(), /*Verify=*/false));
452 }
453 virtual const_op_iterator op_begin() const {
454 return const_cast<User *>(this)->op_begin();
455 }
456 virtual const_op_iterator op_end() const {
457 return const_cast<User *>(this)->op_end();
458 }
459
460 op_range operands() { return make_range<op_iterator>(op_begin(), op_end()); }
462 return make_range<const_op_iterator>(op_begin(), op_end());
463 }
464 Value *getOperand(unsigned OpIdx) const { return getOperandUse(OpIdx).get(); }
465 /// \Returns the operand edge for \p OpIdx. NOTE: This should also work for
466 /// OpIdx == getNumOperands(), which is used for op_end().
467 Use getOperandUse(unsigned OpIdx) const {
468 return getOperandUseInternal(OpIdx, /*Verify=*/true);
469 }
470 virtual unsigned getNumOperands() const {
471 return isa<llvm::User>(Val) ? cast<llvm::User>(Val)->getNumOperands() : 0;
472 }
473
474 virtual void setOperand(unsigned OperandIdx, Value *Operand);
475 /// Replaces any operands that match \p FromV with \p ToV. Returns whether any
476 /// operands were replaced.
477 bool replaceUsesOfWith(Value *FromV, Value *ToV);
478
479#ifndef NDEBUG
480 void verify() const override {
481 assert(isa<llvm::User>(Val) && "Expected User!");
482 }
483 void dumpCommonHeader(raw_ostream &OS) const final;
484 void dumpOS(raw_ostream &OS) const override {
485 // TODO: Remove this tmp implementation once we get the Instruction classes.
486 }
487#endif
488};
489
490class Constant : public sandboxir::User {
492 : sandboxir::User(ClassID::Constant, C, SBCtx) {}
494 : sandboxir::User(ID, C, SBCtx) {}
495 friend class ConstantInt; // For constructor.
496 friend class Function; // For constructor
497 friend class Context; // For constructor.
498 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const override {
499 return getOperandUseDefault(OpIdx, Verify);
500 }
501
502public:
503 /// For isa/dyn_cast.
504 static bool classof(const sandboxir::Value *From) {
505 return From->getSubclassID() == ClassID::Constant ||
506 From->getSubclassID() == ClassID::ConstantInt ||
507 From->getSubclassID() == ClassID::Function;
508 }
510 unsigned getUseOperandNo(const Use &Use) const override {
512 }
513#ifndef NDEBUG
514 void verify() const override {
515 assert(isa<llvm::Constant>(Val) && "Expected Constant!");
516 }
517 void dumpOS(raw_ostream &OS) const override;
518#endif
519};
520
521class ConstantInt : public Constant {
523 : Constant(ClassID::ConstantInt, C, Ctx) {}
524 friend class Context; // For constructor.
525
526 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
527 llvm_unreachable("ConstantInt has no operands!");
528 }
529
530public:
531 /// If Ty is a vector type, return a Constant with a splat of the given
532 /// value. Otherwise return a ConstantInt for the given value.
533 static ConstantInt *get(Type *Ty, uint64_t V, Context &Ctx,
534 bool IsSigned = false);
535
536 // TODO: Implement missing functions.
537
538 /// For isa/dyn_cast.
539 static bool classof(const sandboxir::Value *From) {
540 return From->getSubclassID() == ClassID::ConstantInt;
541 }
542 unsigned getUseOperandNo(const Use &Use) const override {
543 llvm_unreachable("ConstantInt has no operands!");
544 }
545#ifndef NDEBUG
546 void verify() const override {
547 assert(isa<llvm::ConstantInt>(Val) && "Expected a ConstantInst!");
548 }
549 void dumpOS(raw_ostream &OS) const override {
552 }
553#endif
554};
555
556/// Iterator for `Instruction`s in a `BasicBlock.
557/// \Returns an sandboxir::Instruction & when derereferenced.
559public:
560 using difference_type = std::ptrdiff_t;
564 using iterator_category = std::bidirectional_iterator_tag;
565
566private:
569 Context *Ctx;
570 pointer getInstr(llvm::BasicBlock::iterator It) const;
571
572public:
573 BBIterator() : BB(nullptr), Ctx(nullptr) {}
575 : BB(BB), It(It), Ctx(Ctx) {}
576 reference operator*() const { return *getInstr(It); }
579 auto Copy = *this;
580 ++*this;
581 return Copy;
582 }
585 auto Copy = *this;
586 --*this;
587 return Copy;
588 }
589 bool operator==(const BBIterator &Other) const {
590 assert(Ctx == Other.Ctx && "BBIterators in different context!");
591 return It == Other.It;
592 }
593 bool operator!=(const BBIterator &Other) const { return !(*this == Other); }
594 /// \Returns the SBInstruction that corresponds to this iterator, or null if
595 /// the instruction is not found in the IR-to-SandboxIR tables.
596 pointer get() const { return getInstr(It); }
597};
598
599/// Contains a list of sandboxir::Instruction's.
600class BasicBlock : public Value {
601 /// Builds a graph that contains all values in \p BB in their original form
602 /// i.e., no vectorization is taking place here.
603 void buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB);
604 friend class Context; // For `buildBasicBlockFromIR`
605 friend class Instruction; // For LLVM Val.
606
608 : Value(ClassID::Block, BB, SBCtx) {
609 buildBasicBlockFromLLVMIR(BB);
610 }
611
612public:
613 ~BasicBlock() = default;
614 /// For isa/dyn_cast.
615 static bool classof(const Value *From) {
616 return From->getSubclassID() == Value::ClassID::Block;
617 }
618 Function *getParent() const;
620 iterator begin() const;
621 iterator end() const {
622 auto *BB = cast<llvm::BasicBlock>(Val);
623 return iterator(BB, BB->end(), &Ctx);
624 }
625 std::reverse_iterator<iterator> rbegin() const {
626 return std::make_reverse_iterator(end());
627 }
628 std::reverse_iterator<iterator> rend() const {
629 return std::make_reverse_iterator(begin());
630 }
631 Context &getContext() const { return Ctx; }
632 Instruction *getTerminator() const;
633 bool empty() const { return begin() == end(); }
634 Instruction &front() const;
635 Instruction &back() const;
636
637#ifndef NDEBUG
638 void verify() const final {
639 assert(isa<llvm::BasicBlock>(Val) && "Expected BasicBlock!");
640 }
641 void dumpOS(raw_ostream &OS) const final;
642#endif
643};
644
645/// A sandboxir::User with operands, opcode and linked with previous/next
646/// instructions in an instruction list.
648public:
649 enum class Opcode {
650#define OP(OPC) OPC,
651#define OPCODES(...) __VA_ARGS__
652#define DEF_INSTR(ID, OPC, CLASS) OPC
653#include "llvm/SandboxIR/SandboxIRValues.def"
654 };
655
656protected:
658 sandboxir::Context &SBCtx)
659 : sandboxir::User(ID, I, SBCtx), Opc(Opc) {}
660
662
663 /// A SandboxIR Instruction may map to multiple LLVM IR Instruction. This
664 /// returns its topmost LLVM IR instruction.
666 friend class SelectInst; // For getTopmostLLVMInstruction().
667 friend class ExtractElementInst; // For getTopmostLLVMInstruction().
668 friend class InsertElementInst; // For getTopmostLLVMInstruction().
669 friend class BranchInst; // For getTopmostLLVMInstruction().
670 friend class LoadInst; // For getTopmostLLVMInstruction().
671 friend class StoreInst; // For getTopmostLLVMInstruction().
672 friend class ReturnInst; // For getTopmostLLVMInstruction().
673 friend class CallInst; // For getTopmostLLVMInstruction().
674 friend class InvokeInst; // For getTopmostLLVMInstruction().
675 friend class CallBrInst; // For getTopmostLLVMInstruction().
676 friend class GetElementPtrInst; // For getTopmostLLVMInstruction().
677 friend class SwitchInst; // For getTopmostLLVMInstruction().
678 friend class UnaryOperator; // For getTopmostLLVMInstruction().
679 friend class BinaryOperator; // For getTopmostLLVMInstruction().
680 friend class AtomicRMWInst; // For getTopmostLLVMInstruction().
681 friend class AtomicCmpXchgInst; // For getTopmostLLVMInstruction().
682 friend class AllocaInst; // For getTopmostLLVMInstruction().
683 friend class CastInst; // For getTopmostLLVMInstruction().
684 friend class PHINode; // For getTopmostLLVMInstruction().
685 friend class UnreachableInst; // For getTopmostLLVMInstruction().
686
687 /// \Returns the LLVM IR Instructions that this SandboxIR maps to in program
688 /// order.
690 friend class EraseFromParent; // For getLLVMInstrs().
691
692public:
693 static const char *getOpcodeName(Opcode Opc);
694 /// This is used by BasicBlock::iterator.
695 virtual unsigned getNumOfIRInstrs() const = 0;
696 /// \Returns a BasicBlock::iterator for this Instruction.
697 BBIterator getIterator() const;
698 /// \Returns the next sandboxir::Instruction in the block, or nullptr if at
699 /// the end of the block.
700 Instruction *getNextNode() const;
701 /// \Returns the previous sandboxir::Instruction in the block, or nullptr if
702 /// at the beginning of the block.
703 Instruction *getPrevNode() const;
704 /// \Returns this Instruction's opcode. Note that SandboxIR has its own opcode
705 /// state to allow for new SandboxIR-specific instructions.
706 Opcode getOpcode() const { return Opc; }
707 /// Detach this from its parent BasicBlock without deleting it.
708 void removeFromParent();
709 /// Detach this Value from its parent and delete it.
710 void eraseFromParent();
711 /// Insert this detached instruction before \p BeforeI.
712 void insertBefore(Instruction *BeforeI);
713 /// Insert this detached instruction after \p AfterI.
714 void insertAfter(Instruction *AfterI);
715 /// Insert this detached instruction into \p BB at \p WhereIt.
716 void insertInto(BasicBlock *BB, const BBIterator &WhereIt);
717 /// Move this instruction to \p WhereIt.
718 void moveBefore(BasicBlock &BB, const BBIterator &WhereIt);
719 /// Move this instruction before \p Before.
721 moveBefore(*Before->getParent(), Before->getIterator());
722 }
723 /// Move this instruction after \p After.
725 moveBefore(*After->getParent(), std::next(After->getIterator()));
726 }
727 /// \Returns the BasicBlock containing this Instruction, or null if it is
728 /// detached.
729 BasicBlock *getParent() const;
730 /// For isa/dyn_cast.
731 static bool classof(const sandboxir::Value *From);
732
733 /// Determine whether the no signed wrap flag is set.
734 bool hasNoUnsignedWrap() const {
735 return cast<llvm::Instruction>(Val)->hasNoUnsignedWrap();
736 }
737 /// Set or clear the nuw flag on this instruction, which must be an operator
738 /// which supports this flag. See LangRef.html for the meaning of this flag.
739 void setHasNoUnsignedWrap(bool B = true);
740 /// Determine whether the no signed wrap flag is set.
741 bool hasNoSignedWrap() const {
742 return cast<llvm::Instruction>(Val)->hasNoSignedWrap();
743 }
744 /// Set or clear the nsw flag on this instruction, which must be an operator
745 /// which supports this flag. See LangRef.html for the meaning of this flag.
746 void setHasNoSignedWrap(bool B = true);
747 /// Determine whether all fast-math-flags are set.
748 bool isFast() const { return cast<llvm::Instruction>(Val)->isFast(); }
749 /// Set or clear all fast-math-flags on this instruction, which must be an
750 /// operator which supports this flag. See LangRef.html for the meaning of
751 /// this flag.
752 void setFast(bool B);
753 /// Determine whether the allow-reassociation flag is set.
754 bool hasAllowReassoc() const {
755 return cast<llvm::Instruction>(Val)->hasAllowReassoc();
756 }
757 /// Set or clear the reassociation flag on this instruction, which must be
758 /// an operator which supports this flag. See LangRef.html for the meaning of
759 /// this flag.
760 void setHasAllowReassoc(bool B);
761 /// Determine whether the exact flag is set.
762 bool isExact() const { return cast<llvm::Instruction>(Val)->isExact(); }
763 /// Set or clear the exact flag on this instruction, which must be an operator
764 /// which supports this flag. See LangRef.html for the meaning of this flag.
765 void setIsExact(bool B = true);
766 /// Determine whether the no-NaNs flag is set.
767 bool hasNoNaNs() const { return cast<llvm::Instruction>(Val)->hasNoNaNs(); }
768 /// Set or clear the no-nans flag on this instruction, which must be an
769 /// operator which supports this flag. See LangRef.html for the meaning of
770 /// this flag.
771 void setHasNoNaNs(bool B);
772 /// Determine whether the no-infs flag is set.
773 bool hasNoInfs() const { return cast<llvm::Instruction>(Val)->hasNoInfs(); }
774 /// Set or clear the no-infs flag on this instruction, which must be an
775 /// operator which supports this flag. See LangRef.html for the meaning of
776 /// this flag.
777 void setHasNoInfs(bool B);
778 /// Determine whether the no-signed-zeros flag is set.
779 bool hasNoSignedZeros() const {
780 return cast<llvm::Instruction>(Val)->hasNoSignedZeros();
781 }
782 /// Set or clear the no-signed-zeros flag on this instruction, which must be
783 /// an operator which supports this flag. See LangRef.html for the meaning of
784 /// this flag.
785 void setHasNoSignedZeros(bool B);
786 /// Determine whether the allow-reciprocal flag is set.
787 bool hasAllowReciprocal() const {
788 return cast<llvm::Instruction>(Val)->hasAllowReciprocal();
789 }
790 /// Set or clear the allow-reciprocal flag on this instruction, which must be
791 /// an operator which supports this flag. See LangRef.html for the meaning of
792 /// this flag.
793 void setHasAllowReciprocal(bool B);
794 /// Determine whether the allow-contract flag is set.
795 bool hasAllowContract() const {
796 return cast<llvm::Instruction>(Val)->hasAllowContract();
797 }
798 /// Set or clear the allow-contract flag on this instruction, which must be
799 /// an operator which supports this flag. See LangRef.html for the meaning of
800 /// this flag.
801 void setHasAllowContract(bool B);
802 /// Determine whether the approximate-math-functions flag is set.
803 bool hasApproxFunc() const {
804 return cast<llvm::Instruction>(Val)->hasApproxFunc();
805 }
806 /// Set or clear the approximate-math-functions flag on this instruction,
807 /// which must be an operator which supports this flag. See LangRef.html for
808 /// the meaning of this flag.
809 void setHasApproxFunc(bool B);
810 /// Convenience function for getting all the fast-math flags, which must be an
811 /// operator which supports these flags. See LangRef.html for the meaning of
812 /// these flags.
814 return cast<llvm::Instruction>(Val)->getFastMathFlags();
815 }
816 /// Convenience function for setting multiple fast-math flags on this
817 /// instruction, which must be an operator which supports these flags. See
818 /// LangRef.html for the meaning of these flags.
820 /// Convenience function for transferring all fast-math flag values to this
821 /// instruction, which must be an operator which supports these flags. See
822 /// LangRef.html for the meaning of these flags.
824
825#ifndef NDEBUG
826 void dumpOS(raw_ostream &OS) const override;
827#endif
828};
829
830/// Instructions that contain a single LLVM Instruction can inherit from this.
831template <typename LLVMT> class SingleLLVMInstructionImpl : public Instruction {
833 sandboxir::Context &SBCtx)
834 : Instruction(ID, Opc, I, SBCtx) {}
835
836 // All instructions are friends with this so they can call the constructor.
837#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS;
838#include "llvm/SandboxIR/SandboxIRValues.def"
839 friend class UnaryInstruction;
840 friend class CallBase;
841
842 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
843 return getOperandUseDefault(OpIdx, Verify);
844 }
845 SmallVector<llvm::Instruction *, 1> getLLVMInstrs() const final {
846 return {cast<llvm::Instruction>(Val)};
847 }
848
849public:
850 unsigned getUseOperandNo(const Use &Use) const final {
852 }
853 unsigned getNumOfIRInstrs() const final { return 1u; }
854#ifndef NDEBUG
855 void verify() const final { assert(isa<LLVMT>(Val) && "Expected LLVMT!"); }
856 void dumpOS(raw_ostream &OS) const override {
859 }
860#endif
861};
862
863class SelectInst : public SingleLLVMInstructionImpl<llvm::SelectInst> {
864 /// Use Context::createSelectInst(). Don't call the
865 /// constructor directly.
867 : SingleLLVMInstructionImpl(ClassID::Select, Opcode::Select, CI, Ctx) {}
868 friend Context; // for SelectInst()
869 static Value *createCommon(Value *Cond, Value *True, Value *False,
870 const Twine &Name, IRBuilder<> &Builder,
871 Context &Ctx);
872
873public:
874 static Value *create(Value *Cond, Value *True, Value *False,
875 Instruction *InsertBefore, Context &Ctx,
876 const Twine &Name = "");
877 static Value *create(Value *Cond, Value *True, Value *False,
878 BasicBlock *InsertAtEnd, Context &Ctx,
879 const Twine &Name = "");
880 Value *getCondition() { return getOperand(0); }
881 Value *getTrueValue() { return getOperand(1); }
883
884 void setCondition(Value *New) { setOperand(0, New); }
885 void setTrueValue(Value *New) { setOperand(1, New); }
886 void setFalseValue(Value *New) { setOperand(2, New); }
887 void swapValues() { cast<llvm::SelectInst>(Val)->swapValues(); }
888 /// For isa/dyn_cast.
889 static bool classof(const Value *From);
890};
891
893 : public SingleLLVMInstructionImpl<llvm::InsertElementInst> {
894 /// Use Context::createInsertElementInst() instead.
896 : SingleLLVMInstructionImpl(ClassID::InsertElement, Opcode::InsertElement,
897 I, Ctx) {}
898 friend class Context; // For accessing the constructor in create*()
899
900public:
901 static Value *create(Value *Vec, Value *NewElt, Value *Idx,
902 Instruction *InsertBefore, Context &Ctx,
903 const Twine &Name = "");
904 static Value *create(Value *Vec, Value *NewElt, Value *Idx,
905 BasicBlock *InsertAtEnd, Context &Ctx,
906 const Twine &Name = "");
907 static bool classof(const Value *From) {
908 return From->getSubclassID() == ClassID::InsertElement;
909 }
910 static bool isValidOperands(const Value *Vec, const Value *NewElt,
911 const Value *Idx) {
913 Idx->Val);
914 }
915};
916
918 : public SingleLLVMInstructionImpl<llvm::ExtractElementInst> {
919 /// Use Context::createExtractElementInst() instead.
921 : SingleLLVMInstructionImpl(ClassID::ExtractElement,
922 Opcode::ExtractElement, I, Ctx) {}
923 friend class Context; // For accessing the constructor in
924 // create*()
925
926public:
927 static Value *create(Value *Vec, Value *Idx, Instruction *InsertBefore,
928 Context &Ctx, const Twine &Name = "");
929 static Value *create(Value *Vec, Value *Idx, BasicBlock *InsertAtEnd,
930 Context &Ctx, const Twine &Name = "");
931 static bool classof(const Value *From) {
932 return From->getSubclassID() == ClassID::ExtractElement;
933 }
934
935 static bool isValidOperands(const Value *Vec, const Value *Idx) {
937 }
940 const Value *getVectorOperand() const { return getOperand(0); }
941 const Value *getIndexOperand() const { return getOperand(1); }
942
944 return cast<VectorType>(getVectorOperand()->getType());
945 }
946};
947
948class BranchInst : public SingleLLVMInstructionImpl<llvm::BranchInst> {
949 /// Use Context::createBranchInst(). Don't call the constructor directly.
951 : SingleLLVMInstructionImpl(ClassID::Br, Opcode::Br, BI, Ctx) {}
952 friend Context; // for BranchInst()
953
954public:
955 static BranchInst *create(BasicBlock *IfTrue, Instruction *InsertBefore,
956 Context &Ctx);
957 static BranchInst *create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd,
958 Context &Ctx);
959 static BranchInst *create(BasicBlock *IfTrue, BasicBlock *IfFalse,
960 Value *Cond, Instruction *InsertBefore,
961 Context &Ctx);
962 static BranchInst *create(BasicBlock *IfTrue, BasicBlock *IfFalse,
963 Value *Cond, BasicBlock *InsertAtEnd, Context &Ctx);
964 /// For isa/dyn_cast.
965 static bool classof(const Value *From);
966 bool isUnconditional() const {
967 return cast<llvm::BranchInst>(Val)->isUnconditional();
968 }
969 bool isConditional() const {
970 return cast<llvm::BranchInst>(Val)->isConditional();
971 }
972 Value *getCondition() const;
973 void setCondition(Value *V) { setOperand(0, V); }
974 unsigned getNumSuccessors() const { return 1 + isConditional(); }
975 BasicBlock *getSuccessor(unsigned SuccIdx) const;
976 void setSuccessor(unsigned Idx, BasicBlock *NewSucc);
978
979private:
980 struct LLVMBBToSBBB {
981 Context &Ctx;
982 LLVMBBToSBBB(Context &Ctx) : Ctx(Ctx) {}
983 BasicBlock *operator()(llvm::BasicBlock *BB) const;
984 };
985
986 struct ConstLLVMBBToSBBB {
987 Context &Ctx;
988 ConstLLVMBBToSBBB(Context &Ctx) : Ctx(Ctx) {}
989 const BasicBlock *operator()(const llvm::BasicBlock *BB) const;
990 };
991
992public:
997 cast<llvm::BranchInst>(Val)->successors();
998 LLVMBBToSBBB BBMap(Ctx);
999 sb_succ_op_iterator MappedBegin = map_iterator(LLVMRange.begin(), BBMap);
1000 sb_succ_op_iterator MappedEnd = map_iterator(LLVMRange.end(), BBMap);
1001 return make_range(MappedBegin, MappedEnd);
1002 }
1003
1006 ConstLLVMBBToSBBB>;
1009 static_cast<const llvm::BranchInst *>(cast<llvm::BranchInst>(Val))
1010 ->successors();
1011 ConstLLVMBBToSBBB ConstBBMap(Ctx);
1012 const_sb_succ_op_iterator ConstMappedBegin =
1013 map_iterator(ConstLLVMRange.begin(), ConstBBMap);
1014 const_sb_succ_op_iterator ConstMappedEnd =
1015 map_iterator(ConstLLVMRange.end(), ConstBBMap);
1016 return make_range(ConstMappedBegin, ConstMappedEnd);
1017 }
1018};
1019
1020/// An abstract class, parent of unary instructions.
1022 : public SingleLLVMInstructionImpl<llvm::UnaryInstruction> {
1023protected:
1025 Context &Ctx)
1026 : SingleLLVMInstructionImpl(ID, Opc, LLVMI, Ctx) {}
1027
1028public:
1029 static bool classof(const Instruction *I) {
1030 return isa<LoadInst>(I) || isa<CastInst>(I);
1031 }
1032 static bool classof(const Value *V) {
1033 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1034 }
1035};
1036
1037class LoadInst final : public UnaryInstruction {
1038 /// Use LoadInst::create() instead of calling the constructor.
1040 : UnaryInstruction(ClassID::Load, Opcode::Load, LI, Ctx) {}
1041 friend Context; // for LoadInst()
1042
1043public:
1044 /// Return true if this is a load from a volatile memory location.
1045 bool isVolatile() const { return cast<llvm::LoadInst>(Val)->isVolatile(); }
1046 /// Specify whether this is a volatile load or not.
1047 void setVolatile(bool V);
1048
1049 static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
1050 Instruction *InsertBefore, Context &Ctx,
1051 const Twine &Name = "");
1052 static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
1053 Instruction *InsertBefore, bool IsVolatile,
1054 Context &Ctx, const Twine &Name = "");
1055 static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
1056 BasicBlock *InsertAtEnd, Context &Ctx,
1057 const Twine &Name = "");
1058 static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
1059 BasicBlock *InsertAtEnd, bool IsVolatile,
1060 Context &Ctx, const Twine &Name = "");
1061
1062 /// For isa/dyn_cast.
1063 static bool classof(const Value *From);
1064 Value *getPointerOperand() const;
1065 Align getAlign() const { return cast<llvm::LoadInst>(Val)->getAlign(); }
1066 bool isUnordered() const { return cast<llvm::LoadInst>(Val)->isUnordered(); }
1067 bool isSimple() const { return cast<llvm::LoadInst>(Val)->isSimple(); }
1068};
1069
1070class StoreInst final : public SingleLLVMInstructionImpl<llvm::StoreInst> {
1071 /// Use StoreInst::create().
1073 : SingleLLVMInstructionImpl(ClassID::Store, Opcode::Store, SI, Ctx) {}
1074 friend Context; // for StoreInst()
1075
1076public:
1077 /// Return true if this is a store from a volatile memory location.
1078 bool isVolatile() const { return cast<llvm::StoreInst>(Val)->isVolatile(); }
1079 /// Specify whether this is a volatile store or not.
1080 void setVolatile(bool V);
1081
1083 Instruction *InsertBefore, Context &Ctx);
1085 Instruction *InsertBefore, bool IsVolatile,
1086 Context &Ctx);
1088 BasicBlock *InsertAtEnd, Context &Ctx);
1090 BasicBlock *InsertAtEnd, bool IsVolatile,
1091 Context &Ctx);
1092 /// For isa/dyn_cast.
1093 static bool classof(const Value *From);
1094 Value *getValueOperand() const;
1095 Value *getPointerOperand() const;
1096 Align getAlign() const { return cast<llvm::StoreInst>(Val)->getAlign(); }
1097 bool isSimple() const { return cast<llvm::StoreInst>(Val)->isSimple(); }
1098 bool isUnordered() const { return cast<llvm::StoreInst>(Val)->isUnordered(); }
1099};
1100
1101class UnreachableInst final : public Instruction {
1102 /// Use UnreachableInst::create() instead of calling the constructor.
1104 : Instruction(ClassID::Unreachable, Opcode::Unreachable, I, Ctx) {}
1105 friend Context;
1106 Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
1107 return getOperandUseDefault(OpIdx, Verify);
1108 }
1109 SmallVector<llvm::Instruction *, 1> getLLVMInstrs() const final {
1110 return {cast<llvm::Instruction>(Val)};
1111 }
1112
1113public:
1114 static UnreachableInst *create(Instruction *InsertBefore, Context &Ctx);
1115 static UnreachableInst *create(BasicBlock *InsertAtEnd, Context &Ctx);
1116 static bool classof(const Value *From);
1117 unsigned getNumSuccessors() const { return 0; }
1118 unsigned getUseOperandNo(const Use &Use) const final {
1119 llvm_unreachable("UnreachableInst has no operands!");
1120 }
1121 unsigned getNumOfIRInstrs() const final { return 1u; }
1122};
1123
1124class ReturnInst final : public SingleLLVMInstructionImpl<llvm::ReturnInst> {
1125 /// Use ReturnInst::create() instead of calling the constructor.
1127 : SingleLLVMInstructionImpl(ClassID::Ret, Opcode::Ret, I, Ctx) {}
1129 : SingleLLVMInstructionImpl(SubclassID, Opcode::Ret, I, Ctx) {}
1130 friend class Context; // For accessing the constructor in create*()
1131 static ReturnInst *createCommon(Value *RetVal, IRBuilder<> &Builder,
1132 Context &Ctx);
1133
1134public:
1135 static ReturnInst *create(Value *RetVal, Instruction *InsertBefore,
1136 Context &Ctx);
1137 static ReturnInst *create(Value *RetVal, BasicBlock *InsertAtEnd,
1138 Context &Ctx);
1139 static bool classof(const Value *From) {
1140 return From->getSubclassID() == ClassID::Ret;
1141 }
1142 /// \Returns null if there is no return value.
1143 Value *getReturnValue() const;
1144};
1145
1146class CallBase : public SingleLLVMInstructionImpl<llvm::CallBase> {
1149 friend class CallInst; // For constructor.
1150 friend class InvokeInst; // For constructor.
1151 friend class CallBrInst; // For constructor.
1152
1153public:
1154 static bool classof(const Value *From) {
1155 auto Opc = From->getSubclassID();
1156 return Opc == Instruction::ClassID::Call ||
1157 Opc == Instruction::ClassID::Invoke ||
1158 Opc == Instruction::ClassID::CallBr;
1159 }
1160
1162 return cast<llvm::CallBase>(Val)->getFunctionType();
1163 }
1164
1167 return const_cast<CallBase *>(this)->data_operands_begin();
1168 }
1170 auto *LLVMCB = cast<llvm::CallBase>(Val);
1171 auto Dist = LLVMCB->data_operands_end() - LLVMCB->data_operands_begin();
1172 return op_begin() + Dist;
1173 }
1175 auto *LLVMCB = cast<llvm::CallBase>(Val);
1176 auto Dist = LLVMCB->data_operands_end() - LLVMCB->data_operands_begin();
1177 return op_begin() + Dist;
1178 }
1181 }
1184 }
1185 bool data_operands_empty() const {
1187 }
1188 unsigned data_operands_size() const {
1189 return std::distance(data_operands_begin(), data_operands_end());
1190 }
1191 bool isDataOperand(Use U) const {
1192 assert(this == U.getUser() &&
1193 "Only valid to query with a use of this instruction!");
1194 return cast<llvm::CallBase>(Val)->isDataOperand(U.LLVMUse);
1195 }
1196 unsigned getDataOperandNo(Use U) const {
1197 assert(isDataOperand(U) && "Data operand # out of range!");
1198 return cast<llvm::CallBase>(Val)->getDataOperandNo(U.LLVMUse);
1199 }
1200
1201 /// Return the total number operands (not operand bundles) used by
1202 /// every operand bundle in this OperandBundleUser.
1203 unsigned getNumTotalBundleOperands() const {
1204 return cast<llvm::CallBase>(Val)->getNumTotalBundleOperands();
1205 }
1206
1211 }
1213 return const_cast<CallBase *>(this)->arg_end();
1214 }
1216 return make_range(arg_begin(), arg_end());
1217 }
1219 return make_range(arg_begin(), arg_end());
1220 }
1221 bool arg_empty() const { return arg_end() == arg_begin(); }
1222 unsigned arg_size() const { return arg_end() - arg_begin(); }
1223
1224 Value *getArgOperand(unsigned OpIdx) const {
1225 assert(OpIdx < arg_size() && "Out of bounds!");
1226 return getOperand(OpIdx);
1227 }
1228 void setArgOperand(unsigned OpIdx, Value *NewOp) {
1229 assert(OpIdx < arg_size() && "Out of bounds!");
1230 setOperand(OpIdx, NewOp);
1231 }
1232
1233 Use getArgOperandUse(unsigned Idx) const {
1234 assert(Idx < arg_size() && "Out of bounds!");
1235 return getOperandUse(Idx);
1236 }
1238 assert(Idx < arg_size() && "Out of bounds!");
1239 return getOperandUse(Idx);
1240 }
1241
1242 bool isArgOperand(Use U) const {
1243 return cast<llvm::CallBase>(Val)->isArgOperand(U.LLVMUse);
1244 }
1245 unsigned getArgOperandNo(Use U) const {
1246 return cast<llvm::CallBase>(Val)->getArgOperandNo(U.LLVMUse);
1247 }
1248 bool hasArgument(const Value *V) const { return is_contained(args(), V); }
1249
1250 Value *getCalledOperand() const;
1251 Use getCalledOperandUse() const;
1252
1253 Function *getCalledFunction() const;
1254 bool isIndirectCall() const {
1255 return cast<llvm::CallBase>(Val)->isIndirectCall();
1256 }
1257 bool isCallee(Use U) const {
1258 return cast<llvm::CallBase>(Val)->isCallee(U.LLVMUse);
1259 }
1261 const Function *getCaller() const {
1262 return const_cast<CallBase *>(this)->getCaller();
1263 }
1264 bool isMustTailCall() const {
1265 return cast<llvm::CallBase>(Val)->isMustTailCall();
1266 }
1267 bool isTailCall() const { return cast<llvm::CallBase>(Val)->isTailCall(); }
1269 return cast<llvm::CallBase>(Val)->getIntrinsicID();
1270 }
1274 return cast<llvm::CallBase>(Val)->getCallingConv();
1275 }
1276 bool isInlineAsm() const { return cast<llvm::CallBase>(Val)->isInlineAsm(); }
1277};
1278
1279class CallInst final : public CallBase {
1280 /// Use Context::createCallInst(). Don't call the
1281 /// constructor directly.
1283 : CallBase(ClassID::Call, Opcode::Call, I, Ctx) {}
1284 friend class Context; // For accessing the constructor in
1285 // create*()
1286
1287public:
1288 static CallInst *create(FunctionType *FTy, Value *Func,
1289 ArrayRef<Value *> Args, BBIterator WhereIt,
1290 BasicBlock *WhereBB, Context &Ctx,
1291 const Twine &NameStr = "");
1292 static CallInst *create(FunctionType *FTy, Value *Func,
1293 ArrayRef<Value *> Args, Instruction *InsertBefore,
1294 Context &Ctx, const Twine &NameStr = "");
1295 static CallInst *create(FunctionType *FTy, Value *Func,
1296 ArrayRef<Value *> Args, BasicBlock *InsertAtEnd,
1297 Context &Ctx, const Twine &NameStr = "");
1298
1299 static bool classof(const Value *From) {
1300 return From->getSubclassID() == ClassID::Call;
1301 }
1302};
1303
1304class InvokeInst final : public CallBase {
1305 /// Use Context::createInvokeInst(). Don't call the
1306 /// constructor directly.
1308 : CallBase(ClassID::Invoke, Opcode::Invoke, I, Ctx) {}
1309 friend class Context; // For accessing the constructor in
1310 // create*()
1311
1312public:
1313 static InvokeInst *create(FunctionType *FTy, Value *Func,
1314 BasicBlock *IfNormal, BasicBlock *IfException,
1315 ArrayRef<Value *> Args, BBIterator WhereIt,
1316 BasicBlock *WhereBB, Context &Ctx,
1317 const Twine &NameStr = "");
1318 static InvokeInst *create(FunctionType *FTy, Value *Func,
1319 BasicBlock *IfNormal, BasicBlock *IfException,
1320 ArrayRef<Value *> Args, Instruction *InsertBefore,
1321 Context &Ctx, const Twine &NameStr = "");
1322 static InvokeInst *create(FunctionType *FTy, Value *Func,
1323 BasicBlock *IfNormal, BasicBlock *IfException,
1324 ArrayRef<Value *> Args, BasicBlock *InsertAtEnd,
1325 Context &Ctx, const Twine &NameStr = "");
1326
1327 static bool classof(const Value *From) {
1328 return From->getSubclassID() == ClassID::Invoke;
1329 }
1330 BasicBlock *getNormalDest() const;
1331 BasicBlock *getUnwindDest() const;
1332 void setNormalDest(BasicBlock *BB);
1333 void setUnwindDest(BasicBlock *BB);
1334 // TODO: Return a `LandingPadInst` once implemented.
1336 BasicBlock *getSuccessor(unsigned SuccIdx) const;
1337 void setSuccessor(unsigned SuccIdx, BasicBlock *NewSucc) {
1338 assert(SuccIdx < 2 && "Successor # out of range for invoke!");
1339 if (SuccIdx == 0)
1340 setNormalDest(NewSucc);
1341 else
1342 setUnwindDest(NewSucc);
1343 }
1344 unsigned getNumSuccessors() const {
1345 return cast<llvm::InvokeInst>(Val)->getNumSuccessors();
1346 }
1347};
1348
1349class CallBrInst final : public CallBase {
1350 /// Use Context::createCallBrInst(). Don't call the
1351 /// constructor directly.
1353 : CallBase(ClassID::CallBr, Opcode::CallBr, I, Ctx) {}
1354 friend class Context; // For accessing the constructor in
1355 // create*()
1356
1357public:
1358 static CallBrInst *create(FunctionType *FTy, Value *Func,
1359 BasicBlock *DefaultDest,
1360 ArrayRef<BasicBlock *> IndirectDests,
1361 ArrayRef<Value *> Args, BBIterator WhereIt,
1362 BasicBlock *WhereBB, Context &Ctx,
1363 const Twine &NameStr = "");
1364 static CallBrInst *create(FunctionType *FTy, Value *Func,
1365 BasicBlock *DefaultDest,
1366 ArrayRef<BasicBlock *> IndirectDests,
1367 ArrayRef<Value *> Args, Instruction *InsertBefore,
1368 Context &Ctx, const Twine &NameStr = "");
1369 static CallBrInst *create(FunctionType *FTy, Value *Func,
1370 BasicBlock *DefaultDest,
1371 ArrayRef<BasicBlock *> IndirectDests,
1372 ArrayRef<Value *> Args, BasicBlock *InsertAtEnd,
1373 Context &Ctx, const Twine &NameStr = "");
1374 static bool classof(const Value *From) {
1375 return From->getSubclassID() == ClassID::CallBr;
1376 }
1377 unsigned getNumIndirectDests() const {
1378 return cast<llvm::CallBrInst>(Val)->getNumIndirectDests();
1379 }
1380 Value *getIndirectDestLabel(unsigned Idx) const;
1381 Value *getIndirectDestLabelUse(unsigned Idx) const;
1382 BasicBlock *getDefaultDest() const;
1383 BasicBlock *getIndirectDest(unsigned Idx) const;
1385 void setDefaultDest(BasicBlock *BB);
1386 void setIndirectDest(unsigned Idx, BasicBlock *BB);
1387 BasicBlock *getSuccessor(unsigned Idx) const;
1388 unsigned getNumSuccessors() const {
1389 return cast<llvm::CallBrInst>(Val)->getNumSuccessors();
1390 }
1391};
1392
1394 : public SingleLLVMInstructionImpl<llvm::GetElementPtrInst> {
1395 /// Use Context::createGetElementPtrInst(). Don't call
1396 /// the constructor directly.
1398 : SingleLLVMInstructionImpl(ClassID::GetElementPtr, Opcode::GetElementPtr,
1399 I, Ctx) {}
1401 : SingleLLVMInstructionImpl(SubclassID, Opcode::GetElementPtr, I, Ctx) {}
1402 friend class Context; // For accessing the constructor in
1403 // create*()
1404
1405public:
1406 static Value *create(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1407 BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
1408 const Twine &NameStr = "");
1409 static Value *create(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1410 Instruction *InsertBefore, Context &Ctx,
1411 const Twine &NameStr = "");
1412 static Value *create(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1413 BasicBlock *InsertAtEnd, Context &Ctx,
1414 const Twine &NameStr = "");
1415
1416 static bool classof(const Value *From) {
1417 return From->getSubclassID() == ClassID::GetElementPtr;
1418 }
1419
1421 return cast<llvm::GetElementPtrInst>(Val)->getSourceElementType();
1422 }
1424 return cast<llvm::GetElementPtrInst>(Val)->getResultElementType();
1425 }
1426 unsigned getAddressSpace() const {
1427 return cast<llvm::GetElementPtrInst>(Val)->getAddressSpace();
1428 }
1429
1430 inline op_iterator idx_begin() { return op_begin() + 1; }
1432 return const_cast<GetElementPtrInst *>(this)->idx_begin();
1433 }
1434 inline op_iterator idx_end() { return op_end(); }
1436 return const_cast<GetElementPtrInst *>(this)->idx_end();
1437 }
1439 return make_range(idx_begin(), idx_end());
1440 }
1442 return const_cast<GetElementPtrInst *>(this)->indices();
1443 }
1444
1445 Value *getPointerOperand() const;
1446 static unsigned getPointerOperandIndex() {
1448 }
1450 return cast<llvm::GetElementPtrInst>(Val)->getPointerOperandType();
1451 }
1452 unsigned getPointerAddressSpace() const {
1453 return cast<llvm::GetElementPtrInst>(Val)->getPointerAddressSpace();
1454 }
1455 unsigned getNumIndices() const {
1456 return cast<llvm::GetElementPtrInst>(Val)->getNumIndices();
1457 }
1458 bool hasIndices() const {
1459 return cast<llvm::GetElementPtrInst>(Val)->hasIndices();
1460 }
1462 return cast<llvm::GetElementPtrInst>(Val)->hasAllConstantIndices();
1463 }
1465 return cast<llvm::GetElementPtrInst>(Val)->getNoWrapFlags();
1466 }
1467 bool isInBounds() const {
1468 return cast<llvm::GetElementPtrInst>(Val)->isInBounds();
1469 }
1471 return cast<llvm::GetElementPtrInst>(Val)->hasNoUnsignedSignedWrap();
1472 }
1473 bool hasNoUnsignedWrap() const {
1474 return cast<llvm::GetElementPtrInst>(Val)->hasNoUnsignedWrap();
1475 }
1477 return cast<llvm::GetElementPtrInst>(Val)->accumulateConstantOffset(DL,
1478 Offset);
1479 }
1480 // TODO: Add missing member functions.
1481};
1482
1483class SwitchInst : public SingleLLVMInstructionImpl<llvm::SwitchInst> {
1484public:
1486 : SingleLLVMInstructionImpl(ClassID::Switch, Opcode::Switch, SI, Ctx) {}
1487
1488 static constexpr const unsigned DefaultPseudoIndex =
1490
1491 static SwitchInst *create(Value *V, BasicBlock *Dest, unsigned NumCases,
1492 BasicBlock::iterator WhereIt, BasicBlock *WhereBB,
1493 Context &Ctx, const Twine &Name = "");
1494
1495 Value *getCondition() const;
1496 void setCondition(Value *V);
1497 BasicBlock *getDefaultDest() const;
1499 return cast<llvm::SwitchInst>(Val)->defaultDestUndefined();
1500 }
1501 void setDefaultDest(BasicBlock *DefaultCase);
1502 unsigned getNumCases() const {
1503 return cast<llvm::SwitchInst>(Val)->getNumCases();
1504 }
1505
1510 const BasicBlock>;
1513
1514 /// Returns a read/write iterator that points to the first case in the
1515 /// SwitchInst.
1516 CaseIt case_begin() { return CaseIt(this, 0); }
1517 ConstCaseIt case_begin() const { return ConstCaseIt(this, 0); }
1518 /// Returns a read/write iterator that points one past the last in the
1519 /// SwitchInst.
1520 CaseIt case_end() { return CaseIt(this, getNumCases()); }
1521 ConstCaseIt case_end() const { return ConstCaseIt(this, getNumCases()); }
1522 /// Iteration adapter for range-for loops.
1524 return make_range(case_begin(), case_end());
1525 }
1527 return make_range(case_begin(), case_end());
1528 }
1531 return ConstCaseIt(this, DefaultPseudoIndex);
1532 }
1534 return CaseIt(
1535 this,
1536 const_cast<const SwitchInst *>(this)->findCaseValue(C)->getCaseIndex());
1537 }
1539 ConstCaseIt I = llvm::find_if(cases(), [C](const ConstCaseHandle &Case) {
1540 return Case.getCaseValue() == C;
1541 });
1542 if (I != case_end())
1543 return I;
1544 return case_default();
1545 }
1547
1548 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1549 /// This method removes the specified case and its successor from the switch
1550 /// instruction. Note that this operation may reorder the remaining cases at
1551 /// index idx and above.
1552 /// Note:
1553 /// This action invalidates iterators for all cases following the one removed,
1554 /// including the case_end() iterator. It returns an iterator for the next
1555 /// case.
1557
1558 unsigned getNumSuccessors() const {
1559 return cast<llvm::SwitchInst>(Val)->getNumSuccessors();
1560 }
1561 BasicBlock *getSuccessor(unsigned Idx) const;
1562 void setSuccessor(unsigned Idx, BasicBlock *NewSucc);
1563 static bool classof(const Value *From) {
1564 return From->getSubclassID() == ClassID::Switch;
1565 }
1566};
1567
1569 static Opcode getUnaryOpcode(llvm::Instruction::UnaryOps UnOp) {
1570 switch (UnOp) {
1571 case llvm::Instruction::FNeg:
1572 return Opcode::FNeg;
1573 case llvm::Instruction::UnaryOpsEnd:
1574 llvm_unreachable("Bad UnOp!");
1575 }
1576 llvm_unreachable("Unhandled UnOp!");
1577 }
1579 : UnaryInstruction(ClassID::UnOp, getUnaryOpcode(UO->getOpcode()), UO,
1580 Ctx) {}
1581 friend Context; // for constructor.
1582public:
1583 static Value *create(Instruction::Opcode Op, Value *OpV, BBIterator WhereIt,
1584 BasicBlock *WhereBB, Context &Ctx,
1585 const Twine &Name = "");
1586 static Value *create(Instruction::Opcode Op, Value *OpV,
1587 Instruction *InsertBefore, Context &Ctx,
1588 const Twine &Name = "");
1589 static Value *create(Instruction::Opcode Op, Value *OpV,
1590 BasicBlock *InsertAtEnd, Context &Ctx,
1591 const Twine &Name = "");
1593 Value *CopyFrom, BBIterator WhereIt,
1594 BasicBlock *WhereBB, Context &Ctx,
1595 const Twine &Name = "");
1597 Value *CopyFrom,
1598 Instruction *InsertBefore, Context &Ctx,
1599 const Twine &Name = "");
1601 Value *CopyFrom, BasicBlock *InsertAtEnd,
1602 Context &Ctx, const Twine &Name = "");
1603 /// For isa/dyn_cast.
1604 static bool classof(const Value *From) {
1605 return From->getSubclassID() == ClassID::UnOp;
1606 }
1607};
1608
1609class BinaryOperator : public SingleLLVMInstructionImpl<llvm::BinaryOperator> {
1610 static Opcode getBinOpOpcode(llvm::Instruction::BinaryOps BinOp) {
1611 switch (BinOp) {
1612 case llvm::Instruction::Add:
1613 return Opcode::Add;
1614 case llvm::Instruction::FAdd:
1615 return Opcode::FAdd;
1616 case llvm::Instruction::Sub:
1617 return Opcode::Sub;
1618 case llvm::Instruction::FSub:
1619 return Opcode::FSub;
1620 case llvm::Instruction::Mul:
1621 return Opcode::Mul;
1622 case llvm::Instruction::FMul:
1623 return Opcode::FMul;
1624 case llvm::Instruction::UDiv:
1625 return Opcode::UDiv;
1626 case llvm::Instruction::SDiv:
1627 return Opcode::SDiv;
1628 case llvm::Instruction::FDiv:
1629 return Opcode::FDiv;
1630 case llvm::Instruction::URem:
1631 return Opcode::URem;
1632 case llvm::Instruction::SRem:
1633 return Opcode::SRem;
1634 case llvm::Instruction::FRem:
1635 return Opcode::FRem;
1636 case llvm::Instruction::Shl:
1637 return Opcode::Shl;
1638 case llvm::Instruction::LShr:
1639 return Opcode::LShr;
1640 case llvm::Instruction::AShr:
1641 return Opcode::AShr;
1642 case llvm::Instruction::And:
1643 return Opcode::And;
1644 case llvm::Instruction::Or:
1645 return Opcode::Or;
1646 case llvm::Instruction::Xor:
1647 return Opcode::Xor;
1648 case llvm::Instruction::BinaryOpsEnd:
1649 llvm_unreachable("Bad BinOp!");
1650 }
1651 llvm_unreachable("Unhandled BinOp!");
1652 }
1654 : SingleLLVMInstructionImpl(ClassID::BinaryOperator,
1655 getBinOpOpcode(BinOp->getOpcode()), BinOp,
1656 Ctx) {}
1657 friend class Context; // For constructor.
1658
1659public:
1661 BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
1662 const Twine &Name = "");
1664 Instruction *InsertBefore, Context &Ctx,
1665 const Twine &Name = "");
1667 BasicBlock *InsertAtEnd, Context &Ctx,
1668 const Twine &Name = "");
1669
1671 Value *RHS, Value *CopyFrom,
1672 BBIterator WhereIt, BasicBlock *WhereBB,
1673 Context &Ctx, const Twine &Name = "");
1675 Value *RHS, Value *CopyFrom,
1676 Instruction *InsertBefore, Context &Ctx,
1677 const Twine &Name = "");
1679 Value *RHS, Value *CopyFrom,
1680 BasicBlock *InsertAtEnd, Context &Ctx,
1681 const Twine &Name = "");
1682 /// For isa/dyn_cast.
1683 static bool classof(const Value *From) {
1684 return From->getSubclassID() == ClassID::BinaryOperator;
1685 }
1687};
1688
1689class AtomicRMWInst : public SingleLLVMInstructionImpl<llvm::AtomicRMWInst> {
1691 : SingleLLVMInstructionImpl(ClassID::AtomicRMW,
1692 Instruction::Opcode::AtomicRMW, Atomic, Ctx) {
1693 }
1694 friend class Context; // For constructor.
1695
1696public:
1699 return cast<llvm::AtomicRMWInst>(Val)->getOperation();
1700 }
1703 }
1704 static bool isFPOperation(BinOp Op) {
1706 }
1708 cast<llvm::AtomicRMWInst>(Val)->setOperation(Op);
1709 }
1710 Align getAlign() const { return cast<llvm::AtomicRMWInst>(Val)->getAlign(); }
1711 void setAlignment(Align Align);
1712 bool isVolatile() const {
1713 return cast<llvm::AtomicRMWInst>(Val)->isVolatile();
1714 }
1715 void setVolatile(bool V);
1717 return cast<llvm::AtomicRMWInst>(Val)->getOrdering();
1718 }
1719 void setOrdering(AtomicOrdering Ordering);
1721 return cast<llvm::AtomicRMWInst>(Val)->getSyncScopeID();
1722 }
1723 void setSyncScopeID(SyncScope::ID SSID);
1725 const Value *getPointerOperand() const {
1726 return const_cast<AtomicRMWInst *>(this)->getPointerOperand();
1727 }
1729 const Value *getValOperand() const {
1730 return const_cast<AtomicRMWInst *>(this)->getValOperand();
1731 }
1732 unsigned getPointerAddressSpace() const {
1733 return cast<llvm::AtomicRMWInst>(Val)->getPointerAddressSpace();
1734 }
1736 return cast<llvm::AtomicRMWInst>(Val)->isFloatingPointOperation();
1737 }
1738 static bool classof(const Value *From) {
1739 return From->getSubclassID() == ClassID::AtomicRMW;
1740 }
1741
1744 BBIterator WhereIt, BasicBlock *WhereBB,
1745 Context &Ctx,
1747 const Twine &Name = "");
1750 Instruction *InsertBefore, Context &Ctx,
1752 const Twine &Name = "");
1755 BasicBlock *InsertAtEnd, Context &Ctx,
1757 const Twine &Name = "");
1758};
1759
1761 : public SingleLLVMInstructionImpl<llvm::AtomicCmpXchgInst> {
1763 : SingleLLVMInstructionImpl(ClassID::AtomicCmpXchg,
1764 Instruction::Opcode::AtomicCmpXchg, Atomic,
1765 Ctx) {}
1766 friend class Context; // For constructor.
1767
1768public:
1769 /// Return the alignment of the memory that is being allocated by the
1770 /// instruction.
1771 Align getAlign() const {
1772 return cast<llvm::AtomicCmpXchgInst>(Val)->getAlign();
1773 }
1774
1775 void setAlignment(Align Align);
1776 /// Return true if this is a cmpxchg from a volatile memory
1777 /// location.
1778 bool isVolatile() const {
1779 return cast<llvm::AtomicCmpXchgInst>(Val)->isVolatile();
1780 }
1781 /// Specify whether this is a volatile cmpxchg.
1782 void setVolatile(bool V);
1783 /// Return true if this cmpxchg may spuriously fail.
1784 bool isWeak() const { return cast<llvm::AtomicCmpXchgInst>(Val)->isWeak(); }
1785 void setWeak(bool IsWeak);
1788 }
1791 }
1793 return cast<llvm::AtomicCmpXchgInst>(Val)->getSuccessOrdering();
1794 }
1795 void setSuccessOrdering(AtomicOrdering Ordering);
1796
1798 return cast<llvm::AtomicCmpXchgInst>(Val)->getFailureOrdering();
1799 }
1800 void setFailureOrdering(AtomicOrdering Ordering);
1802 return cast<llvm::AtomicCmpXchgInst>(Val)->getMergedOrdering();
1803 }
1805 return cast<llvm::AtomicCmpXchgInst>(Val)->getSyncScopeID();
1806 }
1807 void setSyncScopeID(SyncScope::ID SSID);
1809 const Value *getPointerOperand() const {
1810 return const_cast<AtomicCmpXchgInst *>(this)->getPointerOperand();
1811 }
1812
1814 const Value *getCompareOperand() const {
1815 return const_cast<AtomicCmpXchgInst *>(this)->getCompareOperand();
1816 }
1817
1819 const Value *getNewValOperand() const {
1820 return const_cast<AtomicCmpXchgInst *>(this)->getNewValOperand();
1821 }
1822
1823 /// Returns the address space of the pointer operand.
1824 unsigned getPointerAddressSpace() const {
1825 return cast<llvm::AtomicCmpXchgInst>(Val)->getPointerAddressSpace();
1826 }
1827
1828 static AtomicCmpXchgInst *
1829 create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1830 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
1831 BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
1832 SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
1833 static AtomicCmpXchgInst *
1834 create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1835 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
1836 Instruction *InsertBefore, Context &Ctx,
1837 SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
1838 static AtomicCmpXchgInst *
1839 create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1840 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
1841 BasicBlock *InsertAtEnd, Context &Ctx,
1842 SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
1843};
1844
1845class AllocaInst final : public UnaryInstruction {
1847 : UnaryInstruction(ClassID::Alloca, Instruction::Opcode::Alloca, AI,
1848 Ctx) {}
1849 friend class Context; // For constructor.
1850
1851public:
1852 static AllocaInst *create(Type *Ty, unsigned AddrSpace, BBIterator WhereIt,
1853 BasicBlock *WhereBB, Context &Ctx,
1854 Value *ArraySize = nullptr, const Twine &Name = "");
1855 static AllocaInst *create(Type *Ty, unsigned AddrSpace,
1856 Instruction *InsertBefore, Context &Ctx,
1857 Value *ArraySize = nullptr, const Twine &Name = "");
1858 static AllocaInst *create(Type *Ty, unsigned AddrSpace,
1859 BasicBlock *InsertAtEnd, Context &Ctx,
1860 Value *ArraySize = nullptr, const Twine &Name = "");
1861
1862 /// Return true if there is an allocation size parameter to the allocation
1863 /// instruction that is not 1.
1864 bool isArrayAllocation() const {
1865 return cast<llvm::AllocaInst>(Val)->isArrayAllocation();
1866 }
1867 /// Get the number of elements allocated. For a simple allocation of a single
1868 /// element, this will return a constant 1 value.
1870 const Value *getArraySize() const {
1871 return const_cast<AllocaInst *>(this)->getArraySize();
1872 }
1873 /// Overload to return most specific pointer type.
1875 return cast<llvm::AllocaInst>(Val)->getType();
1876 }
1877 /// Return the address space for the allocation.
1878 unsigned getAddressSpace() const {
1879 return cast<llvm::AllocaInst>(Val)->getAddressSpace();
1880 }
1881 /// Get allocation size in bytes. Returns std::nullopt if size can't be
1882 /// determined, e.g. in case of a VLA.
1883 std::optional<TypeSize> getAllocationSize(const DataLayout &DL) const {
1884 return cast<llvm::AllocaInst>(Val)->getAllocationSize(DL);
1885 }
1886 /// Get allocation size in bits. Returns std::nullopt if size can't be
1887 /// determined, e.g. in case of a VLA.
1888 std::optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const {
1889 return cast<llvm::AllocaInst>(Val)->getAllocationSizeInBits(DL);
1890 }
1891 /// Return the type that is being allocated by the instruction.
1893 return cast<llvm::AllocaInst>(Val)->getAllocatedType();
1894 }
1895 /// for use only in special circumstances that need to generically
1896 /// transform a whole instruction (eg: IR linking and vectorization).
1897 void setAllocatedType(Type *Ty);
1898 /// Return the alignment of the memory that is being allocated by the
1899 /// instruction.
1900 Align getAlign() const { return cast<llvm::AllocaInst>(Val)->getAlign(); }
1901 void setAlignment(Align Align);
1902 /// Return true if this alloca is in the entry block of the function and is a
1903 /// constant size. If so, the code generator will fold it into the
1904 /// prolog/epilog code, so it is basically free.
1905 bool isStaticAlloca() const {
1906 return cast<llvm::AllocaInst>(Val)->isStaticAlloca();
1907 }
1908 /// Return true if this alloca is used as an inalloca argument to a call. Such
1909 /// allocas are never considered static even if they are in the entry block.
1910 bool isUsedWithInAlloca() const {
1911 return cast<llvm::AllocaInst>(Val)->isUsedWithInAlloca();
1912 }
1913 /// Specify whether this alloca is used to represent the arguments to a call.
1914 void setUsedWithInAlloca(bool V);
1915
1916 static bool classof(const Value *From) {
1917 if (auto *I = dyn_cast<Instruction>(From))
1918 return I->getSubclassID() == Instruction::ClassID::Alloca;
1919 return false;
1920 }
1921};
1922
1924 static Opcode getCastOpcode(llvm::Instruction::CastOps CastOp) {
1925 switch (CastOp) {
1926 case llvm::Instruction::ZExt:
1927 return Opcode::ZExt;
1928 case llvm::Instruction::SExt:
1929 return Opcode::SExt;
1930 case llvm::Instruction::FPToUI:
1931 return Opcode::FPToUI;
1932 case llvm::Instruction::FPToSI:
1933 return Opcode::FPToSI;
1934 case llvm::Instruction::FPExt:
1935 return Opcode::FPExt;
1936 case llvm::Instruction::PtrToInt:
1937 return Opcode::PtrToInt;
1938 case llvm::Instruction::IntToPtr:
1939 return Opcode::IntToPtr;
1940 case llvm::Instruction::SIToFP:
1941 return Opcode::SIToFP;
1942 case llvm::Instruction::UIToFP:
1943 return Opcode::UIToFP;
1944 case llvm::Instruction::Trunc:
1945 return Opcode::Trunc;
1946 case llvm::Instruction::FPTrunc:
1947 return Opcode::FPTrunc;
1948 case llvm::Instruction::BitCast:
1949 return Opcode::BitCast;
1950 case llvm::Instruction::AddrSpaceCast:
1951 return Opcode::AddrSpaceCast;
1952 case llvm::Instruction::CastOpsEnd:
1953 llvm_unreachable("Bad CastOp!");
1954 }
1955 llvm_unreachable("Unhandled CastOp!");
1956 }
1957 /// Use Context::createCastInst(). Don't call the
1958 /// constructor directly.
1960 : UnaryInstruction(ClassID::Cast, getCastOpcode(CI->getOpcode()), CI,
1961 Ctx) {}
1962 friend Context; // for SBCastInstruction()
1963
1964public:
1965 static Value *create(Type *DestTy, Opcode Op, Value *Operand,
1966 BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx,
1967 const Twine &Name = "");
1968 static Value *create(Type *DestTy, Opcode Op, Value *Operand,
1969 Instruction *InsertBefore, Context &Ctx,
1970 const Twine &Name = "");
1971 static Value *create(Type *DestTy, Opcode Op, Value *Operand,
1972 BasicBlock *InsertAtEnd, Context &Ctx,
1973 const Twine &Name = "");
1974 /// For isa/dyn_cast.
1975 static bool classof(const Value *From);
1976 Type *getSrcTy() const { return cast<llvm::CastInst>(Val)->getSrcTy(); }
1977 Type *getDestTy() const { return cast<llvm::CastInst>(Val)->getDestTy(); }
1978};
1979
1980// Helper class to simplify stamping out CastInst subclasses.
1981template <Instruction::Opcode Op> class CastInstImpl : public CastInst {
1982public:
1983 static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1984 BasicBlock *WhereBB, Context &Ctx,
1985 const Twine &Name = "") {
1986 return CastInst::create(DestTy, Op, Src, WhereIt, WhereBB, Ctx, Name);
1987 }
1988 static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1989 Context &Ctx, const Twine &Name = "") {
1990 return create(Src, DestTy, InsertBefore->getIterator(),
1991 InsertBefore->getParent(), Ctx, Name);
1992 }
1993 static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1994 Context &Ctx, const Twine &Name = "") {
1995 return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
1996 }
1997
1998 static bool classof(const Value *From) {
1999 if (auto *I = dyn_cast<Instruction>(From))
2000 return I->getOpcode() == Op;
2001 return false;
2002 }
2003};
2004
2005class TruncInst final : public CastInstImpl<Instruction::Opcode::Trunc> {};
2006class ZExtInst final : public CastInstImpl<Instruction::Opcode::ZExt> {};
2007class SExtInst final : public CastInstImpl<Instruction::Opcode::SExt> {};
2008class FPTruncInst final : public CastInstImpl<Instruction::Opcode::FPTrunc> {};
2009class FPExtInst final : public CastInstImpl<Instruction::Opcode::FPExt> {};
2010class UIToFPInst final : public CastInstImpl<Instruction::Opcode::UIToFP> {};
2011class SIToFPInst final : public CastInstImpl<Instruction::Opcode::SIToFP> {};
2012class FPToUIInst final : public CastInstImpl<Instruction::Opcode::FPToUI> {};
2013class FPToSIInst final : public CastInstImpl<Instruction::Opcode::FPToSI> {};
2014class IntToPtrInst final : public CastInstImpl<Instruction::Opcode::IntToPtr> {
2015};
2016class PtrToIntInst final : public CastInstImpl<Instruction::Opcode::PtrToInt> {
2017};
2018class BitCastInst final : public CastInstImpl<Instruction::Opcode::BitCast> {};
2020 : public CastInstImpl<Instruction::Opcode::AddrSpaceCast> {
2021public:
2022 /// \Returns the pointer operand.
2024 /// \Returns the pointer operand.
2025 const Value *getPointerOperand() const {
2026 return const_cast<AddrSpaceCastInst *>(this)->getPointerOperand();
2027 }
2028 /// \Returns the operand index of the pointer operand.
2029 static unsigned getPointerOperandIndex() { return 0u; }
2030 /// \Returns the address space of the pointer operand.
2031 unsigned getSrcAddressSpace() const {
2033 }
2034 /// \Returns the address space of the result.
2035 unsigned getDestAddressSpace() const {
2036 return getType()->getPointerAddressSpace();
2037 }
2038};
2039
2040class PHINode final : public SingleLLVMInstructionImpl<llvm::PHINode> {
2041 /// Use Context::createPHINode(). Don't call the constructor directly.
2043 : SingleLLVMInstructionImpl(ClassID::PHI, Opcode::PHI, PHI, Ctx) {}
2044 friend Context; // for PHINode()
2045 /// Helper for mapped_iterator.
2046 struct LLVMBBToBB {
2047 Context &Ctx;
2048 LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
2049 BasicBlock *operator()(llvm::BasicBlock *LLVMBB) const;
2050 };
2051
2052public:
2053 static PHINode *create(Type *Ty, unsigned NumReservedValues,
2054 Instruction *InsertBefore, Context &Ctx,
2055 const Twine &Name = "");
2056 /// For isa/dyn_cast.
2057 static bool classof(const Value *From);
2058
2061
2063 LLVMBBToBB BBGetter(Ctx);
2064 return const_block_iterator(cast<llvm::PHINode>(Val)->block_begin(),
2065 BBGetter);
2066 }
2068 LLVMBBToBB BBGetter(Ctx);
2069 return const_block_iterator(cast<llvm::PHINode>(Val)->block_end(),
2070 BBGetter);
2071 }
2073 return make_range(block_begin(), block_end());
2074 }
2075
2077
2079
2080 unsigned getNumIncomingValues() const {
2081 return cast<llvm::PHINode>(Val)->getNumIncomingValues();
2082 }
2083 Value *getIncomingValue(unsigned Idx) const;
2084 void setIncomingValue(unsigned Idx, Value *V);
2085 static unsigned getOperandNumForIncomingValue(unsigned Idx) {
2087 }
2088 static unsigned getIncomingValueNumForOperand(unsigned Idx) {
2090 }
2091 BasicBlock *getIncomingBlock(unsigned Idx) const;
2092 BasicBlock *getIncomingBlock(const Use &U) const;
2093
2094 void setIncomingBlock(unsigned Idx, BasicBlock *BB);
2095
2096 void addIncoming(Value *V, BasicBlock *BB);
2097
2098 Value *removeIncomingValue(unsigned Idx);
2100
2101 int getBasicBlockIndex(const BasicBlock *BB) const;
2102 Value *getIncomingValueForBlock(const BasicBlock *BB) const;
2103
2104 Value *hasConstantValue() const;
2105
2107 return cast<llvm::PHINode>(Val)->hasConstantOrUndefValue();
2108 }
2109 bool isComplete() const { return cast<llvm::PHINode>(Val)->isComplete(); }
2110 void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New);
2111 void removeIncomingValueIf(function_ref<bool(unsigned)> Predicate);
2112 // TODO: Implement
2113 // void copyIncomingBlocks(iterator_range<const_block_iterator> BBRange,
2114 // uint32_t ToIdx = 0)
2115};
2116
2117/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
2118/// an OpaqueInstr.
2119class OpaqueInst : public SingleLLVMInstructionImpl<llvm::Instruction> {
2121 : SingleLLVMInstructionImpl(ClassID::Opaque, Opcode::Opaque, I, Ctx) {}
2123 : SingleLLVMInstructionImpl(SubclassID, Opcode::Opaque, I, Ctx) {}
2124 friend class Context; // For constructor.
2125
2126public:
2127 static bool classof(const sandboxir::Value *From) {
2128 return From->getSubclassID() == ClassID::Opaque;
2129 }
2130};
2131
2132class Context {
2133protected:
2136
2137 /// Maps LLVM Value to the corresponding sandboxir::Value. Owns all
2138 /// SandboxIR objects.
2141
2142 /// Remove \p V from the maps and returns the unique_ptr.
2143 std::unique_ptr<Value> detachLLVMValue(llvm::Value *V);
2144 /// Remove \p SBV from all SandboxIR maps and stop owning it. This effectively
2145 /// detaches \p V from the underlying IR.
2146 std::unique_ptr<Value> detach(Value *V);
2147 friend void Instruction::eraseFromParent(); // For detach().
2148 /// Take ownership of VPtr and store it in `LLVMValueToValueMap`.
2149 Value *registerValue(std::unique_ptr<Value> &&VPtr);
2150 friend class EraseFromParent; // For registerValue().
2151 /// This is the actual function that creates sandboxir values for \p V,
2152 /// and among others handles all instruction types.
2154 /// Get or create a sandboxir::Argument for an existing LLVM IR \p LLVMArg.
2156 auto Pair = LLVMValueToValueMap.insert({LLVMArg, nullptr});
2157 auto It = Pair.first;
2158 if (Pair.second) {
2159 It->second = std::unique_ptr<Argument>(new Argument(LLVMArg, *this));
2160 return cast<Argument>(It->second.get());
2161 }
2162 return cast<Argument>(It->second.get());
2163 }
2164 /// Get or create a sandboxir::Value for an existing LLVM IR \p LLVMV.
2166 return getOrCreateValueInternal(LLVMV, 0);
2167 }
2168 /// Get or create a sandboxir::Constant from an existing LLVM IR \p LLVMC.
2170 return cast<Constant>(getOrCreateValueInternal(LLVMC, 0));
2171 }
2172 friend class ConstantInt; // For getOrCreateConstant().
2173 /// Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
2174 /// also create all contents of the block.
2176
2177 friend class BasicBlock; // For getOrCreateValue().
2178
2181
2183 friend SelectInst; // For createSelectInst()
2185 friend InsertElementInst; // For createInsertElementInst()
2187 friend ExtractElementInst; // For createExtractElementInst()
2189 friend BranchInst; // For createBranchInst()
2191 friend LoadInst; // For createLoadInst()
2193 friend StoreInst; // For createStoreInst()
2195 friend ReturnInst; // For createReturnInst()
2197 friend CallInst; // For createCallInst()
2199 friend InvokeInst; // For createInvokeInst()
2201 friend CallBrInst; // For createCallBrInst()
2203 friend GetElementPtrInst; // For createGetElementPtrInst()
2205 friend SwitchInst; // For createSwitchInst()
2207 friend UnaryOperator; // For createUnaryOperator()
2209 friend BinaryOperator; // For createBinaryOperator()
2211 friend AtomicRMWInst; // For createAtomicRMWInst()
2213 friend AtomicCmpXchgInst; // For createAtomicCmpXchgInst()
2215 friend AllocaInst; // For createAllocaInst()
2217 friend CastInst; // For createCastInst()
2219 friend PHINode; // For createPHINode()
2221 friend UnreachableInst; // For createUnreachableInst()
2222
2223public:
2225 : LLVMCtx(LLVMCtx), IRTracker(*this),
2227
2229 /// Convenience function for `getTracker().save()`
2230 void save() { IRTracker.save(); }
2231 /// Convenience function for `getTracker().revert()`
2233 /// Convenience function for `getTracker().accept()`
2235
2237 const sandboxir::Value *getValue(const llvm::Value *V) const {
2238 return getValue(const_cast<llvm::Value *>(V));
2239 }
2240 /// Create a sandboxir::Function for an existing LLVM IR \p F, including all
2241 /// blocks and instructions.
2242 /// This is the main API function for creating Sandbox IR.
2244
2245 /// \Returns the number of values registered with Context.
2246 size_t getNumValues() const { return LLVMValueToValueMap.size(); }
2247};
2248
2249class Function : public Constant {
2250 /// Helper for mapped_iterator.
2251 struct LLVMBBToBB {
2252 Context &Ctx;
2253 LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
2254 BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
2255 return *cast<BasicBlock>(Ctx.getValue(&LLVMBB));
2256 }
2257 };
2258 /// Use Context::createFunction() instead.
2260 : Constant(ClassID::Function, F, Ctx) {}
2261 friend class Context; // For constructor.
2262
2263public:
2264 /// For isa/dyn_cast.
2265 static bool classof(const sandboxir::Value *From) {
2266 return From->getSubclassID() == ClassID::Function;
2267 }
2268
2269 Argument *getArg(unsigned Idx) const {
2270 llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
2271 return cast<Argument>(Ctx.getValue(Arg));
2272 }
2273
2274 size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
2275 bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }
2276
2278 iterator begin() const {
2279 LLVMBBToBB BBGetter(Ctx);
2280 return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
2281 }
2282 iterator end() const {
2283 LLVMBBToBB BBGetter(Ctx);
2284 return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
2285 }
2287 return cast<llvm::Function>(Val)->getFunctionType();
2288 }
2289
2290#ifndef NDEBUG
2291 void verify() const final {
2292 assert(isa<llvm::Function>(Val) && "Expected Function!");
2293 }
2294 void dumpNameAndArgs(raw_ostream &OS) const;
2295 void dumpOS(raw_ostream &OS) const final;
2296#endif
2297};
2298
2299} // namespace sandboxir
2300} // namespace llvm
2301
2302#endif // LLVM_SANDBOXIR_SANDBOXIR_H
aarch64 promote const
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:533
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
ppc ctr loops PowerPC CTR Loops Verify
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:78
an instruction to allocate memory on the stack
Definition: Instructions.h:61
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:495
static bool isValidFailureOrdering(AtomicOrdering Ordering)
Definition: Instructions.h:566
static bool isValidSuccessOrdering(AtomicOrdering Ordering)
Definition: Instructions.h:561
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:696
static bool isFPOperation(BinOp Op)
Definition: Instructions.h:791
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:708
static StringRef getOperationName(BinOp Op)
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
Conditional or Unconditional Branch instruction.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:530
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition: InstrTypes.h:694
ConstantFolder - Create constants with minimum, target independent, folding.
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
This instruction extracts a single (scalar) element from a VectorType value.
static bool isValidOperands(const Value *Vec, const Value *Idx)
Return true if an extractelement instruction can be formed with the specified operands.
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
Class to represent function types.
Definition: DerivedTypes.h:103
Represents flags for the getelementptr instruction/expression.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:915
static unsigned getPointerOperandIndex()
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2686
This instruction inserts a single (scalar) element into a VectorType value.
static bool isValidOperands(const Value *Vec, const Value *NewElt, const Value *Idx)
Return true if an insertelement instruction can be formed with the specified operands.
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:174
static unsigned getOperandNumForIncomingValue(unsigned i)
static unsigned getIncomingValueNumForOperand(unsigned i)
Class to represent pointers.
Definition: DerivedTypes.h:646
Return a value (possibly void), from a function.
This class represents the LLVM 'select' instruction.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:290
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
ConstantIntT * getCaseValue() const
Resolves case value for current case.
Multiway switch.
static const unsigned DefaultPseudoIndex
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
UnaryOps getOpcode() const
Definition: InstrTypes.h:171
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
Base class of all SIMD vector types.
Definition: DerivedTypes.h:403
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
IteratorT end() const
IteratorT begin() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
unsigned getSrcAddressSpace() const
\Returns the address space of the pointer operand.
Definition: SandboxIR.h:2031
Value * getPointerOperand()
\Returns the pointer operand.
Definition: SandboxIR.h:2023
unsigned getDestAddressSpace() const
\Returns the address space of the result.
Definition: SandboxIR.h:2035
const Value * getPointerOperand() const
\Returns the pointer operand.
Definition: SandboxIR.h:2025
static unsigned getPointerOperandIndex()
\Returns the operand index of the pointer operand.
Definition: SandboxIR.h:2029
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
Definition: SandboxIR.h:1910
Value * getArraySize()
Get the number of elements allocated.
Definition: SandboxIR.cpp:1687
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: SandboxIR.h:1892
bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Definition: SandboxIR.h:1905
bool isArrayAllocation() const
Return true if there is an allocation size parameter to the allocation instruction that is not 1.
Definition: SandboxIR.h:1864
std::optional< TypeSize > getAllocationSizeInBits(const DataLayout &DL) const
Get allocation size in bits.
Definition: SandboxIR.h:1888
void setAlignment(Align Align)
Definition: SandboxIR.cpp:1672
unsigned getAddressSpace() const
Return the address space for the allocation.
Definition: SandboxIR.h:1878
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: SandboxIR.h:1900
PointerType * getType() const
Overload to return most specific pointer type.
Definition: SandboxIR.h:1874
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
Definition: SandboxIR.cpp:1680
static AllocaInst * create(Type *Ty, unsigned AddrSpace, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, Value *ArraySize=nullptr, const Twine &Name="")
Definition: SandboxIR.cpp:1639
static bool classof(const Value *From)
Definition: SandboxIR.h:1916
std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
Definition: SandboxIR.h:1883
const Value * getArraySize() const
Definition: SandboxIR.h:1870
void setAllocatedType(Type *Ty)
for use only in special circumstances that need to generically transform a whole instruction (eg: IR ...
Definition: SandboxIR.cpp:1665
Argument of a sandboxir::Function.
Definition: SandboxIR.h:381
void dumpOS(raw_ostream &OS) const final
Definition: SandboxIR.cpp:214
void printAsOperand(raw_ostream &OS) const
Definition: SandboxIR.cpp:211
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:391
static bool classof(const sandboxir::Value *From)
Definition: SandboxIR.h:387
void setSyncScopeID(SyncScope::ID SSID)
Definition: SandboxIR.cpp:1541
void setVolatile(bool V)
Specify whether this is a volatile cmpxchg.
Definition: SandboxIR.cpp:1609
const Value * getNewValOperand() const
Definition: SandboxIR.h:1819
void setSuccessOrdering(AtomicOrdering Ordering)
Definition: SandboxIR.cpp:1623
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: SandboxIR.h:1824
AtomicOrdering getMergedOrdering() const
Definition: SandboxIR.h:1801
const Value * getCompareOperand() const
Definition: SandboxIR.h:1814
static AtomicCmpXchgInst * create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align, AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, SyncScope::ID SSID=SyncScope::System, const Twine &Name="")
Definition: SandboxIR.cpp:1562
const Value * getPointerOperand() const
Definition: SandboxIR.h:1809
AtomicOrdering getFailureOrdering() const
Definition: SandboxIR.h:1797
static bool isValidFailureOrdering(AtomicOrdering Ordering)
Definition: SandboxIR.h:1789
bool isVolatile() const
Return true if this is a cmpxchg from a volatile memory location.
Definition: SandboxIR.h:1778
static bool isValidSuccessOrdering(AtomicOrdering Ordering)
Definition: SandboxIR.h:1786
AtomicOrdering getSuccessOrdering() const
Definition: SandboxIR.h:1792
void setFailureOrdering(AtomicOrdering Ordering)
Definition: SandboxIR.cpp:1631
SyncScope::ID getSyncScopeID() const
Definition: SandboxIR.h:1804
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: SandboxIR.h:1771
bool isWeak() const
Return true if this cmpxchg may spuriously fail.
Definition: SandboxIR.h:1784
static AtomicRMWInst * create(BinOp Op, Value *Ptr, Value *Val, MaybeAlign Align, AtomicOrdering Ordering, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, SyncScope::ID SSID=SyncScope::System, const Twine &Name="")
Definition: SandboxIR.cpp:1509
const Value * getPointerOperand() const
Definition: SandboxIR.h:1725
unsigned getPointerAddressSpace() const
Definition: SandboxIR.h:1732
llvm::AtomicRMWInst::BinOp BinOp
Definition: SandboxIR.h:1697
SyncScope::ID getSyncScopeID() const
Definition: SandboxIR.h:1720
void setAlignment(Align Align)
Definition: SandboxIR.cpp:1473
const Value * getValOperand() const
Definition: SandboxIR.h:1729
void setSyncScopeID(SyncScope::ID SSID)
Definition: SandboxIR.cpp:1494
static StringRef getOperationName(BinOp Op)
Definition: SandboxIR.h:1701
void setOrdering(AtomicOrdering Ordering)
Definition: SandboxIR.cpp:1487
AtomicOrdering getOrdering() const
Definition: SandboxIR.h:1716
static bool classof(const Value *From)
Definition: SandboxIR.h:1738
bool isFloatingPointOperation() const
Definition: SandboxIR.h:1735
static bool isFPOperation(BinOp Op)
Definition: SandboxIR.h:1704
Iterator for Instructions in a `BasicBlock.
Definition: SandboxIR.h:558
BBIterator operator++(int)
Definition: SandboxIR.h:578
bool operator!=(const BBIterator &Other) const
Definition: SandboxIR.h:593
BBIterator operator--(int)
Definition: SandboxIR.h:584
pointer get() const
\Returns the SBInstruction that corresponds to this iterator, or null if the instruction is not found...
Definition: SandboxIR.h:596
reference operator*() const
Definition: SandboxIR.h:576
std::bidirectional_iterator_tag iterator_category
Definition: SandboxIR.h:564
std::ptrdiff_t difference_type
Definition: SandboxIR.h:560
BBIterator(llvm::BasicBlock *BB, llvm::BasicBlock::iterator It, Context *Ctx)
Definition: SandboxIR.h:574
bool operator==(const BBIterator &Other) const
Definition: SandboxIR.h:589
Contains a list of sandboxir::Instruction's.
Definition: SandboxIR.h:600
void dumpOS(raw_ostream &OS) const final
Definition: SandboxIR.cpp:2243
std::reverse_iterator< iterator > rbegin() const
Definition: SandboxIR.h:625
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:615
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:638
std::reverse_iterator< iterator > rend() const
Definition: SandboxIR.h:628
Function * getParent() const
Definition: SandboxIR.cpp:2175
Instruction & front() const
Definition: SandboxIR.cpp:2226
Instruction * getTerminator() const
Definition: SandboxIR.cpp:2220
Context & getContext() const
Definition: SandboxIR.h:631
Instruction & back() const
Definition: SandboxIR.cpp:2234
iterator end() const
Definition: SandboxIR.h:621
static Value * create(Instruction::Opcode Op, Value *LHS, Value *RHS, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1415
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:1683
static Value * createWithCopiedFlags(Instruction::Opcode Op, Value *LHS, Value *RHS, Value *CopyFrom, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1444
unsigned getNumSuccessors() const
Definition: SandboxIR.h:974
iterator_range< sb_succ_op_iterator > successors()
Definition: SandboxIR.h:995
iterator_range< const_sb_succ_op_iterator > successors() const
Definition: SandboxIR.h:1007
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:653
void setCondition(Value *V)
Definition: SandboxIR.h:973
bool isConditional() const
Definition: SandboxIR.h:969
void setSuccessor(unsigned Idx, BasicBlock *NewSucc)
Definition: SandboxIR.cpp:669
bool isUnconditional() const
Definition: SandboxIR.h:966
BasicBlock * getSuccessor(unsigned SuccIdx) const
Definition: SandboxIR.cpp:662
static BranchInst * create(BasicBlock *IfTrue, Instruction *InsertBefore, Context &Ctx)
Definition: SandboxIR.cpp:611
Value * getCondition() const
Definition: SandboxIR.cpp:657
iterator_range< const_op_iterator > args() const
Definition: SandboxIR.h:1218
CallingConv::ID getCallingConv() const
Definition: SandboxIR.h:1273
bool isMustTailCall() const
Definition: SandboxIR.h:1264
const Function * getCaller() const
Definition: SandboxIR.h:1261
FunctionType * getFunctionType() const
Definition: SandboxIR.h:1161
void setCalledFunction(Function *F)
Definition: SandboxIR.cpp:851
op_iterator arg_begin()
Definition: SandboxIR.h:1207
const_op_iterator arg_end() const
Definition: SandboxIR.h:1212
iterator_range< op_iterator > args()
Definition: SandboxIR.h:1215
static bool classof(const Value *From)
Definition: SandboxIR.h:1154
Value * getArgOperand(unsigned OpIdx) const
Definition: SandboxIR.h:1224
bool isInlineAsm() const
Definition: SandboxIR.h:1276
Function * getCalledFunction() const
Definition: SandboxIR.cpp:843
Use getArgOperandUse(unsigned Idx)
Definition: SandboxIR.h:1237
bool isDataOperand(Use U) const
Definition: SandboxIR.h:1191
unsigned getArgOperandNo(Use U) const
Definition: SandboxIR.h:1245
const_op_iterator data_operands_end() const
Definition: SandboxIR.h:1174
op_iterator data_operands_end()
Definition: SandboxIR.h:1169
unsigned getDataOperandNo(Use U) const
Definition: SandboxIR.h:1196
unsigned getNumTotalBundleOperands() const
Return the total number operands (not operand bundles) used by every operand bundle in this OperandBu...
Definition: SandboxIR.h:1203
iterator_range< op_iterator > data_ops()
Definition: SandboxIR.h:1179
const_op_iterator data_operands_begin() const
Definition: SandboxIR.h:1166
bool data_operands_empty() const
Definition: SandboxIR.h:1185
bool hasArgument(const Value *V) const
Definition: SandboxIR.h:1248
void setCalledOperand(Value *V)
Definition: SandboxIR.h:1271
unsigned arg_size() const
Definition: SandboxIR.h:1222
unsigned data_operands_size() const
Definition: SandboxIR.h:1188
const_op_iterator arg_begin() const
Definition: SandboxIR.h:1208
Intrinsic::ID getIntrinsicID() const
Definition: SandboxIR.h:1268
Use getArgOperandUse(unsigned Idx) const
Definition: SandboxIR.h:1233
Value * getCalledOperand() const
Definition: SandboxIR.cpp:834
op_iterator data_operands_begin()
Definition: SandboxIR.h:1165
bool isArgOperand(Use U) const
Definition: SandboxIR.h:1242
void setArgOperand(unsigned OpIdx, Value *NewOp)
Definition: SandboxIR.h:1228
iterator_range< const_op_iterator > data_ops() const
Definition: SandboxIR.h:1182
Use getCalledOperandUse() const
Definition: SandboxIR.cpp:838
bool isCallee(Use U) const
Definition: SandboxIR.h:1257
bool isIndirectCall() const
Definition: SandboxIR.h:1254
static CallBrInst * create(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &NameStr="")
Definition: SandboxIR.cpp:958
BasicBlock * getIndirectDest(unsigned Idx) const
Definition: SandboxIR.cpp:1016
static bool classof(const Value *From)
Definition: SandboxIR.h:1374
void setDefaultDest(BasicBlock *BB)
Definition: SandboxIR.cpp:1027
Value * getIndirectDestLabel(unsigned Idx) const
Definition: SandboxIR.cpp:1005
Value * getIndirectDestLabelUse(unsigned Idx) const
Definition: SandboxIR.cpp:1008
unsigned getNumIndirectDests() const
Definition: SandboxIR.h:1377
SmallVector< BasicBlock *, 16 > getIndirectDests() const
Definition: SandboxIR.cpp:1020
BasicBlock * getDefaultDest() const
Definition: SandboxIR.cpp:1012
BasicBlock * getSuccessor(unsigned Idx) const
Definition: SandboxIR.cpp:1041
void setIndirectDest(unsigned Idx, BasicBlock *BB)
Definition: SandboxIR.cpp:1033
unsigned getNumSuccessors() const
Definition: SandboxIR.h:1388
static CallInst * create(FunctionType *FTy, Value *Func, ArrayRef< Value * > Args, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &NameStr="")
static bool classof(const Value *From)
Definition: SandboxIR.h:1299
static Value * create(Value *Src, Type *DestTy, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.h:1988
static bool classof(const Value *From)
Definition: SandboxIR.h:1998
static Value * create(Value *Src, Type *DestTy, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.h:1983
static Value * create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.h:1993
static Value * create(Type *DestTy, Opcode Op, Value *Operand, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1691
Type * getSrcTy() const
Definition: SandboxIR.h:1976
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:1722
Type * getDestTy() const
Definition: SandboxIR.h:1977
static ConstantInt * get(Type *Ty, uint64_t V, Context &Ctx, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: SandboxIR.cpp:1783
void verify() const override
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:546
unsigned getUseOperandNo(const Use &Use) const override
\Returns the operand index of Use.
Definition: SandboxIR.h:542
void dumpOS(raw_ostream &OS) const override
Definition: SandboxIR.h:549
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:539
sandboxir::Context & getParent() const
Definition: SandboxIR.h:509
void verify() const override
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:514
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:504
void dumpOS(raw_ostream &OS) const override
Definition: SandboxIR.cpp:1777
unsigned getUseOperandNo(const Use &Use) const override
\Returns the operand index of Use.
Definition: SandboxIR.h:510
friend class ConstantInt
Definition: SandboxIR.h:495
CallBrInst * createCallBrInst(llvm::CallBrInst *I)
Definition: SandboxIR.cpp:2103
GetElementPtrInst * createGetElementPtrInst(llvm::GetElementPtrInst *I)
Definition: SandboxIR.cpp:2115
DenseMap< llvm::Value *, std::unique_ptr< sandboxir::Value > > LLVMValueToValueMap
Maps LLVM Value to the corresponding sandboxir::Value.
Definition: SandboxIR.h:2140
Value * registerValue(std::unique_ptr< Value > &&VPtr)
Take ownership of VPtr and store it in LLVMValueToValueMap.
Definition: SandboxIR.cpp:1846
sandboxir::Value * getValue(llvm::Value *V) const
Definition: SandboxIR.cpp:2155
IRBuilder< ConstantFolder > LLVMIRBuilder
Definition: SandboxIR.h:2179
Argument * getOrCreateArgument(llvm::Argument *LLVMArg)
Get or create a sandboxir::Argument for an existing LLVM IR LLVMArg.
Definition: SandboxIR.h:2155
Function * createFunction(llvm::Function *F)
Create a sandboxir::Function for an existing LLVM IR F, including all blocks and instructions.
Definition: SandboxIR.cpp:2162
Value * getOrCreateValueInternal(llvm::Value *V, llvm::User *U=nullptr)
This is the actual function that creates sandboxir values for V, and among others handles all instruc...
Definition: SandboxIR.cpp:1864
std::unique_ptr< Value > detach(Value *V)
Remove SBV from all SandboxIR maps and stop owning it.
Definition: SandboxIR.cpp:1839
SwitchInst * createSwitchInst(llvm::SwitchInst *I)
Definition: SandboxIR.cpp:2120
UnreachableInst * createUnreachableInst(llvm::UnreachableInst *UI)
Definition: SandboxIR.cpp:2108
const sandboxir::Value * getValue(const llvm::Value *V) const
Definition: SandboxIR.h:2237
void accept()
Convenience function for getTracker().accept()
Definition: SandboxIR.h:2234
BranchInst * createBranchInst(llvm::BranchInst *I)
Definition: SandboxIR.cpp:2073
Constant * getOrCreateConstant(llvm::Constant *LLVMC)
Get or create a sandboxir::Constant from an existing LLVM IR LLVMC.
Definition: SandboxIR.h:2169
BasicBlock * createBasicBlock(llvm::BasicBlock *BB)
Create a sandboxir::BasicBlock for an existing LLVM IR BB.
Definition: SandboxIR.cpp:2045
UnaryOperator * createUnaryOperator(llvm::UnaryOperator *I)
Definition: SandboxIR.cpp:2124
ExtractElementInst * createExtractElementInst(llvm::ExtractElementInst *EEI)
Definition: SandboxIR.cpp:2060
LoadInst * createLoadInst(llvm::LoadInst *LI)
Definition: SandboxIR.cpp:2078
AllocaInst * createAllocaInst(llvm::AllocaInst *I)
Definition: SandboxIR.cpp:2142
Context(LLVMContext &LLVMCtx)
Definition: SandboxIR.h:2224
void revert()
Convenience function for getTracker().revert()
Definition: SandboxIR.h:2232
CallInst * createCallInst(llvm::CallInst *I)
Definition: SandboxIR.cpp:2093
AtomicRMWInst * createAtomicRMWInst(llvm::AtomicRMWInst *I)
Definition: SandboxIR.cpp:2132
std::unique_ptr< Value > detachLLVMValue(llvm::Value *V)
Remove V from the maps and returns the unique_ptr.
Definition: SandboxIR.cpp:1828
StoreInst * createStoreInst(llvm::StoreInst *SI)
Definition: SandboxIR.cpp:2083
void save()
Convenience function for getTracker().save()
Definition: SandboxIR.h:2230
Value * getOrCreateValue(llvm::Value *LLVMV)
Get or create a sandboxir::Value for an existing LLVM IR LLVMV.
Definition: SandboxIR.h:2165
InsertElementInst * createInsertElementInst(llvm::InsertElementInst *IEI)
Definition: SandboxIR.cpp:2067
AtomicCmpXchgInst * createAtomicCmpXchgInst(llvm::AtomicCmpXchgInst *I)
Definition: SandboxIR.cpp:2137
ReturnInst * createReturnInst(llvm::ReturnInst *I)
Definition: SandboxIR.cpp:2088
PHINode * createPHINode(llvm::PHINode *I)
Definition: SandboxIR.cpp:2150
SelectInst * createSelectInst(llvm::SelectInst *SI)
Definition: SandboxIR.cpp:2054
CastInst * createCastInst(llvm::CastInst *I)
Definition: SandboxIR.cpp:2146
LLVMContext & LLVMCtx
Definition: SandboxIR.h:2134
BinaryOperator * createBinaryOperator(llvm::BinaryOperator *I)
Definition: SandboxIR.cpp:2128
InvokeInst * createInvokeInst(llvm::InvokeInst *I)
Definition: SandboxIR.cpp:2098
size_t getNumValues() const
\Returns the number of values registered with Context.
Definition: SandboxIR.h:2246
static Value * create(Value *Vec, Value *Idx, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1752
const Value * getVectorOperand() const
Definition: SandboxIR.h:940
const Value * getIndexOperand() const
Definition: SandboxIR.h:941
VectorType * getVectorOperandType() const
Definition: SandboxIR.h:943
static bool classof(const Value *From)
Definition: SandboxIR.h:931
static bool isValidOperands(const Value *Vec, const Value *Idx)
Definition: SandboxIR.h:935
Argument * getArg(unsigned Idx) const
Definition: SandboxIR.h:2269
FunctionType * getFunctionType() const
Definition: SandboxIR.h:2286
iterator begin() const
Definition: SandboxIR.h:2278
void dumpNameAndArgs(raw_ostream &OS) const
Definition: SandboxIR.cpp:1790
size_t arg_size() const
Definition: SandboxIR.h:2274
void dumpOS(raw_ostream &OS) const final
Definition: SandboxIR.cpp:1805
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:2291
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:2265
mapped_iterator< llvm::Function::iterator, LLVMBBToBB > iterator
Definition: SandboxIR.h:2277
iterator end() const
Definition: SandboxIR.h:2282
iterator_range< op_iterator > indices()
Definition: SandboxIR.h:1438
const_op_iterator idx_begin() const
Definition: SandboxIR.h:1431
unsigned getPointerAddressSpace() const
Definition: SandboxIR.h:1452
GEPNoWrapFlags getNoWrapFlags() const
Definition: SandboxIR.h:1464
static Value * create(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &NameStr="")
static unsigned getPointerOperandIndex()
Definition: SandboxIR.h:1446
static bool classof(const Value *From)
Definition: SandboxIR.h:1416
const_op_iterator idx_end() const
Definition: SandboxIR.h:1435
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Definition: SandboxIR.h:1476
iterator_range< const_op_iterator > indices() const
Definition: SandboxIR.h:1441
static Value * create(Value *Vec, Value *NewElt, Value *Idx, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1726
static bool isValidOperands(const Value *Vec, const Value *NewElt, const Value *Idx)
Definition: SandboxIR.h:910
static bool classof(const Value *From)
Definition: SandboxIR.h:907
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: SandboxIR.h:647
bool hasNoUnsignedWrap() const
Determine whether the no signed wrap flag is set.
Definition: SandboxIR.h:734
friend class UnreachableInst
Definition: SandboxIR.h:685
BBIterator getIterator() const
\Returns a BasicBlock::iterator for this Instruction.
Definition: SandboxIR.cpp:331
void removeFromParent()
Detach this from its parent BasicBlock without deleting it.
Definition: SandboxIR.cpp:359
bool hasAllowReassoc() const
Determine whether the allow-reassociation flag is set.
Definition: SandboxIR.h:754
virtual unsigned getNumOfIRInstrs() const =0
This is used by BasicBlock::iterator.
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
Definition: SandboxIR.h:779
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction,...
Definition: SandboxIR.cpp:558
void setHasNoSignedZeros(bool B)
Set or clear the no-signed-zeros flag on this instruction, which must be an operator which supports t...
Definition: SandboxIR.cpp:527
static bool classof(const sandboxir::Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:464
void moveAfter(Instruction *After)
Move this instruction after After.
Definition: SandboxIR.h:724
bool hasAllowContract() const
Determine whether the allow-contract flag is set.
Definition: SandboxIR.h:795
void setHasAllowContract(bool B)
Set or clear the allow-contract flag on this instruction, which must be an operator which supports th...
Definition: SandboxIR.cpp:543
void setIsExact(bool B=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
Definition: SandboxIR.cpp:497
bool hasApproxFunc() const
Determine whether the approximate-math-functions flag is set.
Definition: SandboxIR.h:803
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
Definition: SandboxIR.h:741
void setHasNoNaNs(bool B)
Set or clear the no-nans flag on this instruction, which must be an operator which supports this flag...
Definition: SandboxIR.cpp:511
void insertInto(BasicBlock *BB, const BBIterator &WhereIt)
Insert this detached instruction into BB at WhereIt.
Definition: SandboxIR.cpp:433
Opcode getOpcode() const
\Returns this Instruction's opcode.
Definition: SandboxIR.h:706
void setHasApproxFunc(bool B)
Set or clear the approximate-math-functions flag on this instruction, which must be an operator which...
Definition: SandboxIR.cpp:565
bool isFast() const
Determine whether all fast-math-flags are set.
Definition: SandboxIR.h:748
static const char * getOpcodeName(Opcode Opc)
Definition: SandboxIR.cpp:307
void setHasAllowReassoc(bool B)
Set or clear the reassociation flag on this instruction, which must be an operator which supports thi...
Definition: SandboxIR.cpp:504
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
Definition: SandboxIR.cpp:551
void eraseFromParent()
Detach this Value from its parent and delete it.
Definition: SandboxIR.cpp:367
Instruction * getNextNode() const
\Returns the next sandboxir::Instruction in the block, or nullptr if at the end of the block.
Definition: SandboxIR.cpp:336
void moveBefore(BasicBlock &BB, const BBIterator &WhereIt)
Move this instruction to WhereIt.
Definition: SandboxIR.cpp:391
llvm::Instruction * getTopmostLLVMInstruction() const
A SandboxIR Instruction may map to multiple LLVM IR Instruction.
Definition: SandboxIR.cpp:319
void setHasNoInfs(bool B)
Set or clear the no-infs flag on this instruction, which must be an operator which supports this flag...
Definition: SandboxIR.cpp:519
void insertAfter(Instruction *AfterI)
Insert this detached instruction after AfterI.
Definition: SandboxIR.cpp:429
virtual SmallVector< llvm::Instruction *, 1 > getLLVMInstrs() const =0
\Returns the LLVM IR Instructions that this SandboxIR maps to in program order.
void moveBefore(Instruction *Before)
Move this instruction before Before.
Definition: SandboxIR.h:720
void dumpOS(raw_ostream &OS) const override
Definition: SandboxIR.cpp:573
bool isExact() const
Determine whether the exact flag is set.
Definition: SandboxIR.h:762
Instruction * getPrevNode() const
\Returns the previous sandboxir::Instruction in the block, or nullptr if at the beginning of the bloc...
Definition: SandboxIR.cpp:351
Instruction(ClassID ID, Opcode Opc, llvm::Instruction *I, sandboxir::Context &SBCtx)
Definition: SandboxIR.h:657
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Definition: SandboxIR.h:813
void setHasNoSignedWrap(bool B=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
Definition: SandboxIR.cpp:483
void setHasAllowReciprocal(bool B)
Set or clear the allow-reciprocal flag on this instruction, which must be an operator which supports ...
Definition: SandboxIR.cpp:535
bool hasNoInfs() const
Determine whether the no-infs flag is set.
Definition: SandboxIR.h:773
bool hasNoNaNs() const
Determine whether the no-NaNs flag is set.
Definition: SandboxIR.h:767
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
Definition: SandboxIR.h:787
void setFast(bool B)
Set or clear all fast-math-flags on this instruction, which must be an operator which supports this f...
Definition: SandboxIR.cpp:490
void setHasNoUnsignedWrap(bool B=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
Definition: SandboxIR.cpp:475
void insertBefore(Instruction *BeforeI)
Insert this detached instruction before BeforeI.
Definition: SandboxIR.cpp:415
BasicBlock * getParent() const
\Returns the BasicBlock containing this Instruction, or null if it is detached.
Definition: SandboxIR.cpp:455
Instruction * getLandingPadInst() const
Definition: SandboxIR.cpp:948
BasicBlock * getSuccessor(unsigned SuccIdx) const
Definition: SandboxIR.cpp:953
void setNormalDest(BasicBlock *BB)
Definition: SandboxIR.cpp:940
unsigned getNumSuccessors() const
Definition: SandboxIR.h:1344
static InvokeInst * create(FunctionType *FTy, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &NameStr="")
Definition: SandboxIR.cpp:894
static bool classof(const Value *From)
Definition: SandboxIR.h:1327
void setUnwindDest(BasicBlock *BB)
Definition: SandboxIR.cpp:944
void setSuccessor(unsigned SuccIdx, BasicBlock *NewSucc)
Definition: SandboxIR.h:1337
BasicBlock * getNormalDest() const
Definition: SandboxIR.cpp:932
BasicBlock * getUnwindDest() const
Definition: SandboxIR.cpp:936
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:724
bool isUnordered() const
Definition: SandboxIR.h:1066
static LoadInst * create(Type *Ty, Value *Ptr, MaybeAlign Align, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:689
void setVolatile(bool V)
Specify whether this is a volatile load or not.
Definition: SandboxIR.cpp:682
Align getAlign() const
Definition: SandboxIR.h:1065
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: SandboxIR.h:1045
Value * getPointerOperand() const
Definition: SandboxIR.cpp:728
An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to an OpaqueInstr.
Definition: SandboxIR.h:2119
static bool classof(const sandboxir::Value *From)
Definition: SandboxIR.h:2127
Iterator for the Use edges of a User's operands.
Definition: SandboxIR.h:142
std::input_iterator_tag iterator_category
Definition: SandboxIR.h:155
OperandUseIterator operator++(int)
Definition: SandboxIR.h:160
OperandUseIterator operator+(unsigned Num) const
Definition: SandboxIR.cpp:89
bool operator!=(const OperandUseIterator &Other) const
Definition: SandboxIR.h:168
bool operator==(const OperandUseIterator &Other) const
Definition: SandboxIR.h:165
OperandUseIterator operator-(unsigned Num) const
Definition: SandboxIR.cpp:95
OperandUseIterator & operator++()
Definition: SandboxIR.cpp:66
iterator_range< const_block_iterator > blocks() const
Definition: SandboxIR.h:2072
const_block_iterator block_end() const
Definition: SandboxIR.h:2067
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:1100
const_op_range incoming_values() const
Definition: SandboxIR.h:2078
void setIncomingBlock(unsigned Idx, BasicBlock *BB)
Definition: SandboxIR.cpp:1123
bool hasConstantOrUndefValue() const
Definition: SandboxIR.h:2106
void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New)
Definition: SandboxIR.cpp:1173
void setIncomingValue(unsigned Idx, Value *V)
Definition: SandboxIR.cpp:1107
unsigned getNumIncomingValues() const
Definition: SandboxIR.h:2080
void addIncoming(Value *V, BasicBlock *BB)
Definition: SandboxIR.cpp:1134
bool isComplete() const
Definition: SandboxIR.h:2109
const_block_iterator block_begin() const
Definition: SandboxIR.h:2062
int getBasicBlockIndex(const BasicBlock *BB) const
Definition: SandboxIR.cpp:1159
mapped_iterator< llvm::PHINode::const_block_iterator, LLVMBBToBB > const_block_iterator
Definition: SandboxIR.h:2060
Value * removeIncomingValue(unsigned Idx)
Definition: SandboxIR.cpp:1141
void removeIncomingValueIf(function_ref< bool(unsigned)> Predicate)
Definition: SandboxIR.cpp:1180
Value * hasConstantValue() const
Definition: SandboxIR.cpp:1169
op_range incoming_values()
Definition: SandboxIR.h:2076
static unsigned getIncomingValueNumForOperand(unsigned Idx)
Definition: SandboxIR.h:2088
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Definition: SandboxIR.cpp:1163
BasicBlock * getIncomingBlock(unsigned Idx) const
Definition: SandboxIR.cpp:1114
static unsigned getOperandNumForIncomingValue(unsigned Idx)
Definition: SandboxIR.h:2085
static PHINode * create(Type *Ty, unsigned NumReservedValues, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1092
Value * getIncomingValue(unsigned Idx) const
Definition: SandboxIR.cpp:1104
static bool classof(const Value *From)
Definition: SandboxIR.h:1139
Value * getReturnValue() const
\Returns null if there is no return value.
Definition: SandboxIR.cpp:829
static ReturnInst * create(Value *RetVal, Instruction *InsertBefore, Context &Ctx)
Definition: SandboxIR.cpp:814
static Value * create(Value *Cond, Value *True, Value *False, Instruction *InsertBefore, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:589
void setTrueValue(Value *New)
Definition: SandboxIR.h:885
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:607
void setCondition(Value *New)
Definition: SandboxIR.h:884
void setFalseValue(Value *New)
Definition: SandboxIR.h:886
Instructions that contain a single LLVM Instruction can inherit from this.
Definition: SandboxIR.h:831
void dumpOS(raw_ostream &OS) const override
Definition: SandboxIR.h:856
void verify() const final
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:855
unsigned getUseOperandNo(const Use &Use) const final
\Returns the operand index of Use.
Definition: SandboxIR.h:850
unsigned getNumOfIRInstrs() const final
This is used by BasicBlock::iterator.
Definition: SandboxIR.h:853
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:771
void setVolatile(bool V)
Specify whether this is a volatile store or not.
Definition: SandboxIR.cpp:732
static StoreInst * create(Value *V, Value *Ptr, MaybeAlign Align, Instruction *InsertBefore, Context &Ctx)
Definition: SandboxIR.cpp:739
bool isVolatile() const
Return true if this is a store from a volatile memory location.
Definition: SandboxIR.h:1078
Value * getPointerOperand() const
Definition: SandboxIR.cpp:779
Value * getValueOperand() const
Definition: SandboxIR.cpp:775
BasicBlock * getSuccessor(unsigned Idx) const
Definition: SandboxIR.cpp:1303
CaseIt findCaseValue(const ConstantInt *C)
Definition: SandboxIR.h:1533
iterator_range< ConstCaseIt > cases() const
Definition: SandboxIR.h:1526
bool defaultDestUndefined() const
Definition: SandboxIR.h:1498
void setSuccessor(unsigned Idx, BasicBlock *NewSucc)
Definition: SandboxIR.cpp:1308
void setDefaultDest(BasicBlock *DefaultCase)
Definition: SandboxIR.cpp:1270
CaseIt case_begin()
Returns a read/write iterator that points to the first case in the SwitchInst.
Definition: SandboxIR.h:1516
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
Definition: SandboxIR.h:1523
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Definition: SandboxIR.cpp:1283
llvm::SwitchInst::CaseIteratorImpl< CaseHandle > CaseIt
Definition: SandboxIR.h:1511
SwitchInst(llvm::SwitchInst *SI, Context &Ctx)
Definition: SandboxIR.h:1485
ConstCaseIt case_begin() const
Definition: SandboxIR.h:1517
ConstCaseIt case_end() const
Definition: SandboxIR.h:1521
llvm::SwitchInst::CaseIteratorImpl< ConstCaseHandle > ConstCaseIt
Definition: SandboxIR.h:1512
ConstCaseIt case_default() const
Definition: SandboxIR.h:1530
static bool classof(const Value *From)
Definition: SandboxIR.h:1563
BasicBlock * getDefaultDest() const
Definition: SandboxIR.cpp:1265
static SwitchInst * create(Value *V, BasicBlock *Dest, unsigned NumCases, BasicBlock::iterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1239
Value * getCondition() const
Definition: SandboxIR.cpp:1253
unsigned getNumCases() const
Definition: SandboxIR.h:1502
unsigned getNumSuccessors() const
Definition: SandboxIR.h:1558
CaseIt case_end()
Returns a read/write iterator that points one past the last in the SwitchInst.
Definition: SandboxIR.h:1520
ConstCaseIt findCaseValue(const ConstantInt *C) const
Definition: SandboxIR.h:1538
ConstantInt * findCaseDest(BasicBlock *BB)
Definition: SandboxIR.cpp:1277
static constexpr const unsigned DefaultPseudoIndex
Definition: SandboxIR.h:1488
CaseIt removeCase(CaseIt It)
This method removes the specified case and its successor from the switch instruction.
Definition: SandboxIR.cpp:1290
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition: Tracker.h:342
void revert()
Stops tracking and reverts to saved state.
Definition: Tracker.cpp:229
void save()
Turns on IR tracking.
Definition: Tracker.cpp:227
void accept()
Stops tracking and accept changes.
Definition: Tracker.cpp:237
An abstract class, parent of unary instructions.
Definition: SandboxIR.h:1022
static bool classof(const Instruction *I)
Definition: SandboxIR.h:1029
UnaryInstruction(ClassID ID, Opcode Opc, llvm::Instruction *LLVMI, Context &Ctx)
Definition: SandboxIR.h:1024
static bool classof(const Value *V)
Definition: SandboxIR.h:1032
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.h:1604
static Value * create(Instruction::Opcode Op, Value *OpV, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1317
static Value * createWithCopiedFlags(Instruction::Opcode Op, Value *OpV, Value *CopyFrom, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name="")
Definition: SandboxIR.cpp:1346
static bool classof(const Value *From)
Definition: SandboxIR.cpp:800
static UnreachableInst * create(Instruction *InsertBefore, Context &Ctx)
Definition: SandboxIR.cpp:783
unsigned getNumOfIRInstrs() const final
This is used by BasicBlock::iterator.
Definition: SandboxIR.h:1121
unsigned getUseOperandNo(const Use &Use) const final
\Returns the operand index of Use.
Definition: SandboxIR.h:1118
unsigned getNumSuccessors() const
Definition: SandboxIR.h:1117
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:32
unsigned getOperandNo() const
Definition: SandboxIR.cpp:25
Value * get() const
Definition: SandboxIR.cpp:18
void set(Value *V)
Definition: SandboxIR.cpp:20
class User * getUser() const
Definition: Use.h:54
Iterator for the Use edges of a Value's users.
Definition: SandboxIR.h:178
const sandboxir::Use & getUse() const
Definition: SandboxIR.h:200
std::input_iterator_tag iterator_category
Definition: SandboxIR.h:189
bool operator!=(const UserUseIterator &Other) const
Definition: SandboxIR.h:197
UserUseIterator & operator++()
Definition: SandboxIR.cpp:73
value_type operator*() const
Definition: SandboxIR.h:192
bool operator==(const UserUseIterator &Other) const
Definition: SandboxIR.h:194
A sandboxir::User has operands.
Definition: SandboxIR.h:400
virtual op_iterator op_begin()
Definition: SandboxIR.h:444
void dumpOS(raw_ostream &OS) const override
Definition: SandboxIR.h:484
friend class OperandUseIterator
Definition: SandboxIR.h:413
Value * getOperand(unsigned OpIdx) const
Definition: SandboxIR.h:464
virtual unsigned getUseOperandNo(const Use &Use) const =0
\Returns the operand index of Use.
unsigned getUseOperandNoDefault(const Use &Use) const
The default implementation works only for single-LLVMIR-instruction Users and only if they match exac...
Definition: SandboxIR.h:417
virtual const_op_iterator op_end() const
Definition: SandboxIR.h:456
static bool classof(const Value *From)
For isa/dyn_cast.
Definition: SandboxIR.cpp:238
bool replaceUsesOfWith(Value *FromV, Value *ToV)
Replaces any operands that match FromV with ToV.
Definition: SandboxIR.cpp:260
void verifyUserOfLLVMUse(const llvm::Use &Use) const
Definition: SandboxIR.cpp:232
op_range operands()
Definition: SandboxIR.h:460
void verify() const override
Should crash if there is something wrong with the instruction.
Definition: SandboxIR.h:480
Use getOperandUseDefault(unsigned OpIdx, bool Verify) const
\Returns the Use edge that corresponds to OpIdx.
Definition: SandboxIR.cpp:220
OperandUseIterator op_iterator
Definition: SandboxIR.h:439
const_op_range operands() const
Definition: SandboxIR.h:461
virtual void setOperand(unsigned OperandIdx, Value *Operand)
Definition: SandboxIR.cpp:253
virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const =0
\Returns the Use for the OpIdx'th operand.
void swapOperandsInternal(unsigned OpIdxA, unsigned OpIdxB)
Definition: SandboxIR.h:424
virtual unsigned getNumOperands() const
Definition: SandboxIR.h:470
virtual const_op_iterator op_begin() const
Definition: SandboxIR.h:453
Use getOperandUse(unsigned OpIdx) const
\Returns the operand edge for OpIdx.
Definition: SandboxIR.h:467
virtual op_iterator op_end()
Definition: SandboxIR.h:448
User(ClassID ID, llvm::Value *V, Context &Ctx)
Definition: SandboxIR.h:402
void dumpCommonHeader(raw_ostream &OS) const final
Definition: SandboxIR.cpp:274
A SandboxIR Value has users. This is the base class.
Definition: SandboxIR.h:204
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition: SandboxIR.h:306
LLVM_DUMP_METHOD void dump() const
Definition: SandboxIR.cpp:206
use_iterator use_begin()
Definition: SandboxIR.cpp:115
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: SandboxIR.h:240
std::string getUid() const
Returns the unique id in the form 'SB<number>.' like 'SB1.'.
Definition: SandboxIR.cpp:169
const_use_iterator use_end() const
Definition: SandboxIR.h:290
user_iterator user_begin()
Definition: SandboxIR.cpp:125
void replaceAllUsesWith(Value *Other)
Definition: SandboxIR.cpp:156
ClassID getSubclassID() const
Definition: SandboxIR.h:280
StringRef getName() const
\Returns the LLVM IR name of the bottom-most LLVM value.
Definition: SandboxIR.h:359
virtual void verify() const =0
Should crash if there is something wrong with the instruction.
void dumpCommonFooter(raw_ostream &OS) const
Definition: SandboxIR.cpp:179
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition: SandboxIR.cpp:175
UserUseIterator use_iterator
Definition: SandboxIR.h:282
Context & Ctx
All values point to the context.
Definition: SandboxIR.h:268
ClassID SubclassID
For isa/dyn_cast.
Definition: SandboxIR.h:231
user_iterator user_end()
Definition: SandboxIR.h:310
void dumpCommonSuffix(raw_ostream &OS) const
Definition: SandboxIR.cpp:195
Type * getType() const
Definition: SandboxIR.h:350
friend class Use
Definition: SandboxIR.h:244
friend class LLVMOpUserItToSBTy
Definition: SandboxIR.h:271
virtual ~Value()=default
const_use_iterator use_begin() const
Definition: SandboxIR.h:286
Context & getContext() const
Definition: SandboxIR.h:352
const_user_iterator user_begin() const
Definition: SandboxIR.h:313
bool hasNUsesOrMore(unsigned Num) const
Return true if this value has N uses or more.
Definition: SandboxIR.h:332
Value & operator=(const Value &)=delete
iterator_range< const_user_iterator > users() const
Definition: SandboxIR.h:323
iterator_range< user_iterator > users()
Definition: SandboxIR.h:320
void replaceUsesWithIf(Value *OtherV, llvm::function_ref< bool(const Use &)> ShouldReplace)
Definition: SandboxIR.cpp:138
unsigned getNumUses() const
\Returns the number of user edges (not necessarily to unique users).
Definition: SandboxIR.cpp:136
bool hasNUses(unsigned Num) const
Return true if this Value has exactly N uses.
Definition: SandboxIR.h:341
unsigned UID
A unique ID used for forming the name (used for debugging).
Definition: SandboxIR.h:234
virtual void dumpOS(raw_ostream &OS) const =0
use_iterator use_end()
Definition: SandboxIR.h:289
iterator_range< use_iterator > uses()
Definition: SandboxIR.h:294
const_user_iterator user_end() const
Definition: SandboxIR.h:316
void dumpCommonPrefix(raw_ostream &OS) const
Definition: SandboxIR.cpp:188
void printAsOperandCommon(raw_ostream &OS) const
Definition: SandboxIR.cpp:199
static const char * getSubclassIDStr(ClassID ID)
Definition: SandboxIR.h:214
Value(const Value &)=delete
Disable copies.
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Value &V)
Definition: SandboxIR.h:371
iterator_range< const_use_iterator > uses() const
Definition: SandboxIR.h:297
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
@ System
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:57
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
mapped_iterator< ItTy, FuncTy > map_iterator(ItTy I, FuncTy F)
Definition: STLExtras.h:372
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ Other
Any other memory.
DWARFExpression::Operation Op
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1749
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1886
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
The const version of succ_op_iterator.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Helper for mapped_iterator.
Definition: SandboxIR.h:302
User * operator()(const Use &Use) const
Definition: SandboxIR.h:303