From f0c3354d998507515ab39e26b5292ea0ceb06aef Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Wed, 19 Dec 2007 21:13:37 +0000 Subject: [PATCH] When inlining through an 'nounwind' call, mark inlined calls 'nounwind'. It is important for correct C++ exception handling that nounwind markings do not get lost, so this transformation is actually needed for correctness. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45218 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Instructions.h | 2 ++ include/llvm/ParameterAttributes.h | 8 +++++ include/llvm/Support/CallSite.h | 1 + lib/Transforms/IPO/PruneEH.cpp | 12 +++---- .../Scalar/InstructionCombining.cpp | 11 ++----- lib/Transforms/Utils/InlineFunction.cpp | 32 ++++++++++++++++--- lib/VMCore/Function.cpp | 26 +++++++++++++++ lib/VMCore/Instructions.cpp | 24 ++++++++++++++ .../Inline/2007-12-19-InlineNoUnwind.ll | 19 +++++++++++ 9 files changed, 114 insertions(+), 21 deletions(-) create mode 100644 test/Transforms/Inline/2007-12-19-InlineNoUnwind.ll diff --git a/include/llvm/Instructions.h b/include/llvm/Instructions.h index cdce9ecdfe8..635a8d67b67 100644 --- a/include/llvm/Instructions.h +++ b/include/llvm/Instructions.h @@ -948,6 +948,7 @@ public: bool doesNotThrow() const { return paramHasAttr(0, ParamAttr::NoUnwind); } + void setDoesNotThrow(bool doesNotThrow = true); /// @brief Determine if the call returns a structure. bool isStructReturn() const { @@ -1752,6 +1753,7 @@ public: bool doesNotThrow() const { return paramHasAttr(0, ParamAttr::NoUnwind); } + void setDoesNotThrow(bool doesNotThrow = true); /// @brief Determine if the call returns a structure. bool isStructReturn() const { diff --git a/include/llvm/ParameterAttributes.h b/include/llvm/ParameterAttributes.h index 29bbd23abc1..e582f74493d 100644 --- a/include/llvm/ParameterAttributes.h +++ b/include/llvm/ParameterAttributes.h @@ -149,6 +149,14 @@ class ParamAttrsList : public FoldingSetNode { static const ParamAttrsList *getModified(const ParamAttrsList *PAL, const ParamAttrsVector &modVec); + /// @brief Add the specified attributes to those in PAL at index idx. + static const ParamAttrsList *includeAttrs(const ParamAttrsList *PAL, + uint16_t idx, uint16_t attrs); + + /// @brief Remove the specified attributes from those in PAL at index idx. + static const ParamAttrsList *excludeAttrs(const ParamAttrsList *PAL, + uint16_t idx, uint16_t attrs); + /// Returns whether each of the specified lists of attributes can be safely /// replaced with the other in a function or a function call. /// @brief Whether one attribute list can safely replace the other. diff --git a/include/llvm/Support/CallSite.h b/include/llvm/Support/CallSite.h index ce8e9fff5e3..3c60daf1a2b 100644 --- a/include/llvm/Support/CallSite.h +++ b/include/llvm/Support/CallSite.h @@ -75,6 +75,7 @@ public: /// @brief Determine if the call cannot unwind. bool doesNotThrow() const; + void setDoesNotThrow(bool doesNotThrow = true); /// getType - Return the type of the instruction that generated this call site /// diff --git a/lib/Transforms/IPO/PruneEH.cpp b/lib/Transforms/IPO/PruneEH.cpp index 69bb1f67c74..529f98e504b 100644 --- a/lib/Transforms/IPO/PruneEH.cpp +++ b/lib/Transforms/IPO/PruneEH.cpp @@ -122,17 +122,15 @@ bool PruneEH::runOnSCC(const std::vector &SCC) { // If the SCC doesn't unwind or doesn't throw, note this fact. if (!SCCMightUnwind || !SCCMightReturn) for (unsigned i = 0, e = SCC.size(); i != e; ++i) { - const ParamAttrsList *PAL = SCC[i]->getFunction()->getParamAttrs(); - uint16_t RAttributes = PAL ? PAL->getParamAttrs(0) : 0; + uint16_t NewAttributes = ParamAttr::None; if (!SCCMightUnwind) - RAttributes |= ParamAttr::NoUnwind; + NewAttributes |= ParamAttr::NoUnwind; if (!SCCMightReturn) - RAttributes |= ParamAttr::NoReturn; + NewAttributes |= ParamAttr::NoReturn; - ParamAttrsVector modVec; - modVec.push_back(ParamAttrsWithIndex::get(0, RAttributes)); - PAL = ParamAttrsList::getModified(PAL, modVec); + const ParamAttrsList *PAL = SCC[i]->getFunction()->getParamAttrs(); + PAL = ParamAttrsList::includeAttrs(PAL, 0, NewAttributes); SCC[i]->getFunction()->setParamAttrs(PAL); } diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 166b4844403..d7c492383f8 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -8026,16 +8026,9 @@ Instruction *InstCombiner::visitCallSite(CallSite CS) { } } - if (isa(Callee) && !CS.paramHasAttr(0, ParamAttr::NoUnwind)) { + if (isa(Callee) && !CS.doesNotThrow()) { // Inline asm calls cannot throw - mark them 'nounwind'. - const ParamAttrsList *PAL = CS.getParamAttrs(); - uint16_t RAttributes = PAL ? PAL->getParamAttrs(0) : 0; - RAttributes |= ParamAttr::NoUnwind; - - ParamAttrsVector modVec; - modVec.push_back(ParamAttrsWithIndex::get(0, RAttributes)); - PAL = ParamAttrsList::getModified(PAL, modVec); - CS.setParamAttrs(PAL); + CS.setDoesNotThrow(); Changed = true; } diff --git a/lib/Transforms/Utils/InlineFunction.cpp b/lib/Transforms/Utils/InlineFunction.cpp index a2b834b5869..dba0e69a355 100644 --- a/lib/Transforms/Utils/InlineFunction.cpp +++ b/lib/Transforms/Utils/InlineFunction.cpp @@ -194,6 +194,10 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) { bool MustClearTailCallFlags = isa(TheCall) && !cast(TheCall)->isTailCall(); + // If the call to the callee cannot throw, set the 'nounwind' flag on any + // calls that we inline. + bool MarkNoUnwind = CS.doesNotThrow(); + BasicBlock *OrigBB = TheCall->getParent(); Function *Caller = OrigBB->getParent(); @@ -207,7 +211,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) { std::vector Returns; ClonedCodeInfo InlinedFunctionInfo; Function::iterator FirstNewBlock; - + { // Scope to destroy ValueMap after cloning. DenseMap ValueMap; @@ -323,15 +327,33 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) { // If we are inlining tail call instruction through a call site that isn't // marked 'tail', we must remove the tail marker for any calls in the inlined - // code. - if (MustClearTailCallFlags && InlinedFunctionInfo.ContainsCalls) { + // code. Also, calls inlined through a 'nounwind' call site should be marked + // 'nounwind'. + if (InlinedFunctionInfo.ContainsCalls && + (MustClearTailCallFlags || MarkNoUnwind)) { for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB) for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) - if (CallInst *CI = dyn_cast(I)) - CI->setTailCall(false); + if (CallInst *CI = dyn_cast(I)) { + if (MustClearTailCallFlags) + CI->setTailCall(false); + if (MarkNoUnwind) + CI->setDoesNotThrow(); + } } + // If we are inlining through a 'nounwind' call site then any inlined 'unwind' + // instructions are unreachable. + if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind) + for (Function::iterator BB = FirstNewBlock, E = Caller->end(); + BB != E; ++BB) { + TerminatorInst *Term = BB->getTerminator(); + if (isa(Term)) { + new UnreachableInst(Term); + BB->getInstList().erase(Term); + } + } + // If we are inlining for an invoke instruction, we must make sure to rewrite // any inlined 'unwind' instructions into branches to the invoke exception // destination, and call instructions into invoke instructions. diff --git a/lib/VMCore/Function.cpp b/lib/VMCore/Function.cpp index f985899b207..37e25added1 100644 --- a/lib/VMCore/Function.cpp +++ b/lib/VMCore/Function.cpp @@ -261,6 +261,32 @@ ParamAttrsList::getModified(const ParamAttrsList *PAL, return get(newVec); } +const ParamAttrsList * +ParamAttrsList::includeAttrs(const ParamAttrsList *PAL, + uint16_t idx, uint16_t attrs) { + uint16_t OldAttrs = PAL ? PAL->getParamAttrs(idx) : 0; + uint16_t NewAttrs = OldAttrs | attrs; + if (NewAttrs == OldAttrs) + return PAL; + + ParamAttrsVector modVec; + modVec.push_back(ParamAttrsWithIndex::get(idx, NewAttrs)); + return getModified(PAL, modVec); +} + +const ParamAttrsList * +ParamAttrsList::excludeAttrs(const ParamAttrsList *PAL, + uint16_t idx, uint16_t attrs) { + uint16_t OldAttrs = PAL ? PAL->getParamAttrs(idx) : 0; + uint16_t NewAttrs = OldAttrs & ~attrs; + if (NewAttrs == OldAttrs) + return PAL; + + ParamAttrsVector modVec; + modVec.push_back(ParamAttrsWithIndex::get(idx, NewAttrs)); + return getModified(PAL, modVec); +} + ParamAttrsList::~ParamAttrsList() { ParamAttrsLists->RemoveNode(this); } diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 3531bad2e49..b76b11d4f63 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -71,6 +71,12 @@ bool CallSite::doesNotThrow() const { else return cast(I)->doesNotThrow(); } +void CallSite::setDoesNotThrow(bool doesNotThrow) { + if (CallInst *CI = dyn_cast(I)) + CI->setDoesNotThrow(doesNotThrow); + else + cast(I)->setDoesNotThrow(doesNotThrow); +} //===----------------------------------------------------------------------===// // TerminatorInst Class @@ -405,6 +411,15 @@ bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const { return false; } +void CallInst::setDoesNotThrow(bool doesNotThrow) { + const ParamAttrsList *PAL = getParamAttrs(); + if (doesNotThrow) + PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind); + else + PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind); + setParamAttrs(PAL); +} + //===----------------------------------------------------------------------===// // InvokeInst Implementation @@ -483,6 +498,15 @@ bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const { return false; } +void InvokeInst::setDoesNotThrow(bool doesNotThrow) { + const ParamAttrsList *PAL = getParamAttrs(); + if (doesNotThrow) + PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind); + else + PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind); + setParamAttrs(PAL); +} + //===----------------------------------------------------------------------===// // ReturnInst Implementation diff --git a/test/Transforms/Inline/2007-12-19-InlineNoUnwind.ll b/test/Transforms/Inline/2007-12-19-InlineNoUnwind.ll new file mode 100644 index 00000000000..6264f8f01c3 --- /dev/null +++ b/test/Transforms/Inline/2007-12-19-InlineNoUnwind.ll @@ -0,0 +1,19 @@ +; RUN: llvm-as < %s -o - | opt -inline | llvm-dis | grep nounwind +; RUN: llvm-as < %s -o - | opt -inline | llvm-dis | grep unreachable + +declare i1 @extern() + +define internal i32 @test() { +entry: + %n = call i1 @extern( ) + br i1 %n, label %r, label %u +r: + ret i32 0; +u: + unwind +} + +define i32 @caller() { + %X = call i32 @test( ) nounwind + ret i32 %X +}