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, const Constant, ptrdiff_t> {
26 const_inst_iterator InstI; // Method instruction iterator
27 unsigned OpIdx; // Operand index
29 typedef constant_iterator _Self;
31 inline bool isAtConstant() const {
32 assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
33 "isAtConstant called with invalid arguments!");
34 return isa<Constant>(InstI->getOperand(OpIdx));
38 inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
39 // Advance to first constant... if we are not already at constant or end
40 if (InstI != inst_end(F) && // InstI is valid?
41 (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
45 inline constant_iterator(const Function *F, bool) // end ctor
46 : InstI(inst_end(F)), OpIdx(0) {
49 inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
51 inline bool operator!=(const _Self& x) const { return !operator==(x); }
53 inline pointer operator*() const {
54 assert(isAtConstant() && "Dereferenced an iterator at the end!");
55 return cast<Constant>(InstI->getOperand(OpIdx));
57 inline pointer operator->() const { return operator*(); }
59 inline _Self& operator++() { // Preincrement implementation
62 unsigned NumOperands = InstI->getNumOperands();
63 while (OpIdx < NumOperands && !isAtConstant()) {
67 if (OpIdx < NumOperands) return *this; // Found a constant!
70 } while (!InstI.atEnd());
72 return *this; // At the end of the method
75 inline _Self operator++(int) { // Postincrement
76 _Self tmp = *this; ++*this; return tmp;
79 inline bool atEnd() const { return InstI.atEnd(); }
82 inline constant_iterator constant_begin(const Function *F) {
83 return constant_iterator(F);
86 inline constant_iterator constant_end(const Function *F) {
87 return constant_iterator(F, true);
90 } // End llvm namespace