LLVM  10.0.0
Legalizer.cpp
Go to the documentation of this file.
1 //===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===//
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 /// \file This file implements the LegalizerHelper class to legalize individual
10 /// instructions and the LegalizePass wrapper pass for the primary
11 /// legalization.
12 //
13 //===----------------------------------------------------------------------===//
14 
17 #include "llvm/ADT/SetVector.h"
29 #include "llvm/InitializePasses.h"
30 #include "llvm/Support/Debug.h"
32 
33 #include <iterator>
34 
35 #define DEBUG_TYPE "legalizer"
36 
37 using namespace llvm;
38 
39 static cl::opt<bool>
40  EnableCSEInLegalizer("enable-cse-in-legalizer",
41  cl::desc("Should enable CSE in Legalizer"),
42  cl::Optional, cl::init(false));
43 
44 char Legalizer::ID = 0;
46  "Legalize the Machine IR a function's Machine IR", false,
47  false)
51  "Legalize the Machine IR a function's Machine IR", false,
52  false)
53 
54 Legalizer::Legalizer() : MachineFunctionPass(ID) { }
55 
62 }
63 
64 void Legalizer::init(MachineFunction &MF) {
65 }
66 
67 static bool isArtifact(const MachineInstr &MI) {
68  switch (MI.getOpcode()) {
69  default:
70  return false;
71  case TargetOpcode::G_TRUNC:
72  case TargetOpcode::G_ZEXT:
73  case TargetOpcode::G_ANYEXT:
74  case TargetOpcode::G_SEXT:
75  case TargetOpcode::G_MERGE_VALUES:
76  case TargetOpcode::G_UNMERGE_VALUES:
77  case TargetOpcode::G_CONCAT_VECTORS:
78  case TargetOpcode::G_BUILD_VECTOR:
79  case TargetOpcode::G_EXTRACT:
80  return true;
81  }
82 }
85 
86 namespace {
87 class LegalizerWorkListManager : public GISelChangeObserver {
88  InstListTy &InstList;
89  ArtifactListTy &ArtifactList;
90 #ifndef NDEBUG
92 #endif
93 
94 public:
95  LegalizerWorkListManager(InstListTy &Insts, ArtifactListTy &Arts)
96  : InstList(Insts), ArtifactList(Arts) {}
97 
98  void createdOrChangedInstr(MachineInstr &MI) {
99  // Only legalize pre-isel generic instructions.
100  // Legalization process could generate Target specific pseudo
101  // instructions with generic types. Don't record them
102  if (isPreISelGenericOpcode(MI.getOpcode())) {
103  if (isArtifact(MI))
104  ArtifactList.insert(&MI);
105  else
106  InstList.insert(&MI);
107  }
108  }
109 
110  void createdInstr(MachineInstr &MI) override {
111  LLVM_DEBUG(dbgs() << ".. .. New MI: " << MI);
112  LLVM_DEBUG(NewMIs.push_back(&MI));
113  createdOrChangedInstr(MI);
114  }
115 
116  void printNewInstrs() {
117  LLVM_DEBUG({
118  for (const auto *MI : NewMIs)
119  dbgs() << ".. .. New MI: " << *MI;
120  NewMIs.clear();
121  });
122  }
123 
124  void erasingInstr(MachineInstr &MI) override {
125  LLVM_DEBUG(dbgs() << ".. .. Erasing: " << MI);
126  InstList.remove(&MI);
127  ArtifactList.remove(&MI);
128  }
129 
130  void changingInstr(MachineInstr &MI) override {
131  LLVM_DEBUG(dbgs() << ".. .. Changing MI: " << MI);
132  }
133 
134  void changedInstr(MachineInstr &MI) override {
135  // When insts change, we want to revisit them to legalize them again.
136  // We'll consider them the same as created.
137  LLVM_DEBUG(dbgs() << ".. .. Changed MI: " << MI);
138  createdOrChangedInstr(MI);
139  }
140 };
141 } // namespace
142 
145  ArrayRef<GISelChangeObserver *> AuxObservers,
146  MachineIRBuilder &MIRBuilder) {
148 
149  // Populate worklists.
150  InstListTy InstList;
151  ArtifactListTy ArtifactList;
153  // Perform legalization bottom up so we can DCE as we legalize.
154  // Traverse BB in RPOT and within each basic block, add insts top down,
155  // so when we pop_back_val in the legalization process, we traverse bottom-up.
156  for (auto *MBB : RPOT) {
157  if (MBB->empty())
158  continue;
159  for (MachineInstr &MI : *MBB) {
160  // Only legalize pre-isel generic instructions: others don't have types
161  // and are assumed to be legal.
162  if (!isPreISelGenericOpcode(MI.getOpcode()))
163  continue;
164  if (isArtifact(MI))
165  ArtifactList.deferred_insert(&MI);
166  else
167  InstList.deferred_insert(&MI);
168  }
169  }
170  ArtifactList.finalize();
171  InstList.finalize();
172 
173  // This observer keeps the worklists updated.
174  LegalizerWorkListManager WorkListObserver(InstList, ArtifactList);
175  // We want both WorkListObserver as well as all the auxiliary observers (e.g.
176  // CSEInfo) to observe all changes. Use the wrapper observer.
177  GISelObserverWrapper WrapperObserver(&WorkListObserver);
178  for (GISelChangeObserver *Observer : AuxObservers)
179  WrapperObserver.addObserver(Observer);
180 
181  // Now install the observer as the delegate to MF.
182  // This will keep all the observers notified about new insertions/deletions.
183  RAIIDelegateInstaller DelInstall(MF, &WrapperObserver);
184  LegalizerHelper Helper(MF, LI, WrapperObserver, MIRBuilder);
185  LegalizationArtifactCombiner ArtCombiner(MIRBuilder, MRI, LI);
186  auto RemoveDeadInstFromLists = [&WrapperObserver](MachineInstr *DeadMI) {
187  WrapperObserver.erasingInstr(*DeadMI);
188  };
189  bool Changed = false;
191  do {
192  LLVM_DEBUG(dbgs() << "=== New Iteration ===\n");
193  assert(RetryList.empty() && "Expected no instructions in RetryList");
194  unsigned NumArtifacts = ArtifactList.size();
195  while (!InstList.empty()) {
196  MachineInstr &MI = *InstList.pop_back_val();
198  "Expecting generic opcode");
199  if (isTriviallyDead(MI, MRI)) {
200  LLVM_DEBUG(dbgs() << MI << "Is dead; erasing.\n");
202  continue;
203  }
204 
205  // Do the legalization for this instruction.
206  auto Res = Helper.legalizeInstrStep(MI);
207  // Error out if we couldn't legalize this instruction. We may want to
208  // fall back to DAG ISel instead in the future.
210  // Move illegal artifacts to RetryList instead of aborting because
211  // legalizing InstList may generate artifacts that allow
212  // ArtifactCombiner to combine away them.
213  if (isArtifact(MI)) {
214  LLVM_DEBUG(dbgs() << ".. Not legalized, moving to artifacts retry\n");
215  assert(NumArtifacts == 0 &&
216  "Artifacts are only expected in instruction list starting the "
217  "second iteration, but each iteration starting second must "
218  "start with an empty artifacts list");
219  (void)NumArtifacts;
220  RetryList.push_back(&MI);
221  continue;
222  }
224  return {Changed, &MI};
225  }
226  WorkListObserver.printNewInstrs();
227  Changed |= Res == LegalizerHelper::Legalized;
228  }
229  // Try to combine the instructions in RetryList again if there
230  // are new artifacts. If not, stop legalizing.
231  if (!RetryList.empty()) {
232  if (!ArtifactList.empty()) {
233  while (!RetryList.empty())
234  ArtifactList.insert(RetryList.pop_back_val());
235  } else {
236  LLVM_DEBUG(dbgs() << "No new artifacts created, not retrying!\n");
238  return {Changed, RetryList.front()};
239  }
240  }
241  while (!ArtifactList.empty()) {
242  MachineInstr &MI = *ArtifactList.pop_back_val();
244  "Expecting generic opcode");
245  if (isTriviallyDead(MI, MRI)) {
246  LLVM_DEBUG(dbgs() << MI << "Is dead\n");
247  RemoveDeadInstFromLists(&MI);
249  continue;
250  }
251  SmallVector<MachineInstr *, 4> DeadInstructions;
252  LLVM_DEBUG(dbgs() << "Trying to combine: " << MI);
253  if (ArtCombiner.tryCombineInstruction(MI, DeadInstructions,
254  WrapperObserver)) {
255  WorkListObserver.printNewInstrs();
256  for (auto *DeadMI : DeadInstructions) {
257  LLVM_DEBUG(dbgs() << *DeadMI << "Is dead\n");
258  RemoveDeadInstFromLists(DeadMI);
259  DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
260  }
261  Changed = true;
262  continue;
263  }
264  // If this was not an artifact (that could be combined away), this might
265  // need special handling. Add it to InstList, so when it's processed
266  // there, it has to be legal or specially handled.
267  else {
268  LLVM_DEBUG(dbgs() << ".. Not combined, moving to instructions list\n");
269  InstList.insert(&MI);
270  }
271  }
272  } while (!InstList.empty());
273 
274  return {Changed, /*FailedOn*/ nullptr};
275 }
276 
278  // If the ISel pipeline failed, do not bother running that pass.
279  if (MF.getProperties().hasProperty(
281  return false;
282  LLVM_DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
283  init(MF);
284  const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
286  getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
287  MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
288 
289  const size_t NumBlocks = MF.size();
290 
291  std::unique_ptr<MachineIRBuilder> MIRBuilder;
292  GISelCSEInfo *CSEInfo = nullptr;
293  bool EnableCSE = EnableCSEInLegalizer.getNumOccurrences()
295  : TPC.isGISelCSEEnabled();
296  if (EnableCSE) {
297  MIRBuilder = std::make_unique<CSEMIRBuilder>();
298  CSEInfo = &Wrapper.get(TPC.getCSEConfig());
299  MIRBuilder->setCSEInfo(CSEInfo);
300  } else
301  MIRBuilder = std::make_unique<MachineIRBuilder>();
302 
304  if (EnableCSE && CSEInfo) {
305  // We want CSEInfo in addition to WorkListObserver to observe all changes.
306  AuxObservers.push_back(CSEInfo);
307  }
308 
309  const LegalizerInfo &LI = *MF.getSubtarget().getLegalizerInfo();
310  MFResult Result = legalizeMachineFunction(MF, LI, AuxObservers, *MIRBuilder);
311 
312  if (Result.FailedOn) {
313  reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
314  "unable to legalize instruction", *Result.FailedOn);
315  return false;
316  }
317  // For now don't support if new blocks are inserted - we would need to fix the
318  // outer loop for that.
319  if (MF.size() != NumBlocks) {
320  MachineOptimizationRemarkMissed R("gisel-legalize", "GISelFailure",
321  MF.getFunction().getSubprogram(),
322  /*MBB=*/nullptr);
323  R << "inserting blocks is not supported yet";
324  reportGISelFailure(MF, TPC, MORE, R);
325  return false;
326  }
327  return Result.Changed;
328 }
A simple RAII based CSEInfo installer.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
The CSE Analysis object.
Definition: CSEInfo.h:71
void deferred_insert(MachineInstr *I)
Definition: GISelWorkList.h:54
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition: Utils.cpp:431
const MachineFunctionProperties & getProperties() const
Get the function properties.
unsigned size() const
void push_back(const T &Elt)
Definition: SmallVector.h:211
bool empty() const
Definition: GISelWorkList.h:42
const MachineInstr * FailedOn
Definition: Legalizer.h:36
virtual bool isGISelCSEEnabled() const
Check whether continuous CSE should be enabled in GISel passes.
The actual analysis pass wrapper.
Definition: CSEInfo.h:218
INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE, "Legalize the Machine IR a function's Machine IR", false, false) INITIALIZE_PASS_END(Legalizer
void remove(const MachineInstr *I)
Remove I from the worklist if it exists.
Definition: GISelWorkList.h:85
virtual std::unique_ptr< CSEConfigBase > getCSEConfig() const
Returns the CSEConfig object to use for the current optimization level.
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
bool tryCombineInstruction(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, GISelObserverWrapper &WrapperObserver)
Try to combine away MI.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
MachineIRBuilder & MIRBuilder
Expose MIRBuilder so clients can set their own RecordInsertInstruction functions. ...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:426
Target-Independent Code Generator Pass Configuration Options.
void eraseFromParentAndMarkDBGValuesForRemoval()
Unlink &#39;this&#39; from the containing basic block and delete it.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
static MFResult legalizeMachineFunction(MachineFunction &MF, const LegalizerInfo &LI, ArrayRef< GISelChangeObserver *> AuxObservers, MachineIRBuilder &MIRBuilder)
Definition: Legalizer.cpp:144
virtual const LegalizerInfo * getLegalizerInfo() const
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
Abstract class that contains various methods for clients to notify about changes. ...
unsigned const MachineRegisterInfo * MRI
LegalizeResult legalizeInstrStep(MachineInstr &MI)
Replace MI by a sequence of legal instructions that can implement the same operation.
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.
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1525
This file implements a version of MachineIRBuilder which CSEs insts within a MachineBasicBlock.
Helper class to build MachineInstr.
Represent the analysis usage information of a pass.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: Legalizer.cpp:277
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:50
Simple wrapper that does the following.
Definition: CSEInfo.h:200
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
unsigned size() const
Definition: GISelWorkList.h:44
The optimization diagnostic interface.
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
#define MORE()
Definition: regcomp.c:252
MachineInstr * pop_back_val()
void insert(MachineInstr *I)
Add the specified instruction to the worklist if it isn&#39;t already in it.
Definition: GISelWorkList.h:78
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static char ID
Definition: Legalizer.h:32
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Check whether an instruction MI is dead: it only defines dead virtual registers, and doesn&#39;t have oth...
Definition: Utils.cpp:158
static cl::opt< bool > EnableCSEInLegalizer("enable-cse-in-legalizer", cl::desc("Should enable CSE in Legalizer"), cl::Optional, cl::init(false))
Representation of each machine instruction.
Definition: MachineInstr.h:63
Instruction has been legalized and the MachineFunction changed.
void addObserver(GISelChangeObserver *O)
#define DEBUG_TYPE
Definition: Legalizer.cpp:35
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
static bool isArtifact(const MachineInstr &MI)
Definition: Legalizer.cpp:67
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Legalizer.cpp:56
Diagnostic information for missed-optimization remarks.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasProperty(Property P) const
bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel...
Definition: TargetOpcodes.h:30
print Print MemDeps of function
IRTranslator LLVM IR MI
Simple wrapper observer that takes several observers, and calls each one for each event...
#define LLVM_DEBUG(X)
Definition: Debug.h:122
void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel error as a missed optimization remark to the LLVMContext&#39;s diagnostic stream...
Definition: Utils.cpp:178