OSDN Git Service

X86: Do not optimize branches with undef eflags inputs
authorMatthias Braun <matze@braunis.de>
Mon, 22 Oct 2018 22:52:23 +0000 (22:52 +0000)
committerMatthias Braun <matze@braunis.de>
Mon, 22 Oct 2018 22:52:23 +0000 (22:52 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@344970 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/X86/X86InstrInfo.cpp
test/CodeGen/X86/undef-eflags.mir [new file with mode: 0644]

index 36ef7dc..1eddb27 100644 (file)
@@ -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();
diff --git a/test/CodeGen/X86/undef-eflags.mir b/test/CodeGen/X86/undef-eflags.mir
new file mode 100644 (file)
index 0000000..e5cf58b
--- /dev/null
@@ -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
+...