Skip to content

Commit 7982980

Browse files
authored
[AMDGPUPromoteAlloca][NFC] Avoid unnecessary APInt/int64_t conversions (#157864)
Follow-up to #157682
1 parent 59102db commit 7982980

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
406406
SmallVector<Instruction *> &NewInsts) {
407407
// TODO: Extracting a "multiple of X" from a GEP might be a useful generic
408408
// helper.
409+
LLVMContext &Ctx = GEP->getContext();
409410
unsigned BW = DL.getIndexTypeSizeInBits(GEP->getType());
410411
SmallMapVector<Value *, APInt, 4> VarOffsets;
411412
APInt ConstOffset(BW, 0);
@@ -438,27 +439,24 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
438439

439440
assert(CurPtr == Alloca && "GEP not based on alloca");
440441

441-
unsigned VecElemSize = DL.getTypeAllocSize(VecElemTy);
442+
int64_t VecElemSize = DL.getTypeAllocSize(VecElemTy);
442443
if (VarOffsets.size() > 1)
443444
return nullptr;
444445

445446
APInt IndexQuot;
446-
APInt Rem;
447-
APInt::sdivrem(ConstOffset, APInt(ConstOffset.getBitWidth(), VecElemSize),
448-
IndexQuot, Rem);
449-
if (!Rem.isZero())
447+
int64_t Rem;
448+
APInt::sdivrem(ConstOffset, VecElemSize, IndexQuot, Rem);
449+
if (Rem != 0)
450450
return nullptr;
451451
if (VarOffsets.size() == 0)
452-
return ConstantInt::get(GEP->getContext(), IndexQuot);
452+
return ConstantInt::get(Ctx, IndexQuot);
453453

454454
IRBuilder<> Builder(GEP);
455455

456456
const auto &VarOffset = VarOffsets.front();
457457
APInt OffsetQuot;
458-
APInt::sdivrem(VarOffset.second,
459-
APInt(VarOffset.second.getBitWidth(), VecElemSize), OffsetQuot,
460-
Rem);
461-
if (!Rem.isZero() || OffsetQuot.isZero())
458+
APInt::sdivrem(VarOffset.second, VecElemSize, OffsetQuot, Rem);
459+
if (Rem != 0 || OffsetQuot.isZero())
462460
return nullptr;
463461

464462
Value *Offset = VarOffset.first;
@@ -468,7 +466,7 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
468466

469467
if (!OffsetQuot.isOne()) {
470468
ConstantInt *ConstMul =
471-
ConstantInt::get(OffsetType, OffsetQuot.getSExtValue());
469+
ConstantInt::get(Ctx, OffsetQuot.sext(OffsetType->getBitWidth()));
472470
Offset = Builder.CreateMul(Offset, ConstMul);
473471
if (Instruction *NewInst = dyn_cast<Instruction>(Offset))
474472
NewInsts.push_back(NewInst);
@@ -477,7 +475,7 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
477475
return Offset;
478476

479477
ConstantInt *ConstIndex =
480-
ConstantInt::get(OffsetType, IndexQuot.getSExtValue());
478+
ConstantInt::get(Ctx, IndexQuot.sext(OffsetType->getBitWidth()));
481479
Value *IndexAdd = Builder.CreateAdd(Offset, ConstIndex);
482480
if (Instruction *NewInst = dyn_cast<Instruction>(IndexAdd))
483481
NewInsts.push_back(NewInst);

0 commit comments

Comments
 (0)