#ifndef LLVM_OPERATOR_H
#define LLVM_OPERATOR_H
#include "llvm/Constants.h"
#include "llvm/Instruction.h"
#include "llvm/Type.h"
namespace llvm {
class GetElementPtrInst;
class BinaryOperator;
class ConstantExpr;
class Operator : public User {
private:
void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
void *operator new(size_t s) LLVM_DELETED_FUNCTION;
Operator() LLVM_DELETED_FUNCTION;
~Operator();
public:
unsigned getOpcode() const {
if (const Instruction *I = dyn_cast<Instruction>(this))
return I->getOpcode();
return cast<ConstantExpr>(this)->getOpcode();
}
static unsigned getOpcode(const Value *V) {
if (const Instruction *I = dyn_cast<Instruction>(V))
return I->getOpcode();
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
return CE->getOpcode();
return Instruction::UserOp1;
}
static inline bool classof(const Operator *) { return true; }
static inline bool classof(const Instruction *) { return true; }
static inline bool classof(const ConstantExpr *) { return true; }
static inline bool classof(const Value *V) {
return isa<Instruction>(V) || isa<ConstantExpr>(V);
}
};
class OverflowingBinaryOperator : public Operator {
public:
enum {
NoUnsignedWrap = (1 << 0),
NoSignedWrap = (1 << 1)
};
private:
~OverflowingBinaryOperator();
friend class BinaryOperator;
friend class ConstantExpr;
void setHasNoUnsignedWrap(bool B) {
SubclassOptionalData =
(SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
}
void setHasNoSignedWrap(bool B) {
SubclassOptionalData =
(SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
}
public:
bool hasNoUnsignedWrap() const {
return SubclassOptionalData & NoUnsignedWrap;
}
bool hasNoSignedWrap() const {
return (SubclassOptionalData & NoSignedWrap) != 0;
}
static inline bool classof(const OverflowingBinaryOperator *) { return true; }
static inline bool classof(const Instruction *I) {
return I->getOpcode() == Instruction::Add ||
I->getOpcode() == Instruction::Sub ||
I->getOpcode() == Instruction::Mul ||
I->getOpcode() == Instruction::Shl;
}
static inline bool classof(const ConstantExpr *CE) {
return CE->getOpcode() == Instruction::Add ||
CE->getOpcode() == Instruction::Sub ||
CE->getOpcode() == Instruction::Mul ||
CE->getOpcode() == Instruction::Shl;
}
static inline bool classof(const Value *V) {
return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
(isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
}
};
class PossiblyExactOperator : public Operator {
public:
enum {
IsExact = (1 << 0)
};
private:
~PossiblyExactOperator();
friend class BinaryOperator;
friend class ConstantExpr;
void setIsExact(bool B) {
SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
}
public:
bool isExact() const {
return SubclassOptionalData & IsExact;
}
static bool isPossiblyExactOpcode(unsigned OpC) {
return OpC == Instruction::SDiv ||
OpC == Instruction::UDiv ||
OpC == Instruction::AShr ||
OpC == Instruction::LShr;
}
static inline bool classof(const ConstantExpr *CE) {
return isPossiblyExactOpcode(CE->getOpcode());
}
static inline bool classof(const Instruction *I) {
return isPossiblyExactOpcode(I->getOpcode());
}
static inline bool classof(const Value *V) {
return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
(isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
}
};
class FPMathOperator : public Operator {
private:
~FPMathOperator();
public:
float getFPAccuracy() const;
static inline bool classof(const FPMathOperator *) { return true; }
static inline bool classof(const Instruction *I) {
return I->getType()->isFPOrFPVectorTy();
}
static inline bool classof(const Value *V) {
return isa<Instruction>(V) && classof(cast<Instruction>(V));
}
};
template<typename SuperClass, unsigned Opc>
class ConcreteOperator : public SuperClass {
~ConcreteOperator() LLVM_DELETED_FUNCTION;
public:
static inline bool classof(const ConcreteOperator<SuperClass, Opc> *) {
return true;
}
static inline bool classof(const Instruction *I) {
return I->getOpcode() == Opc;
}
static inline bool classof(const ConstantExpr *CE) {
return CE->getOpcode() == Opc;
}
static inline bool classof(const Value *V) {
return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
(isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
}
};
class AddOperator
: public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
~AddOperator() LLVM_DELETED_FUNCTION;
};
class SubOperator
: public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
~SubOperator() LLVM_DELETED_FUNCTION;
};
class MulOperator
: public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
~MulOperator() LLVM_DELETED_FUNCTION;
};
class ShlOperator
: public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
~ShlOperator() LLVM_DELETED_FUNCTION;
};
class SDivOperator
: public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
~SDivOperator() LLVM_DELETED_FUNCTION;
};
class UDivOperator
: public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
~UDivOperator() LLVM_DELETED_FUNCTION;
};
class AShrOperator
: public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
~AShrOperator() LLVM_DELETED_FUNCTION;
};
class LShrOperator
: public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
~LShrOperator() LLVM_DELETED_FUNCTION;
};
class GEPOperator
: public ConcreteOperator<Operator, Instruction::GetElementPtr> {
~GEPOperator() LLVM_DELETED_FUNCTION;
enum {
IsInBounds = (1 << 0)
};
friend class GetElementPtrInst;
friend class ConstantExpr;
void setIsInBounds(bool B) {
SubclassOptionalData =
(SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
}
public:
bool isInBounds() const {
return SubclassOptionalData & IsInBounds;
}
inline op_iterator idx_begin() { return op_begin()+1; }
inline const_op_iterator idx_begin() const { return op_begin()+1; }
inline op_iterator idx_end() { return op_end(); }
inline const_op_iterator idx_end() const { return op_end(); }
Value *getPointerOperand() {
return getOperand(0);
}
const Value *getPointerOperand() const {
return getOperand(0);
}
static unsigned getPointerOperandIndex() {
return 0U; }
Type *getPointerOperandType() const {
return getPointerOperand()->getType();
}
unsigned getNumIndices() const { return getNumOperands() - 1;
}
bool hasIndices() const {
return getNumOperands() > 1;
}
bool hasAllZeroIndices() const {
for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
if (ConstantInt *C = dyn_cast<ConstantInt>(I))
if (C->isZero())
continue;
return false;
}
return true;
}
bool hasAllConstantIndices() const {
for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
if (!isa<ConstantInt>(I))
return false;
}
return true;
}
};
}
#endif