clang 24.0.0git
CIRForwardReturnSlot.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 a pass that returns the value stored to a function's
10// return slot directly, when that store reaches the only return or
11// immediately precedes the load of the slot.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PassDetail.h"
19#include "llvm/ADT/SmallPtrSet.h"
20
21using namespace mlir;
22
23namespace mlir {
24#define GEN_PASS_DEF_CIRFORWARDRETURNSLOT
25#include "clang/CIR/Dialect/Passes.h.inc"
26} // namespace mlir
27
28namespace {
29
30struct CIRForwardReturnSlotPass
31 : public impl::CIRForwardReturnSlotBase<CIRForwardReturnSlotPass> {
32 using CIRForwardReturnSlotBase::CIRForwardReturnSlotBase;
33
34 void runOnOperation() override;
35};
36
37/// Returns `op` as a store to `slot` that may be forwarded, or null.
38static cir::StoreOp getStoreToSlot(Operation *op, Value slot) {
39 auto store = dyn_cast_if_present<cir::StoreOp>(op);
40 // Classic codegen also forwards the volatile stores that every memory access
41 // in a function with an SEH __try becomes.
43 if (!store || store.getAddr() != slot || store.isVolatileOrAtomic())
44 return {};
45 return store;
46}
47
48/// Returns true if `value` is used after `op` in `op`'s block.
49static bool isUsedAfter(Value value, Operation *op) {
50 Block *block = op->getBlock();
51 return llvm::any_of(value.getUsers(), [&](Operation *user) {
52 return user->getBlock() == block && op->isBeforeInBlock(user);
53 });
54}
55
56/// Returns true if `store` precedes `load` in its block, or if `store`'s block
57/// reaches `load`'s block through a chain of single predecessors. This is the
58/// quick dominance check of classic codegen; full dominance would forward
59/// stores that classic codegen keeps.
60static bool reachesThroughSinglePredecessors(cir::StoreOp store,
61 cir::LoadOp load) {
62 Block *storeBlock = store->getBlock();
63 if (load->getBlock() == storeBlock)
64 return store->isBeforeInBlock(load);
65
66 // Walk up from the load's block until the store's block. Stop at a block
67 // with no single predecessor, or at a block seen before: an unreachable
68 // cycle of single predecessors that never reaches the store.
70 for (Block *block = load->getBlock(); block != storeBlock;
71 block = block->getSinglePredecessor()) {
72 if (!block || !visited.insert(block).second)
73 return false;
74 }
75 return true;
76}
77
78/// Returns the function's only return, or null if it has several or returns
79/// nothing. Classic codegen returns through a single return block, so only
80/// such a function is a candidate.
81static cir::ReturnOp getOnlyValueReturn(cir::FuncOp func) {
82 if (func.getFunctionType().hasVoidReturn())
83 return {};
84 cir::ReturnOp ret;
85 WalkResult walk = func.walk([&](cir::ReturnOp op) {
86 if (ret)
87 return WalkResult::interrupt();
88 ret = op;
89 return WalkResult::advance();
90 });
91 if (walk.wasInterrupted())
92 return {};
93 return ret;
94}
95
96/// Returns the load of a scalar return slot that `ret` returns, or null. The
97/// load must be in the return's block, only feed the return, and be neither
98/// volatile nor atomic.
99static cir::LoadOp getReturnSlotLoad(cir::ReturnOp ret) {
100 // Classic codegen forwards only a return that is not coerced. Call
101 // convention lowering has already replaced the operand of a coerced return.
102 auto load = ret.getInput().front().getDefiningOp<cir::LoadOp>();
103 if (!load || !load->hasOneUse() || load->getBlock() != ret->getBlock() ||
104 load.isVolatileOrAtomic())
105 return {};
106 // Classic codegen stores only scalars to the slot with a single store;
107 // aggregates and complex values are copied or stored by parts.
108 auto slot = load.getAddr().getDefiningOp<cir::AllocaOp>();
109 if (!slot || !slot.getReturnSlot() ||
110 isa<cir::RecordType, cir::ArrayType, cir::ComplexType>(
111 slot.getAllocaType()))
112 return {};
113 return load;
114}
115
116/// Finds the store whose value `load`, the return slot load feeding the
117/// function's only return, can be replaced with. This follows
118/// findDominatingStoreToReturnValue in clang/lib/CodeGen/CGCall.cpp.
119static cir::StoreOp findDominatingStoreToReturnSlot(cir::LoadOp load) {
120 Value slot = load.getAddr();
121
122 // If the slot has other uses than one store and the load, only the operation
123 // right before the load is considered. The store is erased, so nothing may
124 // read the slot after the load.
125 if (!slot.hasNUses(2)) {
126 if (isUsedAfter(slot, load))
127 return {};
128 // Classic codegen also looks past lifetime.end markers and fake uses
129 // between the store and the load.
132 return getStoreToSlot(load->getPrevNode(), slot);
133 }
134
135 Operation *user = *llvm::find_if(slot.getUsers(), [&](Operation *op) {
136 return op != load.getOperation();
137 });
138 cir::StoreOp store = getStoreToSlot(user, slot);
139 if (!store || !reachesThroughSinglePredecessors(store, load))
140 return {};
141 return store;
142}
143
144/// Forwards the store to the return slot of `func`, and returns true if it
145/// changed anything.
146static bool forwardReturnSlot(cir::FuncOp func) {
147 cir::ReturnOp ret = getOnlyValueReturn(func);
148 if (!ret)
149 return false;
150 cir::LoadOp load = getReturnSlotLoad(ret);
151 if (!load)
152 return false;
153 cir::StoreOp store = findDominatingStoreToReturnSlot(load);
154 if (!store)
155 return false;
156
157 auto slot = load.getAddr().getDefiningOp<cir::AllocaOp>();
158 ret.getInputMutable().assign(store.getValue());
159 load.erase();
160 store.erase();
161 if (slot->use_empty())
162 slot.erase();
163 return true;
164}
165
166void CIRForwardReturnSlotPass::runOnOperation() {
167 if (!forwardReturnSlot(getOperation()))
168 markAllAnalysesPreserved();
169}
170
171} // namespace
172
174 return std::make_unique<CIRForwardReturnSlotPass>();
175}
std::unique_ptr< Pass > createCIRForwardReturnSlotPass()
static bool functionUsesSEHTry()
static bool emitLifetimeMarkers()
static bool fakeUseCleanup()