AsmPrinter.cpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513
  1. //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the AsmPrinter class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/AsmPrinter.h"
  14. #include "DwarfDebug.h"
  15. #include "DwarfException.h"
  16. #include "WinException.h"
  17. #include "WinCodeViewLineTables.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/ConstantFolding.h"
  21. #include "llvm/CodeGen/Analysis.h"
  22. #include "llvm/CodeGen/GCMetadataPrinter.h"
  23. #include "llvm/CodeGen/MachineConstantPool.h"
  24. #include "llvm/CodeGen/MachineFrameInfo.h"
  25. #include "llvm/CodeGen/MachineFunction.h"
  26. #include "llvm/CodeGen/MachineInstrBundle.h"
  27. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  28. #include "llvm/CodeGen/MachineLoopInfo.h"
  29. #include "llvm/CodeGen/MachineModuleInfoImpls.h"
  30. #include "llvm/IR/DataLayout.h"
  31. #include "llvm/IR/DebugInfo.h"
  32. #include "llvm/IR/Mangler.h"
  33. #include "llvm/IR/Module.h"
  34. #include "llvm/IR/Operator.h"
  35. #include "llvm/MC/MCAsmInfo.h"
  36. #include "llvm/MC/MCContext.h"
  37. #include "llvm/MC/MCExpr.h"
  38. #include "llvm/MC/MCInst.h"
  39. #include "llvm/MC/MCSection.h"
  40. #include "llvm/MC/MCStreamer.h"
  41. #include "llvm/MC/MCSymbolELF.h"
  42. #include "llvm/MC/MCValue.h"
  43. #include "llvm/Support/ErrorHandling.h"
  44. #include "llvm/Support/Format.h"
  45. #include "llvm/Support/MathExtras.h"
  46. #include "llvm/Support/TargetRegistry.h"
  47. #include "llvm/Support/Timer.h"
  48. #include "llvm/Target/TargetFrameLowering.h"
  49. #include "llvm/Target/TargetInstrInfo.h"
  50. #include "llvm/Target/TargetLowering.h"
  51. #include "llvm/Target/TargetLoweringObjectFile.h"
  52. #include "llvm/Target/TargetRegisterInfo.h"
  53. #include "llvm/Target/TargetSubtargetInfo.h"
  54. using namespace llvm;
  55. #define DEBUG_TYPE "asm-printer"
  56. static const char *const DWARFGroupName = "DWARF Emission";
  57. static const char *const DbgTimerName = "Debug Info Emission";
  58. static const char *const EHTimerName = "DWARF Exception Writer";
  59. static const char *const CodeViewLineTablesGroupName = "CodeView Line Tables";
  60. STATISTIC(EmittedInsts, "Number of machine instrs printed");
  61. char AsmPrinter::ID = 0;
  62. typedef DenseMap<GCStrategy*, std::unique_ptr<GCMetadataPrinter>> gcp_map_type;
  63. static gcp_map_type &getGCMap(void *&P) {
  64. if (!P)
  65. P = new gcp_map_type();
  66. return *(gcp_map_type*)P;
  67. }
  68. /// getGVAlignmentLog2 - Return the alignment to use for the specified global
  69. /// value in log2 form. This rounds up to the preferred alignment if possible
  70. /// and legal.
  71. static unsigned getGVAlignmentLog2(const GlobalValue *GV, const DataLayout &DL,
  72. unsigned InBits = 0) {
  73. unsigned NumBits = 0;
  74. if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
  75. NumBits = DL.getPreferredAlignmentLog(GVar);
  76. // If InBits is specified, round it to it.
  77. if (InBits > NumBits)
  78. NumBits = InBits;
  79. // If the GV has a specified alignment, take it into account.
  80. if (GV->getAlignment() == 0)
  81. return NumBits;
  82. unsigned GVAlign = Log2_32(GV->getAlignment());
  83. // If the GVAlign is larger than NumBits, or if we are required to obey
  84. // NumBits because the GV has an assigned section, obey it.
  85. if (GVAlign > NumBits || GV->hasSection())
  86. NumBits = GVAlign;
  87. return NumBits;
  88. }
  89. AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer)
  90. : MachineFunctionPass(ID), TM(tm), MAI(tm.getMCAsmInfo()),
  91. OutContext(Streamer->getContext()), OutStreamer(std::move(Streamer)),
  92. LastMI(nullptr), LastFn(0), Counter(~0U) {
  93. DD = nullptr;
  94. MMI = nullptr;
  95. LI = nullptr;
  96. MF = nullptr;
  97. CurExceptionSym = CurrentFnSym = CurrentFnSymForSize = nullptr;
  98. CurrentFnBegin = nullptr;
  99. CurrentFnEnd = nullptr;
  100. GCMetadataPrinters = nullptr;
  101. VerboseAsm = OutStreamer->isVerboseAsm();
  102. }
  103. AsmPrinter::~AsmPrinter() {
  104. assert(!DD && Handlers.empty() && "Debug/EH info didn't get finalized");
  105. if (GCMetadataPrinters) {
  106. gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
  107. delete &GCMap;
  108. GCMetadataPrinters = nullptr;
  109. }
  110. }
  111. /// getFunctionNumber - Return a unique ID for the current function.
  112. ///
  113. unsigned AsmPrinter::getFunctionNumber() const {
  114. return MF->getFunctionNumber();
  115. }
  116. const TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
  117. return *TM.getObjFileLowering();
  118. }
  119. /// getDataLayout - Return information about data layout.
  120. const DataLayout &AsmPrinter::getDataLayout() const {
  121. return *TM.getDataLayout();
  122. }
  123. const MCSubtargetInfo &AsmPrinter::getSubtargetInfo() const {
  124. assert(MF && "getSubtargetInfo requires a valid MachineFunction!");
  125. return MF->getSubtarget<MCSubtargetInfo>();
  126. }
  127. void AsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
  128. S.EmitInstruction(Inst, getSubtargetInfo());
  129. }
  130. StringRef AsmPrinter::getTargetTriple() const {
  131. return TM.getTargetTriple().str();
  132. }
  133. /// getCurrentSection() - Return the current section we are emitting to.
  134. const MCSection *AsmPrinter::getCurrentSection() const {
  135. return OutStreamer->getCurrentSection().first;
  136. }
  137. void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
  138. AU.setPreservesAll();
  139. MachineFunctionPass::getAnalysisUsage(AU);
  140. AU.addRequired<MachineModuleInfo>();
  141. AU.addRequired<GCModuleInfo>();
  142. if (isVerbose())
  143. AU.addRequired<MachineLoopInfo>();
  144. }
  145. bool AsmPrinter::doInitialization(Module &M) {
  146. MMI = getAnalysisIfAvailable<MachineModuleInfo>();
  147. // Initialize TargetLoweringObjectFile.
  148. const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
  149. .Initialize(OutContext, TM);
  150. OutStreamer->InitSections(false);
  151. Mang = new Mangler();
  152. // Emit the version-min deplyment target directive if needed.
  153. //
  154. // FIXME: If we end up with a collection of these sorts of Darwin-specific
  155. // or ELF-specific things, it may make sense to have a platform helper class
  156. // that will work with the target helper class. For now keep it here, as the
  157. // alternative is duplicated code in each of the target asm printers that
  158. // use the directive, where it would need the same conditionalization
  159. // anyway.
  160. Triple TT(getTargetTriple());
  161. if (TT.isOSDarwin()) {
  162. unsigned Major, Minor, Update;
  163. TT.getOSVersion(Major, Minor, Update);
  164. // If there is a version specified, Major will be non-zero.
  165. if (Major)
  166. OutStreamer->EmitVersionMin((TT.isMacOSX() ?
  167. MCVM_OSXVersionMin : MCVM_IOSVersionMin),
  168. Major, Minor, Update);
  169. }
  170. // Allow the target to emit any magic that it wants at the start of the file.
  171. EmitStartOfAsmFile(M);
  172. // Very minimal debug info. It is ignored if we emit actual debug info. If we
  173. // don't, this at least helps the user find where a global came from.
  174. if (MAI->hasSingleParameterDotFile()) {
  175. // .file "foo.c"
  176. OutStreamer->EmitFileDirective(M.getModuleIdentifier());
  177. }
  178. GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
  179. assert(MI && "AsmPrinter didn't require GCModuleInfo?");
  180. for (auto &I : *MI)
  181. if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
  182. MP->beginAssembly(M, *MI, *this);
  183. // Emit module-level inline asm if it exists.
  184. if (!M.getModuleInlineAsm().empty()) {
  185. // We're at the module level. Construct MCSubtarget from the default CPU
  186. // and target triple.
  187. std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
  188. TM.getTargetTriple().str(), TM.getTargetCPU(),
  189. TM.getTargetFeatureString()));
  190. OutStreamer->AddComment("Start of file scope inline assembly");
  191. OutStreamer->AddBlankLine();
  192. EmitInlineAsm(M.getModuleInlineAsm()+"\n", *STI, TM.Options.MCOptions);
  193. OutStreamer->AddComment("End of file scope inline assembly");
  194. OutStreamer->AddBlankLine();
  195. }
  196. if (MAI->doesSupportDebugInformation()) {
  197. bool skip_dwarf = false;
  198. if (TM.getTargetTriple().isKnownWindowsMSVCEnvironment()) {
  199. Handlers.push_back(HandlerInfo(new WinCodeViewLineTables(this),
  200. DbgTimerName,
  201. CodeViewLineTablesGroupName));
  202. // FIXME: Don't emit DWARF debug info if there's at least one function
  203. // with AddressSanitizer instrumentation.
  204. // This is a band-aid fix for PR22032.
  205. for (auto &F : M.functions()) {
  206. if (F.hasFnAttribute(Attribute::SanitizeAddress)) {
  207. skip_dwarf = true;
  208. break;
  209. }
  210. }
  211. }
  212. if (!skip_dwarf) {
  213. DD = new DwarfDebug(this, &M);
  214. Handlers.push_back(HandlerInfo(DD, DbgTimerName, DWARFGroupName));
  215. }
  216. }
  217. EHStreamer *ES = nullptr;
  218. switch (MAI->getExceptionHandlingType()) {
  219. case ExceptionHandling::None:
  220. break;
  221. case ExceptionHandling::SjLj:
  222. case ExceptionHandling::DwarfCFI:
  223. ES = new DwarfCFIException(this);
  224. break;
  225. case ExceptionHandling::ARM:
  226. ES = new ARMException(this);
  227. break;
  228. case ExceptionHandling::WinEH:
  229. switch (MAI->getWinEHEncodingType()) {
  230. default: llvm_unreachable("unsupported unwinding information encoding");
  231. case WinEH::EncodingType::Invalid:
  232. break;
  233. case WinEH::EncodingType::X86:
  234. case WinEH::EncodingType::Itanium:
  235. ES = new WinException(this);
  236. break;
  237. }
  238. break;
  239. }
  240. if (ES)
  241. Handlers.push_back(HandlerInfo(ES, EHTimerName, DWARFGroupName));
  242. return false;
  243. }
  244. static bool canBeHidden(const GlobalValue *GV, const MCAsmInfo &MAI) {
  245. if (!MAI.hasWeakDefCanBeHiddenDirective())
  246. return false;
  247. return canBeOmittedFromSymbolTable(GV);
  248. }
  249. void AsmPrinter::EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const {
  250. GlobalValue::LinkageTypes Linkage = GV->getLinkage();
  251. switch (Linkage) {
  252. case GlobalValue::CommonLinkage:
  253. case GlobalValue::LinkOnceAnyLinkage:
  254. case GlobalValue::LinkOnceODRLinkage:
  255. case GlobalValue::WeakAnyLinkage:
  256. case GlobalValue::WeakODRLinkage:
  257. if (MAI->hasWeakDefDirective()) {
  258. // .globl _foo
  259. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
  260. if (!canBeHidden(GV, *MAI))
  261. // .weak_definition _foo
  262. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_WeakDefinition);
  263. else
  264. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_WeakDefAutoPrivate);
  265. } else if (MAI->hasLinkOnceDirective()) {
  266. // .globl _foo
  267. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
  268. //NOTE: linkonce is handled by the section the symbol was assigned to.
  269. } else {
  270. // .weak _foo
  271. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Weak);
  272. }
  273. return;
  274. case GlobalValue::AppendingLinkage:
  275. // FIXME: appending linkage variables should go into a section of
  276. // their name or something. For now, just emit them as external.
  277. case GlobalValue::ExternalLinkage:
  278. // If external or appending, declare as a global symbol.
  279. // .globl _foo
  280. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
  281. return;
  282. case GlobalValue::PrivateLinkage:
  283. case GlobalValue::InternalLinkage:
  284. return;
  285. case GlobalValue::AvailableExternallyLinkage:
  286. llvm_unreachable("Should never emit this");
  287. case GlobalValue::ExternalWeakLinkage:
  288. llvm_unreachable("Don't know how to emit these");
  289. }
  290. llvm_unreachable("Unknown linkage type!");
  291. }
  292. void AsmPrinter::getNameWithPrefix(SmallVectorImpl<char> &Name,
  293. const GlobalValue *GV) const {
  294. TM.getNameWithPrefix(Name, GV, *Mang);
  295. }
  296. MCSymbol *AsmPrinter::getSymbol(const GlobalValue *GV) const {
  297. return TM.getSymbol(GV, *Mang);
  298. }
  299. /// EmitGlobalVariable - Emit the specified global variable to the .s file.
  300. void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
  301. if (GV->hasInitializer()) {
  302. // Check to see if this is a special global used by LLVM, if so, emit it.
  303. if (EmitSpecialLLVMGlobal(GV))
  304. return;
  305. // Skip the emission of global equivalents. The symbol can be emitted later
  306. // on by emitGlobalGOTEquivs in case it turns out to be needed.
  307. if (GlobalGOTEquivs.count(getSymbol(GV)))
  308. return;
  309. if (isVerbose()) {
  310. GV->printAsOperand(OutStreamer->GetCommentOS(),
  311. /*PrintType=*/false, GV->getParent());
  312. OutStreamer->GetCommentOS() << '\n';
  313. }
  314. }
  315. MCSymbol *GVSym = getSymbol(GV);
  316. EmitVisibility(GVSym, GV->getVisibility(), !GV->isDeclaration());
  317. if (!GV->hasInitializer()) // External globals require no extra code.
  318. return;
  319. GVSym->redefineIfPossible();
  320. if (GVSym->isDefined() || GVSym->isVariable())
  321. report_fatal_error("symbol '" + Twine(GVSym->getName()) +
  322. "' is already defined");
  323. if (MAI->hasDotTypeDotSizeDirective())
  324. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
  325. SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
  326. const DataLayout *DL = TM.getDataLayout();
  327. uint64_t Size = DL->getTypeAllocSize(GV->getType()->getElementType());
  328. // If the alignment is specified, we *must* obey it. Overaligning a global
  329. // with a specified alignment is a prompt way to break globals emitted to
  330. // sections and expected to be contiguous (e.g. ObjC metadata).
  331. unsigned AlignLog = getGVAlignmentLog2(GV, *DL);
  332. for (const HandlerInfo &HI : Handlers) {
  333. NamedRegionTimer T(HI.TimerName, HI.TimerGroupName, TimePassesIsEnabled);
  334. HI.Handler->setSymbolSize(GVSym, Size);
  335. }
  336. // Handle common and BSS local symbols (.lcomm).
  337. if (GVKind.isCommon() || GVKind.isBSSLocal()) {
  338. if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
  339. unsigned Align = 1 << AlignLog;
  340. // Handle common symbols.
  341. if (GVKind.isCommon()) {
  342. if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
  343. Align = 0;
  344. // .comm _foo, 42, 4
  345. OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
  346. return;
  347. }
  348. // Handle local BSS symbols.
  349. if (MAI->hasMachoZeroFillDirective()) {
  350. MCSection *TheSection =
  351. getObjFileLowering().SectionForGlobal(GV, GVKind, *Mang, TM);
  352. // .zerofill __DATA, __bss, _foo, 400, 5
  353. OutStreamer->EmitZerofill(TheSection, GVSym, Size, Align);
  354. return;
  355. }
  356. // Use .lcomm only if it supports user-specified alignment.
  357. // Otherwise, while it would still be correct to use .lcomm in some
  358. // cases (e.g. when Align == 1), the external assembler might enfore
  359. // some -unknown- default alignment behavior, which could cause
  360. // spurious differences between external and integrated assembler.
  361. // Prefer to simply fall back to .local / .comm in this case.
  362. if (MAI->getLCOMMDirectiveAlignmentType() != LCOMM::NoAlignment) {
  363. // .lcomm _foo, 42
  364. OutStreamer->EmitLocalCommonSymbol(GVSym, Size, Align);
  365. return;
  366. }
  367. if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
  368. Align = 0;
  369. // .local _foo
  370. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Local);
  371. // .comm _foo, 42, 4
  372. OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
  373. return;
  374. }
  375. MCSection *TheSection =
  376. getObjFileLowering().SectionForGlobal(GV, GVKind, *Mang, TM);
  377. // Handle the zerofill directive on darwin, which is a special form of BSS
  378. // emission.
  379. if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
  380. if (Size == 0) Size = 1; // zerofill of 0 bytes is undefined.
  381. // .globl _foo
  382. OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
  383. // .zerofill __DATA, __common, _foo, 400, 5
  384. OutStreamer->EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
  385. return;
  386. }
  387. // Handle thread local data for mach-o which requires us to output an
  388. // additional structure of data and mangle the original symbol so that we
  389. // can reference it later.
  390. //
  391. // TODO: This should become an "emit thread local global" method on TLOF.
  392. // All of this macho specific stuff should be sunk down into TLOFMachO and
  393. // stuff like "TLSExtraDataSection" should no longer be part of the parent
  394. // TLOF class. This will also make it more obvious that stuff like
  395. // MCStreamer::EmitTBSSSymbol is macho specific and only called from macho
  396. // specific code.
  397. if (GVKind.isThreadLocal() && MAI->hasMachoTBSSDirective()) {
  398. // Emit the .tbss symbol
  399. MCSymbol *MangSym =
  400. OutContext.getOrCreateSymbol(GVSym->getName() + Twine("$tlv$init"));
  401. if (GVKind.isThreadBSS()) {
  402. TheSection = getObjFileLowering().getTLSBSSSection();
  403. OutStreamer->EmitTBSSSymbol(TheSection, MangSym, Size, 1 << AlignLog);
  404. } else if (GVKind.isThreadData()) {
  405. OutStreamer->SwitchSection(TheSection);
  406. EmitAlignment(AlignLog, GV);
  407. OutStreamer->EmitLabel(MangSym);
  408. EmitGlobalConstant(GV->getInitializer());
  409. }
  410. OutStreamer->AddBlankLine();
  411. // Emit the variable struct for the runtime.
  412. MCSection *TLVSect = getObjFileLowering().getTLSExtraDataSection();
  413. OutStreamer->SwitchSection(TLVSect);
  414. // Emit the linkage here.
  415. EmitLinkage(GV, GVSym);
  416. OutStreamer->EmitLabel(GVSym);
  417. // Three pointers in size:
  418. // - __tlv_bootstrap - used to make sure support exists
  419. // - spare pointer, used when mapped by the runtime
  420. // - pointer to mangled symbol above with initializer
  421. unsigned PtrSize = DL->getPointerTypeSize(GV->getType());
  422. OutStreamer->EmitSymbolValue(GetExternalSymbolSymbol("_tlv_bootstrap"),
  423. PtrSize);
  424. OutStreamer->EmitIntValue(0, PtrSize);
  425. OutStreamer->EmitSymbolValue(MangSym, PtrSize);
  426. OutStreamer->AddBlankLine();
  427. return;
  428. }
  429. OutStreamer->SwitchSection(TheSection);
  430. EmitLinkage(GV, GVSym);
  431. EmitAlignment(AlignLog, GV);
  432. OutStreamer->EmitLabel(GVSym);
  433. EmitGlobalConstant(GV->getInitializer());
  434. if (MAI->hasDotTypeDotSizeDirective())
  435. // .size foo, 42
  436. OutStreamer->emitELFSize(cast<MCSymbolELF>(GVSym),
  437. MCConstantExpr::create(Size, OutContext));
  438. OutStreamer->AddBlankLine();
  439. }
  440. /// EmitFunctionHeader - This method emits the header for the current
  441. /// function.
  442. void AsmPrinter::EmitFunctionHeader() {
  443. // Print out constants referenced by the function
  444. EmitConstantPool();
  445. // Print the 'header' of function.
  446. const Function *F = MF->getFunction();
  447. OutStreamer->SwitchSection(
  448. getObjFileLowering().SectionForGlobal(F, *Mang, TM));
  449. EmitVisibility(CurrentFnSym, F->getVisibility());
  450. EmitLinkage(F, CurrentFnSym);
  451. if (MAI->hasFunctionAlignment())
  452. EmitAlignment(MF->getAlignment(), F);
  453. if (MAI->hasDotTypeDotSizeDirective())
  454. OutStreamer->EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
  455. if (isVerbose()) {
  456. F->printAsOperand(OutStreamer->GetCommentOS(),
  457. /*PrintType=*/false, F->getParent());
  458. OutStreamer->GetCommentOS() << '\n';
  459. }
  460. // Emit the prefix data.
  461. if (F->hasPrefixData())
  462. EmitGlobalConstant(F->getPrefixData());
  463. // Emit the CurrentFnSym. This is a virtual function to allow targets to
  464. // do their wild and crazy things as required.
  465. EmitFunctionEntryLabel();
  466. // If the function had address-taken blocks that got deleted, then we have
  467. // references to the dangling symbols. Emit them at the start of the function
  468. // so that we don't get references to undefined symbols.
  469. std::vector<MCSymbol*> DeadBlockSyms;
  470. MMI->takeDeletedSymbolsForFunction(F, DeadBlockSyms);
  471. for (unsigned i = 0, e = DeadBlockSyms.size(); i != e; ++i) {
  472. OutStreamer->AddComment("Address taken block that was later removed");
  473. OutStreamer->EmitLabel(DeadBlockSyms[i]);
  474. }
  475. if (CurrentFnBegin) {
  476. if (MAI->useAssignmentForEHBegin()) {
  477. MCSymbol *CurPos = OutContext.createTempSymbol();
  478. OutStreamer->EmitLabel(CurPos);
  479. OutStreamer->EmitAssignment(CurrentFnBegin,
  480. MCSymbolRefExpr::create(CurPos, OutContext));
  481. } else {
  482. OutStreamer->EmitLabel(CurrentFnBegin);
  483. }
  484. }
  485. // Emit pre-function debug and/or EH information.
  486. for (const HandlerInfo &HI : Handlers) {
  487. NamedRegionTimer T(HI.TimerName, HI.TimerGroupName, TimePassesIsEnabled);
  488. HI.Handler->beginFunction(MF);
  489. }
  490. // Emit the prologue data.
  491. if (F->hasPrologueData())
  492. EmitGlobalConstant(F->getPrologueData());
  493. }
  494. /// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the
  495. /// function. This can be overridden by targets as required to do custom stuff.
  496. void AsmPrinter::EmitFunctionEntryLabel() {
  497. CurrentFnSym->redefineIfPossible();
  498. // The function label could have already been emitted if two symbols end up
  499. // conflicting due to asm renaming. Detect this and emit an error.
  500. if (CurrentFnSym->isVariable())
  501. report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
  502. "' is a protected alias");
  503. if (CurrentFnSym->isDefined())
  504. report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
  505. "' label emitted multiple times to assembly file");
  506. return OutStreamer->EmitLabel(CurrentFnSym);
  507. }
  508. /// emitComments - Pretty-print comments for instructions.
  509. static void emitComments(const MachineInstr &MI, raw_ostream &CommentOS) {
  510. const MachineFunction *MF = MI.getParent()->getParent();
  511. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  512. // Check for spills and reloads
  513. int FI;
  514. const MachineFrameInfo *FrameInfo = MF->getFrameInfo();
  515. // We assume a single instruction only has a spill or reload, not
  516. // both.
  517. const MachineMemOperand *MMO;
  518. if (TII->isLoadFromStackSlotPostFE(&MI, FI)) {
  519. if (FrameInfo->isSpillSlotObjectIndex(FI)) {
  520. MMO = *MI.memoperands_begin();
  521. CommentOS << MMO->getSize() << "-byte Reload\n";
  522. }
  523. } else if (TII->hasLoadFromStackSlot(&MI, MMO, FI)) {
  524. if (FrameInfo->isSpillSlotObjectIndex(FI))
  525. CommentOS << MMO->getSize() << "-byte Folded Reload\n";
  526. } else if (TII->isStoreToStackSlotPostFE(&MI, FI)) {
  527. if (FrameInfo->isSpillSlotObjectIndex(FI)) {
  528. MMO = *MI.memoperands_begin();
  529. CommentOS << MMO->getSize() << "-byte Spill\n";
  530. }
  531. } else if (TII->hasStoreToStackSlot(&MI, MMO, FI)) {
  532. if (FrameInfo->isSpillSlotObjectIndex(FI))
  533. CommentOS << MMO->getSize() << "-byte Folded Spill\n";
  534. }
  535. // Check for spill-induced copies
  536. if (MI.getAsmPrinterFlag(MachineInstr::ReloadReuse))
  537. CommentOS << " Reload Reuse\n";
  538. }
  539. /// emitImplicitDef - This method emits the specified machine instruction
  540. /// that is an implicit def.
  541. void AsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
  542. unsigned RegNo = MI->getOperand(0).getReg();
  543. OutStreamer->AddComment(Twine("implicit-def: ") +
  544. MMI->getContext().getRegisterInfo()->getName(RegNo));
  545. OutStreamer->AddBlankLine();
  546. }
  547. static void emitKill(const MachineInstr *MI, AsmPrinter &AP) {
  548. std::string Str = "kill:";
  549. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  550. const MachineOperand &Op = MI->getOperand(i);
  551. assert(Op.isReg() && "KILL instruction must have only register operands");
  552. Str += ' ';
  553. Str += AP.MMI->getContext().getRegisterInfo()->getName(Op.getReg());
  554. Str += (Op.isDef() ? "<def>" : "<kill>");
  555. }
  556. AP.OutStreamer->AddComment(Str);
  557. AP.OutStreamer->AddBlankLine();
  558. }
  559. /// emitDebugValueComment - This method handles the target-independent form
  560. /// of DBG_VALUE, returning true if it was able to do so. A false return
  561. /// means the target will need to handle MI in EmitInstruction.
  562. static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
  563. // This code handles only the 4-operand target-independent form.
  564. if (MI->getNumOperands() != 4)
  565. return false;
  566. SmallString<128> Str;
  567. raw_svector_ostream OS(Str);
  568. OS << "DEBUG_VALUE: ";
  569. const DILocalVariable *V = MI->getDebugVariable();
  570. if (auto *SP = dyn_cast<DISubprogram>(V->getScope())) {
  571. StringRef Name = SP->getDisplayName();
  572. if (!Name.empty())
  573. OS << Name << ":";
  574. }
  575. OS << V->getName();
  576. const DIExpression *Expr = MI->getDebugExpression();
  577. if (Expr->isBitPiece())
  578. OS << " [bit_piece offset=" << Expr->getBitPieceOffset()
  579. << " size=" << Expr->getBitPieceSize() << "]";
  580. OS << " <- ";
  581. // The second operand is only an offset if it's an immediate.
  582. bool Deref = MI->getOperand(0).isReg() && MI->getOperand(1).isImm();
  583. int64_t Offset = Deref ? MI->getOperand(1).getImm() : 0;
  584. // Register or immediate value. Register 0 means undef.
  585. if (MI->getOperand(0).isFPImm()) {
  586. APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF());
  587. if (MI->getOperand(0).getFPImm()->getType()->isFloatTy()) {
  588. OS << (double)APF.convertToFloat();
  589. } else if (MI->getOperand(0).getFPImm()->getType()->isDoubleTy()) {
  590. OS << APF.convertToDouble();
  591. } else {
  592. // There is no good way to print long double. Convert a copy to
  593. // double. Ah well, it's only a comment.
  594. bool ignored;
  595. APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
  596. &ignored);
  597. OS << "(long double) " << APF.convertToDouble();
  598. }
  599. } else if (MI->getOperand(0).isImm()) {
  600. OS << MI->getOperand(0).getImm();
  601. } else if (MI->getOperand(0).isCImm()) {
  602. MI->getOperand(0).getCImm()->getValue().print(OS, false /*isSigned*/);
  603. } else {
  604. unsigned Reg;
  605. if (MI->getOperand(0).isReg()) {
  606. Reg = MI->getOperand(0).getReg();
  607. } else {
  608. assert(MI->getOperand(0).isFI() && "Unknown operand type");
  609. const TargetFrameLowering *TFI = AP.MF->getSubtarget().getFrameLowering();
  610. Offset += TFI->getFrameIndexReference(*AP.MF,
  611. MI->getOperand(0).getIndex(), Reg);
  612. Deref = true;
  613. }
  614. if (Reg == 0) {
  615. // Suppress offset, it is not meaningful here.
  616. OS << "undef";
  617. // NOTE: Want this comment at start of line, don't emit with AddComment.
  618. AP.OutStreamer->emitRawComment(OS.str());
  619. return true;
  620. }
  621. if (Deref)
  622. OS << '[';
  623. OS << AP.MMI->getContext().getRegisterInfo()->getName(Reg);
  624. }
  625. if (Deref)
  626. OS << '+' << Offset << ']';
  627. // NOTE: Want this comment at start of line, don't emit with AddComment.
  628. AP.OutStreamer->emitRawComment(OS.str());
  629. return true;
  630. }
  631. AsmPrinter::CFIMoveType AsmPrinter::needsCFIMoves() {
  632. if (MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI &&
  633. MF->getFunction()->needsUnwindTableEntry())
  634. return CFI_M_EH;
  635. if (MMI->hasDebugInfo())
  636. return CFI_M_Debug;
  637. return CFI_M_None;
  638. }
  639. bool AsmPrinter::needsSEHMoves() {
  640. return MAI->usesWindowsCFI() && MF->getFunction()->needsUnwindTableEntry();
  641. }
  642. void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) {
  643. ExceptionHandling ExceptionHandlingType = MAI->getExceptionHandlingType();
  644. if (ExceptionHandlingType != ExceptionHandling::DwarfCFI &&
  645. ExceptionHandlingType != ExceptionHandling::ARM)
  646. return;
  647. if (needsCFIMoves() == CFI_M_None)
  648. return;
  649. const MachineModuleInfo &MMI = MF->getMMI();
  650. const std::vector<MCCFIInstruction> &Instrs = MMI.getFrameInstructions();
  651. unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
  652. const MCCFIInstruction &CFI = Instrs[CFIIndex];
  653. emitCFIInstruction(CFI);
  654. }
  655. void AsmPrinter::emitFrameAlloc(const MachineInstr &MI) {
  656. // The operands are the MCSymbol and the frame offset of the allocation.
  657. MCSymbol *FrameAllocSym = MI.getOperand(0).getMCSymbol();
  658. int FrameOffset = MI.getOperand(1).getImm();
  659. // Emit a symbol assignment.
  660. OutStreamer->EmitAssignment(FrameAllocSym,
  661. MCConstantExpr::create(FrameOffset, OutContext));
  662. }
  663. /// EmitFunctionBody - This method emits the body and trailer for a
  664. /// function.
  665. void AsmPrinter::EmitFunctionBody() {
  666. EmitFunctionHeader();
  667. // Emit target-specific gunk before the function body.
  668. EmitFunctionBodyStart();
  669. bool ShouldPrintDebugScopes = MMI->hasDebugInfo();
  670. // Print out code for the function.
  671. bool HasAnyRealCode = false;
  672. for (auto &MBB : *MF) {
  673. // Print a label for the basic block.
  674. EmitBasicBlockStart(MBB);
  675. for (auto &MI : MBB) {
  676. // Print the assembly for the instruction.
  677. if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
  678. !MI.isDebugValue()) {
  679. HasAnyRealCode = true;
  680. ++EmittedInsts;
  681. }
  682. if (ShouldPrintDebugScopes) {
  683. for (const HandlerInfo &HI : Handlers) {
  684. NamedRegionTimer T(HI.TimerName, HI.TimerGroupName,
  685. TimePassesIsEnabled);
  686. HI.Handler->beginInstruction(&MI);
  687. }
  688. }
  689. if (isVerbose())
  690. emitComments(MI, OutStreamer->GetCommentOS());
  691. switch (MI.getOpcode()) {
  692. case TargetOpcode::CFI_INSTRUCTION:
  693. emitCFIInstruction(MI);
  694. break;
  695. case TargetOpcode::LOCAL_ESCAPE:
  696. emitFrameAlloc(MI);
  697. break;
  698. case TargetOpcode::EH_LABEL:
  699. case TargetOpcode::GC_LABEL:
  700. OutStreamer->EmitLabel(MI.getOperand(0).getMCSymbol());
  701. break;
  702. case TargetOpcode::INLINEASM:
  703. EmitInlineAsm(&MI);
  704. break;
  705. case TargetOpcode::DBG_VALUE:
  706. if (isVerbose()) {
  707. if (!emitDebugValueComment(&MI, *this))
  708. EmitInstruction(&MI);
  709. }
  710. break;
  711. case TargetOpcode::IMPLICIT_DEF:
  712. if (isVerbose()) emitImplicitDef(&MI);
  713. break;
  714. case TargetOpcode::KILL:
  715. if (isVerbose()) emitKill(&MI, *this);
  716. break;
  717. default:
  718. EmitInstruction(&MI);
  719. break;
  720. }
  721. if (ShouldPrintDebugScopes) {
  722. for (const HandlerInfo &HI : Handlers) {
  723. NamedRegionTimer T(HI.TimerName, HI.TimerGroupName,
  724. TimePassesIsEnabled);
  725. HI.Handler->endInstruction();
  726. }
  727. }
  728. }
  729. EmitBasicBlockEnd(MBB);
  730. }
  731. // If the function is empty and the object file uses .subsections_via_symbols,
  732. // then we need to emit *something* to the function body to prevent the
  733. // labels from collapsing together. Just emit a noop.
  734. if ((MAI->hasSubsectionsViaSymbols() && !HasAnyRealCode)) {
  735. MCInst Noop;
  736. MF->getSubtarget().getInstrInfo()->getNoopForMachoTarget(Noop);
  737. OutStreamer->AddComment("avoids zero-length function");
  738. // Targets can opt-out of emitting the noop here by leaving the opcode
  739. // unspecified.
  740. if (Noop.getOpcode())
  741. OutStreamer->EmitInstruction(Noop, getSubtargetInfo());
  742. }
  743. const Function *F = MF->getFunction();
  744. for (const auto &BB : *F) {
  745. if (!BB.hasAddressTaken())
  746. continue;
  747. MCSymbol *Sym = GetBlockAddressSymbol(&BB);
  748. if (Sym->isDefined())
  749. continue;
  750. OutStreamer->AddComment("Address of block that was removed by CodeGen");
  751. OutStreamer->EmitLabel(Sym);
  752. }
  753. // Emit target-specific gunk after the function body.
  754. EmitFunctionBodyEnd();
  755. if (!MMI->getLandingPads().empty() || MMI->hasDebugInfo() ||
  756. MAI->hasDotTypeDotSizeDirective()) {
  757. // Create a symbol for the end of function.
  758. CurrentFnEnd = createTempSymbol("func_end");
  759. OutStreamer->EmitLabel(CurrentFnEnd);
  760. }
  761. // If the target wants a .size directive for the size of the function, emit
  762. // it.
  763. if (MAI->hasDotTypeDotSizeDirective()) {
  764. // We can get the size as difference between the function label and the
  765. // temp label.
  766. const MCExpr *SizeExp = MCBinaryExpr::createSub(
  767. MCSymbolRefExpr::create(CurrentFnEnd, OutContext),
  768. MCSymbolRefExpr::create(CurrentFnSymForSize, OutContext), OutContext);
  769. if (auto Sym = dyn_cast<MCSymbolELF>(CurrentFnSym))
  770. OutStreamer->emitELFSize(Sym, SizeExp);
  771. }
  772. for (const HandlerInfo &HI : Handlers) {
  773. NamedRegionTimer T(HI.TimerName, HI.TimerGroupName, TimePassesIsEnabled);
  774. HI.Handler->markFunctionEnd();
  775. }
  776. // Print out jump tables referenced by the function.
  777. EmitJumpTableInfo();
  778. // Emit post-function debug and/or EH information.
  779. for (const HandlerInfo &HI : Handlers) {
  780. NamedRegionTimer T(HI.TimerName, HI.TimerGroupName, TimePassesIsEnabled);
  781. HI.Handler->endFunction(MF);
  782. }
  783. MMI->EndFunction();
  784. OutStreamer->AddBlankLine();
  785. }
  786. /// \brief Compute the number of Global Variables that uses a Constant.
  787. static unsigned getNumGlobalVariableUses(const Constant *C) {
  788. if (!C)
  789. return 0;
  790. if (isa<GlobalVariable>(C))
  791. return 1;
  792. unsigned NumUses = 0;
  793. for (auto *CU : C->users())
  794. NumUses += getNumGlobalVariableUses(dyn_cast<Constant>(CU));
  795. return NumUses;
  796. }
  797. /// \brief Only consider global GOT equivalents if at least one user is a
  798. /// cstexpr inside an initializer of another global variables. Also, don't
  799. /// handle cstexpr inside instructions. During global variable emission,
  800. /// candidates are skipped and are emitted later in case at least one cstexpr
  801. /// isn't replaced by a PC relative GOT entry access.
  802. static bool isGOTEquivalentCandidate(const GlobalVariable *GV,
  803. unsigned &NumGOTEquivUsers) {
  804. // Global GOT equivalents are unnamed private globals with a constant
  805. // pointer initializer to another global symbol. They must point to a
  806. // GlobalVariable or Function, i.e., as GlobalValue.
  807. if (!GV->hasUnnamedAddr() || !GV->hasInitializer() || !GV->isConstant() ||
  808. !GV->isDiscardableIfUnused() || !dyn_cast<GlobalValue>(GV->getOperand(0)))
  809. return false;
  810. // To be a got equivalent, at least one of its users need to be a constant
  811. // expression used by another global variable.
  812. for (auto *U : GV->users())
  813. NumGOTEquivUsers += getNumGlobalVariableUses(dyn_cast<Constant>(U));
  814. return NumGOTEquivUsers > 0;
  815. }
  816. /// \brief Unnamed constant global variables solely contaning a pointer to
  817. /// another globals variable is equivalent to a GOT table entry; it contains the
  818. /// the address of another symbol. Optimize it and replace accesses to these
  819. /// "GOT equivalents" by using the GOT entry for the final global instead.
  820. /// Compute GOT equivalent candidates among all global variables to avoid
  821. /// emitting them if possible later on, after it use is replaced by a GOT entry
  822. /// access.
  823. void AsmPrinter::computeGlobalGOTEquivs(Module &M) {
  824. if (!getObjFileLowering().supportIndirectSymViaGOTPCRel())
  825. return;
  826. for (const auto &G : M.globals()) {
  827. unsigned NumGOTEquivUsers = 0;
  828. if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers))
  829. continue;
  830. const MCSymbol *GOTEquivSym = getSymbol(&G);
  831. GlobalGOTEquivs[GOTEquivSym] = std::make_pair(&G, NumGOTEquivUsers);
  832. }
  833. }
  834. /// \brief Constant expressions using GOT equivalent globals may not be eligible
  835. /// for PC relative GOT entry conversion, in such cases we need to emit such
  836. /// globals we previously omitted in EmitGlobalVariable.
  837. void AsmPrinter::emitGlobalGOTEquivs() {
  838. if (!getObjFileLowering().supportIndirectSymViaGOTPCRel())
  839. return;
  840. SmallVector<const GlobalVariable *, 8> FailedCandidates;
  841. for (auto &I : GlobalGOTEquivs) {
  842. const GlobalVariable *GV = I.second.first;
  843. unsigned Cnt = I.second.second;
  844. if (Cnt)
  845. FailedCandidates.push_back(GV);
  846. }
  847. GlobalGOTEquivs.clear();
  848. for (auto *GV : FailedCandidates)
  849. EmitGlobalVariable(GV);
  850. }
  851. bool AsmPrinter::doFinalization(Module &M) {
  852. // Set the MachineFunction to nullptr so that we can catch attempted
  853. // accesses to MF specific features at the module level and so that
  854. // we can conditionalize accesses based on whether or not it is nullptr.
  855. MF = nullptr;
  856. // Gather all GOT equivalent globals in the module. We really need two
  857. // passes over the globals: one to compute and another to avoid its emission
  858. // in EmitGlobalVariable, otherwise we would not be able to handle cases
  859. // where the got equivalent shows up before its use.
  860. computeGlobalGOTEquivs(M);
  861. // Emit global variables.
  862. for (const auto &G : M.globals())
  863. EmitGlobalVariable(&G);
  864. // Emit remaining GOT equivalent globals.
  865. emitGlobalGOTEquivs();
  866. // Emit visibility info for declarations
  867. for (const Function &F : M) {
  868. if (!F.isDeclarationForLinker())
  869. continue;
  870. GlobalValue::VisibilityTypes V = F.getVisibility();
  871. if (V == GlobalValue::DefaultVisibility)
  872. continue;
  873. MCSymbol *Name = getSymbol(&F);
  874. EmitVisibility(Name, V, false);
  875. }
  876. const TargetLoweringObjectFile &TLOF = getObjFileLowering();
  877. // Emit module flags.
  878. SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
  879. M.getModuleFlagsMetadata(ModuleFlags);
  880. if (!ModuleFlags.empty())
  881. TLOF.emitModuleFlags(*OutStreamer, ModuleFlags, *Mang, TM);
  882. if (TM.getTargetTriple().isOSBinFormatELF()) {
  883. MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
  884. // Output stubs for external and common global variables.
  885. MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
  886. if (!Stubs.empty()) {
  887. OutStreamer->SwitchSection(TLOF.getDataRelSection());
  888. const DataLayout *DL = TM.getDataLayout();
  889. for (const auto &Stub : Stubs) {
  890. OutStreamer->EmitLabel(Stub.first);
  891. OutStreamer->EmitSymbolValue(Stub.second.getPointer(),
  892. DL->getPointerSize());
  893. }
  894. }
  895. }
  896. // Make sure we wrote out everything we need.
  897. OutStreamer->Flush();
  898. // Finalize debug and EH information.
  899. for (const HandlerInfo &HI : Handlers) {
  900. NamedRegionTimer T(HI.TimerName, HI.TimerGroupName,
  901. TimePassesIsEnabled);
  902. HI.Handler->endModule();
  903. delete HI.Handler;
  904. }
  905. Handlers.clear();
  906. DD = nullptr;
  907. // If the target wants to know about weak references, print them all.
  908. if (MAI->getWeakRefDirective()) {
  909. // FIXME: This is not lazy, it would be nice to only print weak references
  910. // to stuff that is actually used. Note that doing so would require targets
  911. // to notice uses in operands (due to constant exprs etc). This should
  912. // happen with the MC stuff eventually.
  913. // Print out module-level global variables here.
  914. for (const auto &G : M.globals()) {
  915. if (!G.hasExternalWeakLinkage())
  916. continue;
  917. OutStreamer->EmitSymbolAttribute(getSymbol(&G), MCSA_WeakReference);
  918. }
  919. for (const auto &F : M) {
  920. if (!F.hasExternalWeakLinkage())
  921. continue;
  922. OutStreamer->EmitSymbolAttribute(getSymbol(&F), MCSA_WeakReference);
  923. }
  924. }
  925. OutStreamer->AddBlankLine();
  926. for (const auto &Alias : M.aliases()) {
  927. MCSymbol *Name = getSymbol(&Alias);
  928. if (Alias.hasExternalLinkage() || !MAI->getWeakRefDirective())
  929. OutStreamer->EmitSymbolAttribute(Name, MCSA_Global);
  930. else if (Alias.hasWeakLinkage() || Alias.hasLinkOnceLinkage())
  931. OutStreamer->EmitSymbolAttribute(Name, MCSA_WeakReference);
  932. else
  933. assert(Alias.hasLocalLinkage() && "Invalid alias linkage");
  934. EmitVisibility(Name, Alias.getVisibility());
  935. // Emit the directives as assignments aka .set:
  936. OutStreamer->EmitAssignment(Name, lowerConstant(Alias.getAliasee()));
  937. }
  938. GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
  939. assert(MI && "AsmPrinter didn't require GCModuleInfo?");
  940. for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
  941. if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(**--I))
  942. MP->finishAssembly(M, *MI, *this);
  943. // Emit llvm.ident metadata in an '.ident' directive.
  944. EmitModuleIdents(M);
  945. // Emit __morestack address if needed for indirect calls.
  946. if (MMI->usesMorestackAddr()) {
  947. MCSection *ReadOnlySection =
  948. getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly(),
  949. /*C=*/nullptr);
  950. OutStreamer->SwitchSection(ReadOnlySection);
  951. MCSymbol *AddrSymbol =
  952. OutContext.getOrCreateSymbol(StringRef("__morestack_addr"));
  953. OutStreamer->EmitLabel(AddrSymbol);
  954. unsigned PtrSize = TM.getDataLayout()->getPointerSize(0);
  955. OutStreamer->EmitSymbolValue(GetExternalSymbolSymbol("__morestack"),
  956. PtrSize);
  957. }
  958. // If we don't have any trampolines, then we don't require stack memory
  959. // to be executable. Some targets have a directive to declare this.
  960. Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
  961. if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
  962. if (MCSection *S = MAI->getNonexecutableStackSection(OutContext))
  963. OutStreamer->SwitchSection(S);
  964. // Allow the target to emit any magic that it wants at the end of the file,
  965. // after everything else has gone out.
  966. EmitEndOfAsmFile(M);
  967. delete Mang; Mang = nullptr;
  968. MMI = nullptr;
  969. OutStreamer->Finish();
  970. OutStreamer->reset();
  971. return false;
  972. }
  973. MCSymbol *AsmPrinter::getCurExceptionSym() {
  974. if (!CurExceptionSym)
  975. CurExceptionSym = createTempSymbol("exception");
  976. return CurExceptionSym;
  977. }
  978. void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
  979. this->MF = &MF;
  980. // Get the function symbol.
  981. CurrentFnSym = getSymbol(MF.getFunction());
  982. CurrentFnSymForSize = CurrentFnSym;
  983. CurrentFnBegin = nullptr;
  984. CurExceptionSym = nullptr;
  985. bool NeedsLocalForSize = MAI->needsLocalForSize();
  986. if (!MMI->getLandingPads().empty() || MMI->hasDebugInfo() ||
  987. NeedsLocalForSize) {
  988. CurrentFnBegin = createTempSymbol("func_begin");
  989. if (NeedsLocalForSize)
  990. CurrentFnSymForSize = CurrentFnBegin;
  991. }
  992. if (isVerbose())
  993. LI = &getAnalysis<MachineLoopInfo>();
  994. }
  995. namespace {
  996. // Keep track the alignment, constpool entries per Section.
  997. struct SectionCPs {
  998. MCSection *S;
  999. unsigned Alignment;
  1000. SmallVector<unsigned, 4> CPEs;
  1001. SectionCPs(MCSection *s, unsigned a) : S(s), Alignment(a) {}
  1002. };
  1003. }
  1004. /// EmitConstantPool - Print to the current output stream assembly
  1005. /// representations of the constants in the constant pool MCP. This is
  1006. /// used to print out constants which have been "spilled to memory" by
  1007. /// the code generator.
  1008. ///
  1009. void AsmPrinter::EmitConstantPool() {
  1010. const MachineConstantPool *MCP = MF->getConstantPool();
  1011. const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
  1012. if (CP.empty()) return;
  1013. // Calculate sections for constant pool entries. We collect entries to go into
  1014. // the same section together to reduce amount of section switch statements.
  1015. SmallVector<SectionCPs, 4> CPSections;
  1016. for (unsigned i = 0, e = CP.size(); i != e; ++i) {
  1017. const MachineConstantPoolEntry &CPE = CP[i];
  1018. unsigned Align = CPE.getAlignment();
  1019. SectionKind Kind =
  1020. CPE.getSectionKind(TM.getDataLayout());
  1021. const Constant *C = nullptr;
  1022. if (!CPE.isMachineConstantPoolEntry())
  1023. C = CPE.Val.ConstVal;
  1024. MCSection *S = getObjFileLowering().getSectionForConstant(Kind, C);
  1025. // The number of sections are small, just do a linear search from the
  1026. // last section to the first.
  1027. bool Found = false;
  1028. unsigned SecIdx = CPSections.size();
  1029. while (SecIdx != 0) {
  1030. if (CPSections[--SecIdx].S == S) {
  1031. Found = true;
  1032. break;
  1033. }
  1034. }
  1035. if (!Found) {
  1036. SecIdx = CPSections.size();
  1037. CPSections.push_back(SectionCPs(S, Align));
  1038. }
  1039. if (Align > CPSections[SecIdx].Alignment)
  1040. CPSections[SecIdx].Alignment = Align;
  1041. CPSections[SecIdx].CPEs.push_back(i);
  1042. }
  1043. // Now print stuff into the calculated sections.
  1044. const MCSection *CurSection = nullptr;
  1045. unsigned Offset = 0;
  1046. for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
  1047. for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
  1048. unsigned CPI = CPSections[i].CPEs[j];
  1049. MCSymbol *Sym = GetCPISymbol(CPI);
  1050. if (!Sym->isUndefined())
  1051. continue;
  1052. if (CurSection != CPSections[i].S) {
  1053. OutStreamer->SwitchSection(CPSections[i].S);
  1054. EmitAlignment(Log2_32(CPSections[i].Alignment));
  1055. CurSection = CPSections[i].S;
  1056. Offset = 0;
  1057. }
  1058. MachineConstantPoolEntry CPE = CP[CPI];
  1059. // Emit inter-object padding for alignment.
  1060. unsigned AlignMask = CPE.getAlignment() - 1;
  1061. unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
  1062. OutStreamer->EmitZeros(NewOffset - Offset);
  1063. Type *Ty = CPE.getType();
  1064. Offset = NewOffset +
  1065. TM.getDataLayout()->getTypeAllocSize(Ty);
  1066. OutStreamer->EmitLabel(Sym);
  1067. if (CPE.isMachineConstantPoolEntry())
  1068. EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
  1069. else
  1070. EmitGlobalConstant(CPE.Val.ConstVal);
  1071. }
  1072. }
  1073. }
  1074. /// EmitJumpTableInfo - Print assembly representations of the jump tables used
  1075. /// by the current function to the current output stream.
  1076. ///
  1077. void AsmPrinter::EmitJumpTableInfo() {
  1078. const DataLayout *DL = MF->getTarget().getDataLayout();
  1079. const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
  1080. if (!MJTI) return;
  1081. if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return;
  1082. const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
  1083. if (JT.empty()) return;
  1084. // Pick the directive to use to print the jump table entries, and switch to
  1085. // the appropriate section.
  1086. const Function *F = MF->getFunction();
  1087. const TargetLoweringObjectFile &TLOF = getObjFileLowering();
  1088. bool JTInDiffSection = !TLOF.shouldPutJumpTableInFunctionSection(
  1089. MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32,
  1090. *F);
  1091. if (JTInDiffSection) {
  1092. // Drop it in the readonly section.
  1093. MCSection *ReadOnlySection = TLOF.getSectionForJumpTable(*F, *Mang, TM);
  1094. OutStreamer->SwitchSection(ReadOnlySection);
  1095. }
  1096. EmitAlignment(Log2_32(
  1097. MJTI->getEntryAlignment(*TM.getDataLayout())));
  1098. // Jump tables in code sections are marked with a data_region directive
  1099. // where that's supported.
  1100. if (!JTInDiffSection)
  1101. OutStreamer->EmitDataRegion(MCDR_DataRegionJT32);
  1102. for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) {
  1103. const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
  1104. // If this jump table was deleted, ignore it.
  1105. if (JTBBs.empty()) continue;
  1106. // For the EK_LabelDifference32 entry, if using .set avoids a relocation,
  1107. /// emit a .set directive for each unique entry.
  1108. if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 &&
  1109. MAI->doesSetDirectiveSuppressesReloc()) {
  1110. SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets;
  1111. const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
  1112. const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext);
  1113. for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
  1114. const MachineBasicBlock *MBB = JTBBs[ii];
  1115. if (!EmittedSets.insert(MBB).second)
  1116. continue;
  1117. // .set LJTSet, LBB32-base
  1118. const MCExpr *LHS =
  1119. MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
  1120. OutStreamer->EmitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()),
  1121. MCBinaryExpr::createSub(LHS, Base,
  1122. OutContext));
  1123. }
  1124. }
  1125. // On some targets (e.g. Darwin) we want to emit two consecutive labels
  1126. // before each jump table. The first label is never referenced, but tells
  1127. // the assembler and linker the extents of the jump table object. The
  1128. // second label is actually referenced by the code.
  1129. if (JTInDiffSection && DL->hasLinkerPrivateGlobalPrefix())
  1130. // FIXME: This doesn't have to have any specific name, just any randomly
  1131. // named and numbered 'l' label would work. Simplify GetJTISymbol.
  1132. OutStreamer->EmitLabel(GetJTISymbol(JTI, true));
  1133. OutStreamer->EmitLabel(GetJTISymbol(JTI));
  1134. for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
  1135. EmitJumpTableEntry(MJTI, JTBBs[ii], JTI);
  1136. }
  1137. if (!JTInDiffSection)
  1138. OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
  1139. }
  1140. /// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the
  1141. /// current stream.
  1142. void AsmPrinter::EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
  1143. const MachineBasicBlock *MBB,
  1144. unsigned UID) const {
  1145. assert(MBB && MBB->getNumber() >= 0 && "Invalid basic block");
  1146. const MCExpr *Value = nullptr;
  1147. switch (MJTI->getEntryKind()) {
  1148. case MachineJumpTableInfo::EK_Inline:
  1149. llvm_unreachable("Cannot emit EK_Inline jump table entry");
  1150. case MachineJumpTableInfo::EK_Custom32:
  1151. Value = MF->getSubtarget().getTargetLowering()->LowerCustomJumpTableEntry(
  1152. MJTI, MBB, UID, OutContext);
  1153. break;
  1154. case MachineJumpTableInfo::EK_BlockAddress:
  1155. // EK_BlockAddress - Each entry is a plain address of block, e.g.:
  1156. // .word LBB123
  1157. Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
  1158. break;
  1159. case MachineJumpTableInfo::EK_GPRel32BlockAddress: {
  1160. // EK_GPRel32BlockAddress - Each entry is an address of block, encoded
  1161. // with a relocation as gp-relative, e.g.:
  1162. // .gprel32 LBB123
  1163. MCSymbol *MBBSym = MBB->getSymbol();
  1164. OutStreamer->EmitGPRel32Value(MCSymbolRefExpr::create(MBBSym, OutContext));
  1165. return;
  1166. }
  1167. case MachineJumpTableInfo::EK_GPRel64BlockAddress: {
  1168. // EK_GPRel64BlockAddress - Each entry is an address of block, encoded
  1169. // with a relocation as gp-relative, e.g.:
  1170. // .gpdword LBB123
  1171. MCSymbol *MBBSym = MBB->getSymbol();
  1172. OutStreamer->EmitGPRel64Value(MCSymbolRefExpr::create(MBBSym, OutContext));
  1173. return;
  1174. }
  1175. case MachineJumpTableInfo::EK_LabelDifference32: {
  1176. // Each entry is the address of the block minus the address of the jump
  1177. // table. This is used for PIC jump tables where gprel32 is not supported.
  1178. // e.g.:
  1179. // .word LBB123 - LJTI1_2
  1180. // If the .set directive avoids relocations, this is emitted as:
  1181. // .set L4_5_set_123, LBB123 - LJTI1_2
  1182. // .word L4_5_set_123
  1183. if (MAI->doesSetDirectiveSuppressesReloc()) {
  1184. Value = MCSymbolRefExpr::create(GetJTSetSymbol(UID, MBB->getNumber()),
  1185. OutContext);
  1186. break;
  1187. }
  1188. Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
  1189. const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
  1190. const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF, UID, OutContext);
  1191. Value = MCBinaryExpr::createSub(Value, Base, OutContext);
  1192. break;
  1193. }
  1194. }
  1195. assert(Value && "Unknown entry kind!");
  1196. unsigned EntrySize =
  1197. MJTI->getEntrySize(*TM.getDataLayout());
  1198. OutStreamer->EmitValue(Value, EntrySize);
  1199. }
  1200. /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
  1201. /// special global used by LLVM. If so, emit it and return true, otherwise
  1202. /// do nothing and return false.
  1203. bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
  1204. if (GV->getName() == "llvm.used") {
  1205. if (MAI->hasNoDeadStrip()) // No need to emit this at all.
  1206. EmitLLVMUsedList(cast<ConstantArray>(GV->getInitializer()));
  1207. return true;
  1208. }
  1209. // Ignore debug and non-emitted data. This handles llvm.compiler.used.
  1210. if (StringRef(GV->getSection()) == "llvm.metadata" ||
  1211. GV->hasAvailableExternallyLinkage())
  1212. return true;
  1213. if (!GV->hasAppendingLinkage()) return false;
  1214. assert(GV->hasInitializer() && "Not a special LLVM global!");
  1215. if (GV->getName() == "llvm.global_ctors") {
  1216. EmitXXStructorList(GV->getInitializer(), /* isCtor */ true);
  1217. if (TM.getRelocationModel() == Reloc::Static &&
  1218. MAI->hasStaticCtorDtorReferenceInStaticMode()) {
  1219. StringRef Sym(".constructors_used");
  1220. OutStreamer->EmitSymbolAttribute(OutContext.getOrCreateSymbol(Sym),
  1221. MCSA_Reference);
  1222. }
  1223. return true;
  1224. }
  1225. if (GV->getName() == "llvm.global_dtors") {
  1226. EmitXXStructorList(GV->getInitializer(), /* isCtor */ false);
  1227. if (TM.getRelocationModel() == Reloc::Static &&
  1228. MAI->hasStaticCtorDtorReferenceInStaticMode()) {
  1229. StringRef Sym(".destructors_used");
  1230. OutStreamer->EmitSymbolAttribute(OutContext.getOrCreateSymbol(Sym),
  1231. MCSA_Reference);
  1232. }
  1233. return true;
  1234. }
  1235. return false;
  1236. }
  1237. /// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
  1238. /// global in the specified llvm.used list for which emitUsedDirectiveFor
  1239. /// is true, as being used with this directive.
  1240. void AsmPrinter::EmitLLVMUsedList(const ConstantArray *InitList) {
  1241. // Should be an array of 'i8*'.
  1242. for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
  1243. const GlobalValue *GV =
  1244. dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
  1245. if (GV)
  1246. OutStreamer->EmitSymbolAttribute(getSymbol(GV), MCSA_NoDeadStrip);
  1247. }
  1248. }
  1249. namespace {
  1250. struct Structor {
  1251. Structor() : Priority(0), Func(nullptr), ComdatKey(nullptr) {}
  1252. int Priority;
  1253. llvm::Constant *Func;
  1254. llvm::GlobalValue *ComdatKey;
  1255. };
  1256. } // end namespace
  1257. /// EmitXXStructorList - Emit the ctor or dtor list taking into account the init
  1258. /// priority.
  1259. void AsmPrinter::EmitXXStructorList(const Constant *List, bool isCtor) {
  1260. // Should be an array of '{ int, void ()* }' structs. The first value is the
  1261. // init priority.
  1262. if (!isa<ConstantArray>(List)) return;
  1263. // Sanity check the structors list.
  1264. const ConstantArray *InitList = dyn_cast<ConstantArray>(List);
  1265. if (!InitList) return; // Not an array!
  1266. StructType *ETy = dyn_cast<StructType>(InitList->getType()->getElementType());
  1267. // FIXME: Only allow the 3-field form in LLVM 4.0.
  1268. if (!ETy || ETy->getNumElements() < 2 || ETy->getNumElements() > 3)
  1269. return; // Not an array of two or three elements!
  1270. if (!isa<IntegerType>(ETy->getTypeAtIndex(0U)) ||
  1271. !isa<PointerType>(ETy->getTypeAtIndex(1U))) return; // Not (int, ptr).
  1272. if (ETy->getNumElements() == 3 && !isa<PointerType>(ETy->getTypeAtIndex(2U)))
  1273. return; // Not (int, ptr, ptr).
  1274. // Gather the structors in a form that's convenient for sorting by priority.
  1275. SmallVector<Structor, 8> Structors;
  1276. for (Value *O : InitList->operands()) {
  1277. ConstantStruct *CS = dyn_cast<ConstantStruct>(O);
  1278. if (!CS) continue; // Malformed.
  1279. if (CS->getOperand(1)->isNullValue())
  1280. break; // Found a null terminator, skip the rest.
  1281. ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
  1282. if (!Priority) continue; // Malformed.
  1283. Structors.push_back(Structor());
  1284. Structor &S = Structors.back();
  1285. S.Priority = Priority->getLimitedValue(65535);
  1286. S.Func = CS->getOperand(1);
  1287. if (ETy->getNumElements() == 3 && !CS->getOperand(2)->isNullValue())
  1288. S.ComdatKey = dyn_cast<GlobalValue>(CS->getOperand(2)->stripPointerCasts());
  1289. }
  1290. // Emit the function pointers in the target-specific order
  1291. const DataLayout *DL = TM.getDataLayout();
  1292. unsigned Align = Log2_32(DL->getPointerPrefAlignment());
  1293. std::stable_sort(Structors.begin(), Structors.end(),
  1294. [](const Structor &L,
  1295. const Structor &R) { return L.Priority < R.Priority; });
  1296. for (Structor &S : Structors) {
  1297. const TargetLoweringObjectFile &Obj = getObjFileLowering();
  1298. const MCSymbol *KeySym = nullptr;
  1299. if (GlobalValue *GV = S.ComdatKey) {
  1300. if (GV->hasAvailableExternallyLinkage())
  1301. // If the associated variable is available_externally, some other TU
  1302. // will provide its dynamic initializer.
  1303. continue;
  1304. KeySym = getSymbol(GV);
  1305. }
  1306. MCSection *OutputSection =
  1307. (isCtor ? Obj.getStaticCtorSection(S.Priority, KeySym)
  1308. : Obj.getStaticDtorSection(S.Priority, KeySym));
  1309. OutStreamer->SwitchSection(OutputSection);
  1310. if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
  1311. EmitAlignment(Align);
  1312. EmitXXStructor(S.Func);
  1313. }
  1314. }
  1315. void AsmPrinter::EmitModuleIdents(Module &M) {
  1316. if (!MAI->hasIdentDirective())
  1317. return;
  1318. if (const NamedMDNode *NMD = M.getNamedMetadata("llvm.ident")) {
  1319. for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
  1320. const MDNode *N = NMD->getOperand(i);
  1321. assert(N->getNumOperands() == 1 &&
  1322. "llvm.ident metadata entry can have only one operand");
  1323. const MDString *S = cast<MDString>(N->getOperand(0));
  1324. OutStreamer->EmitIdent(S->getString());
  1325. }
  1326. }
  1327. }
  1328. //===--------------------------------------------------------------------===//
  1329. // Emission and print routines
  1330. //
  1331. /// EmitInt8 - Emit a byte directive and value.
  1332. ///
  1333. void AsmPrinter::EmitInt8(int Value) const {
  1334. OutStreamer->EmitIntValue(Value, 1);
  1335. }
  1336. /// EmitInt16 - Emit a short directive and value.
  1337. ///
  1338. void AsmPrinter::EmitInt16(int Value) const {
  1339. OutStreamer->EmitIntValue(Value, 2);
  1340. }
  1341. /// EmitInt32 - Emit a long directive and value.
  1342. ///
  1343. void AsmPrinter::EmitInt32(int Value) const {
  1344. OutStreamer->EmitIntValue(Value, 4);
  1345. }
  1346. /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
  1347. /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
  1348. /// .set if it avoids relocations.
  1349. void AsmPrinter::EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
  1350. unsigned Size) const {
  1351. OutStreamer->emitAbsoluteSymbolDiff(Hi, Lo, Size);
  1352. }
  1353. /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
  1354. /// where the size in bytes of the directive is specified by Size and Label
  1355. /// specifies the label. This implicitly uses .set if it is available.
  1356. void AsmPrinter::EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
  1357. unsigned Size,
  1358. bool IsSectionRelative) const {
  1359. if (MAI->needsDwarfSectionOffsetDirective() && IsSectionRelative) {
  1360. OutStreamer->EmitCOFFSecRel32(Label);
  1361. return;
  1362. }
  1363. // Emit Label+Offset (or just Label if Offset is zero)
  1364. const MCExpr *Expr = MCSymbolRefExpr::create(Label, OutContext);
  1365. if (Offset)
  1366. Expr = MCBinaryExpr::createAdd(
  1367. Expr, MCConstantExpr::create(Offset, OutContext), OutContext);
  1368. OutStreamer->EmitValue(Expr, Size);
  1369. }
  1370. //===----------------------------------------------------------------------===//
  1371. // EmitAlignment - Emit an alignment directive to the specified power of
  1372. // two boundary. For example, if you pass in 3 here, you will get an 8
  1373. // byte alignment. If a global value is specified, and if that global has
  1374. // an explicit alignment requested, it will override the alignment request
  1375. // if required for correctness.
  1376. //
  1377. void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalObject *GV) const {
  1378. if (GV)
  1379. NumBits = getGVAlignmentLog2(GV, *TM.getDataLayout(),
  1380. NumBits);
  1381. if (NumBits == 0) return; // 1-byte aligned: no need to emit alignment.
  1382. assert(NumBits <
  1383. static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
  1384. "undefined behavior");
  1385. if (getCurrentSection()->getKind().isText())
  1386. OutStreamer->EmitCodeAlignment(1u << NumBits);
  1387. else
  1388. OutStreamer->EmitValueToAlignment(1u << NumBits);
  1389. }
  1390. //===----------------------------------------------------------------------===//
  1391. // Constant emission.
  1392. //===----------------------------------------------------------------------===//
  1393. const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
  1394. MCContext &Ctx = OutContext;
  1395. if (CV->isNullValue() || isa<UndefValue>(CV))
  1396. return MCConstantExpr::create(0, Ctx);
  1397. if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
  1398. return MCConstantExpr::create(CI->getZExtValue(), Ctx);
  1399. if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
  1400. return MCSymbolRefExpr::create(getSymbol(GV), Ctx);
  1401. if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV))
  1402. return MCSymbolRefExpr::create(GetBlockAddressSymbol(BA), Ctx);
  1403. const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
  1404. if (!CE) {
  1405. llvm_unreachable("Unknown constant value to lower!");
  1406. }
  1407. if (const MCExpr *RelocExpr
  1408. = getObjFileLowering().getExecutableRelativeSymbol(CE, *Mang, TM))
  1409. return RelocExpr;
  1410. switch (CE->getOpcode()) {
  1411. default:
  1412. // If the code isn't optimized, there may be outstanding folding
  1413. // opportunities. Attempt to fold the expression using DataLayout as a
  1414. // last resort before giving up.
  1415. if (Constant *C = ConstantFoldConstantExpression(CE, *TM.getDataLayout()))
  1416. if (C != CE)
  1417. return lowerConstant(C);
  1418. // Otherwise report the problem to the user.
  1419. {
  1420. std::string S;
  1421. raw_string_ostream OS(S);
  1422. OS << "Unsupported expression in static initializer: ";
  1423. CE->printAsOperand(OS, /*PrintType=*/false,
  1424. !MF ? nullptr : MF->getFunction()->getParent());
  1425. report_fatal_error(OS.str());
  1426. }
  1427. case Instruction::GetElementPtr: {
  1428. const DataLayout &DL = *TM.getDataLayout();
  1429. // Generate a symbolic expression for the byte address
  1430. APInt OffsetAI(DL.getPointerTypeSizeInBits(CE->getType()), 0);
  1431. cast<GEPOperator>(CE)->accumulateConstantOffset(DL, OffsetAI);
  1432. const MCExpr *Base = lowerConstant(CE->getOperand(0));
  1433. if (!OffsetAI)
  1434. return Base;
  1435. int64_t Offset = OffsetAI.getSExtValue();
  1436. return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx),
  1437. Ctx);
  1438. }
  1439. case Instruction::Trunc:
  1440. // We emit the value and depend on the assembler to truncate the generated
  1441. // expression properly. This is important for differences between
  1442. // blockaddress labels. Since the two labels are in the same function, it
  1443. // is reasonable to treat their delta as a 32-bit value.
  1444. // FALL THROUGH.
  1445. case Instruction::BitCast:
  1446. return lowerConstant(CE->getOperand(0));
  1447. case Instruction::IntToPtr: {
  1448. const DataLayout &DL = *TM.getDataLayout();
  1449. // Handle casts to pointers by changing them into casts to the appropriate
  1450. // integer type. This promotes constant folding and simplifies this code.
  1451. Constant *Op = CE->getOperand(0);
  1452. Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()),
  1453. false/*ZExt*/);
  1454. return lowerConstant(Op);
  1455. }
  1456. case Instruction::PtrToInt: {
  1457. const DataLayout &DL = *TM.getDataLayout();
  1458. // Support only foldable casts to/from pointers that can be eliminated by
  1459. // changing the pointer to the appropriately sized integer type.
  1460. Constant *Op = CE->getOperand(0);
  1461. Type *Ty = CE->getType();
  1462. const MCExpr *OpExpr = lowerConstant(Op);
  1463. // We can emit the pointer value into this slot if the slot is an
  1464. // integer slot equal to the size of the pointer.
  1465. if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType()))
  1466. return OpExpr;
  1467. // Otherwise the pointer is smaller than the resultant integer, mask off
  1468. // the high bits so we are sure to get a proper truncation if the input is
  1469. // a constant expr.
  1470. unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType());
  1471. const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx);
  1472. return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx);
  1473. }
  1474. // The MC library also has a right-shift operator, but it isn't consistently
  1475. // signed or unsigned between different targets.
  1476. case Instruction::Add:
  1477. case Instruction::Sub:
  1478. case Instruction::Mul:
  1479. case Instruction::SDiv:
  1480. case Instruction::SRem:
  1481. case Instruction::Shl:
  1482. case Instruction::And:
  1483. case Instruction::Or:
  1484. case Instruction::Xor: {
  1485. const MCExpr *LHS = lowerConstant(CE->getOperand(0));
  1486. const MCExpr *RHS = lowerConstant(CE->getOperand(1));
  1487. switch (CE->getOpcode()) {
  1488. default: llvm_unreachable("Unknown binary operator constant cast expr");
  1489. case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx);
  1490. case Instruction::Sub: return MCBinaryExpr::createSub(LHS, RHS, Ctx);
  1491. case Instruction::Mul: return MCBinaryExpr::createMul(LHS, RHS, Ctx);
  1492. case Instruction::SDiv: return MCBinaryExpr::createDiv(LHS, RHS, Ctx);
  1493. case Instruction::SRem: return MCBinaryExpr::createMod(LHS, RHS, Ctx);
  1494. case Instruction::Shl: return MCBinaryExpr::createShl(LHS, RHS, Ctx);
  1495. case Instruction::And: return MCBinaryExpr::createAnd(LHS, RHS, Ctx);
  1496. case Instruction::Or: return MCBinaryExpr::createOr (LHS, RHS, Ctx);
  1497. case Instruction::Xor: return MCBinaryExpr::createXor(LHS, RHS, Ctx);
  1498. }
  1499. }
  1500. }
  1501. }
  1502. static void emitGlobalConstantImpl(const Constant *C, AsmPrinter &AP,
  1503. const Constant *BaseCV = nullptr,
  1504. uint64_t Offset = 0);
  1505. /// isRepeatedByteSequence - Determine whether the given value is
  1506. /// composed of a repeated sequence of identical bytes and return the
  1507. /// byte value. If it is not a repeated sequence, return -1.
  1508. static int isRepeatedByteSequence(const ConstantDataSequential *V) {
  1509. StringRef Data = V->getRawDataValues();
  1510. assert(!Data.empty() && "Empty aggregates should be CAZ node");
  1511. char C = Data[0];
  1512. for (unsigned i = 1, e = Data.size(); i != e; ++i)
  1513. if (Data[i] != C) return -1;
  1514. return static_cast<uint8_t>(C); // Ensure 255 is not returned as -1.
  1515. }
  1516. /// isRepeatedByteSequence - Determine whether the given value is
  1517. /// composed of a repeated sequence of identical bytes and return the
  1518. /// byte value. If it is not a repeated sequence, return -1.
  1519. static int isRepeatedByteSequence(const Value *V, TargetMachine &TM) {
  1520. if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  1521. uint64_t Size = TM.getDataLayout()->getTypeAllocSizeInBits(V->getType());
  1522. assert(Size % 8 == 0);
  1523. // Extend the element to take zero padding into account.
  1524. APInt Value = CI->getValue().zextOrSelf(Size);
  1525. if (!Value.isSplat(8))
  1526. return -1;
  1527. return Value.zextOrTrunc(8).getZExtValue();
  1528. }
  1529. if (const ConstantArray *CA = dyn_cast<ConstantArray>(V)) {
  1530. // Make sure all array elements are sequences of the same repeated
  1531. // byte.
  1532. assert(CA->getNumOperands() != 0 && "Should be a CAZ");
  1533. Constant *Op0 = CA->getOperand(0);
  1534. int Byte = isRepeatedByteSequence(Op0, TM);
  1535. if (Byte == -1)
  1536. return -1;
  1537. // All array elements must be equal.
  1538. for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i)
  1539. if (CA->getOperand(i) != Op0)
  1540. return -1;
  1541. return Byte;
  1542. }
  1543. if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V))
  1544. return isRepeatedByteSequence(CDS);
  1545. return -1;
  1546. }
  1547. static void emitGlobalConstantDataSequential(const ConstantDataSequential *CDS,
  1548. AsmPrinter &AP){
  1549. // See if we can aggregate this into a .fill, if so, emit it as such.
  1550. int Value = isRepeatedByteSequence(CDS, AP.TM);
  1551. if (Value != -1) {
  1552. uint64_t Bytes =
  1553. AP.TM.getDataLayout()->getTypeAllocSize(
  1554. CDS->getType());
  1555. // Don't emit a 1-byte object as a .fill.
  1556. if (Bytes > 1)
  1557. return AP.OutStreamer->EmitFill(Bytes, Value);
  1558. }
  1559. // If this can be emitted with .ascii/.asciz, emit it as such.
  1560. if (CDS->isString())
  1561. return AP.OutStreamer->EmitBytes(CDS->getAsString());
  1562. // Otherwise, emit the values in successive locations.
  1563. unsigned ElementByteSize = CDS->getElementByteSize();
  1564. if (isa<IntegerType>(CDS->getElementType())) {
  1565. for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
  1566. if (AP.isVerbose())
  1567. AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64 "\n",
  1568. CDS->getElementAsInteger(i));
  1569. AP.OutStreamer->EmitIntValue(CDS->getElementAsInteger(i),
  1570. ElementByteSize);
  1571. }
  1572. } else if (ElementByteSize == 4) {
  1573. // FP Constants are printed as integer constants to avoid losing
  1574. // precision.
  1575. assert(CDS->getElementType()->isFloatTy());
  1576. for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
  1577. union {
  1578. float F;
  1579. uint32_t I;
  1580. };
  1581. F = CDS->getElementAsFloat(i);
  1582. if (AP.isVerbose())
  1583. AP.OutStreamer->GetCommentOS() << "float " << F << '\n';
  1584. AP.OutStreamer->EmitIntValue(I, 4);
  1585. }
  1586. } else {
  1587. assert(CDS->getElementType()->isDoubleTy());
  1588. for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
  1589. union {
  1590. double F;
  1591. uint64_t I;
  1592. };
  1593. F = CDS->getElementAsDouble(i);
  1594. if (AP.isVerbose())
  1595. AP.OutStreamer->GetCommentOS() << "double " << F << '\n';
  1596. AP.OutStreamer->EmitIntValue(I, 8);
  1597. }
  1598. }
  1599. const DataLayout &DL = *AP.TM.getDataLayout();
  1600. unsigned Size = DL.getTypeAllocSize(CDS->getType());
  1601. unsigned EmittedSize = DL.getTypeAllocSize(CDS->getType()->getElementType()) *
  1602. CDS->getNumElements();
  1603. if (unsigned Padding = Size - EmittedSize)
  1604. AP.OutStreamer->EmitZeros(Padding);
  1605. }
  1606. static void emitGlobalConstantArray(const ConstantArray *CA, AsmPrinter &AP,
  1607. const Constant *BaseCV, uint64_t Offset) {
  1608. // See if we can aggregate some values. Make sure it can be
  1609. // represented as a series of bytes of the constant value.
  1610. int Value = isRepeatedByteSequence(CA, AP.TM);
  1611. const DataLayout &DL = *AP.TM.getDataLayout();
  1612. if (Value != -1) {
  1613. uint64_t Bytes = DL.getTypeAllocSize(CA->getType());
  1614. AP.OutStreamer->EmitFill(Bytes, Value);
  1615. }
  1616. else {
  1617. for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
  1618. emitGlobalConstantImpl(CA->getOperand(i), AP, BaseCV, Offset);
  1619. Offset += DL.getTypeAllocSize(CA->getOperand(i)->getType());
  1620. }
  1621. }
  1622. }
  1623. static void emitGlobalConstantVector(const ConstantVector *CV, AsmPrinter &AP) {
  1624. for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
  1625. emitGlobalConstantImpl(CV->getOperand(i), AP);
  1626. const DataLayout &DL = *AP.TM.getDataLayout();
  1627. unsigned Size = DL.getTypeAllocSize(CV->getType());
  1628. unsigned EmittedSize = DL.getTypeAllocSize(CV->getType()->getElementType()) *
  1629. CV->getType()->getNumElements();
  1630. if (unsigned Padding = Size - EmittedSize)
  1631. AP.OutStreamer->EmitZeros(Padding);
  1632. }
  1633. static void emitGlobalConstantStruct(const ConstantStruct *CS, AsmPrinter &AP,
  1634. const Constant *BaseCV, uint64_t Offset) {
  1635. // Print the fields in successive locations. Pad to align if needed!
  1636. const DataLayout *DL = AP.TM.getDataLayout();
  1637. unsigned Size = DL->getTypeAllocSize(CS->getType());
  1638. const StructLayout *Layout = DL->getStructLayout(CS->getType());
  1639. uint64_t SizeSoFar = 0;
  1640. for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
  1641. const Constant *Field = CS->getOperand(i);
  1642. // Print the actual field value.
  1643. emitGlobalConstantImpl(Field, AP, BaseCV, Offset+SizeSoFar);
  1644. // Check if padding is needed and insert one or more 0s.
  1645. uint64_t FieldSize = DL->getTypeAllocSize(Field->getType());
  1646. uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1))
  1647. - Layout->getElementOffset(i)) - FieldSize;
  1648. SizeSoFar += FieldSize + PadSize;
  1649. // Insert padding - this may include padding to increase the size of the
  1650. // current field up to the ABI size (if the struct is not packed) as well
  1651. // as padding to ensure that the next field starts at the right offset.
  1652. AP.OutStreamer->EmitZeros(PadSize);
  1653. }
  1654. assert(SizeSoFar == Layout->getSizeInBytes() &&
  1655. "Layout of constant struct may be incorrect!");
  1656. }
  1657. static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP) {
  1658. APInt API = CFP->getValueAPF().bitcastToAPInt();
  1659. // First print a comment with what we think the original floating-point value
  1660. // should have been.
  1661. if (AP.isVerbose()) {
  1662. SmallString<8> StrVal;
  1663. CFP->getValueAPF().toString(StrVal);
  1664. if (CFP->getType())
  1665. CFP->getType()->print(AP.OutStreamer->GetCommentOS());
  1666. else
  1667. AP.OutStreamer->GetCommentOS() << "Printing <null> Type";
  1668. AP.OutStreamer->GetCommentOS() << ' ' << StrVal << '\n';
  1669. }
  1670. // Now iterate through the APInt chunks, emitting them in endian-correct
  1671. // order, possibly with a smaller chunk at beginning/end (e.g. for x87 80-bit
  1672. // floats).
  1673. unsigned NumBytes = API.getBitWidth() / 8;
  1674. unsigned TrailingBytes = NumBytes % sizeof(uint64_t);
  1675. const uint64_t *p = API.getRawData();
  1676. // PPC's long double has odd notions of endianness compared to how LLVM
  1677. // handles it: p[0] goes first for *big* endian on PPC.
  1678. if (AP.TM.getDataLayout()->isBigEndian() &&
  1679. !CFP->getType()->isPPC_FP128Ty()) {
  1680. int Chunk = API.getNumWords() - 1;
  1681. if (TrailingBytes)
  1682. AP.OutStreamer->EmitIntValue(p[Chunk--], TrailingBytes);
  1683. for (; Chunk >= 0; --Chunk)
  1684. AP.OutStreamer->EmitIntValue(p[Chunk], sizeof(uint64_t));
  1685. } else {
  1686. unsigned Chunk;
  1687. for (Chunk = 0; Chunk < NumBytes / sizeof(uint64_t); ++Chunk)
  1688. AP.OutStreamer->EmitIntValue(p[Chunk], sizeof(uint64_t));
  1689. if (TrailingBytes)
  1690. AP.OutStreamer->EmitIntValue(p[Chunk], TrailingBytes);
  1691. }
  1692. // Emit the tail padding for the long double.
  1693. const DataLayout &DL = *AP.TM.getDataLayout();
  1694. AP.OutStreamer->EmitZeros(DL.getTypeAllocSize(CFP->getType()) -
  1695. DL.getTypeStoreSize(CFP->getType()));
  1696. }
  1697. static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) {
  1698. const DataLayout *DL = AP.TM.getDataLayout();
  1699. unsigned BitWidth = CI->getBitWidth();
  1700. // Copy the value as we may massage the layout for constants whose bit width
  1701. // is not a multiple of 64-bits.
  1702. APInt Realigned(CI->getValue());
  1703. uint64_t ExtraBits = 0;
  1704. unsigned ExtraBitsSize = BitWidth & 63;
  1705. if (ExtraBitsSize) {
  1706. // The bit width of the data is not a multiple of 64-bits.
  1707. // The extra bits are expected to be at the end of the chunk of the memory.
  1708. // Little endian:
  1709. // * Nothing to be done, just record the extra bits to emit.
  1710. // Big endian:
  1711. // * Record the extra bits to emit.
  1712. // * Realign the raw data to emit the chunks of 64-bits.
  1713. if (DL->isBigEndian()) {
  1714. // Basically the structure of the raw data is a chunk of 64-bits cells:
  1715. // 0 1 BitWidth / 64
  1716. // [chunk1][chunk2] ... [chunkN].
  1717. // The most significant chunk is chunkN and it should be emitted first.
  1718. // However, due to the alignment issue chunkN contains useless bits.
  1719. // Realign the chunks so that they contain only useless information:
  1720. // ExtraBits 0 1 (BitWidth / 64) - 1
  1721. // chu[nk1 chu][nk2 chu] ... [nkN-1 chunkN]
  1722. ExtraBits = Realigned.getRawData()[0] &
  1723. (((uint64_t)-1) >> (64 - ExtraBitsSize));
  1724. Realigned = Realigned.lshr(ExtraBitsSize);
  1725. } else
  1726. ExtraBits = Realigned.getRawData()[BitWidth / 64];
  1727. }
  1728. // We don't expect assemblers to support integer data directives
  1729. // for more than 64 bits, so we emit the data in at most 64-bit
  1730. // quantities at a time.
  1731. const uint64_t *RawData = Realigned.getRawData();
  1732. for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
  1733. uint64_t Val = DL->isBigEndian() ? RawData[e - i - 1] : RawData[i];
  1734. AP.OutStreamer->EmitIntValue(Val, 8);
  1735. }
  1736. if (ExtraBitsSize) {
  1737. // Emit the extra bits after the 64-bits chunks.
  1738. // Emit a directive that fills the expected size.
  1739. uint64_t Size = AP.TM.getDataLayout()->getTypeAllocSize(
  1740. CI->getType());
  1741. Size -= (BitWidth / 64) * 8;
  1742. assert(Size && Size * 8 >= ExtraBitsSize &&
  1743. (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize)))
  1744. == ExtraBits && "Directive too small for extra bits.");
  1745. AP.OutStreamer->EmitIntValue(ExtraBits, Size);
  1746. }
  1747. }
  1748. /// \brief Transform a not absolute MCExpr containing a reference to a GOT
  1749. /// equivalent global, by a target specific GOT pc relative access to the
  1750. /// final symbol.
  1751. static void handleIndirectSymViaGOTPCRel(AsmPrinter &AP, const MCExpr **ME,
  1752. const Constant *BaseCst,
  1753. uint64_t Offset) {
  1754. // The global @foo below illustrates a global that uses a got equivalent.
  1755. //
  1756. // @bar = global i32 42
  1757. // @gotequiv = private unnamed_addr constant i32* @bar
  1758. // @foo = i32 trunc (i64 sub (i64 ptrtoint (i32** @gotequiv to i64),
  1759. // i64 ptrtoint (i32* @foo to i64))
  1760. // to i32)
  1761. //
  1762. // The cstexpr in @foo is converted into the MCExpr `ME`, where we actually
  1763. // check whether @foo is suitable to use a GOTPCREL. `ME` is usually in the
  1764. // form:
  1765. //
  1766. // foo = cstexpr, where
  1767. // cstexpr := <gotequiv> - "." + <cst>
  1768. // cstexpr := <gotequiv> - (<foo> - <offset from @foo base>) + <cst>
  1769. //
  1770. // After canonicalization by evaluateAsRelocatable `ME` turns into:
  1771. //
  1772. // cstexpr := <gotequiv> - <foo> + gotpcrelcst, where
  1773. // gotpcrelcst := <offset from @foo base> + <cst>
  1774. //
  1775. MCValue MV;
  1776. if (!(*ME)->evaluateAsRelocatable(MV, nullptr, nullptr) || MV.isAbsolute())
  1777. return;
  1778. const MCSymbolRefExpr *SymA = MV.getSymA();
  1779. if (!SymA)
  1780. return;
  1781. // Check that GOT equivalent symbol is cached.
  1782. const MCSymbol *GOTEquivSym = &SymA->getSymbol();
  1783. if (!AP.GlobalGOTEquivs.count(GOTEquivSym))
  1784. return;
  1785. const GlobalValue *BaseGV = dyn_cast<GlobalValue>(BaseCst);
  1786. if (!BaseGV)
  1787. return;
  1788. // Check for a valid base symbol
  1789. const MCSymbol *BaseSym = AP.getSymbol(BaseGV);
  1790. const MCSymbolRefExpr *SymB = MV.getSymB();
  1791. if (!SymB || BaseSym != &SymB->getSymbol())
  1792. return;
  1793. // Make sure to match:
  1794. //
  1795. // gotpcrelcst := <offset from @foo base> + <cst>
  1796. //
  1797. // If gotpcrelcst is positive it means that we can safely fold the pc rel
  1798. // displacement into the GOTPCREL. We can also can have an extra offset <cst>
  1799. // if the target knows how to encode it.
  1800. //
  1801. int64_t GOTPCRelCst = Offset + MV.getConstant();
  1802. if (GOTPCRelCst < 0)
  1803. return;
  1804. if (!AP.getObjFileLowering().supportGOTPCRelWithOffset() && GOTPCRelCst != 0)
  1805. return;
  1806. // Emit the GOT PC relative to replace the got equivalent global, i.e.:
  1807. //
  1808. // bar:
  1809. // .long 42
  1810. // gotequiv:
  1811. // .quad bar
  1812. // foo:
  1813. // .long gotequiv - "." + <cst>
  1814. //
  1815. // is replaced by the target specific equivalent to:
  1816. //
  1817. // bar:
  1818. // .long 42
  1819. // foo:
  1820. // .long bar@GOTPCREL+<gotpcrelcst>
  1821. //
  1822. AsmPrinter::GOTEquivUsePair Result = AP.GlobalGOTEquivs[GOTEquivSym];
  1823. const GlobalVariable *GV = Result.first;
  1824. int NumUses = (int)Result.second;
  1825. const GlobalValue *FinalGV = dyn_cast<GlobalValue>(GV->getOperand(0));
  1826. const MCSymbol *FinalSym = AP.getSymbol(FinalGV);
  1827. *ME = AP.getObjFileLowering().getIndirectSymViaGOTPCRel(
  1828. FinalSym, MV, Offset, AP.MMI, *AP.OutStreamer);
  1829. // Update GOT equivalent usage information
  1830. --NumUses;
  1831. if (NumUses >= 0)
  1832. AP.GlobalGOTEquivs[GOTEquivSym] = std::make_pair(GV, NumUses);
  1833. }
  1834. static void emitGlobalConstantImpl(const Constant *CV, AsmPrinter &AP,
  1835. const Constant *BaseCV, uint64_t Offset) {
  1836. const DataLayout *DL = AP.TM.getDataLayout();
  1837. uint64_t Size = DL->getTypeAllocSize(CV->getType());
  1838. // Globals with sub-elements such as combinations of arrays and structs
  1839. // are handled recursively by emitGlobalConstantImpl. Keep track of the
  1840. // constant symbol base and the current position with BaseCV and Offset.
  1841. if (!BaseCV && CV->hasOneUse())
  1842. BaseCV = dyn_cast<Constant>(CV->user_back());
  1843. if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
  1844. return AP.OutStreamer->EmitZeros(Size);
  1845. if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
  1846. switch (Size) {
  1847. case 1:
  1848. case 2:
  1849. case 4:
  1850. case 8:
  1851. if (AP.isVerbose())
  1852. AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64 "\n",
  1853. CI->getZExtValue());
  1854. AP.OutStreamer->EmitIntValue(CI->getZExtValue(), Size);
  1855. return;
  1856. default:
  1857. emitGlobalConstantLargeInt(CI, AP);
  1858. return;
  1859. }
  1860. }
  1861. if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
  1862. return emitGlobalConstantFP(CFP, AP);
  1863. if (isa<ConstantPointerNull>(CV)) {
  1864. AP.OutStreamer->EmitIntValue(0, Size);
  1865. return;
  1866. }
  1867. if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(CV))
  1868. return emitGlobalConstantDataSequential(CDS, AP);
  1869. if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
  1870. return emitGlobalConstantArray(CVA, AP, BaseCV, Offset);
  1871. if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
  1872. return emitGlobalConstantStruct(CVS, AP, BaseCV, Offset);
  1873. if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
  1874. // Look through bitcasts, which might not be able to be MCExpr'ized (e.g. of
  1875. // vectors).
  1876. if (CE->getOpcode() == Instruction::BitCast)
  1877. return emitGlobalConstantImpl(CE->getOperand(0), AP);
  1878. if (Size > 8) {
  1879. // If the constant expression's size is greater than 64-bits, then we have
  1880. // to emit the value in chunks. Try to constant fold the value and emit it
  1881. // that way.
  1882. Constant *New = ConstantFoldConstantExpression(CE, *DL);
  1883. if (New && New != CE)
  1884. return emitGlobalConstantImpl(New, AP);
  1885. }
  1886. }
  1887. if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
  1888. return emitGlobalConstantVector(V, AP);
  1889. // Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
  1890. // thread the streamer with EmitValue.
  1891. const MCExpr *ME = AP.lowerConstant(CV);
  1892. // Since lowerConstant already folded and got rid of all IR pointer and
  1893. // integer casts, detect GOT equivalent accesses by looking into the MCExpr
  1894. // directly.
  1895. if (AP.getObjFileLowering().supportIndirectSymViaGOTPCRel())
  1896. handleIndirectSymViaGOTPCRel(AP, &ME, BaseCV, Offset);
  1897. AP.OutStreamer->EmitValue(ME, Size);
  1898. }
  1899. /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
  1900. void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
  1901. uint64_t Size =
  1902. TM.getDataLayout()->getTypeAllocSize(CV->getType());
  1903. if (Size)
  1904. emitGlobalConstantImpl(CV, *this);
  1905. else if (MAI->hasSubsectionsViaSymbols()) {
  1906. // If the global has zero size, emit a single byte so that two labels don't
  1907. // look like they are at the same location.
  1908. OutStreamer->EmitIntValue(0, 1);
  1909. }
  1910. }
  1911. void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
  1912. // Target doesn't support this yet!
  1913. llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
  1914. }
  1915. void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const {
  1916. if (Offset > 0)
  1917. OS << '+' << Offset;
  1918. else if (Offset < 0)
  1919. OS << Offset;
  1920. }
  1921. //===----------------------------------------------------------------------===//
  1922. // Symbol Lowering Routines.
  1923. //===----------------------------------------------------------------------===//
  1924. MCSymbol *AsmPrinter::createTempSymbol(const Twine &Name) const {
  1925. return OutContext.createTempSymbol(Name, true);
  1926. }
  1927. MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const {
  1928. return MMI->getAddrLabelSymbol(BA->getBasicBlock());
  1929. }
  1930. MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BasicBlock *BB) const {
  1931. return MMI->getAddrLabelSymbol(BB);
  1932. }
  1933. /// GetCPISymbol - Return the symbol for the specified constant pool entry.
  1934. MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const {
  1935. const DataLayout *DL = TM.getDataLayout();
  1936. return OutContext.getOrCreateSymbol
  1937. (Twine(DL->getPrivateGlobalPrefix()) + "CPI" + Twine(getFunctionNumber())
  1938. + "_" + Twine(CPID));
  1939. }
  1940. /// GetJTISymbol - Return the symbol for the specified jump table entry.
  1941. MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const {
  1942. return MF->getJTISymbol(JTID, OutContext, isLinkerPrivate);
  1943. }
  1944. /// GetJTSetSymbol - Return the symbol for the specified jump table .set
  1945. /// FIXME: privatize to AsmPrinter.
  1946. MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const {
  1947. const DataLayout *DL = TM.getDataLayout();
  1948. return OutContext.getOrCreateSymbol
  1949. (Twine(DL->getPrivateGlobalPrefix()) + Twine(getFunctionNumber()) + "_" +
  1950. Twine(UID) + "_set_" + Twine(MBBID));
  1951. }
  1952. MCSymbol *AsmPrinter::getSymbolWithGlobalValueBase(const GlobalValue *GV,
  1953. StringRef Suffix) const {
  1954. return getObjFileLowering().getSymbolWithGlobalValueBase(GV, Suffix, *Mang,
  1955. TM);
  1956. }
  1957. /// Return the MCSymbol for the specified ExternalSymbol.
  1958. MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
  1959. SmallString<60> NameStr;
  1960. Mangler::getNameWithPrefix(NameStr, Sym, *TM.getDataLayout());
  1961. return OutContext.getOrCreateSymbol(NameStr);
  1962. }
  1963. /// PrintParentLoopComment - Print comments about parent loops of this one.
  1964. static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop,
  1965. unsigned FunctionNumber) {
  1966. if (!Loop) return;
  1967. PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber);
  1968. OS.indent(Loop->getLoopDepth()*2)
  1969. << "Parent Loop BB" << FunctionNumber << "_"
  1970. << Loop->getHeader()->getNumber()
  1971. << " Depth=" << Loop->getLoopDepth() << '\n';
  1972. }
  1973. /// PrintChildLoopComment - Print comments about child loops within
  1974. /// the loop for this basic block, with nesting.
  1975. static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop,
  1976. unsigned FunctionNumber) {
  1977. // Add child loop information
  1978. for (const MachineLoop *CL : *Loop) {
  1979. OS.indent(CL->getLoopDepth()*2)
  1980. << "Child Loop BB" << FunctionNumber << "_"
  1981. << CL->getHeader()->getNumber() << " Depth " << CL->getLoopDepth()
  1982. << '\n';
  1983. PrintChildLoopComment(OS, CL, FunctionNumber);
  1984. }
  1985. }
  1986. /// emitBasicBlockLoopComments - Pretty-print comments for basic blocks.
  1987. static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
  1988. const MachineLoopInfo *LI,
  1989. const AsmPrinter &AP) {
  1990. // Add loop depth information
  1991. const MachineLoop *Loop = LI->getLoopFor(&MBB);
  1992. if (!Loop) return;
  1993. MachineBasicBlock *Header = Loop->getHeader();
  1994. assert(Header && "No header for loop");
  1995. // If this block is not a loop header, just print out what is the loop header
  1996. // and return.
  1997. if (Header != &MBB) {
  1998. AP.OutStreamer->AddComment(" in Loop: Header=BB" +
  1999. Twine(AP.getFunctionNumber())+"_" +
  2000. Twine(Loop->getHeader()->getNumber())+
  2001. " Depth="+Twine(Loop->getLoopDepth()));
  2002. return;
  2003. }
  2004. // Otherwise, it is a loop header. Print out information about child and
  2005. // parent loops.
  2006. raw_ostream &OS = AP.OutStreamer->GetCommentOS();
  2007. PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber());
  2008. OS << "=>";
  2009. OS.indent(Loop->getLoopDepth()*2-2);
  2010. OS << "This ";
  2011. if (Loop->empty())
  2012. OS << "Inner ";
  2013. OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n';
  2014. PrintChildLoopComment(OS, Loop, AP.getFunctionNumber());
  2015. }
  2016. /// EmitBasicBlockStart - This method prints the label for the specified
  2017. /// MachineBasicBlock, an alignment (if present) and a comment describing
  2018. /// it if appropriate.
  2019. void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
  2020. // Emit an alignment directive for this block, if needed.
  2021. if (unsigned Align = MBB.getAlignment())
  2022. EmitAlignment(Align);
  2023. // If the block has its address taken, emit any labels that were used to
  2024. // reference the block. It is possible that there is more than one label
  2025. // here, because multiple LLVM BB's may have been RAUW'd to this block after
  2026. // the references were generated.
  2027. if (MBB.hasAddressTaken()) {
  2028. const BasicBlock *BB = MBB.getBasicBlock();
  2029. if (isVerbose())
  2030. OutStreamer->AddComment("Block address taken");
  2031. for (MCSymbol *Sym : MMI->getAddrLabelSymbolToEmit(BB))
  2032. OutStreamer->EmitLabel(Sym);
  2033. }
  2034. // Print some verbose block comments.
  2035. if (isVerbose()) {
  2036. if (const BasicBlock *BB = MBB.getBasicBlock())
  2037. if (BB->hasName())
  2038. OutStreamer->AddComment("%" + BB->getName());
  2039. emitBasicBlockLoopComments(MBB, LI, *this);
  2040. }
  2041. // Print the main label for the block.
  2042. if (MBB.pred_empty() || isBlockOnlyReachableByFallthrough(&MBB)) {
  2043. if (isVerbose()) {
  2044. // NOTE: Want this comment at start of line, don't emit with AddComment.
  2045. OutStreamer->emitRawComment(" BB#" + Twine(MBB.getNumber()) + ":", false);
  2046. }
  2047. } else {
  2048. OutStreamer->EmitLabel(MBB.getSymbol());
  2049. }
  2050. }
  2051. void AsmPrinter::EmitVisibility(MCSymbol *Sym, unsigned Visibility,
  2052. bool IsDefinition) const {
  2053. MCSymbolAttr Attr = MCSA_Invalid;
  2054. switch (Visibility) {
  2055. default: break;
  2056. case GlobalValue::HiddenVisibility:
  2057. if (IsDefinition)
  2058. Attr = MAI->getHiddenVisibilityAttr();
  2059. else
  2060. Attr = MAI->getHiddenDeclarationVisibilityAttr();
  2061. break;
  2062. case GlobalValue::ProtectedVisibility:
  2063. Attr = MAI->getProtectedVisibilityAttr();
  2064. break;
  2065. }
  2066. if (Attr != MCSA_Invalid)
  2067. OutStreamer->EmitSymbolAttribute(Sym, Attr);
  2068. }
  2069. /// isBlockOnlyReachableByFallthough - Return true if the basic block has
  2070. /// exactly one predecessor and the control transfer mechanism between
  2071. /// the predecessor and this block is a fall-through.
  2072. bool AsmPrinter::
  2073. isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
  2074. // If this is a landing pad, it isn't a fall through. If it has no preds,
  2075. // then nothing falls through to it.
  2076. if (MBB->isLandingPad() || MBB->pred_empty())
  2077. return false;
  2078. // If there isn't exactly one predecessor, it can't be a fall through.
  2079. if (MBB->pred_size() > 1)
  2080. return false;
  2081. // The predecessor has to be immediately before this block.
  2082. MachineBasicBlock *Pred = *MBB->pred_begin();
  2083. if (!Pred->isLayoutSuccessor(MBB))
  2084. return false;
  2085. // If the block is completely empty, then it definitely does fall through.
  2086. if (Pred->empty())
  2087. return true;
  2088. // Check the terminators in the previous blocks
  2089. for (const auto &MI : Pred->terminators()) {
  2090. // If it is not a simple branch, we are in a table somewhere.
  2091. if (!MI.isBranch() || MI.isIndirectBranch())
  2092. return false;
  2093. // If we are the operands of one of the branches, this is not a fall
  2094. // through. Note that targets with delay slots will usually bundle
  2095. // terminators with the delay slot instruction.
  2096. for (ConstMIBundleOperands OP(&MI); OP.isValid(); ++OP) {
  2097. if (OP->isJTI())
  2098. return false;
  2099. if (OP->isMBB() && OP->getMBB() == MBB)
  2100. return false;
  2101. }
  2102. }
  2103. return true;
  2104. }
  2105. GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy &S) {
  2106. if (!S.usesMetadata())
  2107. return nullptr;
  2108. assert(!S.useStatepoints() && "statepoints do not currently support custom"
  2109. " stackmap formats, please see the documentation for a description of"
  2110. " the default format. If you really need a custom serialized format,"
  2111. " please file a bug");
  2112. gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
  2113. gcp_map_type::iterator GCPI = GCMap.find(&S);
  2114. if (GCPI != GCMap.end())
  2115. return GCPI->second.get();
  2116. const char *Name = S.getName().c_str();
  2117. for (GCMetadataPrinterRegistry::iterator
  2118. I = GCMetadataPrinterRegistry::begin(),
  2119. E = GCMetadataPrinterRegistry::end(); I != E; ++I)
  2120. if (strcmp(Name, I->getName()) == 0) {
  2121. std::unique_ptr<GCMetadataPrinter> GMP = I->instantiate();
  2122. GMP->S = &S;
  2123. auto IterBool = GCMap.insert(std::make_pair(&S, std::move(GMP)));
  2124. return IterBool.first->second.get();
  2125. }
  2126. report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name));
  2127. }
  2128. /// Pin vtable to this file.
  2129. AsmPrinterHandler::~AsmPrinterHandler() {}
  2130. void AsmPrinterHandler::markFunctionEnd() {}