Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.9 KiB

  1. //==- llvm/Analysis/ConstantsScanner.h - Iterate over constants -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  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
  12. // pools.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ANALYSIS_CONSTANTSSCANNER_H
  16. #define LLVM_ANALYSIS_CONSTANTSSCANNER_H
  17. #include "llvm/Support/InstIterator.h"
  18. namespace llvm {
  19. class Constant;
  20. class constant_iterator : public std::iterator<std::forward_iterator_tag,
  21. const Constant, ptrdiff_t> {
  22. const_inst_iterator InstI; // Method instruction iterator
  23. unsigned OpIdx; // Operand index
  24. typedef constant_iterator _Self;
  25. inline bool isAtConstant() const {
  26. assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() &&
  27. "isAtConstant called with invalid arguments!");
  28. return isa<Constant>(InstI->getOperand(OpIdx));
  29. }
  30. public:
  31. inline constant_iterator(const Function *F) : InstI(inst_begin(F)), OpIdx(0) {
  32. // Advance to first constant... if we are not already at constant or end
  33. if (InstI != inst_end(F) && // InstI is valid?
  34. (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant?
  35. operator++();
  36. }
  37. inline constant_iterator(const Function *F, bool) // end ctor
  38. : InstI(inst_end(F)), OpIdx(0) {
  39. }
  40. inline bool operator==(const _Self& x) const { return OpIdx == x.OpIdx &&
  41. InstI == x.InstI; }
  42. inline bool operator!=(const _Self& x) const { return !operator==(x); }
  43. inline pointer operator*() const {
  44. assert(isAtConstant() && "Dereferenced an iterator at the end!");
  45. return cast<Constant>(InstI->getOperand(OpIdx));
  46. }
  47. inline pointer operator->() const { return operator*(); }
  48. inline _Self& operator++() { // Preincrement implementation
  49. ++OpIdx;
  50. do {
  51. unsigned NumOperands = InstI->getNumOperands();
  52. while (OpIdx < NumOperands && !isAtConstant()) {
  53. ++OpIdx;
  54. }
  55. if (OpIdx < NumOperands) return *this; // Found a constant!
  56. ++InstI;
  57. OpIdx = 0;
  58. } while (!InstI.atEnd());
  59. return *this; // At the end of the method
  60. }
  61. inline _Self operator++(int) { // Postincrement
  62. _Self tmp = *this; ++*this; return tmp;
  63. }
  64. inline bool atEnd() const { return InstI.atEnd(); }
  65. };
  66. inline constant_iterator constant_begin(const Function *F) {
  67. return constant_iterator(F);
  68. }
  69. inline constant_iterator constant_end(const Function *F) {
  70. return constant_iterator(F, true);
  71. }
  72. } // End llvm namespace
  73. #endif