Skip to content

Commit 20d634f

Browse files
committed
address comments
1 parent 1e26120 commit 20d634f

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

llvm/lib/Target/Mips/Mips16ISelLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace llvm {
3333
private:
3434
bool isEligibleForTailCallOptimization(
3535
const CCState &CCInfo, unsigned NextStackOffset,
36-
const MipsFunctionInfo &FI, bool IsMustTail = false) const override;
36+
const MipsFunctionInfo &FI, bool IsMustTail) const override;
3737

3838
void setMips16HardFloatLibCalls();
3939

llvm/lib/Target/Mips/MipsISelLowering.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,19 +3404,20 @@ MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
34043404
if (MF.getTarget().Options.EmitCallGraphSection && CB && CB->isIndirectCall())
34053405
CSInfo = MachineFunction::CallSiteInfo(*CB);
34063406

3407-
// Check if it's really possible to do a tail call. Restrict it to functions
3408-
// that are part of this compilation unit.
3407+
// Check if it's really possible to do a tail call.
3408+
// For non-musttail calls, restrict to functions that won't require $gp
3409+
// restoration. In PIC mode, calling external functions via tail call can
3410+
// cause issues with $gp register handling (see D24763).
34093411
bool InternalLinkage = false;
34103412
bool IsMustTail = CLI.CB && CLI.CB->isMustTailCall();
34113413
if (IsTailCall) {
34123414
IsTailCall = isEligibleForTailCallOptimization(
34133415
CCInfo, StackSize, *MF.getInfo<MipsFunctionInfo>(), IsMustTail);
3414-
if (IsTailCall) {
3416+
// For non-musttail calls, restrict to local or non-interposable functions
3417+
if (IsTailCall && !IsMustTail) {
34153418
if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
34163419
InternalLinkage = G->getGlobal()->hasInternalLinkage();
3417-
IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() ||
3418-
G->getGlobal()->isDSOLocal() ||
3419-
G->getGlobal()->hasPrivateLinkage() ||
3420+
IsTailCall &= (G->getGlobal()->hasLocalLinkage() ||
34203421
G->getGlobal()->hasHiddenVisibility() ||
34213422
G->getGlobal()->hasProtectedVisibility());
34223423
}

llvm/lib/Target/Mips/MipsISelLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ class TargetRegisterClass;
597597
/// for tail call optimization.
598598
virtual bool isEligibleForTailCallOptimization(
599599
const CCState &CCInfo, unsigned NextStackOffset,
600-
const MipsFunctionInfo &FI, bool IsMustTail = false) const = 0;
600+
const MipsFunctionInfo &FI, bool IsMustTail) const = 0;
601601

602602
/// copyByValArg - Copy argument registers which were used to pass a byval
603603
/// argument to the stack. Create a stack frame object for the byval

llvm/lib/Target/Mips/MipsSEISelLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class TargetRegisterClass;
6565
private:
6666
bool isEligibleForTailCallOptimization(
6767
const CCState &CCInfo, unsigned NextStackOffset,
68-
const MipsFunctionInfo &FI, bool IsMustTail = false) const override;
68+
const MipsFunctionInfo &FI, bool IsMustTail) const override;
6969

