xref: /netbsd-src/external/apache2/llvm/dist/llvm/tools/llvm-mca/CodeRegion.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
1*7330f729Sjoerg //===-------------------------- CodeRegion.cpp -----------------*- C++ -* -===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg /// \file
9*7330f729Sjoerg ///
10*7330f729Sjoerg /// This file implements methods from the CodeRegions interface.
11*7330f729Sjoerg ///
12*7330f729Sjoerg //===----------------------------------------------------------------------===//
13*7330f729Sjoerg 
14*7330f729Sjoerg #include "CodeRegion.h"
15*7330f729Sjoerg 
16*7330f729Sjoerg namespace llvm {
17*7330f729Sjoerg namespace mca {
18*7330f729Sjoerg 
CodeRegions(llvm::SourceMgr & S)19*7330f729Sjoerg CodeRegions::CodeRegions(llvm::SourceMgr &S) : SM(S), FoundErrors(false) {
20*7330f729Sjoerg   // Create a default region for the input code sequence.
21*7330f729Sjoerg   Regions.emplace_back(std::make_unique<CodeRegion>("", SMLoc()));
22*7330f729Sjoerg }
23*7330f729Sjoerg 
isLocInRange(SMLoc Loc) const24*7330f729Sjoerg bool CodeRegion::isLocInRange(SMLoc Loc) const {
25*7330f729Sjoerg   if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
26*7330f729Sjoerg     return false;
27*7330f729Sjoerg   if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
28*7330f729Sjoerg     return false;
29*7330f729Sjoerg   return true;
30*7330f729Sjoerg }
31*7330f729Sjoerg 
beginRegion(StringRef Description,SMLoc Loc)32*7330f729Sjoerg void CodeRegions::beginRegion(StringRef Description, SMLoc Loc) {
33*7330f729Sjoerg   if (ActiveRegions.empty()) {
34*7330f729Sjoerg     // Remove the default region if there is at least one user defined region.
35*7330f729Sjoerg     // By construction, only the default region has an invalid start location.
36*7330f729Sjoerg     if (Regions.size() == 1 && !Regions[0]->startLoc().isValid() &&
37*7330f729Sjoerg         !Regions[0]->endLoc().isValid()) {
38*7330f729Sjoerg       ActiveRegions[Description] = 0;
39*7330f729Sjoerg       Regions[0] = std::make_unique<CodeRegion>(Description, Loc);
40*7330f729Sjoerg       return;
41*7330f729Sjoerg     }
42*7330f729Sjoerg   } else {
43*7330f729Sjoerg     auto It = ActiveRegions.find(Description);
44*7330f729Sjoerg     if (It != ActiveRegions.end()) {
45*7330f729Sjoerg       const CodeRegion &R = *Regions[It->second];
46*7330f729Sjoerg       if (Description.empty()) {
47*7330f729Sjoerg         SM.PrintMessage(Loc, SourceMgr::DK_Error,
48*7330f729Sjoerg                         "found multiple overlapping anonymous regions");
49*7330f729Sjoerg         SM.PrintMessage(R.startLoc(), SourceMgr::DK_Note,
50*7330f729Sjoerg                         "Previous anonymous region was defined here");
51*7330f729Sjoerg         FoundErrors = true;
52*7330f729Sjoerg         return;
53*7330f729Sjoerg       }
54*7330f729Sjoerg 
55*7330f729Sjoerg       SM.PrintMessage(Loc, SourceMgr::DK_Error,
56*7330f729Sjoerg                       "overlapping regions cannot have the same name");
57*7330f729Sjoerg       SM.PrintMessage(R.startLoc(), SourceMgr::DK_Note,
58*7330f729Sjoerg                       "region " + Description + " was previously defined here");
59*7330f729Sjoerg       FoundErrors = true;
60*7330f729Sjoerg       return;
61*7330f729Sjoerg     }
62*7330f729Sjoerg   }
63*7330f729Sjoerg 
64*7330f729Sjoerg   ActiveRegions[Description] = Regions.size();
65*7330f729Sjoerg   Regions.emplace_back(std::make_unique<CodeRegion>(Description, Loc));
66*7330f729Sjoerg }
67*7330f729Sjoerg 
endRegion(StringRef Description,SMLoc Loc)68*7330f729Sjoerg void CodeRegions::endRegion(StringRef Description, SMLoc Loc) {
69*7330f729Sjoerg   if (Description.empty()) {
70*7330f729Sjoerg     // Special case where there is only one user defined region,
71*7330f729Sjoerg     // and this LLVM-MCA-END directive doesn't provide a region name.
72*7330f729Sjoerg     // In this case, we assume that the user simply wanted to just terminate
73*7330f729Sjoerg     // the only active region.
74*7330f729Sjoerg     if (ActiveRegions.size() == 1) {
75*7330f729Sjoerg       auto It = ActiveRegions.begin();
76*7330f729Sjoerg       Regions[It->second]->setEndLocation(Loc);
77*7330f729Sjoerg       ActiveRegions.erase(It);
78*7330f729Sjoerg       return;
79*7330f729Sjoerg     }
80*7330f729Sjoerg 
81*7330f729Sjoerg     // Special case where the region end marker applies to the default region.
82*7330f729Sjoerg     if (ActiveRegions.empty() && Regions.size() == 1 &&
83*7330f729Sjoerg         !Regions[0]->startLoc().isValid() && !Regions[0]->endLoc().isValid()) {
84*7330f729Sjoerg       Regions[0]->setEndLocation(Loc);
85*7330f729Sjoerg       return;
86*7330f729Sjoerg     }
87*7330f729Sjoerg   }
88*7330f729Sjoerg 
89*7330f729Sjoerg   auto It = ActiveRegions.find(Description);
90*7330f729Sjoerg   if (It != ActiveRegions.end()) {
91*7330f729Sjoerg     Regions[It->second]->setEndLocation(Loc);
92*7330f729Sjoerg     ActiveRegions.erase(It);
93*7330f729Sjoerg     return;
94*7330f729Sjoerg   }
95*7330f729Sjoerg 
96*7330f729Sjoerg   FoundErrors = true;
97*7330f729Sjoerg   SM.PrintMessage(Loc, SourceMgr::DK_Error,
98*7330f729Sjoerg                   "found an invalid region end directive");
99*7330f729Sjoerg   if (!Description.empty()) {
100*7330f729Sjoerg     SM.PrintMessage(Loc, SourceMgr::DK_Note,
101*7330f729Sjoerg                     "unable to find an active region named " + Description);
102*7330f729Sjoerg   } else {
103*7330f729Sjoerg     SM.PrintMessage(Loc, SourceMgr::DK_Note,
104*7330f729Sjoerg                     "unable to find an active anonymous region");
105*7330f729Sjoerg   }
106*7330f729Sjoerg }
107*7330f729Sjoerg 
addInstruction(const MCInst & Instruction)108*7330f729Sjoerg void CodeRegions::addInstruction(const MCInst &Instruction) {
109*7330f729Sjoerg   SMLoc Loc = Instruction.getLoc();
110*7330f729Sjoerg   for (UniqueCodeRegion &Region : Regions)
111*7330f729Sjoerg     if (Region->isLocInRange(Loc))
112*7330f729Sjoerg       Region->addInstruction(Instruction);
113*7330f729Sjoerg }
114*7330f729Sjoerg 
115*7330f729Sjoerg } // namespace mca
116*7330f729Sjoerg } // namespace llvm
117