Skip to content

Commit 7557df9

Browse files
committed
[CIR][Transforms] Simplify redundant bitcasts
1 parent 7655c70 commit 7557df9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

clang/lib/CIR/Dialect/Transforms/MergeCleanups.cpp

+23-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace {
3434
/// To:
3535
/// ^bb0:
3636
/// cir.return
37-
struct RemoveRedudantBranches : public OpRewritePattern<BrOp> {
37+
struct RemoveRedundantBranches : public OpRewritePattern<BrOp> {
3838
using OpRewritePattern<BrOp>::OpRewritePattern;
3939

4040
LogicalResult matchAndRewrite(BrOp op,
@@ -80,6 +80,24 @@ struct RemoveEmptySwitch : public OpRewritePattern<SwitchOp> {
8080
}
8181
};
8282

83+
struct RemoveRedundantBitcast : OpRewritePattern<CastOp> {
84+
using OpRewritePattern<CastOp>::OpRewritePattern;
85+
86+
LogicalResult matchAndRewrite(CastOp op,
87+
PatternRewriter &rewriter) const final {
88+
if (op.getKind() != mlir::cir::CastKind::bitcast) {
89+
return failure();
90+
}
91+
llvm::errs() << "RemoveRedundantBitcast\n";
92+
op.dump();
93+
if (op.getSrc().getType() != op.getResult().getType()) {
94+
return failure();
95+
}
96+
rewriter.replaceOp(op, op.getSrc());
97+
return success();
98+
}
99+
};
100+
83101
//===----------------------------------------------------------------------===//
84102
// MergeCleanupsPass
85103
//===----------------------------------------------------------------------===//
@@ -101,9 +119,10 @@ struct MergeCleanupsPass : public MergeCleanupsBase<MergeCleanupsPass> {
101119
void populateMergeCleanupPatterns(RewritePatternSet &patterns) {
102120
// clang-format off
103121
patterns.add<
104-
RemoveRedudantBranches,
122+
RemoveRedundantBranches,
105123
RemoveEmptyScope,
106-
RemoveEmptySwitch
124+
RemoveEmptySwitch,
125+
RemoveRedundantBitcast
107126
>(patterns.getContext());
108127
// clang-format on
109128
}
@@ -116,7 +135,7 @@ void MergeCleanupsPass::runOnOperation() {
116135
// Collect operations to apply patterns.
117136
SmallVector<Operation *, 16> ops;
118137
getOperation()->walk([&](Operation *op) {
119-
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp>(op))
138+
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp>(op))
120139
ops.push_back(op);
121140
});
122141

clang/test/CIR/CodeGen/no-prototype.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ int noProto2();
3636
int test2(int x) {
3737
return noProto2(x);
3838
// CHECK: [[GGO:%.*]] = cir.get_global @noProto2 : !cir.ptr<!cir.func<!s32i (!s32i)>>
39-
// CHECK: [[CAST:%.*]] = cir.cast(bitcast, %3 : !cir.ptr<!cir.func<!s32i (!s32i)>>), !cir.ptr<!cir.func<!s32i (!s32i)>>
40-
// CHECK: {{.*}} = cir.call [[CAST]](%{{[0-9]+}}) : (!cir.ptr<!cir.func<!s32i (!s32i)>>, !s32i) -> !s32i
39+
// CHECK: {{.*}} = cir.call [[GGO]](%{{[0-9]+}}) : (!cir.ptr<!cir.func<!s32i (!s32i)>>, !s32i) -> !s32i
4140
}
4241
int noProto2(int x) { return x; }
4342
// CHECK: cir.func no_proto @noProto2(%arg0: !s32i {{.+}}) -> !s32i

0 commit comments

Comments
 (0)