std::unique_ptr<Expression> divide(Expression *dividend, SignedConstantValue divisor) { assert(divisor != 0); if (auto constant = dividend->as<IntegerConstant>()) { if (constant->value().signedValue() % divisor == 0) { return std::make_unique<IntegerConstant>( SizedValue(constant->value().size(), constant->value().signedValue() / divisor), constant->type()); } } else if (auto binary = dividend->as<BinaryOperator>()) { if (binary->operatorKind() == BinaryOperator::MUL) { if (auto result = divide(binary->left().get(), divisor)) { return std::make_unique<BinaryOperator>(BinaryOperator::MUL, std::move(result), std::move(binary->right())); } else if (auto result = divide(binary->right().get(), divisor)) { return std::make_unique<BinaryOperator>(BinaryOperator::MUL, std::move(binary->left()), std::move(result)); } } } return nullptr; }
IntegerConstant::IntegerConstant(ConstantValue value, const IntegerType *type): Expression(INTEGER_CONSTANT), value_(SizedValue(type->size(), value)), type_(type) {}