2021-12-22 02:21:41 +08:00
|
|
|
//===- bolt/Utils/Utils.cpp - Common helper functions ---------------------===//
|
2020-01-31 05:10:48 +08:00
|
|
|
//
|
2021-03-16 09:04:18 +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
|
2020-01-31 05:10:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Common helper functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-10-09 02:47:10 +08:00
|
|
|
#include "bolt/Utils/Utils.h"
|
2020-12-02 08:29:39 +08:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
|
|
|
#include "llvm/MC/MCDwarf.h"
|
|
|
|
#include "llvm/Support/LEB128.h"
|
2020-01-31 05:10:48 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace bolt {
|
|
|
|
|
|
|
|
void report_error(StringRef Message, std::error_code EC) {
|
|
|
|
assert(EC);
|
|
|
|
errs() << "BOLT-ERROR: '" << Message << "': " << EC.message() << ".\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void report_error(StringRef Message, Error E) {
|
|
|
|
assert(E);
|
|
|
|
errs() << "BOLT-ERROR: '" << Message << "': " << toString(std::move(E))
|
|
|
|
<< ".\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_error(std::error_code EC, StringRef Message) {
|
|
|
|
if (!EC)
|
|
|
|
return;
|
|
|
|
report_error(Message, EC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_error(Error E, Twine Message) {
|
|
|
|
if (!E)
|
|
|
|
return;
|
|
|
|
handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
|
|
|
|
llvm::errs() << "BOLT-ERROR: '" << Message << "': " << EIB.message()
|
|
|
|
<< '\n';
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-30 00:54:08 +08:00
|
|
|
std::string getEscapedName(const StringRef &Name) {
|
|
|
|
std::string Output = Name.str();
|
2021-12-29 10:43:53 +08:00
|
|
|
for (size_t I = 0; I < Output.size(); ++I)
|
2021-06-30 00:54:08 +08:00
|
|
|
if (Output[I] == ' ' || Output[I] == '\\')
|
|
|
|
Output.insert(I++, 1, '\\');
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getUnescapedName(const StringRef &Name) {
|
|
|
|
std::string Output = Name.str();
|
2021-12-29 10:43:53 +08:00
|
|
|
for (size_t I = 0; I < Output.size(); ++I)
|
2021-06-30 00:54:08 +08:00
|
|
|
if (Output[I] == '\\')
|
|
|
|
Output.erase(I++, 1);
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
2020-12-02 08:29:39 +08:00
|
|
|
Optional<uint8_t> readDWARFExpressionTargetReg(StringRef ExprBytes) {
|
|
|
|
uint8_t Opcode = ExprBytes[0];
|
|
|
|
if (Opcode == dwarf::DW_CFA_def_cfa_expression)
|
|
|
|
return NoneType();
|
|
|
|
assert((Opcode == dwarf::DW_CFA_expression ||
|
|
|
|
Opcode == dwarf::DW_CFA_val_expression) &&
|
|
|
|
"invalid DWARF expression CFI");
|
|
|
|
assert(ExprBytes.size() > 1 && "DWARF expression CFI is too short");
|
|
|
|
const uint8_t *const Start =
|
|
|
|
reinterpret_cast<const uint8_t *>(ExprBytes.drop_front(1).data());
|
|
|
|
const uint8_t *const End =
|
|
|
|
reinterpret_cast<const uint8_t *>(Start + ExprBytes.size() - 1);
|
|
|
|
uint8_t Reg = decodeULEB128(Start, nullptr, End);
|
|
|
|
return Reg;
|
|
|
|
}
|
|
|
|
|
2020-01-31 05:10:48 +08:00
|
|
|
} // namespace bolt
|
2020-12-02 08:29:39 +08:00
|
|
|
|
|
|
|
bool operator==(const llvm::MCCFIInstruction &L,
|
|
|
|
const llvm::MCCFIInstruction &R) {
|
|
|
|
if (L.getOperation() != R.getOperation())
|
|
|
|
return false;
|
|
|
|
switch (L.getOperation()) {
|
|
|
|
case MCCFIInstruction::OpRestore:
|
|
|
|
case MCCFIInstruction::OpSameValue:
|
|
|
|
case MCCFIInstruction::OpUndefined:
|
|
|
|
case MCCFIInstruction::OpDefCfaRegister:
|
|
|
|
return L.getRegister() == R.getRegister();
|
|
|
|
case MCCFIInstruction::OpRegister:
|
|
|
|
return L.getRegister() == R.getRegister() &&
|
|
|
|
L.getRegister2() == R.getRegister2();
|
|
|
|
case MCCFIInstruction::OpOffset:
|
|
|
|
case MCCFIInstruction::OpRelOffset:
|
|
|
|
case MCCFIInstruction::OpDefCfa:
|
|
|
|
return L.getRegister() == R.getRegister() && L.getOffset() == R.getOffset();
|
|
|
|
case MCCFIInstruction::OpEscape:
|
|
|
|
return L.getValues() == R.getValues();
|
|
|
|
case MCCFIInstruction::OpRememberState:
|
|
|
|
case MCCFIInstruction::OpRestoreState:
|
|
|
|
return true;
|
|
|
|
case MCCFIInstruction::OpDefCfaOffset:
|
|
|
|
case MCCFIInstruction::OpAdjustCfaOffset:
|
|
|
|
return L.getOffset() == R.getOffset();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 05:10:48 +08:00
|
|
|
} // namespace llvm
|