[llvm] [AMDGPUPromoteAlloca][NFC] Avoid unnecessary APInt/int64_t conversions (PR #157864)
Fabian Ritter via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 10 07:15:51 PDT 2025
https://github.com/ritter-x2a created https://github.com/llvm/llvm-project/pull/157864
Follow-up to #157682
>From 2f75e50257d89d01e1169160f2d2fa7ae70f4080 Mon Sep 17 00:00:00 2001
From: Fabian Ritter <fabian.ritter at amd.com>
Date: Wed, 10 Sep 2025 10:09:11 -0400
Subject: [PATCH] [AMDGPUPromoteAlloca][NFC] Avoid unnecessary APInt/int64_t
conversions
Follow-up to #157682
---
.../lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp | 23 +++++++++----------
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
index 7dbe1235a98b5..e8d5bbe0cd11e 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
@@ -39,6 +39,7 @@
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/IntrinsicsAMDGPU.h"
#include "llvm/IR/IntrinsicsR600.h"
+#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
@@ -406,6 +407,7 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
SmallVector<Instruction *> &NewInsts) {
// TODO: Extracting a "multiple of X" from a GEP might be a useful generic
// helper.
+ LLVMContext &Ctx = GEP->getContext();
unsigned BW = DL.getIndexTypeSizeInBits(GEP->getType());
SmallMapVector<Value *, APInt, 4> VarOffsets;
APInt ConstOffset(BW, 0);
@@ -438,27 +440,24 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
assert(CurPtr == Alloca && "GEP not based on alloca");
- unsigned VecElemSize = DL.getTypeAllocSize(VecElemTy);
+ int64_t VecElemSize = DL.getTypeAllocSize(VecElemTy);
if (VarOffsets.size() > 1)
return nullptr;
APInt IndexQuot;
- APInt Rem;
- APInt::sdivrem(ConstOffset, APInt(ConstOffset.getBitWidth(), VecElemSize),
- IndexQuot, Rem);
- if (!Rem.isZero())
+ int64_t Rem;
+ APInt::sdivrem(ConstOffset, VecElemSize, IndexQuot, Rem);
+ if (Rem != 0)
return nullptr;
if (VarOffsets.size() == 0)
- return ConstantInt::get(GEP->getContext(), IndexQuot);
+ return ConstantInt::get(Ctx, IndexQuot);
IRBuilder<> Builder(GEP);
const auto &VarOffset = VarOffsets.front();
APInt OffsetQuot;
- APInt::sdivrem(VarOffset.second,
- APInt(VarOffset.second.getBitWidth(), VecElemSize), OffsetQuot,
- Rem);
- if (!Rem.isZero() || OffsetQuot.isZero())
+ APInt::sdivrem(VarOffset.second, VecElemSize, OffsetQuot, Rem);
+ if (Rem != 0 || OffsetQuot.isZero())
return nullptr;
Value *Offset = VarOffset.first;
@@ -468,7 +467,7 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
if (!OffsetQuot.isOne()) {
ConstantInt *ConstMul =
- ConstantInt::get(OffsetType, OffsetQuot.getSExtValue());
+ ConstantInt::get(Ctx, OffsetQuot.sext(OffsetType->getBitWidth()));
Offset = Builder.CreateMul(Offset, ConstMul);
if (Instruction *NewInst = dyn_cast<Instruction>(Offset))
NewInsts.push_back(NewInst);
@@ -477,7 +476,7 @@ static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
return Offset;
ConstantInt *ConstIndex =
- ConstantInt::get(OffsetType, IndexQuot.getSExtValue());
+ ConstantInt::get(Ctx, IndexQuot.sext(OffsetType->getBitWidth()));
Value *IndexAdd = Builder.CreateAdd(Offset, ConstIndex);
if (Instruction *NewInst = dyn_cast<Instruction>(IndexAdd))
NewInsts.push_back(NewInst);
More information about the llvm-commits
mailing list