[llvm-commits] [llvm] r125393 - in /llvm/trunk: lib/VMCore/ConstantFold.cpp test/Transforms/InstCombine/bitcast-vec-uniform.ll test/Transforms/InstCombine/fold-vector-select.ll
Nadav Rotem
nadav.rotem at intel.com
Fri Feb 11 11:37:55 PST 2011
Author: nadav
Date: Fri Feb 11 13:37:55 2011
New Revision: 125393
URL: http://llvm.org/viewvc/llvm-project?rev=125393&view=rev
Log:
Fix 9173.
Add more folding patterns to constant expressions of vector selects and vector
bitcasts.
Added:
llvm/trunk/test/Transforms/InstCombine/bitcast-vec-uniform.ll (with props)
llvm/trunk/test/Transforms/InstCombine/fold-vector-select.ll (with props)
Modified:
llvm/trunk/lib/VMCore/ConstantFold.cpp
Modified: llvm/trunk/lib/VMCore/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantFold.cpp?rev=125393&r1=125392&r2=125393&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/ConstantFold.cpp (original)
+++ llvm/trunk/lib/VMCore/ConstantFold.cpp Fri Feb 11 13:37:55 2011
@@ -42,6 +42,10 @@
/// input vector constant are all simple integer or FP values.
static Constant *BitCastConstantVector(ConstantVector *CV,
const VectorType *DstTy) {
+
+ if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
+ if (CV->isNullValue()) return Constant::getNullValue(DstTy);
+
// If this cast changes element count then we can't handle it here:
// doing so requires endianness information. This should be handled by
// Analysis/ConstantFolding.cpp
@@ -689,6 +693,42 @@
if (ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
return CB->getZExtValue() ? V1 : V2;
+ // Check for zero aggregate and ConstantVector of zeros
+ if (Cond->isNullValue()) return V2;
+
+ if (ConstantVector* CondV = dyn_cast<ConstantVector>(Cond)) {
+
+ if (CondV->isAllOnesValue()) return V1;
+
+ const VectorType *VTy = cast<VectorType>(V1->getType());
+ ConstantVector *CP1 = dyn_cast<ConstantVector>(V1);
+ ConstantVector *CP2 = dyn_cast<ConstantVector>(V2);
+
+ if ((CP1 || isa<ConstantAggregateZero>(V1)) &&
+ (CP2 || isa<ConstantAggregateZero>(V2))) {
+
+ // Find the element type of the returned vector
+ const Type *EltTy = VTy->getElementType();
+ unsigned NumElem = VTy->getNumElements();
+ std::vector<Constant*> Res(NumElem);
+
+ bool Valid = true;
+ for (unsigned i = 0; i < NumElem; ++i) {
+ ConstantInt* c = dyn_cast<ConstantInt>(CondV->getOperand(i));
+ if (!c) {
+ Valid = false;
+ break;
+ }
+ Constant *C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
+ Constant *C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
+ Res[i] = c->getZExtValue() ? C1 : C2;
+ }
+ // If we were able to build the vector, return it
+ if (Valid) return ConstantVector::get(Res);
+ }
+ }
+
+
if (isa<UndefValue>(V1)) return V2;
if (isa<UndefValue>(V2)) return V1;
if (isa<UndefValue>(Cond)) return V1;
Added: llvm/trunk/test/Transforms/InstCombine/bitcast-vec-uniform.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/bitcast-vec-uniform.ll?rev=125393&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/bitcast-vec-uniform.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/bitcast-vec-uniform.ll Fri Feb 11 13:37:55 2011
@@ -0,0 +1,14 @@
+; RUN: opt < %s -instcombine -S | not grep bitcast
+
+define <4 x i32> @a(<1 x i64> %y) {
+ %c = bitcast <2 x i64> <i64 0, i64 0> to <4 x i32>
+ ret <4 x i32> %c
+}
+
+define <4 x i32> @b(<1 x i64> %y) {
+ %c = bitcast <2 x i64> <i64 -1, i64 -1> to <4 x i32>
+ ret <4 x i32> %c
+}
+
+
+
Propchange: llvm/trunk/test/Transforms/InstCombine/bitcast-vec-uniform.ll
------------------------------------------------------------------------------
svn:eol-style = native
Added: llvm/trunk/test/Transforms/InstCombine/fold-vector-select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fold-vector-select.ll?rev=125393&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fold-vector-select.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/fold-vector-select.ll Fri Feb 11 13:37:55 2011
@@ -0,0 +1,13 @@
+; RUN: opt < %s -instcombine -S | not grep select
+
+define void @foo(<4 x i32> *%A, <4 x i32> *%B, <4 x i32> *%C, <4 x i32> *%D) {
+ %r = select <4 x i1> <i1 true, i1 true, i1 true, i1 true>, <4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> zeroinitializer
+ %g = select <4 x i1> <i1 false, i1 false, i1 false, i1 false>, <4 x i32> zeroinitializer, <4 x i32> <i32 3, i32 6, i32 9, i32 1>
+ %b = select <4 x i1> <i1 false, i1 true, i1 false, i1 true>, <4 x i32> zeroinitializer, <4 x i32> <i32 7, i32 1, i32 4, i32 9>
+ %a = select <4 x i1> zeroinitializer, <4 x i32> zeroinitializer, <4 x i32> <i32 3, i32 2, i32 8, i32 5>
+ store <4 x i32> %r, <4 x i32>* %A
+ store <4 x i32> %g, <4 x i32>* %B
+ store <4 x i32> %b, <4 x i32>* %C
+ store <4 x i32> %a, <4 x i32>* %D
+ ret void
+}
Propchange: llvm/trunk/test/Transforms/InstCombine/fold-vector-select.ll
------------------------------------------------------------------------------
svn:eol-style = native
More information about the llvm-commits
mailing list