LLVM 19.0.0git
PPCAsmPrinter.cpp
Go to the documentation of this file.
1//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains a printer that converts from our internal representation
10// of machine-dependent LLVM code to PowerPC assembly language. This printer is
11// the output mechanism used by `llc'.
12//
13// Documentation at http://developer.apple.com/documentation/DeveloperTools/
14// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
15//
16//===----------------------------------------------------------------------===//
17
22#include "PPC.h"
23#include "PPCInstrInfo.h"
25#include "PPCSubtarget.h"
26#include "PPCTargetMachine.h"
27#include "PPCTargetStreamer.h"
29#include "llvm/ADT/MapVector.h"
31#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringRef.h"
34#include "llvm/ADT/Twine.h"
46#include "llvm/IR/DataLayout.h"
47#include "llvm/IR/GlobalValue.h"
49#include "llvm/IR/Module.h"
50#include "llvm/MC/MCAsmInfo.h"
51#include "llvm/MC/MCContext.h"
53#include "llvm/MC/MCExpr.h"
54#include "llvm/MC/MCInst.h"
58#include "llvm/MC/MCStreamer.h"
59#include "llvm/MC/MCSymbol.h"
60#include "llvm/MC/MCSymbolELF.h"
62#include "llvm/MC/SectionKind.h"
66#include "llvm/Support/Debug.h"
67#include "llvm/Support/Error.h"
76#include <algorithm>
77#include <cassert>
78#include <cstdint>
79#include <memory>
80#include <new>
81
82using namespace llvm;
83using namespace llvm::XCOFF;
84
85#define DEBUG_TYPE "asmprinter"
86
87STATISTIC(NumTOCEntries, "Number of Total TOC Entries Emitted.");
88STATISTIC(NumTOCConstPool, "Number of Constant Pool TOC Entries.");
89STATISTIC(NumTOCGlobalInternal,
90 "Number of Internal Linkage Global TOC Entries.");
91STATISTIC(NumTOCGlobalExternal,
92 "Number of External Linkage Global TOC Entries.");
93STATISTIC(NumTOCJumpTable, "Number of Jump Table TOC Entries.");
94STATISTIC(NumTOCThreadLocal, "Number of Thread Local TOC Entries.");
95STATISTIC(NumTOCBlockAddress, "Number of Block Address TOC Entries.");
96STATISTIC(NumTOCEHBlock, "Number of EH Block TOC Entries.");
97
99 "aix-ssp-tb-bit", cl::init(false),
100 cl::desc("Enable Passing SSP Canary info in Trackback on AIX"), cl::Hidden);
101
102// Specialize DenseMapInfo to allow
103// std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind> in DenseMap.
104// This specialization is needed here because that type is used as keys in the
105// map representing TOC entries.
106namespace llvm {
107template <>
108struct DenseMapInfo<std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>> {
109 using TOCKey = std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>;
110
111 static inline TOCKey getEmptyKey() {
113 }
114 static inline TOCKey getTombstoneKey() {
116 }
117 static unsigned getHashValue(const TOCKey &PairVal) {
120 DenseMapInfo<int>::getHashValue(PairVal.second));
121 }
122 static bool isEqual(const TOCKey &A, const TOCKey &B) { return A == B; }
123};
124} // end namespace llvm
125
126namespace {
127
128enum {
129 // GNU attribute tags for PowerPC ABI
130 Tag_GNU_Power_ABI_FP = 4,
131 Tag_GNU_Power_ABI_Vector = 8,
132 Tag_GNU_Power_ABI_Struct_Return = 12,
133
134 // GNU attribute values for PowerPC float ABI, as combination of two parts
135 Val_GNU_Power_ABI_NoFloat = 0b00,
136 Val_GNU_Power_ABI_HardFloat_DP = 0b01,
137 Val_GNU_Power_ABI_SoftFloat_DP = 0b10,
138 Val_GNU_Power_ABI_HardFloat_SP = 0b11,
139
140 Val_GNU_Power_ABI_LDBL_IBM128 = 0b0100,
141 Val_GNU_Power_ABI_LDBL_64 = 0b1000,
142 Val_GNU_Power_ABI_LDBL_IEEE128 = 0b1100,
143};
144
145class PPCAsmPrinter : public AsmPrinter {
146protected:
147 // For TLS on AIX, we need to be able to identify TOC entries of specific
148 // VariantKind so we can add the right relocations when we generate the
149 // entries. So each entry is represented by a pair of MCSymbol and
150 // VariantKind. For example, we need to be able to identify the following
151 // entry as a TLSGD entry so we can add the @m relocation:
152 // .tc .i[TC],i[TL]@m
153 // By default, VK_None is used for the VariantKind.
155 MCSymbol *>
156 TOC;
157 const PPCSubtarget *Subtarget = nullptr;
158
159 // Keep track of the number of TLS variables and their corresponding
160 // addresses, which is then used for the assembly printing of
161 // non-TOC-based local-exec variables.
162 MapVector<const GlobalValue *, uint64_t> TLSVarsToAddressMapping;
163
164public:
165 explicit PPCAsmPrinter(TargetMachine &TM,
166 std::unique_ptr<MCStreamer> Streamer)
167 : AsmPrinter(TM, std::move(Streamer)) {}
168
169 StringRef getPassName() const override { return "PowerPC Assembly Printer"; }
170
171 enum TOCEntryType {
172 TOCType_ConstantPool,
173 TOCType_GlobalExternal,
174 TOCType_GlobalInternal,
175 TOCType_JumpTable,
176 TOCType_ThreadLocal,
177 TOCType_BlockAddress,
178 TOCType_EHBlock
179 };
180
181 MCSymbol *lookUpOrCreateTOCEntry(const MCSymbol *Sym, TOCEntryType Type,
183 MCSymbolRefExpr::VariantKind::VK_None);
184
185 bool doInitialization(Module &M) override {
186 if (!TOC.empty())
187 TOC.clear();
189 }
190
191 void emitInstruction(const MachineInstr *MI) override;
192
193 /// This function is for PrintAsmOperand and PrintAsmMemoryOperand,
194 /// invoked by EmitMSInlineAsmStr and EmitGCCInlineAsmStr only.
195 /// The \p MI would be INLINEASM ONLY.
196 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
197
198 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
199 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
200 const char *ExtraCode, raw_ostream &O) override;
201 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
202 const char *ExtraCode, raw_ostream &O) override;
203
204 void LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI);
205 void LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI);
206 void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK);
207 void EmitAIXTlsCallHelper(const MachineInstr *MI);
208 const MCExpr *getAdjustedLocalExecExpr(const MachineOperand &MO,
209 int64_t Offset);
210 bool runOnMachineFunction(MachineFunction &MF) override {
211 Subtarget = &MF.getSubtarget<PPCSubtarget>();
212 bool Changed = AsmPrinter::runOnMachineFunction(MF);
214 return Changed;
215 }
216};
217
218/// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
219class PPCLinuxAsmPrinter : public PPCAsmPrinter {
220public:
221 explicit PPCLinuxAsmPrinter(TargetMachine &TM,
222 std::unique_ptr<MCStreamer> Streamer)
223 : PPCAsmPrinter(TM, std::move(Streamer)) {}
224
225 StringRef getPassName() const override {
226 return "Linux PPC Assembly Printer";
227 }
228
229 void emitGNUAttributes(Module &M);
230
231 void emitStartOfAsmFile(Module &M) override;
232 void emitEndOfAsmFile(Module &) override;
233
234 void emitFunctionEntryLabel() override;
235
236 void emitFunctionBodyStart() override;
237 void emitFunctionBodyEnd() override;
238 void emitInstruction(const MachineInstr *MI) override;
239};
240
241class PPCAIXAsmPrinter : public PPCAsmPrinter {
242private:
243 /// Symbols lowered from ExternalSymbolSDNodes, we will need to emit extern
244 /// linkage for them in AIX.
245 SmallPtrSet<MCSymbol *, 8> ExtSymSDNodeSymbols;
246
247 /// A format indicator and unique trailing identifier to form part of the
248 /// sinit/sterm function names.
249 std::string FormatIndicatorAndUniqueModId;
250
251 // Record a list of GlobalAlias associated with a GlobalObject.
252 // This is used for AIX's extra-label-at-definition aliasing strategy.
254 GOAliasMap;
255
256 uint16_t getNumberOfVRSaved();
257 void emitTracebackTable();
258
260
261 void emitGlobalVariableHelper(const GlobalVariable *);
262
263 // Get the offset of an alias based on its AliaseeObject.
264 uint64_t getAliasOffset(const Constant *C);
265
266public:
267 PPCAIXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
268 : PPCAsmPrinter(TM, std::move(Streamer)) {
269 if (MAI->isLittleEndian())
271 "cannot create AIX PPC Assembly Printer for a little-endian target");
272 }
273
274 StringRef getPassName() const override { return "AIX PPC Assembly Printer"; }
275
276 bool doInitialization(Module &M) override;
277
278 void emitXXStructorList(const DataLayout &DL, const Constant *List,
279 bool IsCtor) override;
280
281 void SetupMachineFunction(MachineFunction &MF) override;
282
283 void emitGlobalVariable(const GlobalVariable *GV) override;
284
285 void emitFunctionDescriptor() override;
286
287 void emitFunctionEntryLabel() override;
288
289 void emitFunctionBodyEnd() override;
290
291 void emitPGORefs(Module &M);
292
293 void emitEndOfAsmFile(Module &) override;
294
295 void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const override;
296
297 void emitInstruction(const MachineInstr *MI) override;
298
299 bool doFinalization(Module &M) override;
300
301 void emitTTypeReference(const GlobalValue *GV, unsigned Encoding) override;
302
303 void emitModuleCommandLines(Module &M) override;
304};
305
306} // end anonymous namespace
307
308void PPCAsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
309 raw_ostream &O) {
310 // Computing the address of a global symbol, not calling it.
311 const GlobalValue *GV = MO.getGlobal();
312 getSymbol(GV)->print(O, MAI);
313 printOffset(MO.getOffset(), O);
314}
315
316void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
317 raw_ostream &O) {
318 const DataLayout &DL = getDataLayout();
319 const MachineOperand &MO = MI->getOperand(OpNo);
320
321 switch (MO.getType()) {
323 // The MI is INLINEASM ONLY and UseVSXReg is always false.
325
326 // Linux assembler (Others?) does not take register mnemonics.
327 // FIXME - What about special registers used in mfspr/mtspr?
329 return;
330 }
332 O << MO.getImm();
333 return;
334
336 MO.getMBB()->getSymbol()->print(O, MAI);
337 return;
339 O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
340 << MO.getIndex();
341 return;
343 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
344 return;
346 PrintSymbolOperand(MO, O);
347 return;
348 }
349
350 default:
351 O << "<unknown operand type: " << (unsigned)MO.getType() << ">";
352 return;
353 }
354}
355
356/// PrintAsmOperand - Print out an operand for an inline asm expression.
357///
358bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
359 const char *ExtraCode, raw_ostream &O) {
360 // Does this asm operand have a single letter operand modifier?
361 if (ExtraCode && ExtraCode[0]) {
362 if (ExtraCode[1] != 0) return true; // Unknown modifier.
363
364 switch (ExtraCode[0]) {
365 default:
366 // See if this is a generic print operand
367 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
368 case 'L': // Write second word of DImode reference.
369 // Verify that this operand has two consecutive registers.
370 if (!MI->getOperand(OpNo).isReg() ||
371 OpNo+1 == MI->getNumOperands() ||
372 !MI->getOperand(OpNo+1).isReg())
373 return true;
374 ++OpNo; // Return the high-part.
375 break;
376 case 'I':
377 // Write 'i' if an integer constant, otherwise nothing. Used to print
378 // addi vs add, etc.
379 if (MI->getOperand(OpNo).isImm())
380 O << "i";
381 return false;
382 case 'x':
383 if(!MI->getOperand(OpNo).isReg())
384 return true;
385 // This operand uses VSX numbering.
386 // If the operand is a VMX register, convert it to a VSX register.
387 Register Reg = MI->getOperand(OpNo).getReg();
388 if (PPC::isVRRegister(Reg))
389 Reg = PPC::VSX32 + (Reg - PPC::V0);
390 else if (PPC::isVFRegister(Reg))
391 Reg = PPC::VSX32 + (Reg - PPC::VF0);
392 const char *RegName;
395 O << RegName;
396 return false;
397 }
398 }
399
400 printOperand(MI, OpNo, O);
401 return false;
402}
403
404// At the moment, all inline asm memory operands are a single register.
405// In any case, the output of this routine should always be just one
406// assembler operand.
407bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
408 const char *ExtraCode,
409 raw_ostream &O) {
410 if (ExtraCode && ExtraCode[0]) {
411 if (ExtraCode[1] != 0) return true; // Unknown modifier.
412
413 switch (ExtraCode[0]) {
414 default: return true; // Unknown modifier.
415 case 'L': // A memory reference to the upper word of a double word op.
416 O << getDataLayout().getPointerSize() << "(";
417 printOperand(MI, OpNo, O);
418 O << ")";
419 return false;
420 case 'y': // A memory reference for an X-form instruction
421 O << "0, ";
422 printOperand(MI, OpNo, O);
423 return false;
424 case 'I':
425 // Write 'i' if an integer constant, otherwise nothing. Used to print
426 // addi vs add, etc.
427 if (MI->getOperand(OpNo).isImm())
428 O << "i";
429 return false;
430 case 'U': // Print 'u' for update form.
431 case 'X': // Print 'x' for indexed form.
432 // FIXME: Currently for PowerPC memory operands are always loaded
433 // into a register, so we never get an update or indexed form.
434 // This is bad even for offset forms, since even if we know we
435 // have a value in -16(r1), we will generate a load into r<n>
436 // and then load from 0(r<n>). Until that issue is fixed,
437 // tolerate 'U' and 'X' but don't output anything.
438 assert(MI->getOperand(OpNo).isReg());
439 return false;
440 }
441 }
442
443 assert(MI->getOperand(OpNo).isReg());
444 O << "0(";
445 printOperand(MI, OpNo, O);
446 O << ")";
447 return false;
448}
449
450static void collectTOCStats(PPCAsmPrinter::TOCEntryType Type) {
451 ++NumTOCEntries;
452 switch (Type) {
453 case PPCAsmPrinter::TOCType_ConstantPool:
454 ++NumTOCConstPool;
455 break;
456 case PPCAsmPrinter::TOCType_GlobalInternal:
457 ++NumTOCGlobalInternal;
458 break;
459 case PPCAsmPrinter::TOCType_GlobalExternal:
460 ++NumTOCGlobalExternal;
461 break;
462 case PPCAsmPrinter::TOCType_JumpTable:
463 ++NumTOCJumpTable;
464 break;
465 case PPCAsmPrinter::TOCType_ThreadLocal:
466 ++NumTOCThreadLocal;
467 break;
468 case PPCAsmPrinter::TOCType_BlockAddress:
469 ++NumTOCBlockAddress;
470 break;
471 case PPCAsmPrinter::TOCType_EHBlock:
472 ++NumTOCEHBlock;
473 break;
474 }
475}
476
478 const TargetMachine &TM,
479 const MachineOperand &MO) {
480 CodeModel::Model ModuleModel = TM.getCodeModel();
481
482 // If the operand is not a global address then there is no
483 // global variable to carry an attribute.
485 return ModuleModel;
486
487 const GlobalValue *GV = MO.getGlobal();
488 assert(GV && "expected global for MO_GlobalAddress");
489
490 return S.getCodeModel(TM, GV);
491}
492
494 switch (CM) {
495 case CodeModel::Large:
497 return;
498 case CodeModel::Small:
500 return;
501 default:
502 report_fatal_error("Invalid code model for AIX");
503 }
504}
505
506/// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
507/// exists for it. If not, create one. Then return a symbol that references
508/// the TOC entry.
509MCSymbol *
510PPCAsmPrinter::lookUpOrCreateTOCEntry(const MCSymbol *Sym, TOCEntryType Type,
512 // If this is a new TOC entry add statistics about it.
513 if (!TOC.contains({Sym, Kind}))
515
516 MCSymbol *&TOCEntry = TOC[{Sym, Kind}];
517 if (!TOCEntry)
518 TOCEntry = createTempSymbol("C");
519 return TOCEntry;
520}
521
522void PPCAsmPrinter::LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI) {
523 unsigned NumNOPBytes = MI.getOperand(1).getImm();
524
525 auto &Ctx = OutStreamer->getContext();
526 MCSymbol *MILabel = Ctx.createTempSymbol();
527 OutStreamer->emitLabel(MILabel);
528
529 SM.recordStackMap(*MILabel, MI);
530 assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
531
532 // Scan ahead to trim the shadow.
533 const MachineBasicBlock &MBB = *MI.getParent();
535 ++MII;
536 while (NumNOPBytes > 0) {
537 if (MII == MBB.end() || MII->isCall() ||
538 MII->getOpcode() == PPC::DBG_VALUE ||
539 MII->getOpcode() == TargetOpcode::PATCHPOINT ||
540 MII->getOpcode() == TargetOpcode::STACKMAP)
541 break;
542 ++MII;
543 NumNOPBytes -= 4;
544 }
545
546 // Emit nops.
547 for (unsigned i = 0; i < NumNOPBytes; i += 4)
548 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
549}
550
551// Lower a patchpoint of the form:
552// [<def>], <id>, <numBytes>, <target>, <numArgs>
553void PPCAsmPrinter::LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI) {
554 auto &Ctx = OutStreamer->getContext();
555 MCSymbol *MILabel = Ctx.createTempSymbol();
556 OutStreamer->emitLabel(MILabel);
557
558 SM.recordPatchPoint(*MILabel, MI);
559 PatchPointOpers Opers(&MI);
560
561 unsigned EncodedBytes = 0;
562 const MachineOperand &CalleeMO = Opers.getCallTarget();
563
564 if (CalleeMO.isImm()) {
565 int64_t CallTarget = CalleeMO.getImm();
566 if (CallTarget) {
567 assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget &&
568 "High 16 bits of call target should be zero.");
569 Register ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg();
570 EncodedBytes = 0;
571 // Materialize the jump address:
572 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI8)
573 .addReg(ScratchReg)
574 .addImm((CallTarget >> 32) & 0xFFFF));
575 ++EncodedBytes;
576 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::RLDIC)
577 .addReg(ScratchReg)
578 .addReg(ScratchReg)
579 .addImm(32).addImm(16));
580 ++EncodedBytes;
581 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORIS8)
582 .addReg(ScratchReg)
583 .addReg(ScratchReg)
584 .addImm((CallTarget >> 16) & 0xFFFF));
585 ++EncodedBytes;
586 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORI8)
587 .addReg(ScratchReg)
588 .addReg(ScratchReg)
589 .addImm(CallTarget & 0xFFFF));
590
591 // Save the current TOC pointer before the remote call.
592 int TOCSaveOffset = Subtarget->getFrameLowering()->getTOCSaveOffset();
593 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::STD)
594 .addReg(PPC::X2)
595 .addImm(TOCSaveOffset)
596 .addReg(PPC::X1));
597 ++EncodedBytes;
598
599 // If we're on ELFv1, then we need to load the actual function pointer
600 // from the function descriptor.
601 if (!Subtarget->isELFv2ABI()) {
602 // Load the new TOC pointer and the function address, but not r11
603 // (needing this is rare, and loading it here would prevent passing it
604 // via a 'nest' parameter.
605 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
606 .addReg(PPC::X2)
607 .addImm(8)
608 .addReg(ScratchReg));
609 ++EncodedBytes;
610 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
611 .addReg(ScratchReg)
612 .addImm(0)
613 .addReg(ScratchReg));
614 ++EncodedBytes;
615 }
616
617 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR8)
618 .addReg(ScratchReg));
619 ++EncodedBytes;
620 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTRL8));
621 ++EncodedBytes;
622
623 // Restore the TOC pointer after the call.
624 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
625 .addReg(PPC::X2)
626 .addImm(TOCSaveOffset)
627 .addReg(PPC::X1));
628 ++EncodedBytes;
629 }
630 } else if (CalleeMO.isGlobal()) {
631 const GlobalValue *GValue = CalleeMO.getGlobal();
632 MCSymbol *MOSymbol = getSymbol(GValue);
633 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext);
634
635 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL8_NOP)
636 .addExpr(SymVar));
637 EncodedBytes += 2;
638 }
639
640 // Each instruction is 4 bytes.
641 EncodedBytes *= 4;
642
643 // Emit padding.
644 unsigned NumBytes = Opers.getNumPatchBytes();
645 assert(NumBytes >= EncodedBytes &&
646 "Patchpoint can't request size less than the length of a call.");
647 assert((NumBytes - EncodedBytes) % 4 == 0 &&
648 "Invalid number of NOP bytes requested!");
649 for (unsigned i = EncodedBytes; i < NumBytes; i += 4)
650 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
651}
652
653/// This helper function creates the TlsGetAddr/TlsGetMod MCSymbol for AIX. We
654/// will create the csect and use the qual-name symbol instead of creating just
655/// the external symbol.
656static MCSymbol *createMCSymbolForTlsGetAddr(MCContext &Ctx, unsigned MIOpc) {
657 StringRef SymName;
658 switch (MIOpc) {
659 default:
660 SymName = ".__tls_get_addr";
661 break;
662 case PPC::GETtlsTpointer32AIX:
663 SymName = ".__get_tpointer";
664 break;
665 case PPC::GETtlsMOD32AIX:
666 case PPC::GETtlsMOD64AIX:
667 SymName = ".__tls_get_mod";
668 break;
669 }
670 return Ctx
674}
675
676void PPCAsmPrinter::EmitAIXTlsCallHelper(const MachineInstr *MI) {
677 assert(Subtarget->isAIXABI() &&
678 "Only expecting to emit calls to get the thread pointer on AIX!");
679
680 MCSymbol *TlsCall = createMCSymbolForTlsGetAddr(OutContext, MI->getOpcode());
681 const MCExpr *TlsRef =
683 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BLA).addExpr(TlsRef));
684}
685
686/// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a
687/// call to __tls_get_addr to the current output stream.
688void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI,
691 unsigned Opcode = PPC::BL8_NOP_TLS;
692
693 assert(MI->getNumOperands() >= 3 && "Expecting at least 3 operands from MI");
694 if (MI->getOperand(2).getTargetFlags() == PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
695 MI->getOperand(2).getTargetFlags() == PPCII::MO_GOT_TLSLD_PCREL_FLAG) {
697 Opcode = PPC::BL8_NOTOC_TLS;
698 }
699 const Module *M = MF->getFunction().getParent();
700
701 assert(MI->getOperand(0).isReg() &&
702 ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) ||
703 (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) &&
704 "GETtls[ld]ADDR[32] must define GPR3");
705 assert(MI->getOperand(1).isReg() &&
706 ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) ||
707 (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) &&
708 "GETtls[ld]ADDR[32] must read GPR3");
709
710 if (Subtarget->isAIXABI()) {
711 // For TLSGD, the variable offset should already be in R4 and the region
712 // handle should already be in R3. We generate an absolute branch to
713 // .__tls_get_addr. For TLSLD, the module handle should already be in R3.
714 // We generate an absolute branch to .__tls_get_mod.
715 Register VarOffsetReg = Subtarget->isPPC64() ? PPC::X4 : PPC::R4;
716 (void)VarOffsetReg;
717 assert((MI->getOpcode() == PPC::GETtlsMOD32AIX ||
718 MI->getOpcode() == PPC::GETtlsMOD64AIX ||
719 (MI->getOperand(2).isReg() &&
720 MI->getOperand(2).getReg() == VarOffsetReg)) &&
721 "GETtls[ld]ADDR[32] must read GPR4");
722 EmitAIXTlsCallHelper(MI);
723 return;
724 }
725
726 MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol("__tls_get_addr");
727
728 if (Subtarget->is32BitELFABI() && isPositionIndependent())
730
731 const MCExpr *TlsRef =
732 MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext);
733
734 // Add 32768 offset to the symbol so we follow up the latest GOT/PLT ABI.
735 if (Kind == MCSymbolRefExpr::VK_PLT && Subtarget->isSecurePlt() &&
736 M->getPICLevel() == PICLevel::BigPIC)
738 TlsRef, MCConstantExpr::create(32768, OutContext), OutContext);
739 const MachineOperand &MO = MI->getOperand(2);
740 const GlobalValue *GValue = MO.getGlobal();
741 MCSymbol *MOSymbol = getSymbol(GValue);
742 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
743 EmitToStreamer(*OutStreamer,
744 MCInstBuilder(Subtarget->isPPC64() ? Opcode
745 : (unsigned)PPC::BL_TLS)
746 .addExpr(TlsRef)
747 .addExpr(SymVar));
748}
749
750/// Map a machine operand for a TOC pseudo-machine instruction to its
751/// corresponding MCSymbol.
753 AsmPrinter &AP) {
754 switch (MO.getType()) {
756 return AP.getSymbol(MO.getGlobal());
758 return AP.GetCPISymbol(MO.getIndex());
760 return AP.GetJTISymbol(MO.getIndex());
763 default:
764 llvm_unreachable("Unexpected operand type to get symbol.");
765 }
766}
767
768static PPCAsmPrinter::TOCEntryType
770 // Use the target flags to determine if this MO is Thread Local.
771 // If we don't do this it comes out as Global.
773 return PPCAsmPrinter::TOCType_ThreadLocal;
774
775 switch (MO.getType()) {
777 const GlobalValue *GlobalV = MO.getGlobal();
778 GlobalValue::LinkageTypes Linkage = GlobalV->getLinkage();
779 if (Linkage == GlobalValue::ExternalLinkage ||
782 return PPCAsmPrinter::TOCType_GlobalExternal;
783
784 return PPCAsmPrinter::TOCType_GlobalInternal;
785 }
787 return PPCAsmPrinter::TOCType_ConstantPool;
789 return PPCAsmPrinter::TOCType_JumpTable;
791 return PPCAsmPrinter::TOCType_BlockAddress;
792 default:
793 llvm_unreachable("Unexpected operand type to get TOC type.");
794 }
795}
796/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
797/// the current output stream.
798///
799void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
800 PPC_MC::verifyInstructionPredicates(MI->getOpcode(),
801 getSubtargetInfo().getFeatureBits());
802
803 MCInst TmpInst;
804 const bool IsPPC64 = Subtarget->isPPC64();
805 const bool IsAIX = Subtarget->isAIXABI();
806 const bool HasAIXSmallLocalTLS = Subtarget->hasAIXSmallLocalExecTLS() ||
807 Subtarget->hasAIXSmallLocalDynamicTLS();
808 const Module *M = MF->getFunction().getParent();
809 PICLevel::Level PL = M->getPICLevel();
810
811#ifndef NDEBUG
812 // Validate that SPE and FPU are mutually exclusive in codegen
813 if (!MI->isInlineAsm()) {
814 for (const MachineOperand &MO: MI->operands()) {
815 if (MO.isReg()) {
816 Register Reg = MO.getReg();
817 if (Subtarget->hasSPE()) {
818 if (PPC::F4RCRegClass.contains(Reg) ||
819 PPC::F8RCRegClass.contains(Reg) ||
820 PPC::VFRCRegClass.contains(Reg) ||
821 PPC::VRRCRegClass.contains(Reg) ||
822 PPC::VSFRCRegClass.contains(Reg) ||
823 PPC::VSSRCRegClass.contains(Reg)
824 )
825 llvm_unreachable("SPE targets cannot have FPRegs!");
826 } else {
827 if (PPC::SPERCRegClass.contains(Reg))
828 llvm_unreachable("SPE register found in FPU-targeted code!");
829 }
830 }
831 }
832 }
833#endif
834
835 auto getTOCRelocAdjustedExprForXCOFF = [this](const MCExpr *Expr,
836 ptrdiff_t OriginalOffset) {
837 // Apply an offset to the TOC-based expression such that the adjusted
838 // notional offset from the TOC base (to be encoded into the instruction's D
839 // or DS field) is the signed 16-bit truncation of the original notional
840 // offset from the TOC base.
841 // This is consistent with the treatment used both by XL C/C++ and
842 // by AIX ld -r.
843 ptrdiff_t Adjustment =
844 OriginalOffset - llvm::SignExtend32<16>(OriginalOffset);
846 Expr, MCConstantExpr::create(-Adjustment, OutContext), OutContext);
847 };
848
849 auto getTOCEntryLoadingExprForXCOFF =
850 [IsPPC64, getTOCRelocAdjustedExprForXCOFF,
851 this](const MCSymbol *MOSymbol, const MCExpr *Expr,
853 MCSymbolRefExpr::VariantKind::VK_None) -> const MCExpr * {
854 const unsigned EntryByteSize = IsPPC64 ? 8 : 4;
855 const auto TOCEntryIter = TOC.find({MOSymbol, VK});
856 assert(TOCEntryIter != TOC.end() &&
857 "Could not find the TOC entry for this symbol.");
858 const ptrdiff_t EntryDistanceFromTOCBase =
859 (TOCEntryIter - TOC.begin()) * EntryByteSize;
860 constexpr int16_t PositiveTOCRange = INT16_MAX;
861
862 if (EntryDistanceFromTOCBase > PositiveTOCRange)
863 return getTOCRelocAdjustedExprForXCOFF(Expr, EntryDistanceFromTOCBase);
864
865 return Expr;
866 };
867 auto GetVKForMO = [&](const MachineOperand &MO) {
868 // For TLS initial-exec and local-exec accesses on AIX, we have one TOC
869 // entry for the symbol (with the variable offset), which is differentiated
870 // by MO_TPREL_FLAG.
871 unsigned Flag = MO.getTargetFlags();
872 if (Flag == PPCII::MO_TPREL_FLAG ||
875 assert(MO.isGlobal() && "Only expecting a global MachineOperand here!\n");
876 TLSModel::Model Model = TM.getTLSModel(MO.getGlobal());
877 if (Model == TLSModel::LocalExec)
878 return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSLE;
879 if (Model == TLSModel::InitialExec)
880 return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSIE;
881 llvm_unreachable("Only expecting local-exec or initial-exec accesses!");
882 }
883 // For GD TLS access on AIX, we have two TOC entries for the symbol (one for
884 // the variable offset and the other for the region handle). They are
885 // differentiated by MO_TLSGD_FLAG and MO_TLSGDM_FLAG.
886 if (Flag == PPCII::MO_TLSGDM_FLAG)
887 return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM;
889 return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGD;
890 // For local-dynamic TLS access on AIX, we have one TOC entry for the symbol
891 // (the variable offset) and one shared TOC entry for the module handle.
892 // They are differentiated by MO_TLSLD_FLAG and MO_TLSLDM_FLAG.
893 if (Flag == PPCII::MO_TLSLD_FLAG && IsAIX)
894 return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSLD;
895 if (Flag == PPCII::MO_TLSLDM_FLAG && IsAIX)
896 return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSML;
897 return MCSymbolRefExpr::VariantKind::VK_None;
898 };
899
900 // Lower multi-instruction pseudo operations.
901 switch (MI->getOpcode()) {
902 default: break;
903 case TargetOpcode::DBG_VALUE:
904 llvm_unreachable("Should be handled target independently");
905 case TargetOpcode::STACKMAP:
906 return LowerSTACKMAP(SM, *MI);
907 case TargetOpcode::PATCHPOINT:
908 return LowerPATCHPOINT(SM, *MI);
909
910 case PPC::MoveGOTtoLR: {
911 // Transform %lr = MoveGOTtoLR
912 // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4
913 // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding
914 // _GLOBAL_OFFSET_TABLE_) has exactly one instruction:
915 // blrl
916 // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local
917 MCSymbol *GOTSymbol =
918 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
919 const MCExpr *OffsExpr =
922 OutContext),
923 MCConstantExpr::create(4, OutContext),
924 OutContext);
925
926 // Emit the 'bl'.
927 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr));
928 return;
929 }
930 case PPC::MovePCtoLR:
931 case PPC::MovePCtoLR8: {
932 // Transform %lr = MovePCtoLR
933 // Into this, where the label is the PIC base:
934 // bl L1$pb
935 // L1$pb:
936 MCSymbol *PICBase = MF->getPICBaseSymbol();
937
938 // Emit the 'bl'.
939 EmitToStreamer(*OutStreamer,
940 MCInstBuilder(PPC::BL)
941 // FIXME: We would like an efficient form for this, so we
942 // don't have to do a lot of extra uniquing.
943 .addExpr(MCSymbolRefExpr::create(PICBase, OutContext)));
944
945 // Emit the label.
946 OutStreamer->emitLabel(PICBase);
947 return;
948 }
949 case PPC::UpdateGBR: {
950 // Transform %rd = UpdateGBR(%rt, %ri)
951 // Into: lwz %rt, .L0$poff - .L0$pb(%ri)
952 // add %rd, %rt, %ri
953 // or into (if secure plt mode is on):
954 // addis r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@ha
955 // addi r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@l
956 // Get the offset from the GOT Base Register to the GOT
957 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
958 if (Subtarget->isSecurePlt() && isPositionIndependent() ) {
959 unsigned PICR = TmpInst.getOperand(0).getReg();
960 MCSymbol *BaseSymbol = OutContext.getOrCreateSymbol(
961 M->getPICLevel() == PICLevel::SmallPIC ? "_GLOBAL_OFFSET_TABLE_"
962 : ".LTOC");
963 const MCExpr *PB =
964 MCSymbolRefExpr::create(MF->getPICBaseSymbol(), OutContext);
965
966 const MCExpr *DeltaExpr = MCBinaryExpr::createSub(
967 MCSymbolRefExpr::create(BaseSymbol, OutContext), PB, OutContext);
968
969 const MCExpr *DeltaHi = PPCMCExpr::createHa(DeltaExpr, OutContext);
970 EmitToStreamer(
971 *OutStreamer,
972 MCInstBuilder(PPC::ADDIS).addReg(PICR).addReg(PICR).addExpr(DeltaHi));
973
974 const MCExpr *DeltaLo = PPCMCExpr::createLo(DeltaExpr, OutContext);
975 EmitToStreamer(
976 *OutStreamer,
977 MCInstBuilder(PPC::ADDI).addReg(PICR).addReg(PICR).addExpr(DeltaLo));
978 return;
979 } else {
980 MCSymbol *PICOffset =
981 MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol(*MF);
982 TmpInst.setOpcode(PPC::LWZ);
983 const MCExpr *Exp =
985 const MCExpr *PB =
986 MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
988 OutContext);
989 const MCOperand TR = TmpInst.getOperand(1);
990 const MCOperand PICR = TmpInst.getOperand(0);
991
992 // Step 1: lwz %rt, .L$poff - .L$pb(%ri)
993 TmpInst.getOperand(1) =
995 TmpInst.getOperand(0) = TR;
996 TmpInst.getOperand(2) = PICR;
997 EmitToStreamer(*OutStreamer, TmpInst);
998
999 TmpInst.setOpcode(PPC::ADD4);
1000 TmpInst.getOperand(0) = PICR;
1001 TmpInst.getOperand(1) = TR;
1002 TmpInst.getOperand(2) = PICR;
1003 EmitToStreamer(*OutStreamer, TmpInst);
1004 return;
1005 }
1006 }
1007 case PPC::LWZtoc: {
1008 // Transform %rN = LWZtoc @op1, %r2
1009 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1010
1011 // Change the opcode to LWZ.
1012 TmpInst.setOpcode(PPC::LWZ);
1013
1014 const MachineOperand &MO = MI->getOperand(1);
1015 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
1016 "Invalid operand for LWZtoc.");
1017
1018 // Map the operand to its corresponding MCSymbol.
1019 const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1020
1021 // Create a reference to the GOT entry for the symbol. The GOT entry will be
1022 // synthesized later.
1023 if (PL == PICLevel::SmallPIC && !IsAIX) {
1024 const MCExpr *Exp =
1026 OutContext);
1027 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1028 EmitToStreamer(*OutStreamer, TmpInst);
1029 return;
1030 }
1031
1032 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1033
1034 // Otherwise, use the TOC. 'TOCEntry' is a label used to reference the
1035 // storage allocated in the TOC which contains the address of
1036 // 'MOSymbol'. Said TOC entry will be synthesized later.
1037 MCSymbol *TOCEntry =
1038 lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
1039 const MCExpr *Exp =
1041
1042 // AIX uses the label directly as the lwz displacement operand for
1043 // references into the toc section. The displacement value will be generated
1044 // relative to the toc-base.
1045 if (IsAIX) {
1046 assert(
1047 getCodeModel(*Subtarget, TM, MO) == CodeModel::Small &&
1048 "This pseudo should only be selected for 32-bit small code model.");
1049 Exp = getTOCEntryLoadingExprForXCOFF(MOSymbol, Exp, VK);
1050 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1051
1052 // Print MO for better readability
1053 if (isVerbose())
1054 OutStreamer->getCommentOS() << MO << '\n';
1055 EmitToStreamer(*OutStreamer, TmpInst);
1056 return;
1057 }
1058
1059 // Create an explicit subtract expression between the local symbol and
1060 // '.LTOC' to manifest the toc-relative offset.
1062 OutContext.getOrCreateSymbol(Twine(".LTOC")), OutContext);
1063 Exp = MCBinaryExpr::createSub(Exp, PB, OutContext);
1064 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1065 EmitToStreamer(*OutStreamer, TmpInst);
1066 return;
1067 }
1068 case PPC::ADDItoc:
1069 case PPC::ADDItoc8: {
1070 assert(IsAIX && TM.getCodeModel() == CodeModel::Small &&
1071 "PseudoOp only valid for small code model AIX");
1072
1073 // Transform %rN = ADDItoc/8 @op1, %r2.
1074 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1075
1076 // Change the opcode to load address.
1077 TmpInst.setOpcode((!IsPPC64) ? (PPC::LA) : (PPC::LA8));
1078
1079 const MachineOperand &MO = MI->getOperand(1);
1080 assert(MO.isGlobal() && "Invalid operand for ADDItoc[8].");
1081
1082 // Map the operand to its corresponding MCSymbol.
1083 const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1084
1085 const MCExpr *Exp =
1087
1088 TmpInst.getOperand(1) = TmpInst.getOperand(2);
1089 TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
1090 EmitToStreamer(*OutStreamer, TmpInst);
1091 return;
1092 }
1093 case PPC::LDtocJTI:
1094 case PPC::LDtocCPT:
1095 case PPC::LDtocBA:
1096 case PPC::LDtoc: {
1097 // Transform %x3 = LDtoc @min1, %x2
1098 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1099
1100 // Change the opcode to LD.
1101 TmpInst.setOpcode(PPC::LD);
1102
1103 const MachineOperand &MO = MI->getOperand(1);
1104 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
1105 "Invalid operand!");
1106
1107 // Map the operand to its corresponding MCSymbol.
1108 const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1109
1110 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1111
1112 // Map the machine operand to its corresponding MCSymbol, then map the
1113 // global address operand to be a reference to the TOC entry we will
1114 // synthesize later.
1115 MCSymbol *TOCEntry =
1116 lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
1117
1120 const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry, VKExpr, OutContext);
1121 TmpInst.getOperand(1) = MCOperand::createExpr(
1122 IsAIX ? getTOCEntryLoadingExprForXCOFF(MOSymbol, Exp, VK) : Exp);
1123
1124 // Print MO for better readability
1125 if (isVerbose() && IsAIX)
1126 OutStreamer->getCommentOS() << MO << '\n';
1127 EmitToStreamer(*OutStreamer, TmpInst);
1128 return;
1129 }
1130 case PPC::ADDIStocHA: {
1131 const MachineOperand &MO = MI->getOperand(2);
1132
1133 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
1134 "Invalid operand for ADDIStocHA.");
1135 assert((IsAIX && !IsPPC64 &&
1136 getCodeModel(*Subtarget, TM, MO) == CodeModel::Large) &&
1137 "This pseudo should only be selected for 32-bit large code model on"
1138 " AIX.");
1139
1140 // Transform %rd = ADDIStocHA %rA, @sym(%r2)
1141 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1142
1143 // Change the opcode to ADDIS.
1144 TmpInst.setOpcode(PPC::ADDIS);
1145
1146 // Map the machine operand to its corresponding MCSymbol.
1147 MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1148
1149 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1150
1151 // If the symbol isn't toc-data then use the TOC on AIX.
1152 // Map the global address operand to be a reference to the TOC entry we
1153 // will synthesize later. 'TOCEntry' is a label used to reference the
1154 // storage allocated in the TOC which contains the address of 'MOSymbol'.
1155 // If the toc-data attribute is used, the TOC entry contains the data
1156 // rather than the address of the MOSymbol.
1157 if (![](const MachineOperand &MO) {
1158 if (!MO.isGlobal())
1159 return false;
1160
1161 const GlobalVariable *GV = dyn_cast<GlobalVariable>(MO.getGlobal());
1162 if (!GV)
1163 return false;
1164
1165 return GV->hasAttribute("toc-data");
1166 }(MO)) {
1167 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
1168 }
1169
1171 MOSymbol, MCSymbolRefExpr::VK_PPC_U, OutContext);
1172 TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
1173 EmitToStreamer(*OutStreamer, TmpInst);
1174 return;
1175 }
1176 case PPC::LWZtocL: {
1177 const MachineOperand &MO = MI->getOperand(1);
1178
1179 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
1180 "Invalid operand for LWZtocL.");
1181 assert(IsAIX && !IsPPC64 &&
1182 getCodeModel(*Subtarget, TM, MO) == CodeModel::Large &&
1183 "This pseudo should only be selected for 32-bit large code model on"
1184 " AIX.");
1185
1186 // Transform %rd = LWZtocL @sym, %rs.
1187 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1188
1189 // Change the opcode to lwz.
1190 TmpInst.setOpcode(PPC::LWZ);
1191
1192 // Map the machine operand to its corresponding MCSymbol.
1193 MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1194
1195 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1196
1197 // Always use TOC on AIX. Map the global address operand to be a reference
1198 // to the TOC entry we will synthesize later. 'TOCEntry' is a label used to
1199 // reference the storage allocated in the TOC which contains the address of
1200 // 'MOSymbol'.
1201 MCSymbol *TOCEntry =
1202 lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
1203 const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry,
1205 OutContext);
1206 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1207 EmitToStreamer(*OutStreamer, TmpInst);
1208 return;
1209 }
1210 case PPC::ADDIStocHA8: {
1211 // Transform %xd = ADDIStocHA8 %x2, @sym
1212 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1213
1214 // Change the opcode to ADDIS8. If the global address is the address of
1215 // an external symbol, is a jump table address, is a block address, or is a
1216 // constant pool index with large code model enabled, then generate a TOC
1217 // entry and reference that. Otherwise, reference the symbol directly.
1218 TmpInst.setOpcode(PPC::ADDIS8);
1219
1220 const MachineOperand &MO = MI->getOperand(2);
1221 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
1222 "Invalid operand for ADDIStocHA8!");
1223
1224 const MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1225
1226 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1227
1228 const bool GlobalToc =
1229 MO.isGlobal() && Subtarget->isGVIndirectSymbol(MO.getGlobal());
1230
1231 const CodeModel::Model CM =
1232 IsAIX ? getCodeModel(*Subtarget, TM, MO) : TM.getCodeModel();
1233
1234 if (GlobalToc || MO.isJTI() || MO.isBlockAddress() ||
1235 (MO.isCPI() && CM == CodeModel::Large))
1236 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
1237
1239
1240 const MCExpr *Exp =
1241 MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
1242
1243 if (!MO.isJTI() && MO.getOffset())
1246 OutContext),
1247 OutContext);
1248
1249 TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
1250 EmitToStreamer(*OutStreamer, TmpInst);
1251 return;
1252 }
1253 case PPC::LDtocL: {
1254 // Transform %xd = LDtocL @sym, %xs
1255 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1256
1257 // Change the opcode to LD. If the global address is the address of
1258 // an external symbol, is a jump table address, is a block address, or is
1259 // a constant pool index with large code model enabled, then generate a
1260 // TOC entry and reference that. Otherwise, reference the symbol directly.
1261 TmpInst.setOpcode(PPC::LD);
1262
1263 const MachineOperand &MO = MI->getOperand(1);
1264 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
1265 MO.isBlockAddress()) &&
1266 "Invalid operand for LDtocL!");
1267
1269 (!MO.isGlobal() || Subtarget->isGVIndirectSymbol(MO.getGlobal())) &&
1270 "LDtocL used on symbol that could be accessed directly is "
1271 "invalid. Must match ADDIStocHA8."));
1272
1273 const MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1274
1275 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1276 CodeModel::Model CM =
1277 IsAIX ? getCodeModel(*Subtarget, TM, MO) : TM.getCodeModel();
1278 if (!MO.isCPI() || CM == CodeModel::Large)
1279 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
1280
1282 const MCExpr *Exp =
1283 MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
1284 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1285 EmitToStreamer(*OutStreamer, TmpInst);
1286 return;
1287 }
1288 case PPC::ADDItocL:
1289 case PPC::ADDItocL8: {
1290 // Transform %xd = ADDItocL %xs, @sym
1291 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1292
1293 unsigned Op = MI->getOpcode();
1294
1295 // Change the opcode to load address for tocdata
1296 TmpInst.setOpcode(Op == PPC::ADDItocL8 ? PPC::ADDI8 : PPC::LA);
1297
1298 const MachineOperand &MO = MI->getOperand(2);
1299 assert((Op == PPC::ADDItocL8)
1300 ? (MO.isGlobal() || MO.isCPI())
1301 : MO.isGlobal() && "Invalid operand for ADDItocL8.");
1302 assert(!(MO.isGlobal() && Subtarget->isGVIndirectSymbol(MO.getGlobal())) &&
1303 "Interposable definitions must use indirect accesses.");
1304
1305 // Map the operand to its corresponding MCSymbol.
1306 const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1307
1309 MOSymbol,
1310 Op == PPC::ADDItocL8 ? MCSymbolRefExpr::VK_PPC_TOC_LO
1312 OutContext);
1313
1314 TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
1315 EmitToStreamer(*OutStreamer, TmpInst);
1316 return;
1317 }
1318 case PPC::ADDISgotTprelHA: {
1319 // Transform: %xd = ADDISgotTprelHA %x2, @sym
1320 // Into: %xd = ADDIS8 %x2, sym@got@tlsgd@ha
1321 assert(IsPPC64 && "Not supported for 32-bit PowerPC");
1322 const MachineOperand &MO = MI->getOperand(2);
1323 const GlobalValue *GValue = MO.getGlobal();
1324 MCSymbol *MOSymbol = getSymbol(GValue);
1325 const MCExpr *SymGotTprel =
1327 OutContext);
1328 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
1329 .addReg(MI->getOperand(0).getReg())
1330 .addReg(MI->getOperand(1).getReg())
1331 .addExpr(SymGotTprel));
1332 return;
1333 }
1334 case PPC::LDgotTprelL:
1335 case PPC::LDgotTprelL32: {
1336 // Transform %xd = LDgotTprelL @sym, %xs
1337 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1338
1339 // Change the opcode to LD.
1340 TmpInst.setOpcode(IsPPC64 ? PPC::LD : PPC::LWZ);
1341 const MachineOperand &MO = MI->getOperand(1);
1342 const GlobalValue *GValue = MO.getGlobal();
1343 MCSymbol *MOSymbol = getSymbol(GValue);
1345 MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO
1347 OutContext);
1348 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1349 EmitToStreamer(*OutStreamer, TmpInst);
1350 return;
1351 }
1352
1353 case PPC::PPC32PICGOT: {
1354 MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
1355 MCSymbol *GOTRef = OutContext.createTempSymbol();
1356 MCSymbol *NextInstr = OutContext.createTempSymbol();
1357
1358 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL)
1359 // FIXME: We would like an efficient form for this, so we don't have to do
1360 // a lot of extra uniquing.
1361 .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext)));
1362 const MCExpr *OffsExpr =
1363 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext),
1364 MCSymbolRefExpr::create(GOTRef, OutContext),
1365 OutContext);
1366 OutStreamer->emitLabel(GOTRef);
1367 OutStreamer->emitValue(OffsExpr, 4);
1368 OutStreamer->emitLabel(NextInstr);
1369 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR)
1370 .addReg(MI->getOperand(0).getReg()));
1371 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ)
1372 .addReg(MI->getOperand(1).getReg())
1373 .addImm(0)
1374 .addReg(MI->getOperand(0).getReg()));
1375 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4)
1376 .addReg(MI->getOperand(0).getReg())
1377 .addReg(MI->getOperand(1).getReg())
1378 .addReg(MI->getOperand(0).getReg()));
1379 return;
1380 }
1381 case PPC::PPC32GOT: {
1382 MCSymbol *GOTSymbol =
1383 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
1384 const MCExpr *SymGotTlsL = MCSymbolRefExpr::create(
1385 GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, OutContext);
1386 const MCExpr *SymGotTlsHA = MCSymbolRefExpr::create(
1387 GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, OutContext);
1388 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI)
1389 .addReg(MI->getOperand(0).getReg())
1390 .addExpr(SymGotTlsL));
1391 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
1392 .addReg(MI->getOperand(0).getReg())
1393 .addReg(MI->getOperand(0).getReg())
1394 .addExpr(SymGotTlsHA));
1395 return;
1396 }
1397 case PPC::ADDIStlsgdHA: {
1398 // Transform: %xd = ADDIStlsgdHA %x2, @sym
1399 // Into: %xd = ADDIS8 %x2, sym@got@tlsgd@ha
1400 assert(IsPPC64 && "Not supported for 32-bit PowerPC");
1401 const MachineOperand &MO = MI->getOperand(2);
1402 const GlobalValue *GValue = MO.getGlobal();
1403 MCSymbol *MOSymbol = getSymbol(GValue);
1404 const MCExpr *SymGotTlsGD =
1406 OutContext);
1407 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
1408 .addReg(MI->getOperand(0).getReg())
1409 .addReg(MI->getOperand(1).getReg())
1410 .addExpr(SymGotTlsGD));
1411 return;
1412 }
1413 case PPC::ADDItlsgdL:
1414 // Transform: %xd = ADDItlsgdL %xs, @sym
1415 // Into: %xd = ADDI8 %xs, sym@got@tlsgd@l
1416 case PPC::ADDItlsgdL32: {
1417 // Transform: %rd = ADDItlsgdL32 %rs, @sym
1418 // Into: %rd = ADDI %rs, sym@got@tlsgd
1419 const MachineOperand &MO = MI->getOperand(2);
1420 const GlobalValue *GValue = MO.getGlobal();
1421 MCSymbol *MOSymbol = getSymbol(GValue);
1422 const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create(
1423 MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO
1425 OutContext);
1426 EmitToStreamer(*OutStreamer,
1427 MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
1428 .addReg(MI->getOperand(0).getReg())
1429 .addReg(MI->getOperand(1).getReg())
1430 .addExpr(SymGotTlsGD));
1431 return;
1432 }
1433 case PPC::GETtlsMOD32AIX:
1434 case PPC::GETtlsMOD64AIX:
1435 // Transform: %r3 = GETtlsMODNNAIX %r3 (for NN == 32/64).
1436 // Into: BLA .__tls_get_mod()
1437 // Input parameter is a module handle (_$TLSML[TC]@ml) for all variables.
1438 case PPC::GETtlsADDR:
1439 // Transform: %x3 = GETtlsADDR %x3, @sym
1440 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd)
1441 case PPC::GETtlsADDRPCREL:
1442 case PPC::GETtlsADDR32AIX:
1443 case PPC::GETtlsADDR64AIX:
1444 // Transform: %r3 = GETtlsADDRNNAIX %r3, %r4 (for NN == 32/64).
1445 // Into: BLA .__tls_get_addr()
1446 // Unlike on Linux, there is no symbol or relocation needed for this call.
1447 case PPC::GETtlsADDR32: {
1448 // Transform: %r3 = GETtlsADDR32 %r3, @sym
1449 // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT
1450 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD);
1451 return;
1452 }
1453 case PPC::GETtlsTpointer32AIX: {
1454 // Transform: %r3 = GETtlsTpointer32AIX
1455 // Into: BLA .__get_tpointer()
1456 EmitAIXTlsCallHelper(MI);
1457 return;
1458 }
1459 case PPC::ADDIStlsldHA: {
1460 // Transform: %xd = ADDIStlsldHA %x2, @sym
1461 // Into: %xd = ADDIS8 %x2, sym@got@tlsld@ha
1462 assert(IsPPC64 && "Not supported for 32-bit PowerPC");
1463 const MachineOperand &MO = MI->getOperand(2);
1464 const GlobalValue *GValue = MO.getGlobal();
1465 MCSymbol *MOSymbol = getSymbol(GValue);
1466 const MCExpr *SymGotTlsLD =
1468 OutContext);
1469 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
1470 .addReg(MI->getOperand(0).getReg())
1471 .addReg(MI->getOperand(1).getReg())
1472 .addExpr(SymGotTlsLD));
1473 return;
1474 }
1475 case PPC::ADDItlsldL:
1476 // Transform: %xd = ADDItlsldL %xs, @sym
1477 // Into: %xd = ADDI8 %xs, sym@got@tlsld@l
1478 case PPC::ADDItlsldL32: {
1479 // Transform: %rd = ADDItlsldL32 %rs, @sym
1480 // Into: %rd = ADDI %rs, sym@got@tlsld
1481 const MachineOperand &MO = MI->getOperand(2);
1482 const GlobalValue *GValue = MO.getGlobal();
1483 MCSymbol *MOSymbol = getSymbol(GValue);
1484 const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create(
1485 MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO
1487 OutContext);
1488 EmitToStreamer(*OutStreamer,
1489 MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
1490 .addReg(MI->getOperand(0).getReg())
1491 .addReg(MI->getOperand(1).getReg())
1492 .addExpr(SymGotTlsLD));
1493 return;
1494 }
1495 case PPC::GETtlsldADDR:
1496 // Transform: %x3 = GETtlsldADDR %x3, @sym
1497 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld)
1498 case PPC::GETtlsldADDRPCREL:
1499 case PPC::GETtlsldADDR32: {
1500 // Transform: %r3 = GETtlsldADDR32 %r3, @sym
1501 // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT
1502 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD);
1503 return;
1504 }
1505 case PPC::ADDISdtprelHA:
1506 // Transform: %xd = ADDISdtprelHA %xs, @sym
1507 // Into: %xd = ADDIS8 %xs, sym@dtprel@ha
1508 case PPC::ADDISdtprelHA32: {
1509 // Transform: %rd = ADDISdtprelHA32 %rs, @sym
1510 // Into: %rd = ADDIS %rs, sym@dtprel@ha
1511 const MachineOperand &MO = MI->getOperand(2);
1512 const GlobalValue *GValue = MO.getGlobal();
1513 MCSymbol *MOSymbol = getSymbol(GValue);
1514 const MCExpr *SymDtprel =
1516 OutContext);
1517 EmitToStreamer(
1518 *OutStreamer,
1519 MCInstBuilder(IsPPC64 ? PPC::ADDIS8 : PPC::ADDIS)
1520 .addReg(MI->getOperand(0).getReg())
1521 .addReg(MI->getOperand(1).getReg())
1522 .addExpr(SymDtprel));
1523 return;
1524 }
1525 case PPC::PADDIdtprel: {
1526 // Transform: %rd = PADDIdtprel %rs, @sym
1527 // Into: %rd = PADDI8 %rs, sym@dtprel
1528 const MachineOperand &MO = MI->getOperand(2);
1529 const GlobalValue *GValue = MO.getGlobal();
1530 MCSymbol *MOSymbol = getSymbol(GValue);
1531 const MCExpr *SymDtprel = MCSymbolRefExpr::create(
1532 MOSymbol, MCSymbolRefExpr::VK_DTPREL, OutContext);
1533 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::PADDI8)
1534 .addReg(MI->getOperand(0).getReg())
1535 .addReg(MI->getOperand(1).getReg())
1536 .addExpr(SymDtprel));
1537 return;
1538 }
1539
1540 case PPC::ADDIdtprelL:
1541 // Transform: %xd = ADDIdtprelL %xs, @sym
1542 // Into: %xd = ADDI8 %xs, sym@dtprel@l
1543 case PPC::ADDIdtprelL32: {
1544 // Transform: %rd = ADDIdtprelL32 %rs, @sym
1545 // Into: %rd = ADDI %rs, sym@dtprel@l
1546 const MachineOperand &MO = MI->getOperand(2);
1547 const GlobalValue *GValue = MO.getGlobal();
1548 MCSymbol *MOSymbol = getSymbol(GValue);
1549 const MCExpr *SymDtprel =
1551 OutContext);
1552 EmitToStreamer(*OutStreamer,
1553 MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
1554 .addReg(MI->getOperand(0).getReg())
1555 .addReg(MI->getOperand(1).getReg())
1556 .addExpr(SymDtprel));
1557 return;
1558 }
1559 case PPC::MFOCRF:
1560 case PPC::MFOCRF8:
1561 if (!Subtarget->hasMFOCRF()) {
1562 // Transform: %r3 = MFOCRF %cr7
1563 // Into: %r3 = MFCR ;; cr7
1564 unsigned NewOpcode =
1565 MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8;
1566 OutStreamer->AddComment(PPCInstPrinter::
1567 getRegisterName(MI->getOperand(1).getReg()));
1568 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
1569 .addReg(MI->getOperand(0).getReg()));
1570 return;
1571 }
1572 break;
1573 case PPC::MTOCRF:
1574 case PPC::MTOCRF8:
1575 if (!Subtarget->hasMFOCRF()) {
1576 // Transform: %cr7 = MTOCRF %r3
1577 // Into: MTCRF mask, %r3 ;; cr7
1578 unsigned NewOpcode =
1579 MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8;
1580 unsigned Mask = 0x80 >> OutContext.getRegisterInfo()
1581 ->getEncodingValue(MI->getOperand(0).getReg());
1582 OutStreamer->AddComment(PPCInstPrinter::
1583 getRegisterName(MI->getOperand(0).getReg()));
1584 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
1585 .addImm(Mask)
1586 .addReg(MI->getOperand(1).getReg()));
1587 return;
1588 }
1589 break;
1590 case PPC::LD:
1591 case PPC::STD:
1592 case PPC::LWA_32:
1593 case PPC::LWA: {
1594 // Verify alignment is legal, so we don't create relocations
1595 // that can't be supported.
1596 unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1;
1597 // For non-TOC-based local-exec TLS accesses with non-zero offsets, the
1598 // machine operand (which is a TargetGlobalTLSAddress) is expected to be
1599 // the same operand for both loads and stores.
1600 for (const MachineOperand &TempMO : MI->operands()) {
1601 if (((TempMO.getTargetFlags() == PPCII::MO_TPREL_FLAG)) &&
1602 TempMO.getOperandNo() == 1)
1603 OpNum = 1;
1604 }
1605 const MachineOperand &MO = MI->getOperand(OpNum);
1606 if (MO.isGlobal()) {
1607 const DataLayout &DL = MO.getGlobal()->getParent()->getDataLayout();
1608 if (MO.getGlobal()->getPointerAlignment(DL) < 4)
1609 llvm_unreachable("Global must be word-aligned for LD, STD, LWA!");
1610 }
1611 // As these load/stores share common code with the following load/stores,
1612 // fall through to the subsequent cases in order to either process the
1613 // non-TOC-based local-exec sequence or to process the instruction normally.
1614 [[fallthrough]];
1615 }
1616 case PPC::LBZ:
1617 case PPC::LBZ8:
1618 case PPC::LHA:
1619 case PPC::LHA8:
1620 case PPC::LHZ:
1621 case PPC::LHZ8:
1622 case PPC::LWZ:
1623 case PPC::LWZ8:
1624 case PPC::STB:
1625 case PPC::STB8:
1626 case PPC::STH:
1627 case PPC::STH8:
1628 case PPC::STW:
1629 case PPC::STW8:
1630 case PPC::LFS:
1631 case PPC::STFS:
1632 case PPC::LFD:
1633 case PPC::STFD:
1634 case PPC::ADDI8: {
1635 // A faster non-TOC-based local-[exec|dynamic] sequence is represented by
1636 // `addi` or a load/store instruction (that directly loads or stores off of
1637 // the thread pointer) with an immediate operand having the MO_TPREL_FLAG.
1638 // Such instructions do not otherwise arise.
1639 if (!HasAIXSmallLocalTLS)
1640 break;
1641 bool IsMIADDI8 = MI->getOpcode() == PPC::ADDI8;
1642 unsigned OpNum = IsMIADDI8 ? 2 : 1;
1643 const MachineOperand &MO = MI->getOperand(OpNum);
1644 unsigned Flag = MO.getTargetFlags();
1645 if (Flag == PPCII::MO_TPREL_FLAG ||
1648 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1649
1650 const MCExpr *Expr = getAdjustedLocalExecExpr(MO, MO.getOffset());
1651 if (Expr)
1652 TmpInst.getOperand(OpNum) = MCOperand::createExpr(Expr);
1653
1654 // Change the opcode to load address if the original opcode is an `addi`.
1655 if (IsMIADDI8)
1656 TmpInst.setOpcode(PPC::LA8);
1657
1658 EmitToStreamer(*OutStreamer, TmpInst);
1659 return;
1660 }
1661 // Now process the instruction normally.
1662 break;
1663 }
1664 case PPC::PseudoEIEIO: {
1665 EmitToStreamer(
1666 *OutStreamer,
1667 MCInstBuilder(PPC::ORI).addReg(PPC::X2).addReg(PPC::X2).addImm(0));
1668 EmitToStreamer(
1669 *OutStreamer,
1670 MCInstBuilder(PPC::ORI).addReg(PPC::X2).addReg(PPC::X2).addImm(0));
1671 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::EnforceIEIO));
1672 return;
1673 }
1674 }
1675
1676 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1677 EmitToStreamer(*OutStreamer, TmpInst);
1678}
1679
1680// For non-TOC-based local-exec variables that have a non-zero offset,
1681// we need to create a new MCExpr that adds the non-zero offset to the address
1682// of the local-exec variable that will be used in either an addi, load or
1683// store. However, the final displacement for these instructions must be
1684// between [-32768, 32768), so if the TLS address + its non-zero offset is
1685// greater than 32KB, a new MCExpr is produced to accommodate this situation.
1686const MCExpr *PPCAsmPrinter::getAdjustedLocalExecExpr(const MachineOperand &MO,
1687 int64_t Offset) {
1688 // Non-zero offsets (for loads, stores or `addi`) require additional handling.
1689 // When the offset is zero, there is no need to create an adjusted MCExpr.
1690 if (!Offset)
1691 return nullptr;
1692
1693 assert(MO.isGlobal() && "Only expecting a global MachineOperand here!");
1694 const GlobalValue *GValue = MO.getGlobal();
1695 // TODO: Handle the aix-small-local-dynamic-tls non-zero offset case.
1696 TLSModel::Model Model = TM.getTLSModel(GValue);
1697 if (Model == TLSModel::LocalDynamic) {
1698 return nullptr;
1699 }
1700 assert(Model == TLSModel::LocalExec &&
1701 "Only local-exec accesses are handled!");
1702
1703 bool IsGlobalADeclaration = GValue->isDeclarationForLinker();
1704 // Find the GlobalVariable that corresponds to the particular TLS variable
1705 // in the TLS variable-to-address mapping. All TLS variables should exist
1706 // within this map, with the exception of TLS variables marked as extern.
1707 const auto TLSVarsMapEntryIter = TLSVarsToAddressMapping.find(GValue);
1708 if (TLSVarsMapEntryIter == TLSVarsToAddressMapping.end())
1709 assert(IsGlobalADeclaration &&
1710 "Only expecting to find extern TLS variables not present in the TLS "
1711 "variable-to-address map!");
1712
1713 unsigned TLSVarAddress =
1714 IsGlobalADeclaration ? 0 : TLSVarsMapEntryIter->second;
1715 ptrdiff_t FinalAddress = (TLSVarAddress + Offset);
1716 // If the address of the TLS variable + the offset is less than 32KB,
1717 // or if the TLS variable is extern, we simply produce an MCExpr to add the
1718 // non-zero offset to the TLS variable address.
1719 // For when TLS variables are extern, this is safe to do because we can
1720 // assume that the address of extern TLS variables are zero.
1721 const MCExpr *Expr = MCSymbolRefExpr::create(
1722 getSymbol(GValue), MCSymbolRefExpr::VK_PPC_AIX_TLSLE, OutContext);
1724 Expr, MCConstantExpr::create(Offset, OutContext), OutContext);
1725 if (FinalAddress >= 32768) {
1726 // Handle the written offset for cases where:
1727 // TLS variable address + Offset > 32KB.
1728
1729 // The assembly that is printed will look like:
1730 // TLSVar@le + Offset - Delta
1731 // where Delta is a multiple of 64KB: ((FinalAddress + 32768) & ~0xFFFF).
1732 ptrdiff_t Delta = ((FinalAddress + 32768) & ~0xFFFF);
1733 // Check that the total instruction displacement fits within [-32768,32768).
1734 [[maybe_unused]] ptrdiff_t InstDisp = TLSVarAddress + Offset - Delta;
1735 assert(((InstDisp < 32768) &&
1736 (InstDisp >= -32768)) &&
1737 "Expecting the instruction displacement for local-exec TLS "
1738 "variables to be between [-32768, 32768)!");
1740 Expr, MCConstantExpr::create(-Delta, OutContext), OutContext);
1741 }
1742
1743 return Expr;
1744}
1745
1746void PPCLinuxAsmPrinter::emitGNUAttributes(Module &M) {
1747 // Emit float ABI into GNU attribute
1748 Metadata *MD = M.getModuleFlag("float-abi");
1749 MDString *FloatABI = dyn_cast_or_null<MDString>(MD);
1750 if (!FloatABI)
1751 return;
1752 StringRef flt = FloatABI->getString();
1753 // TODO: Support emitting soft-fp and hard double/single attributes.
1754 if (flt == "doubledouble")
1755 OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP,
1756 Val_GNU_Power_ABI_HardFloat_DP |
1757 Val_GNU_Power_ABI_LDBL_IBM128);
1758 else if (flt == "ieeequad")
1759 OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP,
1760 Val_GNU_Power_ABI_HardFloat_DP |
1761 Val_GNU_Power_ABI_LDBL_IEEE128);
1762 else if (flt == "ieeedouble")
1763 OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP,
1764 Val_GNU_Power_ABI_HardFloat_DP |
1765 Val_GNU_Power_ABI_LDBL_64);
1766}
1767
1768void PPCLinuxAsmPrinter::emitInstruction(const MachineInstr *MI) {
1769 if (!Subtarget->isPPC64())
1770 return PPCAsmPrinter::emitInstruction(MI);
1771
1772 switch (MI->getOpcode()) {
1773 default:
1774 return PPCAsmPrinter::emitInstruction(MI);
1775 case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
1776 // .begin:
1777 // b .end # lis 0, FuncId[16..32]
1778 // nop # li 0, FuncId[0..15]
1779 // std 0, -8(1)
1780 // mflr 0
1781 // bl __xray_FunctionEntry
1782 // mtlr 0
1783 // .end:
1784 //
1785 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1786 // of instructions change.
1787 MCSymbol *BeginOfSled = OutContext.createTempSymbol();
1788 MCSymbol *EndOfSled = OutContext.createTempSymbol();
1789 OutStreamer->emitLabel(BeginOfSled);
1790 EmitToStreamer(*OutStreamer,
1791 MCInstBuilder(PPC::B).addExpr(
1792 MCSymbolRefExpr::create(EndOfSled, OutContext)));
1793 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
1794 EmitToStreamer(
1795 *OutStreamer,
1796 MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1));
1797 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0));
1798 EmitToStreamer(*OutStreamer,
1799 MCInstBuilder(PPC::BL8_NOP)
1800 .addExpr(MCSymbolRefExpr::create(
1801 OutContext.getOrCreateSymbol("__xray_FunctionEntry"),
1802 OutContext)));
1803 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0));
1804 OutStreamer->emitLabel(EndOfSled);
1805 recordSled(BeginOfSled, *MI, SledKind::FUNCTION_ENTER, 2);
1806 break;
1807 }
1808 case TargetOpcode::PATCHABLE_RET: {
1809 unsigned RetOpcode = MI->getOperand(0).getImm();
1810 MCInst RetInst;
1811 RetInst.setOpcode(RetOpcode);
1812 for (const auto &MO : llvm::drop_begin(MI->operands())) {
1813 MCOperand MCOp;
1814 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, *this))
1815 RetInst.addOperand(MCOp);
1816 }
1817
1818 bool IsConditional;
1819 if (RetOpcode == PPC::BCCLR) {
1820 IsConditional = true;
1821 } else if (RetOpcode == PPC::TCRETURNdi8 || RetOpcode == PPC::TCRETURNri8 ||
1822 RetOpcode == PPC::TCRETURNai8) {
1823 break;
1824 } else if (RetOpcode == PPC::BLR8 || RetOpcode == PPC::TAILB8) {
1825 IsConditional = false;
1826 } else {
1827 EmitToStreamer(*OutStreamer, RetInst);
1828 break;
1829 }
1830
1831 MCSymbol *FallthroughLabel;
1832 if (IsConditional) {
1833 // Before:
1834 // bgtlr cr0
1835 //
1836 // After:
1837 // ble cr0, .end
1838 // .p2align 3
1839 // .begin:
1840 // blr # lis 0, FuncId[16..32]
1841 // nop # li 0, FuncId[0..15]
1842 // std 0, -8(1)
1843 // mflr 0
1844 // bl __xray_FunctionExit
1845 // mtlr 0
1846 // blr
1847 // .end:
1848 //
1849 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1850 // of instructions change.
1851 FallthroughLabel = OutContext.createTempSymbol();
1852 EmitToStreamer(
1853 *OutStreamer,
1854 MCInstBuilder(PPC::BCC)
1855 .addImm(PPC::InvertPredicate(
1856 static_cast<PPC::Predicate>(MI->getOperand(1).getImm())))
1857 .addReg(MI->getOperand(2).getReg())
1858 .addExpr(MCSymbolRefExpr::create(FallthroughLabel, OutContext)));
1859 RetInst = MCInst();
1860 RetInst.setOpcode(PPC::BLR8);
1861 }
1862 // .p2align 3
1863 // .begin:
1864 // b(lr)? # lis 0, FuncId[16..32]
1865 // nop # li 0, FuncId[0..15]
1866 // std 0, -8(1)
1867 // mflr 0
1868 // bl __xray_FunctionExit
1869 // mtlr 0
1870 // b(lr)?
1871 //
1872 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1873 // of instructions change.
1874 OutStreamer->emitCodeAlignment(Align(8), &getSubtargetInfo());
1875 MCSymbol *BeginOfSled = OutContext.createTempSymbol();
1876 OutStreamer->emitLabel(BeginOfSled);
1877 EmitToStreamer(*OutStreamer, RetInst);
1878 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
1879 EmitToStreamer(
1880 *OutStreamer,
1881 MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1));
1882 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0));
1883 EmitToStreamer(*OutStreamer,
1884 MCInstBuilder(PPC::BL8_NOP)
1885 .addExpr(MCSymbolRefExpr::create(
1886 OutContext.getOrCreateSymbol("__xray_FunctionExit"),
1887 OutContext)));
1888 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0));
1889 EmitToStreamer(*OutStreamer, RetInst);
1890 if (IsConditional)
1891 OutStreamer->emitLabel(FallthroughLabel);
1892 recordSled(BeginOfSled, *MI, SledKind::FUNCTION_EXIT, 2);
1893 break;
1894 }
1895 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
1896 llvm_unreachable("PATCHABLE_FUNCTION_EXIT should never be emitted");
1897 case TargetOpcode::PATCHABLE_TAIL_CALL:
1898 // TODO: Define a trampoline `__xray_FunctionTailExit` and differentiate a
1899 // normal function exit from a tail exit.
1900 llvm_unreachable("Tail call is handled in the normal case. See comments "
1901 "around this assert.");
1902 }
1903}
1904
1905void PPCLinuxAsmPrinter::emitStartOfAsmFile(Module &M) {
1906 if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) {
1907 PPCTargetStreamer *TS =
1908 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1909 TS->emitAbiVersion(2);
1910 }
1911
1912 if (static_cast<const PPCTargetMachine &>(TM).isPPC64() ||
1913 !isPositionIndependent())
1915
1916 if (M.getPICLevel() == PICLevel::SmallPIC)
1918
1919 OutStreamer->switchSection(OutContext.getELFSection(
1921
1922 MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC"));
1923 MCSymbol *CurrentPos = OutContext.createTempSymbol();
1924
1925 OutStreamer->emitLabel(CurrentPos);
1926
1927 // The GOT pointer points to the middle of the GOT, in order to reference the
1928 // entire 64kB range. 0x8000 is the midpoint.
1929 const MCExpr *tocExpr =
1930 MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext),
1931 MCConstantExpr::create(0x8000, OutContext),
1932 OutContext);
1933
1934 OutStreamer->emitAssignment(TOCSym, tocExpr);
1935
1936 OutStreamer->switchSection(getObjFileLowering().getTextSection());
1937}
1938
1939void PPCLinuxAsmPrinter::emitFunctionEntryLabel() {
1940 // linux/ppc32 - Normal entry label.
1941 if (!Subtarget->isPPC64() &&
1942 (!isPositionIndependent() ||
1943 MF->getFunction().getParent()->getPICLevel() == PICLevel::SmallPIC))
1945
1946 if (!Subtarget->isPPC64()) {
1947 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1948 if (PPCFI->usesPICBase() && !Subtarget->isSecurePlt()) {
1949 MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol(*MF);
1950 MCSymbol *PICBase = MF->getPICBaseSymbol();
1951 OutStreamer->emitLabel(RelocSymbol);
1952
1953 const MCExpr *OffsExpr =
1955 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
1956 OutContext),
1957 MCSymbolRefExpr::create(PICBase, OutContext),
1958 OutContext);
1959 OutStreamer->emitValue(OffsExpr, 4);
1960 OutStreamer->emitLabel(CurrentFnSym);
1961 return;
1962 } else
1964 }
1965
1966 // ELFv2 ABI - Normal entry label.
1967 if (Subtarget->isELFv2ABI()) {
1968 // In the Large code model, we allow arbitrary displacements between
1969 // the text section and its associated TOC section. We place the
1970 // full 8-byte offset to the TOC in memory immediately preceding
1971 // the function global entry point.
1972 if (TM.getCodeModel() == CodeModel::Large
1973 && !MF->getRegInfo().use_empty(PPC::X2)) {
1974 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1975
1976 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1977 MCSymbol *GlobalEPSymbol = PPCFI->getGlobalEPSymbol(*MF);
1978 const MCExpr *TOCDeltaExpr =
1979 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
1980 MCSymbolRefExpr::create(GlobalEPSymbol,
1981 OutContext),
1982 OutContext);
1983
1984 OutStreamer->emitLabel(PPCFI->getTOCOffsetSymbol(*MF));
1985 OutStreamer->emitValue(TOCDeltaExpr, 8);
1986 }
1988 }
1989
1990 // Emit an official procedure descriptor.
1991 MCSectionSubPair Current = OutStreamer->getCurrentSection();
1992 MCSectionELF *Section = OutStreamer->getContext().getELFSection(
1994 OutStreamer->switchSection(Section);
1995 OutStreamer->emitLabel(CurrentFnSym);
1996 OutStreamer->emitValueToAlignment(Align(8));
1997 MCSymbol *Symbol1 = CurrentFnSymForSize;
1998 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
1999 // entry point.
2000 OutStreamer->emitValue(MCSymbolRefExpr::create(Symbol1, OutContext),
2001 8 /*size*/);
2002 MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC."));
2003 // Generates a R_PPC64_TOC relocation for TOC base insertion.
2004 OutStreamer->emitValue(
2006 8/*size*/);
2007 // Emit a null environment pointer.
2008 OutStreamer->emitIntValue(0, 8 /* size */);
2009 OutStreamer->switchSection(Current.first, Current.second);
2010}
2011
2012void PPCLinuxAsmPrinter::emitEndOfAsmFile(Module &M) {
2013 const DataLayout &DL = getDataLayout();
2014
2015 bool isPPC64 = DL.getPointerSizeInBits() == 64;
2016
2017 PPCTargetStreamer *TS =
2018 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
2019
2020 // If we are using any values provided by Glibc at fixed addresses,
2021 // we need to ensure that the Glibc used at link time actually provides
2022 // those values. All versions of Glibc that do will define the symbol
2023 // named "__parse_hwcap_and_convert_at_platform".
2024 if (static_cast<const PPCTargetMachine &>(TM).hasGlibcHWCAPAccess())
2025 OutStreamer->emitSymbolValue(
2026 GetExternalSymbolSymbol("__parse_hwcap_and_convert_at_platform"),
2027 MAI->getCodePointerSize());
2028 emitGNUAttributes(M);
2029
2030 if (!TOC.empty()) {
2031 const char *Name = isPPC64 ? ".toc" : ".got2";
2032 MCSectionELF *Section = OutContext.getELFSection(
2034 OutStreamer->switchSection(Section);
2035 if (!isPPC64)
2036 OutStreamer->emitValueToAlignment(Align(4));
2037
2038 for (const auto &TOCMapPair : TOC) {
2039 const MCSymbol *const TOCEntryTarget = TOCMapPair.first.first;
2040 MCSymbol *const TOCEntryLabel = TOCMapPair.second;
2041
2042 OutStreamer->emitLabel(TOCEntryLabel);
2043 if (isPPC64)
2044 TS->emitTCEntry(*TOCEntryTarget, TOCMapPair.first.second);
2045 else
2046 OutStreamer->emitSymbolValue(TOCEntryTarget, 4);
2047 }
2048 }
2049
2050 PPCAsmPrinter::emitEndOfAsmFile(M);
2051}
2052
2053/// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2.
2054void PPCLinuxAsmPrinter::emitFunctionBodyStart() {
2055 // In the ELFv2 ABI, in functions that use the TOC register, we need to
2056 // provide two entry points. The ABI guarantees that when calling the
2057 // local entry point, r2 is set up by the caller to contain the TOC base
2058 // for this function, and when calling the global entry point, r12 is set
2059 // up by the caller to hold the address of the global entry point. We
2060 // thus emit a prefix sequence along the following lines:
2061 //
2062 // func:
2063 // .Lfunc_gepNN:
2064 // # global entry point
2065 // addis r2,r12,(.TOC.-.Lfunc_gepNN)@ha
2066 // addi r2,r2,(.TOC.-.Lfunc_gepNN)@l
2067 // .Lfunc_lepNN:
2068 // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
2069 // # local entry point, followed by function body
2070 //
2071 // For the Large code model, we create
2072 //
2073 // .Lfunc_tocNN:
2074 // .quad .TOC.-.Lfunc_gepNN # done by EmitFunctionEntryLabel
2075 // func:
2076 // .Lfunc_gepNN:
2077 // # global entry point
2078 // ld r2,.Lfunc_tocNN-.Lfunc_gepNN(r12)
2079 // add r2,r2,r12
2080 // .Lfunc_lepNN:
2081 // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
2082 // # local entry point, followed by function body
2083 //
2084 // This ensures we have r2 set up correctly while executing the function
2085 // body, no matter which entry point is called.
2086 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
2087 const bool UsesX2OrR2 = !MF->getRegInfo().use_empty(PPC::X2) ||
2088 !MF->getRegInfo().use_empty(PPC::R2);
2089 const bool PCrelGEPRequired = Subtarget->isUsingPCRelativeCalls() &&
2090 UsesX2OrR2 && PPCFI->usesTOCBasePtr();
2091 const bool NonPCrelGEPRequired = !Subtarget->isUsingPCRelativeCalls() &&
2092 Subtarget->isELFv2ABI() && UsesX2OrR2;
2093
2094 // Only do all that if the function uses R2 as the TOC pointer
2095 // in the first place. We don't need the global entry point if the
2096 // function uses R2 as an allocatable register.
2097 if (NonPCrelGEPRequired || PCrelGEPRequired) {
2098 // Note: The logic here must be synchronized with the code in the
2099 // branch-selection pass which sets the offset of the first block in the
2100 // function. This matters because it affects the alignment.
2101 MCSymbol *GlobalEntryLabel = PPCFI->getGlobalEPSymbol(*MF);
2102 OutStreamer->emitLabel(GlobalEntryLabel);
2103 const MCSymbolRefExpr *GlobalEntryLabelExp =
2104 MCSymbolRefExpr::create(GlobalEntryLabel, OutContext);
2105
2106 if (TM.getCodeModel() != CodeModel::Large) {
2107 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
2108 const MCExpr *TOCDeltaExpr =
2109 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
2110 GlobalEntryLabelExp, OutContext);
2111
2112 const MCExpr *TOCDeltaHi = PPCMCExpr::createHa(TOCDeltaExpr, OutContext);
2113 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
2114 .addReg(PPC::X2)
2115 .addReg(PPC::X12)
2116 .addExpr(TOCDeltaHi));
2117
2118 const MCExpr *TOCDeltaLo = PPCMCExpr::createLo(TOCDeltaExpr, OutContext);
2119 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI)
2120 .addReg(PPC::X2)
2121 .addReg(PPC::X2)
2122 .addExpr(TOCDeltaLo));
2123 } else {
2124 MCSymbol *TOCOffset = PPCFI->getTOCOffsetSymbol(*MF);
2125 const MCExpr *TOCOffsetDeltaExpr =
2126 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCOffset, OutContext),
2127 GlobalEntryLabelExp, OutContext);
2128
2129 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
2130 .addReg(PPC::X2)
2131 .addExpr(TOCOffsetDeltaExpr)
2132 .addReg(PPC::X12));
2133 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD8)
2134 .addReg(PPC::X2)
2135 .addReg(PPC::X2)
2136 .addReg(PPC::X12));
2137 }
2138
2139 MCSymbol *LocalEntryLabel = PPCFI->getLocalEPSymbol(*MF);
2140 OutStreamer->emitLabel(LocalEntryLabel);
2141 const MCSymbolRefExpr *LocalEntryLabelExp =
2142 MCSymbolRefExpr::create(LocalEntryLabel, OutContext);
2143 const MCExpr *LocalOffsetExp =
2144 MCBinaryExpr::createSub(LocalEntryLabelExp,
2145 GlobalEntryLabelExp, OutContext);
2146
2147 PPCTargetStreamer *TS =
2148 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
2149 TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp);
2150 } else if (Subtarget->isUsingPCRelativeCalls()) {
2151 // When generating the entry point for a function we have a few scenarios
2152 // based on whether or not that function uses R2 and whether or not that
2153 // function makes calls (or is a leaf function).
2154 // 1) A leaf function that does not use R2 (or treats it as callee-saved
2155 // and preserves it). In this case st_other=0 and both
2156 // the local and global entry points for the function are the same.
2157 // No special entry point code is required.
2158 // 2) A function uses the TOC pointer R2. This function may or may not have
2159 // calls. In this case st_other=[2,6] and the global and local entry
2160 // points are different. Code to correctly setup the TOC pointer in R2
2161 // is put between the global and local entry points. This case is
2162 // covered by the if statatement above.
2163 // 3) A function does not use the TOC pointer R2 but does have calls.
2164 // In this case st_other=1 since we do not know whether or not any
2165 // of the callees clobber R2. This case is dealt with in this else if
2166 // block. Tail calls are considered calls and the st_other should also
2167 // be set to 1 in that case as well.
2168 // 4) The function does not use the TOC pointer but R2 is used inside
2169 // the function. In this case st_other=1 once again.
2170 // 5) This function uses inline asm. We mark R2 as reserved if the function
2171 // has inline asm as we have to assume that it may be used.
2172 if (MF->getFrameInfo().hasCalls() || MF->getFrameInfo().hasTailCall() ||
2173 MF->hasInlineAsm() || (!PPCFI->usesTOCBasePtr() && UsesX2OrR2)) {
2174 PPCTargetStreamer *TS =
2175 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
2176 TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym),
2177 MCConstantExpr::create(1, OutContext));
2178 }
2179 }
2180}
2181
2182/// EmitFunctionBodyEnd - Print the traceback table before the .size
2183/// directive.
2184///
2185void PPCLinuxAsmPrinter::emitFunctionBodyEnd() {
2186 // Only the 64-bit target requires a traceback table. For now,
2187 // we only emit the word of zeroes that GDB requires to find
2188 // the end of the function, and zeroes for the eight-byte
2189 // mandatory fields.
2190 // FIXME: We should fill in the eight-byte mandatory fields as described in
2191 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
2192 // currently make use of these fields).
2193 if (Subtarget->isPPC64()) {
2194 OutStreamer->emitIntValue(0, 4/*size*/);
2195 OutStreamer->emitIntValue(0, 8/*size*/);
2196 }
2197}
2198
2199void PPCAIXAsmPrinter::emitLinkage(const GlobalValue *GV,
2200 MCSymbol *GVSym) const {
2201
2202 assert(MAI->hasVisibilityOnlyWithLinkage() &&
2203 "AIX's linkage directives take a visibility setting.");
2204
2205 MCSymbolAttr LinkageAttr = MCSA_Invalid;
2206 switch (GV->getLinkage()) {
2208 LinkageAttr = GV->isDeclaration() ? MCSA_Extern : MCSA_Global;
2209 break;
2215 LinkageAttr = MCSA_Weak;
2216 break;
2218 LinkageAttr = MCSA_Extern;
2219 break;
2221 return;
2224 "InternalLinkage should not have other visibility setting.");
2225 LinkageAttr = MCSA_LGlobal;
2226 break;
2228 llvm_unreachable("Should never emit this");
2230 llvm_unreachable("CommonLinkage of XCOFF should not come to this path");
2231 }
2232
2233 assert(LinkageAttr != MCSA_Invalid && "LinkageAttr should not MCSA_Invalid.");
2234
2235 MCSymbolAttr VisibilityAttr = MCSA_Invalid;
2236 if (!TM.getIgnoreXCOFFVisibility()) {
2239 "Cannot not be both dllexport and non-default visibility");
2240 switch (GV->getVisibility()) {
2241
2242 // TODO: "internal" Visibility needs to go here.
2244 if (GV->hasDLLExportStorageClass())
2245 VisibilityAttr = MAI->getExportedVisibilityAttr();
2246 break;
2248 VisibilityAttr = MAI->getHiddenVisibilityAttr();
2249 break;
2251 VisibilityAttr = MAI->getProtectedVisibilityAttr();
2252 break;
2253 }
2254 }
2255
2256 // Do not emit the _$TLSML symbol.
2257 if (GV->getThreadLocalMode() == GlobalVariable::LocalDynamicTLSModel &&
2258 GV->hasName() && GV->getName() == "_$TLSML")
2259 return;
2260
2261 OutStreamer->emitXCOFFSymbolLinkageWithVisibility(GVSym, LinkageAttr,
2262 VisibilityAttr);
2263}
2264
2265void PPCAIXAsmPrinter::SetupMachineFunction(MachineFunction &MF) {
2266 // Setup CurrentFnDescSym and its containing csect.
2267 MCSectionXCOFF *FnDescSec =
2268 cast<MCSectionXCOFF>(getObjFileLowering().getSectionForFunctionDescriptor(
2269 &MF.getFunction(), TM));
2270 FnDescSec->setAlignment(Align(Subtarget->isPPC64() ? 8 : 4));
2271
2272 CurrentFnDescSym = FnDescSec->getQualNameSymbol();
2273
2275}
2276
2277uint16_t PPCAIXAsmPrinter::getNumberOfVRSaved() {
2278 // Calculate the number of VRs be saved.
2279 // Vector registers 20 through 31 are marked as reserved and cannot be used
2280 // in the default ABI.
2281 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
2282 if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
2283 TM.getAIXExtendedAltivecABI()) {
2284 const MachineRegisterInfo &MRI = MF->getRegInfo();
2285 for (unsigned Reg = PPC::V20; Reg <= PPC::V31; ++Reg)
2286 if (MRI.isPhysRegModified(Reg))
2287 // Number of VRs saved.
2288 return PPC::V31 - Reg + 1;
2289 }
2290 return 0;
2291}
2292
2293void PPCAIXAsmPrinter::emitFunctionBodyEnd() {
2294
2295 if (!TM.getXCOFFTracebackTable())
2296 return;
2297
2298 emitTracebackTable();
2299
2300 // If ShouldEmitEHBlock returns true, then the eh info table
2301 // will be emitted via `AIXException::endFunction`. Otherwise, we
2302 // need to emit a dumy eh info table when VRs are saved. We could not
2303 // consolidate these two places into one because there is no easy way
2304 // to access register information in `AIXException` class.
2306 (getNumberOfVRSaved() > 0)) {
2307 // Emit dummy EH Info Table.
2308 OutStreamer->switchSection(getObjFileLowering().getCompactUnwindSection());
2309 MCSymbol *EHInfoLabel =
2311 OutStreamer->emitLabel(EHInfoLabel);
2312
2313 // Version number.
2314 OutStreamer->emitInt32(0);
2315
2316 const DataLayout &DL = MMI->getModule()->getDataLayout();
2317 const unsigned PointerSize = DL.getPointerSize();
2318 // Add necessary paddings in 64 bit mode.
2319 OutStreamer->emitValueToAlignment(Align(PointerSize));
2320
2321 OutStreamer->emitIntValue(0, PointerSize);
2322 OutStreamer->emitIntValue(0, PointerSize);
2323 OutStreamer->switchSection(MF->getSection());
2324 }
2325}
2326
2327void PPCAIXAsmPrinter::emitTracebackTable() {
2328
2329 // Create a symbol for the end of function.
2330 MCSymbol *FuncEnd = createTempSymbol(MF->getName());
2331 OutStreamer->emitLabel(FuncEnd);
2332
2333 OutStreamer->AddComment("Traceback table begin");
2334 // Begin with a fullword of zero.
2335 OutStreamer->emitIntValueInHexWithPadding(0, 4 /*size*/);
2336
2337 SmallString<128> CommentString;
2338 raw_svector_ostream CommentOS(CommentString);
2339
2340 auto EmitComment = [&]() {
2341 OutStreamer->AddComment(CommentOS.str());
2342 CommentString.clear();
2343 };
2344
2345 auto EmitCommentAndValue = [&](uint64_t Value, int Size) {
2346 EmitComment();
2347 OutStreamer->emitIntValueInHexWithPadding(Value, Size);
2348 };
2349
2350 unsigned int Version = 0;
2351 CommentOS << "Version = " << Version;
2352 EmitCommentAndValue(Version, 1);
2353
2354 // There is a lack of information in the IR to assist with determining the
2355 // source language. AIX exception handling mechanism would only search for
2356 // personality routine and LSDA area when such language supports exception
2357 // handling. So to be conservatively correct and allow runtime to do its job,
2358 // we need to set it to C++ for now.
2359 TracebackTable::LanguageID LanguageIdentifier =
2361
2362 CommentOS << "Language = "
2363 << getNameForTracebackTableLanguageId(LanguageIdentifier);
2364 EmitCommentAndValue(LanguageIdentifier, 1);
2365
2366 // This is only populated for the third and fourth bytes.
2367 uint32_t FirstHalfOfMandatoryField = 0;
2368
2369 // Emit the 3rd byte of the mandatory field.
2370
2371 // We always set traceback offset bit to true.
2372 FirstHalfOfMandatoryField |= TracebackTable::HasTraceBackTableOffsetMask;
2373
2374 const PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
2375 const MachineRegisterInfo &MRI = MF->getRegInfo();
2376
2377 // Check the function uses floating-point processor instructions or not
2378 for (unsigned Reg = PPC::F0; Reg <= PPC::F31; ++Reg) {
2379 if (MRI.isPhysRegUsed(Reg, /* SkipRegMaskTest */ true)) {
2380 FirstHalfOfMandatoryField |= TracebackTable::IsFloatingPointPresentMask;
2381 break;
2382 }
2383 }
2384
2385#define GENBOOLCOMMENT(Prefix, V, Field) \
2386 CommentOS << (Prefix) << ((V) & (TracebackTable::Field##Mask) ? "+" : "-") \
2387 << #Field
2388
2389#define GENVALUECOMMENT(PrefixAndName, V, Field) \
2390 CommentOS << (PrefixAndName) << " = " \
2391 << static_cast<unsigned>(((V) & (TracebackTable::Field##Mask)) >> \
2392 (TracebackTable::Field##Shift))
2393
2394 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsGlobaLinkage);
2395 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsOutOfLineEpilogOrPrologue);
2396 EmitComment();
2397
2398 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, HasTraceBackTableOffset);
2399 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsInternalProcedure);
2400 EmitComment();
2401
2402 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, HasControlledStorage);
2403 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsTOCless);
2404 EmitComment();
2405
2406 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsFloatingPointPresent);
2407 EmitComment();
2408 GENBOOLCOMMENT("", FirstHalfOfMandatoryField,
2409 IsFloatingPointOperationLogOrAbortEnabled);
2410 EmitComment();
2411
2412 OutStreamer->emitIntValueInHexWithPadding(
2413 (FirstHalfOfMandatoryField & 0x0000ff00) >> 8, 1);
2414
2415 // Set the 4th byte of the mandatory field.
2416 FirstHalfOfMandatoryField |= TracebackTable::IsFunctionNamePresentMask;
2417
2418 const PPCRegisterInfo *RegInfo =
2419 static_cast<const PPCRegisterInfo *>(Subtarget->getRegisterInfo());
2420 Register FrameReg = RegInfo->getFrameRegister(*MF);
2421 if (FrameReg == (Subtarget->isPPC64() ? PPC::X31 : PPC::R31))
2422 FirstHalfOfMandatoryField |= TracebackTable::IsAllocaUsedMask;
2423
2424 const SmallVectorImpl<Register> &MustSaveCRs = FI->getMustSaveCRs();
2425 if (!MustSaveCRs.empty())
2426 FirstHalfOfMandatoryField |= TracebackTable::IsCRSavedMask;
2427
2428 if (FI->mustSaveLR())
2429 FirstHalfOfMandatoryField |= TracebackTable::IsLRSavedMask;
2430
2431 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsInterruptHandler);
2432 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsFunctionNamePresent);
2433 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsAllocaUsed);
2434 EmitComment();
2435 GENVALUECOMMENT("OnConditionDirective", FirstHalfOfMandatoryField,
2436 OnConditionDirective);
2437 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsCRSaved);
2438 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsLRSaved);
2439 EmitComment();
2440 OutStreamer->emitIntValueInHexWithPadding((FirstHalfOfMandatoryField & 0xff),
2441 1);
2442
2443 // Set the 5th byte of mandatory field.
2444 uint32_t SecondHalfOfMandatoryField = 0;
2445
2446 SecondHalfOfMandatoryField |= MF->getFrameInfo().getStackSize()
2448 : 0;
2449
2450 uint32_t FPRSaved = 0;
2451 for (unsigned Reg = PPC::F14; Reg <= PPC::F31; ++Reg) {
2452 if (MRI.isPhysRegModified(Reg)) {
2453 FPRSaved = PPC::F31 - Reg + 1;
2454 break;
2455 }
2456 }
2457 SecondHalfOfMandatoryField |= (FPRSaved << TracebackTable::FPRSavedShift) &
2459 GENBOOLCOMMENT("", SecondHalfOfMandatoryField, IsBackChainStored);
2460 GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, IsFixup);
2461 GENVALUECOMMENT(", NumOfFPRsSaved", SecondHalfOfMandatoryField, FPRSaved);
2462 EmitComment();
2463 OutStreamer->emitIntValueInHexWithPadding(
2464 (SecondHalfOfMandatoryField & 0xff000000) >> 24, 1);
2465
2466 // Set the 6th byte of mandatory field.
2467
2468 // Check whether has Vector Instruction,We only treat instructions uses vector
2469 // register as vector instructions.
2470 bool HasVectorInst = false;
2471 for (unsigned Reg = PPC::V0; Reg <= PPC::V31; ++Reg)
2472 if (MRI.isPhysRegUsed(Reg, /* SkipRegMaskTest */ true)) {
2473 // Has VMX instruction.
2474 HasVectorInst = true;
2475 break;
2476 }
2477
2478 if (FI->hasVectorParms() || HasVectorInst)
2479 SecondHalfOfMandatoryField |= TracebackTable::HasVectorInfoMask;
2480
2481 uint16_t NumOfVRSaved = getNumberOfVRSaved();
2482 bool ShouldEmitEHBlock =
2484
2485 if (ShouldEmitEHBlock)
2486 SecondHalfOfMandatoryField |= TracebackTable::HasExtensionTableMask;
2487
2488 uint32_t GPRSaved = 0;
2489
2490 // X13 is reserved under 64-bit environment.
2491 unsigned GPRBegin = Subtarget->isPPC64() ? PPC::X14 : PPC::R13;
2492 unsigned GPREnd = Subtarget->isPPC64() ? PPC::X31 : PPC::R31;
2493
2494 for (unsigned Reg = GPRBegin; Reg <= GPREnd; ++Reg) {
2495 if (MRI.isPhysRegModified(Reg)) {
2496 GPRSaved = GPREnd - Reg + 1;
2497 break;
2498 }
2499 }
2500
2501 SecondHalfOfMandatoryField |= (GPRSaved << TracebackTable::GPRSavedShift) &
2503
2504 GENBOOLCOMMENT("", SecondHalfOfMandatoryField, HasExtensionTable);
2505 GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, HasVectorInfo);
2506 GENVALUECOMMENT(", NumOfGPRsSaved", SecondHalfOfMandatoryField, GPRSaved);
2507 EmitComment();
2508 OutStreamer->emitIntValueInHexWithPadding(
2509 (SecondHalfOfMandatoryField & 0x00ff0000) >> 16, 1);
2510
2511 // Set the 7th byte of mandatory field.
2512 uint32_t NumberOfFixedParms = FI->getFixedParmsNum();
2513 SecondHalfOfMandatoryField |=
2514 (NumberOfFixedParms << TracebackTable::NumberOfFixedParmsShift) &
2516 GENVALUECOMMENT("NumberOfFixedParms", SecondHalfOfMandatoryField,
2517 NumberOfFixedParms);
2518 EmitComment();
2519 OutStreamer->emitIntValueInHexWithPadding(
2520 (SecondHalfOfMandatoryField & 0x0000ff00) >> 8, 1);
2521
2522 // Set the 8th byte of mandatory field.
2523
2524 // Always set parameter on stack.
2525 SecondHalfOfMandatoryField |= TracebackTable::HasParmsOnStackMask;
2526
2527 uint32_t NumberOfFPParms = FI->getFloatingPointParmsNum();
2528 SecondHalfOfMandatoryField |=
2531
2532 GENVALUECOMMENT("NumberOfFPParms", SecondHalfOfMandatoryField,
2533 NumberOfFloatingPointParms);
2534 GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, HasParmsOnStack);
2535 EmitComment();
2536 OutStreamer->emitIntValueInHexWithPadding(SecondHalfOfMandatoryField & 0xff,
2537 1);
2538
2539 // Generate the optional fields of traceback table.
2540
2541 // Parameter type.
2542 if (NumberOfFixedParms || NumberOfFPParms) {
2543 uint32_t ParmsTypeValue = FI->getParmsType();
2544
2545 Expected<SmallString<32>> ParmsType =
2546 FI->hasVectorParms()
2548 ParmsTypeValue, NumberOfFixedParms, NumberOfFPParms,
2549 FI->getVectorParmsNum())
2550 : XCOFF::parseParmsType(ParmsTypeValue, NumberOfFixedParms,
2551 NumberOfFPParms);
2552
2553 assert(ParmsType && toString(ParmsType.takeError()).c_str());
2554 if (ParmsType) {
2555 CommentOS << "Parameter type = " << ParmsType.get();
2556 EmitComment();
2557 }
2558 OutStreamer->emitIntValueInHexWithPadding(ParmsTypeValue,
2559 sizeof(ParmsTypeValue));
2560 }
2561 // Traceback table offset.
2562 OutStreamer->AddComment("Function size");
2563 if (FirstHalfOfMandatoryField & TracebackTable::HasTraceBackTableOffsetMask) {
2564 MCSymbol *FuncSectSym = getObjFileLowering().getFunctionEntryPointSymbol(
2565 &(MF->getFunction()), TM);
2566 OutStreamer->emitAbsoluteSymbolDiff(FuncEnd, FuncSectSym, 4);
2567 }
2568
2569 // Since we unset the Int_Handler.
2570 if (FirstHalfOfMandatoryField & TracebackTable::IsInterruptHandlerMask)
2571 report_fatal_error("Hand_Mask not implement yet");
2572
2573 if (FirstHalfOfMandatoryField & TracebackTable::HasControlledStorageMask)
2574 report_fatal_error("Ctl_Info not implement yet");
2575
2576 if (FirstHalfOfMandatoryField & TracebackTable::IsFunctionNamePresentMask) {
2577 StringRef Name = MF->getName().substr(0, INT16_MAX);
2578 int16_t NameLength = Name.size();
2579 CommentOS << "Function name len = "
2580 << static_cast<unsigned int>(NameLength);
2581 EmitCommentAndValue(NameLength, 2);
2582 OutStreamer->AddComment("Function Name");
2583 OutStreamer->emitBytes(Name);
2584 }
2585
2586 if (FirstHalfOfMandatoryField & TracebackTable::IsAllocaUsedMask) {
2587 uint8_t AllocReg = XCOFF::AllocRegNo;
2588 OutStreamer->AddComment("AllocaUsed");
2589 OutStreamer->emitIntValueInHex(AllocReg, sizeof(AllocReg));
2590 }
2591
2592 if (SecondHalfOfMandatoryField & TracebackTable::HasVectorInfoMask) {
2593 uint16_t VRData = 0;
2594 if (NumOfVRSaved) {
2595 // Number of VRs saved.
2596 VRData |= (NumOfVRSaved << TracebackTable::NumberOfVRSavedShift) &
2598 // This bit is supposed to set only when the special register
2599 // VRSAVE is saved on stack.
2600 // However, IBM XL compiler sets the bit when any vector registers
2601 // are saved on the stack. We will follow XL's behavior on AIX
2602 // so that we don't get surprise behavior change for C code.
2604 }
2605
2606 // Set has_varargs.
2607 if (FI->getVarArgsFrameIndex())
2609
2610 // Vector parameters number.
2611 unsigned VectorParmsNum = FI->getVectorParmsNum();
2612 VRData |= (VectorParmsNum << TracebackTable::NumberOfVectorParmsShift) &
2614
2615 if (HasVectorInst)
2617
2618 GENVALUECOMMENT("NumOfVRsSaved", VRData, NumberOfVRSaved);
2619 GENBOOLCOMMENT(", ", VRData, IsVRSavedOnStack);
2620 GENBOOLCOMMENT(", ", VRData, HasVarArgs);
2621 EmitComment();
2622 OutStreamer->emitIntValueInHexWithPadding((VRData & 0xff00) >> 8, 1);
2623
2624 GENVALUECOMMENT("NumOfVectorParams", VRData, NumberOfVectorParms);
2625 GENBOOLCOMMENT(", ", VRData, HasVMXInstruction);
2626 EmitComment();
2627 OutStreamer->emitIntValueInHexWithPadding(VRData & 0x00ff, 1);
2628
2629 uint32_t VecParmTypeValue = FI->getVecExtParmsType();
2630
2631 Expected<SmallString<32>> VecParmsType =
2632 XCOFF::parseVectorParmsType(VecParmTypeValue, VectorParmsNum);
2633 assert(VecParmsType && toString(VecParmsType.takeError()).c_str());
2634 if (VecParmsType) {
2635 CommentOS << "Vector Parameter type = " << VecParmsType.get();
2636 EmitComment();
2637 }
2638 OutStreamer->emitIntValueInHexWithPadding(VecParmTypeValue,
2639 sizeof(VecParmTypeValue));
2640 // Padding 2 bytes.
2641 CommentOS << "Padding";
2642 EmitCommentAndValue(0, 2);
2643 }
2644
2645 uint8_t ExtensionTableFlag = 0;
2646 if (SecondHalfOfMandatoryField & TracebackTable::HasExtensionTableMask) {
2647 if (ShouldEmitEHBlock)
2648 ExtensionTableFlag |= ExtendedTBTableFlag::TB_EH_INFO;
2651 ExtensionTableFlag |= ExtendedTBTableFlag::TB_SSP_CANARY;
2652
2653 CommentOS << "ExtensionTableFlag = "
2654 << getExtendedTBTableFlagString(ExtensionTableFlag);
2655 EmitCommentAndValue(ExtensionTableFlag, sizeof(ExtensionTableFlag));
2656 }
2657
2658 if (ExtensionTableFlag & ExtendedTBTableFlag::TB_EH_INFO) {
2659 auto &Ctx = OutStreamer->getContext();
2660 MCSymbol *EHInfoSym =
2662 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(EHInfoSym, TOCType_EHBlock);
2663 const MCSymbol *TOCBaseSym =
2664 cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
2665 ->getQualNameSymbol();
2666 const MCExpr *Exp =
2668 MCSymbolRefExpr::create(TOCBaseSym, Ctx), Ctx);
2669
2670 const DataLayout &DL = getDataLayout();
2671 OutStreamer->emitValueToAlignment(Align(4));
2672 OutStreamer->AddComment("EHInfo Table");
2673 OutStreamer->emitValue(Exp, DL.getPointerSize());
2674 }
2675#undef GENBOOLCOMMENT
2676#undef GENVALUECOMMENT
2677}
2678
2680 return GV->hasAppendingLinkage() &&
2682 // TODO: Linker could still eliminate the GV if we just skip
2683 // handling llvm.used array. Skipping them for now until we or the
2684 // AIX OS team come up with a good solution.
2685 .Case("llvm.used", true)
2686 // It's correct to just skip llvm.compiler.used array here.
2687 .Case("llvm.compiler.used", true)
2688 .Default(false);
2689}
2690
2692 return StringSwitch<bool>(GV->getName())
2693 .Cases("llvm.global_ctors", "llvm.global_dtors", true)
2694 .Default(false);
2695}
2696
2697uint64_t PPCAIXAsmPrinter::getAliasOffset(const Constant *C) {
2698 if (auto *GA = dyn_cast<GlobalAlias>(C))
2699 return getAliasOffset(GA->getAliasee());
2700 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
2701 const MCExpr *LowC = lowerConstant(CE);
2702 const MCBinaryExpr *CBE = dyn_cast<MCBinaryExpr>(LowC);
2703 if (!CBE)
2704 return 0;
2705 if (CBE->getOpcode() != MCBinaryExpr::Add)
2706 report_fatal_error("Only adding an offset is supported now.");
2707 auto *RHS = dyn_cast<MCConstantExpr>(CBE->getRHS());
2708 if (!RHS)
2709 report_fatal_error("Unable to get the offset of alias.");
2710 return RHS->getValue();
2711 }
2712 return 0;
2713}
2714
2715static void tocDataChecks(unsigned PointerSize, const GlobalVariable *GV) {
2716 // TODO: These asserts should be updated as more support for the toc data
2717 // transformation is added (struct support, etc.).
2718 assert(
2719 PointerSize >= GV->getAlign().valueOrOne().value() &&
2720 "GlobalVariables with an alignment requirement stricter than TOC entry "
2721 "size not supported by the toc data transformation.");
2722
2723 Type *GVType = GV->getValueType();
2724 assert(GVType->isSized() && "A GlobalVariable's size must be known to be "
2725 "supported by the toc data transformation.");
2726 if (GV->getParent()->getDataLayout().getTypeSizeInBits(GVType) >
2727 PointerSize * 8)
2729 "A GlobalVariable with size larger than a TOC entry is not currently "
2730 "supported by the toc data transformation.");
2731 if (GV->hasPrivateLinkage())
2732 report_fatal_error("A GlobalVariable with private linkage is not "
2733 "currently supported by the toc data transformation.");
2734}
2735
2736void PPCAIXAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
2737 // Special LLVM global arrays have been handled at the initialization.
2739 return;
2740
2741 // If the Global Variable has the toc-data attribute, it needs to be emitted
2742 // when we emit the .toc section.
2743 if (GV->hasAttribute("toc-data")) {
2744 unsigned PointerSize = GV->getParent()->getDataLayout().getPointerSize();
2745 tocDataChecks(PointerSize, GV);
2746 TOCDataGlobalVars.push_back(GV);
2747 return;
2748 }
2749
2750 emitGlobalVariableHelper(GV);
2751}
2752
2753void PPCAIXAsmPrinter::emitGlobalVariableHelper(const GlobalVariable *GV) {
2754 assert(!GV->getName().starts_with("llvm.") &&
2755 "Unhandled intrinsic global variable.");
2756
2757 if (GV->hasComdat())
2758 report_fatal_error("COMDAT not yet supported by AIX.");
2759
2760 MCSymbolXCOFF *GVSym = cast<MCSymbolXCOFF>(getSymbol(GV));
2761
2762 if (GV->isDeclarationForLinker()) {
2763 emitLinkage(GV, GVSym);
2764 return;
2765 }
2766
2767 SectionKind GVKind = getObjFileLowering().getKindForGlobal(GV, TM);
2768 if (!GVKind.isGlobalWriteableData() && !GVKind.isReadOnly() &&
2769 !GVKind.isThreadLocal()) // Checks for both ThreadData and ThreadBSS.
2770 report_fatal_error("Encountered a global variable kind that is "
2771 "not supported yet.");
2772
2773 // Print GV in verbose mode
2774 if (isVerbose()) {
2775 if (GV->hasInitializer()) {
2776 GV->printAsOperand(OutStreamer->getCommentOS(),
2777 /*PrintType=*/false, GV->getParent());
2778 OutStreamer->getCommentOS() << '\n';
2779 }
2780 }
2781
2782 MCSectionXCOFF *Csect = cast<MCSectionXCOFF>(
2783 getObjFileLowering().SectionForGlobal(GV, GVKind, TM));
2784
2785 // Switch to the containing csect.
2786 OutStreamer->switchSection(Csect);
2787
2788 const DataLayout &DL = GV->getParent()->getDataLayout();
2789
2790 // Handle common and zero-initialized local symbols.
2791 if (GV->hasCommonLinkage() || GVKind.isBSSLocal() ||
2792 GVKind.isThreadBSSLocal()) {
2793 Align Alignment = GV->getAlign().value_or(DL.getPreferredAlign(GV));
2794 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
2795 GVSym->setStorageClass(
2797
2798 if (GVKind.isBSSLocal() && Csect->getMappingClass() == XCOFF::XMC_TD) {
2799 OutStreamer->emitZeros(Size);
2800 } else if (GVKind.isBSSLocal() || GVKind.isThreadBSSLocal()) {
2801 assert(Csect->getMappingClass() != XCOFF::XMC_TD &&
2802 "BSS local toc-data already handled and TLS variables "
2803 "incompatible with XMC_TD");
2804 OutStreamer->emitXCOFFLocalCommonSymbol(
2805 OutContext.getOrCreateSymbol(GVSym->getSymbolTableName()), Size,
2806 GVSym, Alignment);
2807 } else {
2808 OutStreamer->emitCommonSymbol(GVSym, Size, Alignment);
2809 }
2810 return;
2811 }
2812
2813 MCSymbol *EmittedInitSym = GVSym;
2814
2815 // Emit linkage for the global variable and its aliases.
2816 emitLinkage(GV, EmittedInitSym);
2817 for (const GlobalAlias *GA : GOAliasMap[GV])
2818 emitLinkage(GA, getSymbol(GA));
2819
2820 emitAlignment(getGVAlignment(GV, DL), GV);
2821
2822 // When -fdata-sections is enabled, every GlobalVariable will
2823 // be put into its own csect; therefore, label is not necessary here.
2824 if (!TM.getDataSections() || GV->hasSection())
2825 OutStreamer->emitLabel(EmittedInitSym);
2826
2827 // No alias to emit.
2828 if (!GOAliasMap[GV].size()) {
2829 emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
2830 return;
2831 }
2832
2833 // Aliases with the same offset should be aligned. Record the list of aliases
2834 // associated with the offset.
2835 AliasMapTy AliasList;
2836 for (const GlobalAlias *GA : GOAliasMap[GV])
2837 AliasList[getAliasOffset(GA->getAliasee())].push_back(GA);
2838
2839 // Emit alias label and element value for global variable.
2840 emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer(),
2841 &AliasList);
2842}
2843
2844void PPCAIXAsmPrinter::emitFunctionDescriptor() {
2845 const DataLayout &DL = getDataLayout();
2846 const unsigned PointerSize = DL.getPointerSizeInBits() == 64 ? 8 : 4;
2847
2848 MCSectionSubPair Current = OutStreamer->getCurrentSection();
2849 // Emit function descriptor.
2850 OutStreamer->switchSection(
2851 cast<MCSymbolXCOFF>(CurrentFnDescSym)->getRepresentedCsect());
2852
2853 // Emit aliasing label for function descriptor csect.
2854 for (const GlobalAlias *Alias : GOAliasMap[&MF->getFunction()])
2855 OutStreamer->emitLabel(getSymbol(Alias));
2856
2857 // Emit function entry point address.
2858 OutStreamer->emitValue(MCSymbolRefExpr::create(CurrentFnSym, OutContext),
2859 PointerSize);
2860 // Emit TOC base address.
2861 const MCSymbol *TOCBaseSym =
2862 cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
2863 ->getQualNameSymbol();
2864 OutStreamer->emitValue(MCSymbolRefExpr::create(TOCBaseSym, OutContext),
2865 PointerSize);
2866 // Emit a null environment pointer.
2867 OutStreamer->emitIntValue(0, PointerSize);
2868
2869 OutStreamer->switchSection(Current.first, Current.second);
2870}
2871
2872void PPCAIXAsmPrinter::emitFunctionEntryLabel() {
2873 // For functions without user defined section, it's not necessary to emit the
2874 // label when we have individual function in its own csect.
2875 if (!TM.getFunctionSections() || MF->getFunction().hasSection())
2876 PPCAsmPrinter::emitFunctionEntryLabel();
2877
2878 // Emit aliasing label for function entry point label.
2879 for (const GlobalAlias *Alias : GOAliasMap[&MF->getFunction()])
2880 OutStreamer->emitLabel(
2881 getObjFileLowering().getFunctionEntryPointSymbol(Alias, TM));
2882}
2883
2884void PPCAIXAsmPrinter::emitPGORefs(Module &M) {
2885 if (!OutContext.hasXCOFFSection(
2886 "__llvm_prf_cnts",
2888 return;
2889
2890 // When inside a csect `foo`, a .ref directive referring to a csect `bar`
2891 // translates into a relocation entry from `foo` to` bar`. The referring
2892 // csect, `foo`, is identified by its address. If multiple csects have the
2893 // same address (because one or more of them are zero-length), the referring
2894 // csect cannot be determined. Hence, we don't generate the .ref directives
2895 // if `__llvm_prf_cnts` is an empty section.
2896 bool HasNonZeroLengthPrfCntsSection = false;
2897 const DataLayout &DL = M.getDataLayout();
2898 for (GlobalVariable &GV : M.globals())
2899 if (GV.hasSection() && GV.getSection().equals("__llvm_prf_cnts") &&
2900 DL.getTypeAllocSize(GV.getValueType()) > 0) {
2901 HasNonZeroLengthPrfCntsSection = true;
2902 break;
2903 }
2904
2905 if (HasNonZeroLengthPrfCntsSection) {
2906 MCSection *CntsSection = OutContext.getXCOFFSection(
2907 "__llvm_prf_cnts", SectionKind::getData(),
2909 /*MultiSymbolsAllowed*/ true);
2910
2911 OutStreamer->switchSection(CntsSection);
2912 if (OutContext.hasXCOFFSection(
2913 "__llvm_prf_data",
2915 MCSymbol *S = OutContext.getOrCreateSymbol("__llvm_prf_data[RW]");
2916 OutStreamer->emitXCOFFRefDirective(S);
2917 }
2918 if (OutContext.hasXCOFFSection(
2919 "__llvm_prf_names",
2921 MCSymbol *S = OutContext.getOrCreateSymbol("__llvm_prf_names[RO]");
2922 OutStreamer->emitXCOFFRefDirective(S);
2923 }
2924 if (OutContext.hasXCOFFSection(
2925 "__llvm_prf_vnds",
2927 MCSymbol *S = OutContext.getOrCreateSymbol("__llvm_prf_vnds[RW]");
2928 OutStreamer->emitXCOFFRefDirective(S);
2929 }
2930 }
2931}
2932
2933void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) {
2934 // If there are no functions and there are no toc-data definitions in this
2935 // module, we will never need to reference the TOC base.
2936 if (M.empty() && TOCDataGlobalVars.empty())
2937 return;
2938
2939 emitPGORefs(M);
2940
2941 // Switch to section to emit TOC base.
2942 OutStreamer->switchSection(getObjFileLowering().getTOCBaseSection());
2943
2944 PPCTargetStreamer *TS =
2945 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
2946
2947 for (auto &I : TOC) {
2948 MCSectionXCOFF *TCEntry;
2949 // Setup the csect for the current TC entry. If the variant kind is
2950 // VK_PPC_AIX_TLSGDM the entry represents the region handle, we create a
2951 // new symbol to prefix the name with a dot.
2952 if (I.first.second == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM) {
2954 StringRef Prefix = ".";
2955 Name += Prefix;
2956 Name += cast<MCSymbolXCOFF>(I.first.first)->getSymbolTableName();
2957 MCSymbol *S = OutContext.getOrCreateSymbol(Name);
2958 TCEntry = cast<MCSectionXCOFF>(
2959 getObjFileLowering().getSectionForTOCEntry(S, TM));
2960 } else {
2961 TCEntry = cast<MCSectionXCOFF>(
2962 getObjFileLowering().getSectionForTOCEntry(I.first.first, TM));
2963 }
2964 OutStreamer->switchSection(TCEntry);
2965
2966 OutStreamer->emitLabel(I.second);
2967 TS->emitTCEntry(*I.first.first, I.first.second);
2968 }
2969
2970 // Traverse the list of global variables twice, emitting all of the
2971 // non-common global variables before the common ones, as emitting a
2972 // .comm directive changes the scope from .toc to the common symbol.
2973 for (const auto *GV : TOCDataGlobalVars) {
2974 if (!GV->hasCommonLinkage())
2975 emitGlobalVariableHelper(GV);
2976 }
2977 for (const auto *GV : TOCDataGlobalVars) {
2978 if (GV->hasCommonLinkage())
2979 emitGlobalVariableHelper(GV);
2980 }
2981}
2982
2983bool PPCAIXAsmPrinter::doInitialization(Module &M) {
2984 const bool Result = PPCAsmPrinter::doInitialization(M);
2985
2986 auto setCsectAlignment = [this](const GlobalObject *GO) {
2987 // Declarations have 0 alignment which is set by default.
2988 if (GO->isDeclarationForLinker())
2989 return;
2990
2991 SectionKind GOKind = getObjFileLowering().getKindForGlobal(GO, TM);
2992 MCSectionXCOFF *Csect = cast<MCSectionXCOFF>(
2993 getObjFileLowering().SectionForGlobal(GO, GOKind, TM));
2994
2995 Align GOAlign = getGVAlignment(GO, GO->getParent()->getDataLayout());
2996 Csect->ensureMinAlignment(GOAlign);
2997 };
2998
2999 // For all TLS variables, calculate their corresponding addresses and store
3000 // them into TLSVarsToAddressMapping, which will be used to determine whether
3001 // or not local-exec TLS variables require special assembly printing.
3002 uint64_t TLSVarAddress = 0;
3003 auto DL = M.getDataLayout();
3004 for (const auto &G : M.globals()) {
3005 if (G.isThreadLocal() && !G.isDeclaration()) {
3006 TLSVarAddress = alignTo(TLSVarAddress, getGVAlignment(&G, DL));
3007 TLSVarsToAddressMapping[&G] = TLSVarAddress;
3008 TLSVarAddress += DL.getTypeAllocSize(G.getValueType());
3009 }
3010 }
3011
3012 // We need to know, up front, the alignment of csects for the assembly path,
3013 // because once a .csect directive gets emitted, we could not change the
3014 // alignment value on it.
3015 for (const auto &G : M.globals()) {
3017 continue;
3018
3020 // Generate a format indicator and a unique module id to be a part of
3021 // the sinit and sterm function names.
3022 if (FormatIndicatorAndUniqueModId.empty()) {
3023 std::string UniqueModuleId = getUniqueModuleId(&M);
3024 if (UniqueModuleId != "")
3025 // TODO: Use source file full path to generate the unique module id
3026 // and add a format indicator as a part of function name in case we
3027 // will support more than one format.
3028 FormatIndicatorAndUniqueModId = "clang_" + UniqueModuleId.substr(1);
3029 else {
3030 // Use threadId, Pid, and current time as the unique module id when we
3031 // cannot generate one based on a module's strong external symbols.
3032 auto CurTime =
3033 std::chrono::duration_cast<std::chrono::nanoseconds>(
3034 std::chrono::steady_clock::now().time_since_epoch())
3035 .count();
3036 FormatIndicatorAndUniqueModId =
3037 "clangPidTidTime_" + llvm::itostr(sys::Process::getProcessId()) +
3038 "_" + llvm::itostr(llvm::get_threadid()) + "_" +
3039 llvm::itostr(CurTime);
3040 }
3041 }
3042
3043 emitSpecialLLVMGlobal(&G);
3044 continue;
3045 }
3046
3047 setCsectAlignment(&G);
3048 std::optional<CodeModel::Model> OptionalCodeModel = G.getCodeModel();
3049 if (OptionalCodeModel)
3050 setOptionalCodeModel(cast<MCSymbolXCOFF>(getSymbol(&G)),
3051 *OptionalCodeModel);
3052 }
3053
3054 for (const auto &F : M)
3055 setCsectAlignment(&F);
3056
3057 // Construct an aliasing list for each GlobalObject.
3058 for (const auto &Alias : M.aliases()) {
3059 const GlobalObject *Aliasee = Alias.getAliaseeObject();
3060 if (!Aliasee)
3062 "alias without a base object is not yet supported on AIX");
3063
3064 if (Aliasee->hasCommonLinkage()) {
3065 report_fatal_error("Aliases to common variables are not allowed on AIX:"
3066 "\n\tAlias attribute for " +
3067 Alias.getGlobalIdentifier() +
3068 " is invalid because " + Aliasee->getName() +
3069 " is common.",
3070 false);
3071 }
3072
3073 const GlobalVariable *GVar =
3074 dyn_cast_or_null<GlobalVariable>(Alias.getAliaseeObject());
3075 if (GVar) {
3076 std::optional<CodeModel::Model> OptionalCodeModel = GVar->getCodeModel();
3077 if (OptionalCodeModel)
3078 setOptionalCodeModel(cast<MCSymbolXCOFF>(getSymbol(&Alias)),
3079 *OptionalCodeModel);
3080 }
3081
3082 GOAliasMap[Aliasee].push_back(&Alias);
3083 }
3084
3085 return Result;
3086}
3087
3088void PPCAIXAsmPrinter::emitInstruction(const MachineInstr *MI) {
3089 switch (MI->getOpcode()) {
3090 default:
3091 break;
3092 case PPC::TW:
3093 case PPC::TWI:
3094 case PPC::TD:
3095 case PPC::TDI: {
3096 if (MI->getNumOperands() < 5)
3097 break;
3098 const MachineOperand &LangMO = MI->getOperand(3);
3099 const MachineOperand &ReasonMO = MI->getOperand(4);
3100 if (!LangMO.isImm() || !ReasonMO.isImm())
3101 break;
3102 MCSymbol *TempSym = OutContext.createNamedTempSymbol();
3103 OutStreamer->emitLabel(TempSym);
3104 OutStreamer->emitXCOFFExceptDirective(CurrentFnSym, TempSym,
3105 LangMO.getImm(), ReasonMO.getImm(),
3106 Subtarget->isPPC64() ? MI->getMF()->getInstructionCount() * 8 :
3107 MI->getMF()->getInstructionCount() * 4,
3108 MMI->hasDebugInfo());
3109 break;
3110 }
3111 case PPC::GETtlsMOD32AIX:
3112 case PPC::GETtlsMOD64AIX:
3113 case PPC::GETtlsTpointer32AIX:
3114 case PPC::GETtlsADDR64AIX:
3115 case PPC::GETtlsADDR32AIX: {
3116 // A reference to .__tls_get_mod/.__tls_get_addr/.__get_tpointer is unknown
3117 // to the assembler so we need to emit an external symbol reference.
3118 MCSymbol *TlsGetAddr =
3119 createMCSymbolForTlsGetAddr(OutContext, MI->getOpcode());
3120 ExtSymSDNodeSymbols.insert(TlsGetAddr);
3121 break;
3122 }
3123 case PPC::BL8:
3124 case PPC::BL:
3125 case PPC::BL8_NOP:
3126 case PPC::BL_NOP: {
3127 const MachineOperand &MO = MI->getOperand(0);
3128 if (MO.isSymbol()) {
3129 MCSymbolXCOFF *S =
3130 cast<MCSymbolXCOFF>(OutContext.getOrCreateSymbol(MO.getSymbolName()));
3131 ExtSymSDNodeSymbols.insert(S);
3132 }
3133 } break;
3134 case PPC::BL_TLS:
3135 case PPC::BL8_TLS:
3136 case PPC::BL8_TLS_:
3137 case PPC::BL8_NOP_TLS:
3138 report_fatal_error("TLS call not yet implemented");
3139 case PPC::TAILB:
3140 case PPC::TAILB8:
3141 case PPC::TAILBA:
3142 case PPC::TAILBA8:
3143 case PPC::TAILBCTR:
3144 case PPC::TAILBCTR8:
3145 if (MI->getOperand(0).isSymbol())
3146 report_fatal_error("Tail call for extern symbol not yet supported.");
3147 break;
3148 case PPC::DST:
3149 case PPC::DST64:
3150 case PPC::DSTT:
3151 case PPC::DSTT64:
3152 case PPC::DSTST:
3153 case PPC::DSTST64:
3154 case PPC::DSTSTT:
3155 case PPC::DSTSTT64:
3156 EmitToStreamer(
3157 *OutStreamer,
3158 MCInstBuilder(PPC::ORI).addReg(PPC::R0).addReg(PPC::R0).addImm(0));
3159 return;
3160 }
3161 return PPCAsmPrinter::emitInstruction(MI);
3162}
3163
3164bool PPCAIXAsmPrinter::doFinalization(Module &M) {
3165 // Do streamer related finalization for DWARF.
3166 if (!MAI->usesDwarfFileAndLocDirectives() && MMI->hasDebugInfo())
3167 OutStreamer->doFinalizationAtSectionEnd(
3168 OutStreamer->getContext().getObjectFileInfo()->getTextSection());
3169
3170 for (MCSymbol *Sym : ExtSymSDNodeSymbols)
3171 OutStreamer->emitSymbolAttribute(Sym, MCSA_Extern);
3172 return PPCAsmPrinter::doFinalization(M);
3173}
3174
3175static unsigned mapToSinitPriority(int P) {
3176 if (P < 0 || P > 65535)
3177 report_fatal_error("invalid init priority");
3178
3179 if (P <= 20)
3180 return P;
3181
3182 if (P < 81)
3183 return 20 + (P - 20) * 16;
3184
3185 if (P <= 1124)
3186 return 1004 + (P - 81);
3187
3188 if (P < 64512)
3189 return 2047 + (P - 1124) * 33878;
3190
3191 return 2147482625u + (P - 64512);
3192}
3193
3194static std::string convertToSinitPriority(int Priority) {
3195 // This helper function converts clang init priority to values used in sinit
3196 // and sterm functions.
3197 //
3198 // The conversion strategies are:
3199 // We map the reserved clang/gnu priority range [0, 100] into the sinit/sterm
3200 // reserved priority range [0, 1023] by
3201 // - directly mapping the first 21 and the last 20 elements of the ranges
3202 // - linear interpolating the intermediate values with a step size of 16.
3203 //
3204 // We map the non reserved clang/gnu priority range of [101, 65535] into the
3205 // sinit/sterm priority range [1024, 2147483648] by:
3206 // - directly mapping the first and the last 1024 elements of the ranges
3207 // - linear interpolating the intermediate values with a step size of 33878.
3208 unsigned int P = mapToSinitPriority(Priority);
3209
3210 std::string PrioritySuffix;
3211 llvm::raw_string_ostream os(PrioritySuffix);
3212 os << llvm::format_hex_no_prefix(P, 8);
3213 os.flush();
3214 return PrioritySuffix;
3215}
3216
3217void PPCAIXAsmPrinter::emitXXStructorList(const DataLayout &DL,
3218 const Constant *List, bool IsCtor) {
3219 SmallVector<Structor, 8> Structors;
3220 preprocessXXStructorList(DL, List, Structors);
3221 if (Structors.empty())
3222 return;
3223
3224 unsigned Index = 0;
3225 for (Structor &S : Structors) {
3226 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(S.Func))
3227 S.Func = CE->getOperand(0);
3228
3231 (IsCtor ? llvm::Twine("__sinit") : llvm::Twine("__sterm")) +
3232 llvm::Twine(convertToSinitPriority(S.Priority)) +
3233 llvm::Twine("_", FormatIndicatorAndUniqueModId) +
3234 llvm::Twine("_", llvm::utostr(Index++)),
3235 cast<Function>(S.Func));
3236 }
3237}
3238
3239void PPCAIXAsmPrinter::emitTTypeReference(const GlobalValue *GV,
3240 unsigned Encoding) {
3241 if (GV) {
3242 TOCEntryType GlobalType = TOCType_GlobalInternal;
3244 if (Linkage == GlobalValue::ExternalLinkage ||
3247 GlobalType = TOCType_GlobalExternal;
3248 MCSymbol *TypeInfoSym = TM.getSymbol(GV);
3249 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(TypeInfoSym, GlobalType);
3250 const MCSymbol *TOCBaseSym =
3251 cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
3252 ->getQualNameSymbol();
3253 auto &Ctx = OutStreamer->getContext();
3254 const MCExpr *Exp =
3256 MCSymbolRefExpr::create(TOCBaseSym, Ctx), Ctx);
3257 OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding));
3258 } else
3259 OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding));
3260}
3261
3262// Return a pass that prints the PPC assembly code for a MachineFunction to the
3263// given output stream.
3264static AsmPrinter *
3266 std::unique_ptr<MCStreamer> &&Streamer) {
3267 if (tm.getTargetTriple().isOSAIX())
3268 return new PPCAIXAsmPrinter(tm, std::move(Streamer));
3269
3270 return new PPCLinuxAsmPrinter(tm, std::move(Streamer));
3271}
3272
3273void PPCAIXAsmPrinter::emitModuleCommandLines(Module &M) {
3274 const NamedMDNode *NMD = M.getNamedMetadata("llvm.commandline");
3275 if (!NMD || !NMD->getNumOperands())
3276 return;
3277
3278 std::string S;
3279 raw_string_ostream RSOS(S);
3280 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
3281 const MDNode *N = NMD->getOperand(i);
3282 assert(N->getNumOperands() == 1 &&
3283 "llvm.commandline metadata entry can have only one operand");
3284 const MDString *MDS = cast<MDString>(N->getOperand(0));
3285 // Add "@(#)" to support retrieving the command line information with the
3286 // AIX "what" command
3287 RSOS << "@(#)opt " << MDS->getString() << "\n";
3288 RSOS.write('\0');
3289 }
3290 OutStreamer->emitXCOFFCInfoSym(".GCC.command.line", RSOS.str());
3291}
3292
3293// Force static initialization.
3303}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:135
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
#define RegName(no)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
static std::string getRegisterName(const TargetRegisterInfo *TRI, Register Reg)
Definition: MIParser.cpp:1414
This file implements a map that provides insertion order iteration.
Module.h This file contains the declarations for the Module class.
#define P(N)
static void collectTOCStats(PPCAsmPrinter::TOCEntryType Type)
static bool isSpecialLLVMGlobalArrayForStaticInit(const GlobalVariable *GV)
static bool isSpecialLLVMGlobalArrayToSkip(const GlobalVariable *GV)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCAsmPrinter()
#define GENBOOLCOMMENT(Prefix, V, Field)
static MCSymbol * getMCSymbolForTOCPseudoMO(const MachineOperand &MO, AsmPrinter &AP)
Map a machine operand for a TOC pseudo-machine instruction to its corresponding MCSymbol.
static void setOptionalCodeModel(MCSymbolXCOFF *XSym, CodeModel::Model CM)
static AsmPrinter * createPPCAsmPrinterPass(TargetMachine &tm, std::unique_ptr< MCStreamer > &&Streamer)
static PPCAsmPrinter::TOCEntryType getTOCEntryTypeForMO(const MachineOperand &MO)
static CodeModel::Model getCodeModel(const PPCSubtarget &S, const TargetMachine &TM, const MachineOperand &MO)
static std::string convertToSinitPriority(int Priority)
static MCSymbol * createMCSymbolForTlsGetAddr(MCContext &Ctx, unsigned MIOpc)
This helper function creates the TlsGetAddr/TlsGetMod MCSymbol for AIX.
#define GENVALUECOMMENT(PrefixAndName, V, Field)
static unsigned mapToSinitPriority(int P)
static void tocDataChecks(unsigned PointerSize, const GlobalVariable *GV)
static cl::opt< bool > EnableSSPCanaryBitInTB("aix-ssp-tb-bit", cl::init(false), cl::desc("Enable Passing SSP Canary info in Trackback on AIX"), cl::Hidden)
if(VerifyEach)
const char LLVMTargetMachineRef TM
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
Provides a library for accessing information about this process and other processes on the operating ...
static SDValue lowerConstant(SDValue Op, SelectionDAG &DAG, const RISCVSubtarget &Subtarget)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
This file defines the SmallPtrSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
This file contains some functions that are useful when dealing with strings.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:469
Value * RHS
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
virtual void emitInstruction(const MachineInstr *)
Targets should implement this to emit instructions.
Definition: AsmPrinter.h:567
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:700
void emitXRayTable()
Emit a table with all XRay instrumentation points.
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS)
Print the MachineOperand as a symbol.
virtual void SetupMachineFunction(MachineFunction &MF)
This should be called when a new MachineFunction is being processed from runOnMachineFunction.
virtual void emitStartOfAsmFile(Module &)
This virtual method can be overridden by targets that want to emit something at the start of their fi...
Definition: AsmPrinter.h:543
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: AsmPrinter.cpp:449
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: AsmPrinter.h:395
virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
virtual void emitFunctionEntryLabel()
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1017
This is an important base class in LLVM.
Definition: Constant.h:41
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size in bytes, rounded up to a whole number of bytes.
Definition: DataLayout.cpp:750
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:672
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
static GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:525
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:80
bool hasComdat() const
Definition: GlobalObject.h:128
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:110
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:248
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:281
LinkageTypes getLinkage() const
Definition: GlobalValue.h:546
bool hasDefaultVisibility() const
Definition: GlobalValue.h:249
bool hasPrivateLinkage() const
Definition: GlobalValue.h:527
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:271
bool hasDLLExportStorageClass() const
Definition: GlobalValue.h:281
bool isDeclarationForLinker() const
Definition: GlobalValue.h:618
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:375
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:67
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:68
@ ProtectedVisibility
The GV is protected.
Definition: GlobalValue.h:69
bool hasCommonLinkage() const
Definition: GlobalValue.h:532
bool hasAppendingLinkage() const
Definition: GlobalValue.h:525
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:62
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:54
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:57
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:56
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:58
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:53
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:61
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:55
Type * getValueType() const
Definition: GlobalValue.h:296
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists.
bool hasInitializer() const
Definitions have initializers, declarations don't.
std::optional< CodeModel::Model > getCodeModel() const
Get the custom code model of this global if it has one.
Binary assembler expressions.
Definition: MCExpr.h:492
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:642
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:536
Opcode getOpcode() const
Get the kind of this binary expression.
Definition: MCExpr.h:636
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:621
@ Add
Addition.
Definition: MCExpr.h:495
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:81
MCSectionXCOFF * getXCOFFSection(StringRef Section, SectionKind K, std::optional< XCOFF::CsectProperties > CsectProp=std::nullopt, bool MultiSymbolsAllowed=false, const char *BeginSymName=nullptr, std::optional< XCOFF::DwarfSectionSubtypeFlags > DwarfSubtypeFlags=std::nullopt)
Definition: MCContext.cpp:784
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
MCInstBuilder & addReg(unsigned Reg)
Add a new register operand.
Definition: MCInstBuilder.h:37
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
Definition: MCInstBuilder.h:43
MCInstBuilder & addExpr(const MCExpr *Val)
Add a new MCExpr operand.
Definition: MCInstBuilder.h:61
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
void addOperand(const MCOperand Op)
Definition: MCInst.h:210
void setOpcode(unsigned Op)
Definition: MCInst.h:197
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:162
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:26
XCOFF::StorageMappingClass getMappingClass() const
MCSymbolXCOFF * getQualNameSymbol() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
void setAlignment(Align Value)
Definition: MCSection.h:141
void ensureMinAlignment(Align MinAlignment)
Makes sure that Alignment is at least MinAlignment.
Definition: MCSection.h:144
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:397
StringRef getSymbolTableName() const
Definition: MCSymbolXCOFF.h:67
void setPerSymbolCodeModel(MCSymbolXCOFF::CodeModel Model)
Definition: MCSymbolXCOFF.h:85
void setStorageClass(XCOFF::StorageClass SC)
Definition: MCSymbolXCOFF.h:41
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:58
Metadata node.
Definition: Metadata.h:1067
A single uniqued string.
Definition: Metadata.h:720
StringRef getString() const
Definition: Metadata.cpp:610
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
MCSection * getSection() const
Returns the Section this function belongs to.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
const BlockAddress * getBlockAddress() const
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
int64_t getOffset() const
Return the offset from the symbol in this operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
iterator end()
Definition: MapVector.h:71
iterator find(const KeyT &Key)
Definition: MapVector.h:167
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:293
A tuple of MDNodes.
Definition: Metadata.h:1729
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1382
unsigned getNumOperands() const
Definition: Metadata.cpp:1378
uint64_t getTOCSaveOffset() const
getTOCSaveOffset - Return the previous frame offset to save the TOC register – 64-bit SVR4 ABI only.
PPCFunctionInfo - This class is derived from MachineFunction private PowerPC target-specific informat...
MCSymbol * getPICOffsetSymbol(MachineFunction &MF) const
const SmallVectorImpl< Register > & getMustSaveCRs() const
unsigned getFloatingPointParmsNum() const
MCSymbol * getGlobalEPSymbol(MachineFunction &MF) const
MCSymbol * getLocalEPSymbol(MachineFunction &MF) const
unsigned getVectorParmsNum() const
uint32_t getVecExtParmsType() const
MCSymbol * getTOCOffsetSymbol(MachineFunction &MF) const
unsigned getFixedParmsNum() const
static const char * getRegisterName(MCRegister Reg)
static bool hasTLSFlag(unsigned TF)
Definition: PPCInstrInfo.h:315
static const PPCMCExpr * createLo(const MCExpr *Expr, MCContext &Ctx)
Definition: PPCMCExpr.h:49
static const PPCMCExpr * createHa(const MCExpr *Expr, MCContext &Ctx)
Definition: PPCMCExpr.h:57
bool is32BitELFABI() const
Definition: PPCSubtarget.h:219
bool isAIXABI() const
Definition: PPCSubtarget.h:214
const PPCFrameLowering * getFrameLowering() const override
Definition: PPCSubtarget.h:142
bool isPPC64() const
isPPC64 - Return true if we are generating code for 64-bit pointer mode.
bool isUsingPCRelativeCalls() const
CodeModel::Model getCodeModel(const TargetMachine &TM, const GlobalValue *GV) const
Calculates the effective code model for argument GV.
bool isELFv2ABI() const
const PPCRegisterInfo * getRegisterInfo() const override
Definition: PPCSubtarget.h:152
bool isGVIndirectSymbol(const GlobalValue *GV) const
True if the GV will be accessed via an indirect symbol.
Common code between 32-bit and 64-bit PowerPC targets.
bool hasGlibcHWCAPAccess() const
virtual void emitAbiVersion(int AbiVersion)
virtual void emitTCEntry(const MCSymbol &S, MCSymbolRefExpr::VariantKind Kind)
virtual void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset)
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
MI-level patchpoint operands.
Definition: StackMaps.h:76
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
bool isThreadBSSLocal() const
Definition: SectionKind.h:163
static SectionKind getText()
Definition: SectionKind.h:190
bool isBSSLocal() const
Definition: SectionKind.h:170
static SectionKind getData()
Definition: SectionKind.h:213
bool isThreadLocal() const
Definition: SectionKind.h:157
bool isReadOnly() const
Definition: SectionKind.h:131
bool isGlobalWriteableData() const
Definition: SectionKind.h:165
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
bool empty() const
Definition: SmallVector.h:94
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
void recordPatchPoint(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a patchpoint instruction.
Definition: StackMaps.cpp:548
void recordStackMap(const MCSymbol &L, const MachineInstr &MI)
Generate a stackmap record for a stackmap instruction.
Definition: StackMaps.cpp:538
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:567
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:257
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:90
static bool ShouldSetSSPCanaryBitInTB(const MachineFunction *MF)
static MCSymbol * getEHInfoTableSymbol(const MachineFunction *MF)
static XCOFF::StorageClass getStorageClassForGlobal(const GlobalValue *GV)
static bool ShouldEmitEHBlock(const MachineFunction *MF)
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:76
const Triple & getTargetTriple() const
bool isOSAIX() const
Tests whether the OS is AIX.
Definition: Triple.h:694
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
LLVM Value Representation.
Definition: Value.h:74
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:4996
Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition: Value.cpp:926
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:5079
bool hasName() const
Definition: Value.h:261
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:660
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:690
static Pid getProcessId()
Get the process's identifier.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:121
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SHF_ALLOC
Definition: ELF.h:1157
@ SHF_WRITE
Definition: ELF.h:1154
@ SHT_PROGBITS
Definition: ELF.h:1063
const uint64_t Version
Definition: InstrProf.h:1153
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:148
@ MO_TLSLDM_FLAG
MO_TLSLDM_FLAG - on AIX the ML relocation type is only valid for a reference to a TOC symbol from the...
Definition: PPC.h:146
@ MO_TPREL_PCREL_FLAG
MO_TPREL_PCREL_FLAG = MO_PCREL_FLAG | MO_TPREL_FLAG.
Definition: PPC.h:197
@ MO_GOT_TPREL_PCREL_FLAG
MO_GOT_TPREL_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:172
@ MO_TLSGDM_FLAG
MO_TLSGDM_FLAG - If this bit is set the symbol reference is relative to the region handle of TLS Gene...
Definition: PPC.h:154
@ MO_TLSLD_FLAG
MO_TLSLD_FLAG - If this bit is set the symbol reference is relative to TLS Local Dynamic model.
Definition: PPC.h:150
@ MO_TPREL_FLAG
MO_TPREL_FLAG - If this bit is set, the symbol reference is relative to the thread pointer and the sy...
Definition: PPC.h:140
@ MO_GOT_TLSLD_PCREL_FLAG
MO_GOT_TLSLD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:166
@ MO_TLSGD_FLAG
MO_TLSGD_FLAG - If this bit is set the symbol reference is relative to TLS General Dynamic model for ...
Definition: PPC.h:135
@ MO_GOT_TLSGD_PCREL_FLAG
MO_GOT_TLSGD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition: PPC.h:160
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Definition: PPCPredicates.h:26
const char * stripRegisterPrefix(const char *RegName)
stripRegisterPrefix - This method strips the character prefix from a register name so that only the n...
Predicate InvertPredicate(Predicate Opcode)
Invert the specified predicate. != -> ==, < -> >=.
static bool isVRRegister(unsigned Reg)
static bool isVFRegister(unsigned Reg)
@ CE
Windows NT (Windows on ARM)
Reg
All possible values of the reg field in the ModR/M byte.
SmallString< 32 > getExtendedTBTableFlagString(uint8_t Flag)
Definition: XCOFF.cpp:162
Expected< SmallString< 32 > > parseParmsTypeWithVecInfo(uint32_t Value, unsigned FixedParmsNum, unsigned FloatingParmsNum, unsigned VectorParmsNum)
Definition: XCOFF.cpp:188
Expected< SmallString< 32 > > parseParmsType(uint32_t Value, unsigned FixedParmsNum, unsigned FloatingParmsNum)
Definition: XCOFF.cpp:110
Expected< SmallString< 32 > > parseVectorParmsType(uint32_t Value, unsigned ParmsNum)
Definition: XCOFF.cpp:240
@ XMC_RW
Read Write Data.
Definition: XCOFF.h:117
@ XMC_RO
Read Only Constant.
Definition: XCOFF.h:106
@ XMC_TD
Scalar data item in the TOC.
Definition: XCOFF.h:120
@ XMC_PR
Program Code.
Definition: XCOFF.h:105
StringRef getNameForTracebackTableLanguageId(TracebackTable::LanguageID LangId)
Definition: XCOFF.cpp:87
constexpr uint8_t AllocRegNo
Definition: XCOFF.h:44
@ XTY_SD
Csect definition for initialized storage.
Definition: XCOFF.h:242
@ XTY_ER
External reference.
Definition: XCOFF.h:241
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
static unsigned combineHashValue(unsigned a, unsigned b)
Simplistic combination of 32-bit hash values into 32-bit hash values.
Definition: DenseMapInfo.h:29
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
@ Offset
Definition: DWP.cpp:456
Target & getThePPC64LETarget()
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1680
bool LowerPPCMachineOperandToMCOperand(const MachineOperand &MO, MCOperand &OutMO, AsmPrinter &AP)
Target & getThePPC32Target()
std::string getUniqueModuleId(Module *M)
Produce a unique identifier for this module by taking the MD5 sum of the names of the module's strong...
void LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width, bool Upper=false)
format_hex_no_prefix - Output N as a fixed width hexadecimal.
Definition: Format.h:200
Target & getThePPC64Target()
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
Definition: Threading.cpp:33
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:66
Target & getThePPC32LETarget()
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
MCSymbolAttr
Definition: MCDirectives.h:18
@ MCSA_Weak
.weak
Definition: MCDirectives.h:45
@ MCSA_Global
.type _foo, @gnu_unique_object
Definition: MCDirectives.h:30
@ MCSA_Extern
.extern (XCOFF)
Definition: MCDirectives.h:32
@ MCSA_LGlobal
.lglobl (XCOFF)
Definition: MCDirectives.h:31
@ MCSA_Invalid
Not a valid directive.
Definition: MCDirectives.h:19
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
std::pair< const MCSymbol *, MCSymbolRefExpr::VariantKind > TOCKey
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:50
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141
static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn)
RegisterAsmPrinter - Register an AsmPrinter implementation for the given target.
static constexpr uint32_t FPRSavedMask
Definition: XCOFF.h:412
static constexpr uint16_t NumberOfVRSavedMask
Definition: XCOFF.h:442
static constexpr uint8_t NumberOfFloatingPointParmsShift
Definition: XCOFF.h:428
static constexpr uint32_t NumberOfFixedParmsMask
Definition: XCOFF.h:422
static constexpr uint16_t HasVMXInstructionMask
Definition: XCOFF.h:448
static constexpr uint32_t IsLRSavedMask
Definition: XCOFF.h:406
static constexpr uint16_t HasVarArgsMask
Definition: XCOFF.h:444
static constexpr uint32_t IsAllocaUsedMask
Definition: XCOFF.h:403
static constexpr uint16_t IsVRSavedOnStackMask
Definition: XCOFF.h:443
static constexpr uint16_t NumberOfVectorParmsMask
Definition: XCOFF.h:447
static constexpr uint32_t IsFloatingPointPresentMask
Definition: XCOFF.h:396
static constexpr uint32_t FPRSavedShift
Definition: XCOFF.h:413
static constexpr uint32_t NumberOfFloatingPointParmsMask
Definition: XCOFF.h:426
static constexpr uint32_t HasControlledStorageMask
Definition: XCOFF.h:394
static constexpr uint32_t HasExtensionTableMask
Definition: XCOFF.h:416
static constexpr uint32_t HasTraceBackTableOffsetMask
Definition: XCOFF.h:392
static constexpr uint32_t IsCRSavedMask
Definition: XCOFF.h:405
static constexpr uint8_t NumberOfFixedParmsShift
Definition: XCOFF.h:423
static constexpr uint32_t GPRSavedMask
Definition: XCOFF.h:418
static constexpr uint8_t NumberOfVectorParmsShift
Definition: XCOFF.h:449
static constexpr uint32_t HasParmsOnStackMask
Definition: XCOFF.h:427
static constexpr uint32_t IsFunctionNamePresentMask
Definition: XCOFF.h:402
static constexpr uint32_t IsBackChainStoredMask
Definition: XCOFF.h:410
static constexpr uint32_t IsInterruptHandlerMask
Definition: XCOFF.h:401
static constexpr uint32_t HasVectorInfoMask
Definition: XCOFF.h:417
static constexpr uint8_t NumberOfVRSavedShift
Definition: XCOFF.h:445
static constexpr uint32_t GPRSavedShift
Definition: XCOFF.h:419