From f19f6bb31e45c5fc34b17d89d80ed938f61ff047 Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Wed, 13 Jun 2007 05:51:31 +0000 Subject: [PATCH] The fix that was applied for PR1224 stops the compiler crashing but breaks exception handling. The problem described in PR1224 is that invoke is a terminator that can produce a value. The value may be needed in other blocks. The code that writes to registers values needed in other blocks runs before terminators are lowered (in this case invoke) so asserted because the value was not yet available. The fix that was applied was to do invoke lowering earlier, before writing values to registers. The problem this causes is that the code to copy values to registers can be output after the invoke call. If an exception is raised and control is passed to the landing pad then this copy-code will never execute. If the value is needed in some code path reached via the landing pad then that code will get something bogus. So revert the original fix and simply skip invoke values in the general copying to registers code. Instead copy the invoke value to a register in the invoke lowering code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37567 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 56 ++++++++----------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index f97ee721515..72daa66bd57 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -566,7 +566,6 @@ public: // These all get lowered before this pass. void visitInvoke(InvokeInst &I); - void visitInvoke(InvokeInst &I, bool AsTerminator); void visitUnwind(UnwindInst &I); void visitScalarBinary(User &I, unsigned OpCode); @@ -1332,29 +1331,31 @@ void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB, } void SelectionDAGLowering::visitInvoke(InvokeInst &I) { - assert(0 && "Should never be visited directly"); -} -void SelectionDAGLowering::visitInvoke(InvokeInst &I, bool AsTerminator) { // Retrieve successors. MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; + MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; - if (!AsTerminator) { - MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)]; + LowerCallTo(I, I.getCalledValue()->getType(), + I.getCallingConv(), + false, + getValue(I.getOperand(0)), + 3, LandingPad); - LowerCallTo(I, I.getCalledValue()->getType(), - I.getCallingConv(), - false, - getValue(I.getOperand(0)), - 3, LandingPad); - - // Update successor info - CurMBB->addSuccessor(Return); - CurMBB->addSuccessor(LandingPad); - } else { - // Drop into normal successor. - DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), - DAG.getBasicBlock(Return))); + // If the value of the invoke is used outside of its defining block, make it + // available as a virtual register. + if (!I.use_empty()) { + DenseMap::iterator VMI = FuncInfo.ValueMap.find(&I); + if (VMI != FuncInfo.ValueMap.end()) + DAG.setRoot(CopyValueToVirtualRegister(&I, VMI->second)); } + + // Drop into normal successor. + DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), + DAG.getBasicBlock(Return))); + + // Update successor info + CurMBB->addSuccessor(Return); + CurMBB->addSuccessor(LandingPad); } void SelectionDAGLowering::visitUnwind(UnwindInst &I) { @@ -4546,15 +4547,11 @@ void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I) SDL.visit(*I); - - // Lower call part of invoke. - InvokeInst *Invoke = dyn_cast(LLVMBB->getTerminator()); - if (Invoke) SDL.visitInvoke(*Invoke, false); - + // Ensure that all instructions which are used outside of their defining - // blocks are available as virtual registers. + // blocks are available as virtual registers. Invoke is handled elsewhere. for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) - if (!I->use_empty() && !isa(I)) { + if (!I->use_empty() && !isa(I) && !isa(I)) { DenseMap::iterator VMI =FuncInfo.ValueMap.find(I); if (VMI != FuncInfo.ValueMap.end()) UnorderedChains.push_back( @@ -4662,12 +4659,7 @@ void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, } // Lower the terminator after the copies are emitted. - if (Invoke) { - // Just the branch part of invoke. - SDL.visitInvoke(*Invoke, true); - } else { - SDL.visit(*LLVMBB->getTerminator()); - } + SDL.visit(*LLVMBB->getTerminator()); // Copy over any CaseBlock records that may now exist due to SwitchInst // lowering, as well as any jump table information.