1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-01 16:33:37 +01:00
llvm-mirror/include/llvm/Analysis/ConstantsScanner.h

94 lines
2.9 KiB
C
Raw Normal View History

//==- llvm/Analysis/ConstantsScanner.h - Iterate over constants -*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
2001-09-07 21:29:23 +02:00
//
// This class implements an iterator to walk through the constants referenced by
// a method. This is used by the Bitcode & Assembly writers to build constant
2001-09-07 21:29:23 +02:00
// pools.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ANALYSIS_CONSTANTSSCANNER_H
#define LLVM_ANALYSIS_CONSTANTSSCANNER_H
#include "llvm/Support/InstIterator.h"
namespace llvm {
class Constant;
2001-09-07 21:29:23 +02:00
class constant_iterator : public std::iterator<std::forward_iterator_tag,
const Constant, ptrdiff_t> {
const_inst_iterator InstI; // Method instruction iterator
2001-09-07 21:29:23 +02:00
unsigned OpIdx; // Operand index
typedef constant_iterator _Self;
inline bool isAtConstant() const {
assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
2005-04-22 05:27:20 +02:00
"isAtConstant called with invalid arguments!");
return isa<Constant>(InstI->getOperand(OpIdx));
2001-09-07 21:29:23 +02:00
}
public:
inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
2001-09-07 21:29:23 +02:00
// Advance to first constant... if we are not already at constant or end
if (InstI != inst_end(F) && // InstI is valid?
2005-04-22 05:27:20 +02:00
(InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
2001-09-07 21:29:23 +02:00
operator++();
}
inline constant_iterator(const Function *F, bool) // end ctor
: InstI(inst_end(F)), OpIdx(0) {
2001-09-07 21:29:23 +02:00
}
inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
2005-04-22 05:27:20 +02:00
InstI == x.InstI; }
2001-09-07 21:29:23 +02:00
inline bool operator!=(const _Self& x) const { return !operator==(x); }
inline pointer operator*() const {
assert(isAtConstant() && "Dereferenced an iterator at the end!");
return cast<Constant>(InstI->getOperand(OpIdx));
2001-09-07 21:29:23 +02: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 05:27:20 +02:00
++OpIdx;
2001-09-07 21:29:23 +02: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
_Self tmp = *this; ++*this; return tmp;
2001-09-07 21:29:23 +02:00
}
inline bool atEnd() const { return InstI.atEnd(); }
};
inline constant_iterator constant_begin(const Function *F) {
return constant_iterator(F);
2001-09-07 21:29:23 +02:00
}
inline constant_iterator constant_end(const Function *F) {
return constant_iterator(F, true);
2001-09-07 21:29:23 +02:00
}
} // End llvm namespace
2001-09-07 21:29:23 +02:00
#endif