2018-04-10 00:39:52 +08:00
|
|
|
//===-------------------------- CodeRegion.cpp -----------------*- C++ -* -===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-04-10 00:39:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This file implements methods from the CodeRegions interface.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeRegion.h"
|
|
|
|
|
2018-10-30 23:56:08 +08:00
|
|
|
namespace llvm {
|
2018-04-10 00:39:52 +08:00
|
|
|
namespace mca {
|
|
|
|
|
2018-10-23 00:28:07 +08:00
|
|
|
bool CodeRegion::isLocInRange(llvm::SMLoc Loc) const {
|
2018-04-10 00:39:52 +08:00
|
|
|
if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
|
|
|
|
return false;
|
|
|
|
if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:28:07 +08:00
|
|
|
void CodeRegions::beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) {
|
2018-04-10 00:39:52 +08:00
|
|
|
assert(!Regions.empty() && "Missing Default region");
|
|
|
|
const CodeRegion &CurrentRegion = *Regions.back();
|
|
|
|
if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
|
2018-10-23 00:28:07 +08:00
|
|
|
SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
|
2018-04-10 00:39:52 +08:00
|
|
|
"Ignoring invalid region start");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the default region if there are user defined regions.
|
|
|
|
if (!CurrentRegion.startLoc().isValid())
|
|
|
|
Regions.erase(Regions.begin());
|
|
|
|
addRegion(Description, Loc);
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:28:07 +08:00
|
|
|
void CodeRegions::endRegion(llvm::SMLoc Loc) {
|
2018-04-10 00:39:52 +08:00
|
|
|
assert(!Regions.empty() && "Missing Default region");
|
|
|
|
CodeRegion &CurrentRegion = *Regions.back();
|
|
|
|
if (CurrentRegion.endLoc().isValid()) {
|
2018-10-23 00:28:07 +08:00
|
|
|
SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
|
|
|
|
"Ignoring invalid region end");
|
2018-04-10 00:39:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentRegion.setEndLocation(Loc);
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:28:07 +08:00
|
|
|
void CodeRegions::addInstruction(const llvm::MCInst &Instruction) {
|
|
|
|
const llvm::SMLoc &Loc = Instruction.getLoc();
|
2018-04-10 00:39:52 +08:00
|
|
|
const auto It =
|
|
|
|
std::find_if(Regions.rbegin(), Regions.rend(),
|
|
|
|
[Loc](const std::unique_ptr<CodeRegion> &Region) {
|
|
|
|
return Region->isLocInRange(Loc);
|
|
|
|
});
|
|
|
|
if (It != Regions.rend())
|
2018-10-22 23:36:15 +08:00
|
|
|
(*It)->addInstruction(Instruction);
|
2018-04-10 00:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mca
|
2018-10-30 23:56:08 +08:00
|
|
|
} // namespace llvm
|