Skip to content

Commit 24ab761

Browse files
committed
LLT: Add changeNumElements
This is the element analog of changeElementType/changeElementSize
1 parent df8f277 commit 24ab761

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

llvm/include/llvm/Support/LowLevelTypeImpl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ class LLT {
137137
: LLT::scalar(NewEltSize);
138138
}
139139

140+
/// Return a vector or scalar with the same element type and the new number of
141+
/// elements.
142+
LLT changeNumElements(unsigned NewNumElts) const {
143+
return LLT::scalarOrVector(NewNumElts, getScalarType());
144+
}
145+
140146
bool isByteSized() const { return (getSizeInBits() & 7) == 0; }
141147

142148
unsigned getScalarSizeInBits() const {

llvm/unittests/CodeGen/LowLevelTypeTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ TEST(LowLevelTypeTest, ChangeElementType) {
138138
EXPECT_EQ(V2S32, V2P0.changeElementType(S32));
139139
}
140140

141+
TEST(LowLevelTypeTest, ChangeNumElements) {
142+
const LLT P0 = LLT::pointer(0, 32);
143+
const LLT V2P0 = LLT::vector(2, P0);
144+
const LLT V3P0 = LLT::vector(3, P0);
145+
146+
const LLT S64 = LLT::scalar(64);
147+
const LLT V2S64 = LLT::vector(2, 64);
148+
const LLT V3S64 = LLT::vector(3, 64);
149+
150+
// Vector to scalar
151+
EXPECT_EQ(S64, V2S64.changeNumElements(1));
152+
153+
// Vector to vector
154+
EXPECT_EQ(V3S64, V2S64.changeNumElements(3));
155+
156+
// Scalar to vector
157+
EXPECT_EQ(V2S64, S64.changeNumElements(2));
158+
159+
EXPECT_EQ(P0, V2P0.changeNumElements(1));
160+
EXPECT_EQ(V3P0, V2P0.changeNumElements(3));
161+
EXPECT_EQ(V2P0, P0.changeNumElements(2));
162+
}
163+
141164
#ifdef GTEST_HAS_DEATH_TEST
142165
#ifndef NDEBUG
143166

0 commit comments

Comments
 (0)