Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/GlobalISel/Legalizer.cpp -----------------------------===// |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 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 | // |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 10 | /// \file This file implements the LegalizerHelper class to legalize individual |
| 11 | /// instructions and the LegalizePass wrapper pass for the primary |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 12 | /// legalization. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/GlobalISel/Legalizer.h" |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SetVector.h" |
| 18 | #include "llvm/CodeGen/GlobalISel/LegalizerCombiner.h" |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h" |
Ahmed Bougacha | ae9dade | 2017-02-23 21:05:42 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/GlobalISel/Utils.h" |
| 21 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
Quentin Colombet | 5e60bcd | 2016-08-27 02:38:21 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 23 | #include "llvm/CodeGen/TargetPassConfig.h" |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Tim Northover | 991b12b | 2016-08-30 20:51:25 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetInstrInfo.h" |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 27 | |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 28 | #include <iterator> |
| 29 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "legalizer" |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 34 | char Legalizer::ID = 0; |
| 35 | INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE, |
Quentin Colombet | 5e60bcd | 2016-08-27 02:38:21 +0000 | [diff] [blame] | 36 | "Legalize the Machine IR a function's Machine IR", false, |
| 37 | false) |
| 38 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 39 | INITIALIZE_PASS_END(Legalizer, DEBUG_TYPE, |
Quentin Colombet | 5e60bcd | 2016-08-27 02:38:21 +0000 | [diff] [blame] | 40 | "Legalize the Machine IR a function's Machine IR", false, |
| 41 | false) |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 42 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 43 | Legalizer::Legalizer() : MachineFunctionPass(ID) { |
| 44 | initializeLegalizerPass(*PassRegistry::getPassRegistry()); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 47 | void Legalizer::getAnalysisUsage(AnalysisUsage &AU) const { |
Quentin Colombet | 5e60bcd | 2016-08-27 02:38:21 +0000 | [diff] [blame] | 48 | AU.addRequired<TargetPassConfig>(); |
| 49 | MachineFunctionPass::getAnalysisUsage(AU); |
| 50 | } |
| 51 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 52 | void Legalizer::init(MachineFunction &MF) { |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 55 | bool Legalizer::runOnMachineFunction(MachineFunction &MF) { |
Quentin Colombet | 6049524 | 2016-08-27 00:18:24 +0000 | [diff] [blame] | 56 | // If the ISel pipeline failed, do not bother running that pass. |
| 57 | if (MF.getProperties().hasProperty( |
| 58 | MachineFunctionProperties::Property::FailedISel)) |
| 59 | return false; |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 60 | DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n'); |
| 61 | init(MF); |
Quentin Colombet | 5e60bcd | 2016-08-27 02:38:21 +0000 | [diff] [blame] | 62 | const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>(); |
Ahmed Bougacha | ae9dade | 2017-02-23 21:05:42 +0000 | [diff] [blame] | 63 | MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr); |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 64 | LegalizerHelper Helper(MF); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 65 | |
| 66 | // FIXME: an instruction may need more than one pass before it is legal. For |
| 67 | // example on most architectures <3 x i3> is doubly-illegal. It would |
| 68 | // typically proceed along a path like: <3 x i3> -> <3 x i8> -> <8 x i8>. We |
| 69 | // probably want a worklist of instructions rather than naive iterate until |
| 70 | // convergence for performance reasons. |
| 71 | bool Changed = false; |
| 72 | MachineBasicBlock::iterator NextMI; |
Quentin Colombet | d58265a | 2017-10-13 21:16:14 +0000 | [diff] [blame] | 73 | using VecType = SmallSetVector<MachineInstr *, 8>; |
Quentin Colombet | 8622048 | 2017-10-13 21:16:05 +0000 | [diff] [blame] | 74 | VecType WorkList; |
| 75 | VecType CombineList; |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 76 | for (auto &MBB : MF) { |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 77 | for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) { |
| 78 | // Get the next Instruction before we try to legalize, because there's a |
| 79 | // good chance MI will be deleted. |
| 80 | NextMI = std::next(MI); |
Ahmed Bougacha | faf8e9f | 2016-08-02 11:41:09 +0000 | [diff] [blame] | 81 | |
| 82 | // Only legalize pre-isel generic instructions: others don't have types |
| 83 | // and are assumed to be legal. |
| 84 | if (!isPreISelGenericOpcode(MI->getOpcode())) |
| 85 | continue; |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 86 | unsigned NumNewInsns = 0; |
Quentin Colombet | 8622048 | 2017-10-13 21:16:05 +0000 | [diff] [blame] | 87 | WorkList.clear(); |
| 88 | CombineList.clear(); |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 89 | Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) { |
Aditya Nandakumar | 21d8d31 | 2017-05-04 22:00:42 +0000 | [diff] [blame] | 90 | // Only legalize pre-isel generic instructions. |
| 91 | // Legalization process could generate Target specific pseudo |
| 92 | // instructions with generic types. Don't record them |
| 93 | if (isPreISelGenericOpcode(MI->getOpcode())) { |
| 94 | ++NumNewInsns; |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 95 | WorkList.insert(MI); |
| 96 | CombineList.insert(MI); |
Aditya Nandakumar | 21d8d31 | 2017-05-04 22:00:42 +0000 | [diff] [blame] | 97 | } |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 98 | }); |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 99 | WorkList.insert(&*MI); |
Aditya Nandakumar | d2a954d | 2017-10-25 18:49:18 +0000 | [diff] [blame] | 100 | LegalizerCombiner C(Helper.MIRBuilder, MF.getRegInfo(), |
| 101 | Helper.getLegalizerInfo()); |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 102 | bool Changed = false; |
Aditya Nandakumar | eb80a51 | 2017-04-07 21:49:30 +0000 | [diff] [blame] | 103 | LegalizerHelper::LegalizeResult Res; |
Aditya Nandakumar | eb80a51 | 2017-04-07 21:49:30 +0000 | [diff] [blame] | 104 | do { |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 105 | assert(!WorkList.empty() && "Expecting illegal ops"); |
| 106 | while (!WorkList.empty()) { |
| 107 | NumNewInsns = 0; |
| 108 | MachineInstr *CurrInst = WorkList.pop_back_val(); |
| 109 | Res = Helper.legalizeInstrStep(*CurrInst); |
| 110 | // Error out if we couldn't legalize this instruction. We may want to |
| 111 | // fall back to DAG ISel instead in the future. |
Aditya Nandakumar | eb80a51 | 2017-04-07 21:49:30 +0000 | [diff] [blame] | 112 | if (Res == LegalizerHelper::UnableToLegalize) { |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 113 | Helper.MIRBuilder.stopRecordingInsertions(); |
| 114 | if (Res == LegalizerHelper::UnableToLegalize) { |
| 115 | reportGISelFailure(MF, TPC, MORE, "gisel-legalize", |
| 116 | "unable to legalize instruction", *CurrInst); |
| 117 | return false; |
| 118 | } |
Aditya Nandakumar | eb80a51 | 2017-04-07 21:49:30 +0000 | [diff] [blame] | 119 | } |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 120 | Changed |= Res == LegalizerHelper::Legalized; |
| 121 | // If CurrInst was legalized, there's a good chance that it might have |
| 122 | // been erased. So remove it from the Combine List. |
| 123 | if (Res == LegalizerHelper::Legalized) |
| 124 | CombineList.remove(CurrInst); |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 125 | |
| 126 | #ifndef NDEBUG |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 127 | if (NumNewInsns) |
| 128 | for (unsigned I = WorkList.size() - NumNewInsns, |
| 129 | E = WorkList.size(); |
| 130 | I != E; ++I) |
| 131 | DEBUG(dbgs() << ".. .. New MI: " << *WorkList[I];); |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 132 | #endif |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 133 | } |
| 134 | // Do the combines. |
| 135 | while (!CombineList.empty()) { |
| 136 | NumNewInsns = 0; |
| 137 | MachineInstr *CurrInst = CombineList.pop_back_val(); |
| 138 | SmallVector<MachineInstr *, 4> DeadInstructions; |
| 139 | Changed |= C.tryCombineInstruction(*CurrInst, DeadInstructions); |
| 140 | for (auto *DeadMI : DeadInstructions) { |
| 141 | DEBUG(dbgs() << ".. Erasing Dead Instruction " << *DeadMI); |
| 142 | CombineList.remove(DeadMI); |
| 143 | WorkList.remove(DeadMI); |
| 144 | DeadMI->eraseFromParent(); |
| 145 | } |
| 146 | #ifndef NDEBUG |
| 147 | if (NumNewInsns) |
| 148 | for (unsigned I = CombineList.size() - NumNewInsns, |
| 149 | E = CombineList.size(); |
| 150 | I != E; ++I) |
| 151 | DEBUG(dbgs() << ".. .. Combine New MI: " << *CombineList[I];); |
| 152 | #endif |
| 153 | } |
| 154 | } while (!WorkList.empty()); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 155 | |
Aditya Nandakumar | eb80a51 | 2017-04-07 21:49:30 +0000 | [diff] [blame] | 156 | Helper.MIRBuilder.stopRecordingInsertions(); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 157 | } |
Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 158 | } |
Tim Northover | 991b12b | 2016-08-30 20:51:25 +0000 | [diff] [blame] | 159 | |
Tim Northover | 991b12b | 2016-08-30 20:51:25 +0000 | [diff] [blame] | 160 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 161 | MachineIRBuilder MIRBuilder(MF); |
Aditya Nandakumar | d2a954d | 2017-10-25 18:49:18 +0000 | [diff] [blame] | 162 | LegalizerCombiner C(MIRBuilder, MRI, Helper.getLegalizerInfo()); |
Tim Northover | 991b12b | 2016-08-30 20:51:25 +0000 | [diff] [blame] | 163 | for (auto &MBB : MF) { |
| 164 | for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) { |
| 165 | // Get the next Instruction before we try to legalize, because there's a |
| 166 | // good chance MI will be deleted. |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 167 | // TOOD: Perhaps move this to a combiner pass later?. |
Tim Northover | 991b12b | 2016-08-30 20:51:25 +0000 | [diff] [blame] | 168 | NextMI = std::next(MI); |
Aditya Nandakumar | c6615f5 | 2017-08-30 19:32:59 +0000 | [diff] [blame] | 169 | SmallVector<MachineInstr *, 4> DeadInsts; |
| 170 | Changed |= C.tryCombineMerges(*MI, DeadInsts); |
| 171 | for (auto *DeadMI : DeadInsts) |
| 172 | DeadMI->eraseFromParent(); |
Tim Northover | 991b12b | 2016-08-30 20:51:25 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 176 | return Changed; |
| 177 | } |