xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
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 
18 #include "MCTargetDesc/PPCInstPrinter.h"
19 #include "MCTargetDesc/PPCMCExpr.h"
20 #include "MCTargetDesc/PPCMCTargetDesc.h"
21 #include "MCTargetDesc/PPCPredicates.h"
22 #include "PPC.h"
23 #include "PPCInstrInfo.h"
24 #include "PPCMachineFunctionInfo.h"
25 #include "PPCSubtarget.h"
26 #include "PPCTargetMachine.h"
27 #include "PPCTargetStreamer.h"
28 #include "TargetInfo/PowerPCTargetInfo.h"
29 #include "llvm/ADT/MapVector.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/ADT/Triple.h"
33 #include "llvm/ADT/Twine.h"
34 #include "llvm/BinaryFormat/ELF.h"
35 #include "llvm/CodeGen/AsmPrinter.h"
36 #include "llvm/CodeGen/MachineBasicBlock.h"
37 #include "llvm/CodeGen/MachineFunction.h"
38 #include "llvm/CodeGen/MachineInstr.h"
39 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
40 #include "llvm/CodeGen/MachineOperand.h"
41 #include "llvm/CodeGen/MachineRegisterInfo.h"
42 #include "llvm/CodeGen/StackMaps.h"
43 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
44 #include "llvm/IR/DataLayout.h"
45 #include "llvm/IR/GlobalValue.h"
46 #include "llvm/IR/GlobalVariable.h"
47 #include "llvm/IR/Module.h"
48 #include "llvm/MC/MCAsmInfo.h"
49 #include "llvm/MC/MCContext.h"
50 #include "llvm/MC/MCDirectives.h"
51 #include "llvm/MC/MCExpr.h"
52 #include "llvm/MC/MCInst.h"
53 #include "llvm/MC/MCInstBuilder.h"
54 #include "llvm/MC/MCSectionELF.h"
55 #include "llvm/MC/MCSectionXCOFF.h"
56 #include "llvm/MC/MCStreamer.h"
57 #include "llvm/MC/MCSymbol.h"
58 #include "llvm/MC/MCSymbolELF.h"
59 #include "llvm/MC/MCSymbolXCOFF.h"
60 #include "llvm/MC/SectionKind.h"
61 #include "llvm/MC/TargetRegistry.h"
62 #include "llvm/Support/Casting.h"
63 #include "llvm/Support/CodeGen.h"
64 #include "llvm/Support/Debug.h"
65 #include "llvm/Support/Error.h"
66 #include "llvm/Support/ErrorHandling.h"
67 #include "llvm/Support/Process.h"
68 #include "llvm/Support/raw_ostream.h"
69 #include "llvm/Target/TargetMachine.h"
70 #include "llvm/Transforms/Utils/ModuleUtils.h"
71 #include <algorithm>
72 #include <cassert>
73 #include <cstdint>
74 #include <memory>
75 #include <new>
76 
77 using namespace llvm;
78 using namespace llvm::XCOFF;
79 
80 #define DEBUG_TYPE "asmprinter"
81 
82 static cl::opt<bool> EnableSSPCanaryBitInTB(
83     "aix-ssp-tb-bit", cl::init(false),
84     cl::desc("Enable Passing SSP Canary info in Trackback on AIX"), cl::Hidden);
85 
86 // Specialize DenseMapInfo to allow
87 // std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind> in DenseMap.
88 // This specialization is needed here because that type is used as keys in the
89 // map representing TOC entries.
90 namespace llvm {
91 template <>
92 struct DenseMapInfo<std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>> {
93   using TOCKey = std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>;
94 
95   static inline TOCKey getEmptyKey() {
96     return {nullptr, MCSymbolRefExpr::VariantKind::VK_None};
97   }
98   static inline TOCKey getTombstoneKey() {
99     return {nullptr, MCSymbolRefExpr::VariantKind::VK_Invalid};
100   }
101   static unsigned getHashValue(const TOCKey &PairVal) {
102     return detail::combineHashValue(
103         DenseMapInfo<const MCSymbol *>::getHashValue(PairVal.first),
104         DenseMapInfo<int>::getHashValue(PairVal.second));
105   }
106   static bool isEqual(const TOCKey &A, const TOCKey &B) { return A == B; }
107 };
108 } // end namespace llvm
109 
110 namespace {
111 
112 class PPCAsmPrinter : public AsmPrinter {
113 protected:
114   // For TLS on AIX, we need to be able to identify TOC entries of specific
115   // VariantKind so we can add the right relocations when we generate the
116   // entries. So each entry is represented by a pair of MCSymbol and
117   // VariantKind. For example, we need to be able to identify the following
118   // entry as a TLSGD entry so we can add the @m relocation:
119   //   .tc .i[TC],i[TL]@m
120   // By default, VK_None is used for the VariantKind.
121   MapVector<std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>,
122             MCSymbol *>
123       TOC;
124   const PPCSubtarget *Subtarget = nullptr;
125   StackMaps SM;
126 
127 public:
128   explicit PPCAsmPrinter(TargetMachine &TM,
129                          std::unique_ptr<MCStreamer> Streamer)
130       : AsmPrinter(TM, std::move(Streamer)), SM(*this) {}
131 
132   StringRef getPassName() const override { return "PowerPC Assembly Printer"; }
133 
134   MCSymbol *lookUpOrCreateTOCEntry(const MCSymbol *Sym,
135                                    MCSymbolRefExpr::VariantKind Kind =
136                                        MCSymbolRefExpr::VariantKind::VK_None);
137 
138   bool doInitialization(Module &M) override {
139     if (!TOC.empty())
140       TOC.clear();
141     return AsmPrinter::doInitialization(M);
142   }
143 
144   void emitInstruction(const MachineInstr *MI) override;
145 
146   /// This function is for PrintAsmOperand and PrintAsmMemoryOperand,
147   /// invoked by EmitMSInlineAsmStr and EmitGCCInlineAsmStr only.
148   /// The \p MI would be INLINEASM ONLY.
149   void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
150 
151   void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
152   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
153                        const char *ExtraCode, raw_ostream &O) override;
154   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
155                              const char *ExtraCode, raw_ostream &O) override;
156 
157   void emitEndOfAsmFile(Module &M) override;
158 
159   void LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI);
160   void LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI);
161   void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK);
162   bool runOnMachineFunction(MachineFunction &MF) override {
163     Subtarget = &MF.getSubtarget<PPCSubtarget>();
164     bool Changed = AsmPrinter::runOnMachineFunction(MF);
165     emitXRayTable();
166     return Changed;
167   }
168 };
169 
170 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
171 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
172 public:
173   explicit PPCLinuxAsmPrinter(TargetMachine &TM,
174                               std::unique_ptr<MCStreamer> Streamer)
175       : PPCAsmPrinter(TM, std::move(Streamer)) {}
176 
177   StringRef getPassName() const override {
178     return "Linux PPC Assembly Printer";
179   }
180 
181   void emitStartOfAsmFile(Module &M) override;
182   void emitEndOfAsmFile(Module &) override;
183 
184   void emitFunctionEntryLabel() override;
185 
186   void emitFunctionBodyStart() override;
187   void emitFunctionBodyEnd() override;
188   void emitInstruction(const MachineInstr *MI) override;
189 };
190 
191 class PPCAIXAsmPrinter : public PPCAsmPrinter {
192 private:
193   /// Symbols lowered from ExternalSymbolSDNodes, we will need to emit extern
194   /// linkage for them in AIX.
195   SmallPtrSet<MCSymbol *, 8> ExtSymSDNodeSymbols;
196 
197   /// A format indicator and unique trailing identifier to form part of the
198   /// sinit/sterm function names.
199   std::string FormatIndicatorAndUniqueModId;
200 
201   // Record a list of GlobalAlias associated with a GlobalObject.
202   // This is used for AIX's extra-label-at-definition aliasing strategy.
203   DenseMap<const GlobalObject *, SmallVector<const GlobalAlias *, 1>>
204       GOAliasMap;
205 
206   uint16_t getNumberOfVRSaved();
207   void emitTracebackTable();
208 
209   SmallVector<const GlobalVariable *, 8> TOCDataGlobalVars;
210 
211   void emitGlobalVariableHelper(const GlobalVariable *);
212 
213 public:
214   PPCAIXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
215       : PPCAsmPrinter(TM, std::move(Streamer)) {
216     if (MAI->isLittleEndian())
217       report_fatal_error(
218           "cannot create AIX PPC Assembly Printer for a little-endian target");
219   }
220 
221   StringRef getPassName() const override { return "AIX PPC Assembly Printer"; }
222 
223   bool doInitialization(Module &M) override;
224 
225   void emitXXStructorList(const DataLayout &DL, const Constant *List,
226                           bool IsCtor) override;
227 
228   void SetupMachineFunction(MachineFunction &MF) override;
229 
230   void emitGlobalVariable(const GlobalVariable *GV) override;
231 
232   void emitFunctionDescriptor() override;
233 
234   void emitFunctionEntryLabel() override;
235 
236   void emitFunctionBodyEnd() override;
237 
238   void emitEndOfAsmFile(Module &) override;
239 
240   void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const override;
241 
242   void emitInstruction(const MachineInstr *MI) override;
243 
244   bool doFinalization(Module &M) override;
245 
246   void emitTTypeReference(const GlobalValue *GV, unsigned Encoding) override;
247 };
248 
249 } // end anonymous namespace
250 
251 void PPCAsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
252                                        raw_ostream &O) {
253   // Computing the address of a global symbol, not calling it.
254   const GlobalValue *GV = MO.getGlobal();
255   getSymbol(GV)->print(O, MAI);
256   printOffset(MO.getOffset(), O);
257 }
258 
259 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
260                                  raw_ostream &O) {
261   const DataLayout &DL = getDataLayout();
262   const MachineOperand &MO = MI->getOperand(OpNo);
263 
264   switch (MO.getType()) {
265   case MachineOperand::MO_Register: {
266     // The MI is INLINEASM ONLY and UseVSXReg is always false.
267     const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
268 
269     // Linux assembler (Others?) does not take register mnemonics.
270     // FIXME - What about special registers used in mfspr/mtspr?
271     O << PPCRegisterInfo::stripRegisterPrefix(RegName);
272     return;
273   }
274   case MachineOperand::MO_Immediate:
275     O << MO.getImm();
276     return;
277 
278   case MachineOperand::MO_MachineBasicBlock:
279     MO.getMBB()->getSymbol()->print(O, MAI);
280     return;
281   case MachineOperand::MO_ConstantPoolIndex:
282     O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
283       << MO.getIndex();
284     return;
285   case MachineOperand::MO_BlockAddress:
286     GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
287     return;
288   case MachineOperand::MO_GlobalAddress: {
289     PrintSymbolOperand(MO, O);
290     return;
291   }
292 
293   default:
294     O << "<unknown operand type: " << (unsigned)MO.getType() << ">";
295     return;
296   }
297 }
298 
299 /// PrintAsmOperand - Print out an operand for an inline asm expression.
300 ///
301 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
302                                     const char *ExtraCode, raw_ostream &O) {
303   // Does this asm operand have a single letter operand modifier?
304   if (ExtraCode && ExtraCode[0]) {
305     if (ExtraCode[1] != 0) return true; // Unknown modifier.
306 
307     switch (ExtraCode[0]) {
308     default:
309       // See if this is a generic print operand
310       return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
311     case 'L': // Write second word of DImode reference.
312       // Verify that this operand has two consecutive registers.
313       if (!MI->getOperand(OpNo).isReg() ||
314           OpNo+1 == MI->getNumOperands() ||
315           !MI->getOperand(OpNo+1).isReg())
316         return true;
317       ++OpNo;   // Return the high-part.
318       break;
319     case 'I':
320       // Write 'i' if an integer constant, otherwise nothing.  Used to print
321       // addi vs add, etc.
322       if (MI->getOperand(OpNo).isImm())
323         O << "i";
324       return false;
325     case 'x':
326       if(!MI->getOperand(OpNo).isReg())
327         return true;
328       // This operand uses VSX numbering.
329       // If the operand is a VMX register, convert it to a VSX register.
330       Register Reg = MI->getOperand(OpNo).getReg();
331       if (PPCInstrInfo::isVRRegister(Reg))
332         Reg = PPC::VSX32 + (Reg - PPC::V0);
333       else if (PPCInstrInfo::isVFRegister(Reg))
334         Reg = PPC::VSX32 + (Reg - PPC::VF0);
335       const char *RegName;
336       RegName = PPCInstPrinter::getRegisterName(Reg);
337       RegName = PPCRegisterInfo::stripRegisterPrefix(RegName);
338       O << RegName;
339       return false;
340     }
341   }
342 
343   printOperand(MI, OpNo, O);
344   return false;
345 }
346 
347 // At the moment, all inline asm memory operands are a single register.
348 // In any case, the output of this routine should always be just one
349 // assembler operand.
350 
351 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
352                                           const char *ExtraCode,
353                                           raw_ostream &O) {
354   if (ExtraCode && ExtraCode[0]) {
355     if (ExtraCode[1] != 0) return true; // Unknown modifier.
356 
357     switch (ExtraCode[0]) {
358     default: return true;  // Unknown modifier.
359     case 'L': // A memory reference to the upper word of a double word op.
360       O << getDataLayout().getPointerSize() << "(";
361       printOperand(MI, OpNo, O);
362       O << ")";
363       return false;
364     case 'y': // A memory reference for an X-form instruction
365       O << "0, ";
366       printOperand(MI, OpNo, O);
367       return false;
368     case 'I':
369       // Write 'i' if an integer constant, otherwise nothing.  Used to print
370       // addi vs add, etc.
371       if (MI->getOperand(OpNo).isImm())
372         O << "i";
373       return false;
374     case 'U': // Print 'u' for update form.
375     case 'X': // Print 'x' for indexed form.
376       // FIXME: Currently for PowerPC memory operands are always loaded
377       // into a register, so we never get an update or indexed form.
378       // This is bad even for offset forms, since even if we know we
379       // have a value in -16(r1), we will generate a load into r<n>
380       // and then load from 0(r<n>).  Until that issue is fixed,
381       // tolerate 'U' and 'X' but don't output anything.
382       assert(MI->getOperand(OpNo).isReg());
383       return false;
384     }
385   }
386 
387   assert(MI->getOperand(OpNo).isReg());
388   O << "0(";
389   printOperand(MI, OpNo, O);
390   O << ")";
391   return false;
392 }
393 
394 /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
395 /// exists for it.  If not, create one.  Then return a symbol that references
396 /// the TOC entry.
397 MCSymbol *
398 PPCAsmPrinter::lookUpOrCreateTOCEntry(const MCSymbol *Sym,
399                                       MCSymbolRefExpr::VariantKind Kind) {
400   MCSymbol *&TOCEntry = TOC[{Sym, Kind}];
401   if (!TOCEntry)
402     TOCEntry = createTempSymbol("C");
403   return TOCEntry;
404 }
405 
406 void PPCAsmPrinter::emitEndOfAsmFile(Module &M) {
407   emitStackMaps(SM);
408 }
409 
410 void PPCAsmPrinter::LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI) {
411   unsigned NumNOPBytes = MI.getOperand(1).getImm();
412 
413   auto &Ctx = OutStreamer->getContext();
414   MCSymbol *MILabel = Ctx.createTempSymbol();
415   OutStreamer->emitLabel(MILabel);
416 
417   SM.recordStackMap(*MILabel, MI);
418   assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
419 
420   // Scan ahead to trim the shadow.
421   const MachineBasicBlock &MBB = *MI.getParent();
422   MachineBasicBlock::const_iterator MII(MI);
423   ++MII;
424   while (NumNOPBytes > 0) {
425     if (MII == MBB.end() || MII->isCall() ||
426         MII->getOpcode() == PPC::DBG_VALUE ||
427         MII->getOpcode() == TargetOpcode::PATCHPOINT ||
428         MII->getOpcode() == TargetOpcode::STACKMAP)
429       break;
430     ++MII;
431     NumNOPBytes -= 4;
432   }
433 
434   // Emit nops.
435   for (unsigned i = 0; i < NumNOPBytes; i += 4)
436     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
437 }
438 
439 // Lower a patchpoint of the form:
440 // [<def>], <id>, <numBytes>, <target>, <numArgs>
441 void PPCAsmPrinter::LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI) {
442   auto &Ctx = OutStreamer->getContext();
443   MCSymbol *MILabel = Ctx.createTempSymbol();
444   OutStreamer->emitLabel(MILabel);
445 
446   SM.recordPatchPoint(*MILabel, MI);
447   PatchPointOpers Opers(&MI);
448 
449   unsigned EncodedBytes = 0;
450   const MachineOperand &CalleeMO = Opers.getCallTarget();
451 
452   if (CalleeMO.isImm()) {
453     int64_t CallTarget = CalleeMO.getImm();
454     if (CallTarget) {
455       assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget &&
456              "High 16 bits of call target should be zero.");
457       Register ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg();
458       EncodedBytes = 0;
459       // Materialize the jump address:
460       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI8)
461                                       .addReg(ScratchReg)
462                                       .addImm((CallTarget >> 32) & 0xFFFF));
463       ++EncodedBytes;
464       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::RLDIC)
465                                       .addReg(ScratchReg)
466                                       .addReg(ScratchReg)
467                                       .addImm(32).addImm(16));
468       ++EncodedBytes;
469       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORIS8)
470                                       .addReg(ScratchReg)
471                                       .addReg(ScratchReg)
472                                       .addImm((CallTarget >> 16) & 0xFFFF));
473       ++EncodedBytes;
474       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORI8)
475                                       .addReg(ScratchReg)
476                                       .addReg(ScratchReg)
477                                       .addImm(CallTarget & 0xFFFF));
478 
479       // Save the current TOC pointer before the remote call.
480       int TOCSaveOffset = Subtarget->getFrameLowering()->getTOCSaveOffset();
481       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::STD)
482                                       .addReg(PPC::X2)
483                                       .addImm(TOCSaveOffset)
484                                       .addReg(PPC::X1));
485       ++EncodedBytes;
486 
487       // If we're on ELFv1, then we need to load the actual function pointer
488       // from the function descriptor.
489       if (!Subtarget->isELFv2ABI()) {
490         // Load the new TOC pointer and the function address, but not r11
491         // (needing this is rare, and loading it here would prevent passing it
492         // via a 'nest' parameter.
493         EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
494                                         .addReg(PPC::X2)
495                                         .addImm(8)
496                                         .addReg(ScratchReg));
497         ++EncodedBytes;
498         EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
499                                         .addReg(ScratchReg)
500                                         .addImm(0)
501                                         .addReg(ScratchReg));
502         ++EncodedBytes;
503       }
504 
505       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR8)
506                                       .addReg(ScratchReg));
507       ++EncodedBytes;
508       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTRL8));
509       ++EncodedBytes;
510 
511       // Restore the TOC pointer after the call.
512       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
513                                       .addReg(PPC::X2)
514                                       .addImm(TOCSaveOffset)
515                                       .addReg(PPC::X1));
516       ++EncodedBytes;
517     }
518   } else if (CalleeMO.isGlobal()) {
519     const GlobalValue *GValue = CalleeMO.getGlobal();
520     MCSymbol *MOSymbol = getSymbol(GValue);
521     const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext);
522 
523     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL8_NOP)
524                                     .addExpr(SymVar));
525     EncodedBytes += 2;
526   }
527 
528   // Each instruction is 4 bytes.
529   EncodedBytes *= 4;
530 
531   // Emit padding.
532   unsigned NumBytes = Opers.getNumPatchBytes();
533   assert(NumBytes >= EncodedBytes &&
534          "Patchpoint can't request size less than the length of a call.");
535   assert((NumBytes - EncodedBytes) % 4 == 0 &&
536          "Invalid number of NOP bytes requested!");
537   for (unsigned i = EncodedBytes; i < NumBytes; i += 4)
538     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
539 }
540 
541 /// This helper function creates the TlsGetAddr MCSymbol for AIX. We will
542 /// create the csect and use the qual-name symbol instead of creating just the
543 /// external symbol.
544 static MCSymbol *createMCSymbolForTlsGetAddr(MCContext &Ctx) {
545   return Ctx
546       .getXCOFFSection(".__tls_get_addr", SectionKind::getText(),
547                        XCOFF::CsectProperties(XCOFF::XMC_PR, XCOFF::XTY_ER))
548       ->getQualNameSymbol();
549 }
550 
551 /// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a
552 /// call to __tls_get_addr to the current output stream.
553 void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI,
554                                 MCSymbolRefExpr::VariantKind VK) {
555   MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
556   unsigned Opcode = PPC::BL8_NOP_TLS;
557 
558   assert(MI->getNumOperands() >= 3 && "Expecting at least 3 operands from MI");
559   if (MI->getOperand(2).getTargetFlags() == PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
560       MI->getOperand(2).getTargetFlags() == PPCII::MO_GOT_TLSLD_PCREL_FLAG) {
561     Kind = MCSymbolRefExpr::VK_PPC_NOTOC;
562     Opcode = PPC::BL8_NOTOC_TLS;
563   }
564   const Module *M = MF->getFunction().getParent();
565 
566   assert(MI->getOperand(0).isReg() &&
567          ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) ||
568           (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) &&
569          "GETtls[ld]ADDR[32] must define GPR3");
570   assert(MI->getOperand(1).isReg() &&
571          ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) ||
572           (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) &&
573          "GETtls[ld]ADDR[32] must read GPR3");
574 
575   if (Subtarget->isAIXABI()) {
576     // On AIX, the variable offset should already be in R4 and the region handle
577     // should already be in R3.
578     // For TLSGD, which currently is the only supported access model, we only
579     // need to generate an absolute branch to .__tls_get_addr.
580     Register VarOffsetReg = Subtarget->isPPC64() ? PPC::X4 : PPC::R4;
581     (void)VarOffsetReg;
582     assert(MI->getOperand(2).isReg() &&
583            MI->getOperand(2).getReg() == VarOffsetReg &&
584            "GETtls[ld]ADDR[32] must read GPR4");
585     MCSymbol *TlsGetAddr = createMCSymbolForTlsGetAddr(OutContext);
586     const MCExpr *TlsRef = MCSymbolRefExpr::create(
587         TlsGetAddr, MCSymbolRefExpr::VK_None, OutContext);
588     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BLA).addExpr(TlsRef));
589     return;
590   }
591 
592   MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol("__tls_get_addr");
593 
594   if (Subtarget->is32BitELFABI() && isPositionIndependent())
595     Kind = MCSymbolRefExpr::VK_PLT;
596 
597   const MCExpr *TlsRef =
598     MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext);
599 
600   // Add 32768 offset to the symbol so we follow up the latest GOT/PLT ABI.
601   if (Kind == MCSymbolRefExpr::VK_PLT && Subtarget->isSecurePlt() &&
602       M->getPICLevel() == PICLevel::BigPIC)
603     TlsRef = MCBinaryExpr::createAdd(
604         TlsRef, MCConstantExpr::create(32768, OutContext), OutContext);
605   const MachineOperand &MO = MI->getOperand(2);
606   const GlobalValue *GValue = MO.getGlobal();
607   MCSymbol *MOSymbol = getSymbol(GValue);
608   const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
609   EmitToStreamer(*OutStreamer,
610                  MCInstBuilder(Subtarget->isPPC64() ? Opcode
611                                                     : (unsigned)PPC::BL_TLS)
612                      .addExpr(TlsRef)
613                      .addExpr(SymVar));
614 }
615 
616 /// Map a machine operand for a TOC pseudo-machine instruction to its
617 /// corresponding MCSymbol.
618 static MCSymbol *getMCSymbolForTOCPseudoMO(const MachineOperand &MO,
619                                            AsmPrinter &AP) {
620   switch (MO.getType()) {
621   case MachineOperand::MO_GlobalAddress:
622     return AP.getSymbol(MO.getGlobal());
623   case MachineOperand::MO_ConstantPoolIndex:
624     return AP.GetCPISymbol(MO.getIndex());
625   case MachineOperand::MO_JumpTableIndex:
626     return AP.GetJTISymbol(MO.getIndex());
627   case MachineOperand::MO_BlockAddress:
628     return AP.GetBlockAddressSymbol(MO.getBlockAddress());
629   default:
630     llvm_unreachable("Unexpected operand type to get symbol.");
631   }
632 }
633 
634 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
635 /// the current output stream.
636 ///
637 void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
638   MCInst TmpInst;
639   const bool IsPPC64 = Subtarget->isPPC64();
640   const bool IsAIX = Subtarget->isAIXABI();
641   const Module *M = MF->getFunction().getParent();
642   PICLevel::Level PL = M->getPICLevel();
643 
644 #ifndef NDEBUG
645   // Validate that SPE and FPU are mutually exclusive in codegen
646   if (!MI->isInlineAsm()) {
647     for (const MachineOperand &MO: MI->operands()) {
648       if (MO.isReg()) {
649         Register Reg = MO.getReg();
650         if (Subtarget->hasSPE()) {
651           if (PPC::F4RCRegClass.contains(Reg) ||
652               PPC::F8RCRegClass.contains(Reg) ||
653               PPC::VFRCRegClass.contains(Reg) ||
654               PPC::VRRCRegClass.contains(Reg) ||
655               PPC::VSFRCRegClass.contains(Reg) ||
656               PPC::VSSRCRegClass.contains(Reg)
657               )
658             llvm_unreachable("SPE targets cannot have FPRegs!");
659         } else {
660           if (PPC::SPERCRegClass.contains(Reg))
661             llvm_unreachable("SPE register found in FPU-targeted code!");
662         }
663       }
664     }
665   }
666 #endif
667 
668   auto getTOCRelocAdjustedExprForXCOFF = [this](const MCExpr *Expr,
669                                                 ptrdiff_t OriginalOffset) {
670     // Apply an offset to the TOC-based expression such that the adjusted
671     // notional offset from the TOC base (to be encoded into the instruction's D
672     // or DS field) is the signed 16-bit truncation of the original notional
673     // offset from the TOC base.
674     // This is consistent with the treatment used both by XL C/C++ and
675     // by AIX ld -r.
676     ptrdiff_t Adjustment =
677         OriginalOffset - llvm::SignExtend32<16>(OriginalOffset);
678     return MCBinaryExpr::createAdd(
679         Expr, MCConstantExpr::create(-Adjustment, OutContext), OutContext);
680   };
681 
682   auto getTOCEntryLoadingExprForXCOFF =
683       [IsPPC64, getTOCRelocAdjustedExprForXCOFF,
684        this](const MCSymbol *MOSymbol, const MCExpr *Expr,
685              MCSymbolRefExpr::VariantKind VK =
686                  MCSymbolRefExpr::VariantKind::VK_None) -> const MCExpr * {
687     const unsigned EntryByteSize = IsPPC64 ? 8 : 4;
688     const auto TOCEntryIter = TOC.find({MOSymbol, VK});
689     assert(TOCEntryIter != TOC.end() &&
690            "Could not find the TOC entry for this symbol.");
691     const ptrdiff_t EntryDistanceFromTOCBase =
692         (TOCEntryIter - TOC.begin()) * EntryByteSize;
693     constexpr int16_t PositiveTOCRange = INT16_MAX;
694 
695     if (EntryDistanceFromTOCBase > PositiveTOCRange)
696       return getTOCRelocAdjustedExprForXCOFF(Expr, EntryDistanceFromTOCBase);
697 
698     return Expr;
699   };
700   auto GetVKForMO = [&](const MachineOperand &MO) {
701     // For GD TLS access on AIX, we have two TOC entries for the symbol (one for
702     // the variable offset and the other for the region handle). They are
703     // differentiated by MO_TLSGD_FLAG and MO_TLSGDM_FLAG.
704     if (MO.getTargetFlags() & PPCII::MO_TLSGDM_FLAG)
705       return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM;
706     if (MO.getTargetFlags() & PPCII::MO_TLSGD_FLAG)
707       return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGD;
708     return MCSymbolRefExpr::VariantKind::VK_None;
709   };
710 
711   // Lower multi-instruction pseudo operations.
712   switch (MI->getOpcode()) {
713   default: break;
714   case TargetOpcode::DBG_VALUE:
715     llvm_unreachable("Should be handled target independently");
716   case TargetOpcode::STACKMAP:
717     return LowerSTACKMAP(SM, *MI);
718   case TargetOpcode::PATCHPOINT:
719     return LowerPATCHPOINT(SM, *MI);
720 
721   case PPC::MoveGOTtoLR: {
722     // Transform %lr = MoveGOTtoLR
723     // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4
724     // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding
725     // _GLOBAL_OFFSET_TABLE_) has exactly one instruction:
726     //      blrl
727     // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local
728     MCSymbol *GOTSymbol =
729       OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
730     const MCExpr *OffsExpr =
731       MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol,
732                                                       MCSymbolRefExpr::VK_PPC_LOCAL,
733                                                       OutContext),
734                               MCConstantExpr::create(4, OutContext),
735                               OutContext);
736 
737     // Emit the 'bl'.
738     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr));
739     return;
740   }
741   case PPC::MovePCtoLR:
742   case PPC::MovePCtoLR8: {
743     // Transform %lr = MovePCtoLR
744     // Into this, where the label is the PIC base:
745     //     bl L1$pb
746     // L1$pb:
747     MCSymbol *PICBase = MF->getPICBaseSymbol();
748 
749     // Emit the 'bl'.
750     EmitToStreamer(*OutStreamer,
751                    MCInstBuilder(PPC::BL)
752                        // FIXME: We would like an efficient form for this, so we
753                        // don't have to do a lot of extra uniquing.
754                        .addExpr(MCSymbolRefExpr::create(PICBase, OutContext)));
755 
756     // Emit the label.
757     OutStreamer->emitLabel(PICBase);
758     return;
759   }
760   case PPC::UpdateGBR: {
761     // Transform %rd = UpdateGBR(%rt, %ri)
762     // Into: lwz %rt, .L0$poff - .L0$pb(%ri)
763     //       add %rd, %rt, %ri
764     // or into (if secure plt mode is on):
765     //       addis r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@ha
766     //       addi r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@l
767     // Get the offset from the GOT Base Register to the GOT
768     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
769     if (Subtarget->isSecurePlt() && isPositionIndependent() ) {
770       unsigned PICR = TmpInst.getOperand(0).getReg();
771       MCSymbol *BaseSymbol = OutContext.getOrCreateSymbol(
772           M->getPICLevel() == PICLevel::SmallPIC ? "_GLOBAL_OFFSET_TABLE_"
773                                                  : ".LTOC");
774       const MCExpr *PB =
775           MCSymbolRefExpr::create(MF->getPICBaseSymbol(), OutContext);
776 
777       const MCExpr *DeltaExpr = MCBinaryExpr::createSub(
778           MCSymbolRefExpr::create(BaseSymbol, OutContext), PB, OutContext);
779 
780       const MCExpr *DeltaHi = PPCMCExpr::createHa(DeltaExpr, OutContext);
781       EmitToStreamer(
782           *OutStreamer,
783           MCInstBuilder(PPC::ADDIS).addReg(PICR).addReg(PICR).addExpr(DeltaHi));
784 
785       const MCExpr *DeltaLo = PPCMCExpr::createLo(DeltaExpr, OutContext);
786       EmitToStreamer(
787           *OutStreamer,
788           MCInstBuilder(PPC::ADDI).addReg(PICR).addReg(PICR).addExpr(DeltaLo));
789       return;
790     } else {
791       MCSymbol *PICOffset =
792         MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol(*MF);
793       TmpInst.setOpcode(PPC::LWZ);
794       const MCExpr *Exp =
795         MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext);
796       const MCExpr *PB =
797         MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
798                                 MCSymbolRefExpr::VK_None,
799                                 OutContext);
800       const MCOperand TR = TmpInst.getOperand(1);
801       const MCOperand PICR = TmpInst.getOperand(0);
802 
803       // Step 1: lwz %rt, .L$poff - .L$pb(%ri)
804       TmpInst.getOperand(1) =
805           MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext));
806       TmpInst.getOperand(0) = TR;
807       TmpInst.getOperand(2) = PICR;
808       EmitToStreamer(*OutStreamer, TmpInst);
809 
810       TmpInst.setOpcode(PPC::ADD4);
811       TmpInst.getOperand(0) = PICR;
812       TmpInst.getOperand(1) = TR;
813       TmpInst.getOperand(2) = PICR;
814       EmitToStreamer(*OutStreamer, TmpInst);
815       return;
816     }
817   }
818   case PPC::LWZtoc: {
819     // Transform %rN = LWZtoc @op1, %r2
820     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
821 
822     // Change the opcode to LWZ.
823     TmpInst.setOpcode(PPC::LWZ);
824 
825     const MachineOperand &MO = MI->getOperand(1);
826     assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
827            "Invalid operand for LWZtoc.");
828 
829     // Map the operand to its corresponding MCSymbol.
830     const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
831 
832     // Create a reference to the GOT entry for the symbol. The GOT entry will be
833     // synthesized later.
834     if (PL == PICLevel::SmallPIC && !IsAIX) {
835       const MCExpr *Exp =
836         MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_GOT,
837                                 OutContext);
838       TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
839       EmitToStreamer(*OutStreamer, TmpInst);
840       return;
841     }
842 
843     MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
844 
845     // Otherwise, use the TOC. 'TOCEntry' is a label used to reference the
846     // storage allocated in the TOC which contains the address of
847     // 'MOSymbol'. Said TOC entry will be synthesized later.
848     MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol, VK);
849     const MCExpr *Exp =
850         MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_None, OutContext);
851 
852     // AIX uses the label directly as the lwz displacement operand for
853     // references into the toc section. The displacement value will be generated
854     // relative to the toc-base.
855     if (IsAIX) {
856       assert(
857           TM.getCodeModel() == CodeModel::Small &&
858           "This pseudo should only be selected for 32-bit small code model.");
859       Exp = getTOCEntryLoadingExprForXCOFF(MOSymbol, Exp, VK);
860       TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
861 
862       // Print MO for better readability
863       if (isVerbose())
864         OutStreamer->GetCommentOS() << MO << '\n';
865       EmitToStreamer(*OutStreamer, TmpInst);
866       return;
867     }
868 
869     // Create an explicit subtract expression between the local symbol and
870     // '.LTOC' to manifest the toc-relative offset.
871     const MCExpr *PB = MCSymbolRefExpr::create(
872         OutContext.getOrCreateSymbol(Twine(".LTOC")), OutContext);
873     Exp = MCBinaryExpr::createSub(Exp, PB, OutContext);
874     TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
875     EmitToStreamer(*OutStreamer, TmpInst);
876     return;
877   }
878   case PPC::ADDItoc:
879   case PPC::ADDItoc8: {
880     assert(IsAIX && TM.getCodeModel() == CodeModel::Small &&
881            "PseudoOp only valid for small code model AIX");
882 
883     // Transform %rN = ADDItoc/8 @op1, %r2.
884     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
885 
886     // Change the opcode to load address.
887     TmpInst.setOpcode((!IsPPC64) ? (PPC::LA) : (PPC::LA8));
888 
889     const MachineOperand &MO = MI->getOperand(1);
890     assert(MO.isGlobal() && "Invalid operand for ADDItoc[8].");
891 
892     // Map the operand to its corresponding MCSymbol.
893     const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
894 
895     const MCExpr *Exp =
896         MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_None, OutContext);
897 
898     TmpInst.getOperand(1) = TmpInst.getOperand(2);
899     TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
900     EmitToStreamer(*OutStreamer, TmpInst);
901     return;
902   }
903   case PPC::LDtocJTI:
904   case PPC::LDtocCPT:
905   case PPC::LDtocBA:
906   case PPC::LDtoc: {
907     // Transform %x3 = LDtoc @min1, %x2
908     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
909 
910     // Change the opcode to LD.
911     TmpInst.setOpcode(PPC::LD);
912 
913     const MachineOperand &MO = MI->getOperand(1);
914     assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
915            "Invalid operand!");
916 
917     // Map the operand to its corresponding MCSymbol.
918     const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
919 
920     MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
921 
922     // Map the machine operand to its corresponding MCSymbol, then map the
923     // global address operand to be a reference to the TOC entry we will
924     // synthesize later.
925     MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol, VK);
926 
927     MCSymbolRefExpr::VariantKind VKExpr =
928         IsAIX ? MCSymbolRefExpr::VK_None : MCSymbolRefExpr::VK_PPC_TOC;
929     const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry, VKExpr, OutContext);
930     TmpInst.getOperand(1) = MCOperand::createExpr(
931         IsAIX ? getTOCEntryLoadingExprForXCOFF(MOSymbol, Exp, VK) : Exp);
932 
933     // Print MO for better readability
934     if (isVerbose() && IsAIX)
935       OutStreamer->GetCommentOS() << MO << '\n';
936     EmitToStreamer(*OutStreamer, TmpInst);
937     return;
938   }
939   case PPC::ADDIStocHA: {
940     assert((IsAIX && !IsPPC64 && TM.getCodeModel() == CodeModel::Large) &&
941            "This pseudo should only be selected for 32-bit large code model on"
942            " AIX.");
943 
944     // Transform %rd = ADDIStocHA %rA, @sym(%r2)
945     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
946 
947     // Change the opcode to ADDIS.
948     TmpInst.setOpcode(PPC::ADDIS);
949 
950     const MachineOperand &MO = MI->getOperand(2);
951     assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
952            "Invalid operand for ADDIStocHA.");
953 
954     // Map the machine operand to its corresponding MCSymbol.
955     MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
956 
957     MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
958 
959     // Always use TOC on AIX. Map the global address operand to be a reference
960     // to the TOC entry we will synthesize later. 'TOCEntry' is a label used to
961     // reference the storage allocated in the TOC which contains the address of
962     // 'MOSymbol'.
963     MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol, VK);
964     const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry,
965                                                 MCSymbolRefExpr::VK_PPC_U,
966                                                 OutContext);
967     TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
968     EmitToStreamer(*OutStreamer, TmpInst);
969     return;
970   }
971   case PPC::LWZtocL: {
972     assert(IsAIX && !IsPPC64 && TM.getCodeModel() == CodeModel::Large &&
973            "This pseudo should only be selected for 32-bit large code model on"
974            " AIX.");
975 
976     // Transform %rd = LWZtocL @sym, %rs.
977     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
978 
979     // Change the opcode to lwz.
980     TmpInst.setOpcode(PPC::LWZ);
981 
982     const MachineOperand &MO = MI->getOperand(1);
983     assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
984            "Invalid operand for LWZtocL.");
985 
986     // Map the machine operand to its corresponding MCSymbol.
987     MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
988 
989     MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
990 
991     // Always use TOC on AIX. Map the global address operand to be a reference
992     // to the TOC entry we will synthesize later. 'TOCEntry' is a label used to
993     // reference the storage allocated in the TOC which contains the address of
994     // 'MOSymbol'.
995     MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol, VK);
996     const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry,
997                                                 MCSymbolRefExpr::VK_PPC_L,
998                                                 OutContext);
999     TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1000     EmitToStreamer(*OutStreamer, TmpInst);
1001     return;
1002   }
1003   case PPC::ADDIStocHA8: {
1004     // Transform %xd = ADDIStocHA8 %x2, @sym
1005     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1006 
1007     // Change the opcode to ADDIS8. If the global address is the address of
1008     // an external symbol, is a jump table address, is a block address, or is a
1009     // constant pool index with large code model enabled, then generate a TOC
1010     // entry and reference that. Otherwise, reference the symbol directly.
1011     TmpInst.setOpcode(PPC::ADDIS8);
1012 
1013     const MachineOperand &MO = MI->getOperand(2);
1014     assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
1015            "Invalid operand for ADDIStocHA8!");
1016 
1017     const MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1018 
1019     MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1020 
1021     const bool GlobalToc =
1022         MO.isGlobal() && Subtarget->isGVIndirectSymbol(MO.getGlobal());
1023     if (GlobalToc || MO.isJTI() || MO.isBlockAddress() ||
1024         (MO.isCPI() && TM.getCodeModel() == CodeModel::Large))
1025       MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, VK);
1026 
1027     VK = IsAIX ? MCSymbolRefExpr::VK_PPC_U : MCSymbolRefExpr::VK_PPC_TOC_HA;
1028 
1029     const MCExpr *Exp =
1030         MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
1031 
1032     if (!MO.isJTI() && MO.getOffset())
1033       Exp = MCBinaryExpr::createAdd(Exp,
1034                                     MCConstantExpr::create(MO.getOffset(),
1035                                                            OutContext),
1036                                     OutContext);
1037 
1038     TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
1039     EmitToStreamer(*OutStreamer, TmpInst);
1040     return;
1041   }
1042   case PPC::LDtocL: {
1043     // Transform %xd = LDtocL @sym, %xs
1044     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1045 
1046     // Change the opcode to LD. If the global address is the address of
1047     // an external symbol, is a jump table address, is a block address, or is
1048     // a constant pool index with large code model enabled, then generate a
1049     // TOC entry and reference that. Otherwise, reference the symbol directly.
1050     TmpInst.setOpcode(PPC::LD);
1051 
1052     const MachineOperand &MO = MI->getOperand(1);
1053     assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
1054             MO.isBlockAddress()) &&
1055            "Invalid operand for LDtocL!");
1056 
1057     LLVM_DEBUG(assert(
1058         (!MO.isGlobal() || Subtarget->isGVIndirectSymbol(MO.getGlobal())) &&
1059         "LDtocL used on symbol that could be accessed directly is "
1060         "invalid. Must match ADDIStocHA8."));
1061 
1062     const MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1063 
1064     MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1065 
1066     if (!MO.isCPI() || TM.getCodeModel() == CodeModel::Large)
1067       MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, VK);
1068 
1069     VK = IsAIX ? MCSymbolRefExpr::VK_PPC_L : MCSymbolRefExpr::VK_PPC_TOC_LO;
1070     const MCExpr *Exp =
1071         MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
1072     TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1073     EmitToStreamer(*OutStreamer, TmpInst);
1074     return;
1075   }
1076   case PPC::ADDItocL: {
1077     // Transform %xd = ADDItocL %xs, @sym
1078     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1079 
1080     // Change the opcode to ADDI8. If the global address is external, then
1081     // generate a TOC entry and reference that. Otherwise, reference the
1082     // symbol directly.
1083     TmpInst.setOpcode(PPC::ADDI8);
1084 
1085     const MachineOperand &MO = MI->getOperand(2);
1086     assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL.");
1087 
1088     LLVM_DEBUG(assert(
1089         !(MO.isGlobal() && Subtarget->isGVIndirectSymbol(MO.getGlobal())) &&
1090         "Interposable definitions must use indirect access."));
1091 
1092     const MCExpr *Exp =
1093         MCSymbolRefExpr::create(getMCSymbolForTOCPseudoMO(MO, *this),
1094                                 MCSymbolRefExpr::VK_PPC_TOC_LO, OutContext);
1095     TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
1096     EmitToStreamer(*OutStreamer, TmpInst);
1097     return;
1098   }
1099   case PPC::ADDISgotTprelHA: {
1100     // Transform: %xd = ADDISgotTprelHA %x2, @sym
1101     // Into:      %xd = ADDIS8 %x2, sym@got@tlsgd@ha
1102     assert(IsPPC64 && "Not supported for 32-bit PowerPC");
1103     const MachineOperand &MO = MI->getOperand(2);
1104     const GlobalValue *GValue = MO.getGlobal();
1105     MCSymbol *MOSymbol = getSymbol(GValue);
1106     const MCExpr *SymGotTprel =
1107         MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA,
1108                                 OutContext);
1109     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
1110                                  .addReg(MI->getOperand(0).getReg())
1111                                  .addReg(MI->getOperand(1).getReg())
1112                                  .addExpr(SymGotTprel));
1113     return;
1114   }
1115   case PPC::LDgotTprelL:
1116   case PPC::LDgotTprelL32: {
1117     // Transform %xd = LDgotTprelL @sym, %xs
1118     LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1119 
1120     // Change the opcode to LD.
1121     TmpInst.setOpcode(IsPPC64 ? PPC::LD : PPC::LWZ);
1122     const MachineOperand &MO = MI->getOperand(1);
1123     const GlobalValue *GValue = MO.getGlobal();
1124     MCSymbol *MOSymbol = getSymbol(GValue);
1125     const MCExpr *Exp = MCSymbolRefExpr::create(
1126         MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO
1127                           : MCSymbolRefExpr::VK_PPC_GOT_TPREL,
1128         OutContext);
1129     TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1130     EmitToStreamer(*OutStreamer, TmpInst);
1131     return;
1132   }
1133 
1134   case PPC::PPC32PICGOT: {
1135     MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
1136     MCSymbol *GOTRef = OutContext.createTempSymbol();
1137     MCSymbol *NextInstr = OutContext.createTempSymbol();
1138 
1139     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL)
1140       // FIXME: We would like an efficient form for this, so we don't have to do
1141       // a lot of extra uniquing.
1142       .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext)));
1143     const MCExpr *OffsExpr =
1144       MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext),
1145                                 MCSymbolRefExpr::create(GOTRef, OutContext),
1146         OutContext);
1147     OutStreamer->emitLabel(GOTRef);
1148     OutStreamer->emitValue(OffsExpr, 4);
1149     OutStreamer->emitLabel(NextInstr);
1150     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR)
1151                                  .addReg(MI->getOperand(0).getReg()));
1152     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ)
1153                                  .addReg(MI->getOperand(1).getReg())
1154                                  .addImm(0)
1155                                  .addReg(MI->getOperand(0).getReg()));
1156     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4)
1157                                  .addReg(MI->getOperand(0).getReg())
1158                                  .addReg(MI->getOperand(1).getReg())
1159                                  .addReg(MI->getOperand(0).getReg()));
1160     return;
1161   }
1162   case PPC::PPC32GOT: {
1163     MCSymbol *GOTSymbol =
1164         OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
1165     const MCExpr *SymGotTlsL = MCSymbolRefExpr::create(
1166         GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, OutContext);
1167     const MCExpr *SymGotTlsHA = MCSymbolRefExpr::create(
1168         GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, OutContext);
1169     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI)
1170                                  .addReg(MI->getOperand(0).getReg())
1171                                  .addExpr(SymGotTlsL));
1172     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
1173                                  .addReg(MI->getOperand(0).getReg())
1174                                  .addReg(MI->getOperand(0).getReg())
1175                                  .addExpr(SymGotTlsHA));
1176     return;
1177   }
1178   case PPC::ADDIStlsgdHA: {
1179     // Transform: %xd = ADDIStlsgdHA %x2, @sym
1180     // Into:      %xd = ADDIS8 %x2, sym@got@tlsgd@ha
1181     assert(IsPPC64 && "Not supported for 32-bit PowerPC");
1182     const MachineOperand &MO = MI->getOperand(2);
1183     const GlobalValue *GValue = MO.getGlobal();
1184     MCSymbol *MOSymbol = getSymbol(GValue);
1185     const MCExpr *SymGotTlsGD =
1186       MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA,
1187                               OutContext);
1188     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
1189                                  .addReg(MI->getOperand(0).getReg())
1190                                  .addReg(MI->getOperand(1).getReg())
1191                                  .addExpr(SymGotTlsGD));
1192     return;
1193   }
1194   case PPC::ADDItlsgdL:
1195     // Transform: %xd = ADDItlsgdL %xs, @sym
1196     // Into:      %xd = ADDI8 %xs, sym@got@tlsgd@l
1197   case PPC::ADDItlsgdL32: {
1198     // Transform: %rd = ADDItlsgdL32 %rs, @sym
1199     // Into:      %rd = ADDI %rs, sym@got@tlsgd
1200     const MachineOperand &MO = MI->getOperand(2);
1201     const GlobalValue *GValue = MO.getGlobal();
1202     MCSymbol *MOSymbol = getSymbol(GValue);
1203     const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create(
1204         MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO
1205                           : MCSymbolRefExpr::VK_PPC_GOT_TLSGD,
1206         OutContext);
1207     EmitToStreamer(*OutStreamer,
1208                    MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
1209                    .addReg(MI->getOperand(0).getReg())
1210                    .addReg(MI->getOperand(1).getReg())
1211                    .addExpr(SymGotTlsGD));
1212     return;
1213   }
1214   case PPC::GETtlsADDR:
1215     // Transform: %x3 = GETtlsADDR %x3, @sym
1216     // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd)
1217   case PPC::GETtlsADDRPCREL:
1218   case PPC::GETtlsADDR32AIX:
1219   case PPC::GETtlsADDR64AIX:
1220     // Transform: %r3 = GETtlsADDRNNAIX %r3, %r4 (for NN == 32/64).
1221     // Into: BLA .__tls_get_addr()
1222     // Unlike on Linux, there is no symbol or relocation needed for this call.
1223   case PPC::GETtlsADDR32: {
1224     // Transform: %r3 = GETtlsADDR32 %r3, @sym
1225     // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT
1226     EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD);
1227     return;
1228   }
1229   case PPC::ADDIStlsldHA: {
1230     // Transform: %xd = ADDIStlsldHA %x2, @sym
1231     // Into:      %xd = ADDIS8 %x2, sym@got@tlsld@ha
1232     assert(IsPPC64 && "Not supported for 32-bit PowerPC");
1233     const MachineOperand &MO = MI->getOperand(2);
1234     const GlobalValue *GValue = MO.getGlobal();
1235     MCSymbol *MOSymbol = getSymbol(GValue);
1236     const MCExpr *SymGotTlsLD =
1237       MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA,
1238                               OutContext);
1239     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
1240                                  .addReg(MI->getOperand(0).getReg())
1241                                  .addReg(MI->getOperand(1).getReg())
1242                                  .addExpr(SymGotTlsLD));
1243     return;
1244   }
1245   case PPC::ADDItlsldL:
1246     // Transform: %xd = ADDItlsldL %xs, @sym
1247     // Into:      %xd = ADDI8 %xs, sym@got@tlsld@l
1248   case PPC::ADDItlsldL32: {
1249     // Transform: %rd = ADDItlsldL32 %rs, @sym
1250     // Into:      %rd = ADDI %rs, sym@got@tlsld
1251     const MachineOperand &MO = MI->getOperand(2);
1252     const GlobalValue *GValue = MO.getGlobal();
1253     MCSymbol *MOSymbol = getSymbol(GValue);
1254     const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create(
1255         MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO
1256                           : MCSymbolRefExpr::VK_PPC_GOT_TLSLD,
1257         OutContext);
1258     EmitToStreamer(*OutStreamer,
1259                    MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
1260                        .addReg(MI->getOperand(0).getReg())
1261                        .addReg(MI->getOperand(1).getReg())
1262                        .addExpr(SymGotTlsLD));
1263     return;
1264   }
1265   case PPC::GETtlsldADDR:
1266     // Transform: %x3 = GETtlsldADDR %x3, @sym
1267     // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld)
1268   case PPC::GETtlsldADDRPCREL:
1269   case PPC::GETtlsldADDR32: {
1270     // Transform: %r3 = GETtlsldADDR32 %r3, @sym
1271     // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT
1272     EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD);
1273     return;
1274   }
1275   case PPC::ADDISdtprelHA:
1276     // Transform: %xd = ADDISdtprelHA %xs, @sym
1277     // Into:      %xd = ADDIS8 %xs, sym@dtprel@ha
1278   case PPC::ADDISdtprelHA32: {
1279     // Transform: %rd = ADDISdtprelHA32 %rs, @sym
1280     // Into:      %rd = ADDIS %rs, sym@dtprel@ha
1281     const MachineOperand &MO = MI->getOperand(2);
1282     const GlobalValue *GValue = MO.getGlobal();
1283     MCSymbol *MOSymbol = getSymbol(GValue);
1284     const MCExpr *SymDtprel =
1285       MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_HA,
1286                               OutContext);
1287     EmitToStreamer(
1288         *OutStreamer,
1289         MCInstBuilder(IsPPC64 ? PPC::ADDIS8 : PPC::ADDIS)
1290             .addReg(MI->getOperand(0).getReg())
1291             .addReg(MI->getOperand(1).getReg())
1292             .addExpr(SymDtprel));
1293     return;
1294   }
1295   case PPC::PADDIdtprel: {
1296     // Transform: %rd = PADDIdtprel %rs, @sym
1297     // Into:      %rd = PADDI8 %rs, sym@dtprel
1298     const MachineOperand &MO = MI->getOperand(2);
1299     const GlobalValue *GValue = MO.getGlobal();
1300     MCSymbol *MOSymbol = getSymbol(GValue);
1301     const MCExpr *SymDtprel = MCSymbolRefExpr::create(
1302         MOSymbol, MCSymbolRefExpr::VK_DTPREL, OutContext);
1303     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::PADDI8)
1304                                      .addReg(MI->getOperand(0).getReg())
1305                                      .addReg(MI->getOperand(1).getReg())
1306                                      .addExpr(SymDtprel));
1307     return;
1308   }
1309 
1310   case PPC::ADDIdtprelL:
1311     // Transform: %xd = ADDIdtprelL %xs, @sym
1312     // Into:      %xd = ADDI8 %xs, sym@dtprel@l
1313   case PPC::ADDIdtprelL32: {
1314     // Transform: %rd = ADDIdtprelL32 %rs, @sym
1315     // Into:      %rd = ADDI %rs, sym@dtprel@l
1316     const MachineOperand &MO = MI->getOperand(2);
1317     const GlobalValue *GValue = MO.getGlobal();
1318     MCSymbol *MOSymbol = getSymbol(GValue);
1319     const MCExpr *SymDtprel =
1320       MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_LO,
1321                               OutContext);
1322     EmitToStreamer(*OutStreamer,
1323                    MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
1324                        .addReg(MI->getOperand(0).getReg())
1325                        .addReg(MI->getOperand(1).getReg())
1326                        .addExpr(SymDtprel));
1327     return;
1328   }
1329   case PPC::MFOCRF:
1330   case PPC::MFOCRF8:
1331     if (!Subtarget->hasMFOCRF()) {
1332       // Transform: %r3 = MFOCRF %cr7
1333       // Into:      %r3 = MFCR   ;; cr7
1334       unsigned NewOpcode =
1335         MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8;
1336       OutStreamer->AddComment(PPCInstPrinter::
1337                               getRegisterName(MI->getOperand(1).getReg()));
1338       EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
1339                                   .addReg(MI->getOperand(0).getReg()));
1340       return;
1341     }
1342     break;
1343   case PPC::MTOCRF:
1344   case PPC::MTOCRF8:
1345     if (!Subtarget->hasMFOCRF()) {
1346       // Transform: %cr7 = MTOCRF %r3
1347       // Into:      MTCRF mask, %r3 ;; cr7
1348       unsigned NewOpcode =
1349         MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8;
1350       unsigned Mask = 0x80 >> OutContext.getRegisterInfo()
1351                               ->getEncodingValue(MI->getOperand(0).getReg());
1352       OutStreamer->AddComment(PPCInstPrinter::
1353                               getRegisterName(MI->getOperand(0).getReg()));
1354       EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
1355                                      .addImm(Mask)
1356                                      .addReg(MI->getOperand(1).getReg()));
1357       return;
1358     }
1359     break;
1360   case PPC::LD:
1361   case PPC::STD:
1362   case PPC::LWA_32:
1363   case PPC::LWA: {
1364     // Verify alignment is legal, so we don't create relocations
1365     // that can't be supported.
1366     unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1;
1367     const MachineOperand &MO = MI->getOperand(OpNum);
1368     if (MO.isGlobal()) {
1369       const DataLayout &DL = MO.getGlobal()->getParent()->getDataLayout();
1370       if (MO.getGlobal()->getPointerAlignment(DL) < 4)
1371         llvm_unreachable("Global must be word-aligned for LD, STD, LWA!");
1372     }
1373     // Now process the instruction normally.
1374     break;
1375   }
1376   case PPC::PseudoEIEIO: {
1377     EmitToStreamer(
1378         *OutStreamer,
1379         MCInstBuilder(PPC::ORI).addReg(PPC::X2).addReg(PPC::X2).addImm(0));
1380     EmitToStreamer(
1381         *OutStreamer,
1382         MCInstBuilder(PPC::ORI).addReg(PPC::X2).addReg(PPC::X2).addImm(0));
1383     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::EnforceIEIO));
1384     return;
1385   }
1386   }
1387 
1388   LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1389   EmitToStreamer(*OutStreamer, TmpInst);
1390 }
1391 
1392 void PPCLinuxAsmPrinter::emitInstruction(const MachineInstr *MI) {
1393   if (!Subtarget->isPPC64())
1394     return PPCAsmPrinter::emitInstruction(MI);
1395 
1396   switch (MI->getOpcode()) {
1397   default:
1398     return PPCAsmPrinter::emitInstruction(MI);
1399   case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
1400     // .begin:
1401     //   b .end # lis 0, FuncId[16..32]
1402     //   nop    # li  0, FuncId[0..15]
1403     //   std 0, -8(1)
1404     //   mflr 0
1405     //   bl __xray_FunctionEntry
1406     //   mtlr 0
1407     // .end:
1408     //
1409     // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1410     // of instructions change.
1411     MCSymbol *BeginOfSled = OutContext.createTempSymbol();
1412     MCSymbol *EndOfSled = OutContext.createTempSymbol();
1413     OutStreamer->emitLabel(BeginOfSled);
1414     EmitToStreamer(*OutStreamer,
1415                    MCInstBuilder(PPC::B).addExpr(
1416                        MCSymbolRefExpr::create(EndOfSled, OutContext)));
1417     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
1418     EmitToStreamer(
1419         *OutStreamer,
1420         MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1));
1421     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0));
1422     EmitToStreamer(*OutStreamer,
1423                    MCInstBuilder(PPC::BL8_NOP)
1424                        .addExpr(MCSymbolRefExpr::create(
1425                            OutContext.getOrCreateSymbol("__xray_FunctionEntry"),
1426                            OutContext)));
1427     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0));
1428     OutStreamer->emitLabel(EndOfSled);
1429     recordSled(BeginOfSled, *MI, SledKind::FUNCTION_ENTER, 2);
1430     break;
1431   }
1432   case TargetOpcode::PATCHABLE_RET: {
1433     unsigned RetOpcode = MI->getOperand(0).getImm();
1434     MCInst RetInst;
1435     RetInst.setOpcode(RetOpcode);
1436     for (const auto &MO : llvm::drop_begin(MI->operands())) {
1437       MCOperand MCOp;
1438       if (LowerPPCMachineOperandToMCOperand(MO, MCOp, *this))
1439         RetInst.addOperand(MCOp);
1440     }
1441 
1442     bool IsConditional;
1443     if (RetOpcode == PPC::BCCLR) {
1444       IsConditional = true;
1445     } else if (RetOpcode == PPC::TCRETURNdi8 || RetOpcode == PPC::TCRETURNri8 ||
1446                RetOpcode == PPC::TCRETURNai8) {
1447       break;
1448     } else if (RetOpcode == PPC::BLR8 || RetOpcode == PPC::TAILB8) {
1449       IsConditional = false;
1450     } else {
1451       EmitToStreamer(*OutStreamer, RetInst);
1452       break;
1453     }
1454 
1455     MCSymbol *FallthroughLabel;
1456     if (IsConditional) {
1457       // Before:
1458       //   bgtlr cr0
1459       //
1460       // After:
1461       //   ble cr0, .end
1462       // .p2align 3
1463       // .begin:
1464       //   blr    # lis 0, FuncId[16..32]
1465       //   nop    # li  0, FuncId[0..15]
1466       //   std 0, -8(1)
1467       //   mflr 0
1468       //   bl __xray_FunctionExit
1469       //   mtlr 0
1470       //   blr
1471       // .end:
1472       //
1473       // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1474       // of instructions change.
1475       FallthroughLabel = OutContext.createTempSymbol();
1476       EmitToStreamer(
1477           *OutStreamer,
1478           MCInstBuilder(PPC::BCC)
1479               .addImm(PPC::InvertPredicate(
1480                   static_cast<PPC::Predicate>(MI->getOperand(1).getImm())))
1481               .addReg(MI->getOperand(2).getReg())
1482               .addExpr(MCSymbolRefExpr::create(FallthroughLabel, OutContext)));
1483       RetInst = MCInst();
1484       RetInst.setOpcode(PPC::BLR8);
1485     }
1486     // .p2align 3
1487     // .begin:
1488     //   b(lr)? # lis 0, FuncId[16..32]
1489     //   nop    # li  0, FuncId[0..15]
1490     //   std 0, -8(1)
1491     //   mflr 0
1492     //   bl __xray_FunctionExit
1493     //   mtlr 0
1494     //   b(lr)?
1495     //
1496     // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1497     // of instructions change.
1498     OutStreamer->emitCodeAlignment(8, &getSubtargetInfo());
1499     MCSymbol *BeginOfSled = OutContext.createTempSymbol();
1500     OutStreamer->emitLabel(BeginOfSled);
1501     EmitToStreamer(*OutStreamer, RetInst);
1502     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
1503     EmitToStreamer(
1504         *OutStreamer,
1505         MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1));
1506     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0));
1507     EmitToStreamer(*OutStreamer,
1508                    MCInstBuilder(PPC::BL8_NOP)
1509                        .addExpr(MCSymbolRefExpr::create(
1510                            OutContext.getOrCreateSymbol("__xray_FunctionExit"),
1511                            OutContext)));
1512     EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0));
1513     EmitToStreamer(*OutStreamer, RetInst);
1514     if (IsConditional)
1515       OutStreamer->emitLabel(FallthroughLabel);
1516     recordSled(BeginOfSled, *MI, SledKind::FUNCTION_EXIT, 2);
1517     break;
1518   }
1519   case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
1520     llvm_unreachable("PATCHABLE_FUNCTION_EXIT should never be emitted");
1521   case TargetOpcode::PATCHABLE_TAIL_CALL:
1522     // TODO: Define a trampoline `__xray_FunctionTailExit` and differentiate a
1523     // normal function exit from a tail exit.
1524     llvm_unreachable("Tail call is handled in the normal case. See comments "
1525                      "around this assert.");
1526   }
1527 }
1528 
1529 void PPCLinuxAsmPrinter::emitStartOfAsmFile(Module &M) {
1530   if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) {
1531     PPCTargetStreamer *TS =
1532       static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1533 
1534     if (TS)
1535       TS->emitAbiVersion(2);
1536   }
1537 
1538   if (static_cast<const PPCTargetMachine &>(TM).isPPC64() ||
1539       !isPositionIndependent())
1540     return AsmPrinter::emitStartOfAsmFile(M);
1541 
1542   if (M.getPICLevel() == PICLevel::SmallPIC)
1543     return AsmPrinter::emitStartOfAsmFile(M);
1544 
1545   OutStreamer->SwitchSection(OutContext.getELFSection(
1546       ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC));
1547 
1548   MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC"));
1549   MCSymbol *CurrentPos = OutContext.createTempSymbol();
1550 
1551   OutStreamer->emitLabel(CurrentPos);
1552 
1553   // The GOT pointer points to the middle of the GOT, in order to reference the
1554   // entire 64kB range.  0x8000 is the midpoint.
1555   const MCExpr *tocExpr =
1556     MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext),
1557                             MCConstantExpr::create(0x8000, OutContext),
1558                             OutContext);
1559 
1560   OutStreamer->emitAssignment(TOCSym, tocExpr);
1561 
1562   OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1563 }
1564 
1565 void PPCLinuxAsmPrinter::emitFunctionEntryLabel() {
1566   // linux/ppc32 - Normal entry label.
1567   if (!Subtarget->isPPC64() &&
1568       (!isPositionIndependent() ||
1569        MF->getFunction().getParent()->getPICLevel() == PICLevel::SmallPIC))
1570     return AsmPrinter::emitFunctionEntryLabel();
1571 
1572   if (!Subtarget->isPPC64()) {
1573     const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1574     if (PPCFI->usesPICBase() && !Subtarget->isSecurePlt()) {
1575       MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol(*MF);
1576       MCSymbol *PICBase = MF->getPICBaseSymbol();
1577       OutStreamer->emitLabel(RelocSymbol);
1578 
1579       const MCExpr *OffsExpr =
1580         MCBinaryExpr::createSub(
1581           MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
1582                                                                OutContext),
1583                                   MCSymbolRefExpr::create(PICBase, OutContext),
1584           OutContext);
1585       OutStreamer->emitValue(OffsExpr, 4);
1586       OutStreamer->emitLabel(CurrentFnSym);
1587       return;
1588     } else
1589       return AsmPrinter::emitFunctionEntryLabel();
1590   }
1591 
1592   // ELFv2 ABI - Normal entry label.
1593   if (Subtarget->isELFv2ABI()) {
1594     // In the Large code model, we allow arbitrary displacements between
1595     // the text section and its associated TOC section.  We place the
1596     // full 8-byte offset to the TOC in memory immediately preceding
1597     // the function global entry point.
1598     if (TM.getCodeModel() == CodeModel::Large
1599         && !MF->getRegInfo().use_empty(PPC::X2)) {
1600       const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1601 
1602       MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1603       MCSymbol *GlobalEPSymbol = PPCFI->getGlobalEPSymbol(*MF);
1604       const MCExpr *TOCDeltaExpr =
1605         MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
1606                                 MCSymbolRefExpr::create(GlobalEPSymbol,
1607                                                         OutContext),
1608                                 OutContext);
1609 
1610       OutStreamer->emitLabel(PPCFI->getTOCOffsetSymbol(*MF));
1611       OutStreamer->emitValue(TOCDeltaExpr, 8);
1612     }
1613     return AsmPrinter::emitFunctionEntryLabel();
1614   }
1615 
1616   // Emit an official procedure descriptor.
1617   MCSectionSubPair Current = OutStreamer->getCurrentSection();
1618   MCSectionELF *Section = OutStreamer->getContext().getELFSection(
1619       ".opd", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1620   OutStreamer->SwitchSection(Section);
1621   OutStreamer->emitLabel(CurrentFnSym);
1622   OutStreamer->emitValueToAlignment(8);
1623   MCSymbol *Symbol1 = CurrentFnSymForSize;
1624   // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
1625   // entry point.
1626   OutStreamer->emitValue(MCSymbolRefExpr::create(Symbol1, OutContext),
1627                          8 /*size*/);
1628   MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1629   // Generates a R_PPC64_TOC relocation for TOC base insertion.
1630   OutStreamer->emitValue(
1631     MCSymbolRefExpr::create(Symbol2, MCSymbolRefExpr::VK_PPC_TOCBASE, OutContext),
1632     8/*size*/);
1633   // Emit a null environment pointer.
1634   OutStreamer->emitIntValue(0, 8 /* size */);
1635   OutStreamer->SwitchSection(Current.first, Current.second);
1636 }
1637 
1638 void PPCLinuxAsmPrinter::emitEndOfAsmFile(Module &M) {
1639   const DataLayout &DL = getDataLayout();
1640 
1641   bool isPPC64 = DL.getPointerSizeInBits() == 64;
1642 
1643   PPCTargetStreamer *TS =
1644       static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1645 
1646   if (!TOC.empty()) {
1647     const char *Name = isPPC64 ? ".toc" : ".got2";
1648     MCSectionELF *Section = OutContext.getELFSection(
1649         Name, ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1650     OutStreamer->SwitchSection(Section);
1651     if (!isPPC64)
1652       OutStreamer->emitValueToAlignment(4);
1653 
1654     for (const auto &TOCMapPair : TOC) {
1655       const MCSymbol *const TOCEntryTarget = TOCMapPair.first.first;
1656       MCSymbol *const TOCEntryLabel = TOCMapPair.second;
1657 
1658       OutStreamer->emitLabel(TOCEntryLabel);
1659       if (isPPC64 && TS != nullptr)
1660         TS->emitTCEntry(*TOCEntryTarget, TOCMapPair.first.second);
1661       else
1662         OutStreamer->emitSymbolValue(TOCEntryTarget, 4);
1663     }
1664   }
1665 
1666   PPCAsmPrinter::emitEndOfAsmFile(M);
1667 }
1668 
1669 /// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2.
1670 void PPCLinuxAsmPrinter::emitFunctionBodyStart() {
1671   // In the ELFv2 ABI, in functions that use the TOC register, we need to
1672   // provide two entry points.  The ABI guarantees that when calling the
1673   // local entry point, r2 is set up by the caller to contain the TOC base
1674   // for this function, and when calling the global entry point, r12 is set
1675   // up by the caller to hold the address of the global entry point.  We
1676   // thus emit a prefix sequence along the following lines:
1677   //
1678   // func:
1679   // .Lfunc_gepNN:
1680   //         # global entry point
1681   //         addis r2,r12,(.TOC.-.Lfunc_gepNN)@ha
1682   //         addi  r2,r2,(.TOC.-.Lfunc_gepNN)@l
1683   // .Lfunc_lepNN:
1684   //         .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
1685   //         # local entry point, followed by function body
1686   //
1687   // For the Large code model, we create
1688   //
1689   // .Lfunc_tocNN:
1690   //         .quad .TOC.-.Lfunc_gepNN      # done by EmitFunctionEntryLabel
1691   // func:
1692   // .Lfunc_gepNN:
1693   //         # global entry point
1694   //         ld    r2,.Lfunc_tocNN-.Lfunc_gepNN(r12)
1695   //         add   r2,r2,r12
1696   // .Lfunc_lepNN:
1697   //         .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
1698   //         # local entry point, followed by function body
1699   //
1700   // This ensures we have r2 set up correctly while executing the function
1701   // body, no matter which entry point is called.
1702   const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1703   const bool UsesX2OrR2 = !MF->getRegInfo().use_empty(PPC::X2) ||
1704                           !MF->getRegInfo().use_empty(PPC::R2);
1705   const bool PCrelGEPRequired = Subtarget->isUsingPCRelativeCalls() &&
1706                                 UsesX2OrR2 && PPCFI->usesTOCBasePtr();
1707   const bool NonPCrelGEPRequired = !Subtarget->isUsingPCRelativeCalls() &&
1708                                    Subtarget->isELFv2ABI() && UsesX2OrR2;
1709 
1710   // Only do all that if the function uses R2 as the TOC pointer
1711   // in the first place. We don't need the global entry point if the
1712   // function uses R2 as an allocatable register.
1713   if (NonPCrelGEPRequired || PCrelGEPRequired) {
1714     // Note: The logic here must be synchronized with the code in the
1715     // branch-selection pass which sets the offset of the first block in the
1716     // function. This matters because it affects the alignment.
1717     MCSymbol *GlobalEntryLabel = PPCFI->getGlobalEPSymbol(*MF);
1718     OutStreamer->emitLabel(GlobalEntryLabel);
1719     const MCSymbolRefExpr *GlobalEntryLabelExp =
1720       MCSymbolRefExpr::create(GlobalEntryLabel, OutContext);
1721 
1722     if (TM.getCodeModel() != CodeModel::Large) {
1723       MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1724       const MCExpr *TOCDeltaExpr =
1725         MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
1726                                 GlobalEntryLabelExp, OutContext);
1727 
1728       const MCExpr *TOCDeltaHi = PPCMCExpr::createHa(TOCDeltaExpr, OutContext);
1729       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
1730                                    .addReg(PPC::X2)
1731                                    .addReg(PPC::X12)
1732                                    .addExpr(TOCDeltaHi));
1733 
1734       const MCExpr *TOCDeltaLo = PPCMCExpr::createLo(TOCDeltaExpr, OutContext);
1735       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI)
1736                                    .addReg(PPC::X2)
1737                                    .addReg(PPC::X2)
1738                                    .addExpr(TOCDeltaLo));
1739     } else {
1740       MCSymbol *TOCOffset = PPCFI->getTOCOffsetSymbol(*MF);
1741       const MCExpr *TOCOffsetDeltaExpr =
1742         MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCOffset, OutContext),
1743                                 GlobalEntryLabelExp, OutContext);
1744 
1745       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
1746                                    .addReg(PPC::X2)
1747                                    .addExpr(TOCOffsetDeltaExpr)
1748                                    .addReg(PPC::X12));
1749       EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD8)
1750                                    .addReg(PPC::X2)
1751                                    .addReg(PPC::X2)
1752                                    .addReg(PPC::X12));
1753     }
1754 
1755     MCSymbol *LocalEntryLabel = PPCFI->getLocalEPSymbol(*MF);
1756     OutStreamer->emitLabel(LocalEntryLabel);
1757     const MCSymbolRefExpr *LocalEntryLabelExp =
1758        MCSymbolRefExpr::create(LocalEntryLabel, OutContext);
1759     const MCExpr *LocalOffsetExp =
1760       MCBinaryExpr::createSub(LocalEntryLabelExp,
1761                               GlobalEntryLabelExp, OutContext);
1762 
1763     PPCTargetStreamer *TS =
1764       static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1765 
1766     if (TS)
1767       TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp);
1768   } else if (Subtarget->isUsingPCRelativeCalls()) {
1769     // When generating the entry point for a function we have a few scenarios
1770     // based on whether or not that function uses R2 and whether or not that
1771     // function makes calls (or is a leaf function).
1772     // 1) A leaf function that does not use R2 (or treats it as callee-saved
1773     //    and preserves it). In this case st_other=0 and both
1774     //    the local and global entry points for the function are the same.
1775     //    No special entry point code is required.
1776     // 2) A function uses the TOC pointer R2. This function may or may not have
1777     //    calls. In this case st_other=[2,6] and the global and local entry
1778     //    points are different. Code to correctly setup the TOC pointer in R2
1779     //    is put between the global and local entry points. This case is
1780     //    covered by the if statatement above.
1781     // 3) A function does not use the TOC pointer R2 but does have calls.
1782     //    In this case st_other=1 since we do not know whether or not any
1783     //    of the callees clobber R2. This case is dealt with in this else if
1784     //    block. Tail calls are considered calls and the st_other should also
1785     //    be set to 1 in that case as well.
1786     // 4) The function does not use the TOC pointer but R2 is used inside
1787     //    the function. In this case st_other=1 once again.
1788     // 5) This function uses inline asm. We mark R2 as reserved if the function
1789     //    has inline asm as we have to assume that it may be used.
1790     if (MF->getFrameInfo().hasCalls() || MF->getFrameInfo().hasTailCall() ||
1791         MF->hasInlineAsm() || (!PPCFI->usesTOCBasePtr() && UsesX2OrR2)) {
1792       PPCTargetStreamer *TS =
1793           static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1794       if (TS)
1795         TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym),
1796                            MCConstantExpr::create(1, OutContext));
1797     }
1798   }
1799 }
1800 
1801 /// EmitFunctionBodyEnd - Print the traceback table before the .size
1802 /// directive.
1803 ///
1804 void PPCLinuxAsmPrinter::emitFunctionBodyEnd() {
1805   // Only the 64-bit target requires a traceback table.  For now,
1806   // we only emit the word of zeroes that GDB requires to find
1807   // the end of the function, and zeroes for the eight-byte
1808   // mandatory fields.
1809   // FIXME: We should fill in the eight-byte mandatory fields as described in
1810   // the PPC64 ELF ABI (this is a low-priority item because GDB does not
1811   // currently make use of these fields).
1812   if (Subtarget->isPPC64()) {
1813     OutStreamer->emitIntValue(0, 4/*size*/);
1814     OutStreamer->emitIntValue(0, 8/*size*/);
1815   }
1816 }
1817 
1818 void PPCAIXAsmPrinter::emitLinkage(const GlobalValue *GV,
1819                                    MCSymbol *GVSym) const {
1820 
1821   assert(MAI->hasVisibilityOnlyWithLinkage() &&
1822          "AIX's linkage directives take a visibility setting.");
1823 
1824   MCSymbolAttr LinkageAttr = MCSA_Invalid;
1825   switch (GV->getLinkage()) {
1826   case GlobalValue::ExternalLinkage:
1827     LinkageAttr = GV->isDeclaration() ? MCSA_Extern : MCSA_Global;
1828     break;
1829   case GlobalValue::LinkOnceAnyLinkage:
1830   case GlobalValue::LinkOnceODRLinkage:
1831   case GlobalValue::WeakAnyLinkage:
1832   case GlobalValue::WeakODRLinkage:
1833   case GlobalValue::ExternalWeakLinkage:
1834     LinkageAttr = MCSA_Weak;
1835     break;
1836   case GlobalValue::AvailableExternallyLinkage:
1837     LinkageAttr = MCSA_Extern;
1838     break;
1839   case GlobalValue::PrivateLinkage:
1840     return;
1841   case GlobalValue::InternalLinkage:
1842     assert(GV->getVisibility() == GlobalValue::DefaultVisibility &&
1843            "InternalLinkage should not have other visibility setting.");
1844     LinkageAttr = MCSA_LGlobal;
1845     break;
1846   case GlobalValue::AppendingLinkage:
1847     llvm_unreachable("Should never emit this");
1848   case GlobalValue::CommonLinkage:
1849     llvm_unreachable("CommonLinkage of XCOFF should not come to this path");
1850   }
1851 
1852   assert(LinkageAttr != MCSA_Invalid && "LinkageAttr should not MCSA_Invalid.");
1853 
1854   MCSymbolAttr VisibilityAttr = MCSA_Invalid;
1855   if (!TM.getIgnoreXCOFFVisibility()) {
1856     switch (GV->getVisibility()) {
1857 
1858     // TODO: "exported" and "internal" Visibility needs to go here.
1859     case GlobalValue::DefaultVisibility:
1860       break;
1861     case GlobalValue::HiddenVisibility:
1862       VisibilityAttr = MAI->getHiddenVisibilityAttr();
1863       break;
1864     case GlobalValue::ProtectedVisibility:
1865       VisibilityAttr = MAI->getProtectedVisibilityAttr();
1866       break;
1867     }
1868   }
1869 
1870   OutStreamer->emitXCOFFSymbolLinkageWithVisibility(GVSym, LinkageAttr,
1871                                                     VisibilityAttr);
1872 }
1873 
1874 void PPCAIXAsmPrinter::SetupMachineFunction(MachineFunction &MF) {
1875   // Setup CurrentFnDescSym and its containing csect.
1876   MCSectionXCOFF *FnDescSec =
1877       cast<MCSectionXCOFF>(getObjFileLowering().getSectionForFunctionDescriptor(
1878           &MF.getFunction(), TM));
1879   FnDescSec->setAlignment(Align(Subtarget->isPPC64() ? 8 : 4));
1880 
1881   CurrentFnDescSym = FnDescSec->getQualNameSymbol();
1882 
1883   return AsmPrinter::SetupMachineFunction(MF);
1884 }
1885 
1886 uint16_t PPCAIXAsmPrinter::getNumberOfVRSaved() {
1887   // Calculate the number of VRs be saved.
1888   // Vector registers 20 through 31 are marked as reserved and cannot be used
1889   // in the default ABI.
1890   const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
1891   if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
1892       TM.getAIXExtendedAltivecABI()) {
1893     const MachineRegisterInfo &MRI = MF->getRegInfo();
1894     for (unsigned Reg = PPC::V20; Reg <= PPC::V31; ++Reg)
1895       if (MRI.isPhysRegModified(Reg))
1896         // Number of VRs saved.
1897         return PPC::V31 - Reg + 1;
1898   }
1899   return 0;
1900 }
1901 
1902 void PPCAIXAsmPrinter::emitFunctionBodyEnd() {
1903 
1904   if (!TM.getXCOFFTracebackTable())
1905     return;
1906 
1907   emitTracebackTable();
1908 
1909   // If ShouldEmitEHBlock returns true, then the eh info table
1910   // will be emitted via `AIXException::endFunction`. Otherwise, we
1911   // need to emit a dumy eh info table when VRs are saved. We could not
1912   // consolidate these two places into one because there is no easy way
1913   // to access register information in `AIXException` class.
1914   if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF) &&
1915       (getNumberOfVRSaved() > 0)) {
1916     // Emit dummy EH Info Table.
1917     OutStreamer->SwitchSection(getObjFileLowering().getCompactUnwindSection());
1918     MCSymbol *EHInfoLabel =
1919         TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(MF);
1920     OutStreamer->emitLabel(EHInfoLabel);
1921 
1922     // Version number.
1923     OutStreamer->emitInt32(0);
1924 
1925     const DataLayout &DL = MMI->getModule()->getDataLayout();
1926     const unsigned PointerSize = DL.getPointerSize();
1927     // Add necessary paddings in 64 bit mode.
1928     OutStreamer->emitValueToAlignment(PointerSize);
1929 
1930     OutStreamer->emitIntValue(0, PointerSize);
1931     OutStreamer->emitIntValue(0, PointerSize);
1932     OutStreamer->SwitchSection(MF->getSection());
1933   }
1934 }
1935 
1936 void PPCAIXAsmPrinter::emitTracebackTable() {
1937 
1938   // Create a symbol for the end of function.
1939   MCSymbol *FuncEnd = createTempSymbol(MF->getName());
1940   OutStreamer->emitLabel(FuncEnd);
1941 
1942   OutStreamer->AddComment("Traceback table begin");
1943   // Begin with a fullword of zero.
1944   OutStreamer->emitIntValueInHexWithPadding(0, 4 /*size*/);
1945 
1946   SmallString<128> CommentString;
1947   raw_svector_ostream CommentOS(CommentString);
1948 
1949   auto EmitComment = [&]() {
1950     OutStreamer->AddComment(CommentOS.str());
1951     CommentString.clear();
1952   };
1953 
1954   auto EmitCommentAndValue = [&](uint64_t Value, int Size) {
1955     EmitComment();
1956     OutStreamer->emitIntValueInHexWithPadding(Value, Size);
1957   };
1958 
1959   unsigned int Version = 0;
1960   CommentOS << "Version = " << Version;
1961   EmitCommentAndValue(Version, 1);
1962 
1963   // There is a lack of information in the IR to assist with determining the
1964   // source language. AIX exception handling mechanism would only search for
1965   // personality routine and LSDA area when such language supports exception
1966   // handling. So to be conservatively correct and allow runtime to do its job,
1967   // we need to set it to C++ for now.
1968   TracebackTable::LanguageID LanguageIdentifier =
1969       TracebackTable::CPlusPlus; // C++
1970 
1971   CommentOS << "Language = "
1972             << getNameForTracebackTableLanguageId(LanguageIdentifier);
1973   EmitCommentAndValue(LanguageIdentifier, 1);
1974 
1975   //  This is only populated for the third and fourth bytes.
1976   uint32_t FirstHalfOfMandatoryField = 0;
1977 
1978   // Emit the 3rd byte of the mandatory field.
1979 
1980   // We always set traceback offset bit to true.
1981   FirstHalfOfMandatoryField |= TracebackTable::HasTraceBackTableOffsetMask;
1982 
1983   const PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
1984   const MachineRegisterInfo &MRI = MF->getRegInfo();
1985 
1986   // Check the function uses floating-point processor instructions or not
1987   for (unsigned Reg = PPC::F0; Reg <= PPC::F31; ++Reg) {
1988     if (MRI.isPhysRegUsed(Reg, /* SkipRegMaskTest */ true)) {
1989       FirstHalfOfMandatoryField |= TracebackTable::IsFloatingPointPresentMask;
1990       break;
1991     }
1992   }
1993 
1994 #define GENBOOLCOMMENT(Prefix, V, Field)                                       \
1995   CommentOS << (Prefix) << ((V) & (TracebackTable::Field##Mask) ? "+" : "-")   \
1996             << #Field
1997 
1998 #define GENVALUECOMMENT(PrefixAndName, V, Field)                               \
1999   CommentOS << (PrefixAndName) << " = "                                        \
2000             << static_cast<unsigned>(((V) & (TracebackTable::Field##Mask)) >>  \
2001                                      (TracebackTable::Field##Shift))
2002 
2003   GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsGlobaLinkage);
2004   GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsOutOfLineEpilogOrPrologue);
2005   EmitComment();
2006 
2007   GENBOOLCOMMENT("", FirstHalfOfMandatoryField, HasTraceBackTableOffset);
2008   GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsInternalProcedure);
2009   EmitComment();
2010 
2011   GENBOOLCOMMENT("", FirstHalfOfMandatoryField, HasControlledStorage);
2012   GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsTOCless);
2013   EmitComment();
2014 
2015   GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsFloatingPointPresent);
2016   EmitComment();
2017   GENBOOLCOMMENT("", FirstHalfOfMandatoryField,
2018                  IsFloatingPointOperationLogOrAbortEnabled);
2019   EmitComment();
2020 
2021   OutStreamer->emitIntValueInHexWithPadding(
2022       (FirstHalfOfMandatoryField & 0x0000ff00) >> 8, 1);
2023 
2024   // Set the 4th byte of the mandatory field.
2025   FirstHalfOfMandatoryField |= TracebackTable::IsFunctionNamePresentMask;
2026 
2027   const PPCRegisterInfo *RegInfo =
2028       static_cast<const PPCRegisterInfo *>(Subtarget->getRegisterInfo());
2029   Register FrameReg = RegInfo->getFrameRegister(*MF);
2030   if (FrameReg == (Subtarget->isPPC64() ? PPC::X31 : PPC::R31))
2031     FirstHalfOfMandatoryField |= TracebackTable::IsAllocaUsedMask;
2032 
2033   const SmallVectorImpl<Register> &MustSaveCRs = FI->getMustSaveCRs();
2034   if (!MustSaveCRs.empty())
2035     FirstHalfOfMandatoryField |= TracebackTable::IsCRSavedMask;
2036 
2037   if (FI->mustSaveLR())
2038     FirstHalfOfMandatoryField |= TracebackTable::IsLRSavedMask;
2039 
2040   GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsInterruptHandler);
2041   GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsFunctionNamePresent);
2042   GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsAllocaUsed);
2043   EmitComment();
2044   GENVALUECOMMENT("OnConditionDirective", FirstHalfOfMandatoryField,
2045                   OnConditionDirective);
2046   GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsCRSaved);
2047   GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsLRSaved);
2048   EmitComment();
2049   OutStreamer->emitIntValueInHexWithPadding((FirstHalfOfMandatoryField & 0xff),
2050                                             1);
2051 
2052   // Set the 5th byte of mandatory field.
2053   uint32_t SecondHalfOfMandatoryField = 0;
2054 
2055   // Always store back chain.
2056   SecondHalfOfMandatoryField |= TracebackTable::IsBackChainStoredMask;
2057 
2058   uint32_t FPRSaved = 0;
2059   for (unsigned Reg = PPC::F14; Reg <= PPC::F31; ++Reg) {
2060     if (MRI.isPhysRegModified(Reg)) {
2061       FPRSaved = PPC::F31 - Reg + 1;
2062       break;
2063     }
2064   }
2065   SecondHalfOfMandatoryField |= (FPRSaved << TracebackTable::FPRSavedShift) &
2066                                 TracebackTable::FPRSavedMask;
2067   GENBOOLCOMMENT("", SecondHalfOfMandatoryField, IsBackChainStored);
2068   GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, IsFixup);
2069   GENVALUECOMMENT(", NumOfFPRsSaved", SecondHalfOfMandatoryField, FPRSaved);
2070   EmitComment();
2071   OutStreamer->emitIntValueInHexWithPadding(
2072       (SecondHalfOfMandatoryField & 0xff000000) >> 24, 1);
2073 
2074   // Set the 6th byte of mandatory field.
2075 
2076   // Check whether has Vector Instruction,We only treat instructions uses vector
2077   // register as vector instructions.
2078   bool HasVectorInst = false;
2079   for (unsigned Reg = PPC::V0; Reg <= PPC::V31; ++Reg)
2080     if (MRI.isPhysRegUsed(Reg, /* SkipRegMaskTest */ true)) {
2081       // Has VMX instruction.
2082       HasVectorInst = true;
2083       break;
2084     }
2085 
2086   if (FI->hasVectorParms() || HasVectorInst)
2087     SecondHalfOfMandatoryField |= TracebackTable::HasVectorInfoMask;
2088 
2089   uint16_t NumOfVRSaved = getNumberOfVRSaved();
2090   bool ShouldEmitEHBlock =
2091       TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF) || NumOfVRSaved > 0;
2092 
2093   if (ShouldEmitEHBlock)
2094     SecondHalfOfMandatoryField |= TracebackTable::HasExtensionTableMask;
2095 
2096   uint32_t GPRSaved = 0;
2097 
2098   // X13 is reserved under 64-bit environment.
2099   unsigned GPRBegin = Subtarget->isPPC64() ? PPC::X14 : PPC::R13;
2100   unsigned GPREnd = Subtarget->isPPC64() ? PPC::X31 : PPC::R31;
2101 
2102   for (unsigned Reg = GPRBegin; Reg <= GPREnd; ++Reg) {
2103     if (MRI.isPhysRegModified(Reg)) {
2104       GPRSaved = GPREnd - Reg + 1;
2105       break;
2106     }
2107   }
2108 
2109   SecondHalfOfMandatoryField |= (GPRSaved << TracebackTable::GPRSavedShift) &
2110                                 TracebackTable::GPRSavedMask;
2111 
2112   GENBOOLCOMMENT("", SecondHalfOfMandatoryField, HasExtensionTable);
2113   GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, HasVectorInfo);
2114   GENVALUECOMMENT(", NumOfGPRsSaved", SecondHalfOfMandatoryField, GPRSaved);
2115   EmitComment();
2116   OutStreamer->emitIntValueInHexWithPadding(
2117       (SecondHalfOfMandatoryField & 0x00ff0000) >> 16, 1);
2118 
2119   // Set the 7th byte of mandatory field.
2120   uint32_t NumberOfFixedParms = FI->getFixedParmsNum();
2121   SecondHalfOfMandatoryField |=
2122       (NumberOfFixedParms << TracebackTable::NumberOfFixedParmsShift) &
2123       TracebackTable::NumberOfFixedParmsMask;
2124   GENVALUECOMMENT("NumberOfFixedParms", SecondHalfOfMandatoryField,
2125                   NumberOfFixedParms);
2126   EmitComment();
2127   OutStreamer->emitIntValueInHexWithPadding(
2128       (SecondHalfOfMandatoryField & 0x0000ff00) >> 8, 1);
2129 
2130   // Set the 8th byte of mandatory field.
2131 
2132   // Always set parameter on stack.
2133   SecondHalfOfMandatoryField |= TracebackTable::HasParmsOnStackMask;
2134 
2135   uint32_t NumberOfFPParms = FI->getFloatingPointParmsNum();
2136   SecondHalfOfMandatoryField |=
2137       (NumberOfFPParms << TracebackTable::NumberOfFloatingPointParmsShift) &
2138       TracebackTable::NumberOfFloatingPointParmsMask;
2139 
2140   GENVALUECOMMENT("NumberOfFPParms", SecondHalfOfMandatoryField,
2141                   NumberOfFloatingPointParms);
2142   GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, HasParmsOnStack);
2143   EmitComment();
2144   OutStreamer->emitIntValueInHexWithPadding(SecondHalfOfMandatoryField & 0xff,
2145                                             1);
2146 
2147   // Generate the optional fields of traceback table.
2148 
2149   // Parameter type.
2150   if (NumberOfFixedParms || NumberOfFPParms) {
2151     uint32_t ParmsTypeValue = FI->getParmsType();
2152 
2153     Expected<SmallString<32>> ParmsType =
2154         FI->hasVectorParms()
2155             ? XCOFF::parseParmsTypeWithVecInfo(
2156                   ParmsTypeValue, NumberOfFixedParms, NumberOfFPParms,
2157                   FI->getVectorParmsNum())
2158             : XCOFF::parseParmsType(ParmsTypeValue, NumberOfFixedParms,
2159                                     NumberOfFPParms);
2160 
2161     assert(ParmsType && toString(ParmsType.takeError()).c_str());
2162     if (ParmsType) {
2163       CommentOS << "Parameter type = " << ParmsType.get();
2164       EmitComment();
2165     }
2166     OutStreamer->emitIntValueInHexWithPadding(ParmsTypeValue,
2167                                               sizeof(ParmsTypeValue));
2168   }
2169   // Traceback table offset.
2170   OutStreamer->AddComment("Function size");
2171   if (FirstHalfOfMandatoryField & TracebackTable::HasTraceBackTableOffsetMask) {
2172     MCSymbol *FuncSectSym = getObjFileLowering().getFunctionEntryPointSymbol(
2173         &(MF->getFunction()), TM);
2174     OutStreamer->emitAbsoluteSymbolDiff(FuncEnd, FuncSectSym, 4);
2175   }
2176 
2177   // Since we unset the Int_Handler.
2178   if (FirstHalfOfMandatoryField & TracebackTable::IsInterruptHandlerMask)
2179     report_fatal_error("Hand_Mask not implement yet");
2180 
2181   if (FirstHalfOfMandatoryField & TracebackTable::HasControlledStorageMask)
2182     report_fatal_error("Ctl_Info not implement yet");
2183 
2184   if (FirstHalfOfMandatoryField & TracebackTable::IsFunctionNamePresentMask) {
2185     StringRef Name = MF->getName().substr(0, INT16_MAX);
2186     int16_t NameLength = Name.size();
2187     CommentOS << "Function name len = "
2188               << static_cast<unsigned int>(NameLength);
2189     EmitCommentAndValue(NameLength, 2);
2190     OutStreamer->AddComment("Function Name");
2191     OutStreamer->emitBytes(Name);
2192   }
2193 
2194   if (FirstHalfOfMandatoryField & TracebackTable::IsAllocaUsedMask) {
2195     uint8_t AllocReg = XCOFF::AllocRegNo;
2196     OutStreamer->AddComment("AllocaUsed");
2197     OutStreamer->emitIntValueInHex(AllocReg, sizeof(AllocReg));
2198   }
2199 
2200   if (SecondHalfOfMandatoryField & TracebackTable::HasVectorInfoMask) {
2201     uint16_t VRData = 0;
2202     if (NumOfVRSaved) {
2203       // Number of VRs saved.
2204       VRData |= (NumOfVRSaved << TracebackTable::NumberOfVRSavedShift) &
2205                 TracebackTable::NumberOfVRSavedMask;
2206       // This bit is supposed to set only when the special register
2207       // VRSAVE is saved on stack.
2208       // However, IBM XL compiler sets the bit when any vector registers
2209       // are saved on the stack. We will follow XL's behavior on AIX
2210       // so that we don't get surprise behavior change for C code.
2211       VRData |= TracebackTable::IsVRSavedOnStackMask;
2212     }
2213 
2214     // Set has_varargs.
2215     if (FI->getVarArgsFrameIndex())
2216       VRData |= TracebackTable::HasVarArgsMask;
2217 
2218     // Vector parameters number.
2219     unsigned VectorParmsNum = FI->getVectorParmsNum();
2220     VRData |= (VectorParmsNum << TracebackTable::NumberOfVectorParmsShift) &
2221               TracebackTable::NumberOfVectorParmsMask;
2222 
2223     if (HasVectorInst)
2224       VRData |= TracebackTable::HasVMXInstructionMask;
2225 
2226     GENVALUECOMMENT("NumOfVRsSaved", VRData, NumberOfVRSaved);
2227     GENBOOLCOMMENT(", ", VRData, IsVRSavedOnStack);
2228     GENBOOLCOMMENT(", ", VRData, HasVarArgs);
2229     EmitComment();
2230     OutStreamer->emitIntValueInHexWithPadding((VRData & 0xff00) >> 8, 1);
2231 
2232     GENVALUECOMMENT("NumOfVectorParams", VRData, NumberOfVectorParms);
2233     GENBOOLCOMMENT(", ", VRData, HasVMXInstruction);
2234     EmitComment();
2235     OutStreamer->emitIntValueInHexWithPadding(VRData & 0x00ff, 1);
2236 
2237     uint32_t VecParmTypeValue = FI->getVecExtParmsType();
2238 
2239     Expected<SmallString<32>> VecParmsType =
2240         XCOFF::parseVectorParmsType(VecParmTypeValue, VectorParmsNum);
2241     assert(VecParmsType && toString(VecParmsType.takeError()).c_str());
2242     if (VecParmsType) {
2243       CommentOS << "Vector Parameter type = " << VecParmsType.get();
2244       EmitComment();
2245     }
2246     OutStreamer->emitIntValueInHexWithPadding(VecParmTypeValue,
2247                                               sizeof(VecParmTypeValue));
2248     // Padding 2 bytes.
2249     CommentOS << "Padding";
2250     EmitCommentAndValue(0, 2);
2251   }
2252 
2253   uint8_t ExtensionTableFlag = 0;
2254   if (SecondHalfOfMandatoryField & TracebackTable::HasExtensionTableMask) {
2255     if (ShouldEmitEHBlock)
2256       ExtensionTableFlag |= ExtendedTBTableFlag::TB_EH_INFO;
2257     if (EnableSSPCanaryBitInTB &&
2258         TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB(MF))
2259       ExtensionTableFlag |= ExtendedTBTableFlag::TB_SSP_CANARY;
2260 
2261     CommentOS << "ExtensionTableFlag = "
2262               << getExtendedTBTableFlagString(ExtensionTableFlag);
2263     EmitCommentAndValue(ExtensionTableFlag, sizeof(ExtensionTableFlag));
2264   }
2265 
2266   if (ExtensionTableFlag & ExtendedTBTableFlag::TB_EH_INFO) {
2267     auto &Ctx = OutStreamer->getContext();
2268     MCSymbol *EHInfoSym =
2269         TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(MF);
2270     MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(EHInfoSym);
2271     const MCSymbol *TOCBaseSym =
2272         cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
2273             ->getQualNameSymbol();
2274     const MCExpr *Exp =
2275         MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCEntry, Ctx),
2276                                 MCSymbolRefExpr::create(TOCBaseSym, Ctx), Ctx);
2277 
2278     const DataLayout &DL = getDataLayout();
2279     OutStreamer->emitValueToAlignment(4);
2280     OutStreamer->AddComment("EHInfo Table");
2281     OutStreamer->emitValue(Exp, DL.getPointerSize());
2282   }
2283 #undef GENBOOLCOMMENT
2284 #undef GENVALUECOMMENT
2285 }
2286 
2287 static bool isSpecialLLVMGlobalArrayToSkip(const GlobalVariable *GV) {
2288   return GV->hasAppendingLinkage() &&
2289          StringSwitch<bool>(GV->getName())
2290              // TODO: Linker could still eliminate the GV if we just skip
2291              // handling llvm.used array. Skipping them for now until we or the
2292              // AIX OS team come up with a good solution.
2293              .Case("llvm.used", true)
2294              // It's correct to just skip llvm.compiler.used array here.
2295              .Case("llvm.compiler.used", true)
2296              .Default(false);
2297 }
2298 
2299 static bool isSpecialLLVMGlobalArrayForStaticInit(const GlobalVariable *GV) {
2300   return StringSwitch<bool>(GV->getName())
2301       .Cases("llvm.global_ctors", "llvm.global_dtors", true)
2302       .Default(false);
2303 }
2304 
2305 void PPCAIXAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
2306   // Special LLVM global arrays have been handled at the initialization.
2307   if (isSpecialLLVMGlobalArrayToSkip(GV) || isSpecialLLVMGlobalArrayForStaticInit(GV))
2308     return;
2309 
2310   // If the Global Variable has the toc-data attribute, it needs to be emitted
2311   // when we emit the .toc section.
2312   if (GV->hasAttribute("toc-data")) {
2313     TOCDataGlobalVars.push_back(GV);
2314     return;
2315   }
2316 
2317   emitGlobalVariableHelper(GV);
2318 }
2319 
2320 void PPCAIXAsmPrinter::emitGlobalVariableHelper(const GlobalVariable *GV) {
2321   assert(!GV->getName().startswith("llvm.") &&
2322          "Unhandled intrinsic global variable.");
2323 
2324   if (GV->hasComdat())
2325     report_fatal_error("COMDAT not yet supported by AIX.");
2326 
2327   MCSymbolXCOFF *GVSym = cast<MCSymbolXCOFF>(getSymbol(GV));
2328 
2329   if (GV->isDeclarationForLinker()) {
2330     emitLinkage(GV, GVSym);
2331     return;
2332   }
2333 
2334   SectionKind GVKind = getObjFileLowering().getKindForGlobal(GV, TM);
2335   if (!GVKind.isGlobalWriteableData() && !GVKind.isReadOnly() &&
2336       !GVKind.isThreadLocal()) // Checks for both ThreadData and ThreadBSS.
2337     report_fatal_error("Encountered a global variable kind that is "
2338                        "not supported yet.");
2339 
2340   // Print GV in verbose mode
2341   if (isVerbose()) {
2342     if (GV->hasInitializer()) {
2343       GV->printAsOperand(OutStreamer->GetCommentOS(),
2344                          /*PrintType=*/false, GV->getParent());
2345       OutStreamer->GetCommentOS() << '\n';
2346     }
2347   }
2348 
2349   MCSectionXCOFF *Csect = cast<MCSectionXCOFF>(
2350       getObjFileLowering().SectionForGlobal(GV, GVKind, TM));
2351 
2352   // Switch to the containing csect.
2353   OutStreamer->SwitchSection(Csect);
2354 
2355   const DataLayout &DL = GV->getParent()->getDataLayout();
2356 
2357   // Handle common and zero-initialized local symbols.
2358   if (GV->hasCommonLinkage() || GVKind.isBSSLocal() ||
2359       GVKind.isThreadBSSLocal()) {
2360     Align Alignment = GV->getAlign().getValueOr(DL.getPreferredAlign(GV));
2361     uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
2362     GVSym->setStorageClass(
2363         TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GV));
2364 
2365     if (GVKind.isBSSLocal() || GVKind.isThreadBSSLocal())
2366       OutStreamer->emitXCOFFLocalCommonSymbol(
2367           OutContext.getOrCreateSymbol(GVSym->getSymbolTableName()), Size,
2368           GVSym, Alignment.value());
2369     else
2370       OutStreamer->emitCommonSymbol(GVSym, Size, Alignment.value());
2371     return;
2372   }
2373 
2374   MCSymbol *EmittedInitSym = GVSym;
2375   emitLinkage(GV, EmittedInitSym);
2376   emitAlignment(getGVAlignment(GV, DL), GV);
2377 
2378   // When -fdata-sections is enabled, every GlobalVariable will
2379   // be put into its own csect; therefore, label is not necessary here.
2380   if (!TM.getDataSections() || GV->hasSection()) {
2381     OutStreamer->emitLabel(EmittedInitSym);
2382   }
2383 
2384   // Emit aliasing label for global variable.
2385   llvm::for_each(GOAliasMap[GV], [this](const GlobalAlias *Alias) {
2386     OutStreamer->emitLabel(getSymbol(Alias));
2387   });
2388 
2389   emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
2390 }
2391 
2392 void PPCAIXAsmPrinter::emitFunctionDescriptor() {
2393   const DataLayout &DL = getDataLayout();
2394   const unsigned PointerSize = DL.getPointerSizeInBits() == 64 ? 8 : 4;
2395 
2396   MCSectionSubPair Current = OutStreamer->getCurrentSection();
2397   // Emit function descriptor.
2398   OutStreamer->SwitchSection(
2399       cast<MCSymbolXCOFF>(CurrentFnDescSym)->getRepresentedCsect());
2400 
2401   // Emit aliasing label for function descriptor csect.
2402   llvm::for_each(GOAliasMap[&MF->getFunction()],
2403                  [this](const GlobalAlias *Alias) {
2404                    OutStreamer->emitLabel(getSymbol(Alias));
2405                  });
2406 
2407   // Emit function entry point address.
2408   OutStreamer->emitValue(MCSymbolRefExpr::create(CurrentFnSym, OutContext),
2409                          PointerSize);
2410   // Emit TOC base address.
2411   const MCSymbol *TOCBaseSym =
2412       cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
2413           ->getQualNameSymbol();
2414   OutStreamer->emitValue(MCSymbolRefExpr::create(TOCBaseSym, OutContext),
2415                          PointerSize);
2416   // Emit a null environment pointer.
2417   OutStreamer->emitIntValue(0, PointerSize);
2418 
2419   OutStreamer->SwitchSection(Current.first, Current.second);
2420 }
2421 
2422 void PPCAIXAsmPrinter::emitFunctionEntryLabel() {
2423   // It's not necessary to emit the label when we have individual
2424   // function in its own csect.
2425   if (!TM.getFunctionSections())
2426     PPCAsmPrinter::emitFunctionEntryLabel();
2427 
2428   // Emit aliasing label for function entry point label.
2429   llvm::for_each(
2430       GOAliasMap[&MF->getFunction()], [this](const GlobalAlias *Alias) {
2431         OutStreamer->emitLabel(
2432             getObjFileLowering().getFunctionEntryPointSymbol(Alias, TM));
2433       });
2434 }
2435 
2436 void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) {
2437   // If there are no functions and there are no toc-data definitions in this
2438   // module, we will never need to reference the TOC base.
2439   if (M.empty() && TOCDataGlobalVars.empty())
2440     return;
2441 
2442   // Switch to section to emit TOC base.
2443   OutStreamer->SwitchSection(getObjFileLowering().getTOCBaseSection());
2444 
2445   PPCTargetStreamer *TS =
2446       static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
2447 
2448   for (auto &I : TOC) {
2449     MCSectionXCOFF *TCEntry;
2450     // Setup the csect for the current TC entry. If the variant kind is
2451     // VK_PPC_AIX_TLSGDM the entry represents the region handle, we create a
2452     // new symbol to prefix the name with a dot.
2453     if (I.first.second == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM) {
2454       SmallString<128> Name;
2455       StringRef Prefix = ".";
2456       Name += Prefix;
2457       Name += I.first.first->getName();
2458       MCSymbol *S = OutContext.getOrCreateSymbol(Name);
2459       TCEntry = cast<MCSectionXCOFF>(
2460           getObjFileLowering().getSectionForTOCEntry(S, TM));
2461     } else {
2462       TCEntry = cast<MCSectionXCOFF>(
2463           getObjFileLowering().getSectionForTOCEntry(I.first.first, TM));
2464     }
2465     OutStreamer->SwitchSection(TCEntry);
2466 
2467     OutStreamer->emitLabel(I.second);
2468     if (TS != nullptr)
2469       TS->emitTCEntry(*I.first.first, I.first.second);
2470   }
2471 
2472   for (const auto *GV : TOCDataGlobalVars)
2473     emitGlobalVariableHelper(GV);
2474 }
2475 
2476 bool PPCAIXAsmPrinter::doInitialization(Module &M) {
2477   const bool Result = PPCAsmPrinter::doInitialization(M);
2478 
2479   auto setCsectAlignment = [this](const GlobalObject *GO) {
2480     // Declarations have 0 alignment which is set by default.
2481     if (GO->isDeclarationForLinker())
2482       return;
2483 
2484     SectionKind GOKind = getObjFileLowering().getKindForGlobal(GO, TM);
2485     MCSectionXCOFF *Csect = cast<MCSectionXCOFF>(
2486         getObjFileLowering().SectionForGlobal(GO, GOKind, TM));
2487 
2488     Align GOAlign = getGVAlignment(GO, GO->getParent()->getDataLayout());
2489     if (GOAlign > Csect->getAlignment())
2490       Csect->setAlignment(GOAlign);
2491   };
2492 
2493   // We need to know, up front, the alignment of csects for the assembly path,
2494   // because once a .csect directive gets emitted, we could not change the
2495   // alignment value on it.
2496   for (const auto &G : M.globals()) {
2497     if (isSpecialLLVMGlobalArrayToSkip(&G))
2498       continue;
2499 
2500     if (isSpecialLLVMGlobalArrayForStaticInit(&G)) {
2501       // Generate a format indicator and a unique module id to be a part of
2502       // the sinit and sterm function names.
2503       if (FormatIndicatorAndUniqueModId.empty()) {
2504         std::string UniqueModuleId = getUniqueModuleId(&M);
2505         if (UniqueModuleId != "")
2506           // TODO: Use source file full path to generate the unique module id
2507           // and add a format indicator as a part of function name in case we
2508           // will support more than one format.
2509           FormatIndicatorAndUniqueModId = "clang_" + UniqueModuleId.substr(1);
2510         else
2511           // Use the Pid and current time as the unique module id when we cannot
2512           // generate one based on a module's strong external symbols.
2513           // FIXME: Adjust the comment accordingly after we use source file full
2514           // path instead.
2515           FormatIndicatorAndUniqueModId =
2516               "clangPidTime_" + llvm::itostr(sys::Process::getProcessId()) +
2517               "_" + llvm::itostr(time(nullptr));
2518       }
2519 
2520       emitSpecialLLVMGlobal(&G);
2521       continue;
2522     }
2523 
2524     setCsectAlignment(&G);
2525   }
2526 
2527   for (const auto &F : M)
2528     setCsectAlignment(&F);
2529 
2530   // Construct an aliasing list for each GlobalObject.
2531   for (const auto &Alias : M.aliases()) {
2532     const GlobalObject *Base = Alias.getAliaseeObject();
2533     if (!Base)
2534       report_fatal_error(
2535           "alias without a base object is not yet supported on AIX");
2536     GOAliasMap[Base].push_back(&Alias);
2537   }
2538 
2539   return Result;
2540 }
2541 
2542 void PPCAIXAsmPrinter::emitInstruction(const MachineInstr *MI) {
2543   switch (MI->getOpcode()) {
2544   default:
2545     break;
2546   case PPC::GETtlsADDR64AIX:
2547   case PPC::GETtlsADDR32AIX: {
2548     // The reference to .__tls_get_addr is unknown to the assembler
2549     // so we need to emit an external symbol reference.
2550     MCSymbol *TlsGetAddr = createMCSymbolForTlsGetAddr(OutContext);
2551     ExtSymSDNodeSymbols.insert(TlsGetAddr);
2552     break;
2553   }
2554   case PPC::BL8:
2555   case PPC::BL:
2556   case PPC::BL8_NOP:
2557   case PPC::BL_NOP: {
2558     const MachineOperand &MO = MI->getOperand(0);
2559     if (MO.isSymbol()) {
2560       MCSymbolXCOFF *S =
2561           cast<MCSymbolXCOFF>(OutContext.getOrCreateSymbol(MO.getSymbolName()));
2562       ExtSymSDNodeSymbols.insert(S);
2563     }
2564   } break;
2565   case PPC::BL_TLS:
2566   case PPC::BL8_TLS:
2567   case PPC::BL8_TLS_:
2568   case PPC::BL8_NOP_TLS:
2569     report_fatal_error("TLS call not yet implemented");
2570   case PPC::TAILB:
2571   case PPC::TAILB8:
2572   case PPC::TAILBA:
2573   case PPC::TAILBA8:
2574   case PPC::TAILBCTR:
2575   case PPC::TAILBCTR8:
2576     if (MI->getOperand(0).isSymbol())
2577       report_fatal_error("Tail call for extern symbol not yet supported.");
2578     break;
2579   case PPC::DST:
2580   case PPC::DST64:
2581   case PPC::DSTT:
2582   case PPC::DSTT64:
2583   case PPC::DSTST:
2584   case PPC::DSTST64:
2585   case PPC::DSTSTT:
2586   case PPC::DSTSTT64:
2587     EmitToStreamer(
2588         *OutStreamer,
2589         MCInstBuilder(PPC::ORI).addReg(PPC::R0).addReg(PPC::R0).addImm(0));
2590     return;
2591   }
2592   return PPCAsmPrinter::emitInstruction(MI);
2593 }
2594 
2595 bool PPCAIXAsmPrinter::doFinalization(Module &M) {
2596   // Do streamer related finalization for DWARF.
2597   if (!MAI->usesDwarfFileAndLocDirectives() && MMI->hasDebugInfo())
2598     OutStreamer->doFinalizationAtSectionEnd(
2599         OutStreamer->getContext().getObjectFileInfo()->getTextSection());
2600 
2601   for (MCSymbol *Sym : ExtSymSDNodeSymbols)
2602     OutStreamer->emitSymbolAttribute(Sym, MCSA_Extern);
2603   return PPCAsmPrinter::doFinalization(M);
2604 }
2605 
2606 static unsigned mapToSinitPriority(int P) {
2607   if (P < 0 || P > 65535)
2608     report_fatal_error("invalid init priority");
2609 
2610   if (P <= 20)
2611     return P;
2612 
2613   if (P < 81)
2614     return 20 + (P - 20) * 16;
2615 
2616   if (P <= 1124)
2617     return 1004 + (P - 81);
2618 
2619   if (P < 64512)
2620     return 2047 + (P - 1124) * 33878;
2621 
2622   return 2147482625u + (P - 64512);
2623 }
2624 
2625 static std::string convertToSinitPriority(int Priority) {
2626   // This helper function converts clang init priority to values used in sinit
2627   // and sterm functions.
2628   //
2629   // The conversion strategies are:
2630   // We map the reserved clang/gnu priority range [0, 100] into the sinit/sterm
2631   // reserved priority range [0, 1023] by
2632   // - directly mapping the first 21 and the last 20 elements of the ranges
2633   // - linear interpolating the intermediate values with a step size of 16.
2634   //
2635   // We map the non reserved clang/gnu priority range of [101, 65535] into the
2636   // sinit/sterm priority range [1024, 2147483648] by:
2637   // - directly mapping the first and the last 1024 elements of the ranges
2638   // - linear interpolating the intermediate values with a step size of 33878.
2639   unsigned int P = mapToSinitPriority(Priority);
2640 
2641   std::string PrioritySuffix;
2642   llvm::raw_string_ostream os(PrioritySuffix);
2643   os << llvm::format_hex_no_prefix(P, 8);
2644   os.flush();
2645   return PrioritySuffix;
2646 }
2647 
2648 void PPCAIXAsmPrinter::emitXXStructorList(const DataLayout &DL,
2649                                           const Constant *List, bool IsCtor) {
2650   SmallVector<Structor, 8> Structors;
2651   preprocessXXStructorList(DL, List, Structors);
2652   if (Structors.empty())
2653     return;
2654 
2655   unsigned Index = 0;
2656   for (Structor &S : Structors) {
2657     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(S.Func))
2658       S.Func = CE->getOperand(0);
2659 
2660     llvm::GlobalAlias::create(
2661         GlobalValue::ExternalLinkage,
2662         (IsCtor ? llvm::Twine("__sinit") : llvm::Twine("__sterm")) +
2663             llvm::Twine(convertToSinitPriority(S.Priority)) +
2664             llvm::Twine("_", FormatIndicatorAndUniqueModId) +
2665             llvm::Twine("_", llvm::utostr(Index++)),
2666         cast<Function>(S.Func));
2667   }
2668 }
2669 
2670 void PPCAIXAsmPrinter::emitTTypeReference(const GlobalValue *GV,
2671                                           unsigned Encoding) {
2672   if (GV) {
2673     MCSymbol *TypeInfoSym = TM.getSymbol(GV);
2674     MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(TypeInfoSym);
2675     const MCSymbol *TOCBaseSym =
2676         cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
2677             ->getQualNameSymbol();
2678     auto &Ctx = OutStreamer->getContext();
2679     const MCExpr *Exp =
2680         MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCEntry, Ctx),
2681                                 MCSymbolRefExpr::create(TOCBaseSym, Ctx), Ctx);
2682     OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding));
2683   } else
2684     OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding));
2685 }
2686 
2687 // Return a pass that prints the PPC assembly code for a MachineFunction to the
2688 // given output stream.
2689 static AsmPrinter *
2690 createPPCAsmPrinterPass(TargetMachine &tm,
2691                         std::unique_ptr<MCStreamer> &&Streamer) {
2692   if (tm.getTargetTriple().isOSAIX())
2693     return new PPCAIXAsmPrinter(tm, std::move(Streamer));
2694 
2695   return new PPCLinuxAsmPrinter(tm, std::move(Streamer));
2696 }
2697 
2698 // Force static initialization.
2699 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCAsmPrinter() {
2700   TargetRegistry::RegisterAsmPrinter(getThePPC32Target(),
2701                                      createPPCAsmPrinterPass);
2702   TargetRegistry::RegisterAsmPrinter(getThePPC32LETarget(),
2703                                      createPPCAsmPrinterPass);
2704   TargetRegistry::RegisterAsmPrinter(getThePPC64Target(),
2705                                      createPPCAsmPrinterPass);
2706   TargetRegistry::RegisterAsmPrinter(getThePPC64LETarget(),
2707                                      createPPCAsmPrinterPass);
2708 }
2709