• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using namespace llvm;
18 
19 namespace mca {
20 
isLocInRange(SMLoc Loc) const21 bool CodeRegion::isLocInRange(SMLoc Loc) const {
22   if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
23     return false;
24   if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
25     return false;
26   return true;
27 }
28 
beginRegion(StringRef Description,SMLoc Loc)29 void CodeRegions::beginRegion(StringRef Description, SMLoc Loc) {
30   assert(!Regions.empty() && "Missing Default region");
31   const CodeRegion &CurrentRegion = *Regions.back();
32   if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
33     SM.PrintMessage(Loc, SourceMgr::DK_Warning,
34                     "Ignoring invalid region start");
35     return;
36   }
37 
38   // Remove the default region if there are user defined regions.
39   if (!CurrentRegion.startLoc().isValid())
40     Regions.erase(Regions.begin());
41   addRegion(Description, Loc);
42 }
43 
endRegion(SMLoc Loc)44 void CodeRegions::endRegion(SMLoc Loc) {
45   assert(!Regions.empty() && "Missing Default region");
46   CodeRegion &CurrentRegion = *Regions.back();
47   if (CurrentRegion.endLoc().isValid()) {
48     SM.PrintMessage(Loc, SourceMgr::DK_Warning, "Ignoring invalid region end");
49     return;
50   }
51 
52   CurrentRegion.setEndLocation(Loc);
53 }
54 
addInstruction(std::unique_ptr<const MCInst> Instruction)55 void CodeRegions::addInstruction(std::unique_ptr<const MCInst> Instruction) {
56   const 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(std::move(Instruction));
64 }
65 
66 } // namespace mca
67