Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Lanai/LanaiAsmPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- LanaiAsmPrinter.cpp - Lanai LLVM assembly writer ------------------===//
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 a printer that converts from our internal representation
10
// of machine-dependent LLVM code to the Lanai assembly language.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "MCTargetDesc/LanaiInstPrinter.h"
15
#include "LanaiAluCode.h"
16
#include "LanaiCondCode.h"
17
#include "LanaiInstrInfo.h"
18
#include "LanaiMCInstLower.h"
19
#include "LanaiTargetMachine.h"
20
#include "TargetInfo/LanaiTargetInfo.h"
21
#include "llvm/CodeGen/AsmPrinter.h"
22
#include "llvm/CodeGen/MachineConstantPool.h"
23
#include "llvm/CodeGen/MachineFunctionPass.h"
24
#include "llvm/CodeGen/MachineInstr.h"
25
#include "llvm/CodeGen/MachineModuleInfo.h"
26
#include "llvm/IR/Constants.h"
27
#include "llvm/IR/DerivedTypes.h"
28
#include "llvm/IR/Mangler.h"
29
#include "llvm/IR/Module.h"
30
#include "llvm/MC/MCAsmInfo.h"
31
#include "llvm/MC/MCInst.h"
32
#include "llvm/MC/MCInstBuilder.h"
33
#include "llvm/MC/MCStreamer.h"
34
#include "llvm/MC/MCSymbol.h"
35
#include "llvm/Support/TargetRegistry.h"
36
#include "llvm/Support/raw_ostream.h"
37
38
#define DEBUG_TYPE "asm-printer"
39
40
using namespace llvm;
41
42
namespace {
43
class LanaiAsmPrinter : public AsmPrinter {
44
public:
45
  explicit LanaiAsmPrinter(TargetMachine &TM,
46
                           std::unique_ptr<MCStreamer> Streamer)
47
22
      : AsmPrinter(TM, std::move(Streamer)) {}
48
49
92
  StringRef getPassName() const override { return "Lanai Assembly Printer"; }
50
51
  void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
52
  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
53
                       const char *ExtraCode, raw_ostream &O) override;
54
  void EmitInstruction(const MachineInstr *MI) override;
55
  bool isBlockOnlyReachableByFallthrough(
56
      const MachineBasicBlock *MBB) const override;
57
58
private:
59
  void customEmitInstruction(const MachineInstr *MI);
60
  void emitCallInstruction(const MachineInstr *MI);
61
};
62
} // end of anonymous namespace
63
64
void LanaiAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
65
0
                                   raw_ostream &O) {
66
0
  const MachineOperand &MO = MI->getOperand(OpNum);
67
0
68
0
  switch (MO.getType()) {
69
0
  case MachineOperand::MO_Register:
70
0
    O << LanaiInstPrinter::getRegisterName(MO.getReg());
71
0
    break;
72
0
73
0
  case MachineOperand::MO_Immediate:
74
0
    O << MO.getImm();
75
0
    break;
76
0
77
0
  case MachineOperand::MO_MachineBasicBlock:
78
0
    O << *MO.getMBB()->getSymbol();
79
0
    break;
80
0
81
0
  case MachineOperand::MO_GlobalAddress:
82
0
    O << *getSymbol(MO.getGlobal());
83
0
    break;
84
0
85
0
  case MachineOperand::MO_BlockAddress: {
86
0
    MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
87
0
    O << BA->getName();
88
0
    break;
89
0
  }
90
0
91
0
  case MachineOperand::MO_ExternalSymbol:
92
0
    O << *GetExternalSymbolSymbol(MO.getSymbolName());
93
0
    break;
94
0
95
0
  case MachineOperand::MO_JumpTableIndex:
96
0
    O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
97
0
      << MO.getIndex();
98
0
    break;
99
0
100
0
  case MachineOperand::MO_ConstantPoolIndex:
101
0
    O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
102
0
      << MO.getIndex();
103
0
    return;
104
0
105
0
  default:
106
0
    llvm_unreachable("<unknown operand type>");
107
0
  }
108
0
}
109
110
// PrintAsmOperand - Print out an operand for an inline asm expression.
111
bool LanaiAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
112
3
                                      const char *ExtraCode, raw_ostream &O) {
113
3
  // Does this asm operand have a single letter operand modifier?
114
3
  if (ExtraCode && ExtraCode[0]) {
115
3
    if (ExtraCode[1])
116
0
      return true; // Unknown modifier.
117
3
118
3
    switch (ExtraCode[0]) {
119
3
    // The highest-numbered register of a pair.
120
3
    case 'H': {
121
0
      if (OpNo == 0)
122
0
        return true;
123
0
      const MachineOperand &FlagsOP = MI->getOperand(OpNo - 1);
124
0
      if (!FlagsOP.isImm())
125
0
        return true;
126
0
      unsigned Flags = FlagsOP.getImm();
127
0
      unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
128
0
      if (NumVals != 2)
129
0
        return true;
130
0
      unsigned RegOp = OpNo + 1;
131
0
      if (RegOp >= MI->getNumOperands())
132
0
        return true;
133
0
      const MachineOperand &MO = MI->getOperand(RegOp);
134
0
      if (!MO.isReg())
135
0
        return true;
136
0
      unsigned Reg = MO.getReg();
137
0
      O << LanaiInstPrinter::getRegisterName(Reg);
138
0
      return false;
139
0
    }
140
3
    default:
141
3
      return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
142
0
    }
143
0
  }
144
0
  printOperand(MI, OpNo, O);
145
0
  return false;
146
0
}
147
148
//===----------------------------------------------------------------------===//
149
13
void LanaiAsmPrinter::emitCallInstruction(const MachineInstr *MI) {
150
13
  assert((MI->getOpcode() == Lanai::CALL || MI->getOpcode() == Lanai::CALLR) &&
151
13
         "Unsupported call function");
152
13
153
13
  LanaiMCInstLower MCInstLowering(OutContext, *this);
154
13
  MCSubtargetInfo STI = getSubtargetInfo();
155
13
  // Insert save rca instruction immediately before the call.
156
13
  // TODO: We should generate a pc-relative mov instruction here instead
157
13
  // of pc + 16 (should be mov .+16 %rca).
158
13
  OutStreamer->EmitInstruction(MCInstBuilder(Lanai::ADD_I_LO)
159
13
                                   .addReg(Lanai::RCA)
160
13
                                   .addReg(Lanai::PC)
161
13
                                   .addImm(16),
162
13
                               STI);
163
13
164
13
  // Push rca onto the stack.
165
13
  //   st %rca, [--%sp]
166
13
  OutStreamer->EmitInstruction(MCInstBuilder(Lanai::SW_RI)
167
13
                                   .addReg(Lanai::RCA)
168
13
                                   .addReg(Lanai::SP)
169
13
                                   .addImm(-4)
170
13
                                   .addImm(LPAC::makePreOp(LPAC::ADD)),
171
13
                               STI);
172
13
173
13
  // Lower the call instruction.
174
13
  if (MI->getOpcode() == Lanai::CALL) {
175
13
    MCInst TmpInst;
176
13
    MCInstLowering.Lower(MI, TmpInst);
177
13
    TmpInst.setOpcode(Lanai::BT);
178
13
    OutStreamer->EmitInstruction(TmpInst, STI);
179
13
  } else {
180
0
    OutStreamer->EmitInstruction(MCInstBuilder(Lanai::ADD_R)
181
0
                                     .addReg(Lanai::PC)
182
0
                                     .addReg(MI->getOperand(0).getReg())
183
0
                                     .addReg(Lanai::R0)
184
0
                                     .addImm(LPCC::ICC_T),
185
0
                                 STI);
186
0
  }
187
13
}
188
189
903
void LanaiAsmPrinter::customEmitInstruction(const MachineInstr *MI) {
190
903
  LanaiMCInstLower MCInstLowering(OutContext, *this);
191
903
  MCSubtargetInfo STI = getSubtargetInfo();
192
903
  MCInst TmpInst;
193
903
  MCInstLowering.Lower(MI, TmpInst);
194
903
  OutStreamer->EmitInstruction(TmpInst, STI);
195
903
}
196
197
793
void LanaiAsmPrinter::EmitInstruction(const MachineInstr *MI) {
198
793
  MachineBasicBlock::const_instr_iterator I = MI->getIterator();
199
793
  MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
200
793
201
916
  do {
202
916
    if (I->isCall()) {
203
13
      emitCallInstruction(&*I);
204
13
      continue;
205
13
    }
206
903
207
903
    customEmitInstruction(&*I);
208
916
  } while ((++I != E) && 
I->isInsideBundle()797
);
209
793
}
210
211
// isBlockOnlyReachableByFallthough - Return true if the basic block has
212
// exactly one predecessor and the control transfer mechanism between
213
// the predecessor and this block is a fall-through.
214
// FIXME: could the overridden cases be handled in AnalyzeBranch?
215
bool LanaiAsmPrinter::isBlockOnlyReachableByFallthrough(
216
81
    const MachineBasicBlock *MBB) const {
217
81
  // The predecessor has to be immediately before this block.
218
81
  const MachineBasicBlock *Pred = *MBB->pred_begin();
219
81
220
81
  // If the predecessor is a switch statement, assume a jump table
221
81
  // implementation, so it is not a fall through.
222
81
  if (const BasicBlock *B = Pred->getBasicBlock())
223
78
    if (isa<SwitchInst>(B->getTerminator()))
224
0
      return false;
225
81
226
81
  // Check default implementation
227
81
  if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
228
57
    return false;
229
24
230
24
  // Otherwise, check the last instruction.
231
24
  // Check if the last terminator is an unconditional branch.
232
24
  MachineBasicBlock::const_iterator I = Pred->end();
233
24
  while (I != Pred->begin() && !(--I)->isTerminator()) {
234
0
  }
235
24
236
24
  return !I->isBarrier();
237
24
}
238
239
// Force static initialization.
240
78.9k
extern "C" void LLVMInitializeLanaiAsmPrinter() {
241
78.9k
  RegisterAsmPrinter<LanaiAsmPrinter> X(getTheLanaiTarget());
242
78.9k
}