36 UnwindV2Start(
Epilog.UnwindV2Start), EpilogEnd(
Epilog.End),
37 EpilogSize(EpilogSize_), Loc(
Epilog.Loc) {}
71 for (
const auto &
I : Insns) {
90 Count += (
I.Offset > 512 * 1024 - 8) ? 3 : 2;
121 if (inst.
Offset > 512 * 1024 - 8) {
134 b2 |= (((inst.
Offset - 8) >> 3) & 0x0F) << 4;
210static std::optional<int64_t>
221 if (!Diff->evaluateAsAbsolute(
value, Assembler))
237 info->Symbol = Label;
240 bool LastEpilogIsAtEnd =
false;
241 bool AddPaddingEpilogCode =
false;
243 bool EnableUnwindV2 = (
info->Version >= 2) && !
info->EpilogMap.empty();
244 if (EnableUnwindV2) {
245 auto &LastEpilog =
info->EpilogMap.back().second;
254 OS->getAssembler(), LastEpilog.End, LastEpilog.UnwindV2Start);
257 "Failed to evaluate epilog size for Unwind v2 in " +
258 info->Function->getName());
262 if (*MaybeSize >= (int64_t)UINT8_MAX) {
264 "Epilog size is too large for Unwind v2 in " +
265 info->Function->getName());
268 EpilogSize = *MaybeSize + 1;
274 OS->getAssembler(),
info->FuncletOrFuncEnd, LastEpilog.UnwindV2Start);
275 LastEpilogIsAtEnd = (LastEpilogToFuncEnd == EpilogSize);
278 size_t numEpilogCodes =
279 info->EpilogMap.size() + (LastEpilogIsAtEnd ? 0 : 1);
280 if ((numEpilogCodes % 2) != 0) {
281 AddPaddingEpilogCode =
true;
286 if ((
size_t)numCodes + numEpilogCodes > UINT8_MAX) {
288 "Too many unwind codes with Unwind v2 enabled in " +
289 info->Function->getName());
293 numCodes += numEpilogCodes;
298 if (
info->ChainedParent)
301 if (
info->HandlesUnwind)
303 if (
info->HandlesExceptions)
316 if (
info->LastFrameInst >= 0) {
324 if (EnableUnwindV2) {
326 OS->ensureHeadroom(
info->EpilogMap.size() * 2);
332 uint8_t Flags = LastEpilogIsAtEnd ? 0x01 : 0;
333 OS->emitInt8(EpilogSize);
336 if (LastEpilogIsAtEnd)
344 EpilogSize, context);
346 OS->appendContents(2, 0);
349 if (AddPaddingEpilogCode)
354 for (
uint8_t c = 0; c < numInst; ++c) {
356 info->Instructions.pop_back();
375 else if (numCodes == 0) {
389 Asm->getContext().reportError(
390 Loc,
"Failed to evaluate epilog offset for Unwind v2 in " +
395 constexpr uint16_t MaxEpilogOffset = 0x0fff;
396 if (*
Offset > MaxEpilogOffset) {
397 Asm->getContext().reportError(
405 if (
Size != (EpilogSize - 1)) {
406 Asm->getContext().reportError(
407 Loc,
"Size of this epilog does not match size of last epilog in " +
412 auto HighBits = *
Offset >> 8;
436 bool HandlerData)
const {
466 std::optional<int64_t> MaybeDiff =
479 std::optional<int64_t> MaybeDistance =
485 for (
const auto &
I : Insns) {
501 if (Distance != InstructionBytes) {
505 " bytes of instructions in range, but .seh directives "
506 "corresponding to " +
507 Twine(InstructionBytes) +
" bytes\n");
513 for (
const auto &
I : Insns) {
628 b = (inst.
Offset >> 4) & 0x1F;
645 b = (w & 0x00FF0000) >> 16;
647 b = (w & 0x0000FF00) >> 8;
669 b |= (inst.
Offset >> 3) & 0x1F;
674 b |= ((inst.
Offset - 1) >> 3) & 0x3F;
679 b |= (inst.
Offset >> 3) & 0x3F;
685 b = 0xD0 | ((reg & 0xC) >> 2);
687 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
693 b = 0xD4 | ((reg & 0x8) >> 3);
695 b = ((reg & 0x7) << 5) | ((inst.
Offset >> 3) - 1);
701 b = 0xC8 | ((reg & 0xC) >> 2);
703 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
709 b = 0xCC | ((reg & 0xC) >> 2);
711 b = ((reg & 0x3) << 6) | ((inst.
Offset >> 3) - 1);
717 assert((reg % 2) == 0 &&
"Saved reg must be 19+2*X");
719 b = 0xD6 | ((reg & 0x7) >> 2);
721 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
727 b = 0xDC | ((reg & 0x4) >> 2);
729 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
737 b = ((reg & 0x7) << 5) | ((inst.
Offset >> 3) - 1);
743 b = 0xD8 | ((reg & 0x4) >> 2);
745 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
751 b = 0xDA | ((reg & 0x4) >> 2);
753 b = ((reg & 0x3) << 6) | ((inst.
Offset >> 3) - 1);
802 int Writeback =
Op / 6;
804 int Mode = (
Op / 2) % 3;
806 if (Writeback || Paired || Mode == 2)
813 b = inst.
Register | (Writeback << 5) | (Paired << 6);
832 b = ((inst.
Offset & 0xC0) >> 1) | reg;
834 b = 0xC0 | (inst.
Offset & 0x3F);
844 b = ((inst.
Offset & 0xC0) >> 1) | 0x10 | reg;
846 b = 0xC0 | (inst.
Offset & 0x3F);
859 const std::vector<MCSymbol *>& Epilogs,
861 for (
auto *EpilogStart : Epilogs) {
862 auto InstrsIter =
info->EpilogMap.find(EpilogStart);
864 "Epilog not found in EpilogMap");
865 const auto &Instrs = InstrsIter->second.Instructions;
867 if (Instrs.size() != EpilogInstrs.size())
871 for (
unsigned i = 0; i < Instrs.size(); ++i)
872 if (Instrs[i] != EpilogInstrs[i]) {
885 unsigned PrevOffset = -1;
886 unsigned PrevRegister = -1;
894 Inst.Register == 29) {
898 Inst.Register == 19 && Inst.Offset <= 248) {
904 Inst.Register == PrevRegister + 2 &&
905 Inst.Offset == PrevOffset + 16) {
920 PrevRegister = Inst.Register;
922 PrevOffset = Inst.Offset;
923 PrevRegister = Inst.Register;
937 for (
auto It = Instructions.rbegin(); It != Instructions.rend(); It++)
938 VisitInstruction(*It);
941 VisitInstruction(Inst);
948 const std::vector<WinEH::Instruction> &
Epilog) {
955 for (
int I =
Epilog.size() - 1;
I >= 0;
I--) {
970 int PrologCodeBytes) {
976 const std::vector<WinEH::Instruction> &
Epilog =
977 info->EpilogMap[
Sym].Instructions;
983 if (DistanceFromEnd / 4 !=
Epilog.size())
991 if (PrologCodeBytes <= 31 &&
993 RetVal = PrologCodeBytes;
1002 if (
Offset > 31 || PrologCodeBytes > 124)
1012 int PackedEpilogOffset) {
1013 if (PackedEpilogOffset == 0) {
1019 }
else if (PackedEpilogOffset == 1) {
1029 unsigned RegI = 0, RegF = 0;
1030 int Predecrement = 0;
1042 bool StandaloneLR =
false, FPLRPair =
false;
1055 if (Location != Start)
1060 if (Location != Start2)
1066 if (Location != Start2 && Location != Start3)
1068 Predecrement = Inst.
Offset;
1073 if (Location != Start2 && Location != Start3)
1075 Predecrement = Inst.
Offset;
1079 StandaloneLR =
true;
1083 Location = FloatRegs;
1110 StandaloneLR =
true;
1114 Location = FloatRegs;
1121 StandaloneLR =
true;
1122 Location = FloatRegs;
1129 if (Location != FloatRegs || RegF == 0 || Inst.
Register != 8 + RegF ||
1130 Inst.
Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
1133 Location = InputArgs;
1136 if ((Location != Start2 && Location != Start3) || Inst.
Register != 8)
1138 Predecrement = Inst.
Offset;
1140 Location = FloatRegs;
1143 if ((Location !=
IntRegs && Location != FloatRegs) ||
1145 Inst.
Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
1148 Location = FloatRegs;
1153 else if (Location == FloatRegs)
1159 if (Location !=
IntRegs && Location != FloatRegs && Location != InputArgs)
1161 Location = InputArgs;
1166 if (Location != Start2 && Location != Start3 && Location !=
IntRegs &&
1167 Location != FloatRegs && Location != InputArgs &&
1168 Location != StackAdjust)
1178 Location = StackAdjust;
1183 if (Location != Start2 && Location != Start3 && Location !=
IntRegs &&
1184 Location != FloatRegs && Location != InputArgs)
1187 Location = FrameRecord;
1192 if (Location != StackAdjust || Inst.
Offset != 0)
1194 Location = FrameRecord;
1198 if (Location != FrameRecord)
1242 if (RegI > 10 || RegF > 8)
1244 if (StandaloneLR && FPLRPair)
1246 if (FPLRPair && Location !=
End)
1248 if (Nops != 0 && Nops != 4)
1250 if (PAC && !FPLRPair)
1263 int IntSZ = 8 * RegI;
1266 int FpSZ = 8 * RegF;
1267 int SavSZ = (IntSZ + FpSZ + 8 * 8 *
H + 0xF) & ~0xF;
1268 if (Predecrement != SavSZ)
1275 if (FrameSize > 0x1FF)
1277 assert(RegF != 1 &&
"One single float reg not allowed");
1280 assert(FuncLength <= 0x7FF &&
"FuncLength should have been checked earlier");
1282 int CR = PAC ? 2 : FPLRPair ? 3 : StandaloneLR ? 1 : 0;
1283 info->PackedInfo |= Flag << 0;
1284 info->PackedInfo |= (FuncLength & 0x7FF) << 2;
1285 info->PackedInfo |= (RegF & 0x7) << 13;
1286 info->PackedInfo |= (RegI & 0xF) << 16;
1287 info->PackedInfo |= (
H & 0x1) << 20;
1288 info->PackedInfo |= (CR & 0x3) << 21;
1289 info->PackedInfo |= (FrameSize & 0x1FF) << 23;
1298 std::vector<MCSymbol *> EpilogStarts;
1300 EpilogStarts.push_back(
I.first);
1303 std::vector<MCSymbol *> AddedEpilogs;
1304 for (
auto *S : EpilogStarts) {
1306 auto &EpilogInstrs =
info->EpilogMap[S].Instructions;
1312 if (MatchingEpilog) {
1314 "Duplicate epilog not found");
1315 EpilogInfo[EpilogStart] = EpilogInfo.
lookup(MatchingEpilog);
1318 EpilogInstrs.clear();
1320 EpilogInstrs)) >= 0) {
1321 EpilogInfo[EpilogStart] = PrologOffset;
1325 EpilogInfo[EpilogStart] += 1;
1328 EpilogInstrs.
clear();
1330 EpilogInfo[EpilogStart] = TotalCodeBytes;
1331 TotalCodeBytes += CodeBytes;
1332 AddedEpilogs.push_back(EpilogStart);
1339 int64_t RawFuncLength) {
1340 if (
info->PrologEnd)
1342 info->PrologEnd,
info->Function->getName(),
1344 struct EpilogStartEnd {
1351 for (
auto &
I :
info->EpilogMap) {
1353 auto &Instrs =
I.second.Instructions;
1356 info->Function->getName(),
"epilogue");
1358 "Epilogs should be monotonically ordered");
1365 int64_t SegLimit = 0xFFFFC;
1366 int64_t SegOffset = 0;
1368 if (RawFuncLength > SegLimit) {
1370 int64_t RemainingLength = RawFuncLength;
1372 while (RemainingLength > SegLimit) {
1376 int64_t SegLength = SegLimit;
1377 int64_t SegEnd = SegOffset + SegLength;
1381 while (E < Epilogs.
size() && Epilogs[E].End < SegEnd) {
1383 EpilogsInSegment[Epilogs[E].Start] = Epilogs[E].Offset;
1394 if (E < Epilogs.
size() && Epilogs[E].Offset <= SegEnd)
1396 SegLength = Epilogs[E].
Offset - SegOffset;
1399 SegOffset, SegLength, !SegOffset);
1400 Seg.Epilogs = std::move(EpilogsInSegment);
1401 info->Segments.push_back(Seg);
1403 SegOffset += SegLength;
1404 RemainingLength -= SegLength;
1413 for (; E < Epilogs.
size(); ++E)
1414 LastSeg.Epilogs[Epilogs[E].Start] = Epilogs[E].Offset;
1415 info->Segments.push_back(LastSeg);
1421 bool TryPacked =
true) {
1430 info->Symbol = Label;
1433 bool HasEpilogs = (Seg.
Epilogs.size() != 0);
1438 int PackedEpilogOffset = HasEpilogs ?
1445 if (
info->Segments.size() == 1 && PackedEpilogOffset >= 0 &&
1446 uint32_t(PackedEpilogOffset) < PrologCodeBytes &&
1447 !
info->HandlesExceptions && SegLength <= 0x7ff && TryPacked) {
1462 PrologCodeBytes += 1;
1463 if (PackedEpilogOffset >= 0)
1464 PackedEpilogOffset += 1;
1472 PackedEpilogOffset = 0;
1475 uint32_t TotalCodeBytes = PrologCodeBytes;
1483 uint32_t CodeWords = TotalCodeBytes / 4;
1484 uint32_t CodeWordsMod = TotalCodeBytes % 4;
1488 PackedEpilogOffset >= 0 ? PackedEpilogOffset : Seg.
Epilogs.size();
1489 bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124;
1490 if (!ExtensionWord) {
1491 row1 |= (EpilogCount & 0x1F) << 22;
1492 row1 |= (CodeWords & 0x1F) << 27;
1494 if (
info->HandlesExceptions)
1496 if (PackedEpilogOffset >= 0)
1498 row1 |= SegLength & 0x3FFFF;
1502 if (ExtensionWord) {
1504 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
1506 "SEH unwind data splitting is only implemented for large functions, "
1507 "cases of too many code words or too many epilogs will be done "
1510 row2 |= (CodeWords & 0xFF) << 16;
1511 row2 |= (EpilogCount & 0xFFFF);
1515 if (PackedEpilogOffset < 0) {
1517 for (
auto &
I : EpilogInfo) {
1525 row3 |= (EpilogIndex & 0x3FF) << 22;
1546 auto &EpilogInstrs =
info->EpilogMap[
I.first].Instructions;
1551 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
1553 for (
int i = 0; i < BytesMod; i++)
1556 if (
info->HandlesExceptions)
1566 bool TryPacked =
true) {
1574 if (
info->empty()) {
1575 info->EmitAttempted =
true;
1578 if (
info->EmitAttempted) {
1585 SMLoc(),
"Earlier .seh_handlerdata for " +
info->Function->getName() +
1586 " skipped due to no unwind info at the time "
1587 "(.seh_handlerdata too early?), but the function later "
1588 "did get unwind info that can't be emitted");
1593 for (
auto &
I :
info->EpilogMap)
1596 int64_t RawFuncLength;
1597 if (!
info->FuncletOrFuncEnd) {
1636 for (
auto &S :
info->Segments)
1640 info->Instructions.clear();
1645 for (
const auto &
I : Insns) {
1703 for (J = 3; J > 0; J--)
1704 if (
I.Offset & (0xffu << (8 * J)))
1715 bool *HasCustom =
nullptr) {
1717 for (
const auto &
I : Insns) {
1779 std::optional<int64_t> MaybeDistance =
1784 bool HasCustom =
false;
1788 if (Distance != InstructionBytes) {
1792 " bytes of instructions in range, but .seh directives "
1793 "corresponding to " +
1794 Twine(InstructionBytes) +
" bytes\n");
1826 w = 0x8000 | (inst.
Register & 0x1fff) | (lr << 13);
1827 streamer.
emitInt8((w >> 8) & 0xff);
1828 streamer.
emitInt8((w >> 0) & 0xff);
1851 w = 0xe800 | (inst.
Offset / 4);
1852 streamer.
emitInt8((w >> 8) & 0xff);
1853 streamer.
emitInt8((w >> 0) & 0xff);
1858 w = 0xec00 | (inst.
Register & 0x0ff) | (lr << 8);
1859 streamer.
emitInt8((w >> 8) & 0xff);
1860 streamer.
emitInt8((w >> 0) & 0xff);
1887 streamer.
emitInt8((w >> 8) & 0xff);
1888 streamer.
emitInt8((w >> 0) & 0xff);
1895 streamer.
emitInt8((w >> 16) & 0xff);
1896 streamer.
emitInt8((w >> 8) & 0xff);
1897 streamer.
emitInt8((w >> 0) & 0xff);
1904 streamer.
emitInt8((w >> 8) & 0xff);
1905 streamer.
emitInt8((w >> 0) & 0xff);
1912 streamer.
emitInt8((w >> 16) & 0xff);
1913 streamer.
emitInt8((w >> 8) & 0xff);
1914 streamer.
emitInt8((w >> 0) & 0xff);
1932 for (i = 3; i > 0; i--)
1933 if (inst.
Offset & (0xffu << (8 * i)))
1948 const std::vector<WinEH::Instruction> &
Epilog,
1949 bool CanTweakProlog) {
1958 int EndIdx = CanTweakProlog ? 1 : 0;
1959 for (
int I =
Epilog.size() - 1;
I >= EndIdx;
I--) {
1966 if (CanTweakProlog) {
1984 int PrologCodeBytes) {
1986 if (
info->EpilogMap.size() != 1)
1996 if (
info->Instructions.empty() ||
Epilog.empty())
2002 streamer,
info->FuncletOrFuncEnd,
info->EpilogMap.begin()->first);
2007 if (DistanceFromEnd != InstructionBytes)
2015 if (PrologCodeBytes <= 31 &&
2017 RetVal = PrologCodeBytes;
2027 if (
Offset > 31 || PrologCodeBytes > 63)
2036 info->EpilogMap.clear();
2041 unsigned &Folded,
int &
IntRegs) {
2042 if (Mask & (1 << 14)) {
2046 if (Mask & (1 << 11)) {
2056 while ((Mask & 1) == 0) {
2060 if ((Mask & (Mask + 1)) != 0)
2064 while (Mask & (1 <<
N))
2083 bool Homing =
false;
2084 bool HasR11 =
false;
2085 bool HasChain =
false;
2090 unsigned StackAdjust = 0;
2125 if (Step != 1 && Step != 2)
2140 if (Step == 1 && Inst.
Register == 0x0f) {
2148 if (Step != 1 && Step != 2)
2160 if (Step != 3 || !HasR11 ||
IntRegs >= 0 || PF > 0)
2167 if (Step != 3 || !HasR11 || (
IntRegs < 0 && PF == 0))
2174 if (Step != 1 && Step != 2 && Step != 3 && Step != 4)
2187 if (Step != 1 && Step != 2 && Step != 3 && Step != 4 && Step != 5)
2191 if (Inst.
Offset / 4 >= 0x3f4)
2193 StackAdjust = Inst.
Offset / 4;
2198 if (HasR11 && !HasChain) {
2207 if (HasChain && !HasLR)
2211 if (
info->EpilogMap.size() > 1)
2216 if (
info->EpilogMap.size() == 0) {
2223 info->EpilogMap.begin()->second;
2228 streamer,
info->FuncletOrFuncEnd,
info->EpilogMap.begin()->first);
2233 if (DistanceFromEnd != InstructionBytes)
2236 bool GotStackAdjust =
false;
2237 bool GotFloatRegs =
false;
2238 bool GotIntRegs =
false;
2239 bool GotHomingRestore =
false;
2240 bool GotLRRestore =
false;
2241 bool NeedsReturn =
false;
2242 bool GotReturn =
false;
2264 if (Inst.
Offset / 4 >= 0x3f4)
2267 if (Homing && FloatRegs < 0 &&
IntRegs < 0 && StackAdjust == 0 &&
2268 PF == 0 && Inst.
Offset == 16) {
2269 GotHomingRestore =
true;
2272 if (StackAdjust > 0) {
2274 if (StackAdjust != Inst.
Offset / 4)
2276 GotStackAdjust =
true;
2277 }
else if (PF == Inst.
Offset / 4) {
2279 StackAdjust = Inst.
Offset / 4;
2280 GotStackAdjust =
true;
2287 }
else if (Step == 7 || Step == 8 || Step == 9) {
2288 if (!Homing || Inst.
Offset != 16)
2290 GotHomingRestore =
true;
2297 if (Step != 6 && Step != 7)
2300 if (FloatRegs != (
int)(Inst.
Register - 8))
2302 GotFloatRegs =
true;
2309 if (Step != 6 && Step != 7 && Step != 8)
2313 if (Homing && HasLR) {
2317 GotLRRestore =
true;
2323 if (HasLR != (Inst.
Offset == 1))
2326 GotLRRestore = Inst.
Offset == 1;
2345 if (Step != 6 && Step != 7 && Step != 8)
2350 bool CurHasLR =
false, CurHasR11 =
false;
2355 if (EF != PF && EF != StackAdjust)
2358 if (Homing && HasLR) {
2362 GotLRRestore =
true;
2368 if (CurHasLR != HasLR)
2370 GotLRRestore = CurHasLR;
2397 if (Step != 6 && Step != 7 && Step != 8 && Step != 9)
2399 if (!Homing || Inst.
Offset != 20 || GotLRRestore)
2401 GotLRRestore =
true;
2402 GotHomingRestore =
true;
2412 if (Step != 6 && Step != 7 && Step != 8 && Step != 9 && Step != 10)
2421 if (StackAdjust > 0 && !GotStackAdjust && EF == 0)
2423 if (FloatRegs >= 0 && !GotFloatRegs)
2425 if (
IntRegs >= 0 && !GotIntRegs)
2427 if (Homing && !GotHomingRestore)
2429 if (HasLR && !GotLRRestore)
2431 if (NeedsReturn && !GotReturn)
2435 assert(PF == 0 || EF == 0 ||
2437 if (PF > 0 || EF > 0) {
2438 StackAdjust = PF > 0 ? (PF - 1) : (EF - 1);
2439 assert(StackAdjust <= 3);
2440 StackAdjust |= 0x3f0;
2442 StackAdjust |= 1 << 2;
2444 StackAdjust |= 1 << 3;
2447 assert(FuncLength <= 0x7FF &&
"FuncLength should have been checked earlier");
2448 int Flag =
info->Fragment ? 0x02 : 0x01;
2449 int H = Homing ? 1 : 0;
2450 int L = HasLR ? 1 : 0;
2451 int C = HasChain ? 1 : 0;
2458 }
else if (FloatRegs >= 0) {
2467 info->PackedInfo |= Flag << 0;
2468 info->PackedInfo |= (FuncLength & 0x7FF) << 2;
2469 info->PackedInfo |= (Ret & 0x3) << 13;
2470 info->PackedInfo |=
H << 15;
2471 info->PackedInfo |= Reg << 16;
2472 info->PackedInfo |= R << 19;
2473 info->PackedInfo |= L << 20;
2474 info->PackedInfo |=
C << 21;
2475 assert(StackAdjust <= 0x3ff);
2476 info->PackedInfo |= StackAdjust << 22;
2483 bool TryPacked =
true) {
2491 if (
info->empty()) {
2492 info->EmitAttempted =
true;
2495 if (
info->EmitAttempted) {
2502 SMLoc(),
"Earlier .seh_handlerdata for " +
info->Function->getName() +
2503 " skipped due to no unwind info at the time "
2504 "(.seh_handlerdata too early?), but the function later "
2505 "did get unwind info that can't be emitted");
2514 info->Symbol = Label;
2516 if (!
info->PrologEnd)
2518 info->Function->getName() +
2519 " not correctly terminated");
2521 if (
info->PrologEnd && !
info->Fragment)
2523 info->PrologEnd,
info->Function->getName(),
2525 for (
auto &
I :
info->EpilogMap) {
2529 info->Function->getName(),
"epilogue");
2530 if (
Epilog.Instructions.empty() ||
2533 SMLoc(),
"Epilogue in " +
info->Function->getName() +
2534 " not correctly terminated");
2537 std::optional<int64_t> RawFuncLength;
2538 const MCExpr *FuncLengthExpr =
nullptr;
2539 if (!
info->FuncletOrFuncEnd) {
2558 FuncLength = (
uint32_t)*RawFuncLength / 2;
2559 if (FuncLength > 0x3FFFF)
2562 uint32_t TotalCodeBytes = PrologCodeBytes;
2564 if (!
info->HandlesExceptions && RawFuncLength && FuncLength <= 0x7ff &&
2578 int PackedEpilogOffset =
2584 std::vector<MCSymbol *> AddedEpilogs;
2586 bool CanTweakProlog =
true;
2587 for (
auto &
I :
info->EpilogMap) {
2589 auto &EpilogInstrs =
I.second.Instructions;
2595 if (MatchingEpilog) {
2597 "Duplicate epilog not found");
2598 EpilogInfo[EpilogStart] = EpilogInfo.
lookup(MatchingEpilog);
2601 EpilogInstrs.clear();
2603 info->Instructions, EpilogInstrs, CanTweakProlog)) >= 0) {
2604 if (CanTweakProlog) {
2607 info->Instructions.front() = EpilogInstrs.back();
2609 CanTweakProlog =
false;
2611 EpilogInfo[EpilogStart] = PrologOffset;
2614 EpilogInstrs.
clear();
2616 EpilogInfo[EpilogStart] = TotalCodeBytes;
2617 TotalCodeBytes += CodeBytes;
2618 AddedEpilogs.push_back(EpilogStart);
2624 uint32_t CodeWords = TotalCodeBytes / 4;
2625 uint32_t CodeWordsMod = TotalCodeBytes % 4;
2629 PackedEpilogOffset >= 0 ? PackedEpilogOffset :
info->EpilogMap.size();
2630 bool ExtensionWord = EpilogCount > 31 || CodeWords > 15;
2631 if (!ExtensionWord) {
2632 row1 |= (EpilogCount & 0x1F) << 23;
2633 row1 |= (CodeWords & 0x0F) << 28;
2635 if (
info->HandlesExceptions)
2637 if (PackedEpilogOffset >= 0)
2641 row1 |= FuncLength & 0x3FFFF;
2651 if (ExtensionWord) {
2653 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
2656 row2 |= (CodeWords & 0xFF) << 16;
2657 row2 |= (EpilogCount & 0xFFFF);
2661 if (PackedEpilogOffset < 0) {
2663 for (
auto &
I : EpilogInfo) {
2667 std::optional<int64_t> MaybeEpilogOffset =
2669 const MCExpr *OffsetExpr =
nullptr;
2671 if (MaybeEpilogOffset)
2672 EpilogOffset = *MaybeEpilogOffset / 2;
2676 assert(
info->EpilogMap.contains(EpilogStart));
2677 unsigned Condition =
info->EpilogMap[EpilogStart].Condition;
2678 assert(Condition <= 0xf);
2681 row3 |= Condition << 20;
2682 row3 |= (EpilogIndex & 0x3FF) << 24;
2683 if (MaybeEpilogOffset)
2695 for (
uint8_t c = 0; c < numInst; ++c) {
2697 info->Instructions.pop_back();
2702 for (
auto &
I :
info->EpilogMap) {
2703 auto &EpilogInstrs =
I.second.Instructions;
2708 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
2710 for (
int i = 0; i < BytesMod; i++)
2713 if (
info->HandlesExceptions)
2725 for (
const auto &S :
info->Segments) {
2727 if (
info->PackedInfo)
2744 if (
info->PackedInfo)
2780 bool HandlerData)
const {
2787 if (!
info->FuncletOrFuncEnd) {
2825 bool HandlerData)
const {
2832 if (!
info->FuncletOrFuncEnd) {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
Given that RA is a live value
std::optional< std::vector< StOtherPiece > > Other
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 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.
This class represents an Operation in the Expression.
Tagged union holding either a T or a Error.
This class is intended to be used as a base class for asm properties and features specific to the tar...
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.
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
Extension point for target-specific MCExpr subclasses to implement.
MCExpr that represents the epilog unwind code in an unwind table.
MCFragment * findAssociatedFragment() const override
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm) const override
static MCUnwindV2EpilogTargetExpr * create(const WinEH::FrameInfo &FrameInfo, const WinEH::FrameInfo::Epilog &Epilog, uint8_t EpilogSize_, MCContext &Ctx)
void visitUsedExpr(MCStreamer &Streamer) const override
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.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
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
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
UnwindOpcodes
UnwindOpcodes - Enumeration whose values specify a single operation in the prolog of a function.
@ UOP_WideSaveRegsR4R11LR
@ 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.
This is an optimization pass for GlobalISel generic memory operations.
@ FK_Data_2
A two-byte fixup.
auto reverse(ContainerTy &&C)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
This struct is a compact representation of a valid (non-zero power of two) alignment.
std::vector< Instruction > Instructions
MapVector< MCSymbol *, int64_t > Epilogs
const MCSymbol * Function
const MCSymbol * FuncletOrFuncEnd