<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Sep 4, 2016 at 8:37 PM, Dean Michael Berris <span dir="ltr"><<a href="mailto:dean.berris@gmail.com" target="_blank">dean.berris@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">We do something very similar here for XRay, and I would think the approach would be similar. What XRay does are the following:<br>
<br>
- Find the machine instructions in a MachineFunctionPass that look interesting from the XRay perspective. Theses turn out to be: the beginning of the function (not really an instruction but a location), tail calls, and returns. I suspect you can very simply find the call instructions for the platform you're interested in and insert/wrap it in a pseudo instruction.<br>
- When lowering, emit the actual assembly sequence that you want.<br>
<br>
For your use-case though I think you may need to hook into function call lowering so you can insert your instruction sequence before stack adjustments are performed (if you want to insert your intercepts before any stack operations as opposed to just before actually calling the function).<br>
<br></blockquote><div>Hi Dean,</div><div>Thank you very much! </div><div><br></div><div>For the function call lowering, do you mean lowercall function? I did insert the instruction before the stack adjustments, but the inserted code appears in the prologue of the function, other than before the function call. Maybe I did something wrong with the iterator. </div><div><br></div><div>So you mean I should insert a pseudo instruction in the machinefunction pass, then replace it when function call lowering?  (Like segemented stack implementation?)</div><div><br></div><div>Thanks!</div><div>Shucai</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Hope this helps.<br>
<span class="gmail-HOEnZb"><font color="#888888"><br>
-- Dean<br>
</font></span><div class="gmail-HOEnZb"><div class="gmail-h5"><br>
> On 5 Sep 2016, at 00:23, Ryan Taylor via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
> Personally I would add a new pass that iterates, looks for the call you want then inserts the new instruction.<br>
><br>
><br>
> On Sep 4, 2016 10:20, "SHUCAI YAO" <<a href="mailto:yaos4@mcmaster.ca">yaos4@mcmaster.ca</a>> wrote:<br>
><br>
><br>
> On Sun, Sep 4, 2016 at 7:44 AM, Ryan Taylor <<a href="mailto:ryta1203@gmail.com">ryta1203@gmail.com</a>> wrote:<br>
> Mehdi,<br>
><br>
>  Sorry, I misread his original post.<br>
><br>
>  So something like:<br>
><br>
>  XXXInsrtInfo   *XII;    // target instruction info<br>
>   MachineBasicBlock::iterator MI = MachineBasicBlock(<wbr>YourCallInst);<br>
>  MachineBasicBlock *MBB = YourCallInst->getParent(); // basic block location of your call inst<br>
>  BuildMI(*MBB, MI, DebugLoc(), XII->get(XXX:::INSTRUCTION)...<wbr>...);<br>
><br>
> The BuildMI params are going to depend on what you want to do with the instruction being inserted.<br>
>  <a href="http://llvm.org/docs/doxygen/html/MachineInstrBuilder_8h.html" rel="noreferrer" target="_blank">http://llvm.org/docs/doxygen/<wbr>html/MachineInstrBuilder_8h.<wbr>html</a><br>
><br>
> -Ryan<br>
><br>
> Hi Ryan,<br>
>     I need to add two instructions for each function call. Do you mean I should add this snippet in the Lowercall function? Or I should add a new pass?<br>
><br>
> Thanks!<br>
><br>
><br>
><br>
><br>
> On Sun, Sep 4, 2016 at 1:45 AM, Mehdi Amini <<a href="mailto:mehdi.amini@apple.com">mehdi.amini@apple.com</a>> wrote:<br>
><br>
>> On Sep 3, 2016, at 6:18 PM, Ryan Taylor via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
>><br>
>> So one way might look like this:<br>
>><br>
>> IRBuilder<> Builder(&*BB); // BB = Function::iterator OR IRBuilder<> Builder(CallInst->getParent())<wbr>;<br>
>> Builder.SetInsertPoint(<wbr>CallInst);<br>
>> InstructionClass *YourNewInstruction = builder.<wbr>CreateInstructionClass(.....); // InstructionClass = type of instruction you are inserting<br>
>><br>
><br>
> I’m not sure how the IRBuilder would work at the MI level, as Shucai was asking.<br>
><br>
>><br>
>><br>
>><br>
>> On Sat, Sep 3, 2016 at 6:04 PM, Ryan Taylor <<a href="mailto:ryta1203@gmail.com">ryta1203@gmail.com</a>> wrote:<br>
>> Take a look at IRBuilder and SetInsertPoint().<br>
>><br>
>><br>
>> On Sep 3, 2016 18:02, "SHUCAI YAO via llvm-dev" <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> I'm trying to insert some instructions before each function calls (before arguments push):<br>
>>    lea %EAX, label                      ----- new instructions<br>
>>    mov [ESP+stacksize], %EAX  ----- new instructions<br>
>>    push arg1<br>
>>    push arg2<br>
>>    ...<br>
>>    push argn<br>
>>    call callee_name<br>
>><br>
>> I am a newbie to LLVM. I tried to use buildMI() to insert the instructions in the lowercall() function. But I couldn't put these instructions in the right positions. Is there a way to locate the position by using MachineBasicBlock iterator?<br>
><br>
> Can you describe more precisely what are you trying to achieve?<br>
> I.e. what are these instructions? Why do you want to do that? It may lead to a different answer.<br>
><br>
> I'm trying to implement something similar to segmented stack mechanism by using LLVM. Instead of inserting comparison code in the prologue of the function, I would like do the probe before arguments pushed. The segmentd stacks append a guarded page. This guard page will call the addmorestack function if the probe instructions touch this guarded page. Otherwise, it only stroe the return address in the bottom of the callee stack frame.<br>
><br>
> In order to achieve this, for each function call, two instructions are needed to be inserted:<br>
>      LEA  %EAX, callee_return_label<br>
>      MOV [ESP- callee_stack_frame_size - arguments_size], %EAX<br>
>      PUSH argn<br>
>      ...<br>
>      PUSH arg1<br>
>      JMP callee_name<br>
> callee_return_label:<br>
>      ...<br>
><br>
> So I need to insert two instructions (LEA and MOV) before each function call. I don't know when how to insert these two instructions.<br>
><br>
> Thanks!<br>
> Shucai<br>
><br>
><br>
> —<br>
> Mehdi<br>
><br>
><br>
</div></div><div class="gmail-HOEnZb"><div class="gmail-h5">> ______________________________<wbr>_________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br>
</div></div></blockquote></div><br></div></div>