summaryrefslogtreecommitdiff
path: root/gnu/llvm/tools/llvm-mca/CodeRegion.cpp
blob: 29a27c50c171f3ff5f99fedf53ffc4a1c51af298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//===-------------------------- CodeRegion.cpp -----------------*- C++ -* -===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file implements methods from the CodeRegions interface.
///
//===----------------------------------------------------------------------===//

#include "CodeRegion.h"

namespace llvm {
namespace mca {

bool CodeRegion::isLocInRange(llvm::SMLoc Loc) const {
  if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
    return false;
  if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
    return false;
  return true;
}

void CodeRegions::beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) {
  assert(!Regions.empty() && "Missing Default region");
  const CodeRegion &CurrentRegion = *Regions.back();
  if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
    SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
                    "Ignoring invalid region start");
    return;
  }

  // Remove the default region if there are user defined regions.
  if (!CurrentRegion.startLoc().isValid())
    Regions.erase(Regions.begin());
  addRegion(Description, Loc);
}

void CodeRegions::endRegion(llvm::SMLoc Loc) {
  assert(!Regions.empty() && "Missing Default region");
  CodeRegion &CurrentRegion = *Regions.back();
  if (CurrentRegion.endLoc().isValid()) {
    SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
                    "Ignoring invalid region end");
    return;
  }

  CurrentRegion.setEndLocation(Loc);
}

void CodeRegions::addInstruction(const llvm::MCInst &Instruction) {
  const llvm::SMLoc &Loc = Instruction.getLoc();
  const auto It =
      std::find_if(Regions.rbegin(), Regions.rend(),
                   [Loc](const std::unique_ptr<CodeRegion> &Region) {
                     return Region->isLocInRange(Loc);
                   });
  if (It != Regions.rend())
    (*It)->addInstruction(Instruction);
}

} // namespace mca
} // namespace llvm