Skip to content

Commit 4c28344

Browse files
committed
Subroutine creating an array from a vector
1 parent 15cf98c commit 4c28344

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

llvm/lib/Target/DirectX/DXILDataScalarization.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ bool DataScalarizerVisitor::visitStoreInst(StoreInst &SI) {
172172
return false;
173173
}
174174

175+
// Allocates and populates an array equivalent to the vector operand Vec.
176+
// Returns the array and the type of the array.
177+
static std::pair<Value *, Type *>
178+
allocaArrayFromVector(IRBuilder<> &Builder, Value *Vec, Type *IdxTy) {
179+
Type *ArrTy = equivalentArrayTypeFromVector(Vec->getType());
180+
Value *ArrAlloca = Builder.CreateAlloca(ArrTy);
181+
for (unsigned I = 0; I < ArrTy->getArrayNumElements(); ++I) {
182+
Value *EE = Builder.CreateExtractElement(Vec, I);
183+
Value *GEP = Builder.CreateInBoundsGEP(
184+
ArrTy, ArrAlloca,
185+
{ConstantInt::get(IdxTy, 0), ConstantInt::get(IdxTy, I)});
186+
Builder.CreateStore(EE, GEP);
187+
}
188+
return std::make_pair(ArrAlloca, ArrTy);
189+
}
190+
175191
bool DataScalarizerVisitor::visitInsertElementInst(InsertElementInst &IEI) {
176192
Value *Vec = IEI.getOperand(0);
177193
Value *Val = IEI.getOperand(1);
@@ -183,19 +199,9 @@ bool DataScalarizerVisitor::visitInsertElementInst(InsertElementInst &IEI) {
183199
return false;
184200

185201
IRBuilder<> Builder(&IEI);
186-
Type *VecTy = Vec->getType();
187-
188-
Type *ArrTy = equivalentArrayTypeFromVector(VecTy);
189-
Value *ArrAlloca = Builder.CreateAlloca(ArrTy);
190-
191-
for (unsigned I = 0; I < ArrTy->getArrayNumElements(); ++I) {
192-
Value *EE = Builder.CreateExtractElement(Vec, I);
193-
Value *GEP = Builder.CreateInBoundsGEP(
194-
ArrTy, ArrAlloca,
195-
{ConstantInt::get(IndexTy, 0), ConstantInt::get(IndexTy, I)});
196-
Builder.CreateStore(EE, GEP);
197-
}
198-
202+
std::pair<Value *, Type *> Arr = allocaArrayFromVector(Builder, Vec, IndexTy);
203+
Value *ArrAlloca = Arr.first;
204+
Type *ArrTy = Arr.second;
199205
Value *GEP = Builder.CreateInBoundsGEP(ArrTy, ArrAlloca,
200206
{ConstantInt::get(IndexTy, 0), Index});
201207
Builder.CreateStore(Val, GEP);
@@ -212,18 +218,10 @@ bool DataScalarizerVisitor::visitExtractElementInst(ExtractElementInst &EEI) {
212218
return false;
213219

214220
IRBuilder<> Builder(&EEI);
215-
VectorType *VecTy = EEI.getVectorOperandType();
216-
217-
Type *ArrTy = equivalentArrayTypeFromVector(VecTy);
218-
Value *ArrAlloca = Builder.CreateAlloca(ArrTy);
219-
220-
for (unsigned I = 0; I < ArrTy->getArrayNumElements(); ++I) {
221-
Value *EE = Builder.CreateExtractElement(EEI.getVectorOperand(), I);
222-
Value *GEP = Builder.CreateInBoundsGEP(
223-
ArrTy, ArrAlloca,
224-
{ConstantInt::get(IndexTy, 0), ConstantInt::get(IndexTy, I)});
225-
Builder.CreateStore(EE, GEP);
226-
}
221+
std::pair<Value *, Type *> Arr =
222+
allocaArrayFromVector(Builder, EEI.getVectorOperand(), IndexTy);
223+
Value *ArrAlloca = Arr.first;
224+
Type *ArrTy = Arr.second;
227225

228226
Value *GEP = Builder.CreateInBoundsGEP(ArrTy, ArrAlloca,
229227
{ConstantInt::get(IndexTy, 0), Index});

0 commit comments

Comments
 (0)