summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
authorDavid Majnemer <david.majnemer@gmail.com>2016-07-29 04:06:09 +0000
committerDavid Majnemer <david.majnemer@gmail.com>2016-07-29 04:06:09 +0000
commite4218cf11e163aaa89ef08619af2053c91985ed6 (patch)
tree54c8334075373e4007705f88a6bfaf7c55f91455 /llvm
parent09d4f177fc28f603ad53769ceab4300270bf6654 (diff)
downloadbcm5719-llvm-e4218cf11e163aaa89ef08619af2053c91985ed6.tar.gz
bcm5719-llvm-e4218cf11e163aaa89ef08619af2053c91985ed6.zip
[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. llvm-svn: 277104
Diffstat (limited to 'llvm')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp13
-rw-r--r--llvm/test/Transforms/InstCombine/cast.ll6
2 files changed, 17 insertions, 2 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 010f5d62f06..60b94a903e3 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -90,6 +90,11 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
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 *DestTy, const DataLayout &DL) {
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 *DestTy, const DataLayout &DL) {
// 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);
diff --git a/llvm/test/Transforms/InstCombine/cast.ll b/llvm/test/Transforms/InstCombine/cast.ll
index 2acb68738ff..46ae14c85cc 100644
--- a/llvm/test/Transforms/InstCombine/cast.ll
+++ b/llvm/test/Transforms/InstCombine/cast.ll
@@ -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)
+}
OpenPOWER on IntegriCloud