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