<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 30, 2017, at 3:11 AM, Martin J. O'Riordan via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">At the moment I am in the process of getting our out-of-tree compiler up to date with respect to the v4.0 branch, and thankfully all is going well. However, one of the most common changes I have had to make has to do with iterations over machine instructions and basic blocks.<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">These are changes I have had to make incrementally each time we catch up with a release of LLVM, and basically the issue is that I have to replace something like:<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">void foo(MachineInstruction* x);<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">MachineBasicBlock::iterator index = ...;<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">foo(index);<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">to:<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">foo(<span style="background-color: yellow; background-position: initial initial; background-repeat: initial initial;" class="">&*</span>index);<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">This pattern of having to make this edit (adding the ‘</span><span style="font-size: 12pt; font-family: 'Courier New'; background-color: yellow;" class="">&*</span><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’) looks really scary to me, I am always nervous of expressions that look like this, and over the past three or so releases we have had to make similar changes (usually, but not always to various MI/MBB iterators).<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">So my guess is that we are using the wrong iterator, or an inappropriate iterator; but there are so many iterators now that I wonder if there is guidance on which iterator is most appropriate to a particular context or usage.<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">The main types of iteration we need are over the instructions in a basic-block, sometimes including the MI contents of bundles, and sometimes over the bundle (treating them as a single MI). And of course the ‘</span><span style="font-size: 12pt; font-family: 'Courier New';" class="">const</span><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ versus editable iterations.<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">Does anybody have pointers to existing developer documentation advising which iterators to use, when and how? Or have advice they can give here in this forum if such documentation does not exist (I haven’t found it if it does exist)?</span></div></div></div></blockquote><div><br class=""></div><div>I don't think there is documentation or broader consensus/patterns throughout the CodeGen library.</div><div>As for myself: I try to use MachineInstr* as much as possible: Using range based for esp. with make_range let's you mostly avoid MachineBasicBlock::iterator. I tend to only use the iterator when I want to indicate a position inside a basic block (for inserting instructions before/behind it).</div><div><br class=""></div><div>- Matthias</div></div></body></html>