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/Support/InstIterator.h"
13 #include "llvm/Instruction.h"
14 #include "Support/iterator"
17 class constant_iterator : public forward_iterator<const Constant, ptrdiff_t> {
18 const_inst_iterator InstI; // Method instruction iterator
19 unsigned OpIdx; // Operand index
21 typedef constant_iterator _Self;
23 inline bool isAtConstant() const {
24 assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
25 "isAtConstant called with invalid arguments!");
26 return isa<Constant>(InstI->getOperand(OpIdx));
30 inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
31 // Advance to first constant... if we are not already at constant or end
32 if (InstI != inst_end(F) && // InstI is valid?
33 (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
37 inline constant_iterator(const Function *F, bool) // end ctor
38 : InstI(inst_end(F)), OpIdx(0) {
41 inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
43 inline bool operator!=(const _Self& x) const { return !operator==(x); }
45 inline pointer operator*() const {
46 assert(isAtConstant() && "Dereferenced an iterator at the end!");
47 return cast<Constant>(InstI->getOperand(OpIdx));
49 inline pointer operator->() const { return operator*(); }
51 inline _Self& operator++() { // Preincrement implementation
54 unsigned NumOperands = InstI->getNumOperands();
55 while (OpIdx < NumOperands && !isAtConstant()) {
59 if (OpIdx < NumOperands) return *this; // Found a constant!
62 } while (!InstI.atEnd());
64 return *this; // At the end of the method
67 inline _Self operator++(int) { // Postincrement
68 _Self tmp = *this; ++*this; return tmp;
71 inline bool atEnd() const { return InstI.atEnd(); }
74 inline constant_iterator constant_begin(const Function *F) {
75 return constant_iterator(F);
78 inline constant_iterator constant_end(const Function *F) {
79 return constant_iterator(F, true);