<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 6/21/16 3:10 AM, Pierre Gagelin
      wrote:<br>
    </div>
    <blockquote
cite="mid:CALAmmgTNZTy8wbCukrhYYCU41-eYLwK0jk3R2bEv9jQJ5LgUog@mail.gmail.com"
      type="cite">
      <meta http-equiv="Context-Type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div>
          <div>
            <div>Hi,<br>
              <br>
            </div>
            Thanks both of you for the help. I just missed that Create
            function had many optional arguments... sorry for that.
            However my problem wasn't coming from here (IRBuilder
            CreateCall function still return a pointer to CallInst so I
            just added 2 times the call?). I didn't wanted to detail the
            all issue previously because I knew I had a problem with my
            syntax. So here's my problem:<br>
            <br>
          </div>
          I am doing this replacement operation in a FunctionPass (this
          is no restriction, I could do it on a ModulePass if that's a
          solution). On each Function given I do an iteration over the
          instructions and replace (if it's a call to the right
          function) the call. It's like this:<br>
          <br>
          bool FDPFunction::runOnFunction(Function &F) {<br>
        </div>
          bool res = false;<br>
        <div>  TLI =
          &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();<br>
          <br>
            for (inst_iterator i = inst_begin(F), e = inst_end(F); i !=
          e; ++i) {<br>
              Instruction *I = &*i;<br>
              <br>
              if (!I) {<br>
                errs() << "error: null pointer instruction\n";<br>
                break;<br>
              } else if (isMyFunctionCall(I, TLI)) {<br>
                errs() << "found a call to the function to
          replace\n";<br>
        </div>
        <div>      res = true;<br>
        </div>
        <div>      replacement_function(I);<br>
              } else {<br>
                errs() << "default: trash instruction\n";<br>
              }<br>
            }<br>
          <br>
            return res;<br>
          }<br>
          <br>
        </div>
        <div>The problem is that after the first replacement, the
          iterator becomes a null pointer. </div>
      </div>
    </blockquote>
    <br>
    You might be invalidating the iterator after inserting the first
    CallInst.  There are multiple ways to solve this problem:<br>
    <br>
    <ol>
      <li>Make your pass derive from the InstVistor class and implement
        a visitCallInst() method.  I believe the iterators in the
        InstVistor class do not get invalidated when new instructions
        are added.</li>
      <li>Iterate over all the instructions first and record the ones to
        replace in a container (e.g., a std::vector<>).  A second
        loop iterates over the container and replaces all the CallInsts
        stored therein.</li>
      <li>Look up the iterator invalidation rules for the instruction
        iterator and use the iterator in a way that does not invalidate
        the iterator.</li>
    </ol>
    <br>
    Regards,<br>
    <br>
    John Criswell<br>
    <br>
    <br>
    <blockquote
