LLVM 24.0.0git
ARMAsmPrinter.cpp
Go to the documentation of this file.
1//===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
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 GAS-format ARM assembly language.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARMAsmPrinter.h"
15#include "ARM.h"
18#include "ARMTargetMachine.h"
19#include "ARMTargetObjectFile.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/Mangler.h"
30#include "llvm/IR/Module.h"
31#include "llvm/IR/Type.h"
32#include "llvm/MC/MCAsmInfo.h"
33#include "llvm/MC/MCAssembler.h"
34#include "llvm/MC/MCContext.h"
36#include "llvm/MC/MCInst.h"
39#include "llvm/MC/MCStreamer.h"
40#include "llvm/MC/MCSymbol.h"
44#include "llvm/Support/Debug.h"
48using namespace llvm;
49
50#define DEBUG_TYPE "asm-printer"
51
53 std::unique_ptr<MCStreamer> Streamer)
54 : AsmPrinter(TM, std::move(Streamer), ID), AFI(nullptr), MCP(nullptr),
55 InConstantPool(false), OptimizationGoals(-1) {}
56
58 return static_cast<const ARMBaseTargetMachine &>(TM);
59}
60
62 // Make sure to terminate any constant pools that were at the end
63 // of the function.
64 if (!InConstantPool)
65 return;
66 InConstantPool = false;
67 OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
68}
69
71 auto &TS =
72 static_cast<ARMTargetStreamer &>(*OutStreamer->getTargetStreamer());
73 if (AFI->isThumbFunction()) {
74 TS.emitCode16();
75 TS.emitThumbFunc(CurrentFnSym);
76 } else {
77 TS.emitCode32();
78 }
79
80 // Emit symbol for CMSE non-secure entry point
81 if (AFI->isCmseNSEntryFunction()) {
82 MCSymbol *S =
83 OutContext.getOrCreateSymbol("__acle_se_" + CurrentFnSym->getName());
84 emitLinkage(&MF->getFunction(), S);
85 OutStreamer->emitSymbolAttribute(S, MCSA_ELF_TypeFunction);
86 OutStreamer->emitLabel(S);
87 }
89}
90
92 uint64_t Size = getDataLayout().getTypeAllocSize(CV->getType());
93 assert(Size && "C++ constructor pointer had zero size!");
94
96 assert(GV && "C++ constructor pointer was not a GlobalValue!");
97
99 GetARMGVSymbol(GV, ARMII::MO_NO_FLAG),
100 (TM.getTargetTriple().isOSBinFormatELF() ? ARM::S_TARGET1 : ARM::S_None),
101 OutContext);
102
103 OutStreamer->emitValue(E, Size);
104}
105
106// An alias to a cmse entry function should also emit a `__acle_se_` symbol.
107void ARMAsmPrinter::emitCMSEVeneerAlias(const GlobalAlias &GA) {
109 if (!BaseFn || !BaseFn->hasFnAttribute("cmse_nonsecure_entry"))
110 return;
111
112 MCSymbol *AliasSym = getSymbol(&GA);
113 MCSymbol *FnSym = getSymbol(BaseFn);
114
115 MCSymbol *SEAliasSym =
116 OutContext.getOrCreateSymbol(Twine("__acle_se_") + AliasSym->getName());
117 MCSymbol *SEBaseSym =
118 OutContext.getOrCreateSymbol(Twine("__acle_se_") + FnSym->getName());
119
120 // Mirror alias linkage/visibility onto the veneer-alias symbol.
121 emitLinkage(&GA, SEAliasSym);
122 OutStreamer->emitSymbolAttribute(SEAliasSym, MCSA_ELF_TypeFunction);
123 emitVisibility(SEAliasSym, GA.getVisibility());
124
125 // emit "__acle_se_<alias> = __acle_se_<aliasee>"
126 const MCExpr *SEExpr = MCSymbolRefExpr::create(SEBaseSym, OutContext);
127 OutStreamer->emitAssignment(SEAliasSym, SEExpr);
128}
129
132 emitCMSEVeneerAlias(GA);
133}
134
136 if (PromotedGlobals.count(GV))
137 // The global was promoted into a constant pool. It should not be emitted.
138 return;
140}
141
142/// runOnMachineFunction - This uses the emitInstruction()
143/// method to print assembly for each instruction.
144///
146 AFI = MF.getInfo<ARMFunctionInfo>();
147 MCP = MF.getConstantPool();
148
150 const Function &F = MF.getFunction();
151 const TargetMachine& TM = MF.getTarget();
152
153 // Collect all globals that had their storage promoted to a constant pool.
154 // Functions are emitted before variables, so this accumulates promoted
155 // globals from all functions in PromotedGlobals.
156 PromotedGlobals.insert_range(AFI->getGlobalsPromotedToConstantPool());
157
158 // Calculate this function's optimization goal.
159 unsigned OptimizationGoal;
160 if (F.hasOptNone())
161 // For best debugging illusion, speed and small size sacrificed
162 OptimizationGoal = 6;
163 else if (F.hasMinSize())
164 // Aggressively for small size, speed and debug illusion sacrificed
165 OptimizationGoal = 4;
166 else if (F.hasOptSize())
167 // For small size, but speed and debugging illusion preserved
168 OptimizationGoal = 3;
169 else if (TM.getOptLevel() == CodeGenOptLevel::Aggressive)
170 // Aggressively for speed, small size and debug illusion sacrificed
171 OptimizationGoal = 2;
172 else if (TM.getOptLevel() > CodeGenOptLevel::None)
173 // For speed, but small size and good debug illusion preserved
174 OptimizationGoal = 1;
175 else // TM.getOptLevel() == CodeGenOptLevel::None
176 // For good debugging, but speed and small size preserved
177 OptimizationGoal = 5;
178
179 // Combine a new optimization goal with existing ones.
180 if (OptimizationGoals == -1) // uninitialized goals
181 OptimizationGoals = OptimizationGoal;
182 else if (OptimizationGoals != (int)OptimizationGoal) // conflicting goals
183 OptimizationGoals = 0;
184
185 if (TM.getTargetTriple().isOSBinFormatCOFF()) {
186 bool Local = F.hasLocalLinkage();
190
191 OutStreamer->beginCOFFSymbolDef(CurrentFnSym);
192 OutStreamer->emitCOFFSymbolStorageClass(Scl);
193 OutStreamer->emitCOFFSymbolType(Type);
194 OutStreamer->endCOFFSymbolDef();
195 }
196
197 // Emit the rest of the function body.
199
200 // Emit the XRay table for this function.
202
203 // If we need V4T thumb mode Register Indirect Jump pads, emit them.
204 // These are created per function, rather than per TU, since it's
205 // relatively easy to exceed the thumb branch range within a TU.
206 if (! ThumbIndirectPads.empty()) {
207 auto &TS =
208 static_cast<ARMTargetStreamer &>(*OutStreamer->getTargetStreamer());
209 TS.emitCode16();
211 for (std::pair<unsigned, MCSymbol *> &TIP : ThumbIndirectPads) {
212 OutStreamer->emitLabel(TIP.second);
214 .addReg(TIP.first)
215 // Add predicate operands.
217 .addReg(0));
218 }
219 ThumbIndirectPads.clear();
220 }
221
222 // We didn't modify anything.
223 return false;
224}
225
227 raw_ostream &O) {
228 assert(MO.isGlobal() && "caller should check MO.isGlobal");
229 unsigned TF = MO.getTargetFlags();
230 if (TF & ARMII::MO_LO16)
231 O << ":lower16:";
232 else if (TF & ARMII::MO_HI16)
233 O << ":upper16:";
234 else if (TF & ARMII::MO_LO_0_7)
235 O << ":lower0_7:";
236 else if (TF & ARMII::MO_LO_8_15)
237 O << ":lower8_15:";
238 else if (TF & ARMII::MO_HI_0_7)
239 O << ":upper0_7:";
240 else if (TF & ARMII::MO_HI_8_15)
241 O << ":upper8_15:";
242
243 GetARMGVSymbol(MO.getGlobal(), TF)->print(O, MAI);
244 printOffset(MO.getOffset(), O);
245}
246
248 raw_ostream &O) {
249 const MachineOperand &MO = MI->getOperand(OpNum);
250
251 switch (MO.getType()) {
252 default: llvm_unreachable("<unknown operand type>");
254 Register Reg = MO.getReg();
255 assert(Reg.isPhysical());
256 assert(!MO.getSubReg() && "Subregs should be eliminated!");
257 if(ARM::GPRPairRegClass.contains(Reg)) {
258 const MachineFunction &MF = *MI->getParent()->getParent();
259 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
260 Reg = TRI->getSubReg(Reg, ARM::gsub_0);
261 }
263 break;
264 }
266 O << '#';
267 unsigned TF = MO.getTargetFlags();
268 if (TF == ARMII::MO_LO16)
269 O << ":lower16:";
270 else if (TF == ARMII::MO_HI16)
271 O << ":upper16:";
272 else if (TF == ARMII::MO_LO_0_7)
273 O << ":lower0_7:";
274 else if (TF == ARMII::MO_LO_8_15)
275 O << ":lower8_15:";
276 else if (TF == ARMII::MO_HI_0_7)
277 O << ":upper0_7:";
278 else if (TF == ARMII::MO_HI_8_15)
279 O << ":upper8_15:";
280 O << MO.getImm();
281 break;
282 }
284 MO.getMBB()->getSymbol()->print(O, MAI);
285 return;
287 PrintSymbolOperand(MO, O);
288 break;
289 }
291 assert(!MF->getSubtarget<ARMSubtarget>().genExecuteOnly() &&
292 "execute-only should not generate constant pools");
293 GetCPISymbol(MO.getIndex())->print(O, MAI);
294 break;
295 }
296}
297
299 // The AsmPrinter::GetCPISymbol superclass method tries to use CPID as
300 // indexes in MachineConstantPool, which isn't in sync with indexes used here.
301 const DataLayout &DL = getDataLayout();
302 return OutContext.getOrCreateSymbol(Twine(DL.getInternalSymbolPrefix()) +
303 "CPI" + Twine(getFunctionNumber()) + "_" +
304 Twine(CPID));
305}
306
307//===--------------------------------------------------------------------===//
308
309MCSymbol *ARMAsmPrinter::
310GetARMJTIPICJumpTableLabel(unsigned uid) const {
311 const DataLayout &DL = getDataLayout();
312 SmallString<60> Name;
313 raw_svector_ostream(Name) << DL.getInternalSymbolPrefix() << "JTI"
314 << getFunctionNumber() << '_' << uid;
315 return OutContext.getOrCreateSymbol(Name);
316}
317
319 const char *ExtraCode, raw_ostream &O) {
320 // Does this asm operand have a single letter operand modifier?
321 if (ExtraCode && ExtraCode[0]) {
322 if (ExtraCode[1] != 0) return true; // Unknown modifier.
323
324 switch (ExtraCode[0]) {
325 default:
326 // See if this is a generic print operand
327 return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);
328 case 'P': // Print a VFP double precision register.
329 case 'q': // Print a NEON quad precision register.
330 printOperand(MI, OpNum, O);
331 return false;
332 case 'y': // Print a VFP single precision register as indexed double.
333 if (MI->getOperand(OpNum).isReg()) {
334 MCRegister Reg = MI->getOperand(OpNum).getReg().asMCReg();
335 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
336 // Find the 'd' register that has this 's' register as a sub-register,
337 // and determine the lane number.
338 for (MCPhysReg SR : TRI->superregs(Reg)) {
339 if (!ARM::DPRRegClass.contains(SR))
340 continue;
341 bool Lane0 = TRI->getSubReg(SR, ARM::ssub_0) == Reg;
342 O << ARMInstPrinter::getRegisterName(SR) << (Lane0 ? "[0]" : "[1]");
343 return false;
344 }
345 }
346 return true;
347 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
348 if (!MI->getOperand(OpNum).isImm())
349 return true;
350 O << ~(MI->getOperand(OpNum).getImm());
351 return false;
352 case 'L': // The low 16 bits of an immediate constant.
353 if (!MI->getOperand(OpNum).isImm())
354 return true;
355 O << (MI->getOperand(OpNum).getImm() & 0xffff);
356 return false;
357 case 'M': { // A register range suitable for LDM/STM.
358 if (!MI->getOperand(OpNum).isReg())
359 return true;
360 const MachineOperand &MO = MI->getOperand(OpNum);
361 Register RegBegin = MO.getReg();
362 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
363 // already got the operands in registers that are operands to the
364 // inline asm statement.
365 O << "{";
366 if (ARM::GPRPairRegClass.contains(RegBegin)) {
367 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
368 Register Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
369 O << ARMInstPrinter::getRegisterName(Reg0) << ", ";
370 RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
371 }
372 O << ARMInstPrinter::getRegisterName(RegBegin);
373
374 // FIXME: The register allocator not only may not have given us the
375 // registers in sequence, but may not be in ascending registers. This
376 // will require changes in the register allocator that'll need to be
377 // propagated down here if the operands change.
378 unsigned RegOps = OpNum + 1;
379 while (MI->getOperand(RegOps).isReg()) {
380 O << ", "
381 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
382 RegOps++;
383 }
384
385 O << "}";
386
387 return false;
388 }
389 case 'R': // The most significant register of a pair.
390 case 'Q': { // The least significant register of a pair.
391 if (OpNum == 0)
392 return true;
393 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
394 if (!FlagsOP.isImm())
395 return true;
396 InlineAsm::Flag F(FlagsOP.getImm());
397
398 // This operand may not be the one that actually provides the register. If
399 // it's tied to a previous one then we should refer instead to that one
400 // for registers and their classes.
401 unsigned TiedIdx;
402 if (F.isUseOperandTiedToDef(TiedIdx)) {
403 for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
404 unsigned OpFlags = MI->getOperand(OpNum).getImm();
405 const InlineAsm::Flag F(OpFlags);
406 OpNum += F.getNumOperandRegisters() + 1;
407 }
408 F = InlineAsm::Flag(MI->getOperand(OpNum).getImm());
409
410 // Later code expects OpNum to be pointing at the register rather than
411 // the flags.
412 OpNum += 1;
413 }
414
415 const unsigned NumVals = F.getNumOperandRegisters();
416 unsigned RC;
417 bool FirstHalf;
418 const ARMBaseTargetMachine &ATM =
419 static_cast<const ARMBaseTargetMachine &>(TM);
420
421 // 'Q' should correspond to the low order register and 'R' to the high
422 // order register. Whether this corresponds to the upper or lower half
423 // depends on the endianness mode.
424 if (ExtraCode[0] == 'Q')
425 FirstHalf = ATM.isLittleEndian();
426 else
427 // ExtraCode[0] == 'R'.
428 FirstHalf = !ATM.isLittleEndian();
429 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
430 if (F.hasRegClassConstraint(RC) &&
431 ARM::GPRPairRegClass.hasSubClassEq(TRI->getRegClass(RC))) {
432 if (NumVals != 1)
433 return true;
434 const MachineOperand &MO = MI->getOperand(OpNum);
435 if (!MO.isReg())
436 return true;
437 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
438 Register Reg =
439 TRI->getSubReg(MO.getReg(), FirstHalf ? ARM::gsub_0 : ARM::gsub_1);
441 return false;
442 }
443 if (NumVals != 2)
444 return true;
445 unsigned RegOp = FirstHalf ? OpNum : OpNum + 1;
446 if (RegOp >= MI->getNumOperands())
447 return true;
448 const MachineOperand &MO = MI->getOperand(RegOp);
449 if (!MO.isReg())
450 return true;
451 Register Reg = MO.getReg();
453 return false;
454 }
455
456 case 'e': // The low doubleword register of a NEON quad register.
457 case 'f': { // The high doubleword register of a NEON quad register.
458 if (!MI->getOperand(OpNum).isReg())
459 return true;
460 Register Reg = MI->getOperand(OpNum).getReg();
461 if (!ARM::QPRRegClass.contains(Reg))
462 return true;
463 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
464 Register SubReg =
465 TRI->getSubReg(Reg, ExtraCode[0] == 'e' ? ARM::dsub_0 : ARM::dsub_1);
467 return false;
468 }
469
470 // This modifier is not yet supported.
471 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
472 return true;
473 case 'H': { // The highest-numbered register of a pair.
474 const MachineOperand &MO = MI->getOperand(OpNum);
475 if (!MO.isReg())
476 return true;
477 const MachineFunction &MF = *MI->getParent()->getParent();
478 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
479 Register Reg = MO.getReg();
480 if(!ARM::GPRPairRegClass.contains(Reg))
481 return false;
482 Reg = TRI->getSubReg(Reg, ARM::gsub_1);
484 return false;
485 }
486 }
487 }
488
489 printOperand(MI, OpNum, O);
490 return false;
491}
492
494 unsigned OpNum, const char *ExtraCode,
495 raw_ostream &O) {
496 // Does this asm operand have a single letter operand modifier?
497 if (ExtraCode && ExtraCode[0]) {
498 if (ExtraCode[1] != 0) return true; // Unknown modifier.
499
500 switch (ExtraCode[0]) {
501 case 'A': // A memory operand for a VLD1/VST1 instruction.
502 default: return true; // Unknown modifier.
503 case 'm': // The base register of a memory operand.
504 if (!MI->getOperand(OpNum).isReg())
505 return true;
506 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
507 return false;
508 }
509 }
510
511 const MachineOperand &MO = MI->getOperand(OpNum);
512 assert(MO.isReg() && "unexpected inline asm memory operand");
513 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
514 return false;
515}
516
517static bool isThumb(const MCSubtargetInfo& STI) {
518 return STI.hasFeature(ARM::ModeThumb);
519}
520
522 const MCSubtargetInfo *EndInfo,
523 const MachineInstr *MI) {
524 // If either end mode is unknown (EndInfo == NULL) or different than
525 // the start mode, then restore the start mode.
526 const bool WasThumb = isThumb(StartInfo);
527 if (!EndInfo || WasThumb != isThumb(*EndInfo)) {
528 auto &TS =
529 static_cast<ARMTargetStreamer &>(*OutStreamer->getTargetStreamer());
530 if (WasThumb)
531 TS.emitCode16();
532 else
533 TS.emitCode32();
534 }
535}
536
538 const Triple &TT = TM.getTargetTriple();
539 auto &TS =
540 static_cast<ARMTargetStreamer &>(*OutStreamer->getTargetStreamer());
541 // Use unified assembler syntax.
543
544 // Emit ARM Build Attributes
545 if (TT.isOSBinFormatELF())
546 emitAttributes();
547
548 // Use the triple's architecture and subarchitecture to determine
549 // if we're thumb for the purposes of the top level code16 state.
550 if (!M.getModuleInlineAsm().empty() && TT.isThumb())
551 TS.emitCode16();
552}
553
554static void
557 // L_foo$stub:
558 OutStreamer.emitLabel(StubLabel);
559 // .indirect_symbol _foo
561
562 if (MCSym.getInt())
563 // External to current translation unit.
564 OutStreamer.emitIntValue(0, 4/*size*/);
565 else
566 // Internal to current translation unit.
567 //
568 // When we place the LSDA into the TEXT section, the type info
569 // pointers need to be indirect and pc-rel. We accomplish this by
570 // using NLPs; however, sometimes the types are local to the file.
571 // We need to fill in the value for the NLP in those cases.
572 OutStreamer.emitValue(
573 MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()),
574 4 /*size*/);
575}
576
577
579 const Triple &TT = TM.getTargetTriple();
580 if (TT.isOSBinFormatMachO()) {
581 // All darwin targets use mach-o.
582 const TargetLoweringObjectFileMachO &TLOFMacho =
584 MachineModuleInfoMachO &MMIMacho =
585 MMI->getObjFileInfo<MachineModuleInfoMachO>();
586
587 // Output non-lazy-pointers for external and common global variables.
589
590 if (!Stubs.empty()) {
591 // Switch with ".non_lazy_symbol_pointer" directive.
592 OutStreamer->switchSection(TLOFMacho.getNonLazySymbolPointerSection());
594
595 for (auto &Stub : Stubs)
596 emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
597
598 Stubs.clear();
599 OutStreamer->addBlankLine();
600 }
601
602 Stubs = MMIMacho.GetThreadLocalGVStubList();
603 if (!Stubs.empty()) {
604 // Switch with ".non_lazy_symbol_pointer" directive.
605 OutStreamer->switchSection(TLOFMacho.getThreadLocalPointerSection());
607
608 for (auto &Stub : Stubs)
609 emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
610
611 Stubs.clear();
612 OutStreamer->addBlankLine();
613 }
614
615 // Funny Darwin hack: This flag tells the linker that no global symbols
616 // contain code that falls through to other global symbols (e.g. the obvious
617 // implementation of multiple entry points). If this doesn't occur, the
618 // linker can safely perform dead code stripping. Since LLVM never
619 // generates code that does this, it is always safe to set.
620 OutStreamer->emitSubsectionsViaSymbols();
621 }
622
623 // The last attribute to be emitted is ABI_optimization_goals
624 MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
625 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
626
627 if (OptimizationGoals > 0 &&
628 (TT.isTargetAEABI() || TT.isTargetGNUAEABI() || TT.isTargetMuslAEABI()))
630 OptimizationGoals = -1;
631
633}
634
635//===----------------------------------------------------------------------===//
636// Helper routines for emitStartOfAsmFile() and emitEndOfAsmFile()
637// FIXME:
638// The following seem like one-off assembler flags, but they actually need
639// to appear in the .ARM.attributes section in ELF.
640// Instead of subclassing the MCELFStreamer, we do the work here.
641
642// Returns true if all function definitions have the same function attribute
643// value. It also returns true when the module has no functions.
646 return !any_of(M, [&](const Function &F) {
647 if (F.isDeclaration())
648 return false;
649 return F.getFnAttribute(Attr).getValueAsString() != Value;
650 });
651}
652// Returns true if all functions definitions have the same denormal mode.
653// It also returns true when the module has no functions.
656 return !any_of(M, [&](const Function &F) {
657 if (F.isDeclaration())
658 return false;
659 return F.getDenormalFPEnv() != Value;
660 });
661}
662
663// Returns true if all functions have different denormal modes.
665 auto F = M.functions().begin();
666 auto E = M.functions().end();
667 if (F == E)
668 return false;
669 DenormalFPEnv Value = F->getDenormalFPEnv();
670 ++F;
671 return std::any_of(F, E, [&](const Function &F) {
672 return !F.isDeclaration() && F.getDenormalFPEnv() != Value;
673 });
674}
675
676void ARMAsmPrinter::emitAttributes() {
677 MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
678 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
679
681
682 ATS.switchVendor("aeabi");
683
684 // Compute ARM ELF Attributes based on the default subtarget that
685 // we'd have constructed. The existing ARM behavior isn't LTO clean
686 // anyhow.
687 // FIXME: For ifunc related functions we could iterate over and look
688 // for a feature string that doesn't match the default one.
689 const Triple &TT = TM.getTargetTriple();
690 StringRef CPU = TM.getTargetCPU();
691 StringRef FS = TM.getTargetFeatureString();
692 std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
693 if (!FS.empty()) {
694 if (!ArchFS.empty())
695 ArchFS = (Twine(ArchFS) + "," + FS).str();
696 else
697 ArchFS = std::string(FS);
698 }
699 const ARMBaseTargetMachine &ATM =
700 static_cast<const ARMBaseTargetMachine &>(TM);
701 FloatABI::ABIType FloatABI = ATM.getFloatABI(*MMI->getModule());
702 ARM::ARMABI ABI = ATM.getEffectiveABI(*MMI->getModule());
703 const ARMSubtarget STI(TT, std::string(CPU), ArchFS, ATM,
704 ATM.isLittleEndian(), FloatABI, ABI);
705
706 // Emit build attributes for the available hardware.
707 ATS.emitTargetAttributes(STI);
708
709 // RW data addressing.
710 if (isPositionIndependent()) {
713 } else if (STI.isRWPI()) {
714 // RWPI specific attributes.
717 }
718
719 // RO data addressing.
720 if (isPositionIndependent() || STI.isROPI()) {
723 }
724
725 // GOT use.
726 if (isPositionIndependent()) {
729 } else {
732 }
733
734 // Set FP Denormals.
736 MMI->getModule()->getModuleFlag("arm-eabi-fp-denormal"))) {
737 if (unsigned TagVal = DM->getZExtValue())
739 } else if (checkDenormalAttributeConsistency(*MMI->getModule(),
743 else if (checkDenormalAttributeConsistency(*MMI->getModule(),
747 else if (checkDenormalAttributeInconsistency(*MMI->getModule()) ||
752 else {
753 if (!STI.hasVFP2Base()) {
754 // When the target doesn't have an FPU (by design or
755 // intention), the assumptions made on the software support
756 // mirror that of the equivalent hardware support *if it
757 // existed*. For v7 and better we indicate that denormals are
758 // flushed preserving sign, and for V6 we indicate that
759 // denormals are flushed to positive zero.
760 if (STI.hasV7Ops())
763 } else if (STI.hasVFP3Base()) {
764 // In VFPv4, VFPv4U, VFPv3, or VFPv3U, it is preserved. That is,
765 // the sign bit of the zero matches the sign bit of the input or
766 // result that is being flushed to zero.
769 }
770 // For VFPv2 implementations it is implementation defined as
771 // to whether denormals are flushed to positive zero or to
772 // whatever the sign of zero is (ARM v7AR ARM 2.7.5). Historically
773 // LLVM has chosen to flush this to positive zero (most likely for
774 // GCC compatibility), so that's the chosen value here (the
775 // absence of its emission implies zero).
776 }
777
778 // Set FP exceptions and rounding
780 MMI->getModule()->getModuleFlag("arm-eabi-fp-exceptions"))) {
781 if (unsigned TagVal = Ex->getZExtValue())
783 } else if (checkFunctionsAttributeConsistency(*MMI->getModule(),
784 "no-trapping-math", "true"))
787 else {
789
790 // If the user has permitted this code to choose the IEEE 754
791 // rounding at run-time, emit the rounding attribute.
792 if (TM.Options.HonorSignDependentRoundingFPMathOption)
794 }
795
796 // Generate ABI tags from module flags.
797 if (auto *NumModel = mdconst::extract_or_null<ConstantInt>(
798 MMI->getModule()->getModuleFlag("arm-eabi-fp-number-model"))) {
799 if (unsigned TagVal = NumModel->getZExtValue())
801 } else
804
805 // FIXME: add more flags to ARMBuildAttributes.h
806 // 8-bytes alignment stuff.
809
810 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
811 if (STI.isAAPCS_ABI() && STI.isTargetHardFloat())
813
814 // FIXME: To support emitting this build attribute as GCC does, the
815 // -mfp16-format option and associated plumbing must be
816 // supported. For now the __fp16 type is exposed by default, so this
817 // attribute should be emitted with value 1.
820
821 if (const Module *SourceModule = MMI->getModule()) {
822 // ABI_PCS_wchar_t to indicate wchar_t width
823 // FIXME: There is no way to emit value 0 (wchar_t prohibited).
824 int WCharWidth = TM.getTargetTriple().getDefaultWCharSize();
825 if (auto WCharWidthValue = mdconst::extract_or_null<ConstantInt>(
826 SourceModule->getModuleFlag("wchar_size")))
827 WCharWidth = WCharWidthValue->getZExtValue();
828 assert((WCharWidth == 2 || WCharWidth == 4) &&
829 "wchar_t width must be 2 or 4 bytes");
831
832 // ABI_enum_size to indicate enum width
833 // FIXME: There is no way to emit value 0 (enums prohibited) or value 3
834 // (all enums contain a value needing 32 bits to encode).
835 if (auto EnumWidthValue = mdconst::extract_or_null<ConstantInt>(
836 SourceModule->getModuleFlag("min_enum_size"))) {
837 int EnumWidth = EnumWidthValue->getZExtValue();
838 assert((EnumWidth == 1 || EnumWidth == 4) &&
839 "Minimum enum width must be 1 or 4 bytes");
840 int EnumBuildAttr = EnumWidth == 1 ? 1 : 2;
842 }
843
845 SourceModule->getModuleFlag("sign-return-address"));
846 if (PACValue && PACValue->isOne()) {
847 // If "+pacbti" is used as an architecture extension,
848 // Tag_PAC_extension is emitted in
849 // ARMTargetStreamer::emitTargetAttributes().
850 if (!STI.hasPACBTI()) {
853 }
855 }
856
858 SourceModule->getModuleFlag("branch-target-enforcement"));
859 if (BTIValue && !BTIValue->isZero()) {
860 // If "+pacbti" is used as an architecture extension,
861 // Tag_BTI_extension is emitted in
862 // ARMTargetStreamer::emitTargetAttributes().
863 if (!STI.hasPACBTI()) {
866 }
868 }
869 }
870
871 // We currently do not support using R9 as the TLS pointer.
872 if (STI.isRWPI())
875 else if (STI.isR9Reserved())
878 else
881}
882
883//===----------------------------------------------------------------------===//
884
885static MCSymbol *getBFLabel(StringRef Prefix, unsigned FunctionNumber,
886 unsigned LabelId, MCContext &Ctx) {
887
888 MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix)
889 + "BF" + Twine(FunctionNumber) + "_" + Twine(LabelId));
890 return Label;
891}
892
893static MCSymbol *getPICLabel(StringRef Prefix, unsigned FunctionNumber,
894 unsigned LabelId, MCContext &Ctx) {
895
896 MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix)
897 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
898 return Label;
899}
900
902 switch (Modifier) {
904 return ARM::S_None;
905 case ARMCP::TLSGD:
906 return ARM::S_TLSGD;
907 case ARMCP::TPOFF:
908 return ARM::S_TPOFF;
909 case ARMCP::GOTTPOFF:
910 return ARM::S_GOTTPOFF;
911 case ARMCP::SBREL:
912 return ARM::S_SBREL;
913 case ARMCP::GOT_PREL:
914 return ARM::S_GOT_PREL;
915 case ARMCP::SECREL:
916 return ARM::S_COFF_SECREL;
917 }
918 llvm_unreachable("Invalid ARMCPModifier!");
919}
920
921MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
922 unsigned char TargetFlags) {
923 const Triple &TT = TM.getTargetTriple();
924 if (TT.isOSBinFormatMachO()) {
925 bool IsIndirect =
926 (TargetFlags & ARMII::MO_NONLAZY) && getTM().isGVIndirectSymbol(GV);
927
928 if (!IsIndirect)
929 return getSymbol(GV);
930
931 // FIXME: Remove this when Darwin transition to @GOT like syntax.
932 MCSymbol *MCSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
933 MachineModuleInfoMachO &MMIMachO =
934 MMI->getObjFileInfo<MachineModuleInfoMachO>();
936 GV->isThreadLocal() ? MMIMachO.getThreadLocalGVStubEntry(MCSym)
937 : MMIMachO.getGVStubEntry(MCSym);
938
939 if (!StubSym.getPointer())
941 !GV->hasInternalLinkage());
942 return MCSym;
943 } else if (TT.isOSBinFormatCOFF()) {
944 assert(TT.isOSWindows() && "Windows is the only supported COFF target");
945
946 bool IsIndirect =
947 (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB));
948 if (!IsIndirect)
949 return getSymbol(GV);
950
951 SmallString<128> Name;
952 if (TargetFlags & ARMII::MO_DLLIMPORT)
953 Name = "__imp_";
954 else if (TargetFlags & ARMII::MO_COFFSTUB)
955 Name = ".refptr.";
956 getNameWithPrefix(Name, GV);
957
958 MCSymbol *MCSym = OutContext.getOrCreateSymbol(Name);
959
960 if (TargetFlags & ARMII::MO_COFFSTUB) {
961 MachineModuleInfoCOFF &MMICOFF =
962 MMI->getObjFileInfo<MachineModuleInfoCOFF>();
964 MMICOFF.getGVStubEntry(MCSym);
965
966 if (!StubSym.getPointer())
968 }
969
970 return MCSym;
971 } else if (TT.isOSBinFormatELF()) {
972 return getSymbolPreferLocal(*GV);
973 }
974 llvm_unreachable("unexpected target");
975}
976
979 const DataLayout &DL = getDataLayout();
980 int Size = DL.getTypeAllocSize(MCPV->getType());
981
982 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
983
984 if (ACPV->isPromotedGlobal()) {
985 // This constant pool entry is actually a global whose storage has been
986 // promoted into the constant pool. This global may be referenced still
987 // by debug information, and due to the way AsmPrinter is set up, the debug
988 // info is immutable by the time we decide to promote globals to constant
989 // pools. Because of this, we need to ensure we emit a symbol for the global
990 // with private linkage (the default) so debug info can refer to it.
991 //
992 // However, if this global is promoted into several functions we must ensure
993 // we don't try and emit duplicate symbols!
994 auto *ACPC = cast<ARMConstantPoolConstant>(ACPV);
995 for (const auto *GV : ACPC->promotedGlobals()) {
996 if (!EmittedPromotedGlobalLabels.count(GV)) {
997 MCSymbol *GVSym = getSymbol(GV);
998 OutStreamer->emitLabel(GVSym);
999 EmittedPromotedGlobalLabels.insert(GV);
1000 }
1001 }
1002 return emitGlobalConstant(DL, ACPC->getPromotedGlobalInit());
1003 }
1004
1005 MCSymbol *MCSym;
1006 if (ACPV->isLSDA()) {
1007 MCSym = getMBBExceptionSym(MF->front());
1008 } else if (ACPV->isBlockAddress()) {
1009 const BlockAddress *BA =
1010 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
1011 MCSym = GetBlockAddressSymbol(BA);
1012 } else if (ACPV->isGlobalValue()) {
1013 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
1014
1015 // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
1016 // flag the global as MO_NONLAZY.
1017 unsigned char TF =
1018 TM.getTargetTriple().isOSBinFormatMachO() ? ARMII::MO_NONLAZY : 0;
1019 MCSym = GetARMGVSymbol(GV, TF);
1020
1021 // For dso_local weak symbols in ELF PIC mode, the assembler would eagerly
1022 // resolve a PC-relative expression like sym-(LPC+8) when the symbol and
1023 // reference are in the same section, preventing the linker from overriding
1024 // a weak definition with a non-weak definition from another section. Use a
1025 // .reloc directive rather than a fixup to force the generation of a
1026 // relocation (R_ARM_REL32) so the linker can perform the override. This is
1027 // restricted to dso_local, non-TLS symbols: a preemptible/external weak
1028 // symbol (e.g. an extern_weak reference) must use the GOT, as R_ARM_REL32
1029 // against an external symbol cannot be used when making a shared object;
1030 // and TLS symbols require TLS-specific relocations, not R_ARM_REL32.
1031 if (GV->isWeakForLinker() && GV->isDSOLocal() && !GV->isThreadLocal() &&
1032 TM.getTargetTriple().isOSBinFormatELF() && TM.isPositionIndependent() &&
1033 ACPV->getPCAdjustment() != 0) {
1034 MCSymbol *CPILabel = OutContext.createTempSymbol();
1035 OutStreamer->emitLabel(CPILabel);
1036 // Emit local-only expression: CPILabel - (LPC+PCAdj)
1037 const MCExpr *LocalExpr = MCSymbolRefExpr::create(CPILabel, OutContext);
1038 MCSymbol *PCLabel =
1039 getPICLabel(DL.getInternalSymbolPrefix(), getFunctionNumber(),
1040 ACPV->getLabelId(), OutContext);
1041 const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
1042 PCRelExpr = MCBinaryExpr::createAdd(
1043 PCRelExpr,
1045 OutContext);
1046 LocalExpr = MCBinaryExpr::createSub(LocalExpr, PCRelExpr, OutContext);
1047 OutStreamer->emitValue(LocalExpr, Size);
1048 // Emit .reloc to force linker resolution of the weak symbol.
1049 const MCExpr *CPIExpr = MCSymbolRefExpr::create(CPILabel, OutContext);
1050 const MCExpr *SymExpr = MCSymbolRefExpr::create(MCSym, OutContext);
1051 OutStreamer->emitRelocDirective(*CPIExpr, "R_ARM_REL32", SymExpr,
1052 SMLoc());
1053 return;
1054 }
1055 } else if (ACPV->isMachineBasicBlock()) {
1056 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
1057 MCSym = MBB->getSymbol();
1058 } else {
1059 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
1060 auto Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
1061 MCSym = GetExternalSymbolSymbol(Sym);
1062 }
1063
1064 // Create an MCSymbol for the reference.
1065 const MCExpr *Expr = MCSymbolRefExpr::create(
1067
1068 if (ACPV->getPCAdjustment()) {
1069 MCSymbol *PCLabel =
1070 getPICLabel(DL.getInternalSymbolPrefix(), getFunctionNumber(),
1071 ACPV->getLabelId(), OutContext);
1072 const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
1073 PCRelExpr =
1074 MCBinaryExpr::createAdd(PCRelExpr,
1076 OutContext),
1077 OutContext);
1078 if (ACPV->mustAddCurrentAddress()) {
1079 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
1080 // label, so just emit a local label end reference that instead.
1081 MCSymbol *DotSym = OutContext.createTempSymbol();
1082 OutStreamer->emitLabel(DotSym);
1083 const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext);
1084 PCRelExpr = MCBinaryExpr::createSub(PCRelExpr, DotExpr, OutContext);
1085 }
1086 Expr = MCBinaryExpr::createSub(Expr, PCRelExpr, OutContext);
1087 }
1088 OutStreamer->emitValue(Expr, Size);
1089}
1090
1092 const MachineOperand &MO1 = MI->getOperand(1);
1093 unsigned JTI = MO1.getIndex();
1094
1095 // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
1096 // ARM mode tables.
1097 emitAlignment(Align(4));
1098
1099 // Emit a label for the jump table.
1100 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
1101 OutStreamer->emitLabel(JTISymbol);
1102
1103 // Mark the jump table as data-in-code.
1104 OutStreamer->emitDataRegion(MCDR_DataRegionJT32);
1105
1106 // Emit each entry of the table.
1107 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1108 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1109 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1110
1111 for (MachineBasicBlock *MBB : JTBBs) {
1112 // Construct an MCExpr for the entry. We want a value of the form:
1113 // (BasicBlockAddr - TableBeginAddr)
1114 //
1115 // For example, a table with entries jumping to basic blocks BB0 and BB1
1116 // would look like:
1117 // LJTI_0_0:
1118 // .word (LBB0 - LJTI_0_0)
1119 // .word (LBB1 - LJTI_0_0)
1120 const MCExpr *Expr = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
1121
1122 const ARMSubtarget &STI = MF->getSubtarget<ARMSubtarget>();
1123 if (isPositionIndependent() || STI.isROPI())
1124 Expr = MCBinaryExpr::createSub(Expr, MCSymbolRefExpr::create(JTISymbol,
1125 OutContext),
1126 OutContext);
1127 // If we're generating a table of Thumb addresses in static relocation
1128 // model, we need to add one to keep interworking correctly.
1129 else if (AFI->isThumbFunction())
1131 OutContext);
1132 OutStreamer->emitValue(Expr, 4);
1133 }
1134 // Mark the end of jump table data-in-code region.
1135 OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
1136}
1137
1139 const MachineOperand &MO1 = MI->getOperand(1);
1140 unsigned JTI = MO1.getIndex();
1141
1142 // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
1143 // ARM mode tables.
1144 emitAlignment(Align(4));
1145
1146 // Emit a label for the jump table.
1147 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
1148 OutStreamer->emitLabel(JTISymbol);
1149
1150 // Emit each entry of the table.
1151 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1152 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1153 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1154
1155 for (MachineBasicBlock *MBB : JTBBs) {
1156 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(),
1157 OutContext);
1158 // If this isn't a TBB or TBH, the entries are direct branch instructions.
1160 .addExpr(MBBSymbolExpr)
1161 .addImm(ARMCC::AL)
1162 .addReg(0));
1163 }
1164}
1165
1167 unsigned OffsetWidth) {
1168 assert((OffsetWidth == 1 || OffsetWidth == 2) && "invalid tbb/tbh width");
1169 const MachineOperand &MO1 = MI->getOperand(1);
1170 unsigned JTI = MO1.getIndex();
1171
1172 const ARMSubtarget &STI = MF->getSubtarget<ARMSubtarget>();
1173 if (STI.isThumb1Only())
1174 emitAlignment(Align(4));
1175
1176 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
1177 OutStreamer->emitLabel(JTISymbol);
1178
1179 // Emit each entry of the table.
1180 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1181 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1182 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1183
1184 // Mark the jump table as data-in-code.
1185 OutStreamer->emitDataRegion(OffsetWidth == 1 ? MCDR_DataRegionJT8
1187
1188 for (auto *MBB : JTBBs) {
1189 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(),
1190 OutContext);
1191 // Otherwise it's an offset from the dispatch instruction. Construct an
1192 // MCExpr for the entry. We want a value of the form:
1193 // (BasicBlockAddr - TBBInstAddr + 4) / 2
1194 //
1195 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1196 // would look like:
1197 // LJTI_0_0:
1198 // .byte (LBB0 - (LCPI0_0 + 4)) / 2
1199 // .byte (LBB1 - (LCPI0_0 + 4)) / 2
1200 // where LCPI0_0 is a label defined just before the TBB instruction using
1201 // this table.
1202 MCSymbol *TBInstPC = GetCPISymbol(MI->getOperand(0).getImm());
1203 const MCExpr *Expr = MCBinaryExpr::createAdd(
1206 Expr = MCBinaryExpr::createSub(MBBSymbolExpr, Expr, OutContext);
1208 OutContext);
1209 OutStreamer->emitValue(Expr, OffsetWidth);
1210 }
1211 // Mark the end of jump table data-in-code region. 32-bit offsets use
1212 // actual branch instructions here, so we don't mark those as a data-region
1213 // at all.
1214 OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
1215
1216 // Make sure the next instruction is 2-byte aligned.
1217 emitAlignment(Align(2));
1218}
1219
1220std::tuple<const MCSymbol *, uint64_t, const MCSymbol *,
1223 const MachineInstr *BranchInstr,
1224 const MCSymbol *BranchLabel) const {
1226 const MCSymbol *BaseLabel;
1227 uint64_t BaseOffset = 0;
1228 switch (BranchInstr->getOpcode()) {
1229 case ARM::BR_JTadd:
1230 case ARM::BR_JTr:
1231 case ARM::tBR_JTr:
1232 // Word relative to the jump table address.
1234 BaseLabel = GetARMJTIPICJumpTableLabel(JTI);
1235 break;
1236 case ARM::tTBH_JT:
1237 case ARM::t2TBH_JT:
1238 // half-word shifted left, relative to *after* the branch instruction.
1240 BranchLabel = GetCPISymbol(BranchInstr->getOperand(3).getImm());
1241 BaseLabel = BranchLabel;
1242 BaseOffset = 4;
1243 break;
1244 case ARM::tTBB_JT:
1245 case ARM::t2TBB_JT:
1246 // byte shifted left, relative to *after* the branch instruction.
1248 BranchLabel = GetCPISymbol(BranchInstr->getOperand(3).getImm());
1249 BaseLabel = BranchLabel;
1250 BaseOffset = 4;
1251 break;
1252 case ARM::t2BR_JT:
1253 // Direct jump.
1254 BaseLabel = nullptr;
1256 break;
1257 default:
1258 llvm_unreachable("Unknown jump table instruction");
1259 }
1260
1261 return std::make_tuple(BaseLabel, BaseOffset, BranchLabel, EntrySize);
1262}
1263
1264void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1266 "Only instruction which are involved into frame setup code are allowed");
1267
1268 MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
1269 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1270 const MachineFunction &MF = *MI->getParent()->getParent();
1271 const TargetRegisterInfo *TargetRegInfo =
1273 const MachineRegisterInfo &MachineRegInfo = MF.getRegInfo();
1274
1275 Register FramePtr = TargetRegInfo->getFrameRegister(MF);
1276 unsigned Opc = MI->getOpcode();
1277 unsigned SrcReg, DstReg;
1278
1279 switch (Opc) {
1280 case ARM::tPUSH:
1281 // special case: tPUSH does not have src/dst regs.
1282 SrcReg = DstReg = ARM::SP;
1283 break;
1284 case ARM::tLDRpci:
1285 case ARM::t2MOVi16:
1286 case ARM::t2MOVTi16:
1287 case ARM::tMOVi8:
1288 case ARM::tADDi8:
1289 case ARM::tLSLri:
1290 // special cases:
1291 // 1) for Thumb1 code we sometimes materialize the constant via constpool
1292 // load.
1293 // 2) for Thumb1 execute only code we materialize the constant via the
1294 // following pattern:
1295 // movs r3, #:upper8_15:<const>
1296 // lsls r3, #8
1297 // adds r3, #:upper0_7:<const>
1298 // lsls r3, #8
1299 // adds r3, #:lower8_15:<const>
1300 // lsls r3, #8
1301 // adds r3, #:lower0_7:<const>
1302 // So we need to special-case MOVS, ADDS and LSLS, and keep track of
1303 // where we are in the sequence with the simplest of state machines.
1304 // 3) for Thumb2 execute only code we materialize the constant via
1305 // immediate constants in 2 separate instructions (MOVW/MOVT).
1306 SrcReg = ~0U;
1307 DstReg = MI->getOperand(0).getReg();
1308 break;
1309 case ARM::VMRS:
1310 SrcReg = ARM::FPSCR;
1311 DstReg = MI->getOperand(0).getReg();
1312 break;
1313 case ARM::VMRS_FPEXC:
1314 SrcReg = ARM::FPEXC;
1315 DstReg = MI->getOperand(0).getReg();
1316 break;
1317 default:
1318 SrcReg = MI->getOperand(1).getReg();
1319 DstReg = MI->getOperand(0).getReg();
1320 break;
1321 }
1322
1323 // Try to figure out the unwinding opcode out of src / dst regs.
1324 if (MI->mayStore()) {
1325 // Register saves.
1326 assert(DstReg == ARM::SP &&
1327 "Only stack pointer as a destination reg is supported");
1328
1330 // Skip src & dst reg, and pred ops.
1331 unsigned StartOp = 2 + 2;
1332 // Use all the operands.
1333 unsigned NumOffset = 0;
1334 // Amount of SP adjustment folded into a push, before the
1335 // registers are stored (pad at higher addresses).
1336 unsigned PadBefore = 0;
1337 // Amount of SP adjustment folded into a push, after the
1338 // registers are stored (pad at lower addresses).
1339 unsigned PadAfter = 0;
1340
1341 switch (Opc) {
1342 default:
1343 MI->print(errs());
1344 llvm_unreachable("Unsupported opcode for unwinding information");
1345 case ARM::tPUSH:
1346 // Special case here: no src & dst reg, but two extra imp ops.
1347 StartOp = 2; NumOffset = 2;
1348 [[fallthrough]];
1349 case ARM::STMDB_UPD:
1350 case ARM::t2STMDB_UPD:
1351 case ARM::VSTMDDB_UPD:
1352 assert(SrcReg == ARM::SP &&
1353 "Only stack pointer as a source reg is supported");
1354 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1355 i != NumOps; ++i) {
1356 const MachineOperand &MO = MI->getOperand(i);
1357 // Actually, there should never be any impdef stuff here. Skip it
1358 // temporary to workaround PR11902.
1359 if (MO.isImplicit())
1360 continue;
1361 // Registers, pushed as a part of folding an SP update into the
1362 // push instruction are marked as undef and should not be
1363 // restored when unwinding, because the function can modify the
1364 // corresponding stack slots.
1365 if (MO.isUndef()) {
1366 assert(RegList.empty() &&
1367 "Pad registers must come before restored ones");
1368 unsigned Width =
1369 TargetRegInfo->getRegSizeInBits(MO.getReg(), MachineRegInfo) / 8;
1370 PadAfter += Width;
1371 continue;
1372 }
1373 // Check for registers that are remapped (for a Thumb1 prologue that
1374 // saves high registers).
1375 Register Reg = MO.getReg();
1376 if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(Reg))
1377 Reg = RemappedReg;
1378 RegList.push_back(Reg);
1379 }
1380 break;
1381 case ARM::STR_PRE_IMM:
1382 case ARM::STR_PRE_REG:
1383 case ARM::t2STR_PRE:
1384 assert(MI->getOperand(2).getReg() == ARM::SP &&
1385 "Only stack pointer as a source reg is supported");
1386 if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg))
1387 SrcReg = RemappedReg;
1388
1389 RegList.push_back(SrcReg);
1390 break;
1391 case ARM::t2STRD_PRE:
1392 assert(MI->getOperand(3).getReg() == ARM::SP &&
1393 "Only stack pointer as a source reg is supported");
1394 SrcReg = MI->getOperand(1).getReg();
1395 if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg))
1396 SrcReg = RemappedReg;
1397 RegList.push_back(SrcReg);
1398 SrcReg = MI->getOperand(2).getReg();
1399 if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg))
1400 SrcReg = RemappedReg;
1401 RegList.push_back(SrcReg);
1402 PadBefore = -MI->getOperand(4).getImm() - 8;
1403 break;
1404 }
1405 if (MAI.getExceptionHandlingType() == ExceptionHandling::ARM) {
1406 if (PadBefore)
1407 ATS.emitPad(PadBefore);
1408 ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1409 // Account for the SP adjustment, folded into the push.
1410 if (PadAfter)
1411 ATS.emitPad(PadAfter);
1412 }
1413 } else {
1414 // Changes of stack / frame pointer.
1415 if (SrcReg == ARM::SP) {
1416 int64_t Offset = 0;
1417 switch (Opc) {
1418 default:
1419 MI->print(errs());
1420 llvm_unreachable("Unsupported opcode for unwinding information");
1421 case ARM::tLDRspi:
1422 // Used to restore LR in a prologue which uses it as a temporary, has
1423 // no effect on unwind tables.
1424 return;
1425 case ARM::MOVr:
1426 case ARM::tMOVr:
1427 Offset = 0;
1428 break;
1429 case ARM::ADDri:
1430 case ARM::t2ADDri:
1431 case ARM::t2ADDri12:
1432 case ARM::t2ADDspImm:
1433 case ARM::t2ADDspImm12:
1434 Offset = -MI->getOperand(2).getImm();
1435 break;
1436 case ARM::SUBri:
1437 case ARM::t2SUBri:
1438 case ARM::t2SUBri12:
1439 case ARM::t2SUBspImm:
1440 case ARM::t2SUBspImm12:
1441 Offset = MI->getOperand(2).getImm();
1442 break;
1443 case ARM::tSUBspi:
1444 Offset = MI->getOperand(2).getImm()*4;
1445 break;
1446 case ARM::tADDspi:
1447 case ARM::tADDrSPi:
1448 Offset = -MI->getOperand(2).getImm()*4;
1449 break;
1450 case ARM::tADDhirr:
1451 Offset =
1452 -AFI->EHPrologueOffsetInRegs.lookup(MI->getOperand(2).getReg());
1453 break;
1454 }
1455
1456 if (MAI.getExceptionHandlingType() == ExceptionHandling::ARM) {
1457 if (DstReg == FramePtr && FramePtr != ARM::SP)
1458 // Set-up of the frame pointer. Positive values correspond to "add"
1459 // instruction.
1460 ATS.emitSetFP(FramePtr, ARM::SP, -Offset);
1461 else if (DstReg == ARM::SP) {
1462 // Change of SP by an offset. Positive values correspond to "sub"
1463 // instruction.
1464 ATS.emitPad(Offset);
1465 } else {
1466 // Move of SP to a register. Positive values correspond to an "add"
1467 // instruction.
1468 ATS.emitMovSP(DstReg, -Offset);
1469 }
1470 }
1471 } else if (DstReg == ARM::SP) {
1472 MI->print(errs());
1473 llvm_unreachable("Unsupported opcode for unwinding information");
1474 } else {
1475 int64_t Offset = 0;
1476 switch (Opc) {
1477 case ARM::tMOVr:
1478 // If a Thumb1 function spills r8-r11, we copy the values to low
1479 // registers before pushing them. Record the copy so we can emit the
1480 // correct ".save" later.
1481 AFI->EHPrologueRemappedRegs[DstReg] = SrcReg;
1482 break;
1483 case ARM::VMRS:
1484 case ARM::VMRS_FPEXC:
1485 // If a function spills FPSCR or FPEXC, we copy the values to low
1486 // registers before pushing them. However, we can't issue annotations
1487 // for FP status registers because ".save" requires GPR registers, and
1488 // ".vsave" requires DPR registers, so don't record the copy and simply
1489 // emit annotations for the source registers used for the store.
1490 break;
1491 case ARM::tLDRpci: {
1492 // Grab the constpool index and check, whether it corresponds to
1493 // original or cloned constpool entry.
1494 unsigned CPI = MI->getOperand(1).getIndex();
1495 const MachineConstantPool *MCP = MF.getConstantPool();
1496 if (CPI >= MCP->getConstants().size())
1497 CPI = AFI->getOriginalCPIdx(CPI);
1498 assert(CPI != -1U && "Invalid constpool index");
1499
1500 // Derive the actual offset.
1501 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1502 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1503 Offset = cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
1504 AFI->EHPrologueOffsetInRegs[DstReg] = Offset;
1505 break;
1506 }
1507 case ARM::t2MOVi16:
1508 Offset = MI->getOperand(1).getImm();
1509 AFI->EHPrologueOffsetInRegs[DstReg] = Offset;
1510 break;
1511 case ARM::t2MOVTi16:
1512 Offset = MI->getOperand(2).getImm();
1513 AFI->EHPrologueOffsetInRegs[DstReg] |= (Offset << 16);
1514 break;
1515 case ARM::tMOVi8:
1516 Offset = MI->getOperand(2).getImm();
1517 AFI->EHPrologueOffsetInRegs[DstReg] = Offset;
1518 break;
1519 case ARM::tLSLri:
1520 assert(MI->getOperand(3).getImm() == 8 &&
1521 "The shift amount is not equal to 8");
1522 assert(MI->getOperand(2).getReg() == MI->getOperand(0).getReg() &&
1523 "The source register is not equal to the destination register");
1524 AFI->EHPrologueOffsetInRegs[DstReg] <<= 8;
1525 break;
1526 case ARM::tADDi8:
1527 assert(MI->getOperand(2).getReg() == MI->getOperand(0).getReg() &&
1528 "The source register is not equal to the destination register");
1529 Offset = MI->getOperand(3).getImm();
1530 AFI->EHPrologueOffsetInRegs[DstReg] += Offset;
1531 break;
1532 case ARM::t2PAC:
1533 case ARM::t2PACBTI:
1534 AFI->EHPrologueRemappedRegs[ARM::R12] = ARM::RA_AUTH_CODE;
1535 break;
1536 default:
1537 MI->print(errs());
1538 llvm_unreachable("Unsupported opcode for unwinding information");
1539 }
1540 }
1541 }
1542}
1543
1544// Simple pseudo-instructions have their lowering (with expansion to real
1545// instructions) auto-generated.
1546#include "ARMGenMCPseudoLowering.inc"
1547
1548// Helper function to check if a register is live (used as an implicit operand)
1549// in the given call instruction.
1551 for (const MachineOperand &MO : Call.implicit_operands()) {
1552 if (MO.isReg() && MO.getReg() == Reg && MO.isUse()) {
1553 return true;
1554 }
1555 }
1556 return false;
1557}
1558
1559void ARMAsmPrinter::EmitKCFI_CHECK_ARM32(Register AddrReg, int64_t Type,
1560 const MachineInstr &Call,
1561 int64_t PrefixNops) {
1562 // Choose scratch register: r12 primary, r3 if target is r12.
1563 unsigned ScratchReg = ARM::R12;
1564 if (AddrReg == ARM::R12) {
1565 ScratchReg = ARM::R3;
1566 }
1567
1568 // Calculate ESR for ARM mode (16-bit): 0x8000 | (scratch_reg << 5) | addr_reg
1569 // Note: scratch_reg is always 0x1F since the EOR sequence clobbers it.
1570 const ARMBaseRegisterInfo *TRI = static_cast<const ARMBaseRegisterInfo *>(
1571 MF->getSubtarget().getRegisterInfo());
1572 unsigned AddrIndex = TRI->getEncodingValue(AddrReg);
1573 unsigned ESR = 0x8000 | (31 << 5) | (AddrIndex & 31);
1574
1575 // Check if r3 is live and needs to be spilled.
1576 bool NeedSpillR3 =
1577 (ScratchReg == ARM::R3) && isRegisterLiveInCall(Call, ARM::R3);
1578
1579 // If we need to spill r3, push it first.
1580 if (NeedSpillR3) {
1581 // push {r3}
1582 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::STMDB_UPD)
1583 .addReg(ARM::SP)
1584 .addReg(ARM::SP)
1585 .addImm(ARMCC::AL)
1586 .addReg(0)
1587 .addReg(ARM::R3));
1588 }
1589
1590 // Clear bit 0 of target address to handle Thumb function pointers.
1591 // In 32-bit ARM, function pointers may have the low bit set to indicate
1592 // Thumb state when ARM/Thumb interworking is enabled (ARMv4T and later).
1593 // We need to clear it to avoid an alignment fault when loading.
1594 // bic scratch, target, #1
1595 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BICri)
1596 .addReg(ScratchReg)
1597 .addReg(AddrReg)
1598 .addImm(1)
1599 .addImm(ARMCC::AL)
1600 .addReg(0)
1601 .addReg(0));
1602
1603 // ldr scratch, [scratch, #-(PrefixNops * 4 + 4)]
1604 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
1605 .addReg(ScratchReg)
1606 .addReg(ScratchReg)
1607 .addImm(-(PrefixNops * 4 + 4))
1608 .addImm(ARMCC::AL)
1609 .addReg(0));
1610
1611 // Each EOR instruction XORs one byte of the type, shifted to its position.
1612 for (int i = 0; i < 4; i++) {
1613 uint8_t byte = (Type >> (i * 8)) & 0xFF;
1614 uint32_t imm = byte << (i * 8);
1615 bool isLast = (i == 3);
1616
1617 // Encode as ARM modified immediate.
1618 int SOImmVal = ARM_AM::getSOImmVal(imm);
1619 assert(SOImmVal != -1 &&
1620 "Cannot encode immediate as ARM modified immediate");
1621
1622 // eor[s] scratch, scratch, #imm (last one sets flags with CPSR)
1624 MCInstBuilder(ARM::EORri)
1625 .addReg(ScratchReg)
1626 .addReg(ScratchReg)
1627 .addImm(SOImmVal)
1628 .addImm(ARMCC::AL)
1629 .addReg(0)
1630 .addReg(isLast ? ARM::CPSR : ARM::NoRegister));
1631 }
1632
1633 // If we spilled r3, restore it immediately after the comparison.
1634 // This must happen before the branch so r3 is valid on both paths.
1635 if (NeedSpillR3) {
1636 // pop {r3}
1637 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDMIA_UPD)
1638 .addReg(ARM::SP)
1639 .addReg(ARM::SP)
1640 .addImm(ARMCC::AL)
1641 .addReg(0)
1642 .addReg(ARM::R3));
1643 }
1644
1645 // beq .Lpass (branch if types match, i.e., scratch is zero)
1646 MCSymbol *Pass = OutContext.createTempSymbol();
1648 MCInstBuilder(ARM::Bcc)
1650 .addImm(ARMCC::EQ)
1651 .addReg(ARM::CPSR));
1652
1653 // udf #ESR (trap with encoded diagnostic)
1654 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::UDF).addImm(ESR));
1655
1656 OutStreamer->emitLabel(Pass);
1657}
1658
1659void ARMAsmPrinter::EmitKCFI_CHECK_Thumb2(Register AddrReg, int64_t Type,
1660 const MachineInstr &Call,
1661 int64_t PrefixNops) {
1662 // Choose scratch register: r12 primary, r3 if target is r12.
1663 unsigned ScratchReg = ARM::R12;
1664 if (AddrReg == ARM::R12) {
1665 ScratchReg = ARM::R3;
1666 }
1667
1668 // Calculate ESR for Thumb mode (8-bit): 0x80 | addr_reg
1669 // Bit 7: KCFI trap indicator
1670 // Bits 6-5: Reserved
1671 // Bits 4-0: Address register encoding
1672 const ARMBaseRegisterInfo *TRI = static_cast<const ARMBaseRegisterInfo *>(
1673 MF->getSubtarget().getRegisterInfo());
1674 unsigned AddrIndex = TRI->getEncodingValue(AddrReg);
1675 unsigned ESR = 0x80 | (AddrIndex & 0x1F);
1676
1677 // Check if r3 is live and needs to be spilled.
1678 bool NeedSpillR3 =
1679 (ScratchReg == ARM::R3) && isRegisterLiveInCall(Call, ARM::R3);
1680
1681 // If we need to spill r3, push it first.
1682 if (NeedSpillR3) {
1683 // push {r3}
1685 *OutStreamer,
1686 MCInstBuilder(ARM::tPUSH).addImm(ARMCC::AL).addReg(0).addReg(ARM::R3));
1687 }
1688
1689 // Clear bit 0 of target address to handle Thumb function pointers.
1690 // In 32-bit ARM, function pointers may have the low bit set to indicate
1691 // Thumb state when ARM/Thumb interworking is enabled (ARMv4T and later).
1692 // We need to clear it to avoid an alignment fault when loading.
1693 // bic scratch, target, #1
1694 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2BICri)
1695 .addReg(ScratchReg)
1696 .addReg(AddrReg)
1697 .addImm(1)
1698 .addImm(ARMCC::AL)
1699 .addReg(0)
1700 .addReg(0));
1701
1702 // ldr scratch, [scratch, #-(PrefixNops * 4 + 4)]
1703 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi8)
1704 .addReg(ScratchReg)
1705 .addReg(ScratchReg)
1706 .addImm(-(PrefixNops * 4 + 4))
1707 .addImm(ARMCC::AL)
1708 .addReg(0));
1709
1710 // Each EOR instruction XORs one byte of the type, shifted to its position.
1711 for (int i = 0; i < 4; i++) {
1712 uint8_t byte = (Type >> (i * 8)) & 0xFF;
1713 uint32_t imm = byte << (i * 8);
1714 bool isLast = (i == 3);
1715
1716 // Verify the immediate can be encoded as Thumb2 modified immediate.
1717 assert(ARM_AM::getT2SOImmVal(imm) != -1 &&
1718 "Cannot encode immediate as Thumb2 modified immediate");
1719
1720 // eor[s] scratch, scratch, #imm (last one sets flags with CPSR)
1722 MCInstBuilder(ARM::t2EORri)
1723 .addReg(ScratchReg)
1724 .addReg(ScratchReg)
1725 .addImm(imm)
1726 .addImm(ARMCC::AL)
1727 .addReg(0)
1728 .addReg(isLast ? ARM::CPSR : ARM::NoRegister));
1729 }
1730
1731 // If we spilled r3, restore it immediately after the comparison.
1732 // This must happen before the branch so r3 is valid on both paths.
1733 if (NeedSpillR3) {
1734 // pop {r3}
1736 *OutStreamer,
1737 MCInstBuilder(ARM::tPOP).addImm(ARMCC::AL).addReg(0).addReg(ARM::R3));
1738 }
1739
1740 // beq .Lpass (branch if types match, i.e., scratch is zero)
1741 MCSymbol *Pass = OutContext.createTempSymbol();
1743 MCInstBuilder(ARM::t2Bcc)
1745 .addImm(ARMCC::EQ)
1746 .addReg(ARM::CPSR));
1747
1748 // udf #ESR (trap with encoded diagnostic)
1749 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tUDF).addImm(ESR));
1750
1751 OutStreamer->emitLabel(Pass);
1752}
1753
1754void ARMAsmPrinter::EmitKCFI_CHECK_Thumb1(Register AddrReg, int64_t Type,
1755 const MachineInstr &Call,
1756 int64_t PrefixNops) {
1757 // For Thumb1, use R2 unconditionally as scratch register (a low register
1758 // required for tLDRi). R3 is used for building the type hash.
1759 unsigned ScratchReg = ARM::R2;
1760 unsigned TempReg = ARM::R3;
1761
1762 // Check if r3 is live and needs to be spilled.
1763 bool NeedSpillR3 = isRegisterLiveInCall(Call, ARM::R3);
1764
1765 // Spill r3 if needed
1766 if (NeedSpillR3) {
1768 *OutStreamer,
1769 MCInstBuilder(ARM::tPUSH).addImm(ARMCC::AL).addReg(0).addReg(ARM::R3));
1770 }
1771
1772 // Check if r2 is live and needs to be spilled.
1773 bool NeedSpillR2 = isRegisterLiveInCall(Call, ARM::R2);
1774
1775 // Push R2 if it's live
1776 if (NeedSpillR2) {
1778 *OutStreamer,
1779 MCInstBuilder(ARM::tPUSH).addImm(ARMCC::AL).addReg(0).addReg(ARM::R2));
1780 }
1781
1782 // Clear bit 0 from target address
1783 // TempReg (R3) is used first as helper for BIC, then later for building type
1784 // hash.
1785
1786 // movs temp, #1
1787 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8)
1788 .addReg(TempReg)
1789 .addReg(ARM::CPSR)
1790 .addImm(1)
1791 .addImm(ARMCC::AL)
1792 .addReg(0));
1793
1794 // mov scratch, target
1795 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
1796 .addReg(ScratchReg)
1797 .addReg(AddrReg)
1798 .addImm(ARMCC::AL));
1799
1800 // bics scratch, temp (scratch = scratch & ~temp)
1801 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBIC)
1802 .addReg(ScratchReg)
1803 .addReg(ARM::CPSR)
1804 .addReg(ScratchReg)
1805 .addReg(TempReg)
1806 .addImm(ARMCC::AL)
1807 .addReg(0));
1808
1809 // Load type hash. Thumb1 doesn't support negative offsets, so subtract.
1810 int offset = PrefixNops * 4 + 4;
1811
1812 // subs scratch, #offset
1813 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tSUBi8)
1814 .addReg(ScratchReg)
1815 .addReg(ARM::CPSR)
1816 .addReg(ScratchReg)
1817 .addImm(offset)
1818 .addImm(ARMCC::AL)
1819 .addReg(0));
1820
1821 // ldr scratch, [scratch, #0]
1822 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
1823 .addReg(ScratchReg)
1824 .addReg(ScratchReg)
1825 .addImm(0)
1826 .addImm(ARMCC::AL)
1827 .addReg(0));
1828
1829 // Load expected type inline (instead of EOR sequence)
1830 //
1831 // This creates the 32-bit value byte-by-byte in the temp register:
1832 // movs temp, #byte3 (high byte)
1833 // lsls temp, temp, #8
1834 // adds temp, #byte2
1835 // lsls temp, temp, #8
1836 // adds temp, #byte1
1837 // lsls temp, temp, #8
1838 // adds temp, #byte0 (low byte)
1839
1840 uint8_t byte0 = (Type >> 0) & 0xFF;
1841 uint8_t byte1 = (Type >> 8) & 0xFF;
1842 uint8_t byte2 = (Type >> 16) & 0xFF;
1843 uint8_t byte3 = (Type >> 24) & 0xFF;
1844
1845 // movs temp, #byte3 (start with high byte)
1846 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8)
1847 .addReg(TempReg)
1848 .addReg(ARM::CPSR)
1849 .addImm(byte3)
1850 .addImm(ARMCC::AL)
1851 .addReg(0));
1852
1853 // lsls temp, temp, #8
1854 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
1855 .addReg(TempReg)
1856 .addReg(ARM::CPSR)
1857 .addReg(TempReg)
1858 .addImm(8)
1859 .addImm(ARMCC::AL)
1860 .addReg(0));
1861
1862 // adds temp, #byte2
1863 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDi8)
1864 .addReg(TempReg)
1865 .addReg(ARM::CPSR)
1866 .addReg(TempReg)
1867 .addImm(byte2)
1868 .addImm(ARMCC::AL)
1869 .addReg(0));
1870
1871 // lsls temp, temp, #8
1872 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
1873 .addReg(TempReg)
1874 .addReg(ARM::CPSR)
1875 .addReg(TempReg)
1876 .addImm(8)
1877 .addImm(ARMCC::AL)
1878 .addReg(0));
1879
1880 // adds temp, #byte1
1881 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDi8)
1882 .addReg(TempReg)
1883 .addReg(ARM::CPSR)
1884 .addReg(TempReg)
1885 .addImm(byte1)
1886 .addImm(ARMCC::AL)
1887 .addReg(0));
1888
1889 // lsls temp, temp, #8
1890 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
1891 .addReg(TempReg)
1892 .addReg(ARM::CPSR)
1893 .addReg(TempReg)
1894 .addImm(8)
1895 .addImm(ARMCC::AL)
1896 .addReg(0));
1897
1898 // adds temp, #byte0 (low byte)
1899 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDi8)
1900 .addReg(TempReg)
1901 .addReg(ARM::CPSR)
1902 .addReg(TempReg)
1903 .addImm(byte0)
1904 .addImm(ARMCC::AL)
1905 .addReg(0));
1906
1907 // cmp scratch, temp
1908 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tCMPr)
1909 .addReg(ScratchReg)
1910 .addReg(TempReg)
1911 .addImm(ARMCC::AL)
1912 .addReg(0));
1913
1914 // Restore registers if spilled (pop in reverse order of push: R2, then R3)
1915 if (NeedSpillR2) {
1916 // pop {r2}
1918 *OutStreamer,
1919 MCInstBuilder(ARM::tPOP).addImm(ARMCC::AL).addReg(0).addReg(ARM::R2));
1920 }
1921
1922 // Restore r3 if spilled
1923 if (NeedSpillR3) {
1924 // pop {r3}
1926 *OutStreamer,
1927 MCInstBuilder(ARM::tPOP).addImm(ARMCC::AL).addReg(0).addReg(ARM::R3));
1928 }
1929
1930 // beq .Lpass (branch if types match, i.e., scratch == temp)
1931 MCSymbol *Pass = OutContext.createTempSymbol();
1933 MCInstBuilder(ARM::tBcc)
1935 .addImm(ARMCC::EQ)
1936 .addReg(ARM::CPSR));
1937
1938 // bkpt #0 (trap with encoded diagnostic)
1939 EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBKPT).addImm(0));
1940
1941 OutStreamer->emitLabel(Pass);
1942}
1943
1945 Register AddrReg = MI.getOperand(0).getReg();
1946 const int64_t Type = MI.getOperand(1).getImm();
1947
1948 // Get the call instruction that follows this KCFI_CHECK.
1949 assert(std::next(MI.getIterator())->isCall() &&
1950 "KCFI_CHECK not followed by a call instruction");
1951 const MachineInstr &Call = *std::next(MI.getIterator());
1952
1953 // Adjust the offset for patchable-function-prefix.
1954 int64_t PrefixNops = MI.getMF()->getFunction().getFnAttributeAsParsedInteger(
1955 "patchable-function-prefix");
1956
1957 // Emit the appropriate instruction sequence based on the opcode variant.
1958 switch (MI.getOpcode()) {
1959 case ARM::KCFI_CHECK_ARM:
1960 EmitKCFI_CHECK_ARM32(AddrReg, Type, Call, PrefixNops);
1961 break;
1962 case ARM::KCFI_CHECK_Thumb2:
1963 EmitKCFI_CHECK_Thumb2(AddrReg, Type, Call, PrefixNops);
1964 break;
1965 case ARM::KCFI_CHECK_Thumb1:
1966 EmitKCFI_CHECK_Thumb1(AddrReg, Type, Call, PrefixNops);
1967 break;
1968 default:
1969 llvm_unreachable("Unexpected KCFI_CHECK opcode");
1970 }
1971}
1972
1974 ARM_MC::verifyInstructionPredicates(MI->getOpcode(),
1975 getSubtargetInfo().getFeatureBits());
1976
1977 const ARMSubtarget &STI = MF->getSubtarget<ARMSubtarget>();
1978 const DataLayout &DL = getDataLayout();
1979 MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
1980 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1981
1982 // If we just ended a constant pool, mark it as such.
1983 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1984 OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
1985 InConstantPool = false;
1986 }
1987
1988 // Emit unwinding stuff for frame-related instructions
1989 if (TM.getTargetTriple().isTargetEHABICompatible() &&
1990 MI->getFlag(MachineInstr::FrameSetup))
1991 EmitUnwindingInstruction(MI);
1992
1993 // Do any auto-generated pseudo lowerings.
1994 if (MCInst OutInst; lowerPseudoInstExpansion(MI, OutInst)) {
1995 EmitToStreamer(*OutStreamer, OutInst);
1996 return;
1997 }
1998
1999 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
2000 "Pseudo flag setting opcode should be expanded early");
2001
2002 // Check for manual lowerings.
2003 unsigned Opc = MI->getOpcode();
2004 switch (Opc) {
2005 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
2006 case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
2007 case ARM::KCFI_CHECK_ARM:
2008 case ARM::KCFI_CHECK_Thumb2:
2009 case ARM::KCFI_CHECK_Thumb1:
2011 return;
2012 case ARM::LEApcrel:
2013 case ARM::tLEApcrel:
2014 case ARM::t2LEApcrel: {
2015 // FIXME: Need to also handle globals and externals
2016 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
2017 EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() ==
2018 ARM::t2LEApcrel ? ARM::t2ADR
2019 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
2020 : ARM::ADR))
2021 .addReg(MI->getOperand(0).getReg())
2023 // Add predicate operands.
2024 .addImm(MI->getOperand(2).getImm())
2025 .addReg(MI->getOperand(3).getReg()));
2026 return;
2027 }
2028 case ARM::LEApcrelJT:
2029 case ARM::tLEApcrelJT:
2030 case ARM::t2LEApcrelJT: {
2031 MCSymbol *JTIPICSymbol =
2032 GetARMJTIPICJumpTableLabel(MI->getOperand(1).getIndex());
2033 EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() ==
2034 ARM::t2LEApcrelJT ? ARM::t2ADR
2035 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
2036 : ARM::ADR))
2037 .addReg(MI->getOperand(0).getReg())
2039 // Add predicate operands.
2040 .addImm(MI->getOperand(2).getImm())
2041 .addReg(MI->getOperand(3).getReg()));
2042 return;
2043 }
2044 // Darwin call instructions are just normal call instructions with different
2045 // clobber semantics (they clobber R9).
2046 case ARM::BX_CALL: {
2048 .addReg(ARM::LR)
2049 .addReg(ARM::PC)
2050 // Add predicate operands.
2051 .addImm(ARMCC::AL)
2052 .addReg(0)
2053 // Add 's' bit operand (always reg0 for this)
2054 .addReg(0));
2055
2056 assert(STI.hasV4TOps() && "Expected V4TOps for BX call");
2058 MCInstBuilder(ARM::BX).addReg(MI->getOperand(0).getReg()));
2059 return;
2060 }
2061 case ARM::tBX_CALL: {
2062 assert(!STI.hasV5TOps() && "Expected BLX to be selected for v5t+");
2063
2064 // On ARM v4t, when doing a call from thumb mode, we need to ensure
2065 // that the saved lr has its LSB set correctly (the arch doesn't
2066 // have blx).
2067 // So here we generate a bl to a small jump pad that does bx rN.
2068 // The jump pads are emitted after the function body.
2069
2070 Register TReg = MI->getOperand(0).getReg();
2071 MCSymbol *TRegSym = nullptr;
2072 for (std::pair<unsigned, MCSymbol *> &TIP : ThumbIndirectPads) {
2073 if (TIP.first == TReg) {
2074 TRegSym = TIP.second;
2075 break;
2076 }
2077 }
2078
2079 if (!TRegSym) {
2080 TRegSym = OutContext.createTempSymbol();
2081 ThumbIndirectPads.push_back(std::make_pair(TReg, TRegSym));
2082 }
2083
2084 // Create a link-saving branch to the Reg Indirect Jump Pad.
2086 // Predicate comes first here.
2087 .addImm(ARMCC::AL).addReg(0)
2088 .addExpr(MCSymbolRefExpr::create(TRegSym, OutContext)));
2089 return;
2090 }
2091 case ARM::BMOVPCRX_CALL: {
2093 .addReg(ARM::LR)
2094 .addReg(ARM::PC)
2095 // Add predicate operands.
2096 .addImm(ARMCC::AL)
2097 .addReg(0)
2098 // Add 's' bit operand (always reg0 for this)
2099 .addReg(0));
2100
2102 .addReg(ARM::PC)
2103 .addReg(MI->getOperand(0).getReg())
2104 // Add predicate operands.
2106 .addReg(0)
2107 // Add 's' bit operand (always reg0 for this)
2108 .addReg(0));
2109 return;
2110 }
2111 case ARM::BMOVPCB_CALL: {
2113 .addReg(ARM::LR)
2114 .addReg(ARM::PC)
2115 // Add predicate operands.
2116 .addImm(ARMCC::AL)
2117 .addReg(0)
2118 // Add 's' bit operand (always reg0 for this)
2119 .addReg(0));
2120
2121 const MachineOperand &Op = MI->getOperand(0);
2122 const GlobalValue *GV = Op.getGlobal();
2123 const unsigned TF = Op.getTargetFlags();
2124 MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
2125 const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
2127 .addExpr(GVSymExpr)
2128 // Add predicate operands.
2129 .addImm(ARMCC::AL)
2130 .addReg(0));
2131 return;
2132 }
2133 case ARM::MOVi16_ga_pcrel:
2134 case ARM::t2MOVi16_ga_pcrel: {
2135 MCInst TmpInst;
2136 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
2137 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
2138
2139 unsigned TF = MI->getOperand(1).getTargetFlags();
2140 const GlobalValue *GV = MI->getOperand(1).getGlobal();
2141 MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
2142 const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
2143
2144 MCSymbol *LabelSym =
2145 getPICLabel(DL.getInternalSymbolPrefix(), getFunctionNumber(),
2146 MI->getOperand(2).getImm(), OutContext);
2147 const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext);
2148 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
2149 const MCExpr *PCRelExpr = ARM::createLower16(
2151 GVSymExpr,
2152 MCBinaryExpr::createAdd(LabelSymExpr,
2154 OutContext),
2155 OutContext),
2156 OutContext);
2157 TmpInst.addOperand(MCOperand::createExpr(PCRelExpr));
2158
2159 // Add predicate operands.
2161 TmpInst.addOperand(MCOperand::createReg(0));
2162 // Add 's' bit operand (always reg0 for this)
2163 TmpInst.addOperand(MCOperand::createReg(0));
2164 EmitToStreamer(*OutStreamer, TmpInst);
2165 return;
2166 }
2167 case ARM::MOVTi16_ga_pcrel:
2168 case ARM::t2MOVTi16_ga_pcrel: {
2169 MCInst TmpInst;
2170 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
2171 ? ARM::MOVTi16 : ARM::t2MOVTi16);
2172 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
2173 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
2174
2175 unsigned TF = MI->getOperand(2).getTargetFlags();
2176 const GlobalValue *GV = MI->getOperand(2).getGlobal();
2177 MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
2178 const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
2179
2180 MCSymbol *LabelSym =
2181 getPICLabel(DL.getInternalSymbolPrefix(), getFunctionNumber(),
2182 MI->getOperand(3).getImm(), OutContext);
2183 const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext);
2184 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
2185 const MCExpr *PCRelExpr = ARM::createUpper16(
2187 GVSymExpr,
2188 MCBinaryExpr::createAdd(LabelSymExpr,
2190 OutContext),
2191 OutContext),
2192 OutContext);
2193 TmpInst.addOperand(MCOperand::createExpr(PCRelExpr));
2194 // Add predicate operands.
2196 TmpInst.addOperand(MCOperand::createReg(0));
2197 // Add 's' bit operand (always reg0 for this)
2198 TmpInst.addOperand(MCOperand::createReg(0));
2199 EmitToStreamer(*OutStreamer, TmpInst);
2200 return;
2201 }
2202 case ARM::t2BFi:
2203 case ARM::t2BFic:
2204 case ARM::t2BFLi:
2205 case ARM::t2BFr:
2206 case ARM::t2BFLr: {
2207 // This is a Branch Future instruction.
2208
2209 const MCExpr *BranchLabel = MCSymbolRefExpr::create(
2210 getBFLabel(DL.getInternalSymbolPrefix(), getFunctionNumber(),
2211 MI->getOperand(0).getIndex(), OutContext),
2212 OutContext);
2213
2214 auto MCInst = MCInstBuilder(Opc).addExpr(BranchLabel);
2215 if (MI->getOperand(1).isReg()) {
2216 // For BFr/BFLr
2217 MCInst.addReg(MI->getOperand(1).getReg());
2218 } else {
2219 // For BFi/BFLi/BFic
2220 const MCExpr *BranchTarget;
2221 if (MI->getOperand(1).isMBB())
2222 BranchTarget = MCSymbolRefExpr::create(
2223 MI->getOperand(1).getMBB()->getSymbol(), OutContext);
2224 else if (MI->getOperand(1).isGlobal()) {
2225 const GlobalValue *GV = MI->getOperand(1).getGlobal();
2226 BranchTarget = MCSymbolRefExpr::create(
2227 GetARMGVSymbol(GV, MI->getOperand(1).getTargetFlags()), OutContext);
2228 } else if (MI->getOperand(1).isSymbol()) {
2229 BranchTarget = MCSymbolRefExpr::create(
2230 GetExternalSymbolSymbol(MI->getOperand(1).getSymbolName()),
2231 OutContext);
2232 } else
2233 llvm_unreachable("Unhandled operand kind in Branch Future instruction");
2234
2235 MCInst.addExpr(BranchTarget);
2236 }
2237
2238 if (Opc == ARM::t2BFic) {
2239 const MCExpr *ElseLabel = MCSymbolRefExpr::create(
2240 getBFLabel(DL.getInternalSymbolPrefix(), getFunctionNumber(),
2241 MI->getOperand(2).getIndex(), OutContext),
2242 OutContext);
2243 MCInst.addExpr(ElseLabel);
2244 MCInst.addImm(MI->getOperand(3).getImm());
2245 } else {
2246 MCInst.addImm(MI->getOperand(2).getImm())
2247 .addReg(MI->getOperand(3).getReg());
2248 }
2249
2251 return;
2252 }
2253 case ARM::t2BF_LabelPseudo: {
2254 // This is a pseudo op for a label used by a branch future instruction
2255
2256 // Emit the label.
2257 OutStreamer->emitLabel(
2258 getBFLabel(DL.getInternalSymbolPrefix(), getFunctionNumber(),
2259 MI->getOperand(0).getIndex(), OutContext));
2260 return;
2261 }
2262 case ARM::tPICADD: {
2263 // This is a pseudo op for a label + instruction sequence, which looks like:
2264 // LPC0:
2265 // add r0, pc
2266 // This adds the address of LPC0 to r0.
2267
2268 // Emit the label.
2269 OutStreamer->emitLabel(getPICLabel(DL.getInternalSymbolPrefix(),
2271 MI->getOperand(2).getImm(), OutContext));
2272
2273 // Form and emit the add.
2275 .addReg(MI->getOperand(0).getReg())
2276 .addReg(MI->getOperand(0).getReg())
2277 .addReg(ARM::PC)
2278 // Add predicate operands.
2280 .addReg(0));
2281 return;
2282 }
2283 case ARM::PICADD: {
2284 // This is a pseudo op for a label + instruction sequence, which looks like:
2285 // LPC0:
2286 // add r0, pc, r0
2287 // This adds the address of LPC0 to r0.
2288
2289 // Emit the label.
2290 OutStreamer->emitLabel(getPICLabel(DL.getInternalSymbolPrefix(),
2292 MI->getOperand(2).getImm(), OutContext));
2293
2294 // Form and emit the add.
2296 .addReg(MI->getOperand(0).getReg())
2297 .addReg(ARM::PC)
2298 .addReg(MI->getOperand(1).getReg())
2299 // Add predicate operands.
2300 .addImm(MI->getOperand(3).getImm())
2301 .addReg(MI->getOperand(4).getReg())
2302 // Add 's' bit operand (always reg0 for this)
2303 .addReg(0));
2304 return;
2305 }
2306 case ARM::PICSTR:
2307 case ARM::PICSTRB:
2308 case ARM::PICSTRH:
2309 case ARM::PICLDR:
2310 case ARM::PICLDRB:
2311 case ARM::PICLDRH:
2312 case ARM::PICLDRSB:
2313 case ARM::PICLDRSH: {
2314 // This is a pseudo op for a label + instruction sequence, which looks like:
2315 // LPC0:
2316 // OP r0, [pc, r0]
2317 // The LCP0 label is referenced by a constant pool entry in order to get
2318 // a PC-relative address at the ldr instruction.
2319
2320 // Emit the label.
2321 OutStreamer->emitLabel(getPICLabel(DL.getInternalSymbolPrefix(),
2323 MI->getOperand(2).getImm(), OutContext));
2324
2325 // Form and emit the load
2326 unsigned Opcode;
2327 switch (MI->getOpcode()) {
2328 default:
2329 llvm_unreachable("Unexpected opcode!");
2330 case ARM::PICSTR: Opcode = ARM::STRrs; break;
2331 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
2332 case ARM::PICSTRH: Opcode = ARM::STRH; break;
2333 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
2334 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
2335 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
2336 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
2337 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
2338 }
2340 .addReg(MI->getOperand(0).getReg())
2341 .addReg(ARM::PC)
2342 .addReg(MI->getOperand(1).getReg())
2343 .addImm(0)
2344 // Add predicate operands.
2345 .addImm(MI->getOperand(3).getImm())
2346 .addReg(MI->getOperand(4).getReg()));
2347
2348 return;
2349 }
2350 case ARM::CONSTPOOL_ENTRY: {
2351 assert(!STI.genExecuteOnly() &&
2352 "execute-only should not generate constant pools");
2353
2354 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
2355 /// in the function. The first operand is the ID# for this instruction, the
2356 /// second is the index into the MachineConstantPool that this is, the third
2357 /// is the size in bytes of this constant pool entry.
2358 /// The required alignment is specified on the basic block holding this MI.
2359 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
2360 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
2361
2362 // If this is the first entry of the pool, mark it.
2363 if (!InConstantPool) {
2364 OutStreamer->emitDataRegion(MCDR_DataRegion);
2365 InConstantPool = true;
2366 }
2367
2368 OutStreamer->emitLabel(GetCPISymbol(LabelId));
2369
2370 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
2371 if (MCPE.isMachineConstantPoolEntry())
2373 else
2375 return;
2376 }
2377 case ARM::JUMPTABLE_ADDRS:
2379 return;
2380 case ARM::JUMPTABLE_INSTS:
2382 return;
2383 case ARM::JUMPTABLE_TBB:
2384 case ARM::JUMPTABLE_TBH:
2385 emitJumpTableTBInst(MI, MI->getOpcode() == ARM::JUMPTABLE_TBB ? 1 : 2);
2386 return;
2387 case ARM::t2BR_JT: {
2389 .addReg(ARM::PC)
2390 .addReg(MI->getOperand(0).getReg())
2391 // Add predicate operands.
2393 .addReg(0));
2394 return;
2395 }
2396 case ARM::t2TBB_JT:
2397 case ARM::t2TBH_JT: {
2398 unsigned Opc = MI->getOpcode() == ARM::t2TBB_JT ? ARM::t2TBB : ARM::t2TBH;
2399 // Lower and emit the PC label, then the instruction itself.
2400 OutStreamer->emitLabel(GetCPISymbol(MI->getOperand(3).getImm()));
2402 .addReg(MI->getOperand(0).getReg())
2403 .addReg(MI->getOperand(1).getReg())
2404 // Add predicate operands.
2406 .addReg(0));
2407 return;
2408 }
2409 case ARM::tTBB_JT:
2410 case ARM::tTBH_JT: {
2411
2412 bool Is8Bit = MI->getOpcode() == ARM::tTBB_JT;
2413 Register Base = MI->getOperand(0).getReg();
2414 Register Idx = MI->getOperand(1).getReg();
2415 assert(MI->getOperand(1).isKill() && "We need the index register as scratch!");
2416
2417 // Multiply up idx if necessary.
2418 if (!Is8Bit)
2420 .addReg(Idx)
2421 .addReg(ARM::CPSR)
2422 .addReg(Idx)
2423 .addImm(1)
2424 // Add predicate operands.
2425 .addImm(ARMCC::AL)
2426 .addReg(0));
2427
2428 if (Base == ARM::PC) {
2429 // TBB [base, idx] =
2430 // ADDS idx, idx, base
2431 // LDRB idx, [idx, #4] ; or LDRH if TBH
2432 // LSLS idx, #1
2433 // ADDS pc, pc, idx
2434
2435 // When using PC as the base, it's important that there is no padding
2436 // between the last ADDS and the start of the jump table. The jump table
2437 // is 4-byte aligned, so we ensure we're 4 byte aligned here too.
2438 //
2439 // FIXME: Ideally we could vary the LDRB index based on the padding
2440 // between the sequence and jump table, however that relies on MCExprs
2441 // for load indexes which are currently not supported.
2442 OutStreamer->emitCodeAlignment(Align(4), getSubtargetInfo());
2444 .addReg(Idx)
2445 .addReg(Idx)
2446 .addReg(Base)
2447 // Add predicate operands.
2448 .addImm(ARMCC::AL)
2449 .addReg(0));
2450
2451 unsigned Opc = Is8Bit ? ARM::tLDRBi : ARM::tLDRHi;
2453 .addReg(Idx)
2454 .addReg(Idx)
2455 .addImm(Is8Bit ? 4 : 2)
2456 // Add predicate operands.
2457 .addImm(ARMCC::AL)
2458 .addReg(0));
2459 } else {
2460 // TBB [base, idx] =
2461 // LDRB idx, [base, idx] ; or LDRH if TBH
2462 // LSLS idx, #1
2463 // ADDS pc, pc, idx
2464
2465 unsigned Opc = Is8Bit ? ARM::tLDRBr : ARM::tLDRHr;
2467 .addReg(Idx)
2468 .addReg(Base)
2469 .addReg(Idx)
2470 // Add predicate operands.
2471 .addImm(ARMCC::AL)
2472 .addReg(0));
2473 }
2474
2476 .addReg(Idx)
2477 .addReg(ARM::CPSR)
2478 .addReg(Idx)
2479 .addImm(1)
2480 // Add predicate operands.
2481 .addImm(ARMCC::AL)
2482 .addReg(0));
2483
2484 OutStreamer->emitLabel(GetCPISymbol(MI->getOperand(3).getImm()));
2486 .addReg(ARM::PC)
2487 .addReg(ARM::PC)
2488 .addReg(Idx)
2489 // Add predicate operands.
2490 .addImm(ARMCC::AL)
2491 .addReg(0));
2492 return;
2493 }
2494 case ARM::tBR_JTr:
2495 case ARM::BR_JTr: {
2496 // mov pc, target
2497 MCInst TmpInst;
2498 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
2499 ARM::MOVr : ARM::tMOVr;
2500 TmpInst.setOpcode(Opc);
2501 TmpInst.addOperand(MCOperand::createReg(ARM::PC));
2502 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
2503 // Add predicate operands.
2505 TmpInst.addOperand(MCOperand::createReg(0));
2506 // Add 's' bit operand (always reg0 for this)
2507 if (Opc == ARM::MOVr)
2508 TmpInst.addOperand(MCOperand::createReg(0));
2509 EmitToStreamer(*OutStreamer, TmpInst);
2510 return;
2511 }
2512 case ARM::BR_JTm_i12: {
2513 // ldr pc, target
2514 MCInst TmpInst;
2515 TmpInst.setOpcode(ARM::LDRi12);
2516 TmpInst.addOperand(MCOperand::createReg(ARM::PC));
2517 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
2518 TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm()));
2519 // Add predicate operands.
2521 TmpInst.addOperand(MCOperand::createReg(0));
2522 EmitToStreamer(*OutStreamer, TmpInst);
2523 return;
2524 }
2525 case ARM::BR_JTm_rs: {
2526 // ldr pc, target
2527 MCInst TmpInst;
2528 TmpInst.setOpcode(ARM::LDRrs);
2529 TmpInst.addOperand(MCOperand::createReg(ARM::PC));
2530 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
2531 TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
2532 TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm()));
2533 // Add predicate operands.
2535 TmpInst.addOperand(MCOperand::createReg(0));
2536 EmitToStreamer(*OutStreamer, TmpInst);
2537 return;
2538 }
2539 case ARM::BR_JTadd: {
2540 // add pc, target, idx
2542 .addReg(ARM::PC)
2543 .addReg(MI->getOperand(0).getReg())
2544 .addReg(MI->getOperand(1).getReg())
2545 // Add predicate operands.
2547 .addReg(0)
2548 // Add 's' bit operand (always reg0 for this)
2549 .addReg(0));
2550 return;
2551 }
2552 case ARM::SPACE:
2553 OutStreamer->emitZeros(MI->getOperand(1).getImm());
2554 return;
2555 case ARM::TRAP: {
2556 // Non-Darwin binutils don't yet support the "trap" mnemonic.
2557 // FIXME: Remove this special case when they do.
2558 if (!TM.getTargetTriple().isOSBinFormatMachO()) {
2559 uint32_t Val = 0xe7ffdefeUL;
2560 OutStreamer->AddComment("trap");
2561 ATS.emitInst(Val);
2562 return;
2563 }
2564 break;
2565 }
2566 case ARM::tTRAP: {
2567 // Non-Darwin binutils don't yet support the "trap" mnemonic.
2568 // FIXME: Remove this special case when they do.
2569 if (!TM.getTargetTriple().isOSBinFormatMachO()) {
2570 uint16_t Val = 0xdefe;
2571 OutStreamer->AddComment("trap");
2572 ATS.emitInst(Val, 'n');
2573 return;
2574 }
2575 break;
2576 }
2577 case ARM::t2Int_eh_sjlj_setjmp:
2578 case ARM::t2Int_eh_sjlj_setjmp_nofp:
2579 case ARM::tInt_eh_sjlj_setjmp: {
2580 // Two incoming args: GPR:$src, GPR:$val
2581 // mov $val, pc
2582 // adds $val, #7
2583 // str $val, [$src, #4]
2584 // movs r0, #0
2585 // b LSJLJEH
2586 // movs r0, #1
2587 // LSJLJEH:
2588 Register SrcReg = MI->getOperand(0).getReg();
2589 Register ValReg = MI->getOperand(1).getReg();
2590 MCSymbol *Label = OutContext.createTempSymbol("SJLJEH");
2591 OutStreamer->AddComment("eh_setjmp begin");
2593 .addReg(ValReg)
2594 .addReg(ARM::PC)
2595 // Predicate.
2596 .addImm(ARMCC::AL)
2597 .addReg(0));
2598
2600 .addReg(ValReg)
2601 // 's' bit operand
2602 .addReg(ARM::CPSR)
2603 .addReg(ValReg)
2604 .addImm(7)
2605 // Predicate.
2606 .addImm(ARMCC::AL)
2607 .addReg(0));
2608
2610 .addReg(ValReg)
2611 .addReg(SrcReg)
2612 // The offset immediate is #4. The operand value is scaled by 4 for the
2613 // tSTR instruction.
2614 .addImm(1)
2615 // Predicate.
2616 .addImm(ARMCC::AL)
2617 .addReg(0));
2618
2620 .addReg(ARM::R0)
2621 .addReg(ARM::CPSR)
2622 .addImm(0)
2623 // Predicate.
2624 .addImm(ARMCC::AL)
2625 .addReg(0));
2626
2627 const MCExpr *SymbolExpr = MCSymbolRefExpr::create(Label, OutContext);
2629 .addExpr(SymbolExpr)
2630 .addImm(ARMCC::AL)
2631 .addReg(0));
2632
2633 OutStreamer->AddComment("eh_setjmp end");
2635 .addReg(ARM::R0)
2636 .addReg(ARM::CPSR)
2637 .addImm(1)
2638 // Predicate.
2639 .addImm(ARMCC::AL)
2640 .addReg(0));
2641
2642 OutStreamer->emitLabel(Label);
2643 return;
2644 }
2645
2646 case ARM::Int_eh_sjlj_setjmp_nofp:
2647 case ARM::Int_eh_sjlj_setjmp: {
2648 // Two incoming args: GPR:$src, GPR:$val
2649 // add $val, pc, #8
2650 // str $val, [$src, #+4]
2651 // mov r0, #0
2652 // add pc, pc, #0
2653 // mov r0, #1
2654 Register SrcReg = MI->getOperand(0).getReg();
2655 Register ValReg = MI->getOperand(1).getReg();
2656
2657 OutStreamer->AddComment("eh_setjmp begin");
2659 .addReg(ValReg)
2660 .addReg(ARM::PC)
2661 .addImm(8)
2662 // Predicate.
2663 .addImm(ARMCC::AL)
2664 .addReg(0)
2665 // 's' bit operand (always reg0 for this).
2666 .addReg(0));
2667
2669 .addReg(ValReg)
2670 .addReg(SrcReg)
2671 .addImm(4)
2672 // Predicate.
2673 .addImm(ARMCC::AL)
2674 .addReg(0));
2675
2677 .addReg(ARM::R0)
2678 .addImm(0)
2679 // Predicate.
2680 .addImm(ARMCC::AL)
2681 .addReg(0)
2682 // 's' bit operand (always reg0 for this).
2683 .addReg(0));
2684
2686 .addReg(ARM::PC)
2687 .addReg(ARM::PC)
2688 .addImm(0)
2689 // Predicate.
2690 .addImm(ARMCC::AL)
2691 .addReg(0)
2692 // 's' bit operand (always reg0 for this).
2693 .addReg(0));
2694
2695 OutStreamer->AddComment("eh_setjmp end");
2697 .addReg(ARM::R0)
2698 .addImm(1)
2699 // Predicate.
2700 .addImm(ARMCC::AL)
2701 .addReg(0)
2702 // 's' bit operand (always reg0 for this).
2703 .addReg(0));
2704 return;
2705 }
2706 case ARM::Int_eh_sjlj_longjmp: {
2707 // ldr sp, [$src, #8]
2708 // ldr $scratch, [$src, #4]
2709 // ldr r7, [$src]
2710 // bx $scratch
2711 Register SrcReg = MI->getOperand(0).getReg();
2712 Register ScratchReg = MI->getOperand(1).getReg();
2714 .addReg(ARM::SP)
2715 .addReg(SrcReg)
2716 .addImm(8)
2717 // Predicate.
2718 .addImm(ARMCC::AL)
2719 .addReg(0));
2720
2722 .addReg(ScratchReg)
2723 .addReg(SrcReg)
2724 .addImm(4)
2725 // Predicate.
2726 .addImm(ARMCC::AL)
2727 .addReg(0));
2728
2729 if (STI.isTargetDarwin() || STI.isTargetWindows()) {
2730 // These platforms always use the same frame register
2732 .addReg(STI.getFramePointerReg())
2733 .addReg(SrcReg)
2734 .addImm(0)
2735 // Predicate.
2737 .addReg(0));
2738 } else {
2739 // If the calling code might use either R7 or R11 as
2740 // frame pointer register, restore it into both.
2742 .addReg(ARM::R7)
2743 .addReg(SrcReg)
2744 .addImm(0)
2745 // Predicate.
2746 .addImm(ARMCC::AL)
2747 .addReg(0));
2749 .addReg(ARM::R11)
2750 .addReg(SrcReg)
2751 .addImm(0)
2752 // Predicate.
2753 .addImm(ARMCC::AL)
2754 .addReg(0));
2755 }
2756
2757 assert(STI.hasV4TOps());
2759 .addReg(ScratchReg)
2760 // Predicate.
2761 .addImm(ARMCC::AL)
2762 .addReg(0));
2763 return;
2764 }
2765 case ARM::tInt_eh_sjlj_longjmp: {
2766 // ldr $scratch, [$src, #8]
2767 // mov sp, $scratch
2768 // ldr $scratch, [$src, #4]
2769 // ldr r7, [$src]
2770 // bx $scratch
2771 Register SrcReg = MI->getOperand(0).getReg();
2772 Register ScratchReg = MI->getOperand(1).getReg();
2773
2775 .addReg(ScratchReg)
2776 .addReg(SrcReg)
2777 // The offset immediate is #8. The operand value is scaled by 4 for the
2778 // tLDR instruction.
2779 .addImm(2)
2780 // Predicate.
2781 .addImm(ARMCC::AL)
2782 .addReg(0));
2783
2785 .addReg(ARM::SP)
2786 .addReg(ScratchReg)
2787 // Predicate.
2788 .addImm(ARMCC::AL)
2789 .addReg(0));
2790
2792 .addReg(ScratchReg)
2793 .addReg(SrcReg)
2794 .addImm(1)
2795 // Predicate.
2796 .addImm(ARMCC::AL)
2797 .addReg(0));
2798
2799 if (STI.isTargetDarwin() || STI.isTargetWindows()) {
2800 // These platforms always use the same frame register
2802 .addReg(STI.getFramePointerReg())
2803 .addReg(SrcReg)
2804 .addImm(0)
2805 // Predicate.
2807 .addReg(0));
2808 } else {
2809 // If the calling code might use either R7 or R11 as
2810 // frame pointer register, restore it into both.
2812 .addReg(ARM::R7)
2813 .addReg(SrcReg)
2814 .addImm(0)
2815 // Predicate.
2816 .addImm(ARMCC::AL)
2817 .addReg(0));
2819 .addReg(ARM::R11)
2820 .addReg(SrcReg)
2821 .addImm(0)
2822 // Predicate.
2823 .addImm(ARMCC::AL)
2824 .addReg(0));
2825 }
2826
2828 .addReg(ScratchReg)
2829 // Predicate.
2830 .addImm(ARMCC::AL)
2831 .addReg(0));
2832 return;
2833 }
2834 case ARM::tInt_WIN_eh_sjlj_longjmp: {
2835 // ldr.w r11, [$src, #0]
2836 // ldr.w sp, [$src, #8]
2837 // ldr.w pc, [$src, #4]
2838
2839 Register SrcReg = MI->getOperand(0).getReg();
2840
2842 .addReg(ARM::R11)
2843 .addReg(SrcReg)
2844 .addImm(0)
2845 // Predicate
2846 .addImm(ARMCC::AL)
2847 .addReg(0));
2849 .addReg(ARM::SP)
2850 .addReg(SrcReg)
2851 .addImm(8)
2852 // Predicate
2853 .addImm(ARMCC::AL)
2854 .addReg(0));
2856 .addReg(ARM::PC)
2857 .addReg(SrcReg)
2858 .addImm(4)
2859 // Predicate
2860 .addImm(ARMCC::AL)
2861 .addReg(0));
2862 return;
2863 }
2864 case ARM::PATCHABLE_FUNCTION_ENTER:
2866 return;
2867 case ARM::PATCHABLE_FUNCTION_EXIT:
2869 return;
2870 case ARM::PATCHABLE_TAIL_CALL:
2872 return;
2873 case ARM::SpeculationBarrierISBDSBEndBB: {
2874 // Print DSB SYS + ISB
2875 MCInst TmpInstDSB;
2876 TmpInstDSB.setOpcode(ARM::DSB);
2877 TmpInstDSB.addOperand(MCOperand::createImm(0xf));
2878 EmitToStreamer(*OutStreamer, TmpInstDSB);
2879 MCInst TmpInstISB;
2880 TmpInstISB.setOpcode(ARM::ISB);
2881 TmpInstISB.addOperand(MCOperand::createImm(0xf));
2882 EmitToStreamer(*OutStreamer, TmpInstISB);
2883 return;
2884 }
2885 case ARM::t2SpeculationBarrierISBDSBEndBB: {
2886 // Print DSB SYS + ISB
2887 MCInst TmpInstDSB;
2888 TmpInstDSB.setOpcode(ARM::t2DSB);
2889 TmpInstDSB.addOperand(MCOperand::createImm(0xf));
2891 TmpInstDSB.addOperand(MCOperand::createReg(0));
2892 EmitToStreamer(*OutStreamer, TmpInstDSB);
2893 MCInst TmpInstISB;
2894 TmpInstISB.setOpcode(ARM::t2ISB);
2895 TmpInstISB.addOperand(MCOperand::createImm(0xf));
2897 TmpInstISB.addOperand(MCOperand::createReg(0));
2898 EmitToStreamer(*OutStreamer, TmpInstISB);
2899 return;
2900 }
2901 case ARM::SpeculationBarrierSBEndBB: {
2902 // Print SB
2903 MCInst TmpInstSB;
2904 TmpInstSB.setOpcode(ARM::SB);
2905 EmitToStreamer(*OutStreamer, TmpInstSB);
2906 return;
2907 }
2908 case ARM::t2SpeculationBarrierSBEndBB: {
2909 // Print SB
2910 MCInst TmpInstSB;
2911 TmpInstSB.setOpcode(ARM::t2SB);
2912 EmitToStreamer(*OutStreamer, TmpInstSB);
2913 return;
2914 }
2915
2916 case ARM::SEH_StackAlloc:
2917 ATS.emitARMWinCFIAllocStack(MI->getOperand(0).getImm(),
2918 MI->getOperand(1).getImm());
2919 return;
2920
2921 case ARM::SEH_SaveRegs:
2922 case ARM::SEH_SaveRegs_Ret:
2923 ATS.emitARMWinCFISaveRegMask(MI->getOperand(0).getImm(),
2924 MI->getOperand(1).getImm());
2925 return;
2926
2927 case ARM::SEH_SaveSP:
2928 ATS.emitARMWinCFISaveSP(MI->getOperand(0).getImm());
2929 return;
2930
2931 case ARM::SEH_SaveFRegs:
2932 ATS.emitARMWinCFISaveFRegs(MI->getOperand(0).getImm(),
2933 MI->getOperand(1).getImm());
2934 return;
2935
2936 case ARM::SEH_SaveLR:
2937 ATS.emitARMWinCFISaveLR(MI->getOperand(0).getImm());
2938 return;
2939
2940 case ARM::SEH_Nop:
2941 case ARM::SEH_Nop_Ret:
2942 ATS.emitARMWinCFINop(MI->getOperand(0).getImm());
2943 return;
2944
2945 case ARM::SEH_PrologEnd:
2946 ATS.emitARMWinCFIPrologEnd(/*Fragment=*/false);
2947 return;
2948
2949 case ARM::SEH_EpilogStart:
2951 return;
2952
2953 case ARM::SEH_EpilogEnd:
2955 return;
2956 }
2957
2958 MCInst TmpInst;
2959 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
2960
2961 EmitToStreamer(*OutStreamer, TmpInst);
2962}
2963
2964char ARMAsmPrinter::ID = 0;
2965
2966INITIALIZE_PASS(ARMAsmPrinter, "arm-asm-printer", "ARM Assembly Printer", false,
2967 false)
2968
2969//===----------------------------------------------------------------------===//
2970// Target Registry Stuff
2971//===----------------------------------------------------------------------===//
2972
2973// Force static initialization.
2974extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
2975LLVMInitializeARMAsmPrinter() {
2980}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isRegisterLiveInCall(const MachineInstr &Call, MCRegister Reg)
static void emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel, MachineModuleInfoImpl::StubValueTy &MCSym)
static uint8_t getModifierSpecifier(ARMCP::ARMCPModifier Modifier)
static MCSymbol * getPICLabel(StringRef Prefix, unsigned FunctionNumber, unsigned LabelId, MCContext &Ctx)
static bool checkDenormalAttributeInconsistency(const Module &M)
static bool checkDenormalAttributeConsistency(const Module &M, DenormalFPEnv Value)
static bool checkFunctionsAttributeConsistency(const Module &M, StringRef Attr, StringRef Value)
static bool isThumb(const MCSubtargetInfo &STI)
static MCSymbol * getBFLabel(StringRef Prefix, unsigned FunctionNumber, unsigned LabelId, MCContext &Ctx)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static RegisterPass< DebugifyModulePass > DM("debugify", "Attach debug info to everything")
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
#define F(x, y, z)
Definition MD5.cpp:54
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
This file defines the SmallString class.
std::unique_ptr< MCStreamer > && Streamer
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static const unsigned FramePtr
void emitJumpTableAddrs(const MachineInstr *MI)
void emitJumpTableTBInst(const MachineInstr *MI, unsigned OffsetWidth)
void emitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This uses the emitInstruction() method to print assembly for each instruction.
MCSymbol * GetCPISymbol(unsigned CPID) const override
Return the symbol for the specified constant pool entry.
void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O)
void emitStartOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the start of their fi...
ARMAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
void emitFunctionEntryLabel() override
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
void LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI)
void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override
EmitMachineConstantPoolValue - Print a machine constantpool value to the .s file.
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
void emitXXStructor(const DataLayout &DL, const Constant *CV) override
Targets can override this to change how global constants that are part of a C++ static/global constru...
void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI)
void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI)
void emitEndOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the end of their file...
std::tuple< const MCSymbol *, uint64_t, const MCSymbol *, codeview::JumpTableEntrySize > getCodeViewJumpTableInfo(int JTI, const MachineInstr *BranchInstr, const MCSymbol *BranchLabel) const override
Gets information required to create a CodeView debug symbol for a jump table.
void emitJumpTableInsts(const MachineInstr *MI)
const ARMBaseTargetMachine & getTM() const
void emitGlobalVariable(const GlobalVariable *GV) override
Emit the specified global variable to the .s file.
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
void emitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override
Print the MachineOperand as a symbol.
void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, const MCSubtargetInfo *EndInfo, const MachineInstr *MI) override
Let the target do anything it needs to do after emitting inlineasm.
void LowerKCFI_CHECK(const MachineInstr &MI)
void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override
ARM::ARMABI getEffectiveABI(const Module &M) const
Returns the ABI in effect for M: the "target-abi" module flag if present, otherwise the legacy -targe...
bool isGVIndirectSymbol(const GlobalValue *GV) const
FloatABI::ABIType getFloatABI(const Module &M) const
Returns the floating-point ABI in effect for M: the "float-abi" module flag if present,...
ARMConstantPoolValue - ARM specific constantpool value.
unsigned char getPCAdjustment() const
ARMCP::ARMCPModifier getModifier() const
ARMFunctionInfo - This class is derived from MachineFunctionInfo and contains private ARM-specific in...
static const char * getRegisterName(MCRegister Reg, unsigned AltIdx=ARM::NoRegAltName)
bool isThumb1Only() const
MCPhysReg getFramePointerReg() const
bool isTargetWindows() const
bool isTargetDarwin() const
void emitTargetAttributes(const MCSubtargetInfo &STI)
Emit the build attributes that only depend on the hardware that we expect.
virtual void emitSetFP(MCRegister FpReg, MCRegister SpReg, int64_t Offset=0)
virtual void finishAttributeSection()
virtual void emitMovSP(MCRegister Reg, int64_t Offset=0)
virtual void emitARMWinCFISaveSP(unsigned Reg)
virtual void emitInst(uint32_t Inst, char Suffix='\0')
virtual void emitARMWinCFISaveLR(unsigned Offset)
virtual void emitTextAttribute(unsigned Attribute, StringRef String)
virtual void emitARMWinCFIAllocStack(unsigned Size, bool Wide)
virtual void emitARMWinCFISaveRegMask(unsigned Mask, bool Wide)
virtual void emitRegSave(const SmallVectorImpl< MCRegister > &RegList, bool isVector)
virtual void emitARMWinCFIEpilogEnd()
virtual void emitARMWinCFIPrologEnd(bool Fragment)
virtual void switchVendor(StringRef Vendor)
virtual void emitARMWinCFISaveFRegs(unsigned First, unsigned Last)
virtual void emitARMWinCFIEpilogStart(unsigned Condition)
virtual void emitPad(int64_t Offset)
virtual void emitAttribute(unsigned Attribute, unsigned Value)
virtual void emitARMWinCFINop(bool Wide)
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
MCSymbol * getSymbolWithGlobalValueBase(const GlobalValue *GV, StringRef Suffix) const
Return the MCSymbol for a private symbol with global value name as its base, with the specified suffi...
MCSymbol * getSymbol(const GlobalValue *GV) const
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
virtual void emitGlobalVariable(const GlobalVariable *GV)
Emit the specified global variable to the .s file.
TargetMachine & TM
Target machine description.
Definition AsmPrinter.h:94
void emitXRayTable()
Emit a table with all XRay instrumentation points.
virtual void emitGlobalAlias(const Module &M, const GlobalAlias &GA)
Align emitAlignment(Align Alignment, const GlobalObject *GV=nullptr, unsigned MaxBytesToEmit=0) const
Emit an alignment directive to the specified power of two boundary.
MCSymbol * getMBBExceptionSym(const MachineBasicBlock &MBB)
MachineFunction * MF
The current machine function.
Definition AsmPrinter.h:109
virtual void SetupMachineFunction(MachineFunction &MF)
This should be called when a new MachineFunction is being processed from runOnMachineFunction.
void emitFunctionBody()
This method emits the body and trailer for a function.
virtual void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const
This emits linkage information about GVSym based on GV, if this is supported by the target.
unsigned getFunctionNumber() const
Return a unique ID for the current function.
AsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer, char &ID=AsmPrinter::ID)
void printOffset(int64_t Offset, raw_ostream &OS) const
This is just convenient handler for printing offsets.
void emitGlobalConstant(const DataLayout &DL, const Constant *CV, AliasMapTy *AliasList=nullptr)
EmitGlobalConstant - Print a general LLVM constant to the .s file.
MCSymbol * getSymbolPreferLocal(const GlobalValue &GV) const
Similar to getSymbol() but preferred for references.
MCSymbol * CurrentFnSym
The symbol for the current function.
Definition AsmPrinter.h:128
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition AsmPrinter.h:112
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition AsmPrinter.h:101
bool isPositionIndependent() const
void emitVisibility(MCSymbol *Sym, unsigned Visibility, bool IsDefinition=true) const
This emits visibility information about symbol, if this is supported by the target.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition AsmPrinter.h:106
const MCAsmInfo & MAI
Target Asm Printer information.
Definition AsmPrinter.h:97
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV) const
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
const DataLayout & getDataLayout() const
Return information about data layout.
virtual void emitFunctionEntryLabel()
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
MCSymbol * GetExternalSymbolSymbol(const Twine &Sym) const
Return the MCSymbol for the specified ExternalSymbol.
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
The address of a basic block.
Definition Constants.h:1088
This is an important base class in LLVM.
Definition Constant.h:43
const Constant * stripPointerCasts() const
Definition Constant.h:233
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:730
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:730
bool isDSOLocal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
VisibilityTypes getVisibility() const
bool hasInternalLinkage() const
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:342
static const MCBinaryExpr * createDiv(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:352
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:427
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
MCInstBuilder & addReg(MCRegister Reg)
Add a new register operand.
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
MCInstBuilder & addExpr(const MCExpr *Val)
Add a new MCExpr operand.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
MCSection * getThreadLocalPointerSection() const
MCSection * getNonLazySymbolPointerSection() const
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Streaming machine code generation interface.
Definition MCStreamer.h:222
virtual bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
Add the given Attribute to Symbol.
MCContext & getContext() const
Definition MCStreamer.h:326
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers.
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition MCSymbol.cpp:59
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
Target specific streamer interface.
Definition MCStreamer.h:95
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
This class is a data container for one entry in a MachineConstantPool.
union llvm::MachineConstantPoolEntry::@004270020304201266316354007027341142157160323045 Val
The constant itself.
bool isMachineConstantPoolEntry() const
isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry is indeed a target specific ...
MachineConstantPoolValue * MachineCPVal
Abstract base class for all machine specific constantpool value subclasses.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
const std::vector< MachineJumpTableEntry > & getJumpTables() const
StubValueTy & getGVStubEntry(MCSymbol *Sym)
std::vector< std::pair< MCSymbol *, StubValueTy > > SymbolListTy
PointerIntPair< MCSymbol *, 1, bool > StubValueTy
MachineModuleInfoMachO - This is a MachineModuleInfoImpl implementation for MachO targets.
StubValueTy & getGVStubEntry(MCSymbol *Sym)
StubValueTy & getThreadLocalGVStubEntry(MCSymbol *Sym)
SymbolListTy GetGVStubList()
Accessor methods to return the set of stubs in sorted order.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
int64_t getOffset() const
Return the offset from the symbol in this operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
Pass(PassKind K, char &pid)
Definition Pass.h:105
IntType getInt() const
PointerTy getPointer() const
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represents a location in source code.
Definition SMLoc.h:22
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Primary interface to the complete machine description for the target machine.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TypeSize getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SECREL
Thread Pointer Offset.
@ GOT_PREL
Thread Local Storage (General Dynamic Mode)
@ SBREL
Section Relative (Windows TLS)
@ GOTTPOFF
Global Offset Table, PC Relative.
@ TPOFF
Global Offset Table, Thread Pointer Offset.
@ MO_LO16
MO_LO16 - On a symbol operand, this represents a relocation containing lower 16 bit of the address.
@ MO_LO_0_7
MO_LO_0_7 - On a symbol operand, this represents a relocation containing bits 0 through 7 of the addr...
@ MO_LO_8_15
MO_LO_8_15 - On a symbol operand, this represents a relocation containing bits 8 through 15 of the ad...
@ MO_NONLAZY
MO_NONLAZY - This is an independent flag, on a symbol operand "FOO" it represents a symbol which,...
@ MO_HI_8_15
MO_HI_8_15 - On a symbol operand, this represents a relocation containing bits 24 through 31 of the a...
@ MO_HI16
MO_HI16 - On a symbol operand, this represents a relocation containing higher 16 bit of the address.
@ MO_DLLIMPORT
MO_DLLIMPORT - On a symbol operand, this represents that the reference to the symbol is for an import...
@ MO_HI_0_7
MO_HI_0_7 - On a symbol operand, this represents a relocation containing bits 16 through 23 of the ad...
@ MO_COFFSTUB
MO_COFFSTUB - On a symbol operand "FOO", this indicates that the reference is actually to the "....
int getSOImmVal(unsigned Arg)
getSOImmVal - Given a 32-bit immediate, if it is something that can fit into an shifter_operand immed...
int getT2SOImmVal(unsigned Arg)
getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit into a Thumb-2 shifter_oper...
std::string ParseARMTriple(const Triple &TT, StringRef CPU)
const MCSpecifierExpr * createLower16(const MCExpr *Expr, MCContext &Ctx)
const MCSpecifierExpr * createUpper16(const MCExpr *Expr, MCContext &Ctx)
SymbolStorageClass
Storage class tells where and what the symbol represents.
Definition COFF.h:218
@ IMAGE_SYM_CLASS_EXTERNAL
External symbol.
Definition COFF.h:224
@ IMAGE_SYM_CLASS_STATIC
Static.
Definition COFF.h:225
@ IMAGE_SYM_DTYPE_FUNCTION
A function that returns a base type.
Definition COFF.h:276
@ SCT_COMPLEX_TYPE_SHIFT
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
Definition COFF.h:280
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:683
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Target & getTheThumbBETarget()
@ MCDR_DataRegionEnd
.end_data_region
@ MCDR_DataRegion
.data_region
@ MCDR_DataRegionJT8
.data_region jt8
@ MCDR_DataRegionJT32
.data_region jt32
@ MCDR_DataRegionJT16
.data_region jt16
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void LowerARMMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, ARMAsmPrinter &AP)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Target & getTheARMLETarget()
unsigned convertAddSubFlagsOpcode(unsigned OldOpc)
Map pseudo instructions that imply an 'S' bit onto real opcodes.
@ MCSA_IndirectSymbol
.indirect_symbol (MachO)
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
Target & getTheARMBETarget()
Target & getTheThumbLETarget()
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Represents the full denormal controls for a function, including the default mode and the f32 specific...
static constexpr DenormalMode getPositiveZero()
static constexpr DenormalMode getPreserveSign()
static constexpr DenormalMode getIEEE()
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...