1*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric //
9*700637cbSDimitry Andric // This file implements MemorySlot-related interfaces for CIR dialect
10*700637cbSDimitry Andric // operations.
11*700637cbSDimitry Andric //
12*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
13*700637cbSDimitry Andric
14*700637cbSDimitry Andric #include "clang/CIR/Dialect/IR/CIRDialect.h"
15*700637cbSDimitry Andric
16*700637cbSDimitry Andric using namespace mlir;
17*700637cbSDimitry Andric
18*700637cbSDimitry Andric /// Conditions the deletion of the operation to the removal of all its uses.
forwardToUsers(Operation * op,SmallVectorImpl<OpOperand * > & newBlockingUses)19*700637cbSDimitry Andric static bool forwardToUsers(Operation *op,
20*700637cbSDimitry Andric SmallVectorImpl<OpOperand *> &newBlockingUses) {
21*700637cbSDimitry Andric for (Value result : op->getResults())
22*700637cbSDimitry Andric for (OpOperand &use : result.getUses())
23*700637cbSDimitry Andric newBlockingUses.push_back(&use);
24*700637cbSDimitry Andric return true;
25*700637cbSDimitry Andric }
26*700637cbSDimitry Andric
27*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
28*700637cbSDimitry Andric // Interfaces for AllocaOp
29*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
30*700637cbSDimitry Andric
getPromotableSlots()31*700637cbSDimitry Andric llvm::SmallVector<MemorySlot> cir::AllocaOp::getPromotableSlots() {
32*700637cbSDimitry Andric return {MemorySlot{getResult(), getAllocaType()}};
33*700637cbSDimitry Andric }
34*700637cbSDimitry Andric
getDefaultValue(const MemorySlot & slot,OpBuilder & builder)35*700637cbSDimitry Andric Value cir::AllocaOp::getDefaultValue(const MemorySlot &slot,
36*700637cbSDimitry Andric OpBuilder &builder) {
37*700637cbSDimitry Andric return builder.create<cir::ConstantOp>(getLoc(),
38*700637cbSDimitry Andric cir::UndefAttr::get(slot.elemType));
39*700637cbSDimitry Andric }
40*700637cbSDimitry Andric
handleBlockArgument(const MemorySlot & slot,BlockArgument argument,OpBuilder & builder)41*700637cbSDimitry Andric void cir::AllocaOp::handleBlockArgument(const MemorySlot &slot,
42*700637cbSDimitry Andric BlockArgument argument,
43*700637cbSDimitry Andric OpBuilder &builder) {}
44*700637cbSDimitry Andric
45*700637cbSDimitry Andric std::optional<PromotableAllocationOpInterface>
handlePromotionComplete(const MemorySlot & slot,Value defaultValue,OpBuilder & builder)46*700637cbSDimitry Andric cir::AllocaOp::handlePromotionComplete(const MemorySlot &slot,
47*700637cbSDimitry Andric Value defaultValue, OpBuilder &builder) {
48*700637cbSDimitry Andric if (defaultValue && defaultValue.use_empty())
49*700637cbSDimitry Andric defaultValue.getDefiningOp()->erase();
50*700637cbSDimitry Andric this->erase();
51*700637cbSDimitry Andric return std::nullopt;
52*700637cbSDimitry Andric }
53*700637cbSDimitry Andric
54*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
55*700637cbSDimitry Andric // Interfaces for LoadOp
56*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
57*700637cbSDimitry Andric
loadsFrom(const MemorySlot & slot)58*700637cbSDimitry Andric bool cir::LoadOp::loadsFrom(const MemorySlot &slot) {
59*700637cbSDimitry Andric return getAddr() == slot.ptr;
60*700637cbSDimitry Andric }
61*700637cbSDimitry Andric
storesTo(const MemorySlot & slot)62*700637cbSDimitry Andric bool cir::LoadOp::storesTo(const MemorySlot &slot) { return false; }
63*700637cbSDimitry Andric
getStored(const MemorySlot & slot,OpBuilder & builder,Value reachingDef,const DataLayout & dataLayout)64*700637cbSDimitry Andric Value cir::LoadOp::getStored(const MemorySlot &slot, OpBuilder &builder,
65*700637cbSDimitry Andric Value reachingDef, const DataLayout &dataLayout) {
66*700637cbSDimitry Andric llvm_unreachable("getStored should not be called on LoadOp");
67*700637cbSDimitry Andric }
68*700637cbSDimitry Andric
canUsesBeRemoved(const MemorySlot & slot,const SmallPtrSetImpl<OpOperand * > & blockingUses,SmallVectorImpl<OpOperand * > & newBlockingUses,const DataLayout & dataLayout)69*700637cbSDimitry Andric bool cir::LoadOp::canUsesBeRemoved(
70*700637cbSDimitry Andric const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
71*700637cbSDimitry Andric SmallVectorImpl<OpOperand *> &newBlockingUses,
72*700637cbSDimitry Andric const DataLayout &dataLayout) {
73*700637cbSDimitry Andric if (blockingUses.size() != 1)
74*700637cbSDimitry Andric return false;
75*700637cbSDimitry Andric Value blockingUse = (*blockingUses.begin())->get();
76*700637cbSDimitry Andric return blockingUse == slot.ptr && getAddr() == slot.ptr &&
77*700637cbSDimitry Andric getType() == slot.elemType;
78*700637cbSDimitry Andric }
79*700637cbSDimitry Andric
removeBlockingUses(const MemorySlot & slot,const SmallPtrSetImpl<OpOperand * > & blockingUses,OpBuilder & builder,Value reachingDefinition,const DataLayout & dataLayout)80*700637cbSDimitry Andric DeletionKind cir::LoadOp::removeBlockingUses(
81*700637cbSDimitry Andric const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
82*700637cbSDimitry Andric OpBuilder &builder, Value reachingDefinition,
83*700637cbSDimitry Andric const DataLayout &dataLayout) {
84*700637cbSDimitry Andric getResult().replaceAllUsesWith(reachingDefinition);
85*700637cbSDimitry Andric return DeletionKind::Delete;
86*700637cbSDimitry Andric }
87*700637cbSDimitry Andric
88*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
89*700637cbSDimitry Andric // Interfaces for StoreOp
90*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
91*700637cbSDimitry Andric
loadsFrom(const MemorySlot & slot)92*700637cbSDimitry Andric bool cir::StoreOp::loadsFrom(const MemorySlot &slot) { return false; }
93*700637cbSDimitry Andric
storesTo(const MemorySlot & slot)94*700637cbSDimitry Andric bool cir::StoreOp::storesTo(const MemorySlot &slot) {
95*700637cbSDimitry Andric return getAddr() == slot.ptr;
96*700637cbSDimitry Andric }
97*700637cbSDimitry Andric
getStored(const MemorySlot & slot,OpBuilder & builder,Value reachingDef,const DataLayout & dataLayout)98*700637cbSDimitry Andric Value cir::StoreOp::getStored(const MemorySlot &slot, OpBuilder &builder,
99*700637cbSDimitry Andric Value reachingDef, const DataLayout &dataLayout) {
100*700637cbSDimitry Andric return getValue();
101*700637cbSDimitry Andric }
102*700637cbSDimitry Andric
canUsesBeRemoved(const MemorySlot & slot,const SmallPtrSetImpl<OpOperand * > & blockingUses,SmallVectorImpl<OpOperand * > & newBlockingUses,const DataLayout & dataLayout)103*700637cbSDimitry Andric bool cir::StoreOp::canUsesBeRemoved(
104*700637cbSDimitry Andric const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
105*700637cbSDimitry Andric SmallVectorImpl<OpOperand *> &newBlockingUses,
106*700637cbSDimitry Andric const DataLayout &dataLayout) {
107*700637cbSDimitry Andric if (blockingUses.size() != 1)
108*700637cbSDimitry Andric return false;
109*700637cbSDimitry Andric Value blockingUse = (*blockingUses.begin())->get();
110*700637cbSDimitry Andric return blockingUse == slot.ptr && getAddr() == slot.ptr &&
111*700637cbSDimitry Andric getValue() != slot.ptr && slot.elemType == getValue().getType();
112*700637cbSDimitry Andric }
113*700637cbSDimitry Andric
removeBlockingUses(const MemorySlot & slot,const SmallPtrSetImpl<OpOperand * > & blockingUses,OpBuilder & builder,Value reachingDefinition,const DataLayout & dataLayout)114*700637cbSDimitry Andric DeletionKind cir::StoreOp::removeBlockingUses(
115*700637cbSDimitry Andric const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
116*700637cbSDimitry Andric OpBuilder &builder, Value reachingDefinition,
117*700637cbSDimitry Andric const DataLayout &dataLayout) {
118*700637cbSDimitry Andric return DeletionKind::Delete;
119*700637cbSDimitry Andric }
120*700637cbSDimitry Andric
121*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
122*700637cbSDimitry Andric // Interfaces for CastOp
123*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
124*700637cbSDimitry Andric
canUsesBeRemoved(const SmallPtrSetImpl<OpOperand * > & blockingUses,SmallVectorImpl<OpOperand * > & newBlockingUses,const DataLayout & dataLayout)125*700637cbSDimitry Andric bool cir::CastOp::canUsesBeRemoved(
126*700637cbSDimitry Andric const SmallPtrSetImpl<OpOperand *> &blockingUses,
127*700637cbSDimitry Andric SmallVectorImpl<OpOperand *> &newBlockingUses,
128*700637cbSDimitry Andric const DataLayout &dataLayout) {
129*700637cbSDimitry Andric if (getKind() == cir::CastKind::bitcast)
130*700637cbSDimitry Andric return forwardToUsers(*this, newBlockingUses);
131*700637cbSDimitry Andric return false;
132*700637cbSDimitry Andric }
133*700637cbSDimitry Andric
removeBlockingUses(const SmallPtrSetImpl<OpOperand * > & blockingUses,OpBuilder & builder)134*700637cbSDimitry Andric DeletionKind cir::CastOp::removeBlockingUses(
135*700637cbSDimitry Andric const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
136*700637cbSDimitry Andric return DeletionKind::Delete;
137*700637cbSDimitry Andric }
138