Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/SystemZ/SystemZExpandPseudo.cpp
Line
Count
Source
1
//==-- SystemZExpandPseudo.cpp - Expand pseudo instructions -------*- C++ -*-=//
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 pass that expands pseudo instructions into target
10
// instructions to allow proper scheduling and other late optimizations.  This
11
// pass should be run after register allocation but before the post-regalloc
12
// scheduling pass.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "SystemZ.h"
17
#include "SystemZInstrInfo.h"
18
#include "SystemZSubtarget.h"
19
#include "llvm/CodeGen/LivePhysRegs.h"
20
#include "llvm/CodeGen/MachineFunctionPass.h"
21
#include "llvm/CodeGen/MachineInstrBuilder.h"
22
using namespace llvm;
23
24
9.13k
#define SYSTEMZ_EXPAND_PSEUDO_NAME "SystemZ pseudo instruction expansion pass"
25
26
namespace llvm {
27
  void initializeSystemZExpandPseudoPass(PassRegistry&);
28
}
29
30
namespace {
31
class SystemZExpandPseudo : public MachineFunctionPass {
32
public:
33
  static char ID;
34
1.01k
  SystemZExpandPseudo() : MachineFunctionPass(ID) {
35
1.01k
    initializeSystemZExpandPseudoPass(*PassRegistry::getPassRegistry());
36
1.01k
  }
37
38
  const SystemZInstrInfo *TII;
39
40
  bool runOnMachineFunction(MachineFunction &Fn) override;
41
42
9.13k
  StringRef getPassName() const override { return SYSTEMZ_EXPAND_PSEUDO_NAME; }
43
44
private:
45
  bool expandMBB(MachineBasicBlock &MBB);
46
  bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
47
                MachineBasicBlock::iterator &NextMBBI);
48
  bool expandLOCRMux(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
49
                     MachineBasicBlock::iterator &NextMBBI);
50
};
51
char SystemZExpandPseudo::ID = 0;
52
}
53
54
INITIALIZE_PASS(SystemZExpandPseudo, "systemz-expand-pseudo",
55
                SYSTEMZ_EXPAND_PSEUDO_NAME, false, false)
56
57
/// Returns an instance of the pseudo instruction expansion pass.
58
1.01k
FunctionPass *llvm::createSystemZExpandPseudoPass(SystemZTargetMachine &TM) {
59
1.01k
  return new SystemZExpandPseudo();
60
1.01k
}
61
62
// MI is a load-register-on-condition pseudo instruction that could not be
63
// handled as a single hardware instruction.  Replace it by a branch sequence.
64
bool SystemZExpandPseudo::expandLOCRMux(MachineBasicBlock &MBB,
65
                                        MachineBasicBlock::iterator MBBI,
66
8
                                        MachineBasicBlock::iterator &NextMBBI) {
67
8
  MachineFunction &MF = *MBB.getParent();
68
8
  const BasicBlock *BB = MBB.getBasicBlock();
69
8
  MachineInstr &MI = *MBBI;
70
8
  DebugLoc DL = MI.getDebugLoc();
71
8
  unsigned DestReg = MI.getOperand(0).getReg();
72
8
  unsigned SrcReg = MI.getOperand(2).getReg();
73
8
  unsigned CCValid = MI.getOperand(3).getImm();
74
8
  unsigned CCMask = MI.getOperand(4).getImm();
75
8
76
8
  LivePhysRegs LiveRegs(TII->getRegisterInfo());
77
8
  LiveRegs.addLiveOuts(MBB);
78
38
  for (auto I = std::prev(MBB.end()); I != MBBI; 
--I30
)
79
30
    LiveRegs.stepBackward(*I);
80
8
81
8
  // Splice MBB at MI, moving the rest of the block into RestMBB.
82
8
  MachineBasicBlock *RestMBB = MF.CreateMachineBasicBlock(BB);
83
8
  MF.insert(std::next(MachineFunction::iterator(MBB)), RestMBB);
84
8
  RestMBB->splice(RestMBB->begin(), &MBB, MI, MBB.end());
85
8
  RestMBB->transferSuccessors(&MBB);
86
398
  for (auto I = LiveRegs.begin(); I != LiveRegs.end(); 
++I390
)
87
390
    RestMBB->addLiveIn(*I);
88
8
89
8
  // Create a new block MoveMBB to hold the move instruction.
90
8
  MachineBasicBlock *MoveMBB = MF.CreateMachineBasicBlock(BB);
91
8
  MF.insert(std::next(MachineFunction::iterator(MBB)), MoveMBB);
92
8
  MoveMBB->addLiveIn(SrcReg);
93
398
  for (auto I = LiveRegs.begin(); I != LiveRegs.end(); 
++I390
)
94
390
    MoveMBB->addLiveIn(*I);
95
8
96
8
  // At the end of MBB, create a conditional branch to RestMBB if the
97
8
  // condition is false, otherwise fall through to MoveMBB.
98
8
  BuildMI(&MBB, DL, TII->get(SystemZ::BRC))
99
8
    .addImm(CCValid).addImm(CCMask ^ CCValid).addMBB(RestMBB);
100
8
  MBB.addSuccessor(RestMBB);
101
8
  MBB.addSuccessor(MoveMBB);
102
8
103
8
  // In MoveMBB, emit an instruction to move SrcReg into DestReg,
104
8
  // then fall through to RestMBB.
105
8
  TII->copyPhysReg(*MoveMBB, MoveMBB->end(), DL, DestReg, SrcReg,
106
8
                   MI.getOperand(2).isKill());
107
8
  MoveMBB->addSuccessor(RestMBB);
108
8
109
8
  NextMBBI = MBB.end();
110
8
  MI.eraseFromParent();
111
8
  return true;
112
8
}
113
114
/// If MBBI references a pseudo instruction that should be expanded here,
115
/// do the expansion and return true.  Otherwise return false.
116
bool SystemZExpandPseudo::expandMI(MachineBasicBlock &MBB,
117
                                   MachineBasicBlock::iterator MBBI,
118
66.0k
                                   MachineBasicBlock::iterator &NextMBBI) {
119
66.0k
  MachineInstr &MI = *MBBI;
120
66.0k
  switch (MI.getOpcode()) {
121
66.0k
  case SystemZ::LOCRMux:
122
8
    return expandLOCRMux(MBB, MBBI, NextMBBI);
123
66.0k
  default:
124
66.0k
    break;
125
66.0k
  }
126
66.0k
  return false;
127
66.0k
}
128
129
/// Iterate over the instructions in basic block MBB and expand any
130
/// pseudo instructions.  Return true if anything was modified.
131
11.9k
bool SystemZExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
132
11.9k
  bool Modified = false;
133
11.9k
134
11.9k
  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
135
78.0k
  while (MBBI != E) {
136
66.0k
    MachineBasicBlock::iterator NMBBI = std::next(MBBI);
137
66.0k
    Modified |= expandMI(MBB, MBBI, NMBBI);
138
66.0k
    MBBI = NMBBI;
139
66.0k
  }
140
11.9k
141
11.9k
  return Modified;
142
11.9k
}
143
144
8.12k
bool SystemZExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
145
8.12k
  TII = static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
146
8.12k
147
8.12k
  bool Modified = false;
148
8.12k
  for (auto &MBB : MF)
149
11.9k
    Modified |= expandMBB(MBB);
150
8.12k
  return Modified;
151
8.12k
}
152