@@ -172,36 +172,41 @@ 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) {
175+ static bool replaceDynamicInsertElementInst (InsertElementInst &IEI) {
176+ IRBuilder<> Builder (&IEI);
177+
178+ Value *Vec = IEI.getOperand (0 );
179+ Value *Val = IEI.getOperand (1 );
180+ Value *Index = IEI.getOperand (2 );
181+ Type *IndexTy = Index->getType ();
182+
179183 Type *ArrTy = equivalentArrayTypeFromVector (Vec->getType ());
180184 Value *ArrAlloca = Builder.CreateAlloca (ArrTy);
181- for (unsigned I = 0 ; I < ArrTy->getArrayNumElements (); ++I) {
185+ const uint64_t ArrNumElems = ArrTy->getArrayNumElements ();
186+
187+ SmallVector<Value *, 4 > GEPs (ArrNumElems);
188+ for (unsigned I = 0 ; I < ArrNumElems; ++I) {
182189 Value *EE = Builder.CreateExtractElement (Vec, I);
183190 Value *GEP = Builder.CreateInBoundsGEP (
184191 ArrTy, ArrAlloca,
185- {ConstantInt::get (IdxTy , 0 ), ConstantInt::get (IdxTy , I)});
192+ {ConstantInt::get (IndexTy , 0 ), ConstantInt::get (IndexTy , I)});
186193 Builder.CreateStore (EE, GEP);
194+ GEPs[I] = GEP;
187195 }
188- return std::make_pair (ArrAlloca, ArrTy);
189- }
190196
191- static bool replaceDynamicInsertElementInst (InsertElementInst &IEI) {
192- IRBuilder<> Builder (&IEI);
197+ Value *GEPForStore = Builder.CreateInBoundsGEP (
198+ ArrTy, ArrAlloca, {ConstantInt::get (IndexTy, 0 ), Index});
199+ Builder.CreateStore (Val, GEPForStore);
193200
194- Value *Vec = IEI.getOperand (0 );
195- Value *Val = IEI.getOperand (1 );
196- Value *Index = IEI.getOperand (2 );
197- Type *IndexTy = Index->getType ();
201+ Value *NewIEI = PoisonValue::get (Vec->getType ());
202+ for (unsigned I = 0 ; I < ArrNumElems; ++I) {
203+ Value *GEP = GEPs[I];
204+ Value *Load = Builder.CreateLoad (ArrTy->getArrayElementType (), GEP);
205+ NewIEI =
206+ Builder.CreateInsertElement (NewIEI, Load, ConstantInt::get (IndexTy, I));
207+ }
198208
199- std::pair<Value *, Type *> Arr = allocaArrayFromVector (Builder, Vec, IndexTy);
200- Value *ArrAlloca = Arr.first ;
201- Type *ArrTy = Arr.second ;
202- Value *GEP = Builder.CreateInBoundsGEP (ArrTy, ArrAlloca,
203- {ConstantInt::get (IndexTy, 0 ), Index});
204- Builder.CreateStore (Val, GEP);
209+ IEI.replaceAllUsesWith (NewIEI);
205210 IEI.eraseFromParent ();
206211 return true ;
207212}
@@ -220,10 +225,15 @@ static bool replaceDynamicExtractElementInst(ExtractElementInst &EEI) {
220225 Value *Index = EEI.getIndexOperand ();
221226 Type *IndexTy = Index->getType ();
222227
223- std::pair<Value *, Type *> Arr =
224- allocaArrayFromVector (Builder, EEI.getVectorOperand (), IndexTy);
225- Value *ArrAlloca = Arr.first ;
226- Type *ArrTy = Arr.second ;
228+ Type *ArrTy = equivalentArrayTypeFromVector (EEI.getVectorOperandType ());
229+ Value *ArrAlloca = Builder.CreateAlloca (ArrTy);
230+ for (unsigned I = 0 ; I < ArrTy->getArrayNumElements (); ++I) {
231+ Value *EE = Builder.CreateExtractElement (EEI.getVectorOperand (), I);
232+ Value *GEP = Builder.CreateInBoundsGEP (
233+ ArrTy, ArrAlloca,
234+ {ConstantInt::get (IndexTy, 0 ), ConstantInt::get (IndexTy, I)});
235+ Builder.CreateStore (EE, GEP);
236+ }
227237
228238 Value *GEP = Builder.CreateInBoundsGEP (ArrTy, ArrAlloca,
229239 {ConstantInt::get (IndexTy, 0 ), Index});
0 commit comments