<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:1.2em 0px!important">Hello all,</p>
<p style="margin:1.2em 0px!important">I would like to propose a large change to LLVM that I would be happy to implement.</p>
<p style="margin:1.2em 0px!important">The instruction selection legalizer knows of three different ways to legalize an action (ignoring an already legal action).</p>
<ul style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px">Expansion</li>
<li style="margin:0.5em 0px">Promotion</li>
<li style="margin:0.5em 0px">Custom</li>
</ul>
<p style="margin:1.2em 0px!important">Expanding a node will lead to one of two things - the operation will be split into several equivalent smaller operations (i.e. 64-bit mul => two 32-bit multiplications), or it will be expanded into a different operation(s) (i.e. <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">SDIVREM</code> => <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">SDIV</code> + <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">SREM</code> or a runtime library call).</p>
<p style="margin:1.2em 0px!important">This can be ambiguous - should a 128-bit <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">SDIVREM</code> be expanded into two 64-bit <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">SDIVREM</code>s or a 128-bit libcall.</p>
<p style="margin:1.2em 0px!important">It would be useful for LLVM to distinguish between these two operations - i.e. instead of <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">LegalizeAction::Expand</code>, we have <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">LegalizeAction::Expand</code> and <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">LegalizeAction::Split</code>.</p>
<ul style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px"><p style="margin:1.2em 0px!important;margin:0.5em 0px!important"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">Expand</code> should always expand the node into a different pattern (such as <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">MULHU</code> expanding to <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">MUL</code> and a copy of the top half).</p>
</li>
<li style="margin:0.5em 0px"><p style="margin:1.2em 0px!important;margin:0.5em 0px!important"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">Split</code> will always split an operation into several smaller, operations (such as 128-bit addition being split into a 64-bit addition and a 64-bit addition with carry)</p>
</li>
</ul>
<p style="margin:1.2em 0px!important">Full disclosure: I’m working on a backend on which the pointer type is illegal (Atmel AVR : 8-bit microcontroller with 16-bit pointers). The problem with the ambiguity with expand today is that LLVM implicitly assumes that as a 16-bit register class exists, then 16-bit addition/subtraction should exist and there is no point splitting into two 8-bit operations.</p>
<p style="margin:1.2em 0px!important">Currently we work around this by defining 16-bit pseudo instructions for every instruction which is not properly supported. This gives us very sub-optimal code and introduces many bugs. More detail about the problem can be found in the old mailing list <a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2015-June/087277.html">archives</a>.</p>
<p style="margin:1.2em 0px!important">I’m not sure if this requires an RFC? It would be a very large breaking change. Perhaps a less drastic solution can be found, like leaving <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">Expand</code> as it is, but also adding <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248)">LegalizeAction::Split</code> which must expand only using smaller operations.</p>
<p style="margin:1.2em 0px!important">Again, I would be happy to work on this.</p>
<p style="margin:1.2em 0px!important">Cheers,<br>Dylan McKay</p>
<div title="MDH:SGVsbG8gYWxsLDxkaXY+PGJyPjwvZGl2PjxkaXY+SSB3b3VsZCBsaWtlIHRvIHByb3Bvc2UgYSBs
YXJnZSBjaGFuZ2UgdG8gTExWTSB0aGF0IEkgd291bGQgYmUgaGFwcHkgdG8gaW1wbGVtZW50Ljwv
ZGl2PjxkaXY+PGJyPjwvZGl2PjxkaXY+VGhlIGluc3RydWN0aW9uIHNlbGVjd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