This is an archive of the discontinued LLVM Phabricator instance.

When canonicalizing gep indices, prefer zext when possible (SelectionDAGBuilder)
AbandonedPublic

Authored by reames on Feb 13 2015, 2:37 PM.

Details

Reviewers
majnemer
hfinkel
Summary

This was split from http://reviews.llvm.org/D7255 and implements the SelectionDAGBuilder parts.

We eagerly canonicalize gep indices to the width of a machine register (i64 on x86_64). To do so, we insert sign extensions (sext) to promote smaller types. This change substitutes zero extensions where we know the value being extended is positive. This is motivated by the fact that zero extensions are generally cheaper on x86.

Diff Detail

Event Timeline

reames updated this revision to Diff 19934.Feb 13 2015, 2:37 PM
reames retitled this revision from to When canonicalizing gep indices, prefer zext when possible (SelectionDAGBuilder).
reames updated this object.
reames edited the test plan for this revision. (Show Details)
reames added reviewers: hfinkel, majnemer.
reames added a subscriber: Unknown Object (MLST).
sanjoy added a subscriber: sanjoy.Feb 13 2015, 3:06 PM

Small drop-by comment inline. I don't know enough about SelectionDAG to LGTM this.

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
3465

A TODO here (definitely not this change) could be to exploit the case where you *know* that the high bit is set (i.e. KnownOne == true), and emit a bitwise OR of 0xffff0000 with a zext of the value. I don't know if that will actually be faster, even on x86, but it does remove the data dependence of the higher bits on the sign bit of the source (narrower) value.

hfinkel edited edge metadata.Feb 13 2015, 3:52 PM

Part of the issue is that this canonicalization already exists in DAGCombine (at the end of DAGCombiner::visitSIGN_EXTEND):

// fold (sext x) -> (zext x) if the sign bit is known zero.
if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
    DAG.SignBitIsZero(N0))
  return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);

As a result, I'm not sure this is needed.

reames abandoned this revision.Feb 13 2015, 4:03 PM

Part of the issue is that this canonicalization already exists in DAGCombine (at the end of DAGCombiner::visitSIGN_EXTEND):

// fold (sext x) -> (zext x) if the sign bit is known zero.
if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
    DAG.SignBitIsZero(N0))
  return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);

As a result, I'm not sure this is needed.

I agree. Given this, I find it really odd I saw the sign extensions to start with... I suspect we're missing something here.

Until we figure out what that something is, I'm going to abandon this change for now.