@@ -34,7 +34,7 @@ namespace {
34
34
// / To:
35
35
// / ^bb0:
36
36
// / cir.return
37
- struct RemoveRedudantBranches : public OpRewritePattern <BrOp> {
37
+ struct RemoveRedundantBranches : public OpRewritePattern <BrOp> {
38
38
using OpRewritePattern<BrOp>::OpRewritePattern;
39
39
40
40
LogicalResult matchAndRewrite (BrOp op,
@@ -80,6 +80,22 @@ struct RemoveEmptySwitch : public OpRewritePattern<SwitchOp> {
80
80
}
81
81
};
82
82
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
+
83
99
// ===----------------------------------------------------------------------===//
84
100
// MergeCleanupsPass
85
101
// ===----------------------------------------------------------------------===//
@@ -101,9 +117,10 @@ struct MergeCleanupsPass : public MergeCleanupsBase<MergeCleanupsPass> {
101
117
void populateMergeCleanupPatterns (RewritePatternSet &patterns) {
102
118
// clang-format off
103
119
patterns.add <
104
- RemoveRedudantBranches ,
120
+ RemoveRedundantBranches ,
105
121
RemoveEmptyScope,
106
- RemoveEmptySwitch
122
+ RemoveEmptySwitch,
123
+ RemoveRedundantBitcast
107
124
>(patterns.getContext ());
108
125
// clang-format on
109
126
}
@@ -116,7 +133,7 @@ void MergeCleanupsPass::runOnOperation() {
116
133
// Collect operations to apply patterns.
117
134
SmallVector<Operation *, 16 > ops;
118
135
getOperation ()->walk ([&](Operation *op) {
119
- if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp>(op))
136
+ if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp >(op))
120
137
ops.push_back (op);
121
138
});
122
139
0 commit comments