[PATCH] D61433: -Oz: Reuse constants in registers instead of canonicalizing operations that use a different constant

Ramakota Reddy via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 2 05:10:17 PDT 2019


ramred01 created this revision.
ramred01 added reviewers: javed.absar, olista01.

This patch fixes an issue with -Oz and -Os compilation where it is profitable to reuse constants that are already in registers rather than canonicalizing an operation that changes the constant used, thereby having to materialize the constant again.

e.g:
int test(int* x)
{

  if (x > (int)655360000L)
     return *x - (int)655360000L;
  return x;

}

In the above code, although the constant used in the compare is reused in the subtraction, LLVM canonicalizes the subtraction to an addition with a negative constant.  That means that the constant that was materialized in a register for the compare cannot be reused for the subsequent operation and another constant (which is the negative of the first one) is materialized.

We solve this by tracking the constants that are used in a function and wherever we see a new constant, we check is it is the negative of an earlier used constant or just one off from an earlier used constant.  If so, then we reverse the canonicalization for that instruction so as to use the same constant as was used before.


https://reviews.llvm.org/D61433

Files:
  include/llvm/CodeGen/TargetInstrInfo.h
  lib/CodeGen/MachineCSE.cpp
  lib/Target/AArch64/AArch64InstrInfo.cpp
  lib/Target/AArch64/AArch64InstrInfo.h
  test/CodeGen/AArch64/better_use_of_existing_constants.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61433.197751.patch
Type: text/x-patch
Size: 13288 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190502/824dd068/attachment.bin>


More information about the llvm-commits mailing list