[llvm] r344970 - X86: Do not optimize branches with undef eflags inputs

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 22 15:52:23 PDT 2018


Author: matze
Date: Mon Oct 22 15:52:23 2018
New Revision: 344970

URL: http://llvm.org/viewvc/llvm-project?rev=344970&view=rev
Log:
X86: Do not optimize branches with undef eflags inputs

analyzeBranch()/insertBranch() etc. do not properly deal with an undef
flag on the eflags input and used to produce invalid MIR.  I don't see
this ever affecting real world inputs (I don't think it is possible to
produce undef flags with llvm IR), so I simply changed the code to bail
out in this case.

rdar://42122367

Added:
    llvm/trunk/test/CodeGen/X86/undef-eflags.mir
Modified:
    llvm/trunk/lib/Target/X86/X86InstrInfo.cpp

Modified: llvm/trunk/lib/Target/X86/X86InstrInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86InstrInfo.cpp?rev=344970&r1=344969&r2=344970&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86InstrInfo.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86InstrInfo.cpp Mon Oct 22 15:52:23 2018
@@ -2640,6 +2640,11 @@ bool X86InstrInfo::AnalyzeBranchImpl(
     if (BranchCode == X86::COND_INVALID)
       return true;  // Can't handle indirect branch.
 
+    // In practice we should never have an undef eflags operand, if we do
+    // abort here as we are not prepared to preserve the flag.
+    if (I->getOperand(1).isUndef())
+      return true;
+
     // Working from the bottom, handle the first conditional branch.
     if (Cond.empty()) {
       MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();

Added: llvm/trunk/test/CodeGen/X86/undef-eflags.mir
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/undef-eflags.mir?rev=344970&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/undef-eflags.mir (added)
+++ llvm/trunk/test/CodeGen/X86/undef-eflags.mir Mon Oct 22 15:52:23 2018
@@ -0,0 +1,18 @@
+# RUN: llc -o - %s -mtriple=x86_64-- -verify-machineinstrs -run-pass branch-folder | FileCheck %s
+# Check that we do not generate invalid MIR when optimizing condjumps with undef
+# flags on the eflags input (currently we should just bail out).
+---
+# CHECK-LABEL: name: fallundef
+name: fallundef
+tracksRegLiveness: true
+body: |
+  bb.0:
+    JE_1 %bb.1, implicit undef $eflags
+    ; CHECK: JE_1 %bb.1, implicit undef $eflags
+    JMP_1 %bb.2
+  bb.1:
+    RET 2, undef $eax
+
+  bb.2:
+    RET 0, undef $eax
+...




More information about the llvm-commits mailing list