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 "llvm/ADT/iterator"
27 class constant_iterator : public forward_iterator<const Constant, ptrdiff_t> {
28 const_inst_iterator InstI; // Method instruction iterator
29 unsigned OpIdx; // Operand index
31 typedef constant_iterator _Self;
33 inline bool isAtConstant() const {
34 assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
35 "isAtConstant called with invalid arguments!");
36 return isa<Constant>(InstI->getOperand(OpIdx));
40 inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
41 // Advance to first constant... if we are not already at constant or end
42 if (InstI != inst_end(F) && // InstI is valid?
43 (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
47 inline constant_iterator(const Function *F, bool) // end ctor
48 : InstI(inst_end(F)), OpIdx(0) {
51 inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
53 inline bool operator!=(const _Self& x) const { return !operator==(x); }
55 inline pointer operator*() const {
56 assert(isAtConstant() && "Dereferenced an iterator at the end!");
57 return cast<Constant>(InstI->getOperand(OpIdx));
59 inline pointer operator->() const { return operator*(); }
61 inline _Self& operator++() { // Preincrement implementation
64 unsigned NumOperands = InstI->getNumOperands();
65 while (OpIdx < NumOperands && !isAtConstant()) {
69 if (OpIdx < NumOperands) return *this; // Found a constant!
72 } while (!InstI.atEnd());
74 return *this; // At the end of the method
77 inline _Self operator++(int) { // Postincrement
78 _Self tmp = *this; ++*this; return tmp;
81 inline bool atEnd() const { return InstI.atEnd(); }
84 inline constant_iterator constant_begin(const Function *F) {
85 return constant_iterator(F);
88 inline constant_iterator constant_end(const Function *F) {
89 return constant_iterator(F, true);
92 } // End llvm namespace