Index: llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp =================================================================== --- llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp +++ llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp @@ -460,6 +460,13 @@ SmallVector ArgumentPHIs; bool RemovableCallsMustBeMarkedTail = false; + // These variables are also populated by createTailRecurseLoopHeader, and + // allow us to keep track of the return value in the case we return something + // other than the call we're removing. + PHINode *RetPN = nullptr; + PHINode *RetKnownPN = nullptr; + SmallVector RetSelects; + TailRecursionEliminator(Function &F, const TargetTransformInfo *TTI, AliasAnalysis *AA, OptimizationRemarkEmitter *ORE, DomTreeUpdater &DTU) @@ -577,6 +584,24 @@ PN->addIncoming(&*I, NewEntry); ArgumentPHIs.push_back(PN); } + + // If the function doen't return void, create a PHI node to store the return + // value, and create another PHI node to track whether we know the return + // value. + Type *RetType = F.getReturnType(); + if (!RetType->isVoidTy()) { + RetPN = PHINode::Create(RetType, 2, "ret.tr", &HeaderBB->front()); + + // Populate the return value PHI node with a dummy value to start since we + // can't know our return value at function entry. + RetPN->addIncoming(Constant::getNullValue(RetType), NewEntry); + + RetKnownPN = PHINode::Create(Type::getInt1Ty(F.getContext()), 2, + "ret.known.tr", InsertPos); + RetKnownPN->addIncoming(ConstantInt::getFalse(RetKnownPN->getType()), + NewEntry); + } + // The entry block was changed from HeaderBB to NewEntry. // The forward DominatorTree needs to be recalculated when the EntryBB is // changed. In this corner-case we recalculate the entire tree. @@ -616,11 +641,7 @@ // value for the accumulator is placed in this variable. If this value is set // then we actually perform accumulator recursion elimination instead of // simple tail recursion elimination. If the operation is an LLVM instruction - // (eg: "add") then it is recorded in AccumulatorRecursionInstr. If not, then - // we are handling the case when the return instruction returns a constant C - // which is different to the constant returned by other return instructions - // (which is recorded in AccumulatorRecursionEliminationInitVal). This is a - // special case of accumulator recursion, the operation being "return C". + // (eg: "add") then it is recorded in AccumulatorRecursionInstr. Value *AccumulatorRecursionEliminationInitVal = nullptr; Instruction *AccumulatorRecursionInstr = nullptr; @@ -647,26 +668,6 @@ } } - // We can only transform call/return pairs that either ignore the return value - // of the call and return void, ignore the value of the call and return a - // constant, return the value returned by the tail call, or that are being - // accumulator recursion variable eliminated. - if (Ret->getNumOperands() == 1 && Ret->getReturnValue() != CI && - !isa(Ret->getReturnValue()) && - AccumulatorRecursionEliminationInitVal == nullptr && - !getCommonReturnValue(nullptr, CI)) { - // One case remains that we are able to handle: the current return - // instruction returns a constant, and all other return instructions - // return a different constant. - if (!isDynamicConstant(Ret->getReturnValue(), CI, Ret)) - return false; // Current return instruction does not return a constant. - // Check that all other return instructions return a common constant. If - // so, record it in AccumulatorRecursionEliminationInitVal. - AccumulatorRecursionEliminationInitVal = getCommonReturnValue(Ret, CI); - if (!AccumulatorRecursionEliminationInitVal) - return false; - } - BasicBlock *BB = Ret->getParent(); using namespace ore; @@ -698,20 +699,15 @@ PHINode *AccPN = insertAccumulator(AccumulatorRecursionEliminationInitVal); Instruction *AccRecInstr = AccumulatorRecursionInstr; - if (AccRecInstr) { - // Add an incoming argument for the current block, which is computed by - // our associative and commutative accumulator instruction. - AccPN->addIncoming(AccRecInstr, BB); - - // Next, rewrite the accumulator recursion instruction so that it does not - // use the result of the call anymore, instead, use the PHI node we just - // inserted. - AccRecInstr->setOperand(AccRecInstr->getOperand(0) != CI, AccPN); - } else { - // Add an incoming argument for the current block, which is just the - // constant returned by the current return instruction. - AccPN->addIncoming(Ret->getReturnValue(), BB); - } + + // Add an incoming argument for the current block, which is computed by + // our associative and commutative accumulator instruction. + AccPN->addIncoming(AccRecInstr, BB); + + // Next, rewrite the accumulator recursion instruction so that it does not + // use the result of the call anymore, instead, use the PHI node we just + // inserted. + AccRecInstr->setOperand(AccRecInstr->getOperand(0) != CI, AccPN); // Finally, rewrite any return instructions in the program to return the PHI // node instead of the "initval" that they do currently. This loop will @@ -722,6 +718,25 @@ ++NumAccumAdded; } + // Update our return value tracking + if (RetPN) { + if (Ret->getReturnValue() == CI || AccumulatorRecursionEliminationInitVal) { + // Defer selecting a return value + RetPN->addIncoming(RetPN, BB); + RetKnownPN->addIncoming(RetKnownPN, BB); + } else { + // We found a return value we want to use, insert a select instruction to + // select it if we don't already know what our return value will be and + // store the result in our return value PHI node. + SelectInst *SI = SelectInst::Create( + RetKnownPN, RetPN, Ret->getReturnValue(), "current.ret.tr", Ret); + RetSelects.push_back(SI); + + RetPN->addIncoming(SI, BB); + RetKnownPN->addIncoming(ConstantInt::getTrue(RetKnownPN->getType()), BB); + } + } + // Now that all of the PHI nodes are in place, remove the call and // ret instructions, replacing them with an unconditional branch. BranchInst *NewBI = BranchInst::Create(HeaderBB, Ret); @@ -804,6 +819,35 @@ PN->eraseFromParent(); } } + + if (RetPN) { + if (RetSelects.empty()) { + // If we didn't insert any select instructions, then we know we didn't + // try to store a return value and we can remove the PHI nodes we + // inserted. It doesn't seem like it should be necessary to replace the + // uses of these PHI nodes, since they can only be used by themselves, + // but we hit an assertion that they still have uses if we don't. + BasicBlock *Entry = &F.getEntryBlock(); + RetPN->replaceAllUsesWith(RetPN->getIncomingValueForBlock(Entry)); + RetKnownPN->replaceAllUsesWith( + RetKnownPN->getIncomingValueForBlock(Entry)); + + RetPN->eraseFromParent(); + RetKnownPN->eraseFromParent(); + } else { + // We need to insert a select instruction before any return left in the + // function to select our stored return value if we have one. + for (BasicBlock &BB : F) { + ReturnInst *RI = dyn_cast(BB.getTerminator()); + if (!RI) + continue; + + SelectInst *SI = SelectInst::Create( + RetKnownPN, RetPN, RI->getOperand(0), "current.ret.tr", RI); + RI->setOperand(0, SI); + } + } + } } bool TailRecursionEliminator::eliminate(Function &F, Index: llvm/test/Transforms/TailCallElim/2010-06-26-MultipleReturnValues.ll =================================================================== --- llvm/test/Transforms/TailCallElim/2010-06-26-MultipleReturnValues.ll +++ llvm/test/Transforms/TailCallElim/2010-06-26-MultipleReturnValues.ll @@ -1,20 +1,58 @@ ; RUN: opt < %s -tailcallelim -verify-dom-info -S | FileCheck %s ; PR7328 ; PR7506 -define i32 @foo(i32 %x) { -; CHECK-LABEL: define i32 @foo( -; CHECK: %accumulator.tr = phi i32 [ 1, %entry ], [ 0, %body ] +define i32 @test1_constants(i32 %x) { entry: %cond = icmp ugt i32 %x, 0 ; [#uses=1] br i1 %cond, label %return, label %body body: ; preds = %entry %y = add i32 %x, 1 ; [#uses=1] - %tmp = call i32 @foo(i32 %y) ; [#uses=0] -; CHECK-NOT: call + %tmp = call i32 @test1_constants(i32 %y) ; [#uses=0] ret i32 0 -; CHECK: ret i32 %accumulator.tr return: ; preds = %entry ret i32 1 } + +; CHECK-LABEL: define i32 @test1_constants( +; CHECK: tailrecurse: +; CHECK: %ret.tr = phi i32 [ 0, %entry ], [ %current.ret.tr, %body ] +; CHECK: %ret.known.tr = phi i1 [ false, %entry ], [ true, %body ] +; CHECK: body: +; CHECK-NOT: call +; CHECK: %current.ret.tr = select i1 %ret.known.tr, i32 %ret.tr, i32 0 +; CHECK-NOT: ret +; CHECK: return: +; CHECK: %current.ret.tr1 = select i1 %ret.known.tr, i32 %ret.tr, i32 1 +; CHECK: ret i32 %current.ret.tr1 + +define i32 @test2_non_constants(i32 %x) { +entry: + %cond = icmp ugt i32 %x, 0 + br i1 %cond, label %return, label %body + +body: + %y = add i32 %x, 1 + %helper.0 = call i32 @test2_helper() + %tmp = call i32 @test2_non_constants(i32 %y) + ret i32 %helper.0 + +return: + %helper.1 = call i32 @test2_helper() + ret i32 %helper.1 +} + +declare i32 @test2_helper() + +; CHECK-LABEL: define i32 @test2_non_constants( +; CHECK: tailrecurse: +; CHECK: %ret.tr = phi i32 [ 0, %entry ], [ %current.ret.tr, %body ] +; CHECK: %ret.known.tr = phi i1 [ false, %entry ], [ true, %body ] +; CHECK: body: +; CHECK-NOT: call i32 @test2_non_constants(i32 %y) +; CHECK: %current.ret.tr = select i1 %ret.known.tr, i32 %ret.tr, i32 %helper.0 +; CHECK-NOT: ret +; CHECK: return: +; CHECK: %current.ret.tr1 = select i1 %ret.known.tr, i32 %ret.tr, i32 %helper.1 +; CHECK: ret i32 %current.ret.tr1 Index: llvm/test/Transforms/TailCallElim/basic.ll =================================================================== --- llvm/test/Transforms/TailCallElim/basic.ll +++ llvm/test/Transforms/TailCallElim/basic.ll @@ -46,8 +46,16 @@ ; plunked it into the demo script, so maybe they care about it. define i32 @test3(i32 %c) { ; CHECK: i32 @test3 +; CHECK: tailrecurse: +; CHECK: %ret.tr = phi i32 [ 0, %entry ], [ %current.ret.tr, %else ] +; CHECK: %ret.known.tr = phi i1 [ false, %entry ], [ true, %else ] +; CHECK: else: ; CHECK-NOT: call -; CHECK: ret i32 0 +; CHECK: %current.ret.tr = select i1 %ret.known.tr, i32 %ret.tr, i32 0 +; CHECK-NOT: ret +; CHECK: return: +; CHECK: %current.ret.tr1 = select i1 %ret.known.tr, i32 %ret.tr, i32 0 +; CHECK: ret i32 %current.ret.tr1 entry: %tmp.1 = icmp eq i32 %c, 0 ; [#uses=1] br i1 %tmp.1, label %return, label %else