<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 12/17/12 10:34 AM, Alexandru Ionut
      Diaconescu wrote:<br>
    </div>
    <blockquote
cite="mid:CAPBXs324uC=N0pDG+mvpH4405CoB6uXNEU1+qUR7-tpFZO5Ryg@mail.gmail.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=ISO-8859-1">
      Hello,<br>
      <br>
      I am a beginner of LLVM. I am trying to move among the
      instructions of a BasicBlock and I cannot. In this particular
      example, I try to get the previous instruction of the end
      instruction. I am trying 2 methods: <br>
      <br>
      <br>
      <br>
      1. I have the following sequence of code:<br>
      <br>
      bool patternDC::runOnBasicBlock(BasicBlock &BB) {<br>
      ...<br>
      if (BB.getTerminator())<br>
        {<br>
                  Instruction* current = BB.getTerminator();<br>
                  errs() << "\n LAST:
      "<<*current<<"\n";<br clear="all">
      <br>
                  Instruction* prev = &BB.back();<br>
                  errs() << "\n <span id="result_box"
        class="short_text" lang="en"><span class="hps">PENULTIMATE</span></span>:
      "<<*prev<<"\n";<br>
      ...<br>
      <br>
      The terminal prints the SAME instruction. I don't know how back()
      works. (Definition at line <a moz-do-not-send="true" class="el"
        href="http://llvm.org/doxygen/BasicBlock_8h_source.html#l00199">199</a>
      of file <a moz-do-not-send="true" class="el"
        href="http://llvm.org/doxygen/BasicBlock_8h_source.html">BasicBlock.h</a>.)<br>
    </blockquote>
    <br>
    I believe BasicBlock::back() returns an iterator to the last
    instruction in the BasicBlock which should be its terminator
    instruction (basic blocks are required to have a TerminatorInst as
    their last instruction).<br>
    <br>
    <br>
    <blockquote
cite="mid:CAPBXs324uC=N0pDG+mvpH4405CoB6uXNEU1+qUR7-tpFZO5Ryg@mail.gmail.com"
      type="cite">
      <br>
      <br>
      <br>
      2. I also tried :<br>
      <br>
      bool patternDC::runOnBasicBlock(BasicBlock &BB) {<br>
      ...<br>
      BasicBlock::const_iterator I = BB.begin();<br>
      BasicBlock::const_iterator E = BB.end();<br>
      BasicBlock::const_iterator prev_iter,last_iter;<br>
      prev_iter = NULL; last_iter = NULL;<br>
      for(;I!=E;I++){<br>
          prev_iter = last_iter;<br>
          last_iter = I;<br>
      }<br>
      if(prev_iter){<br>
          errs() << "prev_iter: " <<
      *(dyn_cast<Instruction>(prev_iter)) << "\n";<br>
      }<br>
      if(last_iter){<br>
          errs() << "last_iter: " <<
      *(dyn_cast<Instruction>(last_iter)) << "\n";<br>
      }<br>
      // not related to the main question: uncomment the next line for
      an unusual behavior: lastlast is DIFFERENT from last. lastlast is
      kind of parts of the BasicBlock<br>
      // errs() << "lastlast: " <<
      *(dyn_cast<Instruction>(I)) << "\n";<br>
      ...<br>
      Instruction* prev = *(dyn_cast<Instruction*>(prev_iter));<br>
      errs() << "\n prev: "<<*prev<<"\n";<br>
      <br>
      The terminal prints well prev and last, but I have compilation
      errors when trying to assign to Instruction* prev<br>
      The Clang error is: <br>
      ".....<br>
      /home/alex/llvm/include/llvm/Support/Casting.h:51:28: error:
      ‘classof’ is not a member of ‘llvm::Instruction*’"<br>
      <br>
      <br>
      If someone knows a better way to use any element from the basic
      block or knows why these are not working, please let me know :)<br>
    </blockquote>
    <br>
    Sometimes you have to dereference an iterator to get the thing that
    it's pointing at.  Try using:<br>
    <br>
    Instruction* prev = (dyn_cast<Instruction>(*prev_iter));<br>
    <br>
    That might work.<br>
    <br>
    -- John T.<br>
    <br>
    <blockquote
cite="mid:CAPBXs324uC=N0pDG+mvpH4405CoB6uXNEU1+qUR7-tpFZO5Ryg@mail.gmail.com"
      type="cite"><br>
      Thank you,<br>
      Alex<br>
      <br>
      -- <br>
      <font
        style="background-color:rgb(255,255,255);color:rgb(153,153,153)">Best
        regards,</font><br
        style="background-color:rgb(255,255,255);color:rgb(153,153,153)">
      <font
        style="background-color:rgb(255,255,255);color:rgb(153,153,153)">Alexandru
        Ionut Diaconescu</font><br>
      <br>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a>
<a class="moz-txt-link-freetext" href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>