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 {
|
|
|
|
|
2019-05-08 18:44:05 +08:00
|
|
|
CodeRegions::CodeRegions(llvm::SourceMgr &S) : SM(S) {
|
|
|
|
// Create a default region for the input code sequence.
|
2019-05-08 19:00:43 +08:00
|
|
|
Regions.emplace_back(make_unique<CodeRegion>("", SMLoc()));
|
2019-05-08 18:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeRegion::isLocInRange(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;
|
|
|
|
}
|
|
|
|
|
2019-05-08 18:44:05 +08:00
|
|
|
void CodeRegions::beginRegion(StringRef Description, 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()) {
|
2019-05-08 18:44:05 +08:00
|
|
|
SM.PrintMessage(Loc, 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());
|
2019-05-08 18:44:05 +08:00
|
|
|
Regions.emplace_back(make_unique<CodeRegion>(Description, Loc));
|
2018-04-10 00:39:52 +08:00
|
|
|
}
|
|
|
|
|
2019-05-08 18:44:05 +08:00
|
|
|
void CodeRegions::endRegion(SMLoc Loc) {
|
2018-04-10 00:39:52 +08:00
|
|
|
assert(!Regions.empty() && "Missing Default region");
|
|
|
|
CodeRegion &CurrentRegion = *Regions.back();
|
|
|
|
if (CurrentRegion.endLoc().isValid()) {
|
2019-05-08 18:44:05 +08:00
|
|
|
SM.PrintMessage(Loc, SourceMgr::DK_Warning,
|
2018-10-23 00:28:07 +08:00
|
|
|
"Ignoring invalid region end");
|
2018-04-10 00:39:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentRegion.setEndLocation(Loc);
|
|
|
|
}
|
|
|
|
|
2019-05-08 18:44:05 +08:00
|
|
|
void CodeRegions::addInstruction(const MCInst &Instruction) {
|
|
|
|
const SMLoc &Loc = Instruction.getLoc();
|
2018-04-10 00:39:52 +08:00
|
|
|
const auto It =
|
|
|
|
std::find_if(Regions.rbegin(), Regions.rend(),
|
2019-05-08 18:44:05 +08:00
|
|
|
[Loc](const UniqueCodeRegion &Region) {
|
2018-04-10 00:39:52 +08:00
|
|
|
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
|