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
14 #include "llvm/Support/InstIterator.h"
15 #include "llvm/Instruction.h"
16 #include "Support/iterator"
19 class constant_iterator : public forward_iterator<const Constant, ptrdiff_t> {
20 const_inst_iterator InstI; // Method instruction iterator
21 unsigned OpIdx; // Operand index
23 typedef constant_iterator _Self;
25 inline bool isAtConstant() const {
26 assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
27 "isAtConstant called with invalid arguments!");
28 return isa<Constant>(InstI->getOperand(OpIdx));
32 inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
33 // Advance to first constant... if we are not already at constant or end
34 if (InstI != inst_end(F) && // InstI is valid?
35 (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
39 inline constant_iterator(const Function *F, bool) // end ctor
40 : InstI(inst_end(F)), OpIdx(0) {
43 inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
45 inline bool operator!=(const _Self& x) const { return !operator==(x); }
47 inline pointer operator*() const {
48 assert(isAtConstant() && "Dereferenced an iterator at the end!");
49 return cast<Constant>(InstI->getOperand(OpIdx));
51 inline pointer operator->() const { return operator*(); }
53 inline _Self& operator++() { // Preincrement implementation
56 unsigned NumOperands = InstI->getNumOperands();
57 while (OpIdx < NumOperands && !isAtConstant()) {
61 if (OpIdx < NumOperands) return *this; // Found a constant!
64 } while (!InstI.atEnd());
66 return *this; // At the end of the method
69 inline _Self operator++(int) { // Postincrement
70 _Self tmp = *this; ++*this; return tmp;
73 inline bool atEnd() const { return InstI.atEnd(); }
76 inline constant_iterator constant_begin(const Function *F) {
77 return constant_iterator(F);
80 inline constant_iterator constant_end(const Function *F) {
81 return constant_iterator(F, true);