[PATCH] D11768: FastISel fails to remove dead code

Wolfgang Pieb via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 29 14:45:57 PDT 2015


wolfgangp updated this revision to Diff 36039.
wolfgangp added a comment.

Incorporated feedback by Hans. Sorry for the delay.


http://reviews.llvm.org/D11768

Files:
  lib/CodeGen/SelectionDAG/FastISel.cpp
  test/CodeGen/X86/fast-isel-deadcode.ll

Index: test/CodeGen/X86/fast-isel-deadcode.ll
===================================================================
--- test/CodeGen/X86/fast-isel-deadcode.ll
+++ test/CodeGen/X86/fast-isel-deadcode.ll
@@ -0,0 +1,46 @@
+; RUN: llc < %s -O0 -march=x86 | FileCheck %s
+;
+; Generated from
+;
+; extern bool bar (long double);
+; bool foo(long double x, long double y)
+; {
+;   return (x == y) || (bar(x));
+; }
+;
+; ModuleID = 'test.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define zeroext i1 @foo(x86_fp80 %x, x86_fp80 %y) {
+entry:
+  %x.addr = alloca x86_fp80, align 16
+  %y.addr = alloca x86_fp80, align 16
+  store x86_fp80 %x, x86_fp80* %x.addr, align 16
+  store x86_fp80 %y, x86_fp80* %y.addr, align 16
+  %0 = load x86_fp80, x86_fp80* %x.addr, align 16
+  %1 = load x86_fp80, x86_fp80* %y.addr, align 16
+  %cmp = fcmp oeq x86_fp80 %0, %1
+
+; Make sure that there is no dead code generated
+; from Fast-ISel Phi-node handling. We should only
+; see one movb of the constant 1, feeding the PHI
+; node in lor.end
+; CHECK-LABEL: foo:
+; CHECK: movb $1
+; CHECK-NOT: movb $1
+; CHECK-LABEL: .LBB0_1:
+
+  br i1 %cmp, label %lor.end, label %lor.rhs
+
+lor.rhs:                                          ; preds = %entry
+  %2 = load x86_fp80, x86_fp80* %x.addr, align 16
+  %call = call zeroext i1 @bar(x86_fp80 %2)
+  br label %lor.end
+
+lor.end:                                          ; preds = %lor.rhs, %entry
+  %3 = phi i1 [ true, %entry ], [ %call, %lor.rhs ]
+  ret i1 %3
+}
+
+declare zeroext i1 @bar(x86_fp80)
Index: lib/CodeGen/SelectionDAG/FastISel.cpp
===================================================================
--- lib/CodeGen/SelectionDAG/FastISel.cpp
+++ lib/CodeGen/SelectionDAG/FastISel.cpp
@@ -1330,6 +1330,7 @@
 }
 
 bool FastISel::selectInstruction(const Instruction *I) {
+  MachineInstr *SavedLastLocalValue = getLastLocalValue();
   // Just before the terminator instruction, insert instructions to
   // feed PHI nodes in successor blocks.
   if (isa<TerminatorInst>(I))
@@ -1383,8 +1384,21 @@
 
   DbgLoc = DebugLoc();
   // Undo phi node updates, because they will be added again by SelectionDAG.
-  if (isa<TerminatorInst>(I))
+  if (isa<TerminatorInst>(I)) {
+    // PHI node handling may have generated some local value instructions.
+    // Remove them like other dead code.
+    MachineInstr *CurLastLocalValue = getLastLocalValue();
+    if (CurLastLocalValue != SavedLastLocalValue) {
+      MachineBasicBlock::iterator FirstDeadInst(SavedLastLocalValue);
+      if (SavedLastLocalValue)
+        ++FirstDeadInst;
+      else
+        FirstDeadInst = FuncInfo.MBB->getFirstNonPHI();
+      setLastLocalValue(SavedLastLocalValue);
+      removeDeadCode(FirstDeadInst, FuncInfo.InsertPt);
+    }
     FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
+  }
   return false;
 }



-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11768.36039.patch
Type: text/x-patch
Size: 2914 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150929/118bafd8/attachment.bin>


More information about the llvm-commits mailing list