1//===- MemorySlotUtils.cpp - Utilities for MemorySlot interfaces ----------===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements common utilities for implementing MemorySlot interfaces,
10// in particular PromotableRegionOpInterface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Interfaces/Utils/MemorySlotUtils.h"
15
16using namespace mlir;
17
18void mlir::memoryslot::updateTerminator(
19 Block *block, Value defaultReachingDef,
20 const DenseMap<Block *, Value> &reachingAtBlockEnd) {
21 Value blockReachingDef = reachingAtBlockEnd.lookup(Val: block);
22 if (!blockReachingDef)
23 blockReachingDef = defaultReachingDef;
24 Operation *terminator = block->getTerminator();
25 terminator->insertOperands(index: terminator->getNumOperands(), operands: {blockReachingDef});
26}
27
28Operation *mlir::memoryslot::replaceWithNewResults(RewriterBase &rewriter,
29 Operation *op,
30 TypeRange resultTypes) {
31 RewriterBase::InsertionGuard guard(rewriter);
32 rewriter.setInsertionPoint(op);
33 OperationState state(op->getLoc(), op->getName(), op->getOperands(),
34 resultTypes,
35 op->getDiscardableAttrDictionary().getValue());
36 state.propertiesAttr = op->getPropertiesAsAttribute();
37 unsigned numRegions = op->getNumRegions();
38 for (unsigned i = 0; i < numRegions; ++i)
39 state.addRegion();
40 Operation *newOp = rewriter.create(state);
41 rewriter.startOpModification(op: newOp);
42 rewriter.startOpModification(op);
43 for (unsigned i = 0; i < numRegions; ++i)
44 newOp->getRegion(index: i).takeBody(other&: op->getRegion(index: i));
45 rewriter.finalizeOpModification(op);
46 rewriter.finalizeOpModification(op: newOp);
47
48 rewriter.replaceAllOpUsesWith(
49 from: op, to: newOp->getResults().take_front(n: op->getNumResults()));
50 rewriter.eraseOp(op);
51 return newOp;
52}
53