[llvm] r235700 - Correct extractelement constant folding
Pawel Bylica
chfast at gmail.com
Fri Apr 24 00:42:35 PDT 2015
Author: chfast
Date: Fri Apr 24 02:42:35 2015
New Revision: 235700
URL: http://llvm.org/viewvc/llvm-project?rev=235700&view=rev
Log:
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
Modified:
llvm/trunk/lib/IR/ConstantFold.cpp
llvm/trunk/unittests/IR/ConstantsTest.cpp
Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=235700&r1=235699&r2=235700&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Fri Apr 24 02:42:35 2015
@@ -789,11 +789,10 @@ Constant *llvm::ConstantFoldExtractEleme
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;
}
Modified: llvm/trunk/unittests/IR/ConstantsTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/ConstantsTest.cpp?rev=235700&r1=235699&r2=235700&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/ConstantsTest.cpp (original)
+++ llvm/trunk/unittests/IR/ConstantsTest.cpp Fri Apr 24 02:42:35 2015
@@ -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
More information about the llvm-commits
mailing list