diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 5ef5b60ed5a52..72841a1c08441 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -291,6 +291,9 @@ def CIR_ConstantOp : CIR_Op<"const", [ return ptrAttr.isNullValue(); return false; } + + template + T getValueAttr() { return mlir::dyn_cast(getValue()); } }]; let hasFolder = 1; diff --git a/clang/lib/CIR/CodeGen/CIRGenClass.cpp b/clang/lib/CIR/CodeGen/CIRGenClass.cpp index 50cca0e63611e..72b9d177e4c63 100644 --- a/clang/lib/CIR/CodeGen/CIRGenClass.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenClass.cpp @@ -349,12 +349,16 @@ void CIRGenFunction::emitCXXAggrConstructorCall( // doesn't happen, but it's not clear that it's worth it. // Optimize for a constant count. - auto constantCount = dyn_cast(numElements.getDefiningOp()); - if (constantCount) { - auto constIntAttr = mlir::dyn_cast(constantCount.getValue()); - // Just skip out if the constant count is zero. - if (constIntAttr && constIntAttr.getUInt() == 0) - return; + if (auto constantCount = numElements.getDefiningOp()) { + if (auto constIntAttr = constantCount.getValueAttr()) { + // Just skip out if the constant count is zero. + if (constIntAttr.getUInt() == 0) + return; + // Otherwise, emit the check. + } + + if (constantCount.use_empty()) + constantCount.erase(); } else { // Otherwise, emit the check. cgm.errorNYI(e->getSourceRange(), "dynamic-length array expression"); @@ -417,9 +421,6 @@ void CIRGenFunction::emitCXXAggrConstructorCall( builder.create(loc); }); } - - if (constantCount.use_empty()) - constantCount.erase(); } void CIRGenFunction::emitDelegateCXXConstructorCall( diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index 27a53b7c41090..761d8d304e973 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -721,8 +721,8 @@ static const Expr *getSimpleArrayDecayOperand(const Expr *e) { static cir::IntAttr getConstantIndexOrNull(mlir::Value idx) { // TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr? - if (auto constantOp = dyn_cast(idx.getDefiningOp())) - return mlir::dyn_cast(constantOp.getValue()); + if (auto constantOp = idx.getDefiningOp()) + return constantOp.getValueAttr(); return {}; } @@ -730,8 +730,7 @@ static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx, CharUnits eltSize) { // If we have a constant index, we can use the exact offset of the // element we're accessing. - const cir::IntAttr constantIdx = getConstantIndexOrNull(idx); - if (constantIdx) { + if (const cir::IntAttr constantIdx = getConstantIndexOrNull(idx)) { const CharUnits offset = constantIdx.getValue().getZExtValue() * eltSize; return arrayAlign.alignmentAtOffset(offset); } diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index a95b51d96b30a..32c1c1ada3c53 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -48,8 +48,8 @@ struct BinOpInfo { /// Check if the binop can result in integer overflow. bool mayHaveIntegerOverflow() const { // Without constant input, we can't rule out overflow. - auto lhsci = dyn_cast(lhs.getDefiningOp()); - auto rhsci = dyn_cast(rhs.getDefiningOp()); + auto lhsci = lhs.getDefiningOp(); + auto rhsci = rhs.getDefiningOp(); if (!lhsci || !rhsci) return true; diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 1c3a31091aa8d..263ff15d9e005 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -1833,7 +1833,7 @@ LogicalResult cir::GetMemberOp::verify() { OpFoldResult cir::VecCreateOp::fold(FoldAdaptor adaptor) { if (llvm::any_of(getElements(), [](mlir::Value value) { - return !mlir::isa(value.getDefiningOp()); + return !value.getDefiningOp(); })) return {}; diff --git a/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp b/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp index 3b7f08c441405..3c6f76892d5cb 100644 --- a/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp +++ b/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp @@ -97,8 +97,8 @@ struct SimplifyTernary final : public OpRewritePattern { // Check whether the region/block contains a cir.const followed by a // cir.yield that yields the value. auto yieldOp = mlir::cast(onlyBlock.getTerminator()); - auto yieldValueDefOp = mlir::dyn_cast_if_present( - yieldOp.getArgs()[0].getDefiningOp()); + auto yieldValueDefOp = + yieldOp.getArgs()[0].getDefiningOp(); return yieldValueDefOp && yieldValueDefOp->getBlock() == &onlyBlock; } }; @@ -126,18 +126,13 @@ struct SimplifySelect : public OpRewritePattern { LogicalResult matchAndRewrite(SelectOp op, PatternRewriter &rewriter) const final { - mlir::Operation *trueValueOp = op.getTrueValue().getDefiningOp(); - mlir::Operation *falseValueOp = op.getFalseValue().getDefiningOp(); - auto trueValueConstOp = - mlir::dyn_cast_if_present(trueValueOp); - auto falseValueConstOp = - mlir::dyn_cast_if_present(falseValueOp); - if (!trueValueConstOp || !falseValueConstOp) + auto trueValueOp = op.getTrueValue().getDefiningOp(); + auto falseValueOp = op.getFalseValue().getDefiningOp(); + if (!trueValueOp || !falseValueOp) return mlir::failure(); - auto trueValue = mlir::dyn_cast(trueValueConstOp.getValue()); - auto falseValue = - mlir::dyn_cast(falseValueConstOp.getValue()); + auto trueValue = trueValueOp.getValueAttr(); + auto falseValue = falseValueOp.getValueAttr(); if (!trueValue || !falseValue) return mlir::failure(); @@ -265,8 +260,7 @@ struct SimplifyVecSplat : public OpRewritePattern { LogicalResult matchAndRewrite(VecSplatOp op, PatternRewriter &rewriter) const override { mlir::Value splatValue = op.getValue(); - auto constant = - mlir::dyn_cast_if_present(splatValue.getDefiningOp()); + auto constant = splatValue.getDefiningOp(); if (!constant) return mlir::failure(); diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 957a51ab334aa..895872b6a14db 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -1935,12 +1935,11 @@ mlir::LogicalResult CIRToLLVMSelectOpLowering::matchAndRewrite( cir::SelectOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { auto getConstantBool = [](mlir::Value value) -> cir::BoolAttr { - auto definingOp = - mlir::dyn_cast_if_present(value.getDefiningOp()); + auto definingOp = value.getDefiningOp(); if (!definingOp) return {}; - auto constValue = mlir::dyn_cast(definingOp.getValue()); + auto constValue = definingOp.getValueAttr(); if (!constValue) return {};