28class MCUnwindV2EpilogTargetExpr final :
public MCTargetExpr {
36 MCUnwindV2EpilogTargetExpr(
const WinEH::FrameInfo &FrameInfo,
37 const WinEH::FrameInfo::Epilog &
Epilog,
40 UnwindV2Start(
Epilog.UnwindV2Start), EpilogEnd(
Epilog.End),
41 EpilogSize(EpilogSize_), Loc(
Epilog.Loc) {}
44 static MCUnwindV2EpilogTargetExpr *
45 create(
const WinEH::FrameInfo &FrameInfo,
46 const WinEH::FrameInfo::Epilog &
Epilog, uint8_t EpilogSize_,
48 return new (Ctx) MCUnwindV2EpilogTargetExpr(FrameInfo,
Epilog, EpilogSize_);
51 void printImpl(raw_ostream &OS,
const MCAsmInfo *MAI)
const override {
53 UnwindV2Start->
print(OS, MAI);
56 bool evaluateAsRelocatableImpl(MCValue &Res,
57 const MCAssembler *Asm)
const override;
59 void visitUsedExpr(MCStreamer &Streamer)
const override {
63 MCFragment *findAssociatedFragment()
const override {
73 for (
const auto &
I : Insns) {
92 Count += (
I.Offset > 512 * 1024 - 8) ? 3 : 2;
123 if (inst.
Offset > 512 * 1024 - 8) {
136 b2 |= (((inst.
Offset - 8) >> 3) & 0x0F) << 4;
212static std::optional<int64_t>
223 if (!Diff->evaluateAsAbsolute(value, Assembler))
239 info->Symbol = Label;
242 bool LastEpilogIsAtEnd =
false;
243 bool AddPaddingEpilogCode =
false;
245 bool EnableUnwindV2 = (
info->Version >= 2) && !
info->EpilogMap.empty();
246 if (EnableUnwindV2) {
247 auto &LastEpilog =
info->EpilogMap.back().second;
256 OS->
getAssembler(), LastEpilog.End, LastEpilog.UnwindV2Start);
259 "Failed to evaluate epilog size for Unwind v2 in " +
260 info->Function->getName());
264 if (*MaybeSize >= (int64_t)UINT8_MAX) {
266 "Epilog size is too large for Unwind v2 in " +
267 info->Function->getName());
270 EpilogSize = *MaybeSize + 1;
277 LastEpilogIsAtEnd = (LastEpilogToFuncEnd == EpilogSize);
280 size_t numEpilogCodes =
281 info->EpilogMap.size() + (LastEpilogIsAtEnd ? 0 : 1);
282 if ((numEpilogCodes % 2) != 0) {
283 AddPaddingEpilogCode =
true;
288 if ((
size_t)numCodes + numEpilogCodes > UINT8_MAX) {
290 "Too many unwind codes with Unwind v2 enabled in " +
291 info->Function->getName());
295 numCodes += numEpilogCodes;
300 if (
info->ChainedParent)
303 if (
info->HandlesUnwind)
305 if (
info->HandlesExceptions)
318 if (
info->LastFrameInst >= 0) {
326 if (EnableUnwindV2) {
334 uint8_t Flags = LastEpilogIsAtEnd ? 0x01 : 0;
338 if (LastEpilogIsAtEnd)
345 auto *MCE = MCUnwindV2EpilogTargetExpr::create(*
info,
Epilog.second,
346 EpilogSize, context);
351 if (AddPaddingEpilogCode)
356 for (
uint8_t c = 0; c < numInst; ++c) {
358 info->Instructions.pop_back();
377 else if (numCodes == 0) {
385bool MCUnwindV2EpilogTargetExpr::evaluateAsRelocatableImpl(
391 Asm->getContext().reportError(
392 Loc,
"Failed to evaluate epilog offset for Unwind v2 in " +
397 constexpr uint16_t MaxEpilogOffset = 0x0fff;
398 if (*
Offset > MaxEpilogOffset) {
399 Asm->getContext().reportError(
401 "Epilog offset is too large for Unwind v2 in " +
Function->getName());
407 if (
Size != (EpilogSize - 1)) {
408 Asm->getContext().reportError(
409 Loc,
"Size of this epilog does not match size of last epilog in " +
414 auto HighBits = *
Offset >> 8;
438 bool HandlerData)
const {
468 std::optional<int64_t> MaybeDiff =
481 std::optional<int64_t> MaybeDistance =
487 for (
const auto &
I : Insns) {
503 if (Distance != InstructionBytes) {
505 SMLoc(),
"Incorrect size for " + Name +
" " +
Type +
": " +
507 " bytes of instructions in range, but .seh directives "
508 "corresponding to " +
509 Twine(InstructionBytes) +
" bytes\n");
515 for (
const auto &
I : Insns) {
630 b = (inst.
Offset >> 4) & 0x1F;
647 b = (w & 0x00FF0000) >> 16;
649 b = (w & 0x0000FF00) >> 8;
671 b |= (inst.
Offset >> 3) & 0x1F;
676 b |= ((inst.
Offset - 1) >> 3) & 0x3F;
681 b |= (inst.
Offset >> 3) & 0x3F;
687 b = 0xD0 | ((reg & 0xC) >> 2);
689 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
695 b = 0xD4 | ((reg & 0x8) >> 3);
697 b = ((reg & 0x7) << 5) | ((inst.
Offset >> 3) - 1);
703 b = 0xC8 | ((reg & 0xC) >> 2);
705 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
711 b = 0xCC | ((reg & 0xC) >> 2);
713 b = ((reg & 0x3) << 6) | ((inst.
Offset >> 3) - 1);
719 assert((reg % 2) == 0 &&
"Saved reg must be 19+2*X");
721 b = 0xD6 | ((reg & 0x7) >> 2);
723 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
729 b = 0xDC | ((reg & 0x4) >> 2);
731 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
739 b = ((reg & 0x7) << 5) | ((inst.
Offset >> 3) - 1);
745 b = 0xD8 | ((reg & 0x4) >> 2);
747 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
753 b = 0xDA | ((reg & 0x4) >> 2);
755 b = ((reg & 0x3) << 6) | ((inst.
Offset >> 3) - 1);
804 int Writeback =
Op / 6;
808 if (Writeback || Paired ||
Mode == 2)
815 b = inst.
Register | (Writeback << 5) | (Paired << 6);
834 b = ((inst.
Offset & 0xC0) >> 1) | reg;
836 b = 0xC0 | (inst.
Offset & 0x3F);
846 b = ((inst.
Offset & 0xC0) >> 1) | 0x10 | reg;
848 b = 0xC0 | (inst.
Offset & 0x3F);
861 const std::vector<MCSymbol *>& Epilogs,
863 for (
auto *EpilogStart : Epilogs) {
864 auto InstrsIter =
info->EpilogMap.find(EpilogStart);
866 "Epilog not found in EpilogMap");
867 const auto &Instrs = InstrsIter->second.Instructions;
869 if (Instrs.size() != EpilogInstrs.size())
873 for (
unsigned i = 0; i < Instrs.size(); ++i)
874 if (Instrs[i] != EpilogInstrs[i]) {
887 unsigned PrevOffset = -1;
888 unsigned PrevRegister = -1;
896 Inst.Register == 29) {
900 Inst.Register == 19 && Inst.Offset <= 248) {
906 Inst.Register == PrevRegister + 2 &&
907 Inst.Offset == PrevOffset + 16) {
922 PrevRegister = Inst.Register;
924 PrevOffset = Inst.Offset;
925 PrevRegister = Inst.Register;
939 for (
auto It = Instructions.rbegin(); It != Instructions.rend(); It++)
940 VisitInstruction(*It);
943 VisitInstruction(Inst);
950 const std::vector<WinEH::Instruction> &
Epilog) {
957 for (
int I =
Epilog.size() - 1;
I >= 0;
I--) {
972 int PrologCodeBytes) {
978 const std::vector<WinEH::Instruction> &
Epilog =
979 info->EpilogMap[Sym].Instructions;
985 if (DistanceFromEnd / 4 !=
Epilog.size())
993 if (PrologCodeBytes <= 31 &&
995 RetVal = PrologCodeBytes;
1004 if (
Offset > 31 || PrologCodeBytes > 124)
1009 info->EpilogMap.erase(Sym);
1014 int PackedEpilogOffset) {
1015 if (PackedEpilogOffset == 0) {
1021 }
else if (PackedEpilogOffset == 1) {
1031 unsigned RegI = 0, RegF = 0;
1032 int Predecrement = 0;
1044 bool StandaloneLR =
false, FPLRPair =
false;
1057 if (Location != Start)
1062 if (Location != Start2)
1068 if (Location != Start2 && Location != Start3)
1070 Predecrement = Inst.
Offset;
1075 if (Location != Start2 && Location != Start3)
1077 Predecrement = Inst.
Offset;
1081 StandaloneLR =
true;
1085 Location = FloatRegs;
1112 StandaloneLR =
true;
1116 Location = FloatRegs;
1123 StandaloneLR =
true;
1124 Location = FloatRegs;
1131 if (Location != FloatRegs || RegF == 0 || Inst.
Register != 8 + RegF ||
1132 Inst.
Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
1135 Location = InputArgs;
1138 if ((Location != Start2 && Location != Start3) || Inst.
Register != 8)
1140 Predecrement = Inst.
Offset;
1142 Location = FloatRegs;
1145 if ((Location !=
IntRegs && Location != FloatRegs) ||
1147 Inst.
Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
1150 Location = FloatRegs;
1155 else if (Location == FloatRegs)
1161 if (Location !=
IntRegs && Location != FloatRegs && Location != InputArgs)
1163 Location = InputArgs;
1168 if (Location != Start2 && Location != Start3 && Location !=
IntRegs &&
1169 Location != FloatRegs && Location != InputArgs &&
1170 Location != StackAdjust)
1180 Location = StackAdjust;
1185 if (Location != Start2 && Location != Start3 && Location !=
IntRegs &&
1186 Location != FloatRegs && Location != InputArgs)
1189 Location = FrameRecord;
1194 if (Location != StackAdjust || Inst.
Offset != 0)
1196 Location = FrameRecord;
1200 if (Location != FrameRecord)
1244 if (RegI > 10 || RegF > 8)
1246 if (StandaloneLR && FPLRPair)
1248 if (FPLRPair && Location != End)
1250 if (Nops != 0 && Nops != 4)
1252 if (PAC && !FPLRPair)
1265 int IntSZ = 8 * RegI;
1268 int FpSZ = 8 * RegF;
1269 int SavSZ = (IntSZ + FpSZ + 8 * 8 *
H + 0xF) & ~0xF;
1270 if (Predecrement != SavSZ)
1277 if (FrameSize > 0x1FF)
1279 assert(RegF != 1 &&
"One single float reg not allowed");
1282 assert(FuncLength <= 0x7FF &&
"FuncLength should have been checked earlier");
1284 int CR = PAC ? 2 : FPLRPair ? 3 : StandaloneLR ? 1 : 0;
1285 info->PackedInfo |= Flag << 0;
1286 info->PackedInfo |= (FuncLength & 0x7FF) << 2;
1287 info->PackedInfo |= (RegF & 0x7) << 13;
1288 info->PackedInfo |= (RegI & 0xF) << 16;
1289 info->PackedInfo |= (
H & 0x1) << 20;
1290 info->PackedInfo |= (CR & 0x3) << 21;
1291 info->PackedInfo |= (FrameSize & 0x1FF) << 23;
1300 std::vector<MCSymbol *> EpilogStarts;
1302 EpilogStarts.push_back(
I.first);
1305 std::vector<MCSymbol *> AddedEpilogs;
1306 for (
auto *S : EpilogStarts) {
1308 auto &EpilogInstrs =
info->EpilogMap[S].Instructions;
1314 if (MatchingEpilog) {
1316 "Duplicate epilog not found");
1317 EpilogInfo[EpilogStart] = EpilogInfo.
lookup(MatchingEpilog);
1320 EpilogInstrs.clear();
1322 EpilogInstrs)) >= 0) {
1323 EpilogInfo[EpilogStart] = PrologOffset;
1327 EpilogInfo[EpilogStart] += 1;
1330 EpilogInstrs.
clear();
1332 EpilogInfo[EpilogStart] = TotalCodeBytes;
1333 TotalCodeBytes += CodeBytes;
1334 AddedEpilogs.push_back(EpilogStart);
1341 int64_t RawFuncLength) {
1342 if (
info->PrologEnd)
1344 info->PrologEnd,
info->Function->getName(),
1346 struct EpilogStartEnd {
1353 for (
auto &
I :
info->EpilogMap) {
1355 auto &Instrs =
I.second.Instructions;
1358 info->Function->getName(),
"epilogue");
1360 "Epilogs should be monotonically ordered");
1367 int64_t SegLimit = 0xFFFFC;
1368 int64_t SegOffset = 0;
1370 if (RawFuncLength > SegLimit) {
1372 int64_t RemainingLength = RawFuncLength;
1374 while (RemainingLength > SegLimit) {
1378 int64_t SegLength = SegLimit;
1379 int64_t SegEnd = SegOffset + SegLength;
1383 while (
E < Epilogs.
size() && Epilogs[
E].End < SegEnd) {
1385 EpilogsInSegment[Epilogs[
E].Start] = Epilogs[
E].Offset;
1396 if (
E < Epilogs.
size() && Epilogs[
E].Offset <= SegEnd)
1398 SegLength = Epilogs[
E].Offset - SegOffset;
1401 SegOffset, SegLength, !SegOffset);
1402 Seg.Epilogs = std::move(EpilogsInSegment);
1403 info->Segments.push_back(Seg);
1405 SegOffset += SegLength;
1406 RemainingLength -= SegLength;
1415 for (;
E < Epilogs.
size(); ++
E)
1416 LastSeg.Epilogs[Epilogs[
E].Start] = Epilogs[
E].Offset;
1417 info->Segments.push_back(LastSeg);
1423 bool TryPacked =
true) {
1432 info->Symbol = Label;
1435 bool HasEpilogs = (Seg.
Epilogs.size() != 0);
1440 int PackedEpilogOffset = HasEpilogs ?
1447 if (
info->Segments.size() == 1 && PackedEpilogOffset >= 0 &&
1448 uint32_t(PackedEpilogOffset) < PrologCodeBytes &&
1449 !
info->HandlesExceptions && SegLength <= 0x7ff && TryPacked) {
1464 PrologCodeBytes += 1;
1465 if (PackedEpilogOffset >= 0)
1466 PackedEpilogOffset += 1;
1474 PackedEpilogOffset = 0;
1477 uint32_t TotalCodeBytes = PrologCodeBytes;
1485 uint32_t CodeWords = TotalCodeBytes / 4;
1486 uint32_t CodeWordsMod = TotalCodeBytes % 4;
1490 PackedEpilogOffset >= 0 ? PackedEpilogOffset : Seg.
Epilogs.size();
1491 bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124;
1492 if (!ExtensionWord) {
1493 row1 |= (EpilogCount & 0x1F) << 22;
1494 row1 |= (CodeWords & 0x1F) << 27;
1496 if (
info->HandlesExceptions)
1498 if (PackedEpilogOffset >= 0)
1500 row1 |= SegLength & 0x3FFFF;
1504 if (ExtensionWord) {
1506 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
1508 "SEH unwind data splitting is only implemented for large functions, "
1509 "cases of too many code words or too many epilogs will be done "
1512 row2 |= (CodeWords & 0xFF) << 16;
1513 row2 |= (EpilogCount & 0xFFFF);
1517 if (PackedEpilogOffset < 0) {
1519 for (
auto &
I : EpilogInfo) {
1527 row3 |= (EpilogIndex & 0x3FF) << 22;
1548 auto &EpilogInstrs =
info->EpilogMap[
I.first].Instructions;
1553 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
1555 for (
int i = 0; i < BytesMod; i++)
1558 if (
info->HandlesExceptions)
1568 bool TryPacked =
true) {
1576 if (
info->empty()) {
1577 info->EmitAttempted =
true;
1580 if (
info->EmitAttempted) {
1587 SMLoc(),
"Earlier .seh_handlerdata for " +
info->Function->getName() +
1588 " skipped due to no unwind info at the time "
1589 "(.seh_handlerdata too early?), but the function later "
1590 "did get unwind info that can't be emitted");
1595 for (
auto &
I :
info->EpilogMap)
1598 int64_t RawFuncLength;
1599 if (!
info->FuncletOrFuncEnd) {
1638 for (
auto &S :
info->Segments)
1642 info->Instructions.clear();
1647 for (
const auto &
I : Insns) {
1705 for (J = 3; J > 0; J--)
1706 if (
I.Offset & (0xffu << (8 * J)))
1717 bool *HasCustom =
nullptr) {
1719 for (
const auto &
I : Insns) {
1781 std::optional<int64_t> MaybeDistance =
1786 bool HasCustom =
false;
1790 if (Distance != InstructionBytes) {
1792 SMLoc(),
"Incorrect size for " + Name +
" " +
Type +
": " +
1794 " bytes of instructions in range, but .seh directives "
1795 "corresponding to " +
1796 Twine(InstructionBytes) +
" bytes\n");
1828 w = 0x8000 | (inst.
Register & 0x1fff) | (lr << 13);
1829 streamer.
emitInt8((w >> 8) & 0xff);
1830 streamer.
emitInt8((w >> 0) & 0xff);
1853 w = 0xe800 | (inst.
Offset / 4);
1854 streamer.
emitInt8((w >> 8) & 0xff);
1855 streamer.
emitInt8((w >> 0) & 0xff);
1860 w = 0xec00 | (inst.
Register & 0x0ff) | (lr << 8);
1861 streamer.
emitInt8((w >> 8) & 0xff);
1862 streamer.
emitInt8((w >> 0) & 0xff);
1889 streamer.
emitInt8((w >> 8) & 0xff);
1890 streamer.
emitInt8((w >> 0) & 0xff);
1897 streamer.
emitInt8((w >> 16) & 0xff);
1898 streamer.
emitInt8((w >> 8) & 0xff);
1899 streamer.
emitInt8((w >> 0) & 0xff);
1906 streamer.
emitInt8((w >> 8) & 0xff);
1907 streamer.
emitInt8((w >> 0) & 0xff);
1914 streamer.
emitInt8((w >> 16) & 0xff);
1915 streamer.
emitInt8((w >> 8) & 0xff);
1916 streamer.
emitInt8((w >> 0) & 0xff);
1934 for (i = 3; i > 0; i--)
1935 if (inst.
Offset & (0xffu << (8 * i)))
1950 const std::vector<WinEH::Instruction> &
Epilog,
1951 bool CanTweakProlog) {
1960 int EndIdx = CanTweakProlog ? 1 : 0;
1961 for (
int I =
Epilog.size() - 1;
I >= EndIdx;
I--) {
1968 if (CanTweakProlog) {
1986 int PrologCodeBytes) {
1988 if (
info->EpilogMap.size() != 1)
1998 if (
info->Instructions.empty() ||
Epilog.empty())
2004 streamer,
info->FuncletOrFuncEnd,
info->EpilogMap.begin()->first);
2009 if (DistanceFromEnd != InstructionBytes)
2017 if (PrologCodeBytes <= 31 &&
2019 RetVal = PrologCodeBytes;
2029 if (
Offset > 31 || PrologCodeBytes > 63)
2038 info->EpilogMap.clear();
2043 unsigned &Folded,
int &
IntRegs) {
2044 if (Mask & (1 << 14)) {
2048 if (Mask & (1 << 11)) {
2058 while ((Mask & 1) == 0) {
2062 if ((Mask & (Mask + 1)) != 0)
2066 while (Mask & (1 <<
N))
2085 bool Homing =
false;
2086 bool HasR11 =
false;
2087 bool HasChain =
false;
2092 unsigned StackAdjust = 0;
2127 if (Step != 1 && Step != 2)
2142 if (Step == 1 && Inst.
Register == 0x0f) {
2150 if (Step != 1 && Step != 2)
2162 if (Step != 3 || !HasR11 ||
IntRegs >= 0 || PF > 0)
2169 if (Step != 3 || !HasR11 || (
IntRegs < 0 && PF == 0))
2176 if (Step != 1 && Step != 2 && Step != 3 && Step != 4)
2189 if (Step != 1 && Step != 2 && Step != 3 && Step != 4 && Step != 5)
2193 if (Inst.
Offset / 4 >= 0x3f4)
2195 StackAdjust = Inst.
Offset / 4;
2200 if (HasR11 && !HasChain) {
2209 if (HasChain && !HasLR)
2213 if (
info->EpilogMap.size() > 1)
2218 if (
info->EpilogMap.size() == 0) {
2225 info->EpilogMap.begin()->second;
2230 streamer,
info->FuncletOrFuncEnd,
info->EpilogMap.begin()->first);
2235 if (DistanceFromEnd != InstructionBytes)
2238 bool GotStackAdjust =
false;
2239 bool GotFloatRegs =
false;
2240 bool GotIntRegs =
false;
2241 bool GotHomingRestore =
false;
2242 bool GotLRRestore =
false;
2243 bool NeedsReturn =
false;
2244 bool GotReturn =
false;
2266 if (Inst.
Offset / 4 >= 0x3f4)
2269 if (Homing && FloatRegs < 0 &&
IntRegs < 0 && StackAdjust == 0 &&
2270 PF == 0 && Inst.
Offset == 16) {
2271 GotHomingRestore =
true;
2274 if (StackAdjust > 0) {
2276 if (StackAdjust != Inst.
Offset / 4)
2278 GotStackAdjust =
true;
2279 }
else if (PF == Inst.
Offset / 4) {
2281 StackAdjust = Inst.
Offset / 4;
2282 GotStackAdjust =
true;
2289 }
else if (Step == 7 || Step == 8 || Step == 9) {
2290 if (!Homing || Inst.
Offset != 16)
2292 GotHomingRestore =
true;
2299 if (Step != 6 && Step != 7)
2302 if (FloatRegs != (
int)(Inst.
Register - 8))
2304 GotFloatRegs =
true;
2311 if (Step != 6 && Step != 7 && Step != 8)
2315 if (Homing && HasLR) {
2319 GotLRRestore =
true;
2325 if (HasLR != (Inst.
Offset == 1))
2328 GotLRRestore = Inst.
Offset == 1;
2347 if (Step != 6 && Step != 7 && Step != 8)
2352 bool CurHasLR =
false, CurHasR11 =
false;
2357 if (EF != PF && EF != StackAdjust)
2360 if (Homing && HasLR) {
2364 GotLRRestore =
true;
2370 if (CurHasLR != HasLR)
2372 GotLRRestore = CurHasLR;
2399 if (Step != 6 && Step != 7 && Step != 8 && Step != 9)
2401 if (!Homing || Inst.
Offset != 20 || GotLRRestore)
2403 GotLRRestore =
true;
2404 GotHomingRestore =
true;
2414 if (Step != 6 && Step != 7 && Step != 8 && Step != 9 && Step != 10)
2423 if (StackAdjust > 0 && !GotStackAdjust && EF == 0)
2425 if (FloatRegs >= 0 && !GotFloatRegs)
2427 if (
IntRegs >= 0 && !GotIntRegs)
2429 if (Homing && !GotHomingRestore)
2431 if (HasLR && !GotLRRestore)
2433 if (NeedsReturn && !GotReturn)
2437 assert(PF == 0 || EF == 0 ||
2439 if (PF > 0 || EF > 0) {
2440 StackAdjust = PF > 0 ? (PF - 1) : (EF - 1);
2441 assert(StackAdjust <= 3);
2442 StackAdjust |= 0x3f0;
2444 StackAdjust |= 1 << 2;
2446 StackAdjust |= 1 << 3;
2449 assert(FuncLength <= 0x7FF &&
"FuncLength should have been checked earlier");
2450 int Flag =
info->Fragment ? 0x02 : 0x01;
2451 int H = Homing ? 1 : 0;
2452 int L = HasLR ? 1 : 0;
2453 int C = HasChain ? 1 : 0;
2460 }
else if (FloatRegs >= 0) {
2469 info->PackedInfo |= Flag << 0;
2470 info->PackedInfo |= (FuncLength & 0x7FF) << 2;
2471 info->PackedInfo |= (Ret & 0x3) << 13;
2472 info->PackedInfo |=
H << 15;
2473 info->PackedInfo |=
Reg << 16;
2474 info->PackedInfo |= R << 19;
2475 info->PackedInfo |= L << 20;
2476 info->PackedInfo |=
C << 21;
2477 assert(StackAdjust <= 0x3ff);
2478 info->PackedInfo |= StackAdjust << 22;
2485 bool TryPacked =
true) {
2493 if (
info->empty()) {
2494 info->EmitAttempted =
true;
2497 if (
info->EmitAttempted) {
2504 SMLoc(),
"Earlier .seh_handlerdata for " +
info->Function->getName() +
2505 " skipped due to no unwind info at the time "
2506 "(.seh_handlerdata too early?), but the function later "
2507 "did get unwind info that can't be emitted");
2516 info->Symbol = Label;
2518 if (!
info->PrologEnd)
2520 info->Function->getName() +
2521 " not correctly terminated");
2523 if (
info->PrologEnd && !
info->Fragment)
2525 info->PrologEnd,
info->Function->getName(),
2527 for (
auto &
I :
info->EpilogMap) {
2531 info->Function->getName(),
"epilogue");
2532 if (
Epilog.Instructions.empty() ||
2535 SMLoc(),
"Epilogue in " +
info->Function->getName() +
2536 " not correctly terminated");
2539 std::optional<int64_t> RawFuncLength;
2540 const MCExpr *FuncLengthExpr =
nullptr;
2541 if (!
info->FuncletOrFuncEnd) {
2560 FuncLength = (
uint32_t)*RawFuncLength / 2;
2561 if (FuncLength > 0x3FFFF)
2564 uint32_t TotalCodeBytes = PrologCodeBytes;
2566 if (!
info->HandlesExceptions && RawFuncLength && FuncLength <= 0x7ff &&
2580 int PackedEpilogOffset =
2586 std::vector<MCSymbol *> AddedEpilogs;
2588 bool CanTweakProlog =
true;
2589 for (
auto &
I :
info->EpilogMap) {
2591 auto &EpilogInstrs =
I.second.Instructions;
2597 if (MatchingEpilog) {
2599 "Duplicate epilog not found");
2600 EpilogInfo[EpilogStart] = EpilogInfo.
lookup(MatchingEpilog);
2603 EpilogInstrs.clear();
2605 info->Instructions, EpilogInstrs, CanTweakProlog)) >= 0) {
2606 if (CanTweakProlog) {
2609 info->Instructions.front() = EpilogInstrs.back();
2611 CanTweakProlog =
false;
2613 EpilogInfo[EpilogStart] = PrologOffset;
2616 EpilogInstrs.clear();
2618 EpilogInfo[EpilogStart] = TotalCodeBytes;
2619 TotalCodeBytes += CodeBytes;
2620 AddedEpilogs.push_back(EpilogStart);
2626 uint32_t CodeWords = TotalCodeBytes / 4;
2627 uint32_t CodeWordsMod = TotalCodeBytes % 4;
2631 PackedEpilogOffset >= 0 ? PackedEpilogOffset :
info->EpilogMap.size();
2632 bool ExtensionWord = EpilogCount > 31 || CodeWords > 15;
2633 if (!ExtensionWord) {
2634 row1 |= (EpilogCount & 0x1F) << 23;
2635 row1 |= (CodeWords & 0x0F) << 28;
2637 if (
info->HandlesExceptions)
2639 if (PackedEpilogOffset >= 0)
2643 row1 |= FuncLength & 0x3FFFF;
2653 if (ExtensionWord) {
2655 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
2658 row2 |= (CodeWords & 0xFF) << 16;
2659 row2 |= (EpilogCount & 0xFFFF);
2663 if (PackedEpilogOffset < 0) {
2665 for (
auto &
I : EpilogInfo) {
2669 std::optional<int64_t> MaybeEpilogOffset =
2671 const MCExpr *OffsetExpr =
nullptr;
2673 if (MaybeEpilogOffset)
2674 EpilogOffset = *MaybeEpilogOffset / 2;
2678 assert(
info->EpilogMap.contains(EpilogStart));
2679 unsigned Condition =
info->EpilogMap[EpilogStart].Condition;
2680 assert(Condition <= 0xf);
2683 row3 |= Condition << 20;
2684 row3 |= (EpilogIndex & 0x3FF) << 24;
2685 if (MaybeEpilogOffset)
2697 for (
uint8_t c = 0; c < numInst; ++c) {
2699 info->Instructions.pop_back();
2704 for (
auto &
I :
info->EpilogMap) {
2705 auto &EpilogInstrs =
I.second.Instructions;
2710 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
2712 for (
int i = 0; i < BytesMod; i++)
2715 if (
info->HandlesExceptions)
2727 for (
const auto &S :
info->Segments) {
2729 if (
info->PackedInfo)
2746 if (
info->PackedInfo)
2782 bool HandlerData)
const {
2789 if (!
info->FuncletOrFuncEnd) {
2827 bool HandlerData)
const {
2834 if (!
info->FuncletOrFuncEnd) {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static int checkARM64PackedEpilog(MCStreamer &streamer, WinEH::FrameInfo *info, WinEH::FrameInfo::Segment *Seg, int PrologCodeBytes)
static void ARM64EmitUnwindInfoForSegment(MCStreamer &streamer, WinEH::FrameInfo *info, WinEH::FrameInfo::Segment &Seg, bool TryPacked=true)
static uint32_t ARMCountOfUnwindCodes(ArrayRef< WinEH::Instruction > Insns)
static uint32_t ARM64CountOfUnwindCodes(ArrayRef< WinEH::Instruction > Insns)
static void checkARMInstructions(MCStreamer &Streamer, ArrayRef< WinEH::Instruction > Insns, const MCSymbol *Begin, const MCSymbol *End, StringRef Name, StringRef Type)
static bool isARMTerminator(const WinEH::Instruction &inst)
static void ARM64EmitUnwindCode(MCStreamer &streamer, const WinEH::Instruction &inst)
static void simplifyARM64Opcodes(std::vector< WinEH::Instruction > &Instructions, bool Reverse)
static void ARMEmitUnwindCode(MCStreamer &streamer, const WinEH::Instruction &inst)
static std::optional< int64_t > GetOptionalAbsDifference(const MCAssembler &Assembler, const MCSymbol *LHS, const MCSymbol *RHS)
static int getARM64OffsetInProlog(const std::vector< WinEH::Instruction > &Prolog, const std::vector< WinEH::Instruction > &Epilog)
static void ARMEmitRuntimeFunction(MCStreamer &streamer, const WinEH::FrameInfo *info)
static bool tryARM64PackedUnwind(WinEH::FrameInfo *info, uint32_t FuncLength, int PackedEpilogOffset)
static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info)
static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin, WinEH::Instruction &inst)
static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info, bool TryPacked=true)
static int getARMOffsetInProlog(const std::vector< WinEH::Instruction > &Prolog, const std::vector< WinEH::Instruction > &Epilog, bool CanTweakProlog)
static void ARM64EmitRuntimeFunction(MCStreamer &streamer, const WinEH::FrameInfo *info)
static void EmitSymbolRefWithOfs(MCStreamer &streamer, const MCSymbol *Base, int64_t Offset)
static bool parseRegMask(unsigned Mask, bool &HasLR, bool &HasR11, unsigned &Folded, int &IntRegs)
static int checkARMPackedEpilog(MCStreamer &streamer, WinEH::FrameInfo *info, int PrologCodeBytes)
static int64_t GetAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS, const MCSymbol *RHS)
static void ARM64FindSegmentsInFunction(MCStreamer &streamer, WinEH::FrameInfo *info, int64_t RawFuncLength)
static bool tryARMPackedUnwind(MCStreamer &streamer, WinEH::FrameInfo *info, uint32_t FuncLength)
static MCSymbol * FindMatchingEpilog(const std::vector< WinEH::Instruction > &EpilogInstrs, const std::vector< MCSymbol * > &Epilogs, const WinEH::FrameInfo *info)
static void EmitRuntimeFunction(MCStreamer &streamer, const WinEH::FrameInfo *info)
static void checkARM64Instructions(MCStreamer &Streamer, ArrayRef< WinEH::Instruction > Insns, const MCSymbol *Begin, const MCSymbol *End, StringRef Name, StringRef Type)
static const MCExpr * GetSubDivExpr(MCStreamer &Streamer, const MCSymbol *LHS, const MCSymbol *RHS, int Div)
static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS, const MCSymbol *RHS)
static void ARMEmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info, bool TryPacked=true)
static uint32_t ARMCountOfInstructionBytes(ArrayRef< WinEH::Instruction > Insns, bool *HasCustom=nullptr)
static void ARM64ProcessEpilogs(WinEH::FrameInfo *info, WinEH::FrameInfo::Segment *Seg, uint32_t &TotalCodeBytes, MapVector< MCSymbol *, uint32_t > &EpilogInfo)
static uint8_t CountOfUnwindCodes(std::vector< WinEH::Instruction > &Insns)
static void printImpl(const MCAsmInfo &MAI, raw_ostream &OS, const MCSpecifierExpr &Expr)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static const MCPhysReg IntRegs[32]
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Tagged union holding either a T or a Error.
MCContext & getContext() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
static const MCBinaryExpr * createOr(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
static const MCBinaryExpr * createDiv(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Context object for machine code objects.
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Base class for the full range of assembler expressions which are needed for parsing.
Streaming object file generation interface.
MCAssembler & getAssembler()
void appendContents(ArrayRef< char > Contents)
void addFixup(const MCExpr *Value, MCFixupKind Kind)
void ensureHeadroom(size_t Headroom)
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Streaming machine code generation interface.
virtual MCSymbol * emitCFILabel()
When emitting an object file, create and emit a real label.
MCSection * getAssociatedPDataSection(const MCSection *TextSec)
Get the .pdata section used for the given section.
MCContext & getContext() const
MCSection * getAssociatedXDataSection(const MCSection *TextSec)
Get the .xdata section used for the given section.
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
virtual void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitInt16(uint64_t Value)
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
void emitInt32(uint64_t Value)
ArrayRef< std::unique_ptr< WinEH::FrameInfo > > getWinFrameInfos() const
void emitInt8(uint64_t Value)
Represent a reference to a symbol from inside an expression.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
MCFragment * getFragment() const
static MCValue get(const MCSymbol *SymA, const MCSymbol *SymB=nullptr, int64_t Val=0, uint32_t Specifier=0)
This class implements a map that also provides access to all stored values in a deterministic order.
bool contains(const KeyT &Key) const
ValueT lookup(const KeyT &Key) const
Represents a location in source code.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StackOffset holds a fixed and a scalable offset in bytes.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI, bool HandlerData) const override
void Emit(MCStreamer &Streamer) const override
This emits the unwind info sections (.pdata and .xdata in PE/COFF).
void Emit(MCStreamer &Streamer) const override
This emits the unwind info sections (.pdata and .xdata in PE/COFF).
void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI, bool HandlerData) const override
void Emit(MCStreamer &Streamer) const override
This emits the unwind info sections (.pdata and .xdata in PE/COFF).
void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI, bool HandlerData) const override
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
@ UNW_TerminateHandler
UNW_TerminateHandler - Specifies that this function has a termination handler.
@ UNW_ExceptionHandler
UNW_ExceptionHandler - Specifies that this function has an exception handler.
@ UNW_ChainInfo
UNW_ChainInfo - Specifies that this UnwindInfo structure is chained to another one.
UnwindOpcodes
UnwindOpcodes - Enumeration whose values specify a single operation in the prolog of a function.
@ UOP_WideSaveRegsR4R11LR
This is an optimization pass for GlobalISel generic memory operations.
auto reverse(ContainerTy &&C)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
FunctionAddr VTableAddr Count
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ FK_Data_2
A two-byte fixup.
DWARFExpression::Operation Op
This struct is a compact representation of a valid (non-zero power of two) alignment.
std::vector< Instruction > Instructions
MapVector< MCSymbol *, int64_t > Epilogs