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/Constants.h"
19 #include "llvm/Instruction.h"
20 #include "llvm/Type.h"
24 class GetElementPtrInst;
28 /// Operator - This is a utility class that provides an abstraction for the
29 /// common functionality between Instructions and ConstantExprs.
31 class Operator : public User {
33 // Do not implement any of these. The Operator class is intended to be used
34 // as a utility, and is never itself instantiated.
35 void *operator new(size_t, unsigned);
36 void *operator new(size_t s);
41 /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
43 unsigned getOpcode() const {
44 if (const Instruction *I = dyn_cast<Instruction>(this))
45 return I->getOpcode();
46 return cast<ConstantExpr>(this)->getOpcode();
49 /// getOpcode - If V is an Instruction or ConstantExpr, return its
50 /// opcode. Otherwise return UserOp1.
52 static unsigned getOpcode(const Value *V) {
53 if (const Instruction *I = dyn_cast<Instruction>(V))
54 return I->getOpcode();
55 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
56 return CE->getOpcode();
57 return Instruction::UserOp1;
60 static inline bool classof(const Operator *) { return true; }
61 static inline bool classof(const Instruction *) { return true; }
62 static inline bool classof(const ConstantExpr *) { return true; }
63 static inline bool classof(const Value *V) {
64 return isa<Instruction>(V) || isa<ConstantExpr>(V);
68 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
69 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
70 /// despite that operator having the potential for overflow.
72 class OverflowingBinaryOperator : public Operator {
75 NoUnsignedWrap = (1 << 0),
76 NoSignedWrap = (1 << 1)
80 ~OverflowingBinaryOperator(); // do not implement
82 friend class BinaryOperator;
83 friend class ConstantExpr;
84 void setHasNoUnsignedWrap(bool B) {
85 SubclassOptionalData =
86 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
88 void setHasNoSignedWrap(bool B) {
89 SubclassOptionalData =
90 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
94 /// hasNoUnsignedWrap - Test whether this operation is known to never
95 /// undergo unsigned overflow, aka the nuw property.
96 bool hasNoUnsignedWrap() const {
97 return SubclassOptionalData & NoUnsignedWrap;
100 /// hasNoSignedWrap - Test whether this operation is known to never
101 /// undergo signed overflow, aka the nsw property.
102 bool hasNoSignedWrap() const {
103 return (SubclassOptionalData & NoSignedWrap) != 0;
106 static inline bool classof(const OverflowingBinaryOperator *) { return true; }
107 static inline bool classof(const Instruction *I) {
108 return I->getOpcode() == Instruction::Add ||
109 I->getOpcode() == Instruction::Sub ||
110 I->getOpcode() == Instruction::Mul ||
111 I->getOpcode() == Instruction::Shl;
113 static inline bool classof(const ConstantExpr *CE) {
114 return CE->getOpcode() == Instruction::Add ||
115 CE->getOpcode() == Instruction::Sub ||
116 CE->getOpcode() == Instruction::Mul ||
117 CE->getOpcode() == Instruction::Shl;
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 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
126 /// "exact", indicating that no bits are destroyed.
127 class PossiblyExactOperator : public Operator {
134 ~PossiblyExactOperator(); // do not implement
136 friend class BinaryOperator;
137 friend class ConstantExpr;
138 void setIsExact(bool B) {
139 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
143 /// isExact - Test whether this division is known to be exact, with
145 bool isExact() const {
146 return SubclassOptionalData & IsExact;
149 static bool isPossiblyExactOpcode(unsigned OpC) {
150 return OpC == Instruction::SDiv ||
151 OpC == Instruction::UDiv ||
152 OpC == Instruction::AShr ||
153 OpC == Instruction::LShr;
155 static inline bool classof(const ConstantExpr *CE) {
156 return isPossiblyExactOpcode(CE->getOpcode());
158 static inline bool classof(const Instruction *I) {
159 return isPossiblyExactOpcode(I->getOpcode());
161 static inline bool classof(const Value *V) {
162 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
163 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
167 /// FPMathOperator - Utility class for floating point operations which can have
168 /// information about relaxed accuracy requirements attached to them.
169 class FPMathOperator : public Operator {
171 ~FPMathOperator(); // do not implement
175 /// \brief Get the maximum error permitted by this operation in ULPs. An
176 /// accuracy of 0.0 means that the operation should be performed with the
177 /// default precision.
178 float getFPAccuracy() const;
180 static inline bool classof(const FPMathOperator *) { return true; }
181 static inline bool classof(const Instruction *I) {
182 return I->getType()->isFPOrFPVectorTy();
184 static inline bool classof(const Value *V) {
185 return isa<Instruction>(V) && classof(cast<Instruction>(V));
190 /// ConcreteOperator - A helper template for defining operators for individual
192 template<typename SuperClass, unsigned Opc>
193 class ConcreteOperator : public SuperClass {
194 ~ConcreteOperator(); // DO NOT IMPLEMENT
196 static inline bool classof(const ConcreteOperator<SuperClass, Opc> *) {
199 static inline bool classof(const Instruction *I) {
200 return I->getOpcode() == Opc;
202 static inline bool classof(const ConstantExpr *CE) {
203 return CE->getOpcode() == Opc;
205 static inline bool classof(const Value *V) {
206 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
207 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
212 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
213 ~AddOperator(); // DO NOT IMPLEMENT
216 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
217 ~SubOperator(); // DO NOT IMPLEMENT
220 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
221 ~MulOperator(); // DO NOT IMPLEMENT
224 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
225 ~ShlOperator(); // DO NOT IMPLEMENT
230 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
231 ~SDivOperator(); // DO NOT IMPLEMENT
234 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
235 ~UDivOperator(); // DO NOT IMPLEMENT
238 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
239 ~AShrOperator(); // DO NOT IMPLEMENT
242 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
243 ~LShrOperator(); // DO NOT IMPLEMENT
249 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
250 ~GEPOperator(); // DO NOT IMPLEMENT
253 IsInBounds = (1 << 0)
256 friend class GetElementPtrInst;
257 friend class ConstantExpr;
258 void setIsInBounds(bool B) {
259 SubclassOptionalData =
260 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
264 /// isInBounds - Test whether this is an inbounds GEP, as defined
266 bool isInBounds() const {
267 return SubclassOptionalData & IsInBounds;
270 inline op_iterator idx_begin() { return op_begin()+1; }
271 inline const_op_iterator idx_begin() const { return op_begin()+1; }
272 inline op_iterator idx_end() { return op_end(); }
273 inline const_op_iterator idx_end() const { return op_end(); }
275 Value *getPointerOperand() {
276 return getOperand(0);
278 const Value *getPointerOperand() const {
279 return getOperand(0);
281 static unsigned getPointerOperandIndex() {
282 return 0U; // get index for modifying correct operand
285 /// getPointerOperandType - Method to return the pointer operand as a
287 Type *getPointerOperandType() const {
288 return getPointerOperand()->getType();
291 unsigned getNumIndices() const { // Note: always non-negative
292 return getNumOperands() - 1;
295 bool hasIndices() const {
296 return getNumOperands() > 1;
299 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
300 /// zeros. If so, the result pointer and the first operand have the same
301 /// value, just potentially different types.
302 bool hasAllZeroIndices() const {
303 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
304 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
312 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
313 /// constant integers. If so, the result pointer and the first operand have
314 /// a constant offset between them.
315 bool hasAllConstantIndices() const {
316 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
317 if (!isa<ConstantInt>(I))
324 } // End llvm namespace