<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54991>54991</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
MIPS backend doesn't combine div+rem optimally
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:MIPS
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
efriedma-quic
</td>
</tr>
</table>
<pre>
Consider the following testcase:
```
int a(int x, int y) { return x / y; }
int b(int x, int y) { return x % y; }
void c(int x, int y, int *a, int *b) { *a = x / y; *b = x % y; }
```
Compile with "-target mips -O2 -fomit-frame-pointer", and you get:
```
a: # @a
div $zero, $4, $5
teq $5, $zero, 7
jr $ra
mflo $2
b: # @b
div $zero, $4, $5
teq $5, $zero, 7
jr $ra
mfhi $2
c: # @c
div $zero, $4, $5
teq $5, $zero, 7
mflo $1
sw $1, 0($6)
mul $1, $1, $5
subu $1, $4, $1
jr $ra
sw $1, 0($7)
```
The code for "c" is suboptimal: it should use mfhi instead of computing the remainder using a multiply+sub.
godbolt: https://godbolt.org/z/hbbqPd4Pe
Originally reported at https://discourse.llvm.org/t/how-to-cross-compile-a-c-code-to-mips-r2000/61872 .
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFVM1uozAQfhq4WETGQAgHDk2jSntYtdLuCxjbBHcNpv5pmj79jhOSJqi7Si-7EcH2zHwz34yZaTTf1_d6sJILg1wnUKuV0js5bJET1jFqRZTdRXgT4dN7iafncJSDQzQiq7C-ReQehc0-IhWKyjUywnkzoDcUkQeQZmuQbj6AzS3AYgZ81ZIj9gnyuInIHb3YNyePQY6ibHNFBvRn2TzOLNHj-173o1QC7aTrAEMSR81WONTL0aLkkaCk1b10SWtoL5JRAwthwC4wogNHe-0R2P-9phTU6KZfRDIU5ZgecScpl6-TNn8XRofYsM2ntbg2duLlZFxMFidQeW35bE5BczOL2LdKTypy1DRfzaH5_zl08joH9tUc2D_J4aLY6bXG7s7ZpQGIoUtgv4QemLnw6srwYp3Rsr7xM8v8jLi1tH_gVZ55fdprP2EcMc3DTDKh16DnCZI2UNKjkz1V4X6kQ7bTXnHkrTheohysE5Qj3QK-H707jDPwZkRP5RBGnbdBRkMhnBwVTI81uF1cht9q3mgVWhV1zo029Cx5gGdSLLTZwukd_l3TvDzx_Elc4h-N3MqBKrWHuKM2TnBE3cwXl5Zpb6xYKPXaTy5dcKl3idMJM9rahB2nTkITloSKBE2YOIkhGEMxH5bpqiRoEfM641VW0dhJp0T9_dvTD9RQ9kvA6OFa2CEipQtFaeQgwvcJeUNR0FRPtY-9UfUsXRh1UBoAwSGwnJZkNPpZsEBWWuuFhU2RV1Uad3VBirZqcIZbukpb2lRCFCxNyxVmmOCWxYo2Qtk6KtZwqRNDiBgIh2lZbGJZE0wIziHDslhm-WKZ4hWHr49WvGSrJYV-C9epzpWLTX3g1fitBaWS1tkPJbVWbgchDjHBP_Wu06YWrZGC9zR58ZLFh0TqQxa_Aest3qw">