1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements MemorySlot-related interfaces for CIR dialect
// operations.
//
//===----------------------------------------------------------------------===//
#include "clang/CIR/Dialect/IR/CIRDialect.h"
using namespace mlir;
/// Conditions the deletion of the operation to the removal of all its uses.
static bool forwardToUsers(Operation *op,
SmallVectorImpl<OpOperand *> &newBlockingUses) {
for (Value result : op->getResults())
for (OpOperand &use : result.getUses())
newBlockingUses.push_back(&use);
return true;
}
//===----------------------------------------------------------------------===//
// Interfaces for AllocaOp
//===----------------------------------------------------------------------===//
llvm::SmallVector<MemorySlot> cir::AllocaOp::getPromotableSlots() {
return {MemorySlot{getResult(), getAllocaType()}};
}
Value cir::AllocaOp::getDefaultValue(const MemorySlot &slot,
OpBuilder &builder) {
return builder.create<cir::ConstantOp>(getLoc(),
cir::UndefAttr::get(slot.elemType));
}
void cir::AllocaOp::handleBlockArgument(const MemorySlot &slot,
BlockArgument argument,
OpBuilder &builder) {}
std::optional<PromotableAllocationOpInterface>
cir::AllocaOp::handlePromotionComplete(const MemorySlot &slot,
Value defaultValue, OpBuilder &builder) {
if (defaultValue && defaultValue.use_empty())
defaultValue.getDefiningOp()->erase();
this->erase();
return std::nullopt;
}
//===----------------------------------------------------------------------===//
// Interfaces for LoadOp
//===----------------------------------------------------------------------===//
bool cir::LoadOp::loadsFrom(const MemorySlot &slot) {
return getAddr() == slot.ptr;
}
bool cir::LoadOp::storesTo(const MemorySlot &slot) { return false; }
Value cir::LoadOp::getStored(const MemorySlot &slot, OpBuilder &builder,
Value reachingDef, const DataLayout &dataLayout) {
llvm_unreachable("getStored should not be called on LoadOp");
}
bool cir::LoadOp::canUsesBeRemoved(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
SmallVectorImpl<OpOperand *> &newBlockingUses,
const DataLayout &dataLayout) {
if (blockingUses.size() != 1)
return false;
Value blockingUse = (*blockingUses.begin())->get();
return blockingUse == slot.ptr && getAddr() == slot.ptr &&
getType() == slot.elemType;
}
DeletionKind cir::LoadOp::removeBlockingUses(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
OpBuilder &builder, Value reachingDefinition,
const DataLayout &dataLayout) {
getResult().replaceAllUsesWith(reachingDefinition);
return DeletionKind::Delete;
}
//===----------------------------------------------------------------------===//
// Interfaces for StoreOp
//===----------------------------------------------------------------------===//
bool cir::StoreOp::loadsFrom(const MemorySlot &slot) { return false; }
bool cir::StoreOp::storesTo(const MemorySlot &slot) {
return getAddr() == slot.ptr;
}
Value cir::StoreOp::getStored(const MemorySlot &slot, OpBuilder &builder,
Value reachingDef, const DataLayout &dataLayout) {
return getValue();
}
bool cir::StoreOp::canUsesBeRemoved(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
SmallVectorImpl<OpOperand *> &newBlockingUses,
const DataLayout &dataLayout) {
if (blockingUses.size() != 1)
return false;
Value blockingUse = (*blockingUses.begin())->get();
return blockingUse == slot.ptr && getAddr() == slot.ptr &&
getValue() != slot.ptr && slot.elemType == getValue().getType();
}
DeletionKind cir::StoreOp::removeBlockingUses(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
OpBuilder &builder, Value reachingDefinition,
const DataLayout &dataLayout) {
return DeletionKind::Delete;
}
//===----------------------------------------------------------------------===//
// Interfaces for CopyOp
//===----------------------------------------------------------------------===//
bool cir::CopyOp::loadsFrom(const MemorySlot &slot) {
return getSrc() == slot.ptr;
}
bool cir::CopyOp::storesTo(const MemorySlot &slot) {
return getDst() == slot.ptr;
}
Value cir::CopyOp::getStored(const MemorySlot &slot, OpBuilder &builder,
Value reachingDef, const DataLayout &dataLayout) {
return cir::LoadOp::create(builder, getLoc(), slot.elemType, getSrc());
}
DeletionKind cir::CopyOp::removeBlockingUses(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
OpBuilder &builder, mlir::Value reachingDefinition,
const DataLayout &dataLayout) {
if (loadsFrom(slot))
cir::StoreOp::create(builder, getLoc(), reachingDefinition, getDst(),
/*isVolatile=*/false,
/*alignment=*/mlir::IntegerAttr{},
/*mem-order=*/cir::MemOrderAttr());
return DeletionKind::Delete;
}
bool cir::CopyOp::canUsesBeRemoved(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
SmallVectorImpl<OpOperand *> &newBlockingUses,
const DataLayout &dataLayout) {
if (getDst() == getSrc())
return false;
return getLength(dataLayout) == dataLayout.getTypeSize(slot.elemType);
}
//===----------------------------------------------------------------------===//
// Interfaces for CastOp
//===----------------------------------------------------------------------===//
bool cir::CastOp::canUsesBeRemoved(
const SmallPtrSetImpl<OpOperand *> &blockingUses,
SmallVectorImpl<OpOperand *> &newBlockingUses,
const DataLayout &dataLayout) {
if (getKind() == cir::CastKind::bitcast)
return forwardToUsers(*this, newBlockingUses);
return false;
}
DeletionKind cir::CastOp::removeBlockingUses(
const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
return DeletionKind::Delete;
}
|