Skip to content

Commit 2563fd6

Browse files
committed
[BOLT][NFC] Use std::optional in MCPlusBuilder
Reviewed By: maksfb, #bolt Differential Revision: https://reviews.llvm.org/D139260
1 parent 370e476 commit 2563fd6

13 files changed

+42
-37
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,8 @@ class BinaryFunction {
894894
BinaryBasicBlock *getLandingPadBBFor(const BinaryBasicBlock &BB,
895895
const MCInst &InvokeInst) const {
896896
assert(BC.MIB->isInvoke(InvokeInst) && "must be invoke instruction");
897-
const Optional<MCPlus::MCLandingPad> LP = BC.MIB->getEHInfo(InvokeInst);
897+
const std::optional<MCPlus::MCLandingPad> LP =
898+
BC.MIB->getEHInfo(InvokeInst);
898899
if (LP && LP->first) {
899900
BinaryBasicBlock *LBB = BB.getLandingPad(LP->first);
900901
assert(LBB && "Landing pad should be defined");

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "bolt/Core/Relocation.h"
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/BitVector.h"
21-
#include "llvm/ADT/Optional.h"
2221
#include "llvm/ADT/StringMap.h"
2322
#include "llvm/MC/MCAsmBackend.h"
2423
#include "llvm/MC/MCDisassembler/MCSymbolizer.h"
@@ -33,6 +32,7 @@
3332
#include <cassert>
3433
#include <cstdint>
3534
#include <map>
35+
#include <optional>
3636
#include <system_error>
3737
#include <unordered_map>
3838
#include <unordered_set>
@@ -132,8 +132,8 @@ class MCPlusBuilder {
132132
AnnotationInst->addOperand(MCOperand::createImm(AnnotationValue));
133133
}
134134

135-
Optional<int64_t> getAnnotationOpValue(const MCInst &Inst,
136-
unsigned Index) const {
135+
std::optional<int64_t> getAnnotationOpValue(const MCInst &Inst,
136+
unsigned Index) const {
137137
const MCInst *AnnotationInst = getAnnotationInst(Inst);
138138
if (!AnnotationInst)
139139
return std::nullopt;
@@ -1049,7 +1049,7 @@ class MCPlusBuilder {
10491049
}
10501050

10511051
/// Return handler and action info for invoke instruction if present.
1052-
Optional<MCPlus::MCLandingPad> getEHInfo(const MCInst &Inst) const;
1052+
std::optional<MCPlus::MCLandingPad> getEHInfo(const MCInst &Inst) const;
10531053

10541054
/// Add handler and action info for call instruction.
10551055
void addEHInfo(MCInst &Inst, const MCPlus::MCLandingPad &LP);
@@ -1081,7 +1081,7 @@ class MCPlusBuilder {
10811081
bool unsetJumpTable(MCInst &Inst);
10821082

10831083
/// Return destination of conditional tail call instruction if \p Inst is one.
1084-
Optional<uint64_t> getConditionalTailCall(const MCInst &Inst) const;
1084+
std::optional<uint64_t> getConditionalTailCall(const MCInst &Inst) const;
10851085

10861086
/// Mark the \p Instruction as a conditional tail call, and set its
10871087
/// destination address if it is known. If \p Instruction was already marked,
@@ -1093,7 +1093,7 @@ class MCPlusBuilder {
10931093
bool unsetConditionalTailCall(MCInst &Inst);
10941094

10951095
/// Return offset of \p Inst in the original function, if available.
1096-
Optional<uint32_t> getOffset(const MCInst &Inst) const;
1096+
std::optional<uint32_t> getOffset(const MCInst &Inst) const;
10971097

10981098
/// Return the offset if the annotation is present, or \p Default otherwise.
10991099
uint32_t getOffsetWithDefault(const MCInst &Inst, uint32_t Default) const;
@@ -1591,8 +1591,8 @@ class MCPlusBuilder {
15911591

15921592
/// Create a target-specific relocation out of the \p Fixup.
15931593
/// Note that not every fixup could be converted into a relocation.
1594-
virtual Optional<Relocation> createRelocation(const MCFixup &Fixup,
1595-
const MCAsmBackend &MAB) const {
1594+
virtual std::optional<Relocation>
1595+
createRelocation(const MCFixup &Fixup, const MCAsmBackend &MAB) const {
15961596
llvm_unreachable("not implemented");
15971597
return Relocation();
15981598
}
@@ -1666,7 +1666,7 @@ class MCPlusBuilder {
16661666
}
16671667

16681668
/// Return annotation index matching the \p Name.
1669-
Optional<unsigned> getAnnotationIndex(StringRef Name) const {
1669+
std::optional<unsigned> getAnnotationIndex(StringRef Name) const {
16701670
auto AI = AnnotationNameIndexMap.find(Name);
16711671
if (AI != AnnotationNameIndexMap.end())
16721672
return AI->second;
@@ -1744,7 +1744,7 @@ class MCPlusBuilder {
17441744
/// Use hasAnnotation() if the annotation may not exist.
17451745
template <typename ValueType>
17461746
ValueType &getAnnotationAs(const MCInst &Inst, unsigned Index) const {
1747-
Optional<int64_t> Value = getAnnotationOpValue(Inst, Index);
1747+
std::optional<int64_t> Value = getAnnotationOpValue(Inst, Index);
17481748
assert(Value && "annotation should exist");
17491749
return reinterpret_cast<MCPlus::MCSimpleAnnotation<ValueType> *>(*Value)
17501750
->getValue();

bolt/lib/Core/BinaryContext.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,8 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
18461846
if (MIB->isTailCall(Instruction))
18471847
OS << " # TAILCALL ";
18481848
if (MIB->isInvoke(Instruction)) {
1849-
const Optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Instruction);
1849+
const std::optional<MCPlus::MCLandingPad> EHInfo =
1850+
MIB->getEHInfo(Instruction);
18501851
OS << " # handler: ";
18511852
if (EHInfo->first)
18521853
OS << *EHInfo->first;
@@ -1864,7 +1865,7 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
18641865
OS << " # UNKNOWN CONTROL FLOW";
18651866
}
18661867
}
1867-
if (Optional<uint32_t> Offset = MIB->getOffset(Instruction))
1868+
if (std::optional<uint32_t> Offset = MIB->getOffset(Instruction))
18681869
OS << " # Offset: " << *Offset;
18691870

18701871
MIB->printAnnotations(Instruction, OS);

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ bool BinaryFunction::scanExternalRefs() {
15481548

15491549
// Create relocation for every fixup.
15501550
for (const MCFixup &Fixup : Fixups) {
1551-
Optional<Relocation> Rel = BC.MIB->createRelocation(Fixup, *BC.MAB);
1551+
std::optional<Relocation> Rel = BC.MIB->createRelocation(Fixup, *BC.MAB);
15521552
if (!Rel) {
15531553
Success = false;
15541554
continue;
@@ -1923,7 +1923,8 @@ void BinaryFunction::recomputeLandingPads() {
19231923
if (!BC.MIB->isInvoke(Instr))
19241924
continue;
19251925

1926-
const Optional<MCPlus::MCLandingPad> EHInfo = BC.MIB->getEHInfo(Instr);
1926+
const std::optional<MCPlus::MCLandingPad> EHInfo =
1927+
BC.MIB->getEHInfo(Instr);
19271928
if (!EHInfo || !EHInfo->first)
19281929
continue;
19291930

@@ -2280,7 +2281,7 @@ void BinaryFunction::removeConditionalTailCalls() {
22802281
if (!CTCInstr)
22812282
continue;
22822283

2283-
Optional<uint64_t> TargetAddressOrNone =
2284+
std::optional<uint64_t> TargetAddressOrNone =
22842285
BC.MIB->getConditionalTailCall(*CTCInstr);
22852286
if (!TargetAddressOrNone)
22862287
continue;

bolt/lib/Core/Exceptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ void BinaryFunction::updateEHRanges() {
393393
// Extract exception handling information from the instruction.
394394
const MCSymbol *LP = nullptr;
395395
uint64_t Action = 0;
396-
if (const Optional<MCPlus::MCLandingPad> EHInfo =
396+
if (const std::optional<MCPlus::MCLandingPad> EHInfo =
397397
BC.MIB->getEHInfo(*II))
398398
std::tie(LP, Action) = *EHInfo;
399399

bolt/lib/Core/MCPlusBuilder.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ bool MCPlusBuilder::isTailCall(const MCInst &Inst) const {
133133
return false;
134134
}
135135

136-
Optional<MCLandingPad> MCPlusBuilder::getEHInfo(const MCInst &Inst) const {
136+
std::optional<MCLandingPad> MCPlusBuilder::getEHInfo(const MCInst &Inst) const {
137137
if (!isCall(Inst))
138138
return std::nullopt;
139-
Optional<int64_t> LPSym =
139+
std::optional<int64_t> LPSym =
140140
getAnnotationOpValue(Inst, MCAnnotation::kEHLandingPad);
141141
if (!LPSym)
142142
return std::nullopt;
143-
Optional<int64_t> Action =
143+
std::optional<int64_t> Action =
144144
getAnnotationOpValue(Inst, MCAnnotation::kEHAction);
145145
if (!Action)
146146
return std::nullopt;
@@ -171,7 +171,7 @@ bool MCPlusBuilder::updateEHInfo(MCInst &Inst, const MCLandingPad &LP) {
171171
}
172172

173173
int64_t MCPlusBuilder::getGnuArgsSize(const MCInst &Inst) const {
174-
Optional<int64_t> Value =
174+
std::optional<int64_t> Value =
175175
getAnnotationOpValue(Inst, MCAnnotation::kGnuArgsSize);
176176
if (!Value)
177177
return -1LL;
@@ -188,7 +188,7 @@ void MCPlusBuilder::addGnuArgsSize(MCInst &Inst, int64_t GnuArgsSize,
188188
}
189189

190190
uint64_t MCPlusBuilder::getJumpTable(const MCInst &Inst) const {
191-
Optional<int64_t> Value =
191+
std::optional<int64_t> Value =
192192
getAnnotationOpValue(Inst, MCAnnotation::kJumpTable);
193193
if (!Value)
194194
return 0;
@@ -216,9 +216,9 @@ bool MCPlusBuilder::unsetJumpTable(MCInst &Inst) {
216216
return true;
217217
}
218218

219-
Optional<uint64_t>
219+
std::optional<uint64_t>
220220
MCPlusBuilder::getConditionalTailCall(const MCInst &Inst) const {
221-
Optional<int64_t> Value =
221+
std::optional<int64_t> Value =
222222
getAnnotationOpValue(Inst, MCAnnotation::kConditionalTailCall);
223223
if (!Value)
224224
return std::nullopt;
@@ -240,16 +240,17 @@ bool MCPlusBuilder::unsetConditionalTailCall(MCInst &Inst) {
240240
return true;
241241
}
242242

243-
Optional<uint32_t> MCPlusBuilder::getOffset(const MCInst &Inst) const {
244-
Optional<int64_t> Value = getAnnotationOpValue(Inst, MCAnnotation::kOffset);
243+
std::optional<uint32_t> MCPlusBuilder::getOffset(const MCInst &Inst) const {
244+
std::optional<int64_t> Value =
245+
getAnnotationOpValue(Inst, MCAnnotation::kOffset);
245246
if (!Value)
246247
return std::nullopt;
247248
return static_cast<uint32_t>(*Value);
248249
}
249250

250251
uint32_t MCPlusBuilder::getOffsetWithDefault(const MCInst &Inst,
251252
uint32_t Default) const {
252-
if (Optional<uint32_t> Offset = getOffset(Inst))
253+
if (std::optional<uint32_t> Offset = getOffset(Inst))
253254
return *Offset;
254255
return Default;
255256
}

bolt/lib/Passes/DataflowAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void doForAllPreds(const BinaryBasicBlock &BB,
5858
for (MCInst &Inst : *Thrower) {
5959
if (!MIB->isInvoke(Inst))
6060
continue;
61-
const Optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Inst);
61+
const std::optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Inst);
6262
if (!EHInfo || EHInfo->first != BB.getLabel())
6363
continue;
6464
Task(ProgramPoint(&Inst));

bolt/lib/Passes/IdenticalCodeFolding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ bool isInstrEquivalentWith(const MCInst &InstA, const BinaryBasicBlock &BBA,
110110
// NB: there's no need to compare jump table indirect jump instructions
111111
// separately as jump tables are handled by comparing corresponding
112112
// symbols.
113-
const Optional<MCPlus::MCLandingPad> EHInfoA = BC.MIB->getEHInfo(InstA);
114-
const Optional<MCPlus::MCLandingPad> EHInfoB = BC.MIB->getEHInfo(InstB);
113+
const std::optional<MCPlus::MCLandingPad> EHInfoA = BC.MIB->getEHInfo(InstA);
114+
const std::optional<MCPlus::MCLandingPad> EHInfoB = BC.MIB->getEHInfo(InstB);
115115

116116
if (EHInfoA || EHInfoB) {
117117
if (!EHInfoA && (EHInfoB->first || EHInfoB->second))

bolt/lib/Passes/Inliner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ Inliner::inlineCall(BinaryBasicBlock &CallerBB,
250250
const bool CSIsInvoke = BC.MIB->isInvoke(*CallInst);
251251
const bool CSIsTailCall = BC.MIB->isTailCall(*CallInst);
252252
const int64_t CSGNUArgsSize = BC.MIB->getGnuArgsSize(*CallInst);
253-
const Optional<MCPlus::MCLandingPad> CSEHInfo = BC.MIB->getEHInfo(*CallInst);
253+
const std::optional<MCPlus::MCLandingPad> CSEHInfo =
254+
BC.MIB->getEHInfo(*CallInst);
254255

255256
// Split basic block at the call site if there will be more incoming edges
256257
// coming from the callee.

bolt/lib/Passes/SplitFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ SplitFunctions::createEHTrampolines(BinaryFunction &BF) const {
443443
std::vector<BinaryBasicBlock *> Blocks(BF.pbegin(), BF.pend());
444444
for (BinaryBasicBlock *BB : Blocks) {
445445
for (MCInst &Instr : *BB) {
446-
const Optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Instr);
446+
const std::optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Instr);
447447
if (!EHInfo || !EHInfo->first)
448448
continue;
449449

bolt/lib/Profile/YAMLProfileWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void convert(const BinaryFunction &BF,
5353
continue;
5454

5555
yaml::bolt::CallSiteInfo CSI;
56-
Optional<uint32_t> Offset = BC.MIB->getOffset(Instr);
56+
std::optional<uint32_t> Offset = BC.MIB->getOffset(Instr);
5757
if (!Offset || *Offset < BB->getInputOffset())
5858
continue;
5959
CSI.Offset = *Offset - BB->getInputOffset();

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ class X86MCPlusBuilder : public MCPlusBuilder {
25882588
return Code;
25892589
}
25902590

2591-
Optional<Relocation>
2591+
std::optional<Relocation>
25922592
createRelocation(const MCFixup &Fixup,
25932593
const MCAsmBackend &MAB) const override {
25942594
const MCFixupKindInfo &FKI = MAB.getFixupKindInfo(Fixup.getKind());
@@ -3594,15 +3594,15 @@ class X86MCPlusBuilder : public MCPlusBuilder {
35943594

35953595
if (CallOrJmp.getOpcode() == X86::CALL64r ||
35963596
CallOrJmp.getOpcode() == X86::CALL64pcrel32) {
3597-
if (Optional<uint32_t> Offset = getOffset(CallInst))
3597+
if (std::optional<uint32_t> Offset = getOffset(CallInst))
35983598
// Annotated as duplicated call
35993599
setOffset(CallOrJmp, *Offset);
36003600
}
36013601

36023602
if (isInvoke(CallInst) && !isInvoke(CallOrJmp)) {
36033603
// Copy over any EH or GNU args size information from the original
36043604
// call.
3605-
Optional<MCPlus::MCLandingPad> EHInfo = getEHInfo(CallInst);
3605+
std::optional<MCPlus::MCLandingPad> EHInfo = getEHInfo(CallInst);
36063606
if (EHInfo)
36073607
addEHInfo(CallOrJmp, *EHInfo);
36083608
int64_t GnuArgsSize = getGnuArgsSize(CallInst);

bolt/unittests/Core/MCPlusBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ TEST_P(MCPlusBuilderTester, Annotation) {
140140
// Test encodeAnnotationImm using this indirect way
141141
BC->MIB->addEHInfo(Inst, MCPlus::MCLandingPad(LPSymbol, Value));
142142
// Round-trip encoding-decoding check for negative values
143-
Optional<MCPlus::MCLandingPad> EHInfo = BC->MIB->getEHInfo(Inst);
143+
std::optional<MCPlus::MCLandingPad> EHInfo = BC->MIB->getEHInfo(Inst);
144144
ASSERT_TRUE(EHInfo.has_value());
145145
MCPlus::MCLandingPad LP = EHInfo.value();
146146
uint64_t DecodedValue = LP.second;

0 commit comments

Comments
 (0)