Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 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 | |
Fangrui Song | 467c307 | 2018-10-30 15:56:08 +0000 | [diff] [blame] | 17 | namespace llvm { |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 18 | namespace mca { |
| 19 | |
Andrea Di Biagio | b582972 | 2018-10-22 16:28:07 +0000 | [diff] [blame] | 20 | bool CodeRegion::isLocInRange(llvm::SMLoc Loc) const { |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 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 | |
Andrea Di Biagio | b582972 | 2018-10-22 16:28:07 +0000 | [diff] [blame] | 28 | void CodeRegions::beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) { |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 29 | assert(!Regions.empty() && "Missing Default region"); |
| 30 | const CodeRegion &CurrentRegion = *Regions.back(); |
| 31 | if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) { |
Andrea Di Biagio | b582972 | 2018-10-22 16:28:07 +0000 | [diff] [blame] | 32 | SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning, |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 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 | |
Andrea Di Biagio | b582972 | 2018-10-22 16:28:07 +0000 | [diff] [blame] | 43 | void CodeRegions::endRegion(llvm::SMLoc Loc) { |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 44 | assert(!Regions.empty() && "Missing Default region"); |
| 45 | CodeRegion &CurrentRegion = *Regions.back(); |
| 46 | if (CurrentRegion.endLoc().isValid()) { |
Andrea Di Biagio | b582972 | 2018-10-22 16:28:07 +0000 | [diff] [blame] | 47 | SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning, |
| 48 | "Ignoring invalid region end"); |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
| 52 | CurrentRegion.setEndLocation(Loc); |
| 53 | } |
| 54 | |
Andrea Di Biagio | b582972 | 2018-10-22 16:28:07 +0000 | [diff] [blame] | 55 | void CodeRegions::addInstruction(const llvm::MCInst &Instruction) { |
| 56 | const llvm::SMLoc &Loc = Instruction.getLoc(); |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 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()) |
Andrea Di Biagio | 2ec3239 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 63 | (*It)->addInstruction(Instruction); |
Andrea Di Biagio | aae4cd3 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace mca |
Fangrui Song | 467c307 | 2018-10-30 15:56:08 +0000 | [diff] [blame] | 67 | } // namespace llvm |