brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · 0559b49 Raw
100 lines · cpp
1//===- ExpressionSimplification.cpp - Simplify HLFIR expressions ----------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "flang/Optimizer/Builder/FIRBuilder.h"10#include "flang/Optimizer/HLFIR/HLFIROps.h"11#include "flang/Optimizer/HLFIR/Passes.h"12#include "mlir/Transforms/GreedyPatternRewriteDriver.h"13 14namespace hlfir {15#define GEN_PASS_DEF_EXPRESSIONSIMPLIFICATION16#include "flang/Optimizer/HLFIR/Passes.h.inc"17} // namespace hlfir18 19// Get the first user of `op`.20// Note that we consider the first user to be the one on the lowest line of21// the emitted HLFIR. The user iterator considers the opposite.22template <typename UserOp>23static UserOp getFirstUser(mlir::Operation *op) {24  auto it = op->user_begin(), end = op->user_end(), prev = it;25  for (; it != end; prev = it++)26    ;27  if (prev != end)28    if (auto userOp = mlir::dyn_cast<UserOp>(*prev))29      return userOp;30  return {};31}32 33// Get the last user of `op`.34// Note that we consider the last user to be the one on the highest line of35// the emitted HLFIR. The user iterator considers the opposite.36template <typename UserOp>37static UserOp getLastUser(mlir::Operation *op) {38  if (!op->getUsers().empty())39    if (auto userOp = mlir::dyn_cast<UserOp>(*op->user_begin()))40      return userOp;41  return {};42}43 44namespace {45 46// Trim operations can be erased in certain expressions, such as character47// comparisons.48// Since a character comparison appends spaces to the shorter character,49// calls to trim() that are used only in the comparison can be eliminated.50//51// Example:52// `trim(x) == trim(y)`53// can be simplified to54// `x == y`55class EraseTrim : public mlir::OpRewritePattern<hlfir::CharTrimOp> {56public:57  using mlir::OpRewritePattern<hlfir::CharTrimOp>::OpRewritePattern;58 59  llvm::LogicalResult60  matchAndRewrite(hlfir::CharTrimOp trimOp,61                  mlir::PatternRewriter &rewriter) const override {62    int trimUses = std::distance(trimOp->use_begin(), trimOp->use_end());63    auto cmpCharOp = getFirstUser<hlfir::CmpCharOp>(trimOp);64    auto destroyOp = getLastUser<hlfir::DestroyOp>(trimOp);65    if (!cmpCharOp || !destroyOp || trimUses != 2)66      return rewriter.notifyMatchFailure(67          trimOp, "hlfir.char_trim is not used (only) by hlfir.cmpchar");68 69    rewriter.eraseOp(destroyOp);70    rewriter.replaceOp(trimOp, trimOp.getChr());71    return mlir::success();72  }73};74 75class ExpressionSimplificationPass76    : public hlfir::impl::ExpressionSimplificationBase<77          ExpressionSimplificationPass> {78public:79  void runOnOperation() override {80    mlir::MLIRContext *context = &getContext();81 82    mlir::GreedyRewriteConfig config;83    // Prevent the pattern driver from merging blocks.84    config.setRegionSimplificationLevel(85        mlir::GreedySimplifyRegionLevel::Disabled);86 87    mlir::RewritePatternSet patterns(context);88    patterns.insert<EraseTrim>(context);89 90    if (mlir::failed(mlir::applyPatternsGreedily(91            getOperation(), std::move(patterns), config))) {92      mlir::emitError(getOperation()->getLoc(),93                      "failure in HLFIR expression simplification");94      signalPassFailure();95    }96  }97};98 99} // namespace100