X86AsmBackend.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. //===-- X86AsmBackend.cpp - X86 Assembler Backend -------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "MCTargetDesc/X86BaseInfo.h"
  9. #include "MCTargetDesc/X86FixupKinds.h"
  10. #include "llvm/ADT/StringSwitch.h"
  11. #include "llvm/BinaryFormat/ELF.h"
  12. #include "llvm/BinaryFormat/MachO.h"
  13. #include "llvm/MC/MCAsmBackend.h"
  14. #include "llvm/MC/MCAsmLayout.h"
  15. #include "llvm/MC/MCAssembler.h"
  16. #include "llvm/MC/MCCodeEmitter.h"
  17. #include "llvm/MC/MCContext.h"
  18. #include "llvm/MC/MCDwarf.h"
  19. #include "llvm/MC/MCELFObjectWriter.h"
  20. #include "llvm/MC/MCExpr.h"
  21. #include "llvm/MC/MCFixupKindInfo.h"
  22. #include "llvm/MC/MCInst.h"
  23. #include "llvm/MC/MCInstrInfo.h"
  24. #include "llvm/MC/MCMachObjectWriter.h"
  25. #include "llvm/MC/MCObjectStreamer.h"
  26. #include "llvm/MC/MCObjectWriter.h"
  27. #include "llvm/MC/MCRegisterInfo.h"
  28. #include "llvm/MC/MCSectionMachO.h"
  29. #include "llvm/MC/MCSubtargetInfo.h"
  30. #include "llvm/MC/MCValue.h"
  31. #include "llvm/MC/TargetRegistry.h"
  32. #include "llvm/Support/CommandLine.h"
  33. #include "llvm/Support/ErrorHandling.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. using namespace llvm;
  36. namespace {
  37. /// A wrapper for holding a mask of the values from X86::AlignBranchBoundaryKind
  38. class X86AlignBranchKind {
  39. private:
  40. uint8_t AlignBranchKind = 0;
  41. public:
  42. void operator=(const std::string &Val) {
  43. if (Val.empty())
  44. return;
  45. SmallVector<StringRef, 6> BranchTypes;
  46. StringRef(Val).split(BranchTypes, '+', -1, false);
  47. for (auto BranchType : BranchTypes) {
  48. if (BranchType == "fused")
  49. addKind(X86::AlignBranchFused);
  50. else if (BranchType == "jcc")
  51. addKind(X86::AlignBranchJcc);
  52. else if (BranchType == "jmp")
  53. addKind(X86::AlignBranchJmp);
  54. else if (BranchType == "call")
  55. addKind(X86::AlignBranchCall);
  56. else if (BranchType == "ret")
  57. addKind(X86::AlignBranchRet);
  58. else if (BranchType == "indirect")
  59. addKind(X86::AlignBranchIndirect);
  60. else {
  61. errs() << "invalid argument " << BranchType.str()
  62. << " to -x86-align-branch=; each element must be one of: fused, "
  63. "jcc, jmp, call, ret, indirect.(plus separated)\n";
  64. }
  65. }
  66. }
  67. operator uint8_t() const { return AlignBranchKind; }
  68. void addKind(X86::AlignBranchBoundaryKind Value) { AlignBranchKind |= Value; }
  69. };
  70. X86AlignBranchKind X86AlignBranchKindLoc;
  71. cl::opt<unsigned> X86AlignBranchBoundary(
  72. "x86-align-branch-boundary", cl::init(0),
  73. cl::desc(
  74. "Control how the assembler should align branches with NOP. If the "
  75. "boundary's size is not 0, it should be a power of 2 and no less "
  76. "than 32. Branches will be aligned to prevent from being across or "
  77. "against the boundary of specified size. The default value 0 does not "
  78. "align branches."));
  79. cl::opt<X86AlignBranchKind, true, cl::parser<std::string>> X86AlignBranch(
  80. "x86-align-branch",
  81. cl::desc(
  82. "Specify types of branches to align (plus separated list of types):"
  83. "\njcc indicates conditional jumps"
  84. "\nfused indicates fused conditional jumps"
  85. "\njmp indicates direct unconditional jumps"
  86. "\ncall indicates direct and indirect calls"
  87. "\nret indicates rets"
  88. "\nindirect indicates indirect unconditional jumps"),
  89. cl::location(X86AlignBranchKindLoc));
  90. cl::opt<bool> X86AlignBranchWithin32BBoundaries(
  91. "x86-branches-within-32B-boundaries", cl::init(false),
  92. cl::desc(
  93. "Align selected instructions to mitigate negative performance impact "
  94. "of Intel's micro code update for errata skx102. May break "
  95. "assumptions about labels corresponding to particular instructions, "
  96. "and should be used with caution."));
  97. cl::opt<unsigned> X86PadMaxPrefixSize(
  98. "x86-pad-max-prefix-size", cl::init(0),
  99. cl::desc("Maximum number of prefixes to use for padding"));
  100. cl::opt<bool> X86PadForAlign(
  101. "x86-pad-for-align", cl::init(false), cl::Hidden,
  102. cl::desc("Pad previous instructions to implement align directives"));
  103. cl::opt<bool> X86PadForBranchAlign(
  104. "x86-pad-for-branch-align", cl::init(true), cl::Hidden,
  105. cl::desc("Pad previous instructions to implement branch alignment"));
  106. class X86AsmBackend : public MCAsmBackend {
  107. const MCSubtargetInfo &STI;
  108. std::unique_ptr<const MCInstrInfo> MCII;
  109. X86AlignBranchKind AlignBranchType;
  110. Align AlignBoundary;
  111. unsigned TargetPrefixMax = 0;
  112. MCInst PrevInst;
  113. MCBoundaryAlignFragment *PendingBA = nullptr;
  114. std::pair<MCFragment *, size_t> PrevInstPosition;
  115. bool CanPadInst;
  116. uint8_t determinePaddingPrefix(const MCInst &Inst) const;
  117. bool isMacroFused(const MCInst &Cmp, const MCInst &Jcc) const;
  118. bool needAlign(const MCInst &Inst) const;
  119. bool canPadBranches(MCObjectStreamer &OS) const;
  120. bool canPadInst(const MCInst &Inst, MCObjectStreamer &OS) const;
  121. public:
  122. X86AsmBackend(const Target &T, const MCSubtargetInfo &STI)
  123. : MCAsmBackend(support::little), STI(STI),
  124. MCII(T.createMCInstrInfo()) {
  125. if (X86AlignBranchWithin32BBoundaries) {
  126. // At the moment, this defaults to aligning fused branches, unconditional
  127. // jumps, and (unfused) conditional jumps with nops. Both the
  128. // instructions aligned and the alignment method (nop vs prefix) may
  129. // change in the future.
  130. AlignBoundary = assumeAligned(32);;
  131. AlignBranchType.addKind(X86::AlignBranchFused);
  132. AlignBranchType.addKind(X86::AlignBranchJcc);
  133. AlignBranchType.addKind(X86::AlignBranchJmp);
  134. }
  135. // Allow overriding defaults set by main flag
  136. if (X86AlignBranchBoundary.getNumOccurrences())
  137. AlignBoundary = assumeAligned(X86AlignBranchBoundary);
  138. if (X86AlignBranch.getNumOccurrences())
  139. AlignBranchType = X86AlignBranchKindLoc;
  140. if (X86PadMaxPrefixSize.getNumOccurrences())
  141. TargetPrefixMax = X86PadMaxPrefixSize;
  142. }
  143. bool allowAutoPadding() const override;
  144. bool allowEnhancedRelaxation() const override;
  145. void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst,
  146. const MCSubtargetInfo &STI) override;
  147. void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst) override;
  148. unsigned getNumFixupKinds() const override {
  149. return X86::NumTargetFixupKinds;
  150. }
  151. Optional<MCFixupKind> getFixupKind(StringRef Name) const override;
  152. const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
  153. bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
  154. const MCValue &Target) override;
  155. void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
  156. const MCValue &Target, MutableArrayRef<char> Data,
  157. uint64_t Value, bool IsResolved,
  158. const MCSubtargetInfo *STI) const override;
  159. bool mayNeedRelaxation(const MCInst &Inst,
  160. const MCSubtargetInfo &STI) const override;
  161. bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
  162. const MCRelaxableFragment *DF,
  163. const MCAsmLayout &Layout) const override;
  164. void relaxInstruction(MCInst &Inst,
  165. const MCSubtargetInfo &STI) const override;
  166. bool padInstructionViaRelaxation(MCRelaxableFragment &RF,
  167. MCCodeEmitter &Emitter,
  168. unsigned &RemainingSize) const;
  169. bool padInstructionViaPrefix(MCRelaxableFragment &RF, MCCodeEmitter &Emitter,
  170. unsigned &RemainingSize) const;
  171. bool padInstructionEncoding(MCRelaxableFragment &RF, MCCodeEmitter &Emitter,
  172. unsigned &RemainingSize) const;
  173. void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const override;
  174. unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const override;
  175. bool writeNopData(raw_ostream &OS, uint64_t Count,
  176. const MCSubtargetInfo *STI) const override;
  177. };
  178. } // end anonymous namespace
  179. static unsigned getRelaxedOpcodeBranch(const MCInst &Inst, bool Is16BitMode) {
  180. unsigned Op = Inst.getOpcode();
  181. switch (Op) {
  182. default:
  183. return Op;
  184. case X86::JCC_1:
  185. return (Is16BitMode) ? X86::JCC_2 : X86::JCC_4;
  186. case X86::JMP_1:
  187. return (Is16BitMode) ? X86::JMP_2 : X86::JMP_4;
  188. }
  189. }
  190. static unsigned getRelaxedOpcodeArith(const MCInst &Inst) {
  191. unsigned Op = Inst.getOpcode();
  192. switch (Op) {
  193. default:
  194. return Op;
  195. // IMUL
  196. case X86::IMUL16rri8: return X86::IMUL16rri;
  197. case X86::IMUL16rmi8: return X86::IMUL16rmi;
  198. case X86::IMUL32rri8: return X86::IMUL32rri;
  199. case X86::IMUL32rmi8: return X86::IMUL32rmi;
  200. case X86::IMUL64rri8: return X86::IMUL64rri32;
  201. case X86::IMUL64rmi8: return X86::IMUL64rmi32;
  202. // AND
  203. case X86::AND16ri8: return X86::AND16ri;
  204. case X86::AND16mi8: return X86::AND16mi;
  205. case X86::AND32ri8: return X86::AND32ri;
  206. case X86::AND32mi8: return X86::AND32mi;
  207. case X86::AND64ri8: return X86::AND64ri32;
  208. case X86::AND64mi8: return X86::AND64mi32;
  209. // OR
  210. case X86::OR16ri8: return X86::OR16ri;
  211. case X86::OR16mi8: return X86::OR16mi;
  212. case X86::OR32ri8: return X86::OR32ri;
  213. case X86::OR32mi8: return X86::OR32mi;
  214. case X86::OR64ri8: return X86::OR64ri32;
  215. case X86::OR64mi8: return X86::OR64mi32;
  216. // XOR
  217. case X86::XOR16ri8: return X86::XOR16ri;
  218. case X86::XOR16mi8: return X86::XOR16mi;
  219. case X86::XOR32ri8: return X86::XOR32ri;
  220. case X86::XOR32mi8: return X86::XOR32mi;
  221. case X86::XOR64ri8: return X86::XOR64ri32;
  222. case X86::XOR64mi8: return X86::XOR64mi32;
  223. // ADD
  224. case X86::ADD16ri8: return X86::ADD16ri;
  225. case X86::ADD16mi8: return X86::ADD16mi;
  226. case X86::ADD32ri8: return X86::ADD32ri;
  227. case X86::ADD32mi8: return X86::ADD32mi;
  228. case X86::ADD64ri8: return X86::ADD64ri32;
  229. case X86::ADD64mi8: return X86::ADD64mi32;
  230. // ADC
  231. case X86::ADC16ri8: return X86::ADC16ri;
  232. case X86::ADC16mi8: return X86::ADC16mi;
  233. case X86::ADC32ri8: return X86::ADC32ri;
  234. case X86::ADC32mi8: return X86::ADC32mi;
  235. case X86::ADC64ri8: return X86::ADC64ri32;
  236. case X86::ADC64mi8: return X86::ADC64mi32;
  237. // SUB
  238. case X86::SUB16ri8: return X86::SUB16ri;
  239. case X86::SUB16mi8: return X86::SUB16mi;
  240. case X86::SUB32ri8: return X86::SUB32ri;
  241. case X86::SUB32mi8: return X86::SUB32mi;
  242. case X86::SUB64ri8: return X86::SUB64ri32;
  243. case X86::SUB64mi8: return X86::SUB64mi32;
  244. // SBB
  245. case X86::SBB16ri8: return X86::SBB16ri;
  246. case X86::SBB16mi8: return X86::SBB16mi;
  247. case X86::SBB32ri8: return X86::SBB32ri;
  248. case X86::SBB32mi8: return X86::SBB32mi;
  249. case X86::SBB64ri8: return X86::SBB64ri32;
  250. case X86::SBB64mi8: return X86::SBB64mi32;
  251. // CMP
  252. case X86::CMP16ri8: return X86::CMP16ri;
  253. case X86::CMP16mi8: return X86::CMP16mi;
  254. case X86::CMP32ri8: return X86::CMP32ri;
  255. case X86::CMP32mi8: return X86::CMP32mi;
  256. case X86::CMP64ri8: return X86::CMP64ri32;
  257. case X86::CMP64mi8: return X86::CMP64mi32;
  258. // PUSH
  259. case X86::PUSH32i8: return X86::PUSHi32;
  260. case X86::PUSH16i8: return X86::PUSHi16;
  261. case X86::PUSH64i8: return X86::PUSH64i32;
  262. }
  263. }
  264. static unsigned getRelaxedOpcode(const MCInst &Inst, bool Is16BitMode) {
  265. unsigned R = getRelaxedOpcodeArith(Inst);
  266. if (R != Inst.getOpcode())
  267. return R;
  268. return getRelaxedOpcodeBranch(Inst, Is16BitMode);
  269. }
  270. static X86::CondCode getCondFromBranch(const MCInst &MI,
  271. const MCInstrInfo &MCII) {
  272. unsigned Opcode = MI.getOpcode();
  273. switch (Opcode) {
  274. default:
  275. return X86::COND_INVALID;
  276. case X86::JCC_1: {
  277. const MCInstrDesc &Desc = MCII.get(Opcode);
  278. return static_cast<X86::CondCode>(
  279. MI.getOperand(Desc.getNumOperands() - 1).getImm());
  280. }
  281. }
  282. }
  283. static X86::SecondMacroFusionInstKind
  284. classifySecondInstInMacroFusion(const MCInst &MI, const MCInstrInfo &MCII) {
  285. X86::CondCode CC = getCondFromBranch(MI, MCII);
  286. return classifySecondCondCodeInMacroFusion(CC);
  287. }
  288. /// Check if the instruction uses RIP relative addressing.
  289. static bool isRIPRelative(const MCInst &MI, const MCInstrInfo &MCII) {
  290. unsigned Opcode = MI.getOpcode();
  291. const MCInstrDesc &Desc = MCII.get(Opcode);
  292. uint64_t TSFlags = Desc.TSFlags;
  293. unsigned CurOp = X86II::getOperandBias(Desc);
  294. int MemoryOperand = X86II::getMemoryOperandNo(TSFlags);
  295. if (MemoryOperand < 0)
  296. return false;
  297. unsigned BaseRegNum = MemoryOperand + CurOp + X86::AddrBaseReg;
  298. unsigned BaseReg = MI.getOperand(BaseRegNum).getReg();
  299. return (BaseReg == X86::RIP);
  300. }
  301. /// Check if the instruction is a prefix.
  302. static bool isPrefix(const MCInst &MI, const MCInstrInfo &MCII) {
  303. return X86II::isPrefix(MCII.get(MI.getOpcode()).TSFlags);
  304. }
  305. /// Check if the instruction is valid as the first instruction in macro fusion.
  306. static bool isFirstMacroFusibleInst(const MCInst &Inst,
  307. const MCInstrInfo &MCII) {
  308. // An Intel instruction with RIP relative addressing is not macro fusible.
  309. if (isRIPRelative(Inst, MCII))
  310. return false;
  311. X86::FirstMacroFusionInstKind FIK =
  312. X86::classifyFirstOpcodeInMacroFusion(Inst.getOpcode());
  313. return FIK != X86::FirstMacroFusionInstKind::Invalid;
  314. }
  315. /// X86 can reduce the bytes of NOP by padding instructions with prefixes to
  316. /// get a better peformance in some cases. Here, we determine which prefix is
  317. /// the most suitable.
  318. ///
  319. /// If the instruction has a segment override prefix, use the existing one.
  320. /// If the target is 64-bit, use the CS.
  321. /// If the target is 32-bit,
  322. /// - If the instruction has a ESP/EBP base register, use SS.
  323. /// - Otherwise use DS.
  324. uint8_t X86AsmBackend::determinePaddingPrefix(const MCInst &Inst) const {
  325. assert((STI.hasFeature(X86::Mode32Bit) || STI.hasFeature(X86::Mode64Bit)) &&
  326. "Prefixes can be added only in 32-bit or 64-bit mode.");
  327. const MCInstrDesc &Desc = MCII->get(Inst.getOpcode());
  328. uint64_t TSFlags = Desc.TSFlags;
  329. // Determine where the memory operand starts, if present.
  330. int MemoryOperand = X86II::getMemoryOperandNo(TSFlags);
  331. if (MemoryOperand != -1)
  332. MemoryOperand += X86II::getOperandBias(Desc);
  333. unsigned SegmentReg = 0;
  334. if (MemoryOperand >= 0) {
  335. // Check for explicit segment override on memory operand.
  336. SegmentReg = Inst.getOperand(MemoryOperand + X86::AddrSegmentReg).getReg();
  337. }
  338. switch (TSFlags & X86II::FormMask) {
  339. default:
  340. break;
  341. case X86II::RawFrmDstSrc: {
  342. // Check segment override opcode prefix as needed (not for %ds).
  343. if (Inst.getOperand(2).getReg() != X86::DS)
  344. SegmentReg = Inst.getOperand(2).getReg();
  345. break;
  346. }
  347. case X86II::RawFrmSrc: {
  348. // Check segment override opcode prefix as needed (not for %ds).
  349. if (Inst.getOperand(1).getReg() != X86::DS)
  350. SegmentReg = Inst.getOperand(1).getReg();
  351. break;
  352. }
  353. case X86II::RawFrmMemOffs: {
  354. // Check segment override opcode prefix as needed.
  355. SegmentReg = Inst.getOperand(1).getReg();
  356. break;
  357. }
  358. }
  359. if (SegmentReg != 0)
  360. return X86::getSegmentOverridePrefixForReg(SegmentReg);
  361. if (STI.hasFeature(X86::Mode64Bit))
  362. return X86::CS_Encoding;
  363. if (MemoryOperand >= 0) {
  364. unsigned BaseRegNum = MemoryOperand + X86::AddrBaseReg;
  365. unsigned BaseReg = Inst.getOperand(BaseRegNum).getReg();
  366. if (BaseReg == X86::ESP || BaseReg == X86::EBP)
  367. return X86::SS_Encoding;
  368. }
  369. return X86::DS_Encoding;
  370. }
  371. /// Check if the two instructions will be macro-fused on the target cpu.
  372. bool X86AsmBackend::isMacroFused(const MCInst &Cmp, const MCInst &Jcc) const {
  373. const MCInstrDesc &InstDesc = MCII->get(Jcc.getOpcode());
  374. if (!InstDesc.isConditionalBranch())
  375. return false;
  376. if (!isFirstMacroFusibleInst(Cmp, *MCII))
  377. return false;
  378. const X86::FirstMacroFusionInstKind CmpKind =
  379. X86::classifyFirstOpcodeInMacroFusion(Cmp.getOpcode());
  380. const X86::SecondMacroFusionInstKind BranchKind =
  381. classifySecondInstInMacroFusion(Jcc, *MCII);
  382. return X86::isMacroFused(CmpKind, BranchKind);
  383. }
  384. /// Check if the instruction has a variant symbol operand.
  385. static bool hasVariantSymbol(const MCInst &MI) {
  386. for (auto &Operand : MI) {
  387. if (!Operand.isExpr())
  388. continue;
  389. const MCExpr &Expr = *Operand.getExpr();
  390. if (Expr.getKind() == MCExpr::SymbolRef &&
  391. cast<MCSymbolRefExpr>(Expr).getKind() != MCSymbolRefExpr::VK_None)
  392. return true;
  393. }
  394. return false;
  395. }
  396. bool X86AsmBackend::allowAutoPadding() const {
  397. return (AlignBoundary != Align(1) && AlignBranchType != X86::AlignBranchNone);
  398. }
  399. bool X86AsmBackend::allowEnhancedRelaxation() const {
  400. return allowAutoPadding() && TargetPrefixMax != 0 && X86PadForBranchAlign;
  401. }
  402. /// X86 has certain instructions which enable interrupts exactly one
  403. /// instruction *after* the instruction which stores to SS. Return true if the
  404. /// given instruction has such an interrupt delay slot.
  405. static bool hasInterruptDelaySlot(const MCInst &Inst) {
  406. switch (Inst.getOpcode()) {
  407. case X86::POPSS16:
  408. case X86::POPSS32:
  409. case X86::STI:
  410. return true;
  411. case X86::MOV16sr:
  412. case X86::MOV32sr:
  413. case X86::MOV64sr:
  414. case X86::MOV16sm:
  415. if (Inst.getOperand(0).getReg() == X86::SS)
  416. return true;
  417. break;
  418. }
  419. return false;
  420. }
  421. /// Check if the instruction to be emitted is right after any data.
  422. static bool
  423. isRightAfterData(MCFragment *CurrentFragment,
  424. const std::pair<MCFragment *, size_t> &PrevInstPosition) {
  425. MCFragment *F = CurrentFragment;
  426. // Empty data fragments may be created to prevent further data being
  427. // added into the previous fragment, we need to skip them since they
  428. // have no contents.
  429. for (; isa_and_nonnull<MCDataFragment>(F); F = F->getPrevNode())
  430. if (cast<MCDataFragment>(F)->getContents().size() != 0)
  431. break;
  432. // Since data is always emitted into a DataFragment, our check strategy is
  433. // simple here.
  434. // - If the fragment is a DataFragment
  435. // - If it's not the fragment where the previous instruction is,
  436. // returns true.
  437. // - If it's the fragment holding the previous instruction but its
  438. // size changed since the the previous instruction was emitted into
  439. // it, returns true.
  440. // - Otherwise returns false.
  441. // - If the fragment is not a DataFragment, returns false.
  442. if (auto *DF = dyn_cast_or_null<MCDataFragment>(F))
  443. return DF != PrevInstPosition.first ||
  444. DF->getContents().size() != PrevInstPosition.second;
  445. return false;
  446. }
  447. /// \returns the fragment size if it has instructions, otherwise returns 0.
  448. static size_t getSizeForInstFragment(const MCFragment *F) {
  449. if (!F || !F->hasInstructions())
  450. return 0;
  451. // MCEncodedFragmentWithContents being templated makes this tricky.
  452. switch (F->getKind()) {
  453. default:
  454. llvm_unreachable("Unknown fragment with instructions!");
  455. case MCFragment::FT_Data:
  456. return cast<MCDataFragment>(*F).getContents().size();
  457. case MCFragment::FT_Relaxable:
  458. return cast<MCRelaxableFragment>(*F).getContents().size();
  459. case MCFragment::FT_CompactEncodedInst:
  460. return cast<MCCompactEncodedInstFragment>(*F).getContents().size();
  461. }
  462. }
  463. /// Return true if we can insert NOP or prefixes automatically before the
  464. /// the instruction to be emitted.
  465. bool X86AsmBackend::canPadInst(const MCInst &Inst, MCObjectStreamer &OS) const {
  466. if (hasVariantSymbol(Inst))
  467. // Linker may rewrite the instruction with variant symbol operand(e.g.
  468. // TLSCALL).
  469. return false;
  470. if (hasInterruptDelaySlot(PrevInst))
  471. // If this instruction follows an interrupt enabling instruction with a one
  472. // instruction delay, inserting a nop would change behavior.
  473. return false;
  474. if (isPrefix(PrevInst, *MCII))
  475. // If this instruction follows a prefix, inserting a nop/prefix would change
  476. // semantic.
  477. return false;
  478. if (isPrefix(Inst, *MCII))
  479. // If this instruction is a prefix, inserting a prefix would change
  480. // semantic.
  481. return false;
  482. if (isRightAfterData(OS.getCurrentFragment(), PrevInstPosition))
  483. // If this instruction follows any data, there is no clear
  484. // instruction boundary, inserting a nop/prefix would change semantic.
  485. return false;
  486. return true;
  487. }
  488. bool X86AsmBackend::canPadBranches(MCObjectStreamer &OS) const {
  489. if (!OS.getAllowAutoPadding())
  490. return false;
  491. assert(allowAutoPadding() && "incorrect initialization!");
  492. // We only pad in text section.
  493. if (!OS.getCurrentSectionOnly()->getKind().isText())
  494. return false;
  495. // To be Done: Currently don't deal with Bundle cases.
  496. if (OS.getAssembler().isBundlingEnabled())
  497. return false;
  498. // Branches only need to be aligned in 32-bit or 64-bit mode.
  499. if (!(STI.hasFeature(X86::Mode64Bit) || STI.hasFeature(X86::Mode32Bit)))
  500. return false;
  501. return true;
  502. }
  503. /// Check if the instruction operand needs to be aligned.
  504. bool X86AsmBackend::needAlign(const MCInst &Inst) const {
  505. const MCInstrDesc &Desc = MCII->get(Inst.getOpcode());
  506. return (Desc.isConditionalBranch() &&
  507. (AlignBranchType & X86::AlignBranchJcc)) ||
  508. (Desc.isUnconditionalBranch() &&
  509. (AlignBranchType & X86::AlignBranchJmp)) ||
  510. (Desc.isCall() && (AlignBranchType & X86::AlignBranchCall)) ||
  511. (Desc.isReturn() && (AlignBranchType & X86::AlignBranchRet)) ||
  512. (Desc.isIndirectBranch() &&
  513. (AlignBranchType & X86::AlignBranchIndirect));
  514. }
  515. /// Insert BoundaryAlignFragment before instructions to align branches.
  516. void X86AsmBackend::emitInstructionBegin(MCObjectStreamer &OS,
  517. const MCInst &Inst, const MCSubtargetInfo &STI) {
  518. CanPadInst = canPadInst(Inst, OS);
  519. if (!canPadBranches(OS))
  520. return;
  521. if (!isMacroFused(PrevInst, Inst))
  522. // Macro fusion doesn't happen indeed, clear the pending.
  523. PendingBA = nullptr;
  524. if (!CanPadInst)
  525. return;
  526. if (PendingBA && OS.getCurrentFragment()->getPrevNode() == PendingBA) {
  527. // Macro fusion actually happens and there is no other fragment inserted
  528. // after the previous instruction.
  529. //
  530. // Do nothing here since we already inserted a BoudaryAlign fragment when
  531. // we met the first instruction in the fused pair and we'll tie them
  532. // together in emitInstructionEnd.
  533. //
  534. // Note: When there is at least one fragment, such as MCAlignFragment,
  535. // inserted after the previous instruction, e.g.
  536. //
  537. // \code
  538. // cmp %rax %rcx
  539. // .align 16
  540. // je .Label0
  541. // \ endcode
  542. //
  543. // We will treat the JCC as a unfused branch although it may be fused
  544. // with the CMP.
  545. return;
  546. }
  547. if (needAlign(Inst) || ((AlignBranchType & X86::AlignBranchFused) &&
  548. isFirstMacroFusibleInst(Inst, *MCII))) {
  549. // If we meet a unfused branch or the first instuction in a fusiable pair,
  550. // insert a BoundaryAlign fragment.
  551. OS.insert(PendingBA = new MCBoundaryAlignFragment(AlignBoundary, STI));
  552. }
  553. }
  554. /// Set the last fragment to be aligned for the BoundaryAlignFragment.
  555. void X86AsmBackend::emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst) {
  556. PrevInst = Inst;
  557. MCFragment *CF = OS.getCurrentFragment();
  558. PrevInstPosition = std::make_pair(CF, getSizeForInstFragment(CF));
  559. if (auto *F = dyn_cast_or_null<MCRelaxableFragment>(CF))
  560. F->setAllowAutoPadding(CanPadInst);
  561. if (!canPadBranches(OS))
  562. return;
  563. if (!needAlign(Inst) || !PendingBA)
  564. return;
  565. // Tie the aligned instructions into a a pending BoundaryAlign.
  566. PendingBA->setLastFragment(CF);
  567. PendingBA = nullptr;
  568. // We need to ensure that further data isn't added to the current
  569. // DataFragment, so that we can get the size of instructions later in
  570. // MCAssembler::relaxBoundaryAlign. The easiest way is to insert a new empty
  571. // DataFragment.
  572. if (isa_and_nonnull<MCDataFragment>(CF))
  573. OS.insert(new MCDataFragment());
  574. // Update the maximum alignment on the current section if necessary.
  575. MCSection *Sec = OS.getCurrentSectionOnly();
  576. if (AlignBoundary.value() > Sec->getAlignment())
  577. Sec->setAlignment(AlignBoundary);
  578. }
  579. Optional<MCFixupKind> X86AsmBackend::getFixupKind(StringRef Name) const {
  580. if (STI.getTargetTriple().isOSBinFormatELF()) {
  581. unsigned Type;
  582. if (STI.getTargetTriple().getArch() == Triple::x86_64) {
  583. Type = llvm::StringSwitch<unsigned>(Name)
  584. #define ELF_RELOC(X, Y) .Case(#X, Y)
  585. #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
  586. #undef ELF_RELOC
  587. .Case("BFD_RELOC_NONE", ELF::R_X86_64_NONE)
  588. .Case("BFD_RELOC_8", ELF::R_X86_64_8)
  589. .Case("BFD_RELOC_16", ELF::R_X86_64_16)
  590. .Case("BFD_RELOC_32", ELF::R_X86_64_32)
  591. .Case("BFD_RELOC_64", ELF::R_X86_64_64)
  592. .Default(-1u);
  593. } else {
  594. Type = llvm::StringSwitch<unsigned>(Name)
  595. #define ELF_RELOC(X, Y) .Case(#X, Y)
  596. #include "llvm/BinaryFormat/ELFRelocs/i386.def"
  597. #undef ELF_RELOC
  598. .Case("BFD_RELOC_NONE", ELF::R_386_NONE)
  599. .Case("BFD_RELOC_8", ELF::R_386_8)
  600. .Case("BFD_RELOC_16", ELF::R_386_16)
  601. .Case("BFD_RELOC_32", ELF::R_386_32)
  602. .Default(-1u);
  603. }
  604. if (Type == -1u)
  605. return None;
  606. return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
  607. }
  608. return MCAsmBackend::getFixupKind(Name);
  609. }
  610. const MCFixupKindInfo &X86AsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
  611. const static MCFixupKindInfo Infos[X86::NumTargetFixupKinds] = {
  612. {"reloc_riprel_4byte", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
  613. {"reloc_riprel_4byte_movq_load", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
  614. {"reloc_riprel_4byte_relax", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
  615. {"reloc_riprel_4byte_relax_rex", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
  616. {"reloc_signed_4byte", 0, 32, 0},
  617. {"reloc_signed_4byte_relax", 0, 32, 0},
  618. {"reloc_global_offset_table", 0, 32, 0},
  619. {"reloc_global_offset_table8", 0, 64, 0},
  620. {"reloc_branch_4byte_pcrel", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
  621. };
  622. // Fixup kinds from .reloc directive are like R_386_NONE/R_X86_64_NONE. They
  623. // do not require any extra processing.
  624. if (Kind >= FirstLiteralRelocationKind)
  625. return MCAsmBackend::getFixupKindInfo(FK_NONE);
  626. if (Kind < FirstTargetFixupKind)
  627. return MCAsmBackend::getFixupKindInfo(Kind);
  628. assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
  629. "Invalid kind!");
  630. assert(Infos[Kind - FirstTargetFixupKind].Name && "Empty fixup name!");
  631. return Infos[Kind - FirstTargetFixupKind];
  632. }
  633. bool X86AsmBackend::shouldForceRelocation(const MCAssembler &,
  634. const MCFixup &Fixup,
  635. const MCValue &) {
  636. return Fixup.getKind() >= FirstLiteralRelocationKind;
  637. }
  638. static unsigned getFixupKindSize(unsigned Kind) {
  639. switch (Kind) {
  640. default:
  641. llvm_unreachable("invalid fixup kind!");
  642. case FK_NONE:
  643. return 0;
  644. case FK_PCRel_1:
  645. case FK_SecRel_1:
  646. case FK_Data_1:
  647. return 1;
  648. case FK_PCRel_2:
  649. case FK_SecRel_2:
  650. case FK_Data_2:
  651. return 2;
  652. case FK_PCRel_4:
  653. case X86::reloc_riprel_4byte:
  654. case X86::reloc_riprel_4byte_relax:
  655. case X86::reloc_riprel_4byte_relax_rex:
  656. case X86::reloc_riprel_4byte_movq_load:
  657. case X86::reloc_signed_4byte:
  658. case X86::reloc_signed_4byte_relax:
  659. case X86::reloc_global_offset_table:
  660. case X86::reloc_branch_4byte_pcrel:
  661. case FK_SecRel_4:
  662. case FK_Data_4:
  663. return 4;
  664. case FK_PCRel_8:
  665. case FK_SecRel_8:
  666. case FK_Data_8:
  667. case X86::reloc_global_offset_table8:
  668. return 8;
  669. }
  670. }
  671. void X86AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
  672. const MCValue &Target,
  673. MutableArrayRef<char> Data,
  674. uint64_t Value, bool IsResolved,
  675. const MCSubtargetInfo *STI) const {
  676. unsigned Kind = Fixup.getKind();
  677. if (Kind >= FirstLiteralRelocationKind)
  678. return;
  679. unsigned Size = getFixupKindSize(Kind);
  680. assert(Fixup.getOffset() + Size <= Data.size() && "Invalid fixup offset!");
  681. int64_t SignedValue = static_cast<int64_t>(Value);
  682. if ((Target.isAbsolute() || IsResolved) &&
  683. getFixupKindInfo(Fixup.getKind()).Flags &
  684. MCFixupKindInfo::FKF_IsPCRel) {
  685. // check that PC relative fixup fits into the fixup size.
  686. if (Size > 0 && !isIntN(Size * 8, SignedValue))
  687. Asm.getContext().reportError(
  688. Fixup.getLoc(), "value of " + Twine(SignedValue) +
  689. " is too large for field of " + Twine(Size) +
  690. ((Size == 1) ? " byte." : " bytes."));
  691. } else {
  692. // Check that uppper bits are either all zeros or all ones.
  693. // Specifically ignore overflow/underflow as long as the leakage is
  694. // limited to the lower bits. This is to remain compatible with
  695. // other assemblers.
  696. assert((Size == 0 || isIntN(Size * 8 + 1, SignedValue)) &&
  697. "Value does not fit in the Fixup field");
  698. }
  699. for (unsigned i = 0; i != Size; ++i)
  700. Data[Fixup.getOffset() + i] = uint8_t(Value >> (i * 8));
  701. }
  702. bool X86AsmBackend::mayNeedRelaxation(const MCInst &Inst,
  703. const MCSubtargetInfo &STI) const {
  704. // Branches can always be relaxed in either mode.
  705. if (getRelaxedOpcodeBranch(Inst, false) != Inst.getOpcode())
  706. return true;
  707. // Check if this instruction is ever relaxable.
  708. if (getRelaxedOpcodeArith(Inst) == Inst.getOpcode())
  709. return false;
  710. // Check if the relaxable operand has an expression. For the current set of
  711. // relaxable instructions, the relaxable operand is always the last operand.
  712. unsigned RelaxableOp = Inst.getNumOperands() - 1;
  713. if (Inst.getOperand(RelaxableOp).isExpr())
  714. return true;
  715. return false;
  716. }
  717. bool X86AsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
  718. uint64_t Value,
  719. const MCRelaxableFragment *DF,
  720. const MCAsmLayout &Layout) const {
  721. // Relax if the value is too big for a (signed) i8.
  722. return !isInt<8>(Value);
  723. }
  724. // FIXME: Can tblgen help at all here to verify there aren't other instructions
  725. // we can relax?
  726. void X86AsmBackend::relaxInstruction(MCInst &Inst,
  727. const MCSubtargetInfo &STI) const {
  728. // The only relaxations X86 does is from a 1byte pcrel to a 4byte pcrel.
  729. bool Is16BitMode = STI.getFeatureBits()[X86::Mode16Bit];
  730. unsigned RelaxedOp = getRelaxedOpcode(Inst, Is16BitMode);
  731. if (RelaxedOp == Inst.getOpcode()) {
  732. SmallString<256> Tmp;
  733. raw_svector_ostream OS(Tmp);
  734. Inst.dump_pretty(OS);
  735. OS << "\n";
  736. report_fatal_error("unexpected instruction to relax: " + OS.str());
  737. }
  738. Inst.setOpcode(RelaxedOp);
  739. }
  740. /// Return true if this instruction has been fully relaxed into it's most
  741. /// general available form.
  742. static bool isFullyRelaxed(const MCRelaxableFragment &RF) {
  743. auto &Inst = RF.getInst();
  744. auto &STI = *RF.getSubtargetInfo();
  745. bool Is16BitMode = STI.getFeatureBits()[X86::Mode16Bit];
  746. return getRelaxedOpcode(Inst, Is16BitMode) == Inst.getOpcode();
  747. }
  748. bool X86AsmBackend::padInstructionViaPrefix(MCRelaxableFragment &RF,
  749. MCCodeEmitter &Emitter,
  750. unsigned &RemainingSize) const {
  751. if (!RF.getAllowAutoPadding())
  752. return false;
  753. // If the instruction isn't fully relaxed, shifting it around might require a
  754. // larger value for one of the fixups then can be encoded. The outer loop
  755. // will also catch this before moving to the next instruction, but we need to
  756. // prevent padding this single instruction as well.
  757. if (!isFullyRelaxed(RF))
  758. return false;
  759. const unsigned OldSize = RF.getContents().size();
  760. if (OldSize == 15)
  761. return false;
  762. const unsigned MaxPossiblePad = std::min(15 - OldSize, RemainingSize);
  763. const unsigned RemainingPrefixSize = [&]() -> unsigned {
  764. SmallString<15> Code;
  765. raw_svector_ostream VecOS(Code);
  766. Emitter.emitPrefix(RF.getInst(), VecOS, STI);
  767. assert(Code.size() < 15 && "The number of prefixes must be less than 15.");
  768. // TODO: It turns out we need a decent amount of plumbing for the target
  769. // specific bits to determine number of prefixes its safe to add. Various
  770. // targets (older chips mostly, but also Atom family) encounter decoder
  771. // stalls with too many prefixes. For testing purposes, we set the value
  772. // externally for the moment.
  773. unsigned ExistingPrefixSize = Code.size();
  774. if (TargetPrefixMax <= ExistingPrefixSize)
  775. return 0;
  776. return TargetPrefixMax - ExistingPrefixSize;
  777. }();
  778. const unsigned PrefixBytesToAdd =
  779. std::min(MaxPossiblePad, RemainingPrefixSize);
  780. if (PrefixBytesToAdd == 0)
  781. return false;
  782. const uint8_t Prefix = determinePaddingPrefix(RF.getInst());
  783. SmallString<256> Code;
  784. Code.append(PrefixBytesToAdd, Prefix);
  785. Code.append(RF.getContents().begin(), RF.getContents().end());
  786. RF.getContents() = Code;
  787. // Adjust the fixups for the change in offsets
  788. for (auto &F : RF.getFixups()) {
  789. F.setOffset(F.getOffset() + PrefixBytesToAdd);
  790. }
  791. RemainingSize -= PrefixBytesToAdd;
  792. return true;
  793. }
  794. bool X86AsmBackend::padInstructionViaRelaxation(MCRelaxableFragment &RF,
  795. MCCodeEmitter &Emitter,
  796. unsigned &RemainingSize) const {
  797. if (isFullyRelaxed(RF))
  798. // TODO: There are lots of other tricks we could apply for increasing
  799. // encoding size without impacting performance.
  800. return false;
  801. MCInst Relaxed = RF.getInst();
  802. relaxInstruction(Relaxed, *RF.getSubtargetInfo());
  803. SmallVector<MCFixup, 4> Fixups;
  804. SmallString<15> Code;
  805. raw_svector_ostream VecOS(Code);
  806. Emitter.encodeInstruction(Relaxed, VecOS, Fixups, *RF.getSubtargetInfo());
  807. const unsigned OldSize = RF.getContents().size();
  808. const unsigned NewSize = Code.size();
  809. assert(NewSize >= OldSize && "size decrease during relaxation?");
  810. unsigned Delta = NewSize - OldSize;
  811. if (Delta > RemainingSize)
  812. return false;
  813. RF.setInst(Relaxed);
  814. RF.getContents() = Code;
  815. RF.getFixups() = Fixups;
  816. RemainingSize -= Delta;
  817. return true;
  818. }
  819. bool X86AsmBackend::padInstructionEncoding(MCRelaxableFragment &RF,
  820. MCCodeEmitter &Emitter,
  821. unsigned &RemainingSize) const {
  822. bool Changed = false;
  823. if (RemainingSize != 0)
  824. Changed |= padInstructionViaRelaxation(RF, Emitter, RemainingSize);
  825. if (RemainingSize != 0)
  826. Changed |= padInstructionViaPrefix(RF, Emitter, RemainingSize);
  827. return Changed;
  828. }
  829. void X86AsmBackend::finishLayout(MCAssembler const &Asm,
  830. MCAsmLayout &Layout) const {
  831. // See if we can further relax some instructions to cut down on the number of
  832. // nop bytes required for code alignment. The actual win is in reducing
  833. // instruction count, not number of bytes. Modern X86-64 can easily end up
  834. // decode limited. It is often better to reduce the number of instructions
  835. // (i.e. eliminate nops) even at the cost of increasing the size and
  836. // complexity of others.
  837. if (!X86PadForAlign && !X86PadForBranchAlign)
  838. return;
  839. // The processed regions are delimitered by LabeledFragments. -g may have more
  840. // MCSymbols and therefore different relaxation results. X86PadForAlign is
  841. // disabled by default to eliminate the -g vs non -g difference.
  842. DenseSet<MCFragment *> LabeledFragments;
  843. for (const MCSymbol &S : Asm.symbols())
  844. LabeledFragments.insert(S.getFragment(false));
  845. for (MCSection &Sec : Asm) {
  846. if (!Sec.getKind().isText())
  847. continue;
  848. SmallVector<MCRelaxableFragment *, 4> Relaxable;
  849. for (MCSection::iterator I = Sec.begin(), IE = Sec.end(); I != IE; ++I) {
  850. MCFragment &F = *I;
  851. if (LabeledFragments.count(&F))
  852. Relaxable.clear();
  853. if (F.getKind() == MCFragment::FT_Data ||
  854. F.getKind() == MCFragment::FT_CompactEncodedInst)
  855. // Skip and ignore
  856. continue;
  857. if (F.getKind() == MCFragment::FT_Relaxable) {
  858. auto &RF = cast<MCRelaxableFragment>(*I);
  859. Relaxable.push_back(&RF);
  860. continue;
  861. }
  862. auto canHandle = [](MCFragment &F) -> bool {
  863. switch (F.getKind()) {
  864. default:
  865. return false;
  866. case MCFragment::FT_Align:
  867. return X86PadForAlign;
  868. case MCFragment::FT_BoundaryAlign:
  869. return X86PadForBranchAlign;
  870. }
  871. };
  872. // For any unhandled kind, assume we can't change layout.
  873. if (!canHandle(F)) {
  874. Relaxable.clear();
  875. continue;
  876. }
  877. #ifndef NDEBUG
  878. const uint64_t OrigOffset = Layout.getFragmentOffset(&F);
  879. #endif
  880. const uint64_t OrigSize = Asm.computeFragmentSize(Layout, F);
  881. // To keep the effects local, prefer to relax instructions closest to
  882. // the align directive. This is purely about human understandability
  883. // of the resulting code. If we later find a reason to expand
  884. // particular instructions over others, we can adjust.
  885. MCFragment *FirstChangedFragment = nullptr;
  886. unsigned RemainingSize = OrigSize;
  887. while (!Relaxable.empty() && RemainingSize != 0) {
  888. auto &RF = *Relaxable.pop_back_val();
  889. // Give the backend a chance to play any tricks it wishes to increase
  890. // the encoding size of the given instruction. Target independent code
  891. // will try further relaxation, but target's may play further tricks.
  892. if (padInstructionEncoding(RF, Asm.getEmitter(), RemainingSize))
  893. FirstChangedFragment = &RF;
  894. // If we have an instruction which hasn't been fully relaxed, we can't
  895. // skip past it and insert bytes before it. Changing its starting
  896. // offset might require a larger negative offset than it can encode.
  897. // We don't need to worry about larger positive offsets as none of the
  898. // possible offsets between this and our align are visible, and the
  899. // ones afterwards aren't changing.
  900. if (!isFullyRelaxed(RF))
  901. break;
  902. }
  903. Relaxable.clear();
  904. if (FirstChangedFragment) {
  905. // Make sure the offsets for any fragments in the effected range get
  906. // updated. Note that this (conservatively) invalidates the offsets of
  907. // those following, but this is not required.
  908. Layout.invalidateFragmentsFrom(FirstChangedFragment);
  909. }
  910. // BoundaryAlign explicitly tracks it's size (unlike align)
  911. if (F.getKind() == MCFragment::FT_BoundaryAlign)
  912. cast<MCBoundaryAlignFragment>(F).setSize(RemainingSize);
  913. #ifndef NDEBUG
  914. const uint64_t FinalOffset = Layout.getFragmentOffset(&F);
  915. const uint64_t FinalSize = Asm.computeFragmentSize(Layout, F);
  916. assert(OrigOffset + OrigSize == FinalOffset + FinalSize &&
  917. "can't move start of next fragment!");
  918. assert(FinalSize == RemainingSize && "inconsistent size computation?");
  919. #endif
  920. // If we're looking at a boundary align, make sure we don't try to pad
  921. // its target instructions for some following directive. Doing so would
  922. // break the alignment of the current boundary align.
  923. if (auto *BF = dyn_cast<MCBoundaryAlignFragment>(&F)) {
  924. const MCFragment *LastFragment = BF->getLastFragment();
  925. if (!LastFragment)
  926. continue;
  927. while (&*I != LastFragment)
  928. ++I;
  929. }
  930. }
  931. }
  932. // The layout is done. Mark every fragment as valid.
  933. for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
  934. MCSection &Section = *Layout.getSectionOrder()[i];
  935. Layout.getFragmentOffset(&*Section.getFragmentList().rbegin());
  936. Asm.computeFragmentSize(Layout, *Section.getFragmentList().rbegin());
  937. }
  938. }
  939. unsigned X86AsmBackend::getMaximumNopSize(const MCSubtargetInfo &STI) const {
  940. if (STI.hasFeature(X86::Mode16Bit))
  941. return 4;
  942. if (!STI.hasFeature(X86::FeatureNOPL) && !STI.hasFeature(X86::Mode64Bit))
  943. return 1;
  944. if (STI.getFeatureBits()[X86::TuningFast7ByteNOP])
  945. return 7;
  946. if (STI.getFeatureBits()[X86::TuningFast15ByteNOP])
  947. return 15;
  948. if (STI.getFeatureBits()[X86::TuningFast11ByteNOP])
  949. return 11;
  950. // FIXME: handle 32-bit mode
  951. // 15-bytes is the longest single NOP instruction, but 10-bytes is
  952. // commonly the longest that can be efficiently decoded.
  953. return 10;
  954. }
  955. /// Write a sequence of optimal nops to the output, covering \p Count
  956. /// bytes.
  957. /// \return - true on success, false on failure
  958. bool X86AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
  959. const MCSubtargetInfo *STI) const {
  960. static const char Nops32Bit[10][11] = {
  961. // nop
  962. "\x90",
  963. // xchg %ax,%ax
  964. "\x66\x90",
  965. // nopl (%[re]ax)
  966. "\x0f\x1f\x00",
  967. // nopl 0(%[re]ax)
  968. "\x0f\x1f\x40\x00",
  969. // nopl 0(%[re]ax,%[re]ax,1)
  970. "\x0f\x1f\x44\x00\x00",
  971. // nopw 0(%[re]ax,%[re]ax,1)
  972. "\x66\x0f\x1f\x44\x00\x00",
  973. // nopl 0L(%[re]ax)
  974. "\x0f\x1f\x80\x00\x00\x00\x00",
  975. // nopl 0L(%[re]ax,%[re]ax,1)
  976. "\x0f\x1f\x84\x00\x00\x00\x00\x00",
  977. // nopw 0L(%[re]ax,%[re]ax,1)
  978. "\x66\x0f\x1f\x84\x00\x00\x00\x00\x00",
  979. // nopw %cs:0L(%[re]ax,%[re]ax,1)
  980. "\x66\x2e\x0f\x1f\x84\x00\x00\x00\x00\x00",
  981. };
  982. // 16-bit mode uses different nop patterns than 32-bit.
  983. static const char Nops16Bit[4][11] = {
  984. // nop
  985. "\x90",
  986. // xchg %eax,%eax
  987. "\x66\x90",
  988. // lea 0(%si),%si
  989. "\x8d\x74\x00",
  990. // lea 0w(%si),%si
  991. "\x8d\xb4\x00\x00",
  992. };
  993. const char(*Nops)[11] =
  994. STI->getFeatureBits()[X86::Mode16Bit] ? Nops16Bit : Nops32Bit;
  995. uint64_t MaxNopLength = (uint64_t)getMaximumNopSize(*STI);
  996. // Emit as many MaxNopLength NOPs as needed, then emit a NOP of the remaining
  997. // length.
  998. do {
  999. const uint8_t ThisNopLength = (uint8_t) std::min(Count, MaxNopLength);
  1000. const uint8_t Prefixes = ThisNopLength <= 10 ? 0 : ThisNopLength - 10;
  1001. for (uint8_t i = 0; i < Prefixes; i++)
  1002. OS << '\x66';
  1003. const uint8_t Rest = ThisNopLength - Prefixes;
  1004. if (Rest != 0)
  1005. OS.write(Nops[Rest - 1], Rest);
  1006. Count -= ThisNopLength;
  1007. } while (Count != 0);
  1008. return true;
  1009. }
  1010. /* *** */
  1011. namespace {
  1012. class ELFX86AsmBackend : public X86AsmBackend {
  1013. public:
  1014. uint8_t OSABI;
  1015. ELFX86AsmBackend(const Target &T, uint8_t OSABI, const MCSubtargetInfo &STI)
  1016. : X86AsmBackend(T, STI), OSABI(OSABI) {}
  1017. };
  1018. class ELFX86_32AsmBackend : public ELFX86AsmBackend {
  1019. public:
  1020. ELFX86_32AsmBackend(const Target &T, uint8_t OSABI,
  1021. const MCSubtargetInfo &STI)
  1022. : ELFX86AsmBackend(T, OSABI, STI) {}
  1023. std::unique_ptr<MCObjectTargetWriter>
  1024. createObjectTargetWriter() const override {
  1025. return createX86ELFObjectWriter(/*IsELF64*/ false, OSABI, ELF::EM_386);
  1026. }
  1027. };
  1028. class ELFX86_X32AsmBackend : public ELFX86AsmBackend {
  1029. public:
  1030. ELFX86_X32AsmBackend(const Target &T, uint8_t OSABI,
  1031. const MCSubtargetInfo &STI)
  1032. : ELFX86AsmBackend(T, OSABI, STI) {}
  1033. std::unique_ptr<MCObjectTargetWriter>
  1034. createObjectTargetWriter() const override {
  1035. return createX86ELFObjectWriter(/*IsELF64*/ false, OSABI,
  1036. ELF::EM_X86_64);
  1037. }
  1038. };
  1039. class ELFX86_IAMCUAsmBackend : public ELFX86AsmBackend {
  1040. public:
  1041. ELFX86_IAMCUAsmBackend(const Target &T, uint8_t OSABI,
  1042. const MCSubtargetInfo &STI)
  1043. : ELFX86AsmBackend(T, OSABI, STI) {}
  1044. std::unique_ptr<MCObjectTargetWriter>
  1045. createObjectTargetWriter() const override {
  1046. return createX86ELFObjectWriter(/*IsELF64*/ false, OSABI,
  1047. ELF::EM_IAMCU);
  1048. }
  1049. };
  1050. class ELFX86_64AsmBackend : public ELFX86AsmBackend {
  1051. public:
  1052. ELFX86_64AsmBackend(const Target &T, uint8_t OSABI,
  1053. const MCSubtargetInfo &STI)
  1054. : ELFX86AsmBackend(T, OSABI, STI) {}
  1055. std::unique_ptr<MCObjectTargetWriter>
  1056. createObjectTargetWriter() const override {
  1057. return createX86ELFObjectWriter(/*IsELF64*/ true, OSABI, ELF::EM_X86_64);
  1058. }
  1059. };
  1060. class WindowsX86AsmBackend : public X86AsmBackend {
  1061. bool Is64Bit;
  1062. public:
  1063. WindowsX86AsmBackend(const Target &T, bool is64Bit,
  1064. const MCSubtargetInfo &STI)
  1065. : X86AsmBackend(T, STI)
  1066. , Is64Bit(is64Bit) {
  1067. }
  1068. Optional<MCFixupKind> getFixupKind(StringRef Name) const override {
  1069. return StringSwitch<Optional<MCFixupKind>>(Name)
  1070. .Case("dir32", FK_Data_4)
  1071. .Case("secrel32", FK_SecRel_4)
  1072. .Case("secidx", FK_SecRel_2)
  1073. .Default(MCAsmBackend::getFixupKind(Name));
  1074. }
  1075. std::unique_ptr<MCObjectTargetWriter>
  1076. createObjectTargetWriter() const override {
  1077. return createX86WinCOFFObjectWriter(Is64Bit);
  1078. }
  1079. };
  1080. namespace CU {
  1081. /// Compact unwind encoding values.
  1082. enum CompactUnwindEncodings {
  1083. /// [RE]BP based frame where [RE]BP is pused on the stack immediately after
  1084. /// the return address, then [RE]SP is moved to [RE]BP.
  1085. UNWIND_MODE_BP_FRAME = 0x01000000,
  1086. /// A frameless function with a small constant stack size.
  1087. UNWIND_MODE_STACK_IMMD = 0x02000000,
  1088. /// A frameless function with a large constant stack size.
  1089. UNWIND_MODE_STACK_IND = 0x03000000,
  1090. /// No compact unwind encoding is available.
  1091. UNWIND_MODE_DWARF = 0x04000000,
  1092. /// Mask for encoding the frame registers.
  1093. UNWIND_BP_FRAME_REGISTERS = 0x00007FFF,
  1094. /// Mask for encoding the frameless registers.
  1095. UNWIND_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF
  1096. };
  1097. } // namespace CU
  1098. class DarwinX86AsmBackend : public X86AsmBackend {
  1099. const MCRegisterInfo &MRI;
  1100. /// Number of registers that can be saved in a compact unwind encoding.
  1101. enum { CU_NUM_SAVED_REGS = 6 };
  1102. mutable unsigned SavedRegs[CU_NUM_SAVED_REGS];
  1103. Triple TT;
  1104. bool Is64Bit;
  1105. unsigned OffsetSize; ///< Offset of a "push" instruction.
  1106. unsigned MoveInstrSize; ///< Size of a "move" instruction.
  1107. unsigned StackDivide; ///< Amount to adjust stack size by.
  1108. protected:
  1109. /// Size of a "push" instruction for the given register.
  1110. unsigned PushInstrSize(unsigned Reg) const {
  1111. switch (Reg) {
  1112. case X86::EBX:
  1113. case X86::ECX:
  1114. case X86::EDX:
  1115. case X86::EDI:
  1116. case X86::ESI:
  1117. case X86::EBP:
  1118. case X86::RBX:
  1119. case X86::RBP:
  1120. return 1;
  1121. case X86::R12:
  1122. case X86::R13:
  1123. case X86::R14:
  1124. case X86::R15:
  1125. return 2;
  1126. }
  1127. return 1;
  1128. }
  1129. private:
  1130. /// Get the compact unwind number for a given register. The number
  1131. /// corresponds to the enum lists in compact_unwind_encoding.h.
  1132. int getCompactUnwindRegNum(unsigned Reg) const {
  1133. static const MCPhysReg CU32BitRegs[7] = {
  1134. X86::EBX, X86::ECX, X86::EDX, X86::EDI, X86::ESI, X86::EBP, 0
  1135. };
  1136. static const MCPhysReg CU64BitRegs[] = {
  1137. X86::RBX, X86::R12, X86::R13, X86::R14, X86::R15, X86::RBP, 0
  1138. };
  1139. const MCPhysReg *CURegs = Is64Bit ? CU64BitRegs : CU32BitRegs;
  1140. for (int Idx = 1; *CURegs; ++CURegs, ++Idx)
  1141. if (*CURegs == Reg)
  1142. return Idx;
  1143. return -1;
  1144. }
  1145. /// Return the registers encoded for a compact encoding with a frame
  1146. /// pointer.
  1147. uint32_t encodeCompactUnwindRegistersWithFrame() const {
  1148. // Encode the registers in the order they were saved --- 3-bits per
  1149. // register. The list of saved registers is assumed to be in reverse
  1150. // order. The registers are numbered from 1 to CU_NUM_SAVED_REGS.
  1151. uint32_t RegEnc = 0;
  1152. for (int i = 0, Idx = 0; i != CU_NUM_SAVED_REGS; ++i) {
  1153. unsigned Reg = SavedRegs[i];
  1154. if (Reg == 0) break;
  1155. int CURegNum = getCompactUnwindRegNum(Reg);
  1156. if (CURegNum == -1) return ~0U;
  1157. // Encode the 3-bit register number in order, skipping over 3-bits for
  1158. // each register.
  1159. RegEnc |= (CURegNum & 0x7) << (Idx++ * 3);
  1160. }
  1161. assert((RegEnc & 0x3FFFF) == RegEnc &&
  1162. "Invalid compact register encoding!");
  1163. return RegEnc;
  1164. }
  1165. /// Create the permutation encoding used with frameless stacks. It is
  1166. /// passed the number of registers to be saved and an array of the registers
  1167. /// saved.
  1168. uint32_t encodeCompactUnwindRegistersWithoutFrame(unsigned RegCount) const {
  1169. // The saved registers are numbered from 1 to 6. In order to encode the
  1170. // order in which they were saved, we re-number them according to their
  1171. // place in the register order. The re-numbering is relative to the last
  1172. // re-numbered register. E.g., if we have registers {6, 2, 4, 5} saved in
  1173. // that order:
  1174. //
  1175. // Orig Re-Num
  1176. // ---- ------
  1177. // 6 6
  1178. // 2 2
  1179. // 4 3
  1180. // 5 3
  1181. //
  1182. for (unsigned i = 0; i < RegCount; ++i) {
  1183. int CUReg = getCompactUnwindRegNum(SavedRegs[i]);
  1184. if (CUReg == -1) return ~0U;
  1185. SavedRegs[i] = CUReg;
  1186. }
  1187. // Reverse the list.
  1188. std::reverse(&SavedRegs[0], &SavedRegs[CU_NUM_SAVED_REGS]);
  1189. uint32_t RenumRegs[CU_NUM_SAVED_REGS];
  1190. for (unsigned i = CU_NUM_SAVED_REGS - RegCount; i < CU_NUM_SAVED_REGS; ++i){
  1191. unsigned Countless = 0;
  1192. for (unsigned j = CU_NUM_SAVED_REGS - RegCount; j < i; ++j)
  1193. if (SavedRegs[j] < SavedRegs[i])
  1194. ++Countless;
  1195. RenumRegs[i] = SavedRegs[i] - Countless - 1;
  1196. }
  1197. // Take the renumbered values and encode them into a 10-bit number.
  1198. uint32_t permutationEncoding = 0;
  1199. switch (RegCount) {
  1200. case 6:
  1201. permutationEncoding |= 120 * RenumRegs[0] + 24 * RenumRegs[1]
  1202. + 6 * RenumRegs[2] + 2 * RenumRegs[3]
  1203. + RenumRegs[4];
  1204. break;
  1205. case 5:
  1206. permutationEncoding |= 120 * RenumRegs[1] + 24 * RenumRegs[2]
  1207. + 6 * RenumRegs[3] + 2 * RenumRegs[4]
  1208. + RenumRegs[5];
  1209. break;
  1210. case 4:
  1211. permutationEncoding |= 60 * RenumRegs[2] + 12 * RenumRegs[3]
  1212. + 3 * RenumRegs[4] + RenumRegs[5];
  1213. break;
  1214. case 3:
  1215. permutationEncoding |= 20 * RenumRegs[3] + 4 * RenumRegs[4]
  1216. + RenumRegs[5];
  1217. break;
  1218. case 2:
  1219. permutationEncoding |= 5 * RenumRegs[4] + RenumRegs[5];
  1220. break;
  1221. case 1:
  1222. permutationEncoding |= RenumRegs[5];
  1223. break;
  1224. }
  1225. assert((permutationEncoding & 0x3FF) == permutationEncoding &&
  1226. "Invalid compact register encoding!");
  1227. return permutationEncoding;
  1228. }
  1229. public:
  1230. DarwinX86AsmBackend(const Target &T, const MCRegisterInfo &MRI,
  1231. const MCSubtargetInfo &STI)
  1232. : X86AsmBackend(T, STI), MRI(MRI), TT(STI.getTargetTriple()),
  1233. Is64Bit(TT.isArch64Bit()) {
  1234. memset(SavedRegs, 0, sizeof(SavedRegs));
  1235. OffsetSize = Is64Bit ? 8 : 4;
  1236. MoveInstrSize = Is64Bit ? 3 : 2;
  1237. StackDivide = Is64Bit ? 8 : 4;
  1238. }
  1239. std::unique_ptr<MCObjectTargetWriter>
  1240. createObjectTargetWriter() const override {
  1241. uint32_t CPUType = cantFail(MachO::getCPUType(TT));
  1242. uint32_t CPUSubType = cantFail(MachO::getCPUSubType(TT));
  1243. return createX86MachObjectWriter(Is64Bit, CPUType, CPUSubType);
  1244. }
  1245. /// Implementation of algorithm to generate the compact unwind encoding
  1246. /// for the CFI instructions.
  1247. uint32_t
  1248. generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction> Instrs) const override {
  1249. if (Instrs.empty()) return 0;
  1250. // Reset the saved registers.
  1251. unsigned SavedRegIdx = 0;
  1252. memset(SavedRegs, 0, sizeof(SavedRegs));
  1253. bool HasFP = false;
  1254. // Encode that we are using EBP/RBP as the frame pointer.
  1255. uint32_t CompactUnwindEncoding = 0;
  1256. unsigned SubtractInstrIdx = Is64Bit ? 3 : 2;
  1257. unsigned InstrOffset = 0;
  1258. unsigned StackAdjust = 0;
  1259. unsigned StackSize = 0;
  1260. unsigned NumDefCFAOffsets = 0;
  1261. int MinAbsOffset = std::numeric_limits<int>::max();
  1262. for (const MCCFIInstruction &Inst : Instrs) {
  1263. switch (Inst.getOperation()) {
  1264. default:
  1265. // Any other CFI directives indicate a frame that we aren't prepared
  1266. // to represent via compact unwind, so just bail out.
  1267. return 0;
  1268. case MCCFIInstruction::OpDefCfaRegister: {
  1269. // Defines a frame pointer. E.g.
  1270. //
  1271. // movq %rsp, %rbp
  1272. // L0:
  1273. // .cfi_def_cfa_register %rbp
  1274. //
  1275. HasFP = true;
  1276. // If the frame pointer is other than esp/rsp, we do not have a way to
  1277. // generate a compact unwinding representation, so bail out.
  1278. if (*MRI.getLLVMRegNum(Inst.getRegister(), true) !=
  1279. (Is64Bit ? X86::RBP : X86::EBP))
  1280. return 0;
  1281. // Reset the counts.
  1282. memset(SavedRegs, 0, sizeof(SavedRegs));
  1283. StackAdjust = 0;
  1284. SavedRegIdx = 0;
  1285. MinAbsOffset = std::numeric_limits<int>::max();
  1286. InstrOffset += MoveInstrSize;
  1287. break;
  1288. }
  1289. case MCCFIInstruction::OpDefCfaOffset: {
  1290. // Defines a new offset for the CFA. E.g.
  1291. //
  1292. // With frame:
  1293. //
  1294. // pushq %rbp
  1295. // L0:
  1296. // .cfi_def_cfa_offset 16
  1297. //
  1298. // Without frame:
  1299. //
  1300. // subq $72, %rsp
  1301. // L0:
  1302. // .cfi_def_cfa_offset 80
  1303. //
  1304. StackSize = Inst.getOffset() / StackDivide;
  1305. ++NumDefCFAOffsets;
  1306. break;
  1307. }
  1308. case MCCFIInstruction::OpOffset: {
  1309. // Defines a "push" of a callee-saved register. E.g.
  1310. //
  1311. // pushq %r15
  1312. // pushq %r14
  1313. // pushq %rbx
  1314. // L0:
  1315. // subq $120, %rsp
  1316. // L1:
  1317. // .cfi_offset %rbx, -40
  1318. // .cfi_offset %r14, -32
  1319. // .cfi_offset %r15, -24
  1320. //
  1321. if (SavedRegIdx == CU_NUM_SAVED_REGS)
  1322. // If there are too many saved registers, we cannot use a compact
  1323. // unwind encoding.
  1324. return CU::UNWIND_MODE_DWARF;
  1325. unsigned Reg = *MRI.getLLVMRegNum(Inst.getRegister(), true);
  1326. SavedRegs[SavedRegIdx++] = Reg;
  1327. StackAdjust += OffsetSize;
  1328. MinAbsOffset = std::min(MinAbsOffset, abs(Inst.getOffset()));
  1329. InstrOffset += PushInstrSize(Reg);
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. StackAdjust /= StackDivide;
  1335. if (HasFP) {
  1336. if ((StackAdjust & 0xFF) != StackAdjust)
  1337. // Offset was too big for a compact unwind encoding.
  1338. return CU::UNWIND_MODE_DWARF;
  1339. // We don't attempt to track a real StackAdjust, so if the saved registers
  1340. // aren't adjacent to rbp we can't cope.
  1341. if (SavedRegIdx != 0 && MinAbsOffset != 3 * (int)OffsetSize)
  1342. return CU::UNWIND_MODE_DWARF;
  1343. // Get the encoding of the saved registers when we have a frame pointer.
  1344. uint32_t RegEnc = encodeCompactUnwindRegistersWithFrame();
  1345. if (RegEnc == ~0U) return CU::UNWIND_MODE_DWARF;
  1346. CompactUnwindEncoding |= CU::UNWIND_MODE_BP_FRAME;
  1347. CompactUnwindEncoding |= (StackAdjust & 0xFF) << 16;
  1348. CompactUnwindEncoding |= RegEnc & CU::UNWIND_BP_FRAME_REGISTERS;
  1349. } else {
  1350. SubtractInstrIdx += InstrOffset;
  1351. ++StackAdjust;
  1352. if ((StackSize & 0xFF) == StackSize) {
  1353. // Frameless stack with a small stack size.
  1354. CompactUnwindEncoding |= CU::UNWIND_MODE_STACK_IMMD;
  1355. // Encode the stack size.
  1356. CompactUnwindEncoding |= (StackSize & 0xFF) << 16;
  1357. } else {
  1358. if ((StackAdjust & 0x7) != StackAdjust)
  1359. // The extra stack adjustments are too big for us to handle.
  1360. return CU::UNWIND_MODE_DWARF;
  1361. // Frameless stack with an offset too large for us to encode compactly.
  1362. CompactUnwindEncoding |= CU::UNWIND_MODE_STACK_IND;
  1363. // Encode the offset to the nnnnnn value in the 'subl $nnnnnn, ESP'
  1364. // instruction.
  1365. CompactUnwindEncoding |= (SubtractInstrIdx & 0xFF) << 16;
  1366. // Encode any extra stack adjustments (done via push instructions).
  1367. CompactUnwindEncoding |= (StackAdjust & 0x7) << 13;
  1368. }
  1369. // Encode the number of registers saved. (Reverse the list first.)
  1370. std::reverse(&SavedRegs[0], &SavedRegs[SavedRegIdx]);
  1371. CompactUnwindEncoding |= (SavedRegIdx & 0x7) << 10;
  1372. // Get the encoding of the saved registers when we don't have a frame
  1373. // pointer.
  1374. uint32_t RegEnc = encodeCompactUnwindRegistersWithoutFrame(SavedRegIdx);
  1375. if (RegEnc == ~0U) return CU::UNWIND_MODE_DWARF;
  1376. // Encode the register encoding.
  1377. CompactUnwindEncoding |=
  1378. RegEnc & CU::UNWIND_FRAMELESS_STACK_REG_PERMUTATION;
  1379. }
  1380. return CompactUnwindEncoding;
  1381. }
  1382. };
  1383. } // end anonymous namespace
  1384. MCAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
  1385. const MCSubtargetInfo &STI,
  1386. const MCRegisterInfo &MRI,
  1387. const MCTargetOptions &Options) {
  1388. const Triple &TheTriple = STI.getTargetTriple();
  1389. if (TheTriple.isOSBinFormatMachO())
  1390. return new DarwinX86AsmBackend(T, MRI, STI);
  1391. if (TheTriple.isOSWindows() && TheTriple.isOSBinFormatCOFF())
  1392. return new WindowsX86AsmBackend(T, false, STI);
  1393. uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
  1394. if (TheTriple.isOSIAMCU())
  1395. return new ELFX86_IAMCUAsmBackend(T, OSABI, STI);
  1396. return new ELFX86_32AsmBackend(T, OSABI, STI);
  1397. }
  1398. MCAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
  1399. const MCSubtargetInfo &STI,
  1400. const MCRegisterInfo &MRI,
  1401. const MCTargetOptions &Options) {
  1402. const Triple &TheTriple = STI.getTargetTriple();
  1403. if (TheTriple.isOSBinFormatMachO())
  1404. return new DarwinX86AsmBackend(T, MRI, STI);
  1405. if (TheTriple.isOSWindows() && TheTriple.isOSBinFormatCOFF())
  1406. return new WindowsX86AsmBackend(T, true, STI);
  1407. uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
  1408. if (TheTriple.isX32())
  1409. return new ELFX86_X32AsmBackend(T, OSABI, STI);
  1410. return new ELFX86_64AsmBackend(T, OSABI, STI);
  1411. }