Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Lanai/LanaiMCInstLower.cpp
Line
Count
Source (jump to first uncovered line)
1
//=-- LanaiMCInstLower.cpp - Convert Lanai 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 Lanai MachineInstrs to their corresponding
10
// MCInst records.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "LanaiMCInstLower.h"
15
16
#include "MCTargetDesc/LanaiBaseInfo.h"
17
#include "MCTargetDesc/LanaiMCExpr.h"
18
#include "llvm/ADT/SmallString.h"
19
#include "llvm/CodeGen/AsmPrinter.h"
20
#include "llvm/CodeGen/MachineBasicBlock.h"
21
#include "llvm/CodeGen/MachineInstr.h"
22
#include "llvm/IR/Constants.h"
23
#include "llvm/MC/MCAsmInfo.h"
24
#include "llvm/MC/MCContext.h"
25
#include "llvm/MC/MCExpr.h"
26
#include "llvm/MC/MCInst.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include "llvm/Support/raw_ostream.h"
29
30
using namespace llvm;
31
32
MCSymbol *
33
22
LanaiMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
34
22
  return Printer.getSymbol(MO.getGlobal());
35
22
}
36
37
MCSymbol *
38
0
LanaiMCInstLower::GetBlockAddressSymbol(const MachineOperand &MO) const {
39
0
  return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
40
0
}
41
42
MCSymbol *
43
7
LanaiMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const {
44
7
  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
45
7
}
46
47
0
MCSymbol *LanaiMCInstLower::GetJumpTableSymbol(const MachineOperand &MO) const {
48
0
  SmallString<256> Name;
49
0
  raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
50
0
                            << Printer.getFunctionNumber() << '_'
51
0
                            << MO.getIndex();
52
0
  // Create a symbol for the name.
53
0
  return Ctx.getOrCreateSymbol(Name.str());
54
0
}
55
56
MCSymbol *
57
0
LanaiMCInstLower::GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
58
0
  SmallString<256> Name;
59
0
  raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
60
0
                            << Printer.getFunctionNumber() << '_'
61
0
                            << MO.getIndex();
62
0
  // Create a symbol for the name.
63
0
  return Ctx.getOrCreateSymbol(Name.str());
64
0
}
65
66
MCOperand LanaiMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
67
29
                                               MCSymbol *Sym) const {
68
29
  LanaiMCExpr::VariantKind Kind;
69
29
70
29
  switch (MO.getTargetFlags()) {
71
29
  case LanaiII::MO_NO_FLAG:
72
15
    Kind = LanaiMCExpr::VK_Lanai_None;
73
15
    break;
74
29
  case LanaiII::MO_ABS_HI:
75
7
    Kind = LanaiMCExpr::VK_Lanai_ABS_HI;
76
7
    break;
77
29
  case LanaiII::MO_ABS_LO:
78
7
    Kind = LanaiMCExpr::VK_Lanai_ABS_LO;
79
7
    break;
80
29
  default:
81
0
    llvm_unreachable("Unknown target flag on GV operand");
82
29
  }
83
29
84
29
  const MCExpr *Expr =
85
29
      MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx);
86
29
  if (!MO.isJTI() && MO.getOffset())
87
0
    Expr = MCBinaryExpr::createAdd(
88
0
        Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
89
29
  Expr = LanaiMCExpr::create(Kind, Expr, Ctx);
90
29
  return MCOperand::createExpr(Expr);
91
29
}
92
93
916
void LanaiMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
94
916
  OutMI.setOpcode(MI->getOpcode());
95
916
96
3.93k
  for (unsigned I = 0, E = MI->getNumOperands(); I != E; 
++I3.01k
) {
97
3.01k
    const MachineOperand &MO = MI->getOperand(I);
98
3.01k
99
3.01k
    MCOperand MCOp;
100
3.01k
    switch (MO.getType()) {
101
3.01k
    case MachineOperand::MO_Register:
102
1.96k
      // Ignore all implicit register operands.
103
1.96k
      if (MO.isImplicit())
104
396
        continue;
105
1.56k
      MCOp = MCOperand::createReg(MO.getReg());
106
1.56k
      break;
107
1.56k
    case MachineOperand::MO_Immediate:
108
993
      MCOp = MCOperand::createImm(MO.getImm());
109
993
      break;
110
1.56k
    case MachineOperand::MO_MachineBasicBlock:
111
20
      MCOp = MCOperand::createExpr(
112
20
          MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
113
20
      break;
114
1.56k
    case MachineOperand::MO_RegisterMask:
115
13
      continue;
116
1.56k
    case MachineOperand::MO_GlobalAddress:
117
22
      MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
118
22
      break;
119
1.56k
    case MachineOperand::MO_BlockAddress:
120
0
      MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO));
121
0
      break;
122
1.56k
    case MachineOperand::MO_ExternalSymbol:
123
7
      MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
124
7
      break;
125
1.56k
    case MachineOperand::MO_JumpTableIndex:
126
0
      MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
127
0
      break;
128
1.56k
    case MachineOperand::MO_ConstantPoolIndex:
129
0
      MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
130
0
      break;
131
1.56k
    default:
132
0
      MI->print(errs());
133
0
      llvm_unreachable("unknown operand type");
134
2.60k
    }
135
2.60k
136
2.60k
    OutMI.addOperand(MCOp);
137
2.60k
  }
138
916
}