LLVM  5.0.0
Legalizer.cpp
Go to the documentation of this file.
1 //===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===//
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 /// \file This file implements the LegalizerHelper class to legalize individual
11 /// instructions and the LegalizePass wrapper pass for the primary
12 /// legalization.
13 //
14 //===----------------------------------------------------------------------===//
15 
22 #include "llvm/Support/Debug.h"
25 
26 #include <iterator>
27 
28 #define DEBUG_TYPE "legalizer"
29 
30 using namespace llvm;
31 
32 char Legalizer::ID = 0;
34  "Legalize the Machine IR a function's Machine IR", false,
35  false)
38  "Legalize the Machine IR a function's Machine IR", false,
39  false)
40 
41 Legalizer::Legalizer() : MachineFunctionPass(ID) {
43 }
44 
48 }
49 
50 void Legalizer::init(MachineFunction &MF) {
51 }
52 
54  const TargetInstrInfo &TII,
55  MachineIRBuilder &MIRBuilder) {
56  if (MI.getOpcode() != TargetOpcode::G_UNMERGE_VALUES)
57  return false;
58 
59  unsigned NumDefs = MI.getNumOperands() - 1;
60  unsigned SrcReg = MI.getOperand(NumDefs).getReg();
61  MachineInstr &MergeI = *MRI.def_instr_begin(SrcReg);
62  if (MergeI.getOpcode() != TargetOpcode::G_MERGE_VALUES)
63  return false;
64 
65  const unsigned NumMergeRegs = MergeI.getNumOperands() - 1;
66 
67  if (NumMergeRegs < NumDefs) {
68  if (NumDefs % NumMergeRegs != 0)
69  return false;
70 
71  MIRBuilder.setInstr(MI);
72  // Transform to UNMERGEs, for example
73  // %1 = G_MERGE_VALUES %4, %5
74  // %9, %10, %11, %12 = G_UNMERGE_VALUES %1
75  // to
76  // %9, %10 = G_UNMERGE_VALUES %4
77  // %11, %12 = G_UNMERGE_VALUES %5
78 
79  const unsigned NewNumDefs = NumDefs / NumMergeRegs;
80  for (unsigned Idx = 0; Idx < NumMergeRegs; ++Idx) {
82  for (unsigned j = 0, DefIdx = Idx * NewNumDefs; j < NewNumDefs;
83  ++j, ++DefIdx)
84  DstRegs.push_back(MI.getOperand(DefIdx).getReg());
85 
86  MIRBuilder.buildUnmerge(DstRegs, MergeI.getOperand(Idx + 1).getReg());
87  }
88 
89  } else if (NumMergeRegs > NumDefs) {
90  if (NumMergeRegs % NumDefs != 0)
91  return false;
92 
93  MIRBuilder.setInstr(MI);
94  // Transform to MERGEs
95  // %6 = G_MERGE_VALUES %17, %18, %19, %20
96  // %7, %8 = G_UNMERGE_VALUES %6
97  // to
98  // %7 = G_MERGE_VALUES %17, %18
99  // %8 = G_MERGE_VALUES %19, %20
100 
101  const unsigned NumRegs = NumMergeRegs / NumDefs;
102  for (unsigned DefIdx = 0; DefIdx < NumDefs; ++DefIdx) {
104  for (unsigned j = 0, Idx = NumRegs * DefIdx + 1; j < NumRegs; ++j, ++Idx)
105  Regs.push_back(MergeI.getOperand(Idx).getReg());
106 
107  MIRBuilder.buildMerge(MI.getOperand(DefIdx).getReg(), Regs);
108  }
109 
110  } else {
111  // FIXME: is a COPY appropriate if the types mismatch? We know both
112  // registers are allocatable by now.
113  if (MRI.getType(MI.getOperand(0).getReg()) !=
114  MRI.getType(MergeI.getOperand(1).getReg()))
115  return false;
116 
117  for (unsigned Idx = 0; Idx < NumDefs; ++Idx)
118  MRI.replaceRegWith(MI.getOperand(Idx).getReg(),
119  MergeI.getOperand(Idx + 1).getReg());
120  }
121 
122  MI.eraseFromParent();
123  if (MRI.use_empty(MergeI.getOperand(0).getReg()))
124  MergeI.eraseFromParent();
125  return true;
126 }
127 
129  // If the ISel pipeline failed, do not bother running that pass.
130  if (MF.getProperties().hasProperty(
132  return false;
133  DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
134  init(MF);
135  const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
136  MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
137  LegalizerHelper Helper(MF);
138 
139  // FIXME: an instruction may need more than one pass before it is legal. For
140  // example on most architectures <3 x i3> is doubly-illegal. It would
141  // typically proceed along a path like: <3 x i3> -> <3 x i8> -> <8 x i8>. We
142  // probably want a worklist of instructions rather than naive iterate until
143  // convergence for performance reasons.
144  bool Changed = false;
146  for (auto &MBB : MF) {
147  for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
148  // Get the next Instruction before we try to legalize, because there's a
149  // good chance MI will be deleted.
150  NextMI = std::next(MI);
151 
152  // Only legalize pre-isel generic instructions: others don't have types
153  // and are assumed to be legal.
154  if (!isPreISelGenericOpcode(MI->getOpcode()))
155  continue;
156  unsigned NumNewInsns = 0;
159  // Only legalize pre-isel generic instructions.
160  // Legalization process could generate Target specific pseudo
161  // instructions with generic types. Don't record them
162  if (isPreISelGenericOpcode(MI->getOpcode())) {
163  ++NumNewInsns;
164  WorkList.push_back(MI);
165  }
166  });
167  WorkList.push_back(&*MI);
168 
169  bool Changed = false;
171  unsigned Idx = 0;
172  do {
173  Res = Helper.legalizeInstrStep(*WorkList[Idx]);
174  // Error out if we couldn't legalize this instruction. We may want to
175  // fall back to DAG ISel instead in the future.
179  reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
180  "unable to legalize instruction",
181  *WorkList[Idx]);
182  return false;
183  }
184  }
185  Changed |= Res == LegalizerHelper::Legalized;
186  ++Idx;
187 
188 #ifndef NDEBUG
189  if (NumNewInsns) {
190  DEBUG(dbgs() << ".. .. Emitted " << NumNewInsns << " insns\n");
191  for (auto I = WorkList.end() - NumNewInsns, E = WorkList.end();
192  I != E; ++I)
193  DEBUG(dbgs() << ".. .. New MI: "; (*I)->print(dbgs()));
194  NumNewInsns = 0;
195  }
196 #endif
197  } while (Idx < WorkList.size());
198 
200  }
201  }
202 
203  MachineRegisterInfo &MRI = MF.getRegInfo();
204  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
205  for (auto &MBB : MF) {
206  for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
207  // Get the next Instruction before we try to legalize, because there's a
208  // good chance MI will be deleted.
209  NextMI = std::next(MI);
210  Changed |= combineMerges(*MI, MRI, TII, Helper.MIRBuilder);
211  }
212  }
213 
214  return Changed;
215 }
void push_back(const T &Elt)
Definition: SmallVector.h:211
MachineIRBuilder MIRBuilder
Expose MIRBuilder so clients can set their own RecordInsertInstruction functions. ...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLT getType(unsigned VReg) const
Get the low-level type of VReg or LLT{} if VReg is not a generic (target independent) virtual registe...
bool hasProperty(Property P) const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Legalize the Machine IR a function s Machine false
Definition: Legalizer.cpp:37
const MachineFunctionProperties & getProperties() const
Get the function properties.
Target-Independent Code Generator Pass Configuration Options.
MachineTypes Machine
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:282
INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,"Legalize the Machine IR a function's Machine IR", false, false) INITIALIZE_PASS_END(Legalizer
const RegList & Regs
void recordInsertions(std::function< void(MachineInstr *)> InsertedInstr)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
TargetInstrInfo - Interface to description of machine instruction set.
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
TracePC TPC
unsigned const MachineRegisterInfo * MRI
LegalizeResult legalizeInstrStep(MachineInstr &MI)
Replace MI by a sequence of legal instructions that can implement the same operation.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Helper class to build MachineInstr.
Represent the analysis usage information of a pass.
void setInstr(MachineInstr &MI)
Set the insertion point to before MI.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: Legalizer.cpp:128
Some kind of error has occurred and we could not legalize this instruction.
Legalize the Machine IR a function s Machine IR
Definition: Legalizer.cpp:37
#define E
Definition: LargeTest.cpp:27
The optimization diagnostic interface.
#define MORE()
Definition: regcomp.c:124
bool combineMerges(MachineInstr &MI, MachineRegisterInfo &MRI, const TargetInstrInfo &TII, MachineIRBuilder &MIRBuilder)
Definition: Legalizer.cpp:53
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel...
Definition: TargetOpcodes.h:31
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
static char ID
Definition: Legalizer.h:33
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:59
Instruction has been legalized and the MachineFunction changed.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
#define DEBUG_TYPE
Definition: Legalizer.cpp:28
def_instr_iterator def_instr_begin(unsigned RegNo) const
void initializeLegalizerPass(PassRegistry &)
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Legalizer.cpp:45
MachineInstrBuilder buildUnmerge(ArrayRef< unsigned > Res, unsigned Op)
Build and insert Res0<def>, ...
unsigned getReg() const
getReg - Returns the register number.
#define DEBUG(X)
Definition: Debug.h:118
print Print MemDeps of function
IRTranslator LLVM IR MI
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineInstrBuilder buildMerge(unsigned Res, ArrayRef< unsigned > Ops)
Build and insert Res<def> = G_MERGE_VALUES Op0, ...
void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel error as a missed optimization remark to the LLVMContext's diagnostic stream...
Definition: Utils.cpp:80
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.