[PATCH] Teach the inliner how to preserve musttail invariants

Chandler Carruth chandlerc at gmail.com
Thu May 15 13:34:35 PDT 2014

On Thu, May 15, 2014 at 2:13 PM, Reid Kleckner <rnk at google.com> wrote:

> Chandler Carruth wrote:
> > It's really surprising to me that this applies to *all* dynamic allocas
> rather than just to inalloca arguments... Maybe I'm just missing the point,
> but I'd appreciate a comment clarifying why this makes sense.
> >
> > Is there a test case that covers this? I can't tell if some of the
> CHECK-NEXT ones actually cover this case.
> I'm not really following.

It's me that isn't following. Any comment like this means I've gotten
confused and am trying to understand where. =]

> Here's what could happen when inlining this fragment:
>   %a = alloca i8, i32 %n
>   call void @use_a(i8* %a)
>   musttail call void @bar()
>   ret void
> Without this change, after inlining, we get this:
>   %sp = call i8* @llvm.stacksave()
>   %a = alloca i8, i32 %n
>   call void @use_a(i8* %a)
>   musttail call void @bar()
>   call void @llvm.stackrestore(i8*)
>   ret void
> First, this fails musttail verification, because now the musttail call
> isn't in tail position.

Is there an example that is actually valid w.r.t. musttail? I have to
imagine so.

> The stackrestore is completely redundant with the ret.  Unlike other rets,
> this ret is going to remain in the inlined function.  It isn't removed
> later and merged back into the normal control flow.

AHHH, right. I get it now. Because we go from tail -> tail, we're *still*
at the end of the function and stackrestore is redundant.

> Would it make more sense to reframe the comment to talk about the fact
> that this ret isn't going a way and stackrestore before ret is pointless?

This actually makes more sense to me. I would even say, the ret *is* going
away, but we're still in a tail position for the outer function which means
that the control flow is getting merged back in.... to an immediate ret
because its actually a tail call.

>  The way I think about it is that we just did a musttail call which
> already did the work of clearing the stack, so there's no reason to restore
> the stack.

Right. I can see why you would say that a tail call "clears the stack" but
i always think of it as a tail call only happens when we don't *need* to
clear the stack.... but I completely see why that's not actually the right
way to think about it. Maybe just explain a touch of both? Maybe this
discussion is all that is needed to educate me. =D

Thanks for the example and pointing out what is actually happening here, it
helped me a lot. =]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140515/9bd5b584/attachment.html>

More information about the llvm-commits mailing list