[BOLT][NFC] Use std::optional in MCPlusBuilder

Reviewed By: maksfb, #bolt

Differential Revision: https://reviews.llvm.org/D139260
This commit is contained in:
Amir Ayupov
2022-12-06 14:15:54 -08:00
parent 370e4761bc
commit 2563fd63c6
13 changed files with 42 additions and 37 deletions

View File

@@ -894,7 +894,8 @@ public:
BinaryBasicBlock *getLandingPadBBFor(const BinaryBasicBlock &BB,
const MCInst &InvokeInst) const {
assert(BC.MIB->isInvoke(InvokeInst) && "must be invoke instruction");
const Optional<MCPlus::MCLandingPad> LP = BC.MIB->getEHInfo(InvokeInst);
const std::optional<MCPlus::MCLandingPad> LP =
BC.MIB->getEHInfo(InvokeInst);
if (LP && LP->first) {
BinaryBasicBlock *LBB = BB.getLandingPad(LP->first);
assert(LBB && "Landing pad should be defined");

View File

@@ -18,7 +18,6 @@
#include "bolt/Core/Relocation.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCDisassembler/MCSymbolizer.h"
@@ -33,6 +32,7 @@
#include <cassert>
#include <cstdint>
#include <map>
#include <optional>
#include <system_error>
#include <unordered_map>
#include <unordered_set>
@@ -132,8 +132,8 @@ private:
AnnotationInst->addOperand(MCOperand::createImm(AnnotationValue));
}
Optional<int64_t> getAnnotationOpValue(const MCInst &Inst,
unsigned Index) const {
std::optional<int64_t> getAnnotationOpValue(const MCInst &Inst,
unsigned Index) const {
const MCInst *AnnotationInst = getAnnotationInst(Inst);
if (!AnnotationInst)
return std::nullopt;
@@ -1049,7 +1049,7 @@ public:
}
/// Return handler and action info for invoke instruction if present.
Optional<MCPlus::MCLandingPad> getEHInfo(const MCInst &Inst) const;
std::optional<MCPlus::MCLandingPad> getEHInfo(const MCInst &Inst) const;
/// Add handler and action info for call instruction.
void addEHInfo(MCInst &Inst, const MCPlus::MCLandingPad &LP);
@@ -1081,7 +1081,7 @@ public:
bool unsetJumpTable(MCInst &Inst);
/// Return destination of conditional tail call instruction if \p Inst is one.
Optional<uint64_t> getConditionalTailCall(const MCInst &Inst) const;
std::optional<uint64_t> getConditionalTailCall(const MCInst &Inst) const;
/// Mark the \p Instruction as a conditional tail call, and set its
/// destination address if it is known. If \p Instruction was already marked,
@@ -1093,7 +1093,7 @@ public:
bool unsetConditionalTailCall(MCInst &Inst);
/// Return offset of \p Inst in the original function, if available.
Optional<uint32_t> getOffset(const MCInst &Inst) const;
std::optional<uint32_t> getOffset(const MCInst &Inst) const;
/// Return the offset if the annotation is present, or \p Default otherwise.
uint32_t getOffsetWithDefault(const MCInst &Inst, uint32_t Default) const;
@@ -1591,8 +1591,8 @@ public:
/// Create a target-specific relocation out of the \p Fixup.
/// Note that not every fixup could be converted into a relocation.
virtual Optional<Relocation> createRelocation(const MCFixup &Fixup,
const MCAsmBackend &MAB) const {
virtual std::optional<Relocation>
createRelocation(const MCFixup &Fixup, const MCAsmBackend &MAB) const {
llvm_unreachable("not implemented");
return Relocation();
}
@@ -1666,7 +1666,7 @@ public:
}
/// Return annotation index matching the \p Name.
Optional<unsigned> getAnnotationIndex(StringRef Name) const {
std::optional<unsigned> getAnnotationIndex(StringRef Name) const {
auto AI = AnnotationNameIndexMap.find(Name);
if (AI != AnnotationNameIndexMap.end())
return AI->second;
@@ -1744,7 +1744,7 @@ public:
/// Use hasAnnotation() if the annotation may not exist.
template <typename ValueType>
ValueType &getAnnotationAs(const MCInst &Inst, unsigned Index) const {
Optional<int64_t> Value = getAnnotationOpValue(Inst, Index);
std::optional<int64_t> Value = getAnnotationOpValue(Inst, Index);
assert(Value && "annotation should exist");
return reinterpret_cast<MCPlus::MCSimpleAnnotation<ValueType> *>(*Value)
->getValue();

View File

@@ -1846,7 +1846,8 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
if (MIB->isTailCall(Instruction))
OS << " # TAILCALL ";
if (MIB->isInvoke(Instruction)) {
const Optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Instruction);
const std::optional<MCPlus::MCLandingPad> EHInfo =
MIB->getEHInfo(Instruction);
OS << " # handler: ";
if (EHInfo->first)
OS << *EHInfo->first;
@@ -1864,7 +1865,7 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
OS << " # UNKNOWN CONTROL FLOW";
}
}
if (Optional<uint32_t> Offset = MIB->getOffset(Instruction))
if (std::optional<uint32_t> Offset = MIB->getOffset(Instruction))
OS << " # Offset: " << *Offset;
MIB->printAnnotations(Instruction, OS);

View File

@@ -1548,7 +1548,7 @@ bool BinaryFunction::scanExternalRefs() {
// Create relocation for every fixup.
for (const MCFixup &Fixup : Fixups) {
Optional<Relocation> Rel = BC.MIB->createRelocation(Fixup, *BC.MAB);
std::optional<Relocation> Rel = BC.MIB->createRelocation(Fixup, *BC.MAB);
if (!Rel) {
Success = false;
continue;
@@ -1923,7 +1923,8 @@ void BinaryFunction::recomputeLandingPads() {
if (!BC.MIB->isInvoke(Instr))
continue;
const Optional<MCPlus::MCLandingPad> EHInfo = BC.MIB->getEHInfo(Instr);
const std::optional<MCPlus::MCLandingPad> EHInfo =
BC.MIB->getEHInfo(Instr);
if (!EHInfo || !EHInfo->first)
continue;
@@ -2280,7 +2281,7 @@ void BinaryFunction::removeConditionalTailCalls() {
if (!CTCInstr)
continue;
Optional<uint64_t> TargetAddressOrNone =
std::optional<uint64_t> TargetAddressOrNone =
BC.MIB->getConditionalTailCall(*CTCInstr);
if (!TargetAddressOrNone)
continue;

View File

@@ -393,7 +393,7 @@ void BinaryFunction::updateEHRanges() {
// Extract exception handling information from the instruction.
const MCSymbol *LP = nullptr;
uint64_t Action = 0;
if (const Optional<MCPlus::MCLandingPad> EHInfo =
if (const std::optional<MCPlus::MCLandingPad> EHInfo =
BC.MIB->getEHInfo(*II))
std::tie(LP, Action) = *EHInfo;

View File

@@ -133,14 +133,14 @@ bool MCPlusBuilder::isTailCall(const MCInst &Inst) const {
return false;
}
Optional<MCLandingPad> MCPlusBuilder::getEHInfo(const MCInst &Inst) const {
std::optional<MCLandingPad> MCPlusBuilder::getEHInfo(const MCInst &Inst) const {
if (!isCall(Inst))
return std::nullopt;
Optional<int64_t> LPSym =
std::optional<int64_t> LPSym =
getAnnotationOpValue(Inst, MCAnnotation::kEHLandingPad);
if (!LPSym)
return std::nullopt;
Optional<int64_t> Action =
std::optional<int64_t> Action =
getAnnotationOpValue(Inst, MCAnnotation::kEHAction);
if (!Action)
return std::nullopt;
@@ -171,7 +171,7 @@ bool MCPlusBuilder::updateEHInfo(MCInst &Inst, const MCLandingPad &LP) {
}
int64_t MCPlusBuilder::getGnuArgsSize(const MCInst &Inst) const {
Optional<int64_t> Value =
std::optional<int64_t> Value =
getAnnotationOpValue(Inst, MCAnnotation::kGnuArgsSize);
if (!Value)
return -1LL;
@@ -188,7 +188,7 @@ void MCPlusBuilder::addGnuArgsSize(MCInst &Inst, int64_t GnuArgsSize,
}
uint64_t MCPlusBuilder::getJumpTable(const MCInst &Inst) const {
Optional<int64_t> Value =
std::optional<int64_t> Value =
getAnnotationOpValue(Inst, MCAnnotation::kJumpTable);
if (!Value)
return 0;
@@ -216,9 +216,9 @@ bool MCPlusBuilder::unsetJumpTable(MCInst &Inst) {
return true;
}
Optional<uint64_t>
std::optional<uint64_t>
MCPlusBuilder::getConditionalTailCall(const MCInst &Inst) const {
Optional<int64_t> Value =
std::optional<int64_t> Value =
getAnnotationOpValue(Inst, MCAnnotation::kConditionalTailCall);
if (!Value)
return std::nullopt;
@@ -240,8 +240,9 @@ bool MCPlusBuilder::unsetConditionalTailCall(MCInst &Inst) {
return true;
}
Optional<uint32_t> MCPlusBuilder::getOffset(const MCInst &Inst) const {
Optional<int64_t> Value = getAnnotationOpValue(Inst, MCAnnotation::kOffset);
std::optional<uint32_t> MCPlusBuilder::getOffset(const MCInst &Inst) const {
std::optional<int64_t> Value =
getAnnotationOpValue(Inst, MCAnnotation::kOffset);
if (!Value)
return std::nullopt;
return static_cast<uint32_t>(*Value);
@@ -249,7 +250,7 @@ Optional<uint32_t> MCPlusBuilder::getOffset(const MCInst &Inst) const {
uint32_t MCPlusBuilder::getOffsetWithDefault(const MCInst &Inst,
uint32_t Default) const {
if (Optional<uint32_t> Offset = getOffset(Inst))
if (std::optional<uint32_t> Offset = getOffset(Inst))
return *Offset;
return Default;
}

View File

@@ -58,7 +58,7 @@ void doForAllPreds(const BinaryBasicBlock &BB,
for (MCInst &Inst : *Thrower) {
if (!MIB->isInvoke(Inst))
continue;
const Optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Inst);
const std::optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Inst);
if (!EHInfo || EHInfo->first != BB.getLabel())
continue;
Task(ProgramPoint(&Inst));

View File

@@ -110,8 +110,8 @@ bool isInstrEquivalentWith(const MCInst &InstA, const BinaryBasicBlock &BBA,
// NB: there's no need to compare jump table indirect jump instructions
// separately as jump tables are handled by comparing corresponding
// symbols.
const Optional<MCPlus::MCLandingPad> EHInfoA = BC.MIB->getEHInfo(InstA);
const Optional<MCPlus::MCLandingPad> EHInfoB = BC.MIB->getEHInfo(InstB);
const std::optional<MCPlus::MCLandingPad> EHInfoA = BC.MIB->getEHInfo(InstA);
const std::optional<MCPlus::MCLandingPad> EHInfoB = BC.MIB->getEHInfo(InstB);
if (EHInfoA || EHInfoB) {
if (!EHInfoA && (EHInfoB->first || EHInfoB->second))

View File

@@ -250,7 +250,8 @@ Inliner::inlineCall(BinaryBasicBlock &CallerBB,
const bool CSIsInvoke = BC.MIB->isInvoke(*CallInst);
const bool CSIsTailCall = BC.MIB->isTailCall(*CallInst);
const int64_t CSGNUArgsSize = BC.MIB->getGnuArgsSize(*CallInst);
const Optional<MCPlus::MCLandingPad> CSEHInfo = BC.MIB->getEHInfo(*CallInst);
const std::optional<MCPlus::MCLandingPad> CSEHInfo =
BC.MIB->getEHInfo(*CallInst);
// Split basic block at the call site if there will be more incoming edges
// coming from the callee.

View File

@@ -443,7 +443,7 @@ SplitFunctions::createEHTrampolines(BinaryFunction &BF) const {
std::vector<BinaryBasicBlock *> Blocks(BF.pbegin(), BF.pend());
for (BinaryBasicBlock *BB : Blocks) {
for (MCInst &Instr : *BB) {
const Optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Instr);
const std::optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Instr);
if (!EHInfo || !EHInfo->first)
continue;

View File

@@ -53,7 +53,7 @@ void convert(const BinaryFunction &BF,
continue;
yaml::bolt::CallSiteInfo CSI;
Optional<uint32_t> Offset = BC.MIB->getOffset(Instr);
std::optional<uint32_t> Offset = BC.MIB->getOffset(Instr);
if (!Offset || *Offset < BB->getInputOffset())
continue;
CSI.Offset = *Offset - BB->getInputOffset();

View File

@@ -2588,7 +2588,7 @@ public:
return Code;
}
Optional<Relocation>
std::optional<Relocation>
createRelocation(const MCFixup &Fixup,
const MCAsmBackend &MAB) const override {
const MCFixupKindInfo &FKI = MAB.getFixupKindInfo(Fixup.getKind());
@@ -3594,7 +3594,7 @@ public:
if (CallOrJmp.getOpcode() == X86::CALL64r ||
CallOrJmp.getOpcode() == X86::CALL64pcrel32) {
if (Optional<uint32_t> Offset = getOffset(CallInst))
if (std::optional<uint32_t> Offset = getOffset(CallInst))
// Annotated as duplicated call
setOffset(CallOrJmp, *Offset);
}
@@ -3602,7 +3602,7 @@ public:
if (isInvoke(CallInst) && !isInvoke(CallOrJmp)) {
// Copy over any EH or GNU args size information from the original
// call.
Optional<MCPlus::MCLandingPad> EHInfo = getEHInfo(CallInst);
std::optional<MCPlus::MCLandingPad> EHInfo = getEHInfo(CallInst);
if (EHInfo)
addEHInfo(CallOrJmp, *EHInfo);
int64_t GnuArgsSize = getGnuArgsSize(CallInst);

View File

@@ -140,7 +140,7 @@ TEST_P(MCPlusBuilderTester, Annotation) {
// Test encodeAnnotationImm using this indirect way
BC->MIB->addEHInfo(Inst, MCPlus::MCLandingPad(LPSymbol, Value));
// Round-trip encoding-decoding check for negative values
Optional<MCPlus::MCLandingPad> EHInfo = BC->MIB->getEHInfo(Inst);
std::optional<MCPlus::MCLandingPad> EHInfo = BC->MIB->getEHInfo(Inst);
ASSERT_TRUE(EHInfo.has_value());
MCPlus::MCLandingPad LP = EHInfo.value();
uint64_t DecodedValue = LP.second;