[PATCH] D45524: Fix incorrect choice of callee-saved registers save/restore points

Momchil Velikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 11 09:14:33 PDT 2018


chill created this revision.
chill added reviewers: thegameg, qcolombet, t.p.northover.
Herald added a subscriber: kristof.beyls.

So, I have this testcase:

  void f(int n, int x[]) {
    if (n < 0)
      return;
  
    int a[n];
  
    for (int i = 0; i < n; i++)
      a[i] = x[n - i - 1];
  
    for (int i = 0; i < n; i++)
      x[i] = a[i] + 1;
  }

that, compiled with -O1/-Os for AArch64 and X86, generates machine code, which
fails to properly restore the stack pointer upon function return.

The testcase allocates a VLA, thus clang generates calls to `llvm.stacksave` and
`llvm.stackrestore`. The latter call is lowered to `mov sp, x2`, however this
move does not affect decisions made by the shrink wrapping pass, as the
instruction does not use or define a callee-saved register.

The end effect is that placement of register save/restore code is such that
along a certain path, the callee-saved registers and the stack pointer are
restored, but then the stack pointer is overwritten with an incorrect value.

This patches fixes the issue this by modifying `ShrinkWrap::useOrDefCSROrFI` to explicitly check
for the stack pointer register (using `TLI.getStackPointerRegisterToSaveRestore`) and also to ignore tall call
instructions (`isCall() && isReturn()`), since they implictly use SP (for AArch{32,64} at least).


Repository:
  rL LLVM

https://reviews.llvm.org/D45524

Files:
  lib/CodeGen/ShrinkWrap.cpp
  test/CodeGen/Generic/shrink-wrapping-vla.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45524.142027.patch
Type: text/x-patch
Size: 6545 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180411/107a1119/attachment.bin>


More information about the llvm-commits mailing list