Commit be3abd3e authored by seven-mile's avatar seven-mile
Browse files

fix: should return rhs

No related merge requests found
Showing with 10 additions and 7 deletions
+10 -7
...@@ -462,6 +462,13 @@ struct ConstantValue : Constant { ...@@ -462,6 +462,13 @@ struct ConstantValue : Constant {
int getInt() const { return std::get<int>(value); } int getInt() const { return std::get<int>(value); }
float getFloat() const { return std::get<float>(value); } float getFloat() const { return std::get<float>(value); }
bool isZero() const {
if (isInt())
return getInt() == 0;
else
return getFloat() == 0.0;
}
void print(std::ostream &os) const override { void print(std::ostream &os) const override {
getType()->print(os); getType()->print(os);
if (isInt()) if (isInt())
......
...@@ -19,7 +19,7 @@ public: ...@@ -19,7 +19,7 @@ public:
auto *lhs = inst->getLHS(), *rhs = inst->getRHS(); auto *lhs = inst->getLHS(), *rhs = inst->getRHS();
bool lhsConst = isa<ConstantValue>(lhs), rhsConst = isa<ConstantValue>(rhs); bool lhsConst = isa<ConstantValue>(lhs), rhsConst = isa<ConstantValue>(rhs);
// assert((!lhsConst || !rhsConst) && "should run constfold first"); // assert((!lhsConst || !rhsConst) && "should run constfold first");
if (lhsConst) { if (lhsConst && !rhsConst) {
if (inst->isCommutable()) { if (inst->isCommutable()) {
std::swap(lhs, rhs); std::swap(lhs, rhs);
std::swap(lhsConst, rhsConst); std::swap(lhsConst, rhsConst);
...@@ -44,13 +44,9 @@ public: ...@@ -44,13 +44,9 @@ public:
} }
} }
if (tag == Value::Tag::Sub && rhsConst) {
rhs = cast<ConstantValue>(rhs)->map([](auto &&arg) { return -arg; });
}
if ((tag == Value::Tag::Add || tag == Value::Tag::Sub) && rhsConst && if ((tag == Value::Tag::Add || tag == Value::Tag::Sub) && rhsConst &&
cast<ConstantValue>(rhs)->getInt() == 0) { cast<ConstantValue>(rhs)->isZero()) {
return rhs; return lhs;
} }
inst->tag = tag; inst->tag = tag;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment