blob: f452d629dcf9f365bc658030e7674944b062b511 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth411fb402014-07-26 05:49:40 +000014#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
Hal Finkel19775142014-03-31 17:48:10 +000016#include "llvm/ADT/SmallSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Paul Redmondf29ddfe2013-02-15 18:45:18 +000018#include "llvm/ADT/Triple.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000019#include "llvm/CodeGen/MachineFunction.h"
Jim Laskey70323a82006-12-14 19:17:33 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Nirav Davefa250ca2016-03-25 21:06:30 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/CallingConv.h"
24#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000026#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DerivedTypes.h"
Chandler Carrutha7c44e62013-01-08 05:11:57 +000028#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/LLVMContext.h"
David Greeneae4f2662010-01-05 01:24:53 +000030#include "llvm/Support/Debug.h"
Jim Grosbachd64dfc12010-06-18 21:43:38 +000031#include "llvm/Support/ErrorHandling.h"
Duncan Sands1826ded2007-10-28 12:59:45 +000032#include "llvm/Support/MathExtras.h"
Chris Lattner13626022009-08-23 06:03:38 +000033#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetFrameLowering.h"
35#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000037#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000038using namespace llvm;
39
Chandler Carruthb1432742014-07-28 17:55:07 +000040#define DEBUG_TYPE "legalizedag"
41
Matthias Braunb9610a62015-11-12 01:02:47 +000042namespace {
43
44struct FloatSignAsInt;
45
Chris Lattnerdc750592005-01-07 07:47:09 +000046//===----------------------------------------------------------------------===//
Sanjay Pateleb4a4d52014-11-21 18:58:38 +000047/// This takes an arbitrary SelectionDAG as input and
Chris Lattnerdc750592005-01-07 07:47:09 +000048/// hacks on it until the target machine can handle it. This involves
49/// eliminating value sizes the machine cannot handle (promoting small sizes to
50/// large sizes or splitting up large values into small values) as well as
51/// eliminating operations the machine cannot handle.
52///
53/// This code also does a small amount of optimization and recognition of idioms
54/// as part of its processing. For example, if a target does not support a
55/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
56/// will attempt merge setcc and brc instructions into brcc's.
57///
Chandler Carruth1f52b3d2014-08-01 19:49:59 +000058class SelectionDAGLegalize {
Dan Gohmanc3349602010-04-19 19:05:59 +000059 const TargetMachine &TM;
Dan Gohman21cea8a2010-04-17 15:26:15 +000060 const TargetLowering &TLI;
Chris Lattnerdc750592005-01-07 07:47:09 +000061 SelectionDAG &DAG;
62
Chandler Carruth411fb402014-07-26 05:49:40 +000063 /// \brief The set of nodes which have already been legalized. We hold a
64 /// reference to it in order to update as necessary on node deletion.
65 SmallPtrSetImpl<SDNode *> &LegalizedNodes;
66
67 /// \brief A set of all the nodes updated during legalization.
68 SmallSetVector<SDNode *, 16> *UpdatedNodes;
Dan Gohman198b7ff2011-11-03 21:49:52 +000069
Matt Arsenault758659232013-05-18 00:21:46 +000070 EVT getSetCCResultType(EVT VT) const {
Mehdi Amini44ede332015-07-09 02:09:04 +000071 return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
Matt Arsenault758659232013-05-18 00:21:46 +000072 }
73
Chris Lattner462505f2006-02-13 09:18:02 +000074 // Libcall insertion helpers.
Scott Michelcf0da6c2009-02-17 22:15:04 +000075
Chris Lattnerdc750592005-01-07 07:47:09 +000076public:
Chandler Carruth411fb402014-07-26 05:49:40 +000077 SelectionDAGLegalize(SelectionDAG &DAG,
Chandler Carruth411fb402014-07-26 05:49:40 +000078 SmallPtrSetImpl<SDNode *> &LegalizedNodes,
79 SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
Chandler Carruth1f52b3d2014-08-01 19:49:59 +000080 : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
81 LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
Chris Lattnerdc750592005-01-07 07:47:09 +000082
Chandler Carruth411fb402014-07-26 05:49:40 +000083 /// \brief Legalizes the given operation.
Dan Gohman198b7ff2011-11-03 21:49:52 +000084 void LegalizeOp(SDNode *Node);
Scott Michelcf0da6c2009-02-17 22:15:04 +000085
Chandler Carruth411fb402014-07-26 05:49:40 +000086private:
Eli Friedmanaee3f622009-06-06 07:04:42 +000087 SDValue OptimizeFloatStore(StoreSDNode *ST);
88
Nadav Rotemde6fd282012-07-11 08:52:09 +000089 void LegalizeLoadOps(SDNode *Node);
90 void LegalizeStoreOps(SDNode *Node);
91
Sanjay Pateleb4a4d52014-11-21 18:58:38 +000092 /// Some targets cannot handle a variable
Nate Begeman6f94f612008-04-25 18:07:40 +000093 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
94 /// is necessary to spill the vector being inserted into to memory, perform
95 /// the insert there, and then read the result back.
Benjamin Kramerbdc49562016-06-12 15:39:02 +000096 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
97 const SDLoc &dl);
98 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx,
99 const SDLoc &dl);
Dan Gohman2a7de412007-10-11 23:57:53 +0000100
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000101 /// Return a vector shuffle operation which
Nate Begeman5f829d82009-04-29 05:20:52 +0000102 /// performs the same shuffe in terms of order or result bytes, but on a type
103 /// whose vector element type is narrower than the original shuffle type.
104 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000105 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl,
Jim Grosbach9b7755f2010-07-02 17:41:59 +0000106 SDValue N1, SDValue N2,
Benjamin Kramer339ced42012-01-15 13:16:05 +0000107 ArrayRef<int> Mask) const;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000108
Tom Stellard08690a12013-09-28 02:50:32 +0000109 bool LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000110 bool &NeedInvert, const SDLoc &dl);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000111
Eli Friedmanb3554152009-05-27 02:21:29 +0000112 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Eric Christopherbcaedb52011-04-20 01:19:45 +0000113 SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000114 unsigned NumOps, bool isSigned, const SDLoc &dl);
Eric Christopherbcaedb52011-04-20 01:19:45 +0000115
Jim Grosbachd64dfc12010-06-18 21:43:38 +0000116 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
117 SDNode *Node, bool isSigned);
Eli Friedmand6f28342009-05-27 03:33:44 +0000118 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
119 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000120 RTLIB::Libcall Call_F128,
121 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikovf93bb392009-11-07 17:14:39 +0000122 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
123 RTLIB::Libcall Call_I8,
124 RTLIB::Libcall Call_I16,
125 RTLIB::Libcall Call_I32,
126 RTLIB::Libcall Call_I64,
Eli Friedmand6f28342009-05-27 03:33:44 +0000127 RTLIB::Libcall Call_I128);
Evan Chengb14ce092011-04-16 03:08:26 +0000128 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000129 void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000130
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000131 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
132 const SDLoc &dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000133 SDValue ExpandBUILD_VECTOR(SDNode *Node);
134 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman2892d822009-05-27 12:20:41 +0000135 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
136 SmallVectorImpl<SDValue> &Results);
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000137 void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL,
138 SDValue Value) const;
139 SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL,
Matthias Braunb9610a62015-11-12 01:02:47 +0000140 SDValue NewIntValue) const;
141 SDValue ExpandFCOPYSIGN(SDNode *Node) const;
142 SDValue ExpandFABS(SDNode *Node) const;
Owen Anderson53aa7a92009-08-10 22:56:29 +0000143 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000144 const SDLoc &dl);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000145 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000146 const SDLoc &dl);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000147 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000148 const SDLoc &dl);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000149
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000150 SDValue ExpandBITREVERSE(SDValue Op, const SDLoc &dl);
151 SDValue ExpandBSWAP(SDValue Op, const SDLoc &dl);
152 SDValue ExpandBitCount(unsigned Opc, SDValue Op, const SDLoc &dl);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000153
Eli Friedman40afdb62009-05-23 22:37:25 +0000154 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenebab5e6e2011-01-26 19:13:22 +0000155 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000156 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman21d349b2009-05-27 01:25:56 +0000157
Dan Gohman198b7ff2011-11-03 21:49:52 +0000158 SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
Chih-Hung Hsiehed7d81e2015-12-03 22:02:40 +0000159 SDValue ExpandConstant(ConstantSDNode *CP);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000160
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000161 // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
162 bool ExpandNode(SDNode *Node);
163 void ConvertNodeToLibcall(SDNode *Node);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000164 void PromoteNode(SDNode *Node);
165
Eli Friedman13477152011-11-11 23:58:27 +0000166public:
Eli Friedman13477152011-11-11 23:58:27 +0000167 // Node replacement helpers
168 void ReplacedNode(SDNode *N) {
Chandler Carruth1f52b3d2014-08-01 19:49:59 +0000169 LegalizedNodes.erase(N);
Chandler Carruth74ec9e12014-08-27 11:22:16 +0000170 if (UpdatedNodes)
171 UpdatedNodes->insert(N);
Eli Friedman13477152011-11-11 23:58:27 +0000172 }
173 void ReplaceNode(SDNode *Old, SDNode *New) {
Chandler Carruthb1432742014-07-28 17:55:07 +0000174 DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
175 dbgs() << " with: "; New->dump(&DAG));
176
Chandler Carruth5a85c7b2014-07-26 05:53:16 +0000177 assert(Old->getNumValues() == New->getNumValues() &&
178 "Replacing one node with another that produces a different number "
179 "of values!");
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000180 DAG.ReplaceAllUsesWith(Old, New);
Chandler Carruth411fb402014-07-26 05:49:40 +0000181 if (UpdatedNodes)
182 UpdatedNodes->insert(New);
Eli Friedman13477152011-11-11 23:58:27 +0000183 ReplacedNode(Old);
184 }
185 void ReplaceNode(SDValue Old, SDValue New) {
Chandler Carruthb1432742014-07-28 17:55:07 +0000186 DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
187 dbgs() << " with: "; New->dump(&DAG));
188
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000189 DAG.ReplaceAllUsesWith(Old, New);
Chandler Carruth411fb402014-07-26 05:49:40 +0000190 if (UpdatedNodes)
191 UpdatedNodes->insert(New.getNode());
Eli Friedman13477152011-11-11 23:58:27 +0000192 ReplacedNode(Old.getNode());
193 }
194 void ReplaceNode(SDNode *Old, const SDValue *New) {
Chandler Carruthb1432742014-07-28 17:55:07 +0000195 DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
196
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000197 DAG.ReplaceAllUsesWith(Old, New);
Chandler Carruthb1432742014-07-28 17:55:07 +0000198 for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
199 DEBUG(dbgs() << (i == 0 ? " with: "
200 : " and: ");
201 New[i]->dump(&DAG));
Chandler Carruthb1432742014-07-28 17:55:07 +0000202 if (UpdatedNodes)
203 UpdatedNodes->insert(New[i].getNode());
204 }
Eli Friedman13477152011-11-11 23:58:27 +0000205 ReplacedNode(Old);
206 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000207};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000208}
Chris Lattnerdc750592005-01-07 07:47:09 +0000209
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000210/// Return a vector shuffle operation which
Nate Begeman5f829d82009-04-29 05:20:52 +0000211/// performs the same shuffe in terms of order or result bytes, but on a type
212/// whose vector element type is narrower than the original shuffle type.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000213/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000214SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType(
215 EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
216 ArrayRef<int> Mask) const {
Nate Begeman5f829d82009-04-29 05:20:52 +0000217 unsigned NumMaskElts = VT.getVectorNumElements();
218 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000219 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner6be79822006-04-04 17:23:26 +0000220
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000221 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
222
223 if (NumEltsGrowth == 1)
Craig Topper2bd8b4b2016-07-01 06:54:47 +0000224 return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask);
Jim Grosbach9b7755f2010-07-02 17:41:59 +0000225
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000226 SmallVector<int, 8> NewMask;
Nate Begeman5f829d82009-04-29 05:20:52 +0000227 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000228 int Idx = Mask[i];
229 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach9b7755f2010-07-02 17:41:59 +0000230 if (Idx < 0)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000231 NewMask.push_back(-1);
232 else
233 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner6be79822006-04-04 17:23:26 +0000234 }
Chris Lattner6be79822006-04-04 17:23:26 +0000235 }
Nate Begeman5f829d82009-04-29 05:20:52 +0000236 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000237 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
Craig Topper2bd8b4b2016-07-01 06:54:47 +0000238 return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask);
Chris Lattner6be79822006-04-04 17:23:26 +0000239}
240
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000241/// Expands the ConstantFP node to an integer constant or
Evan Cheng22cf8992006-12-13 20:57:08 +0000242/// a load from the constant pool.
Dan Gohman198b7ff2011-11-03 21:49:52 +0000243SDValue
244SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
Evan Cheng47833a12006-12-12 21:32:44 +0000245 bool Extend = false;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000246 SDLoc dl(CFP);
Evan Cheng47833a12006-12-12 21:32:44 +0000247
248 // If a FP immediate is precise when represented as a float and if the
249 // target can do an extending load from float to double, we put it into
250 // the constant pool as a float, even if it's is statically typed as a
Chris Lattner3dc38992008-03-05 06:46:58 +0000251 // double. This shrinks FP constants and canonicalizes them for targets where
252 // an FP extending load is the same cost as a normal load (such as on the x87
253 // fp stack or PPC FP unit).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000254 EVT VT = CFP->getValueType(0);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000255 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng22cf8992006-12-13 20:57:08 +0000256 if (!UseCP) {
Owen Anderson9f944592009-08-11 20:47:22 +0000257 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000258 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
Owen Anderson9f944592009-08-11 20:47:22 +0000259 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000260 }
261
Elliot Colp82b14682016-08-03 15:09:21 +0000262 APFloat APF = CFP->getValueAPF();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000263 EVT OrigVT = VT;
264 EVT SVT = VT;
Elliot Colp82b14682016-08-03 15:09:21 +0000265
266 // We don't want to shrink SNaNs. Converting the SNaN back to its real type
267 // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ).
268 if (!APF.isSignaling()) {
269 while (SVT != MVT::f32 && SVT != MVT::f16) {
270 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
271 if (ConstantFPSDNode::isValueValidForType(SVT, APF) &&
272 // Only do this if the target has a native EXTLOAD instruction from
273 // smaller type.
274 TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
275 TLI.ShouldShrinkFPConstant(OrigVT)) {
276 Type *SType = SVT.getTypeForEVT(*DAG.getContext());
277 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
278 VT = SVT;
279 Extend = true;
280 }
Evan Cheng38caf772008-03-04 08:05:30 +0000281 }
Evan Cheng47833a12006-12-12 21:32:44 +0000282 }
283
Mehdi Amini44ede332015-07-09 02:09:04 +0000284 SDValue CPIdx =
285 DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000286 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman198b7ff2011-11-03 21:49:52 +0000287 if (Extend) {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000288 SDValue Result = DAG.getExtLoad(
289 ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
290 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT,
Justin Lebar9c375812016-07-15 18:27:10 +0000291 Alignment);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000292 return Result;
293 }
Justin Lebar9c375812016-07-15 18:27:10 +0000294 SDValue Result = DAG.getLoad(
295 OrigVT, dl, DAG.getEntryNode(), CPIdx,
296 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000297 return Result;
Evan Cheng47833a12006-12-12 21:32:44 +0000298}
299
Chih-Hung Hsiehed7d81e2015-12-03 22:02:40 +0000300/// Expands the Constant node to a load from the constant pool.
301SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) {
302 SDLoc dl(CP);
303 EVT VT = CP->getValueType(0);
304 SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(),
305 TLI.getPointerTy(DAG.getDataLayout()));
306 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Justin Lebar9c375812016-07-15 18:27:10 +0000307 SDValue Result = DAG.getLoad(
308 VT, dl, DAG.getEntryNode(), CPIdx,
309 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
Chih-Hung Hsiehed7d81e2015-12-03 22:02:40 +0000310 return Result;
311}
312
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000313/// Some target cannot handle a variable insertion index for the
314/// INSERT_VECTOR_ELT instruction. In this case, it
Nate Begeman6f94f612008-04-25 18:07:40 +0000315/// is necessary to spill the vector being inserted into to memory, perform
316/// the insert there, and then read the result back.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000317SDValue SelectionDAGLegalize::PerformInsertVectorEltInMemory(SDValue Vec,
318 SDValue Val,
319 SDValue Idx,
320 const SDLoc &dl) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000321 SDValue Tmp1 = Vec;
322 SDValue Tmp2 = Val;
323 SDValue Tmp3 = Idx;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000324
Nate Begeman6f94f612008-04-25 18:07:40 +0000325 // If the target doesn't support this, we have to spill the input vector
326 // to a temporary stack slot, update the element, then reload it. This is
327 // badness. We could also load the value into a vector register (either
328 // with a "move to register" or "extload into register" instruction, then
329 // permute it into place, if the idx is a constant and if the idx is
330 // supported by the target.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000331 EVT VT = Tmp1.getValueType();
332 EVT EltVT = VT.getVectorElementType();
333 EVT IdxVT = Tmp3.getValueType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000334 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000335 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000336
Evan Cheng0e9d9ca2009-10-18 18:16:27 +0000337 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
338
Nate Begeman6f94f612008-04-25 18:07:40 +0000339 // Store the vector.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000340 SDValue Ch = DAG.getStore(
341 DAG.getEntryNode(), dl, Tmp1, StackPtr,
Justin Lebar9c375812016-07-15 18:27:10 +0000342 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
Nate Begeman6f94f612008-04-25 18:07:40 +0000343
344 // Truncate or zero extend offset to target pointer type.
Pete Cooper8acd3862015-07-15 00:43:54 +0000345 Tmp3 = DAG.getZExtOrTrunc(Tmp3, dl, PtrVT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000346 // Add the offset to the index.
Dan Gohman9b80f862010-02-25 15:20:39 +0000347 unsigned EltSize = EltVT.getSizeInBits()/8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000348 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,
349 DAG.getConstant(EltSize, dl, IdxVT));
Dale Johannesenad00f6e2009-02-02 20:41:04 +0000350 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman6f94f612008-04-25 18:07:40 +0000351 // Store the scalar value.
Justin Lebar9c375812016-07-15 18:27:10 +0000352 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000353 // Load the updated vector.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000354 return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack(
Justin Lebar9c375812016-07-15 18:27:10 +0000355 DAG.getMachineFunction(), SPFI));
Nate Begeman6f94f612008-04-25 18:07:40 +0000356}
357
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000358SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
359 SDValue Idx,
360 const SDLoc &dl) {
Eli Friedmana8f9a022009-05-27 02:16:40 +0000361 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
362 // SCALAR_TO_VECTOR requires that the type of the value being inserted
363 // match the element type of the vector being created, except for
364 // integers in which case the inserted value can be over width.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000365 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedmana8f9a022009-05-27 02:16:40 +0000366 if (Val.getValueType() == EltVT ||
367 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
368 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
369 Vec.getValueType(), Val);
370
371 unsigned NumElts = Vec.getValueType().getVectorNumElements();
372 // We generate a shuffle of InVec and ScVec, so the shuffle mask
373 // should be 0,1,2,3,4,5... with the appropriate element replaced with
374 // elt 0 of the RHS.
375 SmallVector<int, 8> ShufOps;
376 for (unsigned i = 0; i != NumElts; ++i)
377 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
378
Craig Topper2bd8b4b2016-07-01 06:54:47 +0000379 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps);
Eli Friedmana8f9a022009-05-27 02:16:40 +0000380 }
381 }
382 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
383}
384
Eli Friedmanaee3f622009-06-06 07:04:42 +0000385SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
386 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
387 // FIXME: We shouldn't do this for TargetConstantFP's.
388 // FIXME: move this to the DAG Combiner! Note that we can't regress due
389 // to phase ordering between legalized code and the dag combiner. This
390 // probably means that we need to integrate dag combiner and legalizer
391 // together.
392 // We generally can't do this one for long doubles.
Nadav Rotem2a148662012-07-11 11:02:16 +0000393 SDValue Chain = ST->getChain();
394 SDValue Ptr = ST->getBasePtr();
Eli Friedmanaee3f622009-06-06 07:04:42 +0000395 unsigned Alignment = ST->getAlignment();
Justin Lebar9c375812016-07-15 18:27:10 +0000396 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
Hal Finkelcc39b672014-07-24 12:16:19 +0000397 AAMDNodes AAInfo = ST->getAAInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000398 SDLoc dl(ST);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000399 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson9f944592009-08-11 20:47:22 +0000400 if (CFP->getValueType(0) == MVT::f32 &&
Dan Gohmane49e7422011-07-15 22:39:09 +0000401 TLI.isTypeLegal(MVT::i32)) {
Nadav Rotem2a148662012-07-11 11:02:16 +0000402 SDValue Con = DAG.getConstant(CFP->getValueAPF().
Eli Friedmanaee3f622009-06-06 07:04:42 +0000403 bitcastToAPInt().zextOrTrunc(32),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000404 SDLoc(CFP), MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +0000405 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), Alignment,
406 MMOFlags, AAInfo);
Chris Lattner6963c1f2010-09-21 17:42:31 +0000407 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000408
Chris Lattner6963c1f2010-09-21 17:42:31 +0000409 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedmanaee3f622009-06-06 07:04:42 +0000410 // If this target supports 64-bit registers, do a single 64-bit store.
Dan Gohmane49e7422011-07-15 22:39:09 +0000411 if (TLI.isTypeLegal(MVT::i64)) {
Nadav Rotem2a148662012-07-11 11:02:16 +0000412 SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000413 zextOrTrunc(64), SDLoc(CFP), MVT::i64);
Nadav Rotem2a148662012-07-11 11:02:16 +0000414 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +0000415 Alignment, MMOFlags, AAInfo);
Chris Lattner6963c1f2010-09-21 17:42:31 +0000416 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000417
Dan Gohmane49e7422011-07-15 22:39:09 +0000418 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
Eli Friedmanaee3f622009-06-06 07:04:42 +0000419 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
420 // stores. If the target supports neither 32- nor 64-bits, this
421 // xform is certainly not worth it.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000422 const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
423 SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
424 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000425 if (DAG.getDataLayout().isBigEndian())
426 std::swap(Lo, Hi);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000427
Justin Lebar9c375812016-07-15 18:27:10 +0000428 Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), Alignment,
429 MMOFlags, AAInfo);
Nadav Rotem2a148662012-07-11 11:02:16 +0000430 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000431 DAG.getConstant(4, dl, Ptr.getValueType()));
Nadav Rotem2a148662012-07-11 11:02:16 +0000432 Hi = DAG.getStore(Chain, dl, Hi, Ptr,
Chris Lattner6963c1f2010-09-21 17:42:31 +0000433 ST->getPointerInfo().getWithOffset(4),
Justin Lebar9c375812016-07-15 18:27:10 +0000434 MinAlign(Alignment, 4U), MMOFlags, AAInfo);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000435
Owen Anderson9f944592009-08-11 20:47:22 +0000436 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000437 }
438 }
439 }
Craig Topperc0196b12014-04-14 00:51:57 +0000440 return SDValue(nullptr, 0);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000441}
442
Nadav Rotemde6fd282012-07-11 08:52:09 +0000443void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
444 StoreSDNode *ST = cast<StoreSDNode>(Node);
Nadav Rotem2a148662012-07-11 11:02:16 +0000445 SDValue Chain = ST->getChain();
446 SDValue Ptr = ST->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000447 SDLoc dl(Node);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000448
449 unsigned Alignment = ST->getAlignment();
Justin Lebar9c375812016-07-15 18:27:10 +0000450 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
Hal Finkelcc39b672014-07-24 12:16:19 +0000451 AAMDNodes AAInfo = ST->getAAInfo();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000452
453 if (!ST->isTruncatingStore()) {
454 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
455 ReplaceNode(ST, OptStore);
456 return;
457 }
458
459 {
Nadav Rotem2a148662012-07-11 11:02:16 +0000460 SDValue Value = ST->getValue();
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000461 MVT VT = Value.getSimpleValueType();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000462 switch (TLI.getOperationAction(ISD::STORE, VT)) {
463 default: llvm_unreachable("This action is not supported yet!");
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000464 case TargetLowering::Legal: {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000465 // If this is an unaligned store and the target doesn't support it,
466 // expand it.
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000467 EVT MemVT = ST->getMemoryVT();
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000468 unsigned AS = ST->getAddressSpace();
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000469 unsigned Align = ST->getAlignment();
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000470 const DataLayout &DL = DAG.getDataLayout();
Matt Arsenault7846d882016-04-21 18:19:11 +0000471 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) {
472 SDValue Result = TLI.expandUnalignedStore(ST, DAG);
473 ReplaceNode(SDValue(ST, 0), Result);
474 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000475 break;
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000476 }
Nadav Rotem2a148662012-07-11 11:02:16 +0000477 case TargetLowering::Custom: {
478 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
Hal Finkelcec70132015-02-24 12:59:47 +0000479 if (Res && Res != SDValue(Node, 0))
Nadav Rotem2a148662012-07-11 11:02:16 +0000480 ReplaceNode(SDValue(Node, 0), Res);
481 return;
482 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000483 case TargetLowering::Promote: {
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000484 MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
Tom Stellardb785bd72012-12-10 21:41:54 +0000485 assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
486 "Can only promote stores to same size type");
487 Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000488 SDValue Result =
Justin Lebar9c375812016-07-15 18:27:10 +0000489 DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
490 Alignment, MMOFlags, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000491 ReplaceNode(SDValue(Node, 0), Result);
492 break;
493 }
494 }
495 return;
496 }
497 } else {
Nadav Rotem2a148662012-07-11 11:02:16 +0000498 SDValue Value = ST->getValue();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000499
500 EVT StVT = ST->getMemoryVT();
501 unsigned StWidth = StVT.getSizeInBits();
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000502 auto &DL = DAG.getDataLayout();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000503
504 if (StWidth != StVT.getStoreSizeInBits()) {
505 // Promote to a byte-sized store with upper bits zero if not
506 // storing an integral number of bytes. For example, promote
507 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
508 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
509 StVT.getStoreSizeInBits());
Nadav Rotem2a148662012-07-11 11:02:16 +0000510 Value = DAG.getZeroExtendInReg(Value, dl, StVT);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000511 SDValue Result =
Justin Lebar9c375812016-07-15 18:27:10 +0000512 DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT,
513 Alignment, MMOFlags, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000514 ReplaceNode(SDValue(Node, 0), Result);
515 } else if (StWidth & (StWidth - 1)) {
516 // If not storing a power-of-2 number of bits, expand as two stores.
517 assert(!StVT.isVector() && "Unsupported truncstore!");
518 unsigned RoundWidth = 1 << Log2_32(StWidth);
519 assert(RoundWidth < StWidth);
520 unsigned ExtraWidth = StWidth - RoundWidth;
521 assert(ExtraWidth < RoundWidth);
522 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
523 "Store size not an integral number of bytes!");
524 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
525 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
526 SDValue Lo, Hi;
527 unsigned IncrementSize;
528
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000529 if (DL.isLittleEndian()) {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000530 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
531 // Store the bottom RoundWidth bits.
Nadav Rotem2a148662012-07-11 11:02:16 +0000532 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +0000533 RoundVT, Alignment, MMOFlags, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000534
535 // Store the remaining ExtraWidth bits.
536 IncrementSize = RoundWidth / 8;
Nadav Rotem2a148662012-07-11 11:02:16 +0000537 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000538 DAG.getConstant(IncrementSize, dl,
539 Ptr.getValueType()));
Mehdi Amini9639d652015-07-09 02:09:20 +0000540 Hi = DAG.getNode(
541 ISD::SRL, dl, Value.getValueType(), Value,
542 DAG.getConstant(RoundWidth, dl,
543 TLI.getShiftAmountTy(Value.getValueType(), DL)));
Justin Lebar9c375812016-07-15 18:27:10 +0000544 Hi = DAG.getTruncStore(
545 Chain, dl, Hi, Ptr,
546 ST->getPointerInfo().getWithOffset(IncrementSize), ExtraVT,
547 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000548 } else {
549 // Big endian - avoid unaligned stores.
550 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
551 // Store the top RoundWidth bits.
Mehdi Amini9639d652015-07-09 02:09:20 +0000552 Hi = DAG.getNode(
553 ISD::SRL, dl, Value.getValueType(), Value,
554 DAG.getConstant(ExtraWidth, dl,
555 TLI.getShiftAmountTy(Value.getValueType(), DL)));
Nadav Rotem2a148662012-07-11 11:02:16 +0000556 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +0000557 RoundVT, Alignment, MMOFlags, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000558
559 // Store the remaining ExtraWidth bits.
560 IncrementSize = RoundWidth / 8;
Nadav Rotem2a148662012-07-11 11:02:16 +0000561 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000562 DAG.getConstant(IncrementSize, dl,
563 Ptr.getValueType()));
Justin Lebar9c375812016-07-15 18:27:10 +0000564 Lo = DAG.getTruncStore(
565 Chain, dl, Value, Ptr,
566 ST->getPointerInfo().getWithOffset(IncrementSize), ExtraVT,
567 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000568 }
569
570 // The order of the stores doesn't matter.
571 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
572 ReplaceNode(SDValue(Node, 0), Result);
573 } else {
Eric Christopher4675c432015-11-25 09:11:53 +0000574 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000575 default: llvm_unreachable("This action is not supported yet!");
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000576 case TargetLowering::Legal: {
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000577 EVT MemVT = ST->getMemoryVT();
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000578 unsigned AS = ST->getAddressSpace();
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000579 unsigned Align = ST->getAlignment();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000580 // If this is an unaligned store and the target doesn't support it,
581 // expand it.
Matt Arsenault7846d882016-04-21 18:19:11 +0000582 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) {
583 SDValue Result = TLI.expandUnalignedStore(ST, DAG);
584 ReplaceNode(SDValue(ST, 0), Result);
585 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000586 break;
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000587 }
Nadav Rotem2a148662012-07-11 11:02:16 +0000588 case TargetLowering::Custom: {
589 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
Hal Finkelcec70132015-02-24 12:59:47 +0000590 if (Res && Res != SDValue(Node, 0))
Nadav Rotem2a148662012-07-11 11:02:16 +0000591 ReplaceNode(SDValue(Node, 0), Res);
592 return;
593 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000594 case TargetLowering::Expand:
595 assert(!StVT.isVector() &&
596 "Vector Stores are handled in LegalizeVectorOps");
597
598 // TRUNCSTORE:i16 i32 -> STORE i16
Nadav Rotem2a148662012-07-11 11:02:16 +0000599 assert(TLI.isTypeLegal(StVT) &&
600 "Do not know how to expand this store!");
601 Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000602 SDValue Result =
Justin Lebar9c375812016-07-15 18:27:10 +0000603 DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
604 Alignment, MMOFlags, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000605 ReplaceNode(SDValue(Node, 0), Result);
606 break;
607 }
608 }
609 }
610}
611
612void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
613 LoadSDNode *LD = cast<LoadSDNode>(Node);
Nadav Rotem2a148662012-07-11 11:02:16 +0000614 SDValue Chain = LD->getChain(); // The chain.
615 SDValue Ptr = LD->getBasePtr(); // The base pointer.
616 SDValue Value; // The value returned by the load op.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000617 SDLoc dl(Node);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000618
619 ISD::LoadExtType ExtType = LD->getExtensionType();
620 if (ExtType == ISD::NON_EXTLOAD) {
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000621 MVT VT = Node->getSimpleValueType(0);
Nadav Rotem2a148662012-07-11 11:02:16 +0000622 SDValue RVal = SDValue(Node, 0);
623 SDValue RChain = SDValue(Node, 1);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000624
625 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
626 default: llvm_unreachable("This action is not supported yet!");
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000627 case TargetLowering::Legal: {
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000628 EVT MemVT = LD->getMemoryVT();
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000629 unsigned AS = LD->getAddressSpace();
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000630 unsigned Align = LD->getAlignment();
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000631 const DataLayout &DL = DAG.getDataLayout();
Evan Chengc5735992012-09-18 01:34:40 +0000632 // If this is an unaligned load and the target doesn't support it,
633 // expand it.
Matt Arsenault7846d882016-04-21 18:19:11 +0000634 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) {
635 std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG);
636 }
Evan Chengc5735992012-09-18 01:34:40 +0000637 break;
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000638 }
Nadav Rotem2a148662012-07-11 11:02:16 +0000639 case TargetLowering::Custom: {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +0000640 if (SDValue Res = TLI.LowerOperation(RVal, DAG)) {
Evan Chengc5735992012-09-18 01:34:40 +0000641 RVal = Res;
642 RChain = Res.getValue(1);
643 }
644 break;
Nadav Rotem2a148662012-07-11 11:02:16 +0000645 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000646 case TargetLowering::Promote: {
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000647 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Tom Stellard30e2aa52012-12-10 21:41:58 +0000648 assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
649 "Can only promote loads to same size type");
Nadav Rotemde6fd282012-07-11 08:52:09 +0000650
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000651 SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
Nadav Rotem2a148662012-07-11 11:02:16 +0000652 RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
653 RChain = Res.getValue(1);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000654 break;
655 }
656 }
Nadav Rotem2a148662012-07-11 11:02:16 +0000657 if (RChain.getNode() != Node) {
658 assert(RVal.getNode() != Node && "Load must be completely replaced");
659 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
660 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
Chandler Carruth411fb402014-07-26 05:49:40 +0000661 if (UpdatedNodes) {
662 UpdatedNodes->insert(RVal.getNode());
663 UpdatedNodes->insert(RChain.getNode());
664 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000665 ReplacedNode(Node);
666 }
667 return;
668 }
669
670 EVT SrcVT = LD->getMemoryVT();
671 unsigned SrcWidth = SrcVT.getSizeInBits();
672 unsigned Alignment = LD->getAlignment();
Justin Lebar9c375812016-07-15 18:27:10 +0000673 MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
Hal Finkelcc39b672014-07-24 12:16:19 +0000674 AAMDNodes AAInfo = LD->getAAInfo();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000675
676 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
677 // Some targets pretend to have an i1 loading operation, and actually
678 // load an i8. This trick is correct for ZEXTLOAD because the top 7
679 // bits are guaranteed to be zero; it helps the optimizers understand
680 // that these bits are zero. It is also useful for EXTLOAD, since it
681 // tells the optimizers that those bits are undefined. It would be
682 // nice to have an effective generic way of getting these benefits...
683 // Until such a way is found, don't insist on promoting i1 here.
684 (SrcVT != MVT::i1 ||
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000685 TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
686 TargetLowering::Promote)) {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000687 // Promote to a byte-sized load if not loading an integral number of
688 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
689 unsigned NewWidth = SrcVT.getStoreSizeInBits();
690 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
691 SDValue Ch;
692
693 // The extra bits are guaranteed to be zero, since we stored them that
694 // way. A zext load from NVT thus automatically gives zext from SrcVT.
695
696 ISD::LoadExtType NewExtType =
697 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
698
699 SDValue Result =
Justin Lebar9c375812016-07-15 18:27:10 +0000700 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0), Chain, Ptr,
701 LD->getPointerInfo(), NVT, Alignment, MMOFlags, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000702
703 Ch = Result.getValue(1); // The chain.
704
705 if (ExtType == ISD::SEXTLOAD)
706 // Having the top bits zero doesn't help when sign extending.
707 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
708 Result.getValueType(),
709 Result, DAG.getValueType(SrcVT));
710 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
711 // All the top bits are guaranteed to be zero - inform the optimizers.
712 Result = DAG.getNode(ISD::AssertZext, dl,
713 Result.getValueType(), Result,
714 DAG.getValueType(SrcVT));
715
Nadav Rotem2a148662012-07-11 11:02:16 +0000716 Value = Result;
717 Chain = Ch;
Nadav Rotemde6fd282012-07-11 08:52:09 +0000718 } else if (SrcWidth & (SrcWidth - 1)) {
719 // If not loading a power-of-2 number of bits, expand as two loads.
720 assert(!SrcVT.isVector() && "Unsupported extload!");
721 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
722 assert(RoundWidth < SrcWidth);
723 unsigned ExtraWidth = SrcWidth - RoundWidth;
724 assert(ExtraWidth < RoundWidth);
725 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
726 "Load size not an integral number of bytes!");
727 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
728 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
729 SDValue Lo, Hi, Ch;
730 unsigned IncrementSize;
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000731 auto &DL = DAG.getDataLayout();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000732
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000733 if (DL.isLittleEndian()) {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000734 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
735 // Load the bottom RoundWidth bits.
Justin Lebar9c375812016-07-15 18:27:10 +0000736 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
737 LD->getPointerInfo(), RoundVT, Alignment, MMOFlags,
738 AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000739
740 // Load the remaining ExtraWidth bits.
741 IncrementSize = RoundWidth / 8;
Nadav Rotem2a148662012-07-11 11:02:16 +0000742 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000743 DAG.getConstant(IncrementSize, dl,
744 Ptr.getValueType()));
Nadav Rotem2a148662012-07-11 11:02:16 +0000745 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +0000746 LD->getPointerInfo().getWithOffset(IncrementSize),
Justin Lebar9c375812016-07-15 18:27:10 +0000747 ExtraVT, MinAlign(Alignment, IncrementSize), MMOFlags,
748 AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000749
750 // Build a factor node to remember that this load is independent of
751 // the other one.
752 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
753 Hi.getValue(1));
754
755 // Move the top bits to the right place.
Mehdi Amini9639d652015-07-09 02:09:20 +0000756 Hi = DAG.getNode(
757 ISD::SHL, dl, Hi.getValueType(), Hi,
758 DAG.getConstant(RoundWidth, dl,
759 TLI.getShiftAmountTy(Hi.getValueType(), DL)));
Nadav Rotemde6fd282012-07-11 08:52:09 +0000760
761 // Join the hi and lo parts.
Nadav Rotem2a148662012-07-11 11:02:16 +0000762 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000763 } else {
764 // Big endian - avoid unaligned loads.
765 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
766 // Load the top RoundWidth bits.
Nadav Rotem2a148662012-07-11 11:02:16 +0000767 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
Justin Lebar9c375812016-07-15 18:27:10 +0000768 LD->getPointerInfo(), RoundVT, Alignment, MMOFlags,
769 AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000770
771 // Load the remaining ExtraWidth bits.
772 IncrementSize = RoundWidth / 8;
Nadav Rotem2a148662012-07-11 11:02:16 +0000773 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000774 DAG.getConstant(IncrementSize, dl,
775 Ptr.getValueType()));
Justin Lebar9c375812016-07-15 18:27:10 +0000776 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +0000777 LD->getPointerInfo().getWithOffset(IncrementSize),
Justin Lebar9c375812016-07-15 18:27:10 +0000778 ExtraVT, MinAlign(Alignment, IncrementSize), MMOFlags,
779 AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000780
781 // Build a factor node to remember that this load is independent of
782 // the other one.
783 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
784 Hi.getValue(1));
785
786 // Move the top bits to the right place.
Mehdi Amini9639d652015-07-09 02:09:20 +0000787 Hi = DAG.getNode(
788 ISD::SHL, dl, Hi.getValueType(), Hi,
789 DAG.getConstant(ExtraWidth, dl,
790 TLI.getShiftAmountTy(Hi.getValueType(), DL)));
Nadav Rotemde6fd282012-07-11 08:52:09 +0000791
792 // Join the hi and lo parts.
Nadav Rotem2a148662012-07-11 11:02:16 +0000793 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000794 }
795
Nadav Rotem2a148662012-07-11 11:02:16 +0000796 Chain = Ch;
Nadav Rotemde6fd282012-07-11 08:52:09 +0000797 } else {
798 bool isCustom = false;
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000799 switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
800 SrcVT.getSimpleVT())) {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000801 default: llvm_unreachable("This action is not supported yet!");
802 case TargetLowering::Custom:
Matt Arsenault95b714c2014-03-11 00:01:25 +0000803 isCustom = true;
Justin Bognerb03fd122016-08-17 05:10:15 +0000804 LLVM_FALLTHROUGH;
Nadav Rotem2a148662012-07-11 11:02:16 +0000805 case TargetLowering::Legal: {
Matt Arsenault95b714c2014-03-11 00:01:25 +0000806 Value = SDValue(Node, 0);
807 Chain = SDValue(Node, 1);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000808
Matt Arsenault95b714c2014-03-11 00:01:25 +0000809 if (isCustom) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +0000810 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
Matt Arsenault95b714c2014-03-11 00:01:25 +0000811 Value = Res;
812 Chain = Res.getValue(1);
813 }
814 } else {
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000815 // If this is an unaligned load and the target doesn't support it,
816 // expand it.
Matt Arsenault95b714c2014-03-11 00:01:25 +0000817 EVT MemVT = LD->getMemoryVT();
818 unsigned AS = LD->getAddressSpace();
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000819 unsigned Align = LD->getAlignment();
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000820 const DataLayout &DL = DAG.getDataLayout();
Matt Arsenault7846d882016-04-21 18:19:11 +0000821 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) {
822 std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG);
823 }
Matt Arsenault95b714c2014-03-11 00:01:25 +0000824 }
825 break;
Nadav Rotem2a148662012-07-11 11:02:16 +0000826 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000827 case TargetLowering::Expand:
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000828 EVT DestVT = Node->getValueType(0);
829 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000830 // If the source type is not legal, see if there is a legal extload to
831 // an intermediate type that we can then extend further.
832 EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
833 if (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
834 TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT)) {
835 // If we are loading a legal type, this is a non-extload followed by a
836 // full extend.
837 ISD::LoadExtType MidExtType =
838 (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
839
840 SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
841 SrcVT, LD->getMemOperand());
842 unsigned ExtendOp =
843 ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType);
844 Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
845 Chain = Load.getValue(1);
Matt Arsenault95b714c2014-03-11 00:01:25 +0000846 break;
Matt Arsenault95b714c2014-03-11 00:01:25 +0000847 }
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000848
849 // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
850 // normal undefined upper bits behavior to allow using an in-reg extend
851 // with the illegal FP type, so load as an integer and do the
852 // from-integer conversion.
853 if (SrcVT.getScalarType() == MVT::f16) {
854 EVT ISrcVT = SrcVT.changeTypeToInteger();
855 EVT IDestVT = DestVT.changeTypeToInteger();
856 EVT LoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
857
858 SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, LoadVT,
859 Chain, Ptr, ISrcVT,
860 LD->getMemOperand());
861 Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result);
862 Chain = Result.getValue(1);
863 break;
864 }
Matt Arsenault95b714c2014-03-11 00:01:25 +0000865 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000866
Matt Arsenault95b714c2014-03-11 00:01:25 +0000867 assert(!SrcVT.isVector() &&
868 "Vector Loads are handled in LegalizeVectorOps");
Nadav Rotemde6fd282012-07-11 08:52:09 +0000869
Matt Arsenault95b714c2014-03-11 00:01:25 +0000870 // FIXME: This does not work for vectors on most targets. Sign-
871 // and zero-extend operations are currently folded into extending
872 // loads, whether they are legal or not, and then we end up here
873 // without any support for legalizing them.
874 assert(ExtType != ISD::EXTLOAD &&
875 "EXTLOAD should always be supported!");
876 // Turn the unsupported load into an EXTLOAD followed by an
877 // explicit zero/sign extend inreg.
878 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
879 Node->getValueType(0),
880 Chain, Ptr, SrcVT,
881 LD->getMemOperand());
882 SDValue ValRes;
883 if (ExtType == ISD::SEXTLOAD)
884 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
885 Result.getValueType(),
886 Result, DAG.getValueType(SrcVT));
887 else
Sanjay Patelb06441a2014-11-21 18:05:59 +0000888 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Matt Arsenault95b714c2014-03-11 00:01:25 +0000889 Value = ValRes;
890 Chain = Result.getValue(1);
891 break;
Nadav Rotemde6fd282012-07-11 08:52:09 +0000892 }
893 }
894
895 // Since loads produce two values, make sure to remember that we legalized
896 // both of them.
Nadav Rotem2a148662012-07-11 11:02:16 +0000897 if (Chain.getNode() != Node) {
898 assert(Value.getNode() != Node && "Load must be completely replaced");
899 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
900 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
Chandler Carruth411fb402014-07-26 05:49:40 +0000901 if (UpdatedNodes) {
902 UpdatedNodes->insert(Value.getNode());
903 UpdatedNodes->insert(Chain.getNode());
904 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000905 ReplacedNode(Node);
906 }
907}
908
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000909/// Return a legal replacement for the given operation, with all legal operands.
Dan Gohman198b7ff2011-11-03 21:49:52 +0000910void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
Chandler Carruthb1432742014-07-28 17:55:07 +0000911 DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
912
Dan Gohman198b7ff2011-11-03 21:49:52 +0000913 if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
914 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000915
Pete Cooperaf61ac72015-06-26 19:23:20 +0000916#ifndef NDEBUG
Eli Friedman5e0d1502009-05-24 02:46:31 +0000917 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chih-Hung Hsiehed7d81e2015-12-03 22:02:40 +0000918 assert((TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
919 TargetLowering::TypeLegal ||
920 TLI.isTypeLegal(Node->getValueType(i))) &&
Eli Friedman5e0d1502009-05-24 02:46:31 +0000921 "Unexpected illegal type!");
922
Pete Cooper8fc121d2015-06-26 19:08:33 +0000923 for (const SDValue &Op : Node->op_values())
Chih-Hung Hsiehed7d81e2015-12-03 22:02:40 +0000924 assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) ==
925 TargetLowering::TypeLegal ||
926 TLI.isTypeLegal(Op.getValueType()) ||
927 Op.getOpcode() == ISD::TargetConstant) &&
928 "Unexpected illegal type!");
Pete Cooperaf61ac72015-06-26 19:23:20 +0000929#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000930
Eli Friedman21d349b2009-05-27 01:25:56 +0000931 // Figure out the correct action; the way to query this varies by opcode
Bill Wendlingfb4ee9b2011-01-26 22:21:35 +0000932 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman21d349b2009-05-27 01:25:56 +0000933 bool SimpleFinishLegalizing = true;
Chris Lattnerdc750592005-01-07 07:47:09 +0000934 switch (Node->getOpcode()) {
Eli Friedman21d349b2009-05-27 01:25:56 +0000935 case ISD::INTRINSIC_W_CHAIN:
936 case ISD::INTRINSIC_WO_CHAIN:
937 case ISD::INTRINSIC_VOID:
Eli Friedman21d349b2009-05-27 01:25:56 +0000938 case ISD::STACKSAVE:
Owen Anderson9f944592009-08-11 20:47:22 +0000939 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman21d349b2009-05-27 01:25:56 +0000940 break;
Yury Gribovd7dbb662015-12-01 11:40:55 +0000941 case ISD::GET_DYNAMIC_AREA_OFFSET:
942 Action = TLI.getOperationAction(Node->getOpcode(),
943 Node->getValueType(0));
944 break;
Hal Finkel71c2ba32012-03-24 03:53:52 +0000945 case ISD::VAARG:
946 Action = TLI.getOperationAction(Node->getOpcode(),
947 Node->getValueType(0));
948 if (Action != TargetLowering::Promote)
949 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
950 break;
Tim Northoverfd7e4242014-07-17 10:51:23 +0000951 case ISD::FP_TO_FP16:
Eli Friedman21d349b2009-05-27 01:25:56 +0000952 case ISD::SINT_TO_FP:
953 case ISD::UINT_TO_FP:
954 case ISD::EXTRACT_VECTOR_ELT:
955 Action = TLI.getOperationAction(Node->getOpcode(),
956 Node->getOperand(0).getValueType());
957 break;
958 case ISD::FP_ROUND_INREG:
959 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000960 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman21d349b2009-05-27 01:25:56 +0000961 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
962 break;
963 }
Eli Friedman342e8df2011-08-24 20:50:09 +0000964 case ISD::ATOMIC_STORE: {
965 Action = TLI.getOperationAction(Node->getOpcode(),
966 Node->getOperand(2).getValueType());
967 break;
968 }
Eli Friedmane1bc3792009-05-28 03:06:16 +0000969 case ISD::SELECT_CC:
970 case ISD::SETCC:
971 case ISD::BR_CC: {
972 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
Hans Wennborgdcc25002015-11-19 16:35:08 +0000973 Node->getOpcode() == ISD::SETCC ? 2 :
974 Node->getOpcode() == ISD::SETCCE ? 3 : 1;
Eli Friedmane1bc3792009-05-28 03:06:16 +0000975 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Patrik Hagglunddeee9002012-12-19 10:09:26 +0000976 MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
Eli Friedmane1bc3792009-05-28 03:06:16 +0000977 ISD::CondCode CCCode =
978 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
979 Action = TLI.getCondCodeAction(CCCode, OpVT);
980 if (Action == TargetLowering::Legal) {
981 if (Node->getOpcode() == ISD::SELECT_CC)
982 Action = TLI.getOperationAction(Node->getOpcode(),
983 Node->getValueType(0));
984 else
985 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
986 }
987 break;
988 }
Eli Friedman21d349b2009-05-27 01:25:56 +0000989 case ISD::LOAD:
990 case ISD::STORE:
Eli Friedman5df72022009-05-28 03:56:57 +0000991 // FIXME: Model these properly. LOAD and STORE are complicated, and
992 // STORE expects the unlegalized operand in some cases.
993 SimpleFinishLegalizing = false;
994 break;
Eli Friedman21d349b2009-05-27 01:25:56 +0000995 case ISD::CALLSEQ_START:
996 case ISD::CALLSEQ_END:
Eli Friedman5df72022009-05-28 03:56:57 +0000997 // FIXME: This shouldn't be necessary. These nodes have special properties
998 // dealing with the recursive nature of legalization. Removing this
999 // special case should be done as part of making LegalizeDAG non-recursive.
1000 SimpleFinishLegalizing = false;
1001 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00001002 case ISD::EXTRACT_ELEMENT:
1003 case ISD::FLT_ROUNDS_:
Eli Friedman21d349b2009-05-27 01:25:56 +00001004 case ISD::FPOWI:
1005 case ISD::MERGE_VALUES:
1006 case ISD::EH_RETURN:
1007 case ISD::FRAME_TO_ARGS_OFFSET:
Hal Finkel5081ac22016-09-01 10:28:47 +00001008 case ISD::EH_DWARF_CFA:
Jim Grosbachdc0a0652010-07-06 23:44:52 +00001009 case ISD::EH_SJLJ_SETJMP:
1010 case ISD::EH_SJLJ_LONGJMP:
Matthias Braun3cd00c12015-07-16 22:34:16 +00001011 case ISD::EH_SJLJ_SETUP_DISPATCH:
Eli Friedmand6f28342009-05-27 03:33:44 +00001012 // These operations lie about being legal: when they claim to be legal,
1013 // they should actually be expanded.
Eli Friedman21d349b2009-05-27 01:25:56 +00001014 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1015 if (Action == TargetLowering::Legal)
1016 Action = TargetLowering::Expand;
1017 break;
Duncan Sandsa0984362011-09-06 13:37:06 +00001018 case ISD::INIT_TRAMPOLINE:
1019 case ISD::ADJUST_TRAMPOLINE:
Eli Friedman21d349b2009-05-27 01:25:56 +00001020 case ISD::FRAMEADDR:
1021 case ISD::RETURNADDR:
Eli Friedman2892d822009-05-27 12:20:41 +00001022 // These operations lie about being legal: when they claim to be legal,
1023 // they should actually be custom-lowered.
1024 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1025 if (Action == TargetLowering::Legal)
1026 Action = TargetLowering::Custom;
Eli Friedman21d349b2009-05-27 01:25:56 +00001027 break;
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00001028 case ISD::READCYCLECOUNTER:
1029 // READCYCLECOUNTER returns an i64, even if type legalization might have
1030 // expanded that to several smaller types.
1031 Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
1032 break;
Renato Golinc7aea402014-05-06 16:51:25 +00001033 case ISD::READ_REGISTER:
1034 case ISD::WRITE_REGISTER:
1035 // Named register is legal in the DAG, but blocked by register name
1036 // selection if not implemented by target (to chose the correct register)
1037 // They'll be converted to Copy(To/From)Reg.
1038 Action = TargetLowering::Legal;
1039 break;
Shuxin Yangcdde0592012-10-19 20:11:16 +00001040 case ISD::DEBUGTRAP:
1041 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1042 if (Action == TargetLowering::Expand) {
1043 // replace ISD::DEBUGTRAP with ISD::TRAP
1044 SDValue NewVal;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001045 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
Shuxin Yang1479fcd2012-10-19 23:00:20 +00001046 Node->getOperand(0));
Shuxin Yangcdde0592012-10-19 20:11:16 +00001047 ReplaceNode(Node, NewVal.getNode());
1048 LegalizeOp(NewVal.getNode());
1049 return;
1050 }
1051 break;
1052
Chris Lattnerdc750592005-01-07 07:47:09 +00001053 default:
Chris Lattner3eb86932005-05-14 06:34:48 +00001054 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman21d349b2009-05-27 01:25:56 +00001055 Action = TargetLowering::Legal;
1056 } else {
1057 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattner3eb86932005-05-14 06:34:48 +00001058 }
Eli Friedman21d349b2009-05-27 01:25:56 +00001059 break;
1060 }
1061
1062 if (SimpleFinishLegalizing) {
Peter Collingbourne8eb05fd2012-05-20 18:36:15 +00001063 SDNode *NewNode = Node;
Eli Friedman21d349b2009-05-27 01:25:56 +00001064 switch (Node->getOpcode()) {
1065 default: break;
Eli Friedman21d349b2009-05-27 01:25:56 +00001066 case ISD::SHL:
1067 case ISD::SRL:
1068 case ISD::SRA:
1069 case ISD::ROTL:
Elena Demikhovskydcc86d52016-09-07 20:54:33 +00001070 case ISD::ROTR: {
Eli Friedman21d349b2009-05-27 01:25:56 +00001071 // Legalizing shifts/rotates requires adjusting the shift amount
1072 // to the appropriate width.
Elena Demikhovskydcc86d52016-09-07 20:54:33 +00001073 SDValue Op0 = Node->getOperand(0);
1074 SDValue Op1 = Node->getOperand(1);
1075 if (!Op1.getValueType().isVector()) {
1076 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1);
1077 // The getShiftAmountOperand() may create a new operand node or
1078 // return the existing one. If new operand is created we need
1079 // to update the parent node.
1080 // Do not try to legalize SAO here! It will be automatically legalized
1081 // in the next round.
1082 if (SAO != Op1)
1083 NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO);
Dan Gohman198b7ff2011-11-03 21:49:52 +00001084 }
Elena Demikhovskydcc86d52016-09-07 20:54:33 +00001085 }
1086 break;
Dan Gohman4906f732009-08-18 23:36:17 +00001087 case ISD::SRL_PARTS:
1088 case ISD::SRA_PARTS:
Elena Demikhovskydcc86d52016-09-07 20:54:33 +00001089 case ISD::SHL_PARTS: {
Dan Gohman4906f732009-08-18 23:36:17 +00001090 // Legalizing shifts/rotates requires adjusting the shift amount
1091 // to the appropriate width.
Elena Demikhovskydcc86d52016-09-07 20:54:33 +00001092 SDValue Op0 = Node->getOperand(0);
1093 SDValue Op1 = Node->getOperand(1);
1094 SDValue Op2 = Node->getOperand(2);
1095 if (!Op2.getValueType().isVector()) {
1096 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2);
1097 // The getShiftAmountOperand() may create a new operand node or
1098 // return the existing one. If new operand is created we need
1099 // to update the parent node.
1100 if (SAO != Op2)
1101 NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO);
Dan Gohman198b7ff2011-11-03 21:49:52 +00001102 }
Elena Demikhovskydcc86d52016-09-07 20:54:33 +00001103 }
1104 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00001105 }
1106
Dan Gohman198b7ff2011-11-03 21:49:52 +00001107 if (NewNode != Node) {
Chandler Carruth411fb402014-07-26 05:49:40 +00001108 ReplaceNode(Node, NewNode);
Dan Gohman198b7ff2011-11-03 21:49:52 +00001109 Node = NewNode;
1110 }
Eli Friedman21d349b2009-05-27 01:25:56 +00001111 switch (Action) {
1112 case TargetLowering::Legal:
Dan Gohman198b7ff2011-11-03 21:49:52 +00001113 return;
Nadav Rotem2a148662012-07-11 11:02:16 +00001114 case TargetLowering::Custom: {
Eli Friedman21d349b2009-05-27 01:25:56 +00001115 // FIXME: The handling for custom lowering with multiple results is
1116 // a complete mess.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001117 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
Chandler Carruth98655fa2014-07-26 05:52:51 +00001118 if (!(Res.getNode() != Node || Res.getResNo() != 0))
1119 return;
1120
1121 if (Node->getNumValues() == 1) {
1122 // We can just directly replace this node with the lowered value.
1123 ReplaceNode(SDValue(Node, 0), Res);
1124 return;
Eli Friedman21d349b2009-05-27 01:25:56 +00001125 }
Chandler Carruth98655fa2014-07-26 05:52:51 +00001126
1127 SmallVector<SDValue, 8> ResultVals;
1128 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1129 ResultVals.push_back(Res.getValue(i));
1130 ReplaceNode(Node, ResultVals.data());
Dan Gohman198b7ff2011-11-03 21:49:52 +00001131 return;
Eli Friedman21d349b2009-05-27 01:25:56 +00001132 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001133 LLVM_FALLTHROUGH;
Nadav Rotem2a148662012-07-11 11:02:16 +00001134 }
Eli Friedman21d349b2009-05-27 01:25:56 +00001135 case TargetLowering::Expand:
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00001136 if (ExpandNode(Node))
1137 return;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001138 LLVM_FALLTHROUGH;
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00001139 case TargetLowering::LibCall:
1140 ConvertNodeToLibcall(Node);
Dan Gohman198b7ff2011-11-03 21:49:52 +00001141 return;
Eli Friedman21d349b2009-05-27 01:25:56 +00001142 case TargetLowering::Promote:
Dan Gohman198b7ff2011-11-03 21:49:52 +00001143 PromoteNode(Node);
1144 return;
Eli Friedman21d349b2009-05-27 01:25:56 +00001145 }
1146 }
1147
1148 switch (Node->getOpcode()) {
1149 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00001150#ifndef NDEBUG
David Greeneae4f2662010-01-05 01:24:53 +00001151 dbgs() << "NODE: ";
1152 Node->dump( &DAG);
1153 dbgs() << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00001154#endif
Craig Topperee4dab52012-02-05 08:31:47 +00001155 llvm_unreachable("Do not know how to legalize this operator!");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001156
Dan Gohman198b7ff2011-11-03 21:49:52 +00001157 case ISD::CALLSEQ_START:
Dan Gohman9b9c9702011-10-29 00:41:52 +00001158 case ISD::CALLSEQ_END:
Dan Gohman198b7ff2011-11-03 21:49:52 +00001159 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001160 case ISD::LOAD: {
Nadav Rotemde6fd282012-07-11 08:52:09 +00001161 return LegalizeLoadOps(Node);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001162 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001163 case ISD::STORE: {
Nadav Rotemde6fd282012-07-11 08:52:09 +00001164 return LegalizeStoreOps(Node);
Evan Cheng31d15fa2005-12-23 07:29:34 +00001165 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001166 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001167}
1168
Eli Friedman40afdb62009-05-23 22:37:25 +00001169SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1170 SDValue Vec = Op.getOperand(0);
1171 SDValue Idx = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001172 SDLoc dl(Op);
Hal Finkel90adf0f2014-03-30 15:10:18 +00001173
1174 // Before we generate a new store to a temporary stack slot, see if there is
1175 // already one that we can use. There often is because when we scalarize
1176 // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
1177 // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
1178 // the vector. If all are expanded here, we don't want one store per vector
1179 // element.
Robert Lougherf0033b22015-12-09 14:34:10 +00001180
1181 // Caches for hasPredecessorHelper
1182 SmallPtrSet<const SDNode *, 32> Visited;
1183 SmallVector<const SDNode *, 16> Worklist;
Nirav Davefa250ca2016-03-25 21:06:30 +00001184 Worklist.push_back(Idx.getNode());
Hal Finkel90adf0f2014-03-30 15:10:18 +00001185 SDValue StackPtr, Ch;
1186 for (SDNode::use_iterator UI = Vec.getNode()->use_begin(),
1187 UE = Vec.getNode()->use_end(); UI != UE; ++UI) {
1188 SDNode *User = *UI;
1189 if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
1190 if (ST->isIndexed() || ST->isTruncatingStore() ||
1191 ST->getValue() != Vec)
1192 continue;
1193
1194 // Make sure that nothing else could have stored into the destination of
1195 // this store.
1196 if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
1197 continue;
1198
Robert Lougherf0033b22015-12-09 14:34:10 +00001199 // If the index is dependent on the store we will introduce a cycle when
1200 // creating the load (the load uses the index, and by replacing the chain
1201 // we will make the index dependent on the load).
Nirav Davefa250ca2016-03-25 21:06:30 +00001202 if (SDNode::hasPredecessorHelper(ST, Visited, Worklist))
Robert Lougherf0033b22015-12-09 14:34:10 +00001203 continue;
1204
Hal Finkel90adf0f2014-03-30 15:10:18 +00001205 StackPtr = ST->getBasePtr();
1206 Ch = SDValue(ST, 0);
1207 break;
1208 }
1209 }
1210
1211 if (!Ch.getNode()) {
1212 // Store the value to a temporary stack slot, then LOAD the returned part.
1213 StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1214 Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00001215 MachinePointerInfo());
Hal Finkel90adf0f2014-03-30 15:10:18 +00001216 }
Eli Friedman40afdb62009-05-23 22:37:25 +00001217
1218 // Add the offset to the index.
Dan Gohman9b80f862010-02-25 15:20:39 +00001219 unsigned EltSize =
1220 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman40afdb62009-05-23 22:37:25 +00001221 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001222 DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType()));
Eli Friedman40afdb62009-05-23 22:37:25 +00001223
Mehdi Amini44ede332015-07-09 02:09:04 +00001224 Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout()));
Eli Friedman40afdb62009-05-23 22:37:25 +00001225 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1226
Ahmed Bougachac8097612015-03-09 22:51:05 +00001227 SDValue NewLoad;
1228
Eli Friedman2b77eef2009-07-09 22:01:03 +00001229 if (Op.getValueType().isVector())
Justin Lebar9c375812016-07-15 18:27:10 +00001230 NewLoad =
1231 DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, MachinePointerInfo());
Ahmed Bougachac8097612015-03-09 22:51:05 +00001232 else
Justin Lebar9c375812016-07-15 18:27:10 +00001233 NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
1234 MachinePointerInfo(),
1235 Vec.getValueType().getVectorElementType());
Ahmed Bougachac8097612015-03-09 22:51:05 +00001236
1237 // Replace the chain going out of the store, by the one out of the load.
1238 DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
1239
1240 // We introduced a cycle though, so update the loads operands, making sure
1241 // to use the original store's chain as an incoming chain.
1242 SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(),
1243 NewLoad->op_end());
1244 NewLoadOperands[0] = Ch;
1245 NewLoad =
1246 SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
1247 return NewLoad;
Eli Friedman40afdb62009-05-23 22:37:25 +00001248}
1249
David Greenebab5e6e2011-01-26 19:13:22 +00001250SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1251 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1252
1253 SDValue Vec = Op.getOperand(0);
1254 SDValue Part = Op.getOperand(1);
1255 SDValue Idx = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001256 SDLoc dl(Op);
David Greenebab5e6e2011-01-26 19:13:22 +00001257
1258 // Store the value to a temporary stack slot, then LOAD the returned part.
1259
1260 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1261 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001262 MachinePointerInfo PtrInfo =
1263 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
David Greenebab5e6e2011-01-26 19:13:22 +00001264
1265 // First store the whole vector.
Justin Lebar9c375812016-07-15 18:27:10 +00001266 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo);
David Greenebab5e6e2011-01-26 19:13:22 +00001267
1268 // Then store the inserted part.
1269
1270 // Add the offset to the index.
1271 unsigned EltSize =
1272 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1273
1274 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001275 DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType()));
Mehdi Amini44ede332015-07-09 02:09:04 +00001276 Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout()));
David Greenebab5e6e2011-01-26 19:13:22 +00001277
1278 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1279 StackPtr);
1280
1281 // Store the subvector.
Justin Lebar9c375812016-07-15 18:27:10 +00001282 Ch = DAG.getStore(Ch, dl, Part, SubStackPtr, MachinePointerInfo());
David Greenebab5e6e2011-01-26 19:13:22 +00001283
1284 // Finally, load the updated vector.
Justin Lebar9c375812016-07-15 18:27:10 +00001285 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo);
David Greenebab5e6e2011-01-26 19:13:22 +00001286}
1287
Eli Friedmanaee3f622009-06-06 07:04:42 +00001288SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1289 // We can't handle this case efficiently. Allocate a sufficiently
1290 // aligned object on the stack, store each element into it, then load
1291 // the result as a vector.
1292 // Create the stack frame object.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001293 EVT VT = Node->getValueType(0);
Dale Johannesenb91eba32009-11-21 00:53:23 +00001294 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001295 SDLoc dl(Node);
Eli Friedmanaee3f622009-06-06 07:04:42 +00001296 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001297 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001298 MachinePointerInfo PtrInfo =
1299 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
Eli Friedmanaee3f622009-06-06 07:04:42 +00001300
1301 // Emit a store of each element to the stack slot.
1302 SmallVector<SDValue, 8> Stores;
Dan Gohman9b80f862010-02-25 15:20:39 +00001303 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedmanaee3f622009-06-06 07:04:42 +00001304 // Store (in the right endianness) the elements to memory.
1305 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1306 // Ignore undef elements.
Sanjay Patel57195842016-03-14 17:28:46 +00001307 if (Node->getOperand(i).isUndef()) continue;
Eli Friedmanaee3f622009-06-06 07:04:42 +00001308
1309 unsigned Offset = TypeByteSize*i;
1310
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001311 SDValue Idx = DAG.getConstant(Offset, dl, FIPtr.getValueType());
Eli Friedmanaee3f622009-06-06 07:04:42 +00001312 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1313
Dan Gohman2a8e3772010-02-25 20:30:49 +00001314 // If the destination vector element type is narrower than the source
1315 // element type, only store the bits necessary.
1316 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesenb91eba32009-11-21 00:53:23 +00001317 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattner1ffcf522010-09-21 16:36:31 +00001318 Node->getOperand(i), Idx,
Justin Lebar9c375812016-07-15 18:27:10 +00001319 PtrInfo.getWithOffset(Offset), EltVT));
Mon P Wang586d9972010-01-24 00:05:03 +00001320 } else
Justin Lebar9c375812016-07-15 18:27:10 +00001321 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
1322 Idx, PtrInfo.getWithOffset(Offset)));
Eli Friedmanaee3f622009-06-06 07:04:42 +00001323 }
1324
1325 SDValue StoreChain;
1326 if (!Stores.empty()) // Not all undef elements?
Craig Topper48d114b2014-04-26 18:35:24 +00001327 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
Eli Friedmanaee3f622009-06-06 07:04:42 +00001328 else
1329 StoreChain = DAG.getEntryNode();
1330
1331 // Result is a load from the stack slot.
Justin Lebar9c375812016-07-15 18:27:10 +00001332 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo);
Eli Friedmanaee3f622009-06-06 07:04:42 +00001333}
1334
Matthias Braunb9610a62015-11-12 01:02:47 +00001335namespace {
1336/// Keeps track of state when getting the sign of a floating-point value as an
1337/// integer.
1338struct FloatSignAsInt {
1339 EVT FloatVT;
1340 SDValue Chain;
1341 SDValue FloatPtr;
1342 SDValue IntPtr;
1343 MachinePointerInfo IntPointerInfo;
1344 MachinePointerInfo FloatPointerInfo;
1345 SDValue IntValue;
1346 APInt SignMask;
Matthias Braun848e79c2016-02-19 04:44:19 +00001347 uint8_t SignBit;
Matthias Braunb9610a62015-11-12 01:02:47 +00001348};
1349}
Duncan Sands4c55f762010-03-12 11:45:06 +00001350
Matthias Braunb9610a62015-11-12 01:02:47 +00001351/// Bitcast a floating-point value to an integer value. Only bitcast the part
1352/// containing the sign bit if the target has no integer value capable of
1353/// holding all bits of the floating-point value.
1354void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001355 const SDLoc &DL,
1356 SDValue Value) const {
Matthias Braunb9610a62015-11-12 01:02:47 +00001357 EVT FloatVT = Value.getValueType();
1358 unsigned NumBits = FloatVT.getSizeInBits();
1359 State.FloatVT = FloatVT;
1360 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
1361 // Convert to an integer of the same size.
Dan Gohmane49e7422011-07-15 22:39:09 +00001362 if (TLI.isTypeLegal(IVT)) {
Matthias Braunb9610a62015-11-12 01:02:47 +00001363 State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
1364 State.SignMask = APInt::getSignBit(NumBits);
Matthias Braun848e79c2016-02-19 04:44:19 +00001365 State.SignBit = NumBits - 1;
Matthias Braunb9610a62015-11-12 01:02:47 +00001366 return;
Eli Friedman2892d822009-05-27 12:20:41 +00001367 }
Matthias Braunb9610a62015-11-12 01:02:47 +00001368
1369 auto &DataLayout = DAG.getDataLayout();
1370 // Store the float to memory, then load the sign part out as an integer.
1371 MVT LoadTy = TLI.getRegisterType(*DAG.getContext(), MVT::i8);
1372 // First create a temporary that is aligned for both the load and store.
1373 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1374 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1375 // Then store the float to it.
1376 State.FloatPtr = StackPtr;
1377 MachineFunction &MF = DAG.getMachineFunction();
1378 State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
1379 State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00001380 State.FloatPointerInfo);
Matthias Braunb9610a62015-11-12 01:02:47 +00001381
1382 SDValue IntPtr;
1383 if (DataLayout.isBigEndian()) {
1384 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1385 // Load out a legal integer with the same sign bit as the float.
1386 IntPtr = StackPtr;
1387 State.IntPointerInfo = State.FloatPointerInfo;
1388 } else {
1389 // Advance the pointer so that the loaded byte will contain the sign bit.
1390 unsigned ByteOffset = (FloatVT.getSizeInBits() / 8) - 1;
1391 IntPtr = DAG.getNode(ISD::ADD, DL, StackPtr.getValueType(), StackPtr,
1392 DAG.getConstant(ByteOffset, DL, StackPtr.getValueType()));
1393 State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
1394 ByteOffset);
1395 }
1396
1397 State.IntPtr = IntPtr;
Justin Lebar9c375812016-07-15 18:27:10 +00001398 State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr,
1399 State.IntPointerInfo, MVT::i8);
Matthias Braunb9610a62015-11-12 01:02:47 +00001400 State.SignMask = APInt::getOneBitSet(LoadTy.getSizeInBits(), 7);
Matthias Braun848e79c2016-02-19 04:44:19 +00001401 State.SignBit = 7;
Matthias Braunb9610a62015-11-12 01:02:47 +00001402}
1403
1404/// Replace the integer value produced by getSignAsIntValue() with a new value
1405/// and cast the result back to a floating-point type.
1406SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001407 const SDLoc &DL,
1408 SDValue NewIntValue) const {
Matthias Braunb9610a62015-11-12 01:02:47 +00001409 if (!State.Chain)
1410 return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
1411
1412 // Override the part containing the sign bit in the value stored on the stack.
1413 SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00001414 State.IntPointerInfo, MVT::i8);
Matthias Braunb9610a62015-11-12 01:02:47 +00001415 return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00001416 State.FloatPointerInfo);
Matthias Braunb9610a62015-11-12 01:02:47 +00001417}
1418
1419SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
1420 SDLoc DL(Node);
1421 SDValue Mag = Node->getOperand(0);
1422 SDValue Sign = Node->getOperand(1);
1423
1424 // Get sign bit into an integer value.
1425 FloatSignAsInt SignAsInt;
1426 getSignAsIntValue(SignAsInt, DL, Sign);
1427
1428 EVT IntVT = SignAsInt.IntValue.getValueType();
1429 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1430 SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
1431 SignMask);
1432
1433 // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X)
1434 EVT FloatVT = Mag.getValueType();
1435 if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
1436 TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
1437 SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
1438 SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
1439 SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
1440 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
1441 return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
1442 }
1443
Matthias Braun848e79c2016-02-19 04:44:19 +00001444 // Transform Mag value to integer, and clear the sign bit.
Matthias Braunb9610a62015-11-12 01:02:47 +00001445 FloatSignAsInt MagAsInt;
1446 getSignAsIntValue(MagAsInt, DL, Mag);
Matthias Braun848e79c2016-02-19 04:44:19 +00001447 EVT MagVT = MagAsInt.IntValue.getValueType();
1448 SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
1449 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
Matthias Braunb9610a62015-11-12 01:02:47 +00001450 ClearSignMask);
Matthias Braunb9610a62015-11-12 01:02:47 +00001451
Matthias Braun848e79c2016-02-19 04:44:19 +00001452 // Get the signbit at the right position for MagAsInt.
1453 int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
1454 if (SignBit.getValueSizeInBits() > ClearedSign.getValueSizeInBits()) {
1455 if (ShiftAmount > 0) {
1456 SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, IntVT);
1457 SignBit = DAG.getNode(ISD::SRL, DL, IntVT, SignBit, ShiftCnst);
1458 } else if (ShiftAmount < 0) {
1459 SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, IntVT);
1460 SignBit = DAG.getNode(ISD::SHL, DL, IntVT, SignBit, ShiftCnst);
1461 }
1462 SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
1463 } else if (SignBit.getValueSizeInBits() < ClearedSign.getValueSizeInBits()) {
1464 SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
1465 if (ShiftAmount > 0) {
1466 SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, MagVT);
1467 SignBit = DAG.getNode(ISD::SRL, DL, MagVT, SignBit, ShiftCnst);
1468 } else if (ShiftAmount < 0) {
1469 SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, MagVT);
1470 SignBit = DAG.getNode(ISD::SHL, DL, MagVT, SignBit, ShiftCnst);
1471 }
1472 }
1473
1474 // Store the part with the modified sign and convert back to float.
1475 SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit);
Matthias Braunb9610a62015-11-12 01:02:47 +00001476 return modifySignAsInt(MagAsInt, DL, CopiedSign);
1477}
1478
1479SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
1480 SDLoc DL(Node);
1481 SDValue Value = Node->getOperand(0);
1482
1483 // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
1484 EVT FloatVT = Value.getValueType();
1485 if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
1486 SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
1487 return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
1488 }
1489
1490 // Transform value to integer, clear the sign bit and transform back.
1491 FloatSignAsInt ValueAsInt;
1492 getSignAsIntValue(ValueAsInt, DL, Value);
1493 EVT IntVT = ValueAsInt.IntValue.getValueType();
1494 SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
1495 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
1496 ClearSignMask);
1497 return modifySignAsInt(ValueAsInt, DL, ClearedSign);
Eli Friedman2892d822009-05-27 12:20:41 +00001498}
1499
Eli Friedman2892d822009-05-27 12:20:41 +00001500void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1501 SmallVectorImpl<SDValue> &Results) {
1502 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1503 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1504 " not tell us which reg is the stack pointer!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00001505 SDLoc dl(Node);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001506 EVT VT = Node->getValueType(0);
Eli Friedman2892d822009-05-27 12:20:41 +00001507 SDValue Tmp1 = SDValue(Node, 0);
1508 SDValue Tmp2 = SDValue(Node, 1);
1509 SDValue Tmp3 = Node->getOperand(2);
1510 SDValue Chain = Tmp1.getOperand(0);
1511
1512 // Chain the dynamic stack allocation so that it doesn't modify the stack
1513 // pointer when other instructions are using the stack.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001514 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, dl, true), dl);
Eli Friedman2892d822009-05-27 12:20:41 +00001515
1516 SDValue Size = Tmp2.getOperand(1);
1517 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1518 Chain = SP.getValue(1);
1519 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Eric Christopherd9134482014-08-04 21:25:23 +00001520 unsigned StackAlign =
Eric Christopher85de8f92014-10-09 01:35:27 +00001521 DAG.getSubtarget().getFrameLowering()->getStackAlignment();
Eli Friedman2892d822009-05-27 12:20:41 +00001522 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Elena Demikhovsky82a46eb2013-10-14 07:26:51 +00001523 if (Align > StackAlign)
1524 Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001525 DAG.getConstant(-(uint64_t)Align, dl, VT));
Eli Friedman2892d822009-05-27 12:20:41 +00001526 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1527
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001528 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true),
1529 DAG.getIntPtrConstant(0, dl, true), SDValue(), dl);
Eli Friedman2892d822009-05-27 12:20:41 +00001530
1531 Results.push_back(Tmp1);
1532 Results.push_back(Tmp2);
1533}
1534
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00001535/// Legalize a SETCC with given LHS and RHS and condition code CC on the current
1536/// target.
Daniel Sandersedc071b2013-11-21 13:24:49 +00001537///
Tom Stellard08690a12013-09-28 02:50:32 +00001538/// If the SETCC has been legalized using AND / OR, then the legalized node
Daniel Sandersedc071b2013-11-21 13:24:49 +00001539/// will be stored in LHS. RHS and CC will be set to SDValue(). NeedInvert
1540/// will be set to false.
1541///
Tom Stellard08690a12013-09-28 02:50:32 +00001542/// If the SETCC has been legalized by using getSetCCSwappedOperands(),
Daniel Sandersedc071b2013-11-21 13:24:49 +00001543/// then the values of LHS and RHS will be swapped, CC will be set to the
1544/// new condition, and NeedInvert will be set to false.
1545///
1546/// If the SETCC has been legalized using the inverse condcode, then LHS and
1547/// RHS will be unchanged, CC will set to the inverted condcode, and NeedInvert
1548/// will be set to true. The caller must invert the result of the SETCC with
Pete Cooper7fd1d722014-05-12 23:26:58 +00001549/// SelectionDAG::getLogicalNOT() or take equivalent action to swap the effect
1550/// of a true/false result.
Daniel Sandersedc071b2013-11-21 13:24:49 +00001551///
Tom Stellard08690a12013-09-28 02:50:32 +00001552/// \returns true if the SetCC has been legalized, false if it hasn't.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001553bool SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT, SDValue &LHS,
1554 SDValue &RHS, SDValue &CC,
Daniel Sandersedc071b2013-11-21 13:24:49 +00001555 bool &NeedInvert,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001556 const SDLoc &dl) {
Patrik Hagglunddeee9002012-12-19 10:09:26 +00001557 MVT OpVT = LHS.getSimpleValueType();
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001558 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
Daniel Sandersedc071b2013-11-21 13:24:49 +00001559 NeedInvert = false;
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001560 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Craig Topperee4dab52012-02-05 08:31:47 +00001561 default: llvm_unreachable("Unknown condition code action!");
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001562 case TargetLowering::Legal:
1563 // Nothing to do.
1564 break;
1565 case TargetLowering::Expand: {
Tom Stellardcd428182013-09-28 02:50:38 +00001566 ISD::CondCode InvCC = ISD::getSetCCSwappedOperands(CCCode);
1567 if (TLI.isCondCodeLegal(InvCC, OpVT)) {
1568 std::swap(LHS, RHS);
1569 CC = DAG.getCondCode(InvCC);
1570 return true;
1571 }
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001572 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1573 unsigned Opc = 0;
1574 switch (CCCode) {
Craig Topperee4dab52012-02-05 08:31:47 +00001575 default: llvm_unreachable("Don't know how to expand this condition!");
Stephen Lincfe7f352013-07-08 00:37:03 +00001576 case ISD::SETO:
Micah Villmow0242b9b2012-10-10 20:50:51 +00001577 assert(TLI.getCondCodeAction(ISD::SETOEQ, OpVT)
1578 == TargetLowering::Legal
1579 && "If SETO is expanded, SETOEQ must be legal!");
1580 CC1 = ISD::SETOEQ; CC2 = ISD::SETOEQ; Opc = ISD::AND; break;
Stephen Lincfe7f352013-07-08 00:37:03 +00001581 case ISD::SETUO:
Micah Villmow0242b9b2012-10-10 20:50:51 +00001582 assert(TLI.getCondCodeAction(ISD::SETUNE, OpVT)
1583 == TargetLowering::Legal
1584 && "If SETUO is expanded, SETUNE must be legal!");
1585 CC1 = ISD::SETUNE; CC2 = ISD::SETUNE; Opc = ISD::OR; break;
1586 case ISD::SETOEQ:
1587 case ISD::SETOGT:
1588 case ISD::SETOGE:
1589 case ISD::SETOLT:
1590 case ISD::SETOLE:
Stephen Lincfe7f352013-07-08 00:37:03 +00001591 case ISD::SETONE:
1592 case ISD::SETUEQ:
1593 case ISD::SETUNE:
1594 case ISD::SETUGT:
1595 case ISD::SETUGE:
1596 case ISD::SETULT:
Micah Villmow0242b9b2012-10-10 20:50:51 +00001597 case ISD::SETULE:
1598 // If we are floating point, assign and break, otherwise fall through.
1599 if (!OpVT.isInteger()) {
1600 // We can use the 4th bit to tell if we are the unordered
1601 // or ordered version of the opcode.
1602 CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO;
1603 Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND;
1604 CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10);
1605 break;
1606 }
1607 // Fallthrough if we are unsigned integer.
Justin Bognerb03fd122016-08-17 05:10:15 +00001608 LLVM_FALLTHROUGH;
Micah Villmow0242b9b2012-10-10 20:50:51 +00001609 case ISD::SETLE:
1610 case ISD::SETGT:
1611 case ISD::SETGE:
1612 case ISD::SETLT:
Tom Stellardcd428182013-09-28 02:50:38 +00001613 // We only support using the inverted operation, which is computed above
1614 // and not a different manner of supporting expanding these cases.
1615 llvm_unreachable("Don't know how to expand this condition!");
Daniel Sandersedc071b2013-11-21 13:24:49 +00001616 case ISD::SETNE:
1617 case ISD::SETEQ:
1618 // Try inverting the result of the inverse condition.
1619 InvCC = CCCode == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ;
1620 if (TLI.isCondCodeLegal(InvCC, OpVT)) {
1621 CC = DAG.getCondCode(InvCC);
1622 NeedInvert = true;
1623 return true;
1624 }
1625 // If inverting the condition didn't work then we have no means to expand
1626 // the condition.
1627 llvm_unreachable("Don't know how to expand this condition!");
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001628 }
Stephen Lincfe7f352013-07-08 00:37:03 +00001629
Micah Villmow0242b9b2012-10-10 20:50:51 +00001630 SDValue SetCC1, SetCC2;
1631 if (CCCode != ISD::SETO && CCCode != ISD::SETUO) {
1632 // If we aren't the ordered or unorder operation,
1633 // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS).
1634 SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1635 SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1636 } else {
1637 // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS)
1638 SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1);
1639 SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2);
1640 }
Dale Johannesenad00f6e2009-02-02 20:41:04 +00001641 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001642 RHS = SDValue();
1643 CC = SDValue();
Tom Stellard08690a12013-09-28 02:50:32 +00001644 return true;
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001645 }
1646 }
Tom Stellard08690a12013-09-28 02:50:32 +00001647 return false;
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001648}
1649
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00001650/// Emit a store/load combination to the stack. This stores
Chris Lattner87bc3e72008-01-16 07:45:30 +00001651/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1652/// a load from the stack slot to DestVT, extending it if needed.
1653/// The resultant code need not be legal.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001654SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
1655 EVT DestVT, const SDLoc &dl) {
Chris Lattner36e663d2005-12-23 00:16:34 +00001656 // Create the stack frame object.
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00001657 unsigned SrcAlign = DAG.getDataLayout().getPrefTypeAlignment(
1658 SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001659 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001660
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001661 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1662 int SPFI = StackPtrFI->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001663 MachinePointerInfo PtrInfo =
1664 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001665
Duncan Sands13237ac2008-06-06 12:08:01 +00001666 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1667 unsigned SlotSize = SlotVT.getSizeInBits();
1668 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattner229907c2011-07-18 04:54:35 +00001669 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00001670 unsigned DestAlign = DAG.getDataLayout().getPrefTypeAlignment(DestType);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001671
Chris Lattner87bc3e72008-01-16 07:45:30 +00001672 // Emit a store to the stack slot. Use a truncstore if the input value is
1673 // later than DestVT.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001674 SDValue Store;
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001675
Chris Lattner87bc3e72008-01-16 07:45:30 +00001676 if (SrcSize > SlotSize)
Justin Lebar9c375812016-07-15 18:27:10 +00001677 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, PtrInfo,
1678 SlotVT, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00001679 else {
1680 assert(SrcSize == SlotSize && "Invalid store");
Justin Lebar9c375812016-07-15 18:27:10 +00001681 Store =
1682 DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, PtrInfo, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00001683 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001684
Chris Lattner36e663d2005-12-23 00:16:34 +00001685 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00001686 if (SlotSize == DestSize)
Justin Lebar9c375812016-07-15 18:27:10 +00001687 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001688
Chris Lattner87bc3e72008-01-16 07:45:30 +00001689 assert(SlotSize < DestSize && "Unknown extension!");
Justin Lebar9c375812016-07-15 18:27:10 +00001690 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT,
1691 DestAlign);
Chris Lattner36e663d2005-12-23 00:16:34 +00001692}
1693
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001694SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001695 SDLoc dl(Node);
Chris Lattner6be79822006-04-04 17:23:26 +00001696 // Create a vector sized/aligned stack slot, store the value to element #0,
1697 // then load the whole vector back out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001698 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman2d489b52008-02-06 22:27:42 +00001699
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001700 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1701 int SPFI = StackPtrFI->getIndex();
1702
Alex Lorenze40c8a22015-08-11 23:09:45 +00001703 SDValue Ch = DAG.getTruncStore(
1704 DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
1705 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
Justin Lebar9c375812016-07-15 18:27:10 +00001706 Node->getValueType(0).getVectorElementType());
Alex Lorenze40c8a22015-08-11 23:09:45 +00001707 return DAG.getLoad(
1708 Node->getValueType(0), dl, Ch, StackPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00001709 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
Chris Lattner6be79822006-04-04 17:23:26 +00001710}
1711
Hal Finkelb811b6d2014-03-31 19:42:55 +00001712static bool
1713ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG,
1714 const TargetLowering &TLI, SDValue &Res) {
1715 unsigned NumElems = Node->getNumOperands();
1716 SDLoc dl(Node);
1717 EVT VT = Node->getValueType(0);
1718
1719 // Try to group the scalars into pairs, shuffle the pairs together, then
1720 // shuffle the pairs of pairs together, etc. until the vector has
1721 // been built. This will work only if all of the necessary shuffle masks
1722 // are legal.
1723
1724 // We do this in two phases; first to check the legality of the shuffles,
1725 // and next, assuming that all shuffles are legal, to create the new nodes.
1726 for (int Phase = 0; Phase < 2; ++Phase) {
1727 SmallVector<std::pair<SDValue, SmallVector<int, 16> >, 16> IntermedVals,
1728 NewIntermedVals;
1729 for (unsigned i = 0; i < NumElems; ++i) {
1730 SDValue V = Node->getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00001731 if (V.isUndef())
Hal Finkelb811b6d2014-03-31 19:42:55 +00001732 continue;
1733
1734 SDValue Vec;
1735 if (Phase)
1736 Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
1737 IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
1738 }
1739
1740 while (IntermedVals.size() > 2) {
1741 NewIntermedVals.clear();
1742 for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
1743 // This vector and the next vector are shuffled together (simply to
1744 // append the one to the other).
1745 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1746
1747 SmallVector<int, 16> FinalIndices;
1748 FinalIndices.reserve(IntermedVals[i].second.size() +
1749 IntermedVals[i+1].second.size());
Simon Pilgrimbb8a40f2016-06-21 14:37:39 +00001750
Hal Finkelb811b6d2014-03-31 19:42:55 +00001751 int k = 0;
1752 for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
1753 ++j, ++k) {
1754 ShuffleVec[k] = j;
1755 FinalIndices.push_back(IntermedVals[i].second[j]);
1756 }
1757 for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
1758 ++j, ++k) {
1759 ShuffleVec[k] = NumElems + j;
1760 FinalIndices.push_back(IntermedVals[i+1].second[j]);
1761 }
1762
1763 SDValue Shuffle;
1764 if (Phase)
1765 Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
1766 IntermedVals[i+1].first,
Craig Topper2bd8b4b2016-07-01 06:54:47 +00001767 ShuffleVec);
Hal Finkelb811b6d2014-03-31 19:42:55 +00001768 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1769 return false;
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001770 NewIntermedVals.push_back(
1771 std::make_pair(Shuffle, std::move(FinalIndices)));
Hal Finkelb811b6d2014-03-31 19:42:55 +00001772 }
1773
1774 // If we had an odd number of defined values, then append the last
1775 // element to the array of new vectors.
1776 if ((IntermedVals.size() & 1) != 0)
1777 NewIntermedVals.push_back(IntermedVals.back());
1778
1779 IntermedVals.swap(NewIntermedVals);
1780 }
1781
1782 assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
1783 "Invalid number of intermediate vectors");
1784 SDValue Vec1 = IntermedVals[0].first;
1785 SDValue Vec2;
1786 if (IntermedVals.size() > 1)
1787 Vec2 = IntermedVals[1].first;
1788 else if (Phase)
1789 Vec2 = DAG.getUNDEF(VT);
1790
1791 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1792 for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
1793 ShuffleVec[IntermedVals[0].second[i]] = i;
1794 for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
1795 ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
1796
1797 if (Phase)
Craig Topper2bd8b4b2016-07-01 06:54:47 +00001798 Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
Hal Finkelb811b6d2014-03-31 19:42:55 +00001799 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1800 return false;
1801 }
1802
1803 return true;
1804}
Chris Lattner6be79822006-04-04 17:23:26 +00001805
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00001806/// Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00001807/// support the operation, but do support the resultant vector type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001808SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilsonf6c21952009-04-13 20:20:30 +00001809 unsigned NumElems = Node->getNumOperands();
Eli Friedman32345872009-06-07 06:52:44 +00001810 SDValue Value1, Value2;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001811 SDLoc dl(Node);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001812 EVT VT = Node->getValueType(0);
1813 EVT OpVT = Node->getOperand(0).getValueType();
1814 EVT EltVT = VT.getVectorElementType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001815
1816 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00001817 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001818 bool isOnlyLowElement = true;
Eli Friedman32345872009-06-07 06:52:44 +00001819 bool MoreThanTwoValues = false;
Chris Lattner77e271c2006-03-24 07:29:17 +00001820 bool isConstant = true;
Eli Friedman32345872009-06-07 06:52:44 +00001821 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001822 SDValue V = Node->getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00001823 if (V.isUndef())
Eli Friedman32345872009-06-07 06:52:44 +00001824 continue;
1825 if (i > 0)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001826 isOnlyLowElement = false;
Eli Friedman32345872009-06-07 06:52:44 +00001827 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner77e271c2006-03-24 07:29:17 +00001828 isConstant = false;
Eli Friedman32345872009-06-07 06:52:44 +00001829
1830 if (!Value1.getNode()) {
1831 Value1 = V;
1832 } else if (!Value2.getNode()) {
1833 if (V != Value1)
1834 Value2 = V;
1835 } else if (V != Value1 && V != Value2) {
1836 MoreThanTwoValues = true;
1837 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001838 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001839
Eli Friedman32345872009-06-07 06:52:44 +00001840 if (!Value1.getNode())
1841 return DAG.getUNDEF(VT);
1842
1843 if (isOnlyLowElement)
Bob Wilsonf6c21952009-04-13 20:20:30 +00001844 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001845
Chris Lattner77e271c2006-03-24 07:29:17 +00001846 // If all elements are constants, create a load from the constant pool.
1847 if (isConstant) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001848 SmallVector<Constant*, 16> CV;
Chris Lattner77e271c2006-03-24 07:29:17 +00001849 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00001850 if (ConstantFPSDNode *V =
Chris Lattner77e271c2006-03-24 07:29:17 +00001851 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00001852 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001853 } else if (ConstantSDNode *V =
Bob Wilsonf074ca72009-04-10 18:48:47 +00001854 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen6f7d5b22009-11-10 23:16:41 +00001855 if (OpVT==EltVT)
1856 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1857 else {
1858 // If OpVT and EltVT don't match, EltVT is not legal and the
1859 // element values have been promoted/truncated earlier. Undo this;
1860 // we don't want a v16i8 to become a v16i32 for example.
1861 const ConstantInt *CI = V->getConstantIntValue();
1862 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1863 CI->getZExtValue()));
1864 }
Chris Lattner77e271c2006-03-24 07:29:17 +00001865 } else {
Sanjay Patel57195842016-03-14 17:28:46 +00001866 assert(Node->getOperand(i).isUndef());
Chris Lattner229907c2011-07-18 04:54:35 +00001867 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Andersonb292b8c2009-07-30 23:03:37 +00001868 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner77e271c2006-03-24 07:29:17 +00001869 }
1870 }
Owen Anderson4aa32952009-07-28 21:19:26 +00001871 Constant *CP = ConstantVector::get(CV);
Mehdi Amini44ede332015-07-09 02:09:04 +00001872 SDValue CPIdx =
1873 DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001874 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001875 return DAG.getLoad(
1876 VT, dl, DAG.getEntryNode(), CPIdx,
Justin Lebar9c375812016-07-15 18:27:10 +00001877 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
1878 Alignment);
Chris Lattner77e271c2006-03-24 07:29:17 +00001879 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001880
Hal Finkel19775142014-03-31 17:48:10 +00001881 SmallSet<SDValue, 16> DefinedValues;
1882 for (unsigned i = 0; i < NumElems; ++i) {
Sanjay Patel57195842016-03-14 17:28:46 +00001883 if (Node->getOperand(i).isUndef())
Hal Finkel19775142014-03-31 17:48:10 +00001884 continue;
1885 DefinedValues.insert(Node->getOperand(i));
1886 }
1887
Hal Finkelb811b6d2014-03-31 19:42:55 +00001888 if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
1889 if (!MoreThanTwoValues) {
1890 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1891 for (unsigned i = 0; i < NumElems; ++i) {
1892 SDValue V = Node->getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00001893 if (V.isUndef())
Hal Finkelb811b6d2014-03-31 19:42:55 +00001894 continue;
1895 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1896 }
1897 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
1898 // Get the splatted value into the low element of a vector register.
1899 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1900 SDValue Vec2;
1901 if (Value2.getNode())
1902 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1903 else
1904 Vec2 = DAG.getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001905
Hal Finkelb811b6d2014-03-31 19:42:55 +00001906 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Craig Topper2bd8b4b2016-07-01 06:54:47 +00001907 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
Hal Finkelb811b6d2014-03-31 19:42:55 +00001908 }
1909 } else {
1910 SDValue Res;
1911 if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
1912 return Res;
Evan Cheng1d2e9952006-03-24 01:17:21 +00001913 }
1914 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001915
Eli Friedmanaee3f622009-06-06 07:04:42 +00001916 // Otherwise, we can't handle this case efficiently.
1917 return ExpandVectorBuildThroughStack(Node);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001918}
1919
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00001920// Expand a node into a call to a libcall. If the result value
Chris Lattneraac464e2005-01-21 06:05:23 +00001921// does not fit into a register, return the lo part and set the hi part to the
1922// by-reg argument. If it does fit into a single register, return the result
1923// and leave the Hi part unset.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001924SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedmanb3554152009-05-27 02:21:29 +00001925 bool isSigned) {
Chris Lattneraac464e2005-01-21 06:05:23 +00001926 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00001927 TargetLowering::ArgListEntry Entry;
Pete Cooper8fc121d2015-06-26 19:08:33 +00001928 for (const SDValue &Op : Node->op_values()) {
1929 EVT ArgVT = Op.getValueType();
Chris Lattner229907c2011-07-18 04:54:35 +00001930 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Pete Cooper8fc121d2015-06-26 19:08:33 +00001931 Entry.Node = Op;
1932 Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00001933 Entry.isSExt = isSigned;
Duncan Sands4c95dbd2008-02-14 17:28:50 +00001934 Entry.isZExt = !isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00001935 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00001936 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001937 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00001938 TLI.getPointerTy(DAG.getDataLayout()));
Misha Brukman835702a2005-04-21 22:36:52 +00001939
Chris Lattner229907c2011-07-18 04:54:35 +00001940 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Chengd4b08732010-11-30 23:55:39 +00001941
Evan Chengf8bad082012-04-10 01:51:00 +00001942 // By default, the input chain to this libcall is the entry node of the
1943 // function. If the libcall is going to be emitted as a tail call then
1944 // TLI.isUsedByReturnOnly will change it to the right chain if the return
1945 // node which is being folded has a non-entry input chain.
1946 SDValue InChain = DAG.getEntryNode();
1947
Evan Chengd4b08732010-11-30 23:55:39 +00001948 // isTailCall may be true since the callee does not reference caller stack
Tim Northoverb49a8a92016-03-22 19:14:38 +00001949 // frame. Check if it's in the right position and that the return types match.
Evan Cheng136861d2012-04-10 03:15:18 +00001950 SDValue TCChain = InChain;
Tim Northoverb49a8a92016-03-22 19:14:38 +00001951 const Function *F = DAG.getMachineFunction().getFunction();
1952 bool isTailCall =
1953 TLI.isInTailCallPosition(DAG, Node, TCChain) &&
1954 (RetTy == F->getReturnType() || F->getReturnType()->isVoidTy());
Evan Cheng136861d2012-04-10 03:15:18 +00001955 if (isTailCall)
1956 InChain = TCChain;
1957
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001958 TargetLowering::CallLoweringInfo CLI(DAG);
1959 CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00001960 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001961 .setTailCall(isTailCall).setSExtResult(isSigned).setZExtResult(!isSigned);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001962
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001963 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Chris Lattnera5bf1032005-05-12 04:49:08 +00001964
Evan Chengd4b08732010-11-30 23:55:39 +00001965 if (!CallInfo.second.getNode())
1966 // It's a tailcall, return the chain (which is the DAG root).
1967 return DAG.getRoot();
1968
Eli Friedman4a951bf2009-05-26 08:55:52 +00001969 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00001970}
1971
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00001972/// Generate a libcall taking the given operands as arguments
Eric Christopherbcaedb52011-04-20 01:19:45 +00001973/// and returning a result of type RetVT.
1974SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
1975 const SDValue *Ops, unsigned NumOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001976 bool isSigned, const SDLoc &dl) {
Eric Christopherbcaedb52011-04-20 01:19:45 +00001977 TargetLowering::ArgListTy Args;
1978 Args.reserve(NumOps);
Dan Gohmanae9b1682011-05-16 22:09:53 +00001979
Eric Christopherbcaedb52011-04-20 01:19:45 +00001980 TargetLowering::ArgListEntry Entry;
1981 for (unsigned i = 0; i != NumOps; ++i) {
1982 Entry.Node = Ops[i];
1983 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
1984 Entry.isSExt = isSigned;
1985 Entry.isZExt = !isSigned;
1986 Args.push_back(Entry);
1987 }
1988 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00001989 TLI.getPointerTy(DAG.getDataLayout()));
Dan Gohmanae9b1682011-05-16 22:09:53 +00001990
Chris Lattner229907c2011-07-18 04:54:35 +00001991 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001992
1993 TargetLowering::CallLoweringInfo CLI(DAG);
1994 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00001995 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001996 .setSExtResult(isSigned).setZExtResult(!isSigned);
1997
Justin Holewinskiaa583972012-05-25 16:35:28 +00001998 std::pair<SDValue,SDValue> CallInfo = TLI.LowerCallTo(CLI);
Dan Gohmanae9b1682011-05-16 22:09:53 +00001999
Eric Christopherbcaedb52011-04-20 01:19:45 +00002000 return CallInfo.first;
2001}
2002
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002003// Expand a node into a call to a libcall. Similar to
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002004// ExpandLibCall except that the first operand is the in-chain.
2005std::pair<SDValue, SDValue>
2006SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2007 SDNode *Node,
2008 bool isSigned) {
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002009 SDValue InChain = Node->getOperand(0);
2010
2011 TargetLowering::ArgListTy Args;
2012 TargetLowering::ArgListEntry Entry;
2013 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2014 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattner229907c2011-07-18 04:54:35 +00002015 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002016 Entry.Node = Node->getOperand(i);
2017 Entry.Ty = ArgTy;
2018 Entry.isSExt = isSigned;
2019 Entry.isZExt = !isSigned;
2020 Args.push_back(Entry);
2021 }
2022 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00002023 TLI.getPointerTy(DAG.getDataLayout()));
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002024
Chris Lattner229907c2011-07-18 04:54:35 +00002025 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002026
2027 TargetLowering::CallLoweringInfo CLI(DAG);
2028 CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002029 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002030 .setSExtResult(isSigned).setZExtResult(!isSigned);
2031
Justin Holewinskiaa583972012-05-25 16:35:28 +00002032 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002033
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002034 return CallInfo;
2035}
2036
Eli Friedmand6f28342009-05-27 03:33:44 +00002037SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2038 RTLIB::Libcall Call_F32,
2039 RTLIB::Libcall Call_F64,
2040 RTLIB::Libcall Call_F80,
Tim Northover4bf47bc2013-01-08 17:09:59 +00002041 RTLIB::Libcall Call_F128,
Eli Friedmand6f28342009-05-27 03:33:44 +00002042 RTLIB::Libcall Call_PPCF128) {
2043 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002044 switch (Node->getSimpleValueType(0).SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002045 default: llvm_unreachable("Unexpected request for libcall!");
Owen Anderson9f944592009-08-11 20:47:22 +00002046 case MVT::f32: LC = Call_F32; break;
2047 case MVT::f64: LC = Call_F64; break;
2048 case MVT::f80: LC = Call_F80; break;
Tim Northover4bf47bc2013-01-08 17:09:59 +00002049 case MVT::f128: LC = Call_F128; break;
Owen Anderson9f944592009-08-11 20:47:22 +00002050 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmand6f28342009-05-27 03:33:44 +00002051 }
2052 return ExpandLibCall(LC, Node, false);
2053}
2054
2055SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikovf93bb392009-11-07 17:14:39 +00002056 RTLIB::Libcall Call_I8,
Eli Friedmand6f28342009-05-27 03:33:44 +00002057 RTLIB::Libcall Call_I16,
2058 RTLIB::Libcall Call_I32,
2059 RTLIB::Libcall Call_I64,
2060 RTLIB::Libcall Call_I128) {
2061 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002062 switch (Node->getSimpleValueType(0).SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002063 default: llvm_unreachable("Unexpected request for libcall!");
Anton Korobeynikovf93bb392009-11-07 17:14:39 +00002064 case MVT::i8: LC = Call_I8; break;
2065 case MVT::i16: LC = Call_I16; break;
2066 case MVT::i32: LC = Call_I32; break;
2067 case MVT::i64: LC = Call_I64; break;
Owen Anderson9f944592009-08-11 20:47:22 +00002068 case MVT::i128: LC = Call_I128; break;
Eli Friedmand6f28342009-05-27 03:33:44 +00002069 }
2070 return ExpandLibCall(LC, Node, isSigned);
2071}
2072
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002073/// Issue libcalls to __{u}divmod to compute div / rem pairs.
Evan Chengb14ce092011-04-16 03:08:26 +00002074void
2075SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2076 SmallVectorImpl<SDValue> &Results) {
2077 unsigned Opcode = Node->getOpcode();
2078 bool isSigned = Opcode == ISD::SDIVREM;
2079
2080 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002081 switch (Node->getSimpleValueType(0).SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002082 default: llvm_unreachable("Unexpected request for libcall!");
Evan Chengb14ce092011-04-16 03:08:26 +00002083 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2084 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2085 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2086 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2087 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Chengbd766792011-04-01 00:42:02 +00002088 }
2089
2090 // The input chain to this libcall is the entry node of the function.
2091 // Legalizing the call will automatically add the previous call to the
2092 // dependence.
2093 SDValue InChain = DAG.getEntryNode();
2094
2095 EVT RetVT = Node->getValueType(0);
Chris Lattner229907c2011-07-18 04:54:35 +00002096 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Chengbd766792011-04-01 00:42:02 +00002097
2098 TargetLowering::ArgListTy Args;
2099 TargetLowering::ArgListEntry Entry;
Pete Cooper8fc121d2015-06-26 19:08:33 +00002100 for (const SDValue &Op : Node->op_values()) {
2101 EVT ArgVT = Op.getValueType();
Chris Lattner229907c2011-07-18 04:54:35 +00002102 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Pete Cooper8fc121d2015-06-26 19:08:33 +00002103 Entry.Node = Op;
2104 Entry.Ty = ArgTy;
Evan Chengbd766792011-04-01 00:42:02 +00002105 Entry.isSExt = isSigned;
2106 Entry.isZExt = !isSigned;
2107 Args.push_back(Entry);
2108 }
2109
2110 // Also pass the return address of the remainder.
2111 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2112 Entry.Node = FIPtr;
Micah Villmow51e72462012-10-24 17:25:11 +00002113 Entry.Ty = RetTy->getPointerTo();
Evan Chengbd766792011-04-01 00:42:02 +00002114 Entry.isSExt = isSigned;
2115 Entry.isZExt = !isSigned;
2116 Args.push_back(Entry);
2117
2118 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00002119 TLI.getPointerTy(DAG.getDataLayout()));
Evan Chengbd766792011-04-01 00:42:02 +00002120
Andrew Trickef9de2a2013-05-25 02:42:55 +00002121 SDLoc dl(Node);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002122 TargetLowering::CallLoweringInfo CLI(DAG);
2123 CLI.setDebugLoc(dl).setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002124 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002125 .setSExtResult(isSigned).setZExtResult(!isSigned);
2126
Justin Holewinskiaa583972012-05-25 16:35:28 +00002127 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Evan Chengbd766792011-04-01 00:42:02 +00002128
Evan Chengbd766792011-04-01 00:42:02 +00002129 // Remainder is loaded back from the stack frame.
Justin Lebar9c375812016-07-15 18:27:10 +00002130 SDValue Rem =
2131 DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo());
Evan Chengb14ce092011-04-16 03:08:26 +00002132 Results.push_back(CallInfo.first);
2133 Results.push_back(Rem);
Evan Chengbd766792011-04-01 00:42:02 +00002134}
2135
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002136/// Return true if sincos libcall is available.
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002137static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
2138 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002139 switch (Node->getSimpleValueType(0).SimpleTy) {
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002140 default: llvm_unreachable("Unexpected request for libcall!");
2141 case MVT::f32: LC = RTLIB::SINCOS_F32; break;
2142 case MVT::f64: LC = RTLIB::SINCOS_F64; break;
2143 case MVT::f80: LC = RTLIB::SINCOS_F80; break;
2144 case MVT::f128: LC = RTLIB::SINCOS_F128; break;
2145 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2146 }
Craig Topperc0196b12014-04-14 00:51:57 +00002147 return TLI.getLibcallName(LC) != nullptr;
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002148}
2149
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002150/// Return true if sincos libcall is available and can be used to combine sin
2151/// and cos.
Paul Redmondf29ddfe2013-02-15 18:45:18 +00002152static bool canCombineSinCosLibcall(SDNode *Node, const TargetLowering &TLI,
2153 const TargetMachine &TM) {
2154 if (!isSinCosLibcallAvailable(Node, TLI))
2155 return false;
2156 // GNU sin/cos functions set errno while sincos does not. Therefore
2157 // combining sin and cos is only safe if unsafe-fpmath is enabled.
Daniel Sandersbf2c03e2016-06-21 12:29:03 +00002158 if (TM.getTargetTriple().isGNUEnvironment() && !TM.Options.UnsafeFPMath)
Paul Redmondf29ddfe2013-02-15 18:45:18 +00002159 return false;
2160 return true;
2161}
2162
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002163/// Only issue sincos libcall if both sin and cos are needed.
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002164static bool useSinCos(SDNode *Node) {
2165 unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
2166 ? ISD::FCOS : ISD::FSIN;
Stephen Lincfe7f352013-07-08 00:37:03 +00002167
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002168 SDValue Op0 = Node->getOperand(0);
2169 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2170 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2171 SDNode *User = *UI;
2172 if (User == Node)
2173 continue;
2174 // The other user might have been turned into sincos already.
2175 if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
2176 return true;
2177 }
2178 return false;
2179}
2180
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002181/// Issue libcalls to sincos to compute sin / cos pairs.
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002182void
2183SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
2184 SmallVectorImpl<SDValue> &Results) {
2185 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002186 switch (Node->getSimpleValueType(0).SimpleTy) {
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002187 default: llvm_unreachable("Unexpected request for libcall!");
2188 case MVT::f32: LC = RTLIB::SINCOS_F32; break;
2189 case MVT::f64: LC = RTLIB::SINCOS_F64; break;
2190 case MVT::f80: LC = RTLIB::SINCOS_F80; break;
2191 case MVT::f128: LC = RTLIB::SINCOS_F128; break;
2192 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2193 }
Stephen Lincfe7f352013-07-08 00:37:03 +00002194
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002195 // The input chain to this libcall is the entry node of the function.
2196 // Legalizing the call will automatically add the previous call to the
2197 // dependence.
2198 SDValue InChain = DAG.getEntryNode();
Stephen Lincfe7f352013-07-08 00:37:03 +00002199
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002200 EVT RetVT = Node->getValueType(0);
2201 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Stephen Lincfe7f352013-07-08 00:37:03 +00002202
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002203 TargetLowering::ArgListTy Args;
2204 TargetLowering::ArgListEntry Entry;
Stephen Lincfe7f352013-07-08 00:37:03 +00002205
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002206 // Pass the argument.
2207 Entry.Node = Node->getOperand(0);
2208 Entry.Ty = RetTy;
2209 Entry.isSExt = false;
2210 Entry.isZExt = false;
2211 Args.push_back(Entry);
Stephen Lincfe7f352013-07-08 00:37:03 +00002212
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002213 // Pass the return address of sin.
2214 SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
2215 Entry.Node = SinPtr;
2216 Entry.Ty = RetTy->getPointerTo();
2217 Entry.isSExt = false;
2218 Entry.isZExt = false;
2219 Args.push_back(Entry);
Stephen Lincfe7f352013-07-08 00:37:03 +00002220
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002221 // Also pass the return address of the cos.
2222 SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
2223 Entry.Node = CosPtr;
2224 Entry.Ty = RetTy->getPointerTo();
2225 Entry.isSExt = false;
2226 Entry.isZExt = false;
2227 Args.push_back(Entry);
Stephen Lincfe7f352013-07-08 00:37:03 +00002228
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002229 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00002230 TLI.getPointerTy(DAG.getDataLayout()));
Stephen Lincfe7f352013-07-08 00:37:03 +00002231
Andrew Trickef9de2a2013-05-25 02:42:55 +00002232 SDLoc dl(Node);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002233 TargetLowering::CallLoweringInfo CLI(DAG);
2234 CLI.setDebugLoc(dl).setChain(InChain)
2235 .setCallee(TLI.getLibcallCallingConv(LC),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002236 Type::getVoidTy(*DAG.getContext()), Callee, std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002237
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002238 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2239
Justin Lebar9c375812016-07-15 18:27:10 +00002240 Results.push_back(
2241 DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo()));
2242 Results.push_back(
2243 DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo()));
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002244}
2245
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002246/// This function is responsible for legalizing a
Chris Lattner689bdcc2006-01-28 08:25:58 +00002247/// INT_TO_FP operation of the specified operand when the target requests that
2248/// we expand it. At this point, we know that the result and operand types are
2249/// legal for the target.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002250SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, SDValue Op0,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002251 EVT DestVT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002252 const SDLoc &dl) {
Sanjay Patela2607012015-09-16 16:31:21 +00002253 // TODO: Should any fast-math-flags be set for the created nodes?
Simon Pilgrimbb8a40f2016-06-21 14:37:39 +00002254
Akira Hatanakaadb14f52012-08-28 02:12:42 +00002255 if (Op0.getValueType() == MVT::i32 && TLI.isTypeLegal(MVT::f64)) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002256 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelcf0da6c2009-02-17 22:15:04 +00002257
Chris Lattnera2c7ff32008-01-16 07:03:22 +00002258 // Get the stack frame index of a 8 byte buffer.
Owen Anderson9f944592009-08-11 20:47:22 +00002259 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002260
Chris Lattner689bdcc2006-01-28 08:25:58 +00002261 // word offset constant for Hi/Lo address computation
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002262 SDValue WordOff = DAG.getConstant(sizeof(int), dl,
2263 StackSlot.getValueType());
Chris Lattner689bdcc2006-01-28 08:25:58 +00002264 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002265 SDValue Hi = StackSlot;
Tom Stellard838e2342013-08-26 15:06:10 +00002266 SDValue Lo = DAG.getNode(ISD::ADD, dl, StackSlot.getValueType(),
2267 StackSlot, WordOff);
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00002268 if (DAG.getDataLayout().isLittleEndian())
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00002269 std::swap(Hi, Lo);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002270
Chris Lattner689bdcc2006-01-28 08:25:58 +00002271 // if signed map to unsigned space
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002272 SDValue Op0Mapped;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002273 if (isSigned) {
2274 // constant used to invert sign bit (signed to unsigned mapping)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002275 SDValue SignBit = DAG.getConstant(0x80000000u, dl, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00002276 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002277 } else {
2278 Op0Mapped = Op0;
2279 }
2280 // store the lo of the constructed double - based on integer input
Justin Lebar9c375812016-07-15 18:27:10 +00002281 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op0Mapped, Lo,
2282 MachinePointerInfo());
Chris Lattner689bdcc2006-01-28 08:25:58 +00002283 // initial hi portion of constructed double
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002284 SDValue InitialHi = DAG.getConstant(0x43300000u, dl, MVT::i32);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002285 // store the hi of the constructed double - biased exponent
Justin Lebar9c375812016-07-15 18:27:10 +00002286 SDValue Store2 =
2287 DAG.getStore(Store1, dl, InitialHi, Hi, MachinePointerInfo());
Chris Lattner689bdcc2006-01-28 08:25:58 +00002288 // load the constructed double
Justin Lebar9c375812016-07-15 18:27:10 +00002289 SDValue Load =
2290 DAG.getLoad(MVT::f64, dl, Store2, StackSlot, MachinePointerInfo());
Chris Lattner689bdcc2006-01-28 08:25:58 +00002291 // FP constant to bias correct the final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002292 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonf074ca72009-04-10 18:48:47 +00002293 BitsToDouble(0x4330000080000000ULL) :
2294 BitsToDouble(0x4330000000000000ULL),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002295 dl, MVT::f64);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002296 // subtract the bias
Owen Anderson9f944592009-08-11 20:47:22 +00002297 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002298 // final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002299 SDValue Result;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002300 // handle final rounding
Owen Anderson9f944592009-08-11 20:47:22 +00002301 if (DestVT == MVT::f64) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002302 // do nothing
2303 Result = Sub;
Owen Anderson9f944592009-08-11 20:47:22 +00002304 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesen8525d832009-02-02 19:03:57 +00002305 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002306 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00002307 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen8525d832009-02-02 19:03:57 +00002308 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002309 }
2310 return Result;
2311 }
2312 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002313 // Code below here assumes !isSigned without checking again.
Dan Gohman14e450f2010-03-06 00:00:55 +00002314
2315 // Implementation of unsigned i64 to f64 following the algorithm in
2316 // __floatundidf in compiler_rt. This implementation has the advantage
2317 // of performing rounding correctly, both in the default rounding mode
2318 // and in all alternate rounding modes.
2319 // TODO: Generalize this for use with other types.
2320 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2321 SDValue TwoP52 =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002322 DAG.getConstant(UINT64_C(0x4330000000000000), dl, MVT::i64);
Dan Gohman14e450f2010-03-06 00:00:55 +00002323 SDValue TwoP84PlusTwoP52 =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002324 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), dl,
2325 MVT::f64);
Dan Gohman14e450f2010-03-06 00:00:55 +00002326 SDValue TwoP84 =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002327 DAG.getConstant(UINT64_C(0x4530000000000000), dl, MVT::i64);
Dan Gohman14e450f2010-03-06 00:00:55 +00002328
2329 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2330 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002331 DAG.getConstant(32, dl, MVT::i64));
Dan Gohman14e450f2010-03-06 00:00:55 +00002332 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2333 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peck527da1b2010-11-23 03:31:01 +00002334 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2335 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach9b7755f2010-07-02 17:41:59 +00002336 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2337 TwoP84PlusTwoP52);
Dan Gohman14e450f2010-03-06 00:00:55 +00002338 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2339 }
2340
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002341 // Implementation of unsigned i64 to f32.
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002342 // TODO: Generalize this for use with other types.
2343 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002344 // For unsigned conversions, convert them to signed conversions using the
2345 // algorithm from the x86_64 __floatundidf in compiler_rt.
2346 if (!isSigned) {
2347 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peck527da1b2010-11-23 03:31:01 +00002348
Mehdi Amini9639d652015-07-09 02:09:20 +00002349 SDValue ShiftConst = DAG.getConstant(
2350 1, dl, TLI.getShiftAmountTy(Op0.getValueType(), DAG.getDataLayout()));
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002351 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002352 SDValue AndConst = DAG.getConstant(1, dl, MVT::i64);
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002353 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2354 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peck527da1b2010-11-23 03:31:01 +00002355
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002356 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2357 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peck527da1b2010-11-23 03:31:01 +00002358
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002359 // TODO: This really should be implemented using a branch rather than a
Wesley Peck527da1b2010-11-23 03:31:01 +00002360 // select. We happen to get lucky and machinesink does the right
2361 // thing most of the time. This would be a good candidate for a
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002362 //pseudo-op, or, even better, for whole-function isel.
Matt Arsenault758659232013-05-18 00:21:46 +00002363 SDValue SignBitTest = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002364 Op0, DAG.getConstant(0, dl, MVT::i64), ISD::SETLT);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002365 return DAG.getSelect(dl, MVT::f32, SignBitTest, Slow, Fast);
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002366 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002367
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002368 // Otherwise, implement the fully general conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002369
Jim Grosbach9b7755f2010-07-02 17:41:59 +00002370 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002371 DAG.getConstant(UINT64_C(0xfffffffffffff800), dl, MVT::i64));
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002372 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002373 DAG.getConstant(UINT64_C(0x800), dl, MVT::i64));
Jim Grosbach9b7755f2010-07-02 17:41:59 +00002374 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002375 DAG.getConstant(UINT64_C(0x7ff), dl, MVT::i64));
2376 SDValue Ne = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), And2,
2377 DAG.getConstant(UINT64_C(0), dl, MVT::i64),
2378 ISD::SETNE);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002379 SDValue Sel = DAG.getSelect(dl, MVT::i64, Ne, Or, Op0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002380 SDValue Ge = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), Op0,
2381 DAG.getConstant(UINT64_C(0x0020000000000000), dl,
2382 MVT::i64),
2383 ISD::SETUGE);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002384 SDValue Sel2 = DAG.getSelect(dl, MVT::i64, Ge, Sel, Op0);
Mehdi Amini9639d652015-07-09 02:09:20 +00002385 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType(), DAG.getDataLayout());
Wesley Peck527da1b2010-11-23 03:31:01 +00002386
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002387 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002388 DAG.getConstant(32, dl, SHVT));
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002389 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2390 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2391 SDValue TwoP32 =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002392 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), dl,
2393 MVT::f64);
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002394 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2395 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2396 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2397 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2398 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002399 DAG.getIntPtrConstant(0, dl));
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002400 }
2401
Dan Gohman998c7c22010-03-05 02:40:23 +00002402 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002403
Matt Arsenault758659232013-05-18 00:21:46 +00002404 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(Op0.getValueType()),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002405 Op0,
2406 DAG.getConstant(0, dl, Op0.getValueType()),
Dan Gohman998c7c22010-03-05 02:40:23 +00002407 ISD::SETLT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002408 SDValue Zero = DAG.getIntPtrConstant(0, dl),
2409 Four = DAG.getIntPtrConstant(4, dl);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002410 SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
Dan Gohman998c7c22010-03-05 02:40:23 +00002411 SignSet, Four, Zero);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002412
Dan Gohman998c7c22010-03-05 02:40:23 +00002413 // If the sign bit of the integer is set, the large number will be treated
2414 // as a negative number. To counteract this, the dynamic code adds an
2415 // offset depending on the data type.
2416 uint64_t FF;
Craig Topperd9c27832013-08-15 02:44:19 +00002417 switch (Op0.getSimpleValueType().SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002418 default: llvm_unreachable("Unsupported integer type!");
Dan Gohman998c7c22010-03-05 02:40:23 +00002419 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2420 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2421 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2422 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2423 }
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00002424 if (DAG.getDataLayout().isLittleEndian())
2425 FF <<= 32;
Dan Gohman998c7c22010-03-05 02:40:23 +00002426 Constant *FudgeFactor = ConstantInt::get(
2427 Type::getInt64Ty(*DAG.getContext()), FF);
2428
Mehdi Amini44ede332015-07-09 02:09:04 +00002429 SDValue CPIdx =
2430 DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
Dan Gohman998c7c22010-03-05 02:40:23 +00002431 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Tom Stellard838e2342013-08-26 15:06:10 +00002432 CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
Dan Gohman998c7c22010-03-05 02:40:23 +00002433 Alignment = std::min(Alignment, 4u);
2434 SDValue FudgeInReg;
2435 if (DestVT == MVT::f32)
Alex Lorenze40c8a22015-08-11 23:09:45 +00002436 FudgeInReg = DAG.getLoad(
2437 MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Justin Lebar9c375812016-07-15 18:27:10 +00002438 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2439 Alignment);
Dan Gohman998c7c22010-03-05 02:40:23 +00002440 else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00002441 SDValue Load = DAG.getExtLoad(
2442 ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
2443 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
Justin Lebar9c375812016-07-15 18:27:10 +00002444 Alignment);
Dan Gohman198b7ff2011-11-03 21:49:52 +00002445 HandleSDNode Handle(Load);
2446 LegalizeOp(Load.getNode());
2447 FudgeInReg = Handle.getValue();
Dan Gohman998c7c22010-03-05 02:40:23 +00002448 }
2449
2450 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002451}
2452
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002453/// This function is responsible for legalizing a
Chris Lattner689bdcc2006-01-28 08:25:58 +00002454/// *INT_TO_FP operation of the specified operand when the target requests that
2455/// we promote it. At this point, we know that the result and operand types are
2456/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2457/// operation that takes a larger input.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002458SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT,
Dale Johannesen8525d832009-02-02 19:03:57 +00002459 bool isSigned,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002460 const SDLoc &dl) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002461 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002462 EVT NewInTy = LegalOp.getValueType();
Chris Lattner689bdcc2006-01-28 08:25:58 +00002463
2464 unsigned OpToUse = 0;
2465
2466 // Scan for the appropriate larger type to use.
2467 while (1) {
Owen Anderson9f944592009-08-11 20:47:22 +00002468 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00002469 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00002470
2471 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedmane1bc3792009-05-28 03:06:16 +00002472 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2473 OpToUse = ISD::SINT_TO_FP;
2474 break;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002475 }
Chris Lattner689bdcc2006-01-28 08:25:58 +00002476 if (isSigned) continue;
2477
2478 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedmane1bc3792009-05-28 03:06:16 +00002479 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2480 OpToUse = ISD::UINT_TO_FP;
2481 break;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002482 }
Chris Lattner689bdcc2006-01-28 08:25:58 +00002483
2484 // Otherwise, try a larger type.
2485 }
2486
2487 // Okay, we found the operation and type to use. Zero extend our input to the
2488 // desired type then run the operation on it.
Dale Johannesen8525d832009-02-02 19:03:57 +00002489 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00002490 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesen8525d832009-02-02 19:03:57 +00002491 dl, NewInTy, LegalOp));
Chris Lattner689bdcc2006-01-28 08:25:58 +00002492}
2493
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002494/// This function is responsible for legalizing a
Chris Lattner689bdcc2006-01-28 08:25:58 +00002495/// FP_TO_*INT operation of the specified operand when the target requests that
2496/// we promote it. At this point, we know that the result and operand types are
2497/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2498/// operation that returns a larger result.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002499SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT,
Dale Johannesen8525d832009-02-02 19:03:57 +00002500 bool isSigned,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002501 const SDLoc &dl) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002502 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002503 EVT NewOutTy = DestVT;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002504
2505 unsigned OpToUse = 0;
2506
2507 // Scan for the appropriate larger type to use.
2508 while (1) {
Owen Anderson9f944592009-08-11 20:47:22 +00002509 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00002510 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00002511
Tim Northover65277a22014-06-15 09:27:20 +00002512 // A larger signed type can hold all unsigned values of the requested type,
2513 // so using FP_TO_SINT is valid
Eli Friedmane1bc3792009-05-28 03:06:16 +00002514 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002515 OpToUse = ISD::FP_TO_SINT;
2516 break;
2517 }
Chris Lattner689bdcc2006-01-28 08:25:58 +00002518
Tim Northover65277a22014-06-15 09:27:20 +00002519 // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2520 if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002521 OpToUse = ISD::FP_TO_UINT;
2522 break;
2523 }
Chris Lattner689bdcc2006-01-28 08:25:58 +00002524
2525 // Otherwise, try a larger type.
2526 }
2527
Scott Michelcf0da6c2009-02-17 22:15:04 +00002528
Chris Lattnerf81d5882007-11-24 07:07:01 +00002529 // Okay, we found the operation and type to use.
Dale Johannesen8525d832009-02-02 19:03:57 +00002530 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands93e180342008-07-04 11:47:58 +00002531
Chris Lattnerf81d5882007-11-24 07:07:01 +00002532 // Truncate the result of the extended FP_TO_*INT operation to the desired
2533 // size.
Dale Johannesen8525d832009-02-02 19:03:57 +00002534 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002535}
2536
Simon Pilgrim820f87a2016-07-22 16:46:25 +00002537/// Legalize a BITREVERSE scalar/vector operation as a series of mask + shifts.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002538SDValue SelectionDAGLegalize::ExpandBITREVERSE(SDValue Op, const SDLoc &dl) {
James Molloy90111f72015-11-12 12:29:09 +00002539 EVT VT = Op.getValueType();
2540 EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
James Molloybb1dbf52015-11-13 10:02:36 +00002541 unsigned Sz = VT.getScalarSizeInBits();
Chad Rosier1cb56a12016-05-16 20:03:02 +00002542
Simon Pilgrim820f87a2016-07-22 16:46:25 +00002543 SDValue Tmp, Tmp2, Tmp3;
2544
2545 // If we can, perform BSWAP first and then the mask+swap the i4, then i2
2546 // and finally the i1 pairs.
2547 // TODO: We can easily support i4/i2 legal types if any target ever does.
2548 if (Sz >= 8 && isPowerOf2_32(Sz)) {
2549 // Create the masks - repeating the pattern every byte.
2550 APInt MaskHi4(Sz, 0), MaskHi2(Sz, 0), MaskHi1(Sz, 0);
2551 APInt MaskLo4(Sz, 0), MaskLo2(Sz, 0), MaskLo1(Sz, 0);
2552 for (unsigned J = 0; J != Sz; J += 8) {
2553 MaskHi4 = MaskHi4.Or(APInt(Sz, 0xF0ull << J));
2554 MaskLo4 = MaskLo4.Or(APInt(Sz, 0x0Full << J));
2555 MaskHi2 = MaskHi2.Or(APInt(Sz, 0xCCull << J));
2556 MaskLo2 = MaskLo2.Or(APInt(Sz, 0x33ull << J));
2557 MaskHi1 = MaskHi1.Or(APInt(Sz, 0xAAull << J));
2558 MaskLo1 = MaskLo1.Or(APInt(Sz, 0x55ull << J));
2559 }
2560
2561 // BSWAP if the type is wider than a single byte.
2562 Tmp = (Sz > 8 ? DAG.getNode(ISD::BSWAP, dl, VT, Op) : Op);
2563
2564 // swap i4: ((V & 0xF0) >> 4) | ((V & 0x0F) << 4)
2565 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi4, dl, VT));
2566 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo4, dl, VT));
2567 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(4, dl, VT));
2568 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(4, dl, VT));
2569 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
2570
2571 // swap i2: ((V & 0xCC) >> 2) | ((V & 0x33) << 2)
2572 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi2, dl, VT));
2573 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo2, dl, VT));
2574 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(2, dl, VT));
2575 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(2, dl, VT));
2576 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
2577
2578 // swap i1: ((V & 0xAA) >> 1) | ((V & 0x55) << 1)
2579 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi1, dl, VT));
2580 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo1, dl, VT));
2581 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(1, dl, VT));
2582 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(1, dl, VT));
2583 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
2584 return Tmp;
2585 }
2586
James Molloy90111f72015-11-12 12:29:09 +00002587 Tmp = DAG.getConstant(0, dl, VT);
2588 for (unsigned I = 0, J = Sz-1; I < Sz; ++I, --J) {
2589 if (I < J)
2590 Tmp2 =
2591 DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(J - I, dl, SHVT));
2592 else
2593 Tmp2 =
2594 DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(I - J, dl, SHVT));
Simon Pilgrimbb8a40f2016-06-21 14:37:39 +00002595
James Molloybb1dbf52015-11-13 10:02:36 +00002596 APInt Shift(Sz, 1);
2597 Shift = Shift.shl(J);
2598 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(Shift, dl, VT));
James Molloy90111f72015-11-12 12:29:09 +00002599 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp, Tmp2);
2600 }
2601
2602 return Tmp;
2603}
2604
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002605/// Open code the operations for BSWAP of the specified operation.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002606SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, const SDLoc &dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002607 EVT VT = Op.getValueType();
Mehdi Amini9639d652015-07-09 02:09:20 +00002608 EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002609 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Simon Pilgrim820f87a2016-07-22 16:46:25 +00002610 switch (VT.getSimpleVT().getScalarType().SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002611 default: llvm_unreachable("Unhandled Expand type in BSWAP!");
Owen Anderson9f944592009-08-11 20:47:22 +00002612 case MVT::i16:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002613 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2614 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
Dale Johannesena02e45c2009-02-02 22:12:50 +00002615 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson9f944592009-08-11 20:47:22 +00002616 case MVT::i32:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002617 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2618 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2619 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2620 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2621 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
2622 DAG.getConstant(0xFF0000, dl, VT));
2623 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, dl, VT));
Dale Johannesena02e45c2009-02-02 22:12:50 +00002624 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2625 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2626 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson9f944592009-08-11 20:47:22 +00002627 case MVT::i64:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002628 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
2629 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
2630 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2631 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2632 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2633 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2634 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
2635 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
2636 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7,
2637 DAG.getConstant(255ULL<<48, dl, VT));
2638 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6,
2639 DAG.getConstant(255ULL<<40, dl, VT));
2640 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5,
2641 DAG.getConstant(255ULL<<32, dl, VT));
2642 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4,
2643 DAG.getConstant(255ULL<<24, dl, VT));
2644 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
2645 DAG.getConstant(255ULL<<16, dl, VT));
2646 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2,
2647 DAG.getConstant(255ULL<<8 , dl, VT));
Dale Johannesena02e45c2009-02-02 22:12:50 +00002648 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2649 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2650 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2651 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2652 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2653 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2654 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002655 }
2656}
2657
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002658/// Expand the specified bitcount instruction into operations.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002659SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002660 const SDLoc &dl) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002661 switch (Opc) {
Craig Topperee4dab52012-02-05 08:31:47 +00002662 default: llvm_unreachable("Cannot expand this yet!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00002663 case ISD::CTPOP: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002664 EVT VT = Op.getValueType();
Mehdi Amini9639d652015-07-09 02:09:20 +00002665 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
Benjamin Kramerfff25172011-01-15 20:30:30 +00002666 unsigned Len = VT.getSizeInBits();
2667
Benjamin Kramerbec03ea2011-01-15 21:19:37 +00002668 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2669 "CTPOP not implemented for this type.");
2670
Benjamin Kramerfff25172011-01-15 20:30:30 +00002671 // This is the "best" algorithm from
2672 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2673
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002674 SDValue Mask55 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)),
2675 dl, VT);
2676 SDValue Mask33 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)),
2677 dl, VT);
2678 SDValue Mask0F = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)),
2679 dl, VT);
2680 SDValue Mask01 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)),
2681 dl, VT);
Benjamin Kramerfff25172011-01-15 20:30:30 +00002682
2683 // v = v - ((v >> 1) & 0x55555555...)
2684 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2685 DAG.getNode(ISD::AND, dl, VT,
2686 DAG.getNode(ISD::SRL, dl, VT, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002687 DAG.getConstant(1, dl, ShVT)),
Benjamin Kramerfff25172011-01-15 20:30:30 +00002688 Mask55));
2689 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2690 Op = DAG.getNode(ISD::ADD, dl, VT,
2691 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2692 DAG.getNode(ISD::AND, dl, VT,
2693 DAG.getNode(ISD::SRL, dl, VT, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002694 DAG.getConstant(2, dl, ShVT)),
Benjamin Kramerfff25172011-01-15 20:30:30 +00002695 Mask33));
2696 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2697 Op = DAG.getNode(ISD::AND, dl, VT,
2698 DAG.getNode(ISD::ADD, dl, VT, Op,
2699 DAG.getNode(ISD::SRL, dl, VT, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002700 DAG.getConstant(4, dl, ShVT))),
Benjamin Kramerfff25172011-01-15 20:30:30 +00002701 Mask0F);
2702 // v = (v * 0x01010101...) >> (Len - 8)
2703 Op = DAG.getNode(ISD::SRL, dl, VT,
2704 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002705 DAG.getConstant(Len - 8, dl, ShVT));
Owen Andersonb2c80da2011-02-25 21:41:48 +00002706
Chris Lattner689bdcc2006-01-28 08:25:58 +00002707 return Op;
2708 }
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002709 case ISD::CTLZ_ZERO_UNDEF:
2710 // This trivially expands to CTLZ.
2711 return DAG.getNode(ISD::CTLZ, dl, Op.getValueType(), Op);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002712 case ISD::CTLZ: {
Matt Arsenault5ca3c722016-01-11 16:37:46 +00002713 EVT VT = Op.getValueType();
2714 unsigned len = VT.getSizeInBits();
2715
2716 if (TLI.isOperationLegalOrCustom(ISD::CTLZ_ZERO_UNDEF, VT)) {
2717 EVT SetCCVT = getSetCCResultType(VT);
2718 SDValue CTLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, VT, Op);
2719 SDValue Zero = DAG.getConstant(0, dl, VT);
2720 SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ);
2721 return DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero,
2722 DAG.getConstant(len, dl, VT), CTLZ);
2723 }
2724
Chris Lattner689bdcc2006-01-28 08:25:58 +00002725 // for now, we do this:
2726 // x = x | (x >> 1);
2727 // x = x | (x >> 2);
2728 // ...
2729 // x = x | (x >>16);
2730 // x = x | (x >>32); // for 64-bit input
2731 // return popcount(~x);
2732 //
Sanjay Patelbb292212014-09-15 19:47:44 +00002733 // Ref: "Hacker's Delight" by Henry Warren
Mehdi Amini9639d652015-07-09 02:09:20 +00002734 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
Chris Lattner689bdcc2006-01-28 08:25:58 +00002735 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002736 SDValue Tmp3 = DAG.getConstant(1ULL << i, dl, ShVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002737 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesendc93bbc2009-02-06 21:55:48 +00002738 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner689bdcc2006-01-28 08:25:58 +00002739 }
Dale Johannesena02e45c2009-02-02 22:12:50 +00002740 Op = DAG.getNOT(dl, Op, VT);
2741 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002742 }
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002743 case ISD::CTTZ_ZERO_UNDEF:
2744 // This trivially expands to CTTZ.
2745 return DAG.getNode(ISD::CTTZ, dl, Op.getValueType(), Op);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002746 case ISD::CTTZ: {
2747 // for now, we use: { return popcount(~x & (x - 1)); }
2748 // unless the target has ctlz but not ctpop, in which case we use:
2749 // { return 32 - nlz(~x & (x-1)); }
Sanjay Patelbb292212014-09-15 19:47:44 +00002750 // Ref: "Hacker's Delight" by Henry Warren
Owen Anderson53aa7a92009-08-10 22:56:29 +00002751 EVT VT = Op.getValueType();
Dale Johannesena02e45c2009-02-02 22:12:50 +00002752 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2753 DAG.getNOT(dl, Op, VT),
2754 DAG.getNode(ISD::SUB, dl, VT, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002755 DAG.getConstant(1, dl, VT)));
Chris Lattner689bdcc2006-01-28 08:25:58 +00002756 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohman4aa18462009-01-28 17:46:25 +00002757 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2758 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesena02e45c2009-02-02 22:12:50 +00002759 return DAG.getNode(ISD::SUB, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002760 DAG.getConstant(VT.getSizeInBits(), dl, VT),
Dale Johannesena02e45c2009-02-02 22:12:50 +00002761 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2762 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002763 }
2764 }
2765}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002766
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00002767bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00002768 SmallVector<SDValue, 8> Results;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002769 SDLoc dl(Node);
Eli Friedmane1dc1932009-05-28 20:40:34 +00002770 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Daniel Sandersedc071b2013-11-21 13:24:49 +00002771 bool NeedInvert;
Eli Friedman21d349b2009-05-27 01:25:56 +00002772 switch (Node->getOpcode()) {
2773 case ISD::CTPOP:
2774 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002775 case ISD::CTLZ_ZERO_UNDEF:
Eli Friedman21d349b2009-05-27 01:25:56 +00002776 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002777 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman21d349b2009-05-27 01:25:56 +00002778 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2779 Results.push_back(Tmp1);
2780 break;
James Molloy90111f72015-11-12 12:29:09 +00002781 case ISD::BITREVERSE:
2782 Results.push_back(ExpandBITREVERSE(Node->getOperand(0), dl));
2783 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00002784 case ISD::BSWAP:
Bill Wendlingef408db2009-12-23 00:28:23 +00002785 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman21d349b2009-05-27 01:25:56 +00002786 break;
2787 case ISD::FRAMEADDR:
2788 case ISD::RETURNADDR:
2789 case ISD::FRAME_TO_ARGS_OFFSET:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002790 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
Eli Friedman21d349b2009-05-27 01:25:56 +00002791 break;
Hal Finkel5081ac22016-09-01 10:28:47 +00002792 case ISD::EH_DWARF_CFA: {
2793 SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
2794 TLI.getPointerTy(DAG.getDataLayout()));
2795 SDValue Offset = DAG.getNode(ISD::ADD, dl,
2796 CfaArg.getValueType(),
2797 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
2798 CfaArg.getValueType()),
2799 CfaArg);
2800 SDValue FA = DAG.getNode(
2801 ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()),
2802 DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
2803 Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
2804 FA, Offset));
2805 break;
2806 }
Eli Friedman21d349b2009-05-27 01:25:56 +00002807 case ISD::FLT_ROUNDS_:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002808 Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
Eli Friedman21d349b2009-05-27 01:25:56 +00002809 break;
2810 case ISD::EH_RETURN:
Eli Friedman21d349b2009-05-27 01:25:56 +00002811 case ISD::EH_LABEL:
2812 case ISD::PREFETCH:
Eli Friedman21d349b2009-05-27 01:25:56 +00002813 case ISD::VAEND:
Jim Grosbachdc0a0652010-07-06 23:44:52 +00002814 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbachbbdc5d22010-10-19 23:27:08 +00002815 // If the target didn't expand these, there's nothing to do, so just
2816 // preserve the chain and be done.
Jim Grosbachdc0a0652010-07-06 23:44:52 +00002817 Results.push_back(Node->getOperand(0));
2818 break;
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00002819 case ISD::READCYCLECOUNTER:
2820 // If the target didn't expand this, just return 'zero' and preserve the
2821 // chain.
2822 Results.append(Node->getNumValues() - 1,
2823 DAG.getConstant(0, dl, Node->getValueType(0)));
2824 Results.push_back(Node->getOperand(0));
2825 break;
Jim Grosbachdc0a0652010-07-06 23:44:52 +00002826 case ISD::EH_SJLJ_SETJMP:
Jim Grosbachbbdc5d22010-10-19 23:27:08 +00002827 // If the target didn't expand this, just return 'zero' and preserve the
2828 // chain.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002829 Results.push_back(DAG.getConstant(0, dl, MVT::i32));
Eli Friedman21d349b2009-05-27 01:25:56 +00002830 Results.push_back(Node->getOperand(0));
2831 break;
Eli Friedman452aae62011-08-26 02:59:24 +00002832 case ISD::ATOMIC_LOAD: {
2833 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002834 SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
Tim Northover420a2162014-06-13 14:24:07 +00002835 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
2836 SDValue Swap = DAG.getAtomicCmpSwap(
2837 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
2838 Node->getOperand(0), Node->getOperand(1), Zero, Zero,
2839 cast<AtomicSDNode>(Node)->getMemOperand(),
2840 cast<AtomicSDNode>(Node)->getOrdering(),
2841 cast<AtomicSDNode>(Node)->getOrdering(),
2842 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman452aae62011-08-26 02:59:24 +00002843 Results.push_back(Swap.getValue(0));
2844 Results.push_back(Swap.getValue(1));
2845 break;
2846 }
2847 case ISD::ATOMIC_STORE: {
2848 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
2849 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
2850 cast<AtomicSDNode>(Node)->getMemoryVT(),
2851 Node->getOperand(0),
2852 Node->getOperand(1), Node->getOperand(2),
2853 cast<AtomicSDNode>(Node)->getMemOperand(),
2854 cast<AtomicSDNode>(Node)->getOrdering(),
2855 cast<AtomicSDNode>(Node)->getSynchScope());
2856 Results.push_back(Swap.getValue(1));
2857 break;
2858 }
Tim Northover420a2162014-06-13 14:24:07 +00002859 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
2860 // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
2861 // splits out the success value as a comparison. Expanding the resulting
2862 // ATOMIC_CMP_SWAP will produce a libcall.
2863 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
2864 SDValue Res = DAG.getAtomicCmpSwap(
2865 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
2866 Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
2867 Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand(),
2868 cast<AtomicSDNode>(Node)->getSuccessOrdering(),
2869 cast<AtomicSDNode>(Node)->getFailureOrdering(),
2870 cast<AtomicSDNode>(Node)->getSynchScope());
2871
Marcin Koscielnickibbac8902016-05-10 16:49:04 +00002872 SDValue ExtRes = Res;
Tim Northover4498eff2016-03-24 15:38:38 +00002873 SDValue LHS = Res;
2874 SDValue RHS = Node->getOperand(1);
Tim Northover420a2162014-06-13 14:24:07 +00002875
Tim Northover4498eff2016-03-24 15:38:38 +00002876 EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
2877 EVT OuterType = Node->getValueType(0);
Marcin Koscielnickibbac8902016-05-10 16:49:04 +00002878 switch (TLI.getExtendForAtomicOps()) {
2879 case ISD::SIGN_EXTEND:
Tim Northover4498eff2016-03-24 15:38:38 +00002880 LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
2881 DAG.getValueType(AtomicType));
2882 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
2883 Node->getOperand(2), DAG.getValueType(AtomicType));
Marcin Koscielnickibbac8902016-05-10 16:49:04 +00002884 ExtRes = LHS;
2885 break;
2886 case ISD::ZERO_EXTEND:
2887 LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
2888 DAG.getValueType(AtomicType));
Tim Northover4498eff2016-03-24 15:38:38 +00002889 RHS = DAG.getNode(ISD::ZERO_EXTEND, dl, OuterType, Node->getOperand(2));
Marcin Koscielnickibbac8902016-05-10 16:49:04 +00002890 ExtRes = LHS;
2891 break;
2892 case ISD::ANY_EXTEND:
2893 LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
2894 RHS = DAG.getNode(ISD::ZERO_EXTEND, dl, OuterType, Node->getOperand(2));
2895 break;
2896 default:
2897 llvm_unreachable("Invalid atomic op extension");
Tim Northover4498eff2016-03-24 15:38:38 +00002898 }
2899
2900 SDValue Success =
2901 DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
2902
Marcin Koscielnickibbac8902016-05-10 16:49:04 +00002903 Results.push_back(ExtRes.getValue(0));
Tim Northover420a2162014-06-13 14:24:07 +00002904 Results.push_back(Success);
2905 Results.push_back(Res.getValue(1));
2906 break;
2907 }
Eli Friedman2892d822009-05-27 12:20:41 +00002908 case ISD::DYNAMIC_STACKALLOC:
2909 ExpandDYNAMIC_STACKALLOC(Node, Results);
2910 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00002911 case ISD::MERGE_VALUES:
2912 for (unsigned i = 0; i < Node->getNumValues(); i++)
2913 Results.push_back(Node->getOperand(i));
2914 break;
2915 case ISD::UNDEF: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002916 EVT VT = Node->getValueType(0);
Eli Friedman21d349b2009-05-27 01:25:56 +00002917 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002918 Results.push_back(DAG.getConstant(0, dl, VT));
Chris Lattnercd927182010-04-07 23:47:51 +00002919 else {
2920 assert(VT.isFloatingPoint() && "Unknown value type!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002921 Results.push_back(DAG.getConstantFP(0, dl, VT));
Chris Lattnercd927182010-04-07 23:47:51 +00002922 }
Eli Friedman21d349b2009-05-27 01:25:56 +00002923 break;
2924 }
Eli Friedman21d349b2009-05-27 01:25:56 +00002925 case ISD::FP_ROUND:
Wesley Peck527da1b2010-11-23 03:31:01 +00002926 case ISD::BITCAST:
Eli Friedman21d349b2009-05-27 01:25:56 +00002927 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2928 Node->getValueType(0), dl);
2929 Results.push_back(Tmp1);
2930 break;
2931 case ISD::FP_EXTEND:
2932 Tmp1 = EmitStackConvert(Node->getOperand(0),
2933 Node->getOperand(0).getValueType(),
2934 Node->getValueType(0), dl);
2935 Results.push_back(Tmp1);
2936 break;
2937 case ISD::SIGN_EXTEND_INREG: {
2938 // NOTE: we could fall back on load/store here too for targets without
2939 // SAR. However, it is doubtful that any exist.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002940 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman1d459e42009-12-11 21:31:27 +00002941 EVT VT = Node->getValueType(0);
Mehdi Amini9639d652015-07-09 02:09:20 +00002942 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002943 if (VT.isVector())
Dan Gohman1d459e42009-12-11 21:31:27 +00002944 ShiftAmountTy = VT;
Sanjay Patelbd6fca12016-09-14 15:21:00 +00002945 unsigned BitsDiff = VT.getScalarSizeInBits() -
2946 ExtraVT.getScalarSizeInBits();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002947 SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
Eli Friedman21d349b2009-05-27 01:25:56 +00002948 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2949 Node->getOperand(0), ShiftCst);
Bill Wendlingef408db2009-12-23 00:28:23 +00002950 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2951 Results.push_back(Tmp1);
Eli Friedman21d349b2009-05-27 01:25:56 +00002952 break;
2953 }
2954 case ISD::FP_ROUND_INREG: {
2955 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002956 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman21d349b2009-05-27 01:25:56 +00002957
2958 // NOTE: there is a choice here between constantly creating new stack
2959 // slots and always reusing the same one. We currently always create
2960 // new ones, as reuse may inhibit scheduling.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002961 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman21d349b2009-05-27 01:25:56 +00002962 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2963 Node->getValueType(0), dl);
2964 Results.push_back(Tmp1);
2965 break;
2966 }
2967 case ISD::SINT_TO_FP:
2968 case ISD::UINT_TO_FP:
2969 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2970 Node->getOperand(0), Node->getValueType(0), dl);
2971 Results.push_back(Tmp1);
2972 break;
Jan Veselyeca89d22014-07-10 22:40:18 +00002973 case ISD::FP_TO_SINT:
2974 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
2975 Results.push_back(Tmp1);
Tom Stellardaad46592014-06-17 16:53:07 +00002976 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00002977 case ISD::FP_TO_UINT: {
2978 SDValue True, False;
Owen Anderson53aa7a92009-08-10 22:56:29 +00002979 EVT VT = Node->getOperand(0).getValueType();
2980 EVT NVT = Node->getValueType(0);
Tim Northover29178a32013-01-22 09:46:31 +00002981 APFloat apf(DAG.EVTToAPFloatSemantics(VT),
2982 APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman21d349b2009-05-27 01:25:56 +00002983 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2984 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002985 Tmp1 = DAG.getConstantFP(apf, dl, VT);
Matt Arsenault758659232013-05-18 00:21:46 +00002986 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(VT),
Eli Friedman21d349b2009-05-27 01:25:56 +00002987 Node->getOperand(0),
2988 Tmp1, ISD::SETLT);
2989 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Sanjay Patela2607012015-09-16 16:31:21 +00002990 // TODO: Should any fast-math-flags be set for the FSUB?
Bill Wendlingef408db2009-12-23 00:28:23 +00002991 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2992 DAG.getNode(ISD::FSUB, dl, VT,
2993 Node->getOperand(0), Tmp1));
Eli Friedman21d349b2009-05-27 01:25:56 +00002994 False = DAG.getNode(ISD::XOR, dl, NVT, False,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002995 DAG.getConstant(x, dl, NVT));
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002996 Tmp1 = DAG.getSelect(dl, NVT, Tmp2, True, False);
Eli Friedman21d349b2009-05-27 01:25:56 +00002997 Results.push_back(Tmp1);
2998 break;
2999 }
Charles Davis11952592015-08-25 23:27:41 +00003000 case ISD::VAARG:
3001 Results.push_back(DAG.expandVAArg(Node));
Eli Friedman3b251702009-05-27 07:58:35 +00003002 Results.push_back(Results[0].getValue(1));
3003 break;
Charles Davis11952592015-08-25 23:27:41 +00003004 case ISD::VACOPY:
3005 Results.push_back(DAG.expandVACopy(Node));
Eli Friedman21d349b2009-05-27 01:25:56 +00003006 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00003007 case ISD::EXTRACT_VECTOR_ELT:
3008 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3009 // This must be an access of the only element. Return it.
Wesley Peck527da1b2010-11-23 03:31:01 +00003010 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman21d349b2009-05-27 01:25:56 +00003011 Node->getOperand(0));
3012 else
3013 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3014 Results.push_back(Tmp1);
3015 break;
3016 case ISD::EXTRACT_SUBVECTOR:
Bill Wendlingef408db2009-12-23 00:28:23 +00003017 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman21d349b2009-05-27 01:25:56 +00003018 break;
David Greenebab5e6e2011-01-26 19:13:22 +00003019 case ISD::INSERT_SUBVECTOR:
3020 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3021 break;
Eli Friedman3b251702009-05-27 07:58:35 +00003022 case ISD::CONCAT_VECTORS: {
Bill Wendlingef408db2009-12-23 00:28:23 +00003023 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman3b251702009-05-27 07:58:35 +00003024 break;
3025 }
Eli Friedman21d349b2009-05-27 01:25:56 +00003026 case ISD::SCALAR_TO_VECTOR:
Bill Wendlingef408db2009-12-23 00:28:23 +00003027 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman21d349b2009-05-27 01:25:56 +00003028 break;
Eli Friedmana8f9a022009-05-27 02:16:40 +00003029 case ISD::INSERT_VECTOR_ELT:
Bill Wendlingef408db2009-12-23 00:28:23 +00003030 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3031 Node->getOperand(1),
3032 Node->getOperand(2), dl));
Eli Friedmana8f9a022009-05-27 02:16:40 +00003033 break;
Eli Friedman3b251702009-05-27 07:58:35 +00003034 case ISD::VECTOR_SHUFFLE: {
Benjamin Kramer339ced42012-01-15 13:16:05 +00003035 SmallVector<int, 32> NewMask;
3036 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
Eli Friedman3b251702009-05-27 07:58:35 +00003037
Owen Anderson53aa7a92009-08-10 22:56:29 +00003038 EVT VT = Node->getValueType(0);
3039 EVT EltVT = VT.getVectorElementType();
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003040 SDValue Op0 = Node->getOperand(0);
3041 SDValue Op1 = Node->getOperand(1);
3042 if (!TLI.isTypeLegal(EltVT)) {
3043
3044 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
3045
3046 // BUILD_VECTOR operands are allowed to be wider than the element type.
Jack Carter5c0af482013-11-19 23:43:22 +00003047 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
3048 // it.
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003049 if (NewEltVT.bitsLT(EltVT)) {
3050
3051 // Convert shuffle node.
3052 // If original node was v4i64 and the new EltVT is i32,
3053 // cast operands to v8i32 and re-build the mask.
3054
3055 // Calculate new VT, the size of the new VT should be equal to original.
Jack Carter5c0af482013-11-19 23:43:22 +00003056 EVT NewVT =
3057 EVT::getVectorVT(*DAG.getContext(), NewEltVT,
3058 VT.getSizeInBits() / NewEltVT.getSizeInBits());
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003059 assert(NewVT.bitsEq(VT));
3060
3061 // cast operands to new VT
3062 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
3063 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
3064
3065 // Convert the shuffle mask
Jack Carter5c0af482013-11-19 23:43:22 +00003066 unsigned int factor =
3067 NewVT.getVectorNumElements()/VT.getVectorNumElements();
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003068
3069 // EltVT gets smaller
3070 assert(factor > 0);
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003071
3072 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
3073 if (Mask[i] < 0) {
3074 for (unsigned fi = 0; fi < factor; ++fi)
3075 NewMask.push_back(Mask[i]);
3076 }
3077 else {
3078 for (unsigned fi = 0; fi < factor; ++fi)
3079 NewMask.push_back(Mask[i]*factor+fi);
3080 }
3081 }
3082 Mask = NewMask;
3083 VT = NewVT;
3084 }
3085 EltVT = NewEltVT;
3086 }
Eli Friedman3b251702009-05-27 07:58:35 +00003087 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003088 SmallVector<SDValue, 16> Ops;
Eli Friedman3b251702009-05-27 07:58:35 +00003089 for (unsigned i = 0; i != NumElems; ++i) {
3090 if (Mask[i] < 0) {
3091 Ops.push_back(DAG.getUNDEF(EltVT));
3092 continue;
3093 }
3094 unsigned Idx = Mask[i];
3095 if (Idx < NumElems)
Mehdi Amini44ede332015-07-09 02:09:04 +00003096 Ops.push_back(DAG.getNode(
3097 ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
3098 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
Eli Friedman3b251702009-05-27 07:58:35 +00003099 else
Mehdi Amini44ede332015-07-09 02:09:04 +00003100 Ops.push_back(DAG.getNode(
3101 ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
3102 DAG.getConstant(Idx - NumElems, dl,
3103 TLI.getVectorIdxTy(DAG.getDataLayout()))));
Eli Friedman3b251702009-05-27 07:58:35 +00003104 }
Nadav Rotem61bdf792012-01-10 14:28:46 +00003105
Craig Topper48d114b2014-04-26 18:35:24 +00003106 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
Nadav Rotem61bdf792012-01-10 14:28:46 +00003107 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
3108 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
Eli Friedman3b251702009-05-27 07:58:35 +00003109 Results.push_back(Tmp1);
3110 break;
3111 }
Eli Friedman21d349b2009-05-27 01:25:56 +00003112 case ISD::EXTRACT_ELEMENT: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003113 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman21d349b2009-05-27 01:25:56 +00003114 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3115 // 1 -> Hi
3116 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
Mehdi Amini9639d652015-07-09 02:09:20 +00003117 DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
3118 TLI.getShiftAmountTy(
3119 Node->getOperand(0).getValueType(),
3120 DAG.getDataLayout())));
Eli Friedman21d349b2009-05-27 01:25:56 +00003121 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3122 } else {
3123 // 0 -> Lo
3124 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3125 Node->getOperand(0));
3126 }
3127 Results.push_back(Tmp1);
3128 break;
3129 }
Eli Friedmana8f9a022009-05-27 02:16:40 +00003130 case ISD::STACKSAVE:
3131 // Expand to CopyFromReg if the target set
3132 // StackPointerRegisterToSaveRestore.
3133 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendlingef408db2009-12-23 00:28:23 +00003134 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3135 Node->getValueType(0)));
Eli Friedmana8f9a022009-05-27 02:16:40 +00003136 Results.push_back(Results[0].getValue(1));
3137 } else {
Bill Wendlingef408db2009-12-23 00:28:23 +00003138 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedmana8f9a022009-05-27 02:16:40 +00003139 Results.push_back(Node->getOperand(0));
3140 }
3141 break;
3142 case ISD::STACKRESTORE:
Bill Wendlingef408db2009-12-23 00:28:23 +00003143 // Expand to CopyToReg if the target set
3144 // StackPointerRegisterToSaveRestore.
3145 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3146 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3147 Node->getOperand(1)));
3148 } else {
3149 Results.push_back(Node->getOperand(0));
3150 }
Eli Friedmana8f9a022009-05-27 02:16:40 +00003151 break;
Yury Gribovd7dbb662015-12-01 11:40:55 +00003152 case ISD::GET_DYNAMIC_AREA_OFFSET:
3153 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3154 Results.push_back(Results[0].getValue(0));
3155 break;
Eli Friedman2892d822009-05-27 12:20:41 +00003156 case ISD::FCOPYSIGN:
Bill Wendlingef408db2009-12-23 00:28:23 +00003157 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman2892d822009-05-27 12:20:41 +00003158 break;
Eli Friedmand6f28342009-05-27 03:33:44 +00003159 case ISD::FNEG:
3160 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003161 Tmp1 = DAG.getConstantFP(-0.0, dl, Node->getValueType(0));
Sanjay Patela2607012015-09-16 16:31:21 +00003162 // TODO: If FNEG has fast-math-flags, propagate them to the FSUB.
Eli Friedmand6f28342009-05-27 03:33:44 +00003163 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3164 Node->getOperand(0));
3165 Results.push_back(Tmp1);
3166 break;
Matthias Braunb9610a62015-11-12 01:02:47 +00003167 case ISD::FABS:
3168 Results.push_back(ExpandFABS(Node));
Eli Friedmand6f28342009-05-27 03:33:44 +00003169 break;
James Molloy7e9776b2015-05-15 09:03:15 +00003170 case ISD::SMIN:
3171 case ISD::SMAX:
3172 case ISD::UMIN:
3173 case ISD::UMAX: {
3174 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
3175 ISD::CondCode Pred;
3176 switch (Node->getOpcode()) {
3177 default: llvm_unreachable("How did we get here?");
3178 case ISD::SMAX: Pred = ISD::SETGT; break;
3179 case ISD::SMIN: Pred = ISD::SETLT; break;
3180 case ISD::UMAX: Pred = ISD::SETUGT; break;
3181 case ISD::UMIN: Pred = ISD::SETULT; break;
3182 }
3183 Tmp1 = Node->getOperand(0);
3184 Tmp2 = Node->getOperand(1);
3185 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
3186 Results.push_back(Tmp1);
3187 break;
3188 }
Simon Pilgrimbb8a40f2016-06-21 14:37:39 +00003189
Eli Friedmand6f28342009-05-27 03:33:44 +00003190 case ISD::FSIN:
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003191 case ISD::FCOS: {
3192 EVT VT = Node->getValueType(0);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003193 // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
3194 // fcos which share the same operand and both are used.
3195 if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
Paul Redmondf29ddfe2013-02-15 18:45:18 +00003196 canCombineSinCosLibcall(Node, TLI, TM))
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003197 && useSinCos(Node)) {
3198 SDVTList VTs = DAG.getVTList(VT, VT);
3199 Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003200 if (Node->getOpcode() == ISD::FCOS)
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003201 Tmp1 = Tmp1.getValue(1);
3202 Results.push_back(Tmp1);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003203 }
Eli Friedmand6f28342009-05-27 03:33:44 +00003204 break;
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003205 }
Matt Arsenault0dc54c42015-02-20 22:10:33 +00003206 case ISD::FMAD:
3207 llvm_unreachable("Illegal fmad should never be formed");
3208
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003209 case ISD::FP16_TO_FP:
3210 if (Node->getValueType(0) != MVT::f32) {
3211 // We can extend to types bigger than f32 in two steps without changing
3212 // the result. Since "f16 -> f32" is much more commonly available, give
3213 // CodeGen the option of emitting that before resorting to a libcall.
3214 SDValue Res =
3215 DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
3216 Results.push_back(
3217 DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
Tim Northoverfd7e4242014-07-17 10:51:23 +00003218 }
Anton Korobeynikov59e96002010-03-14 18:42:24 +00003219 break;
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003220 case ISD::FP_TO_FP16:
Eric Christopher824f42f2015-05-12 01:26:05 +00003221 if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
Andrea Di Biagioaf3f3972015-02-23 22:59:02 +00003222 SDValue Op = Node->getOperand(0);
3223 MVT SVT = Op.getSimpleValueType();
3224 if ((SVT == MVT::f64 || SVT == MVT::f80) &&
3225 TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
3226 // Under fastmath, we can expand this node into a fround followed by
3227 // a float-half conversion.
3228 SDValue FloatVal = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003229 DAG.getIntPtrConstant(0, dl));
Andrea Di Biagioaf3f3972015-02-23 22:59:02 +00003230 Results.push_back(
Ahmed Bougacha60b201b62016-01-14 19:45:36 +00003231 DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
Andrea Di Biagioaf3f3972015-02-23 22:59:02 +00003232 }
3233 }
Anton Korobeynikov59e96002010-03-14 18:42:24 +00003234 break;
Eli Friedman0e494312009-05-27 07:32:27 +00003235 case ISD::ConstantFP: {
3236 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendlingef408db2009-12-23 00:28:23 +00003237 // Check to see if this FP immediate is already legal.
3238 // If this is a legal constant, turn it into a TargetConstantFP node.
Dan Gohman198b7ff2011-11-03 21:49:52 +00003239 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
3240 Results.push_back(ExpandConstantFP(CFP, true));
Eli Friedman0e494312009-05-27 07:32:27 +00003241 break;
3242 }
Chih-Hung Hsiehed7d81e2015-12-03 22:02:40 +00003243 case ISD::Constant: {
3244 ConstantSDNode *CP = cast<ConstantSDNode>(Node);
3245 Results.push_back(ExpandConstant(CP));
3246 break;
3247 }
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00003248 case ISD::FSUB: {
3249 EVT VT = Node->getValueType(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003250 if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
3251 TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
Sanjay Patela2607012015-09-16 16:31:21 +00003252 const SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(Node)->Flags;
Oliver Stannard51b1d462014-08-21 12:50:31 +00003253 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
Sanjay Patela2607012015-09-16 16:31:21 +00003254 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003255 Results.push_back(Tmp1);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003256 }
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00003257 break;
3258 }
Eli Friedman56883962009-05-27 07:05:37 +00003259 case ISD::SUB: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003260 EVT VT = Node->getValueType(0);
Eli Friedman56883962009-05-27 07:05:37 +00003261 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3262 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3263 "Don't know how to expand this subtraction!");
3264 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003265 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
3266 VT));
3267 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
Bill Wendlingef408db2009-12-23 00:28:23 +00003268 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman56883962009-05-27 07:05:37 +00003269 break;
3270 }
Eli Friedman0e494312009-05-27 07:32:27 +00003271 case ISD::UREM:
3272 case ISD::SREM: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003273 EVT VT = Node->getValueType(0);
Eli Friedman0e494312009-05-27 07:32:27 +00003274 bool isSigned = Node->getOpcode() == ISD::SREM;
3275 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3276 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3277 Tmp2 = Node->getOperand(0);
3278 Tmp3 = Node->getOperand(1);
Artyom Skrobovb844fa72015-10-20 13:06:02 +00003279 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003280 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmane1bc3792009-05-28 03:06:16 +00003281 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003282 Results.push_back(Tmp1);
Eli Friedmane1bc3792009-05-28 03:06:16 +00003283 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedman0e494312009-05-27 07:32:27 +00003284 // X % Y -> X-X/Y*Y
3285 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3286 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3287 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003288 Results.push_back(Tmp1);
3289 }
Eli Friedman56883962009-05-27 07:05:37 +00003290 break;
3291 }
Eli Friedman0e494312009-05-27 07:32:27 +00003292 case ISD::UDIV:
3293 case ISD::SDIV: {
3294 bool isSigned = Node->getOpcode() == ISD::SDIV;
3295 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003296 EVT VT = Node->getValueType(0);
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003297 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3298 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedman0e494312009-05-27 07:32:27 +00003299 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3300 Node->getOperand(1));
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003301 Results.push_back(Tmp1);
3302 }
Eli Friedman56883962009-05-27 07:05:37 +00003303 break;
3304 }
3305 case ISD::MULHU:
3306 case ISD::MULHS: {
3307 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3308 ISD::SMUL_LOHI;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003309 EVT VT = Node->getValueType(0);
Eli Friedman56883962009-05-27 07:05:37 +00003310 SDVTList VTs = DAG.getVTList(VT, VT);
3311 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3312 "If this wasn't legal, it shouldn't have been created!");
3313 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3314 Node->getOperand(1));
3315 Results.push_back(Tmp1.getValue(1));
3316 break;
3317 }
3318 case ISD::MUL: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003319 EVT VT = Node->getValueType(0);
Eli Friedman56883962009-05-27 07:05:37 +00003320 SDVTList VTs = DAG.getVTList(VT, VT);
3321 // See if multiply or divide can be lowered using two-result operations.
3322 // We just need the low half of the multiply; try both the signed
3323 // and unsigned forms. If the target supports both SMUL_LOHI and
3324 // UMUL_LOHI, form a preference by checking which forms of plain
3325 // MULH it supports.
3326 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3327 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3328 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3329 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3330 unsigned OpToUse = 0;
3331 if (HasSMUL_LOHI && !HasMULHS) {
3332 OpToUse = ISD::SMUL_LOHI;
3333 } else if (HasUMUL_LOHI && !HasMULHU) {
3334 OpToUse = ISD::UMUL_LOHI;
3335 } else if (HasSMUL_LOHI) {
3336 OpToUse = ISD::SMUL_LOHI;
3337 } else if (HasUMUL_LOHI) {
3338 OpToUse = ISD::UMUL_LOHI;
3339 }
3340 if (OpToUse) {
Bill Wendlingef408db2009-12-23 00:28:23 +00003341 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3342 Node->getOperand(1)));
Eli Friedman56883962009-05-27 07:05:37 +00003343 break;
3344 }
Tom Stellarda1a5d9a2014-04-11 16:12:01 +00003345
3346 SDValue Lo, Hi;
3347 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
3348 if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
3349 TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
3350 TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
3351 TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
3352 TLI.expandMUL(Node, Lo, Hi, HalfType, DAG)) {
3353 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
3354 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
Mehdi Amini9639d652015-07-09 02:09:20 +00003355 SDValue Shift =
3356 DAG.getConstant(HalfType.getSizeInBits(), dl,
3357 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
Tom Stellarda1a5d9a2014-04-11 16:12:01 +00003358 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3359 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
Tom Stellarda1a5d9a2014-04-11 16:12:01 +00003360 }
Eli Friedman56883962009-05-27 07:05:37 +00003361 break;
3362 }
Eli Friedman2892d822009-05-27 12:20:41 +00003363 case ISD::SADDO:
3364 case ISD::SSUBO: {
3365 SDValue LHS = Node->getOperand(0);
3366 SDValue RHS = Node->getOperand(1);
3367 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3368 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3369 LHS, RHS);
3370 Results.push_back(Sum);
Matt Arsenault3ee37462014-05-28 20:51:42 +00003371 EVT ResultType = Node->getValueType(1);
3372 EVT OType = getSetCCResultType(Node->getValueType(0));
Bill Wendlingef408db2009-12-23 00:28:23 +00003373
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003374 SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
Eli Friedman2892d822009-05-27 12:20:41 +00003375
3376 // LHSSign -> LHS >= 0
3377 // RHSSign -> RHS >= 0
3378 // SumSign -> Sum >= 0
3379 //
3380 // Add:
3381 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3382 // Sub:
3383 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3384 //
3385 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3386 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3387 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3388 Node->getOpcode() == ISD::SADDO ?
3389 ISD::SETEQ : ISD::SETNE);
3390
3391 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3392 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3393
3394 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Daniel Sanderscbd44c52014-07-10 10:18:12 +00003395 Results.push_back(DAG.getBoolExtOrTrunc(Cmp, dl, ResultType, ResultType));
Eli Friedman2892d822009-05-27 12:20:41 +00003396 break;
3397 }
3398 case ISD::UADDO:
3399 case ISD::USUBO: {
3400 SDValue LHS = Node->getOperand(0);
3401 SDValue RHS = Node->getOperand(1);
3402 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3403 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3404 LHS, RHS);
3405 Results.push_back(Sum);
Matt Arsenault3ee37462014-05-28 20:51:42 +00003406
3407 EVT ResultType = Node->getValueType(1);
3408 EVT SetCCType = getSetCCResultType(Node->getValueType(0));
3409 ISD::CondCode CC
3410 = Node->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT;
3411 SDValue SetCC = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
3412
Daniel Sanderscbd44c52014-07-10 10:18:12 +00003413 Results.push_back(DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType));
Eli Friedman2892d822009-05-27 12:20:41 +00003414 break;
3415 }
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003416 case ISD::UMULO:
3417 case ISD::SMULO: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003418 EVT VT = Node->getValueType(0);
Eric Christopherbcaedb52011-04-20 01:19:45 +00003419 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003420 SDValue LHS = Node->getOperand(0);
3421 SDValue RHS = Node->getOperand(1);
3422 SDValue BottomHalf;
3423 SDValue TopHalf;
Nuno Lopes129819d2009-12-23 17:48:10 +00003424 static const unsigned Ops[2][3] =
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003425 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3426 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3427 bool isSigned = Node->getOpcode() == ISD::SMULO;
3428 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3429 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3430 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3431 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3432 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3433 RHS);
3434 TopHalf = BottomHalf.getValue(1);
Eric Christopher83dd2fa2014-04-28 22:24:57 +00003435 } else if (TLI.isTypeLegal(WideVT)) {
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003436 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3437 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3438 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3439 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003440 DAG.getIntPtrConstant(0, dl));
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003441 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003442 DAG.getIntPtrConstant(1, dl));
Eric Christopherbb14f652011-01-20 00:29:24 +00003443 } else {
3444 // We can fall back to a libcall with an illegal type for the MUL if we
3445 // have a libcall big enough.
3446 // Also, we can fall back to a division in some cases, but that's a big
3447 // performance hit in the general case.
Eric Christopherbb14f652011-01-20 00:29:24 +00003448 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3449 if (WideVT == MVT::i16)
3450 LC = RTLIB::MUL_I16;
3451 else if (WideVT == MVT::i32)
3452 LC = RTLIB::MUL_I32;
3453 else if (WideVT == MVT::i64)
3454 LC = RTLIB::MUL_I64;
3455 else if (WideVT == MVT::i128)
3456 LC = RTLIB::MUL_I128;
3457 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanae9b1682011-05-16 22:09:53 +00003458
3459 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherbcaedb52011-04-20 01:19:45 +00003460 // part.
3461 unsigned LoSize = VT.getSizeInBits();
Mehdi Amini44ede332015-07-09 02:09:04 +00003462 SDValue HiLHS =
3463 DAG.getNode(ISD::SRA, dl, VT, RHS,
3464 DAG.getConstant(LoSize - 1, dl,
3465 TLI.getPointerTy(DAG.getDataLayout())));
3466 SDValue HiRHS =
3467 DAG.getNode(ISD::SRA, dl, VT, LHS,
3468 DAG.getConstant(LoSize - 1, dl,
3469 TLI.getPointerTy(DAG.getDataLayout())));
Owen Andersonb2c80da2011-02-25 21:41:48 +00003470
Eric Christopherbcaedb52011-04-20 01:19:45 +00003471 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3472 // pre-lowered to the correct types. This all depends upon WideVT not
3473 // being a legal type for the architecture and thus has to be split to
3474 // two arguments.
3475 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3476 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3477 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003478 DAG.getIntPtrConstant(0, dl));
Eric Christopherbcaedb52011-04-20 01:19:45 +00003479 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003480 DAG.getIntPtrConstant(1, dl));
Dan Gohman198b7ff2011-11-03 21:49:52 +00003481 // Ret is a node with an illegal type. Because such things are not
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00003482 // generally permitted during this phase of legalization, make sure the
3483 // node has no more uses. The above EXTRACT_ELEMENT nodes should have been
3484 // folded.
3485 assert(Ret->use_empty() &&
3486 "Unexpected uses of illegally type from expanded lib call.");
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003487 }
Dan Gohmanae9b1682011-05-16 22:09:53 +00003488
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003489 if (isSigned) {
Mehdi Amini9639d652015-07-09 02:09:20 +00003490 Tmp1 = DAG.getConstant(
3491 VT.getSizeInBits() - 1, dl,
3492 TLI.getShiftAmountTy(BottomHalf.getValueType(), DAG.getDataLayout()));
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003493 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
Matt Arsenault758659232013-05-18 00:21:46 +00003494 TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf, Tmp1,
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003495 ISD::SETNE);
3496 } else {
Matt Arsenault758659232013-05-18 00:21:46 +00003497 TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003498 DAG.getConstant(0, dl, VT), ISD::SETNE);
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003499 }
3500 Results.push_back(BottomHalf);
3501 Results.push_back(TopHalf);
3502 break;
3503 }
Eli Friedman0e494312009-05-27 07:32:27 +00003504 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003505 EVT PairTy = Node->getValueType(0);
Eli Friedman0e494312009-05-27 07:32:27 +00003506 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3507 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Mehdi Amini9639d652015-07-09 02:09:20 +00003508 Tmp2 = DAG.getNode(
3509 ISD::SHL, dl, PairTy, Tmp2,
3510 DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
3511 TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
Bill Wendlingef408db2009-12-23 00:28:23 +00003512 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedman0e494312009-05-27 07:32:27 +00003513 break;
3514 }
Eli Friedman3b251702009-05-27 07:58:35 +00003515 case ISD::SELECT:
3516 Tmp1 = Node->getOperand(0);
3517 Tmp2 = Node->getOperand(1);
3518 Tmp3 = Node->getOperand(2);
Bill Wendlingef408db2009-12-23 00:28:23 +00003519 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman3b251702009-05-27 07:58:35 +00003520 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3521 Tmp2, Tmp3,
3522 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendlingef408db2009-12-23 00:28:23 +00003523 } else {
Eli Friedman3b251702009-05-27 07:58:35 +00003524 Tmp1 = DAG.getSelectCC(dl, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003525 DAG.getConstant(0, dl, Tmp1.getValueType()),
Eli Friedman3b251702009-05-27 07:58:35 +00003526 Tmp2, Tmp3, ISD::SETNE);
Bill Wendlingef408db2009-12-23 00:28:23 +00003527 }
Eli Friedman3b251702009-05-27 07:58:35 +00003528 Results.push_back(Tmp1);
3529 break;
Eli Friedman2892d822009-05-27 12:20:41 +00003530 case ISD::BR_JT: {
3531 SDValue Chain = Node->getOperand(0);
3532 SDValue Table = Node->getOperand(1);
3533 SDValue Index = Node->getOperand(2);
3534
Mehdi Amini44ede332015-07-09 02:09:04 +00003535 EVT PTy = TLI.getPointerTy(DAG.getDataLayout());
Chris Lattnerb6db2c62010-01-25 23:26:13 +00003536
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00003537 const DataLayout &TD = DAG.getDataLayout();
Chris Lattnerb6db2c62010-01-25 23:26:13 +00003538 unsigned EntrySize =
3539 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach9b7755f2010-07-02 17:41:59 +00003540
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003541 Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
3542 DAG.getConstant(EntrySize, dl, Index.getValueType()));
Tom Stellard838e2342013-08-26 15:06:10 +00003543 SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
3544 Index, Table);
Eli Friedman2892d822009-05-27 12:20:41 +00003545
Owen Anderson117c9e82009-08-12 00:36:31 +00003546 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003547 SDValue LD = DAG.getExtLoad(
3548 ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Justin Lebar9c375812016-07-15 18:27:10 +00003549 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT);
Eli Friedman2892d822009-05-27 12:20:41 +00003550 Addr = LD;
Rafael Espindolab1556c42016-06-28 20:13:36 +00003551 if (TM.isPositionIndependent()) {
Eli Friedman2892d822009-05-27 12:20:41 +00003552 // For PIC, the sequence is:
Bill Wendlingef408db2009-12-23 00:28:23 +00003553 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman2892d822009-05-27 12:20:41 +00003554 // RelocBase can be JumpTable, GOT or some sort of global base.
3555 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3556 TLI.getPICJumpTableRelocBase(Table, DAG));
3557 }
Owen Anderson9f944592009-08-11 20:47:22 +00003558 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman2892d822009-05-27 12:20:41 +00003559 Results.push_back(Tmp1);
3560 break;
3561 }
Eli Friedman0e494312009-05-27 07:32:27 +00003562 case ISD::BRCOND:
3563 // Expand brcond's setcc into its constituent parts and create a BR_CC
3564 // Node.
3565 Tmp1 = Node->getOperand(0);
3566 Tmp2 = Node->getOperand(1);
Bill Wendlingef408db2009-12-23 00:28:23 +00003567 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson9f944592009-08-11 20:47:22 +00003568 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedman0e494312009-05-27 07:32:27 +00003569 Tmp1, Tmp2.getOperand(2),
3570 Tmp2.getOperand(0), Tmp2.getOperand(1),
3571 Node->getOperand(2));
Bill Wendlingef408db2009-12-23 00:28:23 +00003572 } else {
Stuart Hastingsaa02c082011-05-13 00:51:54 +00003573 // We test only the i1 bit. Skip the AND if UNDEF.
Sanjay Patel57195842016-03-14 17:28:46 +00003574 Tmp3 = (Tmp2.isUndef()) ? Tmp2 :
Stuart Hastingsaa02c082011-05-13 00:51:54 +00003575 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003576 DAG.getConstant(1, dl, Tmp2.getValueType()));
Owen Anderson9f944592009-08-11 20:47:22 +00003577 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastingsaa02c082011-05-13 00:51:54 +00003578 DAG.getCondCode(ISD::SETNE), Tmp3,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003579 DAG.getConstant(0, dl, Tmp3.getValueType()),
Eli Friedman0e494312009-05-27 07:32:27 +00003580 Node->getOperand(2));
Bill Wendlingef408db2009-12-23 00:28:23 +00003581 }
Eli Friedman0e494312009-05-27 07:32:27 +00003582 Results.push_back(Tmp1);
3583 break;
Eli Friedman5df72022009-05-28 03:56:57 +00003584 case ISD::SETCC: {
3585 Tmp1 = Node->getOperand(0);
3586 Tmp2 = Node->getOperand(1);
3587 Tmp3 = Node->getOperand(2);
Tom Stellard08690a12013-09-28 02:50:32 +00003588 bool Legalized = LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2,
Daniel Sandersedc071b2013-11-21 13:24:49 +00003589 Tmp3, NeedInvert, dl);
Eli Friedman5df72022009-05-28 03:56:57 +00003590
Tom Stellard08690a12013-09-28 02:50:32 +00003591 if (Legalized) {
Daniel Sandersedc071b2013-11-21 13:24:49 +00003592 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
3593 // condition code, create a new SETCC node.
Tom Stellard08690a12013-09-28 02:50:32 +00003594 if (Tmp3.getNode())
3595 Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3596 Tmp1, Tmp2, Tmp3);
3597
Daniel Sandersedc071b2013-11-21 13:24:49 +00003598 // If we expanded the SETCC by inverting the condition code, then wrap
3599 // the existing SETCC in a NOT to restore the intended condition.
3600 if (NeedInvert)
Pete Cooper7fd1d722014-05-12 23:26:58 +00003601 Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
Daniel Sandersedc071b2013-11-21 13:24:49 +00003602
Eli Friedman5df72022009-05-28 03:56:57 +00003603 Results.push_back(Tmp1);
3604 break;
3605 }
3606
3607 // Otherwise, SETCC for the given comparison type must be completely
3608 // illegal; expand it into a SELECT_CC.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003609 EVT VT = Node->getValueType(0);
Tom Stellardd93ef7a2013-03-08 15:37:02 +00003610 int TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00003611 switch (TLI.getBooleanContents(Tmp1->getValueType(0))) {
Tom Stellardd93ef7a2013-03-08 15:37:02 +00003612 case TargetLowering::ZeroOrOneBooleanContent:
3613 case TargetLowering::UndefinedBooleanContent:
3614 TrueValue = 1;
3615 break;
3616 case TargetLowering::ZeroOrNegativeOneBooleanContent:
3617 TrueValue = -1;
3618 break;
3619 }
Eli Friedman5df72022009-05-28 03:56:57 +00003620 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003621 DAG.getConstant(TrueValue, dl, VT),
3622 DAG.getConstant(0, dl, VT),
Tom Stellardd93ef7a2013-03-08 15:37:02 +00003623 Tmp3);
Eli Friedman5df72022009-05-28 03:56:57 +00003624 Results.push_back(Tmp1);
3625 break;
3626 }
Eli Friedmane1dc1932009-05-28 20:40:34 +00003627 case ISD::SELECT_CC: {
3628 Tmp1 = Node->getOperand(0); // LHS
3629 Tmp2 = Node->getOperand(1); // RHS
3630 Tmp3 = Node->getOperand(2); // True
3631 Tmp4 = Node->getOperand(3); // False
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003632 EVT VT = Node->getValueType(0);
Eli Friedmane1dc1932009-05-28 20:40:34 +00003633 SDValue CC = Node->getOperand(4);
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003634 ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
Eli Friedmane1dc1932009-05-28 20:40:34 +00003635
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003636 if (TLI.isCondCodeLegal(CCOp, Tmp1.getSimpleValueType())) {
3637 // If the condition code is legal, then we need to expand this
3638 // node using SETCC and SELECT.
3639 EVT CmpVT = Tmp1.getValueType();
3640 assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
3641 "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
3642 "expanded.");
Mehdi Amini44ede332015-07-09 02:09:04 +00003643 EVT CCVT =
3644 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), CmpVT);
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003645 SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC);
3646 Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4));
3647 break;
3648 }
3649
3650 // SELECT_CC is legal, so the condition code must not be.
Tom Stellard5694d302013-09-28 02:50:43 +00003651 bool Legalized = false;
3652 // Try to legalize by inverting the condition. This is for targets that
3653 // might support an ordered version of a condition, but not the unordered
3654 // version (or vice versa).
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003655 ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp,
Tom Stellard5694d302013-09-28 02:50:43 +00003656 Tmp1.getValueType().isInteger());
3657 if (TLI.isCondCodeLegal(InvCC, Tmp1.getSimpleValueType())) {
3658 // Use the new condition code and swap true and false
3659 Legalized = true;
3660 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
Tom Stellard08690a12013-09-28 02:50:32 +00003661 } else {
Tom Stellard5694d302013-09-28 02:50:43 +00003662 // If The inverse is not legal, then try to swap the arguments using
3663 // the inverse condition code.
3664 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC);
3665 if (TLI.isCondCodeLegal(SwapInvCC, Tmp1.getSimpleValueType())) {
3666 // The swapped inverse condition is legal, so swap true and false,
3667 // lhs and rhs.
3668 Legalized = true;
3669 Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
3670 }
3671 }
3672
3673 if (!Legalized) {
3674 Legalized = LegalizeSetCCCondCode(
Daniel Sandersedc071b2013-11-21 13:24:49 +00003675 getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, NeedInvert,
3676 dl);
Tom Stellard5694d302013-09-28 02:50:43 +00003677
3678 assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
Daniel Sandersedc071b2013-11-21 13:24:49 +00003679
3680 // If we expanded the SETCC by inverting the condition code, then swap
3681 // the True/False operands to match.
3682 if (NeedInvert)
3683 std::swap(Tmp3, Tmp4);
3684
3685 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
3686 // condition code, create a new SELECT_CC node.
Tom Stellard5694d302013-09-28 02:50:43 +00003687 if (CC.getNode()) {
3688 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
3689 Tmp1, Tmp2, Tmp3, Tmp4, CC);
3690 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003691 Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
Tom Stellard5694d302013-09-28 02:50:43 +00003692 CC = DAG.getCondCode(ISD::SETNE);
Jack Carter5c0af482013-11-19 23:43:22 +00003693 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
3694 Tmp2, Tmp3, Tmp4, CC);
Tom Stellard5694d302013-09-28 02:50:43 +00003695 }
Tom Stellard08690a12013-09-28 02:50:32 +00003696 }
Eli Friedmane1dc1932009-05-28 20:40:34 +00003697 Results.push_back(Tmp1);
3698 break;
3699 }
3700 case ISD::BR_CC: {
3701 Tmp1 = Node->getOperand(0); // Chain
3702 Tmp2 = Node->getOperand(2); // LHS
3703 Tmp3 = Node->getOperand(3); // RHS
3704 Tmp4 = Node->getOperand(1); // CC
3705
Tom Stellard08690a12013-09-28 02:50:32 +00003706 bool Legalized = LegalizeSetCCCondCode(getSetCCResultType(
Daniel Sandersedc071b2013-11-21 13:24:49 +00003707 Tmp2.getValueType()), Tmp2, Tmp3, Tmp4, NeedInvert, dl);
Tom Stellard45015d92013-09-28 03:10:17 +00003708 (void)Legalized;
Tom Stellard08690a12013-09-28 02:50:32 +00003709 assert(Legalized && "Can't legalize BR_CC with legal condition!");
Eli Friedmane1dc1932009-05-28 20:40:34 +00003710
Daniel Sandersedc071b2013-11-21 13:24:49 +00003711 // If we expanded the SETCC by inverting the condition code, then wrap
3712 // the existing SETCC in a NOT to restore the intended condition.
3713 if (NeedInvert)
3714 Tmp4 = DAG.getNOT(dl, Tmp4, Tmp4->getValueType(0));
3715
3716 // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
Tom Stellard08690a12013-09-28 02:50:32 +00003717 // node.
3718 if (Tmp4.getNode()) {
3719 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
3720 Tmp4, Tmp2, Tmp3, Node->getOperand(4));
3721 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003722 Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
Tom Stellard08690a12013-09-28 02:50:32 +00003723 Tmp4 = DAG.getCondCode(ISD::SETNE);
Jack Carter5c0af482013-11-19 23:43:22 +00003724 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
3725 Tmp2, Tmp3, Node->getOperand(4));
Tom Stellard08690a12013-09-28 02:50:32 +00003726 }
Eli Friedmane1dc1932009-05-28 20:40:34 +00003727 Results.push_back(Tmp1);
3728 break;
3729 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00003730 case ISD::BUILD_VECTOR:
3731 Results.push_back(ExpandBUILD_VECTOR(Node));
3732 break;
3733 case ISD::SRA:
3734 case ISD::SRL:
3735 case ISD::SHL: {
3736 // Scalarize vector SRA/SRL/SHL.
3737 EVT VT = Node->getValueType(0);
3738 assert(VT.isVector() && "Unable to legalize non-vector shift");
3739 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3740 unsigned NumElem = VT.getVectorNumElements();
3741
3742 SmallVector<SDValue, 8> Scalars;
3743 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003744 SDValue Ex = DAG.getNode(
3745 ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(0),
3746 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3747 SDValue Sh = DAG.getNode(
3748 ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(1),
3749 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
Dan Gohman198b7ff2011-11-03 21:49:52 +00003750 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3751 VT.getScalarType(), Ex, Sh));
3752 }
3753 SDValue Result =
Craig Topper48d114b2014-04-26 18:35:24 +00003754 DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), Scalars);
Eli Friedman13477152011-11-11 23:58:27 +00003755 ReplaceNode(SDValue(Node, 0), Result);
Dan Gohman198b7ff2011-11-03 21:49:52 +00003756 break;
3757 }
Eli Friedmana8f9a022009-05-27 02:16:40 +00003758 case ISD::GLOBAL_OFFSET_TABLE:
3759 case ISD::GlobalAddress:
3760 case ISD::GlobalTLSAddress:
3761 case ISD::ExternalSymbol:
3762 case ISD::ConstantPool:
3763 case ISD::JumpTable:
3764 case ISD::INTRINSIC_W_CHAIN:
3765 case ISD::INTRINSIC_WO_CHAIN:
3766 case ISD::INTRINSIC_VOID:
3767 // FIXME: Custom lowering for these operations shouldn't return null!
Eli Friedmana8f9a022009-05-27 02:16:40 +00003768 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00003769 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00003770
3771 // Replace the original node with the legalized result.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003772 if (Results.empty())
3773 return false;
3774
3775 ReplaceNode(Node, Results.data());
3776 return true;
3777}
3778
3779void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
3780 SmallVector<SDValue, 8> Results;
3781 SDLoc dl(Node);
3782 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
3783 unsigned Opc = Node->getOpcode();
3784 switch (Opc) {
3785 case ISD::ATOMIC_FENCE: {
3786 // If the target didn't lower this, lower it to '__sync_synchronize()' call
3787 // FIXME: handle "fence singlethread" more efficiently.
3788 TargetLowering::ArgListTy Args;
3789
3790 TargetLowering::CallLoweringInfo CLI(DAG);
3791 CLI.setDebugLoc(dl)
3792 .setChain(Node->getOperand(0))
3793 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
3794 DAG.getExternalSymbol("__sync_synchronize",
3795 TLI.getPointerTy(DAG.getDataLayout())),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00003796 std::move(Args));
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003797
3798 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
3799
3800 Results.push_back(CallResult.second);
3801 break;
3802 }
3803 // By default, atomic intrinsics are marked Legal and lowered. Targets
3804 // which don't support them directly, however, may want libcalls, in which
3805 // case they mark them Expand, and we get here.
3806 case ISD::ATOMIC_SWAP:
3807 case ISD::ATOMIC_LOAD_ADD:
3808 case ISD::ATOMIC_LOAD_SUB:
3809 case ISD::ATOMIC_LOAD_AND:
3810 case ISD::ATOMIC_LOAD_OR:
3811 case ISD::ATOMIC_LOAD_XOR:
3812 case ISD::ATOMIC_LOAD_NAND:
3813 case ISD::ATOMIC_LOAD_MIN:
3814 case ISD::ATOMIC_LOAD_MAX:
3815 case ISD::ATOMIC_LOAD_UMIN:
3816 case ISD::ATOMIC_LOAD_UMAX:
3817 case ISD::ATOMIC_CMP_SWAP: {
3818 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
James Y Knightf44fc522016-03-16 22:12:04 +00003819 RTLIB::Libcall LC = RTLIB::getSYNC(Opc, VT);
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003820 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
3821
3822 std::pair<SDValue, SDValue> Tmp = ExpandChainLibCall(LC, Node, false);
3823 Results.push_back(Tmp.first);
3824 Results.push_back(Tmp.second);
3825 break;
3826 }
3827 case ISD::TRAP: {
3828 // If this operation is not supported, lower it to 'abort()' call
3829 TargetLowering::ArgListTy Args;
3830 TargetLowering::CallLoweringInfo CLI(DAG);
3831 CLI.setDebugLoc(dl)
3832 .setChain(Node->getOperand(0))
3833 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
3834 DAG.getExternalSymbol("abort",
3835 TLI.getPointerTy(DAG.getDataLayout())),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00003836 std::move(Args));
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003837 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
3838
3839 Results.push_back(CallResult.second);
3840 break;
3841 }
3842 case ISD::FMINNUM:
3843 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
3844 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
3845 RTLIB::FMIN_PPCF128));
3846 break;
3847 case ISD::FMAXNUM:
3848 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
3849 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
3850 RTLIB::FMAX_PPCF128));
3851 break;
3852 case ISD::FSQRT:
3853 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3854 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
3855 RTLIB::SQRT_PPCF128));
3856 break;
3857 case ISD::FSIN:
3858 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3859 RTLIB::SIN_F80, RTLIB::SIN_F128,
3860 RTLIB::SIN_PPCF128));
3861 break;
3862 case ISD::FCOS:
3863 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3864 RTLIB::COS_F80, RTLIB::COS_F128,
3865 RTLIB::COS_PPCF128));
3866 break;
3867 case ISD::FSINCOS:
3868 // Expand into sincos libcall.
3869 ExpandSinCosLibCall(Node, Results);
3870 break;
3871 case ISD::FLOG:
3872 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3873 RTLIB::LOG_F80, RTLIB::LOG_F128,
3874 RTLIB::LOG_PPCF128));
3875 break;
3876 case ISD::FLOG2:
3877 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3878 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
3879 RTLIB::LOG2_PPCF128));
3880 break;
3881 case ISD::FLOG10:
3882 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3883 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
3884 RTLIB::LOG10_PPCF128));
3885 break;
3886 case ISD::FEXP:
3887 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3888 RTLIB::EXP_F80, RTLIB::EXP_F128,
3889 RTLIB::EXP_PPCF128));
3890 break;
3891 case ISD::FEXP2:
3892 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3893 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
3894 RTLIB::EXP2_PPCF128));
3895 break;
3896 case ISD::FTRUNC:
3897 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3898 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
3899 RTLIB::TRUNC_PPCF128));
3900 break;
3901 case ISD::FFLOOR:
3902 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3903 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
3904 RTLIB::FLOOR_PPCF128));
3905 break;
3906 case ISD::FCEIL:
3907 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3908 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
3909 RTLIB::CEIL_PPCF128));
3910 break;
3911 case ISD::FRINT:
3912 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3913 RTLIB::RINT_F80, RTLIB::RINT_F128,
3914 RTLIB::RINT_PPCF128));
3915 break;
3916 case ISD::FNEARBYINT:
3917 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3918 RTLIB::NEARBYINT_F64,
3919 RTLIB::NEARBYINT_F80,
3920 RTLIB::NEARBYINT_F128,
3921 RTLIB::NEARBYINT_PPCF128));
3922 break;
3923 case ISD::FROUND:
3924 Results.push_back(ExpandFPLibCall(Node, RTLIB::ROUND_F32,
3925 RTLIB::ROUND_F64,
3926 RTLIB::ROUND_F80,
3927 RTLIB::ROUND_F128,
3928 RTLIB::ROUND_PPCF128));
3929 break;
3930 case ISD::FPOWI:
3931 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3932 RTLIB::POWI_F80, RTLIB::POWI_F128,
3933 RTLIB::POWI_PPCF128));
3934 break;
3935 case ISD::FPOW:
3936 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3937 RTLIB::POW_F80, RTLIB::POW_F128,
3938 RTLIB::POW_PPCF128));
3939 break;
3940 case ISD::FDIV:
3941 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3942 RTLIB::DIV_F80, RTLIB::DIV_F128,
3943 RTLIB::DIV_PPCF128));
3944 break;
3945 case ISD::FREM:
3946 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3947 RTLIB::REM_F80, RTLIB::REM_F128,
3948 RTLIB::REM_PPCF128));
3949 break;
3950 case ISD::FMA:
3951 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
3952 RTLIB::FMA_F80, RTLIB::FMA_F128,
3953 RTLIB::FMA_PPCF128));
3954 break;
3955 case ISD::FADD:
3956 Results.push_back(ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
3957 RTLIB::ADD_F80, RTLIB::ADD_F128,
3958 RTLIB::ADD_PPCF128));
3959 break;
3960 case ISD::FMUL:
3961 Results.push_back(ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
3962 RTLIB::MUL_F80, RTLIB::MUL_F128,
3963 RTLIB::MUL_PPCF128));
3964 break;
3965 case ISD::FP16_TO_FP:
3966 if (Node->getValueType(0) == MVT::f32) {
3967 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3968 }
3969 break;
3970 case ISD::FP_TO_FP16: {
3971 RTLIB::Libcall LC =
3972 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
3973 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
3974 Results.push_back(ExpandLibCall(LC, Node, false));
3975 break;
3976 }
3977 case ISD::FSUB:
3978 Results.push_back(ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
3979 RTLIB::SUB_F80, RTLIB::SUB_F128,
3980 RTLIB::SUB_PPCF128));
3981 break;
3982 case ISD::SREM:
3983 Results.push_back(ExpandIntLibCall(Node, true,
3984 RTLIB::SREM_I8,
3985 RTLIB::SREM_I16, RTLIB::SREM_I32,
3986 RTLIB::SREM_I64, RTLIB::SREM_I128));
3987 break;
3988 case ISD::UREM:
3989 Results.push_back(ExpandIntLibCall(Node, false,
3990 RTLIB::UREM_I8,
3991 RTLIB::UREM_I16, RTLIB::UREM_I32,
3992 RTLIB::UREM_I64, RTLIB::UREM_I128));
3993 break;
3994 case ISD::SDIV:
3995 Results.push_back(ExpandIntLibCall(Node, true,
3996 RTLIB::SDIV_I8,
3997 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3998 RTLIB::SDIV_I64, RTLIB::SDIV_I128));
3999 break;
4000 case ISD::UDIV:
4001 Results.push_back(ExpandIntLibCall(Node, false,
4002 RTLIB::UDIV_I8,
4003 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
4004 RTLIB::UDIV_I64, RTLIB::UDIV_I128));
4005 break;
4006 case ISD::SDIVREM:
4007 case ISD::UDIVREM:
4008 // Expand into divrem libcall
4009 ExpandDivRemLibCall(Node, Results);
4010 break;
4011 case ISD::MUL:
4012 Results.push_back(ExpandIntLibCall(Node, false,
4013 RTLIB::MUL_I8,
4014 RTLIB::MUL_I16, RTLIB::MUL_I32,
4015 RTLIB::MUL_I64, RTLIB::MUL_I128));
4016 break;
4017 }
4018
4019 // Replace the original node with the legalized result.
Eli Friedman13477152011-11-11 23:58:27 +00004020 if (!Results.empty())
4021 ReplaceNode(Node, Results.data());
Eli Friedman21d349b2009-05-27 01:25:56 +00004022}
Dan Gohman198b7ff2011-11-03 21:49:52 +00004023
Matt Arsenault0b7958a2015-11-10 18:48:04 +00004024// Determine the vector type to use in place of an original scalar element when
4025// promoting equally sized vectors.
4026static MVT getPromotedVectorElementType(const TargetLowering &TLI,
4027 MVT EltVT, MVT NewEltVT) {
4028 unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
4029 MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
4030 assert(TLI.isTypeLegal(MidVT) && "unexpected");
4031 return MidVT;
4032}
4033
Dan Gohman198b7ff2011-11-03 21:49:52 +00004034void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
4035 SmallVector<SDValue, 8> Results;
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +00004036 MVT OVT = Node->getSimpleValueType(0);
Eli Friedman21d349b2009-05-27 01:25:56 +00004037 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedman97f3f962009-07-17 05:16:04 +00004038 Node->getOpcode() == ISD::SINT_TO_FP ||
Matt Arsenault0b7958a2015-11-10 18:48:04 +00004039 Node->getOpcode() == ISD::SETCC ||
Matt Arsenaulta46aa642015-11-10 18:48:08 +00004040 Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
4041 Node->getOpcode() == ISD::INSERT_VECTOR_ELT) {
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +00004042 OVT = Node->getOperand(0).getSimpleValueType();
Bill Wendlingef408db2009-12-23 00:28:23 +00004043 }
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004044 if (Node->getOpcode() == ISD::BR_CC)
4045 OVT = Node->getOperand(2).getSimpleValueType();
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +00004046 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004047 SDLoc dl(Node);
Eli Friedman3b251702009-05-27 07:58:35 +00004048 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman21d349b2009-05-27 01:25:56 +00004049 switch (Node->getOpcode()) {
4050 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004051 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman21d349b2009-05-27 01:25:56 +00004052 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004053 case ISD::CTLZ_ZERO_UNDEF:
Eli Friedman21d349b2009-05-27 01:25:56 +00004054 case ISD::CTPOP:
4055 // Zero extend the argument.
4056 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Craig Topper7e5fad62016-04-23 05:20:47 +00004057 if (Node->getOpcode() == ISD::CTTZ) {
4058 // The count is the same in the promoted type except if the original
4059 // value was zero. This can be handled by setting the bit just off
4060 // the top of the original type.
4061 auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(),
4062 OVT.getSizeInBits());
4063 Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1,
4064 DAG.getConstant(TopBit, dl, NVT));
4065 }
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004066 // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
4067 // already the correct result.
Jakob Stoklund Olesen6b9f63c2009-07-12 17:43:20 +00004068 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Craig Topper7e5fad62016-04-23 05:20:47 +00004069 if (Node->getOpcode() == ISD::CTLZ ||
4070 Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
Eli Friedman21d349b2009-05-27 01:25:56 +00004071 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4072 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
4073 DAG.getConstant(NVT.getSizeInBits() -
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004074 OVT.getSizeInBits(), dl, NVT));
Eli Friedman21d349b2009-05-27 01:25:56 +00004075 }
Bill Wendlingef408db2009-12-23 00:28:23 +00004076 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman21d349b2009-05-27 01:25:56 +00004077 break;
4078 case ISD::BSWAP: {
4079 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling70794592009-12-22 22:53:39 +00004080 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendlingef408db2009-12-23 00:28:23 +00004081 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
Mehdi Amini9639d652015-07-09 02:09:20 +00004082 Tmp1 = DAG.getNode(
4083 ISD::SRL, dl, NVT, Tmp1,
4084 DAG.getConstant(DiffBits, dl,
4085 TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
Bill Wendlingef408db2009-12-23 00:28:23 +00004086 Results.push_back(Tmp1);
Eli Friedman21d349b2009-05-27 01:25:56 +00004087 break;
4088 }
4089 case ISD::FP_TO_UINT:
4090 case ISD::FP_TO_SINT:
4091 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
4092 Node->getOpcode() == ISD::FP_TO_SINT, dl);
4093 Results.push_back(Tmp1);
4094 break;
4095 case ISD::UINT_TO_FP:
4096 case ISD::SINT_TO_FP:
4097 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
4098 Node->getOpcode() == ISD::SINT_TO_FP, dl);
4099 Results.push_back(Tmp1);
4100 break;
Hal Finkel71c2ba32012-03-24 03:53:52 +00004101 case ISD::VAARG: {
4102 SDValue Chain = Node->getOperand(0); // Get the chain.
4103 SDValue Ptr = Node->getOperand(1); // Get the pointer.
4104
4105 unsigned TruncOp;
4106 if (OVT.isVector()) {
4107 TruncOp = ISD::BITCAST;
4108 } else {
4109 assert(OVT.isInteger()
4110 && "VAARG promotion is supported only for vectors or integer types");
4111 TruncOp = ISD::TRUNCATE;
4112 }
4113
4114 // Perform the larger operation, then convert back
4115 Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
4116 Node->getConstantOperandVal(3));
4117 Chain = Tmp1.getValue(1);
4118
4119 Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
4120
4121 // Modified the chain result - switch anything that used the old chain to
4122 // use the new one.
4123 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
4124 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
Chandler Carruth411fb402014-07-26 05:49:40 +00004125 if (UpdatedNodes) {
4126 UpdatedNodes->insert(Tmp2.getNode());
4127 UpdatedNodes->insert(Chain.getNode());
4128 }
Hal Finkel71c2ba32012-03-24 03:53:52 +00004129 ReplacedNode(Node);
4130 break;
4131 }
Eli Friedmand6f28342009-05-27 03:33:44 +00004132 case ISD::AND:
4133 case ISD::OR:
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004134 case ISD::XOR: {
4135 unsigned ExtOp, TruncOp;
4136 if (OVT.isVector()) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004137 ExtOp = ISD::BITCAST;
4138 TruncOp = ISD::BITCAST;
Chris Lattnercd927182010-04-07 23:47:51 +00004139 } else {
4140 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004141 ExtOp = ISD::ANY_EXTEND;
4142 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004143 }
4144 // Promote each of the values to the new type.
4145 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
4146 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4147 // Perform the larger operation, then convert back
Bill Wendlingef408db2009-12-23 00:28:23 +00004148 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
4149 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmand6f28342009-05-27 03:33:44 +00004150 break;
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004151 }
4152 case ISD::SELECT: {
Eli Friedman3b251702009-05-27 07:58:35 +00004153 unsigned ExtOp, TruncOp;
Tom Stellardc9a67a22014-03-24 16:07:28 +00004154 if (Node->getValueType(0).isVector() ||
4155 Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004156 ExtOp = ISD::BITCAST;
4157 TruncOp = ISD::BITCAST;
Eli Friedman2892d822009-05-27 12:20:41 +00004158 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman3b251702009-05-27 07:58:35 +00004159 ExtOp = ISD::ANY_EXTEND;
4160 TruncOp = ISD::TRUNCATE;
4161 } else {
4162 ExtOp = ISD::FP_EXTEND;
4163 TruncOp = ISD::FP_ROUND;
4164 }
4165 Tmp1 = Node->getOperand(0);
4166 // Promote each of the values to the new type.
4167 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4168 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
4169 // Perform the larger operation, then round down.
Matt Arsenaultd2f03322013-06-14 22:04:37 +00004170 Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
Eli Friedman3b251702009-05-27 07:58:35 +00004171 if (TruncOp != ISD::FP_ROUND)
Bill Wendlingef408db2009-12-23 00:28:23 +00004172 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman3b251702009-05-27 07:58:35 +00004173 else
Bill Wendlingef408db2009-12-23 00:28:23 +00004174 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004175 DAG.getIntPtrConstant(0, dl));
Bill Wendlingef408db2009-12-23 00:28:23 +00004176 Results.push_back(Tmp1);
Eli Friedman3b251702009-05-27 07:58:35 +00004177 break;
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004178 }
Eli Friedman3b251702009-05-27 07:58:35 +00004179 case ISD::VECTOR_SHUFFLE: {
Benjamin Kramer339ced42012-01-15 13:16:05 +00004180 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
Eli Friedman3b251702009-05-27 07:58:35 +00004181
4182 // Cast the two input vectors.
Wesley Peck527da1b2010-11-23 03:31:01 +00004183 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
4184 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman3b251702009-05-27 07:58:35 +00004185
4186 // Convert the shuffle mask to the right # elements.
Bill Wendlingef408db2009-12-23 00:28:23 +00004187 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peck527da1b2010-11-23 03:31:01 +00004188 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman3b251702009-05-27 07:58:35 +00004189 Results.push_back(Tmp1);
4190 break;
4191 }
Eli Friedman5df72022009-05-28 03:56:57 +00004192 case ISD::SETCC: {
Jakob Stoklund Olesen1ae07362009-07-24 18:22:59 +00004193 unsigned ExtOp = ISD::FP_EXTEND;
4194 if (NVT.isInteger()) {
4195 ISD::CondCode CCCode =
4196 cast<CondCodeSDNode>(Node->getOperand(2))->get();
4197 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedman5df72022009-05-28 03:56:57 +00004198 }
Jakob Stoklund Olesen1ae07362009-07-24 18:22:59 +00004199 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
4200 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedman5df72022009-05-28 03:56:57 +00004201 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
4202 Tmp1, Tmp2, Node->getOperand(2)));
4203 break;
4204 }
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004205 case ISD::BR_CC: {
4206 unsigned ExtOp = ISD::FP_EXTEND;
4207 if (NVT.isInteger()) {
4208 ISD::CondCode CCCode =
4209 cast<CondCodeSDNode>(Node->getOperand(1))->get();
4210 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4211 }
4212 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
4213 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
4214 Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
4215 Node->getOperand(0), Node->getOperand(1),
4216 Tmp1, Tmp2, Node->getOperand(4)));
4217 break;
4218 }
Oliver Stannardf5469be2014-08-18 14:22:39 +00004219 case ISD::FADD:
4220 case ISD::FSUB:
4221 case ISD::FMUL:
Pete Coopere69be6d2012-03-19 23:38:12 +00004222 case ISD::FDIV:
Pete Cooper8a3dc0e2012-04-04 19:36:31 +00004223 case ISD::FREM:
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004224 case ISD::FMINNUM:
4225 case ISD::FMAXNUM:
Pete Cooper99415fe2012-01-12 21:46:18 +00004226 case ISD::FPOW: {
4227 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4228 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
Sanjay Patela2607012015-09-16 16:31:21 +00004229 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
4230 Node->getFlags());
Pete Cooper99415fe2012-01-12 21:46:18 +00004231 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004232 Tmp3, DAG.getIntPtrConstant(0, dl)));
Pete Cooper99415fe2012-01-12 21:46:18 +00004233 break;
4234 }
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004235 case ISD::FMA: {
4236 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4237 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
4238 Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
4239 Results.push_back(
4240 DAG.getNode(ISD::FP_ROUND, dl, OVT,
4241 DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004242 DAG.getIntPtrConstant(0, dl)));
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004243 break;
4244 }
Ahmed Bougacha40ded502015-08-13 01:09:43 +00004245 case ISD::FCOPYSIGN:
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004246 case ISD::FPOWI: {
4247 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4248 Tmp2 = Node->getOperand(1);
4249 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Ahmed Bougachaa1966612015-08-13 01:32:30 +00004250
4251 // fcopysign doesn't change anything but the sign bit, so
4252 // (fp_round (fcopysign (fpext a), b))
4253 // is as precise as
4254 // (fp_round (fpext a))
4255 // which is a no-op. Mark it as a TRUNCating FP_ROUND.
4256 const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004257 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
Ahmed Bougachaa1966612015-08-13 01:32:30 +00004258 Tmp3, DAG.getIntPtrConstant(isTrunc, dl)));
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004259 break;
4260 }
4261 case ISD::FFLOOR:
4262 case ISD::FCEIL:
4263 case ISD::FRINT:
4264 case ISD::FNEARBYINT:
4265 case ISD::FROUND:
4266 case ISD::FTRUNC:
4267 case ISD::FNEG:
4268 case ISD::FSQRT:
4269 case ISD::FSIN:
4270 case ISD::FCOS:
Pete Cooper99415fe2012-01-12 21:46:18 +00004271 case ISD::FLOG:
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004272 case ISD::FLOG2:
4273 case ISD::FLOG10:
4274 case ISD::FABS:
4275 case ISD::FEXP:
4276 case ISD::FEXP2: {
Pete Cooper99415fe2012-01-12 21:46:18 +00004277 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4278 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
4279 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004280 Tmp2, DAG.getIntPtrConstant(0, dl)));
Pete Cooper99415fe2012-01-12 21:46:18 +00004281 break;
4282 }
Matt Arsenault29f96632015-10-21 21:10:10 +00004283 case ISD::BUILD_VECTOR: {
4284 MVT EltVT = OVT.getVectorElementType();
4285 MVT NewEltVT = NVT.getVectorElementType();
4286
4287 // Handle bitcasts to a different vector type with the same total bit size
4288 //
4289 // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
4290 // =>
4291 // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
4292
4293 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4294 "Invalid promote type for build_vector");
4295 assert(NewEltVT.bitsLT(EltVT) && "not handled");
4296
Matt Arsenault0b7958a2015-11-10 18:48:04 +00004297 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
Matt Arsenault29f96632015-10-21 21:10:10 +00004298
4299 SmallVector<SDValue, 8> NewOps;
4300 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
4301 SDValue Op = Node->getOperand(I);
4302 NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
4303 }
4304
4305 SDLoc SL(Node);
4306 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps);
4307 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
4308 Results.push_back(CvtVec);
4309 break;
4310 }
Matt Arsenault0b7958a2015-11-10 18:48:04 +00004311 case ISD::EXTRACT_VECTOR_ELT: {
4312 MVT EltVT = OVT.getVectorElementType();
4313 MVT NewEltVT = NVT.getVectorElementType();
4314
4315 // Handle bitcasts to a different vector type with the same total bit size.
4316 //
4317 // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
4318 // =>
4319 // v4i32:castx = bitcast x:v2i64
4320 //
4321 // i64 = bitcast
4322 // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
4323 // (i32 (extract_vector_elt castx, (2 * y + 1)))
4324 //
4325
4326 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4327 "Invalid promote type for extract_vector_elt");
4328 assert(NewEltVT.bitsLT(EltVT) && "not handled");
4329
4330 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4331 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
4332
4333 SDValue Idx = Node->getOperand(1);
4334 EVT IdxVT = Idx.getValueType();
4335 SDLoc SL(Node);
4336 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
4337 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
4338
4339 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
4340
4341 SmallVector<SDValue, 8> NewOps;
4342 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
4343 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
4344 SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
4345
4346 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
4347 CastVec, TmpIdx);
4348 NewOps.push_back(Elt);
4349 }
4350
4351 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, SL, MidVT, NewOps);
4352
4353 Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
4354 break;
4355 }
Matt Arsenaulta46aa642015-11-10 18:48:08 +00004356 case ISD::INSERT_VECTOR_ELT: {
4357 MVT EltVT = OVT.getVectorElementType();
4358 MVT NewEltVT = NVT.getVectorElementType();
4359
4360 // Handle bitcasts to a different vector type with the same total bit size
4361 //
4362 // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
4363 // =>
4364 // v4i32:castx = bitcast x:v2i64
4365 // v2i32:casty = bitcast y:i64
4366 //
4367 // v2i64 = bitcast
4368 // (v4i32 insert_vector_elt
4369 // (v4i32 insert_vector_elt v4i32:castx,
4370 // (extract_vector_elt casty, 0), 2 * z),
4371 // (extract_vector_elt casty, 1), (2 * z + 1))
4372
4373 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4374 "Invalid promote type for insert_vector_elt");
4375 assert(NewEltVT.bitsLT(EltVT) && "not handled");
4376
4377 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4378 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
4379
4380 SDValue Val = Node->getOperand(1);
4381 SDValue Idx = Node->getOperand(2);
4382 EVT IdxVT = Idx.getValueType();
4383 SDLoc SL(Node);
4384
4385 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
4386 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
4387
4388 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
4389 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
4390
4391 SDValue NewVec = CastVec;
4392 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
4393 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
4394 SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
4395
4396 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
4397 CastVal, IdxOffset);
4398
4399 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
4400 NewVec, Elt, InEltIdx);
4401 }
4402
4403 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
4404 break;
4405 }
Matt Arsenaultaa118e22015-11-10 18:48:11 +00004406 case ISD::SCALAR_TO_VECTOR: {
4407 MVT EltVT = OVT.getVectorElementType();
4408 MVT NewEltVT = NVT.getVectorElementType();
4409
4410 // Handle bitcasts to different vector type with the smae total bit size.
4411 //
4412 // e.g. v2i64 = scalar_to_vector x:i64
4413 // =>
4414 // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
4415 //
4416
4417 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4418 SDValue Val = Node->getOperand(0);
4419 SDLoc SL(Node);
4420
4421 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
4422 SDValue Undef = DAG.getUNDEF(MidVT);
4423
4424 SmallVector<SDValue, 8> NewElts;
4425 NewElts.push_back(CastVal);
4426 for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
4427 NewElts.push_back(Undef);
4428
4429 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
4430 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
4431 Results.push_back(CvtVec);
4432 break;
4433 }
Eli Friedman21d349b2009-05-27 01:25:56 +00004434 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00004435
4436 // Replace the original node with the legalized result.
Eli Friedman13477152011-11-11 23:58:27 +00004437 if (!Results.empty())
4438 ReplaceNode(Node, Results.data());
Eli Friedman21d349b2009-05-27 01:25:56 +00004439}
4440
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00004441/// This is the entry point for the file.
Dan Gohmand282f462011-05-16 22:19:54 +00004442void SelectionDAG::Legalize() {
Chandler Carruth411fb402014-07-26 05:49:40 +00004443 AssignTopologicalOrder();
4444
Chandler Carruth411fb402014-07-26 05:49:40 +00004445 SmallPtrSet<SDNode *, 16> LegalizedNodes;
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004446 SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
Chandler Carruth411fb402014-07-26 05:49:40 +00004447
4448 // Visit all the nodes. We start in topological order, so that we see
4449 // nodes with their original operands intact. Legalization can produce
4450 // new nodes which may themselves need to be legalized. Iterate until all
4451 // nodes have been legalized.
4452 for (;;) {
4453 bool AnyLegalized = false;
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004454 for (auto NI = allnodes_end(); NI != allnodes_begin();) {
4455 --NI;
Chandler Carruth411fb402014-07-26 05:49:40 +00004456
Duncan P. N. Exon Smithe400a7d2015-10-13 19:47:46 +00004457 SDNode *N = &*NI;
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004458 if (N->use_empty() && N != getRoot().getNode()) {
4459 ++NI;
4460 DeleteNode(N);
4461 continue;
4462 }
4463
David Blaikie70573dc2014-11-19 07:49:26 +00004464 if (LegalizedNodes.insert(N).second) {
Chandler Carruth411fb402014-07-26 05:49:40 +00004465 AnyLegalized = true;
4466 Legalizer.LegalizeOp(N);
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004467
4468 if (N->use_empty() && N != getRoot().getNode()) {
4469 ++NI;
4470 DeleteNode(N);
4471 }
Chandler Carruth411fb402014-07-26 05:49:40 +00004472 }
4473 }
4474 if (!AnyLegalized)
4475 break;
4476
4477 }
4478
4479 // Remove dead nodes now.
4480 RemoveDeadNodes();
4481}
4482
4483bool SelectionDAG::LegalizeOp(SDNode *N,
4484 SmallSetVector<SDNode *, 16> &UpdatedNodes) {
Chandler Carruth411fb402014-07-26 05:49:40 +00004485 SmallPtrSet<SDNode *, 16> LegalizedNodes;
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004486 SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
Chandler Carruth411fb402014-07-26 05:49:40 +00004487
4488 // Directly insert the node in question, and legalize it. This will recurse
4489 // as needed through operands.
4490 LegalizedNodes.insert(N);
4491 Legalizer.LegalizeOp(N);
4492
4493 return LegalizedNodes.count(N);
Chris Lattnerdc750592005-01-07 07:47:09 +00004494}