[llvm] [SandboxIR] Implement CmpInst, FCmpInst, and ICmpInst (PR #106301)
Thorsten Schütt via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 27 21:17:09 PDT 2024
================
@@ -2974,6 +2989,187 @@ class PHINode final : public SingleLLVMInstructionImpl<llvm::PHINode> {
// uint32_t ToIdx = 0)
};
+// Wraps a static function that takes a single Predicate parameter
+// LLVMValType should be the type of the wrapped class
+#define WRAP_STATIC_PREDICATE(FunctionName) \
+ static auto FunctionName(Predicate P) { return LLVMValType::FunctionName(P); }
+// Wraps a member function that takes no parameters
+// LLVMValType should be the type of the wrapped class
+#define WRAP_MEMBER(FunctionName) \
+ auto FunctionName() { return cast<LLVMValType>(Val)->FunctionName(); }
+// Wraps both--a common idiom in the CmpInst classes
+#define WRAP_BOTH(FunctionName) \
+ WRAP_STATIC_PREDICATE(FunctionName) \
+ WRAP_MEMBER(FunctionName)
+
+class CmpInst : public Instruction {
+protected:
+ using LLVMValType = llvm::CmpInst;
+ /// Use Context::createCmpInst(). Don't call the
+ /// constructor directly.
+ CmpInst(llvm::CmpInst *CI, Context &Ctx, ClassID Id = ClassID::Cmp,
+ Opcode Opc = Opcode::Cmp)
+ : Instruction(Id, Opc, CI, Ctx) {}
+ friend Context; // for CmpInst()
+ Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
+ return getOperandUseDefault(OpIdx, Verify);
+ }
+ SmallVector<llvm::Instruction *, 1> getLLVMInstrs() const final {
+ return {cast<llvm::Instruction>(Val)};
+ }
+ static Value *createCommon(Value *Cond, Value *True, Value *False,
+ const Twine &Name, IRBuilder<> &Builder,
+ Context &Ctx);
+
+public:
+ using Predicate = llvm::CmpInst::Predicate;
+ using PredicateField = llvm::CmpInst::PredicateField;
+ using OtherOps = llvm::Instruction::OtherOps;
+
+ unsigned getUseOperandNo(const Use &Use) const final {
+ return getUseOperandNoDefault(Use);
+ }
+ unsigned getNumOfIRInstrs() const final { return 1u; }
+ static CmpInst *create(OtherOps Op, Predicate Pred, Value *S1, Value *S2,
+ const Twine &Name = "",
+ InsertPosition InsertBefore = nullptr);
+ static CmpInst *CreateWithCopiedFlags(OtherOps Op, Predicate Pred, Value *S1,
+ Value *S2,
+ const Instruction *FlagsSource,
+ const Twine &Name = "",
+ InsertPosition InsertBefore = nullptr);
+ OtherOps getOpcode() const {
+ return static_cast<OtherOps>(Instruction::getOpcode());
+ }
+ void setPredicate(Predicate P);
+ void swapOperands();
+
+ WRAP_MEMBER(getPredicate);
+ WRAP_BOTH(isFPPredicate);
+ WRAP_BOTH(isIntPredicate);
+ WRAP_STATIC_PREDICATE(getPredicateName);
+ WRAP_BOTH(getInversePredicate);
+ WRAP_BOTH(getOrderedPredicate);
+ WRAP_BOTH(getUnorderedPredicate);
+ WRAP_BOTH(getSwappedPredicate);
+ WRAP_BOTH(isStrictPredicate);
+ WRAP_BOTH(isNonStrictPredicate);
+ WRAP_BOTH(getStrictPredicate);
+ WRAP_BOTH(getNonStrictPredicate);
+ WRAP_BOTH(getFlippedStrictnessPredicate);
+ WRAP_MEMBER(isCommutative);
+ WRAP_BOTH(isEquality);
+ WRAP_BOTH(isRelational);
+ WRAP_BOTH(isSigned);
+ WRAP_BOTH(getSignedPredicate);
+ WRAP_BOTH(getUnsignedPredicate);
+ WRAP_BOTH(getFlippedSignednessPredicate);
+ WRAP_BOTH(isTrueWhenEqual);
+ WRAP_BOTH(isFalseWhenEqual);
+ WRAP_BOTH(isUnsigned);
+ WRAP_STATIC_PREDICATE(isOrdered);
+ WRAP_STATIC_PREDICATE(isUnordered);
+
+ static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
+ return llvm::CmpInst::isImpliedTrueByMatchingCmp(Pred1, Pred2);
+ }
+ static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
+ return llvm::CmpInst::isImpliedFalseByMatchingCmp(Pred1, Pred2);
+ }
+
+ /// Methods for support type inquiry through isa, cast, and dyn_cast:
+ static bool classof(const Instruction *From) {
+ return isa<ICmpInst>(From) || isa<FCmpInst>(From);
+ }
+ static bool classof(const Value *From) {
+ return isa<Instruction>(From) && classof(cast<Instruction>(From));
----------------
tschuett wrote:
dyn_cast
https://github.com/llvm/llvm-project/pull/106301
More information about the llvm-commits
mailing list