1 //==- llvm/Analysis/ConstantsScanner.h - Iterate over constants -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 Bitcode & Assembly writers to build constant
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ANALYSIS_CONSTANTSSCANNER_H
17 #define LLVM_ANALYSIS_CONSTANTSSCANNER_H
19 #include "llvm/Support/InstIterator.h"
25 class constant_iterator : public std::iterator<std::forward_iterator_tag,
26 const Constant, ptrdiff_t> {
27 const_inst_iterator InstI; // Method instruction iterator
28 unsigned OpIdx; // Operand index
30 typedef constant_iterator _Self;
32 inline bool isAtConstant() const {
33 assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
34 "isAtConstant called with invalid arguments!");
35 return isa<Constant>(InstI->getOperand(OpIdx));
39 inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
40 // Advance to first constant... if we are not already at constant or end
41 if (InstI != inst_end(F) && // InstI is valid?
42 (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
46 inline constant_iterator(const Function *F, bool) // end ctor
47 : InstI(inst_end(F)), OpIdx(0) {
50 inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
52 inline bool operator!=(const _Self& x) const { return !operator==(x); }
54 inline pointer operator*() const {
55 assert(isAtConstant() && "Dereferenced an iterator at the end!");
56 return cast<Constant>(InstI->getOperand(OpIdx));
58 inline pointer operator->() const { return operator*(); }
60 inline _Self& operator++() { // Preincrement implementation
63 unsigned NumOperands = InstI->getNumOperands();
64 while (OpIdx < NumOperands && !isAtConstant()) {
68 if (OpIdx < NumOperands) return *this; // Found a constant!
71 } while (!InstI.atEnd());
73 return *this; // At the end of the method
76 inline _Self operator++(int) { // Postincrement
77 _Self tmp = *this; ++*this; return tmp;
80 inline bool atEnd() const { return InstI.atEnd(); }
83 inline constant_iterator constant_begin(const Function *F) {
84 return constant_iterator(F);
87 inline constant_iterator constant_end(const Function *F) {
88 return constant_iterator(F, true);
91 } // End llvm namespace