Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Hexagon/HexagonMCInstLower.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- HexagonMCInstLower.cpp - Convert Hexagon 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 Hexagon MachineInstrs to their corresponding
10
// MCInst records.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "Hexagon.h"
15
#include "HexagonAsmPrinter.h"
16
#include "MCTargetDesc/HexagonMCExpr.h"
17
#include "MCTargetDesc/HexagonMCInstrInfo.h"
18
#include "MCTargetDesc/HexagonMCTargetDesc.h"
19
#include "llvm/ADT/APFloat.h"
20
#include "llvm/ADT/APInt.h"
21
#include "llvm/CodeGen/MachineBasicBlock.h"
22
#include "llvm/CodeGen/MachineInstr.h"
23
#include "llvm/CodeGen/MachineOperand.h"
24
#include "llvm/IR/Constants.h"
25
#include "llvm/MC/MCContext.h"
26
#include "llvm/MC/MCExpr.h"
27
#include "llvm/MC/MCInst.h"
28
#include "llvm/Support/ErrorHandling.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include <cassert>
31
32
using namespace llvm;
33
34
namespace llvm {
35
36
void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
37
                      MCInst &MCB, HexagonAsmPrinter &AP);
38
39
} // end namespace llvm
40
41
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
42
2.31k
                              HexagonAsmPrinter &Printer, bool MustExtend) {
43
2.31k
  MCContext &MC = Printer.OutContext;
44
2.31k
  const MCExpr *ME;
45
2.31k
46
2.31k
  // Populate the relocation type based on Hexagon target flags
47
2.31k
  // set on an operand
48
2.31k
  MCSymbolRefExpr::VariantKind RelocationType;
49
2.31k
  switch (MO.getTargetFlags() & ~HexagonII::HMOTF_ConstExtended) {
50
2.31k
  default:
51
2.23k
    RelocationType = MCSymbolRefExpr::VK_None;
52
2.23k
    break;
53
2.31k
  case HexagonII::MO_PCREL:
54
36
    RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
55
36
    break;
56
2.31k
  case HexagonII::MO_GOT:
57
11
    RelocationType = MCSymbolRefExpr::VK_GOT;
58
11
    break;
59
2.31k
  case HexagonII::MO_LO16:
60
0
    RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
61
0
    break;
62
2.31k
  case HexagonII::MO_HI16:
63
0
    RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
64
0
    break;
65
2.31k
  case HexagonII::MO_GPREL:
66
0
    RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
67
0
    break;
68
2.31k
  case HexagonII::MO_GDGOT:
69
12
    RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
70
12
    break;
71
2.31k
  case HexagonII::MO_GDPLT:
72
16
    RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
73
16
    break;
74
2.31k
  case HexagonII::MO_IE:
75
4
    RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
76
4
    break;
77
2.31k
  case HexagonII::MO_IEGOT:
78
2
    RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
79
2
    break;
80
2.31k
  case HexagonII::MO_TPREL:
81
4
    RelocationType = MCSymbolRefExpr::VK_TPREL;
82
4
    break;
83
2.31k
  }
84
2.31k
85
2.31k
  ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
86
2.31k
87
2.31k
  if (!MO.isJTI() && 
MO.getOffset()2.31k
)
88
140
    ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
89
140
                                 MC);
90
2.31k
91
2.31k
  ME = HexagonMCExpr::create(ME, MC);
92
2.31k
  HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
93
2.31k
  return MCOperand::createExpr(ME);
94
2.31k
}
95
96
// Create an MCInst from a MachineInstr
97
void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
98
33.7k
                            MCInst &MCB, HexagonAsmPrinter &AP) {
99
33.7k
  if (MI->getOpcode() == Hexagon::ENDLOOP0) {
100
214
    HexagonMCInstrInfo::setInnerLoop(MCB);
101
214
    return;
102
214
  }
103
33.5k
  if (MI->getOpcode() == Hexagon::ENDLOOP1) {
104
18
    HexagonMCInstrInfo::setOuterLoop(MCB);
105
18
    return;
106
18
  }
107
33.4k
  MCInst *MCI = new (AP.OutContext) MCInst;
108
33.4k
  MCI->setOpcode(MI->getOpcode());
109
33.4k
  assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
110
33.4k
         "MCI opcode should have been set on construction");
111
33.4k
112
154k
  for (unsigned i = 0, e = MI->getNumOperands(); i < e; 
i++120k
) {
113
120k
    const MachineOperand &MO = MI->getOperand(i);
114
120k
    MCOperand MCO;
115
120k
    bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
116
120k
117
120k
    switch (MO.getType()) {
118
120k
    default:
119
0
      MI->print(errs());
120
0
      llvm_unreachable("unknown operand type");
121
120k
    case MachineOperand::MO_RegisterMask:
122
740
      continue;
123
120k
    case MachineOperand::MO_Register:
124
100k
      // Ignore all implicit register operands.
125
100k
      if (MO.isImplicit())
126
32.2k
        continue;
127
68.5k
      MCO = MCOperand::createReg(MO.getReg());
128
68.5k
      break;
129
68.5k
    case MachineOperand::MO_FPImmediate: {
130
0
      APFloat Val = MO.getFPImm()->getValueAPF();
131
0
      // FP immediates are used only when setting GPRs, so they may be dealt
132
0
      // with like regular immediates from this point on.
133
0
      auto Expr = HexagonMCExpr::create(
134
0
          MCConstantExpr::create(*Val.bitcastToAPInt().getRawData(),
135
0
                                 AP.OutContext),
136
0
          AP.OutContext);
137
0
      HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
138
0
      MCO = MCOperand::createExpr(Expr);
139
0
      break;
140
68.5k
    }
141
68.5k
    case MachineOperand::MO_Immediate: {
142
15.8k
      auto Expr = HexagonMCExpr::create(
143
15.8k
          MCConstantExpr::create(MO.getImm(), AP.OutContext), AP.OutContext);
144
15.8k
      HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
145
15.8k
      MCO = MCOperand::createExpr(Expr);
146
15.8k
      break;
147
68.5k
    }
148
68.5k
    case MachineOperand::MO_MachineBasicBlock: {
149
1.06k
      MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
150
1.06k
                                                   AP.OutContext);
151
1.06k
      Expr = HexagonMCExpr::create(Expr, AP.OutContext);
152
1.06k
      HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
153
1.06k
      MCO = MCOperand::createExpr(Expr);
154
1.06k
      break;
155
68.5k
    }
156
68.5k
    case MachineOperand::MO_GlobalAddress:
157
1.88k
      MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
158
1.88k
      break;
159
68.5k
    case MachineOperand::MO_ExternalSymbol:
160
257
      MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
161
257
                         AP, MustExtend);
162
257
      break;
163
68.5k
    case MachineOperand::MO_JumpTableIndex:
164
7
      MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
165
7
      break;
166
68.5k
    case MachineOperand::MO_ConstantPoolIndex:
167
165
      MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
168
165
      break;
169
68.5k
    case MachineOperand::MO_BlockAddress:
170
4
      MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
171
4
                         MustExtend);
172
4
      break;
173
87.7k
    }
174
87.7k
175
87.7k
    MCI->addOperand(MCO);
176
87.7k
  }
177
33.4k
  AP.HexagonProcessInstruction(*MCI, *MI);
178
33.4k
  HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
179
33.4k
  MCB.addOperand(MCOperand::createInst(MCI));
180
33.4k
}