|
| 1 | +//===- MoveOpt.cpp - optimize copies to moves -----------------------------===// |
| 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 "MoveOpt.h" |
| 10 | +#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" |
| 11 | + |
| 12 | +// MoveOptPass method implementations |
| 13 | +bool MoveOptPass::isCopy(cir::CallOp call) { |
| 14 | + // Get the method decl of the callee. |
| 15 | + auto calleeFunc = call.getDirectCallee(theModule); |
| 16 | + auto methodDecl = mlir::dyn_cast_if_present<cir::ASTCXXMethodDeclInterface>( |
| 17 | + calleeFunc.getAstAttr()); |
| 18 | + if (!methodDecl) |
| 19 | + return false; |
| 20 | + |
| 21 | + // Check if this is the copy constructor. |
| 22 | + if (auto ctorDecl = |
| 23 | + mlir::dyn_cast<cir::ASTCXXConstructorDeclInterface>(methodDecl)) { |
| 24 | + return ctorDecl.isCopyConstructor(); |
| 25 | + } |
| 26 | + |
| 27 | + // Check if this is the copy assignment operator. |
| 28 | + return methodDecl.isCopyAssignmentOperator(); |
| 29 | +} |
| 30 | + |
| 31 | +bool MoveOptPass::isLastUse(cir::CallOp call, PointsToAnalysis &pta, |
| 32 | + LiveObjectAnalysis &liveObjects) { |
| 33 | + // Get the pointer being moved. |
| 34 | + auto &ptrUse = call->getOpOperand(1); |
| 35 | + |
| 36 | + // Unpack the pointer use. |
| 37 | + auto ptr = ptrUse.get(); |
| 38 | + |
| 39 | + // Get the set of objects that could be used. |
| 40 | + const auto &pointsTo = pta.valPointsTo[ptr]; |
| 41 | + |
| 42 | + if (pointsTo.contains(pta.unknown())) { |
| 43 | + call->emitRemark("move opt: copied object may be unknown"); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // Have any of the pointees escaped out? |
| 48 | + const auto &escaped = pta.memPointsTo[pta.unknown()]; |
| 49 | + for (auto obj : pointsTo) { |
| 50 | + if (escaped.contains(obj)) { |
| 51 | + call->emitRemark("move opt: copied object may have escaped"); |
| 52 | + return false; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Query the memory analysis. |
| 57 | + if (!liveObjects.isLastUse(ptrUse)) { |
| 58 | + call->emitRemark("move opt: copied object is alive after use"); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | +} |
| 64 | + |
| 65 | +cir::ASTFunctionDeclInterface |
| 66 | +MoveOptPass::getMoveDecl(cir::ASTCXXMethodDeclInterface copyDecl) { |
| 67 | + auto recordDecl = copyDecl.getParent(); |
| 68 | + if (!recordDecl) |
| 69 | + return {}; |
| 70 | + |
| 71 | + // Find the matching move constructor. |
| 72 | + if (isa<ASTCXXConstructorDeclInterface>(copyDecl)) |
| 73 | + return recordDecl.getMoveConstructor(); |
| 74 | + |
| 75 | + // Find the matching move assignment operator. |
| 76 | + return recordDecl.getMoveAssignmentOperator(); |
| 77 | +} |
| 78 | + |
| 79 | +cir::FuncOp MoveOptPass::getMoveFuncFor(cir::CallOp call) { |
| 80 | + // Get the method callee. |
| 81 | + auto copyFunc = call.getDirectCallee(theModule); |
| 82 | + if (!copyFunc) |
| 83 | + return {}; |
| 84 | + |
| 85 | + auto copyDecl = mlir::dyn_cast_if_present<cir::ASTCXXMethodDeclInterface>( |
| 86 | + copyFunc.getAstAttr()); |
| 87 | + if (!copyDecl) |
| 88 | + return {}; |
| 89 | + |
| 90 | + // Convert the copy into a move within the record.. |
| 91 | + auto moveDecl = getMoveDecl(copyDecl); |
| 92 | + if (!moveDecl) |
| 93 | + return {}; |
| 94 | + |
| 95 | + // Get the move function from the module, if it exists. |
| 96 | + auto moveFuncName = moveDecl.getMangledName(); |
| 97 | + mlir::Operation *moveFuncGlobal = |
| 98 | + mlir::SymbolTable::lookupSymbolIn(theModule, moveFuncName); |
| 99 | + |
| 100 | + auto moveFunc = mlir::dyn_cast_if_present<cir::FuncOp>(moveFuncGlobal); |
| 101 | + if (!moveFunc) |
| 102 | + return {}; |
| 103 | + |
| 104 | + return moveFunc; |
| 105 | +} |
| 106 | + |
| 107 | +bool MoveOptPass::isApplicable(cir::CallOp call, PointsToAnalysis &pta, |
| 108 | + LiveObjectAnalysis &liveObjects) { |
| 109 | + return isCopy(call) && getMoveFuncFor(call) && |
| 110 | + isLastUse(call, pta, liveObjects); |
| 111 | +} |
| 112 | + |
| 113 | +void MoveOptPass::transform(cir::CallOp call) { |
| 114 | + // Get the move function for this call. |
| 115 | + auto moveFunc = getMoveFuncFor(call); |
| 116 | + assert(moveFunc && "Missing move function"); |
| 117 | + |
| 118 | + // Replace the copy with a move. |
| 119 | + call.setCallee(moveFunc.getName()); |
| 120 | + |
| 121 | + call.emitRemark("move opt: transformed copy into move"); |
| 122 | +} |
| 123 | + |
| 124 | +void MoveOptPass::runOnOperation() { |
| 125 | + assert(astCtx && "Missing ASTContext, please construct with the right ctor"); |
| 126 | + |
| 127 | + mlir::Operation *op = getOperation(); |
| 128 | + |
| 129 | + // Record the current module as we go. |
| 130 | + if (mlir::isa<mlir::ModuleOp>(op)) |
| 131 | + theModule = mlir::cast<mlir::ModuleOp>(op); |
| 132 | + |
| 133 | + // Run the live object analysis. |
| 134 | + auto pta = getAnalysis<PointsToAnalysis>(); |
| 135 | + auto liveObjects = getAnalysis<LiveObjectAnalysis>(); |
| 136 | + |
| 137 | + // Collect all move constructors. |
| 138 | + llvm::SmallVector<cir::CallOp> copies; |
| 139 | + op->walk([&](cir::CallOp call) { |
| 140 | + if (isApplicable(call, pta, liveObjects)) |
| 141 | + copies.push_back(call); |
| 142 | + }); |
| 143 | + |
| 144 | + if (copies.empty()) |
| 145 | + return; |
| 146 | + |
| 147 | + // Transform each of the moves, if applicable. |
| 148 | + for (auto copy : copies) |
| 149 | + transform(copy); |
| 150 | +} |
| 151 | + |
| 152 | +void MoveOptPass::setASTContext(clang::ASTContext *astCtx) { |
| 153 | + this->astCtx = astCtx; |
| 154 | +} |
| 155 | + |
| 156 | +std::unique_ptr<Pass> mlir::createMoveOptPass() { |
| 157 | + auto pass = std::make_unique<MoveOptPass>(); |
| 158 | + return std::move(pass); |
| 159 | +} |
| 160 | + |
| 161 | +std::unique_ptr<Pass> mlir::createMoveOptPass(clang::ASTContext *astCtx) { |
| 162 | + auto pass = std::make_unique<MoveOptPass>(); |
| 163 | + pass->setASTContext(astCtx); |
| 164 | + return std::move(pass); |
| 165 | +} |
0 commit comments