1 //==-- llvm/Analysis/ConstantsScanner.h - Iterate over constants -*- C++ -*-==//
3 // This class implements an iterator to walk through the constants referenced by
4 // a method. This is used by the Bytecode & Assembly writers to build constant
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ANALYSIS_CONSTANTSSCANNER_H
10 #define LLVM_ANALYSIS_CONSTANTSSCANNER_H
12 #include "llvm/Method.h"
13 #include "llvm/Instruction.h"
17 class constant_iterator
18 : public std::forward_iterator<const ConstPoolVal, ptrdiff_t> {
19 Method::inst_const_iterator InstI; // Method instruction iterator
20 unsigned OpIdx; // Operand index
22 typedef constant_iterator _Self;
24 inline bool isAtConstant() const {
25 assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
26 "isAtConstant called with invalid arguments!");
27 return InstI->getOperand(OpIdx)->isConstant();
31 inline constant_iterator(const Method *M) : InstI(M->inst_begin()), OpIdx(0) {
32 // Advance to first constant... if we are not already at constant or end
33 if (InstI != M->inst_end() && // InstI is valid?
34 (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
38 inline constant_iterator(const Method *M, bool) // end ctor
39 : InstI(M->inst_end()), OpIdx(0) {
42 inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
44 inline bool operator!=(const _Self& x) const { return !operator==(x); }
46 inline pointer operator*() const {
47 assert(isAtConstant() && "Dereferenced an iterator at the end!");
48 return InstI->getOperand(OpIdx)->castConstantAsserting();
50 inline pointer operator->() const { return operator*(); }
52 inline _Self& operator++() { // Preincrement implementation
55 unsigned NumOperands = InstI->getNumOperands();
56 while (OpIdx < NumOperands && !isAtConstant()) {
60 if (OpIdx < NumOperands) return *this; // Found a constant!
63 } while (!InstI.atEnd());
65 return *this; // At the end of the method
68 inline _Self operator++(int) { // Postincrement
69 _Self tmp = *this; ++*this; return tmp;
72 inline bool atEnd() const { return InstI.atEnd(); }
75 inline constant_iterator constant_begin(const Method *M) {
76 return constant_iterator(M);
79 inline constant_iterator constant_end(const Method *M) {
80 return constant_iterator(M, true);