[X86][SSE] Bitcast assertion in XFormVExtractWithShuffleIntoLoad
[oota-llvm.git] / lib / Target / X86 / X86TargetObjectFile.cpp
1 //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "X86TargetObjectFile.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/IR/Mangler.h"
13 #include "llvm/IR/Operator.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCSectionCOFF.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/Support/Dwarf.h"
19 #include "llvm/Target/TargetLowering.h"
20
21 using namespace llvm;
22 using namespace dwarf;
23
24 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
25     const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
26     const TargetMachine &TM, MachineModuleInfo *MMI,
27     MCStreamer &Streamer) const {
28
29   // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
30   // is an indirect pc-relative reference.
31   if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
32     const MCSymbol *Sym = TM.getSymbol(GV, Mang);
33     const MCExpr *Res =
34       MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
35     const MCExpr *Four = MCConstantExpr::Create(4, getContext());
36     return MCBinaryExpr::CreateAdd(Res, Four, getContext());
37   }
38
39   return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
40       GV, Encoding, Mang, TM, MMI, Streamer);
41 }
42
43 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
44     const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
45     MachineModuleInfo *MMI) const {
46   return TM.getSymbol(GV, Mang);
47 }
48
49 void
50 X86LinuxTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) {
51   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
52   InitializeELF(TM.Options.UseInitArray);
53 }
54
55 const MCExpr *
56 X86LinuxTargetObjectFile::getDebugThreadLocalSymbol(
57     const MCSymbol *Sym) const {
58   return MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
59 }
60
61 const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol(
62     const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const {
63   // We are looking for the difference of two symbols, need a subtraction
64   // operation.
65   const SubOperator *Sub = dyn_cast<SubOperator>(CE);
66   if (!Sub)
67     return nullptr;
68
69   // Symbols must first be numbers before we can subtract them, we need to see a
70   // ptrtoint on both subtraction operands.
71   const PtrToIntOperator *SubLHS =
72       dyn_cast<PtrToIntOperator>(Sub->getOperand(0));
73   const PtrToIntOperator *SubRHS =
74       dyn_cast<PtrToIntOperator>(Sub->getOperand(1));
75   if (!SubLHS || !SubRHS)
76     return nullptr;
77
78   // Our symbols should exist in address space zero, cowardly no-op if
79   // otherwise.
80   if (SubLHS->getPointerAddressSpace() != 0 ||
81       SubRHS->getPointerAddressSpace() != 0)
82     return nullptr;
83
84   // Both ptrtoint instructions must wrap global variables:
85   // - Only global variables are eligible for image relative relocations.
86   // - The subtrahend refers to the special symbol __ImageBase, a global.
87   const GlobalVariable *GVLHS =
88       dyn_cast<GlobalVariable>(SubLHS->getPointerOperand());
89   const GlobalVariable *GVRHS =
90       dyn_cast<GlobalVariable>(SubRHS->getPointerOperand());
91   if (!GVLHS || !GVRHS)
92     return nullptr;
93
94   // We expect __ImageBase to be a global variable without a section, externally
95   // defined.
96   //
97   // It should look something like this: @__ImageBase = external constant i8
98   if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" ||
99       !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() ||
100       GVRHS->hasSection())
101     return nullptr;
102
103   // An image-relative, thread-local, symbol makes no sense.
104   if (GVLHS->isThreadLocal())
105     return nullptr;
106
107   return MCSymbolRefExpr::Create(TM.getSymbol(GVLHS, Mang),
108                                  MCSymbolRefExpr::VK_COFF_IMGREL32,
109                                  getContext());
110 }
111
112 static std::string APIntToHexString(const APInt &AI) {
113   unsigned Width = (AI.getBitWidth() / 8) * 2;
114   std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
115   unsigned Size = HexString.size();
116   assert(Width >= Size && "hex string is too large!");
117   HexString.insert(HexString.begin(), Width - Size, '0');
118
119   return HexString;
120 }
121
122
123 static std::string scalarConstantToHexString(const Constant *C) {
124   Type *Ty = C->getType();
125   APInt AI;
126   if (isa<UndefValue>(C)) {
127     AI = APInt(Ty->getPrimitiveSizeInBits(), /*val=*/0);
128   } else if (Ty->isFloatTy() || Ty->isDoubleTy()) {
129     const auto *CFP = cast<ConstantFP>(C);
130     AI = CFP->getValueAPF().bitcastToAPInt();
131   } else if (Ty->isIntegerTy()) {
132     const auto *CI = cast<ConstantInt>(C);
133     AI = CI->getValue();
134   } else {
135     llvm_unreachable("unexpected constant pool element type!");
136   }
137   return APIntToHexString(AI);
138 }
139
140 const MCSection *
141 X86WindowsTargetObjectFile::getSectionForConstant(SectionKind Kind,
142                                                   const Constant *C) const {
143   if (Kind.isReadOnly()) {
144     if (C) {
145       Type *Ty = C->getType();
146       SmallString<32> COMDATSymName;
147       if (Ty->isFloatTy() || Ty->isDoubleTy()) {
148         COMDATSymName = "__real@";
149         COMDATSymName += scalarConstantToHexString(C);
150       } else if (const auto *VTy = dyn_cast<VectorType>(Ty)) {
151         uint64_t NumBits = VTy->getBitWidth();
152         if (NumBits == 128 || NumBits == 256) {
153           COMDATSymName = NumBits == 128 ? "__xmm@" : "__ymm@";
154           for (int I = VTy->getNumElements() - 1, E = -1; I != E; --I)
155             COMDATSymName +=
156                 scalarConstantToHexString(C->getAggregateElement(I));
157         }
158       }
159       if (!COMDATSymName.empty()) {
160         unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
161                                    COFF::IMAGE_SCN_MEM_READ |
162                                    COFF::IMAGE_SCN_LNK_COMDAT;
163         return getContext().getCOFFSection(".rdata", Characteristics, Kind,
164                                            COMDATSymName,
165                                            COFF::IMAGE_COMDAT_SELECT_ANY);
166       }
167     }
168   }
169
170   return TargetLoweringObjectFile::getSectionForConstant(Kind, C);
171 }