1 //==- llvm/Analysis/ConstantsScanner.h - Iterate over constants -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements an iterator to walk through the constants referenced by
11 // a method. This is used by the Bytecode & Assembly writers to build constant
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_CONSTANTSSCANNER_H
17 #define LLVM_ANALYSIS_CONSTANTSSCANNER_H
19 #include "llvm/Support/InstIterator.h"
20 #include "llvm/Instruction.h"
21 #include "Support/iterator"
24 class constant_iterator : public forward_iterator<const Constant, ptrdiff_t> {
25 const_inst_iterator InstI; // Method instruction iterator
26 unsigned OpIdx; // Operand index
28 typedef constant_iterator _Self;
30 inline bool isAtConstant() const {
31 assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
32 "isAtConstant called with invalid arguments!");
33 return isa<Constant>(InstI->getOperand(OpIdx));
37 inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
38 // Advance to first constant... if we are not already at constant or end
39 if (InstI != inst_end(F) && // InstI is valid?
40 (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
44 inline constant_iterator(const Function *F, bool) // end ctor
45 : InstI(inst_end(F)), OpIdx(0) {
48 inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
50 inline bool operator!=(const _Self& x) const { return !operator==(x); }
52 inline pointer operator*() const {
53 assert(isAtConstant() && "Dereferenced an iterator at the end!");
54 return cast<Constant>(InstI->getOperand(OpIdx));
56 inline pointer operator->() const { return operator*(); }
58 inline _Self& operator++() { // Preincrement implementation
61 unsigned NumOperands = InstI->getNumOperands();
62 while (OpIdx < NumOperands && !isAtConstant()) {
66 if (OpIdx < NumOperands) return *this; // Found a constant!
69 } while (!InstI.atEnd());
71 return *this; // At the end of the method
74 inline _Self operator++(int) { // Postincrement
75 _Self tmp = *this; ++*this; return tmp;
78 inline bool atEnd() const { return InstI.atEnd(); }
81 inline constant_iterator constant_begin(const Function *F) {
82 return constant_iterator(F);
85 inline constant_iterator constant_end(const Function *F) {
86 return constant_iterator(F, true);