@@ -300,115 +300,54 @@ bool DataScalarizerVisitor::visitExtractElementInst(ExtractElementInst &EEI) {
300
300
return replaceDynamicExtractElementInst (EEI);
301
301
}
302
302
303
- static void buildConstExprGEPChain (GetElementPtrInst &GEPI, Value *CurrentPtr,
304
- SmallVector<ConstantExpr *, 4 > &GEPChain,
305
- IRBuilder<> &Builder) {
306
- // Process the rest of the chain in reverse order (skipping the innermost)
307
- for (int I = GEPChain.size () - 2 ; I >= 0 ; I--) {
308
- ConstantExpr *CE = GEPChain[I];
309
- GetElementPtrInst *GEPInst =
310
- cast<GetElementPtrInst>(CE->getAsInstruction ());
311
- GEPInst->insertBefore (GEPI.getIterator ());
312
- SmallVector<Value *, MaxVecSize> CurrIndices (GEPInst->indices ());
313
-
314
- // Create a new GEP instruction
315
- Type *SourceTy = GEPInst->getSourceElementType ();
316
- CurrentPtr =
317
- Builder.CreateGEP (SourceTy, CurrentPtr, CurrIndices, GEPInst->getName (),
318
- GEPInst->getNoWrapFlags ());
319
-
320
- // If this is the outermost GEP, update the main GEPI
321
- if (I == 0 ) {
322
- GEPI.setOperand (GEPI.getPointerOperandIndex (), CurrentPtr);
323
- }
324
-
325
- // Clean up the temporary instruction
326
- GEPInst->eraseFromParent ();
327
- }
328
- }
329
-
330
303
bool DataScalarizerVisitor::visitGetElementPtrInst (GetElementPtrInst &GEPI) {
331
- Value *PtrOperand = GEPI.getPointerOperand ();
332
- Type *OrigGEPType = GEPI.getSourceElementType ();
333
- Type *NewGEPType = OrigGEPType;
334
- bool NeedsTransform = false ;
335
- // Check if the pointer operand is a ConstantExpr GEP
336
- if (auto *PtrOpGEPCE = dyn_cast<ConstantExpr>(PtrOperand);
337
- PtrOpGEPCE && PtrOpGEPCE->getOpcode () == Instruction::GetElementPtr) {
338
-
339
- // Collect all nested GEPs in the chain
340
- SmallVector<ConstantExpr *, 4 > GEPChain;
341
- Value *BasePointer = PtrOpGEPCE->getOperand (0 );
342
- GEPChain.push_back (PtrOpGEPCE);
343
-
344
- // Walk up the chain to find all nested GEPs and the base pointer
345
- while (auto *NextGEP = dyn_cast<ConstantExpr>(BasePointer)) {
346
- if (NextGEP->getOpcode () != Instruction::GetElementPtr)
347
- break ;
348
-
349
- GEPChain.push_back (NextGEP);
350
- BasePointer = NextGEP->getOperand (0 );
351
- }
352
-
353
- // Check if the base pointer is a global that needs replacement
354
- if (GlobalVariable *NewGlobal = lookupReplacementGlobal (BasePointer)) {
355
- IRBuilder<> Builder (&GEPI);
356
-
357
- // Create a new GEP for the innermost GEP (last in the chain)
358
- ConstantExpr *InnerGEPCE = GEPChain.back ();
359
- GetElementPtrInst *InnerGEP =
360
- cast<GetElementPtrInst>(InnerGEPCE->getAsInstruction ());
361
- InnerGEP->insertBefore (GEPI.getIterator ());
362
-
363
- SmallVector<Value *, MaxVecSize> Indices (InnerGEP->indices ());
364
- Type *NewGEPType = NewGlobal->getValueType ();
365
- Value *NewInnerGEP =
366
- Builder.CreateGEP (NewGEPType, NewGlobal, Indices, InnerGEP->getName (),
367
- InnerGEP->getNoWrapFlags ());
368
-
369
- // If there's only one GEP in the chain, update the main GEPI directly
370
- if (GEPChain.size () == 1 )
371
- GEPI.setOperand (GEPI.getPointerOperandIndex (), NewInnerGEP);
372
- else
373
- // For multiple GEPs, we need to create a chain of GEPs
374
- buildConstExprGEPChain (GEPI, NewInnerGEP, GEPChain, Builder);
375
-
376
- // Clean up the innermost GEP
377
- InnerGEP->eraseFromParent ();
378
- return true ;
379
- }
304
+ GEPOperator *GOp = cast<GEPOperator>(&GEPI);
305
+ Value *PtrOperand = GOp->getPointerOperand ();
306
+ Type *NewGEPType = GOp->getSourceElementType ();
307
+
308
+ // Unwrap GEP ConstantExprs to find the base operand and element type
309
+ while (auto *CE = dyn_cast<ConstantExpr>(PtrOperand)) {
310
+ if (auto *GEPCE = dyn_cast<GEPOperator>(CE)) {
311
+ GOp = GEPCE;
312
+ PtrOperand = GEPCE->getPointerOperand ();
313
+ NewGEPType = GEPCE->getSourceElementType ();
314
+ } else
315
+ break ;
380
316
}
381
317
382
- if (GlobalVariable *NewGlobal = lookupReplacementGlobal (PtrOperand)) {
383
- NewGEPType = NewGlobal->getValueType ();
384
- PtrOperand = NewGlobal;
318
+ bool NeedsTransform = false ;
319
+ if (auto *GV = lookupReplacementGlobal (PtrOperand)) {
320
+ PtrOperand = GV;
321
+ NewGEPType = GV->getValueType ();
385
322
NeedsTransform = true ;
386
- } else if (AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrOperand)) {
387
- Type *AllocatedType = Alloca ->getAllocatedType ();
388
- // Only transform if the allocated type is an array
389
- if ( AllocatedType != OrigGEPType && isa<ArrayType>(AllocatedType )) {
323
+ } else if (auto *AI = dyn_cast<AllocaInst>(PtrOperand)) {
324
+ Type *AllocatedType = AI ->getAllocatedType ();
325
+ if (isa<ArrayType>(AllocatedType) &&
326
+ AllocatedType != GOp-> getResultElementType ( )) {
390
327
NewGEPType = AllocatedType;
391
328
NeedsTransform = true ;
392
329
}
393
330
}
394
331
395
- // Scalar geps should remain scalars geps. The dxil-flatten-arrays pass will
396
- // convert these scalar geps into flattened array geps
397
- if (!isa<ArrayType>(OrigGEPType))
398
- NewGEPType = OrigGEPType;
399
-
400
- // Note: We bail if this isn't a gep touched via alloca or global
401
- // transformations
402
332
if (!NeedsTransform)
403
333
return false ;
404
334
335
+ // Keep scalar GEPs scalar; dxil-flatten-arrays will do flattening later
336
+ if (!isa<ArrayType>(GOp->getSourceElementType ()))
337
+ NewGEPType = GOp->getSourceElementType ();
338
+
405
339
IRBuilder<> Builder (&GEPI);
406
- SmallVector<Value *, MaxVecSize> Indices (GEPI.indices ());
340
+ Value *NewGEP = Builder.CreateGEP (NewGEPType, PtrOperand,
341
+ SmallVector<Value *, 4 >(GOp->indices ()),
342
+ GOp->getName (), GOp->getNoWrapFlags ());
343
+
344
+ GOp->replaceAllUsesWith (NewGEP);
345
+
346
+ if (auto *CE = dyn_cast<ConstantExpr>(GOp))
347
+ CE->destroyConstant ();
348
+ else if (auto *OldGEPI = dyn_cast<GetElementPtrInst>(GOp))
349
+ OldGEPI->eraseFromParent (); // This will always be true in visit* context
407
350
408
- Value *NewGEP = Builder.CreateGEP (NewGEPType, PtrOperand, Indices,
409
- GEPI.getName (), GEPI.getNoWrapFlags ());
410
- GEPI.replaceAllUsesWith (NewGEP);
411
- GEPI.eraseFromParent ();
412
351
return true ;
413
352
}
414
353
0 commit comments