| Issue |
74124
|
| Summary |
[flang] `(-1)^n` --> `n % 2 == 0 ? 1 : -1`
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
foxtran
|
Dear,
Having the following code,
```fortran
integer function minus_one_pow(x)
implicit none
integer, intent(in) :: x
_one_pow_ = (-1)**x
end function minus_one_pow
```
flang-new generates a very compicated ASM code:
```asm
.file "FIRModule"
.globl one_pow_
.p2align 4, 0x90
.type one_pow_,@function
one_pow_:
movl (%rdi), %ecx
testl %ecx, %ecx
je .LBB0_1
movl %ecx, %eax
andl $1, %eax
negl %eax
orl $1, %eax
cmpl $2, %ecx
jl .LBB0_5
movl $-1, %edx
movl $1, %esi
movl %ecx, %edi
.p2align 4, 0x90
.LBB0_4:
shrl %edi
imull %edx, %edx
testb $2, %cl
movl %edx, %r8d
cmovel %esi, %r8d
imull %r8d, %eax
cmpl $3, %ecx
movl %edi, %ecx
ja .LBB0_4
.LBB0_5:
retq
.LBB0_1:
movl $1, %eax
retq
.Lfunc_end0:
.size one_pow_, .Lfunc_end0-one_pow_
.section ".note.GNU-stack","",@progbits
```
At the same time, GCC produces a little code:
```asm
.file "test.f90"
.text
.p2align 4
.globl one_pow_
.type one_pow_, @function
one_pow_:
.LFB0:
.cfi_startproc
movl (%rdi), %edx
movl $1, %eax
addl %edx, %edx
andl $2, %edx
subl %edx, %eax
ret
.cfi_endproc
.LFE0:
.size one_pow_, .-one_pow_
.ident "GCC: (GNU) 12.2.0 20220819 (HPE)"
.section .note.GNU-stack,"",@progbits
```
Running commands:
```
gfortran test.f90 -O3 -S -o test-gcc.S
flang-new test.f90 -O3 -S -o test-flang.S
```
Used compilers:
- flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git 9584f5834499e6093797d4a28fde209f927ea556
- GNU Fortran (GCC) 12.2.0 20220819 (HPE)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs