<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/135188>135188</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V] Machine Block Placement generates extra jump instructions that are not cleaned up.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:RISC-V
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          stefanp-synopsys
      </td>
    </tr>
</table>

<pre>
    This is a performance issue.
For the following code:
```
int compute1(int);
int compute2(int);
int test();

int branch(int check1, int check2, int check3, int check4, int in) {
  int doadd = 1;
  if (check1) {
    in = compute1(in);
    doadd = 1; // We do the add.
  } else if (check2) {
    in = compute2(in);
    doadd = test(); // Unknown if we should do the add.
  } else if (check3) {
    in *= 7;
    doadd = 0; // Do not do the add.
  }

  if (doadd)
    in += 5;

  // Do a multiply by 7 here to confuse the compiler.
  if (check4) {
    in *= 7;
  }
 return in;
}
```
We can compile for RISCV:
```
clang -O3 -S branch.c -target riscv64-arc-linux-gnu
```
In the final assembly we end up with basic blocks that look like this:
```
< ... more code up here ... >
 64 .LBB0_8:                                # %if.end9
 65         slli    a0, a4, 3
 66         subw    a0, a0, a4
 67         bnez    a2, .LBB0_11
 68 # %bb.9:                                # %if.end9
 69         bnez    a2, .LBB0_2
 70         j       .LBB0_12
 71 .LBB0_10:
 72         mv      a0, s1
 73         j       .LBB0_2
 74 .LBB0_11: # %if.end9
 75         mv      a4, a0
 76         bnez    a2, .LBB0_2
 77 .LBB0_12:
 78         addiw   a0, a4, 5
 79         j .LBB0_2
```
In the above we generate an unconditional jump (line 70) to `LBB0_12` and then from there we jump right back out to `LBB0_2` after a single instruction. I feel like `LBB0_12` should be duplicated into line 70. This would save a jump and only at the cost of a slight code size increase. 

There should be a way to clean up situations where we have:
```
jump A
<...>
A:
<very few instructions, maybe 1 - 3>
jump B 
```

I'm happy to provide a patch for this but I don't know how to approach the issue because machine block placement runs very late. Should this be an improvement in machine block placement? 
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVt1u4zgPfRrlhohhy06cXOQibb8ABb7FLnZmZy4XtEzHmsqSIcnJZJ5-Idn5azvdwRYFHFmHPIcUKRqdk3tNtGGLB7Z4muHgW2M3zlODup-7kza9O7lZZerT5nMrHUgHCD3ZxtgOtSCQzg2UsHS7MxZ8S9AYpcxR6j0IUxPLtyzdsmU6_adbqT0I0_WDp4zxldSe8TXLH-63-DtbnpxnfHV5N72uLGrRjngQLYmXjPFHuKz43Sq_WxXnldSMr4GVwS_EN7XBugaWP0E20gHIBhhfnSmu8GAQkXdxXXQGwL03YHzH-A6-EtQmZg3rOolQVj4BKUe3ZPwjMv4R2V3Kzqx_6RdtjjowHAlcawZV_5KO_K0Ovg005Xvc6Q3lkwFt_Hsk4zmecxutg9obhofgbHE5crjxidANystenaA6QQktWQJvQBjdDI4iWUiTVGST10dY_Gs0ozyw5AerQ4mMGkbRNyX9lUCgPjNBYyz8-fzp8cvb4hcK9R7mv-cw_zQVbiJg7tHuyYOVThyWxRytmCuph-_zvR5eeXjWY5dJjQrQOeoqdQrnSLqGoYej9C1U6KSAShnx4sC36EEZ8wJKvoScSPdWGMsfIUkS6Iyl2LjBV0xneMvy_4VELAtI_v_wkP69YnnM2Ud_jOfA-EI2Cek6Huhycdl0SsnwxDQ0IMY2zCNmecUM1fEGc0YGUHkBVZp-RFDs8lFdlkXQ6iyhqpL1fxO8_pCHB0yZXjDfpuekYtzOzst0TDqU_GLQHcbnGJuLssv8J_5Gd8U1xnz7juRy8dZ7MeUvbC9_IaLyGsEkeXWxwrqWx1cHt4iY9Y3uq7d3Shcrc6BQsXvSZNEToIZBC6Nr6aUJdf1t6PrQp0pqgjINjeoNsGV61rVMAXUd3GlorOnCLxudRlMr962HCsULmMHf2o6mjScLCE7qvSKQ2nk7iMCdwDM0RGpslXvC6aKsCOqhV1KgpzpMCgOTzATi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