blob: 33d050bbcc6ab21ea85deeba085be50a39b6ab59 [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"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/CodeGen/GlobalISel/MachineLegalizeHelper.h"
19#include "llvm/CodeGen/GlobalISel/MachineLegalizer.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Target/TargetSubtargetInfo.h"
22
23#define DEBUG_TYPE "legalize-mir"
24
25using namespace llvm;
26
27char MachineLegalizePass::ID = 0;
28INITIALIZE_PASS(MachineLegalizePass, DEBUG_TYPE,
29 "Legalize the Machine IR a function's Machine IR", false,
Tim Northover884b47e2016-07-26 03:29:18 +000030 false)
Tim Northover33b07d62016-07-22 20:03:43 +000031
32MachineLegalizePass::MachineLegalizePass() : MachineFunctionPass(ID) {
33 initializeMachineLegalizePassPass(*PassRegistry::getPassRegistry());
34}
35
36void MachineLegalizePass::init(MachineFunction &MF) {
37}
38
39bool MachineLegalizePass::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet60495242016-08-27 00:18:24 +000040 // If the ISel pipeline failed, do not bother running that pass.
41 if (MF.getProperties().hasProperty(
42 MachineFunctionProperties::Property::FailedISel))
43 return false;
Tim Northover33b07d62016-07-22 20:03:43 +000044 DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
45 init(MF);
46 const MachineLegalizer &Legalizer = *MF.getSubtarget().getMachineLegalizer();
47 MachineLegalizeHelper Helper(MF);
48
49 // FIXME: an instruction may need more than one pass before it is legal. For
50 // example on most architectures <3 x i3> is doubly-illegal. It would
51 // typically proceed along a path like: <3 x i3> -> <3 x i8> -> <8 x i8>. We
52 // probably want a worklist of instructions rather than naive iterate until
53 // convergence for performance reasons.
54 bool Changed = false;
55 MachineBasicBlock::iterator NextMI;
56 for (auto &MBB : MF)
57 for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) {
58 // Get the next Instruction before we try to legalize, because there's a
59 // good chance MI will be deleted.
60 NextMI = std::next(MI);
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +000061
62 // Only legalize pre-isel generic instructions: others don't have types
63 // and are assumed to be legal.
64 if (!isPreISelGenericOpcode(MI->getOpcode()))
65 continue;
66
Tim Northover33b07d62016-07-22 20:03:43 +000067 auto Res = Helper.legalizeInstr(*MI, Legalizer);
68
69 // Error out if we couldn't legalize this instruction. We may want to fall
70 // back to DAG ISel instead in the future.
71 if (Res == MachineLegalizeHelper::UnableToLegalize) {
72 std::string Msg;
73 raw_string_ostream OS(Msg);
74 OS << "unable to legalize instruction: ";
75 MI->print(OS);
76 report_fatal_error(OS.str());
77 }
78
79 Changed |= Res == MachineLegalizeHelper::Legalized;
80 }
81 return Changed;
82}