blob: f7bbf610fc981ef0e34cdd1d7faa614ac45addbf [file] [log] [blame]
Tim Northover69fa84a2016-10-14 22:18:18 +00001//===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===//
Tim Northover33b07d62016-07-22 20:03:43 +00002//
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//
Tim Northover69fa84a2016-10-14 22:18:18 +000010/// \file This file implements the LegalizerHelper class to legalize individual
11/// instructions and the LegalizePass wrapper pass for the primary
Tim Northover33b07d62016-07-22 20:03:43 +000012/// legalization.
13//
14//===----------------------------------------------------------------------===//
15
Tim Northover69fa84a2016-10-14 22:18:18 +000016#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Aditya Nandakumare6201c82017-11-14 22:42:19 +000017#include "llvm/ADT/PostOrderIterator.h"
Aditya Nandakumarc6615f52017-08-30 19:32:59 +000018#include "llvm/ADT/SetVector.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000019#include "llvm/CodeGen/GlobalISel/GISelWorkList.h"
Aditya Nandakumare6201c82017-11-14 22:42:19 +000020#include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h"
Tim Northover69fa84a2016-10-14 22:18:18 +000021#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000022#include "llvm/CodeGen/GlobalISel/Utils.h"
23#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000025#include "llvm/CodeGen/TargetInstrInfo.h"
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000026#include "llvm/CodeGen/TargetPassConfig.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000027#include "llvm/CodeGen/TargetSubtargetInfo.h"
Tim Northover33b07d62016-07-22 20:03:43 +000028#include "llvm/Support/Debug.h"
Tim Northover33b07d62016-07-22 20:03:43 +000029
Daniel Sanders5377fb32017-04-20 15:46:12 +000030#include <iterator>
31
Tim Northover69fa84a2016-10-14 22:18:18 +000032#define DEBUG_TYPE "legalizer"
Tim Northover33b07d62016-07-22 20:03:43 +000033
34using namespace llvm;
35
Tim Northover69fa84a2016-10-14 22:18:18 +000036char Legalizer::ID = 0;
37INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE,
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000038 "Legalize the Machine IR a function's Machine IR", false,
39 false)
40INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
Tim Northover69fa84a2016-10-14 22:18:18 +000041INITIALIZE_PASS_END(Legalizer, DEBUG_TYPE,
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000042 "Legalize the Machine IR a function's Machine IR", false,
43 false)
Tim Northover33b07d62016-07-22 20:03:43 +000044
Tim Northover69fa84a2016-10-14 22:18:18 +000045Legalizer::Legalizer() : MachineFunctionPass(ID) {
46 initializeLegalizerPass(*PassRegistry::getPassRegistry());
Tim Northover33b07d62016-07-22 20:03:43 +000047}
48
Tim Northover69fa84a2016-10-14 22:18:18 +000049void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const {
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000050 AU.addRequired<TargetPassConfig>();
51 MachineFunctionPass::getAnalysisUsage(AU);
52}
53
Tim Northover69fa84a2016-10-14 22:18:18 +000054void Legalizer::init(MachineFunction &MF) {
Tim Northover33b07d62016-07-22 20:03:43 +000055}
56
Aditya Nandakumare6201c82017-11-14 22:42:19 +000057static bool isArtifact(const MachineInstr &MI) {
58 switch (MI.getOpcode()) {
59 default:
60 return false;
61 case TargetOpcode::G_TRUNC:
62 case TargetOpcode::G_ZEXT:
63 case TargetOpcode::G_ANYEXT:
64 case TargetOpcode::G_SEXT:
65 case TargetOpcode::G_MERGE_VALUES:
66 case TargetOpcode::G_UNMERGE_VALUES:
67 return true;
68 }
69}
70
Tim Northover69fa84a2016-10-14 22:18:18 +000071bool Legalizer::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombet60495242016-08-27 00:18:24 +000072 // If the ISel pipeline failed, do not bother running that pass.
73 if (MF.getProperties().hasProperty(
74 MachineFunctionProperties::Property::FailedISel))
75 return false;
Tim Northover33b07d62016-07-22 20:03:43 +000076 DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n');
77 init(MF);
Quentin Colombet5e60bcd2016-08-27 02:38:21 +000078 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000079 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
Tim Northover69fa84a2016-10-14 22:18:18 +000080 LegalizerHelper Helper(MF);
Tim Northover33b07d62016-07-22 20:03:43 +000081
Aditya Nandakumare6201c82017-11-14 22:42:19 +000082 const size_t NumBlocks = MF.size();
83 MachineRegisterInfo &MRI = MF.getRegInfo();
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +000084
Aditya Nandakumare6201c82017-11-14 22:42:19 +000085 // Populate Insts
86 GISelWorkList<256> InstList;
87 GISelWorkList<128> ArtifactList;
88 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
89 // Perform legalization bottom up so we can DCE as we legalize.
90 // Traverse BB in RPOT and within each basic block, add insts top down,
91 // so when we pop_back_val in the legalization process, we traverse bottom-up.
92 for (auto *MBB : RPOT) {
93 if (MBB->empty())
94 continue;
95 for (MachineInstr &MI : *MBB) {
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +000096 // Only legalize pre-isel generic instructions: others don't have types
97 // and are assumed to be legal.
Aditya Nandakumare6201c82017-11-14 22:42:19 +000098 if (!isPreISelGenericOpcode(MI.getOpcode()))
Ahmed Bougachafaf8e9f2016-08-02 11:41:09 +000099 continue;
Aditya Nandakumare6201c82017-11-14 22:42:19 +0000100 if (isArtifact(MI))
101 ArtifactList.insert(&MI);
102 else
103 InstList.insert(&MI);
Tim Northover33b07d62016-07-22 20:03:43 +0000104 }
Daniel Sanders5377fb32017-04-20 15:46:12 +0000105 }
Aditya Nandakumare6201c82017-11-14 22:42:19 +0000106 Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) {
107 // Only legalize pre-isel generic instructions.
108 // Legalization process could generate Target specific pseudo
109 // instructions with generic types. Don't record them
110 if (isPreISelGenericOpcode(MI->getOpcode())) {
111 if (isArtifact(*MI))
112 ArtifactList.insert(MI);
113 else
114 InstList.insert(MI);
Tim Northover991b12b2016-08-30 20:51:25 +0000115 }
Aditya Nandakumare6201c82017-11-14 22:42:19 +0000116 DEBUG(dbgs() << ".. .. New MI: " << *MI;);
117 });
118 const LegalizerInfo &LInfo(Helper.getLegalizerInfo());
119 LegalizationArtifactCombiner ArtCombiner(Helper.MIRBuilder, MF.getRegInfo(), LInfo);
120 auto RemoveDeadInstFromLists = [&InstList,
121 &ArtifactList](MachineInstr *DeadMI) {
122 InstList.remove(DeadMI);
123 ArtifactList.remove(DeadMI);
124 };
125 bool Changed = false;
126 do {
127 while (!InstList.empty()) {
128 MachineInstr &MI = *InstList.pop_back_val();
129 assert(isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode");
130 if (isTriviallyDead(MI, MRI)) {
131 DEBUG(dbgs() << MI << "Is dead; erasing.\n");
132 MI.eraseFromParentAndMarkDBGValuesForRemoval();
133 continue;
134 }
135
136 // Do the legalization for this instruction.
137 auto Res = Helper.legalizeInstrStep(MI);
138 // Error out if we couldn't legalize this instruction. We may want to
139 // fall back to DAG ISel instead in the future.
140 if (Res == LegalizerHelper::UnableToLegalize) {
141 Helper.MIRBuilder.stopRecordingInsertions();
142 reportGISelFailure(MF, TPC, MORE, "gisel-legalize",
143 "unable to legalize instruction", MI);
144 return false;
145 }
146 Changed |= Res == LegalizerHelper::Legalized;
147 }
148 while (!ArtifactList.empty()) {
149 MachineInstr &MI = *ArtifactList.pop_back_val();
150 assert(isPreISelGenericOpcode(MI.getOpcode()) && "Expecting generic opcode");
151 if (isTriviallyDead(MI, MRI)) {
152 DEBUG(dbgs() << MI << "Is dead; erasing.\n");
153 RemoveDeadInstFromLists(&MI);
154 MI.eraseFromParentAndMarkDBGValuesForRemoval();
155 continue;
156 }
157 SmallVector<MachineInstr *, 4> DeadInstructions;
158 if (ArtCombiner.tryCombineInstruction(MI, DeadInstructions)) {
159 for (auto *DeadMI : DeadInstructions) {
160 DEBUG(dbgs() << ".. Erasing Dead Instruction " << *DeadMI);
161 RemoveDeadInstFromLists(DeadMI);
162 DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
163 }
164 Changed = true;
165 continue;
166 }
167 // If this was not an artifact (that could be combined away), this might
168 // need special handling. Add it to InstList, so when it's processed
169 // there, it has to be legal or specially handled.
170 else
171 InstList.insert(&MI);
172 }
173 } while (!InstList.empty());
174
175 // For now don't support if new blocks are inserted - we would need to fix the
176 // outerloop for that.
177 if (MF.size() != NumBlocks) {
178 MachineOptimizationRemarkMissed R("gisel-legalize", "GISelFailure",
179 MF.getFunction()->getSubprogram(),
180 /*MBB=*/nullptr);
181 R << "inserting blocks is not supported yet";
182 reportGISelFailure(MF, TPC, MORE, R);
183 return false;
Tim Northover991b12b2016-08-30 20:51:25 +0000184 }
185
Tim Northover33b07d62016-07-22 20:03:43 +0000186 return Changed;
187}