1 //===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines various classes for working with Instructions and
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OPERATOR_H
16 #define LLVM_OPERATOR_H
18 #include "llvm/Instruction.h"
19 #include "llvm/Constants.h"
23 class GetElementPtrInst;
25 /// Operator - This is a utility class that provides an abstraction for the
26 /// common functionality between Instructions and ConstantExprs.
28 class Operator : public User {
30 // Do not implement any of these. The Operator class is intended to be used
31 // as a utility, and is never itself instantiated.
32 void *operator new(size_t, unsigned);
33 void *operator new(size_t s);
38 /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
40 unsigned getOpcode() const {
41 if (const Instruction *I = dyn_cast<Instruction>(this))
42 return I->getOpcode();
43 return cast<ConstantExpr>(this)->getOpcode();
46 /// getOpcode - If V is an Instruction or ConstantExpr, return its
47 /// opcode. Otherwise return UserOp1.
49 static unsigned getOpcode(const Value *V) {
50 if (const Instruction *I = dyn_cast<Instruction>(V))
51 return I->getOpcode();
52 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
53 return CE->getOpcode();
54 return Instruction::UserOp1;
57 static inline bool classof(const Operator *) { return true; }
58 static inline bool classof(const Instruction *I) { return true; }
59 static inline bool classof(const ConstantExpr *I) { return true; }
60 static inline bool classof(const Value *V) {
61 return isa<Instruction>(V) || isa<ConstantExpr>(V);
65 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
66 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
67 /// despite that operator having the potential for overflow.
69 class OverflowingBinaryOperator : public Operator {
71 /// hasNoUnsignedOverflow - Test whether this operation is known to never
72 /// undergo unsigned overflow.
73 bool hasNoUnsignedOverflow() const {
74 return SubclassOptionalData & (1 << 0);
76 void setHasNoUnsignedOverflow(bool B) {
77 SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
80 /// hasNoSignedOverflow - Test whether this operation is known to never
81 /// undergo signed overflow.
82 bool hasNoSignedOverflow() const {
83 return SubclassOptionalData & (1 << 1);
85 void setHasNoSignedOverflow(bool B) {
86 SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
89 static inline bool classof(const OverflowingBinaryOperator *) { return true; }
90 static inline bool classof(const Instruction *I) {
91 return I->getOpcode() == Instruction::Add ||
92 I->getOpcode() == Instruction::Sub ||
93 I->getOpcode() == Instruction::Mul;
95 static inline bool classof(const ConstantExpr *CE) {
96 return CE->getOpcode() == Instruction::Add ||
97 CE->getOpcode() == Instruction::Sub ||
98 CE->getOpcode() == Instruction::Mul;
100 static inline bool classof(const Value *V) {
101 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
102 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
106 /// AddOperator - Utility class for integer addition operators.
108 class AddOperator : public OverflowingBinaryOperator {
110 static inline bool classof(const AddOperator *) { return true; }
111 static inline bool classof(const Instruction *I) {
112 return I->getOpcode() == Instruction::Add;
114 static inline bool classof(const ConstantExpr *CE) {
115 return CE->getOpcode() == Instruction::Add;
117 static inline bool classof(const Value *V) {
118 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
119 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
123 /// SubOperator - Utility class for integer subtraction operators.
125 class SubOperator : public OverflowingBinaryOperator {
127 static inline bool classof(const SubOperator *) { return true; }
128 static inline bool classof(const Instruction *I) {
129 return I->getOpcode() == Instruction::Sub;
131 static inline bool classof(const ConstantExpr *CE) {
132 return CE->getOpcode() == Instruction::Sub;
134 static inline bool classof(const Value *V) {
135 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
136 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
140 /// MulOperator - Utility class for integer multiplication operators.
142 class MulOperator : public OverflowingBinaryOperator {
144 static inline bool classof(const MulOperator *) { return true; }
145 static inline bool classof(const Instruction *I) {
146 return I->getOpcode() == Instruction::Mul;
148 static inline bool classof(const ConstantExpr *CE) {
149 return CE->getOpcode() == Instruction::Mul;
151 static inline bool classof(const Value *V) {
152 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
153 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
157 /// SDivOperator - An Operator with opcode Instruction::SDiv.
159 class SDivOperator : public Operator {
161 /// isExact - Test whether this division is known to be exact, with
163 bool isExact() const {
164 return SubclassOptionalData & (1 << 0);
166 void setIsExact(bool B) {
167 SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
170 // Methods for support type inquiry through isa, cast, and dyn_cast:
171 static inline bool classof(const SDivOperator *) { return true; }
172 static inline bool classof(const ConstantExpr *CE) {
173 return CE->getOpcode() == Instruction::SDiv;
175 static inline bool classof(const Instruction *I) {
176 return I->getOpcode() == Instruction::SDiv;
178 static inline bool classof(const Value *V) {
179 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
180 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
184 class GEPOperator : public Operator {
186 /// isInBounds - Test whether this is an inbounds GEP, as defined
188 bool isInBounds() const {
189 return SubclassOptionalData & (1 << 0);
191 void setIsInBounds(bool B) {
192 SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
195 inline op_iterator idx_begin() { return op_begin()+1; }
196 inline const_op_iterator idx_begin() const { return op_begin()+1; }
197 inline op_iterator idx_end() { return op_end(); }
198 inline const_op_iterator idx_end() const { return op_end(); }
200 Value *getPointerOperand() {
201 return getOperand(0);
203 const Value *getPointerOperand() const {
204 return getOperand(0);
206 static unsigned getPointerOperandIndex() {
207 return 0U; // get index for modifying correct operand
210 /// getPointerOperandType - Method to return the pointer operand as a
212 const PointerType *getPointerOperandType() const {
213 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
216 unsigned getNumIndices() const { // Note: always non-negative
217 return getNumOperands() - 1;
220 bool hasIndices() const {
221 return getNumOperands() > 1;
224 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
225 /// zeros. If so, the result pointer and the first operand have the same
226 /// value, just potentially different types.
227 bool hasAllZeroIndices() const {
228 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
229 if (Constant *C = dyn_cast<Constant>(I))
230 if (C->isNullValue())
237 // Methods for support type inquiry through isa, cast, and dyn_cast:
238 static inline bool classof(const GEPOperator *) { return true; }
239 static inline bool classof(const GetElementPtrInst *) { return true; }
240 static inline bool classof(const ConstantExpr *CE) {
241 return CE->getOpcode() == Instruction::GetElementPtr;
243 static inline bool classof(const Instruction *I) {
244 return I->getOpcode() == Instruction::GetElementPtr;
246 static inline bool classof(const Value *V) {
247 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
248 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
252 } // End llvm namespace