blob: 03d788afb3fd4ca4699ac5682f55ad3b955f0723 [file] [log] [blame]
Chris Lattner29233c02010-04-07 18:10:38 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
Chris Lattner6a8e0f52004-08-16 23:15:22 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner6a8e0f52004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Eugene Zelenkod96089b2017-02-14 00:33:36 +000014#include "AsmPrinterHandler.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000015#include "CodeViewDebug.h"
Chris Lattnerf0d6bd32010-04-05 05:11:15 +000016#include "DwarfDebug.h"
17#include "DwarfException.h"
Reid Kleckner60b640b2015-05-28 22:47:01 +000018#include "WinException.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000019#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/ADT/Statistic.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000026#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/ADT/Triple.h"
29#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Analysis/ConstantFolding.h"
Rafael Espindolaf21434c2014-07-30 19:42:16 +000031#include "llvm/CodeGen/Analysis.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000032#include "llvm/CodeGen/AsmPrinter.h"
33#include "llvm/CodeGen/GCMetadata.h"
Gordon Henriksend930f912008-08-17 18:44:35 +000034#include "llvm/CodeGen/GCMetadataPrinter.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000035#include "llvm/CodeGen/GCStrategy.h"
36#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000037#include "llvm/CodeGen/MachineConstantPool.h"
David Greene659c1a92009-11-13 21:34:57 +000038#include "llvm/CodeGen/MachineFrameInfo.h"
David Greene60eb4732009-08-18 19:22:55 +000039#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000040#include "llvm/CodeGen/MachineFunctionPass.h"
41#include "llvm/CodeGen/MachineInstr.h"
Jakob Stoklund Olesen1995b9f2014-01-12 19:24:08 +000042#include "llvm/CodeGen/MachineInstrBundle.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000043#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greenef354b6e2009-08-10 16:38:07 +000044#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000045#include "llvm/CodeGen/MachineMemOperand.h"
Rafael Espindolab91455b2015-04-07 13:42:44 +000046#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000047#include "llvm/CodeGen/MachineOperand.h"
48#include "llvm/IR/BasicBlock.h"
49#include "llvm/IR/Constant.h"
50#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000051#include "llvm/IR/DataLayout.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000052#include "llvm/IR/DebugInfoMetadata.h"
53#include "llvm/IR/DerivedTypes.h"
54#include "llvm/IR/Function.h"
55#include "llvm/IR/GlobalAlias.h"
56#include "llvm/IR/GlobalIFunc.h"
57#include "llvm/IR/GlobalIndirectSymbol.h"
58#include "llvm/IR/GlobalObject.h"
59#include "llvm/IR/GlobalValue.h"
60#include "llvm/IR/GlobalVariable.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000061#include "llvm/IR/Mangler.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000062#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000063#include "llvm/IR/Module.h"
64#include "llvm/IR/Operator.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000065#include "llvm/IR/Value.h"
Chris Lattner1e158692010-04-04 18:34:07 +000066#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9d0e7622009-07-27 21:28:04 +000067#include "llvm/MC/MCContext.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000068#include "llvm/MC/MCDirectives.h"
Chris Lattnerd1acffc2010-01-23 06:17:14 +000069#include "llvm/MC/MCExpr.h"
David Greenede9cd442009-07-16 22:24:20 +000070#include "llvm/MC/MCInst.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000071#include "llvm/MC/MCSection.h"
Dean Michael Berrisf7e7b932017-01-03 04:30:21 +000072#include "llvm/MC/MCSectionELF.h"
73#include "llvm/MC/MCSectionMachO.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000074#include "llvm/MC/MCStreamer.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000075#include "llvm/MC/MCSubtargetInfo.h"
76#include "llvm/MC/MCSymbol.h"
77#include "llvm/MC/MCTargetOptions.h"
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +000078#include "llvm/MC/MCValue.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000079#include "llvm/MC/SectionKind.h"
80#include "llvm/Pass.h"
81#include "llvm/Support/Casting.h"
82#include "llvm/Support/Compiler.h"
83#include "llvm/Support/Dwarf.h"
84#include "llvm/Support/ELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000085#include "llvm/Support/ErrorHandling.h"
86#include "llvm/Support/Format.h"
87#include "llvm/Support/MathExtras.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000088#include "llvm/Support/raw_ostream.h"
Eric Christopher78a3f6c2015-02-20 06:59:48 +000089#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000090#include "llvm/Support/Timer.h"
David Blaikie0252265b2013-06-16 20:34:15 +000091#include "llvm/Target/TargetFrameLowering.h"
David Greene659c1a92009-11-13 21:34:57 +000092#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner90438232006-10-06 22:50:56 +000093#include "llvm/Target/TargetLowering.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000094#include "llvm/Target/TargetLoweringObjectFile.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000095#include "llvm/Target/TargetMachine.h"
Evan Cheng0e7b00d2008-03-15 00:03:38 +000096#include "llvm/Target/TargetRegisterInfo.h"
David Woodhousee6c13e42014-01-28 23:12:42 +000097#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000098#include <algorithm>
99#include <cassert>
100#include <cinttypes>
101#include <cstdint>
102#include <limits>
103#include <memory>
104#include <string>
105#include <utility>
106#include <vector>
107
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000108using namespace llvm;
109
Chandler Carruth1b9dde02014-04-22 02:02:50 +0000110#define DEBUG_TYPE "asm-printer"
111
Matthias Braun9f15a792016-11-18 19:43:18 +0000112static const char *const DWARFGroupName = "dwarf";
113static const char *const DWARFGroupDescription = "DWARF Emission";
114static const char *const DbgTimerName = "emit";
115static const char *const DbgTimerDescription = "Debug Info Emission";
116static const char *const EHTimerName = "write_exception";
117static const char *const EHTimerDescription = "DWARF Exception Writer";
118static const char *const CodeViewLineTablesGroupName = "linetables";
119static const char *const CodeViewLineTablesGroupDescription =
120 "CodeView Line Tables";
Torok Edwinf8dba242010-04-07 10:44:46 +0000121
Chris Lattner94a946c2010-01-28 01:02:27 +0000122STATISTIC(EmittedInsts, "Number of machine instrs printed");
123
Devang Patel8c78a0b2007-05-03 01:11:54 +0000124char AsmPrinter::ID = 0;
Chris Lattnere468f882010-03-13 20:55:24 +0000125
David Blaikieec528ee2014-04-15 05:53:26 +0000126typedef DenseMap<GCStrategy*, std::unique_ptr<GCMetadataPrinter>> gcp_map_type;
Chris Lattner2cf5f9e2010-04-04 17:57:56 +0000127static gcp_map_type &getGCMap(void *&P) {
Craig Topper353eda42014-04-24 06:44:33 +0000128 if (!P)
Chris Lattner2cf5f9e2010-04-04 17:57:56 +0000129 P = new gcp_map_type();
130 return *(gcp_map_type*)P;
131}
132
Chris Lattner08e9e722010-04-28 19:58:07 +0000133/// getGVAlignmentLog2 - Return the alignment to use for the specified global
134/// value in log2 form. This rounds up to the preferred alignment if possible
135/// and legal.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000136static unsigned getGVAlignmentLog2(const GlobalValue *GV, const DataLayout &DL,
Chris Lattner08e9e722010-04-28 19:58:07 +0000137 unsigned InBits = 0) {
138 unsigned NumBits = 0;
139 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000140 NumBits = DL.getPreferredAlignmentLog(GVar);
Jim Grosbach1900c732011-03-29 23:20:22 +0000141
Chris Lattner08e9e722010-04-28 19:58:07 +0000142 // If InBits is specified, round it to it.
143 if (InBits > NumBits)
144 NumBits = InBits;
Jim Grosbach1900c732011-03-29 23:20:22 +0000145
Chris Lattner08e9e722010-04-28 19:58:07 +0000146 // If the GV has a specified alignment, take it into account.
147 if (GV->getAlignment() == 0)
148 return NumBits;
Jim Grosbach1900c732011-03-29 23:20:22 +0000149
Chris Lattner08e9e722010-04-28 19:58:07 +0000150 unsigned GVAlign = Log2_32(GV->getAlignment());
Jim Grosbach1900c732011-03-29 23:20:22 +0000151
Chris Lattner08e9e722010-04-28 19:58:07 +0000152 // If the GVAlign is larger than NumBits, or if we are required to obey
153 // NumBits because the GV has an assigned section, obey it.
154 if (GVAlign > NumBits || GV->hasSection())
155 NumBits = GVAlign;
156 return NumBits;
157}
158
David Blaikie94598322015-01-18 20:29:04 +0000159AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer)
Eric Christopherd9134482014-08-04 21:25:23 +0000160 : MachineFunctionPass(ID), TM(tm), MAI(tm.getMCAsmInfo()),
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000161 OutContext(Streamer->getContext()), OutStreamer(std::move(Streamer)) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000162 VerboseAsm = OutStreamer->isVerboseAsm();
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000163}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000164
Gordon Henriksendbe06d32008-08-17 12:08:44 +0000165AsmPrinter::~AsmPrinter() {
Craig Toppere73658d2014-04-28 04:05:08 +0000166 assert(!DD && Handlers.empty() && "Debug/EH info didn't get finalized");
Jim Grosbach1900c732011-03-29 23:20:22 +0000167
Craig Topper353eda42014-04-24 06:44:33 +0000168 if (GCMetadataPrinters) {
Chris Lattner2cf5f9e2010-04-04 17:57:56 +0000169 gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
Jim Grosbach1900c732011-03-29 23:20:22 +0000170
Chris Lattner2cf5f9e2010-04-04 17:57:56 +0000171 delete &GCMap;
Craig Topper353eda42014-04-24 06:44:33 +0000172 GCMetadataPrinters = nullptr;
Chris Lattner2cf5f9e2010-04-04 17:57:56 +0000173 }
Gordon Henriksendbe06d32008-08-17 12:08:44 +0000174}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000175
Rafael Espindola0db11db2016-06-27 14:19:45 +0000176bool AsmPrinter::isPositionIndependent() const {
Rafael Espindolab1556c42016-06-28 20:13:36 +0000177 return TM.isPositionIndependent();
Rafael Espindola0db11db2016-06-27 14:19:45 +0000178}
179
Chris Lattner67159522010-01-26 04:35:26 +0000180/// getFunctionNumber - Return a unique ID for the current function.
181///
182unsigned AsmPrinter::getFunctionNumber() const {
183 return MF->getFunctionNumber();
184}
185
Dan Gohman53d4a082010-04-17 16:44:48 +0000186const TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Eric Christopher36fe0282015-02-03 07:22:52 +0000187 return *TM.getObjFileLowering();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000188}
189
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000190const DataLayout &AsmPrinter::getDataLayout() const {
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000191 return MMI->getModule()->getDataLayout();
Chris Lattner3a383cb2010-04-05 00:13:49 +0000192}
193
Mehdi Amini26d48132015-07-24 16:04:22 +0000194// Do not use the cached DataLayout because some client use it without a Module
Eric Christophere8d141c2016-10-01 01:50:33 +0000195// (llvm-dsymutil, llvm-dwarfdump).
Mehdi Amini26d48132015-07-24 16:04:22 +0000196unsigned AsmPrinter::getPointerSize() const { return TM.getPointerSize(); }
Mehdi Amini1660cab2015-07-16 05:59:25 +0000197
David Woodhousee6c13e42014-01-28 23:12:42 +0000198const MCSubtargetInfo &AsmPrinter::getSubtargetInfo() const {
Eric Christophera7249ec2015-02-20 07:16:19 +0000199 assert(MF && "getSubtargetInfo requires a valid MachineFunction!");
200 return MF->getSubtarget<MCSubtargetInfo>();
David Woodhousee6c13e42014-01-28 23:12:42 +0000201}
202
203void AsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
204 S.EmitInstruction(Inst, getSubtargetInfo());
205}
206
Chris Lattner0adae252009-08-18 06:15:16 +0000207/// getCurrentSection() - Return the current section we are emitting to.
208const MCSection *AsmPrinter::getCurrentSection() const {
Eric Christopher445c9522016-10-14 05:47:37 +0000209 return OutStreamer->getCurrentSectionOnly();
Chris Lattner0adae252009-08-18 06:15:16 +0000210}
211
Gordon Henriksen5180e852008-01-07 01:30:38 +0000212void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman04023152009-07-31 23:37:33 +0000213 AU.setPreservesAll();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000214 MachineFunctionPass::getAnalysisUsage(AU);
Chris Lattnere468f882010-03-13 20:55:24 +0000215 AU.addRequired<MachineModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +0000216 AU.addRequired<GCModuleInfo>();
Chris Lattner7bde8c02010-04-04 18:52:31 +0000217 if (isVerbose())
David Greenef354b6e2009-08-10 16:38:07 +0000218 AU.addRequired<MachineLoopInfo>();
Gordon Henriksen5180e852008-01-07 01:30:38 +0000219}
220
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000221bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner0124fe32010-04-04 05:09:10 +0000222 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Chris Lattnere468f882010-03-13 20:55:24 +0000223
Chris Lattner51d5b432009-07-31 17:42:42 +0000224 // Initialize TargetLoweringObjectFile.
225 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
226 .Initialize(OutContext, TM);
Jim Grosbach1900c732011-03-29 23:20:22 +0000227
Lang Hames9ff69c82015-04-24 19:11:51 +0000228 OutStreamer->InitSections(false);
Nico Rieck316c3742013-07-04 21:37:26 +0000229
Jim Grosbacheaf60362014-03-18 22:09:08 +0000230 // Emit the version-min deplyment target directive if needed.
231 //
232 // FIXME: If we end up with a collection of these sorts of Darwin-specific
233 // or ELF-specific things, it may make sense to have a platform helper class
234 // that will work with the target helper class. For now keep it here, as the
235 // alternative is duplicated code in each of the target asm printers that
236 // use the directive, where it would need the same conditionalization
237 // anyway.
Eric Christophere8d141c2016-10-01 01:50:33 +0000238 const Triple &TT = TM.getTargetTriple();
Matthias Braun4cc34212016-01-13 01:18:13 +0000239 // If there is a version specified, Major will be non-zero.
240 if (TT.isOSDarwin() && TT.getOSMajorVersion() != 0) {
Jim Grosbacheaf60362014-03-18 22:09:08 +0000241 unsigned Major, Minor, Update;
Matthias Braun4cc34212016-01-13 01:18:13 +0000242 MCVersionMinType VersionType;
243 if (TT.isWatchOS()) {
244 VersionType = MCVM_WatchOSVersionMin;
245 TT.getWatchOSVersion(Major, Minor, Update);
246 } else if (TT.isTvOS()) {
247 VersionType = MCVM_TvOSVersionMin;
248 TT.getiOSVersion(Major, Minor, Update);
249 } else if (TT.isMacOSX()) {
250 VersionType = MCVM_OSXVersionMin;
251 if (!TT.getMacOSXVersion(Major, Minor, Update))
252 Major = 0;
253 } else {
254 VersionType = MCVM_IOSVersionMin;
255 TT.getiOSVersion(Major, Minor, Update);
Tim Northover2d4d1612015-10-28 22:36:05 +0000256 }
Matthias Braun4cc34212016-01-13 01:18:13 +0000257 if (Major != 0)
258 OutStreamer->EmitVersionMin(VersionType, Major, Minor, Update);
Jim Grosbacheaf60362014-03-18 22:09:08 +0000259 }
260
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000261 // Allow the target to emit any magic that it wants at the start of the file.
262 EmitStartOfAsmFile(M);
Rafael Espindolacda011b2008-12-03 11:01:37 +0000263
Chris Lattner601ef332010-01-25 18:58:59 +0000264 // Very minimal debug info. It is ignored if we emit actual debug info. If we
265 // don't, this at least helps the user find where a global came from.
Chris Lattnere9a75a62009-08-22 21:43:10 +0000266 if (MAI->hasSingleParameterDotFile()) {
Chris Lattner601ef332010-01-25 18:58:59 +0000267 // .file "foo.c"
Lang Hames9ff69c82015-04-24 19:11:51 +0000268 OutStreamer->EmitFileDirective(M.getModuleIdentifier());
Rafael Espindolacda011b2008-12-03 11:01:37 +0000269 }
270
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000271 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
272 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000273 for (auto &I : *MI)
274 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Philip Reames1e308972014-12-11 01:47:23 +0000275 MP->beginAssembly(M, *MI, *this);
Chris Lattner8a87fb72010-04-03 21:35:55 +0000276
277 // Emit module-level inline asm if it exists.
Chris Lattnerfed39fa2010-04-03 21:13:18 +0000278 if (!M.getModuleInlineAsm().empty()) {
Akira Hatanaka322ffce2015-03-16 18:02:16 +0000279 // We're at the module level. Construct MCSubtarget from the default CPU
280 // and target triple.
281 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
Daniel Sanders335487a2015-06-16 13:15:50 +0000282 TM.getTargetTriple().str(), TM.getTargetCPU(),
283 TM.getTargetFeatureString()));
Lang Hames9ff69c82015-04-24 19:11:51 +0000284 OutStreamer->AddComment("Start of file scope inline assembly");
285 OutStreamer->AddBlankLine();
Akira Hatanakab11ef082015-11-14 06:35:56 +0000286 EmitInlineAsm(M.getModuleInlineAsm()+"\n",
287 OutContext.getSubtargetCopy(*STI), TM.Options.MCOptions);
Lang Hames9ff69c82015-04-24 19:11:51 +0000288 OutStreamer->AddComment("End of file scope inline assembly");
289 OutStreamer->AddBlankLine();
Chris Lattnerfed39fa2010-04-03 21:13:18 +0000290 }
Chris Lattnere3a79262006-01-23 23:47:53 +0000291
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000292 if (MAI->doesSupportDebugInformation()) {
Reid Kleckner12d2c122015-08-05 22:26:20 +0000293 bool EmitCodeView = MMI->getModule()->getCodeViewFlag();
Shoaib Meenai106e05a2016-11-21 20:13:32 +0000294 if (EmitCodeView && (TM.getTargetTriple().isKnownWindowsMSVCEnvironment() ||
295 TM.getTargetTriple().isWindowsItaniumEnvironment())) {
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000296 Handlers.push_back(HandlerInfo(new CodeViewDebug(this),
Matthias Braun9f15a792016-11-18 19:43:18 +0000297 DbgTimerName, DbgTimerDescription,
298 CodeViewLineTablesGroupName,
299 CodeViewLineTablesGroupDescription));
Timur Iskhodzhanovb6fa52f2014-12-26 17:00:51 +0000300 }
Reid Kleckner12d2c122015-08-05 22:26:20 +0000301 if (!EmitCodeView || MMI->getModule()->getDwarfVersion()) {
Timur Iskhodzhanovb6fa52f2014-12-26 17:00:51 +0000302 DD = new DwarfDebug(this, &M);
Peter Collingbourne5973bc82016-05-20 19:35:35 +0000303 DD->beginModule();
Matthias Braun9f15a792016-11-18 19:43:18 +0000304 Handlers.push_back(HandlerInfo(DD, DbgTimerName, DbgTimerDescription,
305 DWARFGroupName, DWARFGroupDescription));
Timur Iskhodzhanovb6fa52f2014-12-26 17:00:51 +0000306 }
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000307 }
Anton Korobeynikov61d167e2011-01-14 21:57:45 +0000308
Joerg Sonnenberger83963992017-01-05 20:55:28 +0000309 switch (MAI->getExceptionHandlingType()) {
310 case ExceptionHandling::SjLj:
311 case ExceptionHandling::DwarfCFI:
312 case ExceptionHandling::ARM:
313 isCFIMoveForDebugging = true;
314 if (MAI->getExceptionHandlingType() != ExceptionHandling::DwarfCFI)
315 break;
316 for (auto &F: M.getFunctionList()) {
317 // If the module contains any function with unwind data,
318 // .eh_frame has to be emitted.
319 // Ignore functions that won't get emitted.
320 if (!F.isDeclarationForLinker() && F.needsUnwindTableEntry()) {
321 isCFIMoveForDebugging = false;
322 break;
323 }
324 }
325 break;
326 default:
327 isCFIMoveForDebugging = false;
328 break;
329 }
330
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000331 EHStreamer *ES = nullptr;
Rafael Espindola240c96e2011-05-05 19:48:34 +0000332 switch (MAI->getExceptionHandlingType()) {
333 case ExceptionHandling::None:
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000334 break;
Rafael Espindola240c96e2011-05-05 19:48:34 +0000335 case ExceptionHandling::SjLj:
Rafael Espindola240c96e2011-05-05 19:48:34 +0000336 case ExceptionHandling::DwarfCFI:
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000337 ES = new DwarfCFIException(this);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000338 break;
Rafael Espindola240c96e2011-05-05 19:48:34 +0000339 case ExceptionHandling::ARM:
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000340 ES = new ARMException(this);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000341 break;
Reid Kleckner5cc15692015-01-23 18:49:01 +0000342 case ExceptionHandling::WinEH:
Saleem Abdulrasoold1a4ed62014-09-01 23:48:39 +0000343 switch (MAI->getWinEHEncodingType()) {
344 default: llvm_unreachable("unsupported unwinding information encoding");
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000345 case WinEH::EncodingType::Invalid:
346 break;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000347 case WinEH::EncodingType::X86:
Saleem Abdulrasoold1a4ed62014-09-01 23:48:39 +0000348 case WinEH::EncodingType::Itanium:
Reid Kleckner60b640b2015-05-28 22:47:01 +0000349 ES = new WinException(this);
Saleem Abdulrasoold1a4ed62014-09-01 23:48:39 +0000350 break;
351 }
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000352 break;
Rafael Espindola240c96e2011-05-05 19:48:34 +0000353 }
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000354 if (ES)
Matthias Braun9f15a792016-11-18 19:43:18 +0000355 Handlers.push_back(HandlerInfo(ES, EHTimerName, EHTimerDescription,
356 DWARFGroupName, DWARFGroupDescription));
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000357 return false;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000358}
359
Rafael Espindolaa0053422014-02-07 16:07:11 +0000360static bool canBeHidden(const GlobalValue *GV, const MCAsmInfo &MAI) {
Rafael Espindolaa0053422014-02-07 16:07:11 +0000361 if (!MAI.hasWeakDefCanBeHiddenDirective())
362 return false;
363
Rafael Espindolaf21434c2014-07-30 19:42:16 +0000364 return canBeOmittedFromSymbolTable(GV);
Rafael Espindolaa0053422014-02-07 16:07:11 +0000365}
366
Rafael Espindola6f1b2852013-10-30 22:08:11 +0000367void AsmPrinter::EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const {
368 GlobalValue::LinkageTypes Linkage = GV->getLinkage();
Rafael Espindolab0287742013-10-23 21:24:34 +0000369 switch (Linkage) {
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000370 case GlobalValue::CommonLinkage:
371 case GlobalValue::LinkOnceAnyLinkage:
372 case GlobalValue::LinkOnceODRLinkage:
373 case GlobalValue::WeakAnyLinkage:
374 case GlobalValue::WeakODRLinkage:
Rafael Espindola04867ce2013-12-02 23:04:51 +0000375 if (MAI->hasWeakDefDirective()) {
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000376 // .globl _foo
Lang Hames9ff69c82015-04-24 19:11:51 +0000377 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
Bill Wendling578ee402010-08-20 22:05:50 +0000378
Rafael Espindolaa0053422014-02-07 16:07:11 +0000379 if (!canBeHidden(GV, *MAI))
Bill Wendling578ee402010-08-20 22:05:50 +0000380 // .weak_definition _foo
Lang Hames9ff69c82015-04-24 19:11:51 +0000381 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_WeakDefinition);
Bill Wendling578ee402010-08-20 22:05:50 +0000382 else
Lang Hames9ff69c82015-04-24 19:11:51 +0000383 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_WeakDefAutoPrivate);
Rafael Espindola04867ce2013-12-02 23:04:51 +0000384 } else if (MAI->hasLinkOnceDirective()) {
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000385 // .globl _foo
Lang Hames9ff69c82015-04-24 19:11:51 +0000386 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
Duncan Sands2576db72010-05-12 07:11:33 +0000387 //NOTE: linkonce is handled by the section the symbol was assigned to.
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000388 } else {
389 // .weak _foo
Lang Hames9ff69c82015-04-24 19:11:51 +0000390 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Weak);
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000391 }
Rafael Espindolab0287742013-10-23 21:24:34 +0000392 return;
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000393 case GlobalValue::ExternalLinkage:
Rafael Espindolae64619c2016-05-16 21:14:24 +0000394 // If external, declare as a global symbol: .globl _foo
Lang Hames9ff69c82015-04-24 19:11:51 +0000395 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
Rafael Espindolab0287742013-10-23 21:24:34 +0000396 return;
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000397 case GlobalValue::PrivateLinkage:
398 case GlobalValue::InternalLinkage:
Rafael Espindolab0287742013-10-23 21:24:34 +0000399 return;
Rafael Espindolae64619c2016-05-16 21:14:24 +0000400 case GlobalValue::AppendingLinkage:
Rafael Espindolab0287742013-10-23 21:24:34 +0000401 case GlobalValue::AvailableExternallyLinkage:
Rafael Espindolab0287742013-10-23 21:24:34 +0000402 case GlobalValue::ExternalWeakLinkage:
Rafael Espindola610a4e92016-05-11 14:41:30 +0000403 llvm_unreachable("Should never emit this");
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000404 }
Rafael Espindolab0287742013-10-23 21:24:34 +0000405 llvm_unreachable("Unknown linkage type!");
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000406}
407
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000408void AsmPrinter::getNameWithPrefix(SmallVectorImpl<char> &Name,
409 const GlobalValue *GV) const {
Eric Christopher4367c7f2016-09-16 07:33:15 +0000410 TM.getNameWithPrefix(Name, GV, getObjFileLowering().getMangler());
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000411}
412
Rafael Espindola79858aa2013-10-29 17:07:16 +0000413MCSymbol *AsmPrinter::getSymbol(const GlobalValue *GV) const {
Tim Northoverb64fb452016-11-22 16:17:20 +0000414 return TM.getSymbol(GV);
Rafael Espindola79858aa2013-10-29 17:07:16 +0000415}
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000416
Chris Lattner6a160512010-01-19 04:39:15 +0000417/// EmitGlobalVariable - Emit the specified global variable to the .s file.
418void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
Chih-Hung Hsieh57886402016-01-13 23:56:37 +0000419 bool IsEmuTLSVar = TM.Options.EmulatedTLS && GV->isThreadLocal();
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000420 assert(!(IsEmuTLSVar && GV->hasCommonLinkage()) &&
421 "No emulated TLS variables in the common section");
422
Chih-Hung Hsieh57886402016-01-13 23:56:37 +0000423 // Never emit TLS variable xyz in emulated TLS model.
424 // The initialization value is in __emutls_t.xyz instead of xyz.
425 if (IsEmuTLSVar)
426 return;
427
Rafael Espindola7dd4d6e2011-04-05 15:51:32 +0000428 if (GV->hasInitializer()) {
429 // Check to see if this is a special global used by LLVM, if so, emit it.
430 if (EmitSpecialLLVMGlobal(GV))
431 return;
Jim Grosbach1900c732011-03-29 23:20:22 +0000432
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +0000433 // Skip the emission of global equivalents. The symbol can be emitted later
434 // on by emitGlobalGOTEquivs in case it turns out to be needed.
435 if (GlobalGOTEquivs.count(getSymbol(GV)))
436 return;
437
Chih-Hung Hsieh57886402016-01-13 23:56:37 +0000438 if (isVerbose()) {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000439 // When printing the control variable __emutls_v.*,
440 // we don't need to print the original TLS variable name.
Lang Hames9ff69c82015-04-24 19:11:51 +0000441 GV->printAsOperand(OutStreamer->GetCommentOS(),
Rafael Espindola7dd4d6e2011-04-05 15:51:32 +0000442 /*PrintType=*/false, GV->getParent());
Lang Hames9ff69c82015-04-24 19:11:51 +0000443 OutStreamer->GetCommentOS() << '\n';
Rafael Espindola7dd4d6e2011-04-05 15:51:32 +0000444 }
Eric Christopherf3925432010-05-25 21:49:43 +0000445 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000446
Rafael Espindola79858aa2013-10-29 17:07:16 +0000447 MCSymbol *GVSym = getSymbol(GV);
Chih-Hung Hsieh57886402016-01-13 23:56:37 +0000448 MCSymbol *EmittedSym = GVSym;
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000449
Eric Christopher0b6537e2016-07-01 06:07:31 +0000450 // getOrCreateEmuTLSControlSym only creates the symbol with name and default
451 // attributes.
452 // GV's or GVSym's attributes will be used for the EmittedSym.
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000453 EmitVisibility(EmittedSym, GV->getVisibility(), !GV->isDeclaration());
Chris Lattnerc7a062d2010-01-19 05:38:33 +0000454
Rafael Espindola7dd4d6e2011-04-05 15:51:32 +0000455 if (!GV->hasInitializer()) // External globals require no extra code.
456 return;
457
David Majnemer8e92dfe2014-12-24 22:44:29 +0000458 GVSym->redefineIfPossible();
David Majnemer2913eca2014-12-24 23:06:55 +0000459 if (GVSym->isDefined() || GVSym->isVariable())
460 report_fatal_error("symbol '" + Twine(GVSym->getName()) +
461 "' is already defined");
David Majnemer8e92dfe2014-12-24 22:44:29 +0000462
Chris Lattnerbc696442010-01-25 18:33:40 +0000463 if (MAI->hasDotTypeDotSizeDirective())
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000464 OutStreamer->EmitSymbolAttribute(EmittedSym, MCSA_ELF_TypeObject);
Jim Grosbach1900c732011-03-29 23:20:22 +0000465
Chris Lattnerc7a062d2010-01-19 05:38:33 +0000466 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
467
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000468 const DataLayout &DL = GV->getParent()->getDataLayout();
469 uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
Jim Grosbach1900c732011-03-29 23:20:22 +0000470
Chris Lattnerf740a8c2010-04-26 18:46:46 +0000471 // If the alignment is specified, we *must* obey it. Overaligning a global
472 // with a specified alignment is a prompt way to break globals emitted to
473 // sections and expected to be contiguous (e.g. ObjC metadata).
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000474 unsigned AlignLog = getGVAlignmentLog2(GV, DL);
Jim Grosbach1900c732011-03-29 23:20:22 +0000475
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000476 for (const HandlerInfo &HI : Handlers) {
Matthias Braun9f15a792016-11-18 19:43:18 +0000477 NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
478 HI.TimerGroupName, HI.TimerGroupDescription,
479 TimePassesIsEnabled);
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000480 HI.Handler->setSymbolSize(GVSym, Size);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000481 }
Richard Mitton089ed892013-09-23 17:56:20 +0000482
Nirav Davef8d00d52016-06-14 15:09:30 +0000483 // Handle common symbols
484 if (GVKind.isCommon()) {
Chris Lattnerc7a062d2010-01-19 05:38:33 +0000485 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Benjamin Kramer63970512011-09-01 23:04:27 +0000486 unsigned Align = 1 << AlignLog;
Nirav Davef8d00d52016-06-14 15:09:30 +0000487 if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
488 Align = 0;
Jim Grosbach1900c732011-03-29 23:20:22 +0000489
Nirav Davef8d00d52016-06-14 15:09:30 +0000490 // .comm _foo, 42, 4
491 OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
492 return;
493 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000494
Nirav Davef8d00d52016-06-14 15:09:30 +0000495 // Determine to which section this global should be emitted.
Eric Christopher4367c7f2016-09-16 07:33:15 +0000496 MCSection *TheSection = getObjFileLowering().SectionForGlobal(GV, GVKind, TM);
Jim Grosbach1900c732011-03-29 23:20:22 +0000497
Nirav Davef8d00d52016-06-14 15:09:30 +0000498 // If we have a bss global going to a section that supports the
499 // zerofill directive, do so here.
500 if (GVKind.isBSS() && MAI->hasMachoZeroFillDirective() &&
501 TheSection->isVirtualSection()) {
502 if (Size == 0)
503 Size = 1; // zerofill of 0 bytes is undefined.
504 unsigned Align = 1 << AlignLog;
505 EmitLinkage(GV, GVSym);
506 // .zerofill __DATA, __bss, _foo, 400, 5
507 OutStreamer->EmitZerofill(TheSection, GVSym, Size, Align);
508 return;
509 }
510
511 // If this is a BSS local symbol and we are emitting in the BSS
512 // section use .lcomm/.comm directive.
513 if (GVKind.isBSSLocal() &&
514 getObjFileLowering().getBSSSection() == TheSection) {
515 if (Size == 0)
516 Size = 1; // .comm Foo, 0 is undefined, avoid it.
517 unsigned Align = 1 << AlignLog;
Jim Grosbach1900c732011-03-29 23:20:22 +0000518
Ulrich Weigande5f94052012-11-27 16:11:16 +0000519 // Use .lcomm only if it supports user-specified alignment.
520 // Otherwise, while it would still be correct to use .lcomm in some
521 // cases (e.g. when Align == 1), the external assembler might enfore
522 // some -unknown- default alignment behavior, which could cause
523 // spurious differences between external and integrated assembler.
524 // Prefer to simply fall back to .local / .comm in this case.
525 if (MAI->getLCOMMDirectiveAlignmentType() != LCOMM::NoAlignment) {
Chris Lattnercd2915e2010-01-19 06:25:51 +0000526 // .lcomm _foo, 42
Lang Hames9ff69c82015-04-24 19:11:51 +0000527 OutStreamer->EmitLocalCommonSymbol(GVSym, Size, Align);
Chris Lattnercd2915e2010-01-19 06:25:51 +0000528 return;
529 }
Chris Lattner9f06f912010-09-27 06:44:54 +0000530
Chris Lattner9f06f912010-09-27 06:44:54 +0000531 if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
532 Align = 0;
Jim Grosbach1900c732011-03-29 23:20:22 +0000533
Chris Lattnercd2915e2010-01-19 06:25:51 +0000534 // .local _foo
Lang Hames9ff69c82015-04-24 19:11:51 +0000535 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Local);
Chris Lattnercd2915e2010-01-19 06:25:51 +0000536 // .comm _foo, 42, 4
Lang Hames9ff69c82015-04-24 19:11:51 +0000537 OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
Chris Lattnerc7a062d2010-01-19 05:38:33 +0000538 return;
539 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000540
Eric Christopher19a4b842010-05-25 21:28:50 +0000541 // Handle thread local data for mach-o which requires us to output an
542 // additional structure of data and mangle the original symbol so that we
543 // can reference it later.
Chris Lattner419d0aa2010-09-05 20:33:40 +0000544 //
545 // TODO: This should become an "emit thread local global" method on TLOF.
546 // All of this macho specific stuff should be sunk down into TLOFMachO and
547 // stuff like "TLSExtraDataSection" should no longer be part of the parent
548 // TLOF class. This will also make it more obvious that stuff like
549 // MCStreamer::EmitTBSSSymbol is macho specific and only called from macho
550 // specific code.
Chih-Hung Hsieh57886402016-01-13 23:56:37 +0000551 if (GVKind.isThreadLocal() && MAI->hasMachoTBSSDirective()) {
Eric Christopher6fdea1b2010-05-22 00:10:22 +0000552 // Emit the .tbss symbol
Jim Grosbach1900c732011-03-29 23:20:22 +0000553 MCSymbol *MangSym =
Eric Christopher4367c7f2016-09-16 07:33:15 +0000554 OutContext.getOrCreateSymbol(GVSym->getName() + Twine("$tlv$init"));
Jim Grosbach1900c732011-03-29 23:20:22 +0000555
Eric Christopher7af8baf2013-08-07 21:13:06 +0000556 if (GVKind.isThreadBSS()) {
557 TheSection = getObjFileLowering().getTLSBSSSection();
Lang Hames9ff69c82015-04-24 19:11:51 +0000558 OutStreamer->EmitTBSSSymbol(TheSection, MangSym, Size, 1 << AlignLog);
Eric Christopher7af8baf2013-08-07 21:13:06 +0000559 } else if (GVKind.isThreadData()) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000560 OutStreamer->SwitchSection(TheSection);
Eric Christopher19a4b842010-05-25 21:28:50 +0000561
Jim Grosbach1900c732011-03-29 23:20:22 +0000562 EmitAlignment(AlignLog, GV);
Lang Hames9ff69c82015-04-24 19:11:51 +0000563 OutStreamer->EmitLabel(MangSym);
Jim Grosbach1900c732011-03-29 23:20:22 +0000564
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000565 EmitGlobalConstant(GV->getParent()->getDataLayout(),
566 GV->getInitializer());
Eric Christopher19a4b842010-05-25 21:28:50 +0000567 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000568
Lang Hames9ff69c82015-04-24 19:11:51 +0000569 OutStreamer->AddBlankLine();
Jim Grosbach1900c732011-03-29 23:20:22 +0000570
Eric Christopher6fdea1b2010-05-22 00:10:22 +0000571 // Emit the variable struct for the runtime.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000572 MCSection *TLVSect = getObjFileLowering().getTLSExtraDataSection();
Jim Grosbach1900c732011-03-29 23:20:22 +0000573
Lang Hames9ff69c82015-04-24 19:11:51 +0000574 OutStreamer->SwitchSection(TLVSect);
Eric Christopher6fdea1b2010-05-22 00:10:22 +0000575 // Emit the linkage here.
Rafael Espindola6f1b2852013-10-30 22:08:11 +0000576 EmitLinkage(GV, GVSym);
Lang Hames9ff69c82015-04-24 19:11:51 +0000577 OutStreamer->EmitLabel(GVSym);
Jim Grosbach1900c732011-03-29 23:20:22 +0000578
Eric Christopher6fdea1b2010-05-22 00:10:22 +0000579 // Three pointers in size:
580 // - __tlv_bootstrap - used to make sure support exists
581 // - spare pointer, used when mapped by the runtime
582 // - pointer to mangled symbol above with initializer
Mehdi Amini5c0fa582015-07-16 06:04:17 +0000583 unsigned PtrSize = DL.getPointerTypeSize(GV->getType());
Lang Hames9ff69c82015-04-24 19:11:51 +0000584 OutStreamer->EmitSymbolValue(GetExternalSymbolSymbol("_tlv_bootstrap"),
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000585 PtrSize);
Lang Hames9ff69c82015-04-24 19:11:51 +0000586 OutStreamer->EmitIntValue(0, PtrSize);
587 OutStreamer->EmitSymbolValue(MangSym, PtrSize);
Jim Grosbach1900c732011-03-29 23:20:22 +0000588
Lang Hames9ff69c82015-04-24 19:11:51 +0000589 OutStreamer->AddBlankLine();
Eric Christopher27e7ffc2010-05-20 00:49:07 +0000590 return;
591 }
Chris Lattnerc7a062d2010-01-19 05:38:33 +0000592
Nirav Davef8d00d52016-06-14 15:09:30 +0000593 MCSymbol *EmittedInitSym = GVSym;
594
Lang Hames9ff69c82015-04-24 19:11:51 +0000595 OutStreamer->SwitchSection(TheSection);
Chris Lattnerc7a062d2010-01-19 05:38:33 +0000596
Chih-Hung Hsieh57886402016-01-13 23:56:37 +0000597 EmitLinkage(GV, EmittedInitSym);
Chris Lattnere80442a2010-04-26 18:30:45 +0000598 EmitAlignment(AlignLog, GV);
Chris Lattnerdbf5a762010-01-26 23:47:12 +0000599
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000600 OutStreamer->EmitLabel(EmittedInitSym);
Chris Lattnerc7a062d2010-01-19 05:38:33 +0000601
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000602 EmitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
Chris Lattnerc7a062d2010-01-19 05:38:33 +0000603
604 if (MAI->hasDotTypeDotSizeDirective())
Chris Lattner866a5392010-01-25 07:53:05 +0000605 // .size foo, 42
Dan Gohman734c59d2016-12-01 23:39:08 +0000606 OutStreamer->emitELFSize(EmittedInitSym,
Rafael Espindolaa8695762015-06-02 00:25:12 +0000607 MCConstantExpr::create(Size, OutContext));
Jim Grosbach1900c732011-03-29 23:20:22 +0000608
Lang Hames9ff69c82015-04-24 19:11:51 +0000609 OutStreamer->AddBlankLine();
Chris Lattner6a160512010-01-19 04:39:15 +0000610}
611
Petar Jovanovicdbb39352017-01-20 17:53:30 +0000612/// Emit the directive and value for debug thread local expression
613///
614/// \p Value - The value to emit.
615/// \p Size - The size of the integer (in bytes) to emit.
Simon Dardis2e8cdbd2017-02-08 19:03:46 +0000616void AsmPrinter::EmitDebugThreadLocal(const MCExpr *Value,
Petar Jovanovicdbb39352017-01-20 17:53:30 +0000617 unsigned Size) const {
618 OutStreamer->EmitValue(Value, Size);
619}
620
Chris Lattnerc7b91152010-01-26 23:18:44 +0000621/// EmitFunctionHeader - This method emits the header for the current
622/// function.
623void AsmPrinter::EmitFunctionHeader() {
624 // Print out constants referenced by the function
Chris Lattner2b796242010-01-28 00:19:24 +0000625 EmitConstantPool();
Jim Grosbach1900c732011-03-29 23:20:22 +0000626
Chris Lattnerc7b91152010-01-26 23:18:44 +0000627 // Print the 'header' of function.
Chris Lattnerc7b91152010-01-26 23:18:44 +0000628 const Function *F = MF->getFunction();
629
Eric Christopher4367c7f2016-09-16 07:33:15 +0000630 OutStreamer->SwitchSection(getObjFileLowering().SectionForGlobal(F, TM));
Chris Lattnercd995462010-01-28 00:05:10 +0000631 EmitVisibility(CurrentFnSym, F->getVisibility());
Chris Lattnerc7b91152010-01-26 23:18:44 +0000632
Rafael Espindola6f1b2852013-10-30 22:08:11 +0000633 EmitLinkage(F, CurrentFnSym);
Jingyue Wue8290f22015-03-12 01:50:30 +0000634 if (MAI->hasFunctionAlignment())
635 EmitAlignment(MF->getAlignment(), F);
Chris Lattner22fc38d2010-01-26 23:41:48 +0000636
Chris Lattnerc7b91152010-01-26 23:18:44 +0000637 if (MAI->hasDotTypeDotSizeDirective())
Lang Hames9ff69c82015-04-24 19:11:51 +0000638 OutStreamer->EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
Chris Lattnerc7b91152010-01-26 23:18:44 +0000639
Chris Lattner7bde8c02010-04-04 18:52:31 +0000640 if (isVerbose()) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000641 F->printAsOperand(OutStreamer->GetCommentOS(),
Chris Lattnerbac74982010-01-26 23:53:39 +0000642 /*PrintType=*/false, F->getParent());
Lang Hames9ff69c82015-04-24 19:11:51 +0000643 OutStreamer->GetCommentOS() << '\n';
Chris Lattnerc7b91152010-01-26 23:18:44 +0000644 }
Chris Lattnerc7b91152010-01-26 23:18:44 +0000645
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000646 // Emit the prefix data.
647 if (F->hasPrefixData())
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000648 EmitGlobalConstant(F->getParent()->getDataLayout(), F->getPrefixData());
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000649
Dan Gohman4a618822010-02-10 16:03:48 +0000650 // Emit the CurrentFnSym. This is a virtual function to allow targets to
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000651 // do their wild and crazy things as required.
652 EmitFunctionEntryLabel();
Jim Grosbach1900c732011-03-29 23:20:22 +0000653
Chris Lattner561334a2010-03-15 20:39:00 +0000654 // If the function had address-taken blocks that got deleted, then we have
655 // references to the dangling symbols. Emit them at the start of the function
656 // so that we don't get references to undefined symbols.
657 std::vector<MCSymbol*> DeadBlockSyms;
658 MMI->takeDeletedSymbolsForFunction(F, DeadBlockSyms);
659 for (unsigned i = 0, e = DeadBlockSyms.size(); i != e; ++i) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000660 OutStreamer->AddComment("Address taken block that was later removed");
661 OutStreamer->EmitLabel(DeadBlockSyms[i]);
Chris Lattner561334a2010-03-15 20:39:00 +0000662 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000663
Rafael Espindola092b6192015-03-05 19:47:50 +0000664 if (CurrentFnBegin) {
Rafael Espindola629cdba2015-02-27 18:18:39 +0000665 if (MAI->useAssignmentForEHBegin()) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000666 MCSymbol *CurPos = OutContext.createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000667 OutStreamer->EmitLabel(CurPos);
668 OutStreamer->EmitAssignment(CurrentFnBegin,
Jim Grosbach13760bd2015-05-30 01:25:56 +0000669 MCSymbolRefExpr::create(CurPos, OutContext));
Rafael Espindola629cdba2015-02-27 18:18:39 +0000670 } else {
Lang Hames9ff69c82015-04-24 19:11:51 +0000671 OutStreamer->EmitLabel(CurrentFnBegin);
Rafael Espindola629cdba2015-02-27 18:18:39 +0000672 }
673 }
674
Chris Lattnerc7b91152010-01-26 23:18:44 +0000675 // Emit pre-function debug and/or EH information.
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000676 for (const HandlerInfo &HI : Handlers) {
Matthias Braun9f15a792016-11-18 19:43:18 +0000677 NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
678 HI.TimerGroupDescription, TimePassesIsEnabled);
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000679 HI.Handler->beginFunction(MF);
Torok Edwinf8dba242010-04-07 10:44:46 +0000680 }
Peter Collingbournee9f45e22013-10-20 02:16:21 +0000681
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000682 // Emit the prologue data.
683 if (F->hasPrologueData())
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000684 EmitGlobalConstant(F->getParent()->getDataLayout(), F->getPrologueData());
Chris Lattnerc7b91152010-01-26 23:18:44 +0000685}
686
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000687/// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the
688/// function. This can be overridden by targets as required to do custom stuff.
689void AsmPrinter::EmitFunctionEntryLabel() {
David Majnemer58cb80c2014-12-24 10:27:50 +0000690 CurrentFnSym->redefineIfPossible();
691
Chris Lattner35096e82010-05-06 00:05:37 +0000692 // The function label could have already been emitted if two symbols end up
693 // conflicting due to asm renaming. Detect this and emit an error.
David Majnemer2913eca2014-12-24 23:06:55 +0000694 if (CurrentFnSym->isVariable())
695 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
696 "' is a protected alias");
697 if (CurrentFnSym->isDefined())
698 report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
699 "' label emitted multiple times to assembly file");
Chris Lattner35096e82010-05-06 00:05:37 +0000700
Lang Hames9ff69c82015-04-24 19:11:51 +0000701 return OutStreamer->EmitLabel(CurrentFnSym);
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000702}
Chris Lattnerc7b91152010-01-26 23:18:44 +0000703
Chad Rosierb759ede2012-09-07 18:16:38 +0000704/// emitComments - Pretty-print comments for instructions.
705static void emitComments(const MachineInstr &MI, raw_ostream &CommentOS) {
Devang Patelbe305512010-06-29 22:29:15 +0000706 const MachineFunction *MF = MI.getParent()->getParent();
Eric Christopher97ea7622015-02-20 06:35:21 +0000707 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Jim Grosbach1900c732011-03-29 23:20:22 +0000708
Chris Lattner74e68522010-02-10 00:47:53 +0000709 // Check for spills and reloads
710 int FI;
Jim Grosbach1900c732011-03-29 23:20:22 +0000711
Matthias Braun941a7052016-07-28 18:40:00 +0000712 const MachineFrameInfo &MFI = MF->getFrameInfo();
Jim Grosbach1900c732011-03-29 23:20:22 +0000713
Chris Lattner74e68522010-02-10 00:47:53 +0000714 // We assume a single instruction only has a spill or reload, not
715 // both.
716 const MachineMemOperand *MMO;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000717 if (TII->isLoadFromStackSlotPostFE(MI, FI)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000718 if (MFI.isSpillSlotObjectIndex(FI)) {
Chris Lattner74e68522010-02-10 00:47:53 +0000719 MMO = *MI.memoperands_begin();
720 CommentOS << MMO->getSize() << "-byte Reload\n";
721 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000722 } else if (TII->hasLoadFromStackSlot(MI, MMO, FI)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000723 if (MFI.isSpillSlotObjectIndex(FI))
Chris Lattner74e68522010-02-10 00:47:53 +0000724 CommentOS << MMO->getSize() << "-byte Folded Reload\n";
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000725 } else if (TII->isStoreToStackSlotPostFE(MI, FI)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000726 if (MFI.isSpillSlotObjectIndex(FI)) {
Chris Lattner74e68522010-02-10 00:47:53 +0000727 MMO = *MI.memoperands_begin();
728 CommentOS << MMO->getSize() << "-byte Spill\n";
729 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000730 } else if (TII->hasStoreToStackSlot(MI, MMO, FI)) {
Matthias Braun941a7052016-07-28 18:40:00 +0000731 if (MFI.isSpillSlotObjectIndex(FI))
Chris Lattner74e68522010-02-10 00:47:53 +0000732 CommentOS << MMO->getSize() << "-byte Folded Spill\n";
733 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000734
Chris Lattner74e68522010-02-10 00:47:53 +0000735 // Check for spill-induced copies
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000736 if (MI.getAsmPrinterFlag(MachineInstr::ReloadReuse))
737 CommentOS << " Reload Reuse\n";
Chris Lattner74e68522010-02-10 00:47:53 +0000738}
739
Chad Rosierb759ede2012-09-07 18:16:38 +0000740/// emitImplicitDef - This method emits the specified machine instruction
Chris Lattner2b40a202010-04-04 18:58:53 +0000741/// that is an implicit def.
Justin Holewinski660597d2013-10-11 12:39:36 +0000742void AsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
Chris Lattner2b40a202010-04-04 18:58:53 +0000743 unsigned RegNo = MI->getOperand(0).getReg();
Dan Gohman7aa4aba2015-11-17 16:01:28 +0000744
745 SmallString<128> Str;
746 raw_svector_ostream OS(Str);
747 OS << "implicit-def: "
748 << PrintReg(RegNo, MF->getSubtarget().getRegisterInfo());
749
750 OutStreamer->AddComment(OS.str());
Lang Hames9ff69c82015-04-24 19:11:51 +0000751 OutStreamer->AddBlankLine();
Chris Lattner2b40a202010-04-04 18:58:53 +0000752}
753
Chad Rosierb759ede2012-09-07 18:16:38 +0000754static void emitKill(const MachineInstr *MI, AsmPrinter &AP) {
Dan Gohman7aa4aba2015-11-17 16:01:28 +0000755 std::string Str;
756 raw_string_ostream OS(Str);
757 OS << "kill:";
Chris Lattner2b40a202010-04-04 18:58:53 +0000758 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
759 const MachineOperand &Op = MI->getOperand(i);
760 assert(Op.isReg() && "KILL instruction must have only register operands");
Dan Gohman7aa4aba2015-11-17 16:01:28 +0000761 OS << ' '
762 << PrintReg(Op.getReg(),
763 AP.MF->getSubtarget().getRegisterInfo())
764 << (Op.isDef() ? "<def>" : "<kill>");
Chris Lattner2b40a202010-04-04 18:58:53 +0000765 }
Matthias Braun07985802016-07-09 00:18:43 +0000766 AP.OutStreamer->AddComment(OS.str());
Lang Hames9ff69c82015-04-24 19:11:51 +0000767 AP.OutStreamer->AddBlankLine();
Chris Lattner2b40a202010-04-04 18:58:53 +0000768}
769
Chad Rosierb759ede2012-09-07 18:16:38 +0000770/// emitDebugValueComment - This method handles the target-independent form
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000771/// of DBG_VALUE, returning true if it was able to do so. A false return
772/// means the target will need to handle MI in EmitInstruction.
Chad Rosierb759ede2012-09-07 18:16:38 +0000773static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000774 // This code handles only the 4-operand target-independent form.
775 if (MI->getNumOperands() != 4)
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000776 return false;
Chris Lattner74e68522010-02-10 00:47:53 +0000777
Benjamin Kramer43c275f2010-04-07 09:26:51 +0000778 SmallString<128> Str;
779 raw_svector_ostream OS(Str);
Rafael Espindola0b694812014-01-16 16:28:37 +0000780 OS << "DEBUG_VALUE: ";
Benjamin Kramer43c275f2010-04-07 09:26:51 +0000781
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000782 const DILocalVariable *V = MI->getDebugVariable();
783 if (auto *SP = dyn_cast<DISubprogram>(V->getScope())) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000784 StringRef Name = SP->getDisplayName();
David Blaikie2dbebcf2013-06-15 00:33:47 +0000785 if (!Name.empty())
786 OS << Name << ":";
787 }
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000788 OS << V->getName();
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000789
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000790 const DIExpression *Expr = MI->getDebugExpression();
Adrian Prantl49797ca2016-12-22 05:27:12 +0000791 auto Fragment = Expr->getFragmentInfo();
792 if (Fragment)
793 OS << " [fragment offset=" << Fragment->OffsetInBits
794 << " size=" << Fragment->SizeInBits << "]";
Adrian Prantlb1416832014-08-01 22:11:58 +0000795 OS << " <- ";
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000796
Adrian Prantl418d1d12013-07-09 20:28:37 +0000797 // The second operand is only an offset if it's an immediate.
798 bool Deref = MI->getOperand(0).isReg() && MI->getOperand(1).isImm();
799 int64_t Offset = Deref ? MI->getOperand(1).getImm() : 0;
800
Keno Fischer00cbf9a2015-12-19 02:02:44 +0000801 for (unsigned i = 0; i < Expr->getNumElements(); ++i) {
Reid Klecknerf9dddec2016-10-05 18:36:02 +0000802 uint64_t Op = Expr->getElement(i);
Adrian Prantl941fa752016-12-05 18:04:47 +0000803 if (Op == dwarf::DW_OP_LLVM_fragment) {
Reid Klecknerf9dddec2016-10-05 18:36:02 +0000804 // There can't be any operands after this in a valid expression
805 break;
806 } else if (Deref) {
Keno Fischer00cbf9a2015-12-19 02:02:44 +0000807 // We currently don't support extra Offsets or derefs after the first
808 // one. Bail out early instead of emitting an incorrect comment
809 OS << " [complex expression]";
810 AP.OutStreamer->emitRawComment(OS.str());
811 return true;
Reid Klecknerf9dddec2016-10-05 18:36:02 +0000812 } else if (Op == dwarf::DW_OP_deref) {
Keno Fischer00cbf9a2015-12-19 02:02:44 +0000813 Deref = true;
814 continue;
815 }
Reid Klecknerf9dddec2016-10-05 18:36:02 +0000816
Keno Fischer00cbf9a2015-12-19 02:02:44 +0000817 uint64_t ExtraOffset = Expr->getElement(i++);
818 if (Op == dwarf::DW_OP_plus)
819 Offset += ExtraOffset;
820 else {
821 assert(Op == dwarf::DW_OP_minus);
822 Offset -= ExtraOffset;
823 }
824 }
825
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000826 // Register or immediate value. Register 0 means undef.
827 if (MI->getOperand(0).isFPImm()) {
828 APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF());
829 if (MI->getOperand(0).getFPImm()->getType()->isFloatTy()) {
Benjamin Kramer43c275f2010-04-07 09:26:51 +0000830 OS << (double)APF.convertToFloat();
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000831 } else if (MI->getOperand(0).getFPImm()->getType()->isDoubleTy()) {
Benjamin Kramer43c275f2010-04-07 09:26:51 +0000832 OS << APF.convertToDouble();
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000833 } else {
834 // There is no good way to print long double. Convert a copy to
835 // double. Ah well, it's only a comment.
836 bool ignored;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000837 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000838 &ignored);
Benjamin Kramer43c275f2010-04-07 09:26:51 +0000839 OS << "(long double) " << APF.convertToDouble();
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000840 }
841 } else if (MI->getOperand(0).isImm()) {
Benjamin Kramer43c275f2010-04-07 09:26:51 +0000842 OS << MI->getOperand(0).getImm();
Devang Patelf071d722011-06-24 20:46:11 +0000843 } else if (MI->getOperand(0).isCImm()) {
844 MI->getOperand(0).getCImm()->getValue().print(OS, false /*isSigned*/);
Chris Lattnerb50e7952010-04-07 22:29:10 +0000845 } else {
David Blaikie0252265b2013-06-16 20:34:15 +0000846 unsigned Reg;
847 if (MI->getOperand(0).isReg()) {
848 Reg = MI->getOperand(0).getReg();
David Blaikie0252265b2013-06-16 20:34:15 +0000849 } else {
850 assert(MI->getOperand(0).isFI() && "Unknown operand type");
Eric Christophera7249ec2015-02-20 07:16:19 +0000851 const TargetFrameLowering *TFI = AP.MF->getSubtarget().getFrameLowering();
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000852 Offset += TFI->getFrameIndexReference(*AP.MF,
853 MI->getOperand(0).getIndex(), Reg);
David Blaikie0252265b2013-06-16 20:34:15 +0000854 Deref = true;
855 }
David Blaikiec9380db2013-06-15 15:52:58 +0000856 if (Reg == 0) {
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000857 // Suppress offset, it is not meaningful here.
Benjamin Kramer43c275f2010-04-07 09:26:51 +0000858 OS << "undef";
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000859 // NOTE: Want this comment at start of line, don't emit with AddComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000860 AP.OutStreamer->emitRawComment(OS.str());
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000861 return true;
862 }
David Blaikiec9380db2013-06-15 15:52:58 +0000863 if (Deref)
864 OS << '[';
Dan Gohman7aa4aba2015-11-17 16:01:28 +0000865 OS << PrintReg(Reg, AP.MF->getSubtarget().getRegisterInfo());
Chris Lattnerb50e7952010-04-07 22:29:10 +0000866 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000867
David Blaikiec9380db2013-06-15 15:52:58 +0000868 if (Deref)
Adrian Prantl418d1d12013-07-09 20:28:37 +0000869 OS << '+' << Offset << ']';
870
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000871 // NOTE: Want this comment at start of line, don't emit with AddComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000872 AP.OutStreamer->emitRawComment(OS.str());
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000873 return true;
874}
Chris Lattner74e68522010-02-10 00:47:53 +0000875
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +0000876AsmPrinter::CFIMoveType AsmPrinter::needsCFIMoves() {
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000877 if (MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI &&
878 MF->getFunction()->needsUnwindTableEntry())
879 return CFI_M_EH;
Rafael Espindolab7a012a2011-04-29 14:14:06 +0000880
Rafael Espindolaa7160962011-05-06 14:56:22 +0000881 if (MMI->hasDebugInfo())
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +0000882 return CFI_M_Debug;
Rafael Espindolab7a012a2011-04-29 14:14:06 +0000883
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +0000884 return CFI_M_None;
Rafael Espindolab7a012a2011-04-29 14:14:06 +0000885}
886
Charles Davis5638b9f2011-05-28 04:21:04 +0000887bool AsmPrinter::needsSEHMoves() {
Reid Kleckner93acac62014-12-19 22:19:48 +0000888 return MAI->usesWindowsCFI() && MF->getFunction()->needsUnwindTableEntry();
Charles Davis5638b9f2011-05-28 04:21:04 +0000889}
890
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000891void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) {
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000892 ExceptionHandling ExceptionHandlingType = MAI->getExceptionHandlingType();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000893 if (ExceptionHandlingType != ExceptionHandling::DwarfCFI &&
894 ExceptionHandlingType != ExceptionHandling::ARM)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000895 return;
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000896
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +0000897 if (needsCFIMoves() == CFI_M_None)
Rafael Espindolab7a012a2011-04-29 14:14:06 +0000898 return;
899
Matthias Braunf23ef432016-11-30 23:48:42 +0000900 const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000901 unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
902 const MCCFIInstruction &CFI = Instrs[CFIIndex];
903 emitCFIInstruction(CFI);
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000904}
905
Reid Klecknere9b89312015-01-13 00:48:10 +0000906void AsmPrinter::emitFrameAlloc(const MachineInstr &MI) {
907 // The operands are the MCSymbol and the frame offset of the allocation.
908 MCSymbol *FrameAllocSym = MI.getOperand(0).getMCSymbol();
909 int FrameOffset = MI.getOperand(1).getImm();
910
911 // Emit a symbol assignment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000912 OutStreamer->EmitAssignment(FrameAllocSym,
Jim Grosbach13760bd2015-05-30 01:25:56 +0000913 MCConstantExpr::create(FrameOffset, OutContext));
Reid Klecknere9b89312015-01-13 00:48:10 +0000914}
915
Chris Lattner94a946c2010-01-28 01:02:27 +0000916/// EmitFunctionBody - This method emits the body and trailer for a
917/// function.
918void AsmPrinter::EmitFunctionBody() {
Rafael Espindola8dc4e102015-03-17 14:38:30 +0000919 EmitFunctionHeader();
920
Chris Lattnerc968f442010-01-28 01:58:58 +0000921 // Emit target-specific gunk before the function body.
922 EmitFunctionBodyStart();
Jim Grosbach1900c732011-03-29 23:20:22 +0000923
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000924 bool ShouldPrintDebugScopes = MMI->hasDebugInfo();
Jim Grosbach1900c732011-03-29 23:20:22 +0000925
Chris Lattner94a946c2010-01-28 01:02:27 +0000926 // Print out code for the function.
927 bool HasAnyRealCode = false;
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000928 for (auto &MBB : *MF) {
Chris Lattner94a946c2010-01-28 01:02:27 +0000929 // Print a label for the basic block.
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000930 EmitBasicBlockStart(MBB);
931 for (auto &MI : MBB) {
Bill Wendlingbf8370f2010-07-16 22:51:10 +0000932
Chris Lattner94a946c2010-01-28 01:02:27 +0000933 // Print the assembly for the instruction.
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000934 if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
935 !MI.isDebugValue()) {
Chris Lattner94a946c2010-01-28 01:02:27 +0000936 HasAnyRealCode = true;
Evan Chengeb828b62010-04-27 19:38:45 +0000937 ++EmittedInsts;
938 }
939
Torok Edwinf8dba242010-04-07 10:44:46 +0000940 if (ShouldPrintDebugScopes) {
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000941 for (const HandlerInfo &HI : Handlers) {
Matthias Braun9f15a792016-11-18 19:43:18 +0000942 NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
943 HI.TimerGroupName, HI.TimerGroupDescription,
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000944 TimePassesIsEnabled);
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000945 HI.Handler->beginInstruction(&MI);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000946 }
Torok Edwinf8dba242010-04-07 10:44:46 +0000947 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000948
Chris Lattner7bde8c02010-04-04 18:52:31 +0000949 if (isVerbose())
Lang Hames9ff69c82015-04-24 19:11:51 +0000950 emitComments(MI, OutStreamer->GetCommentOS());
Chris Lattner74e68522010-02-10 00:47:53 +0000951
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000952 switch (MI.getOpcode()) {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000953 case TargetOpcode::CFI_INSTRUCTION:
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000954 emitCFIInstruction(MI);
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000955 break;
956
Reid Kleckner60381792015-07-07 22:25:32 +0000957 case TargetOpcode::LOCAL_ESCAPE:
Reid Klecknere9b89312015-01-13 00:48:10 +0000958 emitFrameAlloc(MI);
959 break;
960
Chris Lattnerb06015a2010-02-09 19:54:29 +0000961 case TargetOpcode::EH_LABEL:
962 case TargetOpcode::GC_LABEL:
Lang Hames9ff69c82015-04-24 19:11:51 +0000963 OutStreamer->EmitLabel(MI.getOperand(0).getMCSymbol());
Chris Lattner41ad1902010-02-03 01:00:52 +0000964 break;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000965 case TargetOpcode::INLINEASM:
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000966 EmitInlineAsm(&MI);
Chris Lattner41ad1902010-02-03 01:00:52 +0000967 break;
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000968 case TargetOpcode::DBG_VALUE:
969 if (isVerbose()) {
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000970 if (!emitDebugValueComment(&MI, *this))
971 EmitInstruction(&MI);
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000972 }
973 break;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000974 case TargetOpcode::IMPLICIT_DEF:
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000975 if (isVerbose()) emitImplicitDef(&MI);
Chris Lattner41ad1902010-02-03 01:00:52 +0000976 break;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000977 case TargetOpcode::KILL:
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000978 if (isVerbose()) emitKill(&MI, *this);
Chris Lattner41ad1902010-02-03 01:00:52 +0000979 break;
980 default:
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000981 EmitInstruction(&MI);
Chris Lattner41ad1902010-02-03 01:00:52 +0000982 break;
983 }
Jim Grosbach1900c732011-03-29 23:20:22 +0000984
Torok Edwinf8dba242010-04-07 10:44:46 +0000985 if (ShouldPrintDebugScopes) {
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000986 for (const HandlerInfo &HI : Handlers) {
Matthias Braun9f15a792016-11-18 19:43:18 +0000987 NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
988 HI.TimerGroupName, HI.TimerGroupDescription,
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000989 TimePassesIsEnabled);
Jim Grosbach6623e7f2014-04-16 22:38:02 +0000990 HI.Handler->endInstruction();
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000991 }
Torok Edwinf8dba242010-04-07 10:44:46 +0000992 }
Chris Lattner94a946c2010-01-28 01:02:27 +0000993 }
Lang Hamesf49bc3f2014-07-24 20:40:55 +0000994
995 EmitBasicBlockEnd(MBB);
Chris Lattner94a946c2010-01-28 01:02:27 +0000996 }
Bill Wendlingbf8370f2010-07-16 22:51:10 +0000997
Chris Lattner94a946c2010-01-28 01:02:27 +0000998 // If the function is empty and the object file uses .subsections_via_symbols,
Chris Lattner73de5fb2010-01-28 01:28:58 +0000999 // then we need to emit *something* to the function body to prevent the
Chris Lattner6a5e7062010-04-26 23:37:21 +00001000 // labels from collapsing together. Just emit a noop.
Rafael Espindola6865d6f2014-09-15 18:32:58 +00001001 if ((MAI->hasSubsectionsViaSymbols() && !HasAnyRealCode)) {
Chris Lattner6a5e7062010-04-26 23:37:21 +00001002 MCInst Noop;
Eric Christopher97ea7622015-02-20 06:35:21 +00001003 MF->getSubtarget().getInstrInfo()->getNoopForMachoTarget(Noop);
Lang Hames9ff69c82015-04-24 19:11:51 +00001004 OutStreamer->AddComment("avoids zero-length function");
Owen Andersonbfc80a42014-09-16 20:28:00 +00001005
1006 // Targets can opt-out of emitting the noop here by leaving the opcode
1007 // unspecified.
1008 if (Noop.getOpcode())
Lang Hames9ff69c82015-04-24 19:11:51 +00001009 OutStreamer->EmitInstruction(Noop, getSubtargetInfo());
Chris Lattner6a5e7062010-04-26 23:37:21 +00001010 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001011
Rafael Espindolaf11e7f12011-11-15 19:08:46 +00001012 const Function *F = MF->getFunction();
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001013 for (const auto &BB : *F) {
1014 if (!BB.hasAddressTaken())
Rafael Espindolaf11e7f12011-11-15 19:08:46 +00001015 continue;
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001016 MCSymbol *Sym = GetBlockAddressSymbol(&BB);
Rafael Espindolaf11e7f12011-11-15 19:08:46 +00001017 if (Sym->isDefined())
1018 continue;
Lang Hames9ff69c82015-04-24 19:11:51 +00001019 OutStreamer->AddComment("Address of block that was removed by CodeGen");
1020 OutStreamer->EmitLabel(Sym);
Rafael Espindolaf11e7f12011-11-15 19:08:46 +00001021 }
1022
Chris Lattnerc968f442010-01-28 01:58:58 +00001023 // Emit target-specific gunk after the function body.
1024 EmitFunctionBodyEnd();
Jim Grosbach1900c732011-03-29 23:20:22 +00001025
Matthias Braund0ee66c2016-12-01 19:32:15 +00001026 if (!MF->getLandingPads().empty() || MMI->hasDebugInfo() ||
1027 MF->hasEHFunclets() || MAI->hasDotTypeDotSizeDirective()) {
Rafael Espindola629cdba2015-02-27 18:18:39 +00001028 // Create a symbol for the end of function.
Rafael Espindola9ab09232015-03-17 20:07:06 +00001029 CurrentFnEnd = createTempSymbol("func_end");
Lang Hames9ff69c82015-04-24 19:11:51 +00001030 OutStreamer->EmitLabel(CurrentFnEnd);
Rafael Espindola629cdba2015-02-27 18:18:39 +00001031 }
1032
Chris Lattnerc6d67d42010-04-03 22:19:41 +00001033 // If the target wants a .size directive for the size of the function, emit
1034 // it.
1035 if (MAI->hasDotTypeDotSizeDirective()) {
Rafael Espindola310e4b52015-03-04 01:35:23 +00001036 // We can get the size as difference between the function label and the
1037 // temp label.
NAKAMURA Takumibf6ad022015-06-14 00:23:33 +00001038 const MCExpr *SizeExp = MCBinaryExpr::createSub(
1039 MCSymbolRefExpr::create(CurrentFnEnd, OutContext),
1040 MCSymbolRefExpr::create(CurrentFnSymForSize, OutContext), OutContext);
Dan Gohman734c59d2016-12-01 23:39:08 +00001041 OutStreamer->emitELFSize(CurrentFnSym, SizeExp);
Chris Lattnerc6d67d42010-04-03 22:19:41 +00001042 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001043
Rafael Espindolaa6001792015-03-09 18:29:12 +00001044 for (const HandlerInfo &HI : Handlers) {
Matthias Braun9f15a792016-11-18 19:43:18 +00001045 NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1046 HI.TimerGroupDescription, TimePassesIsEnabled);
Rafael Espindolaa6001792015-03-09 18:29:12 +00001047 HI.Handler->markFunctionEnd();
1048 }
1049
1050 // Print out jump tables referenced by the function.
1051 EmitJumpTableInfo();
1052
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001053 // Emit post-function debug and/or EH information.
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001054 for (const HandlerInfo &HI : Handlers) {
Matthias Braun9f15a792016-11-18 19:43:18 +00001055 NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1056 HI.TimerGroupDescription, TimePassesIsEnabled);
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001057 HI.Handler->endFunction(MF);
Torok Edwinf8dba242010-04-07 10:44:46 +00001058 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001059
Lang Hames9ff69c82015-04-24 19:11:51 +00001060 OutStreamer->AddBlankLine();
Chris Lattner94a946c2010-01-28 01:02:27 +00001061}
1062
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00001063/// \brief Compute the number of Global Variables that uses a Constant.
1064static unsigned getNumGlobalVariableUses(const Constant *C) {
1065 if (!C)
1066 return 0;
1067
1068 if (isa<GlobalVariable>(C))
1069 return 1;
1070
1071 unsigned NumUses = 0;
1072 for (auto *CU : C->users())
1073 NumUses += getNumGlobalVariableUses(dyn_cast<Constant>(CU));
1074
1075 return NumUses;
1076}
1077
1078/// \brief Only consider global GOT equivalents if at least one user is a
1079/// cstexpr inside an initializer of another global variables. Also, don't
1080/// handle cstexpr inside instructions. During global variable emission,
1081/// candidates are skipped and are emitted later in case at least one cstexpr
1082/// isn't replaced by a PC relative GOT entry access.
1083static bool isGOTEquivalentCandidate(const GlobalVariable *GV,
1084 unsigned &NumGOTEquivUsers) {
1085 // Global GOT equivalents are unnamed private globals with a constant
1086 // pointer initializer to another global symbol. They must point to a
1087 // GlobalVariable or Function, i.e., as GlobalValue.
Eric Christopher0b6537e2016-07-01 06:07:31 +00001088 if (!GV->hasGlobalUnnamedAddr() || !GV->hasInitializer() ||
1089 !GV->isConstant() || !GV->isDiscardableIfUnused() ||
1090 !dyn_cast<GlobalValue>(GV->getOperand(0)))
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00001091 return false;
1092
1093 // To be a got equivalent, at least one of its users need to be a constant
1094 // expression used by another global variable.
1095 for (auto *U : GV->users())
Ahmed Bougacha821880a2015-03-27 01:40:54 +00001096 NumGOTEquivUsers += getNumGlobalVariableUses(dyn_cast<Constant>(U));
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00001097
1098 return NumGOTEquivUsers > 0;
1099}
1100
1101/// \brief Unnamed constant global variables solely contaning a pointer to
1102/// another globals variable is equivalent to a GOT table entry; it contains the
1103/// the address of another symbol. Optimize it and replace accesses to these
1104/// "GOT equivalents" by using the GOT entry for the final global instead.
1105/// Compute GOT equivalent candidates among all global variables to avoid
1106/// emitting them if possible later on, after it use is replaced by a GOT entry
1107/// access.
1108void AsmPrinter::computeGlobalGOTEquivs(Module &M) {
1109 if (!getObjFileLowering().supportIndirectSymViaGOTPCRel())
1110 return;
1111
1112 for (const auto &G : M.globals()) {
1113 unsigned NumGOTEquivUsers = 0;
1114 if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers))
1115 continue;
1116
1117 const MCSymbol *GOTEquivSym = getSymbol(&G);
1118 GlobalGOTEquivs[GOTEquivSym] = std::make_pair(&G, NumGOTEquivUsers);
1119 }
1120}
1121
1122/// \brief Constant expressions using GOT equivalent globals may not be eligible
1123/// for PC relative GOT entry conversion, in such cases we need to emit such
1124/// globals we previously omitted in EmitGlobalVariable.
1125void AsmPrinter::emitGlobalGOTEquivs() {
1126 if (!getObjFileLowering().supportIndirectSymViaGOTPCRel())
1127 return;
1128
Bruno Cardoso Lopesb3a58b42015-03-10 20:05:23 +00001129 SmallVector<const GlobalVariable *, 8> FailedCandidates;
1130 for (auto &I : GlobalGOTEquivs) {
1131 const GlobalVariable *GV = I.second.first;
1132 unsigned Cnt = I.second.second;
1133 if (Cnt)
1134 FailedCandidates.push_back(GV);
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00001135 }
Bruno Cardoso Lopesb3a58b42015-03-10 20:05:23 +00001136 GlobalGOTEquivs.clear();
1137
1138 for (auto *GV : FailedCandidates)
1139 EmitGlobalVariable(GV);
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00001140}
1141
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00001142void AsmPrinter::emitGlobalIndirectSymbol(Module &M,
1143 const GlobalIndirectSymbol& GIS) {
1144 MCSymbol *Name = getSymbol(&GIS);
1145
1146 if (GIS.hasExternalLinkage() || !MAI->getWeakRefDirective())
1147 OutStreamer->EmitSymbolAttribute(Name, MCSA_Global);
1148 else if (GIS.hasWeakLinkage() || GIS.hasLinkOnceLinkage())
1149 OutStreamer->EmitSymbolAttribute(Name, MCSA_WeakReference);
1150 else
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001151 assert(GIS.hasLocalLinkage() && "Invalid alias or ifunc linkage");
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00001152
1153 // Set the symbol type to function if the alias has a function type.
1154 // This affects codegen when the aliasee is not a function.
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001155 if (GIS.getType()->getPointerElementType()->isFunctionTy()) {
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00001156 OutStreamer->EmitSymbolAttribute(Name, MCSA_ELF_TypeFunction);
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001157 if (isa<GlobalIFunc>(GIS))
1158 OutStreamer->EmitSymbolAttribute(Name, MCSA_ELF_TypeIndFunction);
1159 }
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00001160
1161 EmitVisibility(Name, GIS.getVisibility());
1162
1163 const MCExpr *Expr = lowerConstant(GIS.getIndirectSymbol());
1164
1165 if (isa<GlobalAlias>(&GIS) && MAI->hasAltEntry() && isa<MCBinaryExpr>(Expr))
1166 OutStreamer->EmitSymbolAttribute(Name, MCSA_AltEntry);
1167
1168 // Emit the directives as assignments aka .set:
1169 OutStreamer->EmitAssignment(Name, Expr);
1170
1171 if (auto *GA = dyn_cast<GlobalAlias>(&GIS)) {
1172 // If the aliasee does not correspond to a symbol in the output, i.e. the
1173 // alias is not of an object or the aliased object is private, then set the
1174 // size of the alias symbol from the type of the alias. We don't do this in
1175 // other situations as the alias and aliasee having differing types but same
1176 // size may be intentional.
1177 const GlobalObject *BaseObject = GA->getBaseObject();
1178 if (MAI->hasDotTypeDotSizeDirective() && GA->getValueType()->isSized() &&
1179 (!BaseObject || BaseObject->hasPrivateLinkage())) {
1180 const DataLayout &DL = M.getDataLayout();
1181 uint64_t Size = DL.getTypeAllocSize(GA->getValueType());
Dan Gohman734c59d2016-12-01 23:39:08 +00001182 OutStreamer->emitELFSize(Name, MCConstantExpr::create(Size, OutContext));
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00001183 }
1184 }
1185}
1186
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001187bool AsmPrinter::doFinalization(Module &M) {
Eric Christopherd43c5c72015-03-20 16:03:39 +00001188 // Set the MachineFunction to nullptr so that we can catch attempted
1189 // accesses to MF specific features at the module level and so that
1190 // we can conditionalize accesses based on whether or not it is nullptr.
1191 MF = nullptr;
1192
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00001193 // Gather all GOT equivalent globals in the module. We really need two
1194 // passes over the globals: one to compute and another to avoid its emission
1195 // in EmitGlobalVariable, otherwise we would not be able to handle cases
1196 // where the got equivalent shows up before its use.
1197 computeGlobalGOTEquivs(M);
1198
Chris Lattner100865e2009-07-21 18:38:57 +00001199 // Emit global variables.
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001200 for (const auto &G : M.globals())
1201 EmitGlobalVariable(&G);
Rafael Espindola6c17d542011-01-28 03:20:10 +00001202
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00001203 // Emit remaining GOT equivalent globals.
1204 emitGlobalGOTEquivs();
1205
Rafael Espindola6c17d542011-01-28 03:20:10 +00001206 // Emit visibility info for declarations
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001207 for (const Function &F : M) {
Rafael Espindola7068cbb2015-07-13 13:55:18 +00001208 if (!F.isDeclarationForLinker())
Rafael Espindola6c17d542011-01-28 03:20:10 +00001209 continue;
1210 GlobalValue::VisibilityTypes V = F.getVisibility();
1211 if (V == GlobalValue::DefaultVisibility)
1212 continue;
1213
Rafael Espindola79858aa2013-10-29 17:07:16 +00001214 MCSymbol *Name = getSymbol(&F);
Stuart Hastingsbf836592011-02-23 02:27:05 +00001215 EmitVisibility(Name, V, false);
Rafael Espindola6c17d542011-01-28 03:20:10 +00001216 }
1217
Rafael Espindolab91455b2015-04-07 13:42:44 +00001218 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
1219
Bill Wendling06df7722012-02-14 21:28:13 +00001220 // Emit module flags.
Bill Wendling734909a2012-02-15 22:36:15 +00001221 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
1222 M.getModuleFlagsMetadata(ModuleFlags);
1223 if (!ModuleFlags.empty())
Eric Christopher4367c7f2016-09-16 07:33:15 +00001224 TLOF.emitModuleFlags(*OutStreamer, ModuleFlags, TM);
Rafael Espindolab91455b2015-04-07 13:42:44 +00001225
Daniel Sanders335487a2015-06-16 13:15:50 +00001226 if (TM.getTargetTriple().isOSBinFormatELF()) {
Rafael Espindolab91455b2015-04-07 13:42:44 +00001227 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
1228
1229 // Output stubs for external and common global variables.
1230 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
1231 if (!Stubs.empty()) {
Rafael Espindola449711c2015-11-18 06:02:15 +00001232 OutStreamer->SwitchSection(TLOF.getDataSection());
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001233 const DataLayout &DL = M.getDataLayout();
Rafael Espindolab91455b2015-04-07 13:42:44 +00001234
1235 for (const auto &Stub : Stubs) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001236 OutStreamer->EmitLabel(Stub.first);
1237 OutStreamer->EmitSymbolValue(Stub.second.getPointer(),
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001238 DL.getPointerSize());
Rafael Espindolab91455b2015-04-07 13:42:44 +00001239 }
1240 }
1241 }
Bill Wendling06df7722012-02-14 21:28:13 +00001242
Chris Lattnerf0d6bd32010-04-05 05:11:15 +00001243 // Finalize debug and EH information.
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001244 for (const HandlerInfo &HI : Handlers) {
Matthias Braun9f15a792016-11-18 19:43:18 +00001245 NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1246 HI.TimerGroupDescription, TimePassesIsEnabled);
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001247 HI.Handler->endModule();
1248 delete HI.Handler;
Chris Lattnerf0d6bd32010-04-05 05:11:15 +00001249 }
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001250 Handlers.clear();
Craig Topper353eda42014-04-24 06:44:33 +00001251 DD = nullptr;
Jim Grosbach1900c732011-03-29 23:20:22 +00001252
Chris Lattner2981dc12009-06-24 18:52:01 +00001253 // If the target wants to know about weak references, print them all.
Chris Lattnere9a75a62009-08-22 21:43:10 +00001254 if (MAI->getWeakRefDirective()) {
Chris Lattner2981dc12009-06-24 18:52:01 +00001255 // FIXME: This is not lazy, it would be nice to only print weak references
1256 // to stuff that is actually used. Note that doing so would require targets
1257 // to notice uses in operands (due to constant exprs etc). This should
1258 // happen with the MC stuff eventually.
Rafael Espindolad7998d02006-12-18 03:37:18 +00001259
Peter Collingbourne6d88fde2016-06-22 20:29:42 +00001260 // Print out module-level global objects here.
1261 for (const auto &GO : M.global_objects()) {
1262 if (!GO.hasExternalWeakLinkage())
Jim Grosbach6623e7f2014-04-16 22:38:02 +00001263 continue;
Peter Collingbourne6d88fde2016-06-22 20:29:42 +00001264 OutStreamer->EmitSymbolAttribute(getSymbol(&GO), MCSA_WeakReference);
Chris Lattner2981dc12009-06-24 18:52:01 +00001265 }
Rafael Espindolad7998d02006-12-18 03:37:18 +00001266 }
1267
Lang Hames9ff69c82015-04-24 19:11:51 +00001268 OutStreamer->AddBlankLine();
Anton Korobeynikova07765b2007-09-06 17:21:48 +00001269
Tim Shen800ed432016-03-31 22:08:19 +00001270 // Print aliases in topological order, that is, for each alias a = b,
1271 // b must be printed before a.
1272 // This is because on some targets (e.g. PowerPC) linker expects aliases in
1273 // such an order to generate correct TOC information.
1274 SmallVector<const GlobalAlias *, 16> AliasStack;
1275 SmallPtrSet<const GlobalAlias *, 16> AliasVisited;
1276 for (const auto &Alias : M.aliases()) {
1277 for (const GlobalAlias *Cur = &Alias; Cur;
1278 Cur = dyn_cast<GlobalAlias>(Cur->getAliasee())) {
1279 if (!AliasVisited.insert(Cur).second)
1280 break;
1281 AliasStack.push_back(Cur);
1282 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001283 for (const GlobalAlias *AncestorAlias : llvm::reverse(AliasStack))
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00001284 emitGlobalIndirectSymbol(M, *AncestorAlias);
Tim Shen800ed432016-03-31 22:08:19 +00001285 AliasStack.clear();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001286 }
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001287 for (const auto &IFunc : M.ifuncs())
1288 emitGlobalIndirectSymbol(M, IFunc);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001289
Duncan Sands5a913d62009-01-28 13:14:17 +00001290 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksend930f912008-08-17 18:44:35 +00001291 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
1292 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
David Blaikie88368ba2014-04-15 05:34:49 +00001293 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(**--I))
Philip Reames1e308972014-12-11 01:47:23 +00001294 MP->finishAssembly(M, *MI, *this);
Gordon Henriksen5180e852008-01-07 01:30:38 +00001295
Rafael Espindola0018a592013-10-16 01:49:05 +00001296 // Emit llvm.ident metadata in an '.ident' directive.
1297 EmitModuleIdents(M);
1298
Peter Collingbourne7ef497b2014-12-30 20:05:19 +00001299 // Emit __morestack address if needed for indirect calls.
1300 if (MMI->usesMorestackAddr()) {
David Majnemera3ea4072016-02-21 01:30:30 +00001301 unsigned Align = 1;
Mehdi Amini5c0fa582015-07-16 06:04:17 +00001302 MCSection *ReadOnlySection = getObjFileLowering().getSectionForConstant(
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001303 getDataLayout(), SectionKind::getReadOnly(),
David Majnemera3ea4072016-02-21 01:30:30 +00001304 /*C=*/nullptr, Align);
Lang Hames9ff69c82015-04-24 19:11:51 +00001305 OutStreamer->SwitchSection(ReadOnlySection);
Peter Collingbourne7ef497b2014-12-30 20:05:19 +00001306
1307 MCSymbol *AddrSymbol =
Jim Grosbach6f482002015-05-18 18:43:14 +00001308 OutContext.getOrCreateSymbol(StringRef("__morestack_addr"));
Lang Hames9ff69c82015-04-24 19:11:51 +00001309 OutStreamer->EmitLabel(AddrSymbol);
Peter Collingbourne7ef497b2014-12-30 20:05:19 +00001310
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001311 unsigned PtrSize = M.getDataLayout().getPointerSize(0);
Lang Hames9ff69c82015-04-24 19:11:51 +00001312 OutStreamer->EmitSymbolValue(GetExternalSymbolSymbol("__morestack"),
1313 PtrSize);
Peter Collingbourne7ef497b2014-12-30 20:05:19 +00001314 }
1315
Dan Gohmanbcde1722008-05-05 00:28:39 +00001316 // If we don't have any trampolines, then we don't require stack memory
1317 // to be executable. Some targets have a directive to declare this.
Chris Lattner2981dc12009-06-24 18:52:01 +00001318 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmanbcde1722008-05-05 00:28:39 +00001319 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001320 if (MCSection *S = MAI->getNonexecutableStackSection(OutContext))
Lang Hames9ff69c82015-04-24 19:11:51 +00001321 OutStreamer->SwitchSection(S);
Jim Grosbach1900c732011-03-29 23:20:22 +00001322
Chris Lattner71a15b12009-09-18 20:17:03 +00001323 // Allow the target to emit any magic that it wants at the end of the file,
1324 // after everything else has gone out.
1325 EmitEndOfAsmFile(M);
Jim Grosbach1900c732011-03-29 23:20:22 +00001326
Craig Topper353eda42014-04-24 06:44:33 +00001327 MMI = nullptr;
Jim Grosbach1900c732011-03-29 23:20:22 +00001328
Lang Hames9ff69c82015-04-24 19:11:51 +00001329 OutStreamer->Finish();
1330 OutStreamer->reset();
Pedro Artigas7212ee42012-12-12 22:59:46 +00001331
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001332 return false;
1333}
1334
Rafael Espindoladc4263c2015-03-17 13:57:48 +00001335MCSymbol *AsmPrinter::getCurExceptionSym() {
1336 if (!CurExceptionSym)
Rafael Espindola9ab09232015-03-17 20:07:06 +00001337 CurExceptionSym = createTempSymbol("exception");
Rafael Espindoladc4263c2015-03-17 13:57:48 +00001338 return CurExceptionSym;
1339}
1340
Chris Lattnerbb644e32005-11-21 07:51:36 +00001341void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner67159522010-01-26 04:35:26 +00001342 this->MF = &MF;
Chris Lattnera0911792010-01-16 01:24:10 +00001343 // Get the function symbol.
Rafael Espindola79858aa2013-10-29 17:07:16 +00001344 CurrentFnSym = getSymbol(MF.getFunction());
Hal Finkelad4d9f52012-02-22 21:11:47 +00001345 CurrentFnSymForSize = CurrentFnSym;
Rafael Espindola092b6192015-03-05 19:47:50 +00001346 CurrentFnBegin = nullptr;
Rafael Espindoladc4263c2015-03-17 13:57:48 +00001347 CurExceptionSym = nullptr;
Rafael Espindola092b6192015-03-05 19:47:50 +00001348 bool NeedsLocalForSize = MAI->needsLocalForSize();
Matthias Braund0ee66c2016-12-01 19:32:15 +00001349 if (!MF.getLandingPads().empty() || MMI->hasDebugInfo() ||
1350 MF.hasEHFunclets() || NeedsLocalForSize) {
Rafael Espindola9ab09232015-03-17 20:07:06 +00001351 CurrentFnBegin = createTempSymbol("func_begin");
Rafael Espindola092b6192015-03-05 19:47:50 +00001352 if (NeedsLocalForSize)
1353 CurrentFnSymForSize = CurrentFnBegin;
1354 }
David Greenef354b6e2009-08-10 16:38:07 +00001355
Chris Lattner7bde8c02010-04-04 18:52:31 +00001356 if (isVerbose())
David Greenef354b6e2009-08-10 16:38:07 +00001357 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001358}
1359
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001360namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001361
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001362// Keep track the alignment, constpool entries per Section.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001363 struct SectionCPs {
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001364 MCSection *S;
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001365 unsigned Alignment;
1366 SmallVector<unsigned, 4> CPEs;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001367
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001368 SectionCPs(MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001369 };
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001370
1371} // end anonymous namespace
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001372
Chris Lattnerf2991ce2005-11-21 08:25:09 +00001373/// EmitConstantPool - Print to the current output stream assembly
1374/// representations of the constants in the constant pool MCP. This is
1375/// used to print out constants which have been "spilled to memory" by
1376/// the code generator.
1377///
Chris Lattner2b796242010-01-28 00:19:24 +00001378void AsmPrinter::EmitConstantPool() {
1379 const MachineConstantPool *MCP = MF->getConstantPool();
Chris Lattnerba972642006-02-09 04:22:52 +00001380 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +00001381 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +00001382
Anton Korobeynikov123afb82008-09-24 22:17:59 +00001383 // Calculate sections for constant pool entries. We collect entries to go into
1384 // the same section together to reduce amount of section switch statements.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001385 SmallVector<SectionCPs, 4> CPSections;
Chris Lattnerf2991ce2005-11-21 08:25:09 +00001386 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerb300a4f2009-07-22 00:28:43 +00001387 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001388 unsigned Align = CPE.getAlignment();
Jim Grosbach1900c732011-03-29 23:20:22 +00001389
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001390 SectionKind Kind = CPE.getSectionKind(&getDataLayout());
Chris Lattnerfb6867c2009-07-26 06:26:55 +00001391
David Majnemer8bce66b2014-07-14 22:57:27 +00001392 const Constant *C = nullptr;
1393 if (!CPE.isMachineConstantPoolEntry())
1394 C = CPE.Val.ConstVal;
1395
David Majnemera3ea4072016-02-21 01:30:30 +00001396 MCSection *S = getObjFileLowering().getSectionForConstant(getDataLayout(),
1397 Kind, C, Align);
Jim Grosbach1900c732011-03-29 23:20:22 +00001398
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001399 // The number of sections are small, just do a linear search from the
1400 // last section to the first.
1401 bool Found = false;
1402 unsigned SecIdx = CPSections.size();
1403 while (SecIdx != 0) {
1404 if (CPSections[--SecIdx].S == S) {
1405 Found = true;
1406 break;
1407 }
1408 }
1409 if (!Found) {
1410 SecIdx = CPSections.size();
1411 CPSections.push_back(SectionCPs(S, Align));
1412 }
1413
1414 if (Align > CPSections[SecIdx].Alignment)
1415 CPSections[SecIdx].Alignment = Align;
1416 CPSections[SecIdx].CPEs.push_back(i);
Evan Chengec1d60b2006-06-29 00:26:09 +00001417 }
1418
Anton Korobeynikov123afb82008-09-24 22:17:59 +00001419 // Now print stuff into the calculated sections.
David Majnemer8bce66b2014-07-14 22:57:27 +00001420 const MCSection *CurSection = nullptr;
1421 unsigned Offset = 0;
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001422 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001423 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
1424 unsigned CPI = CPSections[i].CPEs[j];
David Majnemer8bce66b2014-07-14 22:57:27 +00001425 MCSymbol *Sym = GetCPISymbol(CPI);
1426 if (!Sym->isUndefined())
1427 continue;
1428
1429 if (CurSection != CPSections[i].S) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001430 OutStreamer->SwitchSection(CPSections[i].S);
David Majnemer8bce66b2014-07-14 22:57:27 +00001431 EmitAlignment(Log2_32(CPSections[i].Alignment));
1432 CurSection = CPSections[i].S;
1433 Offset = 0;
1434 }
1435
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001436 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov123afb82008-09-24 22:17:59 +00001437
Chris Lattnerf6190822006-02-09 04:46:04 +00001438 // Emit inter-object padding for alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001439 unsigned AlignMask = CPE.getAlignment() - 1;
1440 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
Lang Hames9ff69c82015-04-24 19:11:51 +00001441 OutStreamer->EmitZeros(NewOffset - Offset);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001442
Chris Lattner229907c2011-07-18 04:54:35 +00001443 Type *Ty = CPE.getType();
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001444 Offset = NewOffset + getDataLayout().getTypeAllocSize(Ty);
Chris Lattner4225a7b2010-01-23 05:19:23 +00001445
Lang Hames9ff69c82015-04-24 19:11:51 +00001446 OutStreamer->EmitLabel(Sym);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001447 if (CPE.isMachineConstantPoolEntry())
1448 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
1449 else
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001450 EmitGlobalConstant(getDataLayout(), CPE.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +00001451 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +00001452 }
1453}
1454
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001455/// EmitJumpTableInfo - Print assembly representations of the jump tables used
Jim Grosbach1900c732011-03-29 23:20:22 +00001456/// by the current function to the current output stream.
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001457///
Chris Lattner94a946c2010-01-28 01:02:27 +00001458void AsmPrinter::EmitJumpTableInfo() {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001459 const DataLayout &DL = MF->getDataLayout();
Chris Lattner94a946c2010-01-28 01:02:27 +00001460 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Craig Topper353eda42014-04-24 06:44:33 +00001461 if (!MJTI) return;
Richard Osborne6d3e92d2010-03-11 14:58:16 +00001462 if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001463 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1464 if (JT.empty()) return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +00001465
Jim Grosbach1900c732011-03-29 23:20:22 +00001466 // Pick the directive to use to print the jump table entries, and switch to
Nate Begeman78756502006-07-27 01:13:04 +00001467 // the appropriate section.
Chris Lattner94a946c2010-01-28 01:02:27 +00001468 const Function *F = MF->getFunction();
Rafael Espindola29786d42015-02-12 17:16:46 +00001469 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Rafael Espindoladf195192015-02-17 23:34:51 +00001470 bool JTInDiffSection = !TLOF.shouldPutJumpTableInFunctionSection(
1471 MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32,
1472 *F);
Rafael Espindolaa6001792015-03-09 18:29:12 +00001473 if (JTInDiffSection) {
1474 // Drop it in the readonly section.
Eric Christopher4367c7f2016-09-16 07:33:15 +00001475 MCSection *ReadOnlySection = TLOF.getSectionForJumpTable(*F, TM);
Lang Hames9ff69c82015-04-24 19:11:51 +00001476 OutStreamer->SwitchSection(ReadOnlySection);
Nate Begeman78756502006-07-27 01:13:04 +00001477 }
Chris Lattnerb6db2c62010-01-25 23:26:13 +00001478
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001479 EmitAlignment(Log2_32(MJTI->getEntryAlignment(DL)));
Jim Grosbach1900c732011-03-29 23:20:22 +00001480
Jim Grosbach361ca342012-09-24 23:06:27 +00001481 // Jump tables in code sections are marked with a data_region directive
1482 // where that's supported.
1483 if (!JTInDiffSection)
Lang Hames9ff69c82015-04-24 19:11:51 +00001484 OutStreamer->EmitDataRegion(MCDR_DataRegionJT32);
Jim Grosbach361ca342012-09-24 23:06:27 +00001485
Chris Lattner4db603f2010-01-26 06:42:44 +00001486 for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) {
1487 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach1900c732011-03-29 23:20:22 +00001488
1489 // If this jump table was deleted, ignore it.
Chris Lattner28bfe382006-10-28 18:10:06 +00001490 if (JTBBs.empty()) continue;
Nate Begeman984c1a42006-08-12 21:29:52 +00001491
Rafael Espindolac606bfe2014-10-21 01:17:30 +00001492 // For the EK_LabelDifference32 entry, if using .set avoids a relocation,
1493 /// emit a .set directive for each unique entry.
Chris Lattnercc0f7342010-01-26 05:15:20 +00001494 if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 &&
Joerg Sonnenberger22982032016-06-18 23:25:37 +00001495 MAI->doesSetDirectiveSuppressReloc()) {
Chris Lattner4db603f2010-01-26 06:42:44 +00001496 SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets;
Eric Christopher504f3882015-02-19 18:46:23 +00001497 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
Chris Lattner94a946c2010-01-28 01:02:27 +00001498 const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext);
Chris Lattner4db603f2010-01-26 06:42:44 +00001499 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
1500 const MachineBasicBlock *MBB = JTBBs[ii];
David Blaikie70573dc2014-11-19 07:49:26 +00001501 if (!EmittedSets.insert(MBB).second)
1502 continue;
Jim Grosbach1900c732011-03-29 23:20:22 +00001503
Chris Lattnerb657c4c2010-01-26 21:53:08 +00001504 // .set LJTSet, LBB32-base
1505 const MCExpr *LHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001506 MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
Lang Hames9ff69c82015-04-24 19:11:51 +00001507 OutStreamer->EmitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()),
Jim Grosbach13760bd2015-05-30 01:25:56 +00001508 MCBinaryExpr::createSub(LHS, Base,
Lang Hames9ff69c82015-04-24 19:11:51 +00001509 OutContext));
Chris Lattner4db603f2010-01-26 06:42:44 +00001510 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001511 }
1512
Duncan Sands75b5d272011-02-15 09:23:02 +00001513 // On some targets (e.g. Darwin) we want to emit two consecutive labels
Chris Lattner0ee2d462007-01-18 01:12:56 +00001514 // before each jump table. The first label is never referenced, but tells
1515 // the assembler and linker the extents of the jump table object. The
1516 // second label is actually referenced by the code.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001517 if (JTInDiffSection && DL.hasLinkerPrivateGlobalPrefix())
Chris Lattner273735b2010-01-26 05:58:28 +00001518 // FIXME: This doesn't have to have any specific name, just any randomly
1519 // named and numbered 'l' label would work. Simplify GetJTISymbol.
Lang Hames9ff69c82015-04-24 19:11:51 +00001520 OutStreamer->EmitLabel(GetJTISymbol(JTI, true));
Chris Lattner4225a7b2010-01-23 05:19:23 +00001521
Lang Hames9ff69c82015-04-24 19:11:51 +00001522 OutStreamer->EmitLabel(GetJTISymbol(JTI));
Chris Lattner4225a7b2010-01-23 05:19:23 +00001523
Chris Lattner279de3e2010-01-26 05:10:10 +00001524 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Chris Lattner4db603f2010-01-26 06:42:44 +00001525 EmitJumpTableEntry(MJTI, JTBBs[ii], JTI);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001526 }
Jim Grosbach361ca342012-09-24 23:06:27 +00001527 if (!JTInDiffSection)
Lang Hames9ff69c82015-04-24 19:11:51 +00001528 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001529}
1530
Chris Lattner279de3e2010-01-26 05:10:10 +00001531/// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the
1532/// current stream.
1533void AsmPrinter::EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
1534 const MachineBasicBlock *MBB,
1535 unsigned UID) const {
Jakob Stoklund Olesen6d4d8582011-02-09 21:52:06 +00001536 assert(MBB && MBB->getNumber() >= 0 && "Invalid basic block");
Craig Topper353eda42014-04-24 06:44:33 +00001537 const MCExpr *Value = nullptr;
Chris Lattnera7c11602010-01-26 03:43:22 +00001538 switch (MJTI->getEntryKind()) {
Richard Osborne6d3e92d2010-03-11 14:58:16 +00001539 case MachineJumpTableInfo::EK_Inline:
David Blaikie46a9f012012-01-20 21:51:11 +00001540 llvm_unreachable("Cannot emit EK_Inline jump table entry");
Chris Lattner5fc41602010-01-26 04:05:28 +00001541 case MachineJumpTableInfo::EK_Custom32:
Eric Christopher504f3882015-02-19 18:46:23 +00001542 Value = MF->getSubtarget().getTargetLowering()->LowerCustomJumpTableEntry(
1543 MJTI, MBB, UID, OutContext);
Chris Lattner5fc41602010-01-26 04:05:28 +00001544 break;
Chris Lattnera7c11602010-01-26 03:43:22 +00001545 case MachineJumpTableInfo::EK_BlockAddress:
1546 // EK_BlockAddress - Each entry is a plain address of block, e.g.:
1547 // .word LBB123
Jim Grosbach13760bd2015-05-30 01:25:56 +00001548 Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
Chris Lattnera7c11602010-01-26 03:43:22 +00001549 break;
Chris Lattnera7c11602010-01-26 03:43:22 +00001550 case MachineJumpTableInfo::EK_GPRel32BlockAddress: {
1551 // EK_GPRel32BlockAddress - Each entry is an address of block, encoded
1552 // with a relocation as gp-relative, e.g.:
1553 // .gprel32 LBB123
Chris Lattner29bdac42010-03-13 21:04:28 +00001554 MCSymbol *MBBSym = MBB->getSymbol();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001555 OutStreamer->EmitGPRel32Value(MCSymbolRefExpr::create(MBBSym, OutContext));
Chris Lattner19bd0392010-01-25 21:10:10 +00001556 return;
Anton Korobeynikov2c638782007-11-14 09:18:41 +00001557 }
Chris Lattner5fc41602010-01-26 04:05:28 +00001558
Akira Hatanakaf0b08442012-02-03 04:33:00 +00001559 case MachineJumpTableInfo::EK_GPRel64BlockAddress: {
1560 // EK_GPRel64BlockAddress - Each entry is an address of block, encoded
1561 // with a relocation as gp-relative, e.g.:
1562 // .gpdword LBB123
1563 MCSymbol *MBBSym = MBB->getSymbol();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001564 OutStreamer->EmitGPRel64Value(MCSymbolRefExpr::create(MBBSym, OutContext));
Akira Hatanakaf0b08442012-02-03 04:33:00 +00001565 return;
1566 }
1567
Chris Lattnera7c11602010-01-26 03:43:22 +00001568 case MachineJumpTableInfo::EK_LabelDifference32: {
Rafael Espindolac606bfe2014-10-21 01:17:30 +00001569 // Each entry is the address of the block minus the address of the jump
1570 // table. This is used for PIC jump tables where gprel32 is not supported.
1571 // e.g.:
Chris Lattnera7c11602010-01-26 03:43:22 +00001572 // .word LBB123 - LJTI1_2
Rafael Espindolac606bfe2014-10-21 01:17:30 +00001573 // If the .set directive avoids relocations, this is emitted as:
Chris Lattnera7c11602010-01-26 03:43:22 +00001574 // .set L4_5_set_123, LBB123 - LJTI1_2
1575 // .word L4_5_set_123
Joerg Sonnenberger22982032016-06-18 23:25:37 +00001576 if (MAI->doesSetDirectiveSuppressReloc()) {
Jim Grosbach13760bd2015-05-30 01:25:56 +00001577 Value = MCSymbolRefExpr::create(GetJTSetSymbol(UID, MBB->getNumber()),
Chris Lattnera7c11602010-01-26 03:43:22 +00001578 OutContext);
1579 break;
1580 }
Jim Grosbach13760bd2015-05-30 01:25:56 +00001581 Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
Eric Christophera7249ec2015-02-20 07:16:19 +00001582 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
Rafael Espindola26cfbea2014-11-06 14:39:49 +00001583 const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF, UID, OutContext);
Jim Grosbach13760bd2015-05-30 01:25:56 +00001584 Value = MCBinaryExpr::createSub(Value, Base, OutContext);
Chris Lattnera7c11602010-01-26 03:43:22 +00001585 break;
1586 }
Anton Korobeynikov2c638782007-11-14 09:18:41 +00001587 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001588
Chris Lattnera7c11602010-01-26 03:43:22 +00001589 assert(Value && "Unknown entry kind!");
Jim Grosbach1900c732011-03-29 23:20:22 +00001590
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001591 unsigned EntrySize = MJTI->getEntrySize(getDataLayout());
Lang Hames9ff69c82015-04-24 19:11:51 +00001592 OutStreamer->EmitValue(Value, EntrySize);
Anton Korobeynikov2c638782007-11-14 09:18:41 +00001593}
1594
Chris Lattnerf0e9aef2005-12-13 06:32:10 +00001595/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
1596/// special global used by LLVM. If so, emit it and return true, otherwise
1597/// do nothing and return false.
1598bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001599 if (GV->getName() == "llvm.used") {
Chris Lattner1deb09c2010-01-23 05:51:36 +00001600 if (MAI->hasNoDeadStrip()) // No need to emit this at all.
Rafael Espindola74f2e462013-04-22 14:58:02 +00001601 EmitLLVMUsedList(cast<ConstantArray>(GV->getInitializer()));
Andrew Lenharthbeb80a92007-08-22 19:33:11 +00001602 return true;
1603 }
1604
Chris Lattner58f9bb22009-07-20 06:14:25 +00001605 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Rafael Espindola83658d62016-05-11 18:21:59 +00001606 if (GV->getSection() == "llvm.metadata" ||
Chris Lattner184f1be2009-04-13 05:44:34 +00001607 GV->hasAvailableExternallyLinkage())
1608 return true;
Jim Grosbach1900c732011-03-29 23:20:22 +00001609
Jim Laskey313570f2006-03-07 22:00:35 +00001610 if (!GV->hasAppendingLinkage()) return false;
1611
1612 assert(GV->hasInitializer() && "Not a special LLVM global!");
Jim Grosbach1900c732011-03-29 23:20:22 +00001613
Chris Lattner317293b2009-03-09 05:52:15 +00001614 if (GV->getName() == "llvm.global_ctors") {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001615 EmitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(),
1616 /* isCtor */ true);
Jim Grosbach1900c732011-03-29 23:20:22 +00001617
Chris Lattner126dab22009-03-09 08:18:48 +00001618 return true;
Jim Grosbach1900c732011-03-29 23:20:22 +00001619 }
1620
Chris Lattner317293b2009-03-09 05:52:15 +00001621 if (GV->getName() == "llvm.global_dtors") {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001622 EmitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(),
1623 /* isCtor */ false);
Chris Lattnere9d28b12010-01-19 04:34:02 +00001624
Chris Lattner126dab22009-03-09 08:18:48 +00001625 return true;
Chris Lattnerf0e9aef2005-12-13 06:32:10 +00001626 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001627
Rafael Espindolae64619c2016-05-16 21:14:24 +00001628 report_fatal_error("unknown special variable");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +00001629}
1630
Chris Lattnere9a75a62009-08-22 21:43:10 +00001631/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesenabb1e772008-09-09 22:29:13 +00001632/// global in the specified llvm.used list for which emitUsedDirectiveFor
1633/// is true, as being used with this directive.
Rafael Espindola74f2e462013-04-22 14:58:02 +00001634void AsmPrinter::EmitLLVMUsedList(const ConstantArray *InitList) {
Dan Gohman4fe64de2009-06-14 23:30:43 +00001635 // Should be an array of 'i8*'.
Chris Lattner66af3902006-09-26 03:38:18 +00001636 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner1fcac9f2009-07-17 22:00:23 +00001637 const GlobalValue *GV =
1638 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Rafael Espindola3b30cb42014-03-06 22:47:08 +00001639 if (GV)
Lang Hames9ff69c82015-04-24 19:11:51 +00001640 OutStreamer->EmitSymbolAttribute(getSymbol(GV), MCSA_NoDeadStrip);
Chris Lattner66af3902006-09-26 03:38:18 +00001641 }
1642}
1643
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001644namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001645
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001646struct Structor {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001647 int Priority = 0;
1648 Constant *Func = nullptr;
1649 GlobalValue *ComdatKey = nullptr;
1650
1651 Structor() = default;
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001652};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001653
1654} // end anonymous namespace
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001655
Duncan Sands4d635422011-08-28 13:17:22 +00001656/// EmitXXStructorList - Emit the ctor or dtor list taking into account the init
1657/// priority.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001658void AsmPrinter::EmitXXStructorList(const DataLayout &DL, const Constant *List,
1659 bool isCtor) {
Chris Lattnerf0e9aef2005-12-13 06:32:10 +00001660 // Should be an array of '{ int, void ()* }' structs. The first value is the
Duncan Sands4d635422011-08-28 13:17:22 +00001661 // init priority.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +00001662 if (!isa<ConstantArray>(List)) return;
Chris Lattner434ffe42005-12-21 01:17:37 +00001663
Duncan Sands4d635422011-08-28 13:17:22 +00001664 // Sanity check the structors list.
1665 const ConstantArray *InitList = dyn_cast<ConstantArray>(List);
1666 if (!InitList) return; // Not an array!
1667 StructType *ETy = dyn_cast<StructType>(InitList->getType()->getElementType());
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001668 // FIXME: Only allow the 3-field form in LLVM 4.0.
1669 if (!ETy || ETy->getNumElements() < 2 || ETy->getNumElements() > 3)
1670 return; // Not an array of two or three elements!
Duncan Sands4d635422011-08-28 13:17:22 +00001671 if (!isa<IntegerType>(ETy->getTypeAtIndex(0U)) ||
1672 !isa<PointerType>(ETy->getTypeAtIndex(1U))) return; // Not (int, ptr).
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001673 if (ETy->getNumElements() == 3 && !isa<PointerType>(ETy->getTypeAtIndex(2U)))
1674 return; // Not (int, ptr, ptr).
Duncan Sands4d635422011-08-28 13:17:22 +00001675
1676 // Gather the structors in a form that's convenient for sorting by priority.
1677 SmallVector<Structor, 8> Structors;
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001678 for (Value *O : InitList->operands()) {
1679 ConstantStruct *CS = dyn_cast<ConstantStruct>(O);
Duncan Sands4d635422011-08-28 13:17:22 +00001680 if (!CS) continue; // Malformed.
1681 if (CS->getOperand(1)->isNullValue())
1682 break; // Found a null terminator, skip the rest.
1683 ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
1684 if (!Priority) continue; // Malformed.
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001685 Structors.push_back(Structor());
1686 Structor &S = Structors.back();
1687 S.Priority = Priority->getLimitedValue(65535);
1688 S.Func = CS->getOperand(1);
1689 if (ETy->getNumElements() == 3 && !CS->getOperand(2)->isNullValue())
Eric Christopher0b6537e2016-07-01 06:07:31 +00001690 S.ComdatKey =
1691 dyn_cast<GlobalValue>(CS->getOperand(2)->stripPointerCasts());
Duncan Sands4d635422011-08-28 13:17:22 +00001692 }
1693
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +00001694 // Emit the function pointers in the target-specific order
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001695 unsigned Align = Log2_32(DL.getPointerPrefAlignment());
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001696 std::stable_sort(Structors.begin(), Structors.end(),
1697 [](const Structor &L,
1698 const Structor &R) { return L.Priority < R.Priority; });
1699 for (Structor &S : Structors) {
1700 const TargetLoweringObjectFile &Obj = getObjFileLowering();
1701 const MCSymbol *KeySym = nullptr;
Hans Wennborg8e873322014-06-04 21:04:54 +00001702 if (GlobalValue *GV = S.ComdatKey) {
Teresa Johnson2d384ac2017-01-18 16:58:43 +00001703 if (GV->isDeclarationForLinker())
1704 // If the associated variable is not defined in this module
1705 // (it might be available_externally, or have been an
1706 // available_externally definition that was dropped by the
1707 // EliminateAvailableExternally pass), some other TU
Hans Wennborg8e873322014-06-04 21:04:54 +00001708 // will provide its dynamic initializer.
1709 continue;
1710
1711 KeySym = getSymbol(GV);
Reid Klecknerfceb76f2014-05-16 20:39:27 +00001712 }
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001713 MCSection *OutputSection =
Rafael Espindola0766ae02014-06-06 19:26:12 +00001714 (isCtor ? Obj.getStaticCtorSection(S.Priority, KeySym)
1715 : Obj.getStaticDtorSection(S.Priority, KeySym));
Lang Hames9ff69c82015-04-24 19:11:51 +00001716 OutStreamer->SwitchSection(OutputSection);
1717 if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +00001718 EmitAlignment(Align);
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001719 EmitXXStructor(DL, S.Func);
Anton Korobeynikov7722a2d2012-01-25 22:24:19 +00001720 }
Chris Lattnerf0e9aef2005-12-13 06:32:10 +00001721}
Chris Lattnerf2991ce2005-11-21 08:25:09 +00001722
Rafael Espindola0018a592013-10-16 01:49:05 +00001723void AsmPrinter::EmitModuleIdents(Module &M) {
1724 if (!MAI->hasIdentDirective())
1725 return;
1726
1727 if (const NamedMDNode *NMD = M.getNamedMetadata("llvm.ident")) {
1728 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001729 const MDNode *N = NMD->getOperand(i);
Adrian Prantlc4fd6b72014-02-11 21:23:02 +00001730 assert(N->getNumOperands() == 1 &&
Rafael Espindola0018a592013-10-16 01:49:05 +00001731 "llvm.ident metadata entry can have only one operand");
1732 const MDString *S = cast<MDString>(N->getOperand(0));
Lang Hames9ff69c82015-04-24 19:11:51 +00001733 OutStreamer->EmitIdent(S->getString());
Rafael Espindola0018a592013-10-16 01:49:05 +00001734 }
1735 }
1736}
1737
Jim Laskey1c055e82007-01-25 15:12:02 +00001738//===--------------------------------------------------------------------===//
1739// Emission and print routines
1740//
1741
Jim Laskey1c055e82007-01-25 15:12:02 +00001742/// EmitInt8 - Emit a byte directive and value.
1743///
1744void AsmPrinter::EmitInt8(int Value) const {
Lang Hames9ff69c82015-04-24 19:11:51 +00001745 OutStreamer->EmitIntValue(Value, 1);
Jim Laskey1c055e82007-01-25 15:12:02 +00001746}
1747
1748/// EmitInt16 - Emit a short directive and value.
1749///
1750void AsmPrinter::EmitInt16(int Value) const {
Lang Hames9ff69c82015-04-24 19:11:51 +00001751 OutStreamer->EmitIntValue(Value, 2);
Jim Laskey1c055e82007-01-25 15:12:02 +00001752}
1753
1754/// EmitInt32 - Emit a long directive and value.
1755///
1756void AsmPrinter::EmitInt32(int Value) const {
Lang Hames9ff69c82015-04-24 19:11:51 +00001757 OutStreamer->EmitIntValue(Value, 4);
Jim Laskey1c055e82007-01-25 15:12:02 +00001758}
1759
Rafael Espindolac606bfe2014-10-21 01:17:30 +00001760/// Emit something like ".long Hi-Lo" where the size in bytes of the directive
1761/// is specified by Size and Hi/Lo specify the labels. This implicitly uses
1762/// .set if it avoids relocations.
Chris Lattner9889c1e2010-03-08 23:58:37 +00001763void AsmPrinter::EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
1764 unsigned Size) const {
Rafael Espindola7c6e6e42015-06-11 18:58:08 +00001765 OutStreamer->emitAbsoluteSymbolDiff(Hi, Lo, Size);
Chris Lattner9889c1e2010-03-08 23:58:37 +00001766}
1767
Jim Grosbach1900c732011-03-29 23:20:22 +00001768/// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
Devang Patelda3ef852010-09-02 16:43:44 +00001769/// where the size in bytes of the directive is specified by Size and Label
1770/// specifies the label. This implicitly uses .set if it is available.
1771void AsmPrinter::EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
Eric Christopherf8da6aa2013-11-23 00:05:06 +00001772 unsigned Size,
1773 bool IsSectionRelative) const {
Matt Arsenault40dddd72013-10-03 19:50:01 +00001774 if (MAI->needsDwarfSectionOffsetDirective() && IsSectionRelative) {
Keno Fischerf7d84ee2017-01-02 03:00:19 +00001775 OutStreamer->EmitCOFFSecRel32(Label, Offset);
1776 if (Size > 4)
1777 OutStreamer->EmitZeros(Size - 4);
Carlo Kok4382da92013-08-02 16:14:15 +00001778 return;
1779 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001780
Nick Lewycky33da3362012-06-22 01:25:12 +00001781 // Emit Label+Offset (or just Label if Offset is zero)
Jim Grosbach13760bd2015-05-30 01:25:56 +00001782 const MCExpr *Expr = MCSymbolRefExpr::create(Label, OutContext);
Nick Lewycky33da3362012-06-22 01:25:12 +00001783 if (Offset)
Jim Grosbach13760bd2015-05-30 01:25:56 +00001784 Expr = MCBinaryExpr::createAdd(
1785 Expr, MCConstantExpr::create(Offset, OutContext), OutContext);
Jim Grosbach1900c732011-03-29 23:20:22 +00001786
Lang Hames9ff69c82015-04-24 19:11:51 +00001787 OutStreamer->EmitValue(Expr, Size);
Devang Patelda3ef852010-09-02 16:43:44 +00001788}
Jim Grosbach1900c732011-03-29 23:20:22 +00001789
Jim Laskey1c055e82007-01-25 15:12:02 +00001790//===----------------------------------------------------------------------===//
1791
Chris Lattner3e3ff302007-05-31 18:57:45 +00001792// EmitAlignment - Emit an alignment directive to the specified power of
1793// two boundary. For example, if you pass in 3 here, you will get an 8
1794// byte alignment. If a global value is specified, and if that global has
Chris Lattner08e9e722010-04-28 19:58:07 +00001795// an explicit alignment requested, it will override the alignment request
1796// if required for correctness.
Chris Lattner3e3ff302007-05-31 18:57:45 +00001797//
Rafael Espindola99e05cf2014-05-13 18:45:48 +00001798void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalObject *GV) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001799 if (GV)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001800 NumBits = getGVAlignmentLog2(GV, GV->getParent()->getDataLayout(), NumBits);
Jim Grosbach1900c732011-03-29 23:20:22 +00001801
Chris Lattner08e9e722010-04-28 19:58:07 +00001802 if (NumBits == 0) return; // 1-byte aligned: no need to emit alignment.
Jim Grosbach1900c732011-03-29 23:20:22 +00001803
Aaron Ballman0d6a0102014-12-16 14:04:11 +00001804 assert(NumBits <
1805 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00001806 "undefined behavior");
Chris Lattner0adae252009-08-18 06:15:16 +00001807 if (getCurrentSection()->getKind().isText())
Lang Hames9ff69c82015-04-24 19:11:51 +00001808 OutStreamer->EmitCodeAlignment(1u << NumBits);
Chris Lattnere4f249b2010-02-23 18:46:22 +00001809 else
Lang Hames9ff69c82015-04-24 19:11:51 +00001810 OutStreamer->EmitValueToAlignment(1u << NumBits);
Chris Lattner1d35c162004-08-17 19:14:29 +00001811}
David Greenefdd25192009-08-05 21:00:52 +00001812
Chris Lattner51003672010-04-20 06:20:21 +00001813//===----------------------------------------------------------------------===//
1814// Constant emission.
1815//===----------------------------------------------------------------------===//
1816
Matt Arsenaultdd3b77d2014-12-16 19:16:14 +00001817const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
1818 MCContext &Ctx = OutContext;
Jim Grosbach1900c732011-03-29 23:20:22 +00001819
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001820 if (CV->isNullValue() || isa<UndefValue>(CV))
Jim Grosbach13760bd2015-05-30 01:25:56 +00001821 return MCConstantExpr::create(0, Ctx);
Chris Lattner7e0c1642010-01-13 04:29:19 +00001822
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001823 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
Jim Grosbach13760bd2015-05-30 01:25:56 +00001824 return MCConstantExpr::create(CI->getZExtValue(), Ctx);
Jim Grosbach1900c732011-03-29 23:20:22 +00001825
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001826 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Jim Grosbach13760bd2015-05-30 01:25:56 +00001827 return MCSymbolRefExpr::create(getSymbol(GV), Ctx);
Bill Wendling0d323ae2010-08-18 18:41:13 +00001828
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001829 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV))
Jim Grosbach13760bd2015-05-30 01:25:56 +00001830 return MCSymbolRefExpr::create(GetBlockAddressSymbol(BA), Ctx);
Jim Grosbach1900c732011-03-29 23:20:22 +00001831
Chris Lattner7e0c1642010-01-13 04:29:19 +00001832 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
Craig Topper353eda42014-04-24 06:44:33 +00001833 if (!CE) {
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001834 llvm_unreachable("Unknown constant value to lower!");
Chris Lattner7e0c1642010-01-13 04:29:19 +00001835 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001836
Chris Lattner7e0c1642010-01-13 04:29:19 +00001837 switch (CE->getOpcode()) {
Dan Gohman4268d6a2010-02-08 22:02:38 +00001838 default:
1839 // If the code isn't optimized, there may be outstanding folding
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001840 // opportunities. Attempt to fold the expression using DataLayout as a
Dan Gohman4268d6a2010-02-08 22:02:38 +00001841 // last resort before giving up.
David Majnemerd536f232016-07-29 03:27:26 +00001842 if (Constant *C = ConstantFoldConstant(CE, getDataLayout()))
Dan Gohmane51b57b2010-02-08 22:19:11 +00001843 if (C != CE)
Matt Arsenaultdd3b77d2014-12-16 19:16:14 +00001844 return lowerConstant(C);
Dan Gohman23922872010-08-04 18:51:09 +00001845
1846 // Otherwise report the problem to the user.
1847 {
Alp Tokere69170a2014-06-26 22:52:05 +00001848 std::string S;
1849 raw_string_ostream OS(S);
Dan Gohman23922872010-08-04 18:51:09 +00001850 OS << "Unsupported expression in static initializer: ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00001851 CE->printAsOperand(OS, /*PrintType=*/false,
Matt Arsenaultdd3b77d2014-12-16 19:16:14 +00001852 !MF ? nullptr : MF->getFunction()->getParent());
Dan Gohman23922872010-08-04 18:51:09 +00001853 report_fatal_error(OS.str());
1854 }
Chris Lattner7e0c1642010-01-13 04:29:19 +00001855 case Instruction::GetElementPtr: {
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001856 // Generate a symbolic expression for the byte address
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001857 APInt OffsetAI(getDataLayout().getPointerTypeSizeInBits(CE->getType()), 0);
1858 cast<GEPOperator>(CE)->accumulateConstantOffset(getDataLayout(), OffsetAI);
Jim Grosbach1900c732011-03-29 23:20:22 +00001859
Matt Arsenaultdd3b77d2014-12-16 19:16:14 +00001860 const MCExpr *Base = lowerConstant(CE->getOperand(0));
Nuno Lopesb6ad9822012-12-30 16:25:48 +00001861 if (!OffsetAI)
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001862 return Base;
Jim Grosbach1900c732011-03-29 23:20:22 +00001863
Nuno Lopesb6ad9822012-12-30 16:25:48 +00001864 int64_t Offset = OffsetAI.getSExtValue();
Jim Grosbach13760bd2015-05-30 01:25:56 +00001865 return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx),
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001866 Ctx);
Chris Lattner7e0c1642010-01-13 04:29:19 +00001867 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001868
Chris Lattner7e0c1642010-01-13 04:29:19 +00001869 case Instruction::Trunc:
1870 // We emit the value and depend on the assembler to truncate the generated
1871 // expression properly. This is important for differences between
1872 // blockaddress labels. Since the two labels are in the same function, it
1873 // is reasonable to treat their delta as a 32-bit value.
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001874 LLVM_FALLTHROUGH;
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001875 case Instruction::BitCast:
Matt Arsenaultdd3b77d2014-12-16 19:16:14 +00001876 return lowerConstant(CE->getOperand(0));
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001877
1878 case Instruction::IntToPtr: {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001879 const DataLayout &DL = getDataLayout();
Eric Christopher8b770652015-01-26 19:03:15 +00001880
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001881 // Handle casts to pointers by changing them into casts to the appropriate
1882 // integer type. This promotes constant folding and simplifies this code.
1883 Constant *Op = CE->getOperand(0);
Matt Arsenaultb7689122013-10-21 20:03:54 +00001884 Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()),
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001885 false/*ZExt*/);
Matt Arsenaultdd3b77d2014-12-16 19:16:14 +00001886 return lowerConstant(Op);
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001887 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001888
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001889 case Instruction::PtrToInt: {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001890 const DataLayout &DL = getDataLayout();
Eric Christopher8b770652015-01-26 19:03:15 +00001891
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001892 // Support only foldable casts to/from pointers that can be eliminated by
1893 // changing the pointer to the appropriately sized integer type.
1894 Constant *Op = CE->getOperand(0);
Chris Lattner229907c2011-07-18 04:54:35 +00001895 Type *Ty = CE->getType();
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001896
Matt Arsenaultdd3b77d2014-12-16 19:16:14 +00001897 const MCExpr *OpExpr = lowerConstant(Op);
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001898
1899 // We can emit the pointer value into this slot if the slot is an
1900 // integer slot equal to the size of the pointer.
Matt Arsenault40dddd72013-10-03 19:50:01 +00001901 if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType()))
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001902 return OpExpr;
1903
1904 // Otherwise the pointer is smaller than the resultant integer, mask off
1905 // the high bits so we are sure to get a proper truncation if the input is
1906 // a constant expr.
Matt Arsenault40dddd72013-10-03 19:50:01 +00001907 unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType());
Jim Grosbach13760bd2015-05-30 01:25:56 +00001908 const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx);
1909 return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx);
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001910 }
Jim Grosbach1900c732011-03-29 23:20:22 +00001911
Peter Collingbourne265ebd72016-04-22 20:40:10 +00001912 case Instruction::Sub: {
1913 GlobalValue *LHSGV;
1914 APInt LHSOffset;
1915 if (IsConstantOffsetFromGlobal(CE->getOperand(0), LHSGV, LHSOffset,
1916 getDataLayout())) {
1917 GlobalValue *RHSGV;
1918 APInt RHSOffset;
1919 if (IsConstantOffsetFromGlobal(CE->getOperand(1), RHSGV, RHSOffset,
1920 getDataLayout())) {
Eric Christopher4367c7f2016-09-16 07:33:15 +00001921 const MCExpr *RelocExpr =
1922 getObjFileLowering().lowerRelativeReference(LHSGV, RHSGV, TM);
Peter Collingbourne265ebd72016-04-22 20:40:10 +00001923 if (!RelocExpr)
1924 RelocExpr = MCBinaryExpr::createSub(
1925 MCSymbolRefExpr::create(getSymbol(LHSGV), Ctx),
1926 MCSymbolRefExpr::create(getSymbol(RHSGV), Ctx), Ctx);
1927 int64_t Addend = (LHSOffset - RHSOffset).getSExtValue();
1928 if (Addend != 0)
1929 RelocExpr = MCBinaryExpr::createAdd(
1930 RelocExpr, MCConstantExpr::create(Addend, Ctx), Ctx);
1931 return RelocExpr;
1932 }
1933 }
1934 }
1935 // else fallthrough
1936
Dan Gohman227077d2010-02-09 00:02:37 +00001937 // The MC library also has a right-shift operator, but it isn't consistently
1938 // signed or unsigned between different targets.
Chris Lattner7e0c1642010-01-13 04:29:19 +00001939 case Instruction::Add:
Dan Gohman227077d2010-02-09 00:02:37 +00001940 case Instruction::Mul:
1941 case Instruction::SDiv:
1942 case Instruction::SRem:
1943 case Instruction::Shl:
Chris Lattner7e0c1642010-01-13 04:29:19 +00001944 case Instruction::And:
1945 case Instruction::Or:
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001946 case Instruction::Xor: {
Matt Arsenaultdd3b77d2014-12-16 19:16:14 +00001947 const MCExpr *LHS = lowerConstant(CE->getOperand(0));
1948 const MCExpr *RHS = lowerConstant(CE->getOperand(1));
Chris Lattner7e0c1642010-01-13 04:29:19 +00001949 switch (CE->getOpcode()) {
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001950 default: llvm_unreachable("Unknown binary operator constant cast expr");
Jim Grosbach13760bd2015-05-30 01:25:56 +00001951 case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx);
1952 case Instruction::Sub: return MCBinaryExpr::createSub(LHS, RHS, Ctx);
1953 case Instruction::Mul: return MCBinaryExpr::createMul(LHS, RHS, Ctx);
1954 case Instruction::SDiv: return MCBinaryExpr::createDiv(LHS, RHS, Ctx);
1955 case Instruction::SRem: return MCBinaryExpr::createMod(LHS, RHS, Ctx);
1956 case Instruction::Shl: return MCBinaryExpr::createShl(LHS, RHS, Ctx);
1957 case Instruction::And: return MCBinaryExpr::createAnd(LHS, RHS, Ctx);
1958 case Instruction::Or: return MCBinaryExpr::createOr (LHS, RHS, Ctx);
1959 case Instruction::Xor: return MCBinaryExpr::createXor(LHS, RHS, Ctx);
Chris Lattner7e0c1642010-01-13 04:29:19 +00001960 }
Chris Lattnerd1acffc2010-01-23 06:17:14 +00001961 }
Chris Lattner6a8e0f52004-08-16 23:15:22 +00001962 }
1963}
Chris Lattner8452a1f2004-08-17 06:36:49 +00001964
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001965static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *C,
1966 AsmPrinter &AP,
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00001967 const Constant *BaseCV = nullptr,
1968 uint64_t Offset = 0);
Chris Lattner51003672010-04-20 06:20:21 +00001969
Justin Bogner3135ba92015-12-08 02:37:48 +00001970static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP);
1971
David Greenecdef71f2011-08-31 17:30:56 +00001972/// isRepeatedByteSequence - Determine whether the given value is
1973/// composed of a repeated sequence of identical bytes and return the
1974/// byte value. If it is not a repeated sequence, return -1.
Chris Lattner5d4497b2012-01-24 09:31:43 +00001975static int isRepeatedByteSequence(const ConstantDataSequential *V) {
1976 StringRef Data = V->getRawDataValues();
1977 assert(!Data.empty() && "Empty aggregates should be CAZ node");
1978 char C = Data[0];
1979 for (unsigned i = 1, e = Data.size(); i != e; ++i)
1980 if (Data[i] != C) return -1;
Chandler Carruth2c469ff2012-01-30 23:47:44 +00001981 return static_cast<uint8_t>(C); // Ensure 255 is not returned as -1.
Chris Lattner5d4497b2012-01-24 09:31:43 +00001982}
1983
Chris Lattner5d4497b2012-01-24 09:31:43 +00001984/// isRepeatedByteSequence - Determine whether the given value is
1985/// composed of a repeated sequence of identical bytes and return the
1986/// byte value. If it is not a repeated sequence, return -1.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001987static int isRepeatedByteSequence(const Value *V, const DataLayout &DL) {
David Greenecdef71f2011-08-31 17:30:56 +00001988 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001989 uint64_t Size = DL.getTypeAllocSizeInBits(V->getType());
Benjamin Kramerc6e8bfc2015-06-17 23:55:17 +00001990 assert(Size % 8 == 0);
David Greenecdef71f2011-08-31 17:30:56 +00001991
Benjamin Kramerc6e8bfc2015-06-17 23:55:17 +00001992 // Extend the element to take zero padding into account.
1993 APInt Value = CI->getValue().zextOrSelf(Size);
1994 if (!Value.isSplat(8))
1995 return -1;
David Greenecdef71f2011-08-31 17:30:56 +00001996
Benjamin Kramerc6e8bfc2015-06-17 23:55:17 +00001997 return Value.zextOrTrunc(8).getZExtValue();
David Greenecdef71f2011-08-31 17:30:56 +00001998 }
1999 if (const ConstantArray *CA = dyn_cast<ConstantArray>(V)) {
2000 // Make sure all array elements are sequences of the same repeated
2001 // byte.
Chris Lattner5d4497b2012-01-24 09:31:43 +00002002 assert(CA->getNumOperands() != 0 && "Should be a CAZ");
Benjamin Kramerc6e8bfc2015-06-17 23:55:17 +00002003 Constant *Op0 = CA->getOperand(0);
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002004 int Byte = isRepeatedByteSequence(Op0, DL);
Benjamin Kramerc6e8bfc2015-06-17 23:55:17 +00002005 if (Byte == -1)
2006 return -1;
David Greenecdef71f2011-08-31 17:30:56 +00002007
Benjamin Kramerc6e8bfc2015-06-17 23:55:17 +00002008 // All array elements must be equal.
2009 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i)
2010 if (CA->getOperand(i) != Op0)
2011 return -1;
David Greenecdef71f2011-08-31 17:30:56 +00002012 return Byte;
2013 }
Eric Christopher7c718e42012-11-20 20:34:44 +00002014
Chris Lattner5d4497b2012-01-24 09:31:43 +00002015 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V))
2016 return isRepeatedByteSequence(CDS);
David Greenecdef71f2011-08-31 17:30:56 +00002017
2018 return -1;
2019}
2020
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002021static void emitGlobalConstantDataSequential(const DataLayout &DL,
2022 const ConstantDataSequential *CDS,
2023 AsmPrinter &AP) {
Chris Lattner5d4497b2012-01-24 09:31:43 +00002024 // See if we can aggregate this into a .fill, if so, emit it as such.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002025 int Value = isRepeatedByteSequence(CDS, DL);
Chris Lattner5d4497b2012-01-24 09:31:43 +00002026 if (Value != -1) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002027 uint64_t Bytes = DL.getTypeAllocSize(CDS->getType());
Chris Lattner0d3785e2012-01-31 03:39:24 +00002028 // Don't emit a 1-byte object as a .fill.
2029 if (Bytes > 1)
Petr Hosekfaef3202016-06-01 01:59:58 +00002030 return AP.OutStreamer->emitFill(Bytes, Value);
Chris Lattner5d4497b2012-01-24 09:31:43 +00002031 }
Eric Christopher7c718e42012-11-20 20:34:44 +00002032
Chris Lattner5d4497b2012-01-24 09:31:43 +00002033 // If this can be emitted with .ascii/.asciz, emit it as such.
2034 if (CDS->isString())
Lang Hames9ff69c82015-04-24 19:11:51 +00002035 return AP.OutStreamer->EmitBytes(CDS->getAsString());
Chris Lattner5d4497b2012-01-24 09:31:43 +00002036
2037 // Otherwise, emit the values in successive locations.
2038 unsigned ElementByteSize = CDS->getElementByteSize();
2039 if (isa<IntegerType>(CDS->getElementType())) {
Chris Lattner9be59592012-01-25 01:27:20 +00002040 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Chris Lattner829400b2012-01-30 05:55:11 +00002041 if (AP.isVerbose())
Lang Hames9ff69c82015-04-24 19:11:51 +00002042 AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64 "\n",
2043 CDS->getElementAsInteger(i));
2044 AP.OutStreamer->EmitIntValue(CDS->getElementAsInteger(i),
2045 ElementByteSize);
Chris Lattner5d4497b2012-01-24 09:31:43 +00002046 }
Chris Lattner51ebe142012-01-30 05:49:43 +00002047 } else {
Justin Bogner3135ba92015-12-08 02:37:48 +00002048 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I)
2049 emitGlobalConstantFP(cast<ConstantFP>(CDS->getElementAsConstant(I)), AP);
Chris Lattner5d4497b2012-01-24 09:31:43 +00002050 }
2051
Matt Arsenault40dddd72013-10-03 19:50:01 +00002052 unsigned Size = DL.getTypeAllocSize(CDS->getType());
2053 unsigned EmittedSize = DL.getTypeAllocSize(CDS->getType()->getElementType()) *
Chris Lattner51ebe142012-01-30 05:49:43 +00002054 CDS->getNumElements();
2055 if (unsigned Padding = Size - EmittedSize)
Lang Hames9ff69c82015-04-24 19:11:51 +00002056 AP.OutStreamer->EmitZeros(Padding);
Chris Lattner5d4497b2012-01-24 09:31:43 +00002057}
2058
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002059static void emitGlobalConstantArray(const DataLayout &DL,
2060 const ConstantArray *CA, AsmPrinter &AP,
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002061 const Constant *BaseCV, uint64_t Offset) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002062 // See if we can aggregate some values. Make sure it can be
2063 // represented as a series of bytes of the constant value.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002064 int Value = isRepeatedByteSequence(CA, DL);
David Greenecdef71f2011-08-31 17:30:56 +00002065
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002066 if (Value != -1) {
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002067 uint64_t Bytes = DL.getTypeAllocSize(CA->getType());
Petr Hosekfaef3202016-06-01 01:59:58 +00002068 AP.OutStreamer->emitFill(Bytes, Value);
Dan Gohman014caa42008-12-22 21:14:27 +00002069 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002070 else {
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002071 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002072 emitGlobalConstantImpl(DL, CA->getOperand(i), AP, BaseCV, Offset);
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002073 Offset += DL.getTypeAllocSize(CA->getOperand(i)->getType());
2074 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002075 }
Dan Gohman014caa42008-12-22 21:14:27 +00002076}
2077
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002078static void emitGlobalConstantVector(const DataLayout &DL,
2079 const ConstantVector *CV, AsmPrinter &AP) {
Chris Lattner71601e82010-01-20 07:41:15 +00002080 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002081 emitGlobalConstantImpl(DL, CV->getOperand(i), AP);
Nick Lewycky6208a2f2011-06-22 18:55:03 +00002082
Matt Arsenault40dddd72013-10-03 19:50:01 +00002083 unsigned Size = DL.getTypeAllocSize(CV->getType());
2084 unsigned EmittedSize = DL.getTypeAllocSize(CV->getType()->getElementType()) *
Nick Lewycky6208a2f2011-06-22 18:55:03 +00002085 CV->getType()->getNumElements();
2086 if (unsigned Padding = Size - EmittedSize)
Lang Hames9ff69c82015-04-24 19:11:51 +00002087 AP.OutStreamer->EmitZeros(Padding);
Dan Gohman014caa42008-12-22 21:14:27 +00002088}
2089
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002090static void emitGlobalConstantStruct(const DataLayout &DL,
2091 const ConstantStruct *CS, AsmPrinter &AP,
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002092 const Constant *BaseCV, uint64_t Offset) {
Dan Gohman014caa42008-12-22 21:14:27 +00002093 // Print the fields in successive locations. Pad to align if needed!
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002094 unsigned Size = DL.getTypeAllocSize(CS->getType());
2095 const StructLayout *Layout = DL.getStructLayout(CS->getType());
Chris Lattnerfefcb152010-01-19 19:10:44 +00002096 uint64_t SizeSoFar = 0;
2097 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattner71601e82010-01-20 07:41:15 +00002098 const Constant *Field = CS->getOperand(i);
Dan Gohman014caa42008-12-22 21:14:27 +00002099
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002100 // Print the actual field value.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002101 emitGlobalConstantImpl(DL, Field, AP, BaseCV, Offset + SizeSoFar);
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002102
Dan Gohman014caa42008-12-22 21:14:27 +00002103 // Check if padding is needed and insert one or more 0s.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002104 uint64_t FieldSize = DL.getTypeAllocSize(Field->getType());
Chris Lattner03cb2a302010-01-20 07:11:32 +00002105 uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1))
2106 - Layout->getElementOffset(i)) - FieldSize;
2107 SizeSoFar += FieldSize + PadSize;
Dan Gohman014caa42008-12-22 21:14:27 +00002108
Dan Gohman014caa42008-12-22 21:14:27 +00002109 // Insert padding - this may include padding to increase the size of the
2110 // current field up to the ABI size (if the struct is not packed) as well
2111 // as padding to ensure that the next field starts at the right offset.
Lang Hames9ff69c82015-04-24 19:11:51 +00002112 AP.OutStreamer->EmitZeros(PadSize);
Dan Gohman014caa42008-12-22 21:14:27 +00002113 }
Chris Lattner03cb2a302010-01-20 07:11:32 +00002114 assert(SizeSoFar == Layout->getSizeInBytes() &&
Dan Gohman014caa42008-12-22 21:14:27 +00002115 "Layout of constant struct may be incorrect!");
2116}
2117
Rafael Espindola64e1af82013-07-02 15:49:13 +00002118static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP) {
Chris Lattnereaef5e12010-01-19 22:16:33 +00002119 APInt API = CFP->getValueAPF().bitcastToAPInt();
Tim Northover3a51aab2013-01-11 10:36:13 +00002120
2121 // First print a comment with what we think the original floating-point value
2122 // should have been.
2123 if (AP.isVerbose()) {
2124 SmallString<8> StrVal;
2125 CFP->getValueAPF().toString(StrVal);
2126
Richard Trieuc1485222014-06-21 02:43:02 +00002127 if (CFP->getType())
Lang Hames9ff69c82015-04-24 19:11:51 +00002128 CFP->getType()->print(AP.OutStreamer->GetCommentOS());
Richard Trieuc1485222014-06-21 02:43:02 +00002129 else
Lang Hames9ff69c82015-04-24 19:11:51 +00002130 AP.OutStreamer->GetCommentOS() << "Printing <null> Type";
2131 AP.OutStreamer->GetCommentOS() << ' ' << StrVal << '\n';
Chris Lattnereaef5e12010-01-19 22:16:33 +00002132 }
Tim Northover3a51aab2013-01-11 10:36:13 +00002133
2134 // Now iterate through the APInt chunks, emitting them in endian-correct
2135 // order, possibly with a smaller chunk at beginning/end (e.g. for x87 80-bit
2136 // floats).
2137 unsigned NumBytes = API.getBitWidth() / 8;
2138 unsigned TrailingBytes = NumBytes % sizeof(uint64_t);
2139 const uint64_t *p = API.getRawData();
2140
2141 // PPC's long double has odd notions of endianness compared to how LLVM
2142 // handles it: p[0] goes first for *big* endian on PPC.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002143 if (AP.getDataLayout().isBigEndian() && !CFP->getType()->isPPC_FP128Ty()) {
Tim Northover3a51aab2013-01-11 10:36:13 +00002144 int Chunk = API.getNumWords() - 1;
2145
2146 if (TrailingBytes)
Lang Hames9ff69c82015-04-24 19:11:51 +00002147 AP.OutStreamer->EmitIntValue(p[Chunk--], TrailingBytes);
Tim Northover3a51aab2013-01-11 10:36:13 +00002148
2149 for (; Chunk >= 0; --Chunk)
Lang Hames9ff69c82015-04-24 19:11:51 +00002150 AP.OutStreamer->EmitIntValue(p[Chunk], sizeof(uint64_t));
Tim Northover3a51aab2013-01-11 10:36:13 +00002151 } else {
2152 unsigned Chunk;
2153 for (Chunk = 0; Chunk < NumBytes / sizeof(uint64_t); ++Chunk)
Lang Hames9ff69c82015-04-24 19:11:51 +00002154 AP.OutStreamer->EmitIntValue(p[Chunk], sizeof(uint64_t));
Tim Northover3a51aab2013-01-11 10:36:13 +00002155
2156 if (TrailingBytes)
Lang Hames9ff69c82015-04-24 19:11:51 +00002157 AP.OutStreamer->EmitIntValue(p[Chunk], TrailingBytes);
Tim Northover3a51aab2013-01-11 10:36:13 +00002158 }
2159
2160 // Emit the tail padding for the long double.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002161 const DataLayout &DL = AP.getDataLayout();
Lang Hames9ff69c82015-04-24 19:11:51 +00002162 AP.OutStreamer->EmitZeros(DL.getTypeAllocSize(CFP->getType()) -
2163 DL.getTypeStoreSize(CFP->getType()));
Dan Gohman014caa42008-12-22 21:14:27 +00002164}
2165
Rafael Espindola64e1af82013-07-02 15:49:13 +00002166static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002167 const DataLayout &DL = AP.getDataLayout();
Dan Gohman014caa42008-12-22 21:14:27 +00002168 unsigned BitWidth = CI->getBitWidth();
Quentin Colombetba366012013-06-07 18:36:03 +00002169
2170 // Copy the value as we may massage the layout for constants whose bit width
2171 // is not a multiple of 64-bits.
2172 APInt Realigned(CI->getValue());
2173 uint64_t ExtraBits = 0;
2174 unsigned ExtraBitsSize = BitWidth & 63;
2175
2176 if (ExtraBitsSize) {
2177 // The bit width of the data is not a multiple of 64-bits.
2178 // The extra bits are expected to be at the end of the chunk of the memory.
2179 // Little endian:
2180 // * Nothing to be done, just record the extra bits to emit.
2181 // Big endian:
2182 // * Record the extra bits to emit.
2183 // * Realign the raw data to emit the chunks of 64-bits.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002184 if (DL.isBigEndian()) {
Quentin Colombetba366012013-06-07 18:36:03 +00002185 // Basically the structure of the raw data is a chunk of 64-bits cells:
2186 // 0 1 BitWidth / 64
2187 // [chunk1][chunk2] ... [chunkN].
2188 // The most significant chunk is chunkN and it should be emitted first.
2189 // However, due to the alignment issue chunkN contains useless bits.
2190 // Realign the chunks so that they contain only useless information:
2191 // ExtraBits 0 1 (BitWidth / 64) - 1
2192 // chu[nk1 chu][nk2 chu] ... [nkN-1 chunkN]
2193 ExtraBits = Realigned.getRawData()[0] &
2194 (((uint64_t)-1) >> (64 - ExtraBitsSize));
2195 Realigned = Realigned.lshr(ExtraBitsSize);
2196 } else
2197 ExtraBits = Realigned.getRawData()[BitWidth / 64];
2198 }
Dan Gohman014caa42008-12-22 21:14:27 +00002199
2200 // We don't expect assemblers to support integer data directives
2201 // for more than 64 bits, so we emit the data in at most 64-bit
2202 // quantities at a time.
Quentin Colombetba366012013-06-07 18:36:03 +00002203 const uint64_t *RawData = Realigned.getRawData();
Dan Gohman014caa42008-12-22 21:14:27 +00002204 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002205 uint64_t Val = DL.isBigEndian() ? RawData[e - i - 1] : RawData[i];
Lang Hames9ff69c82015-04-24 19:11:51 +00002206 AP.OutStreamer->EmitIntValue(Val, 8);
Dan Gohman014caa42008-12-22 21:14:27 +00002207 }
Quentin Colombetba366012013-06-07 18:36:03 +00002208
2209 if (ExtraBitsSize) {
2210 // Emit the extra bits after the 64-bits chunks.
2211
2212 // Emit a directive that fills the expected size.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002213 uint64_t Size = AP.getDataLayout().getTypeAllocSize(CI->getType());
Quentin Colombetba366012013-06-07 18:36:03 +00002214 Size -= (BitWidth / 64) * 8;
2215 assert(Size && Size * 8 >= ExtraBitsSize &&
2216 (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize)))
2217 == ExtraBits && "Directive too small for extra bits.");
Lang Hames9ff69c82015-04-24 19:11:51 +00002218 AP.OutStreamer->EmitIntValue(ExtraBits, Size);
Quentin Colombetba366012013-06-07 18:36:03 +00002219 }
Dan Gohman014caa42008-12-22 21:14:27 +00002220}
2221
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002222/// \brief Transform a not absolute MCExpr containing a reference to a GOT
2223/// equivalent global, by a target specific GOT pc relative access to the
2224/// final symbol.
2225static void handleIndirectSymViaGOTPCRel(AsmPrinter &AP, const MCExpr **ME,
2226 const Constant *BaseCst,
2227 uint64_t Offset) {
2228 // The global @foo below illustrates a global that uses a got equivalent.
2229 //
2230 // @bar = global i32 42
2231 // @gotequiv = private unnamed_addr constant i32* @bar
2232 // @foo = i32 trunc (i64 sub (i64 ptrtoint (i32** @gotequiv to i64),
2233 // i64 ptrtoint (i32* @foo to i64))
2234 // to i32)
2235 //
2236 // The cstexpr in @foo is converted into the MCExpr `ME`, where we actually
2237 // check whether @foo is suitable to use a GOTPCREL. `ME` is usually in the
2238 // form:
2239 //
2240 // foo = cstexpr, where
2241 // cstexpr := <gotequiv> - "." + <cst>
2242 // cstexpr := <gotequiv> - (<foo> - <offset from @foo base>) + <cst>
2243 //
Jim Grosbach13760bd2015-05-30 01:25:56 +00002244 // After canonicalization by evaluateAsRelocatable `ME` turns into:
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002245 //
2246 // cstexpr := <gotequiv> - <foo> + gotpcrelcst, where
2247 // gotpcrelcst := <offset from @foo base> + <cst>
2248 //
2249 MCValue MV;
Jim Grosbach13760bd2015-05-30 01:25:56 +00002250 if (!(*ME)->evaluateAsRelocatable(MV, nullptr, nullptr) || MV.isAbsolute())
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002251 return;
Bruno Cardoso Lopesedb876d2015-06-25 15:17:23 +00002252 const MCSymbolRefExpr *SymA = MV.getSymA();
2253 if (!SymA)
2254 return;
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002255
Bruno Cardoso Lopesedb876d2015-06-25 15:17:23 +00002256 // Check that GOT equivalent symbol is cached.
2257 const MCSymbol *GOTEquivSym = &SymA->getSymbol();
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002258 if (!AP.GlobalGOTEquivs.count(GOTEquivSym))
2259 return;
2260
Bruno Cardoso Lopese8640512015-07-21 21:45:42 +00002261 const GlobalValue *BaseGV = dyn_cast_or_null<GlobalValue>(BaseCst);
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002262 if (!BaseGV)
2263 return;
2264
Bruno Cardoso Lopesedb876d2015-06-25 15:17:23 +00002265 // Check for a valid base symbol
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002266 const MCSymbol *BaseSym = AP.getSymbol(BaseGV);
Bruno Cardoso Lopesedb876d2015-06-25 15:17:23 +00002267 const MCSymbolRefExpr *SymB = MV.getSymB();
2268
2269 if (!SymB || BaseSym != &SymB->getSymbol())
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002270 return;
2271
2272 // Make sure to match:
2273 //
2274 // gotpcrelcst := <offset from @foo base> + <cst>
2275 //
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +00002276 // If gotpcrelcst is positive it means that we can safely fold the pc rel
2277 // displacement into the GOTPCREL. We can also can have an extra offset <cst>
2278 // if the target knows how to encode it.
Bruno Cardoso Lopes52b13912015-03-06 13:48:45 +00002279 //
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002280 int64_t GOTPCRelCst = Offset + MV.getConstant();
2281 if (GOTPCRelCst < 0)
2282 return;
Bruno Cardoso Lopes52b13912015-03-06 13:48:45 +00002283 if (!AP.getObjFileLowering().supportGOTPCRelWithOffset() && GOTPCRelCst != 0)
2284 return;
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002285
2286 // Emit the GOT PC relative to replace the got equivalent global, i.e.:
2287 //
2288 // bar:
2289 // .long 42
2290 // gotequiv:
2291 // .quad bar
2292 // foo:
2293 // .long gotequiv - "." + <cst>
2294 //
2295 // is replaced by the target specific equivalent to:
2296 //
2297 // bar:
2298 // .long 42
2299 // foo:
2300 // .long bar@GOTPCREL+<gotpcrelcst>
2301 //
2302 AsmPrinter::GOTEquivUsePair Result = AP.GlobalGOTEquivs[GOTEquivSym];
2303 const GlobalVariable *GV = Result.first;
Bruno Cardoso Lopesb3a58b42015-03-10 20:05:23 +00002304 int NumUses = (int)Result.second;
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002305 const GlobalValue *FinalGV = dyn_cast<GlobalValue>(GV->getOperand(0));
2306 const MCSymbol *FinalSym = AP.getSymbol(FinalGV);
Bruno Cardoso Lopes618c67a2015-03-06 13:49:05 +00002307 *ME = AP.getObjFileLowering().getIndirectSymViaGOTPCRel(
Lang Hames9ff69c82015-04-24 19:11:51 +00002308 FinalSym, MV, Offset, AP.MMI, *AP.OutStreamer);
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002309
2310 // Update GOT equivalent usage information
2311 --NumUses;
Bruno Cardoso Lopesb3a58b42015-03-10 20:05:23 +00002312 if (NumUses >= 0)
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002313 AP.GlobalGOTEquivs[GOTEquivSym] = std::make_pair(GV, NumUses);
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002314}
2315
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002316static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
2317 AsmPrinter &AP, const Constant *BaseCV,
2318 uint64_t Offset) {
2319 uint64_t Size = DL.getTypeAllocSize(CV->getType());
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002320
2321 // Globals with sub-elements such as combinations of arrays and structs
2322 // are handled recursively by emitGlobalConstantImpl. Keep track of the
2323 // constant symbol base and the current position with BaseCV and Offset.
2324 if (!BaseCV && CV->hasOneUse())
2325 BaseCV = dyn_cast<Constant>(CV->user_back());
2326
Bill Wendling7315c4b2012-03-20 08:56:43 +00002327 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
Lang Hames9ff69c82015-04-24 19:11:51 +00002328 return AP.OutStreamer->EmitZeros(Size);
Chris Lattner03cb2a302010-01-20 07:11:32 +00002329
2330 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner03cb2a302010-01-20 07:11:32 +00002331 switch (Size) {
2332 case 1:
2333 case 2:
2334 case 4:
2335 case 8:
Chris Lattner51003672010-04-20 06:20:21 +00002336 if (AP.isVerbose())
Lang Hames9ff69c82015-04-24 19:11:51 +00002337 AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64 "\n",
2338 CI->getZExtValue());
2339 AP.OutStreamer->EmitIntValue(CI->getZExtValue(), Size);
Chris Lattner03cb2a302010-01-20 07:11:32 +00002340 return;
2341 default:
Rafael Espindola64e1af82013-07-02 15:49:13 +00002342 emitGlobalConstantLargeInt(CI, AP);
Chris Lattner03cb2a302010-01-20 07:11:32 +00002343 return;
2344 }
2345 }
Jim Grosbach1900c732011-03-29 23:20:22 +00002346
Chris Lattnerfefcb152010-01-19 19:10:44 +00002347 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
Rafael Espindola64e1af82013-07-02 15:49:13 +00002348 return emitGlobalConstantFP(CFP, AP);
Chris Lattner8452a1f2004-08-17 06:36:49 +00002349
Chris Lattnerc47ece52010-01-20 17:57:50 +00002350 if (isa<ConstantPointerNull>(CV)) {
Lang Hames9ff69c82015-04-24 19:11:51 +00002351 AP.OutStreamer->EmitIntValue(0, Size);
Chris Lattnerc47ece52010-01-20 17:57:50 +00002352 return;
2353 }
Jim Grosbach1900c732011-03-29 23:20:22 +00002354
Chris Lattner5d4497b2012-01-24 09:31:43 +00002355 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(CV))
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002356 return emitGlobalConstantDataSequential(DL, CDS, AP);
Eric Christopher7c718e42012-11-20 20:34:44 +00002357
Chris Lattner5d4497b2012-01-24 09:31:43 +00002358 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002359 return emitGlobalConstantArray(DL, CVA, AP, BaseCV, Offset);
Jim Grosbach1900c732011-03-29 23:20:22 +00002360
Chris Lattner5d4497b2012-01-24 09:31:43 +00002361 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002362 return emitGlobalConstantStruct(DL, CVS, AP, BaseCV, Offset);
Chris Lattner5d4497b2012-01-24 09:31:43 +00002363
Bill Wendling7315c4b2012-03-20 08:56:43 +00002364 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
2365 // Look through bitcasts, which might not be able to be MCExpr'ized (e.g. of
2366 // vectors).
Chris Lattner5d4497b2012-01-24 09:31:43 +00002367 if (CE->getOpcode() == Instruction::BitCast)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002368 return emitGlobalConstantImpl(DL, CE->getOperand(0), AP);
Bill Wendling7315c4b2012-03-20 08:56:43 +00002369
2370 if (Size > 8) {
2371 // If the constant expression's size is greater than 64-bits, then we have
2372 // to emit the value in chunks. Try to constant fold the value and emit it
2373 // that way.
David Majnemerd536f232016-07-29 03:27:26 +00002374 Constant *New = ConstantFoldConstant(CE, DL);
Bill Wendling7315c4b2012-03-20 08:56:43 +00002375 if (New && New != CE)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002376 return emitGlobalConstantImpl(DL, New, AP);
Bill Wendling7315c4b2012-03-20 08:56:43 +00002377 }
2378 }
Eric Christopher7c718e42012-11-20 20:34:44 +00002379
Chris Lattner5d4497b2012-01-24 09:31:43 +00002380 if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002381 return emitGlobalConstantVector(DL, V, AP);
Eric Christopher7c718e42012-11-20 20:34:44 +00002382
Chris Lattnerd1acffc2010-01-23 06:17:14 +00002383 // Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
2384 // thread the streamer with EmitValue.
Bruno Cardoso Lopes24492b02015-02-23 21:26:18 +00002385 const MCExpr *ME = AP.lowerConstant(CV);
2386
2387 // Since lowerConstant already folded and got rid of all IR pointer and
2388 // integer casts, detect GOT equivalent accesses by looking into the MCExpr
2389 // directly.
2390 if (AP.getObjFileLowering().supportIndirectSymViaGOTPCRel())
2391 handleIndirectSymViaGOTPCRel(AP, &ME, BaseCV, Offset);
2392
Lang Hames9ff69c82015-04-24 19:11:51 +00002393 AP.OutStreamer->EmitValue(ME, Size);
Chris Lattner51003672010-04-20 06:20:21 +00002394}
2395
2396/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002397void AsmPrinter::EmitGlobalConstant(const DataLayout &DL, const Constant *CV) {
2398 uint64_t Size = DL.getTypeAllocSize(CV->getType());
Chris Lattner51003672010-04-20 06:20:21 +00002399 if (Size)
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002400 emitGlobalConstantImpl(DL, CV, *this);
Chris Lattner51003672010-04-20 06:20:21 +00002401 else if (MAI->hasSubsectionsViaSymbols()) {
2402 // If the global has zero size, emit a single byte so that two labels don't
2403 // look like they are at the same location.
Lang Hames9ff69c82015-04-24 19:11:51 +00002404 OutStreamer->EmitIntValue(0, 1);
Chris Lattner51003672010-04-20 06:20:21 +00002405 }
Chris Lattner8452a1f2004-08-17 06:36:49 +00002406}
Chris Lattner061d9e22006-01-27 02:10:10 +00002407
Chris Lattner17f71652008-08-17 07:19:36 +00002408void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00002409 // Target doesn't support this yet!
Torok Edwinfbcc6632009-07-14 16:55:14 +00002410 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Cheng45fe3bc2006-09-12 21:00:35 +00002411}
2412
Chris Lattnerc334d802010-04-04 18:16:38 +00002413void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const {
2414 if (Offset > 0)
2415 OS << '+' << Offset;
2416 else if (Offset < 0)
2417 OS << Offset;
2418}
2419
Chris Lattnera179b522010-04-04 19:25:43 +00002420//===----------------------------------------------------------------------===//
2421// Symbol Lowering Routines.
2422//===----------------------------------------------------------------------===//
2423
Rafael Espindola9ab09232015-03-17 20:07:06 +00002424MCSymbol *AsmPrinter::createTempSymbol(const Twine &Name) const {
2425 return OutContext.createTempSymbol(Name, true);
Rafael Espindola629cdba2015-02-27 18:18:39 +00002426}
Chris Lattneraa23fa92006-02-01 22:41:11 +00002427
Chris Lattnerebc970e2010-02-08 23:10:08 +00002428MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const {
Chris Lattner9efbbcb2010-03-14 17:53:23 +00002429 return MMI->getAddrLabelSymbol(BA->getBasicBlock());
Dan Gohman6c938802009-10-30 01:27:03 +00002430}
2431
Chris Lattner9efbbcb2010-03-14 17:53:23 +00002432MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BasicBlock *BB) const {
2433 return MMI->getAddrLabelSymbol(BB);
Dan Gohman6c938802009-10-30 01:27:03 +00002434}
2435
Chris Lattner4225a7b2010-01-23 05:19:23 +00002436/// GetCPISymbol - Return the symbol for the specified constant pool entry.
2437MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002438 const DataLayout &DL = getDataLayout();
2439 return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
2440 "CPI" + Twine(getFunctionNumber()) + "_" +
2441 Twine(CPID));
Chris Lattner4225a7b2010-01-23 05:19:23 +00002442}
2443
2444/// GetJTISymbol - Return the symbol for the specified jump table entry.
Bill Wendling36321712010-06-29 22:34:52 +00002445MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const {
2446 return MF->getJTISymbol(JTID, OutContext, isLinkerPrivate);
Chris Lattnera602beb2009-09-12 23:02:08 +00002447}
2448
Chris Lattner00d188f2010-01-25 21:17:10 +00002449/// GetJTSetSymbol - Return the symbol for the specified jump table .set
2450/// FIXME: privatize to AsmPrinter.
2451MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002452 const DataLayout &DL = getDataLayout();
2453 return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
2454 Twine(getFunctionNumber()) + "_" +
2455 Twine(UID) + "_set_" + Twine(MBBID));
Chris Lattner00d188f2010-01-25 21:17:10 +00002456}
2457
Rafael Espindolaf4e6b292013-12-02 16:25:47 +00002458MCSymbol *AsmPrinter::getSymbolWithGlobalValueBase(const GlobalValue *GV,
Rafael Espindola61b3d0c2013-11-28 19:35:07 +00002459 StringRef Suffix) const {
Eric Christopher4367c7f2016-09-16 07:33:15 +00002460 return getObjFileLowering().getSymbolWithGlobalValueBase(GV, Suffix, TM);
Chris Lattner0dca0a92010-01-15 23:25:11 +00002461}
2462
Rafael Espindolac233f742015-06-23 13:59:29 +00002463/// Return the MCSymbol for the specified ExternalSymbol.
Chris Lattner91687f62010-01-15 23:18:17 +00002464MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
2465 SmallString<60> NameStr;
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002466 Mangler::getNameWithPrefix(NameStr, Sym, getDataLayout());
Jim Grosbach6f482002015-05-18 18:43:14 +00002467 return OutContext.getOrCreateSymbol(NameStr);
Jim Grosbach1900c732011-03-29 23:20:22 +00002468}
Chris Lattner91687f62010-01-15 23:18:17 +00002469
Chris Lattner978e2f02010-01-22 21:50:41 +00002470/// PrintParentLoopComment - Print comments about parent loops of this one.
2471static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop,
2472 unsigned FunctionNumber) {
Craig Topper353eda42014-04-24 06:44:33 +00002473 if (!Loop) return;
Chris Lattner978e2f02010-01-22 21:50:41 +00002474 PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber);
2475 OS.indent(Loop->getLoopDepth()*2)
2476 << "Parent Loop BB" << FunctionNumber << "_"
2477 << Loop->getHeader()->getNumber()
2478 << " Depth=" << Loop->getLoopDepth() << '\n';
2479}
2480
2481
2482/// PrintChildLoopComment - Print comments about child loops within
2483/// the loop for this basic block, with nesting.
2484static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop,
2485 unsigned FunctionNumber) {
2486 // Add child loop information
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002487 for (const MachineLoop *CL : *Loop) {
2488 OS.indent(CL->getLoopDepth()*2)
Chris Lattner978e2f02010-01-22 21:50:41 +00002489 << "Child Loop BB" << FunctionNumber << "_"
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002490 << CL->getHeader()->getNumber() << " Depth " << CL->getLoopDepth()
Chris Lattner978e2f02010-01-22 21:50:41 +00002491 << '\n';
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002492 PrintChildLoopComment(OS, CL, FunctionNumber);
Chris Lattner978e2f02010-01-22 21:50:41 +00002493 }
2494}
2495
Chad Rosierb759ede2012-09-07 18:16:38 +00002496/// emitBasicBlockLoopComments - Pretty-print comments for basic blocks.
2497static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
Chris Lattnerc334d802010-04-04 18:16:38 +00002498 const MachineLoopInfo *LI,
2499 const AsmPrinter &AP) {
Chris Lattner978e2f02010-01-22 21:50:41 +00002500 // Add loop depth information
2501 const MachineLoop *Loop = LI->getLoopFor(&MBB);
Craig Topper353eda42014-04-24 06:44:33 +00002502 if (!Loop) return;
Jim Grosbach1900c732011-03-29 23:20:22 +00002503
Chris Lattner978e2f02010-01-22 21:50:41 +00002504 MachineBasicBlock *Header = Loop->getHeader();
2505 assert(Header && "No header for loop");
Jim Grosbach1900c732011-03-29 23:20:22 +00002506
Chris Lattner978e2f02010-01-22 21:50:41 +00002507 // If this block is not a loop header, just print out what is the loop header
2508 // and return.
2509 if (Header != &MBB) {
Lang Hames9ff69c82015-04-24 19:11:51 +00002510 AP.OutStreamer->AddComment(" in Loop: Header=BB" +
2511 Twine(AP.getFunctionNumber())+"_" +
2512 Twine(Loop->getHeader()->getNumber())+
2513 " Depth="+Twine(Loop->getLoopDepth()));
Chris Lattner978e2f02010-01-22 21:50:41 +00002514 return;
2515 }
Jim Grosbach1900c732011-03-29 23:20:22 +00002516
Chris Lattner978e2f02010-01-22 21:50:41 +00002517 // Otherwise, it is a loop header. Print out information about child and
2518 // parent loops.
Lang Hames9ff69c82015-04-24 19:11:51 +00002519 raw_ostream &OS = AP.OutStreamer->GetCommentOS();
Jim Grosbach1900c732011-03-29 23:20:22 +00002520
2521 PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber());
2522
Chris Lattner978e2f02010-01-22 21:50:41 +00002523 OS << "=>";
2524 OS.indent(Loop->getLoopDepth()*2-2);
Jim Grosbach1900c732011-03-29 23:20:22 +00002525
Chris Lattner978e2f02010-01-22 21:50:41 +00002526 OS << "This ";
2527 if (Loop->empty())
2528 OS << "Inner ";
2529 OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n';
Jim Grosbach1900c732011-03-29 23:20:22 +00002530
Chris Lattner978e2f02010-01-22 21:50:41 +00002531 PrintChildLoopComment(OS, Loop, AP.getFunctionNumber());
2532}
2533
Chris Lattner1d04aa72009-09-13 18:25:37 +00002534/// EmitBasicBlockStart - This method prints the label for the specified
2535/// MachineBasicBlock, an alignment (if present) and a comment describing
2536/// it if appropriate.
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002537void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
David Majnemera80c1512015-09-29 20:12:33 +00002538 // End the previous funclet and start a new one.
2539 if (MBB.isEHFuncletEntry()) {
2540 for (const HandlerInfo &HI : Handlers) {
2541 HI.Handler->endFunclet();
2542 HI.Handler->beginFunclet(MBB);
2543 }
2544 }
2545
Dan Gohmanc83dc4b2009-10-30 01:34:35 +00002546 // Emit an alignment directive for this block, if needed.
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002547 if (unsigned Align = MBB.getAlignment())
Jakob Stoklund Olesen10e12522011-12-06 01:26:19 +00002548 EmitAlignment(Align);
Evan Chengc7990652008-02-28 00:43:03 +00002549
Chris Lattnerdb035a02010-03-16 00:29:39 +00002550 // If the block has its address taken, emit any labels that were used to
2551 // reference the block. It is possible that there is more than one label
2552 // here, because multiple LLVM BB's may have been RAUW'd to this block after
2553 // the references were generated.
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002554 if (MBB.hasAddressTaken()) {
2555 const BasicBlock *BB = MBB.getBasicBlock();
Chris Lattner7bde8c02010-04-04 18:52:31 +00002556 if (isVerbose())
Lang Hames9ff69c82015-04-24 19:11:51 +00002557 OutStreamer->AddComment("Block address taken");
Jim Grosbach1900c732011-03-29 23:20:22 +00002558
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +00002559 // MBBs can have their address taken as part of CodeGen without having
2560 // their corresponding BB's address taken in IR
2561 if (BB->hasAddressTaken())
2562 for (MCSymbol *Sym : MMI->getAddrLabelSymbolToEmit(BB))
2563 OutStreamer->EmitLabel(Sym);
Dan Gohman6c938802009-10-30 01:27:03 +00002564 }
2565
Jakob Stoklund Olesen936656b2012-03-06 22:27:13 +00002566 // Print some verbose block comments.
2567 if (isVerbose()) {
David Majnemer11234ed2015-12-25 09:37:26 +00002568 if (const BasicBlock *BB = MBB.getBasicBlock()) {
2569 if (BB->hasName()) {
2570 BB->printAsOperand(OutStreamer->GetCommentOS(),
2571 /*PrintType=*/false, BB->getModule());
2572 OutStreamer->GetCommentOS() << '\n';
2573 }
2574 }
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002575 emitBasicBlockLoopComments(MBB, LI, *this);
Jakob Stoklund Olesen936656b2012-03-06 22:27:13 +00002576 }
2577
Dan Gohmanc83dc4b2009-10-30 01:34:35 +00002578 // Print the main label for the block.
Reid Kleckner0e288232015-08-27 23:27:47 +00002579 if (MBB.pred_empty() ||
2580 (isBlockOnlyReachableByFallthrough(&MBB) && !MBB.isEHFuncletEntry())) {
Rafael Espindola499a7482014-01-31 23:14:01 +00002581 if (isVerbose()) {
Chris Lattner17167212010-04-03 22:12:35 +00002582 // NOTE: Want this comment at start of line, don't emit with AddComment.
Lang Hames9ff69c82015-04-24 19:11:51 +00002583 OutStreamer->emitRawComment(" BB#" + Twine(MBB.getNumber()) + ":", false);
Chris Lattnera3eee3c2010-01-22 19:52:01 +00002584 }
Dan Gohman10d3dc52009-10-06 17:38:38 +00002585 } else {
Lang Hames9ff69c82015-04-24 19:11:51 +00002586 OutStreamer->EmitLabel(MBB.getSymbol());
Dan Gohman10d3dc52009-10-06 17:38:38 +00002587 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002588}
Nate Begeman984c1a42006-08-12 21:29:52 +00002589
Stuart Hastingsbf836592011-02-23 02:27:05 +00002590void AsmPrinter::EmitVisibility(MCSymbol *Sym, unsigned Visibility,
2591 bool IsDefinition) const {
Chris Lattner0bfd2792010-01-23 06:53:23 +00002592 MCSymbolAttr Attr = MCSA_Invalid;
Jim Grosbach1900c732011-03-29 23:20:22 +00002593
Chris Lattner0bfd2792010-01-23 06:53:23 +00002594 switch (Visibility) {
2595 default: break;
2596 case GlobalValue::HiddenVisibility:
Stuart Hastingsbf836592011-02-23 02:27:05 +00002597 if (IsDefinition)
2598 Attr = MAI->getHiddenVisibilityAttr();
2599 else
2600 Attr = MAI->getHiddenDeclarationVisibilityAttr();
Chris Lattner0bfd2792010-01-23 06:53:23 +00002601 break;
2602 case GlobalValue::ProtectedVisibility:
2603 Attr = MAI->getProtectedVisibilityAttr();
2604 break;
Chris Lattner7fec1802010-01-15 23:38:51 +00002605 }
Chris Lattner0bfd2792010-01-23 06:53:23 +00002606
2607 if (Attr != MCSA_Invalid)
Lang Hames9ff69c82015-04-24 19:11:51 +00002608 OutStreamer->EmitSymbolAttribute(Sym, Attr);
Chris Lattner7fec1802010-01-15 23:38:51 +00002609}
2610
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002611/// isBlockOnlyReachableByFallthough - Return true if the basic block has
2612/// exactly one predecessor and the control transfer mechanism between
2613/// the predecessor and this block is a fall-through.
Chris Lattner2cf5f9e2010-04-04 17:57:56 +00002614bool AsmPrinter::
2615isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002616 // If this is a landing pad, it isn't a fall through. If it has no preds,
2617 // then nothing falls through to it.
Reid Kleckner0e288232015-08-27 23:27:47 +00002618 if (MBB->isEHPad() || MBB->pred_empty())
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002619 return false;
Jim Grosbach1900c732011-03-29 23:20:22 +00002620
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002621 // If there isn't exactly one predecessor, it can't be a fall through.
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002622 if (MBB->pred_size() > 1)
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002623 return false;
Jim Grosbach1900c732011-03-29 23:20:22 +00002624
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002625 // The predecessor has to be immediately before this block.
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002626 MachineBasicBlock *Pred = *MBB->pred_begin();
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002627 if (!Pred->isLayoutSuccessor(MBB))
2628 return false;
Jim Grosbach1900c732011-03-29 23:20:22 +00002629
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002630 // If the block is completely empty, then it definitely does fall through.
2631 if (Pred->empty())
2632 return true;
Jim Grosbach1900c732011-03-29 23:20:22 +00002633
Eli Friedman8a3264a2011-06-14 19:30:33 +00002634 // Check the terminators in the previous blocks
Jim Grosbach6623e7f2014-04-16 22:38:02 +00002635 for (const auto &MI : Pred->terminators()) {
Eli Friedman8a3264a2011-06-14 19:30:33 +00002636 // If it is not a simple branch, we are in a table somewhere.
Evan Cheng7f8e5632011-12-07 07:15:52 +00002637 if (!MI.isBranch() || MI.isIndirectBranch())
Eli Friedman8a3264a2011-06-14 19:30:33 +00002638 return false;
2639
Jakob Stoklund Olesen1995b9f2014-01-12 19:24:08 +00002640 // If we are the operands of one of the branches, this is not a fall
2641 // through. Note that targets with delay slots will usually bundle
2642 // terminators with the delay slot instruction.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00002643 for (ConstMIBundleOperands OP(MI); OP.isValid(); ++OP) {
Jakob Stoklund Olesen1995b9f2014-01-12 19:24:08 +00002644 if (OP->isJTI())
Rafael Espindolaab205672011-06-15 21:00:28 +00002645 return false;
Jakob Stoklund Olesen1995b9f2014-01-12 19:24:08 +00002646 if (OP->isMBB() && OP->getMBB() == MBB)
Eli Friedman8a3264a2011-06-14 19:30:33 +00002647 return false;
2648 }
2649 }
2650
2651 return true;
Chris Lattner1fa9c2cc2010-02-17 18:52:56 +00002652}
2653
David Blaikie88368ba2014-04-15 05:34:49 +00002654GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy &S) {
2655 if (!S.usesMetadata())
Craig Topper353eda42014-04-24 06:44:33 +00002656 return nullptr;
Chris Lattner2cf5f9e2010-04-04 17:57:56 +00002657
Philip Reames4ac17a32015-01-07 19:07:50 +00002658 assert(!S.useStatepoints() && "statepoints do not currently support custom"
2659 " stackmap formats, please see the documentation for a description of"
2660 " the default format. If you really need a custom serialized format,"
2661 " please file a bug");
2662
Chris Lattner2cf5f9e2010-04-04 17:57:56 +00002663 gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
David Blaikie88368ba2014-04-15 05:34:49 +00002664 gcp_map_type::iterator GCPI = GCMap.find(&S);
Chris Lattner2cf5f9e2010-04-04 17:57:56 +00002665 if (GCPI != GCMap.end())
David Blaikieec528ee2014-04-15 05:53:26 +00002666 return GCPI->second.get();
Jim Grosbach1900c732011-03-29 23:20:22 +00002667
Mehdi Amini74107172016-10-01 15:44:54 +00002668 auto Name = S.getName();
Jim Grosbach1900c732011-03-29 23:20:22 +00002669
Gordon Henriksendbe06d32008-08-17 12:08:44 +00002670 for (GCMetadataPrinterRegistry::iterator
2671 I = GCMetadataPrinterRegistry::begin(),
2672 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
Mehdi Amini74107172016-10-01 15:44:54 +00002673 if (Name == I->getName()) {
David Blaikieec528ee2014-04-15 05:53:26 +00002674 std::unique_ptr<GCMetadataPrinter> GMP = I->instantiate();
David Blaikie88368ba2014-04-15 05:34:49 +00002675 GMP->S = &S;
David Blaikieec528ee2014-04-15 05:53:26 +00002676 auto IterBool = GCMap.insert(std::make_pair(&S, std::move(GMP)));
2677 return IterBool.first->second.get();
Gordon Henriksendbe06d32008-08-17 12:08:44 +00002678 }
Jim Grosbach1900c732011-03-29 23:20:22 +00002679
Chris Lattner2104b8d2010-04-07 22:58:41 +00002680 report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name));
Gordon Henriksendbe06d32008-08-17 12:08:44 +00002681}
Juergen Ributzkadb9ee002013-12-14 12:23:14 +00002682
2683/// Pin vtable to this file.
Eugene Zelenkod96089b2017-02-14 00:33:36 +00002684AsmPrinterHandler::~AsmPrinterHandler() = default;
Rafael Espindolaa6001792015-03-09 18:29:12 +00002685
2686void AsmPrinterHandler::markFunctionEnd() {}
Dean Michael Berris464015442016-09-19 00:54:35 +00002687
Dean Michael Berrisf7e7b932017-01-03 04:30:21 +00002688// In the binary's "xray_instr_map" section, an array of these function entries
2689// describes each instrumentation point. When XRay patches your code, the index
2690// into this table will be given to your handler as a patch point identifier.
2691void AsmPrinter::XRayFunctionEntry::emit(int Bytes, MCStreamer *Out,
2692 const MCSymbol *CurrentFnSym) const {
2693 Out->EmitSymbolValue(Sled, Bytes);
2694 Out->EmitSymbolValue(CurrentFnSym, Bytes);
2695 auto Kind8 = static_cast<uint8_t>(Kind);
2696 Out->EmitBytes(StringRef(reinterpret_cast<const char *>(&Kind8), 1));
2697 Out->EmitBytes(
2698 StringRef(reinterpret_cast<const char *>(&AlwaysInstrument), 1));
2699 Out->EmitZeros(2 * Bytes - 2); // Pad the previous two entries
2700}
2701
2702void AsmPrinter::emitXRayTable() {
2703 if (Sleds.empty())
2704 return;
2705
2706 auto PrevSection = OutStreamer->getCurrentSectionOnly();
2707 auto Fn = MF->getFunction();
2708 MCSection *Section = nullptr;
2709 if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) {
2710 if (Fn->hasComdat()) {
2711 Section = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS,
2712 ELF::SHF_ALLOC | ELF::SHF_GROUP, 0,
2713 Fn->getComdat()->getName());
2714 } else {
2715 Section = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS,
2716 ELF::SHF_ALLOC);
2717 }
2718 } else if (MF->getSubtarget().getTargetTriple().isOSBinFormatMachO()) {
2719 Section = OutContext.getMachOSection("__DATA", "xray_instr_map", 0,
2720 SectionKind::getReadOnlyWithRel());
2721 } else {
2722 llvm_unreachable("Unsupported target");
2723 }
2724
2725 // Before we switch over, we force a reference to a label inside the
2726 // xray_instr_map section. Since this function is always called just
2727 // before the function's end, we assume that this is happening after
2728 // the last return instruction.
2729
2730 auto WordSizeBytes = TM.getPointerSize();
2731 MCSymbol *Tmp = OutContext.createTempSymbol("xray_synthetic_", true);
2732 OutStreamer->EmitCodeAlignment(16);
2733 OutStreamer->EmitSymbolValue(Tmp, WordSizeBytes, false);
2734 OutStreamer->SwitchSection(Section);
2735 OutStreamer->EmitLabel(Tmp);
2736 for (const auto &Sled : Sleds)
2737 Sled.emit(WordSizeBytes, OutStreamer.get(), CurrentFnSym);
2738
2739 OutStreamer->SwitchSection(PrevSection);
2740 Sleds.clear();
2741}
2742
Dean Michael Berris464015442016-09-19 00:54:35 +00002743void AsmPrinter::recordSled(MCSymbol *Sled, const MachineInstr &MI,
2744 SledKind Kind) {
2745 auto Fn = MI.getParent()->getParent()->getFunction();
2746 auto Attr = Fn->getFnAttribute("function-instrument");
2747 bool AlwaysInstrument =
2748 Attr.isStringAttribute() && Attr.getValueAsString() == "xray-always";
2749 Sleds.emplace_back(
2750 XRayFunctionEntry{ Sled, CurrentFnSym, Kind, AlwaysInstrument, Fn });
2751}
Greg Claytone6543972016-11-23 23:30:37 +00002752
2753uint16_t AsmPrinter::getDwarfVersion() const {
2754 return OutStreamer->getContext().getDwarfVersion();
2755}
2756
2757void AsmPrinter::setDwarfVersion(uint16_t Version) {
2758 OutStreamer->getContext().setDwarfVersion(Version);
2759}