Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

[ppc] redundant zero extension when returning bool value #31414

Open Quuxplusone opened 7 years ago

Quuxplusone commented 7 years ago
Bugzilla Link PR32442
Status CONFIRMED
Importance P enhancement
Reported by Carrot (carrot@google.com)
Reported on 2017-03-27 14:45:04 -0700
Last modified on 2017-04-18 00:44:00 -0700
Version trunk
Hardware PC Linux
CC hfinkel@anl.gov, inouehrs@jp.ibm.com, kit.barton@gmail.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Compile the following code with options -m64 -O2 -mvsx -mcpu=power8

bool foo(int i, int* p) {
  if (i==0)
    return false;

  *p = i;
  return true;
}

LLVM generates:

_Z3fooiPi:                              # @_Z3fooiPi
.Lfunc_begin0:
# BB#0:                                 # %entry
        cmplwi   3, 0
        beq      0, .LBB0_2
# BB#1:                                 # %if.end
        stw 3, 0(4)
        li 3, 1
        clrldi   3, 3, 32           // redundant
        blr
.LBB0_2:
        li 3, 0
        clrldi   3, 3, 32           // redundant
        blr

The problem is when PPCBoolRetToInt.cpp converts the old the old cmp/isel
behavior into today's integer operation, it uses  i32 type, on ppc64, it needs
to be zero extended to 64bit before return. We should use i64 on ppc64 and i32
on ppc32.
Quuxplusone commented 7 years ago
The following patch (under review in Phabricator) resolves this issue.
https://reviews.llvm.org/D31319

Generated code with the patch:

_Z3fooiPi:                              # @_Z3fooiPi
.Lfunc_begin0:
# BB#0:                                 # %entry
    cmplwi   3, 0
    beq  0, .LBB0_2
# BB#1:                                 # %if.end
    stw 3, 0(4)
    li 3, 1
    blr
.LBB0_2:
    li 3, 0
    blr