@@ -172,6 +172,22 @@ bool DataScalarizerVisitor::visitStoreInst(StoreInst &SI) {
172
172
return false ;
173
173
}
174
174
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
+
175
191
bool DataScalarizerVisitor::visitInsertElementInst (InsertElementInst &IEI) {
176
192
Value *Vec = IEI.getOperand (0 );
177
193
Value *Val = IEI.getOperand (1 );
@@ -183,19 +199,9 @@ bool DataScalarizerVisitor::visitInsertElementInst(InsertElementInst &IEI) {
183
199
return false ;
184
200
185
201
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 ;
199
205
Value *GEP = Builder.CreateInBoundsGEP (ArrTy, ArrAlloca,
200
206
{ConstantInt::get (IndexTy, 0 ), Index});
201
207
Builder.CreateStore (Val, GEP);
@@ -212,18 +218,10 @@ bool DataScalarizerVisitor::visitExtractElementInst(ExtractElementInst &EEI) {
212
218
return false ;
213
219
214
220
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 ;
227
225
228
226
Value *GEP = Builder.CreateInBoundsGEP (ArrTy, ArrAlloca,
229
227
{ConstantInt::get (IndexTy, 0 ), Index});
0 commit comments