1//===- CIRTransformUtils.cpp - Shared helpers for CIR transforms ----------===//
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#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
10
11#include "clang/CIR/Dialect/IR/CIRTypes.h"
12
13#include "llvm/ADT/DepthFirstIterator.h"
14
15void cir::collectUnreachable(mlir::Operation *parent,
16 llvm::SmallVectorImpl<mlir::Operation *> &ops) {
17 // For every region under `parent`, find the blocks unreachable from the
18 // entry via a forward CFG traversal and collect their ops.
19 llvm::df_iterator_default_set<mlir::Block *, 16> reachable;
20 parent->walk(callback: [&](mlir::Region *region) {
21 // Empty regions have no blocks; single-block regions have only the
22 // entry, which is trivially reachable. Either way, nothing to collect.
23 if (region->empty() || region->hasOneBlock())
24 return;
25
26 // We clear this for each region as we walk the parent because each block
27 // is only in one region, so the reachable blocks from previously visited
28 // regions aren't needed.
29 reachable.clear();
30
31 // The depth_first_ext range iterator internally adds each block to the
32 // reachable set as it visits it, so while this loop looks like it doesn't
33 // do anything, it's actually populating the set of reachable blocks in
34 // this region.
35 for (mlir::Block *blk : llvm::depth_first_ext(G: &region->front(), S&: reachable))
36 (void)blk;
37
38 // Collect the unreachable blocks.
39 for (mlir::Block &blk : *region) {
40 if (reachable.contains(Ptr: &blk))
41 continue;
42 for (mlir::Operation &op : blk)
43 ops.push_back(Elt: &op);
44 }
45 });
46}
47
48mlir::Block *cir::replaceCallWithTryCall(cir::CallOp callOp,
49 mlir::Block *unwindDest,
50 mlir::Location loc,
51 mlir::RewriterBase &rewriter) {
52 mlir::Block *callBlock = callOp->getBlock();
53
54 assert(!callOp.getNothrow() && "call is not expected to throw");
55
56 // Split the block after the call - remaining ops become the normal
57 // destination.
58 mlir::Block *normalDest =
59 rewriter.splitBlock(block: callBlock, before: std::next(x: callOp->getIterator()));
60
61 // Build the try_call to replace the original call.
62 rewriter.setInsertionPoint(callOp);
63 cir::TryCallOp tryCallOp;
64 if (callOp.isIndirect()) {
65 mlir::Value indTarget = callOp.getIndirectCall();
66 auto ptrTy = mlir::cast<cir::PointerType>(Val: indTarget.getType());
67 auto resTy = mlir::cast<cir::FuncType>(Val: ptrTy.getPointee());
68 tryCallOp =
69 cir::TryCallOp::create(builder&: rewriter, location: loc, ind_target: indTarget, fn_type: resTy, normalDest,
70 unwindDest, callOperands: callOp.getArgOperands());
71 } else {
72 mlir::Type resType = callOp->getNumResults() > 0
73 ? callOp->getResult(idx: 0).getType()
74 : mlir::Type();
75 tryCallOp =
76 cir::TryCallOp::create(builder&: rewriter, location: loc, callee: callOp.getCalleeAttr(), resType,
77 normalDest, unwindDest, callOperands: callOp.getArgOperands());
78 }
79
80 // Preserve the call semantics shared by CallOp and TryCallOp. The callee and
81 // operand segments are already populated by TryCallOp::create, and a
82 // throwing call cannot carry the nothrow property. nounwind describes the
83 // callee, so it survives even though this site gains an unwind edge.
84 callOp->getName().walkInherentAttrs(
85 op: callOp, visitor: [&](llvm::StringRef name, mlir::Attribute &attr) {
86 if (name != cir::CIRDialect::getCalleeAttrName() &&
87 name != cir::CIRDialect::getNoThrowAttrName() &&
88 name != cir::CIRDialect::getOperandSegmentSizesAttrName())
89 tryCallOp->setInherentAttr(
90 name: mlir::StringAttr::get(context: callOp->getContext(), bytes: name), value: attr);
91 });
92 for (mlir::NamedAttribute attr : callOp->getDiscardableAttrs())
93 tryCallOp->setDiscardableAttr(name: attr.getName(), value: attr.getValue());
94
95 // Replace uses of the call result with the try_call result. Use the
96 // rewriter API so any listener (e.g. the pattern rewriter in
97 // FlattenCFG) is notified of the in-place modifications to each user.
98 if (callOp->getNumResults() > 0)
99 rewriter.replaceAllUsesWith(from: callOp->getResult(idx: 0), to: tryCallOp.getResult());
100
101 rewriter.eraseOp(op: callOp);
102 return normalDest;
103}
104
105mlir::Block *cir::replaceThrowWithTryThrow(cir::ThrowOp throwOp,
106 mlir::Block *unwindDest,
107 mlir::Location loc,
108 mlir::RewriterBase &rewriter) {
109 // The throw never returns, so the try_throw's normal destination is
110 // literally unreachable. Place it after the throw block in the same region.
111 mlir::Block *throwBlock = throwOp->getBlock();
112 mlir::Block *normalDest;
113 {
114 mlir::OpBuilder::InsertionGuard guard(rewriter);
115 normalDest = rewriter.createBlock(parent: throwBlock->getParent(),
116 insertPt: std::next(x: throwBlock->getIterator()));
117 cir::UnreachableOp::create(builder&: rewriter, location: loc);
118 }
119
120 // Build the try_throw to replace the original throw.
121 rewriter.setInsertionPoint(throwOp);
122 auto tryThrowOp = cir::TryThrowOp::create(
123 builder&: rewriter, location: loc, exception_ptr: throwOp.getExceptionPtr(), type_info: throwOp.getTypeInfoAttr(),
124 dtor: throwOp.getDtorAttr(), normalDest, unwindDest);
125
126 // The shared inherent state is already set by TryThrowOp::create. Preserve
127 // only auxiliary metadata here.
128 for (mlir::NamedAttribute attr : throwOp->getDiscardableAttrs())
129 tryThrowOp->setDiscardableAttr(name: attr.getName(), value: attr.getValue());
130
131 // Erase the throw along with any operations that followed it in its
132 // parent block (typically a cir.unreachable left over from CIR codegen).
133 // They must be removed because try_throw is a terminator and a block
134 // can have only one terminator.
135 while (&throwBlock->back() != tryThrowOp)
136 rewriter.eraseOp(op: &throwBlock->back());
137
138 return normalDest;
139}
140