blob: 29a27c50c171f3ff5f99fedf53ffc4a1c51af298 [file] [log] [blame]
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +00001//===-------------------------- 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
Fangrui Song467c3072018-10-30 15:56:08 +000017namespace llvm {
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +000018namespace mca {
19
Andrea Di Biagiob5829722018-10-22 16:28:07 +000020bool CodeRegion::isLocInRange(llvm::SMLoc Loc) const {
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +000021 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
Andrea Di Biagiob5829722018-10-22 16:28:07 +000028void CodeRegions::beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) {
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +000029 assert(!Regions.empty() && "Missing Default region");
30 const CodeRegion &CurrentRegion = *Regions.back();
31 if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
Andrea Di Biagiob5829722018-10-22 16:28:07 +000032 SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +000033 "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
Andrea Di Biagiob5829722018-10-22 16:28:07 +000043void CodeRegions::endRegion(llvm::SMLoc Loc) {
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +000044 assert(!Regions.empty() && "Missing Default region");
45 CodeRegion &CurrentRegion = *Regions.back();
46 if (CurrentRegion.endLoc().isValid()) {
Andrea Di Biagiob5829722018-10-22 16:28:07 +000047 SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
48 "Ignoring invalid region end");
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +000049 return;
50 }
51
52 CurrentRegion.setEndLocation(Loc);
53}
54
Andrea Di Biagiob5829722018-10-22 16:28:07 +000055void CodeRegions::addInstruction(const llvm::MCInst &Instruction) {
56 const llvm::SMLoc &Loc = Instruction.getLoc();
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +000057 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())
Andrea Di Biagio2ec32392018-10-22 15:36:15 +000063 (*It)->addInstruction(Instruction);
Andrea Di Biagioaae4cd32018-04-09 16:39:52 +000064}
65
66} // namespace mca
Fangrui Song467c3072018-10-30 15:56:08 +000067} // namespace llvm