<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Mircea,<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 9 Mar 2018, at 16:19, Mircea Trofin <<a href="mailto:mtrofin@google.com" class="">mtrofin@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Thanks for the details!<div class=""><br class=""></div><div class="">How should we think of the case where an instruction has memory operands (in the sense that X86II::getMemoryOperandNo >=0), but doesn't have MachineMemOperands? </div></div></div></blockquote><div><br class=""></div><div>I am not sure if this rule is enforced throughout the code generator, but I can see that some places like here (<a href="https://github.com/llvm-mirror/llvm/blob/12d5807/lib/CodeGen/MachineInstr.cpp#L1082" class="">https://github.com/llvm-mirror/llvm/blob/12d5807/lib/CodeGen/MachineInstr.cpp#L1082</a>) and here (<a href="https://github.com/llvm-mirror/llvm/blob/12d5807/lib/CodeGen/MachineLICM.cpp#L881" class="">https://github.com/llvm-mirror/llvm/blob/12d5807/lib/CodeGen/MachineLICM.cpp#L881</a>), we conservatively assume that if there are no memory operands, we follow the mayLoad / mayStore flags from the instruction description.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">I'm seeing an example in the case of __builtin_prefetch (lowered via SelectionDAG::getMemIntrinsicNode, which produces a MachineMemOperand) vs __builtin_ia32_gatherpfdpd, lowered through getPrefetchNode in X86ISelLowering.cpp. The latter doesn't have a MachineMemOperand. </div><div class=""><br class=""></div><div class="">Is the latter technically a bug, maybe, and it so happens nothing is hindered by the absence of the MachineMemOperand, so we're not observing it?</div></div></div></blockquote><div><br class=""></div><div>If what I said above is true (I’m not completely sure either), I suppose adding MachineMemOperands while lowering would allow for more optimizations to kick in.</div><div><br class=""></div><div>Thanks,</div><div><br class=""></div><div>— </div><div>Francis</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">Added Elena, based on commit log in X86ISelLowering.cpp, perhaps she has more context.</div><div class=""><br class=""></div>Thanks!<div class="">Mircea</div><div class=""><br class=""></div><div class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, Mar 8, 2018 at 2:09 PM Francis Visoiu Mistrih <<a href="mailto:francisvm@yahoo.com" class="">francisvm@yahoo.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Mircea,<br class="">
<br class="">
> On 8 Mar 2018, at 18:52, Mircea Trofin via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class="">
><br class="">
> Hello,<br class="">
><br class="">
> I'm trying to understand the relationship between MachineMemOperand and, on X86, memory operands of machine instructions. The latter seem to be operands held in order by the MachineInstr, from an offset onwards - Base, Scale, Index, Displacement, Segment. The former, if I understand it correctly, is used to hold a relationship back to IR load/store instructions.<br class="">
<br class="">
MachineMemOperands are used to represent memory references, so that the (generic) code generator can reason about their dependencies and aliases.<br class="">
<br class="">
You can see them when:<br class="">
* you call MachineInstr::dump (llc -print-after-all, etc.), at the end of the instruction, there is a "; mem:LD4..."<br class="">
* using MIR (llc -stop-before/-run-pass/etc.), at the end of the instruction, ":: (load 4…)”<br class="">
* using llc -asm-verbose, they are emitted as comments "# 4-byte Reload”<br class="">
<br class="">
MachineInstrs will have multiple operands, ex:<br class="">
<br class="">
> %5 = MOV64rm %0, 1, $noreg, 0, $noreg<br class="">
<br class="">
<br class="">
and in order to know which operand is what, the X86 backend uses some enums and helper functions so that they can write code like:<br class="">
<br class="">
> const MachineOperand &BaseReg  = MI->getOperand(Op+X86::AddrBaseReg);<br class="">
<br class="">
> const MCOperand &BaseReg  = MI.getOperand(Op+X86::AddrBaseReg);<br class="">
<br class="">
for both MachineInstrs and MCInsts.<br class="">
<br class="">
><br class="">
> Is it possible to have a X86 MachineInstr with a memory operand (i.e. X86II::getMemoryOperandNo >=0), that has no MachineMemOperand? What about the reverse?<br class="">
><br class="">
<br class="">
* Attaching a MachineMemOperand to an instruction that doesn’t reference any memory:<br class="">
<br class="">
From what I tried, the instruction has to be marked as “mayLoad / mayStore” (you can see them in X86InstrInfo.td) in order to pass the verifier:<br class="">
<br class="">
> $rbx = MOV64rr $rax :: (load 4)<br class="">
<br class="">
> bb.0:<br class="">
>   liveins: $rax<br class="">
>   $rbx = MOV64rr $rax; mem:LD4[<unknown>]<br class="">
><br class="">
> # End machine code for function foo.<br class="">
><br class="">
> *** Bad machine code: Missing mayLoad flag ***<br class="">
> - function:    foo<br class="">
> - basic block: %bb.0  (0x7fc884017678)<br class="">
> - instruction: $rbx = MOV64rr $rax; mem:LD4[<unknown>]<br class="">
><br class="">
> LLVM ERROR: Found 1 machine code errors.<br class="">
<br class="">
* Memory-touching MachineInstrs without MachineMemOperands:<br class="">
<br class="">
> $rbx = MOV64rm _, 1, _, 0, _<br class="">
<br class="">
<br class="">
This is allowed.<br class="">
<br class="">
(anyone, please correct me if I’m wrong on anything here)<br class="">
<br class="">
Cheers,<br class="">
<br class="">
—<br class="">
Francis<br class="">
<br class="">
> Thanks,<br class="">
> Mircea.<br class="">
> _______________________________________________<br class="">
> LLVM Developers mailing list<br class="">
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class="">
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="">
<br class="">
</blockquote></div></div></div>
</div></blockquote></div><br class=""></body></html>