Skip to content

Commit 69b042c

Browse files
committed
[ARM] Glue register copies to tail calls.
This generally follows what other targets do. I don't completely understand why the special case for tail calls existed in the first place; even when the code was committed in r105413, call lowering didn't work in the way described in the comments. Stack protector lowering breaks if the register copies are not glued to a tail call: we have to insert the stack protector check before the tail call, and we choose the location based on the assumption that all physical register dependencies of a tail call are adjacent to the tail call. (See FindSplitPointForStackProtector.) This is sort of fragile, but I don't see any reason to break that assumption. I'm guessing nobody has seen this before just because it's hard to convince the scheduler to actually schedule the code in a way that breaks; even without the glue, the only computation that could actually be scheduled after the register copies is the computation of the call address, and the scheduler usually prefers to schedule that before the copies anyway. Fixes https://bugs.llvm.org/show_bug.cgi?id=41417 Differential Revision: https://reviews.llvm.org/D60427 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360099 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent fb9e306 commit 69b042c

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,32 +1988,10 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
19881988
// Build a sequence of copy-to-reg nodes chained together with token chain
19891989
// and flag operands which copy the outgoing args into the appropriate regs.
19901990
SDValue InFlag;
1991-
// Tail call byval lowering might overwrite argument registers so in case of
1992-
// tail call optimization the copies to registers are lowered later.
1993-
if (!isTailCall)
1994-
for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1995-
Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1996-
RegsToPass[i].second, InFlag);
1997-
InFlag = Chain.getValue(1);
1998-
}
1999-
2000-
// For tail calls lower the arguments to the 'real' stack slot.
2001-
if (isTailCall) {
2002-
// Force all the incoming stack arguments to be loaded from the stack
2003-
// before any new outgoing arguments are stored to the stack, because the
2004-
// outgoing stack slots may alias the incoming argument stack slots, and
2005-
// the alias isn't otherwise explicit. This is slightly more conservative
2006-
// than necessary, because it means that each store effectively depends
2007-
// on every argument instead of just those arguments it would clobber.
2008-
2009-
// Do not flag preceding copytoreg stuff together with the following stuff.
2010-
InFlag = SDValue();
2011-
for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2012-
Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2013-
RegsToPass[i].second, InFlag);
2014-
InFlag = Chain.getValue(1);
2015-
}
2016-
InFlag = SDValue();
1991+
for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1992+
Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1993+
RegsToPass[i].second, InFlag);
1994+
InFlag = Chain.getValue(1);
20171995
}
20181996

20191997
// If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; RUN: llc < %s | FileCheck %s
2+
target triple = "armv6kz-unknown-unknown-gnueabihf"
3+
4+
; Make sure this doesn't crash, and we actually emit a tail call.
5+
; Unfortunately, this test is sort of fragile... the original issue only
6+
; shows up if scheduling happens in a very specific order. But including
7+
; it anyway just to demonstrate the issue.
8+
; CHECK: pop {r4, lr}
9+
10+
@e = external local_unnamed_addr constant [0 x i32 (i32, i32)*], align 4
11+
12+
; Function Attrs: nounwind sspstrong
13+
define i32 @AVI_ChunkRead_p_chk(i32 %g) nounwind sspstrong "target-cpu"="arm1176jzf-s" {
14+
entry:
15+
%b = alloca i8, align 1
16+
%tobool = icmp eq i32 %g, 0
17+
br i1 %tobool, label %if.end, label %if.then
18+
19+
if.then: ; preds = %entry
20+
%add = add nsw i32 %g, 1
21+
%arrayidx = getelementptr inbounds [0 x i32 (i32, i32)*], [0 x i32 (i32, i32)*]* @e, i32 0, i32 %add
22+
%0 = load i32 (i32, i32)*, i32 (i32, i32)** %arrayidx, align 4
23+
%call = tail call i32 %0(i32 0, i32 0) #3
24+
br label %return
25+
26+
if.end: ; preds = %entry
27+
call void @c(i8* nonnull %b)
28+
br label %return
29+
30+
return: ; preds = %if.end, %if.then
31+
%retval.0 = phi i32 [ %call, %if.then ], [ 0, %if.end ]
32+
ret i32 %retval.0
33+
}
34+
35+
declare void @c(i8*)

0 commit comments

Comments
 (0)