1309124Sdim//===-- HexagonTargetObjectFile.cpp ---------------------------------------===//
2234285Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6234285Sdim//
7234285Sdim//===----------------------------------------------------------------------===//
8234285Sdim//
9234285Sdim// This file contains the declarations of the HexagonTargetAsmInfo properties.
10234285Sdim//
11234285Sdim//===----------------------------------------------------------------------===//
12314564Sdim
13309124Sdim#define DEBUG_TYPE "hexagon-sdata"
14234285Sdim
15234285Sdim#include "HexagonTargetObjectFile.h"
16314564Sdim#include "llvm/ADT/SmallString.h"
17314564Sdim#include "llvm/ADT/StringRef.h"
18314564Sdim#include "llvm/ADT/Twine.h"
19321369Sdim#include "llvm/BinaryFormat/ELF.h"
20249423Sdim#include "llvm/IR/DataLayout.h"
21249423Sdim#include "llvm/IR/DerivedTypes.h"
22314564Sdim#include "llvm/IR/GlobalObject.h"
23314564Sdim#include "llvm/IR/GlobalValue.h"
24249423Sdim#include "llvm/IR/GlobalVariable.h"
25314564Sdim#include "llvm/IR/Type.h"
26234285Sdim#include "llvm/MC/MCContext.h"
27314564Sdim#include "llvm/MC/SectionKind.h"
28314564Sdim#include "llvm/Support/Casting.h"
29249423Sdim#include "llvm/Support/CommandLine.h"
30314564Sdim#include "llvm/Support/Debug.h"
31314564Sdim#include "llvm/Support/raw_ostream.h"
32314564Sdim#include "llvm/Target/TargetMachine.h"
33234285Sdim
34234285Sdimusing namespace llvm;
35234285Sdim
36309124Sdimstatic cl::opt<unsigned> SmallDataThreshold("hexagon-small-data-threshold",
37309124Sdim  cl::init(8), cl::Hidden,
38309124Sdim  cl::desc("The maximum size of an object in the sdata section"));
39234285Sdim
40309124Sdimstatic cl::opt<bool> NoSmallDataSorting("mno-sort-sda", cl::init(false),
41309124Sdim  cl::Hidden, cl::desc("Disable small data sections sorting"));
42309124Sdim
43309124Sdimstatic cl::opt<bool> StaticsInSData("hexagon-statics-in-small-data",
44309124Sdim  cl::init(false), cl::Hidden, cl::ZeroOrMore,
45309124Sdim  cl::desc("Allow static variables in .sdata"));
46309124Sdim
47309124Sdimstatic cl::opt<bool> TraceGVPlacement("trace-gv-placement",
48309124Sdim  cl::Hidden, cl::init(false),
49309124Sdim  cl::desc("Trace global value placement"));
50309124Sdim
51321369Sdimstatic cl::opt<bool>
52321369Sdim    EmitJtInText("hexagon-emit-jt-text", cl::Hidden, cl::init(false),
53321369Sdim                 cl::desc("Emit hexagon jump tables in function section"));
54321369Sdim
55321369Sdimstatic cl::opt<bool>
56321369Sdim    EmitLutInText("hexagon-emit-lut-text", cl::Hidden, cl::init(false),
57321369Sdim                 cl::desc("Emit hexagon lookup tables in function section"));
58321369Sdim
59309124Sdim// TraceGVPlacement controls messages for all builds. For builds with assertions
60309124Sdim// (debug or release), messages are also controlled by the usual debug flags
61309124Sdim// (e.g. -debug and -debug-only=globallayout)
62309124Sdim#define TRACE_TO(s, X) s << X
63309124Sdim#ifdef NDEBUG
64314564Sdim#define TRACE(X)                                                               \
65314564Sdim  do {                                                                         \
66314564Sdim    if (TraceGVPlacement) {                                                    \
67314564Sdim      TRACE_TO(errs(), X);                                                     \
68314564Sdim    }                                                                          \
69314564Sdim  } while (false)
70309124Sdim#else
71314564Sdim#define TRACE(X)                                                               \
72314564Sdim  do {                                                                         \
73314564Sdim    if (TraceGVPlacement) {                                                    \
74314564Sdim      TRACE_TO(errs(), X);                                                     \
75314564Sdim    } else {                                                                   \
76341825Sdim      LLVM_DEBUG(TRACE_TO(dbgs(), X));                                         \
77314564Sdim    }                                                                          \
78314564Sdim  } while (false)
79309124Sdim#endif
80309124Sdim
81309124Sdim// Returns true if the section name is such that the symbol will be put
82309124Sdim// in a small data section.
83309124Sdim// For instance, global variables with section attributes such as ".sdata"
84309124Sdim// ".sdata.*", ".sbss", and ".sbss.*" will go into small data.
85309124Sdimstatic bool isSmallDataSection(StringRef Sec) {
86309124Sdim  // sectionName is either ".sdata" or ".sbss". Looking for an exact match
87309124Sdim  // obviates the need for checks for section names such as ".sdatafoo".
88309124Sdim  if (Sec.equals(".sdata") || Sec.equals(".sbss") || Sec.equals(".scommon"))
89309124Sdim    return true;
90309124Sdim  // If either ".sdata." or ".sbss." is a substring of the section name
91309124Sdim  // then put the symbol in small data.
92309124Sdim  return Sec.find(".sdata.") != StringRef::npos ||
93309124Sdim         Sec.find(".sbss.") != StringRef::npos ||
94309124Sdim         Sec.find(".scommon.") != StringRef::npos;
95309124Sdim}
96309124Sdim
97309124Sdimstatic const char *getSectionSuffixForSize(unsigned Size) {
98309124Sdim  switch (Size) {
99309124Sdim  default:
100309124Sdim    return "";
101309124Sdim  case 1:
102309124Sdim    return ".1";
103309124Sdim  case 2:
104309124Sdim    return ".2";
105309124Sdim  case 4:
106309124Sdim    return ".4";
107309124Sdim  case 8:
108309124Sdim    return ".8";
109309124Sdim  }
110309124Sdim}
111309124Sdim
112234285Sdimvoid HexagonTargetObjectFile::Initialize(MCContext &Ctx,
113309124Sdim      const TargetMachine &TM) {
114234285Sdim  TargetLoweringObjectFileELF::Initialize(Ctx, TM);
115280031Sdim  InitializeELF(TM.Options.UseInitArray);
116234285Sdim
117309124Sdim  SmallDataSection =
118309124Sdim    getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
119309124Sdim                               ELF::SHF_WRITE | ELF::SHF_ALLOC |
120309124Sdim                               ELF::SHF_HEX_GPREL);
121309124Sdim  SmallBSSSection =
122309124Sdim    getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
123309124Sdim                               ELF::SHF_WRITE | ELF::SHF_ALLOC |
124309124Sdim                               ELF::SHF_HEX_GPREL);
125234285Sdim}
126234285Sdim
127309124SdimMCSection *HexagonTargetObjectFile::SelectSectionForGlobal(
128314564Sdim    const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
129314564Sdim  TRACE("[SelectSectionForGlobal] GO(" << GO->getName() << ") ");
130314564Sdim  TRACE("input section(" << GO->getSection() << ") ");
131309124Sdim
132314564Sdim  TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
133314564Sdim         << (GO->hasLocalLinkage() ? "local_linkage " : "")
134314564Sdim         << (GO->hasInternalLinkage() ? "internal " : "")
135314564Sdim         << (GO->hasExternalLinkage() ? "external " : "")
136314564Sdim         << (GO->hasCommonLinkage() ? "common_linkage " : "")
137314564Sdim         << (GO->hasCommonLinkage() ? "common " : "" )
138309124Sdim         << (Kind.isCommon() ? "kind_common " : "" )
139309124Sdim         << (Kind.isBSS() ? "kind_bss " : "" )
140309124Sdim         << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
141309124Sdim
142321369Sdim  // If the lookup table is used by more than one function, do not place
143321369Sdim  // it in text section.
144321369Sdim  if (EmitLutInText && GO->getName().startswith("switch.table")) {
145321369Sdim    if (const Function *Fn = getLutUsedFunction(GO))
146321369Sdim      return selectSectionForLookupTable(GO, TM, Fn);
147321369Sdim  }
148321369Sdim
149314564Sdim  if (isGlobalInSmallSection(GO, TM))
150314564Sdim    return selectSmallSectionForGlobal(GO, Kind, TM);
151309124Sdim
152309124Sdim  if (Kind.isCommon()) {
153309124Sdim    // This is purely for LTO+Linker Script because commons don't really have a
154309124Sdim    // section. However, the BitcodeSectionWriter pass will query for the
155309124Sdim    // sections of commons (and the linker expects us to know their section) so
156309124Sdim    // we'll return one here.
157309124Sdim    return BSSSection;
158309124Sdim  }
159309124Sdim
160309124Sdim  TRACE("default_ELF_section\n");
161309124Sdim  // Otherwise, we work the same as ELF.
162314564Sdim  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
163261991Sdim}
164261991Sdim
165309124SdimMCSection *HexagonTargetObjectFile::getExplicitSectionGlobal(
166314564Sdim    const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
167314564Sdim  TRACE("[getExplicitSectionGlobal] GO(" << GO->getName() << ") from("
168314564Sdim        << GO->getSection() << ") ");
169314564Sdim  TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
170314564Sdim         << (GO->hasLocalLinkage() ? "local_linkage " : "")
171314564Sdim         << (GO->hasInternalLinkage() ? "internal " : "")
172314564Sdim         << (GO->hasExternalLinkage() ? "external " : "")
173314564Sdim         << (GO->hasCommonLinkage() ? "common_linkage " : "")
174314564Sdim         << (GO->hasCommonLinkage() ? "common " : "" )
175309124Sdim         << (Kind.isCommon() ? "kind_common " : "" )
176309124Sdim         << (Kind.isBSS() ? "kind_bss " : "" )
177309124Sdim         << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
178309124Sdim
179314564Sdim  if (GO->hasSection()) {
180314564Sdim    StringRef Section = GO->getSection();
181309124Sdim    if (Section.find(".access.text.group") != StringRef::npos)
182314564Sdim      return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
183309124Sdim                                        ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
184309124Sdim    if (Section.find(".access.data.group") != StringRef::npos)
185314564Sdim      return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
186309124Sdim                                        ELF::SHF_WRITE | ELF::SHF_ALLOC);
187309124Sdim  }
188309124Sdim
189314564Sdim  if (isGlobalInSmallSection(GO, TM))
190314564Sdim    return selectSmallSectionForGlobal(GO, Kind, TM);
191309124Sdim
192309124Sdim  // Otherwise, we work the same as ELF.
193309124Sdim  TRACE("default_ELF_section\n");
194314564Sdim  return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind, TM);
195234285Sdim}
196234285Sdim
197309124Sdim/// Return true if this global value should be placed into small data/bss
198309124Sdim/// section.
199314564Sdimbool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
200309124Sdim      const TargetMachine &TM) const {
201344779Sdim  bool HaveSData = isSmallDataEnabled(TM);
202344779Sdim  if (!HaveSData)
203344779Sdim    LLVM_DEBUG(dbgs() << "Small-data allocation is disabled, but symbols "
204344779Sdim                         "may have explicit section assignments...\n");
205234285Sdim  // Only global variables, not functions.
206341825Sdim  LLVM_DEBUG(dbgs() << "Checking if value is in small-data, -G"
207341825Sdim                    << SmallDataThreshold << ": \"" << GO->getName() << "\": ");
208314564Sdim  const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
209309124Sdim  if (!GVar) {
210341825Sdim    LLVM_DEBUG(dbgs() << "no, not a global variable\n");
211234285Sdim    return false;
212309124Sdim  }
213234285Sdim
214309124Sdim  // Globals with external linkage that have an original section set must be
215309124Sdim  // emitted to that section, regardless of whether we would put them into
216309124Sdim  // small data or not. This is how we can support mixing -G0/-G8 in LTO.
217309124Sdim  if (GVar->hasSection()) {
218309124Sdim    bool IsSmall = isSmallDataSection(GVar->getSection());
219341825Sdim    LLVM_DEBUG(dbgs() << (IsSmall ? "yes" : "no")
220341825Sdim                      << ", has section: " << GVar->getSection() << '\n');
221309124Sdim    return IsSmall;
222234285Sdim  }
223234285Sdim
224344779Sdim  // If sdata is disabled, stop the checks here.
225344779Sdim  if (!HaveSData) {
226344779Sdim    LLVM_DEBUG(dbgs() << "no, small-data allocation is disabled\n");
227344779Sdim    return false;
228344779Sdim  }
229344779Sdim
230309124Sdim  if (GVar->isConstant()) {
231341825Sdim    LLVM_DEBUG(dbgs() << "no, is a constant\n");
232309124Sdim    return false;
233309124Sdim  }
234309124Sdim
235309124Sdim  bool IsLocal = GVar->hasLocalLinkage();
236309124Sdim  if (!StaticsInSData && IsLocal) {
237341825Sdim    LLVM_DEBUG(dbgs() << "no, is static\n");
238309124Sdim    return false;
239309124Sdim  }
240309124Sdim
241353358Sdim  Type *GType = GVar->getValueType();
242309124Sdim  if (isa<ArrayType>(GType)) {
243341825Sdim    LLVM_DEBUG(dbgs() << "no, is an array\n");
244309124Sdim    return false;
245309124Sdim  }
246309124Sdim
247309124Sdim  // If the type is a struct with no body provided, treat is conservatively.
248309124Sdim  // There cannot be actual definitions of object of such a type in this CU
249309124Sdim  // (only references), so assuming that they are not in sdata is safe. If
250309124Sdim  // these objects end up in the sdata, the references will still be valid.
251309124Sdim  if (StructType *ST = dyn_cast<StructType>(GType)) {
252309124Sdim    if (ST->isOpaque()) {
253341825Sdim      LLVM_DEBUG(dbgs() << "no, has opaque type\n");
254309124Sdim      return false;
255309124Sdim    }
256309124Sdim  }
257309124Sdim
258309124Sdim  unsigned Size = GVar->getParent()->getDataLayout().getTypeAllocSize(GType);
259309124Sdim  if (Size == 0) {
260341825Sdim    LLVM_DEBUG(dbgs() << "no, has size 0\n");
261309124Sdim    return false;
262309124Sdim  }
263309124Sdim  if (Size > SmallDataThreshold) {
264341825Sdim    LLVM_DEBUG(dbgs() << "no, size exceeds sdata threshold: " << Size << '\n');
265309124Sdim    return false;
266309124Sdim  }
267309124Sdim
268341825Sdim  LLVM_DEBUG(dbgs() << "yes\n");
269309124Sdim  return true;
270234285Sdim}
271234285Sdim
272344779Sdimbool HexagonTargetObjectFile::isSmallDataEnabled(const TargetMachine &TM)
273344779Sdim    const {
274344779Sdim  return SmallDataThreshold > 0 && !TM.isPositionIndependent();
275309124Sdim}
276309124Sdim
277309124Sdimunsigned HexagonTargetObjectFile::getSmallDataSize() const {
278309124Sdim  return SmallDataThreshold;
279309124Sdim}
280309124Sdim
281321369Sdimbool HexagonTargetObjectFile::shouldPutJumpTableInFunctionSection(
282321369Sdim    bool UsesLabelDifference, const Function &F) const {
283321369Sdim  return EmitJtInText;
284321369Sdim}
285321369Sdim
286309124Sdim/// Descends any type down to "elementary" components,
287309124Sdim/// discovering the smallest addressable one.
288309124Sdim/// If zero is returned, declaration will not be modified.
289309124Sdimunsigned HexagonTargetObjectFile::getSmallestAddressableSize(const Type *Ty,
290309124Sdim      const GlobalValue *GV, const TargetMachine &TM) const {
291309124Sdim  // Assign the smallest element access size to the highest
292309124Sdim  // value which assembler can handle.
293309124Sdim  unsigned SmallestElement = 8;
294309124Sdim
295309124Sdim  if (!Ty)
296309124Sdim    return 0;
297309124Sdim  switch (Ty->getTypeID()) {
298309124Sdim  case Type::StructTyID: {
299309124Sdim    const StructType *STy = cast<const StructType>(Ty);
300309124Sdim    for (auto &E : STy->elements()) {
301309124Sdim      unsigned AtomicSize = getSmallestAddressableSize(E, GV, TM);
302309124Sdim      if (AtomicSize < SmallestElement)
303309124Sdim        SmallestElement = AtomicSize;
304309124Sdim    }
305309124Sdim    return (STy->getNumElements() == 0) ? 0 : SmallestElement;
306309124Sdim  }
307309124Sdim  case Type::ArrayTyID: {
308309124Sdim    const ArrayType *ATy = cast<const ArrayType>(Ty);
309309124Sdim    return getSmallestAddressableSize(ATy->getElementType(), GV, TM);
310309124Sdim  }
311309124Sdim  case Type::VectorTyID: {
312309124Sdim    const VectorType *PTy = cast<const VectorType>(Ty);
313309124Sdim    return getSmallestAddressableSize(PTy->getElementType(), GV, TM);
314309124Sdim  }
315309124Sdim  case Type::PointerTyID:
316309124Sdim  case Type::HalfTyID:
317309124Sdim  case Type::FloatTyID:
318309124Sdim  case Type::DoubleTyID:
319309124Sdim  case Type::IntegerTyID: {
320309124Sdim    const DataLayout &DL = GV->getParent()->getDataLayout();
321309124Sdim    // It is unfortunate that DL's function take non-const Type*.
322309124Sdim    return DL.getTypeAllocSize(const_cast<Type*>(Ty));
323309124Sdim  }
324309124Sdim  case Type::FunctionTyID:
325309124Sdim  case Type::VoidTyID:
326309124Sdim  case Type::X86_FP80TyID:
327309124Sdim  case Type::FP128TyID:
328309124Sdim  case Type::PPC_FP128TyID:
329309124Sdim  case Type::LabelTyID:
330309124Sdim  case Type::MetadataTyID:
331309124Sdim  case Type::X86_MMXTyID:
332309124Sdim  case Type::TokenTyID:
333309124Sdim    return 0;
334309124Sdim  }
335309124Sdim
336309124Sdim  return 0;
337309124Sdim}
338309124Sdim
339309124SdimMCSection *HexagonTargetObjectFile::selectSmallSectionForGlobal(
340314564Sdim    const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
341353358Sdim  const Type *GTy = GO->getValueType();
342314564Sdim  unsigned Size = getSmallestAddressableSize(GTy, GO, TM);
343309124Sdim
344309124Sdim  // If we have -ffunction-section or -fdata-section then we should emit the
345309124Sdim  // global value to a unique section specifically for it... even for sdata.
346309124Sdim  bool EmitUniquedSection = TM.getDataSections();
347309124Sdim
348309124Sdim  TRACE("Small data. Size(" << Size << ")");
349234285Sdim  // Handle Small Section classification here.
350309124Sdim  if (Kind.isBSS() || Kind.isBSSLocal()) {
351309124Sdim    // If -mno-sort-sda is not set, find out smallest accessible entity in
352309124Sdim    // declaration and add it to the section name string.
353309124Sdim    // Note. It does not track the actual usage of the value, only its de-
354309124Sdim    // claration. Also, compiler adds explicit pad fields to some struct
355309124Sdim    // declarations - they are currently counted towards smallest addres-
356309124Sdim    // sable entity.
357309124Sdim    if (NoSmallDataSorting) {
358309124Sdim      TRACE(" default sbss\n");
359309124Sdim      return SmallBSSSection;
360309124Sdim    }
361234285Sdim
362309124Sdim    StringRef Prefix(".sbss");
363309124Sdim    SmallString<128> Name(Prefix);
364309124Sdim    Name.append(getSectionSuffixForSize(Size));
365309124Sdim
366309124Sdim    if (EmitUniquedSection) {
367309124Sdim      Name.append(".");
368314564Sdim      Name.append(GO->getName());
369309124Sdim    }
370309124Sdim    TRACE(" unique sbss(" << Name << ")\n");
371309124Sdim    return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
372309124Sdim                ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
373309124Sdim  }
374309124Sdim
375309124Sdim  if (Kind.isCommon()) {
376309124Sdim    // This is purely for LTO+Linker Script because commons don't really have a
377309124Sdim    // section. However, the BitcodeSectionWriter pass will query for the
378309124Sdim    // sections of commons (and the linker expects us to know their section) so
379309124Sdim    // we'll return one here.
380309124Sdim    if (NoSmallDataSorting)
381309124Sdim      return BSSSection;
382309124Sdim
383309124Sdim    Twine Name = Twine(".scommon") + getSectionSuffixForSize(Size);
384309124Sdim    TRACE(" small COMMON (" << Name << ")\n");
385309124Sdim
386309124Sdim    return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
387309124Sdim                                      ELF::SHF_WRITE | ELF::SHF_ALLOC |
388309124Sdim                                      ELF::SHF_HEX_GPREL);
389309124Sdim  }
390309124Sdim
391309124Sdim  // We could have changed sdata object to a constant... in this
392309124Sdim  // case the Kind could be wrong for it.
393309124Sdim  if (Kind.isMergeableConst()) {
394309124Sdim    TRACE(" const_object_as_data ");
395314564Sdim    const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GO);
396309124Sdim    if (GVar->hasSection() && isSmallDataSection(GVar->getSection()))
397309124Sdim      Kind = SectionKind::getData();
398309124Sdim  }
399309124Sdim
400309124Sdim  if (Kind.isData()) {
401309124Sdim    if (NoSmallDataSorting) {
402309124Sdim      TRACE(" default sdata\n");
403309124Sdim      return SmallDataSection;
404309124Sdim    }
405309124Sdim
406309124Sdim    StringRef Prefix(".sdata");
407309124Sdim    SmallString<128> Name(Prefix);
408309124Sdim    Name.append(getSectionSuffixForSize(Size));
409309124Sdim
410309124Sdim    if (EmitUniquedSection) {
411309124Sdim      Name.append(".");
412314564Sdim      Name.append(GO->getName());
413309124Sdim    }
414309124Sdim    TRACE(" unique sdata(" << Name << ")\n");
415309124Sdim    return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS,
416309124Sdim                ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_HEX_GPREL);
417309124Sdim  }
418309124Sdim
419309124Sdim  TRACE("default ELF section\n");
420234285Sdim  // Otherwise, we work the same as ELF.
421314564Sdim  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
422234285Sdim}
423321369Sdim
424321369Sdim// Return the function that uses the lookup table. If there are more
425321369Sdim// than one live function that uses this look table, bail out and place
426321369Sdim// the lookup table in default section.
427321369Sdimconst Function *
428321369SdimHexagonTargetObjectFile::getLutUsedFunction(const GlobalObject *GO) const {
429321369Sdim  const Function *ReturnFn = nullptr;
430321369Sdim  for (auto U : GO->users()) {
431321369Sdim    // validate each instance of user to be a live function.
432321369Sdim    auto *I = dyn_cast<Instruction>(U);
433321369Sdim    if (!I)
434321369Sdim      continue;
435321369Sdim    auto *Bb = I->getParent();
436321369Sdim    if (!Bb)
437321369Sdim      continue;
438321369Sdim    auto *UserFn = Bb->getParent();
439321369Sdim    if (!ReturnFn)
440321369Sdim      ReturnFn = UserFn;
441321369Sdim    else if (ReturnFn != UserFn)
442321369Sdim      return nullptr;
443321369Sdim  }
444321369Sdim  return ReturnFn;
445321369Sdim}
446321369Sdim
447321369SdimMCSection *HexagonTargetObjectFile::selectSectionForLookupTable(
448321369Sdim    const GlobalObject *GO, const TargetMachine &TM, const Function *Fn) const {
449321369Sdim
450321369Sdim  SectionKind Kind = SectionKind::getText();
451321369Sdim  // If the function has explicit section, place the lookup table in this
452321369Sdim  // explicit section.
453321369Sdim  if (Fn->hasSection())
454321369Sdim    return getExplicitSectionGlobal(Fn, Kind, TM);
455321369Sdim
456321369Sdim  const auto *FuncObj = dyn_cast<GlobalObject>(Fn);
457321369Sdim  return SelectSectionForGlobal(FuncObj, Kind, TM);
458321369Sdim}
459