| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/GlobalISel/LegalizerHelper.cpp -----------------------===// | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 | // | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 9 | /// \file This file implements the LegalizerHelper class to legalize | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 10 | /// individual instructions and the LegalizeMachineIR wrapper pass for the | 
|  | 11 | /// primary legalization. | 
|  | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h" | 
| Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/GlobalISel/CallLowering.h" | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h" | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetInstrInfo.h" | 
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/TargetLowering.h" | 
|  | 22 | #include "llvm/CodeGen/TargetSubtargetInfo.h" | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 26 |  | 
| Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "legalizer" | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | using namespace llvm; | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 30 | using namespace LegalizeActions; | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 31 |  | 
| Matt Arsenault | c83b823 | 2019-02-07 17:38:00 +0000 | [diff] [blame] | 32 | /// Try to break down \p OrigTy into \p NarrowTy sized pieces. | 
|  | 33 | /// | 
|  | 34 | /// Returns the number of \p NarrowTy elements needed to reconstruct \p OrigTy, | 
|  | 35 | /// with any leftover piece as type \p LeftoverTy | 
|  | 36 | /// | 
|  | 37 | /// Returns -1 if the breakdown is not satisfiable. | 
|  | 38 | static int getNarrowTypeBreakDown(LLT OrigTy, LLT NarrowTy, LLT &LeftoverTy) { | 
|  | 39 | assert(!LeftoverTy.isValid() && "this is an out argument"); | 
|  | 40 |  | 
|  | 41 | unsigned Size = OrigTy.getSizeInBits(); | 
|  | 42 | unsigned NarrowSize = NarrowTy.getSizeInBits(); | 
|  | 43 | unsigned NumParts = Size / NarrowSize; | 
|  | 44 | unsigned LeftoverSize = Size - NumParts * NarrowSize; | 
|  | 45 | assert(Size > NarrowSize); | 
|  | 46 |  | 
|  | 47 | if (LeftoverSize == 0) | 
|  | 48 | return NumParts; | 
|  | 49 |  | 
|  | 50 | if (NarrowTy.isVector()) { | 
|  | 51 | unsigned EltSize = OrigTy.getScalarSizeInBits(); | 
|  | 52 | if (LeftoverSize % EltSize != 0) | 
|  | 53 | return -1; | 
|  | 54 | LeftoverTy = LLT::scalarOrVector(LeftoverSize / EltSize, EltSize); | 
|  | 55 | } else { | 
|  | 56 | LeftoverTy = LLT::scalar(LeftoverSize); | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | return NumParts; | 
|  | 60 | } | 
|  | 61 |  | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 62 | LegalizerHelper::LegalizerHelper(MachineFunction &MF, | 
| Aditya Nandakumar | 500e3ea | 2019-01-16 00:40:37 +0000 | [diff] [blame] | 63 | GISelChangeObserver &Observer, | 
|  | 64 | MachineIRBuilder &Builder) | 
|  | 65 | : MIRBuilder(Builder), MRI(MF.getRegInfo()), | 
|  | 66 | LI(*MF.getSubtarget().getLegalizerInfo()), Observer(Observer) { | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 67 | MIRBuilder.setMF(MF); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 68 | MIRBuilder.setChangeObserver(Observer); | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 71 | LegalizerHelper::LegalizerHelper(MachineFunction &MF, const LegalizerInfo &LI, | 
| Aditya Nandakumar | 500e3ea | 2019-01-16 00:40:37 +0000 | [diff] [blame] | 72 | GISelChangeObserver &Observer, | 
|  | 73 | MachineIRBuilder &B) | 
|  | 74 | : MIRBuilder(B), MRI(MF.getRegInfo()), LI(LI), Observer(Observer) { | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 75 | MIRBuilder.setMF(MF); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 76 | MIRBuilder.setChangeObserver(Observer); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 77 | } | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 78 | LegalizerHelper::LegalizeResult | 
| Volkan Keles | 685fbda | 2017-03-10 18:34:57 +0000 | [diff] [blame] | 79 | LegalizerHelper::legalizeInstrStep(MachineInstr &MI) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 80 | LLVM_DEBUG(dbgs() << "Legalizing: "; MI.print(dbgs())); | 
| Daniel Sanders | 5377fb3 | 2017-04-20 15:46:12 +0000 | [diff] [blame] | 81 |  | 
| Daniel Sanders | 262ed0e | 2018-01-24 17:17:46 +0000 | [diff] [blame] | 82 | auto Step = LI.getAction(MI, MRI); | 
|  | 83 | switch (Step.Action) { | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 84 | case Legal: | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 85 | LLVM_DEBUG(dbgs() << ".. Already legal\n"); | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 86 | return AlreadyLegal; | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 87 | case Libcall: | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 88 | LLVM_DEBUG(dbgs() << ".. Convert to libcall\n"); | 
| Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 89 | return libcall(MI); | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 90 | case NarrowScalar: | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 91 | LLVM_DEBUG(dbgs() << ".. Narrow scalar\n"); | 
| Daniel Sanders | 262ed0e | 2018-01-24 17:17:46 +0000 | [diff] [blame] | 92 | return narrowScalar(MI, Step.TypeIdx, Step.NewType); | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 93 | case WidenScalar: | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 94 | LLVM_DEBUG(dbgs() << ".. Widen scalar\n"); | 
| Daniel Sanders | 262ed0e | 2018-01-24 17:17:46 +0000 | [diff] [blame] | 95 | return widenScalar(MI, Step.TypeIdx, Step.NewType); | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 96 | case Lower: | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 97 | LLVM_DEBUG(dbgs() << ".. Lower\n"); | 
| Daniel Sanders | 262ed0e | 2018-01-24 17:17:46 +0000 | [diff] [blame] | 98 | return lower(MI, Step.TypeIdx, Step.NewType); | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 99 | case FewerElements: | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 100 | LLVM_DEBUG(dbgs() << ".. Reduce number of elements\n"); | 
| Daniel Sanders | 262ed0e | 2018-01-24 17:17:46 +0000 | [diff] [blame] | 101 | return fewerElementsVector(MI, Step.TypeIdx, Step.NewType); | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 102 | case Custom: | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 103 | LLVM_DEBUG(dbgs() << ".. Custom legalization\n"); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 104 | return LI.legalizeCustom(MI, MRI, MIRBuilder, Observer) ? Legalized | 
|  | 105 | : UnableToLegalize; | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 106 | default: | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 107 | LLVM_DEBUG(dbgs() << ".. Unable to legalize\n"); | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 108 | return UnableToLegalize; | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 |  | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 112 | void LegalizerHelper::extractParts(unsigned Reg, LLT Ty, int NumParts, | 
|  | 113 | SmallVectorImpl<unsigned> &VRegs) { | 
| Tim Northover | bf01729 | 2017-03-03 22:46:09 +0000 | [diff] [blame] | 114 | for (int i = 0; i < NumParts; ++i) | 
| Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 115 | VRegs.push_back(MRI.createGenericVirtualRegister(Ty)); | 
| Tim Northover | bf01729 | 2017-03-03 22:46:09 +0000 | [diff] [blame] | 116 | MIRBuilder.buildUnmerge(VRegs, Reg); | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Matt Arsenault | c7bce73 | 2019-01-31 02:46:05 +0000 | [diff] [blame] | 119 | bool LegalizerHelper::extractParts(unsigned Reg, LLT RegTy, | 
|  | 120 | LLT MainTy, LLT &LeftoverTy, | 
|  | 121 | SmallVectorImpl<unsigned> &VRegs, | 
|  | 122 | SmallVectorImpl<unsigned> &LeftoverRegs) { | 
|  | 123 | assert(!LeftoverTy.isValid() && "this is an out argument"); | 
|  | 124 |  | 
|  | 125 | unsigned RegSize = RegTy.getSizeInBits(); | 
|  | 126 | unsigned MainSize = MainTy.getSizeInBits(); | 
|  | 127 | unsigned NumParts = RegSize / MainSize; | 
|  | 128 | unsigned LeftoverSize = RegSize - NumParts * MainSize; | 
|  | 129 |  | 
|  | 130 | // Use an unmerge when possible. | 
|  | 131 | if (LeftoverSize == 0) { | 
|  | 132 | for (unsigned I = 0; I < NumParts; ++I) | 
|  | 133 | VRegs.push_back(MRI.createGenericVirtualRegister(MainTy)); | 
|  | 134 | MIRBuilder.buildUnmerge(VRegs, Reg); | 
|  | 135 | return true; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | if (MainTy.isVector()) { | 
|  | 139 | unsigned EltSize = MainTy.getScalarSizeInBits(); | 
|  | 140 | if (LeftoverSize % EltSize != 0) | 
|  | 141 | return false; | 
|  | 142 | LeftoverTy = LLT::scalarOrVector(LeftoverSize / EltSize, EltSize); | 
|  | 143 | } else { | 
|  | 144 | LeftoverTy = LLT::scalar(LeftoverSize); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | // For irregular sizes, extract the individual parts. | 
|  | 148 | for (unsigned I = 0; I != NumParts; ++I) { | 
|  | 149 | unsigned NewReg = MRI.createGenericVirtualRegister(MainTy); | 
|  | 150 | VRegs.push_back(NewReg); | 
|  | 151 | MIRBuilder.buildExtract(NewReg, Reg, MainSize * I); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | for (unsigned Offset = MainSize * NumParts; Offset < RegSize; | 
|  | 155 | Offset += LeftoverSize) { | 
|  | 156 | unsigned NewReg = MRI.createGenericVirtualRegister(LeftoverTy); | 
|  | 157 | LeftoverRegs.push_back(NewReg); | 
|  | 158 | MIRBuilder.buildExtract(NewReg, Reg, Offset); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | return true; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | void LegalizerHelper::insertParts(unsigned DstReg, | 
|  | 165 | LLT ResultTy, LLT PartTy, | 
|  | 166 | ArrayRef<unsigned> PartRegs, | 
|  | 167 | LLT LeftoverTy, | 
|  | 168 | ArrayRef<unsigned> LeftoverRegs) { | 
|  | 169 | if (!LeftoverTy.isValid()) { | 
|  | 170 | assert(LeftoverRegs.empty()); | 
|  | 171 |  | 
| Matt Arsenault | 81511e5 | 2019-02-05 00:13:44 +0000 | [diff] [blame] | 172 | if (!ResultTy.isVector()) { | 
|  | 173 | MIRBuilder.buildMerge(DstReg, PartRegs); | 
|  | 174 | return; | 
|  | 175 | } | 
|  | 176 |  | 
| Matt Arsenault | c7bce73 | 2019-01-31 02:46:05 +0000 | [diff] [blame] | 177 | if (PartTy.isVector()) | 
|  | 178 | MIRBuilder.buildConcatVectors(DstReg, PartRegs); | 
|  | 179 | else | 
|  | 180 | MIRBuilder.buildBuildVector(DstReg, PartRegs); | 
|  | 181 | return; | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | unsigned PartSize = PartTy.getSizeInBits(); | 
|  | 185 | unsigned LeftoverPartSize = LeftoverTy.getSizeInBits(); | 
|  | 186 |  | 
|  | 187 | unsigned CurResultReg = MRI.createGenericVirtualRegister(ResultTy); | 
|  | 188 | MIRBuilder.buildUndef(CurResultReg); | 
|  | 189 |  | 
|  | 190 | unsigned Offset = 0; | 
|  | 191 | for (unsigned PartReg : PartRegs) { | 
|  | 192 | unsigned NewResultReg = MRI.createGenericVirtualRegister(ResultTy); | 
|  | 193 | MIRBuilder.buildInsert(NewResultReg, CurResultReg, PartReg, Offset); | 
|  | 194 | CurResultReg = NewResultReg; | 
|  | 195 | Offset += PartSize; | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | for (unsigned I = 0, E = LeftoverRegs.size(); I != E; ++I) { | 
|  | 199 | // Use the original output register for the final insert to avoid a copy. | 
|  | 200 | unsigned NewResultReg = (I + 1 == E) ? | 
|  | 201 | DstReg : MRI.createGenericVirtualRegister(ResultTy); | 
|  | 202 |  | 
|  | 203 | MIRBuilder.buildInsert(NewResultReg, CurResultReg, LeftoverRegs[I], Offset); | 
|  | 204 | CurResultReg = NewResultReg; | 
|  | 205 | Offset += LeftoverPartSize; | 
|  | 206 | } | 
|  | 207 | } | 
|  | 208 |  | 
| Tim Northover | e041841 | 2017-02-08 23:23:39 +0000 | [diff] [blame] | 209 | static RTLIB::Libcall getRTLibDesc(unsigned Opcode, unsigned Size) { | 
|  | 210 | switch (Opcode) { | 
| Diana Picus | e97822e | 2017-04-24 07:22:31 +0000 | [diff] [blame] | 211 | case TargetOpcode::G_SDIV: | 
| Petar Avramovic | 0a5e4eb | 2018-12-18 15:59:51 +0000 | [diff] [blame] | 212 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 213 | return Size == 64 ? RTLIB::SDIV_I64 : RTLIB::SDIV_I32; | 
| Diana Picus | e97822e | 2017-04-24 07:22:31 +0000 | [diff] [blame] | 214 | case TargetOpcode::G_UDIV: | 
| Petar Avramovic | 0a5e4eb | 2018-12-18 15:59:51 +0000 | [diff] [blame] | 215 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 216 | return Size == 64 ? RTLIB::UDIV_I64 : RTLIB::UDIV_I32; | 
| Diana Picus | 02e1101 | 2017-06-15 10:53:31 +0000 | [diff] [blame] | 217 | case TargetOpcode::G_SREM: | 
| Petar Avramovic | 0a5e4eb | 2018-12-18 15:59:51 +0000 | [diff] [blame] | 218 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 219 | return Size == 64 ? RTLIB::SREM_I64 : RTLIB::SREM_I32; | 
| Diana Picus | 02e1101 | 2017-06-15 10:53:31 +0000 | [diff] [blame] | 220 | case TargetOpcode::G_UREM: | 
| Petar Avramovic | 0a5e4eb | 2018-12-18 15:59:51 +0000 | [diff] [blame] | 221 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 222 | return Size == 64 ? RTLIB::UREM_I64 : RTLIB::UREM_I32; | 
| Diana Picus | 0528e2c | 2018-11-26 11:07:02 +0000 | [diff] [blame] | 223 | case TargetOpcode::G_CTLZ_ZERO_UNDEF: | 
|  | 224 | assert(Size == 32 && "Unsupported size"); | 
|  | 225 | return RTLIB::CTLZ_I32; | 
| Diana Picus | 1314a28 | 2017-04-11 10:52:34 +0000 | [diff] [blame] | 226 | case TargetOpcode::G_FADD: | 
|  | 227 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 228 | return Size == 64 ? RTLIB::ADD_F64 : RTLIB::ADD_F32; | 
| Javed Absar | 5cde1cc | 2017-10-30 13:51:56 +0000 | [diff] [blame] | 229 | case TargetOpcode::G_FSUB: | 
|  | 230 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 231 | return Size == 64 ? RTLIB::SUB_F64 : RTLIB::SUB_F32; | 
| Diana Picus | 9faa09b | 2017-11-23 12:44:20 +0000 | [diff] [blame] | 232 | case TargetOpcode::G_FMUL: | 
|  | 233 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 234 | return Size == 64 ? RTLIB::MUL_F64 : RTLIB::MUL_F32; | 
| Diana Picus | c01f7f1 | 2017-11-23 13:26:07 +0000 | [diff] [blame] | 235 | case TargetOpcode::G_FDIV: | 
|  | 236 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 237 | return Size == 64 ? RTLIB::DIV_F64 : RTLIB::DIV_F32; | 
| Jessica Paquette | 84bedac | 2019-01-30 23:46:15 +0000 | [diff] [blame] | 238 | case TargetOpcode::G_FEXP: | 
|  | 239 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 240 | return Size == 64 ? RTLIB::EXP_F64 : RTLIB::EXP_F32; | 
| Tim Northover | e041841 | 2017-02-08 23:23:39 +0000 | [diff] [blame] | 241 | case TargetOpcode::G_FREM: | 
|  | 242 | return Size == 64 ? RTLIB::REM_F64 : RTLIB::REM_F32; | 
|  | 243 | case TargetOpcode::G_FPOW: | 
|  | 244 | return Size == 64 ? RTLIB::POW_F64 : RTLIB::POW_F32; | 
| Diana Picus | e74243d | 2018-01-12 11:30:45 +0000 | [diff] [blame] | 245 | case TargetOpcode::G_FMA: | 
|  | 246 | assert((Size == 32 || Size == 64) && "Unsupported size"); | 
|  | 247 | return Size == 64 ? RTLIB::FMA_F64 : RTLIB::FMA_F32; | 
| Jessica Paquette | 7db82d7 | 2019-01-28 18:34:18 +0000 | [diff] [blame] | 248 | case TargetOpcode::G_FSIN: | 
|  | 249 | assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size"); | 
|  | 250 | return Size == 128 ? RTLIB::SIN_F128 | 
|  | 251 | : Size == 64 ? RTLIB::SIN_F64 : RTLIB::SIN_F32; | 
|  | 252 | case TargetOpcode::G_FCOS: | 
|  | 253 | assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size"); | 
|  | 254 | return Size == 128 ? RTLIB::COS_F128 | 
|  | 255 | : Size == 64 ? RTLIB::COS_F64 : RTLIB::COS_F32; | 
| Jessica Paquette | c49428a | 2019-01-28 19:53:14 +0000 | [diff] [blame] | 256 | case TargetOpcode::G_FLOG10: | 
|  | 257 | assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size"); | 
|  | 258 | return Size == 128 ? RTLIB::LOG10_F128 | 
|  | 259 | : Size == 64 ? RTLIB::LOG10_F64 : RTLIB::LOG10_F32; | 
| Jessica Paquette | 2d73ecd | 2019-01-28 21:27:23 +0000 | [diff] [blame] | 260 | case TargetOpcode::G_FLOG: | 
|  | 261 | assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size"); | 
|  | 262 | return Size == 128 ? RTLIB::LOG_F128 | 
|  | 263 | : Size == 64 ? RTLIB::LOG_F64 : RTLIB::LOG_F32; | 
| Jessica Paquette | 0154bd1 | 2019-01-30 21:16:04 +0000 | [diff] [blame] | 264 | case TargetOpcode::G_FLOG2: | 
|  | 265 | assert((Size == 32 || Size == 64 || Size == 128) && "Unsupported size"); | 
|  | 266 | return Size == 128 ? RTLIB::LOG2_F128 | 
|  | 267 | : Size == 64 ? RTLIB::LOG2_F64 : RTLIB::LOG2_F32; | 
| Tim Northover | e041841 | 2017-02-08 23:23:39 +0000 | [diff] [blame] | 268 | } | 
|  | 269 | llvm_unreachable("Unknown libcall function"); | 
|  | 270 | } | 
|  | 271 |  | 
| Diana Picus | fc1675e | 2017-07-05 12:57:24 +0000 | [diff] [blame] | 272 | LegalizerHelper::LegalizeResult | 
|  | 273 | llvm::createLibcall(MachineIRBuilder &MIRBuilder, RTLIB::Libcall Libcall, | 
|  | 274 | const CallLowering::ArgInfo &Result, | 
|  | 275 | ArrayRef<CallLowering::ArgInfo> Args) { | 
| Diana Picus | e97822e | 2017-04-24 07:22:31 +0000 | [diff] [blame] | 276 | auto &CLI = *MIRBuilder.getMF().getSubtarget().getCallLowering(); | 
|  | 277 | auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering(); | 
| Diana Picus | e97822e | 2017-04-24 07:22:31 +0000 | [diff] [blame] | 278 | const char *Name = TLI.getLibcallName(Libcall); | 
| Diana Picus | d0104ea | 2017-07-06 09:09:33 +0000 | [diff] [blame] | 279 |  | 
| Diana Picus | e97822e | 2017-04-24 07:22:31 +0000 | [diff] [blame] | 280 | MIRBuilder.getMF().getFrameInfo().setHasCalls(true); | 
| Diana Picus | 02e1101 | 2017-06-15 10:53:31 +0000 | [diff] [blame] | 281 | if (!CLI.lowerCall(MIRBuilder, TLI.getLibcallCallingConv(Libcall), | 
|  | 282 | MachineOperand::CreateES(Name), Result, Args)) | 
|  | 283 | return LegalizerHelper::UnableToLegalize; | 
| Diana Picus | d0104ea | 2017-07-06 09:09:33 +0000 | [diff] [blame] | 284 |  | 
| Diana Picus | e97822e | 2017-04-24 07:22:31 +0000 | [diff] [blame] | 285 | return LegalizerHelper::Legalized; | 
|  | 286 | } | 
|  | 287 |  | 
| Diana Picus | 65ed364 | 2018-01-17 13:34:10 +0000 | [diff] [blame] | 288 | // Useful for libcalls where all operands have the same type. | 
| Diana Picus | 02e1101 | 2017-06-15 10:53:31 +0000 | [diff] [blame] | 289 | static LegalizerHelper::LegalizeResult | 
|  | 290 | simpleLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, unsigned Size, | 
|  | 291 | Type *OpType) { | 
|  | 292 | auto Libcall = getRTLibDesc(MI.getOpcode(), Size); | 
| Diana Picus | e74243d | 2018-01-12 11:30:45 +0000 | [diff] [blame] | 293 |  | 
|  | 294 | SmallVector<CallLowering::ArgInfo, 3> Args; | 
|  | 295 | for (unsigned i = 1; i < MI.getNumOperands(); i++) | 
|  | 296 | Args.push_back({MI.getOperand(i).getReg(), OpType}); | 
| Diana Picus | fc1675e | 2017-07-05 12:57:24 +0000 | [diff] [blame] | 297 | return createLibcall(MIRBuilder, Libcall, {MI.getOperand(0).getReg(), OpType}, | 
| Diana Picus | e74243d | 2018-01-12 11:30:45 +0000 | [diff] [blame] | 298 | Args); | 
| Diana Picus | 02e1101 | 2017-06-15 10:53:31 +0000 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
| Diana Picus | 65ed364 | 2018-01-17 13:34:10 +0000 | [diff] [blame] | 301 | static RTLIB::Libcall getConvRTLibDesc(unsigned Opcode, Type *ToType, | 
|  | 302 | Type *FromType) { | 
|  | 303 | auto ToMVT = MVT::getVT(ToType); | 
|  | 304 | auto FromMVT = MVT::getVT(FromType); | 
|  | 305 |  | 
|  | 306 | switch (Opcode) { | 
|  | 307 | case TargetOpcode::G_FPEXT: | 
|  | 308 | return RTLIB::getFPEXT(FromMVT, ToMVT); | 
|  | 309 | case TargetOpcode::G_FPTRUNC: | 
|  | 310 | return RTLIB::getFPROUND(FromMVT, ToMVT); | 
| Diana Picus | 4ed0ee7 | 2018-01-30 07:54:52 +0000 | [diff] [blame] | 311 | case TargetOpcode::G_FPTOSI: | 
|  | 312 | return RTLIB::getFPTOSINT(FromMVT, ToMVT); | 
|  | 313 | case TargetOpcode::G_FPTOUI: | 
|  | 314 | return RTLIB::getFPTOUINT(FromMVT, ToMVT); | 
| Diana Picus | 517531e | 2018-01-30 09:15:17 +0000 | [diff] [blame] | 315 | case TargetOpcode::G_SITOFP: | 
|  | 316 | return RTLIB::getSINTTOFP(FromMVT, ToMVT); | 
|  | 317 | case TargetOpcode::G_UITOFP: | 
|  | 318 | return RTLIB::getUINTTOFP(FromMVT, ToMVT); | 
| Diana Picus | 65ed364 | 2018-01-17 13:34:10 +0000 | [diff] [blame] | 319 | } | 
|  | 320 | llvm_unreachable("Unsupported libcall function"); | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | static LegalizerHelper::LegalizeResult | 
|  | 324 | conversionLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, Type *ToType, | 
|  | 325 | Type *FromType) { | 
|  | 326 | RTLIB::Libcall Libcall = getConvRTLibDesc(MI.getOpcode(), ToType, FromType); | 
|  | 327 | return createLibcall(MIRBuilder, Libcall, {MI.getOperand(0).getReg(), ToType}, | 
|  | 328 | {{MI.getOperand(1).getReg(), FromType}}); | 
|  | 329 | } | 
|  | 330 |  | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 331 | LegalizerHelper::LegalizeResult | 
|  | 332 | LegalizerHelper::libcall(MachineInstr &MI) { | 
| Diana Picus | 02e1101 | 2017-06-15 10:53:31 +0000 | [diff] [blame] | 333 | LLT LLTy = MRI.getType(MI.getOperand(0).getReg()); | 
|  | 334 | unsigned Size = LLTy.getSizeInBits(); | 
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 335 | auto &Ctx = MIRBuilder.getMF().getFunction().getContext(); | 
| Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 336 |  | 
| Diana Picus | fc1675e | 2017-07-05 12:57:24 +0000 | [diff] [blame] | 337 | MIRBuilder.setInstr(MI); | 
|  | 338 |  | 
| Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 339 | switch (MI.getOpcode()) { | 
|  | 340 | default: | 
|  | 341 | return UnableToLegalize; | 
| Diana Picus | e97822e | 2017-04-24 07:22:31 +0000 | [diff] [blame] | 342 | case TargetOpcode::G_SDIV: | 
| Diana Picus | 02e1101 | 2017-06-15 10:53:31 +0000 | [diff] [blame] | 343 | case TargetOpcode::G_UDIV: | 
|  | 344 | case TargetOpcode::G_SREM: | 
| Diana Picus | 0528e2c | 2018-11-26 11:07:02 +0000 | [diff] [blame] | 345 | case TargetOpcode::G_UREM: | 
|  | 346 | case TargetOpcode::G_CTLZ_ZERO_UNDEF: { | 
| Petar Avramovic | 0a5e4eb | 2018-12-18 15:59:51 +0000 | [diff] [blame] | 347 | Type *HLTy = IntegerType::get(Ctx, Size); | 
| Diana Picus | fc1675e | 2017-07-05 12:57:24 +0000 | [diff] [blame] | 348 | auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy); | 
|  | 349 | if (Status != Legalized) | 
|  | 350 | return Status; | 
|  | 351 | break; | 
| Diana Picus | e97822e | 2017-04-24 07:22:31 +0000 | [diff] [blame] | 352 | } | 
| Diana Picus | 1314a28 | 2017-04-11 10:52:34 +0000 | [diff] [blame] | 353 | case TargetOpcode::G_FADD: | 
| Javed Absar | 5cde1cc | 2017-10-30 13:51:56 +0000 | [diff] [blame] | 354 | case TargetOpcode::G_FSUB: | 
| Diana Picus | 9faa09b | 2017-11-23 12:44:20 +0000 | [diff] [blame] | 355 | case TargetOpcode::G_FMUL: | 
| Diana Picus | c01f7f1 | 2017-11-23 13:26:07 +0000 | [diff] [blame] | 356 | case TargetOpcode::G_FDIV: | 
| Diana Picus | e74243d | 2018-01-12 11:30:45 +0000 | [diff] [blame] | 357 | case TargetOpcode::G_FMA: | 
| Tim Northover | e041841 | 2017-02-08 23:23:39 +0000 | [diff] [blame] | 358 | case TargetOpcode::G_FPOW: | 
| Jessica Paquette | 7db82d7 | 2019-01-28 18:34:18 +0000 | [diff] [blame] | 359 | case TargetOpcode::G_FREM: | 
|  | 360 | case TargetOpcode::G_FCOS: | 
| Jessica Paquette | c49428a | 2019-01-28 19:53:14 +0000 | [diff] [blame] | 361 | case TargetOpcode::G_FSIN: | 
| Jessica Paquette | 2d73ecd | 2019-01-28 21:27:23 +0000 | [diff] [blame] | 362 | case TargetOpcode::G_FLOG10: | 
| Jessica Paquette | 0154bd1 | 2019-01-30 21:16:04 +0000 | [diff] [blame] | 363 | case TargetOpcode::G_FLOG: | 
| Jessica Paquette | 84bedac | 2019-01-30 23:46:15 +0000 | [diff] [blame] | 364 | case TargetOpcode::G_FLOG2: | 
|  | 365 | case TargetOpcode::G_FEXP: { | 
| Jessica Paquette | 7db82d7 | 2019-01-28 18:34:18 +0000 | [diff] [blame] | 366 | if (Size > 64) { | 
|  | 367 | LLVM_DEBUG(dbgs() << "Size " << Size << " too large to legalize.\n"); | 
|  | 368 | return UnableToLegalize; | 
|  | 369 | } | 
| Diana Picus | 02e1101 | 2017-06-15 10:53:31 +0000 | [diff] [blame] | 370 | Type *HLTy = Size == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx); | 
| Diana Picus | fc1675e | 2017-07-05 12:57:24 +0000 | [diff] [blame] | 371 | auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy); | 
|  | 372 | if (Status != Legalized) | 
|  | 373 | return Status; | 
|  | 374 | break; | 
| Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 375 | } | 
| Diana Picus | 65ed364 | 2018-01-17 13:34:10 +0000 | [diff] [blame] | 376 | case TargetOpcode::G_FPEXT: { | 
|  | 377 | // FIXME: Support other floating point types (half, fp128 etc) | 
|  | 378 | unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); | 
|  | 379 | unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); | 
|  | 380 | if (ToSize != 64 || FromSize != 32) | 
|  | 381 | return UnableToLegalize; | 
|  | 382 | LegalizeResult Status = conversionLibcall( | 
|  | 383 | MI, MIRBuilder, Type::getDoubleTy(Ctx), Type::getFloatTy(Ctx)); | 
|  | 384 | if (Status != Legalized) | 
|  | 385 | return Status; | 
|  | 386 | break; | 
|  | 387 | } | 
|  | 388 | case TargetOpcode::G_FPTRUNC: { | 
|  | 389 | // FIXME: Support other floating point types (half, fp128 etc) | 
|  | 390 | unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); | 
|  | 391 | unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); | 
|  | 392 | if (ToSize != 32 || FromSize != 64) | 
|  | 393 | return UnableToLegalize; | 
|  | 394 | LegalizeResult Status = conversionLibcall( | 
|  | 395 | MI, MIRBuilder, Type::getFloatTy(Ctx), Type::getDoubleTy(Ctx)); | 
|  | 396 | if (Status != Legalized) | 
|  | 397 | return Status; | 
|  | 398 | break; | 
|  | 399 | } | 
| Diana Picus | 4ed0ee7 | 2018-01-30 07:54:52 +0000 | [diff] [blame] | 400 | case TargetOpcode::G_FPTOSI: | 
|  | 401 | case TargetOpcode::G_FPTOUI: { | 
|  | 402 | // FIXME: Support other types | 
|  | 403 | unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); | 
|  | 404 | unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); | 
|  | 405 | if (ToSize != 32 || (FromSize != 32 && FromSize != 64)) | 
|  | 406 | return UnableToLegalize; | 
|  | 407 | LegalizeResult Status = conversionLibcall( | 
|  | 408 | MI, MIRBuilder, Type::getInt32Ty(Ctx), | 
|  | 409 | FromSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx)); | 
|  | 410 | if (Status != Legalized) | 
|  | 411 | return Status; | 
|  | 412 | break; | 
|  | 413 | } | 
| Diana Picus | 517531e | 2018-01-30 09:15:17 +0000 | [diff] [blame] | 414 | case TargetOpcode::G_SITOFP: | 
|  | 415 | case TargetOpcode::G_UITOFP: { | 
|  | 416 | // FIXME: Support other types | 
|  | 417 | unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); | 
|  | 418 | unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); | 
|  | 419 | if (FromSize != 32 || (ToSize != 32 && ToSize != 64)) | 
|  | 420 | return UnableToLegalize; | 
|  | 421 | LegalizeResult Status = conversionLibcall( | 
|  | 422 | MI, MIRBuilder, | 
|  | 423 | ToSize == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx), | 
|  | 424 | Type::getInt32Ty(Ctx)); | 
|  | 425 | if (Status != Legalized) | 
|  | 426 | return Status; | 
|  | 427 | break; | 
|  | 428 | } | 
| Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 429 | } | 
| Diana Picus | fc1675e | 2017-07-05 12:57:24 +0000 | [diff] [blame] | 430 |  | 
|  | 431 | MI.eraseFromParent(); | 
|  | 432 | return Legalized; | 
| Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 433 | } | 
|  | 434 |  | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 435 | LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI, | 
|  | 436 | unsigned TypeIdx, | 
|  | 437 | LLT NarrowTy) { | 
| Justin Bogner | fde0104 | 2017-01-18 17:29:54 +0000 | [diff] [blame] | 438 | MIRBuilder.setInstr(MI); | 
|  | 439 |  | 
| Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 440 | uint64_t SizeOp0 = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(); | 
|  | 441 | uint64_t NarrowSize = NarrowTy.getSizeInBits(); | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 442 |  | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 443 | switch (MI.getOpcode()) { | 
|  | 444 | default: | 
|  | 445 | return UnableToLegalize; | 
| Tim Northover | ff5e7e1 | 2017-06-30 20:27:36 +0000 | [diff] [blame] | 446 | case TargetOpcode::G_IMPLICIT_DEF: { | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 447 | // FIXME: add support for when SizeOp0 isn't an exact multiple of | 
|  | 448 | // NarrowSize. | 
|  | 449 | if (SizeOp0 % NarrowSize != 0) | 
|  | 450 | return UnableToLegalize; | 
|  | 451 | int NumParts = SizeOp0 / NarrowSize; | 
| Tim Northover | ff5e7e1 | 2017-06-30 20:27:36 +0000 | [diff] [blame] | 452 |  | 
|  | 453 | SmallVector<unsigned, 2> DstRegs; | 
| Volkan Keles | 02bb174 | 2018-02-14 19:58:36 +0000 | [diff] [blame] | 454 | for (int i = 0; i < NumParts; ++i) | 
|  | 455 | DstRegs.push_back( | 
|  | 456 | MIRBuilder.buildUndef(NarrowTy)->getOperand(0).getReg()); | 
| Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 457 |  | 
|  | 458 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 459 | if(MRI.getType(DstReg).isVector()) | 
|  | 460 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 461 | else | 
|  | 462 | MIRBuilder.buildMerge(DstReg, DstRegs); | 
| Tim Northover | ff5e7e1 | 2017-06-30 20:27:36 +0000 | [diff] [blame] | 463 | MI.eraseFromParent(); | 
|  | 464 | return Legalized; | 
|  | 465 | } | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 466 | case TargetOpcode::G_ADD: { | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 467 | // FIXME: add support for when SizeOp0 isn't an exact multiple of | 
|  | 468 | // NarrowSize. | 
|  | 469 | if (SizeOp0 % NarrowSize != 0) | 
|  | 470 | return UnableToLegalize; | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 471 | // Expand in terms of carry-setting/consuming G_ADDE instructions. | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 472 | int NumParts = SizeOp0 / NarrowTy.getSizeInBits(); | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 473 |  | 
| Tim Northover | b18ea16 | 2016-09-20 15:20:36 +0000 | [diff] [blame] | 474 | SmallVector<unsigned, 2> Src1Regs, Src2Regs, DstRegs; | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 475 | extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src1Regs); | 
|  | 476 | extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src2Regs); | 
|  | 477 |  | 
| Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 478 | unsigned CarryIn = MRI.createGenericVirtualRegister(LLT::scalar(1)); | 
|  | 479 | MIRBuilder.buildConstant(CarryIn, 0); | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 480 |  | 
|  | 481 | for (int i = 0; i < NumParts; ++i) { | 
| Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 482 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 483 | unsigned CarryOut = MRI.createGenericVirtualRegister(LLT::scalar(1)); | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 484 |  | 
| Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 485 | MIRBuilder.buildUAdde(DstReg, CarryOut, Src1Regs[i], | 
| Tim Northover | 91c8173 | 2016-08-19 17:17:06 +0000 | [diff] [blame] | 486 | Src2Regs[i], CarryIn); | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 487 |  | 
|  | 488 | DstRegs.push_back(DstReg); | 
|  | 489 | CarryIn = CarryOut; | 
|  | 490 | } | 
| Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 491 | unsigned DstReg = MI.getOperand(0).getReg(); | 
| Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 492 | if(MRI.getType(DstReg).isVector()) | 
|  | 493 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 494 | else | 
|  | 495 | MIRBuilder.buildMerge(DstReg, DstRegs); | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 496 | MI.eraseFromParent(); | 
|  | 497 | return Legalized; | 
|  | 498 | } | 
| Petar Avramovic | 7cecadb | 2019-01-28 12:10:17 +0000 | [diff] [blame] | 499 | case TargetOpcode::G_SUB: { | 
|  | 500 | // FIXME: add support for when SizeOp0 isn't an exact multiple of | 
|  | 501 | // NarrowSize. | 
|  | 502 | if (SizeOp0 % NarrowSize != 0) | 
|  | 503 | return UnableToLegalize; | 
|  | 504 |  | 
|  | 505 | int NumParts = SizeOp0 / NarrowTy.getSizeInBits(); | 
|  | 506 |  | 
|  | 507 | SmallVector<unsigned, 2> Src1Regs, Src2Regs, DstRegs; | 
|  | 508 | extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src1Regs); | 
|  | 509 | extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src2Regs); | 
|  | 510 |  | 
|  | 511 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 512 | unsigned BorrowOut = MRI.createGenericVirtualRegister(LLT::scalar(1)); | 
|  | 513 | MIRBuilder.buildInstr(TargetOpcode::G_USUBO, {DstReg, BorrowOut}, | 
|  | 514 | {Src1Regs[0], Src2Regs[0]}); | 
|  | 515 | DstRegs.push_back(DstReg); | 
|  | 516 | unsigned BorrowIn = BorrowOut; | 
|  | 517 | for (int i = 1; i < NumParts; ++i) { | 
|  | 518 | DstReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 519 | BorrowOut = MRI.createGenericVirtualRegister(LLT::scalar(1)); | 
|  | 520 |  | 
|  | 521 | MIRBuilder.buildInstr(TargetOpcode::G_USUBE, {DstReg, BorrowOut}, | 
|  | 522 | {Src1Regs[i], Src2Regs[i], BorrowIn}); | 
|  | 523 |  | 
|  | 524 | DstRegs.push_back(DstReg); | 
|  | 525 | BorrowIn = BorrowOut; | 
|  | 526 | } | 
|  | 527 | MIRBuilder.buildMerge(MI.getOperand(0).getReg(), DstRegs); | 
|  | 528 | MI.eraseFromParent(); | 
|  | 529 | return Legalized; | 
|  | 530 | } | 
| Matt Arsenault | 211e89d | 2019-01-27 00:52:51 +0000 | [diff] [blame] | 531 | case TargetOpcode::G_MUL: | 
|  | 532 | return narrowScalarMul(MI, TypeIdx, NarrowTy); | 
| Tim Northover | c2d5e6d | 2017-06-26 20:34:13 +0000 | [diff] [blame] | 533 | case TargetOpcode::G_EXTRACT: { | 
|  | 534 | if (TypeIdx != 1) | 
|  | 535 | return UnableToLegalize; | 
|  | 536 |  | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 537 | int64_t SizeOp1 = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); | 
|  | 538 | // FIXME: add support for when SizeOp1 isn't an exact multiple of | 
|  | 539 | // NarrowSize. | 
|  | 540 | if (SizeOp1 % NarrowSize != 0) | 
|  | 541 | return UnableToLegalize; | 
|  | 542 | int NumParts = SizeOp1 / NarrowSize; | 
| Tim Northover | c2d5e6d | 2017-06-26 20:34:13 +0000 | [diff] [blame] | 543 |  | 
|  | 544 | SmallVector<unsigned, 2> SrcRegs, DstRegs; | 
|  | 545 | SmallVector<uint64_t, 2> Indexes; | 
|  | 546 | extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); | 
|  | 547 |  | 
|  | 548 | unsigned OpReg = MI.getOperand(0).getReg(); | 
| Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 549 | uint64_t OpStart = MI.getOperand(2).getImm(); | 
|  | 550 | uint64_t OpSize = MRI.getType(OpReg).getSizeInBits(); | 
| Tim Northover | c2d5e6d | 2017-06-26 20:34:13 +0000 | [diff] [blame] | 551 | for (int i = 0; i < NumParts; ++i) { | 
|  | 552 | unsigned SrcStart = i * NarrowSize; | 
|  | 553 |  | 
|  | 554 | if (SrcStart + NarrowSize <= OpStart || SrcStart >= OpStart + OpSize) { | 
|  | 555 | // No part of the extract uses this subregister, ignore it. | 
|  | 556 | continue; | 
|  | 557 | } else if (SrcStart == OpStart && NarrowTy == MRI.getType(OpReg)) { | 
|  | 558 | // The entire subregister is extracted, forward the value. | 
|  | 559 | DstRegs.push_back(SrcRegs[i]); | 
|  | 560 | continue; | 
|  | 561 | } | 
|  | 562 |  | 
|  | 563 | // OpSegStart is where this destination segment would start in OpReg if it | 
|  | 564 | // extended infinitely in both directions. | 
| Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 565 | int64_t ExtractOffset; | 
|  | 566 | uint64_t SegSize; | 
| Tim Northover | c2d5e6d | 2017-06-26 20:34:13 +0000 | [diff] [blame] | 567 | if (OpStart < SrcStart) { | 
|  | 568 | ExtractOffset = 0; | 
|  | 569 | SegSize = std::min(NarrowSize, OpStart + OpSize - SrcStart); | 
|  | 570 | } else { | 
|  | 571 | ExtractOffset = OpStart - SrcStart; | 
|  | 572 | SegSize = std::min(SrcStart + NarrowSize - OpStart, OpSize); | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | unsigned SegReg = SrcRegs[i]; | 
|  | 576 | if (ExtractOffset != 0 || SegSize != NarrowSize) { | 
|  | 577 | // A genuine extract is needed. | 
|  | 578 | SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize)); | 
|  | 579 | MIRBuilder.buildExtract(SegReg, SrcRegs[i], ExtractOffset); | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | DstRegs.push_back(SegReg); | 
|  | 583 | } | 
|  | 584 |  | 
| Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 585 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 586 | if(MRI.getType(DstReg).isVector()) | 
|  | 587 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 588 | else | 
|  | 589 | MIRBuilder.buildMerge(DstReg, DstRegs); | 
| Tim Northover | c2d5e6d | 2017-06-26 20:34:13 +0000 | [diff] [blame] | 590 | MI.eraseFromParent(); | 
|  | 591 | return Legalized; | 
|  | 592 | } | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 593 | case TargetOpcode::G_INSERT: { | 
| Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 594 | // FIXME: Don't know how to handle secondary types yet. | 
|  | 595 | if (TypeIdx != 0) | 
|  | 596 | return UnableToLegalize; | 
|  | 597 |  | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 598 | // FIXME: add support for when SizeOp0 isn't an exact multiple of | 
|  | 599 | // NarrowSize. | 
|  | 600 | if (SizeOp0 % NarrowSize != 0) | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 601 | return UnableToLegalize; | 
|  | 602 |  | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 603 | int NumParts = SizeOp0 / NarrowSize; | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 604 |  | 
|  | 605 | SmallVector<unsigned, 2> SrcRegs, DstRegs; | 
|  | 606 | SmallVector<uint64_t, 2> Indexes; | 
|  | 607 | extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); | 
|  | 608 |  | 
| Tim Northover | 75e0b91 | 2017-03-06 18:23:04 +0000 | [diff] [blame] | 609 | unsigned OpReg = MI.getOperand(2).getReg(); | 
| Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 610 | uint64_t OpStart = MI.getOperand(3).getImm(); | 
|  | 611 | uint64_t OpSize = MRI.getType(OpReg).getSizeInBits(); | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 612 | for (int i = 0; i < NumParts; ++i) { | 
|  | 613 | unsigned DstStart = i * NarrowSize; | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 614 |  | 
| Tim Northover | 75e0b91 | 2017-03-06 18:23:04 +0000 | [diff] [blame] | 615 | if (DstStart + NarrowSize <= OpStart || DstStart >= OpStart + OpSize) { | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 616 | // No part of the insert affects this subregister, forward the original. | 
|  | 617 | DstRegs.push_back(SrcRegs[i]); | 
|  | 618 | continue; | 
| Tim Northover | 75e0b91 | 2017-03-06 18:23:04 +0000 | [diff] [blame] | 619 | } else if (DstStart == OpStart && NarrowTy == MRI.getType(OpReg)) { | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 620 | // The entire subregister is defined by this insert, forward the new | 
|  | 621 | // value. | 
| Tim Northover | 75e0b91 | 2017-03-06 18:23:04 +0000 | [diff] [blame] | 622 | DstRegs.push_back(OpReg); | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 623 | continue; | 
|  | 624 | } | 
|  | 625 |  | 
| Tim Northover | 2eb18d3 | 2017-03-07 21:24:33 +0000 | [diff] [blame] | 626 | // OpSegStart is where this destination segment would start in OpReg if it | 
|  | 627 | // extended infinitely in both directions. | 
| Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 628 | int64_t ExtractOffset, InsertOffset; | 
|  | 629 | uint64_t SegSize; | 
| Tim Northover | 2eb18d3 | 2017-03-07 21:24:33 +0000 | [diff] [blame] | 630 | if (OpStart < DstStart) { | 
|  | 631 | InsertOffset = 0; | 
|  | 632 | ExtractOffset = DstStart - OpStart; | 
|  | 633 | SegSize = std::min(NarrowSize, OpStart + OpSize - DstStart); | 
|  | 634 | } else { | 
|  | 635 | InsertOffset = OpStart - DstStart; | 
|  | 636 | ExtractOffset = 0; | 
|  | 637 | SegSize = | 
|  | 638 | std::min(NarrowSize - InsertOffset, OpStart + OpSize - DstStart); | 
|  | 639 | } | 
|  | 640 |  | 
|  | 641 | unsigned SegReg = OpReg; | 
|  | 642 | if (ExtractOffset != 0 || SegSize != OpSize) { | 
| Tim Northover | 75e0b91 | 2017-03-06 18:23:04 +0000 | [diff] [blame] | 643 | // A genuine extract is needed. | 
| Tim Northover | 2eb18d3 | 2017-03-07 21:24:33 +0000 | [diff] [blame] | 644 | SegReg = MRI.createGenericVirtualRegister(LLT::scalar(SegSize)); | 
|  | 645 | MIRBuilder.buildExtract(SegReg, OpReg, ExtractOffset); | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 646 | } | 
|  | 647 |  | 
| Tim Northover | 75e0b91 | 2017-03-06 18:23:04 +0000 | [diff] [blame] | 648 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy); | 
| Tim Northover | 2eb18d3 | 2017-03-07 21:24:33 +0000 | [diff] [blame] | 649 | MIRBuilder.buildInsert(DstReg, SrcRegs[i], SegReg, InsertOffset); | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 650 | DstRegs.push_back(DstReg); | 
|  | 651 | } | 
|  | 652 |  | 
|  | 653 | assert(DstRegs.size() == (unsigned)NumParts && "not all parts covered"); | 
| Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 654 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 655 | if(MRI.getType(DstReg).isVector()) | 
|  | 656 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 657 | else | 
|  | 658 | MIRBuilder.buildMerge(DstReg, DstRegs); | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 659 | MI.eraseFromParent(); | 
|  | 660 | return Legalized; | 
|  | 661 | } | 
| Justin Bogner | d09c3ce | 2017-01-19 01:05:48 +0000 | [diff] [blame] | 662 | case TargetOpcode::G_LOAD: { | 
| Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 663 | const auto &MMO = **MI.memoperands_begin(); | 
| Matt Arsenault | 18619af | 2019-01-29 18:13:02 +0000 | [diff] [blame] | 664 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 665 | LLT DstTy = MRI.getType(DstReg); | 
| Matt Arsenault | 7f09fd6 | 2019-02-05 00:26:12 +0000 | [diff] [blame] | 666 | if (DstTy.isVector()) | 
| Matt Arsenault | 045bc9a | 2019-01-30 02:35:38 +0000 | [diff] [blame] | 667 | return UnableToLegalize; | 
| Matt Arsenault | 18619af | 2019-01-29 18:13:02 +0000 | [diff] [blame] | 668 |  | 
|  | 669 | if (8 * MMO.getSize() != DstTy.getSizeInBits()) { | 
|  | 670 | unsigned TmpReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 671 | auto &MMO = **MI.memoperands_begin(); | 
|  | 672 | MIRBuilder.buildLoad(TmpReg, MI.getOperand(1).getReg(), MMO); | 
|  | 673 | MIRBuilder.buildAnyExt(DstReg, TmpReg); | 
|  | 674 | MI.eraseFromParent(); | 
|  | 675 | return Legalized; | 
|  | 676 | } | 
|  | 677 |  | 
| Matt Arsenault | 7f09fd6 | 2019-02-05 00:26:12 +0000 | [diff] [blame] | 678 | return reduceLoadStoreWidth(MI, TypeIdx, NarrowTy); | 
| Justin Bogner | d09c3ce | 2017-01-19 01:05:48 +0000 | [diff] [blame] | 679 | } | 
| Matt Arsenault | 6614f85 | 2019-01-22 19:02:10 +0000 | [diff] [blame] | 680 | case TargetOpcode::G_ZEXTLOAD: | 
|  | 681 | case TargetOpcode::G_SEXTLOAD: { | 
|  | 682 | bool ZExt = MI.getOpcode() == TargetOpcode::G_ZEXTLOAD; | 
|  | 683 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 684 | unsigned PtrReg = MI.getOperand(1).getReg(); | 
|  | 685 |  | 
|  | 686 | unsigned TmpReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 687 | auto &MMO = **MI.memoperands_begin(); | 
|  | 688 | if (MMO.getSize() * 8 == NarrowSize) { | 
|  | 689 | MIRBuilder.buildLoad(TmpReg, PtrReg, MMO); | 
|  | 690 | } else { | 
|  | 691 | unsigned ExtLoad = ZExt ? TargetOpcode::G_ZEXTLOAD | 
|  | 692 | : TargetOpcode::G_SEXTLOAD; | 
|  | 693 | MIRBuilder.buildInstr(ExtLoad) | 
|  | 694 | .addDef(TmpReg) | 
|  | 695 | .addUse(PtrReg) | 
|  | 696 | .addMemOperand(&MMO); | 
|  | 697 | } | 
|  | 698 |  | 
|  | 699 | if (ZExt) | 
|  | 700 | MIRBuilder.buildZExt(DstReg, TmpReg); | 
|  | 701 | else | 
|  | 702 | MIRBuilder.buildSExt(DstReg, TmpReg); | 
|  | 703 |  | 
|  | 704 | MI.eraseFromParent(); | 
|  | 705 | return Legalized; | 
|  | 706 | } | 
| Justin Bogner | fde0104 | 2017-01-18 17:29:54 +0000 | [diff] [blame] | 707 | case TargetOpcode::G_STORE: { | 
| Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 708 | const auto &MMO = **MI.memoperands_begin(); | 
| Matt Arsenault | 18619af | 2019-01-29 18:13:02 +0000 | [diff] [blame] | 709 |  | 
|  | 710 | unsigned SrcReg = MI.getOperand(0).getReg(); | 
|  | 711 | LLT SrcTy = MRI.getType(SrcReg); | 
| Matt Arsenault | 7f09fd6 | 2019-02-05 00:26:12 +0000 | [diff] [blame] | 712 | if (SrcTy.isVector()) | 
|  | 713 | return UnableToLegalize; | 
|  | 714 |  | 
|  | 715 | int NumParts = SizeOp0 / NarrowSize; | 
|  | 716 | unsigned HandledSize = NumParts * NarrowTy.getSizeInBits(); | 
|  | 717 | unsigned LeftoverBits = SrcTy.getSizeInBits() - HandledSize; | 
|  | 718 | if (SrcTy.isVector() && LeftoverBits != 0) | 
|  | 719 | return UnableToLegalize; | 
| Matt Arsenault | 18619af | 2019-01-29 18:13:02 +0000 | [diff] [blame] | 720 |  | 
|  | 721 | if (8 * MMO.getSize() != SrcTy.getSizeInBits()) { | 
|  | 722 | unsigned TmpReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 723 | auto &MMO = **MI.memoperands_begin(); | 
|  | 724 | MIRBuilder.buildTrunc(TmpReg, SrcReg); | 
|  | 725 | MIRBuilder.buildStore(TmpReg, MI.getOperand(1).getReg(), MMO); | 
|  | 726 | MI.eraseFromParent(); | 
|  | 727 | return Legalized; | 
|  | 728 | } | 
|  | 729 |  | 
| Matt Arsenault | 7f09fd6 | 2019-02-05 00:26:12 +0000 | [diff] [blame] | 730 | return reduceLoadStoreWidth(MI, 0, NarrowTy); | 
| Justin Bogner | fde0104 | 2017-01-18 17:29:54 +0000 | [diff] [blame] | 731 | } | 
| Igor Breger | 2953788 | 2017-04-07 14:41:59 +0000 | [diff] [blame] | 732 | case TargetOpcode::G_CONSTANT: { | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 733 | // FIXME: add support for when SizeOp0 isn't an exact multiple of | 
|  | 734 | // NarrowSize. | 
|  | 735 | if (SizeOp0 % NarrowSize != 0) | 
|  | 736 | return UnableToLegalize; | 
|  | 737 | int NumParts = SizeOp0 / NarrowSize; | 
| Igor Breger | 2953788 | 2017-04-07 14:41:59 +0000 | [diff] [blame] | 738 | const APInt &Cst = MI.getOperand(1).getCImm()->getValue(); | 
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 739 | LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); | 
| Igor Breger | 2953788 | 2017-04-07 14:41:59 +0000 | [diff] [blame] | 740 |  | 
|  | 741 | SmallVector<unsigned, 2> DstRegs; | 
|  | 742 | for (int i = 0; i < NumParts; ++i) { | 
|  | 743 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 744 | ConstantInt *CI = | 
|  | 745 | ConstantInt::get(Ctx, Cst.lshr(NarrowSize * i).trunc(NarrowSize)); | 
|  | 746 | MIRBuilder.buildConstant(DstReg, *CI); | 
|  | 747 | DstRegs.push_back(DstReg); | 
|  | 748 | } | 
|  | 749 | unsigned DstReg = MI.getOperand(0).getReg(); | 
| Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 750 | if(MRI.getType(DstReg).isVector()) | 
|  | 751 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 752 | else | 
|  | 753 | MIRBuilder.buildMerge(DstReg, DstRegs); | 
| Igor Breger | 2953788 | 2017-04-07 14:41:59 +0000 | [diff] [blame] | 754 | MI.eraseFromParent(); | 
|  | 755 | return Legalized; | 
|  | 756 | } | 
| Matt Arsenault | 81511e5 | 2019-02-05 00:13:44 +0000 | [diff] [blame] | 757 | case TargetOpcode::G_SELECT: | 
|  | 758 | return narrowScalarSelect(MI, TypeIdx, NarrowTy); | 
| Petar Avramovic | 150fd43 | 2018-12-18 11:36:14 +0000 | [diff] [blame] | 759 | case TargetOpcode::G_AND: | 
|  | 760 | case TargetOpcode::G_OR: | 
|  | 761 | case TargetOpcode::G_XOR: { | 
| Quentin Colombet | c2f3cea | 2017-10-03 04:53:56 +0000 | [diff] [blame] | 762 | // Legalize bitwise operation: | 
|  | 763 | // A = BinOp<Ty> B, C | 
|  | 764 | // into: | 
|  | 765 | // B1, ..., BN = G_UNMERGE_VALUES B | 
|  | 766 | // C1, ..., CN = G_UNMERGE_VALUES C | 
|  | 767 | // A1 = BinOp<Ty/N> B1, C2 | 
|  | 768 | // ... | 
|  | 769 | // AN = BinOp<Ty/N> BN, CN | 
|  | 770 | // A = G_MERGE_VALUES A1, ..., AN | 
| Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 771 |  | 
|  | 772 | // FIXME: add support for when SizeOp0 isn't an exact multiple of | 
|  | 773 | // NarrowSize. | 
|  | 774 | if (SizeOp0 % NarrowSize != 0) | 
|  | 775 | return UnableToLegalize; | 
|  | 776 | int NumParts = SizeOp0 / NarrowSize; | 
| Quentin Colombet | c2f3cea | 2017-10-03 04:53:56 +0000 | [diff] [blame] | 777 |  | 
|  | 778 | // List the registers where the destination will be scattered. | 
|  | 779 | SmallVector<unsigned, 2> DstRegs; | 
|  | 780 | // List the registers where the first argument will be split. | 
|  | 781 | SmallVector<unsigned, 2> SrcsReg1; | 
|  | 782 | // List the registers where the second argument will be split. | 
|  | 783 | SmallVector<unsigned, 2> SrcsReg2; | 
|  | 784 | // Create all the temporary registers. | 
|  | 785 | for (int i = 0; i < NumParts; ++i) { | 
|  | 786 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 787 | unsigned SrcReg1 = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 788 | unsigned SrcReg2 = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 789 |  | 
|  | 790 | DstRegs.push_back(DstReg); | 
|  | 791 | SrcsReg1.push_back(SrcReg1); | 
|  | 792 | SrcsReg2.push_back(SrcReg2); | 
|  | 793 | } | 
|  | 794 | // Explode the big arguments into smaller chunks. | 
|  | 795 | MIRBuilder.buildUnmerge(SrcsReg1, MI.getOperand(1).getReg()); | 
|  | 796 | MIRBuilder.buildUnmerge(SrcsReg2, MI.getOperand(2).getReg()); | 
|  | 797 |  | 
|  | 798 | // Do the operation on each small part. | 
|  | 799 | for (int i = 0; i < NumParts; ++i) | 
| Petar Avramovic | 150fd43 | 2018-12-18 11:36:14 +0000 | [diff] [blame] | 800 | MIRBuilder.buildInstr(MI.getOpcode(), {DstRegs[i]}, | 
|  | 801 | {SrcsReg1[i], SrcsReg2[i]}); | 
| Quentin Colombet | c2f3cea | 2017-10-03 04:53:56 +0000 | [diff] [blame] | 802 |  | 
|  | 803 | // Gather the destination registers into the final destination. | 
|  | 804 | unsigned DstReg = MI.getOperand(0).getReg(); | 
| Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 805 | if(MRI.getType(DstReg).isVector()) | 
|  | 806 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 807 | else | 
|  | 808 | MIRBuilder.buildMerge(DstReg, DstRegs); | 
| Quentin Colombet | c2f3cea | 2017-10-03 04:53:56 +0000 | [diff] [blame] | 809 | MI.eraseFromParent(); | 
|  | 810 | return Legalized; | 
|  | 811 | } | 
| Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 812 | case TargetOpcode::G_SHL: | 
|  | 813 | case TargetOpcode::G_LSHR: | 
| Matt Arsenault | fbec8fe | 2019-02-07 19:37:44 +0000 | [diff] [blame] | 814 | case TargetOpcode::G_ASHR: | 
|  | 815 | return narrowScalarShift(MI, TypeIdx, NarrowTy); | 
| Matt Arsenault | d5684f7 | 2019-01-31 02:09:57 +0000 | [diff] [blame] | 816 | case TargetOpcode::G_CTLZ: | 
|  | 817 | case TargetOpcode::G_CTLZ_ZERO_UNDEF: | 
|  | 818 | case TargetOpcode::G_CTTZ: | 
|  | 819 | case TargetOpcode::G_CTTZ_ZERO_UNDEF: | 
|  | 820 | case TargetOpcode::G_CTPOP: | 
|  | 821 | if (TypeIdx != 0) | 
|  | 822 | return UnableToLegalize; // TODO | 
|  | 823 |  | 
|  | 824 | Observer.changingInstr(MI); | 
|  | 825 | narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT); | 
|  | 826 | Observer.changedInstr(MI); | 
|  | 827 | return Legalized; | 
| Matt Arsenault | cbaada6 | 2019-02-02 23:29:55 +0000 | [diff] [blame] | 828 | case TargetOpcode::G_INTTOPTR: | 
|  | 829 | if (TypeIdx != 1) | 
|  | 830 | return UnableToLegalize; | 
|  | 831 |  | 
|  | 832 | Observer.changingInstr(MI); | 
|  | 833 | narrowScalarSrc(MI, NarrowTy, 1); | 
|  | 834 | Observer.changedInstr(MI); | 
|  | 835 | return Legalized; | 
|  | 836 | case TargetOpcode::G_PTRTOINT: | 
|  | 837 | if (TypeIdx != 0) | 
|  | 838 | return UnableToLegalize; | 
|  | 839 |  | 
|  | 840 | Observer.changingInstr(MI); | 
|  | 841 | narrowScalarDst(MI, NarrowTy, 0, TargetOpcode::G_ZEXT); | 
|  | 842 | Observer.changedInstr(MI); | 
|  | 843 | return Legalized; | 
| Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 844 | } | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 845 | } | 
|  | 846 |  | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 847 | void LegalizerHelper::widenScalarSrc(MachineInstr &MI, LLT WideTy, | 
|  | 848 | unsigned OpIdx, unsigned ExtOpcode) { | 
|  | 849 | MachineOperand &MO = MI.getOperand(OpIdx); | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 850 | auto ExtB = MIRBuilder.buildInstr(ExtOpcode, {WideTy}, {MO.getReg()}); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 851 | MO.setReg(ExtB->getOperand(0).getReg()); | 
|  | 852 | } | 
|  | 853 |  | 
| Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 854 | void LegalizerHelper::narrowScalarSrc(MachineInstr &MI, LLT NarrowTy, | 
|  | 855 | unsigned OpIdx) { | 
|  | 856 | MachineOperand &MO = MI.getOperand(OpIdx); | 
|  | 857 | auto ExtB = MIRBuilder.buildInstr(TargetOpcode::G_TRUNC, {NarrowTy}, | 
|  | 858 | {MO.getReg()}); | 
|  | 859 | MO.setReg(ExtB->getOperand(0).getReg()); | 
|  | 860 | } | 
|  | 861 |  | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 862 | void LegalizerHelper::widenScalarDst(MachineInstr &MI, LLT WideTy, | 
|  | 863 | unsigned OpIdx, unsigned TruncOpcode) { | 
|  | 864 | MachineOperand &MO = MI.getOperand(OpIdx); | 
|  | 865 | unsigned DstExt = MRI.createGenericVirtualRegister(WideTy); | 
|  | 866 | MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 867 | MIRBuilder.buildInstr(TruncOpcode, {MO.getReg()}, {DstExt}); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 868 | MO.setReg(DstExt); | 
|  | 869 | } | 
|  | 870 |  | 
| Matt Arsenault | d5684f7 | 2019-01-31 02:09:57 +0000 | [diff] [blame] | 871 | void LegalizerHelper::narrowScalarDst(MachineInstr &MI, LLT NarrowTy, | 
|  | 872 | unsigned OpIdx, unsigned ExtOpcode) { | 
|  | 873 | MachineOperand &MO = MI.getOperand(OpIdx); | 
|  | 874 | unsigned DstTrunc = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 875 | MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); | 
|  | 876 | MIRBuilder.buildInstr(ExtOpcode, {MO.getReg()}, {DstTrunc}); | 
|  | 877 | MO.setReg(DstTrunc); | 
|  | 878 | } | 
|  | 879 |  | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 880 | LegalizerHelper::LegalizeResult | 
| Matt Arsenault | 888aa5d | 2019-02-03 00:07:33 +0000 | [diff] [blame] | 881 | LegalizerHelper::widenScalarMergeValues(MachineInstr &MI, unsigned TypeIdx, | 
|  | 882 | LLT WideTy) { | 
|  | 883 | if (TypeIdx != 1) | 
|  | 884 | return UnableToLegalize; | 
|  | 885 |  | 
|  | 886 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 887 | LLT DstTy = MRI.getType(DstReg); | 
|  | 888 | if (!DstTy.isScalar()) | 
|  | 889 | return UnableToLegalize; | 
|  | 890 |  | 
|  | 891 | unsigned NumOps = MI.getNumOperands(); | 
|  | 892 | unsigned NumSrc = MI.getNumOperands() - 1; | 
|  | 893 | unsigned PartSize = DstTy.getSizeInBits() / NumSrc; | 
|  | 894 |  | 
|  | 895 | unsigned Src1 = MI.getOperand(1).getReg(); | 
|  | 896 | unsigned ResultReg = MIRBuilder.buildZExt(DstTy, Src1)->getOperand(0).getReg(); | 
|  | 897 |  | 
|  | 898 | for (unsigned I = 2; I != NumOps; ++I) { | 
|  | 899 | const unsigned Offset = (I - 1) * PartSize; | 
|  | 900 |  | 
|  | 901 | unsigned SrcReg = MI.getOperand(I).getReg(); | 
|  | 902 | assert(MRI.getType(SrcReg) == LLT::scalar(PartSize)); | 
|  | 903 |  | 
|  | 904 | auto ZextInput = MIRBuilder.buildZExt(DstTy, SrcReg); | 
|  | 905 |  | 
|  | 906 | unsigned NextResult = I + 1 == NumOps ? DstReg : | 
|  | 907 | MRI.createGenericVirtualRegister(DstTy); | 
|  | 908 |  | 
|  | 909 | auto ShiftAmt = MIRBuilder.buildConstant(DstTy, Offset); | 
|  | 910 | auto Shl = MIRBuilder.buildShl(DstTy, ZextInput, ShiftAmt); | 
|  | 911 | MIRBuilder.buildOr(NextResult, ResultReg, Shl); | 
|  | 912 | ResultReg = NextResult; | 
|  | 913 | } | 
|  | 914 |  | 
|  | 915 | MI.eraseFromParent(); | 
|  | 916 | return Legalized; | 
|  | 917 | } | 
|  | 918 |  | 
|  | 919 | LegalizerHelper::LegalizeResult | 
|  | 920 | LegalizerHelper::widenScalarUnmergeValues(MachineInstr &MI, unsigned TypeIdx, | 
|  | 921 | LLT WideTy) { | 
|  | 922 | if (TypeIdx != 0) | 
|  | 923 | return UnableToLegalize; | 
|  | 924 |  | 
|  | 925 | unsigned NumDst = MI.getNumOperands() - 1; | 
|  | 926 | unsigned SrcReg = MI.getOperand(NumDst).getReg(); | 
|  | 927 | LLT SrcTy = MRI.getType(SrcReg); | 
|  | 928 | if (!SrcTy.isScalar()) | 
|  | 929 | return UnableToLegalize; | 
|  | 930 |  | 
|  | 931 | unsigned Dst0Reg = MI.getOperand(0).getReg(); | 
|  | 932 | LLT DstTy = MRI.getType(Dst0Reg); | 
|  | 933 | if (!DstTy.isScalar()) | 
|  | 934 | return UnableToLegalize; | 
|  | 935 |  | 
|  | 936 | unsigned NewSrcSize = NumDst * WideTy.getSizeInBits(); | 
|  | 937 | LLT NewSrcTy = LLT::scalar(NewSrcSize); | 
|  | 938 | unsigned SizeDiff = WideTy.getSizeInBits() - DstTy.getSizeInBits(); | 
|  | 939 |  | 
|  | 940 | auto WideSrc = MIRBuilder.buildZExt(NewSrcTy, SrcReg); | 
|  | 941 |  | 
|  | 942 | for (unsigned I = 1; I != NumDst; ++I) { | 
|  | 943 | auto ShiftAmt = MIRBuilder.buildConstant(NewSrcTy, SizeDiff * I); | 
|  | 944 | auto Shl = MIRBuilder.buildShl(NewSrcTy, WideSrc, ShiftAmt); | 
|  | 945 | WideSrc = MIRBuilder.buildOr(NewSrcTy, WideSrc, Shl); | 
|  | 946 | } | 
|  | 947 |  | 
|  | 948 | Observer.changingInstr(MI); | 
|  | 949 |  | 
|  | 950 | MI.getOperand(NumDst).setReg(WideSrc->getOperand(0).getReg()); | 
|  | 951 | for (unsigned I = 0; I != NumDst; ++I) | 
|  | 952 | widenScalarDst(MI, WideTy, I); | 
|  | 953 |  | 
|  | 954 | Observer.changedInstr(MI); | 
|  | 955 |  | 
|  | 956 | return Legalized; | 
|  | 957 | } | 
|  | 958 |  | 
|  | 959 | LegalizerHelper::LegalizeResult | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 960 | LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) { | 
| Tim Northover | 3c73e36 | 2016-08-23 18:20:09 +0000 | [diff] [blame] | 961 | MIRBuilder.setInstr(MI); | 
|  | 962 |  | 
| Tim Northover | 3233581 | 2016-08-04 18:35:11 +0000 | [diff] [blame] | 963 | switch (MI.getOpcode()) { | 
|  | 964 | default: | 
|  | 965 | return UnableToLegalize; | 
| Matt Arsenault | 0e5d856 | 2019-02-02 23:56:00 +0000 | [diff] [blame] | 966 | case TargetOpcode::G_EXTRACT: { | 
|  | 967 | if (TypeIdx != 1) | 
|  | 968 | return UnableToLegalize; | 
|  | 969 |  | 
|  | 970 | unsigned SrcReg = MI.getOperand(1).getReg(); | 
|  | 971 | LLT SrcTy = MRI.getType(SrcReg); | 
|  | 972 | if (!SrcTy.isVector()) | 
|  | 973 | return UnableToLegalize; | 
|  | 974 |  | 
|  | 975 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 976 | LLT DstTy = MRI.getType(DstReg); | 
|  | 977 | if (DstTy != SrcTy.getElementType()) | 
|  | 978 | return UnableToLegalize; | 
|  | 979 |  | 
|  | 980 | unsigned Offset = MI.getOperand(2).getImm(); | 
|  | 981 | if (Offset % SrcTy.getScalarSizeInBits() != 0) | 
|  | 982 | return UnableToLegalize; | 
|  | 983 |  | 
|  | 984 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); | 
|  | 985 |  | 
|  | 986 | MI.getOperand(2).setImm((WideTy.getSizeInBits() / SrcTy.getSizeInBits()) * | 
|  | 987 | Offset); | 
|  | 988 | widenScalarDst(MI, WideTy.getScalarType(), 0); | 
|  | 989 |  | 
|  | 990 | return Legalized; | 
|  | 991 | } | 
| Matt Arsenault | 888aa5d | 2019-02-03 00:07:33 +0000 | [diff] [blame] | 992 | case TargetOpcode::G_MERGE_VALUES: | 
|  | 993 | return widenScalarMergeValues(MI, TypeIdx, WideTy); | 
|  | 994 | case TargetOpcode::G_UNMERGE_VALUES: | 
|  | 995 | return widenScalarUnmergeValues(MI, TypeIdx, WideTy); | 
| Aditya Nandakumar | 6d47a41 | 2018-08-29 03:17:08 +0000 | [diff] [blame] | 996 | case TargetOpcode::G_UADDO: | 
|  | 997 | case TargetOpcode::G_USUBO: { | 
|  | 998 | if (TypeIdx == 1) | 
|  | 999 | return UnableToLegalize; // TODO | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 1000 | auto LHSZext = MIRBuilder.buildInstr(TargetOpcode::G_ZEXT, {WideTy}, | 
|  | 1001 | {MI.getOperand(2).getReg()}); | 
|  | 1002 | auto RHSZext = MIRBuilder.buildInstr(TargetOpcode::G_ZEXT, {WideTy}, | 
|  | 1003 | {MI.getOperand(3).getReg()}); | 
| Aditya Nandakumar | 6d47a41 | 2018-08-29 03:17:08 +0000 | [diff] [blame] | 1004 | unsigned Opcode = MI.getOpcode() == TargetOpcode::G_UADDO | 
|  | 1005 | ? TargetOpcode::G_ADD | 
|  | 1006 | : TargetOpcode::G_SUB; | 
|  | 1007 | // Do the arithmetic in the larger type. | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 1008 | auto NewOp = MIRBuilder.buildInstr(Opcode, {WideTy}, {LHSZext, RHSZext}); | 
| Aditya Nandakumar | 6d47a41 | 2018-08-29 03:17:08 +0000 | [diff] [blame] | 1009 | LLT OrigTy = MRI.getType(MI.getOperand(0).getReg()); | 
|  | 1010 | APInt Mask = APInt::getAllOnesValue(OrigTy.getSizeInBits()); | 
|  | 1011 | auto AndOp = MIRBuilder.buildInstr( | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 1012 | TargetOpcode::G_AND, {WideTy}, | 
|  | 1013 | {NewOp, MIRBuilder.buildConstant(WideTy, Mask.getZExtValue())}); | 
| Aditya Nandakumar | 6d47a41 | 2018-08-29 03:17:08 +0000 | [diff] [blame] | 1014 | // There is no overflow if the AndOp is the same as NewOp. | 
|  | 1015 | MIRBuilder.buildICmp(CmpInst::ICMP_NE, MI.getOperand(1).getReg(), NewOp, | 
|  | 1016 | AndOp); | 
|  | 1017 | // Now trunc the NewOp to the original result. | 
|  | 1018 | MIRBuilder.buildTrunc(MI.getOperand(0).getReg(), NewOp); | 
|  | 1019 | MI.eraseFromParent(); | 
|  | 1020 | return Legalized; | 
|  | 1021 | } | 
| Aditya Nandakumar | c106183 | 2018-08-22 17:59:18 +0000 | [diff] [blame] | 1022 | case TargetOpcode::G_CTTZ: | 
|  | 1023 | case TargetOpcode::G_CTTZ_ZERO_UNDEF: | 
|  | 1024 | case TargetOpcode::G_CTLZ: | 
|  | 1025 | case TargetOpcode::G_CTLZ_ZERO_UNDEF: | 
|  | 1026 | case TargetOpcode::G_CTPOP: { | 
| Matt Arsenault | d5684f7 | 2019-01-31 02:09:57 +0000 | [diff] [blame] | 1027 | if (TypeIdx == 0) { | 
| Matt Arsenault | 3d6a49b | 2019-02-04 22:26:33 +0000 | [diff] [blame] | 1028 | Observer.changingInstr(MI); | 
| Matt Arsenault | d5684f7 | 2019-01-31 02:09:57 +0000 | [diff] [blame] | 1029 | widenScalarDst(MI, WideTy, 0); | 
| Matt Arsenault | 3d6a49b | 2019-02-04 22:26:33 +0000 | [diff] [blame] | 1030 | Observer.changedInstr(MI); | 
| Matt Arsenault | d5684f7 | 2019-01-31 02:09:57 +0000 | [diff] [blame] | 1031 | return Legalized; | 
|  | 1032 | } | 
|  | 1033 |  | 
| Matt Arsenault | 3d6a49b | 2019-02-04 22:26:33 +0000 | [diff] [blame] | 1034 | unsigned SrcReg = MI.getOperand(1).getReg(); | 
|  | 1035 |  | 
| Aditya Nandakumar | c106183 | 2018-08-22 17:59:18 +0000 | [diff] [blame] | 1036 | // First ZEXT the input. | 
| Matt Arsenault | 3d6a49b | 2019-02-04 22:26:33 +0000 | [diff] [blame] | 1037 | auto MIBSrc = MIRBuilder.buildZExt(WideTy, SrcReg); | 
|  | 1038 | LLT CurTy = MRI.getType(SrcReg); | 
| Aditya Nandakumar | c106183 | 2018-08-22 17:59:18 +0000 | [diff] [blame] | 1039 | if (MI.getOpcode() == TargetOpcode::G_CTTZ) { | 
|  | 1040 | // The count is the same in the larger type except if the original | 
|  | 1041 | // value was zero.  This can be handled by setting the bit just off | 
|  | 1042 | // the top of the original type. | 
|  | 1043 | auto TopBit = | 
|  | 1044 | APInt::getOneBitSet(WideTy.getSizeInBits(), CurTy.getSizeInBits()); | 
| Matt Arsenault | 3d6a49b | 2019-02-04 22:26:33 +0000 | [diff] [blame] | 1045 | MIBSrc = MIRBuilder.buildOr( | 
|  | 1046 | WideTy, MIBSrc, MIRBuilder.buildConstant(WideTy, TopBit)); | 
| Aditya Nandakumar | c106183 | 2018-08-22 17:59:18 +0000 | [diff] [blame] | 1047 | } | 
| Matt Arsenault | 3d6a49b | 2019-02-04 22:26:33 +0000 | [diff] [blame] | 1048 |  | 
| Aditya Nandakumar | c106183 | 2018-08-22 17:59:18 +0000 | [diff] [blame] | 1049 | // Perform the operation at the larger size. | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 1050 | auto MIBNewOp = MIRBuilder.buildInstr(MI.getOpcode(), {WideTy}, {MIBSrc}); | 
| Aditya Nandakumar | c106183 | 2018-08-22 17:59:18 +0000 | [diff] [blame] | 1051 | // This is already the correct result for CTPOP and CTTZs | 
|  | 1052 | if (MI.getOpcode() == TargetOpcode::G_CTLZ || | 
|  | 1053 | MI.getOpcode() == TargetOpcode::G_CTLZ_ZERO_UNDEF) { | 
|  | 1054 | // The correct result is NewOp - (Difference in widety and current ty). | 
|  | 1055 | unsigned SizeDiff = WideTy.getSizeInBits() - CurTy.getSizeInBits(); | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 1056 | MIBNewOp = MIRBuilder.buildInstr( | 
|  | 1057 | TargetOpcode::G_SUB, {WideTy}, | 
|  | 1058 | {MIBNewOp, MIRBuilder.buildConstant(WideTy, SizeDiff)}); | 
| Aditya Nandakumar | c106183 | 2018-08-22 17:59:18 +0000 | [diff] [blame] | 1059 | } | 
| Matt Arsenault | 3d6a49b | 2019-02-04 22:26:33 +0000 | [diff] [blame] | 1060 |  | 
|  | 1061 | MIRBuilder.buildZExtOrTrunc(MI.getOperand(0), MIBNewOp); | 
|  | 1062 | MI.eraseFromParent(); | 
| Aditya Nandakumar | c106183 | 2018-08-22 17:59:18 +0000 | [diff] [blame] | 1063 | return Legalized; | 
|  | 1064 | } | 
| Matt Arsenault | d1bfc8d | 2019-01-31 02:34:03 +0000 | [diff] [blame] | 1065 | case TargetOpcode::G_BSWAP: { | 
|  | 1066 | Observer.changingInstr(MI); | 
|  | 1067 | unsigned DstReg = MI.getOperand(0).getReg(); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1068 |  | 
| Matt Arsenault | d1bfc8d | 2019-01-31 02:34:03 +0000 | [diff] [blame] | 1069 | unsigned ShrReg = MRI.createGenericVirtualRegister(WideTy); | 
|  | 1070 | unsigned DstExt = MRI.createGenericVirtualRegister(WideTy); | 
|  | 1071 | unsigned ShiftAmtReg = MRI.createGenericVirtualRegister(WideTy); | 
|  | 1072 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); | 
|  | 1073 |  | 
|  | 1074 | MI.getOperand(0).setReg(DstExt); | 
|  | 1075 |  | 
|  | 1076 | MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt()); | 
|  | 1077 |  | 
|  | 1078 | LLT Ty = MRI.getType(DstReg); | 
|  | 1079 | unsigned DiffBits = WideTy.getScalarSizeInBits() - Ty.getScalarSizeInBits(); | 
|  | 1080 | MIRBuilder.buildConstant(ShiftAmtReg, DiffBits); | 
|  | 1081 | MIRBuilder.buildInstr(TargetOpcode::G_LSHR) | 
|  | 1082 | .addDef(ShrReg) | 
|  | 1083 | .addUse(DstExt) | 
|  | 1084 | .addUse(ShiftAmtReg); | 
|  | 1085 |  | 
|  | 1086 | MIRBuilder.buildTrunc(DstReg, ShrReg); | 
|  | 1087 | Observer.changedInstr(MI); | 
|  | 1088 | return Legalized; | 
|  | 1089 | } | 
| Tim Northover | 61c1614 | 2016-08-04 21:39:49 +0000 | [diff] [blame] | 1090 | case TargetOpcode::G_ADD: | 
|  | 1091 | case TargetOpcode::G_AND: | 
|  | 1092 | case TargetOpcode::G_MUL: | 
|  | 1093 | case TargetOpcode::G_OR: | 
|  | 1094 | case TargetOpcode::G_XOR: | 
| Justin Bogner | ddb80ae | 2017-01-19 07:51:17 +0000 | [diff] [blame] | 1095 | case TargetOpcode::G_SUB: | 
| Tim Northover | 3233581 | 2016-08-04 18:35:11 +0000 | [diff] [blame] | 1096 | // Perform operation at larger width (any extension is fine here, high bits | 
|  | 1097 | // don't affect the result) and then truncate the result back to the | 
|  | 1098 | // original type. | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1099 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1100 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); | 
|  | 1101 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); | 
|  | 1102 | widenScalarDst(MI, WideTy); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1103 | Observer.changedInstr(MI); | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1104 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1105 |  | 
| Roman Tereshin | 6d26638 | 2018-05-09 21:43:30 +0000 | [diff] [blame] | 1106 | case TargetOpcode::G_SHL: | 
| Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 1107 | Observer.changingInstr(MI); | 
|  | 1108 |  | 
|  | 1109 | if (TypeIdx == 0) { | 
|  | 1110 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); | 
|  | 1111 | widenScalarDst(MI, WideTy); | 
|  | 1112 | } else { | 
|  | 1113 | assert(TypeIdx == 1); | 
|  | 1114 | // The "number of bits to shift" operand must preserve its value as an | 
|  | 1115 | // unsigned integer: | 
|  | 1116 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); | 
|  | 1117 | } | 
|  | 1118 |  | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1119 | Observer.changedInstr(MI); | 
| Roman Tereshin | 6d26638 | 2018-05-09 21:43:30 +0000 | [diff] [blame] | 1120 | return Legalized; | 
|  | 1121 |  | 
| Tim Northover | 7a753d9 | 2016-08-26 17:46:06 +0000 | [diff] [blame] | 1122 | case TargetOpcode::G_SDIV: | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1123 | case TargetOpcode::G_SREM: | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1124 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1125 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); | 
|  | 1126 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); | 
|  | 1127 | widenScalarDst(MI, WideTy); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1128 | Observer.changedInstr(MI); | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1129 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1130 |  | 
| Roman Tereshin | 6d26638 | 2018-05-09 21:43:30 +0000 | [diff] [blame] | 1131 | case TargetOpcode::G_ASHR: | 
| Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 1132 | case TargetOpcode::G_LSHR: | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1133 | Observer.changingInstr(MI); | 
| Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 1134 |  | 
|  | 1135 | if (TypeIdx == 0) { | 
|  | 1136 | unsigned CvtOp = MI.getOpcode() == TargetOpcode::G_ASHR ? | 
|  | 1137 | TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT; | 
|  | 1138 |  | 
|  | 1139 | widenScalarSrc(MI, WideTy, 1, CvtOp); | 
|  | 1140 | widenScalarDst(MI, WideTy); | 
|  | 1141 | } else { | 
|  | 1142 | assert(TypeIdx == 1); | 
|  | 1143 | // The "number of bits to shift" operand must preserve its value as an | 
|  | 1144 | // unsigned integer: | 
|  | 1145 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); | 
|  | 1146 | } | 
|  | 1147 |  | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1148 | Observer.changedInstr(MI); | 
| Roman Tereshin | 6d26638 | 2018-05-09 21:43:30 +0000 | [diff] [blame] | 1149 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1150 | case TargetOpcode::G_UDIV: | 
|  | 1151 | case TargetOpcode::G_UREM: | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1152 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1153 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); | 
|  | 1154 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ZEXT); | 
|  | 1155 | widenScalarDst(MI, WideTy); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1156 | Observer.changedInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1157 | return Legalized; | 
|  | 1158 |  | 
|  | 1159 | case TargetOpcode::G_SELECT: | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1160 | Observer.changingInstr(MI); | 
| Petar Avramovic | 09dff33 | 2018-12-25 14:42:30 +0000 | [diff] [blame] | 1161 | if (TypeIdx == 0) { | 
|  | 1162 | // Perform operation at larger width (any extension is fine here, high | 
|  | 1163 | // bits don't affect the result) and then truncate the result back to the | 
|  | 1164 | // original type. | 
|  | 1165 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_ANYEXT); | 
|  | 1166 | widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_ANYEXT); | 
|  | 1167 | widenScalarDst(MI, WideTy); | 
|  | 1168 | } else { | 
| Matt Arsenault | 6d8e1b4 | 2019-01-30 02:57:43 +0000 | [diff] [blame] | 1169 | bool IsVec = MRI.getType(MI.getOperand(1).getReg()).isVector(); | 
| Petar Avramovic | 09dff33 | 2018-12-25 14:42:30 +0000 | [diff] [blame] | 1170 | // Explicit extension is required here since high bits affect the result. | 
| Matt Arsenault | 6d8e1b4 | 2019-01-30 02:57:43 +0000 | [diff] [blame] | 1171 | widenScalarSrc(MI, WideTy, 1, MIRBuilder.getBoolExtOp(IsVec, false)); | 
| Petar Avramovic | 09dff33 | 2018-12-25 14:42:30 +0000 | [diff] [blame] | 1172 | } | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1173 | Observer.changedInstr(MI); | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1174 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1175 |  | 
| Ahmed Bougacha | b613706 | 2017-01-23 21:10:14 +0000 | [diff] [blame] | 1176 | case TargetOpcode::G_FPTOSI: | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1177 | case TargetOpcode::G_FPTOUI: | 
| Ahmed Bougacha | b613706 | 2017-01-23 21:10:14 +0000 | [diff] [blame] | 1178 | if (TypeIdx != 0) | 
|  | 1179 | return UnableToLegalize; | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1180 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1181 | widenScalarDst(MI, WideTy); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1182 | Observer.changedInstr(MI); | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1183 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1184 |  | 
| Ahmed Bougacha | d294823 | 2017-01-20 01:37:24 +0000 | [diff] [blame] | 1185 | case TargetOpcode::G_SITOFP: | 
| Ahmed Bougacha | d294823 | 2017-01-20 01:37:24 +0000 | [diff] [blame] | 1186 | if (TypeIdx != 1) | 
|  | 1187 | return UnableToLegalize; | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1188 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1189 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_SEXT); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1190 | Observer.changedInstr(MI); | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1191 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1192 |  | 
|  | 1193 | case TargetOpcode::G_UITOFP: | 
|  | 1194 | if (TypeIdx != 1) | 
|  | 1195 | return UnableToLegalize; | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1196 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1197 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1198 | Observer.changedInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1199 | return Legalized; | 
|  | 1200 |  | 
|  | 1201 | case TargetOpcode::G_INSERT: | 
| Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 1202 | if (TypeIdx != 0) | 
|  | 1203 | return UnableToLegalize; | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1204 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1205 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT); | 
|  | 1206 | widenScalarDst(MI, WideTy); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1207 | Observer.changedInstr(MI); | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1208 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1209 |  | 
| Daniel Sanders | 5eb9f58 | 2018-04-28 18:14:50 +0000 | [diff] [blame] | 1210 | case TargetOpcode::G_LOAD: | 
| Amara Emerson | cbc02c7 | 2018-02-01 20:47:03 +0000 | [diff] [blame] | 1211 | // For some types like i24, we might try to widen to i32. To properly handle | 
|  | 1212 | // this we should be using a dedicated extending load, until then avoid | 
|  | 1213 | // trying to legalize. | 
|  | 1214 | if (alignTo(MRI.getType(MI.getOperand(0).getReg()).getSizeInBits(), 8) != | 
|  | 1215 | WideTy.getSizeInBits()) | 
|  | 1216 | return UnableToLegalize; | 
| Daniel Sanders | 5eb9f58 | 2018-04-28 18:14:50 +0000 | [diff] [blame] | 1217 | LLVM_FALLTHROUGH; | 
|  | 1218 | case TargetOpcode::G_SEXTLOAD: | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1219 | case TargetOpcode::G_ZEXTLOAD: | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1220 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1221 | widenScalarDst(MI, WideTy); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1222 | Observer.changedInstr(MI); | 
| Tim Northover | 3c73e36 | 2016-08-23 18:20:09 +0000 | [diff] [blame] | 1223 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1224 |  | 
| Tim Northover | 3c73e36 | 2016-08-23 18:20:09 +0000 | [diff] [blame] | 1225 | case TargetOpcode::G_STORE: { | 
| Matt Arsenault | 92c5001 | 2019-01-30 02:04:31 +0000 | [diff] [blame] | 1226 | if (TypeIdx != 0) | 
|  | 1227 | return UnableToLegalize; | 
|  | 1228 |  | 
|  | 1229 | LLT Ty = MRI.getType(MI.getOperand(0).getReg()); | 
|  | 1230 | if (!isPowerOf2_32(Ty.getSizeInBits())) | 
| Tim Northover | 548feee | 2017-03-21 22:22:05 +0000 | [diff] [blame] | 1231 | return UnableToLegalize; | 
|  | 1232 |  | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1233 | Observer.changingInstr(MI); | 
| Matt Arsenault | 92c5001 | 2019-01-30 02:04:31 +0000 | [diff] [blame] | 1234 |  | 
|  | 1235 | unsigned ExtType = Ty.getScalarSizeInBits() == 1 ? | 
|  | 1236 | TargetOpcode::G_ZEXT : TargetOpcode::G_ANYEXT; | 
|  | 1237 | widenScalarSrc(MI, WideTy, 0, ExtType); | 
|  | 1238 |  | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1239 | Observer.changedInstr(MI); | 
| Tim Northover | 3c73e36 | 2016-08-23 18:20:09 +0000 | [diff] [blame] | 1240 | return Legalized; | 
|  | 1241 | } | 
| Tim Northover | ea904f9 | 2016-08-19 22:40:00 +0000 | [diff] [blame] | 1242 | case TargetOpcode::G_CONSTANT: { | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1243 | MachineOperand &SrcMO = MI.getOperand(1); | 
|  | 1244 | LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); | 
|  | 1245 | const APInt &Val = SrcMO.getCImm()->getValue().sext(WideTy.getSizeInBits()); | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1246 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1247 | SrcMO.setCImm(ConstantInt::get(Ctx, Val)); | 
|  | 1248 |  | 
|  | 1249 | widenScalarDst(MI, WideTy); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1250 | Observer.changedInstr(MI); | 
| Tim Northover | ea904f9 | 2016-08-19 22:40:00 +0000 | [diff] [blame] | 1251 | return Legalized; | 
|  | 1252 | } | 
| Tim Northover | a11be04 | 2016-08-19 22:40:08 +0000 | [diff] [blame] | 1253 | case TargetOpcode::G_FCONSTANT: { | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1254 | MachineOperand &SrcMO = MI.getOperand(1); | 
| Amara Emerson | 77a5c96 | 2018-01-27 07:07:20 +0000 | [diff] [blame] | 1255 | LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1256 | APFloat Val = SrcMO.getFPImm()->getValueAPF(); | 
| Amara Emerson | 77a5c96 | 2018-01-27 07:07:20 +0000 | [diff] [blame] | 1257 | bool LosesInfo; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1258 | switch (WideTy.getSizeInBits()) { | 
|  | 1259 | case 32: | 
|  | 1260 | Val.convert(APFloat::IEEEsingle(), APFloat::rmTowardZero, &LosesInfo); | 
|  | 1261 | break; | 
|  | 1262 | case 64: | 
|  | 1263 | Val.convert(APFloat::IEEEdouble(), APFloat::rmTowardZero, &LosesInfo); | 
|  | 1264 | break; | 
|  | 1265 | default: | 
|  | 1266 | llvm_unreachable("Unhandled fp widen type"); | 
| Tim Northover | 6cd4b23 | 2016-08-23 21:01:26 +0000 | [diff] [blame] | 1267 | } | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1268 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1269 | SrcMO.setFPImm(ConstantFP::get(Ctx, Val)); | 
|  | 1270 |  | 
|  | 1271 | widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1272 | Observer.changedInstr(MI); | 
| Roman Tereshin | 25cbfe6 | 2018-05-08 22:53:09 +0000 | [diff] [blame] | 1273 | return Legalized; | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1274 | } | 
| Matt Arsenault | befee40 | 2019-01-09 07:34:14 +0000 | [diff] [blame] | 1275 | case TargetOpcode::G_IMPLICIT_DEF: { | 
|  | 1276 | Observer.changingInstr(MI); | 
|  | 1277 | widenScalarDst(MI, WideTy); | 
|  | 1278 | Observer.changedInstr(MI); | 
|  | 1279 | return Legalized; | 
|  | 1280 | } | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1281 | case TargetOpcode::G_BRCOND: | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1282 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1283 | widenScalarSrc(MI, WideTy, 0, TargetOpcode::G_ANYEXT); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1284 | Observer.changedInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1285 | return Legalized; | 
|  | 1286 |  | 
|  | 1287 | case TargetOpcode::G_FCMP: | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1288 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1289 | if (TypeIdx == 0) | 
|  | 1290 | widenScalarDst(MI, WideTy); | 
|  | 1291 | else { | 
|  | 1292 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_FPEXT); | 
|  | 1293 | widenScalarSrc(MI, WideTy, 3, TargetOpcode::G_FPEXT); | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1294 | } | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1295 | Observer.changedInstr(MI); | 
| Roman Tereshin | 27bba44 | 2018-05-09 01:43:12 +0000 | [diff] [blame] | 1296 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1297 |  | 
|  | 1298 | case TargetOpcode::G_ICMP: | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1299 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1300 | if (TypeIdx == 0) | 
|  | 1301 | widenScalarDst(MI, WideTy); | 
|  | 1302 | else { | 
|  | 1303 | unsigned ExtOpcode = CmpInst::isSigned(static_cast<CmpInst::Predicate>( | 
|  | 1304 | MI.getOperand(1).getPredicate())) | 
|  | 1305 | ? TargetOpcode::G_SEXT | 
|  | 1306 | : TargetOpcode::G_ZEXT; | 
|  | 1307 | widenScalarSrc(MI, WideTy, 2, ExtOpcode); | 
|  | 1308 | widenScalarSrc(MI, WideTy, 3, ExtOpcode); | 
|  | 1309 | } | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1310 | Observer.changedInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1311 | return Legalized; | 
|  | 1312 |  | 
|  | 1313 | case TargetOpcode::G_GEP: | 
| Tim Northover | 22d82cf | 2016-09-15 11:02:19 +0000 | [diff] [blame] | 1314 | assert(TypeIdx == 1 && "unable to legalize pointer of GEP"); | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1315 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1316 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1317 | Observer.changedInstr(MI); | 
| Tim Northover | 22d82cf | 2016-09-15 11:02:19 +0000 | [diff] [blame] | 1318 | return Legalized; | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1319 |  | 
| Aditya Nandakumar | 892979e | 2017-08-25 04:57:27 +0000 | [diff] [blame] | 1320 | case TargetOpcode::G_PHI: { | 
|  | 1321 | assert(TypeIdx == 0 && "Expecting only Idx 0"); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1322 |  | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1323 | Observer.changingInstr(MI); | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1324 | for (unsigned I = 1; I < MI.getNumOperands(); I += 2) { | 
|  | 1325 | MachineBasicBlock &OpMBB = *MI.getOperand(I + 1).getMBB(); | 
|  | 1326 | MIRBuilder.setInsertPt(OpMBB, OpMBB.getFirstTerminator()); | 
|  | 1327 | widenScalarSrc(MI, WideTy, I, TargetOpcode::G_ANYEXT); | 
| Aditya Nandakumar | 892979e | 2017-08-25 04:57:27 +0000 | [diff] [blame] | 1328 | } | 
| Roman Tereshin | d5fa9fd | 2018-05-09 17:28:18 +0000 | [diff] [blame] | 1329 |  | 
|  | 1330 | MachineBasicBlock &MBB = *MI.getParent(); | 
|  | 1331 | MIRBuilder.setInsertPt(MBB, --MBB.getFirstNonPHI()); | 
|  | 1332 | widenScalarDst(MI, WideTy); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1333 | Observer.changedInstr(MI); | 
| Aditya Nandakumar | 892979e | 2017-08-25 04:57:27 +0000 | [diff] [blame] | 1334 | return Legalized; | 
|  | 1335 | } | 
| Matt Arsenault | 6378629 | 2019-01-22 20:38:15 +0000 | [diff] [blame] | 1336 | case TargetOpcode::G_EXTRACT_VECTOR_ELT: { | 
|  | 1337 | if (TypeIdx == 0) { | 
|  | 1338 | unsigned VecReg = MI.getOperand(1).getReg(); | 
|  | 1339 | LLT VecTy = MRI.getType(VecReg); | 
|  | 1340 | Observer.changingInstr(MI); | 
|  | 1341 |  | 
|  | 1342 | widenScalarSrc(MI, LLT::vector(VecTy.getNumElements(), | 
|  | 1343 | WideTy.getSizeInBits()), | 
|  | 1344 | 1, TargetOpcode::G_SEXT); | 
|  | 1345 |  | 
|  | 1346 | widenScalarDst(MI, WideTy, 0); | 
|  | 1347 | Observer.changedInstr(MI); | 
|  | 1348 | return Legalized; | 
|  | 1349 | } | 
|  | 1350 |  | 
| Amara Emerson | cbd86d8 | 2018-10-25 14:04:54 +0000 | [diff] [blame] | 1351 | if (TypeIdx != 2) | 
|  | 1352 | return UnableToLegalize; | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 1353 | Observer.changingInstr(MI); | 
| Amara Emerson | cbd86d8 | 2018-10-25 14:04:54 +0000 | [diff] [blame] | 1354 | widenScalarSrc(MI, WideTy, 2, TargetOpcode::G_SEXT); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 1355 | Observer.changedInstr(MI); | 
| Amara Emerson | cbd86d8 | 2018-10-25 14:04:54 +0000 | [diff] [blame] | 1356 | return Legalized; | 
| Matt Arsenault | 6378629 | 2019-01-22 20:38:15 +0000 | [diff] [blame] | 1357 | } | 
| Matt Arsenault | 745fd9f | 2019-01-20 19:10:31 +0000 | [diff] [blame] | 1358 | case TargetOpcode::G_FADD: | 
|  | 1359 | case TargetOpcode::G_FMUL: | 
|  | 1360 | case TargetOpcode::G_FSUB: | 
|  | 1361 | case TargetOpcode::G_FMA: | 
|  | 1362 | case TargetOpcode::G_FNEG: | 
|  | 1363 | case TargetOpcode::G_FABS: | 
|  | 1364 | case TargetOpcode::G_FDIV: | 
|  | 1365 | case TargetOpcode::G_FREM: | 
| Jessica Paquette | 453ab1d | 2018-12-21 17:05:26 +0000 | [diff] [blame] | 1366 | case TargetOpcode::G_FCEIL: | 
| Jessica Paquette | 7db82d7 | 2019-01-28 18:34:18 +0000 | [diff] [blame] | 1367 | case TargetOpcode::G_FCOS: | 
|  | 1368 | case TargetOpcode::G_FSIN: | 
| Jessica Paquette | c49428a | 2019-01-28 19:53:14 +0000 | [diff] [blame] | 1369 | case TargetOpcode::G_FLOG10: | 
| Jessica Paquette | 2d73ecd | 2019-01-28 21:27:23 +0000 | [diff] [blame] | 1370 | case TargetOpcode::G_FLOG: | 
| Jessica Paquette | 0154bd1 | 2019-01-30 21:16:04 +0000 | [diff] [blame] | 1371 | case TargetOpcode::G_FLOG2: | 
| Jessica Paquette | 22457f8 | 2019-01-30 21:03:52 +0000 | [diff] [blame] | 1372 | case TargetOpcode::G_FSQRT: | 
| Jessica Paquette | 84bedac | 2019-01-30 23:46:15 +0000 | [diff] [blame] | 1373 | case TargetOpcode::G_FEXP: | 
| Matt Arsenault | 745fd9f | 2019-01-20 19:10:31 +0000 | [diff] [blame] | 1374 | assert(TypeIdx == 0); | 
| Jessica Paquette | 453ab1d | 2018-12-21 17:05:26 +0000 | [diff] [blame] | 1375 | Observer.changingInstr(MI); | 
| Matt Arsenault | 745fd9f | 2019-01-20 19:10:31 +0000 | [diff] [blame] | 1376 |  | 
|  | 1377 | for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) | 
|  | 1378 | widenScalarSrc(MI, WideTy, I, TargetOpcode::G_FPEXT); | 
|  | 1379 |  | 
| Jessica Paquette | 453ab1d | 2018-12-21 17:05:26 +0000 | [diff] [blame] | 1380 | widenScalarDst(MI, WideTy, 0, TargetOpcode::G_FPTRUNC); | 
|  | 1381 | Observer.changedInstr(MI); | 
|  | 1382 | return Legalized; | 
| Matt Arsenault | cbaada6 | 2019-02-02 23:29:55 +0000 | [diff] [blame] | 1383 | case TargetOpcode::G_INTTOPTR: | 
|  | 1384 | if (TypeIdx != 1) | 
|  | 1385 | return UnableToLegalize; | 
|  | 1386 |  | 
|  | 1387 | Observer.changingInstr(MI); | 
|  | 1388 | widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ZEXT); | 
|  | 1389 | Observer.changedInstr(MI); | 
|  | 1390 | return Legalized; | 
|  | 1391 | case TargetOpcode::G_PTRTOINT: | 
|  | 1392 | if (TypeIdx != 0) | 
|  | 1393 | return UnableToLegalize; | 
|  | 1394 |  | 
|  | 1395 | Observer.changingInstr(MI); | 
|  | 1396 | widenScalarDst(MI, WideTy, 0); | 
|  | 1397 | Observer.changedInstr(MI); | 
|  | 1398 | return Legalized; | 
| Tim Northover | 3233581 | 2016-08-04 18:35:11 +0000 | [diff] [blame] | 1399 | } | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 1400 | } | 
|  | 1401 |  | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 1402 | LegalizerHelper::LegalizeResult | 
|  | 1403 | LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { | 
| Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 1404 | using namespace TargetOpcode; | 
| Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 1405 | MIRBuilder.setInstr(MI); | 
|  | 1406 |  | 
|  | 1407 | switch(MI.getOpcode()) { | 
|  | 1408 | default: | 
|  | 1409 | return UnableToLegalize; | 
|  | 1410 | case TargetOpcode::G_SREM: | 
|  | 1411 | case TargetOpcode::G_UREM: { | 
| Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 1412 | unsigned QuotReg = MRI.createGenericVirtualRegister(Ty); | 
|  | 1413 | MIRBuilder.buildInstr(MI.getOpcode() == G_SREM ? G_SDIV : G_UDIV) | 
| Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 1414 | .addDef(QuotReg) | 
|  | 1415 | .addUse(MI.getOperand(1).getReg()) | 
|  | 1416 | .addUse(MI.getOperand(2).getReg()); | 
|  | 1417 |  | 
| Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 1418 | unsigned ProdReg = MRI.createGenericVirtualRegister(Ty); | 
|  | 1419 | MIRBuilder.buildMul(ProdReg, QuotReg, MI.getOperand(2).getReg()); | 
|  | 1420 | MIRBuilder.buildSub(MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), | 
|  | 1421 | ProdReg); | 
| Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 1422 | MI.eraseFromParent(); | 
|  | 1423 | return Legalized; | 
|  | 1424 | } | 
| Tim Northover | 0a9b279 | 2017-02-08 21:22:15 +0000 | [diff] [blame] | 1425 | case TargetOpcode::G_SMULO: | 
|  | 1426 | case TargetOpcode::G_UMULO: { | 
|  | 1427 | // Generate G_UMULH/G_SMULH to check for overflow and a normal G_MUL for the | 
|  | 1428 | // result. | 
|  | 1429 | unsigned Res = MI.getOperand(0).getReg(); | 
|  | 1430 | unsigned Overflow = MI.getOperand(1).getReg(); | 
|  | 1431 | unsigned LHS = MI.getOperand(2).getReg(); | 
|  | 1432 | unsigned RHS = MI.getOperand(3).getReg(); | 
|  | 1433 |  | 
|  | 1434 | MIRBuilder.buildMul(Res, LHS, RHS); | 
|  | 1435 |  | 
|  | 1436 | unsigned Opcode = MI.getOpcode() == TargetOpcode::G_SMULO | 
|  | 1437 | ? TargetOpcode::G_SMULH | 
|  | 1438 | : TargetOpcode::G_UMULH; | 
|  | 1439 |  | 
|  | 1440 | unsigned HiPart = MRI.createGenericVirtualRegister(Ty); | 
|  | 1441 | MIRBuilder.buildInstr(Opcode) | 
|  | 1442 | .addDef(HiPart) | 
|  | 1443 | .addUse(LHS) | 
|  | 1444 | .addUse(RHS); | 
|  | 1445 |  | 
|  | 1446 | unsigned Zero = MRI.createGenericVirtualRegister(Ty); | 
|  | 1447 | MIRBuilder.buildConstant(Zero, 0); | 
| Amara Emerson | 9de6213 | 2018-01-03 04:56:56 +0000 | [diff] [blame] | 1448 |  | 
|  | 1449 | // For *signed* multiply, overflow is detected by checking: | 
|  | 1450 | // (hi != (lo >> bitwidth-1)) | 
|  | 1451 | if (Opcode == TargetOpcode::G_SMULH) { | 
|  | 1452 | unsigned Shifted = MRI.createGenericVirtualRegister(Ty); | 
|  | 1453 | unsigned ShiftAmt = MRI.createGenericVirtualRegister(Ty); | 
|  | 1454 | MIRBuilder.buildConstant(ShiftAmt, Ty.getSizeInBits() - 1); | 
|  | 1455 | MIRBuilder.buildInstr(TargetOpcode::G_ASHR) | 
|  | 1456 | .addDef(Shifted) | 
|  | 1457 | .addUse(Res) | 
|  | 1458 | .addUse(ShiftAmt); | 
|  | 1459 | MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Shifted); | 
|  | 1460 | } else { | 
|  | 1461 | MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Zero); | 
|  | 1462 | } | 
| Tim Northover | 0a9b279 | 2017-02-08 21:22:15 +0000 | [diff] [blame] | 1463 | MI.eraseFromParent(); | 
|  | 1464 | return Legalized; | 
|  | 1465 | } | 
| Volkan Keles | 5698b2a | 2017-03-08 18:09:14 +0000 | [diff] [blame] | 1466 | case TargetOpcode::G_FNEG: { | 
|  | 1467 | // TODO: Handle vector types once we are able to | 
|  | 1468 | // represent them. | 
|  | 1469 | if (Ty.isVector()) | 
|  | 1470 | return UnableToLegalize; | 
|  | 1471 | unsigned Res = MI.getOperand(0).getReg(); | 
|  | 1472 | Type *ZeroTy; | 
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1473 | LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext(); | 
| Volkan Keles | 5698b2a | 2017-03-08 18:09:14 +0000 | [diff] [blame] | 1474 | switch (Ty.getSizeInBits()) { | 
|  | 1475 | case 16: | 
|  | 1476 | ZeroTy = Type::getHalfTy(Ctx); | 
|  | 1477 | break; | 
|  | 1478 | case 32: | 
|  | 1479 | ZeroTy = Type::getFloatTy(Ctx); | 
|  | 1480 | break; | 
|  | 1481 | case 64: | 
|  | 1482 | ZeroTy = Type::getDoubleTy(Ctx); | 
|  | 1483 | break; | 
| Amara Emerson | b6ddbef | 2017-12-19 17:21:35 +0000 | [diff] [blame] | 1484 | case 128: | 
|  | 1485 | ZeroTy = Type::getFP128Ty(Ctx); | 
|  | 1486 | break; | 
| Volkan Keles | 5698b2a | 2017-03-08 18:09:14 +0000 | [diff] [blame] | 1487 | default: | 
|  | 1488 | llvm_unreachable("unexpected floating-point type"); | 
|  | 1489 | } | 
|  | 1490 | ConstantFP &ZeroForNegation = | 
|  | 1491 | *cast<ConstantFP>(ConstantFP::getZeroValueForNegation(ZeroTy)); | 
| Volkan Keles | 02bb174 | 2018-02-14 19:58:36 +0000 | [diff] [blame] | 1492 | auto Zero = MIRBuilder.buildFConstant(Ty, ZeroForNegation); | 
| Volkan Keles | 5698b2a | 2017-03-08 18:09:14 +0000 | [diff] [blame] | 1493 | MIRBuilder.buildInstr(TargetOpcode::G_FSUB) | 
|  | 1494 | .addDef(Res) | 
| Volkan Keles | 02bb174 | 2018-02-14 19:58:36 +0000 | [diff] [blame] | 1495 | .addUse(Zero->getOperand(0).getReg()) | 
| Volkan Keles | 5698b2a | 2017-03-08 18:09:14 +0000 | [diff] [blame] | 1496 | .addUse(MI.getOperand(1).getReg()); | 
|  | 1497 | MI.eraseFromParent(); | 
|  | 1498 | return Legalized; | 
|  | 1499 | } | 
| Volkan Keles | 225921a | 2017-03-10 21:25:09 +0000 | [diff] [blame] | 1500 | case TargetOpcode::G_FSUB: { | 
|  | 1501 | // Lower (G_FSUB LHS, RHS) to (G_FADD LHS, (G_FNEG RHS)). | 
|  | 1502 | // First, check if G_FNEG is marked as Lower. If so, we may | 
|  | 1503 | // end up with an infinite loop as G_FSUB is used to legalize G_FNEG. | 
| Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 1504 | if (LI.getAction({G_FNEG, {Ty}}).Action == Lower) | 
| Volkan Keles | 225921a | 2017-03-10 21:25:09 +0000 | [diff] [blame] | 1505 | return UnableToLegalize; | 
|  | 1506 | unsigned Res = MI.getOperand(0).getReg(); | 
|  | 1507 | unsigned LHS = MI.getOperand(1).getReg(); | 
|  | 1508 | unsigned RHS = MI.getOperand(2).getReg(); | 
|  | 1509 | unsigned Neg = MRI.createGenericVirtualRegister(Ty); | 
|  | 1510 | MIRBuilder.buildInstr(TargetOpcode::G_FNEG).addDef(Neg).addUse(RHS); | 
|  | 1511 | MIRBuilder.buildInstr(TargetOpcode::G_FADD) | 
|  | 1512 | .addDef(Res) | 
|  | 1513 | .addUse(LHS) | 
|  | 1514 | .addUse(Neg); | 
|  | 1515 | MI.eraseFromParent(); | 
|  | 1516 | return Legalized; | 
|  | 1517 | } | 
| Daniel Sanders | aef1dfc | 2017-11-30 20:11:42 +0000 | [diff] [blame] | 1518 | case TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS: { | 
|  | 1519 | unsigned OldValRes = MI.getOperand(0).getReg(); | 
|  | 1520 | unsigned SuccessRes = MI.getOperand(1).getReg(); | 
|  | 1521 | unsigned Addr = MI.getOperand(2).getReg(); | 
|  | 1522 | unsigned CmpVal = MI.getOperand(3).getReg(); | 
|  | 1523 | unsigned NewVal = MI.getOperand(4).getReg(); | 
|  | 1524 | MIRBuilder.buildAtomicCmpXchg(OldValRes, Addr, CmpVal, NewVal, | 
|  | 1525 | **MI.memoperands_begin()); | 
|  | 1526 | MIRBuilder.buildICmp(CmpInst::ICMP_EQ, SuccessRes, OldValRes, CmpVal); | 
|  | 1527 | MI.eraseFromParent(); | 
|  | 1528 | return Legalized; | 
|  | 1529 | } | 
| Daniel Sanders | 5eb9f58 | 2018-04-28 18:14:50 +0000 | [diff] [blame] | 1530 | case TargetOpcode::G_LOAD: | 
|  | 1531 | case TargetOpcode::G_SEXTLOAD: | 
|  | 1532 | case TargetOpcode::G_ZEXTLOAD: { | 
|  | 1533 | // Lower to a memory-width G_LOAD and a G_SEXT/G_ZEXT/G_ANYEXT | 
|  | 1534 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 1535 | unsigned PtrReg = MI.getOperand(1).getReg(); | 
|  | 1536 | LLT DstTy = MRI.getType(DstReg); | 
|  | 1537 | auto &MMO = **MI.memoperands_begin(); | 
|  | 1538 |  | 
|  | 1539 | if (DstTy.getSizeInBits() == MMO.getSize() /* in bytes */ * 8) { | 
| Daniel Sanders | 2de9d4a | 2018-04-30 17:20:01 +0000 | [diff] [blame] | 1540 | // In the case of G_LOAD, this was a non-extending load already and we're | 
|  | 1541 | // about to lower to the same instruction. | 
|  | 1542 | if (MI.getOpcode() == TargetOpcode::G_LOAD) | 
|  | 1543 | return UnableToLegalize; | 
| Daniel Sanders | 5eb9f58 | 2018-04-28 18:14:50 +0000 | [diff] [blame] | 1544 | MIRBuilder.buildLoad(DstReg, PtrReg, MMO); | 
|  | 1545 | MI.eraseFromParent(); | 
|  | 1546 | return Legalized; | 
|  | 1547 | } | 
|  | 1548 |  | 
|  | 1549 | if (DstTy.isScalar()) { | 
|  | 1550 | unsigned TmpReg = MRI.createGenericVirtualRegister( | 
|  | 1551 | LLT::scalar(MMO.getSize() /* in bytes */ * 8)); | 
|  | 1552 | MIRBuilder.buildLoad(TmpReg, PtrReg, MMO); | 
|  | 1553 | switch (MI.getOpcode()) { | 
|  | 1554 | default: | 
|  | 1555 | llvm_unreachable("Unexpected opcode"); | 
|  | 1556 | case TargetOpcode::G_LOAD: | 
|  | 1557 | MIRBuilder.buildAnyExt(DstReg, TmpReg); | 
|  | 1558 | break; | 
|  | 1559 | case TargetOpcode::G_SEXTLOAD: | 
|  | 1560 | MIRBuilder.buildSExt(DstReg, TmpReg); | 
|  | 1561 | break; | 
|  | 1562 | case TargetOpcode::G_ZEXTLOAD: | 
|  | 1563 | MIRBuilder.buildZExt(DstReg, TmpReg); | 
|  | 1564 | break; | 
|  | 1565 | } | 
|  | 1566 | MI.eraseFromParent(); | 
|  | 1567 | return Legalized; | 
|  | 1568 | } | 
|  | 1569 |  | 
|  | 1570 | return UnableToLegalize; | 
|  | 1571 | } | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 1572 | case TargetOpcode::G_CTLZ_ZERO_UNDEF: | 
|  | 1573 | case TargetOpcode::G_CTTZ_ZERO_UNDEF: | 
|  | 1574 | case TargetOpcode::G_CTLZ: | 
|  | 1575 | case TargetOpcode::G_CTTZ: | 
|  | 1576 | case TargetOpcode::G_CTPOP: | 
|  | 1577 | return lowerBitCount(MI, TypeIdx, Ty); | 
| Petar Avramovic | b8276f2 | 2018-12-17 12:31:07 +0000 | [diff] [blame] | 1578 | case G_UADDE: { | 
|  | 1579 | unsigned Res = MI.getOperand(0).getReg(); | 
|  | 1580 | unsigned CarryOut = MI.getOperand(1).getReg(); | 
|  | 1581 | unsigned LHS = MI.getOperand(2).getReg(); | 
|  | 1582 | unsigned RHS = MI.getOperand(3).getReg(); | 
|  | 1583 | unsigned CarryIn = MI.getOperand(4).getReg(); | 
|  | 1584 |  | 
|  | 1585 | unsigned TmpRes = MRI.createGenericVirtualRegister(Ty); | 
|  | 1586 | unsigned ZExtCarryIn = MRI.createGenericVirtualRegister(Ty); | 
|  | 1587 |  | 
|  | 1588 | MIRBuilder.buildAdd(TmpRes, LHS, RHS); | 
|  | 1589 | MIRBuilder.buildZExt(ZExtCarryIn, CarryIn); | 
|  | 1590 | MIRBuilder.buildAdd(Res, TmpRes, ZExtCarryIn); | 
|  | 1591 | MIRBuilder.buildICmp(CmpInst::ICMP_ULT, CarryOut, Res, LHS); | 
|  | 1592 |  | 
|  | 1593 | MI.eraseFromParent(); | 
|  | 1594 | return Legalized; | 
|  | 1595 | } | 
| Petar Avramovic | 7cecadb | 2019-01-28 12:10:17 +0000 | [diff] [blame] | 1596 | case G_USUBO: { | 
|  | 1597 | unsigned Res = MI.getOperand(0).getReg(); | 
|  | 1598 | unsigned BorrowOut = MI.getOperand(1).getReg(); | 
|  | 1599 | unsigned LHS = MI.getOperand(2).getReg(); | 
|  | 1600 | unsigned RHS = MI.getOperand(3).getReg(); | 
|  | 1601 |  | 
|  | 1602 | MIRBuilder.buildSub(Res, LHS, RHS); | 
|  | 1603 | MIRBuilder.buildICmp(CmpInst::ICMP_ULT, BorrowOut, LHS, RHS); | 
|  | 1604 |  | 
|  | 1605 | MI.eraseFromParent(); | 
|  | 1606 | return Legalized; | 
|  | 1607 | } | 
|  | 1608 | case G_USUBE: { | 
|  | 1609 | unsigned Res = MI.getOperand(0).getReg(); | 
|  | 1610 | unsigned BorrowOut = MI.getOperand(1).getReg(); | 
|  | 1611 | unsigned LHS = MI.getOperand(2).getReg(); | 
|  | 1612 | unsigned RHS = MI.getOperand(3).getReg(); | 
|  | 1613 | unsigned BorrowIn = MI.getOperand(4).getReg(); | 
|  | 1614 |  | 
|  | 1615 | unsigned TmpRes = MRI.createGenericVirtualRegister(Ty); | 
|  | 1616 | unsigned ZExtBorrowIn = MRI.createGenericVirtualRegister(Ty); | 
|  | 1617 | unsigned LHS_EQ_RHS = MRI.createGenericVirtualRegister(LLT::scalar(1)); | 
|  | 1618 | unsigned LHS_ULT_RHS = MRI.createGenericVirtualRegister(LLT::scalar(1)); | 
|  | 1619 |  | 
|  | 1620 | MIRBuilder.buildSub(TmpRes, LHS, RHS); | 
|  | 1621 | MIRBuilder.buildZExt(ZExtBorrowIn, BorrowIn); | 
|  | 1622 | MIRBuilder.buildSub(Res, TmpRes, ZExtBorrowIn); | 
|  | 1623 | MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LHS_EQ_RHS, LHS, RHS); | 
|  | 1624 | MIRBuilder.buildICmp(CmpInst::ICMP_ULT, LHS_ULT_RHS, LHS, RHS); | 
|  | 1625 | MIRBuilder.buildSelect(BorrowOut, LHS_EQ_RHS, BorrowIn, LHS_ULT_RHS); | 
|  | 1626 |  | 
|  | 1627 | MI.eraseFromParent(); | 
|  | 1628 | return Legalized; | 
|  | 1629 | } | 
| Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 1630 | } | 
|  | 1631 | } | 
|  | 1632 |  | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 1633 | LegalizerHelper::LegalizeResult LegalizerHelper::fewerElementsVectorImplicitDef( | 
|  | 1634 | MachineInstr &MI, unsigned TypeIdx, LLT NarrowTy) { | 
|  | 1635 | SmallVector<unsigned, 2> DstRegs; | 
|  | 1636 |  | 
|  | 1637 | unsigned NarrowSize = NarrowTy.getSizeInBits(); | 
|  | 1638 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 1639 | unsigned Size = MRI.getType(DstReg).getSizeInBits(); | 
|  | 1640 | int NumParts = Size / NarrowSize; | 
|  | 1641 | // FIXME: Don't know how to handle the situation where the small vectors | 
|  | 1642 | // aren't all the same size yet. | 
|  | 1643 | if (Size % NarrowSize != 0) | 
|  | 1644 | return UnableToLegalize; | 
|  | 1645 |  | 
|  | 1646 | for (int i = 0; i < NumParts; ++i) { | 
|  | 1647 | unsigned TmpReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 1648 | MIRBuilder.buildUndef(TmpReg); | 
|  | 1649 | DstRegs.push_back(TmpReg); | 
|  | 1650 | } | 
|  | 1651 |  | 
|  | 1652 | if (NarrowTy.isVector()) | 
|  | 1653 | MIRBuilder.buildConcatVectors(DstReg, DstRegs); | 
|  | 1654 | else | 
|  | 1655 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 1656 |  | 
|  | 1657 | MI.eraseFromParent(); | 
|  | 1658 | return Legalized; | 
|  | 1659 | } | 
|  | 1660 |  | 
|  | 1661 | LegalizerHelper::LegalizeResult | 
|  | 1662 | LegalizerHelper::fewerElementsVectorBasic(MachineInstr &MI, unsigned TypeIdx, | 
|  | 1663 | LLT NarrowTy) { | 
| Matt Arsenault | ccefbbd | 2019-01-30 02:22:13 +0000 | [diff] [blame] | 1664 | const unsigned Opc = MI.getOpcode(); | 
|  | 1665 | const unsigned NumOps = MI.getNumOperands() - 1; | 
|  | 1666 | const unsigned NarrowSize = NarrowTy.getSizeInBits(); | 
|  | 1667 | const unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 1668 | const unsigned Flags = MI.getFlags(); | 
|  | 1669 | const LLT DstTy = MRI.getType(DstReg); | 
|  | 1670 | const unsigned Size = DstTy.getSizeInBits(); | 
|  | 1671 | const int NumParts = Size / NarrowSize; | 
|  | 1672 | const LLT EltTy = DstTy.getElementType(); | 
|  | 1673 | const unsigned EltSize = EltTy.getSizeInBits(); | 
|  | 1674 | const unsigned BitsForNumParts = NarrowSize * NumParts; | 
|  | 1675 |  | 
|  | 1676 | // Check if we have any leftovers. If we do, then only handle the case where | 
|  | 1677 | // the leftover is one element. | 
|  | 1678 | if (BitsForNumParts != Size && BitsForNumParts + EltSize != Size) | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 1679 | return UnableToLegalize; | 
|  | 1680 |  | 
| Matt Arsenault | ccefbbd | 2019-01-30 02:22:13 +0000 | [diff] [blame] | 1681 | if (BitsForNumParts != Size) { | 
|  | 1682 | unsigned AccumDstReg = MRI.createGenericVirtualRegister(DstTy); | 
|  | 1683 | MIRBuilder.buildUndef(AccumDstReg); | 
|  | 1684 |  | 
|  | 1685 | // Handle the pieces which evenly divide into the requested type with | 
|  | 1686 | // extract/op/insert sequence. | 
|  | 1687 | for (unsigned Offset = 0; Offset < BitsForNumParts; Offset += NarrowSize) { | 
|  | 1688 | SmallVector<SrcOp, 4> SrcOps; | 
|  | 1689 | for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) { | 
|  | 1690 | unsigned PartOpReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 1691 | MIRBuilder.buildExtract(PartOpReg, MI.getOperand(I).getReg(), Offset); | 
|  | 1692 | SrcOps.push_back(PartOpReg); | 
|  | 1693 | } | 
|  | 1694 |  | 
|  | 1695 | unsigned PartDstReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 1696 | MIRBuilder.buildInstr(Opc, {PartDstReg}, SrcOps, Flags); | 
|  | 1697 |  | 
|  | 1698 | unsigned PartInsertReg = MRI.createGenericVirtualRegister(DstTy); | 
|  | 1699 | MIRBuilder.buildInsert(PartInsertReg, AccumDstReg, PartDstReg, Offset); | 
|  | 1700 | AccumDstReg = PartInsertReg; | 
|  | 1701 | Offset += NarrowSize; | 
|  | 1702 | } | 
|  | 1703 |  | 
|  | 1704 | // Handle the remaining element sized leftover piece. | 
|  | 1705 | SmallVector<SrcOp, 4> SrcOps; | 
|  | 1706 | for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) { | 
|  | 1707 | unsigned PartOpReg = MRI.createGenericVirtualRegister(EltTy); | 
|  | 1708 | MIRBuilder.buildExtract(PartOpReg, MI.getOperand(I).getReg(), | 
|  | 1709 | BitsForNumParts); | 
|  | 1710 | SrcOps.push_back(PartOpReg); | 
|  | 1711 | } | 
|  | 1712 |  | 
|  | 1713 | unsigned PartDstReg = MRI.createGenericVirtualRegister(EltTy); | 
|  | 1714 | MIRBuilder.buildInstr(Opc, {PartDstReg}, SrcOps, Flags); | 
|  | 1715 | MIRBuilder.buildInsert(DstReg, AccumDstReg, PartDstReg, BitsForNumParts); | 
|  | 1716 | MI.eraseFromParent(); | 
|  | 1717 |  | 
|  | 1718 | return Legalized; | 
|  | 1719 | } | 
|  | 1720 |  | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 1721 | SmallVector<unsigned, 2> DstRegs, Src0Regs, Src1Regs, Src2Regs; | 
|  | 1722 |  | 
|  | 1723 | extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src0Regs); | 
|  | 1724 |  | 
|  | 1725 | if (NumOps >= 2) | 
|  | 1726 | extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src1Regs); | 
|  | 1727 |  | 
|  | 1728 | if (NumOps >= 3) | 
|  | 1729 | extractParts(MI.getOperand(3).getReg(), NarrowTy, NumParts, Src2Regs); | 
|  | 1730 |  | 
|  | 1731 | for (int i = 0; i < NumParts; ++i) { | 
|  | 1732 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy); | 
|  | 1733 |  | 
|  | 1734 | if (NumOps == 1) | 
|  | 1735 | MIRBuilder.buildInstr(Opc, {DstReg}, {Src0Regs[i]}, Flags); | 
|  | 1736 | else if (NumOps == 2) { | 
|  | 1737 | MIRBuilder.buildInstr(Opc, {DstReg}, {Src0Regs[i], Src1Regs[i]}, Flags); | 
|  | 1738 | } else if (NumOps == 3) { | 
|  | 1739 | MIRBuilder.buildInstr(Opc, {DstReg}, | 
|  | 1740 | {Src0Regs[i], Src1Regs[i], Src2Regs[i]}, Flags); | 
|  | 1741 | } | 
|  | 1742 |  | 
|  | 1743 | DstRegs.push_back(DstReg); | 
|  | 1744 | } | 
|  | 1745 |  | 
|  | 1746 | if (NarrowTy.isVector()) | 
|  | 1747 | MIRBuilder.buildConcatVectors(DstReg, DstRegs); | 
|  | 1748 | else | 
|  | 1749 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 1750 |  | 
|  | 1751 | MI.eraseFromParent(); | 
|  | 1752 | return Legalized; | 
|  | 1753 | } | 
|  | 1754 |  | 
| Matt Arsenault | c83b823 | 2019-02-07 17:38:00 +0000 | [diff] [blame] | 1755 | // Handle splitting vector operations which need to have the same number of | 
|  | 1756 | // elements in each type index, but each type index may have a different element | 
|  | 1757 | // type. | 
|  | 1758 | // | 
|  | 1759 | // e.g.  <4 x s64> = G_SHL <4 x s64>, <4 x s32> -> | 
|  | 1760 | //       <2 x s64> = G_SHL <2 x s64>, <2 x s32> | 
|  | 1761 | //       <2 x s64> = G_SHL <2 x s64>, <2 x s32> | 
|  | 1762 | // | 
|  | 1763 | // Also handles some irregular breakdown cases, e.g. | 
|  | 1764 | // e.g.  <3 x s64> = G_SHL <3 x s64>, <3 x s32> -> | 
|  | 1765 | //       <2 x s64> = G_SHL <2 x s64>, <2 x s32> | 
|  | 1766 | //             s64 = G_SHL s64, s32 | 
|  | 1767 | LegalizerHelper::LegalizeResult | 
|  | 1768 | LegalizerHelper::fewerElementsVectorMultiEltType( | 
|  | 1769 | MachineInstr &MI, unsigned TypeIdx, LLT NarrowTyArg) { | 
|  | 1770 | if (TypeIdx != 0) | 
|  | 1771 | return UnableToLegalize; | 
|  | 1772 |  | 
|  | 1773 | const LLT NarrowTy0 = NarrowTyArg; | 
|  | 1774 | const unsigned NewNumElts = | 
|  | 1775 | NarrowTy0.isVector() ? NarrowTy0.getNumElements() : 1; | 
|  | 1776 |  | 
|  | 1777 | const unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 1778 | LLT DstTy = MRI.getType(DstReg); | 
|  | 1779 | LLT LeftoverTy0; | 
|  | 1780 |  | 
|  | 1781 | // All of the operands need to have the same number of elements, so if we can | 
|  | 1782 | // determine a type breakdown for the result type, we can for all of the | 
|  | 1783 | // source types. | 
|  | 1784 | int NumParts = getNarrowTypeBreakDown(DstTy, NarrowTy0, LeftoverTy0); | 
|  | 1785 | if (NumParts < 0) | 
|  | 1786 | return UnableToLegalize; | 
|  | 1787 |  | 
|  | 1788 | SmallVector<MachineInstrBuilder, 4> NewInsts; | 
|  | 1789 |  | 
|  | 1790 | SmallVector<unsigned, 4> DstRegs, LeftoverDstRegs; | 
|  | 1791 | SmallVector<unsigned, 4> PartRegs, LeftoverRegs; | 
|  | 1792 |  | 
|  | 1793 | for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) { | 
|  | 1794 | LLT LeftoverTy; | 
|  | 1795 | unsigned SrcReg = MI.getOperand(I).getReg(); | 
|  | 1796 | LLT SrcTyI = MRI.getType(SrcReg); | 
|  | 1797 | LLT NarrowTyI = LLT::scalarOrVector(NewNumElts, SrcTyI.getScalarType()); | 
|  | 1798 | LLT LeftoverTyI; | 
|  | 1799 |  | 
|  | 1800 | // Split this operand into the requested typed registers, and any leftover | 
|  | 1801 | // required to reproduce the original type. | 
|  | 1802 | if (!extractParts(SrcReg, SrcTyI, NarrowTyI, LeftoverTyI, PartRegs, | 
|  | 1803 | LeftoverRegs)) | 
|  | 1804 | return UnableToLegalize; | 
|  | 1805 |  | 
|  | 1806 | if (I == 1) { | 
|  | 1807 | // For the first operand, create an instruction for each part and setup | 
|  | 1808 | // the result. | 
|  | 1809 | for (unsigned PartReg : PartRegs) { | 
|  | 1810 | unsigned PartDstReg = MRI.createGenericVirtualRegister(NarrowTy0); | 
|  | 1811 | NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode()) | 
|  | 1812 | .addDef(PartDstReg) | 
|  | 1813 | .addUse(PartReg)); | 
|  | 1814 | DstRegs.push_back(PartDstReg); | 
|  | 1815 | } | 
|  | 1816 |  | 
|  | 1817 | for (unsigned LeftoverReg : LeftoverRegs) { | 
|  | 1818 | unsigned PartDstReg = MRI.createGenericVirtualRegister(LeftoverTy0); | 
|  | 1819 | NewInsts.push_back(MIRBuilder.buildInstrNoInsert(MI.getOpcode()) | 
|  | 1820 | .addDef(PartDstReg) | 
|  | 1821 | .addUse(LeftoverReg)); | 
|  | 1822 | LeftoverDstRegs.push_back(PartDstReg); | 
|  | 1823 | } | 
|  | 1824 | } else { | 
|  | 1825 | assert(NewInsts.size() == PartRegs.size() + LeftoverRegs.size()); | 
|  | 1826 |  | 
|  | 1827 | // Add the newly created operand splits to the existing instructions. The | 
|  | 1828 | // odd-sized pieces are ordered after the requested NarrowTyArg sized | 
|  | 1829 | // pieces. | 
|  | 1830 | unsigned InstCount = 0; | 
|  | 1831 | for (unsigned J = 0, JE = PartRegs.size(); J != JE; ++J) | 
|  | 1832 | NewInsts[InstCount++].addUse(PartRegs[J]); | 
|  | 1833 | for (unsigned J = 0, JE = LeftoverRegs.size(); J != JE; ++J) | 
|  | 1834 | NewInsts[InstCount++].addUse(LeftoverRegs[J]); | 
|  | 1835 | } | 
|  | 1836 |  | 
|  | 1837 | PartRegs.clear(); | 
|  | 1838 | LeftoverRegs.clear(); | 
|  | 1839 | } | 
|  | 1840 |  | 
|  | 1841 | // Insert the newly built operations and rebuild the result register. | 
|  | 1842 | for (auto &MIB : NewInsts) | 
|  | 1843 | MIRBuilder.insertInstr(MIB); | 
|  | 1844 |  | 
|  | 1845 | insertParts(DstReg, DstTy, NarrowTy0, DstRegs, LeftoverTy0, LeftoverDstRegs); | 
|  | 1846 |  | 
|  | 1847 | MI.eraseFromParent(); | 
|  | 1848 | return Legalized; | 
|  | 1849 | } | 
|  | 1850 |  | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 1851 | LegalizerHelper::LegalizeResult | 
| Matt Arsenault | ca67634 | 2019-01-25 02:36:32 +0000 | [diff] [blame] | 1852 | LegalizerHelper::fewerElementsVectorCasts(MachineInstr &MI, unsigned TypeIdx, | 
|  | 1853 | LLT NarrowTy) { | 
|  | 1854 | if (TypeIdx != 0) | 
|  | 1855 | return UnableToLegalize; | 
|  | 1856 |  | 
|  | 1857 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 1858 | unsigned SrcReg = MI.getOperand(1).getReg(); | 
|  | 1859 | LLT DstTy = MRI.getType(DstReg); | 
|  | 1860 | LLT SrcTy = MRI.getType(SrcReg); | 
|  | 1861 |  | 
|  | 1862 | LLT NarrowTy0 = NarrowTy; | 
|  | 1863 | LLT NarrowTy1; | 
|  | 1864 | unsigned NumParts; | 
|  | 1865 |  | 
| Matt Arsenault | cbaada6 | 2019-02-02 23:29:55 +0000 | [diff] [blame] | 1866 | if (NarrowTy.isVector()) { | 
| Matt Arsenault | ca67634 | 2019-01-25 02:36:32 +0000 | [diff] [blame] | 1867 | // Uneven breakdown not handled. | 
|  | 1868 | NumParts = DstTy.getNumElements() / NarrowTy.getNumElements(); | 
|  | 1869 | if (NumParts * NarrowTy.getNumElements() != DstTy.getNumElements()) | 
|  | 1870 | return UnableToLegalize; | 
|  | 1871 |  | 
|  | 1872 | NarrowTy1 = LLT::vector(NumParts, SrcTy.getElementType().getSizeInBits()); | 
| Matt Arsenault | cbaada6 | 2019-02-02 23:29:55 +0000 | [diff] [blame] | 1873 | } else { | 
|  | 1874 | NumParts = DstTy.getNumElements(); | 
|  | 1875 | NarrowTy1 = SrcTy.getElementType(); | 
| Matt Arsenault | ca67634 | 2019-01-25 02:36:32 +0000 | [diff] [blame] | 1876 | } | 
|  | 1877 |  | 
|  | 1878 | SmallVector<unsigned, 4> SrcRegs, DstRegs; | 
|  | 1879 | extractParts(SrcReg, NarrowTy1, NumParts, SrcRegs); | 
|  | 1880 |  | 
|  | 1881 | for (unsigned I = 0; I < NumParts; ++I) { | 
|  | 1882 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy0); | 
|  | 1883 | MachineInstr *NewInst = MIRBuilder.buildInstr(MI.getOpcode()) | 
|  | 1884 | .addDef(DstReg) | 
|  | 1885 | .addUse(SrcRegs[I]); | 
|  | 1886 |  | 
|  | 1887 | NewInst->setFlags(MI.getFlags()); | 
|  | 1888 | DstRegs.push_back(DstReg); | 
|  | 1889 | } | 
|  | 1890 |  | 
|  | 1891 | if (NarrowTy.isVector()) | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 1892 | MIRBuilder.buildConcatVectors(DstReg, DstRegs); | 
| Matt Arsenault | 1b1e685 | 2019-01-25 02:59:34 +0000 | [diff] [blame] | 1893 | else | 
|  | 1894 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 1895 |  | 
|  | 1896 | MI.eraseFromParent(); | 
|  | 1897 | return Legalized; | 
|  | 1898 | } | 
|  | 1899 |  | 
|  | 1900 | LegalizerHelper::LegalizeResult | 
|  | 1901 | LegalizerHelper::fewerElementsVectorCmp(MachineInstr &MI, unsigned TypeIdx, | 
|  | 1902 | LLT NarrowTy) { | 
|  | 1903 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 1904 | unsigned Src0Reg = MI.getOperand(2).getReg(); | 
|  | 1905 | LLT DstTy = MRI.getType(DstReg); | 
|  | 1906 | LLT SrcTy = MRI.getType(Src0Reg); | 
|  | 1907 |  | 
|  | 1908 | unsigned NumParts; | 
|  | 1909 | LLT NarrowTy0, NarrowTy1; | 
|  | 1910 |  | 
|  | 1911 | if (TypeIdx == 0) { | 
|  | 1912 | unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1; | 
|  | 1913 | unsigned OldElts = DstTy.getNumElements(); | 
|  | 1914 |  | 
|  | 1915 | NarrowTy0 = NarrowTy; | 
|  | 1916 | NumParts = NarrowTy.isVector() ? (OldElts / NewElts) : DstTy.getNumElements(); | 
|  | 1917 | NarrowTy1 = NarrowTy.isVector() ? | 
|  | 1918 | LLT::vector(NarrowTy.getNumElements(), SrcTy.getScalarSizeInBits()) : | 
|  | 1919 | SrcTy.getElementType(); | 
|  | 1920 |  | 
|  | 1921 | } else { | 
|  | 1922 | unsigned NewElts = NarrowTy.isVector() ? NarrowTy.getNumElements() : 1; | 
|  | 1923 | unsigned OldElts = SrcTy.getNumElements(); | 
|  | 1924 |  | 
|  | 1925 | NumParts = NarrowTy.isVector() ? (OldElts / NewElts) : | 
|  | 1926 | NarrowTy.getNumElements(); | 
|  | 1927 | NarrowTy0 = LLT::vector(NarrowTy.getNumElements(), | 
|  | 1928 | DstTy.getScalarSizeInBits()); | 
|  | 1929 | NarrowTy1 = NarrowTy; | 
|  | 1930 | } | 
|  | 1931 |  | 
|  | 1932 | // FIXME: Don't know how to handle the situation where the small vectors | 
|  | 1933 | // aren't all the same size yet. | 
|  | 1934 | if (NarrowTy1.isVector() && | 
|  | 1935 | NarrowTy1.getNumElements() * NumParts != DstTy.getNumElements()) | 
|  | 1936 | return UnableToLegalize; | 
|  | 1937 |  | 
|  | 1938 | CmpInst::Predicate Pred | 
|  | 1939 | = static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate()); | 
|  | 1940 |  | 
|  | 1941 | SmallVector<unsigned, 2> Src1Regs, Src2Regs, DstRegs; | 
|  | 1942 | extractParts(MI.getOperand(2).getReg(), NarrowTy1, NumParts, Src1Regs); | 
|  | 1943 | extractParts(MI.getOperand(3).getReg(), NarrowTy1, NumParts, Src2Regs); | 
|  | 1944 |  | 
|  | 1945 | for (unsigned I = 0; I < NumParts; ++I) { | 
|  | 1946 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy0); | 
|  | 1947 | DstRegs.push_back(DstReg); | 
|  | 1948 |  | 
|  | 1949 | if (MI.getOpcode() == TargetOpcode::G_ICMP) | 
|  | 1950 | MIRBuilder.buildICmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]); | 
|  | 1951 | else { | 
|  | 1952 | MachineInstr *NewCmp | 
|  | 1953 | = MIRBuilder.buildFCmp(Pred, DstReg, Src1Regs[I], Src2Regs[I]); | 
|  | 1954 | NewCmp->setFlags(MI.getFlags()); | 
|  | 1955 | } | 
|  | 1956 | } | 
|  | 1957 |  | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 1958 | if (NarrowTy1.isVector()) | 
| Matt Arsenault | ca67634 | 2019-01-25 02:36:32 +0000 | [diff] [blame] | 1959 | MIRBuilder.buildConcatVectors(DstReg, DstRegs); | 
|  | 1960 | else | 
|  | 1961 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 1962 |  | 
|  | 1963 | MI.eraseFromParent(); | 
|  | 1964 | return Legalized; | 
|  | 1965 | } | 
|  | 1966 |  | 
|  | 1967 | LegalizerHelper::LegalizeResult | 
| Matt Arsenault | dc6c785 | 2019-01-30 04:19:31 +0000 | [diff] [blame] | 1968 | LegalizerHelper::fewerElementsVectorSelect(MachineInstr &MI, unsigned TypeIdx, | 
|  | 1969 | LLT NarrowTy) { | 
|  | 1970 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 1971 | unsigned CondReg = MI.getOperand(1).getReg(); | 
|  | 1972 |  | 
|  | 1973 | unsigned NumParts = 0; | 
|  | 1974 | LLT NarrowTy0, NarrowTy1; | 
|  | 1975 |  | 
|  | 1976 | LLT DstTy = MRI.getType(DstReg); | 
|  | 1977 | LLT CondTy = MRI.getType(CondReg); | 
|  | 1978 | unsigned Size = DstTy.getSizeInBits(); | 
|  | 1979 |  | 
|  | 1980 | assert(TypeIdx == 0 || CondTy.isVector()); | 
|  | 1981 |  | 
|  | 1982 | if (TypeIdx == 0) { | 
|  | 1983 | NarrowTy0 = NarrowTy; | 
|  | 1984 | NarrowTy1 = CondTy; | 
|  | 1985 |  | 
|  | 1986 | unsigned NarrowSize = NarrowTy0.getSizeInBits(); | 
|  | 1987 | // FIXME: Don't know how to handle the situation where the small vectors | 
|  | 1988 | // aren't all the same size yet. | 
|  | 1989 | if (Size % NarrowSize != 0) | 
|  | 1990 | return UnableToLegalize; | 
|  | 1991 |  | 
|  | 1992 | NumParts = Size / NarrowSize; | 
|  | 1993 |  | 
|  | 1994 | // Need to break down the condition type | 
|  | 1995 | if (CondTy.isVector()) { | 
|  | 1996 | if (CondTy.getNumElements() == NumParts) | 
|  | 1997 | NarrowTy1 = CondTy.getElementType(); | 
|  | 1998 | else | 
|  | 1999 | NarrowTy1 = LLT::vector(CondTy.getNumElements() / NumParts, | 
|  | 2000 | CondTy.getScalarSizeInBits()); | 
|  | 2001 | } | 
|  | 2002 | } else { | 
|  | 2003 | NumParts = CondTy.getNumElements(); | 
|  | 2004 | if (NarrowTy.isVector()) { | 
|  | 2005 | // TODO: Handle uneven breakdown. | 
|  | 2006 | if (NumParts * NarrowTy.getNumElements() != CondTy.getNumElements()) | 
|  | 2007 | return UnableToLegalize; | 
|  | 2008 |  | 
|  | 2009 | return UnableToLegalize; | 
|  | 2010 | } else { | 
|  | 2011 | NarrowTy0 = DstTy.getElementType(); | 
|  | 2012 | NarrowTy1 = NarrowTy; | 
|  | 2013 | } | 
|  | 2014 | } | 
|  | 2015 |  | 
|  | 2016 | SmallVector<unsigned, 2> DstRegs, Src0Regs, Src1Regs, Src2Regs; | 
|  | 2017 | if (CondTy.isVector()) | 
|  | 2018 | extractParts(MI.getOperand(1).getReg(), NarrowTy1, NumParts, Src0Regs); | 
|  | 2019 |  | 
|  | 2020 | extractParts(MI.getOperand(2).getReg(), NarrowTy0, NumParts, Src1Regs); | 
|  | 2021 | extractParts(MI.getOperand(3).getReg(), NarrowTy0, NumParts, Src2Regs); | 
|  | 2022 |  | 
|  | 2023 | for (unsigned i = 0; i < NumParts; ++i) { | 
|  | 2024 | unsigned DstReg = MRI.createGenericVirtualRegister(NarrowTy0); | 
|  | 2025 | MIRBuilder.buildSelect(DstReg, CondTy.isVector() ? Src0Regs[i] : CondReg, | 
|  | 2026 | Src1Regs[i], Src2Regs[i]); | 
|  | 2027 | DstRegs.push_back(DstReg); | 
|  | 2028 | } | 
|  | 2029 |  | 
|  | 2030 | if (NarrowTy0.isVector()) | 
|  | 2031 | MIRBuilder.buildConcatVectors(DstReg, DstRegs); | 
|  | 2032 | else | 
|  | 2033 | MIRBuilder.buildBuildVector(DstReg, DstRegs); | 
|  | 2034 |  | 
|  | 2035 | MI.eraseFromParent(); | 
|  | 2036 | return Legalized; | 
|  | 2037 | } | 
|  | 2038 |  | 
|  | 2039 | LegalizerHelper::LegalizeResult | 
| Matt Arsenault | 7f09fd6 | 2019-02-05 00:26:12 +0000 | [diff] [blame] | 2040 | LegalizerHelper::reduceLoadStoreWidth(MachineInstr &MI, unsigned TypeIdx, | 
|  | 2041 | LLT NarrowTy) { | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2042 | // FIXME: Don't know how to handle secondary types yet. | 
|  | 2043 | if (TypeIdx != 0) | 
|  | 2044 | return UnableToLegalize; | 
|  | 2045 |  | 
| Matt Arsenault | cfca2a7 | 2019-01-27 22:36:24 +0000 | [diff] [blame] | 2046 | MachineMemOperand *MMO = *MI.memoperands_begin(); | 
|  | 2047 |  | 
|  | 2048 | // This implementation doesn't work for atomics. Give up instead of doing | 
|  | 2049 | // something invalid. | 
|  | 2050 | if (MMO->getOrdering() != AtomicOrdering::NotAtomic || | 
|  | 2051 | MMO->getFailureOrdering() != AtomicOrdering::NotAtomic) | 
|  | 2052 | return UnableToLegalize; | 
|  | 2053 |  | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2054 | bool IsLoad = MI.getOpcode() == TargetOpcode::G_LOAD; | 
|  | 2055 | unsigned ValReg = MI.getOperand(0).getReg(); | 
|  | 2056 | unsigned AddrReg = MI.getOperand(1).getReg(); | 
| Matt Arsenault | c7bce73 | 2019-01-31 02:46:05 +0000 | [diff] [blame] | 2057 | LLT ValTy = MRI.getType(ValReg); | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2058 |  | 
| Matt Arsenault | c7bce73 | 2019-01-31 02:46:05 +0000 | [diff] [blame] | 2059 | int NumParts = -1; | 
|  | 2060 | LLT LeftoverTy; | 
|  | 2061 | SmallVector<unsigned, 8> NarrowRegs, NarrowLeftoverRegs; | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2062 | if (IsLoad) { | 
| Matt Arsenault | c7bce73 | 2019-01-31 02:46:05 +0000 | [diff] [blame] | 2063 | NumParts = getNarrowTypeBreakDown(ValTy, NarrowTy, LeftoverTy); | 
|  | 2064 | } else { | 
|  | 2065 | if (extractParts(ValReg, ValTy, NarrowTy, LeftoverTy, NarrowRegs, | 
|  | 2066 | NarrowLeftoverRegs)) | 
|  | 2067 | NumParts = NarrowRegs.size(); | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2068 | } | 
| Matt Arsenault | c7bce73 | 2019-01-31 02:46:05 +0000 | [diff] [blame] | 2069 |  | 
|  | 2070 | if (NumParts == -1) | 
|  | 2071 | return UnableToLegalize; | 
|  | 2072 |  | 
|  | 2073 | const LLT OffsetTy = LLT::scalar(MRI.getType(AddrReg).getScalarSizeInBits()); | 
|  | 2074 |  | 
|  | 2075 | unsigned TotalSize = ValTy.getSizeInBits(); | 
|  | 2076 |  | 
|  | 2077 | // Split the load/store into PartTy sized pieces starting at Offset. If this | 
|  | 2078 | // is a load, return the new registers in ValRegs. For a store, each elements | 
|  | 2079 | // of ValRegs should be PartTy. Returns the next offset that needs to be | 
|  | 2080 | // handled. | 
|  | 2081 | auto splitTypePieces = [=](LLT PartTy, SmallVectorImpl<unsigned> &ValRegs, | 
|  | 2082 | unsigned Offset) -> unsigned { | 
|  | 2083 | MachineFunction &MF = MIRBuilder.getMF(); | 
|  | 2084 | unsigned PartSize = PartTy.getSizeInBits(); | 
|  | 2085 | for (unsigned Idx = 0, E = NumParts; Idx != E && Offset < TotalSize; | 
|  | 2086 | Offset += PartSize, ++Idx) { | 
|  | 2087 | unsigned ByteSize = PartSize / 8; | 
|  | 2088 | unsigned ByteOffset = Offset / 8; | 
|  | 2089 | unsigned NewAddrReg = 0; | 
|  | 2090 |  | 
|  | 2091 | MIRBuilder.materializeGEP(NewAddrReg, AddrReg, OffsetTy, ByteOffset); | 
|  | 2092 |  | 
|  | 2093 | MachineMemOperand *NewMMO = | 
|  | 2094 | MF.getMachineMemOperand(MMO, ByteOffset, ByteSize); | 
|  | 2095 |  | 
|  | 2096 | if (IsLoad) { | 
|  | 2097 | unsigned Dst = MRI.createGenericVirtualRegister(PartTy); | 
|  | 2098 | ValRegs.push_back(Dst); | 
|  | 2099 | MIRBuilder.buildLoad(Dst, NewAddrReg, *NewMMO); | 
|  | 2100 | } else { | 
|  | 2101 | MIRBuilder.buildStore(ValRegs[Idx], NewAddrReg, *NewMMO); | 
|  | 2102 | } | 
|  | 2103 | } | 
|  | 2104 |  | 
|  | 2105 | return Offset; | 
|  | 2106 | }; | 
|  | 2107 |  | 
|  | 2108 | unsigned HandledOffset = splitTypePieces(NarrowTy, NarrowRegs, 0); | 
|  | 2109 |  | 
|  | 2110 | // Handle the rest of the register if this isn't an even type breakdown. | 
|  | 2111 | if (LeftoverTy.isValid()) | 
|  | 2112 | splitTypePieces(LeftoverTy, NarrowLeftoverRegs, HandledOffset); | 
|  | 2113 |  | 
|  | 2114 | if (IsLoad) { | 
|  | 2115 | insertParts(ValReg, ValTy, NarrowTy, NarrowRegs, | 
|  | 2116 | LeftoverTy, NarrowLeftoverRegs); | 
|  | 2117 | } | 
|  | 2118 |  | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2119 | MI.eraseFromParent(); | 
|  | 2120 | return Legalized; | 
|  | 2121 | } | 
|  | 2122 |  | 
|  | 2123 | LegalizerHelper::LegalizeResult | 
| Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 2124 | LegalizerHelper::fewerElementsVector(MachineInstr &MI, unsigned TypeIdx, | 
|  | 2125 | LLT NarrowTy) { | 
| Matt Arsenault | 1b1e685 | 2019-01-25 02:59:34 +0000 | [diff] [blame] | 2126 | using namespace TargetOpcode; | 
| Volkan Keles | 574d737 | 2018-12-14 22:11:20 +0000 | [diff] [blame] | 2127 |  | 
|  | 2128 | MIRBuilder.setInstr(MI); | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2129 | switch (MI.getOpcode()) { | 
|  | 2130 | case G_IMPLICIT_DEF: | 
|  | 2131 | return fewerElementsVectorImplicitDef(MI, TypeIdx, NarrowTy); | 
|  | 2132 | case G_AND: | 
|  | 2133 | case G_OR: | 
|  | 2134 | case G_XOR: | 
|  | 2135 | case G_ADD: | 
|  | 2136 | case G_SUB: | 
|  | 2137 | case G_MUL: | 
|  | 2138 | case G_SMULH: | 
|  | 2139 | case G_UMULH: | 
|  | 2140 | case G_FADD: | 
|  | 2141 | case G_FMUL: | 
|  | 2142 | case G_FSUB: | 
|  | 2143 | case G_FNEG: | 
|  | 2144 | case G_FABS: | 
|  | 2145 | case G_FDIV: | 
|  | 2146 | case G_FREM: | 
|  | 2147 | case G_FMA: | 
|  | 2148 | case G_FPOW: | 
|  | 2149 | case G_FEXP: | 
|  | 2150 | case G_FEXP2: | 
|  | 2151 | case G_FLOG: | 
|  | 2152 | case G_FLOG2: | 
|  | 2153 | case G_FLOG10: | 
|  | 2154 | case G_FCEIL: | 
|  | 2155 | case G_INTRINSIC_ROUND: | 
|  | 2156 | case G_INTRINSIC_TRUNC: | 
| Jessica Paquette | 7db82d7 | 2019-01-28 18:34:18 +0000 | [diff] [blame] | 2157 | case G_FCOS: | 
|  | 2158 | case G_FSIN: | 
| Jessica Paquette | 22457f8 | 2019-01-30 21:03:52 +0000 | [diff] [blame] | 2159 | case G_FSQRT: | 
| Matt Arsenault | d1bfc8d | 2019-01-31 02:34:03 +0000 | [diff] [blame] | 2160 | case G_BSWAP: | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2161 | return fewerElementsVectorBasic(MI, TypeIdx, NarrowTy); | 
| Matt Arsenault | c83b823 | 2019-02-07 17:38:00 +0000 | [diff] [blame] | 2162 | case G_SHL: | 
|  | 2163 | case G_LSHR: | 
|  | 2164 | case G_ASHR: | 
|  | 2165 | return fewerElementsVectorMultiEltType(MI, TypeIdx, NarrowTy); | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2166 | case G_ZEXT: | 
|  | 2167 | case G_SEXT: | 
|  | 2168 | case G_ANYEXT: | 
|  | 2169 | case G_FPEXT: | 
|  | 2170 | case G_FPTRUNC: | 
|  | 2171 | case G_SITOFP: | 
|  | 2172 | case G_UITOFP: | 
|  | 2173 | case G_FPTOSI: | 
|  | 2174 | case G_FPTOUI: | 
| Matt Arsenault | cbaada6 | 2019-02-02 23:29:55 +0000 | [diff] [blame] | 2175 | case G_INTTOPTR: | 
|  | 2176 | case G_PTRTOINT: | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2177 | return fewerElementsVectorCasts(MI, TypeIdx, NarrowTy); | 
|  | 2178 | case G_ICMP: | 
|  | 2179 | case G_FCMP: | 
|  | 2180 | return fewerElementsVectorCmp(MI, TypeIdx, NarrowTy); | 
| Matt Arsenault | dc6c785 | 2019-01-30 04:19:31 +0000 | [diff] [blame] | 2181 | case G_SELECT: | 
|  | 2182 | return fewerElementsVectorSelect(MI, TypeIdx, NarrowTy); | 
| Matt Arsenault | 816c9b3e | 2019-01-27 21:53:09 +0000 | [diff] [blame] | 2183 | case G_LOAD: | 
|  | 2184 | case G_STORE: | 
| Matt Arsenault | 7f09fd6 | 2019-02-05 00:26:12 +0000 | [diff] [blame] | 2185 | return reduceLoadStoreWidth(MI, TypeIdx, NarrowTy); | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 2186 | default: | 
|  | 2187 | return UnableToLegalize; | 
| Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 2188 | } | 
|  | 2189 | } | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2190 |  | 
|  | 2191 | LegalizerHelper::LegalizeResult | 
| Matt Arsenault | fbec8fe | 2019-02-07 19:37:44 +0000 | [diff] [blame] | 2192 | LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &MI, const APInt &Amt, | 
|  | 2193 | const LLT HalfTy, const LLT AmtTy) { | 
|  | 2194 |  | 
|  | 2195 | unsigned InL = MRI.createGenericVirtualRegister(HalfTy); | 
|  | 2196 | unsigned InH = MRI.createGenericVirtualRegister(HalfTy); | 
|  | 2197 | MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1).getReg()); | 
|  | 2198 |  | 
|  | 2199 | if (Amt.isNullValue()) { | 
|  | 2200 | MIRBuilder.buildMerge(MI.getOperand(0).getReg(), {InL, InH}); | 
|  | 2201 | MI.eraseFromParent(); | 
|  | 2202 | return Legalized; | 
|  | 2203 | } | 
|  | 2204 |  | 
|  | 2205 | LLT NVT = HalfTy; | 
|  | 2206 | unsigned NVTBits = HalfTy.getSizeInBits(); | 
|  | 2207 | unsigned VTBits = 2 * NVTBits; | 
|  | 2208 |  | 
|  | 2209 | SrcOp Lo(0), Hi(0); | 
|  | 2210 | if (MI.getOpcode() == TargetOpcode::G_SHL) { | 
|  | 2211 | if (Amt.ugt(VTBits)) { | 
|  | 2212 | Lo = Hi = MIRBuilder.buildConstant(NVT, 0); | 
|  | 2213 | } else if (Amt.ugt(NVTBits)) { | 
|  | 2214 | Lo = MIRBuilder.buildConstant(NVT, 0); | 
|  | 2215 | Hi = MIRBuilder.buildShl(NVT, InL, | 
|  | 2216 | MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); | 
|  | 2217 | } else if (Amt == NVTBits) { | 
|  | 2218 | Lo = MIRBuilder.buildConstant(NVT, 0); | 
|  | 2219 | Hi = InL; | 
|  | 2220 | } else { | 
|  | 2221 | Lo = MIRBuilder.buildShl(NVT, InL, MIRBuilder.buildConstant(AmtTy, Amt)); | 
| Matt Arsenault | e98cab1 | 2019-02-07 20:44:08 +0000 | [diff] [blame^] | 2222 | auto OrLHS = | 
|  | 2223 | MIRBuilder.buildShl(NVT, InH, MIRBuilder.buildConstant(AmtTy, Amt)); | 
|  | 2224 | auto OrRHS = MIRBuilder.buildLShr( | 
|  | 2225 | NVT, InL, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); | 
|  | 2226 | Hi = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); | 
| Matt Arsenault | fbec8fe | 2019-02-07 19:37:44 +0000 | [diff] [blame] | 2227 | } | 
|  | 2228 | } else if (MI.getOpcode() == TargetOpcode::G_LSHR) { | 
|  | 2229 | if (Amt.ugt(VTBits)) { | 
|  | 2230 | Lo = Hi = MIRBuilder.buildConstant(NVT, 0); | 
|  | 2231 | } else if (Amt.ugt(NVTBits)) { | 
|  | 2232 | Lo = MIRBuilder.buildLShr(NVT, InH, | 
|  | 2233 | MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); | 
|  | 2234 | Hi = MIRBuilder.buildConstant(NVT, 0); | 
|  | 2235 | } else if (Amt == NVTBits) { | 
|  | 2236 | Lo = InH; | 
|  | 2237 | Hi = MIRBuilder.buildConstant(NVT, 0); | 
|  | 2238 | } else { | 
|  | 2239 | auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt); | 
|  | 2240 |  | 
|  | 2241 | auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst); | 
|  | 2242 | auto OrRHS = MIRBuilder.buildShl( | 
|  | 2243 | NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); | 
|  | 2244 |  | 
|  | 2245 | Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); | 
|  | 2246 | Hi = MIRBuilder.buildLShr(NVT, InH, ShiftAmtConst); | 
|  | 2247 | } | 
|  | 2248 | } else { | 
|  | 2249 | if (Amt.ugt(VTBits)) { | 
|  | 2250 | Hi = Lo = MIRBuilder.buildAShr( | 
|  | 2251 | NVT, InH, MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); | 
|  | 2252 | } else if (Amt.ugt(NVTBits)) { | 
|  | 2253 | Lo = MIRBuilder.buildAShr(NVT, InH, | 
|  | 2254 | MIRBuilder.buildConstant(AmtTy, Amt - NVTBits)); | 
|  | 2255 | Hi = MIRBuilder.buildAShr(NVT, InH, | 
|  | 2256 | MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); | 
|  | 2257 | } else if (Amt == NVTBits) { | 
|  | 2258 | Lo = InH; | 
|  | 2259 | Hi = MIRBuilder.buildAShr(NVT, InH, | 
|  | 2260 | MIRBuilder.buildConstant(AmtTy, NVTBits - 1)); | 
|  | 2261 | } else { | 
|  | 2262 | auto ShiftAmtConst = MIRBuilder.buildConstant(AmtTy, Amt); | 
|  | 2263 |  | 
|  | 2264 | auto OrLHS = MIRBuilder.buildLShr(NVT, InL, ShiftAmtConst); | 
|  | 2265 | auto OrRHS = MIRBuilder.buildShl( | 
|  | 2266 | NVT, InH, MIRBuilder.buildConstant(AmtTy, -Amt + NVTBits)); | 
|  | 2267 |  | 
|  | 2268 | Lo = MIRBuilder.buildOr(NVT, OrLHS, OrRHS); | 
|  | 2269 | Hi = MIRBuilder.buildAShr(NVT, InH, ShiftAmtConst); | 
|  | 2270 | } | 
|  | 2271 | } | 
|  | 2272 |  | 
|  | 2273 | MIRBuilder.buildMerge(MI.getOperand(0).getReg(), {Lo.getReg(), Hi.getReg()}); | 
|  | 2274 | MI.eraseFromParent(); | 
|  | 2275 |  | 
|  | 2276 | return Legalized; | 
|  | 2277 | } | 
|  | 2278 |  | 
|  | 2279 | // TODO: Optimize if constant shift amount. | 
|  | 2280 | LegalizerHelper::LegalizeResult | 
|  | 2281 | LegalizerHelper::narrowScalarShift(MachineInstr &MI, unsigned TypeIdx, | 
|  | 2282 | LLT RequestedTy) { | 
|  | 2283 | if (TypeIdx == 1) { | 
|  | 2284 | Observer.changingInstr(MI); | 
|  | 2285 | narrowScalarSrc(MI, RequestedTy, 2); | 
|  | 2286 | Observer.changedInstr(MI); | 
|  | 2287 | return Legalized; | 
|  | 2288 | } | 
|  | 2289 |  | 
|  | 2290 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 2291 | LLT DstTy = MRI.getType(DstReg); | 
|  | 2292 | if (DstTy.isVector()) | 
|  | 2293 | return UnableToLegalize; | 
|  | 2294 |  | 
|  | 2295 | unsigned Amt = MI.getOperand(2).getReg(); | 
|  | 2296 | LLT ShiftAmtTy = MRI.getType(Amt); | 
|  | 2297 | const unsigned DstEltSize = DstTy.getScalarSizeInBits(); | 
|  | 2298 | if (DstEltSize % 2 != 0) | 
|  | 2299 | return UnableToLegalize; | 
|  | 2300 |  | 
|  | 2301 | // Ignore the input type. We can only go to exactly half the size of the | 
|  | 2302 | // input. If that isn't small enough, the resulting pieces will be further | 
|  | 2303 | // legalized. | 
|  | 2304 | const unsigned NewBitSize = DstEltSize / 2; | 
|  | 2305 | const LLT HalfTy = LLT::scalar(NewBitSize); | 
|  | 2306 | const LLT CondTy = LLT::scalar(1); | 
|  | 2307 |  | 
|  | 2308 | if (const MachineInstr *KShiftAmt = | 
|  | 2309 | getOpcodeDef(TargetOpcode::G_CONSTANT, Amt, MRI)) { | 
|  | 2310 | return narrowScalarShiftByConstant( | 
|  | 2311 | MI, KShiftAmt->getOperand(1).getCImm()->getValue(), HalfTy, ShiftAmtTy); | 
|  | 2312 | } | 
|  | 2313 |  | 
|  | 2314 | // TODO: Expand with known bits. | 
|  | 2315 |  | 
|  | 2316 | // Handle the fully general expansion by an unknown amount. | 
|  | 2317 | auto NewBits = MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize); | 
|  | 2318 |  | 
|  | 2319 | unsigned InL = MRI.createGenericVirtualRegister(HalfTy); | 
|  | 2320 | unsigned InH = MRI.createGenericVirtualRegister(HalfTy); | 
|  | 2321 | MIRBuilder.buildUnmerge({InL, InH}, MI.getOperand(1).getReg()); | 
|  | 2322 |  | 
|  | 2323 | auto AmtExcess = MIRBuilder.buildSub(ShiftAmtTy, Amt, NewBits); | 
|  | 2324 | auto AmtLack = MIRBuilder.buildSub(ShiftAmtTy, NewBits, Amt); | 
|  | 2325 |  | 
|  | 2326 | auto Zero = MIRBuilder.buildConstant(ShiftAmtTy, 0); | 
|  | 2327 | auto IsShort = MIRBuilder.buildICmp(ICmpInst::ICMP_ULT, CondTy, Amt, NewBits); | 
|  | 2328 | auto IsZero = MIRBuilder.buildICmp(ICmpInst::ICMP_EQ, CondTy, Amt, Zero); | 
|  | 2329 |  | 
|  | 2330 | unsigned ResultRegs[2]; | 
|  | 2331 | switch (MI.getOpcode()) { | 
|  | 2332 | case TargetOpcode::G_SHL: { | 
|  | 2333 | // Short: ShAmt < NewBitSize | 
|  | 2334 | auto LoS = MIRBuilder.buildShl(HalfTy, InH, Amt); | 
|  | 2335 |  | 
|  | 2336 | auto OrLHS = MIRBuilder.buildShl(HalfTy, InH, Amt); | 
|  | 2337 | auto OrRHS = MIRBuilder.buildLShr(HalfTy, InL, AmtLack); | 
|  | 2338 | auto HiS = MIRBuilder.buildOr(HalfTy, OrLHS, OrRHS); | 
|  | 2339 |  | 
|  | 2340 | // Long: ShAmt >= NewBitSize | 
|  | 2341 | auto LoL = MIRBuilder.buildConstant(HalfTy, 0);         // Lo part is zero. | 
|  | 2342 | auto HiL = MIRBuilder.buildShl(HalfTy, InL, AmtExcess); // Hi from Lo part. | 
|  | 2343 |  | 
|  | 2344 | auto Lo = MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL); | 
|  | 2345 | auto Hi = MIRBuilder.buildSelect( | 
|  | 2346 | HalfTy, IsZero, InH, MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL)); | 
|  | 2347 |  | 
|  | 2348 | ResultRegs[0] = Lo.getReg(0); | 
|  | 2349 | ResultRegs[1] = Hi.getReg(0); | 
|  | 2350 | break; | 
|  | 2351 | } | 
|  | 2352 | case TargetOpcode::G_LSHR: { | 
|  | 2353 | // Short: ShAmt < NewBitSize | 
|  | 2354 | auto HiS = MIRBuilder.buildLShr(HalfTy, InH, Amt); | 
|  | 2355 |  | 
|  | 2356 | auto OrLHS = MIRBuilder.buildLShr(HalfTy, InL, Amt); | 
|  | 2357 | auto OrRHS = MIRBuilder.buildShl(HalfTy, InH, AmtLack); | 
|  | 2358 | auto LoS = MIRBuilder.buildOr(HalfTy, OrLHS, OrRHS); | 
|  | 2359 |  | 
|  | 2360 | // Long: ShAmt >= NewBitSize | 
|  | 2361 | auto HiL = MIRBuilder.buildConstant(HalfTy, 0);          // Hi part is zero. | 
|  | 2362 | auto LoL = MIRBuilder.buildLShr(HalfTy, InH, AmtExcess); // Lo from Hi part. | 
|  | 2363 |  | 
|  | 2364 | auto Lo = MIRBuilder.buildSelect( | 
|  | 2365 | HalfTy, IsZero, InL, MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL)); | 
|  | 2366 | auto Hi = MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL); | 
|  | 2367 |  | 
|  | 2368 | ResultRegs[0] = Lo.getReg(0); | 
|  | 2369 | ResultRegs[1] = Hi.getReg(0); | 
|  | 2370 | break; | 
|  | 2371 | } | 
|  | 2372 | case TargetOpcode::G_ASHR: { | 
|  | 2373 | // Short: ShAmt < NewBitSize | 
|  | 2374 | auto HiS = MIRBuilder.buildAShr(HalfTy, InH, Amt); | 
|  | 2375 |  | 
|  | 2376 | auto OrLHS = MIRBuilder.buildLShr(HalfTy, InL, Amt); | 
|  | 2377 | auto OrRHS = MIRBuilder.buildLShr(HalfTy, InH, AmtLack); | 
|  | 2378 | auto LoS = MIRBuilder.buildOr(HalfTy, OrLHS, OrRHS); | 
|  | 2379 |  | 
|  | 2380 | // Long: ShAmt >= NewBitSize | 
|  | 2381 |  | 
|  | 2382 | // Sign of Hi part. | 
|  | 2383 | auto HiL = MIRBuilder.buildAShr( | 
|  | 2384 | HalfTy, InH, MIRBuilder.buildConstant(ShiftAmtTy, NewBitSize - 1)); | 
|  | 2385 |  | 
|  | 2386 | auto LoL = MIRBuilder.buildAShr(HalfTy, InH, AmtExcess); // Lo from Hi part. | 
|  | 2387 |  | 
|  | 2388 | auto Lo = MIRBuilder.buildSelect( | 
|  | 2389 | HalfTy, IsZero, InL, MIRBuilder.buildSelect(HalfTy, IsShort, LoS, LoL)); | 
|  | 2390 |  | 
|  | 2391 | auto Hi = MIRBuilder.buildSelect(HalfTy, IsShort, HiS, HiL); | 
|  | 2392 |  | 
|  | 2393 | ResultRegs[0] = Lo.getReg(0); | 
|  | 2394 | ResultRegs[1] = Hi.getReg(0); | 
|  | 2395 | break; | 
|  | 2396 | } | 
|  | 2397 | default: | 
|  | 2398 | llvm_unreachable("not a shift"); | 
|  | 2399 | } | 
|  | 2400 |  | 
|  | 2401 | MIRBuilder.buildMerge(DstReg, ResultRegs); | 
|  | 2402 | MI.eraseFromParent(); | 
|  | 2403 | return Legalized; | 
|  | 2404 | } | 
|  | 2405 |  | 
|  | 2406 | LegalizerHelper::LegalizeResult | 
| Matt Arsenault | 211e89d | 2019-01-27 00:52:51 +0000 | [diff] [blame] | 2407 | LegalizerHelper::narrowScalarMul(MachineInstr &MI, unsigned TypeIdx, LLT NewTy) { | 
|  | 2408 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 2409 | unsigned Src0 = MI.getOperand(1).getReg(); | 
|  | 2410 | unsigned Src1 = MI.getOperand(2).getReg(); | 
|  | 2411 | LLT Ty = MRI.getType(DstReg); | 
|  | 2412 | if (Ty.isVector()) | 
|  | 2413 | return UnableToLegalize; | 
|  | 2414 |  | 
|  | 2415 | unsigned Size = Ty.getSizeInBits(); | 
|  | 2416 | unsigned NewSize = Size / 2; | 
|  | 2417 | if (Size != 2 * NewSize) | 
|  | 2418 | return UnableToLegalize; | 
|  | 2419 |  | 
|  | 2420 | LLT HalfTy = LLT::scalar(NewSize); | 
|  | 2421 | // TODO: if HalfTy != NewTy, handle the breakdown all at once? | 
|  | 2422 |  | 
|  | 2423 | unsigned ShiftAmt = MRI.createGenericVirtualRegister(Ty); | 
|  | 2424 | unsigned Lo = MRI.createGenericVirtualRegister(HalfTy); | 
|  | 2425 | unsigned Hi = MRI.createGenericVirtualRegister(HalfTy); | 
|  | 2426 | unsigned ExtLo = MRI.createGenericVirtualRegister(Ty); | 
|  | 2427 | unsigned ExtHi = MRI.createGenericVirtualRegister(Ty); | 
|  | 2428 | unsigned ShiftedHi = MRI.createGenericVirtualRegister(Ty); | 
|  | 2429 |  | 
|  | 2430 | SmallVector<unsigned, 2> Src0Parts; | 
|  | 2431 | SmallVector<unsigned, 2> Src1Parts; | 
|  | 2432 |  | 
|  | 2433 | extractParts(Src0, HalfTy, 2, Src0Parts); | 
|  | 2434 | extractParts(Src1, HalfTy, 2, Src1Parts); | 
|  | 2435 |  | 
|  | 2436 | MIRBuilder.buildMul(Lo, Src0Parts[0], Src1Parts[0]); | 
|  | 2437 |  | 
|  | 2438 | // TODO: Use smulh or umulh depending on what the target has. | 
|  | 2439 | MIRBuilder.buildUMulH(Hi, Src0Parts[1], Src1Parts[1]); | 
|  | 2440 |  | 
|  | 2441 | MIRBuilder.buildConstant(ShiftAmt, NewSize); | 
|  | 2442 | MIRBuilder.buildAnyExt(ExtHi, Hi); | 
|  | 2443 | MIRBuilder.buildShl(ShiftedHi, ExtHi, ShiftAmt); | 
|  | 2444 |  | 
|  | 2445 | MIRBuilder.buildZExt(ExtLo, Lo); | 
|  | 2446 | MIRBuilder.buildOr(DstReg, ExtLo, ShiftedHi); | 
|  | 2447 | MI.eraseFromParent(); | 
|  | 2448 | return Legalized; | 
|  | 2449 | } | 
|  | 2450 |  | 
|  | 2451 | LegalizerHelper::LegalizeResult | 
| Matt Arsenault | 81511e5 | 2019-02-05 00:13:44 +0000 | [diff] [blame] | 2452 | LegalizerHelper::narrowScalarSelect(MachineInstr &MI, unsigned TypeIdx, | 
|  | 2453 | LLT NarrowTy) { | 
|  | 2454 | if (TypeIdx != 0) | 
|  | 2455 | return UnableToLegalize; | 
|  | 2456 |  | 
|  | 2457 | unsigned CondReg = MI.getOperand(1).getReg(); | 
|  | 2458 | LLT CondTy = MRI.getType(CondReg); | 
|  | 2459 | if (CondTy.isVector()) // TODO: Handle vselect | 
|  | 2460 | return UnableToLegalize; | 
|  | 2461 |  | 
|  | 2462 | unsigned DstReg = MI.getOperand(0).getReg(); | 
|  | 2463 | LLT DstTy = MRI.getType(DstReg); | 
|  | 2464 |  | 
|  | 2465 | SmallVector<unsigned, 4> DstRegs, DstLeftoverRegs; | 
|  | 2466 | SmallVector<unsigned, 4> Src1Regs, Src1LeftoverRegs; | 
|  | 2467 | SmallVector<unsigned, 4> Src2Regs, Src2LeftoverRegs; | 
|  | 2468 | LLT LeftoverTy; | 
|  | 2469 | if (!extractParts(MI.getOperand(2).getReg(), DstTy, NarrowTy, LeftoverTy, | 
|  | 2470 | Src1Regs, Src1LeftoverRegs)) | 
|  | 2471 | return UnableToLegalize; | 
|  | 2472 |  | 
|  | 2473 | LLT Unused; | 
|  | 2474 | if (!extractParts(MI.getOperand(3).getReg(), DstTy, NarrowTy, Unused, | 
|  | 2475 | Src2Regs, Src2LeftoverRegs)) | 
|  | 2476 | llvm_unreachable("inconsistent extractParts result"); | 
|  | 2477 |  | 
|  | 2478 | for (unsigned I = 0, E = Src1Regs.size(); I != E; ++I) { | 
|  | 2479 | auto Select = MIRBuilder.buildSelect(NarrowTy, | 
|  | 2480 | CondReg, Src1Regs[I], Src2Regs[I]); | 
|  | 2481 | DstRegs.push_back(Select->getOperand(0).getReg()); | 
|  | 2482 | } | 
|  | 2483 |  | 
|  | 2484 | for (unsigned I = 0, E = Src1LeftoverRegs.size(); I != E; ++I) { | 
|  | 2485 | auto Select = MIRBuilder.buildSelect( | 
|  | 2486 | LeftoverTy, CondReg, Src1LeftoverRegs[I], Src2LeftoverRegs[I]); | 
|  | 2487 | DstLeftoverRegs.push_back(Select->getOperand(0).getReg()); | 
|  | 2488 | } | 
|  | 2489 |  | 
|  | 2490 | insertParts(DstReg, DstTy, NarrowTy, DstRegs, | 
|  | 2491 | LeftoverTy, DstLeftoverRegs); | 
|  | 2492 |  | 
|  | 2493 | MI.eraseFromParent(); | 
|  | 2494 | return Legalized; | 
|  | 2495 | } | 
|  | 2496 |  | 
|  | 2497 | LegalizerHelper::LegalizeResult | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2498 | LegalizerHelper::lowerBitCount(MachineInstr &MI, unsigned TypeIdx, LLT Ty) { | 
|  | 2499 | unsigned Opc = MI.getOpcode(); | 
|  | 2500 | auto &TII = *MI.getMF()->getSubtarget().getInstrInfo(); | 
| Diana Picus | 0528e2c | 2018-11-26 11:07:02 +0000 | [diff] [blame] | 2501 | auto isSupported = [this](const LegalityQuery &Q) { | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2502 | auto QAction = LI.getAction(Q).Action; | 
| Diana Picus | 0528e2c | 2018-11-26 11:07:02 +0000 | [diff] [blame] | 2503 | return QAction == Legal || QAction == Libcall || QAction == Custom; | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2504 | }; | 
|  | 2505 | switch (Opc) { | 
|  | 2506 | default: | 
|  | 2507 | return UnableToLegalize; | 
|  | 2508 | case TargetOpcode::G_CTLZ_ZERO_UNDEF: { | 
|  | 2509 | // This trivially expands to CTLZ. | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 2510 | Observer.changingInstr(MI); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2511 | MI.setDesc(TII.get(TargetOpcode::G_CTLZ)); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 2512 | Observer.changedInstr(MI); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2513 | return Legalized; | 
|  | 2514 | } | 
|  | 2515 | case TargetOpcode::G_CTLZ: { | 
|  | 2516 | unsigned SrcReg = MI.getOperand(1).getReg(); | 
|  | 2517 | unsigned Len = Ty.getSizeInBits(); | 
| Matt Arsenault | d5684f7 | 2019-01-31 02:09:57 +0000 | [diff] [blame] | 2518 | if (isSupported({TargetOpcode::G_CTLZ_ZERO_UNDEF, {Ty, Ty}})) { | 
| Diana Picus | 0528e2c | 2018-11-26 11:07:02 +0000 | [diff] [blame] | 2519 | // If CTLZ_ZERO_UNDEF is supported, emit that and a select for zero. | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 2520 | auto MIBCtlzZU = MIRBuilder.buildInstr(TargetOpcode::G_CTLZ_ZERO_UNDEF, | 
|  | 2521 | {Ty}, {SrcReg}); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2522 | auto MIBZero = MIRBuilder.buildConstant(Ty, 0); | 
|  | 2523 | auto MIBLen = MIRBuilder.buildConstant(Ty, Len); | 
|  | 2524 | auto MIBICmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1), | 
|  | 2525 | SrcReg, MIBZero); | 
|  | 2526 | MIRBuilder.buildSelect(MI.getOperand(0).getReg(), MIBICmp, MIBLen, | 
|  | 2527 | MIBCtlzZU); | 
|  | 2528 | MI.eraseFromParent(); | 
|  | 2529 | return Legalized; | 
|  | 2530 | } | 
|  | 2531 | // for now, we do this: | 
|  | 2532 | // NewLen = NextPowerOf2(Len); | 
|  | 2533 | // x = x | (x >> 1); | 
|  | 2534 | // x = x | (x >> 2); | 
|  | 2535 | // ... | 
|  | 2536 | // x = x | (x >>16); | 
|  | 2537 | // x = x | (x >>32); // for 64-bit input | 
|  | 2538 | // Upto NewLen/2 | 
|  | 2539 | // return Len - popcount(x); | 
|  | 2540 | // | 
|  | 2541 | // Ref: "Hacker's Delight" by Henry Warren | 
|  | 2542 | unsigned Op = SrcReg; | 
|  | 2543 | unsigned NewLen = PowerOf2Ceil(Len); | 
|  | 2544 | for (unsigned i = 0; (1U << i) <= (NewLen / 2); ++i) { | 
|  | 2545 | auto MIBShiftAmt = MIRBuilder.buildConstant(Ty, 1ULL << i); | 
|  | 2546 | auto MIBOp = MIRBuilder.buildInstr( | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 2547 | TargetOpcode::G_OR, {Ty}, | 
|  | 2548 | {Op, MIRBuilder.buildInstr(TargetOpcode::G_LSHR, {Ty}, | 
|  | 2549 | {Op, MIBShiftAmt})}); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2550 | Op = MIBOp->getOperand(0).getReg(); | 
|  | 2551 | } | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 2552 | auto MIBPop = MIRBuilder.buildInstr(TargetOpcode::G_CTPOP, {Ty}, {Op}); | 
|  | 2553 | MIRBuilder.buildInstr(TargetOpcode::G_SUB, {MI.getOperand(0).getReg()}, | 
|  | 2554 | {MIRBuilder.buildConstant(Ty, Len), MIBPop}); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2555 | MI.eraseFromParent(); | 
|  | 2556 | return Legalized; | 
|  | 2557 | } | 
|  | 2558 | case TargetOpcode::G_CTTZ_ZERO_UNDEF: { | 
|  | 2559 | // This trivially expands to CTTZ. | 
| Daniel Sanders | d001e0e | 2018-12-12 23:48:13 +0000 | [diff] [blame] | 2560 | Observer.changingInstr(MI); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2561 | MI.setDesc(TII.get(TargetOpcode::G_CTTZ)); | 
| Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 2562 | Observer.changedInstr(MI); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2563 | return Legalized; | 
|  | 2564 | } | 
|  | 2565 | case TargetOpcode::G_CTTZ: { | 
|  | 2566 | unsigned SrcReg = MI.getOperand(1).getReg(); | 
|  | 2567 | unsigned Len = Ty.getSizeInBits(); | 
| Matt Arsenault | d5684f7 | 2019-01-31 02:09:57 +0000 | [diff] [blame] | 2568 | if (isSupported({TargetOpcode::G_CTTZ_ZERO_UNDEF, {Ty, Ty}})) { | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2569 | // If CTTZ_ZERO_UNDEF is legal or custom, emit that and a select with | 
|  | 2570 | // zero. | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 2571 | auto MIBCttzZU = MIRBuilder.buildInstr(TargetOpcode::G_CTTZ_ZERO_UNDEF, | 
|  | 2572 | {Ty}, {SrcReg}); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2573 | auto MIBZero = MIRBuilder.buildConstant(Ty, 0); | 
|  | 2574 | auto MIBLen = MIRBuilder.buildConstant(Ty, Len); | 
|  | 2575 | auto MIBICmp = MIRBuilder.buildICmp(CmpInst::ICMP_EQ, LLT::scalar(1), | 
|  | 2576 | SrcReg, MIBZero); | 
|  | 2577 | MIRBuilder.buildSelect(MI.getOperand(0).getReg(), MIBICmp, MIBLen, | 
|  | 2578 | MIBCttzZU); | 
|  | 2579 | MI.eraseFromParent(); | 
|  | 2580 | return Legalized; | 
|  | 2581 | } | 
|  | 2582 | // for now, we use: { return popcount(~x & (x - 1)); } | 
|  | 2583 | // unless the target has ctlz but not ctpop, in which case we use: | 
|  | 2584 | // { return 32 - nlz(~x & (x-1)); } | 
|  | 2585 | // Ref: "Hacker's Delight" by Henry Warren | 
|  | 2586 | auto MIBCstNeg1 = MIRBuilder.buildConstant(Ty, -1); | 
|  | 2587 | auto MIBNot = | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 2588 | MIRBuilder.buildInstr(TargetOpcode::G_XOR, {Ty}, {SrcReg, MIBCstNeg1}); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2589 | auto MIBTmp = MIRBuilder.buildInstr( | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 2590 | TargetOpcode::G_AND, {Ty}, | 
|  | 2591 | {MIBNot, MIRBuilder.buildInstr(TargetOpcode::G_ADD, {Ty}, | 
|  | 2592 | {SrcReg, MIBCstNeg1})}); | 
| Matt Arsenault | d5684f7 | 2019-01-31 02:09:57 +0000 | [diff] [blame] | 2593 | if (!isSupported({TargetOpcode::G_CTPOP, {Ty, Ty}}) && | 
|  | 2594 | isSupported({TargetOpcode::G_CTLZ, {Ty, Ty}})) { | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2595 | auto MIBCstLen = MIRBuilder.buildConstant(Ty, Len); | 
|  | 2596 | MIRBuilder.buildInstr( | 
| Aditya Nandakumar | cef44a2 | 2018-12-11 00:48:50 +0000 | [diff] [blame] | 2597 | TargetOpcode::G_SUB, {MI.getOperand(0).getReg()}, | 
|  | 2598 | {MIBCstLen, | 
|  | 2599 | MIRBuilder.buildInstr(TargetOpcode::G_CTLZ, {Ty}, {MIBTmp})}); | 
| Aditya Nandakumar | c0333f7 | 2018-08-21 17:30:31 +0000 | [diff] [blame] | 2600 | MI.eraseFromParent(); | 
|  | 2601 | return Legalized; | 
|  | 2602 | } | 
|  | 2603 | MI.setDesc(TII.get(TargetOpcode::G_CTPOP)); | 
|  | 2604 | MI.getOperand(1).setReg(MIBTmp->getOperand(0).getReg()); | 
|  | 2605 | return Legalized; | 
|  | 2606 | } | 
|  | 2607 | } | 
|  | 2608 | } |