From 4cd53531fdaf94786e19e9157744c6092b8de046 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Mon, 8 Sep 2014 20:24:10 +0000 Subject: [PATCH] Fast-ISel: Remove dead code after falling back from selecting call instructions (PR20863) Previously, fast-isel would not clean up after failing to select a call instruction, because it would have called flushLocalValueMap() which moves the insertion point, making SavedInsertPt in selectInstruction() invalid. Fixing this by making SavedInsertPt a member variable, and having flushLocalValueMap() update it. This removes some redundant code at -O0, and more importantly fixes PR20863. Differential Revision: http://reviews.llvm.org/D5249 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217401 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/FastISel.h | 3 +++ lib/CodeGen/SelectionDAG/FastISel.cpp | 25 ++++++++++--------------- test/CodeGen/X86/fast-isel-x86.ll | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/llvm/CodeGen/FastISel.h b/include/llvm/CodeGen/FastISel.h index 561cb89ff4c..1232ec2aec9 100644 --- a/include/llvm/CodeGen/FastISel.h +++ b/include/llvm/CodeGen/FastISel.h @@ -539,6 +539,9 @@ private: /// across heavy instructions like calls. void flushLocalValueMap(); + /// \brief Insertion point before trying to select the current instruction. + MachineBasicBlock::iterator SavedInsertPt; + /// \brief Add a stackmap or patchpoint intrinsic call's live variable /// operands to a stackmap or patchpoint machine instruction. bool addStackMapLiveVars(SmallVectorImpl &Ops, diff --git a/lib/CodeGen/SelectionDAG/FastISel.cpp b/lib/CodeGen/SelectionDAG/FastISel.cpp index 0311140a299..40f387f3ba3 100644 --- a/lib/CodeGen/SelectionDAG/FastISel.cpp +++ b/lib/CodeGen/SelectionDAG/FastISel.cpp @@ -127,6 +127,7 @@ void FastISel::flushLocalValueMap() { LocalValueMap.clear(); LastLocalValue = EmitStartPt; recomputeInsertPt(); + SavedInsertPt = FuncInfo.InsertPt; } bool FastISel::hasTrivialKill(const Value *V) { @@ -1296,7 +1297,7 @@ bool FastISel::selectInstruction(const Instruction *I) { DbgLoc = I->getDebugLoc(); - MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt; + SavedInsertPt = FuncInfo.InsertPt; if (const auto *Call = dyn_cast(I)) { const Function *F = Call->getCalledFunction(); @@ -1322,13 +1323,10 @@ bool FastISel::selectInstruction(const Instruction *I) { DbgLoc = DebugLoc(); return true; } - // Remove dead code. However, ignore call instructions since we've flushed - // the local value map and recomputed the insert point. - if (!isa(I)) { - recomputeInsertPt(); - if (SavedInsertPt != FuncInfo.InsertPt) - removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); - } + // Remove dead code. + recomputeInsertPt(); + if (SavedInsertPt != FuncInfo.InsertPt) + removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); SavedInsertPt = FuncInfo.InsertPt; } // Next, try calling the target to attempt to handle the instruction. @@ -1337,13 +1335,10 @@ bool FastISel::selectInstruction(const Instruction *I) { DbgLoc = DebugLoc(); return true; } - // Remove dead code. However, ignore call instructions since we've flushed - // the local value map and recomputed the insert point. - if (!isa(I)) { - recomputeInsertPt(); - if (SavedInsertPt != FuncInfo.InsertPt) - removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); - } + // Remove dead code. + recomputeInsertPt(); + if (SavedInsertPt != FuncInfo.InsertPt) + removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); DbgLoc = DebugLoc(); // Undo phi node updates, because they will be added again by SelectionDAG. diff --git a/test/CodeGen/X86/fast-isel-x86.ll b/test/CodeGen/X86/fast-isel-x86.ll index a212a7c6876..61e9b98f6e7 100644 --- a/test/CodeGen/X86/fast-isel-x86.ll +++ b/test/CodeGen/X86/fast-isel-x86.ll @@ -60,3 +60,21 @@ entry: ; CHECK: addl $28 } declare fastcc void @test4fastccsret(%struct.a* sret) + + +; Check that fast-isel cleans up when it fails to lower a call instruction. +define void @test5() { +entry: + %call = call i32 @test5dllimport(i32 42) + ret void +; CHECK-LABEL: test5: +; Local value area is still there: +; CHECK: movl $42, {{%[a-z]+}} +; Fast-ISel's arg push is not here: +; CHECK-NOT: movl $42, (%esp) +; SDag-ISel's arg push: +; CHECK: movl %esp, [[REGISTER:%[a-z]+]] +; CHECK: movl $42, ([[REGISTER]]) +; CHECK: movl __imp__test5dllimport +} +declare dllimport i32 @test5dllimport(i32) -- 2.34.1