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 {
70 ~OverflowingBinaryOperator(); // do not implement
72 /// hasNoUnsignedWrap - Test whether this operation is known to never
73 /// undergo unsigned overflow, aka the nuw property.
74 bool hasNoUnsignedWrap() const {
75 return SubclassOptionalData & (1 << 0);
77 void setHasNoUnsignedWrap(bool B) {
78 SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
81 /// hasNoSignedWrap - Test whether this operation is known to never
82 /// undergo signed overflow, aka the nsw property.
83 bool hasNoSignedWrap() const {
84 return SubclassOptionalData & (1 << 1);
86 void setHasNoSignedWrap(bool B) {
87 SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
90 static inline bool classof(const OverflowingBinaryOperator *) { return true; }
91 static inline bool classof(const Instruction *I) {
92 return I->getOpcode() == Instruction::Add ||
93 I->getOpcode() == Instruction::Sub ||
94 I->getOpcode() == Instruction::Mul;
96 static inline bool classof(const ConstantExpr *CE) {
97 return CE->getOpcode() == Instruction::Add ||
98 CE->getOpcode() == Instruction::Sub ||
99 CE->getOpcode() == Instruction::Mul;
101 static inline bool classof(const Value *V) {
102 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
103 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
107 /// AddOperator - Utility class for integer addition operators.
109 class AddOperator : public OverflowingBinaryOperator {
110 ~AddOperator(); // do not implement
112 static inline bool classof(const AddOperator *) { return true; }
113 static inline bool classof(const Instruction *I) {
114 return I->getOpcode() == Instruction::Add;
116 static inline bool classof(const ConstantExpr *CE) {
117 return CE->getOpcode() == Instruction::Add;
119 static inline bool classof(const Value *V) {
120 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
121 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
125 /// SubOperator - Utility class for integer subtraction operators.
127 class SubOperator : public OverflowingBinaryOperator {
128 ~SubOperator(); // do not implement
130 static inline bool classof(const SubOperator *) { return true; }
131 static inline bool classof(const Instruction *I) {
132 return I->getOpcode() == Instruction::Sub;
134 static inline bool classof(const ConstantExpr *CE) {
135 return CE->getOpcode() == Instruction::Sub;
137 static inline bool classof(const Value *V) {
138 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
139 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
143 /// MulOperator - Utility class for integer multiplication operators.
145 class MulOperator : public OverflowingBinaryOperator {
146 ~MulOperator(); // do not implement
148 static inline bool classof(const MulOperator *) { return true; }
149 static inline bool classof(const Instruction *I) {
150 return I->getOpcode() == Instruction::Mul;
152 static inline bool classof(const ConstantExpr *CE) {
153 return CE->getOpcode() == Instruction::Mul;
155 static inline bool classof(const Value *V) {
156 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
157 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
161 /// SDivOperator - An Operator with opcode Instruction::SDiv.
163 class SDivOperator : public Operator {
164 ~SDivOperator(); // do not implement
166 /// isExact - Test whether this division is known to be exact, with
168 bool isExact() const {
169 return SubclassOptionalData & (1 << 0);
171 void setIsExact(bool B) {
172 SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
175 // Methods for support type inquiry through isa, cast, and dyn_cast:
176 static inline bool classof(const SDivOperator *) { return true; }
177 static inline bool classof(const ConstantExpr *CE) {
178 return CE->getOpcode() == Instruction::SDiv;
180 static inline bool classof(const Instruction *I) {
181 return I->getOpcode() == Instruction::SDiv;
183 static inline bool classof(const Value *V) {
184 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
185 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
189 class GEPOperator : public Operator {
190 ~GEPOperator(); // do not implement
192 /// isInBounds - Test whether this is an inbounds GEP, as defined
194 bool isInBounds() const {
195 return SubclassOptionalData & (1 << 0);
197 void setIsInBounds(bool B) {
198 SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
201 inline op_iterator idx_begin() { return op_begin()+1; }
202 inline const_op_iterator idx_begin() const { return op_begin()+1; }
203 inline op_iterator idx_end() { return op_end(); }
204 inline const_op_iterator idx_end() const { return op_end(); }
206 Value *getPointerOperand() {
207 return getOperand(0);
209 const Value *getPointerOperand() const {
210 return getOperand(0);
212 static unsigned getPointerOperandIndex() {
213 return 0U; // get index for modifying correct operand
216 /// getPointerOperandType - Method to return the pointer operand as a
218 const PointerType *getPointerOperandType() const {
219 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
222 unsigned getNumIndices() const { // Note: always non-negative
223 return getNumOperands() - 1;
226 bool hasIndices() const {
227 return getNumOperands() > 1;
230 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
231 /// zeros. If so, the result pointer and the first operand have the same
232 /// value, just potentially different types.
233 bool hasAllZeroIndices() const {
234 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
235 if (Constant *C = dyn_cast<Constant>(I))
236 if (C->isNullValue())
243 // Methods for support type inquiry through isa, cast, and dyn_cast:
244 static inline bool classof(const GEPOperator *) { return true; }
245 static inline bool classof(const GetElementPtrInst *) { return true; }
246 static inline bool classof(const ConstantExpr *CE) {
247 return CE->getOpcode() == Instruction::GetElementPtr;
249 static inline bool classof(const Instruction *I) {
250 return I->getOpcode() == Instruction::GetElementPtr;
252 static inline bool classof(const Value *V) {
253 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
254 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
258 } // End llvm namespace