Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/PowerPC/PPCMCInstLower.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- PPCMCInstLower.cpp - Convert PPC MachineInstr to an MCInst --------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains code to lower PPC MachineInstrs to their corresponding
10
// MCInst records.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "MCTargetDesc/PPCMCExpr.h"
15
#include "PPC.h"
16
#include "PPCSubtarget.h"
17
#include "llvm/ADT/SmallString.h"
18
#include "llvm/ADT/Twine.h"
19
#include "llvm/CodeGen/AsmPrinter.h"
20
#include "llvm/CodeGen/MachineFunction.h"
21
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
22
#include "llvm/CodeGen/TargetLowering.h"
23
#include "llvm/IR/DataLayout.h"
24
#include "llvm/IR/GlobalValue.h"
25
#include "llvm/IR/Mangler.h"
26
#include "llvm/MC/MCAsmInfo.h"
27
#include "llvm/MC/MCExpr.h"
28
#include "llvm/MC/MCInst.h"
29
#include "llvm/Target/TargetLoweringObjectFile.h"
30
using namespace llvm;
31
32
0
static MachineModuleInfoMachO &getMachOMMI(AsmPrinter &AP) {
33
0
  return AP.MMI->getObjFileInfo<MachineModuleInfoMachO>();
34
0
}
35
36
static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO,
37
6.31k
                                      AsmPrinter &AP) {
38
6.31k
  const TargetMachine &TM = AP.TM;
39
6.31k
  Mangler &Mang = TM.getObjFileLowering()->getMangler();
40
6.31k
  const DataLayout &DL = AP.getDataLayout();
41
6.31k
  MCContext &Ctx = AP.OutContext;
42
6.31k
43
6.31k
  SmallString<128> Name;
44
6.31k
  StringRef Suffix;
45
6.31k
  if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG)
46
0
    Suffix = "$non_lazy_ptr";
47
6.31k
48
6.31k
  if (!Suffix.empty())
49
0
    Name += DL.getPrivateGlobalPrefix();
50
6.31k
51
6.31k
  if (!MO.isGlobal()) {
52
682
    assert(MO.isSymbol() && "Isn't a symbol reference");
53
682
    Mangler::getNameWithPrefix(Name, MO.getSymbolName(), DL);
54
5.63k
  } else {
55
5.63k
    const GlobalValue *GV = MO.getGlobal();
56
5.63k
    TM.getNameWithPrefix(Name, GV, Mang);
57
5.63k
  }
58
6.31k
59
6.31k
  Name += Suffix;
60
6.31k
  MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
61
6.31k
62
6.31k
  // If the symbol reference is actually to a non_lazy_ptr, not to the symbol,
63
6.31k
  // then add the suffix.
64
6.31k
  if (MO.getTargetFlags() & PPCII::MO_NLP_FLAG) {
65
0
    MachineModuleInfoMachO &MachO = getMachOMMI(AP);
66
0
67
0
    MachineModuleInfoImpl::StubValueTy &StubSym = MachO.getGVStubEntry(Sym);
68
0
69
0
    if (!StubSym.getPointer()) {
70
0
      assert(MO.isGlobal() && "Extern symbol not handled yet");
71
0
      StubSym = MachineModuleInfoImpl::
72
0
                   StubValueTy(AP.getSymbol(MO.getGlobal()),
73
0
                               !MO.getGlobal()->hasInternalLinkage());
74
0
    }
75
0
    return Sym;
76
0
  }
77
6.31k
78
6.31k
  return Sym;
