Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/XCore/XCoreMCInstLower.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- XCoreMCInstLower.cpp - Convert XCore MachineInstr to 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
/// \file
10
/// This file contains code to lower XCore MachineInstrs to their
11
/// corresponding MCInst records.
12
///
13
//===----------------------------------------------------------------------===//
14
#include "XCoreMCInstLower.h"
15
#include "llvm/CodeGen/AsmPrinter.h"
16
#include "llvm/CodeGen/MachineFunction.h"
17
#include "llvm/CodeGen/MachineInstr.h"
18
#include "llvm/CodeGen/MachineOperand.h"
19
#include "llvm/IR/Mangler.h"
20
#include "llvm/MC/MCContext.h"
21
#include "llvm/MC/MCExpr.h"
22
#include "llvm/MC/MCInst.h"
23
24
using namespace llvm;
25
26
XCoreMCInstLower::XCoreMCInstLower(class AsmPrinter &asmprinter)
27
70
    : Printer(asmprinter) {}
28
29
278
void XCoreMCInstLower::Initialize(MCContext *C) { Ctx = C; }
30
31
MCOperand XCoreMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
32
                                               MachineOperandType MOTy,
33
238
                                               unsigned Offset) const {
34
238
  MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
35
238
  const MCSymbol *Symbol;
36
238
37
238
  switch (MOTy) {
38
238
    case MachineOperand::MO_MachineBasicBlock:
39
34
      Symbol = MO.getMBB()->getSymbol();
40
34
      break;
41
238
    case MachineOperand::MO_GlobalAddress:
42
135
      Symbol = Printer.getSymbol(MO.getGlobal());
43
135
      Offset += MO.getOffset();
44
135
      break;
45
238
    case MachineOperand::MO_BlockAddress:
46
5
      Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress());
47
5
      Offset += MO.getOffset();
48
5
      break;
49
238
    case MachineOperand::MO_ExternalSymbol:
50
34
      Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName());
51
34
      Offset += MO.getOffset();
52
34
      break;
53
238
    case MachineOperand::MO_JumpTableIndex:
54
0
      Symbol = Printer.GetJTISymbol(MO.getIndex());
55
0
      break;
56
238
    case MachineOperand::MO_ConstantPoolIndex:
57
30
      Symbol = Printer.GetCPISymbol(MO.getIndex());
58
30
      Offset += MO.getOffset();
59
30
      break;
60
238
    default:
61
0
      llvm_unreachable("<unknown operand type>");
62
238
  }
63
238
64
238
  const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx);
65
238
66
238
  if (!Offset)
67
222
    return MCOperand::createExpr(MCSym);
68
16
69
16
  // Assume offset is never negative.
70
16
  assert(Offset > 0);
71
16
72
16
  const MCConstantExpr *OffsetExpr =  MCConstantExpr::create(Offset, *Ctx);
73
16
  const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);
74
16
  return MCOperand::createExpr(Add);
75
16
}
76
77
MCOperand XCoreMCInstLower::LowerOperand(const MachineOperand &MO,
78
4.74k
                                         unsigned offset) const {
79
4.74k
  MachineOperandType MOTy = MO.getType();
80
4.74k
81
4.74k
  switch (MOTy) {
82
4.74k
    
default: 0
llvm_unreachable0
("unknown operand type");
83
4.74k
    case MachineOperand::MO_Register:
84
3.56k
      // Ignore all implicit register operands.
85
3.56k
      if (MO.isImplicit()) 
break2.03k
;
86
1.53k
      return MCOperand::createReg(MO.getReg());
87
1.53k
    case MachineOperand::MO_Immediate:
88
937
      return MCOperand::createImm(MO.getImm() + offset);
89
1.53k
    case MachineOperand::MO_MachineBasicBlock:
90
238
    case MachineOperand::MO_GlobalAddress:
91
238
    case MachineOperand::MO_ExternalSymbol:
92
238
    case MachineOperand::MO_JumpTableIndex:
93
238
    case MachineOperand::MO_ConstantPoolIndex:
94
238
    case MachineOperand::MO_BlockAddress:
95
238
      return LowerSymbolOperand(MO, MOTy, offset);
96
238
    case MachineOperand::MO_RegisterMask:
97
0
      break;
98
2.03k
  }
99
2.03k
100
2.03k
  return MCOperand();
101
2.03k
}
102
103
1.48k
void XCoreMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
104
1.48k
  OutMI.setOpcode(MI->getOpcode());
105
1.48k
106
6.22k
  for (unsigned i = 0, e = MI->getNumOperands(); i != e; 
++i4.74k
) {
107
4.74k
    const MachineOperand &MO = MI->getOperand(i);
108
4.74k
    MCOperand MCOp = LowerOperand(MO);
109
4.74k
110
4.74k
    if (MCOp.isValid())
111
2.71k
      OutMI.addOperand(MCOp);
112
4.74k
  }
113
1.48k
}