LLVM  9.0.0svn
SystemZExpandPseudo.cpp
Go to the documentation of this file.
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"
22 using namespace llvm;
23 
24 #define SYSTEMZ_EXPAND_PSEUDO_NAME "SystemZ pseudo instruction expansion pass"
25 
26 namespace llvm {
28 }
29 
30 namespace {
31 class SystemZExpandPseudo : public MachineFunctionPass {
32 public:
33  static char ID;
34  SystemZExpandPseudo() : MachineFunctionPass(ID) {
36  }
37 
38  const SystemZInstrInfo *TII;
39 
40  bool runOnMachineFunction(MachineFunction &Fn) override;
41 
42  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 };
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.
59  return new SystemZExpandPseudo();
60 }
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,
66  MachineBasicBlock::iterator &NextMBBI) {
67  MachineFunction &MF = *MBB.getParent();
68  const BasicBlock *BB = MBB.getBasicBlock();
69  MachineInstr &MI = *MBBI;
70  DebugLoc DL = MI.getDebugLoc();
71  unsigned DestReg = MI.getOperand(0).getReg();
72  unsigned SrcReg = MI.getOperand(2).getReg();
73  unsigned CCValid = MI.getOperand(3).getImm();
74  unsigned CCMask = MI.getOperand(4).getImm();
75 
76  LivePhysRegs LiveRegs(TII->getRegisterInfo());
77  LiveRegs.addLiveOuts(MBB);
78  for (auto I = std::prev(MBB.end()); I != MBBI; --I)
79  LiveRegs.stepBackward(*I);
80 
81  // Splice MBB at MI, moving the rest of the block into RestMBB.
83  MF.insert(std::next(MachineFunction::iterator(MBB)), RestMBB);
84  RestMBB->splice(RestMBB->begin(), &MBB, MI, MBB.end());
85  RestMBB->transferSuccessors(&MBB);
86  for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
87  RestMBB->addLiveIn(*I);
88 
89  // Create a new block MoveMBB to hold the move instruction.
91  MF.insert(std::next(MachineFunction::iterator(MBB)), MoveMBB);
92  MoveMBB->addLiveIn(SrcReg);
93  for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
94  MoveMBB->addLiveIn(*I);
95 
96  // At the end of MBB, create a conditional branch to RestMBB if the
97  // condition is false, otherwise fall through to MoveMBB.
98  BuildMI(&MBB, DL, TII->get(SystemZ::BRC))
99  .addImm(CCValid).addImm(CCMask ^ CCValid).addMBB(RestMBB);
100  MBB.addSuccessor(RestMBB);
101  MBB.addSuccessor(MoveMBB);
102 
103  // In MoveMBB, emit an instruction to move SrcReg into DestReg,
104  // then fall through to RestMBB.
105  TII->copyPhysReg(*MoveMBB, MoveMBB->end(), DL, DestReg, SrcReg,
106  MI.getOperand(2).isKill());
107  MoveMBB->addSuccessor(RestMBB);
108 
109  NextMBBI = MBB.end();
110  MI.eraseFromParent();
111  return true;
112 }
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,
118  MachineBasicBlock::iterator &NextMBBI) {
119  MachineInstr &MI = *MBBI;
120  switch (MI.getOpcode()) {
121  case SystemZ::LOCRMux:
122  return expandLOCRMux(MBB, MBBI, NextMBBI);
123  default:
124  break;
125  }
126  return false;
127 }
128 
129 /// Iterate over the instructions in basic block MBB and expand any
130 /// pseudo instructions. Return true if anything was modified.
131 bool SystemZExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
132  bool Modified = false;
133 
134  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
135  while (MBBI != E) {
136  MachineBasicBlock::iterator NMBBI = std::next(MBBI);
137  Modified |= expandMI(MBB, MBBI, NMBBI);
138  MBBI = NMBBI;
139  }
140 
141  return Modified;
142 }
143 
144 bool SystemZExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
145  TII = static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
146 
147  bool Modified = false;
148  for (auto &MBB : MF)
149  Modified |= expandMBB(MBB);
150  return Modified;
151 }
152 
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:23
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:384
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:33
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:410
#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:57
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:284
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:63
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:48
#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:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:415