blob: ba56eb09db1246f061d6c9b2343be4a8abe5b86e [file] [log] [blame]
Tim Northover33b07d62016-07-22 20:03:43 +00001//===-- llvm/CodeGen/GlobalISel/MachineLegalizePass.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 LegalizeHelper class to legalize individual
11/// instructions and the MachineLegalizePass wrapper pass for the primary
12/// legalization.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/CodeGen/GlobalISel/MachineLegalizePass.h"
Tim Northover33b07d62016-07-22 20:03:43 +000017#include "llvm/CodeGen/GlobalISel/MachineLegalizeHelper.h"
18#include "llvm/CodeGen/GlobalISel/MachineLegalizer.h"
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/CodeGen/TargetPassConfig.h"
Tim Northover33b07d62016-07-22 20:03:43 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Target/TargetSubtargetInfo.h"
23
24#define DEBUG_TYPE "legalize-mir"
25
26using namespace llvm;
27
28char MachineLegalizePass::ID = 0;
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000029INITIALIZE_PASS_BEGIN(MachineLegalizePass, DEBUG_TYPE,
30 "Legalize the Machine IR a function's Machine IR", false,
31 false)
32INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
33INITIALIZE_PASS_END(MachineLegalizePass, DEBUG_TYPE,
34 "Legalize the Machine IR a function's Machine IR", false,
35 false)
Tim Northover33b07d62016-07-22 20:03:43 +000036
37MachineLegalizePass::MachineLegalizePass() : MachineFunctionPass(ID) {
38 initializeMachineLegalizePassPass(*PassRegistry::getPassRegistry());
39}
40
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000041void MachineLegalizePass::getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<TargetPassConfig>();
43 MachineFunctionPass::getAnalysisUsage(AU);
44}
45
Tim Northover33b07d62016-07-22 20:03:43 +000046void MachineLegalizePass::init(MachineFunction &MF) {
47}
48
49bool MachineLegalizePass::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet60495242016-08-27 00:18:24 +000050 // If the ISel pipeline failed, do not bother running that pass.
51 if (MF.getProperties().hasProperty(
52 MachineFunctionProperties::Property::FailedISel))
53 return false;
Tim Northover33b07d62016-07-22 20:03:43 +000054 DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
55 init(MF);
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000056 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
Tim Northover33b07d62016-07-22 20:03:43 +000057 const MachineLegalizer &Legalizer = *MF.getSubtarget().getMachineLegalizer();
58 MachineLegalizeHelper Helper(MF);
59
60 // FIXME: an instruction may need more than one pass before it is legal. For
61 // example on most architectures <3 x i3> is doubly-illegal. It would
62 // typically proceed along a path like: <3 x i3> -> <3 x i8> -> <8 x i8>. We
63 // probably want a worklist of instructions rather than naive iterate until
64 // convergence for performance reasons.
65 bool Changed = false;
66 MachineBasicBlock::iterator NextMI;
67 for (auto &MBB : MF)
68 for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
69 // Get the next Instruction before we try to legalize, because there's a
70 // good chance MI will be deleted.
71 NextMI = std::next(MI);
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +000072
73 // Only legalize pre-isel generic instructions: others don't have types
74 // and are assumed to be legal.
75 if (!isPreISelGenericOpcode(MI->getOpcode()))
76 continue;
77
Tim Northover33b07d62016-07-22 20:03:43 +000078 auto Res = Helper.legalizeInstr(*MI, Legalizer);
79
80 // Error out if we couldn't legalize this instruction. We may want to fall
81 // back to DAG ISel instead in the future.
82 if (Res == MachineLegalizeHelper::UnableToLegalize) {
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000083 if (!TPC.isGlobalISelAbortEnabled()) {
84 MF.getProperties().set(
85 MachineFunctionProperties::Property::FailedISel);
86 return false;
87 }
Tim Northover33b07d62016-07-22 20:03:43 +000088 std::string Msg;
89 raw_string_ostream OS(Msg);
90 OS << "unable to legalize instruction: ";
91 MI->print(OS);
92 report_fatal_error(OS.str());
93 }
94
95 Changed |= Res == MachineLegalizeHelper::Legalized;
96 }
97 return Changed;
98}