LegalizeVectorOps.cpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. //===- LegalizeVectorOps.cpp - Implement SelectionDAG::LegalizeVectors ----===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the SelectionDAG::LegalizeVectors method.
  10. //
  11. // The vector legalizer looks for vector operations which might need to be
  12. // scalarized and legalizes them. This is a separate step from Legalize because
  13. // scalarizing can introduce illegal types. For example, suppose we have an
  14. // ISD::SDIV of type v2i64 on x86-32. The type is legal (for example, addition
  15. // on a v2i64 is legal), but ISD::SDIV isn't legal, so we have to unroll the
  16. // operation, which introduces nodes with the illegal type i64 which must be
  17. // expanded. Similarly, suppose we have an ISD::SRA of type v16i8 on PowerPC;
  18. // the operation must be unrolled, which introduces nodes with the illegal
  19. // type i8 which must be promoted.
  20. //
  21. // This does not legalize vector manipulations like ISD::BUILD_VECTOR,
  22. // or operations that happen to take a vector which are custom-lowered;
  23. // the legalization for such operations never produces nodes
  24. // with illegal types, so it's okay to put off legalizing them until
  25. // SelectionDAG::Legalize runs.
  26. //
  27. //===----------------------------------------------------------------------===//
  28. #include "llvm/ADT/APInt.h"
  29. #include "llvm/ADT/DenseMap.h"
  30. #include "llvm/ADT/SmallVector.h"
  31. #include "llvm/CodeGen/ISDOpcodes.h"
  32. #include "llvm/CodeGen/MachineMemOperand.h"
  33. #include "llvm/CodeGen/SelectionDAG.h"
  34. #include "llvm/CodeGen/SelectionDAGNodes.h"
  35. #include "llvm/CodeGen/TargetLowering.h"
  36. #include "llvm/CodeGen/ValueTypes.h"
  37. #include "llvm/IR/DataLayout.h"
  38. #include "llvm/Support/Casting.h"
  39. #include "llvm/Support/Compiler.h"
  40. #include "llvm/Support/Debug.h"
  41. #include "llvm/Support/ErrorHandling.h"
  42. #include "llvm/Support/MachineValueType.h"
  43. #include "llvm/Support/MathExtras.h"
  44. #include <cassert>
  45. #include <cstdint>
  46. #include <iterator>
  47. #include <utility>
  48. using namespace llvm;
  49. #define DEBUG_TYPE "legalizevectorops"
  50. namespace {
  51. class VectorLegalizer {
  52. SelectionDAG& DAG;
  53. const TargetLowering &TLI;
  54. bool Changed = false; // Keep track of whether anything changed
  55. /// For nodes that are of legal width, and that have more than one use, this
  56. /// map indicates what regularized operand to use. This allows us to avoid
  57. /// legalizing the same thing more than once.
  58. SmallDenseMap<SDValue, SDValue, 64> LegalizedNodes;
  59. /// Adds a node to the translation cache.
  60. void AddLegalizedOperand(SDValue From, SDValue To) {
  61. LegalizedNodes.insert(std::make_pair(From, To));
  62. // If someone requests legalization of the new node, return itself.
  63. if (From != To)
  64. LegalizedNodes.insert(std::make_pair(To, To));
  65. }
  66. /// Legalizes the given node.
  67. SDValue LegalizeOp(SDValue Op);
  68. /// Assuming the node is legal, "legalize" the results.
  69. SDValue TranslateLegalizeResults(SDValue Op, SDNode *Result);
  70. /// Make sure Results are legal and update the translation cache.
  71. SDValue RecursivelyLegalizeResults(SDValue Op,
  72. MutableArrayRef<SDValue> Results);
  73. /// Wrapper to interface LowerOperation with a vector of Results.
  74. /// Returns false if the target wants to use default expansion. Otherwise
  75. /// returns true. If return is true and the Results are empty, then the
  76. /// target wants to keep the input node as is.
  77. bool LowerOperationWrapper(SDNode *N, SmallVectorImpl<SDValue> &Results);
  78. /// Implements unrolling a VSETCC.
  79. SDValue UnrollVSETCC(SDNode *Node);
  80. /// Implement expand-based legalization of vector operations.
  81. ///
  82. /// This is just a high-level routine to dispatch to specific code paths for
  83. /// operations to legalize them.
  84. void Expand(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  85. /// Implements expansion for FP_TO_UINT; falls back to UnrollVectorOp if
  86. /// FP_TO_SINT isn't legal.
  87. void ExpandFP_TO_UINT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  88. /// Implements expansion for UINT_TO_FLOAT; falls back to UnrollVectorOp if
  89. /// SINT_TO_FLOAT and SHR on vectors isn't legal.
  90. void ExpandUINT_TO_FLOAT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  91. /// Implement expansion for SIGN_EXTEND_INREG using SRL and SRA.
  92. SDValue ExpandSEXTINREG(SDNode *Node);
  93. /// Implement expansion for ANY_EXTEND_VECTOR_INREG.
  94. ///
  95. /// Shuffles the low lanes of the operand into place and bitcasts to the proper
  96. /// type. The contents of the bits in the extended part of each element are
  97. /// undef.
  98. SDValue ExpandANY_EXTEND_VECTOR_INREG(SDNode *Node);
  99. /// Implement expansion for SIGN_EXTEND_VECTOR_INREG.
  100. ///
  101. /// Shuffles the low lanes of the operand into place, bitcasts to the proper
  102. /// type, then shifts left and arithmetic shifts right to introduce a sign
  103. /// extension.
  104. SDValue ExpandSIGN_EXTEND_VECTOR_INREG(SDNode *Node);
  105. /// Implement expansion for ZERO_EXTEND_VECTOR_INREG.
  106. ///
  107. /// Shuffles the low lanes of the operand into place and blends zeros into
  108. /// the remaining lanes, finally bitcasting to the proper type.
  109. SDValue ExpandZERO_EXTEND_VECTOR_INREG(SDNode *Node);
  110. /// Expand bswap of vectors into a shuffle if legal.
  111. SDValue ExpandBSWAP(SDNode *Node);
  112. /// Implement vselect in terms of XOR, AND, OR when blend is not
  113. /// supported by the target.
  114. SDValue ExpandVSELECT(SDNode *Node);
  115. SDValue ExpandVP_SELECT(SDNode *Node);
  116. SDValue ExpandVP_MERGE(SDNode *Node);
  117. SDValue ExpandSELECT(SDNode *Node);
  118. std::pair<SDValue, SDValue> ExpandLoad(SDNode *N);
  119. SDValue ExpandStore(SDNode *N);
  120. SDValue ExpandFNEG(SDNode *Node);
  121. void ExpandFSUB(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  122. void ExpandSETCC(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  123. void ExpandBITREVERSE(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  124. void ExpandUADDSUBO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  125. void ExpandSADDSUBO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  126. void ExpandMULO(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  127. void ExpandFixedPointDiv(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  128. void ExpandStrictFPOp(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  129. void ExpandREM(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  130. void UnrollStrictFPOp(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  131. /// Implements vector promotion.
  132. ///
  133. /// This is essentially just bitcasting the operands to a different type and
  134. /// bitcasting the result back to the original type.
  135. void Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  136. /// Implements [SU]INT_TO_FP vector promotion.
  137. ///
  138. /// This is a [zs]ext of the input operand to a larger integer type.
  139. void PromoteINT_TO_FP(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  140. /// Implements FP_TO_[SU]INT vector promotion of the result type.
  141. ///
  142. /// It is promoted to a larger integer type. The result is then
  143. /// truncated back to the original type.
  144. void PromoteFP_TO_INT(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  145. public:
  146. VectorLegalizer(SelectionDAG& dag) :
  147. DAG(dag), TLI(dag.getTargetLoweringInfo()) {}
  148. /// Begin legalizer the vector operations in the DAG.
  149. bool Run();
  150. };
  151. } // end anonymous namespace
  152. bool VectorLegalizer::Run() {
  153. // Before we start legalizing vector nodes, check if there are any vectors.
  154. bool HasVectors = false;
  155. for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
  156. E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I) {
  157. // Check if the values of the nodes contain vectors. We don't need to check
  158. // the operands because we are going to check their values at some point.
  159. HasVectors = llvm::any_of(I->values(), [](EVT T) { return T.isVector(); });
  160. // If we found a vector node we can start the legalization.
  161. if (HasVectors)
  162. break;
  163. }
  164. // If this basic block has no vectors then no need to legalize vectors.
  165. if (!HasVectors)
  166. return false;
  167. // The legalize process is inherently a bottom-up recursive process (users
  168. // legalize their uses before themselves). Given infinite stack space, we
  169. // could just start legalizing on the root and traverse the whole graph. In
  170. // practice however, this causes us to run out of stack space on large basic
  171. // blocks. To avoid this problem, compute an ordering of the nodes where each
  172. // node is only legalized after all of its operands are legalized.
  173. DAG.AssignTopologicalOrder();
  174. for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
  175. E = std::prev(DAG.allnodes_end()); I != std::next(E); ++I)
  176. LegalizeOp(SDValue(&*I, 0));
  177. // Finally, it's possible the root changed. Get the new root.
  178. SDValue OldRoot = DAG.getRoot();
  179. assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
  180. DAG.setRoot(LegalizedNodes[OldRoot]);
  181. LegalizedNodes.clear();
  182. // Remove dead nodes now.
  183. DAG.RemoveDeadNodes();
  184. return Changed;
  185. }
  186. SDValue VectorLegalizer::TranslateLegalizeResults(SDValue Op, SDNode *Result) {
  187. assert(Op->getNumValues() == Result->getNumValues() &&
  188. "Unexpected number of results");
  189. // Generic legalization: just pass the operand through.
  190. for (unsigned i = 0, e = Op->getNumValues(); i != e; ++i)
  191. AddLegalizedOperand(Op.getValue(i), SDValue(Result, i));
  192. return SDValue(Result, Op.getResNo());
  193. }
  194. SDValue
  195. VectorLegalizer::RecursivelyLegalizeResults(SDValue Op,
  196. MutableArrayRef<SDValue> Results) {
  197. assert(Results.size() == Op->getNumValues() &&
  198. "Unexpected number of results");
  199. // Make sure that the generated code is itself legal.
  200. for (unsigned i = 0, e = Results.size(); i != e; ++i) {
  201. Results[i] = LegalizeOp(Results[i]);
  202. AddLegalizedOperand(Op.getValue(i), Results[i]);
  203. }
  204. return Results[Op.getResNo()];
  205. }
  206. SDValue VectorLegalizer::LegalizeOp(SDValue Op) {
  207. // Note that LegalizeOp may be reentered even from single-use nodes, which
  208. // means that we always must cache transformed nodes.
  209. DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
  210. if (I != LegalizedNodes.end()) return I->second;
  211. // Legalize the operands
  212. SmallVector<SDValue, 8> Ops;
  213. for (const SDValue &Oper : Op->op_values())
  214. Ops.push_back(LegalizeOp(Oper));
  215. SDNode *Node = DAG.UpdateNodeOperands(Op.getNode(), Ops);
  216. bool HasVectorValueOrOp =
  217. llvm::any_of(Node->values(), [](EVT T) { return T.isVector(); }) ||
  218. llvm::any_of(Node->op_values(),
  219. [](SDValue O) { return O.getValueType().isVector(); });
  220. if (!HasVectorValueOrOp)
  221. return TranslateLegalizeResults(Op, Node);
  222. TargetLowering::LegalizeAction Action = TargetLowering::Legal;
  223. EVT ValVT;
  224. switch (Op.getOpcode()) {
  225. default:
  226. return TranslateLegalizeResults(Op, Node);
  227. case ISD::LOAD: {
  228. LoadSDNode *LD = cast<LoadSDNode>(Node);
  229. ISD::LoadExtType ExtType = LD->getExtensionType();
  230. EVT LoadedVT = LD->getMemoryVT();
  231. if (LoadedVT.isVector() && ExtType != ISD::NON_EXTLOAD)
  232. Action = TLI.getLoadExtAction(ExtType, LD->getValueType(0), LoadedVT);
  233. break;
  234. }
  235. case ISD::STORE: {
  236. StoreSDNode *ST = cast<StoreSDNode>(Node);
  237. EVT StVT = ST->getMemoryVT();
  238. MVT ValVT = ST->getValue().getSimpleValueType();
  239. if (StVT.isVector() && ST->isTruncatingStore())
  240. Action = TLI.getTruncStoreAction(ValVT, StVT);
  241. break;
  242. }
  243. case ISD::MERGE_VALUES:
  244. Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
  245. // This operation lies about being legal: when it claims to be legal,
  246. // it should actually be expanded.
  247. if (Action == TargetLowering::Legal)
  248. Action = TargetLowering::Expand;
  249. break;
  250. #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
  251. case ISD::STRICT_##DAGN:
  252. #include "llvm/IR/ConstrainedOps.def"
  253. ValVT = Node->getValueType(0);
  254. if (Op.getOpcode() == ISD::STRICT_SINT_TO_FP ||
  255. Op.getOpcode() == ISD::STRICT_UINT_TO_FP)
  256. ValVT = Node->getOperand(1).getValueType();
  257. Action = TLI.getOperationAction(Node->getOpcode(), ValVT);
  258. // If we're asked to expand a strict vector floating-point operation,
  259. // by default we're going to simply unroll it. That is usually the
  260. // best approach, except in the case where the resulting strict (scalar)
  261. // operations would themselves use the fallback mutation to non-strict.
  262. // In that specific case, just do the fallback on the vector op.
  263. if (Action == TargetLowering::Expand && !TLI.isStrictFPEnabled() &&
  264. TLI.getStrictFPOperationAction(Node->getOpcode(), ValVT) ==
  265. TargetLowering::Legal) {
  266. EVT EltVT = ValVT.getVectorElementType();
  267. if (TLI.getOperationAction(Node->getOpcode(), EltVT)
  268. == TargetLowering::Expand &&
  269. TLI.getStrictFPOperationAction(Node->getOpcode(), EltVT)
  270. == TargetLowering::Legal)
  271. Action = TargetLowering::Legal;
  272. }
  273. break;
  274. case ISD::ADD:
  275. case ISD::SUB:
  276. case ISD::MUL:
  277. case ISD::MULHS:
  278. case ISD::MULHU:
  279. case ISD::SDIV:
  280. case ISD::UDIV:
  281. case ISD::SREM:
  282. case ISD::UREM:
  283. case ISD::SDIVREM:
  284. case ISD::UDIVREM:
  285. case ISD::FADD:
  286. case ISD::FSUB:
  287. case ISD::FMUL:
  288. case ISD::FDIV:
  289. case ISD::FREM:
  290. case ISD::AND:
  291. case ISD::OR:
  292. case ISD::XOR:
  293. case ISD::SHL:
  294. case ISD::SRA:
  295. case ISD::SRL:
  296. case ISD::FSHL:
  297. case ISD::FSHR:
  298. case ISD::ROTL:
  299. case ISD::ROTR:
  300. case ISD::ABS:
  301. case ISD::BSWAP:
  302. case ISD::BITREVERSE:
  303. case ISD::CTLZ:
  304. case ISD::CTTZ:
  305. case ISD::CTLZ_ZERO_UNDEF:
  306. case ISD::CTTZ_ZERO_UNDEF:
  307. case ISD::CTPOP:
  308. case ISD::SELECT:
  309. case ISD::VSELECT:
  310. case ISD::SELECT_CC:
  311. case ISD::ZERO_EXTEND:
  312. case ISD::ANY_EXTEND:
  313. case ISD::TRUNCATE:
  314. case ISD::SIGN_EXTEND:
  315. case ISD::FP_TO_SINT:
  316. case ISD::FP_TO_UINT:
  317. case ISD::FNEG:
  318. case ISD::FABS:
  319. case ISD::FMINNUM:
  320. case ISD::FMAXNUM:
  321. case ISD::FMINNUM_IEEE:
  322. case ISD::FMAXNUM_IEEE:
  323. case ISD::FMINIMUM:
  324. case ISD::FMAXIMUM:
  325. case ISD::FCOPYSIGN:
  326. case ISD::FSQRT:
  327. case ISD::FSIN:
  328. case ISD::FCOS:
  329. case ISD::FPOWI:
  330. case ISD::FPOW:
  331. case ISD::FLOG:
  332. case ISD::FLOG2:
  333. case ISD::FLOG10:
  334. case ISD::FEXP:
  335. case ISD::FEXP2:
  336. case ISD::FCEIL:
  337. case ISD::FTRUNC:
  338. case ISD::FRINT:
  339. case ISD::FNEARBYINT:
  340. case ISD::FROUND:
  341. case ISD::FROUNDEVEN:
  342. case ISD::FFLOOR:
  343. case ISD::FP_ROUND:
  344. case ISD::FP_EXTEND:
  345. case ISD::FMA:
  346. case ISD::SIGN_EXTEND_INREG:
  347. case ISD::ANY_EXTEND_VECTOR_INREG:
  348. case ISD::SIGN_EXTEND_VECTOR_INREG:
  349. case ISD::ZERO_EXTEND_VECTOR_INREG:
  350. case ISD::SMIN:
  351. case ISD::SMAX:
  352. case ISD::UMIN:
  353. case ISD::UMAX:
  354. case ISD::SMUL_LOHI:
  355. case ISD::UMUL_LOHI:
  356. case ISD::SADDO:
  357. case ISD::UADDO:
  358. case ISD::SSUBO:
  359. case ISD::USUBO:
  360. case ISD::SMULO:
  361. case ISD::UMULO:
  362. case ISD::FCANONICALIZE:
  363. case ISD::SADDSAT:
  364. case ISD::UADDSAT:
  365. case ISD::SSUBSAT:
  366. case ISD::USUBSAT:
  367. case ISD::SSHLSAT:
  368. case ISD::USHLSAT:
  369. case ISD::FP_TO_SINT_SAT:
  370. case ISD::FP_TO_UINT_SAT:
  371. case ISD::MGATHER:
  372. Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
  373. break;
  374. case ISD::SMULFIX:
  375. case ISD::SMULFIXSAT:
  376. case ISD::UMULFIX:
  377. case ISD::UMULFIXSAT:
  378. case ISD::SDIVFIX:
  379. case ISD::SDIVFIXSAT:
  380. case ISD::UDIVFIX:
  381. case ISD::UDIVFIXSAT: {
  382. unsigned Scale = Node->getConstantOperandVal(2);
  383. Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
  384. Node->getValueType(0), Scale);
  385. break;
  386. }
  387. case ISD::SINT_TO_FP:
  388. case ISD::UINT_TO_FP:
  389. case ISD::VECREDUCE_ADD:
  390. case ISD::VECREDUCE_MUL:
  391. case ISD::VECREDUCE_AND:
  392. case ISD::VECREDUCE_OR:
  393. case ISD::VECREDUCE_XOR:
  394. case ISD::VECREDUCE_SMAX:
  395. case ISD::VECREDUCE_SMIN:
  396. case ISD::VECREDUCE_UMAX:
  397. case ISD::VECREDUCE_UMIN:
  398. case ISD::VECREDUCE_FADD:
  399. case ISD::VECREDUCE_FMUL:
  400. case ISD::VECREDUCE_FMAX:
  401. case ISD::VECREDUCE_FMIN:
  402. Action = TLI.getOperationAction(Node->getOpcode(),
  403. Node->getOperand(0).getValueType());
  404. break;
  405. case ISD::VECREDUCE_SEQ_FADD:
  406. case ISD::VECREDUCE_SEQ_FMUL:
  407. Action = TLI.getOperationAction(Node->getOpcode(),
  408. Node->getOperand(1).getValueType());
  409. break;
  410. case ISD::SETCC: {
  411. MVT OpVT = Node->getOperand(0).getSimpleValueType();
  412. ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
  413. Action = TLI.getCondCodeAction(CCCode, OpVT);
  414. if (Action == TargetLowering::Legal)
  415. Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
  416. break;
  417. }
  418. #define BEGIN_REGISTER_VP_SDNODE(VPID, LEGALPOS, ...) \
  419. case ISD::VPID: { \
  420. EVT LegalizeVT = LEGALPOS < 0 ? Node->getValueType(-(1 + LEGALPOS)) \
  421. : Node->getOperand(LEGALPOS).getValueType(); \
  422. Action = TLI.getOperationAction(Node->getOpcode(), LegalizeVT); \
  423. } break;
  424. #include "llvm/IR/VPIntrinsics.def"
  425. }
  426. LLVM_DEBUG(dbgs() << "\nLegalizing vector op: "; Node->dump(&DAG));
  427. SmallVector<SDValue, 8> ResultVals;
  428. switch (Action) {
  429. default: llvm_unreachable("This action is not supported yet!");
  430. case TargetLowering::Promote:
  431. assert((Op.getOpcode() != ISD::LOAD && Op.getOpcode() != ISD::STORE) &&
  432. "This action is not supported yet!");
  433. LLVM_DEBUG(dbgs() << "Promoting\n");
  434. Promote(Node, ResultVals);
  435. assert(!ResultVals.empty() && "No results for promotion?");
  436. break;
  437. case TargetLowering::Legal:
  438. LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
  439. break;
  440. case TargetLowering::Custom:
  441. LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
  442. if (LowerOperationWrapper(Node, ResultVals))
  443. break;
  444. LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
  445. LLVM_FALLTHROUGH;
  446. case TargetLowering::Expand:
  447. LLVM_DEBUG(dbgs() << "Expanding\n");
  448. Expand(Node, ResultVals);
  449. break;
  450. }
  451. if (ResultVals.empty())
  452. return TranslateLegalizeResults(Op, Node);
  453. Changed = true;
  454. return RecursivelyLegalizeResults(Op, ResultVals);
  455. }
  456. // FIXME: This is very similar to TargetLowering::LowerOperationWrapper. Can we
  457. // merge them somehow?
  458. bool VectorLegalizer::LowerOperationWrapper(SDNode *Node,
  459. SmallVectorImpl<SDValue> &Results) {
  460. SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
  461. if (!Res.getNode())
  462. return false;
  463. if (Res == SDValue(Node, 0))
  464. return true;
  465. // If the original node has one result, take the return value from
  466. // LowerOperation as is. It might not be result number 0.
  467. if (Node->getNumValues() == 1) {
  468. Results.push_back(Res);
  469. return true;
  470. }
  471. // If the original node has multiple results, then the return node should
  472. // have the same number of results.
  473. assert((Node->getNumValues() == Res->getNumValues()) &&
  474. "Lowering returned the wrong number of results!");
  475. // Places new result values base on N result number.
  476. for (unsigned I = 0, E = Node->getNumValues(); I != E; ++I)
  477. Results.push_back(Res.getValue(I));
  478. return true;
  479. }
  480. void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
  481. // For a few operations there is a specific concept for promotion based on
  482. // the operand's type.
  483. switch (Node->getOpcode()) {
  484. case ISD::SINT_TO_FP:
  485. case ISD::UINT_TO_FP:
  486. case ISD::STRICT_SINT_TO_FP:
  487. case ISD::STRICT_UINT_TO_FP:
  488. // "Promote" the operation by extending the operand.
  489. PromoteINT_TO_FP(Node, Results);
  490. return;
  491. case ISD::FP_TO_UINT:
  492. case ISD::FP_TO_SINT:
  493. case ISD::STRICT_FP_TO_UINT:
  494. case ISD::STRICT_FP_TO_SINT:
  495. // Promote the operation by extending the operand.
  496. PromoteFP_TO_INT(Node, Results);
  497. return;
  498. case ISD::FP_ROUND:
  499. case ISD::FP_EXTEND:
  500. // These operations are used to do promotion so they can't be promoted
  501. // themselves.
  502. llvm_unreachable("Don't know how to promote this operation!");
  503. }
  504. // There are currently two cases of vector promotion:
  505. // 1) Bitcasting a vector of integers to a different type to a vector of the
  506. // same overall length. For example, x86 promotes ISD::AND v2i32 to v1i64.
  507. // 2) Extending a vector of floats to a vector of the same number of larger
  508. // floats. For example, AArch64 promotes ISD::FADD on v4f16 to v4f32.
  509. assert(Node->getNumValues() == 1 &&
  510. "Can't promote a vector with multiple results!");
  511. MVT VT = Node->getSimpleValueType(0);
  512. MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
  513. SDLoc dl(Node);
  514. SmallVector<SDValue, 4> Operands(Node->getNumOperands());
  515. for (unsigned j = 0; j != Node->getNumOperands(); ++j) {
  516. if (Node->getOperand(j).getValueType().isVector())
  517. if (Node->getOperand(j)
  518. .getValueType()
  519. .getVectorElementType()
  520. .isFloatingPoint() &&
  521. NVT.isVector() && NVT.getVectorElementType().isFloatingPoint())
  522. Operands[j] = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(j));
  523. else
  524. Operands[j] = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(j));
  525. else
  526. Operands[j] = Node->getOperand(j);
  527. }
  528. SDValue Res =
  529. DAG.getNode(Node->getOpcode(), dl, NVT, Operands, Node->getFlags());
  530. if ((VT.isFloatingPoint() && NVT.isFloatingPoint()) ||
  531. (VT.isVector() && VT.getVectorElementType().isFloatingPoint() &&
  532. NVT.isVector() && NVT.getVectorElementType().isFloatingPoint()))
  533. Res = DAG.getNode(ISD::FP_ROUND, dl, VT, Res, DAG.getIntPtrConstant(0, dl));
  534. else
  535. Res = DAG.getNode(ISD::BITCAST, dl, VT, Res);
  536. Results.push_back(Res);
  537. }
  538. void VectorLegalizer::PromoteINT_TO_FP(SDNode *Node,
  539. SmallVectorImpl<SDValue> &Results) {
  540. // INT_TO_FP operations may require the input operand be promoted even
  541. // when the type is otherwise legal.
  542. bool IsStrict = Node->isStrictFPOpcode();
  543. MVT VT = Node->getOperand(IsStrict ? 1 : 0).getSimpleValueType();
  544. MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
  545. assert(NVT.getVectorNumElements() == VT.getVectorNumElements() &&
  546. "Vectors have different number of elements!");
  547. SDLoc dl(Node);
  548. SmallVector<SDValue, 4> Operands(Node->getNumOperands());
  549. unsigned Opc = (Node->getOpcode() == ISD::UINT_TO_FP ||
  550. Node->getOpcode() == ISD::STRICT_UINT_TO_FP)
  551. ? ISD::ZERO_EXTEND
  552. : ISD::SIGN_EXTEND;
  553. for (unsigned j = 0; j != Node->getNumOperands(); ++j) {
  554. if (Node->getOperand(j).getValueType().isVector())
  555. Operands[j] = DAG.getNode(Opc, dl, NVT, Node->getOperand(j));
  556. else
  557. Operands[j] = Node->getOperand(j);
  558. }
  559. if (IsStrict) {
  560. SDValue Res = DAG.getNode(Node->getOpcode(), dl,
  561. {Node->getValueType(0), MVT::Other}, Operands);
  562. Results.push_back(Res);
  563. Results.push_back(Res.getValue(1));
  564. return;
  565. }
  566. SDValue Res =
  567. DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Operands);
  568. Results.push_back(Res);
  569. }
  570. // For FP_TO_INT we promote the result type to a vector type with wider
  571. // elements and then truncate the result. This is different from the default
  572. // PromoteVector which uses bitcast to promote thus assumning that the
  573. // promoted vector type has the same overall size.
  574. void VectorLegalizer::PromoteFP_TO_INT(SDNode *Node,
  575. SmallVectorImpl<SDValue> &Results) {
  576. MVT VT = Node->getSimpleValueType(0);
  577. MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
  578. bool IsStrict = Node->isStrictFPOpcode();
  579. assert(NVT.getVectorNumElements() == VT.getVectorNumElements() &&
  580. "Vectors have different number of elements!");
  581. unsigned NewOpc = Node->getOpcode();
  582. // Change FP_TO_UINT to FP_TO_SINT if possible.
  583. // TODO: Should we only do this if FP_TO_UINT itself isn't legal?
  584. if (NewOpc == ISD::FP_TO_UINT &&
  585. TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
  586. NewOpc = ISD::FP_TO_SINT;
  587. if (NewOpc == ISD::STRICT_FP_TO_UINT &&
  588. TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT))
  589. NewOpc = ISD::STRICT_FP_TO_SINT;
  590. SDLoc dl(Node);
  591. SDValue Promoted, Chain;
  592. if (IsStrict) {
  593. Promoted = DAG.getNode(NewOpc, dl, {NVT, MVT::Other},
  594. {Node->getOperand(0), Node->getOperand(1)});
  595. Chain = Promoted.getValue(1);
  596. } else
  597. Promoted = DAG.getNode(NewOpc, dl, NVT, Node->getOperand(0));
  598. // Assert that the converted value fits in the original type. If it doesn't
  599. // (eg: because the value being converted is too big), then the result of the
  600. // original operation was undefined anyway, so the assert is still correct.
  601. if (Node->getOpcode() == ISD::FP_TO_UINT ||
  602. Node->getOpcode() == ISD::STRICT_FP_TO_UINT)
  603. NewOpc = ISD::AssertZext;
  604. else
  605. NewOpc = ISD::AssertSext;
  606. Promoted = DAG.getNode(NewOpc, dl, NVT, Promoted,
  607. DAG.getValueType(VT.getScalarType()));
  608. Promoted = DAG.getNode(ISD::TRUNCATE, dl, VT, Promoted);
  609. Results.push_back(Promoted);
  610. if (IsStrict)
  611. Results.push_back(Chain);
  612. }
  613. std::pair<SDValue, SDValue> VectorLegalizer::ExpandLoad(SDNode *N) {
  614. LoadSDNode *LD = cast<LoadSDNode>(N);
  615. return TLI.scalarizeVectorLoad(LD, DAG);
  616. }
  617. SDValue VectorLegalizer::ExpandStore(SDNode *N) {
  618. StoreSDNode *ST = cast<StoreSDNode>(N);
  619. SDValue TF = TLI.scalarizeVectorStore(ST, DAG);
  620. return TF;
  621. }
  622. void VectorLegalizer::Expand(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
  623. switch (Node->getOpcode()) {
  624. case ISD::LOAD: {
  625. std::pair<SDValue, SDValue> Tmp = ExpandLoad(Node);
  626. Results.push_back(Tmp.first);
  627. Results.push_back(Tmp.second);
  628. return;
  629. }
  630. case ISD::STORE:
  631. Results.push_back(ExpandStore(Node));
  632. return;
  633. case ISD::MERGE_VALUES:
  634. for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
  635. Results.push_back(Node->getOperand(i));
  636. return;
  637. case ISD::SIGN_EXTEND_INREG:
  638. Results.push_back(ExpandSEXTINREG(Node));
  639. return;
  640. case ISD::ANY_EXTEND_VECTOR_INREG:
  641. Results.push_back(ExpandANY_EXTEND_VECTOR_INREG(Node));
  642. return;
  643. case ISD::SIGN_EXTEND_VECTOR_INREG:
  644. Results.push_back(ExpandSIGN_EXTEND_VECTOR_INREG(Node));
  645. return;
  646. case ISD::ZERO_EXTEND_VECTOR_INREG:
  647. Results.push_back(ExpandZERO_EXTEND_VECTOR_INREG(Node));
  648. return;
  649. case ISD::BSWAP:
  650. Results.push_back(ExpandBSWAP(Node));
  651. return;
  652. case ISD::VSELECT:
  653. Results.push_back(ExpandVSELECT(Node));
  654. return;
  655. case ISD::VP_SELECT:
  656. Results.push_back(ExpandVP_SELECT(Node));
  657. return;
  658. case ISD::SELECT:
  659. Results.push_back(ExpandSELECT(Node));
  660. return;
  661. case ISD::FP_TO_UINT:
  662. ExpandFP_TO_UINT(Node, Results);
  663. return;
  664. case ISD::UINT_TO_FP:
  665. ExpandUINT_TO_FLOAT(Node, Results);
  666. return;
  667. case ISD::FNEG:
  668. Results.push_back(ExpandFNEG(Node));
  669. return;
  670. case ISD::FSUB:
  671. ExpandFSUB(Node, Results);
  672. return;
  673. case ISD::SETCC:
  674. ExpandSETCC(Node, Results);
  675. return;
  676. case ISD::ABS:
  677. if (SDValue Expanded = TLI.expandABS(Node, DAG)) {
  678. Results.push_back(Expanded);
  679. return;
  680. }
  681. break;
  682. case ISD::BITREVERSE:
  683. ExpandBITREVERSE(Node, Results);
  684. return;
  685. case ISD::CTPOP:
  686. if (SDValue Expanded = TLI.expandCTPOP(Node, DAG)) {
  687. Results.push_back(Expanded);
  688. return;
  689. }
  690. break;
  691. case ISD::CTLZ:
  692. case ISD::CTLZ_ZERO_UNDEF:
  693. if (SDValue Expanded = TLI.expandCTLZ(Node, DAG)) {
  694. Results.push_back(Expanded);
  695. return;
  696. }
  697. break;
  698. case ISD::CTTZ:
  699. case ISD::CTTZ_ZERO_UNDEF:
  700. if (SDValue Expanded = TLI.expandCTTZ(Node, DAG)) {
  701. Results.push_back(Expanded);
  702. return;
  703. }
  704. break;
  705. case ISD::FSHL:
  706. case ISD::FSHR:
  707. if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG)) {
  708. Results.push_back(Expanded);
  709. return;
  710. }
  711. break;
  712. case ISD::ROTL:
  713. case ISD::ROTR:
  714. if (SDValue Expanded = TLI.expandROT(Node, false /*AllowVectorOps*/, DAG)) {
  715. Results.push_back(Expanded);
  716. return;
  717. }
  718. break;
  719. case ISD::FMINNUM:
  720. case ISD::FMAXNUM:
  721. if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG)) {
  722. Results.push_back(Expanded);
  723. return;
  724. }
  725. break;
  726. case ISD::SMIN:
  727. case ISD::SMAX:
  728. case ISD::UMIN:
  729. case ISD::UMAX:
  730. if (SDValue Expanded = TLI.expandIntMINMAX(Node, DAG)) {
  731. Results.push_back(Expanded);
  732. return;
  733. }
  734. break;
  735. case ISD::UADDO:
  736. case ISD::USUBO:
  737. ExpandUADDSUBO(Node, Results);
  738. return;
  739. case ISD::SADDO:
  740. case ISD::SSUBO:
  741. ExpandSADDSUBO(Node, Results);
  742. return;
  743. case ISD::UMULO:
  744. case ISD::SMULO:
  745. ExpandMULO(Node, Results);
  746. return;
  747. case ISD::USUBSAT:
  748. case ISD::SSUBSAT:
  749. case ISD::UADDSAT:
  750. case ISD::SADDSAT:
  751. if (SDValue Expanded = TLI.expandAddSubSat(Node, DAG)) {
  752. Results.push_back(Expanded);
  753. return;
  754. }
  755. break;
  756. case ISD::SMULFIX:
  757. case ISD::UMULFIX:
  758. if (SDValue Expanded = TLI.expandFixedPointMul(Node, DAG)) {
  759. Results.push_back(Expanded);
  760. return;
  761. }
  762. break;
  763. case ISD::SMULFIXSAT:
  764. case ISD::UMULFIXSAT:
  765. // FIXME: We do not expand SMULFIXSAT/UMULFIXSAT here yet, not sure exactly
  766. // why. Maybe it results in worse codegen compared to the unroll for some
  767. // targets? This should probably be investigated. And if we still prefer to
  768. // unroll an explanation could be helpful.
  769. break;
  770. case ISD::SDIVFIX:
  771. case ISD::UDIVFIX:
  772. ExpandFixedPointDiv(Node, Results);
  773. return;
  774. case ISD::SDIVFIXSAT:
  775. case ISD::UDIVFIXSAT:
  776. break;
  777. #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
  778. case ISD::STRICT_##DAGN:
  779. #include "llvm/IR/ConstrainedOps.def"
  780. ExpandStrictFPOp(Node, Results);
  781. return;
  782. case ISD::VECREDUCE_ADD:
  783. case ISD::VECREDUCE_MUL:
  784. case ISD::VECREDUCE_AND:
  785. case ISD::VECREDUCE_OR:
  786. case ISD::VECREDUCE_XOR:
  787. case ISD::VECREDUCE_SMAX:
  788. case ISD::VECREDUCE_SMIN:
  789. case ISD::VECREDUCE_UMAX:
  790. case ISD::VECREDUCE_UMIN:
  791. case ISD::VECREDUCE_FADD:
  792. case ISD::VECREDUCE_FMUL:
  793. case ISD::VECREDUCE_FMAX:
  794. case ISD::VECREDUCE_FMIN:
  795. Results.push_back(TLI.expandVecReduce(Node, DAG));
  796. return;
  797. case ISD::VECREDUCE_SEQ_FADD:
  798. case ISD::VECREDUCE_SEQ_FMUL:
  799. Results.push_back(TLI.expandVecReduceSeq(Node, DAG));
  800. return;
  801. case ISD::SREM:
  802. case ISD::UREM:
  803. ExpandREM(Node, Results);
  804. return;
  805. case ISD::VP_MERGE:
  806. Results.push_back(ExpandVP_MERGE(Node));
  807. return;
  808. }
  809. Results.push_back(DAG.UnrollVectorOp(Node));
  810. }
  811. SDValue VectorLegalizer::ExpandSELECT(SDNode *Node) {
  812. // Lower a select instruction where the condition is a scalar and the
  813. // operands are vectors. Lower this select to VSELECT and implement it
  814. // using XOR AND OR. The selector bit is broadcasted.
  815. EVT VT = Node->getValueType(0);
  816. SDLoc DL(Node);
  817. SDValue Mask = Node->getOperand(0);
  818. SDValue Op1 = Node->getOperand(1);
  819. SDValue Op2 = Node->getOperand(2);
  820. assert(VT.isVector() && !Mask.getValueType().isVector()
  821. && Op1.getValueType() == Op2.getValueType() && "Invalid type");
  822. // If we can't even use the basic vector operations of
  823. // AND,OR,XOR, we will have to scalarize the op.
  824. // Notice that the operation may be 'promoted' which means that it is
  825. // 'bitcasted' to another type which is handled.
  826. // Also, we need to be able to construct a splat vector using either
  827. // BUILD_VECTOR or SPLAT_VECTOR.
  828. // FIXME: Should we also permit fixed-length SPLAT_VECTOR as a fallback to
  829. // BUILD_VECTOR?
  830. if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
  831. TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
  832. TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand ||
  833. TLI.getOperationAction(VT.isFixedLengthVector() ? ISD::BUILD_VECTOR
  834. : ISD::SPLAT_VECTOR,
  835. VT) == TargetLowering::Expand)
  836. return DAG.UnrollVectorOp(Node);
  837. // Generate a mask operand.
  838. EVT MaskTy = VT.changeVectorElementTypeToInteger();
  839. // What is the size of each element in the vector mask.
  840. EVT BitTy = MaskTy.getScalarType();
  841. Mask = DAG.getSelect(DL, BitTy, Mask, DAG.getAllOnesConstant(DL, BitTy),
  842. DAG.getConstant(0, DL, BitTy));
  843. // Broadcast the mask so that the entire vector is all one or all zero.
  844. if (VT.isFixedLengthVector())
  845. Mask = DAG.getSplatBuildVector(MaskTy, DL, Mask);
  846. else
  847. Mask = DAG.getSplatVector(MaskTy, DL, Mask);
  848. // Bitcast the operands to be the same type as the mask.
  849. // This is needed when we select between FP types because
  850. // the mask is a vector of integers.
  851. Op1 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op1);
  852. Op2 = DAG.getNode(ISD::BITCAST, DL, MaskTy, Op2);
  853. SDValue NotMask = DAG.getNOT(DL, Mask, MaskTy);
  854. Op1 = DAG.getNode(ISD::AND, DL, MaskTy, Op1, Mask);
  855. Op2 = DAG.getNode(ISD::AND, DL, MaskTy, Op2, NotMask);
  856. SDValue Val = DAG.getNode(ISD::OR, DL, MaskTy, Op1, Op2);
  857. return DAG.getNode(ISD::BITCAST, DL, Node->getValueType(0), Val);
  858. }
  859. SDValue VectorLegalizer::ExpandSEXTINREG(SDNode *Node) {
  860. EVT VT = Node->getValueType(0);
  861. // Make sure that the SRA and SHL instructions are available.
  862. if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Expand ||
  863. TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Expand)
  864. return DAG.UnrollVectorOp(Node);
  865. SDLoc DL(Node);
  866. EVT OrigTy = cast<VTSDNode>(Node->getOperand(1))->getVT();
  867. unsigned BW = VT.getScalarSizeInBits();
  868. unsigned OrigBW = OrigTy.getScalarSizeInBits();
  869. SDValue ShiftSz = DAG.getConstant(BW - OrigBW, DL, VT);
  870. SDValue Op = DAG.getNode(ISD::SHL, DL, VT, Node->getOperand(0), ShiftSz);
  871. return DAG.getNode(ISD::SRA, DL, VT, Op, ShiftSz);
  872. }
  873. // Generically expand a vector anyext in register to a shuffle of the relevant
  874. // lanes into the appropriate locations, with other lanes left undef.
  875. SDValue VectorLegalizer::ExpandANY_EXTEND_VECTOR_INREG(SDNode *Node) {
  876. SDLoc DL(Node);
  877. EVT VT = Node->getValueType(0);
  878. int NumElements = VT.getVectorNumElements();
  879. SDValue Src = Node->getOperand(0);
  880. EVT SrcVT = Src.getValueType();
  881. int NumSrcElements = SrcVT.getVectorNumElements();
  882. // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector
  883. // into a larger vector type.
  884. if (SrcVT.bitsLE(VT)) {
  885. assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 &&
  886. "ANY_EXTEND_VECTOR_INREG vector size mismatch");
  887. NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits();
  888. SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(),
  889. NumSrcElements);
  890. Src = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, SrcVT, DAG.getUNDEF(SrcVT),
  891. Src, DAG.getVectorIdxConstant(0, DL));
  892. }
  893. // Build a base mask of undef shuffles.
  894. SmallVector<int, 16> ShuffleMask;
  895. ShuffleMask.resize(NumSrcElements, -1);
  896. // Place the extended lanes into the correct locations.
  897. int ExtLaneScale = NumSrcElements / NumElements;
  898. int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
  899. for (int i = 0; i < NumElements; ++i)
  900. ShuffleMask[i * ExtLaneScale + EndianOffset] = i;
  901. return DAG.getNode(
  902. ISD::BITCAST, DL, VT,
  903. DAG.getVectorShuffle(SrcVT, DL, Src, DAG.getUNDEF(SrcVT), ShuffleMask));
  904. }
  905. SDValue VectorLegalizer::ExpandSIGN_EXTEND_VECTOR_INREG(SDNode *Node) {
  906. SDLoc DL(Node);
  907. EVT VT = Node->getValueType(0);
  908. SDValue Src = Node->getOperand(0);
  909. EVT SrcVT = Src.getValueType();
  910. // First build an any-extend node which can be legalized above when we
  911. // recurse through it.
  912. SDValue Op = DAG.getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Src);
  913. // Now we need sign extend. Do this by shifting the elements. Even if these
  914. // aren't legal operations, they have a better chance of being legalized
  915. // without full scalarization than the sign extension does.
  916. unsigned EltWidth = VT.getScalarSizeInBits();
  917. unsigned SrcEltWidth = SrcVT.getScalarSizeInBits();
  918. SDValue ShiftAmount = DAG.getConstant(EltWidth - SrcEltWidth, DL, VT);
  919. return DAG.getNode(ISD::SRA, DL, VT,
  920. DAG.getNode(ISD::SHL, DL, VT, Op, ShiftAmount),
  921. ShiftAmount);
  922. }
  923. // Generically expand a vector zext in register to a shuffle of the relevant
  924. // lanes into the appropriate locations, a blend of zero into the high bits,
  925. // and a bitcast to the wider element type.
  926. SDValue VectorLegalizer::ExpandZERO_EXTEND_VECTOR_INREG(SDNode *Node) {
  927. SDLoc DL(Node);
  928. EVT VT = Node->getValueType(0);
  929. int NumElements = VT.getVectorNumElements();
  930. SDValue Src = Node->getOperand(0);
  931. EVT SrcVT = Src.getValueType();
  932. int NumSrcElements = SrcVT.getVectorNumElements();
  933. // *_EXTEND_VECTOR_INREG SrcVT can be smaller than VT - so insert the vector
  934. // into a larger vector type.
  935. if (SrcVT.bitsLE(VT)) {
  936. assert((VT.getSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 &&
  937. "ZERO_EXTEND_VECTOR_INREG vector size mismatch");
  938. NumSrcElements = VT.getSizeInBits() / SrcVT.getScalarSizeInBits();
  939. SrcVT = EVT::getVectorVT(*DAG.getContext(), SrcVT.getScalarType(),
  940. NumSrcElements);
  941. Src = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, SrcVT, DAG.getUNDEF(SrcVT),
  942. Src, DAG.getVectorIdxConstant(0, DL));
  943. }
  944. // Build up a zero vector to blend into this one.
  945. SDValue Zero = DAG.getConstant(0, DL, SrcVT);
  946. // Shuffle the incoming lanes into the correct position, and pull all other
  947. // lanes from the zero vector.
  948. SmallVector<int, 16> ShuffleMask;
  949. ShuffleMask.reserve(NumSrcElements);
  950. for (int i = 0; i < NumSrcElements; ++i)
  951. ShuffleMask.push_back(i);
  952. int ExtLaneScale = NumSrcElements / NumElements;
  953. int EndianOffset = DAG.getDataLayout().isBigEndian() ? ExtLaneScale - 1 : 0;
  954. for (int i = 0; i < NumElements; ++i)
  955. ShuffleMask[i * ExtLaneScale + EndianOffset] = NumSrcElements + i;
  956. return DAG.getNode(ISD::BITCAST, DL, VT,
  957. DAG.getVectorShuffle(SrcVT, DL, Zero, Src, ShuffleMask));
  958. }
  959. static void createBSWAPShuffleMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) {
  960. int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8;
  961. for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I)
  962. for (int J = ScalarSizeInBytes - 1; J >= 0; --J)
  963. ShuffleMask.push_back((I * ScalarSizeInBytes) + J);
  964. }
  965. SDValue VectorLegalizer::ExpandBSWAP(SDNode *Node) {
  966. EVT VT = Node->getValueType(0);
  967. // Scalable vectors can't use shuffle expansion.
  968. if (VT.isScalableVector())
  969. return TLI.expandBSWAP(Node, DAG);
  970. // Generate a byte wise shuffle mask for the BSWAP.
  971. SmallVector<int, 16> ShuffleMask;
  972. createBSWAPShuffleMask(VT, ShuffleMask);
  973. EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, ShuffleMask.size());
  974. // Only emit a shuffle if the mask is legal.
  975. if (TLI.isShuffleMaskLegal(ShuffleMask, ByteVT)) {
  976. SDLoc DL(Node);
  977. SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0));
  978. Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT), ShuffleMask);
  979. return DAG.getNode(ISD::BITCAST, DL, VT, Op);
  980. }
  981. // If we have the appropriate vector bit operations, it is better to use them
  982. // than unrolling and expanding each component.
  983. if (TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
  984. TLI.isOperationLegalOrCustom(ISD::SRL, VT) &&
  985. TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) &&
  986. TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT))
  987. return TLI.expandBSWAP(Node, DAG);
  988. // Otherwise unroll.
  989. return DAG.UnrollVectorOp(Node);
  990. }
  991. void VectorLegalizer::ExpandBITREVERSE(SDNode *Node,
  992. SmallVectorImpl<SDValue> &Results) {
  993. EVT VT = Node->getValueType(0);
  994. // We can't unroll or use shuffles for scalable vectors.
  995. if (VT.isScalableVector()) {
  996. Results.push_back(TLI.expandBITREVERSE(Node, DAG));
  997. return;
  998. }
  999. // If we have the scalar operation, it's probably cheaper to unroll it.
  1000. if (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, VT.getScalarType())) {
  1001. SDValue Tmp = DAG.UnrollVectorOp(Node);
  1002. Results.push_back(Tmp);
  1003. return;
  1004. }
  1005. // If the vector element width is a whole number of bytes, test if its legal
  1006. // to BSWAP shuffle the bytes and then perform the BITREVERSE on the byte
  1007. // vector. This greatly reduces the number of bit shifts necessary.
  1008. unsigned ScalarSizeInBits = VT.getScalarSizeInBits();
  1009. if (ScalarSizeInBits > 8 && (ScalarSizeInBits % 8) == 0) {
  1010. SmallVector<int, 16> BSWAPMask;
  1011. createBSWAPShuffleMask(VT, BSWAPMask);
  1012. EVT ByteVT = EVT::getVectorVT(*DAG.getContext(), MVT::i8, BSWAPMask.size());
  1013. if (TLI.isShuffleMaskLegal(BSWAPMask, ByteVT) &&
  1014. (TLI.isOperationLegalOrCustom(ISD::BITREVERSE, ByteVT) ||
  1015. (TLI.isOperationLegalOrCustom(ISD::SHL, ByteVT) &&
  1016. TLI.isOperationLegalOrCustom(ISD::SRL, ByteVT) &&
  1017. TLI.isOperationLegalOrCustomOrPromote(ISD::AND, ByteVT) &&
  1018. TLI.isOperationLegalOrCustomOrPromote(ISD::OR, ByteVT)))) {
  1019. SDLoc DL(Node);
  1020. SDValue Op = DAG.getNode(ISD::BITCAST, DL, ByteVT, Node->getOperand(0));
  1021. Op = DAG.getVectorShuffle(ByteVT, DL, Op, DAG.getUNDEF(ByteVT),
  1022. BSWAPMask);
  1023. Op = DAG.getNode(ISD::BITREVERSE, DL, ByteVT, Op);
  1024. Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
  1025. Results.push_back(Op);
  1026. return;
  1027. }
  1028. }
  1029. // If we have the appropriate vector bit operations, it is better to use them
  1030. // than unrolling and expanding each component.
  1031. if (TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
  1032. TLI.isOperationLegalOrCustom(ISD::SRL, VT) &&
  1033. TLI.isOperationLegalOrCustomOrPromote(ISD::AND, VT) &&
  1034. TLI.isOperationLegalOrCustomOrPromote(ISD::OR, VT)) {
  1035. Results.push_back(TLI.expandBITREVERSE(Node, DAG));
  1036. return;
  1037. }
  1038. // Otherwise unroll.
  1039. SDValue Tmp = DAG.UnrollVectorOp(Node);
  1040. Results.push_back(Tmp);
  1041. }
  1042. SDValue VectorLegalizer::ExpandVSELECT(SDNode *Node) {
  1043. // Implement VSELECT in terms of XOR, AND, OR
  1044. // on platforms which do not support blend natively.
  1045. SDLoc DL(Node);
  1046. SDValue Mask = Node->getOperand(0);
  1047. SDValue Op1 = Node->getOperand(1);
  1048. SDValue Op2 = Node->getOperand(2);
  1049. EVT VT = Mask.getValueType();
  1050. // If we can't even use the basic vector operations of
  1051. // AND,OR,XOR, we will have to scalarize the op.
  1052. // Notice that the operation may be 'promoted' which means that it is
  1053. // 'bitcasted' to another type which is handled.
  1054. if (TLI.getOperationAction(ISD::AND, VT) == TargetLowering::Expand ||
  1055. TLI.getOperationAction(ISD::XOR, VT) == TargetLowering::Expand ||
  1056. TLI.getOperationAction(ISD::OR, VT) == TargetLowering::Expand)
  1057. return DAG.UnrollVectorOp(Node);
  1058. // This operation also isn't safe with AND, OR, XOR when the boolean type is
  1059. // 0/1 and the select operands aren't also booleans, as we need an all-ones
  1060. // vector constant to mask with.
  1061. // FIXME: Sign extend 1 to all ones if that's legal on the target.
  1062. auto BoolContents = TLI.getBooleanContents(Op1.getValueType());
  1063. if (BoolContents != TargetLowering::ZeroOrNegativeOneBooleanContent &&
  1064. !(BoolContents == TargetLowering::ZeroOrOneBooleanContent &&
  1065. Op1.getValueType().getVectorElementType() == MVT::i1))
  1066. return DAG.UnrollVectorOp(Node);
  1067. // If the mask and the type are different sizes, unroll the vector op. This
  1068. // can occur when getSetCCResultType returns something that is different in
  1069. // size from the operand types. For example, v4i8 = select v4i32, v4i8, v4i8.
  1070. if (VT.getSizeInBits() != Op1.getValueSizeInBits())
  1071. return DAG.UnrollVectorOp(Node);
  1072. // Bitcast the operands to be the same type as the mask.
  1073. // This is needed when we select between FP types because
  1074. // the mask is a vector of integers.
  1075. Op1 = DAG.getNode(ISD::BITCAST, DL, VT, Op1);
  1076. Op2 = DAG.getNode(ISD::BITCAST, DL, VT, Op2);
  1077. SDValue NotMask = DAG.getNOT(DL, Mask, VT);
  1078. Op1 = DAG.getNode(ISD::AND, DL, VT, Op1, Mask);
  1079. Op2 = DAG.getNode(ISD::AND, DL, VT, Op2, NotMask);
  1080. SDValue Val = DAG.getNode(ISD::OR, DL, VT, Op1, Op2);
  1081. return DAG.getNode(ISD::BITCAST, DL, Node->getValueType(0), Val);
  1082. }
  1083. SDValue VectorLegalizer::ExpandVP_SELECT(SDNode *Node) {
  1084. // Implement VP_SELECT in terms of VP_XOR, VP_AND and VP_OR on platforms which
  1085. // do not support it natively.
  1086. SDLoc DL(Node);
  1087. SDValue Mask = Node->getOperand(0);
  1088. SDValue Op1 = Node->getOperand(1);
  1089. SDValue Op2 = Node->getOperand(2);
  1090. SDValue EVL = Node->getOperand(3);
  1091. EVT VT = Mask.getValueType();
  1092. // If we can't even use the basic vector operations of
  1093. // VP_AND,VP_OR,VP_XOR, we will have to scalarize the op.
  1094. if (TLI.getOperationAction(ISD::VP_AND, VT) == TargetLowering::Expand ||
  1095. TLI.getOperationAction(ISD::VP_XOR, VT) == TargetLowering::Expand ||
  1096. TLI.getOperationAction(ISD::VP_OR, VT) == TargetLowering::Expand)
  1097. return DAG.UnrollVectorOp(Node);
  1098. // This operation also isn't safe when the operands aren't also booleans.
  1099. if (Op1.getValueType().getVectorElementType() != MVT::i1)
  1100. return DAG.UnrollVectorOp(Node);
  1101. SDValue Ones = DAG.getAllOnesConstant(DL, VT);
  1102. SDValue NotMask = DAG.getNode(ISD::VP_XOR, DL, VT, Mask, Ones, Mask, EVL);
  1103. Op1 = DAG.getNode(ISD::VP_AND, DL, VT, Op1, Mask, Mask, EVL);
  1104. Op2 = DAG.getNode(ISD::VP_AND, DL, VT, Op2, NotMask, Mask, EVL);
  1105. return DAG.getNode(ISD::VP_OR, DL, VT, Op1, Op2, Mask, EVL);
  1106. }
  1107. SDValue VectorLegalizer::ExpandVP_MERGE(SDNode *Node) {
  1108. // Implement VP_MERGE in terms of VSELECT. Construct a mask where vector
  1109. // indices less than the EVL/pivot are true. Combine that with the original
  1110. // mask for a full-length mask. Use a full-length VSELECT to select between
  1111. // the true and false values.
  1112. SDLoc DL(Node);
  1113. SDValue Mask = Node->getOperand(0);
  1114. SDValue Op1 = Node->getOperand(1);
  1115. SDValue Op2 = Node->getOperand(2);
  1116. SDValue EVL = Node->getOperand(3);
  1117. EVT MaskVT = Mask.getValueType();
  1118. bool IsFixedLen = MaskVT.isFixedLengthVector();
  1119. EVT EVLVecVT = EVT::getVectorVT(*DAG.getContext(), EVL.getValueType(),
  1120. MaskVT.getVectorElementCount());
  1121. // If we can't construct the EVL mask efficiently, it's better to unroll.
  1122. if ((IsFixedLen &&
  1123. !TLI.isOperationLegalOrCustom(ISD::BUILD_VECTOR, EVLVecVT)) ||
  1124. (!IsFixedLen &&
  1125. (!TLI.isOperationLegalOrCustom(ISD::STEP_VECTOR, EVLVecVT) ||
  1126. !TLI.isOperationLegalOrCustom(ISD::SPLAT_VECTOR, EVLVecVT))))
  1127. return DAG.UnrollVectorOp(Node);
  1128. // If using a SETCC would result in a different type than the mask type,
  1129. // unroll.
  1130. if (TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
  1131. EVLVecVT) != MaskVT)
  1132. return DAG.UnrollVectorOp(Node);
  1133. SDValue StepVec = DAG.getStepVector(DL, EVLVecVT);
  1134. SDValue SplatEVL = IsFixedLen ? DAG.getSplatBuildVector(EVLVecVT, DL, EVL)
  1135. : DAG.getSplatVector(EVLVecVT, DL, EVL);
  1136. SDValue EVLMask =
  1137. DAG.getSetCC(DL, MaskVT, StepVec, SplatEVL, ISD::CondCode::SETULT);
  1138. SDValue FullMask = DAG.getNode(ISD::AND, DL, MaskVT, Mask, EVLMask);
  1139. return DAG.getSelect(DL, Node->getValueType(0), FullMask, Op1, Op2);
  1140. }
  1141. void VectorLegalizer::ExpandFP_TO_UINT(SDNode *Node,
  1142. SmallVectorImpl<SDValue> &Results) {
  1143. // Attempt to expand using TargetLowering.
  1144. SDValue Result, Chain;
  1145. if (TLI.expandFP_TO_UINT(Node, Result, Chain, DAG)) {
  1146. Results.push_back(Result);
  1147. if (Node->isStrictFPOpcode())
  1148. Results.push_back(Chain);
  1149. return;
  1150. }
  1151. // Otherwise go ahead and unroll.
  1152. if (Node->isStrictFPOpcode()) {
  1153. UnrollStrictFPOp(Node, Results);
  1154. return;
  1155. }
  1156. Results.push_back(DAG.UnrollVectorOp(Node));
  1157. }
  1158. void VectorLegalizer::ExpandUINT_TO_FLOAT(SDNode *Node,
  1159. SmallVectorImpl<SDValue> &Results) {
  1160. bool IsStrict = Node->isStrictFPOpcode();
  1161. unsigned OpNo = IsStrict ? 1 : 0;
  1162. SDValue Src = Node->getOperand(OpNo);
  1163. EVT VT = Src.getValueType();
  1164. SDLoc DL(Node);
  1165. // Attempt to expand using TargetLowering.
  1166. SDValue Result;
  1167. SDValue Chain;
  1168. if (TLI.expandUINT_TO_FP(Node, Result, Chain, DAG)) {
  1169. Results.push_back(Result);
  1170. if (IsStrict)
  1171. Results.push_back(Chain);
  1172. return;
  1173. }
  1174. // Make sure that the SINT_TO_FP and SRL instructions are available.
  1175. if (((!IsStrict && TLI.getOperationAction(ISD::SINT_TO_FP, VT) ==
  1176. TargetLowering::Expand) ||
  1177. (IsStrict && TLI.getOperationAction(ISD::STRICT_SINT_TO_FP, VT) ==
  1178. TargetLowering::Expand)) ||
  1179. TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Expand) {
  1180. if (IsStrict) {
  1181. UnrollStrictFPOp(Node, Results);
  1182. return;
  1183. }
  1184. Results.push_back(DAG.UnrollVectorOp(Node));
  1185. return;
  1186. }
  1187. unsigned BW = VT.getScalarSizeInBits();
  1188. assert((BW == 64 || BW == 32) &&
  1189. "Elements in vector-UINT_TO_FP must be 32 or 64 bits wide");
  1190. SDValue HalfWord = DAG.getConstant(BW / 2, DL, VT);
  1191. // Constants to clear the upper part of the word.
  1192. // Notice that we can also use SHL+SHR, but using a constant is slightly
  1193. // faster on x86.
  1194. uint64_t HWMask = (BW == 64) ? 0x00000000FFFFFFFF : 0x0000FFFF;
  1195. SDValue HalfWordMask = DAG.getConstant(HWMask, DL, VT);
  1196. // Two to the power of half-word-size.
  1197. SDValue TWOHW =
  1198. DAG.getConstantFP(1ULL << (BW / 2), DL, Node->getValueType(0));
  1199. // Clear upper part of LO, lower HI
  1200. SDValue HI = DAG.getNode(ISD::SRL, DL, VT, Src, HalfWord);
  1201. SDValue LO = DAG.getNode(ISD::AND, DL, VT, Src, HalfWordMask);
  1202. if (IsStrict) {
  1203. // Convert hi and lo to floats
  1204. // Convert the hi part back to the upper values
  1205. // TODO: Can any fast-math-flags be set on these nodes?
  1206. SDValue fHI = DAG.getNode(ISD::STRICT_SINT_TO_FP, DL,
  1207. {Node->getValueType(0), MVT::Other},
  1208. {Node->getOperand(0), HI});
  1209. fHI = DAG.getNode(ISD::STRICT_FMUL, DL, {Node->getValueType(0), MVT::Other},
  1210. {fHI.getValue(1), fHI, TWOHW});
  1211. SDValue fLO = DAG.getNode(ISD::STRICT_SINT_TO_FP, DL,
  1212. {Node->getValueType(0), MVT::Other},
  1213. {Node->getOperand(0), LO});
  1214. SDValue TF = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, fHI.getValue(1),
  1215. fLO.getValue(1));
  1216. // Add the two halves
  1217. SDValue Result =
  1218. DAG.getNode(ISD::STRICT_FADD, DL, {Node->getValueType(0), MVT::Other},
  1219. {TF, fHI, fLO});
  1220. Results.push_back(Result);
  1221. Results.push_back(Result.getValue(1));
  1222. return;
  1223. }
  1224. // Convert hi and lo to floats
  1225. // Convert the hi part back to the upper values
  1226. // TODO: Can any fast-math-flags be set on these nodes?
  1227. SDValue fHI = DAG.getNode(ISD::SINT_TO_FP, DL, Node->getValueType(0), HI);
  1228. fHI = DAG.getNode(ISD::FMUL, DL, Node->getValueType(0), fHI, TWOHW);
  1229. SDValue fLO = DAG.getNode(ISD::SINT_TO_FP, DL, Node->getValueType(0), LO);
  1230. // Add the two halves
  1231. Results.push_back(
  1232. DAG.getNode(ISD::FADD, DL, Node->getValueType(0), fHI, fLO));
  1233. }
  1234. SDValue VectorLegalizer::ExpandFNEG(SDNode *Node) {
  1235. if (TLI.isOperationLegalOrCustom(ISD::FSUB, Node->getValueType(0))) {
  1236. SDLoc DL(Node);
  1237. SDValue Zero = DAG.getConstantFP(-0.0, DL, Node->getValueType(0));
  1238. // TODO: If FNEG had fast-math-flags, they'd get propagated to this FSUB.
  1239. return DAG.getNode(ISD::FSUB, DL, Node->getValueType(0), Zero,
  1240. Node->getOperand(0));
  1241. }
  1242. return DAG.UnrollVectorOp(Node);
  1243. }
  1244. void VectorLegalizer::ExpandFSUB(SDNode *Node,
  1245. SmallVectorImpl<SDValue> &Results) {
  1246. // For floating-point values, (a-b) is the same as a+(-b). If FNEG is legal,
  1247. // we can defer this to operation legalization where it will be lowered as
  1248. // a+(-b).
  1249. EVT VT = Node->getValueType(0);
  1250. if (TLI.isOperationLegalOrCustom(ISD::FNEG, VT) &&
  1251. TLI.isOperationLegalOrCustom(ISD::FADD, VT))
  1252. return; // Defer to LegalizeDAG
  1253. SDValue Tmp = DAG.UnrollVectorOp(Node);
  1254. Results.push_back(Tmp);
  1255. }
  1256. void VectorLegalizer::ExpandSETCC(SDNode *Node,
  1257. SmallVectorImpl<SDValue> &Results) {
  1258. bool NeedInvert = false;
  1259. SDLoc dl(Node);
  1260. MVT OpVT = Node->getOperand(0).getSimpleValueType();
  1261. ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
  1262. if (TLI.getCondCodeAction(CCCode, OpVT) != TargetLowering::Expand) {
  1263. Results.push_back(UnrollVSETCC(Node));
  1264. return;
  1265. }
  1266. SDValue Chain;
  1267. SDValue LHS = Node->getOperand(0);
  1268. SDValue RHS = Node->getOperand(1);
  1269. SDValue CC = Node->getOperand(2);
  1270. bool Legalized = TLI.LegalizeSetCCCondCode(DAG, Node->getValueType(0), LHS,
  1271. RHS, CC, NeedInvert, dl, Chain);
  1272. if (Legalized) {
  1273. // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
  1274. // condition code, create a new SETCC node.
  1275. if (CC.getNode())
  1276. LHS = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), LHS, RHS, CC,
  1277. Node->getFlags());
  1278. // If we expanded the SETCC by inverting the condition code, then wrap
  1279. // the existing SETCC in a NOT to restore the intended condition.
  1280. if (NeedInvert)
  1281. LHS = DAG.getLogicalNOT(dl, LHS, LHS->getValueType(0));
  1282. } else {
  1283. // Otherwise, SETCC for the given comparison type must be completely
  1284. // illegal; expand it into a SELECT_CC.
  1285. EVT VT = Node->getValueType(0);
  1286. LHS =
  1287. DAG.getNode(ISD::SELECT_CC, dl, VT, LHS, RHS,
  1288. DAG.getBoolConstant(true, dl, VT, LHS.getValueType()),
  1289. DAG.getBoolConstant(false, dl, VT, LHS.getValueType()), CC);
  1290. LHS->setFlags(Node->getFlags());
  1291. }
  1292. Results.push_back(LHS);
  1293. }
  1294. void VectorLegalizer::ExpandUADDSUBO(SDNode *Node,
  1295. SmallVectorImpl<SDValue> &Results) {
  1296. SDValue Result, Overflow;
  1297. TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
  1298. Results.push_back(Result);
  1299. Results.push_back(Overflow);
  1300. }
  1301. void VectorLegalizer::ExpandSADDSUBO(SDNode *Node,
  1302. SmallVectorImpl<SDValue> &Results) {
  1303. SDValue Result, Overflow;
  1304. TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
  1305. Results.push_back(Result);
  1306. Results.push_back(Overflow);
  1307. }
  1308. void VectorLegalizer::ExpandMULO(SDNode *Node,
  1309. SmallVectorImpl<SDValue> &Results) {
  1310. SDValue Result, Overflow;
  1311. if (!TLI.expandMULO(Node, Result, Overflow, DAG))
  1312. std::tie(Result, Overflow) = DAG.UnrollVectorOverflowOp(Node);
  1313. Results.push_back(Result);
  1314. Results.push_back(Overflow);
  1315. }
  1316. void VectorLegalizer::ExpandFixedPointDiv(SDNode *Node,
  1317. SmallVectorImpl<SDValue> &Results) {
  1318. SDNode *N = Node;
  1319. if (SDValue Expanded = TLI.expandFixedPointDiv(N->getOpcode(), SDLoc(N),
  1320. N->getOperand(0), N->getOperand(1), N->getConstantOperandVal(2), DAG))
  1321. Results.push_back(Expanded);
  1322. }
  1323. void VectorLegalizer::ExpandStrictFPOp(SDNode *Node,
  1324. SmallVectorImpl<SDValue> &Results) {
  1325. if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP) {
  1326. ExpandUINT_TO_FLOAT(Node, Results);
  1327. return;
  1328. }
  1329. if (Node->getOpcode() == ISD::STRICT_FP_TO_UINT) {
  1330. ExpandFP_TO_UINT(Node, Results);
  1331. return;
  1332. }
  1333. UnrollStrictFPOp(Node, Results);
  1334. }
  1335. void VectorLegalizer::ExpandREM(SDNode *Node,
  1336. SmallVectorImpl<SDValue> &Results) {
  1337. assert((Node->getOpcode() == ISD::SREM || Node->getOpcode() == ISD::UREM) &&
  1338. "Expected REM node");
  1339. SDValue Result;
  1340. if (!TLI.expandREM(Node, Result, DAG))
  1341. Result = DAG.UnrollVectorOp(Node);
  1342. Results.push_back(Result);
  1343. }
  1344. void VectorLegalizer::UnrollStrictFPOp(SDNode *Node,
  1345. SmallVectorImpl<SDValue> &Results) {
  1346. EVT VT = Node->getValueType(0);
  1347. EVT EltVT = VT.getVectorElementType();
  1348. unsigned NumElems = VT.getVectorNumElements();
  1349. unsigned NumOpers = Node->getNumOperands();
  1350. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  1351. EVT TmpEltVT = EltVT;
  1352. if (Node->getOpcode() == ISD::STRICT_FSETCC ||
  1353. Node->getOpcode() == ISD::STRICT_FSETCCS)
  1354. TmpEltVT = TLI.getSetCCResultType(DAG.getDataLayout(),
  1355. *DAG.getContext(), TmpEltVT);
  1356. EVT ValueVTs[] = {TmpEltVT, MVT::Other};
  1357. SDValue Chain = Node->getOperand(0);
  1358. SDLoc dl(Node);
  1359. SmallVector<SDValue, 32> OpValues;
  1360. SmallVector<SDValue, 32> OpChains;
  1361. for (unsigned i = 0; i < NumElems; ++i) {
  1362. SmallVector<SDValue, 4> Opers;
  1363. SDValue Idx = DAG.getVectorIdxConstant(i, dl);
  1364. // The Chain is the first operand.
  1365. Opers.push_back(Chain);
  1366. // Now process the remaining operands.
  1367. for (unsigned j = 1; j < NumOpers; ++j) {
  1368. SDValue Oper = Node->getOperand(j);
  1369. EVT OperVT = Oper.getValueType();
  1370. if (OperVT.isVector())
  1371. Oper = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  1372. OperVT.getVectorElementType(), Oper, Idx);
  1373. Opers.push_back(Oper);
  1374. }
  1375. SDValue ScalarOp = DAG.getNode(Node->getOpcode(), dl, ValueVTs, Opers);
  1376. SDValue ScalarResult = ScalarOp.getValue(0);
  1377. SDValue ScalarChain = ScalarOp.getValue(1);
  1378. if (Node->getOpcode() == ISD::STRICT_FSETCC ||
  1379. Node->getOpcode() == ISD::STRICT_FSETCCS)
  1380. ScalarResult = DAG.getSelect(dl, EltVT, ScalarResult,
  1381. DAG.getAllOnesConstant(dl, EltVT),
  1382. DAG.getConstant(0, dl, EltVT));
  1383. OpValues.push_back(ScalarResult);
  1384. OpChains.push_back(ScalarChain);
  1385. }
  1386. SDValue Result = DAG.getBuildVector(VT, dl, OpValues);
  1387. SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OpChains);
  1388. Results.push_back(Result);
  1389. Results.push_back(NewChain);
  1390. }
  1391. SDValue VectorLegalizer::UnrollVSETCC(SDNode *Node) {
  1392. EVT VT = Node->getValueType(0);
  1393. unsigned NumElems = VT.getVectorNumElements();
  1394. EVT EltVT = VT.getVectorElementType();
  1395. SDValue LHS = Node->getOperand(0);
  1396. SDValue RHS = Node->getOperand(1);
  1397. SDValue CC = Node->getOperand(2);
  1398. EVT TmpEltVT = LHS.getValueType().getVectorElementType();
  1399. SDLoc dl(Node);
  1400. SmallVector<SDValue, 8> Ops(NumElems);
  1401. for (unsigned i = 0; i < NumElems; ++i) {
  1402. SDValue LHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, LHS,
  1403. DAG.getVectorIdxConstant(i, dl));
  1404. SDValue RHSElem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT, RHS,
  1405. DAG.getVectorIdxConstant(i, dl));
  1406. Ops[i] = DAG.getNode(ISD::SETCC, dl,
  1407. TLI.getSetCCResultType(DAG.getDataLayout(),
  1408. *DAG.getContext(), TmpEltVT),
  1409. LHSElem, RHSElem, CC);
  1410. Ops[i] = DAG.getSelect(dl, EltVT, Ops[i], DAG.getAllOnesConstant(dl, EltVT),
  1411. DAG.getConstant(0, dl, EltVT));
  1412. }
  1413. return DAG.getBuildVector(VT, dl, Ops);
  1414. }
  1415. bool SelectionDAG::LegalizeVectors() {
  1416. return VectorLegalizer(*this).Run();
  1417. }