Skip to content

[CIR][NFC] Remove redundant pointer casts #1609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def PtrStrideOp : CIR_Op<"ptr_stride",
let extraClassDeclaration = [{
// Get type pointed by the base pointer.
mlir::Type getElementTy() {
return mlir::cast<cir::PointerType>(getBase().getType()).getPointee();
return getBase().getType().getPointee();
}
}];
}
Expand Down Expand Up @@ -2870,7 +2870,7 @@ def GetMemberOp : CIR_Op<"get_member"> {

/// Return the result type.
cir::PointerType getResultTy() {
return mlir::cast<cir::PointerType>(getResult().getType());
return getResult().getType();
}
}];

Expand Down
13 changes: 6 additions & 7 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -961,8 +961,8 @@ OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
//===----------------------------------------------------------------------===//

LogicalResult cir::ComplexRealPtrOp::verify() {
auto resultPointeeTy = mlir::cast<cir::PointerType>(getType()).getPointee();
auto operandPtrTy = mlir::cast<cir::PointerType>(getOperand().getType());
mlir::Type resultPointeeTy = getType().getPointee();
cir::PointerType operandPtrTy = getOperand().getType();
auto operandPointeeTy =
mlir::cast<cir::ComplexType>(operandPtrTy.getPointee());

Expand All @@ -976,8 +976,8 @@ LogicalResult cir::ComplexRealPtrOp::verify() {
}

LogicalResult cir::ComplexImagPtrOp::verify() {
auto resultPointeeTy = mlir::cast<cir::PointerType>(getType()).getPointee();
auto operandPtrTy = mlir::cast<cir::PointerType>(getOperand().getType());
mlir::Type resultPointeeTy = getType().getPointee();
cir::PointerType operandPtrTy = getOperand().getType();
auto operandPointeeTy =
mlir::cast<cir::ComplexType>(operandPtrTy.getPointee());

Expand Down Expand Up @@ -3661,7 +3661,7 @@ LogicalResult cir::GetMethodOp::verify() {
auto methodTy = getMethod().getType();

// Assume objectTy is !cir.ptr<!T>
auto objectPtrTy = mlir::cast<cir::PointerType>(getObject().getType());
cir::PointerType objectPtrTy = getObject().getType();
auto objectTy = objectPtrTy.getPointee();

if (methodTy.getClsTy() != objectTy) {
Expand All @@ -3670,8 +3670,7 @@ LogicalResult cir::GetMethodOp::verify() {
}

// Assume methodFuncTy is !cir.func<!Ret (!Args)>
auto calleePtrTy = mlir::cast<cir::PointerType>(getCallee().getType());
auto calleeTy = mlir::cast<cir::FuncType>(calleePtrTy.getPointee());
auto calleeTy = mlir::cast<cir::FuncType>(getCallee().getType().getPointee());
auto methodFuncTy = methodTy.getMemberFuncTy();

// We verify at here that calleeTy is !cir.func<!Ret (!cir.ptr<!void>, !Args)>
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ void LifetimeCheckPass::classifyAndInitTypeCategories(mlir::Value addr,
if (eltAddr.use_empty())
return;

auto eltTy = mlir::cast<cir::PointerType>(eltAddr.getType()).getPointee();
auto eltTy = eltAddr.getType().getPointee();

// Classify exploded types. Keep alloca original location.
classifyAndInitTypeCategories(eltAddr, eltTy, loc, ++nestLevel);
Expand Down
8 changes: 2 additions & 6 deletions clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1392,9 +1392,7 @@ void LoweringPreparePass::lowerArrayDtor(ArrayDtor op) {

auto eltTy = op->getRegion(0).getArgument(0).getType();
auto arrayLen =
mlir::cast<cir::ArrayType>(
mlir::cast<cir::PointerType>(op.getAddr().getType()).getPointee())
.getSize();
mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
lowerArrayDtorCtorIntoLoop(builder, op, eltTy, op.getAddr(), arrayLen);
}

Expand Down Expand Up @@ -1458,9 +1456,7 @@ void LoweringPreparePass::lowerArrayCtor(ArrayCtor op) {

auto eltTy = op->getRegion(0).getArgument(0).getType();
auto arrayLen =
mlir::cast<cir::ArrayType>(
mlir::cast<cir::PointerType>(op.getAddr().getType()).getPointee())
.getSize();
mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
lowerArrayDtorCtorIntoLoop(builder, op, eltTy, op.getAddr(), arrayLen);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ void ItaniumCXXABI::lowerGetMethod(
llvm_unreachable("ARM method ptr abi NYI");

// Apply the adjustment to the 'this' pointer.
mlir::Type thisVoidPtrTy = cir::PointerType::get(
cir::VoidType::get(rewriter.getContext()),
mlir::cast<cir::PointerType>(op.getObject().getType()).getAddrSpace());
mlir::Type thisVoidPtrTy =
cir::PointerType::get(cir::VoidType::get(rewriter.getContext()),
op.getObject().getType().getAddrSpace());
mlir::Value thisVoidPtr = rewriter.create<cir::CastOp>(
op.getLoc(), thisVoidPtrTy, cir::CastKind::bitcast, loweredObjectPtr);
adjustedThis = rewriter.create<cir::PtrStrideOp>(op.getLoc(), thisVoidPtrTy,
Expand Down Expand Up @@ -377,8 +377,7 @@ void ItaniumCXXABI::lowerGetMethod(
auto vtablePtrTy = cir::PointerType::get(
cir::IntType::get(rewriter.getContext(), 8, true));
auto vtablePtrPtrTy = cir::PointerType::get(
vtablePtrTy,
mlir::cast<cir::PointerType>(op.getObject().getType()).getAddrSpace());
vtablePtrTy, op.getObject().getType().getAddrSpace());
auto vtablePtrPtr = rewriter.create<cir::CastOp>(
op.getLoc(), vtablePtrPtrTy, cir::CastKind::bitcast, loweredObjectPtr);
mlir::Value vtablePtr = rewriter.create<cir::LoadOp>(
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "mlir/Target/LLVMIR/Export.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/LoweringHelpers.h"
#include "clang/CIR/MissingFeatures.h"
Expand Down Expand Up @@ -2319,7 +2320,7 @@ mlir::LogicalResult CIRToLLVMComplexImagOpLowering::matchAndRewrite(
mlir::LogicalResult CIRToLLVMComplexRealPtrOpLowering::matchAndRewrite(
cir::ComplexRealPtrOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
auto operandTy = mlir::cast<cir::PointerType>(op.getOperand().getType());
cir::PointerType operandTy = op.getOperand().getType();
auto resultLLVMTy = getTypeConverter()->convertType(op.getResult().getType());
auto elementLLVMTy = getTypeConverter()->convertType(operandTy.getPointee());

Expand All @@ -2334,7 +2335,7 @@ mlir::LogicalResult CIRToLLVMComplexRealPtrOpLowering::matchAndRewrite(
mlir::LogicalResult CIRToLLVMComplexImagPtrOpLowering::matchAndRewrite(
cir::ComplexImagPtrOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
auto operandTy = mlir::cast<cir::PointerType>(op.getOperand().getType());
cir::PointerType operandTy = op.getOperand().getType();
auto resultLLVMTy = getTypeConverter()->convertType(op.getResult().getType());
auto elementLLVMTy = getTypeConverter()->convertType(operandTy.getPointee());

Expand Down Expand Up @@ -3666,7 +3667,7 @@ mlir::LogicalResult CIRToLLVMPtrDiffOpLowering::matchAndRewrite(
auto diff =
rewriter.create<mlir::LLVM::SubOp>(op.getLoc(), llvmDstTy, lhs, rhs);

auto ptrTy = mlir::cast<cir::PointerType>(op.getLhs().getType());
cir::PointerType ptrTy = op.getLhs().getType();
auto typeSize = getTypeSize(ptrTy.getPointee(), *op);

// Avoid silly division by 1.
Expand Down
Loading