LLVM  10.0.0svn
SystemZPostRewrite.cpp
Go to the documentation of this file.
1 //==---- SystemZPostRewrite.cpp - Select pseudos after RegAlloc ---*- 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 is run immediately after VirtRegRewriter
10 // but before MachineCopyPropagation. The purpose is to lower pseudos to
11 // target instructions before any later pass might substitute a register for
12 // another.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "SystemZ.h"
17 #include "SystemZInstrInfo.h"
18 #include "SystemZSubtarget.h"
19 #include "llvm/ADT/Statistic.h"
22 using namespace llvm;
23 
24 #define SYSTEMZ_POSTREWRITE_NAME "SystemZ Post Rewrite pass"
25 
26 #define DEBUG_TYPE "systemz-postrewrite"
27 STATISTIC(MemFoldCopies, "Number of copies inserted before folded mem ops.");
28 
29 namespace llvm {
31 }
32 
33 namespace {
34 
35 class SystemZPostRewrite : public MachineFunctionPass {
36 public:
37  static char ID;
38  SystemZPostRewrite() : MachineFunctionPass(ID) {
40  }
41 
42  const SystemZInstrInfo *TII;
43 
44  bool runOnMachineFunction(MachineFunction &Fn) override;
45 
46  StringRef getPassName() const override { return SYSTEMZ_POSTREWRITE_NAME; }
47 
48  void getAnalysisUsage(AnalysisUsage &AU) const override {
49  AU.setPreservesAll();
51  }
52 
53 private:
54  bool selectMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
55  MachineBasicBlock::iterator &NextMBBI);
56  bool selectMBB(MachineBasicBlock &MBB);
57 };
58 
59 char SystemZPostRewrite::ID = 0;
60 
61 } // end anonymous namespace
62 
63 INITIALIZE_PASS(SystemZPostRewrite, "systemz-post-rewrite",
64  SYSTEMZ_POSTREWRITE_NAME, false, false)
65 
66 /// Returns an instance of the Post Rewrite pass.
68  return new SystemZPostRewrite();
69 }
70 
71 /// If MBBI references a pseudo instruction that should be selected here,
72 /// do it and return true. Otherwise return false.
73 bool SystemZPostRewrite::selectMI(MachineBasicBlock &MBB,
75  MachineBasicBlock::iterator &NextMBBI) {
76  MachineInstr &MI = *MBBI;
77  unsigned Opcode = MI.getOpcode();
78 
79  // Note: If this could be done during regalloc in foldMemoryOperandImpl()
80  // while also updating the LiveIntervals, there would be no need for the
81  // MemFoldPseudo to begin with.
82  int TargetMemOpcode = SystemZ::getTargetMemOpcode(Opcode);
83  if (TargetMemOpcode != -1) {
84  MI.setDesc(TII->get(TargetMemOpcode));
85  MI.tieOperands(0, 1);
86  Register DstReg = MI.getOperand(0).getReg();
87  MachineOperand &SrcMO = MI.getOperand(1);
88  if (DstReg != SrcMO.getReg()) {
89  BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(SystemZ::COPY), DstReg)
90  .addReg(SrcMO.getReg());
91  SrcMO.setReg(DstReg);
92  MemFoldCopies++;
93  }
94  return true;
95  }
96 
97  return false;
98 }
99 
100 /// Iterate over the instructions in basic block MBB and select any
101 /// pseudo instructions. Return true if anything was modified.
102 bool SystemZPostRewrite::selectMBB(MachineBasicBlock &MBB) {
103  bool Modified = false;
104 
105  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
106  while (MBBI != E) {
107  MachineBasicBlock::iterator NMBBI = std::next(MBBI);
108  Modified |= selectMI(MBB, MBBI, NMBBI);
109  MBBI = NMBBI;
110  }
111 
112  return Modified;
113 }
114 
115 bool SystemZPostRewrite::runOnMachineFunction(MachineFunction &MF) {
116  TII = static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
117 
118  bool Modified = false;
119  for (auto &MBB : MF)
120  Modified |= selectMBB(MBB);
121 
122  return Modified;
123 }
124 
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
int getTargetMemOpcode(uint16_t Opcode)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:385
STATISTIC(NumFunctions, "Total number of functions")
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
INITIALIZE_PASS(SystemZPostRewrite, "systemz-post-rewrite", SYSTEMZ_POSTREWRITE_NAME, false, false) FunctionPass *llvm
Returns an instance of the Post Rewrite pass.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:411
void setReg(Register Reg)
Change the register this operand corresponds to.
virtual const TargetInstrInfo * getInstrInfo() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
FunctionPass * createSystemZPostRewritePass(SystemZTargetMachine &TM)
void initializeSystemZPostRewritePass(PassRegistry &)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
MachineOperand class - Representation of each machine instruction operand.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Representation of each machine instruction.
Definition: MachineInstr.h:64
#define SYSTEMZ_POSTREWRITE_NAME
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
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.