1//===- RegionWithScore.cpp - A Region with score tracking -----------------===//
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 "llvm/Transforms/Vectorize/SandboxVectorizer/RegionWithScore.h"
10#include "llvm/SandboxIR/Function.h"
11
12namespace llvm::sandboxir {
13
14InstructionCost ScoreBoard::getCost(Instruction *I) const {
15 auto *LLVMI = cast<llvm::Instruction>(Val: I->Val);
16 SmallVector<const llvm::Value *> Operands(LLVMI->operands());
17 return TTI.getInstructionCost(U: LLVMI, Operands, CostKind);
18}
19
20void ScoreBoard::remove(Instruction *I) {
21 auto Cost = getCost(I);
22 if (Rgn.contains(I))
23 // If `I` is one the newly added ones, then we should adjust `AfterCost`
24 AfterCost -= Cost;
25 else
26 // If `I` is one of the original instructions (outside the region) then it
27 // is part of the original code, so adjust `BeforeCost`.
28 BeforeCost += Cost;
29}
30
31#ifndef NDEBUG
32void ScoreBoard::dump() const { dump(dbgs()); }
33#endif
34
35SmallVector<std::unique_ptr<RegionWithScore>>
36RegionWithScore::createRegionsFromMD(Function &F,
37 const TargetTransformInfo &TTI) {
38 return Region::createRegionsFromMD<RegionWithScore>(F, Factory: [&F, &TTI]() {
39 return std::make_unique<RegionWithScore>(args&: F.getContext(), args: TTI);
40 });
41}
42
43} // namespace llvm::sandboxir
44