LLVM  5.0.0
InstructionSelect.cpp
Go to the documentation of this file.
1 //===- llvm/CodeGen/GlobalISel/InstructionSelect.cpp - InstructionSelect ---==//
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 /// \file
10 /// This file implements the InstructionSelect class.
11 //===----------------------------------------------------------------------===//
12 
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/ADT/Twine.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
26 #include "llvm/Support/Debug.h"
29 
30 #define DEBUG_TYPE "instruction-select"
31 
32 using namespace llvm;
33 
34 char InstructionSelect::ID = 0;
36  "Select target instructions out of generic instructions",
37  false, false)
40  "Select target instructions out of generic instructions",
41  false, false)
42 
43 InstructionSelect::InstructionSelect() : MachineFunctionPass(ID) {
45 }
46 
50 }
51 
53  const MachineRegisterInfo &MRI = MF.getRegInfo();
54 
55  // No matter what happens, whether we successfully select the function or not,
56  // nothing is going to use the vreg types after us. Make sure they disappear.
57  auto ClearVRegTypesOnReturn =
58  make_scope_exit([&]() { MRI.getVRegToType().clear(); });
59 
60  // If the ISel pipeline failed, do not bother running that pass.
61  if (MF.getProperties().hasProperty(
63  return false;
64 
65  DEBUG(dbgs() << "Selecting function: " << MF.getName() << '\n');
66 
67  const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
69  assert(ISel && "Cannot work without InstructionSelector");
70 
71  // An optimization remark emitter. Used to report failures.
72  MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
73 
74  // FIXME: There are many other MF/MFI fields we need to initialize.
75 
76 #ifndef NDEBUG
77  // Check that our input is fully legal: we require the function to have the
78  // Legalized property, so it should be.
79  // FIXME: This should be in the MachineVerifier, but it can't use the
80  // LegalizerInfo as it's currently in the separate GlobalISel library.
81  // The RegBankSelected property is already checked in the verifier. Note
82  // that it has the same layering problem, but we only use inline methods so
83  // end up not needing to link against the GlobalISel library.
84  if (const LegalizerInfo *MLI = MF.getSubtarget().getLegalizerInfo())
85  for (MachineBasicBlock &MBB : MF)
86  for (MachineInstr &MI : MBB)
87  if (isPreISelGenericOpcode(MI.getOpcode()) && !MLI->isLegal(MI, MRI)) {
88  reportGISelFailure(MF, TPC, MORE, "gisel-select",
89  "instruction is not legal", MI);
90  return false;
91  }
92 
93 #endif
94  // FIXME: We could introduce new blocks and will need to fix the outer loop.
95  // Until then, keep track of the number of blocks to assert that we don't.
96  const size_t NumBlocks = MF.size();
97 
98  for (MachineBasicBlock *MBB : post_order(&MF)) {
99  if (MBB->empty())
100  continue;
101 
102  // Select instructions in reverse block order. We permit erasing so have
103  // to resort to manually iterating and recognizing the begin (rend) case.
104  bool ReachedBegin = false;
105  for (auto MII = std::prev(MBB->end()), Begin = MBB->begin();
106  !ReachedBegin;) {
107 #ifndef NDEBUG
108  // Keep track of the insertion range for debug printing.
109  const auto AfterIt = std::next(MII);
110 #endif
111  // Select this instruction.
112  MachineInstr &MI = *MII;
113 
114  // And have our iterator point to the next instruction, if there is one.
115  if (MII == Begin)
116  ReachedBegin = true;
117  else
118  --MII;
119 
120  DEBUG(dbgs() << "Selecting: \n " << MI);
121 
122  // We could have folded this instruction away already, making it dead.
123  // If so, erase it.
124  if (isTriviallyDead(MI, MRI)) {
125  DEBUG(dbgs() << "Is dead; erasing.\n");
127  continue;
128  }
129 
130  if (!ISel->select(MI)) {
131  // FIXME: It would be nice to dump all inserted instructions. It's
132  // not obvious how, esp. considering select() can insert after MI.
133  reportGISelFailure(MF, TPC, MORE, "gisel-select", "cannot select", MI);
134  return false;
135  }
136 
137  // Dump the range of instructions that MI expanded into.
138  DEBUG({
139  auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII);
140  dbgs() << "Into:\n";
141  for (auto &InsertedMI : make_range(InsertedBegin, AfterIt))
142  dbgs() << " " << InsertedMI;
143  dbgs() << '\n';
144  });
145  }
146  }
147 
148  const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
149 
150  // Now that selection is complete, there are no more generic vregs. Verify
151  // that the size of the now-constrained vreg is unchanged and that it has a
152  // register class.
153  for (auto &VRegToType : MRI.getVRegToType()) {
154  unsigned VReg = VRegToType.first;
155  auto *RC = MRI.getRegClassOrNull(VReg);
156  MachineInstr *MI = nullptr;
157  if (!MRI.def_empty(VReg))
158  MI = &*MRI.def_instr_begin(VReg);
159  else if (!MRI.use_empty(VReg))
160  MI = &*MRI.use_instr_begin(VReg);
161 
162  if (MI && !RC) {
163  reportGISelFailure(MF, TPC, MORE, "gisel-select",
164  "VReg has no regclass after selection", *MI);
165  return false;
166  } else if (!RC)
167  continue;
168 
169  if (VRegToType.second.isValid() &&
170  VRegToType.second.getSizeInBits() > TRI.getRegSizeInBits(*RC)) {
171  reportGISelFailure(MF, TPC, MORE, "gisel-select",
172  "VReg has explicit size different from class size",
173  *MI);
174  return false;
175  }
176  }
177 
178  if (MF.size() != NumBlocks) {
179  MachineOptimizationRemarkMissed R("gisel-select", "GISelFailure",
180  MF.getFunction()->getSubprogram(),
181  /*MBB=*/nullptr);
182  R << "inserting blocks is not supported yet";
183  reportGISelFailure(MF, TPC, MORE, R);
184  return false;
185  }
186 
187  auto &TLI = *MF.getSubtarget().getTargetLowering();
188  TLI.finalizeLowering(MF);
189 
190  // FIXME: Should we accurately track changes?
191  return true;
192 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool hasProperty(Property P) const
void initializeInstructionSelectPass(PassRegistry &)
unsigned getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:47
VRegToTypeMap & getVRegToType() const
Accessor for VRegToType.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
use_instr_iterator use_instr_begin(unsigned RegNo) const
const MachineFunctionProperties & getProperties() const
Get the function properties.
Target-Independent Code Generator Pass Configuration Options.
void eraseFromParentAndMarkDBGValuesForRemoval()
Unlink 'this' from the containing basic block and delete it.
virtual const InstructionSelector * getInstructionSelector() const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
TracePC TPC
unsigned const MachineRegisterInfo * MRI
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
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...
iterator_range< po_iterator< T > > post_order(const T &G)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
#define DEBUG_TYPE
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
The optimization diagnostic interface.
#define MORE()
Definition: regcomp.c:124
This pass is responsible for selecting generic machine instructions to target-specific instructions...
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
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides the logic to select generic machine instructions.
bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Check whether an instruction MI is dead: it only defines dead virtual registers, and doesn't have oth...
Definition: Utils.cpp:59
Representation of each machine instruction.
Definition: MachineInstr.h:59
def_instr_iterator def_instr_begin(unsigned RegNo) const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
INITIALIZE_PASS_BEGIN(InstructionSelect, DEBUG_TYPE,"Select target instructions out of generic instructions", false, false) INITIALIZE_PASS_END(InstructionSelect
Select target instructions out of generic false
Diagnostic information for missed-optimization remarks.
const TargetRegisterClass * getRegClassOrNull(unsigned Reg) const
Return the register class of Reg, or null if Reg has not been assigned a register class yet...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const LegalizerInfo * getLegalizerInfo() const
bool def_empty(unsigned RegNo) const
def_empty - Return true if there are no instructions defining the specified register (it may be live-...
#define DEBUG(X)
Definition: Debug.h:118
IRTranslator LLVM IR MI
inst_range instructions(Function *F)
Definition: InstIterator.h:134
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
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
This file describes how to lower LLVM code to machine code.
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.