blob: 591c45feb6da2271239fb9fec41f6d5c146f7b3a [file] [log] [blame]
Andrea Di Biagioc6590122018-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
Andrea Di Biagioc6590122018-04-09 16:39:52 +000017namespace mca {
18
Andrea Di Biagiodb158be2018-10-22 16:28:07 +000019bool CodeRegion::isLocInRange(llvm::SMLoc Loc) const {
Andrea Di Biagioc6590122018-04-09 16:39:52 +000020 if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
21 return false;
22 if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
23 return false;
24 return true;
25}
26
Andrea Di Biagiodb158be2018-10-22 16:28:07 +000027void CodeRegions::beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) {
Andrea Di Biagioc6590122018-04-09 16:39:52 +000028 assert(!Regions.empty() && "Missing Default region");
29 const CodeRegion &CurrentRegion = *Regions.back();
30 if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
Andrea Di Biagiodb158be2018-10-22 16:28:07 +000031 SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
Andrea Di Biagioc6590122018-04-09 16:39:52 +000032 "Ignoring invalid region start");
33 return;
34 }
35
36 // Remove the default region if there are user defined regions.
37 if (!CurrentRegion.startLoc().isValid())
38 Regions.erase(Regions.begin());
39 addRegion(Description, Loc);
40}
41
Andrea Di Biagiodb158be2018-10-22 16:28:07 +000042void CodeRegions::endRegion(llvm::SMLoc Loc) {
Andrea Di Biagioc6590122018-04-09 16:39:52 +000043 assert(!Regions.empty() && "Missing Default region");
44 CodeRegion &CurrentRegion = *Regions.back();
45 if (CurrentRegion.endLoc().isValid()) {
Andrea Di Biagiodb158be2018-10-22 16:28:07 +000046 SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
47 "Ignoring invalid region end");
Andrea Di Biagioc6590122018-04-09 16:39:52 +000048 return;
49 }
50
51 CurrentRegion.setEndLocation(Loc);
52}
53
Andrea Di Biagiodb158be2018-10-22 16:28:07 +000054void CodeRegions::addInstruction(const llvm::MCInst &Instruction) {
55 const llvm::SMLoc &Loc = Instruction.getLoc();
Andrea Di Biagioc6590122018-04-09 16:39:52 +000056 const auto It =
57 std::find_if(Regions.rbegin(), Regions.rend(),
58 [Loc](const std::unique_ptr<CodeRegion> &Region) {
59 return Region->isLocInRange(Loc);
60 });
61 if (It != Regions.rend())
Andrea Di Biagio01b9fd62018-10-22 15:36:15 +000062 (*It)->addInstruction(Instruction);
Andrea Di Biagioc6590122018-04-09 16:39:52 +000063}
64
65} // namespace mca