<div dir="ltr"><div class="markdown-here-wrapper" style><p style="margin:1.2em 0px!important">I’m working on a <a href="https://github.com/avr-llvm/llvm">backend</a> for the 8-bit AVR microcontroller architecture. As you can imagine, there is no support for hardware division. Because of this, llvm automatically lowers all divisions into calls to the runtime library - be it libgcc or compiler-rt.</p>
<p style="margin:1.2em 0px!important">The runtime function in libgcc to perform 16-bit unsigned division/remainder is called <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)">__udivmodhi4</code>. LLVM by default assumes this function to be named <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)">udivhi3</code> (see <a href="https://github.com/llvm-mirror/llvm/blob/master/lib/CodeGen/TargetLoweringBase.cpp#L70"><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)">lib/CodeGen/TargetLoweringBase.cpp</code></a>.</p>
<p style="margin:1.2em 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)">udivhi3</code> does not exist any longer, it has been superseded by <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)">__divmodhi4</code>, which returns the quotient and a remainder. Due to this, it has a different signature. I can change the name of the function in <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)">TargetLoweringBase.cpp</code>, so that it points to the right function, by LLVM will not know about the change in calling convention (a custom one is used in the AVR implementation of the function), or the differing result type.</p>
<p style="margin:1.2em 0px!important">I cannot figure out how to tell LLVM about the differing signature of the function. What is the best way to fix this?</p>
<p style="margin:1.2em 0px!important">I can think of several solutions:</p>
<ul style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px">Create a pseudo instruction which pattern matches on integer divisions. Write a pass to expand this into the appropriate runtime library calls.</li>
<li style="margin:0.5em 0px">Manually write an implementation of the original <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)">udivhi3</code> function, and add it to compiler-rt. This would render binaries compiled with AVR-LLVM incompatible with libgcc.</li>
<li style="margin:0.5em 0px">Somehow modify LLVM to use the right calling convention, arguments, and result for the function.</li></ul><div>What is the best way to go about solving this?<br></div><div><br></div><div>Thanks,</div><div>Dylan</div>
<div title="MDH:SSdtIHdvcmtpbmcgb24gYSA8YSBocmVmPSJodHRwczovL2dpdGh1Yi5jb20vYXZyLWxsdm0vbGx2
bSI+YmFja2VuZDwvYT4gZm9yIHRoZSA4LWJpdCBBVlIgbWljcm9jb250cm9sbGVyIGFyY2hpdGVj
dHVyZS4gQXMgeW91IGNhbiBpbWFnaW5lLCB0aGVyZSBpcyBubyBzdXBwb3J0IGZvciBoYXJkd2Fy
ZSBkaXZpc2lvbi4gQmVjYXVzZSBvZiB0aGlzLCBsbHZtIGF1dG9tYXRpY2FsbHkgbG93ZXJzIGFs
bCBkaXZpc2lvbnMgaW50byBjYWxscyB0byB0aGUgcnVudGltZSBsaWJyYXJ5IC0gYmUgaXQgbGli
Z2NjIG9yIGNvbXBpbGVyLXJ0LjxkaXY+PGJyPjwvZG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