<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Bad codegen for rotate ops"
   href="https://bugs.llvm.org/show_bug.cgi?id=41358">41358</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Bad codegen for rotate ops
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Backend: AVR
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>hello@dsprenkels.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I am aware the AVR target is still experimental, so I am a bit unsure wether
this report is actually useful. Anyway, here goes:

I think the codegen for rotate instructions on AVR is incorrect. For example,
the IR

```
define i8 @example(i8, i8) {
  %a = tail call i8 @llvm.fshl.i8(i8 %0, i8 %0, i8 %1)
  ret i8 %a
}

declare i8 @llvm.fshl.i8(i8, i8, i8)
```

compiles to:

```
        .text
        .file   "<stdin>"
        .globl  example                 ; -- Begin function example
        .p2align        1
        .type   example,@function
example:                                ; @example
; %bb.0:
        cpi     r22, 0
        breq    LBB0_2
LBB0_1:                                 ; =>This Inner Loop Header: Depth=1
        rol     r24
        subi    r22, 1
        brne    LBB0_1
LBB0_2:
        ret
.Lfunc_end0:
        .size   example, .Lfunc_end0-example
                                        ; -- End function

        ; Declaring this symbol tells the CRT that it should
        ;copy all variables from program memory to RAM on startup
        .globl  __do_copy_data
        ; Declaring this symbol tells the CRT that it should
        ;clear the zeroed data section on startup
        .globl  __do_clear_bss

```

However, the Microchip documentation mentions that on AVR, rotates pass through
the carry bit in the status register. Should the codegen be updated?

For context, avr-gcc also produces a different result:
<a href="https://godbolt.org/z/Cs-dif">https://godbolt.org/z/Cs-dif</a>

As a fix, I would propose something like this:

```
        .text
        .file   "<stdin>"
        .globl  example                 ; -- Begin function example
        .p2align        1
        .type   example,@function
example:                                ; @example
; %bb.0:
        cpi     r22, 0
        breq    LBB0_2
        eor     r20                     ; get zero in a register
        clc                             ; clear the carry bit
LBB0_1:                                 ; =>This Inner Loop Header: Depth=1
        rol     r24                     ; do a carried rotate to the left
        adc     r24, r20                ; add the carry bit and set the carry
bit to 0
        dec     r22                     ; dec loop counter (without tainting
carry bit)
        brne    LBB0_1
LBB0_2:
        ret
.Lfunc_end0:
        .size   example, .Lfunc_end0-example
                                        ; -- End function

        ; Declaring this symbol tells the CRT that it should
        ;copy all variables from program memory to RAM on startup
        .globl  __do_copy_data
        ; Declaring this symbol tells the CRT that it should
        ;clear the zeroed data section on startup
        .globl  __do_clear_bss

```</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>