[llvm-bugs] [Bug 39092] New: Inefficient lowering of constant, negative, anyext return values

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Sep 26 13:49:01 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=39092

            Bug ID: 39092
           Summary: Inefficient lowering of constant, negative, anyext
                    return values
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Common Code Generator Code
          Assignee: unassignedbugs at nondot.org
          Reporter: asb at lowrisc.org
                CC: llvm-bugs at lists.llvm.org

Consider a function returning a constant negative value, e.g.

define i16 @neg_const() nounwind {
  ret i16 -2047
}

Compile this on a target for which i16 is not a legal type, e.g. PowerPC. For
llc -mtriple=powerpc, the following assembly is generated:
        lis 3, 0
        ori 3, 3, 65436
        blr


We'd rather see:
        li 3, -100
        blr

The issue is that SelectionDAGBuilder::visitRet will call
SelectionDAG::getCopyToParts with ExtendKind = ISD::ANY_EXTEND. This calls
SelectionDAG::getNode with ISD::ANY_EXTEND, which will recognise it has a
constant argument and choose to zero-extend it. This generates the i32 constant
65436 (i.e. zext of int16(-100)) rather than the preferable -100.

By the time target instruction selection code is reached, there is no way to
know that a sign-extended form of the original constant could have been used.

As a quick experiment I changed the constant-folding code in
SelectionDAG::getNode so it performs a sign-extend for ANY_EXTEND input. That
fixes this particular issue as expected but leads to incorrect codegen
elsewhere, though I haven't yet had a chance to explore in more detail.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180926/9cc0fe83/attachment.html>


More information about the llvm-bugs mailing list