summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPawel Bylica <chfast@gmail.com>2015-04-24 07:42:35 +0000
committerPawel Bylica <chfast@gmail.com>2015-04-24 07:42:35 +0000
commitbce9c2e263f90cf54ad01803b15a076cf466abb9 (patch)
tree5c151fdd79ba234235e5fca18c7fb31fc7cb785f
parent86ac44744a0207fea281fc2267fffaa69660fc03 (diff)
downloadbcm5719-llvm-bce9c2e263f90cf54ad01803b15a076cf466abb9.tar.gz
bcm5719-llvm-bce9c2e263f90cf54ad01803b15a076cf466abb9.zip
Correct extractelement constant folding
Summary: Constant folding of extractelement with out-of-bound index produces undef also for indexes bigger than 64bit (instead of crash assert failure as before) Test Plan: Unit tests included. Reviewers: majnemer Reviewed By: majnemer Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D9225 llvm-svn: 235700
-rw-r--r--llvm/lib/IR/ConstantFold.cpp5
-rw-r--r--llvm/unittests/IR/ConstantsTest.cpp8
2 files changed, 10 insertions, 3 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index d3caf04489d..03d7c5e7b95 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -789,11 +789,10 @@ Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
return UndefValue::get(Val->getType()->getVectorElementType());
if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
- uint64_t Index = CIdx->getZExtValue();
// ee({w,x,y,z}, wrong_value) -> undef
- if (Index >= Val->getType()->getVectorNumElements())
+ if (CIdx->uge(Val->getType()->getVectorNumElements()))
return UndefValue::get(Val->getType()->getVectorElementType());
- return Val->getAggregateElement(Index);
+ return Val->getAggregateElement(CIdx->getZExtValue());
}
return nullptr;
}
diff --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp
index 0c7777d7678..d02217dc355 100644
--- a/llvm/unittests/IR/ConstantsTest.cpp
+++ b/llvm/unittests/IR/ConstantsTest.cpp
@@ -187,6 +187,10 @@ TEST(ConstantsTest, AsInstructionsTest) {
Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
Constant *One = ConstantInt::get(Int32Ty, 1);
+ Constant *Two = ConstantInt::get(Int64Ty, 2);
+ Constant *Big = ConstantInt::get(getGlobalContext(),
+ APInt{256, uint64_t(-1), true});
+ Constant *Undef = UndefValue::get(Int64Ty);
#define P0STR "ptrtoint (i32** @dummy to i32)"
#define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
@@ -255,6 +259,10 @@ TEST(ConstantsTest, AsInstructionsTest) {
CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
P6STR ", i32 1");
+
+ EXPECT_TRUE(isa<UndefValue>(ConstantExpr::getExtractElement(P6, Two)));
+ EXPECT_TRUE(isa<UndefValue>(ConstantExpr::getExtractElement(P6, Big)));
+ EXPECT_TRUE(isa<UndefValue>(ConstantExpr::getExtractElement(P6, Undef)));
}
#ifdef GTEST_HAS_DEATH_TEST
OpenPOWER on IntegriCloud