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;
27 /// Operator - This is a utility class that provides an abstraction for the
28 /// common functionality between Instructions and ConstantExprs.
30 class Operator : public User {
32 // Do not implement any of these. The Operator class is intended to be used
33 // as a utility, and is never itself instantiated.
34 void *operator new(size_t, unsigned);
35 void *operator new(size_t s);
40 /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
42 unsigned getOpcode() const {
43 if (const Instruction *I = dyn_cast<Instruction>(this))
44 return I->getOpcode();
45 return cast<ConstantExpr>(this)->getOpcode();
48 /// getOpcode - If V is an Instruction or ConstantExpr, return its
49 /// opcode. Otherwise return UserOp1.
51 static unsigned getOpcode(const Value *V) {
52 if (const Instruction *I = dyn_cast<Instruction>(V))
53 return I->getOpcode();
54 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
55 return CE->getOpcode();
56 return Instruction::UserOp1;
59 static inline bool classof(const Operator *) { return true; }
60 static inline bool classof(const Instruction *) { return true; }
61 static inline bool classof(const ConstantExpr *) { return true; }
62 static inline bool classof(const Value *V) {
63 return isa<Instruction>(V) || isa<ConstantExpr>(V);
67 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
68 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
69 /// despite that operator having the potential for overflow.
71 class OverflowingBinaryOperator : public Operator {
74 NoUnsignedWrap = (1 << 0),
75 NoSignedWrap = (1 << 1)
79 ~OverflowingBinaryOperator(); // do not implement
81 friend class BinaryOperator;
82 friend class ConstantExpr;
83 void setHasNoUnsignedWrap(bool B) {
84 SubclassOptionalData =
85 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
87 void setHasNoSignedWrap(bool B) {
88 SubclassOptionalData =
89 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
93 /// hasNoUnsignedWrap - Test whether this operation is known to never
94 /// undergo unsigned overflow, aka the nuw property.
95 bool hasNoUnsignedWrap() const {
96 return SubclassOptionalData & NoUnsignedWrap;
99 /// hasNoSignedWrap - Test whether this operation is known to never
100 /// undergo signed overflow, aka the nsw property.
101 bool hasNoSignedWrap() const {
102 return (SubclassOptionalData & NoSignedWrap) != 0;
105 static inline bool classof(const OverflowingBinaryOperator *) { return true; }
106 static inline bool classof(const Instruction *I) {
107 return I->getOpcode() == Instruction::Add ||
108 I->getOpcode() == Instruction::Sub ||
109 I->getOpcode() == Instruction::Mul ||
110 I->getOpcode() == Instruction::Shl;
112 static inline bool classof(const ConstantExpr *CE) {
113 return CE->getOpcode() == Instruction::Add ||
114 CE->getOpcode() == Instruction::Sub ||
115 CE->getOpcode() == Instruction::Mul ||
116 CE->getOpcode() == Instruction::Shl;
118 static inline bool classof(const Value *V) {
119 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
120 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
124 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
125 /// "exact", indicating that no bits are destroyed.
126 class PossiblyExactOperator : public Operator {
133 ~PossiblyExactOperator(); // do not implement
135 friend class BinaryOperator;
136 friend class ConstantExpr;
137 void setIsExact(bool B) {
138 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
142 /// isExact - Test whether this division is known to be exact, with
144 bool isExact() const {
145 return SubclassOptionalData & IsExact;
148 static bool isPossiblyExactOpcode(unsigned OpC) {
149 return OpC == Instruction::SDiv ||
150 OpC == Instruction::UDiv ||
151 OpC == Instruction::AShr ||
152 OpC == Instruction::LShr;
154 static inline bool classof(const ConstantExpr *CE) {
155 return isPossiblyExactOpcode(CE->getOpcode());
157 static inline bool classof(const Instruction *I) {
158 return isPossiblyExactOpcode(I->getOpcode());
160 static inline bool classof(const Value *V) {
161 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
162 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
168 /// ConcreteOperator - A helper template for defining operators for individual
170 template<typename SuperClass, unsigned Opc>
171 class ConcreteOperator : public SuperClass {
172 ~ConcreteOperator(); // DO NOT IMPLEMENT
174 static inline bool classof(const ConcreteOperator<SuperClass, Opc> *) {
177 static inline bool classof(const Instruction *I) {
178 return I->getOpcode() == Opc;
180 static inline bool classof(const ConstantExpr *CE) {
181 return CE->getOpcode() == Opc;
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)));
190 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
191 ~AddOperator(); // DO NOT IMPLEMENT
194 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
195 ~SubOperator(); // DO NOT IMPLEMENT
198 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
199 ~MulOperator(); // DO NOT IMPLEMENT
202 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
203 ~ShlOperator(); // DO NOT IMPLEMENT
208 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
209 ~SDivOperator(); // DO NOT IMPLEMENT
212 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
213 ~UDivOperator(); // DO NOT IMPLEMENT
216 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
217 ~AShrOperator(); // DO NOT IMPLEMENT
220 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
221 ~LShrOperator(); // DO NOT IMPLEMENT
227 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
228 ~GEPOperator(); // DO NOT IMPLEMENT
231 IsInBounds = (1 << 0)
234 friend class GetElementPtrInst;
235 friend class ConstantExpr;
236 void setIsInBounds(bool B) {
237 SubclassOptionalData =
238 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
242 /// isInBounds - Test whether this is an inbounds GEP, as defined
244 bool isInBounds() const {
245 return SubclassOptionalData & IsInBounds;
248 inline op_iterator idx_begin() { return op_begin()+1; }
249 inline const_op_iterator idx_begin() const { return op_begin()+1; }
250 inline op_iterator idx_end() { return op_end(); }
251 inline const_op_iterator idx_end() const { return op_end(); }
253 Value *getPointerOperand() {
254 return getOperand(0);
256 const Value *getPointerOperand() const {
257 return getOperand(0);
259 static unsigned getPointerOperandIndex() {
260 return 0U; // get index for modifying correct operand
263 /// getPointerOperandType - Method to return the pointer operand as a
265 Type *getPointerOperandType() const {
266 return getPointerOperand()->getType();
269 unsigned getNumIndices() const { // Note: always non-negative
270 return getNumOperands() - 1;
273 bool hasIndices() const {
274 return getNumOperands() > 1;
277 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
278 /// zeros. If so, the result pointer and the first operand have the same
279 /// value, just potentially different types.
280 bool hasAllZeroIndices() const {
281 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
282 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
290 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
291 /// constant integers. If so, the result pointer and the first operand have
292 /// a constant offset between them.
293 bool hasAllConstantIndices() const {
294 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
295 if (!isa<ConstantInt>(I))
302 } // End llvm namespace