79
6.31k
}
80
81
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
82
9.91k
                              AsmPrinter &Printer, bool isDarwin) {
83
9.91k
  MCContext &Ctx = Printer.OutContext;
84
9.91k
  MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
85
9.91k
86
9.91k
  unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
87
9.91k
88
9.91k
  switch (access) {
89
9.91k
    case PPCII::MO_TPREL_LO:
90
10
      RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO;
91
10
      break;
92
9.91k
    case PPCII::MO_TPREL_HA:
93
9
      RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA;
94
9
      break;
95
9.91k
    case PPCII::MO_DTPREL_LO:
96
6
      RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO;
97
6
      break;
98
9.91k
    case PPCII::MO_TLSLD_LO:
99
0
      RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO;
100
0
      break;
101
9.91k
    case PPCII::MO_TOC_LO:
102
1.25k
      RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO;
103
1.25k
      break;
104
9.91k
    case PPCII::MO_TLS:
105
22
      RefKind = MCSymbolRefExpr::VK_PPC_TLS;
106
22
      break;
107
9.91k
  }
108
9.91k
109
9.91k
 if (MO.getTargetFlags() == PPCII::MO_PLT)
110
362
    RefKind = MCSymbolRefExpr::VK_PLT;
111
9.91k
112
9.91k
  const MachineFunction *MF = MO.getParent()->getParent()->getParent();
113
9.91k
  const Module *M = MF->getFunction().getParent();
114
9.91k
  const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
115
9.91k
  const TargetMachine &TM = Printer.TM;
116
9.91k
  const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
117
9.91k
  // If -msecure-plt -fPIC, add 32768 to symbol.
118
9.91k
  if (Subtarget->isSecurePlt() && 
TM.isPositionIndependent()85
&&
119
9.91k
      
M->getPICLevel() == PICLevel::BigPIC39
&&
120
9.91k
      
MO.getTargetFlags() == PPCII::MO_PLT14
)
121
7
    Expr =
122
7
        MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
123
9.91k
124
9.91k
  if (!MO.isJTI() && 
MO.getOffset()9.89k
)
125
85
    Expr = MCBinaryExpr::createAdd(Expr,
126
85
                                   MCConstantExpr::create(MO.getOffset(), Ctx),
127
85
                                   Ctx);
128
9.91k
129
9.91k
  // Subtract off the PIC base if required.
130
9.91k
  if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
131
18
    const MachineFunction *MF = MO.getParent()->getParent()->getParent();
132
18
133
18
    const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
134
18
    Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
135
18
  }
136
9.91k
137
9.91k
  // Add ha16() / lo16() markers if required.
138
9.91k
  switch (access) {
139
9.91k
    case PPCII::MO_LO:
140
251
      Expr = PPCMCExpr::createLo(Expr, isDarwin, Ctx);
141
251
      break;
142
9.91k
    case PPCII::MO_HA:
143
248
      Expr = PPCMCExpr::createHa(Expr, isDarwin, Ctx);
144
248
      break;
145
9.91k
  }
146
9.91k
147
9.91k
  return MCOperand::createExpr(Expr);
148
9.91k
}
149
150
void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
151
99.4k
                                        AsmPrinter &AP, bool isDarwin) {
152
99.4k
  OutMI.setOpcode(MI->getOpcode());
153
99.4k
154
420k
  for (unsigned i = 0, e = MI->getNumOperands(); i != e; 
++i321k
) {
155
321k
    MCOperand MCOp;
156
321k
    if (LowerPPCMachineOperandToMCOperand(MI->getOperand(i), MCOp, AP,
157
321k
                                          isDarwin))
158
318k
      OutMI.addOperand(MCOp);
159
321k
  }
160
99.4k
}
161
162
bool llvm::LowerPPCMachineOperandToMCOperand(const MachineOperand &MO,
163
                                             MCOperand &OutMO, AsmPrinter &AP,
164
321k
                                             bool isDarwin) {
165
321k
  switch (MO.getType()) {
166
321k
  default:
167
0
    llvm_unreachable("unknown operand type");
168
321k
  case MachineOperand::MO_Register:
169
258k
    assert(!MO.getSubReg() && "Subregs should be eliminated!");
170
258k
    assert(MO.getReg() > PPC::NoRegister &&
171
258k
           MO.getReg() < PPC::NUM_TARGET_REGS &&
172
258k
           "Invalid register for this target!");
173
258k
    OutMO = MCOperand::createReg(MO.getReg());
174
258k
    return true;
175
321k
  case MachineOperand::MO_Immediate:
176
47.0k
    OutMO = MCOperand::createImm(MO.getImm());
177
47.0k
    return true;
178
321k
  case MachineOperand::MO_MachineBasicBlock:
179
3.31k
    OutMO = MCOperand::createExpr(
180
3.31k
        MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), AP.OutContext));
181
3.31k
    return true;
182
321k
  case MachineOperand::MO_GlobalAddress:
183
6.31k
  case MachineOperand::MO_ExternalSymbol:
184
6.31k
    OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, isDarwin);
185
6.31k
    return true;
186
6.31k
  case MachineOperand::MO_JumpTableIndex:
187
26
    OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, isDarwin);
188
26
    return true;
189
6.31k
  case MachineOperand::MO_ConstantPoolIndex:
190
3.55k
    OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, isDarwin);
191
3.55k
    return true;
192
6.31k
  case MachineOperand::MO_BlockAddress:
193
23
    OutMO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
194
23
                         isDarwin);
195
23
    return true;
196
6.31k
  case MachineOperand::MO_RegisterMask:
197
2.25k
    return false;
198
321k
  }
199
321k
}