Index: lib/Transforms/IPO/ArgumentPromotion.cpp =================================================================== --- lib/Transforms/IPO/ArgumentPromotion.cpp +++ lib/Transforms/IPO/ArgumentPromotion.cpp @@ -145,7 +145,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; Index: test/Transforms/ArgumentPromotion/tail.ll =================================================================== --- test/Transforms/ArgumentPromotion/tail.ll +++ test/Transforms/ArgumentPromotion/tail.ll @@ -18,3 +18,18 @@ call void @bar(%pair* byval %Data) ret void } + +; Don't fire on musttail calls, since the calling convention can't be changed. +declare i8* @musttail_inner(%pair* byval) + +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 +}