LLVM  14.0.0git
InstIterator.h
Go to the documentation of this file.
1 //===- InstIterator.h - Classes for inst iteration --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains definitions of two iterators for iterating over the
10 // instructions in a function. This is effectively a wrapper around a two level
11 // iterator that can probably be genericized later.
12 //
13 // Note that this iterator gets invalidated any time that basic blocks or
14 // instructions are moved around.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_IR_INSTITERATOR_H
19 #define LLVM_IR_INSTITERATOR_H
20 
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Function.h"
25 #include <iterator>
26 
27 namespace llvm {
28 
29 // This class implements inst_begin() & inst_end() for
30 // inst_iterator and const_inst_iterator's.
31 //
32 template <class BB_t, class BB_i_t, class BI_t, class II_t> class InstIterator {
33  using BBty = BB_t;
34  using BBIty = BB_i_t;
35  using BIty = BI_t;
36  using IIty = II_t;
37  BB_t *BBs; // BasicBlocksType
38  BB_i_t BB; // BasicBlocksType::iterator
39  BI_t BI; // BasicBlock::iterator
40 
41 public:
42  using iterator_category = std::bidirectional_iterator_tag;
43  using value_type = IIty;
44  using difference_type = signed;
45  using pointer = IIty *;
46  using reference = IIty &;
47 
48  // Default constructor
49  InstIterator() = default;
50 
51  // Copy constructor...
52  template<typename A, typename B, typename C, typename D>
54  : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
55 
56  template<typename A, typename B, typename C, typename D>
58  : BBs(II.BBs), BB(II.BB), BI(II.BI) {}
59 
60  template<class M> InstIterator(M &m)
61  : BBs(&m.getBasicBlockList()), BB(BBs->begin()) { // begin ctor
62  if (BB != BBs->end()) {
63  BI = BB->begin();
64  advanceToNextBB();
65  }
66  }
67 
68  template<class M> InstIterator(M &m, bool)
69  : BBs(&m.getBasicBlockList()), BB(BBs->end()) { // end ctor
70  }
71 
72  // Accessors to get at the underlying iterators...
73  inline BBIty &getBasicBlockIterator() { return BB; }
74  inline BIty &getInstructionIterator() { return BI; }
75 
76  inline reference operator*() const { return *BI; }
77  inline pointer operator->() const { return &operator*(); }
78 
79  inline bool operator==(const InstIterator &y) const {
80  return BB == y.BB && (BB == BBs->end() || BI == y.BI);
81  }
82  inline bool operator!=(const InstIterator& y) const {
83  return !operator==(y);
84  }
85 
87  ++BI;
88  advanceToNextBB();
89  return *this;
90  }
91  inline InstIterator operator++(int) {
92  InstIterator tmp = *this; ++*this; return tmp;
93  }
94 
96  while (BB == BBs->end() || BI == BB->begin()) {
97  --BB;
98  BI = BB->end();
99  }
100  --BI;
101  return *this;
102  }
103  inline InstIterator operator--(int) {
104  InstIterator tmp = *this; --*this; return tmp;
105  }
106 
107  inline bool atEnd() const { return BB == BBs->end(); }
108 
109 private:
110  inline void advanceToNextBB() {
111  // The only way that the II could be broken is if it is now pointing to
112  // the end() of the current BasicBlock and there are successor BBs.
113  while (BI == BB->end()) {
114  ++BB;
115  if (BB == BBs->end()) break;
116  BI = BB->begin();
117  }
118  }
119 };
120 
121 using inst_iterator =
122  InstIterator<SymbolTableList<BasicBlock>, Function::iterator,
124 using const_inst_iterator =
127  const Instruction>;
130 
132 inline inst_iterator inst_end(Function *F) { return inst_iterator(*F, true); }
134  return inst_range(inst_begin(F), inst_end(F));
135 }
137  return const_inst_iterator(*F);
138 }
140  return const_inst_iterator(*F, true);
141 }
144 }
146 inline inst_iterator inst_end(Function &F) { return inst_iterator(F, true); }
148  return inst_range(inst_begin(F), inst_end(F));
149 }
151  return const_inst_iterator(F);
152 }
154  return const_inst_iterator(F, true);
155 }
158 }
159 
160 } // end namespace llvm
161 
162 #endif // LLVM_IR_INSTITERATOR_H
llvm::InstIterator::operator--
InstIterator & operator--()
Definition: InstIterator.h:95
llvm::inst_iterator
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
Definition: InstIterator.h:123
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:90
llvm::Function
Definition: Function.h:61
llvm::InstIterator::operator++
InstIterator & operator++()
Definition: InstIterator.h:86
llvm::const_inst_range
iterator_range< const_inst_iterator > const_inst_range
Definition: InstIterator.h:129
llvm::const_inst_iterator
InstIterator< const SymbolTableList< BasicBlock >, Function::const_iterator, BasicBlock::const_iterator, const Instruction > const_inst_iterator
Definition: InstIterator.h:127
llvm::inst_range
iterator_range< inst_iterator > inst_range
Definition: InstIterator.h:128
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
tmp
alloca< 16 x float >, align 16 %tmp2=alloca< 16 x float >, align 16 store< 16 x float > %A,< 16 x float > *%tmp %s=bitcast< 16 x float > *%tmp to i8 *%s2=bitcast< 16 x float > *%tmp2 to i8 *call void @llvm.memcpy.i64(i8 *%s, i8 *%s2, i64 64, i32 16) %R=load< 16 x float > *%tmp2 ret< 16 x float > %R } declare void @llvm.memcpy.i64(i8 *nocapture, i8 *nocapture, i64, i32) nounwind which compiles to:_foo:subl $140, %esp movaps %xmm3, 112(%esp) movaps %xmm2, 96(%esp) movaps %xmm1, 80(%esp) movaps %xmm0, 64(%esp) movl 60(%esp), %eax movl %eax, 124(%esp) movl 56(%esp), %eax movl %eax, 120(%esp) movl 52(%esp), %eax< many many more 32-bit copies > movaps(%esp), %xmm0 movaps 16(%esp), %xmm1 movaps 32(%esp), %xmm2 movaps 48(%esp), %xmm3 addl $140, %esp ret On Nehalem, it may even be cheaper to just use movups when unaligned than to fall back to lower-granularity chunks. Implement processor-specific optimizations for parity with GCC on these processors. GCC does two optimizations:1. ix86_pad_returns inserts a noop before ret instructions if immediately preceded by a conditional branch or is the target of a jump. 2. ix86_avoid_jump_misspredicts inserts noops in cases where a 16-byte block of code contains more than 3 branches. The first one is done for all AMDs, Core2, and "Generic" The second one is done for:Atom, Pentium Pro, all AMDs, Pentium 4, Nocona, Core 2, and "Generic" Testcase:int x(int a) { return(a &0xf0)> >4 tmp
Definition: README.txt:1347
llvm::InstIterator::InstIterator
InstIterator()=default
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::InstIterator::getInstructionIterator
BIty & getInstructionIterator()
Definition: InstIterator.h:74
llvm::InstIterator::operator++
InstIterator operator++(int)
Definition: InstIterator.h:91
llvm::Instruction
Definition: Instruction.h:45
llvm::InstIterator::pointer
IIty * pointer
Definition: InstIterator.h:45
llvm::InstIterator::operator==
bool operator==(const InstIterator &y) const
Definition: InstIterator.h:79
llvm::InstIterator::InstIterator
InstIterator(InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:57
BasicBlock.h
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
llvm::InstIterator::operator*
reference operator*() const
Definition: InstIterator.h:76
llvm::InstIterator::value_type
IIty value_type
Definition: InstIterator.h:43
llvm::InstIterator::operator--
InstIterator operator--(int)
Definition: InstIterator.h:103
iterator_range.h
llvm::InstIterator::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition: InstIterator.h:42
llvm::InstIterator::operator->
pointer operator->() const
Definition: InstIterator.h:77
llvm::InstIterator::InstIterator
InstIterator(const InstIterator< A, B, C, D > &II)
Definition: InstIterator.h:53
llvm::InstIterator::reference
IIty & reference
Definition: InstIterator.h:46
Function.h
llvm::inst_end
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132
y
into llvm powi allowing the code generator to produce balanced multiplication trees the intrinsic needs to be extended to support and second the code generator needs to be enhanced to lower these to multiplication trees Interesting testcase for add shift mul int y
Definition: README.txt:61
llvm::InstIterator::InstIterator
InstIterator(M &m)
Definition: InstIterator.h:60
llvm::InstIterator::getBasicBlockIterator
BBIty & getBasicBlockIterator()
Definition: InstIterator.h:73
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::InstIterator::atEnd
bool atEnd() const
Definition: InstIterator.h:107
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::inst_begin
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
llvm::InstIterator::difference_type
signed difference_type
Definition: InstIterator.h:44
llvm::InstIterator::InstIterator
InstIterator(M &m, bool)
Definition: InstIterator.h:68
llvm::InstIterator::operator!=
bool operator!=(const InstIterator &y) const
Definition: InstIterator.h:82
llvm::InstIterator
Definition: InstIterator.h:32
SymbolTableListTraits.h
llvm::Function::const_iterator
BasicBlockListType::const_iterator const_iterator
Definition: Function.h:67
llvm::BasicBlock::const_iterator
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:91
llvm::Function::iterator
BasicBlockListType::iterator iterator
Definition: Function.h:66