LLVM  8.0.1
SystemZExpandPseudo.cpp
Go to the documentation of this file.
1 //==-- SystemZExpandPseudo.cpp - Expand pseudo instructions -------*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a pass that expands pseudo instructions into target
11 // instructions to allow proper scheduling and other late optimizations. This
12 // pass should be run after register allocation but before the post-regalloc
13 // scheduling pass.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "SystemZ.h"
18 #include "SystemZInstrInfo.h"
19 #include "SystemZSubtarget.h"
23 using namespace llvm;
24 
25 #define SYSTEMZ_EXPAND_PSEUDO_NAME "SystemZ pseudo instruction expansion pass"
26 
27 namespace llvm {
29 }
30 
31 namespace {
32 class SystemZExpandPseudo : public MachineFunctionPass {
33 public:
34  static char ID;
35  SystemZExpandPseudo() : MachineFunctionPass(ID) {
37  }
38 
39  const SystemZInstrInfo *TII;
40 
41  bool runOnMachineFunction(MachineFunction &Fn) override;
42 
43  StringRef getPassName() const override { return SYSTEMZ_EXPAND_PSEUDO_NAME; }
44 
45 private:
46  bool expandMBB(MachineBasicBlock &MBB);
47  bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
48  MachineBasicBlock::iterator &NextMBBI);
49  bool expandLOCRMux(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
50  MachineBasicBlock::iterator &NextMBBI);
51 };
53 }
54 
55 INITIALIZE_PASS(SystemZExpandPseudo, "systemz-expand-pseudo",
56  SYSTEMZ_EXPAND_PSEUDO_NAME, false, false)
57 
58 /// Returns an instance of the pseudo instruction expansion pass.
60  return new SystemZExpandPseudo();
61 }
62 
63 // MI is a load-register-on-condition pseudo instruction that could not be
64 // handled as a single hardware instruction. Replace it by a branch sequence.
65 bool SystemZExpandPseudo::expandLOCRMux(MachineBasicBlock &MBB,
67  MachineBasicBlock::iterator &NextMBBI) {
68  MachineFunction &MF = *MBB.getParent();
69  const BasicBlock *BB = MBB.getBasicBlock();
70  MachineInstr &MI = *MBBI;
71  DebugLoc DL = MI.getDebugLoc();
72  unsigned DestReg = MI.getOperand(0).getReg();
73  unsigned SrcReg = MI.getOperand(2).getReg();
74  unsigned CCValid = MI.getOperand(3).getImm();
75  unsigned CCMask = MI.getOperand(4).getImm();
76 
77  LivePhysRegs LiveRegs(TII->getRegisterInfo());
78  LiveRegs.addLiveOuts(MBB);
79  for (auto I = std::prev(MBB.end()); I != MBBI; --I)
80  LiveRegs.stepBackward(*I);
81 
82  // Splice MBB at MI, moving the rest of the block into RestMBB.
84  MF.insert(std::next(MachineFunction::iterator(MBB)), RestMBB);
85  RestMBB->splice(RestMBB->begin(), &MBB, MI, MBB.end());
86  RestMBB->transferSuccessors(&MBB);
87  for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
88  RestMBB->addLiveIn(*I);
89 
90  // Create a new block MoveMBB to hold the move instruction.
92  MF.insert(std::next(MachineFunction::iterator(MBB)), MoveMBB);
93  MoveMBB->addLiveIn(SrcReg);
94  for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
95  MoveMBB->addLiveIn(*I);
96 
97  // At the end of MBB, create a conditional branch to RestMBB if the
98  // condition is false, otherwise fall through to MoveMBB.
99  BuildMI(&MBB, DL, TII->get(SystemZ::BRC))
100  .addImm(CCValid).addImm(CCMask ^ CCValid).addMBB(RestMBB);
101  MBB.addSuccessor(RestMBB);
102  MBB.addSuccessor(MoveMBB);
103 
104  // In MoveMBB, emit an instruction to move SrcReg into DestReg,
105  // then fall through to RestMBB.
106  TII->copyPhysReg(*MoveMBB, MoveMBB->end(), DL, DestReg, SrcReg,
107  MI.getOperand(2).isKill());
108  MoveMBB->addSuccessor(RestMBB);
109 
110  NextMBBI = MBB.end();
111  MI.eraseFromParent();
112  return true;
113 }
114 
115 /// If MBBI references a pseudo instruction that should be expanded here,
116 /// do the expansion and return true. Otherwise return false.
117 bool SystemZExpandPseudo::expandMI(MachineBasicBlock &MBB,
119  MachineBasicBlock::iterator &NextMBBI) {
120  MachineInstr &MI = *MBBI;
121  switch (MI.getOpcode()) {
122  case SystemZ::LOCRMux:
123  return expandLOCRMux(MBB, MBBI, NextMBBI);
124  default:
125  break;
126  }
127  return false;
128 }
129 
130 /// Iterate over the instructions in basic block MBB and expand any
131 /// pseudo instructions. Return true if anything was modified.
132 bool SystemZExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
133  bool Modified = false;
134 
135  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
136  while (MBBI != E) {
137  MachineBasicBlock::iterator NMBBI = std::next(MBBI);
138  Modified |= expandMI(MBB, MBBI, NMBBI);
139  MBBI = NMBBI;
140  }
141 
142  return Modified;
143 }
144 
145 bool SystemZExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
146  TII = static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
147 
148  bool Modified = false;
149  for (auto &MBB : MF)
150  Modified |= expandMBB(MBB);
151  return Modified;
152 }
153 
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:383
unsigned getReg() const
getReg - Returns the register number.
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
A debug info location.
Definition: DebugLoc.h:34
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
#define SYSTEMZ_EXPAND_PSEUDO_NAME
virtual const TargetInstrInfo * getInstrInfo() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
FunctionPass * createSystemZExpandPseudoPass(SystemZTargetMachine &TM)
Iterator for intrusive lists based on ilist_node.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void initializeSystemZExpandPseudoPass(PassRegistry &)
int64_t getImm() const
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, unsigned DestReg, unsigned SrcReg, bool KillSrc) const override
Emit instructions to copy a pair of physical registers.
Representation of each machine instruction.
Definition: MachineInstr.h:64
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB &#39;Other&#39; at the position From, and insert it into this MBB right before &#39;...
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:49
#define I(x, y, z)
Definition: MD5.cpp:58
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
void insert(iterator MBBI, MachineBasicBlock *MBB)
INITIALIZE_PASS(SystemZExpandPseudo, "systemz-expand-pseudo", SYSTEMZ_EXPAND_PSEUDO_NAME, false, false) FunctionPass *llvm
Returns an instance of the pseudo instruction expansion pass.
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414