Skip to content

Commit 0450e12

Browse files
committed
[CIR][Transforms] Simplify redundant bitcasts
1 parent 7655c70 commit 0450e12

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

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

+21-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,22 @@ 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+
if (op.getSrc().getType() != op.getResult().getType()) {
92+
return failure();
93+
}
94+
rewriter.replaceOp(op, op.getSrc());
95+
return success();
96+
}
97+
};
98+
8399
//===----------------------------------------------------------------------===//
84100
// MergeCleanupsPass
85101
//===----------------------------------------------------------------------===//
@@ -101,9 +117,10 @@ struct MergeCleanupsPass : public MergeCleanupsBase<MergeCleanupsPass> {
101117
void populateMergeCleanupPatterns(RewritePatternSet &patterns) {
102118
// clang-format off
103119
patterns.add<
104-
RemoveRedudantBranches,
120+
RemoveRedundantBranches,
105121
RemoveEmptyScope,
106-
RemoveEmptySwitch
122+
RemoveEmptySwitch,
123+
RemoveRedundantBitcast
107124
>(patterns.getContext());
108125
// clang-format on
109126
}
@@ -116,7 +133,7 @@ void MergeCleanupsPass::runOnOperation() {
116133
// Collect operations to apply patterns.
117134
SmallVector<Operation *, 16> ops;
118135
getOperation()->walk([&](Operation *op) {
119-
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp>(op))
136+
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp>(op))
120137
ops.push_back(op);
121138
});
122139

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)