2003-09-30 18:37:50 +00:00
|
|
|
//==- llvm/Analysis/ConstantsScanner.h - Iterate over constants -*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-07 19:29:23 +00:00
|
|
|
//
|
|
|
|
// This class implements an iterator to walk through the constants referenced by
|
2007-07-05 17:07:56 +00:00
|
|
|
// a method. This is used by the Bitcode & Assembly writers to build constant
|
2001-09-07 19:29:23 +00:00
|
|
|
// pools.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_CONSTANTSSCANNER_H
|
|
|
|
#define LLVM_ANALYSIS_CONSTANTSSCANNER_H
|
|
|
|
|
2014-03-04 10:30:26 +00:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
class Constant;
|
2001-09-07 19:29:23 +00:00
|
|
|
|
2009-08-27 23:44:33 +00:00
|
|
|
class constant_iterator : public std::iterator<std::forward_iterator_tag,
|
|
|
|
const Constant, ptrdiff_t> {
|
2002-02-12 21:04:35 +00:00
|
|
|
const_inst_iterator InstI; // Method instruction iterator
|
2001-09-07 19:29:23 +00:00
|
|
|
unsigned OpIdx; // Operand index
|
|
|
|
|
|
|
|
typedef constant_iterator _Self;
|
|
|
|
|
|
|
|
inline bool isAtConstant() const {
|
|
|
|
assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
|
2005-04-22 03:27:20 +00:00
|
|
|
"isAtConstant called with invalid arguments!");
|
2001-12-03 22:26:30 +00:00
|
|
|
return isa<Constant>(InstI->getOperand(OpIdx));
|
2001-09-07 19:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2002-03-23 22:51:58 +00:00
|
|
|
inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
|
2001-09-07 19:29:23 +00:00
|
|
|
// Advance to first constant... if we are not already at constant or end
|
2002-03-23 22:51:58 +00:00
|
|
|
if (InstI != inst_end(F) && // InstI is valid?
|
2005-04-22 03:27:20 +00:00
|
|
|
(InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
|
2001-09-07 19:29:23 +00:00
|
|
|
operator++();
|
|
|
|
}
|
|
|
|
|
2002-03-23 22:51:58 +00:00
|
|
|
inline constant_iterator(const Function *F, bool) // end ctor
|
|
|
|
: InstI(inst_end(F)), OpIdx(0) {
|
2001-09-07 19:29:23 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
|
2005-04-22 03:27:20 +00:00
|
|
|
InstI == x.InstI; }
|
2001-09-07 19:29:23 +00:00
|
|
|
inline bool operator!=(const _Self& x) const { return !operator==(x); }
|
|
|
|
|
|
|
|
inline pointer operator*() const {
|
|
|
|
assert(isAtConstant() && "Dereferenced an iterator at the end!");
|
2001-12-03 22:26:30 +00:00
|
|
|
return cast<Constant>(InstI->getOperand(OpIdx));
|
2001-09-07 19:29:23 +00:00
|
|
|
}
|
|
|
|
inline pointer operator->() const { return operator*(); }
|
|
|
|
|
|
|
|
inline _Self& operator++() { // Preincrement implementation
|
|
|
|
++OpIdx;
|
|
|
|
do {
|
|
|
|
unsigned NumOperands = InstI->getNumOperands();
|
|
|
|
while (OpIdx < NumOperands && !isAtConstant()) {
|
2005-04-22 03:27:20 +00:00
|
|
|
++OpIdx;
|
2001-09-07 19:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OpIdx < NumOperands) return *this; // Found a constant!
|
|
|
|
++InstI;
|
|
|
|
OpIdx = 0;
|
|
|
|
} while (!InstI.atEnd());
|
|
|
|
|
|
|
|
return *this; // At the end of the method
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _Self operator++(int) { // Postincrement
|
2005-04-21 20:19:05 +00:00
|
|
|
_Self tmp = *this; ++*this; return tmp;
|
2001-09-07 19:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool atEnd() const { return InstI.atEnd(); }
|
|
|
|
};
|
|
|
|
|
2002-03-23 22:51:58 +00:00
|
|
|
inline constant_iterator constant_begin(const Function *F) {
|
|
|
|
return constant_iterator(F);
|
2001-09-07 19:29:23 +00:00
|
|
|
}
|
|
|
|
|
2002-03-23 22:51:58 +00:00
|
|
|
inline constant_iterator constant_end(const Function *F) {
|
|
|
|
return constant_iterator(F, true);
|
2001-09-07 19:29:23 +00:00
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-09-07 19:29:23 +00:00
|
|
|
#endif
|