1 | //===- DIExpressionLegalization.cpp - DIExpression Legalization Patterns --===// |
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 "mlir/Dialect/LLVMIR/Transforms/DIExpressionLegalization.h" |
10 | |
11 | #include "llvm/BinaryFormat/Dwarf.h" |
12 | |
13 | using namespace mlir; |
14 | using namespace LLVM; |
15 | |
16 | //===----------------------------------------------------------------------===// |
17 | // MergeFragments |
18 | //===----------------------------------------------------------------------===// |
19 | |
20 | MergeFragments::OpIterT MergeFragments::match(OpIterRange operators) const { |
21 | OpIterT it = operators.begin(); |
22 | if (it == operators.end() || |
23 | it->getOpcode() != llvm::dwarf::DW_OP_LLVM_fragment) |
24 | return operators.begin(); |
25 | |
26 | ++it; |
27 | if (it == operators.end() || |
28 | it->getOpcode() != llvm::dwarf::DW_OP_LLVM_fragment) |
29 | return operators.begin(); |
30 | |
31 | return ++it; |
32 | } |
33 | |
34 | SmallVector<MergeFragments::OperatorT> |
35 | MergeFragments::replace(OpIterRange operators) const { |
36 | OpIterT it = operators.begin(); |
37 | OperatorT first = *(it++); |
38 | OperatorT second = *it; |
39 | // Add offsets & select the size of the earlier operator (the one closer to |
40 | // the IR value). |
41 | uint64_t offset = first.getArguments()[0] + second.getArguments()[0]; |
42 | uint64_t size = first.getArguments()[1]; |
43 | OperatorT newOp = OperatorT::get( |
44 | first.getContext(), llvm::dwarf::DW_OP_LLVM_fragment, {offset, size}); |
45 | return SmallVector<OperatorT>{newOp}; |
46 | } |
47 | |
48 | //===----------------------------------------------------------------------===// |
49 | // Runner |
50 | //===----------------------------------------------------------------------===// |
51 | |
52 | void mlir::LLVM::legalizeDIExpressionsRecursively(Operation *op) { |
53 | LLVM::DIExpressionRewriter rewriter; |
54 | rewriter.addPattern(pattern: std::make_unique<MergeFragments>()); |
55 | |
56 | AttrTypeReplacer replacer; |
57 | replacer.addReplacement([&rewriter](LLVM::DIExpressionAttr expr) { |
58 | return rewriter.simplify(expr); |
59 | }); |
60 | replacer.recursivelyReplaceElementsIn(op); |
61 | } |
62 | |