Skip to content

Commit 97a23ab

Browse files
committed
AMDGPUPrintfRuntimeBinding.cpp - drop unnecessary casts/dyn_casts. NFCI.
GetElementPtrInst::Create returns a GetElementPtrInst* so we don't need to cast. Similarly IntegerType inherits from the Type base class. Also, I've used auto* in a few places to cleanup the code. Helps fix some clang-tidy warnings which saw the dyn_casts and warned that these can return null.
1 parent 65c6ae3 commit 97a23ab

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,8 @@ bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
379379
ConstantInt::get(Ctx, APInt(32, StringRef("0"), 10));
380380
ZeroIdxList.push_back(zeroInt);
381381

382-
GetElementPtrInst *BufferIdx =
383-
dyn_cast<GetElementPtrInst>(GetElementPtrInst::Create(
384-
nullptr, pcall, ZeroIdxList, "PrintBuffID", Brnch));
382+
GetElementPtrInst *BufferIdx = GetElementPtrInst::Create(
383+
nullptr, pcall, ZeroIdxList, "PrintBuffID", Brnch);
385384

386385
Type *idPointer = PointerType::get(I32Ty, AMDGPUAS::GLOBAL_ADDRESS);
387386
Value *id_gep_cast =
@@ -395,8 +394,8 @@ bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
395394

396395
FourthIdxList.push_back(fourInt); // 1st 4 bytes hold the printf_id
397396
// the following GEP is the buffer pointer
398-
BufferIdx = cast<GetElementPtrInst>(GetElementPtrInst::Create(
399-
nullptr, pcall, FourthIdxList, "PrintBuffGep", Brnch));
397+
BufferIdx = GetElementPtrInst::Create(nullptr, pcall, FourthIdxList,
398+
"PrintBuffGep", Brnch);
400399

401400
Type *Int32Ty = Type::getInt32Ty(Ctx);
402401
Type *Int64Ty = Type::getInt64Ty(Ctx);
@@ -409,17 +408,15 @@ bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
409408
if (ArgType->isFPOrFPVectorTy() && !isa<VectorType>(ArgType)) {
410409
Type *IType = (ArgType->isFloatTy()) ? Int32Ty : Int64Ty;
411410
if (OpConvSpecifiers[ArgCount - 1] == 'f') {
412-
ConstantFP *fpCons = dyn_cast<ConstantFP>(Arg);
413-
if (fpCons) {
414-
APFloat Val(fpCons->getValueAPF());
411+
if (auto *FpCons = dyn_cast<ConstantFP>(Arg)) {
412+
APFloat Val(FpCons->getValueAPF());
415413
bool Lost = false;
416414
Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
417415
&Lost);
418416
Arg = ConstantFP::get(Ctx, Val);
419417
IType = Int32Ty;
420-
} else {
421-
FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg);
422-
if (FpExt && FpExt->getType()->isDoubleTy() &&
418+
} else if (auto *FpExt = dyn_cast<FPExtInst>(Arg)) {
419+
if (FpExt->getType()->isDoubleTy() &&
423420
FpExt->getOperand(0)->getType()->isFloatTy()) {
424421
Arg = FpExt->getOperand(0);
425422
IType = Int32Ty;
@@ -431,9 +428,8 @@ bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
431428
} else if (ArgType->getTypeID() == Type::PointerTyID) {
432429
if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
433430
const char *S = NonLiteralStr;
434-
if (ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
435-
GlobalVariable *GV =
436-
dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
431+
if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
432+
auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
437433
if (GV && GV->hasInitializer()) {
438434
Constant *Init = GV->getInitializer();
439435
ConstantDataArray *CA = dyn_cast<ConstantDataArray>(Init);
@@ -491,27 +487,27 @@ bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
491487
switch (EleSize) {
492488
default:
493489
EleCount = TotalSize / 64;
494-
IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
490+
IType = Type::getInt64Ty(ArgType->getContext());
495491
break;
496492
case 8:
497493
if (EleCount >= 8) {
498494
EleCount = TotalSize / 64;
499-
IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
495+
IType = Type::getInt64Ty(ArgType->getContext());
500496
} else if (EleCount >= 3) {
501497
EleCount = 1;
502-
IType = dyn_cast<Type>(Type::getInt32Ty(ArgType->getContext()));
498+
IType = Type::getInt32Ty(ArgType->getContext());
503499
} else {
504500
EleCount = 1;
505-
IType = dyn_cast<Type>(Type::getInt16Ty(ArgType->getContext()));
501+
IType = Type::getInt16Ty(ArgType->getContext());
506502
}
507503
break;
508504
case 16:
509505
if (EleCount >= 3) {
510506
EleCount = TotalSize / 64;
511-
IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
507+
IType = Type::getInt64Ty(ArgType->getContext());
512508
} else {
513509
EleCount = 1;
514-
IType = dyn_cast<Type>(Type::getInt32Ty(ArgType->getContext()));
510+
IType = Type::getInt32Ty(ArgType->getContext());
515511
}
516512
break;
517513
}
@@ -539,8 +535,8 @@ bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
539535
(void)StBuff;
540536
if (I + 1 == E && ArgCount + 1 == CI->getNumArgOperands())
541537
break;
542-
BufferIdx = dyn_cast<GetElementPtrInst>(GetElementPtrInst::Create(
543-
nullptr, BufferIdx, BuffOffset, "PrintBuffNextPtr", Brnch));
538+
BufferIdx = GetElementPtrInst::Create(nullptr, BufferIdx, BuffOffset,
539+
"PrintBuffNextPtr", Brnch);
544540
LLVM_DEBUG(dbgs() << "inserting gep to the printf buffer:\n"
545541
<< *BufferIdx << '\n');
546542
}

0 commit comments

Comments
 (0)