[LLVMbugs] [Bug 12592] New: instcombine mis-optimizes vector select (ConstantFoldSelectInstruction() bug?)

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Apr 18 15:46:45 PDT 2012


http://llvm.org/bugs/show_bug.cgi?id=12592

             Bug #: 12592
           Summary: instcombine mis-optimizes vector select
                    (ConstantFoldSelectInstruction() bug?)
           Product: new-bugs
           Version: trunk
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: matt at pharr.org
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified


Created attachment 8409
  --> http://llvm.org/bugs/attachment.cgi?id=8409
test case

The attached testcase does a vector select with compile-time constant
true/false values, selecting between a vector of 1 values and a vector of 0
values.  By inspection, it should resolve to the vector <1, 0, 0, 0, 0, 0, 0,
0>, but when it passes through instcombine with top-of-tree, it turns into the
wrong result:

% opt -instcombine bug.ll -o - | llvm-dis
...
  ret <8 x i32> <i32 0, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
...
%

The issue seems to be with ConstantFoldSelectInstruction() which seems to have
a condition switched.  With the following patch, I get the result I expect:

diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index b743287..9b1c756 100644
--- a/lib/VMCore/ConstantFold.cpp
+++ b/lib/VMCore/ConstantFold.cpp
@@ -700,7 +700,7 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant
*Cond,
       ConstantInt *Cond = dyn_cast<ConstantInt>(CondV->getOperand(i));
       if (Cond == 0) break;

-      Constant *Res = (Cond->getZExtValue() ? V2 :
V1)->getAggregateElement(i);
+      Constant *Res = (Cond->getZExtValue() ? V1 :
V2)->getAggregateElement(i);
       if (Res == 0) break;
       Result.push_back(Res);
     }

Note, however, that I haven't tested this patch beyond this immediate case.

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list