7070
void
7171
getOpndList(SmallVectorImpl<SDValue> &Ops,

llvm/test/CodeGen/Mips/musttail.ll

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,77 @@ define i32 @test_musttail_args(i32 %x, i32 %y, i32 %z) {
2121
%ret = musttail call i32 @callee_args(i32 %x, i32 %y, i32 %z)
2222
ret i32 %ret
2323
}
24+
25+
; Test musttail with many arguments that spill to stack (involves memory)
26+
; MIPS O32 ABI: first 4 args in $a0-$a3, rest on stack
27+
declare i32 @many_args_callee(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g, i32 %h)
28+
29+
define i32 @test_musttail_many_args(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g, i32 %h) {
30+
; MIPS32-LABEL: test_musttail_many_args:
31+
; MIPS32: # %bb.0:
32+
; MIPS32-NEXT: lw $1, 24($sp)
33+
; MIPS32-NEXT: lw $2, 20($sp)
34+
; MIPS32-NEXT: lw $3, 16($sp)
35+
; MIPS32-NEXT: sw $3, 16($sp)
36+
; MIPS32-NEXT: sw $2, 20($sp)
37+
; MIPS32-NEXT: sw $1, 24($sp)
38+
; MIPS32-NEXT: lw $1, 28($sp)
39+
; MIPS32-NEXT: j many_args_callee
40+
; MIPS32-NEXT: sw $1, 28($sp)
41+
;
42+
; MIPS64-LABEL: test_musttail_many_args:
43+
; MIPS64: # %bb.0:
44+
; MIPS64-NEXT: j many_args_callee
45+
; MIPS64-NEXT: nop
46+
%ret = musttail call i32 @many_args_callee(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g, i32 %h)
47+
ret i32 %ret
48+
}
49+
50+
; Test musttail with large struct passed by value (involves memory)
51+
%struct.large = type { i32, i32, i32, i32, i32, i32, i32, i32 }
52+
53+
declare i32 @callee_with_struct(%struct.large %s, i32 %x)
54+
55+
define i32 @test_musttail_struct(%struct.large %s, i32 %x) {
56+
; MIPS32-LABEL: test_musttail_struct:
57+
; MIPS32: # %bb.0:
58+
; MIPS32-NEXT: lw $1, 28($sp)
59+
; MIPS32-NEXT: lw $2, 24($sp)
60+
; MIPS32-NEXT: lw $3, 20($sp)
61+
; MIPS32-NEXT: lw $8, 16($sp)
62+
; MIPS32-NEXT: sw $8, 16($sp)
63+
; MIPS32-NEXT: sw $3, 20($sp)
64+
; MIPS32-NEXT: sw $2, 24($sp)
65+
; MIPS32-NEXT: sw $1, 28($sp)
66+
; MIPS32-NEXT: lw $1, 32($sp)
67+
; MIPS32-NEXT: j callee_with_struct
68+
; MIPS32-NEXT: sw $1, 32($sp)
69+
;
70+
; MIPS64-LABEL: test_musttail_struct:
71+
; MIPS64: # %bb.0:
72+
; MIPS64-NEXT: ld $1, 0($sp)
73+
; MIPS64-NEXT: j callee_with_struct
74+
; MIPS64-NEXT: sd $1, 0($sp)
75+
%ret = musttail call i32 @callee_with_struct(%struct.large %s, i32 %x)
76+
ret i32 %ret
77+
}
78+
79+
; Test musttail with mixed int and float arguments that use stack
80+
declare float @mixed_args_callee(i32 %a, float %b, i32 %c, float %d, i32 %e, float %f)
81+
82+
define float @test_musttail_mixed_args(i32 %a, float %b, i32 %c, float %d, i32 %e, float %f) {
83+
; MIPS32-LABEL: test_musttail_mixed_args:
84+
; MIPS32: # %bb.0:
85+
; MIPS32-NEXT: lw $1, 16($sp)
86+
; MIPS32-NEXT: sw $1, 16($sp)
87+
; MIPS32-NEXT: lwc1 $f0, 20($sp)
88+
; MIPS32-NEXT: j mixed_args_callee
89+
; MIPS32-NEXT: swc1 $f0, 20($sp)
90+
;
91+
; MIPS64-LABEL: test_musttail_mixed_args:
92+
; MIPS64: # %bb.0:
93+
; MIPS64-NEXT: j mixed_args_callee
94+
; MIPS64-NEXT: nop
95+
%ret = musttail call float @mixed_args_callee(i32 %a, float %b, i32 %c, float %d, i32 %e, float %f)
96+
ret float %ret
97+
}

0 commit comments

Comments
 (0)