cite="mid:CALAmmgTNZTy8wbCukrhYYCU41-eYLwK0jk3R2bEv9jQJ5LgUog@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div>With the break instruction the program is correctly
          instrumented for the first call (about that: what is the use
          of replaceAllUsesWith function? I thought it would replace all
          uses of the function... but just one is done). I can then link
          it and execute with lli (I still get a: "*** Error in `lli':
          corrupted double-linked list: 0xaddress ***" but after the
          whole execution is done).<br>
          <br>
        </div>
        <div>But if I remove the break and the if condition associated,
          well I get a null pointer exception : Assertion `Val
          && "isa<> used on a null pointer"' failed<br>
          <br>
        </div>
        <div>I tried to clone the current instruction to not modify the
          iterator but the ReplaceInstWithInst will cause troubles
          (probably because a copied instruction has no parents):<br>
          <br>
          #0 0x0000000002a95b5c
          llvm::sys::PrintStackTrace(llvm::raw_ostream&)
          /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:400:0<br>
          #1 0x0000000002a95edf PrintStaI tried to clone the current
          instruction to not modify the iterator but the
          ReplaceInstWithInst will cause
          troublesckTraceSignalHandler(void*)
          /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:468:0<br>
          #2 0x0000000002a94145 llvm::sys::RunSignalHandlers()
          /home/pierre/Desktop/llvm/lib/Support/Signals.cpp:44:0<br>
          #3 0x0000000002a953a8 SignalHandler(int)
          /home/pierre/Desktop/llvm/lib/Support/Unix/Signals.inc:254:0<br>
          #4 0x00007fe358d3dd10 __restore_rt
          (/lib/x86_64-linux-gnu/libpthread.so.0+0x10d10)<br>
          #5 0x00000000011fe489 llvm::iplist<llvm::Instruction,
          llvm::SymbolTableListTraits<llvm::Instruction>
          >::insert(llvm::ilist_iterator<llvm::Instruction>,
          llvm::Instruction*)
          /home/pierre/Desktop/llvm/include/llvm/ADT/ilist.h:415:0<br>
          #6 0x0000000002abc20d llvm::ReI tried to clone the current
          instruction to not modify the iterator but the
          ReplaceInstWithInst will cause
troublesplaceInstWithInst(llvm::SymbolTableList<llvm::Instruction>&,
          llvm::ilist_iterator<llvm::Instruction>&,
          llvm::Instruction*)
          /home/pierre/Desktop/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp:198:0<br>
          #7 0x0000000002abc2a5
          llvm::ReplaceInstWithInst(llvm::Instruction*,
          llvm::Instruction*)
          /home/pierre/Desktop/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp:210:0<br>
          <br>
        </div>
        <div>Any idea on what could be the cause of the null pointer and
          how to avoid it?<br>
          <br>
        </div>
        <div>Thanks a lot for your time!<br>
        </div>
        <div>Pierre<br>
        </div>
        <div><br>
        </div>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On 20 June 2016 at 15:45, John Criswell
          <span dir="ltr"><<a moz-do-not-send="true"
              href="mailto:jtcriswel@gmail.com" target="_blank">jtcriswel@gmail.com</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote">
            <div><span class="">
                <div>On 6/20/16 6:17 AM, Pierre Gagelin via llvm-dev
                  wrote:<br>
                </div>
                <blockquote type="cite">
                  <div dir="ltr">
                    <div>
                      <div>
                        <div>Hi everyone,<br>
                          <br>
                        </div>
                        I am trying to replace the call of a certain
                        function with a call to another function. It
                        would for example replace the following:<br>
                        <br>
                        %call = tail call noalias i8* @func(i64 10)<br>
                      </div>
                      <div>by<br>
                        %call = tail call noalias i8* @other_func(i64
                        10)<br>
                      </div>
                      <div><br>
                      </div>
                      <div>I managed to declare other_func correctly but
                        I am having troubles to understand how I should
                        proceed to do the replacement.<br>
                      </div>
                      <div><br>
                        I tried to use ReplaceInstWithInst function as
                        follows:<br>
                        <br>
                        CallInst *call_to_other_func_inst =
                        IRBuilder.CreateCall(ptr_to_other_func, args);<br>
                        ReplaceInstWithInst(call_to_func_inst, newI);<br>
                        <br>
                      </div>
                      LLVM builds correctly but the instrumentation
                      crashes at optimization time. I know this isn't
                      the correct way to do it because IRBuilder
                      generates IR and I just want to have an instance
                      of a CallInst. But I don't see how it is supposed
                      to be done?<br>
                    </div>
                  </div>
                </blockquote>
                <br>
              </span> Do you have assertions enabled?  If so, is the
              crash an assertion failure and, if so, which assertion is
              failing?  It's nearly impossible to tell what the problem
              is just by knowing that it is crashing.<span class=""><br>
                <br>
                <blockquote type="cite">
                  <div dir="ltr">
                    <div><br>
                      There are methods to create CallInst in the
                      Instruction.h file but those needs to give an
                      inserting point. Shoud I insert the call to
                      other_func before the one to func and just remove
                      the call instruction to func?<br>
                    </div>
                  </div>
                </blockquote>
                <br>
              </span> Yes.  You need to place the new call instruction
              within the same basic block as the old call instruction. 
              Placing it right before the old call instruction is a good
              place.<br>
              <br>
              The code that Ashutosh provided is what I'd use to do what
              you're doing.<br>
              <br>
              Regards,<br>
              <br>
              John Criswell<br>
              <br>
              <blockquote type="cite"><span class="">
                  <div dir="ltr">
                    <div><br>
                    </div>
                    <div>Thanks for your help,<br>
                    </div>
                    <div>Pierre<br>
                    </div>
                  </div>
                  <br>
                  <fieldset></fieldset>
                  <br>
                </span>
                <pre>_______________________________________________
LLVM Developers mailing list
<a moz-do-not-send="true" href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>
<a moz-do-not-send="true" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><span class="HOEnZb">
</span></pre>
                <span class="HOEnZb"> </span></blockquote>
              <span class="HOEnZb"> <br>
                <p><br>
                </p>
                <pre cols="72">-- 
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
<a moz-do-not-send="true" href="http://www.cs.rochester.edu/u/criswell" target="_blank">http://www.cs.rochester.edu/u/criswell</a></pre>
              </span></div>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
    <p><br>
    </p>
    <pre class="moz-signature" cols="72">-- 
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
<a class="moz-txt-link-freetext" href="http://www.cs.rochester.edu/u/criswell">http://www.cs.rochester.edu/u/criswell</a></pre>
  </body>
</html>