CIRCT 23.0.0git
Loading...
Searching...
No Matches
AssumeTwoValued.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
12
13using namespace circt;
14using namespace circt::comb;
15
16namespace circt {
17namespace comb {
18#define GEN_PASS_DEF_ASSUMETWOVALUED
19#include "circt/Dialect/Comb/Passes.h.inc"
20} // namespace comb
21} // namespace circt
22
23namespace {
24struct ICmpOpConversion : OpRewritePattern<ICmpOp> {
26
27 LogicalResult matchAndRewrite(ICmpOp op,
28 PatternRewriter &rewriter) const override {
29 ICmpPredicate newPredicate;
30 switch (op.getPredicate()) {
31 case ICmpPredicate::ceq:
32 case ICmpPredicate::weq:
33 newPredicate = ICmpPredicate::eq;
34 break;
35 case ICmpPredicate::cne:
36 case ICmpPredicate::wne:
37 newPredicate = ICmpPredicate::ne;
38 break;
39 default:
40 return failure();
41 }
42 rewriter.replaceOpWithNewOp<ICmpOp>(op, newPredicate, op.getLhs(),
43 op.getRhs(), /*twoState=*/true);
44 return success();
45 }
46};
47
48template <typename OpTy>
49struct AddTwoStateFlag : OpRewritePattern<OpTy> {
51
52 LogicalResult matchAndRewrite(OpTy op,
53 PatternRewriter &rewriter) const override {
54 if (op.getTwoState())
55 return failure();
56 rewriter.modifyOpInPlace(op, [&] { op.setTwoState(true); });
57 return success();
58 }
59};
60
61} // namespace
62
63namespace {
64class AssumeTwoValued : public impl::AssumeTwoValuedBase<AssumeTwoValued> {
65public:
66 using AssumeTwoValuedBase::AssumeTwoValuedBase;
67
68 void runOnOperation() override;
69};
70} // namespace
71
72// Alias for brevity.
73template <typename OpTy>
74using TS = AddTwoStateFlag<OpTy>;
75
76void AssumeTwoValued::runOnOperation() {
77 auto *ctx = &getContext();
78 RewritePatternSet patterns(ctx);
79
81 .add<ICmpOpConversion, TS<AddOp>, TS<AndOp>, TS<DivSOp>, TS<DivUOp>,
84
85 if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
86 return signalPassFailure();
87}
AddTwoStateFlag< OpTy > TS
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition comb.py:1