[llvm] Reland: Port Swift's merge function pass to llvm: merging functions that differ in constants (PR #71584)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 7 13:11:57 PST 2023


================
@@ -0,0 +1,164 @@
+//===--- FunctionComparatorIgnoringConst.cpp - Function Comparator --------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Utils/FunctionComparatorIgnoringConst.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Transforms/Utils/MergeFunctionsIgnoringConst.h"
+
+using namespace llvm;
+
+/// Returns true if the \OpIdx operand of \p CI is the callee operand.
+static bool isCalleeOperand(const CallBase *CI, unsigned OpIdx) {
+  return &CI->getCalledOperandUse() == &CI->getOperandUse(OpIdx);
+}
+
+bool isEligibleInstrunctionForConstantSharing(const Instruction *I) {
+  switch (I->getOpcode()) {
+  case Instruction::Load:
+  case Instruction::Store:
+  case Instruction::Call:
+    return true;
+  default: {
+    return false;
+  }
+  }
+}
+
+static bool canParameterizeCallOperand(const CallBase *CI, unsigned OpIdx) {
----------------
nikic wrote:

We have canReplaceOperandWithVariable() for this general purpose. I think we should use that one, and if necessary extend it with additional checks. (Any such extensions should be done in a separate review though.)

https://github.com/llvm/llvm-project/pull/71584


More information about the llvm-commits mailing list