@@ -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,24 @@ 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
+ 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
+
83
101
// ===----------------------------------------------------------------------===//
84
102
// MergeCleanupsPass
85
103
// ===----------------------------------------------------------------------===//
@@ -101,9 +119,10 @@ struct MergeCleanupsPass : public MergeCleanupsBase<MergeCleanupsPass> {
101
119
void populateMergeCleanupPatterns (RewritePatternSet &patterns) {
102
120
// clang-format off
103
121
patterns.add <
104
- RemoveRedudantBranches ,
122
+ RemoveRedundantBranches ,
105
123
RemoveEmptyScope,
106
- RemoveEmptySwitch
124
+ RemoveEmptySwitch,
125
+ RemoveRedundantBitcast
107
126
>(patterns.getContext ());
108
127
// clang-format on
109
128
}
@@ -116,7 +135,7 @@ void MergeCleanupsPass::runOnOperation() {
116
135
// Collect operations to apply patterns.
117
136
SmallVector<Operation *, 16 > ops;
118
137
getOperation ()->walk ([&](Operation *op) {
119
- if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp>(op))
138
+ if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp >(op))
120
139
ops.push_back (op);
121
140
});
122
141
0 commit comments