]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/llvm-mca/CodeRegion.cpp
Vendor import of llvm trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.git] / tools / llvm-mca / CodeRegion.cpp
1 //===-------------------------- CodeRegion.cpp -----------------*- C++ -* -===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
11 /// This file implements methods from the CodeRegions interface.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "CodeRegion.h"
16
17 namespace llvm {
18 namespace mca {
19
20 bool CodeRegion::isLocInRange(llvm::SMLoc Loc) const {
21   if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
22     return false;
23   if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
24     return false;
25   return true;
26 }
27
28 void CodeRegions::beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) {
29   assert(!Regions.empty() && "Missing Default region");
30   const CodeRegion &CurrentRegion = *Regions.back();
31   if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
32     SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
33                     "Ignoring invalid region start");
34     return;
35   }
36
37   // Remove the default region if there are user defined regions.
38   if (!CurrentRegion.startLoc().isValid())
39     Regions.erase(Regions.begin());
40   addRegion(Description, Loc);
41 }
42
43 void CodeRegions::endRegion(llvm::SMLoc Loc) {
44   assert(!Regions.empty() && "Missing Default region");
45   CodeRegion &CurrentRegion = *Regions.back();
46   if (CurrentRegion.endLoc().isValid()) {
47     SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
48                     "Ignoring invalid region end");
49     return;
50   }
51
52   CurrentRegion.setEndLocation(Loc);
53 }
54
55 void CodeRegions::addInstruction(const llvm::MCInst &Instruction) {
56   const llvm::SMLoc &Loc = Instruction.getLoc();
57   const auto It =
58       std::find_if(Regions.rbegin(), Regions.rend(),
59                    [Loc](const std::unique_ptr<CodeRegion> &Region) {
60                      return Region->isLocInRange(Loc);
61                    });
62   if (It != Regions.rend())
63     (*It)->addInstruction(Instruction);
64 }
65
66 } // namespace mca
67 } // namespace llvm