[llvm] r198267 - Fold vector selects with undef elements in the condition. Fixes PR18319.

Nick Lewycky nicholas at mxc.ca
Tue Dec 31 11:30:48 PST 2013


Author: nicholas
Date: Tue Dec 31 13:30:47 2013
New Revision: 198267

URL: http://llvm.org/viewvc/llvm-project?rev=198267&view=rev
Log:
Fold vector selects with undef elements in the condition. Fixes PR18319.
Patch by Ilia Filippov!

Added:
    llvm/trunk/test/Assembler/ConstantExprFoldSelect.ll
Modified:
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/test/Bitcode/select.ll

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=198267&r1=198266&r2=198267&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Tue Dec 31 13:30:47 2013
@@ -705,12 +705,21 @@ Constant *llvm::ConstantFoldSelectInstru
     SmallVector<Constant*, 16> Result;
     Type *Ty = IntegerType::get(CondV->getContext(), 32);
     for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
-      ConstantInt *Cond = dyn_cast<ConstantInt>(CondV->getOperand(i));
-      if (Cond == 0) break;
-      
-      Constant *V = Cond->isNullValue() ? V2 : V1;
-      Constant *Res = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
-      Result.push_back(Res);
+      Constant *V;
+      Constant *V1Element = ConstantExpr::getExtractElement(V1,
+                                                    ConstantInt::get(Ty, i));
+      Constant *V2Element = ConstantExpr::getExtractElement(V2,
+                                                    ConstantInt::get(Ty, i));
+      Constant *Cond = dyn_cast<Constant>(CondV->getOperand(i));
+      if (V1Element == V2Element) {
+        V = V1Element;
+      } else if (isa<UndefValue>(Cond)) {
+        V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
+      } else {
+        if (!isa<ConstantInt>(Cond)) break;
+        V = Cond->isNullValue() ? V2Element : V1Element;
+      }
+      Result.push_back(V);
     }
     
     // If we were able to build the vector, return it.

Added: llvm/trunk/test/Assembler/ConstantExprFoldSelect.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/ConstantExprFoldSelect.ll?rev=198267&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/ConstantExprFoldSelect.ll (added)
+++ llvm/trunk/test/Assembler/ConstantExprFoldSelect.ll Tue Dec 31 13:30:47 2013
@@ -0,0 +1,8 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+; PR18319
+
+define void @function() {
+  %c = trunc <4 x i16> select (<4 x i1> <i1 undef, i1 undef, i1 false, i1 true>, <4 x i16> <i16 undef, i16 2, i16 3, i16 4>, <4 x i16> <i16 -1, i16 -2, i16 -3, i16 -4>) to <4 x i8>
+; CHECK: <i16 undef, i16 -2, i16 -3, i16 4>
+  ret void
+}

Modified: llvm/trunk/test/Bitcode/select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/select.ll?rev=198267&r1=198266&r2=198267&view=diff
==============================================================================
--- llvm/trunk/test/Bitcode/select.ll (original)
+++ llvm/trunk/test/Bitcode/select.ll Tue Dec 31 13:30:47 2013
@@ -5,5 +5,5 @@ define <2 x i32> @main() {
 }
 
 ; CHECK: define <2 x i32> @main() {
-; CHECK:   ret <2 x i32> select (<2 x i1> <i1 false, i1 undef>, <2 x i32> zeroinitializer, <2 x i32> <i32 0, i32 undef>)
+; CHECK:   ret <2 x i32> <i32 0, i32 undef>
 ; CHECK: }





More information about the llvm-commits mailing list