2009-07-17 20:47:02 +00:00
|
|
|
//===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines various classes for working with Instructions and
|
|
|
|
// ConstantExprs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_IR_OPERATOR_H
|
|
|
|
#define LLVM_IR_OPERATOR_H
|
2009-07-17 20:47:02 +00:00
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2012-12-11 11:05:15 +00:00
|
|
|
#include "llvm/Support/GetElementPtrTypeIterator.h"
|
2009-07-17 20:47:02 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2009-08-11 17:03:18 +00:00
|
|
|
class GetElementPtrInst;
|
2009-09-07 23:54:19 +00:00
|
|
|
class BinaryOperator;
|
|
|
|
class ConstantExpr;
|
2009-08-11 17:03:18 +00:00
|
|
|
|
2009-07-17 20:47:02 +00:00
|
|
|
/// Operator - This is a utility class that provides an abstraction for the
|
|
|
|
/// common functionality between Instructions and ConstantExprs.
|
|
|
|
///
|
|
|
|
class Operator : public User {
|
|
|
|
private:
|
2013-02-18 23:11:17 +00:00
|
|
|
// The Operator class is intended to be used as a utility, and is never itself
|
|
|
|
// instantiated.
|
2012-09-18 02:05:45 +00:00
|
|
|
void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
|
|
|
|
void *operator new(size_t s) LLVM_DELETED_FUNCTION;
|
|
|
|
Operator() LLVM_DELETED_FUNCTION;
|
2012-10-24 00:30:41 +00:00
|
|
|
|
2012-10-24 19:17:42 +00:00
|
|
|
protected:
|
2012-10-24 00:30:41 +00:00
|
|
|
// NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
|
|
|
|
// an overridden method that's not deleted in the base class. Cannot leave
|
|
|
|
// this unimplemented because that leads to an ODR-violation.
|
2012-09-28 07:17:01 +00:00
|
|
|
~Operator();
|
2009-07-17 20:47:02 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// getOpcode - Return the opcode for this Instruction or ConstantExpr.
|
|
|
|
///
|
|
|
|
unsigned getOpcode() const {
|
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(this))
|
|
|
|
return I->getOpcode();
|
|
|
|
return cast<ConstantExpr>(this)->getOpcode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getOpcode - If V is an Instruction or ConstantExpr, return its
|
|
|
|
/// opcode. Otherwise return UserOp1.
|
|
|
|
///
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-10-14 20:28:33 +00:00
|
|
|
static inline bool classof(const Instruction *) { return true; }
|
|
|
|
static inline bool classof(const ConstantExpr *) { return true; }
|
2009-07-17 20:47:02 +00:00
|
|
|
static inline bool classof(const Value *V) {
|
|
|
|
return isa<Instruction>(V) || isa<ConstantExpr>(V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// OverflowingBinaryOperator - Utility class for integer arithmetic operators
|
2009-07-20 20:32:43 +00:00
|
|
|
/// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
|
|
|
|
/// despite that operator having the potential for overflow.
|
2009-07-17 20:47:02 +00:00
|
|
|
///
|
|
|
|
class OverflowingBinaryOperator : public Operator {
|
2009-09-07 23:54:19 +00:00
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
NoUnsignedWrap = (1 << 0),
|
|
|
|
NoSignedWrap = (1 << 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class BinaryOperator;
|
|
|
|
friend class ConstantExpr;
|
|
|
|
void setHasNoUnsignedWrap(bool B) {
|
|
|
|
SubclassOptionalData =
|
|
|
|
(SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
|
|
|
|
}
|
|
|
|
void setHasNoSignedWrap(bool B) {
|
|
|
|
SubclassOptionalData =
|
|
|
|
(SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
|
|
|
|
}
|
|
|
|
|
2009-07-17 20:47:02 +00:00
|
|
|
public:
|
2009-08-20 17:11:38 +00:00
|
|
|
/// hasNoUnsignedWrap - Test whether this operation is known to never
|
|
|
|
/// undergo unsigned overflow, aka the nuw property.
|
|
|
|
bool hasNoUnsignedWrap() const {
|
2009-09-07 23:54:19 +00:00
|
|
|
return SubclassOptionalData & NoUnsignedWrap;
|
2009-07-17 20:47:02 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 17:11:38 +00:00
|
|
|
/// hasNoSignedWrap - Test whether this operation is known to never
|
|
|
|
/// undergo signed overflow, aka the nsw property.
|
|
|
|
bool hasNoSignedWrap() const {
|
2010-09-25 20:27:36 +00:00
|
|
|
return (SubclassOptionalData & NoSignedWrap) != 0;
|
2009-07-17 20:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool classof(const Instruction *I) {
|
|
|
|
return I->getOpcode() == Instruction::Add ||
|
|
|
|
I->getOpcode() == Instruction::Sub ||
|
2011-02-07 16:40:21 +00:00
|
|
|
I->getOpcode() == Instruction::Mul ||
|
|
|
|
I->getOpcode() == Instruction::Shl;
|
2009-07-17 20:47:02 +00:00
|
|
|
}
|
|
|
|
static inline bool classof(const ConstantExpr *CE) {
|
|
|
|
return CE->getOpcode() == Instruction::Add ||
|
|
|
|
CE->getOpcode() == Instruction::Sub ||
|
2011-02-07 16:40:21 +00:00
|
|
|
CE->getOpcode() == Instruction::Mul ||
|
|
|
|
CE->getOpcode() == Instruction::Shl;
|
2009-07-24 18:12:25 +00:00
|
|
|
}
|
|
|
|
static inline bool classof(const Value *V) {
|
|
|
|
return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
|
|
|
|
(isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
|
2009-07-17 20:47:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-06 21:44:57 +00:00
|
|
|
/// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
|
|
|
|
/// "exact", indicating that no bits are destroyed.
|
|
|
|
class PossiblyExactOperator : public Operator {
|
2009-09-07 23:54:19 +00:00
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
IsExact = (1 << 0)
|
|
|
|
};
|
2012-11-15 22:34:00 +00:00
|
|
|
|
2012-04-14 15:43:22 +00:00
|
|
|
private:
|
2009-09-07 23:54:19 +00:00
|
|
|
friend class BinaryOperator;
|
|
|
|
friend class ConstantExpr;
|
|
|
|
void setIsExact(bool B) {
|
|
|
|
SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
|
|
|
|
}
|
2012-11-15 22:34:00 +00:00
|
|
|
|
2009-07-17 20:47:02 +00:00
|
|
|
public:
|
|
|
|
/// isExact - Test whether this division is known to be exact, with
|
|
|
|
/// zero remainder.
|
|
|
|
bool isExact() const {
|
2009-09-07 23:54:19 +00:00
|
|
|
return SubclassOptionalData & IsExact;
|
2009-07-17 20:47:02 +00:00
|
|
|
}
|
2012-11-15 22:34:00 +00:00
|
|
|
|
2011-02-07 16:40:21 +00:00
|
|
|
static bool isPossiblyExactOpcode(unsigned OpC) {
|
|
|
|
return OpC == Instruction::SDiv ||
|
|
|
|
OpC == Instruction::UDiv ||
|
|
|
|
OpC == Instruction::AShr ||
|
|
|
|
OpC == Instruction::LShr;
|
2011-02-06 21:44:57 +00:00
|
|
|
}
|
2009-07-17 20:47:02 +00:00
|
|
|
static inline bool classof(const ConstantExpr *CE) {
|
2011-02-07 16:40:21 +00:00
|
|
|
return isPossiblyExactOpcode(CE->getOpcode());
|
2009-07-17 20:47:02 +00:00
|
|
|
}
|
|
|
|
static inline bool classof(const Instruction *I) {
|
2011-02-07 16:40:21 +00:00
|
|
|
return isPossiblyExactOpcode(I->getOpcode());
|
2009-07-17 20:47:02 +00:00
|
|
|
}
|
|
|
|
static inline bool classof(const Value *V) {
|
|
|
|
return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
|
|
|
|
(isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
|
|
|
|
}
|
|
|
|
};
|
2012-04-16 16:28:59 +00:00
|
|
|
|
2012-11-27 00:40:00 +00:00
|
|
|
/// Convenience struct for specifying and reasoning about fast-math flags.
|
2012-12-09 21:12:04 +00:00
|
|
|
class FastMathFlags {
|
|
|
|
private:
|
|
|
|
friend class FPMathOperator;
|
|
|
|
unsigned Flags;
|
|
|
|
FastMathFlags(unsigned F) : Flags(F) { }
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
UnsafeAlgebra = (1 << 0),
|
|
|
|
NoNaNs = (1 << 1),
|
|
|
|
NoInfs = (1 << 2),
|
|
|
|
NoSignedZeros = (1 << 3),
|
|
|
|
AllowReciprocal = (1 << 4)
|
|
|
|
};
|
|
|
|
|
|
|
|
FastMathFlags() : Flags(0)
|
2012-11-27 00:40:00 +00:00
|
|
|
{ }
|
|
|
|
|
2012-11-28 21:11:25 +00:00
|
|
|
/// Whether any flag is set
|
2012-12-09 21:12:04 +00:00
|
|
|
bool any() { return Flags != 0; }
|
2012-11-28 21:11:25 +00:00
|
|
|
|
|
|
|
/// Set all the flags to false
|
2012-12-09 21:12:04 +00:00
|
|
|
void clear() { Flags = 0; }
|
|
|
|
|
|
|
|
/// Flag queries
|
|
|
|
bool noNaNs() { return 0 != (Flags & NoNaNs); }
|
|
|
|
bool noInfs() { return 0 != (Flags & NoInfs); }
|
|
|
|
bool noSignedZeros() { return 0 != (Flags & NoSignedZeros); }
|
|
|
|
bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); }
|
|
|
|
bool unsafeAlgebra() { return 0 != (Flags & UnsafeAlgebra); }
|
|
|
|
|
|
|
|
/// Flag setters
|
|
|
|
void setNoNaNs() { Flags |= NoNaNs; }
|
|
|
|
void setNoInfs() { Flags |= NoInfs; }
|
|
|
|
void setNoSignedZeros() { Flags |= NoSignedZeros; }
|
|
|
|
void setAllowReciprocal() { Flags |= AllowReciprocal; }
|
|
|
|
void setUnsafeAlgebra() {
|
|
|
|
Flags |= UnsafeAlgebra;
|
|
|
|
setNoNaNs();
|
|
|
|
setNoInfs();
|
|
|
|
setNoSignedZeros();
|
|
|
|
setAllowReciprocal();
|
2012-11-28 21:11:25 +00:00
|
|
|
}
|
2012-11-27 00:40:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-16 16:28:59 +00:00
|
|
|
/// FPMathOperator - Utility class for floating point operations which can have
|
|
|
|
/// information about relaxed accuracy requirements attached to them.
|
|
|
|
class FPMathOperator : public Operator {
|
2012-11-27 00:40:00 +00:00
|
|
|
private:
|
|
|
|
friend class Instruction;
|
|
|
|
|
|
|
|
void setHasUnsafeAlgebra(bool B) {
|
|
|
|
SubclassOptionalData =
|
2012-12-09 21:12:04 +00:00
|
|
|
(SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
|
|
|
|
(B * FastMathFlags::UnsafeAlgebra);
|
2012-11-27 00:40:00 +00:00
|
|
|
|
|
|
|
// Unsafe algebra implies all the others
|
|
|
|
if (B) {
|
|
|
|
setHasNoNaNs(true);
|
|
|
|
setHasNoInfs(true);
|
|
|
|
setHasNoSignedZeros(true);
|
|
|
|
setHasAllowReciprocal(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void setHasNoNaNs(bool B) {
|
|
|
|
SubclassOptionalData =
|
2012-12-09 21:12:04 +00:00
|
|
|
(SubclassOptionalData & ~FastMathFlags::NoNaNs) |
|
|
|
|
(B * FastMathFlags::NoNaNs);
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
void setHasNoInfs(bool B) {
|
|
|
|
SubclassOptionalData =
|
2012-12-09 21:12:04 +00:00
|
|
|
(SubclassOptionalData & ~FastMathFlags::NoInfs) |
|
|
|
|
(B * FastMathFlags::NoInfs);
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
void setHasNoSignedZeros(bool B) {
|
|
|
|
SubclassOptionalData =
|
2012-12-09 21:12:04 +00:00
|
|
|
(SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
|
|
|
|
(B * FastMathFlags::NoSignedZeros);
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
void setHasAllowReciprocal(bool B) {
|
|
|
|
SubclassOptionalData =
|
2012-12-09 21:12:04 +00:00
|
|
|
(SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
|
|
|
|
(B * FastMathFlags::AllowReciprocal);
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function for setting all the fast-math flags
|
|
|
|
void setFastMathFlags(FastMathFlags FMF) {
|
2012-12-09 21:12:04 +00:00
|
|
|
SubclassOptionalData |= FMF.Flags;
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Test whether this operation is permitted to be
|
|
|
|
/// algebraically transformed, aka the 'A' fast-math property.
|
|
|
|
bool hasUnsafeAlgebra() const {
|
2012-12-09 21:12:04 +00:00
|
|
|
return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Test whether this operation's arguments and results are to be
|
|
|
|
/// treated as non-NaN, aka the 'N' fast-math property.
|
|
|
|
bool hasNoNaNs() const {
|
2012-12-09 21:12:04 +00:00
|
|
|
return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Test whether this operation's arguments and results are to be
|
|
|
|
/// treated as NoN-Inf, aka the 'I' fast-math property.
|
|
|
|
bool hasNoInfs() const {
|
2012-12-09 21:12:04 +00:00
|
|
|
return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Test whether this operation can treat the sign of zero
|
|
|
|
/// as insignificant, aka the 'S' fast-math property.
|
|
|
|
bool hasNoSignedZeros() const {
|
2012-12-09 21:12:04 +00:00
|
|
|
return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Test whether this operation is permitted to use
|
|
|
|
/// reciprocal instead of division, aka the 'R' fast-math property.
|
|
|
|
bool hasAllowReciprocal() const {
|
2012-12-09 21:12:04 +00:00
|
|
|
return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience function for getting all the fast-math flags
|
|
|
|
FastMathFlags getFastMathFlags() const {
|
2012-12-09 21:12:04 +00:00
|
|
|
return FastMathFlags(SubclassOptionalData);
|
2012-11-27 00:40:00 +00:00
|
|
|
}
|
|
|
|
|
2012-04-16 16:28:59 +00:00
|
|
|
/// \brief Get the maximum error permitted by this operation in ULPs. An
|
|
|
|
/// accuracy of 0.0 means that the operation should be performed with the
|
2012-04-16 19:39:33 +00:00
|
|
|
/// default precision.
|
2012-04-16 16:28:59 +00:00
|
|
|
float getFPAccuracy() const;
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|
2009-07-17 20:47:02 +00:00
|
|
|
|
2012-11-15 22:34:00 +00:00
|
|
|
|
2011-02-07 16:40:21 +00:00
|
|
|
/// ConcreteOperator - A helper template for defining operators for individual
|
|
|
|
/// opcodes.
|
|
|
|
template<typename SuperClass, unsigned Opc>
|
|
|
|
class ConcreteOperator : public SuperClass {
|
2011-02-06 21:44:57 +00:00
|
|
|
public:
|
|
|
|
static inline bool classof(const Instruction *I) {
|
2011-02-07 16:40:21 +00:00
|
|
|
return I->getOpcode() == Opc;
|
|
|
|
}
|
|
|
|
static inline bool classof(const ConstantExpr *CE) {
|
|
|
|
return CE->getOpcode() == Opc;
|
2011-02-06 21:44:57 +00:00
|
|
|
}
|
|
|
|
static inline bool classof(const Value *V) {
|
|
|
|
return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
|
2011-02-07 16:40:21 +00:00
|
|
|
(isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
|
2011-02-06 21:44:57 +00:00
|
|
|
}
|
|
|
|
};
|
2011-02-07 16:40:21 +00:00
|
|
|
|
|
|
|
class AddOperator
|
2011-05-08 01:59:22 +00:00
|
|
|
: public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
|
|
|
|
};
|
2011-02-07 16:40:21 +00:00
|
|
|
class SubOperator
|
2011-05-08 01:59:22 +00:00
|
|
|
: public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
|
|
|
|
};
|
2011-02-07 16:40:21 +00:00
|
|
|
class MulOperator
|
2011-05-08 01:59:22 +00:00
|
|
|
: public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
|
|
|
|
};
|
2011-02-07 16:40:21 +00:00
|
|
|
class ShlOperator
|
2011-05-08 01:59:22 +00:00
|
|
|
: public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
|
|
|
|
};
|
2011-02-07 16:40:21 +00:00
|
|
|
|
2012-10-17 05:15:58 +00:00
|
|
|
|
2011-02-07 16:40:21 +00:00
|
|
|
class SDivOperator
|
2011-05-08 01:59:22 +00:00
|
|
|
: public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
|
|
|
|
};
|
2011-02-07 16:40:21 +00:00
|
|
|
class UDivOperator
|
2011-05-08 01:59:22 +00:00
|
|
|
: public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
|
|
|
|
};
|
2011-02-07 16:40:21 +00:00
|
|
|
class AShrOperator
|
2011-05-08 01:59:22 +00:00
|
|
|
: public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
|
|
|
|
};
|
2011-02-07 16:40:21 +00:00
|
|
|
class LShrOperator
|
2011-05-08 01:59:22 +00:00
|
|
|
: public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
|
|
|
|
};
|
2012-10-17 05:15:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-07 16:40:21 +00:00
|
|
|
class GEPOperator
|
|
|
|
: public ConcreteOperator<Operator, Instruction::GetElementPtr> {
|
2009-09-07 23:54:19 +00:00
|
|
|
enum {
|
|
|
|
IsInBounds = (1 << 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class GetElementPtrInst;
|
|
|
|
friend class ConstantExpr;
|
|
|
|
void setIsInBounds(bool B) {
|
|
|
|
SubclassOptionalData =
|
|
|
|
(SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
|
|
|
|
}
|
|
|
|
|
2009-07-17 21:33:58 +00:00
|
|
|
public:
|
2009-07-27 21:53:46 +00:00
|
|
|
/// isInBounds - Test whether this is an inbounds GEP, as defined
|
|
|
|
/// by LangRef.html.
|
|
|
|
bool isInBounds() const {
|
2009-09-07 23:54:19 +00:00
|
|
|
return SubclassOptionalData & IsInBounds;
|
2009-07-27 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 23:55:56 +00:00
|
|
|
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; // get index for modifying correct operand
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getPointerOperandType - Method to return the pointer operand as a
|
|
|
|
/// PointerType.
|
2011-12-05 06:29:09 +00:00
|
|
|
Type *getPointerOperandType() const {
|
|
|
|
return getPointerOperand()->getType();
|
2009-07-17 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 22:27:29 +00:00
|
|
|
/// getPointerAddressSpace - Method to return the address space of the
|
|
|
|
/// pointer operand.
|
|
|
|
unsigned getPointerAddressSpace() const {
|
|
|
|
return cast<PointerType>(getPointerOperandType())->getAddressSpace();
|
|
|
|
}
|
|
|
|
|
2009-07-17 23:55:56 +00:00
|
|
|
unsigned getNumIndices() const { // Note: always non-negative
|
|
|
|
return getNumOperands() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasIndices() const {
|
|
|
|
return getNumOperands() > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// hasAllZeroIndices - Return true if all of the indices of this GEP are
|
|
|
|
/// zeros. If so, the result pointer and the first operand have the same
|
|
|
|
/// value, just potentially different types.
|
|
|
|
bool hasAllZeroIndices() const {
|
|
|
|
for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
|
2011-02-07 16:40:21 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(I))
|
|
|
|
if (C->isZero())
|
2009-07-17 23:55:56 +00:00
|
|
|
continue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-21 05:52:45 +00:00
|
|
|
/// hasAllConstantIndices - Return true if all of the indices of this GEP are
|
|
|
|
/// constant integers. If so, the result pointer and the first operand have
|
|
|
|
/// a constant offset between them.
|
|
|
|
bool hasAllConstantIndices() const {
|
|
|
|
for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
|
|
|
|
if (!isa<ConstantInt>(I))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-11 11:05:15 +00:00
|
|
|
|
|
|
|
/// \brief Accumulate the constant address offset of this GEP if possible.
|
|
|
|
///
|
|
|
|
/// This routine accepts an APInt into which it will accumulate the constant
|
|
|
|
/// offset of this GEP if the GEP is in fact constant. If the GEP is not
|
|
|
|
/// all-constant, it returns false and the value of the offset APInt is
|
|
|
|
/// undefined (it is *not* preserved!). The APInt passed into this routine
|
2013-09-12 01:07:54 +00:00
|
|
|
/// must be at exactly as wide as the IntPtr type for the address space of the
|
|
|
|
/// base GEP pointer.
|
2012-12-11 11:05:15 +00:00
|
|
|
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
|
|
|
|
assert(Offset.getBitWidth() ==
|
|
|
|
DL.getPointerSizeInBits(getPointerAddressSpace()) &&
|
|
|
|
"The offset must have exactly as many bits as our pointer.");
|
|
|
|
|
|
|
|
for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
|
|
|
|
GTI != GTE; ++GTI) {
|
|
|
|
ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
|
|
|
|
if (!OpC)
|
|
|
|
return false;
|
|
|
|
if (OpC->isZero())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Handle a struct index, which adds its field offset to the pointer.
|
|
|
|
if (StructType *STy = dyn_cast<StructType>(*GTI)) {
|
|
|
|
unsigned ElementIdx = OpC->getZExtValue();
|
|
|
|
const StructLayout *SL = DL.getStructLayout(STy);
|
|
|
|
Offset += APInt(Offset.getBitWidth(),
|
|
|
|
SL->getElementOffset(ElementIdx));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For array or vector indices, scale the index by the size of the type.
|
|
|
|
APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
|
|
|
|
Offset += Index * APInt(Offset.getBitWidth(),
|
|
|
|
DL.getTypeAllocSize(GTI.getIndexedType()));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-17 21:33:58 +00:00
|
|
|
};
|
|
|
|
|
2009-07-17 20:47:02 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|