[llvm] r277104 - [ConstantFolding] Fold bitcasts of vectors w/ undef elements
David Majnemer via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 28 21:06:10 PDT 2016
Author: majnemer
Date: Thu Jul 28 23:06:09 2016
New Revision: 277104
URL: http://llvm.org/viewvc/llvm-project?rev=277104&view=rev
Log:
[ConstantFolding] Fold bitcasts of vectors w/ undef elements
An undef vector element can be treated as if it had any value. Folding
such a vector element to 0 in a bitcast can open up further folding
opportunities.
Modified:
llvm/trunk/lib/Analysis/ConstantFolding.cpp
llvm/trunk/test/Transforms/InstCombine/cast.ll
Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=277104&r1=277103&r2=277104&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Thu Jul 28 23:06:09 2016
@@ -90,6 +90,11 @@ Constant *FoldBitCast(Constant *C, Type
else
Element = C->getAggregateElement(i);
+ if (Element && isa<UndefValue>(Element)) {
+ Result <<= BitShift;
+ continue;
+ }
+
auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
if (!ElementCI)
return ConstantExpr::getBitCast(C, DestTy);
@@ -180,7 +185,11 @@ Constant *FoldBitCast(Constant *C, Type
Constant *Elt = Zero;
unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
for (unsigned j = 0; j != Ratio; ++j) {
- Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++));
+ Constant *Src = C->getAggregateElement(SrcElt++);
+ if (Src && isa<UndefValue>(Src))
+ Src = Constant::getNullValue(SrcEltTy);
+ else
+ Src = dyn_cast_or_null<ConstantInt>(Src);
if (!Src) // Reject constantexpr elements.
return ConstantExpr::getBitCast(C, DestTy);
@@ -206,7 +215,7 @@ Constant *FoldBitCast(Constant *C, Type
// Loop over each source value, expanding into multiple results.
for (unsigned i = 0; i != NumSrcElt; ++i) {
- auto *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i));
+ auto *Src = dyn_cast_or_null<ConstantInt>(C->getAggregateElement(i));
if (!Src) // Reject constantexpr elements.
return ConstantExpr::getBitCast(C, DestTy);
Modified: llvm/trunk/test/Transforms/InstCombine/cast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast.ll?rev=277104&r1=277103&r2=277104&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/cast.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/cast.ll Thu Jul 28 23:06:09 2016
@@ -1380,3 +1380,9 @@ define i64 @PR28745() {
%b = zext i32 extractvalue ({ i32 } select (i1 icmp eq (i16 extractelement (<2 x i16> bitcast (<1 x i32> <i32 1> to <2 x i16>), i32 0), i16 0), { i32 } { i32 1 }, { i32 } zeroinitializer), 0) to i64
ret i64 %b
}
+
+define i32 @test89() {
+; CHECK-LABEL: @test89(
+; CHECK-NEXT: ret i32 393216
+ ret i32 bitcast (<2 x i16> <i16 6, i16 undef> to i32)
+}
More information about the llvm-commits
mailing list