In the function "Analysis.cpp:isInTailCallPosition", it only checks whether a call is in a tail call position if the call has side effects, access memory or it is not safe to speculative execute. Therefore, a speculatable function will not go through tail call position check and improperly tail called when it is not in a tail-call position. An example as below:
IR reproducer
define dso_local void @caller(double* nocapture %res, double %a) local_unnamed_addr #0 {
entry:
%call = tail call double @callee(double %a) #2
store double %call, double* %res, align 8
ret void
}
define double @callee(double) local_unnamed_addr #1 {
ret double 4.5
}
attributes #0 = { nounwind }
attributes #1 = { readnone speculatable }
attributes #2 = { nounwind noinline }produces a tail call in the caller without storing the result, which can be seen in the initial SDAG below
llc < reproducer.ll -debug-only=isel -mtriple=x86_64
=== caller
Initial selection DAG: %bb.0 'caller:entry'
SelectionDAG has 12 nodes:
t0: ch = EntryToken
t2: i64,ch = CopyFromReg t0, Register:i64 %0
t5: i64 = GlobalAddress<double (double)* @callee> 0
t4: f64,ch = CopyFromReg t0, Register:f64 %1
t7: ch,glue = CopyToReg t0, Register:f64 $xmm0, t4
t11: ch,glue = X86ISD::TC_RETURN t7, TargetGlobalAddress:i64<double (double)* @callee> 0, Constant:i32<0>, Register:f64 $xmm0, RegisterMask:Untyped, t7:1This patch enables tail call position check for speculatable functions.
Please add a FIXME explaining this call could be moved to make it a tail call.