$OpenBSD: patch-lib_Target_X86_X86FlagsCopyLowering_cpp,v 1.1 2018/08/27 08:46:52 ajacoutot Exp $

[x86] Fix a really subtle miscompile due to a somewhat glaring bug in EFLAGS
copy lowering.

Index: lib/Target/X86/X86FlagsCopyLowering.cpp
--- lib/Target/X86/X86FlagsCopyLowering.cpp.orig
+++ lib/Target/X86/X86FlagsCopyLowering.cpp
@@ -608,9 +608,12 @@ X86FlagsCopyLoweringPass::collectCondsInRegs(MachineBa
   for (MachineInstr &MI : llvm::reverse(
            llvm::make_range(MBB.instr_begin(), CopyDefI.getIterator()))) {
     X86::CondCode Cond = X86::getCondFromSETOpc(MI.getOpcode());
-    if (Cond != X86::COND_INVALID && MI.getOperand(0).isReg() &&
-        TRI->isVirtualRegister(MI.getOperand(0).getReg()))
+    if (Cond != X86::COND_INVALID && !MI.mayStore() && MI.getOperand(0).isReg() &&
+        TRI->isVirtualRegister(MI.getOperand(0).getReg())) {
+      assert(MI.getOperand(0).isDef() &&
+             "A non-storing SETcc should always define a register!");
       CondRegs[Cond] = MI.getOperand(0).getReg();
+    }
 
     // Stop scanning when we see the first definition of the EFLAGS as prior to
     // this we would potentially capture the wrong flag state.
