Index: lib/Transforms/IPO/ArgumentPromotion.cpp =================================================================== --- lib/Transforms/IPO/ArgumentPromotion.cpp +++ lib/Transforms/IPO/ArgumentPromotion.cpp @@ -153,7 +153,19 @@ if (CS.getInstruction()->getParent()->getParent() == F) isSelfRecursive = true; } - + + // Third check: See if this function contains any musttail call sites. If so, + // we can't hack on the calling convention without breaking musttail ABI + // guarantees. + for (auto CR : *CGN) { + Value *Instr = CR.first; + CallInst *CS = dyn_cast_or_null(Instr); + if (!CS) + continue; + if (CS->isMustTailCall()) + return nullptr; + } + // Check to see which arguments are promotable. If an argument is promotable, // add it to ArgsToPromote. SmallPtrSet ArgsToPromote; @@ -829,6 +841,7 @@ CallInst *Call = dyn_cast(U); if (!Call) continue; + assert(!Call->isMustTailCall()); Call->setTailCall(false); } continue; Index: test/Transforms/ArgumentPromotion/tail.ll =================================================================== --- test/Transforms/ArgumentPromotion/tail.ll +++ test/Transforms/ArgumentPromotion/tail.ll @@ -18,3 +18,19 @@ call void @bar(%pair* byval %Data) ret void } + +declare i8* @musttail_inner(%pair* byval) + +; Don't promote arguments of internal functions if they contain musttail calls +; to functions that can't be promoted at the same time. +define internal i8* @musttail_middle(%pair* byval %Data) { +; CHECK: define internal i8* @musttail_middle(%pair* byval %Data) +; CHECK: musttail call i8* @musttail_inner(%pair* byval %Data) + %r = musttail call i8* @musttail_inner(%pair* byval %Data) + ret i8* %r +} + +define i8* @musttail_outer(%pair* byval %Data) { + %r = call i8* @musttail_middle(%pair* byval %Data) + ret i8* %r +}