[flang-commits] [flang] [flang] Simplify the comparison of characters (PR #154593)
Leandro Lupori via flang-commits
flang-commits at lists.llvm.org
Tue Aug 26 12:59:48 PDT 2025
================
@@ -0,0 +1,294 @@
+#include "flang/Optimizer/Builder/FIRBuilder.h"
+#include "flang/Optimizer/HLFIR/HLFIROps.h"
+#include "flang/Optimizer/HLFIR/Passes.h"
+#include "flang/Runtime/entry-names.h"
+#include "llvm/Support/DebugLog.h"
+
+namespace hlfir {
+#define GEN_PASS_DEF_EXPRESSIONSIMPLIFICATION
+#include "flang/Optimizer/HLFIR/Passes.h.inc"
+} // namespace hlfir
+
+#define DEBUG_TYPE "expression-simplification"
+
+#define INDENT(n) std::string((n) * 2, ' ')
+
+static void removeOperands(mlir::Operation *op, int nestLevel);
+
+static void removeOp(mlir::Operation *op, int parentUses, int nestLevel) {
+ int opUses = std::distance(op->getUses().begin(), op->getUses().end());
+
+ if (opUses <= parentUses) {
+ LDBG() << INDENT(nestLevel) << "remove: " << *op;
+ removeOperands(op, nestLevel);
+ op->dropAllReferences();
+ op->dropAllUses();
+ op->erase();
+ }
+}
+
+static void removeOp(mlir::Operation *op) {
+ removeOp(op, /*parentUses=*/0, /*nestLevel=*/0);
+ LDBG();
+}
+
+static void removeOperands(mlir::Operation *op, int nestLevel) {
+ for (mlir::Value operand : op->getOperands()) {
+ if (!operand)
+ // Already removed.
+ continue;
+ if (mlir::Operation *operandOp = operand.getDefiningOp()) {
+ int uses = 0;
+ for (mlir::Operation *user : operandOp->getUsers())
+ if (user == op)
+ ++uses;
+ removeOp(operandOp, uses, nestLevel + 1);
+ }
+ }
+}
+
+template <typename UserOp>
+static UserOp getFirstUser(mlir::Operation *op) {
+ auto it = op->user_begin(), end = op->user_end(), prev = it;
+ for (; it != end; prev = it++)
+ ;
+ if (prev != end)
+ if (auto userOp = mlir::dyn_cast<UserOp>(*prev))
+ return userOp;
+ return {};
+}
+
+template <typename UserOp>
+static UserOp getLastUser(mlir::Operation *op) {
+ if (!op->getUsers().empty())
+ if (auto userOp = mlir::dyn_cast<UserOp>(*op->user_begin()))
+ return userOp;
+ return {};
+}
+
+template <typename UserOp>
+static UserOp getPreviousUser(mlir::Operation *op, mlir::Operation *curUser) {
----------------
luporl wrote:
What I have observed is that the user at `begin` is on the highest line in the emitted HLFIR, while `--end` is on the lowest one. That's why it made more sense to me to name these functions in this way, instead of considering the first user to be that returned by `begin`.
Do you think it would be OK to just add a comment explaining this?
https://github.com/llvm/llvm-project/pull/154593
More information about the flang-commits
mailing list