blob: b465ffb5d1be10bc8360d43db18a2e3242ffeeb5 [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 Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth411fb402014-07-26 05:49:40 +000015#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallPtrSet.h"
Hal Finkel19775142014-03-31 17:48:10 +000017#include "llvm/ADT/SmallSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallVector.h"
Paul Redmondf29ddfe2013-02-15 18:45:18 +000019#include "llvm/ADT/Triple.h"
Evan Chengd4b08732010-11-30 23:55:39 +000020#include "llvm/CodeGen/Analysis.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000021#include "llvm/CodeGen/MachineFunction.h"
Jim Laskey70323a82006-12-14 19:17:33 +000022#include "llvm/CodeGen/MachineJumpTableInfo.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.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000096 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Andrew Trickef9de2a2013-05-25 02:42:55 +000097 SDValue Idx, SDLoc dl);
Eli Friedmana8f9a022009-05-27 02:16:40 +000098 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
Andrew Trickef9de2a2013-05-25 02:42:55 +000099 SDValue Idx, 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>
Andrew Trickef9de2a2013-05-25 02:42:55 +0000105 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, 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,
Daniel Sandersedc071b2013-11-21 13:24:49 +0000110 bool &NeedInvert, 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,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000114 unsigned NumOps, bool isSigned, 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
Andrew Trickef9de2a2013-05-25 02:42:55 +0000131 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, SDLoc dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000132 SDValue ExpandBUILD_VECTOR(SDNode *Node);
133 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman2892d822009-05-27 12:20:41 +0000134 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
135 SmallVectorImpl<SDValue> &Results);
Matthias Braunb9610a62015-11-12 01:02:47 +0000136 void getSignAsIntValue(FloatSignAsInt &State, SDLoc DL, SDValue Value) const;
137 SDValue modifySignAsInt(const FloatSignAsInt &State, SDLoc DL,
138 SDValue NewIntValue) const;
139 SDValue ExpandFCOPYSIGN(SDNode *Node) const;
140 SDValue ExpandFABS(SDNode *Node) const;
Owen Anderson53aa7a92009-08-10 22:56:29 +0000141 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000142 SDLoc dl);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000143 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000144 SDLoc dl);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000145 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000146 SDLoc dl);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000147
James Molloy90111f72015-11-12 12:29:09 +0000148 SDValue ExpandBITREVERSE(SDValue Op, SDLoc dl);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000149 SDValue ExpandBSWAP(SDValue Op, SDLoc dl);
150 SDValue ExpandBitCount(unsigned Opc, SDValue Op, SDLoc dl);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000151
Eli Friedman40afdb62009-05-23 22:37:25 +0000152 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenebab5e6e2011-01-26 19:13:22 +0000153 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000154 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman21d349b2009-05-27 01:25:56 +0000155
Dan Gohman198b7ff2011-11-03 21:49:52 +0000156 SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
157
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000158 // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
159 bool ExpandNode(SDNode *Node);
160 void ConvertNodeToLibcall(SDNode *Node);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000161 void PromoteNode(SDNode *Node);
162
Eli Friedman13477152011-11-11 23:58:27 +0000163public:
Eli Friedman13477152011-11-11 23:58:27 +0000164 // Node replacement helpers
165 void ReplacedNode(SDNode *N) {
Chandler Carruth1f52b3d2014-08-01 19:49:59 +0000166 LegalizedNodes.erase(N);
Chandler Carruth74ec9e12014-08-27 11:22:16 +0000167 if (UpdatedNodes)
168 UpdatedNodes->insert(N);
Eli Friedman13477152011-11-11 23:58:27 +0000169 }
170 void ReplaceNode(SDNode *Old, SDNode *New) {
Chandler Carruthb1432742014-07-28 17:55:07 +0000171 DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
172 dbgs() << " with: "; New->dump(&DAG));
173
Chandler Carruth5a85c7b2014-07-26 05:53:16 +0000174 assert(Old->getNumValues() == New->getNumValues() &&
175 "Replacing one node with another that produces a different number "
176 "of values!");
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000177 DAG.ReplaceAllUsesWith(Old, New);
Chandler Carruth411fb402014-07-26 05:49:40 +0000178 for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i)
179 DAG.TransferDbgValues(SDValue(Old, i), SDValue(New, i));
180 if (UpdatedNodes)
181 UpdatedNodes->insert(New);
Eli Friedman13477152011-11-11 23:58:27 +0000182 ReplacedNode(Old);
183 }
184 void ReplaceNode(SDValue Old, SDValue New) {
Chandler Carruthb1432742014-07-28 17:55:07 +0000185 DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
186 dbgs() << " with: "; New->dump(&DAG));
187
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000188 DAG.ReplaceAllUsesWith(Old, New);
Chandler Carruth411fb402014-07-26 05:49:40 +0000189 DAG.TransferDbgValues(Old, New);
190 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 Carruth411fb402014-07-26 05:49:40 +0000202 DAG.TransferDbgValues(SDValue(Old, i), New[i]);
Chandler Carruthb1432742014-07-28 17:55:07 +0000203 if (UpdatedNodes)
204 UpdatedNodes->insert(New[i].getNode());
205 }
Eli Friedman13477152011-11-11 23:58:27 +0000206 ReplacedNode(Old);
207 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000208};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000209}
Chris Lattnerdc750592005-01-07 07:47:09 +0000210
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000211/// Return a vector shuffle operation which
Nate Begeman5f829d82009-04-29 05:20:52 +0000212/// performs the same shuffe in terms of order or result bytes, but on a type
213/// whose vector element type is narrower than the original shuffle type.
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000214/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach9b7755f2010-07-02 17:41:59 +0000215SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +0000216SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, SDLoc dl,
Nate Begeman5f829d82009-04-29 05:20:52 +0000217 SDValue N1, SDValue N2,
Benjamin Kramer339ced42012-01-15 13:16:05 +0000218 ArrayRef<int> Mask) const {
Nate Begeman5f829d82009-04-29 05:20:52 +0000219 unsigned NumMaskElts = VT.getVectorNumElements();
220 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000221 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner6be79822006-04-04 17:23:26 +0000222
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000223 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
224
225 if (NumEltsGrowth == 1)
226 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach9b7755f2010-07-02 17:41:59 +0000227
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000228 SmallVector<int, 8> NewMask;
Nate Begeman5f829d82009-04-29 05:20:52 +0000229 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000230 int Idx = Mask[i];
231 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach9b7755f2010-07-02 17:41:59 +0000232 if (Idx < 0)
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000233 NewMask.push_back(-1);
234 else
235 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner6be79822006-04-04 17:23:26 +0000236 }
Chris Lattner6be79822006-04-04 17:23:26 +0000237 }
Nate Begeman5f829d82009-04-29 05:20:52 +0000238 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000239 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
240 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner6be79822006-04-04 17:23:26 +0000241}
242
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000243/// Expands the ConstantFP node to an integer constant or
Evan Cheng22cf8992006-12-13 20:57:08 +0000244/// a load from the constant pool.
Dan Gohman198b7ff2011-11-03 21:49:52 +0000245SDValue
246SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
Evan Cheng47833a12006-12-12 21:32:44 +0000247 bool Extend = false;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000248 SDLoc dl(CFP);
Evan Cheng47833a12006-12-12 21:32:44 +0000249
250 // If a FP immediate is precise when represented as a float and if the
251 // target can do an extending load from float to double, we put it into
252 // the constant pool as a float, even if it's is statically typed as a
Chris Lattner3dc38992008-03-05 06:46:58 +0000253 // double. This shrinks FP constants and canonicalizes them for targets where
254 // an FP extending load is the same cost as a normal load (such as on the x87
255 // fp stack or PPC FP unit).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000256 EVT VT = CFP->getValueType(0);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000257 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng22cf8992006-12-13 20:57:08 +0000258 if (!UseCP) {
Owen Anderson9f944592009-08-11 20:47:22 +0000259 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000260 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
Owen Anderson9f944592009-08-11 20:47:22 +0000261 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000262 }
263
Owen Anderson53aa7a92009-08-10 22:56:29 +0000264 EVT OrigVT = VT;
265 EVT SVT = VT;
Oliver Stannard6eda6ff2014-07-11 13:33:46 +0000266 while (SVT != MVT::f32 && SVT != MVT::f16) {
Owen Anderson9f944592009-08-11 20:47:22 +0000267 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman35b6f9a2010-06-18 14:01:07 +0000268 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Cheng38caf772008-03-04 08:05:30 +0000269 // Only do this if the target has a native EXTLOAD instruction from
270 // smaller type.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000271 TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
Chris Lattner3dc38992008-03-05 06:46:58 +0000272 TLI.ShouldShrinkFPConstant(OrigVT)) {
Chris Lattner229907c2011-07-18 04:54:35 +0000273 Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Anderson487375e2009-07-29 18:55:55 +0000274 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Cheng38caf772008-03-04 08:05:30 +0000275 VT = SVT;
276 Extend = true;
277 }
Evan Cheng47833a12006-12-12 21:32:44 +0000278 }
279
Mehdi Amini44ede332015-07-09 02:09:04 +0000280 SDValue CPIdx =
281 DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000282 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman198b7ff2011-11-03 21:49:52 +0000283 if (Extend) {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000284 SDValue Result = DAG.getExtLoad(
285 ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
286 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT,
287 false, false, false, Alignment);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000288 return Result;
289 }
290 SDValue Result =
Alex Lorenze40c8a22015-08-11 23:09:45 +0000291 DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
292 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
293 false, false, false, Alignment);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000294 return Result;
Evan Cheng47833a12006-12-12 21:32:44 +0000295}
296
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000297/// Expands an unaligned store to 2 half-size stores.
Dan Gohman198b7ff2011-11-03 21:49:52 +0000298static void ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
299 const TargetLowering &TLI,
Eli Friedman13477152011-11-11 23:58:27 +0000300 SelectionDAGLegalize *DAGLegalize) {
Eli Friedmand257a462011-11-16 02:43:15 +0000301 assert(ST->getAddressingMode() == ISD::UNINDEXED &&
302 "unaligned indexed stores not implemented!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000303 SDValue Chain = ST->getChain();
304 SDValue Ptr = ST->getBasePtr();
305 SDValue Val = ST->getValue();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000306 EVT VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000307 int Alignment = ST->getAlignment();
Matt Arsenault2ba54c32013-10-30 23:30:05 +0000308 unsigned AS = ST->getAddressSpace();
309
Andrew Trickef9de2a2013-05-25 02:42:55 +0000310 SDLoc dl(ST);
Duncan Sands13237ac2008-06-06 12:08:01 +0000311 if (ST->getMemoryVT().isFloatingPoint() ||
312 ST->getMemoryVT().isVector()) {
Owen Anderson117c9e82009-08-12 00:36:31 +0000313 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands8f352fe2008-12-12 21:47:02 +0000314 if (TLI.isTypeLegal(intVT)) {
315 // Expand to a bitconvert of the value to the integer type of the
316 // same size, then a (misaligned) int store.
317 // FIXME: Does not handle truncating floating point stores!
Wesley Peck527da1b2010-11-23 03:31:01 +0000318 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000319 Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
320 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Eli Friedman13477152011-11-11 23:58:27 +0000321 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000322 return;
Duncan Sands8f352fe2008-12-12 21:47:02 +0000323 }
Dan Gohmanabffc992011-05-17 22:22:52 +0000324 // Do a (aligned) store to a stack slot, then copy from the stack slot
325 // to the final destination using (unaligned) integer loads and stores.
326 EVT StoredVT = ST->getMemoryVT();
Patrik Hagglundbad545c2012-12-19 11:48:16 +0000327 MVT RegVT =
Dan Gohmanabffc992011-05-17 22:22:52 +0000328 TLI.getRegisterType(*DAG.getContext(),
329 EVT::getIntegerVT(*DAG.getContext(),
330 StoredVT.getSizeInBits()));
331 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
332 unsigned RegBytes = RegVT.getSizeInBits() / 8;
333 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
334
335 // Make sure the stack slot is also aligned for the register type.
336 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
337
338 // Perform the original store, only redirected to the stack slot.
339 SDValue Store = DAG.getTruncStore(Chain, dl,
340 Val, StackPtr, MachinePointerInfo(),
341 StoredVT, false, false, 0);
Mehdi Amini44ede332015-07-09 02:09:04 +0000342 SDValue Increment = DAG.getConstant(
343 RegBytes, dl, TLI.getPointerTy(DAG.getDataLayout(), AS));
Dan Gohmanabffc992011-05-17 22:22:52 +0000344 SmallVector<SDValue, 8> Stores;
345 unsigned Offset = 0;
346
347 // Do all but one copies using the full register width.
348 for (unsigned i = 1; i < NumRegs; i++) {
349 // Load one integer register's worth from the stack slot.
350 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
351 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000352 false, false, false, 0);
Dan Gohmanabffc992011-05-17 22:22:52 +0000353 // Store it to the final location. Remember the store.
354 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
355 ST->getPointerInfo().getWithOffset(Offset),
356 ST->isVolatile(), ST->isNonTemporal(),
357 MinAlign(ST->getAlignment(), Offset)));
358 // Increment the pointers.
359 Offset += RegBytes;
360 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
361 Increment);
362 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
363 }
364
365 // The last store may be partial. Do a truncating store. On big-endian
366 // machines this requires an extending load from the stack slot to ensure
367 // that the bits are in the right place.
368 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
369 8 * (StoredBytes - Offset));
370
371 // Load from the stack slot.
372 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
373 MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +0000374 MemVT, false, false, false, 0);
Dan Gohmanabffc992011-05-17 22:22:52 +0000375
376 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
377 ST->getPointerInfo()
378 .getWithOffset(Offset),
379 MemVT, ST->isVolatile(),
380 ST->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000381 MinAlign(ST->getAlignment(), Offset),
Hal Finkelcc39b672014-07-24 12:16:19 +0000382 ST->getAAInfo()));
Dan Gohmanabffc992011-05-17 22:22:52 +0000383 // The order of the stores doesn't matter - say it with a TokenFactor.
Craig Topper48d114b2014-04-26 18:35:24 +0000384 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
Eli Friedman13477152011-11-11 23:58:27 +0000385 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000386 return;
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000387 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000388 assert(ST->getMemoryVT().isInteger() &&
389 !ST->getMemoryVT().isVector() &&
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000390 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000391 // Get the half-size VT
Ken Dyckdf5561d2009-12-17 20:09:43 +0000392 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands13237ac2008-06-06 12:08:01 +0000393 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000394 int IncrementSize = NumBits / 8;
395
396 // Divide the stored value in two parts.
Mehdi Amini9639d652015-07-09 02:09:20 +0000397 SDValue ShiftAmount =
398 DAG.getConstant(NumBits, dl, TLI.getShiftAmountTy(Val.getValueType(),
399 DAG.getDataLayout()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000400 SDValue Lo = Val;
Dale Johannesenad00f6e2009-02-02 20:41:04 +0000401 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000402
403 // Store the two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000404 SDValue Store1, Store2;
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000405 Store1 = DAG.getTruncStore(Chain, dl,
406 DAG.getDataLayout().isLittleEndian() ? Lo : Hi,
407 Ptr, ST->getPointerInfo(), NewStoredVT,
David Greene39c6d012010-02-15 17:00:31 +0000408 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Matt Arsenault2ba54c32013-10-30 23:30:05 +0000409
Dale Johannesenad00f6e2009-02-02 20:41:04 +0000410 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Mehdi Amini44ede332015-07-09 02:09:04 +0000411 DAG.getConstant(IncrementSize, dl,
412 TLI.getPointerTy(DAG.getDataLayout(), AS)));
Duncan Sands1826ded2007-10-28 12:59:45 +0000413 Alignment = MinAlign(Alignment, IncrementSize);
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000414 Store2 = DAG.getTruncStore(
415 Chain, dl, DAG.getDataLayout().isLittleEndian() ? Hi : Lo, Ptr,
416 ST->getPointerInfo().getWithOffset(IncrementSize), NewStoredVT,
417 ST->isVolatile(), ST->isNonTemporal(), Alignment, ST->getAAInfo());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000418
Dan Gohman198b7ff2011-11-03 21:49:52 +0000419 SDValue Result =
420 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Eli Friedman13477152011-11-11 23:58:27 +0000421 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000422}
423
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000424/// Expands an unaligned load to 2 half-size loads.
Dan Gohman198b7ff2011-11-03 21:49:52 +0000425static void
426ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
427 const TargetLowering &TLI,
428 SDValue &ValResult, SDValue &ChainResult) {
Eli Friedmand257a462011-11-16 02:43:15 +0000429 assert(LD->getAddressingMode() == ISD::UNINDEXED &&
430 "unaligned indexed loads not implemented!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000431 SDValue Chain = LD->getChain();
432 SDValue Ptr = LD->getBasePtr();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000433 EVT VT = LD->getValueType(0);
434 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000435 SDLoc dl(LD);
Duncan Sands13237ac2008-06-06 12:08:01 +0000436 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson117c9e82009-08-12 00:36:31 +0000437 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Nadav Roteme0f84d32012-08-09 01:56:44 +0000438 if (TLI.isTypeLegal(intVT) && TLI.isTypeLegal(LoadedVT)) {
Duncan Sands8f352fe2008-12-12 21:47:02 +0000439 // Expand to a (misaligned) integer load of the same size,
440 // then bitconvert to floating point or vector.
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000441 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr,
442 LD->getMemOperand());
Wesley Peck527da1b2010-11-23 03:31:01 +0000443 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Nadav Roteme0f84d32012-08-09 01:56:44 +0000444 if (LoadedVT != VT)
445 Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND :
446 ISD::ANY_EXTEND, dl, VT, Result);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000447
Dan Gohman198b7ff2011-11-03 21:49:52 +0000448 ValResult = Result;
Hal Finkelcaf11492015-08-04 06:29:12 +0000449 ChainResult = newLoad.getValue(1);
Dan Gohman198b7ff2011-11-03 21:49:52 +0000450 return;
Duncan Sands8f352fe2008-12-12 21:47:02 +0000451 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000452
Chris Lattner1ffcf522010-09-21 16:36:31 +0000453 // Copy the value to a (aligned) stack slot using (unaligned) integer
454 // loads and stores, then do a (aligned) load from the stack slot.
Patrik Hagglundbad545c2012-12-19 11:48:16 +0000455 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
Chris Lattner1ffcf522010-09-21 16:36:31 +0000456 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
457 unsigned RegBytes = RegVT.getSizeInBits() / 8;
458 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
459
460 // Make sure the stack slot is also aligned for the register type.
461 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
462
Mehdi Amini44ede332015-07-09 02:09:04 +0000463 SDValue Increment =
464 DAG.getConstant(RegBytes, dl, TLI.getPointerTy(DAG.getDataLayout()));
Chris Lattner1ffcf522010-09-21 16:36:31 +0000465 SmallVector<SDValue, 8> Stores;
466 SDValue StackPtr = StackBase;
467 unsigned Offset = 0;
468
469 // Do all but one copies using the full register width.
470 for (unsigned i = 1; i < NumRegs; i++) {
471 // Load one integer register's worth from the original location.
472 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
473 LD->getPointerInfo().getWithOffset(Offset),
474 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000475 LD->isInvariant(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000476 MinAlign(LD->getAlignment(), Offset),
Hal Finkelcc39b672014-07-24 12:16:19 +0000477 LD->getAAInfo());
Chris Lattner1ffcf522010-09-21 16:36:31 +0000478 // Follow the load with a store to the stack slot. Remember the store.
479 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner676c61d2010-09-21 18:41:36 +0000480 MachinePointerInfo(), false, false, 0));
Chris Lattner1ffcf522010-09-21 16:36:31 +0000481 // Increment the pointers.
482 Offset += RegBytes;
483 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
484 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
485 Increment);
486 }
487
488 // The last copy may be partial. Do an extending load.
489 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
490 8 * (LoadedBytes - Offset));
Stuart Hastings81c43062011-02-16 16:23:55 +0000491 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattner1ffcf522010-09-21 16:36:31 +0000492 LD->getPointerInfo().getWithOffset(Offset),
493 MemVT, LD->isVolatile(),
494 LD->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +0000495 LD->isInvariant(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000496 MinAlign(LD->getAlignment(), Offset),
Hal Finkelcc39b672014-07-24 12:16:19 +0000497 LD->getAAInfo());
Chris Lattner1ffcf522010-09-21 16:36:31 +0000498 // Follow the load with a store to the stack slot. Remember the store.
499 // On big-endian machines this requires a truncating store to ensure
500 // that the bits end up in the right place.
501 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
502 MachinePointerInfo(), MemVT,
503 false, false, 0));
504
505 // The order of the stores doesn't matter - say it with a TokenFactor.
Craig Topper48d114b2014-04-26 18:35:24 +0000506 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
Chris Lattner1ffcf522010-09-21 16:36:31 +0000507
508 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastings81c43062011-02-16 16:23:55 +0000509 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Louis Gerbarg67474e32014-07-31 21:45:05 +0000510 MachinePointerInfo(), LoadedVT, false,false, false,
511 0);
Chris Lattner1ffcf522010-09-21 16:36:31 +0000512
513 // Callers expect a MERGE_VALUES node.
Dan Gohman198b7ff2011-11-03 21:49:52 +0000514 ValResult = Load;
515 ChainResult = TF;
516 return;
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000517 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000518 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner09c03932007-11-19 21:38:03 +0000519 "Unaligned load of unsupported type.");
520
Dale Johannesenbf76a082008-02-27 22:36:00 +0000521 // Compute the new VT that is half the size of the old one. This is an
522 // integer MVT.
Duncan Sands13237ac2008-06-06 12:08:01 +0000523 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000524 EVT NewLoadedVT;
Owen Anderson117c9e82009-08-12 00:36:31 +0000525 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattner09c03932007-11-19 21:38:03 +0000526 NumBits >>= 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000527
Chris Lattner09c03932007-11-19 21:38:03 +0000528 unsigned Alignment = LD->getAlignment();
529 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000530 ISD::LoadExtType HiExtType = LD->getExtensionType();
531
532 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
533 if (HiExtType == ISD::NON_EXTLOAD)
534 HiExtType = ISD::ZEXTLOAD;
535
536 // Load the value in two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000537 SDValue Lo, Hi;
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000538 if (DAG.getDataLayout().isLittleEndian()) {
Stuart Hastings81c43062011-02-16 16:23:55 +0000539 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattner1ffcf522010-09-21 16:36:31 +0000540 NewLoadedVT, LD->isVolatile(),
Louis Gerbarg67474e32014-07-31 21:45:05 +0000541 LD->isNonTemporal(), LD->isInvariant(), Alignment,
542 LD->getAAInfo());
Dale Johannesenad00f6e2009-02-02 20:41:04 +0000543 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000544 DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
Stuart Hastings81c43062011-02-16 16:23:55 +0000545 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattner1ffcf522010-09-21 16:36:31 +0000546 LD->getPointerInfo().getWithOffset(IncrementSize),
547 NewLoadedVT, LD->isVolatile(),
Louis Gerbarg67474e32014-07-31 21:45:05 +0000548 LD->isNonTemporal(),LD->isInvariant(),
549 MinAlign(Alignment, IncrementSize), LD->getAAInfo());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000550 } else {
Stuart Hastings81c43062011-02-16 16:23:55 +0000551 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattner1ffcf522010-09-21 16:36:31 +0000552 NewLoadedVT, LD->isVolatile(),
Louis Gerbarg67474e32014-07-31 21:45:05 +0000553 LD->isNonTemporal(), LD->isInvariant(), Alignment,
554 LD->getAAInfo());
Dale Johannesenad00f6e2009-02-02 20:41:04 +0000555 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000556 DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
Stuart Hastings81c43062011-02-16 16:23:55 +0000557 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattner1ffcf522010-09-21 16:36:31 +0000558 LD->getPointerInfo().getWithOffset(IncrementSize),
559 NewLoadedVT, LD->isVolatile(),
Louis Gerbarg67474e32014-07-31 21:45:05 +0000560 LD->isNonTemporal(), LD->isInvariant(),
561 MinAlign(Alignment, IncrementSize), LD->getAAInfo());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000562 }
563
564 // aggregate the two parts
Mehdi Amini9639d652015-07-09 02:09:20 +0000565 SDValue ShiftAmount =
566 DAG.getConstant(NumBits, dl, TLI.getShiftAmountTy(Hi.getValueType(),
567 DAG.getDataLayout()));
Dale Johannesenad00f6e2009-02-02 20:41:04 +0000568 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
569 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000570
Owen Anderson9f944592009-08-11 20:47:22 +0000571 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000572 Hi.getValue(1));
573
Dan Gohman198b7ff2011-11-03 21:49:52 +0000574 ValResult = Result;
575 ChainResult = TF;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000576}
Evan Cheng003feb02007-01-04 21:56:39 +0000577
Sanjay Pateleb4a4d52014-11-21 18:58:38 +0000578/// Some target cannot handle a variable insertion index for the
579/// INSERT_VECTOR_ELT instruction. In this case, it
Nate Begeman6f94f612008-04-25 18:07:40 +0000580/// is necessary to spill the vector being inserted into to memory, perform
581/// the insert there, and then read the result back.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000582SDValue SelectionDAGLegalize::
Dale Johannesenad00f6e2009-02-02 20:41:04 +0000583PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000584 SDLoc dl) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000585 SDValue Tmp1 = Vec;
586 SDValue Tmp2 = Val;
587 SDValue Tmp3 = Idx;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000588
Nate Begeman6f94f612008-04-25 18:07:40 +0000589 // If the target doesn't support this, we have to spill the input vector
590 // to a temporary stack slot, update the element, then reload it. This is
591 // badness. We could also load the value into a vector register (either
592 // with a "move to register" or "extload into register" instruction, then
593 // permute it into place, if the idx is a constant and if the idx is
594 // supported by the target.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000595 EVT VT = Tmp1.getValueType();
596 EVT EltVT = VT.getVectorElementType();
597 EVT IdxVT = Tmp3.getValueType();
Mehdi Amini44ede332015-07-09 02:09:04 +0000598 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000599 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000600
Evan Cheng0e9d9ca2009-10-18 18:16:27 +0000601 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
602
Nate Begeman6f94f612008-04-25 18:07:40 +0000603 // Store the vector.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000604 SDValue Ch = DAG.getStore(
605 DAG.getEntryNode(), dl, Tmp1, StackPtr,
606 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), false,
607 false, 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000608
609 // Truncate or zero extend offset to target pointer type.
Pete Cooper8acd3862015-07-15 00:43:54 +0000610 Tmp3 = DAG.getZExtOrTrunc(Tmp3, dl, PtrVT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000611 // Add the offset to the index.
Dan Gohman9b80f862010-02-25 15:20:39 +0000612 unsigned EltSize = EltVT.getSizeInBits()/8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000613 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,
614 DAG.getConstant(EltSize, dl, IdxVT));
Dale Johannesenad00f6e2009-02-02 20:41:04 +0000615 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman6f94f612008-04-25 18:07:40 +0000616 // Store the scalar value.
Chris Lattnera35499e2010-09-21 07:32:19 +0000617 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene39c6d012010-02-15 17:00:31 +0000618 false, false, 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000619 // Load the updated vector.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000620 return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack(
621 DAG.getMachineFunction(), SPFI),
622 false, false, false, 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000623}
624
Mon P Wang4dd832d2008-12-09 05:46:39 +0000625
Eli Friedmana8f9a022009-05-27 02:16:40 +0000626SDValue SelectionDAGLegalize::
Andrew Trickef9de2a2013-05-25 02:42:55 +0000627ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, SDLoc dl) {
Eli Friedmana8f9a022009-05-27 02:16:40 +0000628 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
629 // SCALAR_TO_VECTOR requires that the type of the value being inserted
630 // match the element type of the vector being created, except for
631 // integers in which case the inserted value can be over width.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000632 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedmana8f9a022009-05-27 02:16:40 +0000633 if (Val.getValueType() == EltVT ||
634 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
635 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
636 Vec.getValueType(), Val);
637
638 unsigned NumElts = Vec.getValueType().getVectorNumElements();
639 // We generate a shuffle of InVec and ScVec, so the shuffle mask
640 // should be 0,1,2,3,4,5... with the appropriate element replaced with
641 // elt 0 of the RHS.
642 SmallVector<int, 8> ShufOps;
643 for (unsigned i = 0; i != NumElts; ++i)
644 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
645
646 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
647 &ShufOps[0]);
648 }
649 }
650 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
651}
652
Eli Friedmanaee3f622009-06-06 07:04:42 +0000653SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
654 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
655 // FIXME: We shouldn't do this for TargetConstantFP's.
656 // FIXME: move this to the DAG Combiner! Note that we can't regress due
657 // to phase ordering between legalized code and the dag combiner. This
658 // probably means that we need to integrate dag combiner and legalizer
659 // together.
660 // We generally can't do this one for long doubles.
Nadav Rotem2a148662012-07-11 11:02:16 +0000661 SDValue Chain = ST->getChain();
662 SDValue Ptr = ST->getBasePtr();
Eli Friedmanaee3f622009-06-06 07:04:42 +0000663 unsigned Alignment = ST->getAlignment();
664 bool isVolatile = ST->isVolatile();
David Greene39c6d012010-02-15 17:00:31 +0000665 bool isNonTemporal = ST->isNonTemporal();
Hal Finkelcc39b672014-07-24 12:16:19 +0000666 AAMDNodes AAInfo = ST->getAAInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000667 SDLoc dl(ST);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000668 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson9f944592009-08-11 20:47:22 +0000669 if (CFP->getValueType(0) == MVT::f32 &&
Dan Gohmane49e7422011-07-15 22:39:09 +0000670 TLI.isTypeLegal(MVT::i32)) {
Nadav Rotem2a148662012-07-11 11:02:16 +0000671 SDValue Con = DAG.getConstant(CFP->getValueAPF().
Eli Friedmanaee3f622009-06-06 07:04:42 +0000672 bitcastToAPInt().zextOrTrunc(32),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000673 SDLoc(CFP), MVT::i32);
Nadav Rotem2a148662012-07-11 11:02:16 +0000674 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000675 isVolatile, isNonTemporal, Alignment, AAInfo);
Chris Lattner6963c1f2010-09-21 17:42:31 +0000676 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000677
Chris Lattner6963c1f2010-09-21 17:42:31 +0000678 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedmanaee3f622009-06-06 07:04:42 +0000679 // If this target supports 64-bit registers, do a single 64-bit store.
Dan Gohmane49e7422011-07-15 22:39:09 +0000680 if (TLI.isTypeLegal(MVT::i64)) {
Nadav Rotem2a148662012-07-11 11:02:16 +0000681 SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000682 zextOrTrunc(64), SDLoc(CFP), MVT::i64);
Nadav Rotem2a148662012-07-11 11:02:16 +0000683 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000684 isVolatile, isNonTemporal, Alignment, AAInfo);
Chris Lattner6963c1f2010-09-21 17:42:31 +0000685 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000686
Dan Gohmane49e7422011-07-15 22:39:09 +0000687 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
Eli Friedmanaee3f622009-06-06 07:04:42 +0000688 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
689 // stores. If the target supports neither 32- nor 64-bits, this
690 // xform is certainly not worth it.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000691 const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
692 SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
693 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000694 if (DAG.getDataLayout().isBigEndian())
695 std::swap(Lo, Hi);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000696
Nadav Rotem2a148662012-07-11 11:02:16 +0000697 Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), isVolatile,
Hal Finkelcc39b672014-07-24 12:16:19 +0000698 isNonTemporal, Alignment, AAInfo);
Nadav Rotem2a148662012-07-11 11:02:16 +0000699 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000700 DAG.getConstant(4, dl, Ptr.getValueType()));
Nadav Rotem2a148662012-07-11 11:02:16 +0000701 Hi = DAG.getStore(Chain, dl, Hi, Ptr,
Chris Lattner6963c1f2010-09-21 17:42:31 +0000702 ST->getPointerInfo().getWithOffset(4),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000703 isVolatile, isNonTemporal, MinAlign(Alignment, 4U),
Hal Finkelcc39b672014-07-24 12:16:19 +0000704 AAInfo);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000705
Owen Anderson9f944592009-08-11 20:47:22 +0000706 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000707 }
708 }
709 }
Craig Topperc0196b12014-04-14 00:51:57 +0000710 return SDValue(nullptr, 0);
Eli Friedmanaee3f622009-06-06 07:04:42 +0000711}
712
Nadav Rotemde6fd282012-07-11 08:52:09 +0000713void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
714 StoreSDNode *ST = cast<StoreSDNode>(Node);
Nadav Rotem2a148662012-07-11 11:02:16 +0000715 SDValue Chain = ST->getChain();
716 SDValue Ptr = ST->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000717 SDLoc dl(Node);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000718
719 unsigned Alignment = ST->getAlignment();
720 bool isVolatile = ST->isVolatile();
721 bool isNonTemporal = ST->isNonTemporal();
Hal Finkelcc39b672014-07-24 12:16:19 +0000722 AAMDNodes AAInfo = ST->getAAInfo();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000723
724 if (!ST->isTruncatingStore()) {
725 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
726 ReplaceNode(ST, OptStore);
727 return;
728 }
729
730 {
Nadav Rotem2a148662012-07-11 11:02:16 +0000731 SDValue Value = ST->getValue();
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000732 MVT VT = Value.getSimpleValueType();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000733 switch (TLI.getOperationAction(ISD::STORE, VT)) {
734 default: llvm_unreachable("This action is not supported yet!");
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000735 case TargetLowering::Legal: {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000736 // If this is an unaligned store and the target doesn't support it,
737 // expand it.
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000738 EVT MemVT = ST->getMemoryVT();
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000739 unsigned AS = ST->getAddressSpace();
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000740 unsigned Align = ST->getAlignment();
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000741 const DataLayout &DL = DAG.getDataLayout();
742 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align))
743 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000744 break;
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000745 }
Nadav Rotem2a148662012-07-11 11:02:16 +0000746 case TargetLowering::Custom: {
747 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
Hal Finkelcec70132015-02-24 12:59:47 +0000748 if (Res && Res != SDValue(Node, 0))
Nadav Rotem2a148662012-07-11 11:02:16 +0000749 ReplaceNode(SDValue(Node, 0), Res);
750 return;
751 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000752 case TargetLowering::Promote: {
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000753 MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
Tom Stellardb785bd72012-12-10 21:41:54 +0000754 assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
755 "Can only promote stores to same size type");
756 Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000757 SDValue Result =
Nadav Rotem2a148662012-07-11 11:02:16 +0000758 DAG.getStore(Chain, dl, Value, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +0000759 ST->getPointerInfo(), isVolatile,
Hal Finkelcc39b672014-07-24 12:16:19 +0000760 isNonTemporal, Alignment, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000761 ReplaceNode(SDValue(Node, 0), Result);
762 break;
763 }
764 }
765 return;
766 }
767 } else {
Nadav Rotem2a148662012-07-11 11:02:16 +0000768 SDValue Value = ST->getValue();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000769
770 EVT StVT = ST->getMemoryVT();
771 unsigned StWidth = StVT.getSizeInBits();
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000772 auto &DL = DAG.getDataLayout();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000773
774 if (StWidth != StVT.getStoreSizeInBits()) {
775 // Promote to a byte-sized store with upper bits zero if not
776 // storing an integral number of bytes. For example, promote
777 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
778 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
779 StVT.getStoreSizeInBits());
Nadav Rotem2a148662012-07-11 11:02:16 +0000780 Value = DAG.getZeroExtendInReg(Value, dl, StVT);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000781 SDValue Result =
Nadav Rotem2a148662012-07-11 11:02:16 +0000782 DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
Sanjay Patelb06441a2014-11-21 18:05:59 +0000783 NVT, isVolatile, isNonTemporal, Alignment, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000784 ReplaceNode(SDValue(Node, 0), Result);
785 } else if (StWidth & (StWidth - 1)) {
786 // If not storing a power-of-2 number of bits, expand as two stores.
787 assert(!StVT.isVector() && "Unsupported truncstore!");
788 unsigned RoundWidth = 1 << Log2_32(StWidth);
789 assert(RoundWidth < StWidth);
790 unsigned ExtraWidth = StWidth - RoundWidth;
791 assert(ExtraWidth < RoundWidth);
792 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
793 "Store size not an integral number of bytes!");
794 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
795 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
796 SDValue Lo, Hi;
797 unsigned IncrementSize;
798
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +0000799 if (DL.isLittleEndian()) {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000800 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
801 // Store the bottom RoundWidth bits.
Nadav Rotem2a148662012-07-11 11:02:16 +0000802 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
Nadav Rotemde6fd282012-07-11 08:52:09 +0000803 RoundVT,
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000804 isVolatile, isNonTemporal, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +0000805 AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000806
807 // Store the remaining ExtraWidth bits.
808 IncrementSize = RoundWidth / 8;
Nadav Rotem2a148662012-07-11 11:02:16 +0000809 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000810 DAG.getConstant(IncrementSize, dl,
811 Ptr.getValueType()));
Mehdi Amini9639d652015-07-09 02:09:20 +0000812 Hi = DAG.getNode(
813 ISD::SRL, dl, Value.getValueType(), Value,
814 DAG.getConstant(RoundWidth, dl,
815 TLI.getShiftAmountTy(Value.getValueType(), DL)));
Nadav Rotem2a148662012-07-11 11:02:16 +0000816 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +0000817 ST->getPointerInfo().getWithOffset(IncrementSize),
818 ExtraVT, isVolatile, isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +0000819 MinAlign(Alignment, IncrementSize), AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000820 } else {
821 // Big endian - avoid unaligned stores.
822 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
823 // Store the top RoundWidth bits.
Mehdi Amini9639d652015-07-09 02:09:20 +0000824 Hi = DAG.getNode(
825 ISD::SRL, dl, Value.getValueType(), Value,
826 DAG.getConstant(ExtraWidth, dl,
827 TLI.getShiftAmountTy(Value.getValueType(), DL)));
Nadav Rotem2a148662012-07-11 11:02:16 +0000828 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000829 RoundVT, isVolatile, isNonTemporal, Alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +0000830 AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000831
832 // Store the remaining ExtraWidth bits.
833 IncrementSize = RoundWidth / 8;
Nadav Rotem2a148662012-07-11 11:02:16 +0000834 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000835 DAG.getConstant(IncrementSize, dl,
836 Ptr.getValueType()));
Nadav Rotem2a148662012-07-11 11:02:16 +0000837 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +0000838 ST->getPointerInfo().getWithOffset(IncrementSize),
839 ExtraVT, isVolatile, isNonTemporal,
Hal Finkelcc39b672014-07-24 12:16:19 +0000840 MinAlign(Alignment, IncrementSize), AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000841 }
842
843 // The order of the stores doesn't matter.
844 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
845 ReplaceNode(SDValue(Node, 0), Result);
846 } else {
Patrik Hagglundd7cdcf82012-12-19 08:28:51 +0000847 switch (TLI.getTruncStoreAction(ST->getValue().getSimpleValueType(),
848 StVT.getSimpleVT())) {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000849 default: llvm_unreachable("This action is not supported yet!");
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000850 case TargetLowering::Legal: {
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000851 EVT MemVT = ST->getMemoryVT();
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000852 unsigned AS = ST->getAddressSpace();
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000853 unsigned Align = ST->getAlignment();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000854 // If this is an unaligned store and the target doesn't support it,
855 // expand it.
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000856 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align))
857 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000858 break;
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000859 }
Nadav Rotem2a148662012-07-11 11:02:16 +0000860 case TargetLowering::Custom: {
861 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
Hal Finkelcec70132015-02-24 12:59:47 +0000862 if (Res && Res != SDValue(Node, 0))
Nadav Rotem2a148662012-07-11 11:02:16 +0000863 ReplaceNode(SDValue(Node, 0), Res);
864 return;
865 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000866 case TargetLowering::Expand:
867 assert(!StVT.isVector() &&
868 "Vector Stores are handled in LegalizeVectorOps");
869
870 // TRUNCSTORE:i16 i32 -> STORE i16
Nadav Rotem2a148662012-07-11 11:02:16 +0000871 assert(TLI.isTypeLegal(StVT) &&
872 "Do not know how to expand this store!");
873 Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000874 SDValue Result =
Nadav Rotem2a148662012-07-11 11:02:16 +0000875 DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000876 isVolatile, isNonTemporal, Alignment, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000877 ReplaceNode(SDValue(Node, 0), Result);
878 break;
879 }
880 }
881 }
882}
883
884void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
885 LoadSDNode *LD = cast<LoadSDNode>(Node);
Nadav Rotem2a148662012-07-11 11:02:16 +0000886 SDValue Chain = LD->getChain(); // The chain.
887 SDValue Ptr = LD->getBasePtr(); // The base pointer.
888 SDValue Value; // The value returned by the load op.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000889 SDLoc dl(Node);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000890
891 ISD::LoadExtType ExtType = LD->getExtensionType();
892 if (ExtType == ISD::NON_EXTLOAD) {
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000893 MVT VT = Node->getSimpleValueType(0);
Nadav Rotem2a148662012-07-11 11:02:16 +0000894 SDValue RVal = SDValue(Node, 0);
895 SDValue RChain = SDValue(Node, 1);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000896
897 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
898 default: llvm_unreachable("This action is not supported yet!");
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000899 case TargetLowering::Legal: {
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000900 EVT MemVT = LD->getMemoryVT();
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000901 unsigned AS = LD->getAddressSpace();
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000902 unsigned Align = LD->getAlignment();
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000903 const DataLayout &DL = DAG.getDataLayout();
Evan Chengc5735992012-09-18 01:34:40 +0000904 // If this is an unaligned load and the target doesn't support it,
905 // expand it.
Sanjay Patel0f9dcf82015-07-29 18:24:18 +0000906 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align))
907 ExpandUnalignedLoad(cast<LoadSDNode>(Node), DAG, TLI, RVal, RChain);
Evan Chengc5735992012-09-18 01:34:40 +0000908 break;
Matt Arsenault1b55dd92014-02-05 23:16:05 +0000909 }
Nadav Rotem2a148662012-07-11 11:02:16 +0000910 case TargetLowering::Custom: {
Evan Chengc5735992012-09-18 01:34:40 +0000911 SDValue Res = TLI.LowerOperation(RVal, DAG);
912 if (Res.getNode()) {
913 RVal = Res;
914 RChain = Res.getValue(1);
915 }
916 break;
Nadav Rotem2a148662012-07-11 11:02:16 +0000917 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000918 case TargetLowering::Promote: {
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +0000919 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Tom Stellard30e2aa52012-12-10 21:41:58 +0000920 assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
921 "Can only promote loads to same size type");
Nadav Rotemde6fd282012-07-11 08:52:09 +0000922
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000923 SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
Nadav Rotem2a148662012-07-11 11:02:16 +0000924 RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
925 RChain = Res.getValue(1);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000926 break;
927 }
928 }
Nadav Rotem2a148662012-07-11 11:02:16 +0000929 if (RChain.getNode() != Node) {
930 assert(RVal.getNode() != Node && "Load must be completely replaced");
931 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
932 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
Chandler Carruth411fb402014-07-26 05:49:40 +0000933 if (UpdatedNodes) {
934 UpdatedNodes->insert(RVal.getNode());
935 UpdatedNodes->insert(RChain.getNode());
936 }
Nadav Rotemde6fd282012-07-11 08:52:09 +0000937 ReplacedNode(Node);
938 }
939 return;
940 }
941
942 EVT SrcVT = LD->getMemoryVT();
943 unsigned SrcWidth = SrcVT.getSizeInBits();
944 unsigned Alignment = LD->getAlignment();
945 bool isVolatile = LD->isVolatile();
946 bool isNonTemporal = LD->isNonTemporal();
Louis Gerbarg67474e32014-07-31 21:45:05 +0000947 bool isInvariant = LD->isInvariant();
Hal Finkelcc39b672014-07-24 12:16:19 +0000948 AAMDNodes AAInfo = LD->getAAInfo();
Nadav Rotemde6fd282012-07-11 08:52:09 +0000949
950 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
951 // Some targets pretend to have an i1 loading operation, and actually
952 // load an i8. This trick is correct for ZEXTLOAD because the top 7
953 // bits are guaranteed to be zero; it helps the optimizers understand
954 // that these bits are zero. It is also useful for EXTLOAD, since it
955 // tells the optimizers that those bits are undefined. It would be
956 // nice to have an effective generic way of getting these benefits...
957 // Until such a way is found, don't insist on promoting i1 here.
958 (SrcVT != MVT::i1 ||
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000959 TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
960 TargetLowering::Promote)) {
Nadav Rotemde6fd282012-07-11 08:52:09 +0000961 // Promote to a byte-sized load if not loading an integral number of
962 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
963 unsigned NewWidth = SrcVT.getStoreSizeInBits();
964 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
965 SDValue Ch;
966
967 // The extra bits are guaranteed to be zero, since we stored them that
968 // way. A zext load from NVT thus automatically gives zext from SrcVT.
969
970 ISD::LoadExtType NewExtType =
971 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
972
973 SDValue Result =
974 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Nadav Rotem2a148662012-07-11 11:02:16 +0000975 Chain, Ptr, LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +0000976 NVT, isVolatile, isNonTemporal, isInvariant, Alignment,
977 AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +0000978
979 Ch = Result.getValue(1); // The chain.
980
981 if (ExtType == ISD::SEXTLOAD)
982 // Having the top bits zero doesn't help when sign extending.
983 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
984 Result.getValueType(),
985 Result, DAG.getValueType(SrcVT));
986 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
987 // All the top bits are guaranteed to be zero - inform the optimizers.
988 Result = DAG.getNode(ISD::AssertZext, dl,
989 Result.getValueType(), Result,
990 DAG.getValueType(SrcVT));
991
Nadav Rotem2a148662012-07-11 11:02:16 +0000992 Value = Result;
993 Chain = Ch;
Nadav Rotemde6fd282012-07-11 08:52:09 +0000994 } else if (SrcWidth & (SrcWidth - 1)) {
995 // If not loading a power-of-2 number of bits, expand as two loads.
996 assert(!SrcVT.isVector() && "Unsupported extload!");
997 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
998 assert(RoundWidth < SrcWidth);
999 unsigned ExtraWidth = SrcWidth - RoundWidth;
1000 assert(ExtraWidth < RoundWidth);
1001 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1002 "Load size not an integral number of bytes!");
1003 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1004 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1005 SDValue Lo, Hi, Ch;
1006 unsigned IncrementSize;
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00001007 auto &DL = DAG.getDataLayout();
Nadav Rotemde6fd282012-07-11 08:52:09 +00001008
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00001009 if (DL.isLittleEndian()) {
Nadav Rotemde6fd282012-07-11 08:52:09 +00001010 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1011 // Load the bottom RoundWidth bits.
1012 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Nadav Rotem2a148662012-07-11 11:02:16 +00001013 Chain, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +00001014 LD->getPointerInfo(), RoundVT, isVolatile,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001015 isNonTemporal, isInvariant, Alignment, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +00001016
1017 // Load the remaining ExtraWidth bits.
1018 IncrementSize = RoundWidth / 8;
Nadav Rotem2a148662012-07-11 11:02:16 +00001019 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001020 DAG.getConstant(IncrementSize, dl,
1021 Ptr.getValueType()));
Nadav Rotem2a148662012-07-11 11:02:16 +00001022 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +00001023 LD->getPointerInfo().getWithOffset(IncrementSize),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001024 ExtraVT, isVolatile, isNonTemporal, isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00001025 MinAlign(Alignment, IncrementSize), AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +00001026
1027 // Build a factor node to remember that this load is independent of
1028 // the other one.
1029 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1030 Hi.getValue(1));
1031
1032 // Move the top bits to the right place.
Mehdi Amini9639d652015-07-09 02:09:20 +00001033 Hi = DAG.getNode(
1034 ISD::SHL, dl, Hi.getValueType(), Hi,
1035 DAG.getConstant(RoundWidth, dl,
1036 TLI.getShiftAmountTy(Hi.getValueType(), DL)));
Nadav Rotemde6fd282012-07-11 08:52:09 +00001037
1038 // Join the hi and lo parts.
Nadav Rotem2a148662012-07-11 11:02:16 +00001039 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Nadav Rotemde6fd282012-07-11 08:52:09 +00001040 } else {
1041 // Big endian - avoid unaligned loads.
1042 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1043 // Load the top RoundWidth bits.
Nadav Rotem2a148662012-07-11 11:02:16 +00001044 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +00001045 LD->getPointerInfo(), RoundVT, isVolatile,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001046 isNonTemporal, isInvariant, Alignment, AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +00001047
1048 // Load the remaining ExtraWidth bits.
1049 IncrementSize = RoundWidth / 8;
Nadav Rotem2a148662012-07-11 11:02:16 +00001050 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001051 DAG.getConstant(IncrementSize, dl,
1052 Ptr.getValueType()));
Nadav Rotemde6fd282012-07-11 08:52:09 +00001053 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Nadav Rotem2a148662012-07-11 11:02:16 +00001054 dl, Node->getValueType(0), Chain, Ptr,
Nadav Rotemde6fd282012-07-11 08:52:09 +00001055 LD->getPointerInfo().getWithOffset(IncrementSize),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001056 ExtraVT, isVolatile, isNonTemporal, isInvariant,
Hal Finkelcc39b672014-07-24 12:16:19 +00001057 MinAlign(Alignment, IncrementSize), AAInfo);
Nadav Rotemde6fd282012-07-11 08:52:09 +00001058
1059 // Build a factor node to remember that this load is independent of
1060 // the other one.
1061 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1062 Hi.getValue(1));
1063
1064 // Move the top bits to the right place.
Mehdi Amini9639d652015-07-09 02:09:20 +00001065 Hi = DAG.getNode(
1066 ISD::SHL, dl, Hi.getValueType(), Hi,
1067 DAG.getConstant(ExtraWidth, dl,
1068 TLI.getShiftAmountTy(Hi.getValueType(), DL)));
Nadav Rotemde6fd282012-07-11 08:52:09 +00001069
1070 // Join the hi and lo parts.
Nadav Rotem2a148662012-07-11 11:02:16 +00001071 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Nadav Rotemde6fd282012-07-11 08:52:09 +00001072 }
1073
Nadav Rotem2a148662012-07-11 11:02:16 +00001074 Chain = Ch;
Nadav Rotemde6fd282012-07-11 08:52:09 +00001075 } else {
1076 bool isCustom = false;
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +00001077 switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
1078 SrcVT.getSimpleVT())) {
Nadav Rotemde6fd282012-07-11 08:52:09 +00001079 default: llvm_unreachable("This action is not supported yet!");
1080 case TargetLowering::Custom:
Matt Arsenault95b714c2014-03-11 00:01:25 +00001081 isCustom = true;
1082 // FALLTHROUGH
Nadav Rotem2a148662012-07-11 11:02:16 +00001083 case TargetLowering::Legal: {
Matt Arsenault95b714c2014-03-11 00:01:25 +00001084 Value = SDValue(Node, 0);
1085 Chain = SDValue(Node, 1);
Nadav Rotemde6fd282012-07-11 08:52:09 +00001086
Matt Arsenault95b714c2014-03-11 00:01:25 +00001087 if (isCustom) {
1088 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
1089 if (Res.getNode()) {
1090 Value = Res;
1091 Chain = Res.getValue(1);
1092 }
1093 } else {
Sanjay Patel0f9dcf82015-07-29 18:24:18 +00001094 // If this is an unaligned load and the target doesn't support it,
1095 // expand it.
Matt Arsenault95b714c2014-03-11 00:01:25 +00001096 EVT MemVT = LD->getMemoryVT();
1097 unsigned AS = LD->getAddressSpace();
Matt Arsenault6f2a5262014-07-27 17:46:40 +00001098 unsigned Align = LD->getAlignment();
Sanjay Patel0f9dcf82015-07-29 18:24:18 +00001099 const DataLayout &DL = DAG.getDataLayout();
1100 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align))
1101 ExpandUnalignedLoad(cast<LoadSDNode>(Node), DAG, TLI, Value, Chain);
Matt Arsenault95b714c2014-03-11 00:01:25 +00001102 }
1103 break;
Nadav Rotem2a148662012-07-11 11:02:16 +00001104 }
Nadav Rotemde6fd282012-07-11 08:52:09 +00001105 case TargetLowering::Expand:
Matt Arsenaultacd68b52015-09-09 01:12:27 +00001106 EVT DestVT = Node->getValueType(0);
1107 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
Matt Arsenaultbd223422015-01-14 01:35:17 +00001108 // If the source type is not legal, see if there is a legal extload to
1109 // an intermediate type that we can then extend further.
1110 EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
1111 if (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
1112 TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT)) {
1113 // If we are loading a legal type, this is a non-extload followed by a
1114 // full extend.
1115 ISD::LoadExtType MidExtType =
1116 (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
1117
1118 SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
1119 SrcVT, LD->getMemOperand());
1120 unsigned ExtendOp =
1121 ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType);
1122 Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1123 Chain = Load.getValue(1);
Matt Arsenault95b714c2014-03-11 00:01:25 +00001124 break;
Matt Arsenault95b714c2014-03-11 00:01:25 +00001125 }
Matt Arsenaultacd68b52015-09-09 01:12:27 +00001126
1127 // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
1128 // normal undefined upper bits behavior to allow using an in-reg extend
1129 // with the illegal FP type, so load as an integer and do the
1130 // from-integer conversion.
1131 if (SrcVT.getScalarType() == MVT::f16) {
1132 EVT ISrcVT = SrcVT.changeTypeToInteger();
1133 EVT IDestVT = DestVT.changeTypeToInteger();
1134 EVT LoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
1135
1136 SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, LoadVT,
1137 Chain, Ptr, ISrcVT,
1138 LD->getMemOperand());
1139 Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result);
1140 Chain = Result.getValue(1);
1141 break;
1142 }
Matt Arsenault95b714c2014-03-11 00:01:25 +00001143 }
Nadav Rotemde6fd282012-07-11 08:52:09 +00001144
Matt Arsenault95b714c2014-03-11 00:01:25 +00001145 assert(!SrcVT.isVector() &&
1146 "Vector Loads are handled in LegalizeVectorOps");
Nadav Rotemde6fd282012-07-11 08:52:09 +00001147
Matt Arsenault95b714c2014-03-11 00:01:25 +00001148 // FIXME: This does not work for vectors on most targets. Sign-
1149 // and zero-extend operations are currently folded into extending
1150 // loads, whether they are legal or not, and then we end up here
1151 // without any support for legalizing them.
1152 assert(ExtType != ISD::EXTLOAD &&
1153 "EXTLOAD should always be supported!");
1154 // Turn the unsupported load into an EXTLOAD followed by an
1155 // explicit zero/sign extend inreg.
1156 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
1157 Node->getValueType(0),
1158 Chain, Ptr, SrcVT,
1159 LD->getMemOperand());
1160 SDValue ValRes;
1161 if (ExtType == ISD::SEXTLOAD)
1162 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1163 Result.getValueType(),
1164 Result, DAG.getValueType(SrcVT));
1165 else
Sanjay Patelb06441a2014-11-21 18:05:59 +00001166 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Matt Arsenault95b714c2014-03-11 00:01:25 +00001167 Value = ValRes;
1168 Chain = Result.getValue(1);
1169 break;
Nadav Rotemde6fd282012-07-11 08:52:09 +00001170 }
1171 }
1172
1173 // Since loads produce two values, make sure to remember that we legalized
1174 // both of them.
Nadav Rotem2a148662012-07-11 11:02:16 +00001175 if (Chain.getNode() != Node) {
1176 assert(Value.getNode() != Node && "Load must be completely replaced");
1177 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
1178 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
Chandler Carruth411fb402014-07-26 05:49:40 +00001179 if (UpdatedNodes) {
1180 UpdatedNodes->insert(Value.getNode());
1181 UpdatedNodes->insert(Chain.getNode());
1182 }
Nadav Rotemde6fd282012-07-11 08:52:09 +00001183 ReplacedNode(Node);
1184 }
1185}
1186
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00001187/// Return a legal replacement for the given operation, with all legal operands.
Dan Gohman198b7ff2011-11-03 21:49:52 +00001188void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
Chandler Carruthb1432742014-07-28 17:55:07 +00001189 DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
1190
Dan Gohman198b7ff2011-11-03 21:49:52 +00001191 if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
1192 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001193
Pete Cooperaf61ac72015-06-26 19:23:20 +00001194#ifndef NDEBUG
Eli Friedman5e0d1502009-05-24 02:46:31 +00001195 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohmane49e7422011-07-15 22:39:09 +00001196 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
1197 TargetLowering::TypeLegal &&
Eli Friedman5e0d1502009-05-24 02:46:31 +00001198 "Unexpected illegal type!");
1199
Pete Cooper8fc121d2015-06-26 19:08:33 +00001200 for (const SDValue &Op : Node->op_values())
Dan Gohmane49e7422011-07-15 22:39:09 +00001201 assert((TLI.getTypeAction(*DAG.getContext(),
Pete Cooper8fc121d2015-06-26 19:08:33 +00001202 Op.getValueType()) == TargetLowering::TypeLegal ||
1203 Op.getOpcode() == ISD::TargetConstant) &&
1204 "Unexpected illegal type!");
Pete Cooperaf61ac72015-06-26 19:23:20 +00001205#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00001206
Eli Friedman21d349b2009-05-27 01:25:56 +00001207 // Figure out the correct action; the way to query this varies by opcode
Bill Wendlingfb4ee9b2011-01-26 22:21:35 +00001208 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman21d349b2009-05-27 01:25:56 +00001209 bool SimpleFinishLegalizing = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001210 switch (Node->getOpcode()) {
Eli Friedman21d349b2009-05-27 01:25:56 +00001211 case ISD::INTRINSIC_W_CHAIN:
1212 case ISD::INTRINSIC_WO_CHAIN:
1213 case ISD::INTRINSIC_VOID:
Eli Friedman21d349b2009-05-27 01:25:56 +00001214 case ISD::STACKSAVE:
Owen Anderson9f944592009-08-11 20:47:22 +00001215 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman21d349b2009-05-27 01:25:56 +00001216 break;
Hal Finkel71c2ba32012-03-24 03:53:52 +00001217 case ISD::VAARG:
1218 Action = TLI.getOperationAction(Node->getOpcode(),
1219 Node->getValueType(0));
1220 if (Action != TargetLowering::Promote)
1221 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
1222 break;
Tim Northoverfd7e4242014-07-17 10:51:23 +00001223 case ISD::FP_TO_FP16:
Eli Friedman21d349b2009-05-27 01:25:56 +00001224 case ISD::SINT_TO_FP:
1225 case ISD::UINT_TO_FP:
1226 case ISD::EXTRACT_VECTOR_ELT:
1227 Action = TLI.getOperationAction(Node->getOpcode(),
1228 Node->getOperand(0).getValueType());
1229 break;
1230 case ISD::FP_ROUND_INREG:
1231 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001232 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman21d349b2009-05-27 01:25:56 +00001233 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
1234 break;
1235 }
Eli Friedman342e8df2011-08-24 20:50:09 +00001236 case ISD::ATOMIC_STORE: {
1237 Action = TLI.getOperationAction(Node->getOpcode(),
1238 Node->getOperand(2).getValueType());
1239 break;
1240 }
Eli Friedmane1bc3792009-05-28 03:06:16 +00001241 case ISD::SELECT_CC:
1242 case ISD::SETCC:
1243 case ISD::BR_CC: {
1244 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
Hans Wennborgdcc25002015-11-19 16:35:08 +00001245 Node->getOpcode() == ISD::SETCC ? 2 :
1246 Node->getOpcode() == ISD::SETCCE ? 3 : 1;
Eli Friedmane1bc3792009-05-28 03:06:16 +00001247 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Patrik Hagglunddeee9002012-12-19 10:09:26 +00001248 MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
Eli Friedmane1bc3792009-05-28 03:06:16 +00001249 ISD::CondCode CCCode =
1250 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
1251 Action = TLI.getCondCodeAction(CCCode, OpVT);
1252 if (Action == TargetLowering::Legal) {
1253 if (Node->getOpcode() == ISD::SELECT_CC)
1254 Action = TLI.getOperationAction(Node->getOpcode(),
1255 Node->getValueType(0));
1256 else
1257 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
1258 }
1259 break;
1260 }
Eli Friedman21d349b2009-05-27 01:25:56 +00001261 case ISD::LOAD:
1262 case ISD::STORE:
Eli Friedman5df72022009-05-28 03:56:57 +00001263 // FIXME: Model these properly. LOAD and STORE are complicated, and
1264 // STORE expects the unlegalized operand in some cases.
1265 SimpleFinishLegalizing = false;
1266 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00001267 case ISD::CALLSEQ_START:
1268 case ISD::CALLSEQ_END:
Eli Friedman5df72022009-05-28 03:56:57 +00001269 // FIXME: This shouldn't be necessary. These nodes have special properties
1270 // dealing with the recursive nature of legalization. Removing this
1271 // special case should be done as part of making LegalizeDAG non-recursive.
1272 SimpleFinishLegalizing = false;
1273 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00001274 case ISD::EXTRACT_ELEMENT:
1275 case ISD::FLT_ROUNDS_:
Eli Friedman21d349b2009-05-27 01:25:56 +00001276 case ISD::FPOWI:
1277 case ISD::MERGE_VALUES:
1278 case ISD::EH_RETURN:
1279 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachdc0a0652010-07-06 23:44:52 +00001280 case ISD::EH_SJLJ_SETJMP:
1281 case ISD::EH_SJLJ_LONGJMP:
Matthias Braun3cd00c12015-07-16 22:34:16 +00001282 case ISD::EH_SJLJ_SETUP_DISPATCH:
Eli Friedmand6f28342009-05-27 03:33:44 +00001283 // These operations lie about being legal: when they claim to be legal,
1284 // they should actually be expanded.
Eli Friedman21d349b2009-05-27 01:25:56 +00001285 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1286 if (Action == TargetLowering::Legal)
1287 Action = TargetLowering::Expand;
1288 break;
Duncan Sandsa0984362011-09-06 13:37:06 +00001289 case ISD::INIT_TRAMPOLINE:
1290 case ISD::ADJUST_TRAMPOLINE:
Eli Friedman21d349b2009-05-27 01:25:56 +00001291 case ISD::FRAMEADDR:
1292 case ISD::RETURNADDR:
Eli Friedman2892d822009-05-27 12:20:41 +00001293 // These operations lie about being legal: when they claim to be legal,
1294 // they should actually be custom-lowered.
1295 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1296 if (Action == TargetLowering::Legal)
1297 Action = TargetLowering::Custom;
Eli Friedman21d349b2009-05-27 01:25:56 +00001298 break;
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00001299 case ISD::READCYCLECOUNTER:
1300 // READCYCLECOUNTER returns an i64, even if type legalization might have
1301 // expanded that to several smaller types.
1302 Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
1303 break;
Renato Golinc7aea402014-05-06 16:51:25 +00001304 case ISD::READ_REGISTER:
1305 case ISD::WRITE_REGISTER:
1306 // Named register is legal in the DAG, but blocked by register name
1307 // selection if not implemented by target (to chose the correct register)
1308 // They'll be converted to Copy(To/From)Reg.
1309 Action = TargetLowering::Legal;
1310 break;
Shuxin Yangcdde0592012-10-19 20:11:16 +00001311 case ISD::DEBUGTRAP:
1312 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1313 if (Action == TargetLowering::Expand) {
1314 // replace ISD::DEBUGTRAP with ISD::TRAP
1315 SDValue NewVal;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001316 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
Shuxin Yang1479fcd2012-10-19 23:00:20 +00001317 Node->getOperand(0));
Shuxin Yangcdde0592012-10-19 20:11:16 +00001318 ReplaceNode(Node, NewVal.getNode());
1319 LegalizeOp(NewVal.getNode());
1320 return;
1321 }
1322 break;
1323
Chris Lattnerdc750592005-01-07 07:47:09 +00001324 default:
Chris Lattner3eb86932005-05-14 06:34:48 +00001325 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman21d349b2009-05-27 01:25:56 +00001326 Action = TargetLowering::Legal;
1327 } else {
1328 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattner3eb86932005-05-14 06:34:48 +00001329 }
Eli Friedman21d349b2009-05-27 01:25:56 +00001330 break;
1331 }
1332
1333 if (SimpleFinishLegalizing) {
Peter Collingbourne8eb05fd2012-05-20 18:36:15 +00001334 SDNode *NewNode = Node;
Eli Friedman21d349b2009-05-27 01:25:56 +00001335 switch (Node->getOpcode()) {
1336 default: break;
Eli Friedman21d349b2009-05-27 01:25:56 +00001337 case ISD::SHL:
1338 case ISD::SRL:
1339 case ISD::SRA:
1340 case ISD::ROTL:
1341 case ISD::ROTR:
1342 // Legalizing shifts/rotates requires adjusting the shift amount
1343 // to the appropriate width.
Peter Collingbourne8eb05fd2012-05-20 18:36:15 +00001344 if (!Node->getOperand(1).getValueType().isVector()) {
1345 SDValue SAO =
1346 DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
1347 Node->getOperand(1));
Dan Gohman198b7ff2011-11-03 21:49:52 +00001348 HandleSDNode Handle(SAO);
1349 LegalizeOp(SAO.getNode());
Peter Collingbourne8eb05fd2012-05-20 18:36:15 +00001350 NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
1351 Handle.getValue());
Dan Gohman198b7ff2011-11-03 21:49:52 +00001352 }
Eli Friedman21d349b2009-05-27 01:25:56 +00001353 break;
Dan Gohman4906f732009-08-18 23:36:17 +00001354 case ISD::SRL_PARTS:
1355 case ISD::SRA_PARTS:
1356 case ISD::SHL_PARTS:
1357 // Legalizing shifts/rotates requires adjusting the shift amount
1358 // to the appropriate width.
Peter Collingbourne8eb05fd2012-05-20 18:36:15 +00001359 if (!Node->getOperand(2).getValueType().isVector()) {
1360 SDValue SAO =
1361 DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
1362 Node->getOperand(2));
Dan Gohman198b7ff2011-11-03 21:49:52 +00001363 HandleSDNode Handle(SAO);
1364 LegalizeOp(SAO.getNode());
Peter Collingbourne8eb05fd2012-05-20 18:36:15 +00001365 NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
1366 Node->getOperand(1),
1367 Handle.getValue());
Dan Gohman198b7ff2011-11-03 21:49:52 +00001368 }
Dan Gohman2fa67c92009-08-18 23:52:48 +00001369 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00001370 }
1371
Dan Gohman198b7ff2011-11-03 21:49:52 +00001372 if (NewNode != Node) {
Chandler Carruth411fb402014-07-26 05:49:40 +00001373 ReplaceNode(Node, NewNode);
Dan Gohman198b7ff2011-11-03 21:49:52 +00001374 Node = NewNode;
1375 }
Eli Friedman21d349b2009-05-27 01:25:56 +00001376 switch (Action) {
1377 case TargetLowering::Legal:
Dan Gohman198b7ff2011-11-03 21:49:52 +00001378 return;
Nadav Rotem2a148662012-07-11 11:02:16 +00001379 case TargetLowering::Custom: {
Eli Friedman21d349b2009-05-27 01:25:56 +00001380 // FIXME: The handling for custom lowering with multiple results is
1381 // a complete mess.
Nadav Rotem2a148662012-07-11 11:02:16 +00001382 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
1383 if (Res.getNode()) {
Chandler Carruth98655fa2014-07-26 05:52:51 +00001384 if (!(Res.getNode() != Node || Res.getResNo() != 0))
1385 return;
1386
1387 if (Node->getNumValues() == 1) {
1388 // We can just directly replace this node with the lowered value.
1389 ReplaceNode(SDValue(Node, 0), Res);
1390 return;
Eli Friedman21d349b2009-05-27 01:25:56 +00001391 }
Chandler Carruth98655fa2014-07-26 05:52:51 +00001392
1393 SmallVector<SDValue, 8> ResultVals;
1394 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1395 ResultVals.push_back(Res.getValue(i));
1396 ReplaceNode(Node, ResultVals.data());
Dan Gohman198b7ff2011-11-03 21:49:52 +00001397 return;
Eli Friedman21d349b2009-05-27 01:25:56 +00001398 }
Nadav Rotem2a148662012-07-11 11:02:16 +00001399 }
Eli Friedman21d349b2009-05-27 01:25:56 +00001400 // FALL THROUGH
1401 case TargetLowering::Expand:
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00001402 if (ExpandNode(Node))
1403 return;
1404 // FALL THROUGH
1405 case TargetLowering::LibCall:
1406 ConvertNodeToLibcall(Node);
Dan Gohman198b7ff2011-11-03 21:49:52 +00001407 return;
Eli Friedman21d349b2009-05-27 01:25:56 +00001408 case TargetLowering::Promote:
Dan Gohman198b7ff2011-11-03 21:49:52 +00001409 PromoteNode(Node);
1410 return;
Eli Friedman21d349b2009-05-27 01:25:56 +00001411 }
1412 }
1413
1414 switch (Node->getOpcode()) {
1415 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00001416#ifndef NDEBUG
David Greeneae4f2662010-01-05 01:24:53 +00001417 dbgs() << "NODE: ";
1418 Node->dump( &DAG);
1419 dbgs() << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00001420#endif
Craig Topperee4dab52012-02-05 08:31:47 +00001421 llvm_unreachable("Do not know how to legalize this operator!");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001422
Dan Gohman198b7ff2011-11-03 21:49:52 +00001423 case ISD::CALLSEQ_START:
Dan Gohman9b9c9702011-10-29 00:41:52 +00001424 case ISD::CALLSEQ_END:
Dan Gohman198b7ff2011-11-03 21:49:52 +00001425 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001426 case ISD::LOAD: {
Nadav Rotemde6fd282012-07-11 08:52:09 +00001427 return LegalizeLoadOps(Node);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001428 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001429 case ISD::STORE: {
Nadav Rotemde6fd282012-07-11 08:52:09 +00001430 return LegalizeStoreOps(Node);
Evan Cheng31d15fa2005-12-23 07:29:34 +00001431 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001432 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001433}
1434
Eli Friedman40afdb62009-05-23 22:37:25 +00001435SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1436 SDValue Vec = Op.getOperand(0);
1437 SDValue Idx = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001438 SDLoc dl(Op);
Hal Finkel90adf0f2014-03-30 15:10:18 +00001439
1440 // Before we generate a new store to a temporary stack slot, see if there is
1441 // already one that we can use. There often is because when we scalarize
1442 // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
1443 // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
1444 // the vector. If all are expanded here, we don't want one store per vector
1445 // element.
1446 SDValue StackPtr, Ch;
1447 for (SDNode::use_iterator UI = Vec.getNode()->use_begin(),
1448 UE = Vec.getNode()->use_end(); UI != UE; ++UI) {
1449 SDNode *User = *UI;
1450 if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
1451 if (ST->isIndexed() || ST->isTruncatingStore() ||
1452 ST->getValue() != Vec)
1453 continue;
1454
1455 // Make sure that nothing else could have stored into the destination of
1456 // this store.
1457 if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
1458 continue;
1459
1460 StackPtr = ST->getBasePtr();
1461 Ch = SDValue(ST, 0);
1462 break;
1463 }
1464 }
1465
1466 if (!Ch.getNode()) {
1467 // Store the value to a temporary stack slot, then LOAD the returned part.
1468 StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1469 Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1470 MachinePointerInfo(), false, false, 0);
1471 }
Eli Friedman40afdb62009-05-23 22:37:25 +00001472
1473 // Add the offset to the index.
Dan Gohman9b80f862010-02-25 15:20:39 +00001474 unsigned EltSize =
1475 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman40afdb62009-05-23 22:37:25 +00001476 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001477 DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType()));
Eli Friedman40afdb62009-05-23 22:37:25 +00001478
Mehdi Amini44ede332015-07-09 02:09:04 +00001479 Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout()));
Eli Friedman40afdb62009-05-23 22:37:25 +00001480 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1481
Ahmed Bougachac8097612015-03-09 22:51:05 +00001482 SDValue NewLoad;
1483
Eli Friedman2b77eef2009-07-09 22:01:03 +00001484 if (Op.getValueType().isVector())
Ahmed Bougachac8097612015-03-09 22:51:05 +00001485 NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1486 MachinePointerInfo(), false, false, false, 0);
1487 else
1488 NewLoad = DAG.getExtLoad(
1489 ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, MachinePointerInfo(),
1490 Vec.getValueType().getVectorElementType(), false, false, false, 0);
1491
1492 // Replace the chain going out of the store, by the one out of the load.
1493 DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
1494
1495 // We introduced a cycle though, so update the loads operands, making sure
1496 // to use the original store's chain as an incoming chain.
1497 SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(),
1498 NewLoad->op_end());
1499 NewLoadOperands[0] = Ch;
1500 NewLoad =
1501 SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
1502 return NewLoad;
Eli Friedman40afdb62009-05-23 22:37:25 +00001503}
1504
David Greenebab5e6e2011-01-26 19:13:22 +00001505SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1506 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1507
1508 SDValue Vec = Op.getOperand(0);
1509 SDValue Part = Op.getOperand(1);
1510 SDValue Idx = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001511 SDLoc dl(Op);
David Greenebab5e6e2011-01-26 19:13:22 +00001512
1513 // Store the value to a temporary stack slot, then LOAD the returned part.
1514
1515 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1516 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001517 MachinePointerInfo PtrInfo =
1518 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
David Greenebab5e6e2011-01-26 19:13:22 +00001519
1520 // First store the whole vector.
1521 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1522 false, false, 0);
1523
1524 // Then store the inserted part.
1525
1526 // Add the offset to the index.
1527 unsigned EltSize =
1528 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1529
1530 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001531 DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType()));
Mehdi Amini44ede332015-07-09 02:09:04 +00001532 Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout()));
David Greenebab5e6e2011-01-26 19:13:22 +00001533
1534 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1535 StackPtr);
1536
1537 // Store the subvector.
Owen Andersonb5a25992014-11-18 20:50:19 +00001538 Ch = DAG.getStore(Ch, dl, Part, SubStackPtr,
David Greenebab5e6e2011-01-26 19:13:22 +00001539 MachinePointerInfo(), false, false, 0);
1540
1541 // Finally, load the updated vector.
1542 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001543 false, false, false, 0);
David Greenebab5e6e2011-01-26 19:13:22 +00001544}
1545
Eli Friedmanaee3f622009-06-06 07:04:42 +00001546SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1547 // We can't handle this case efficiently. Allocate a sufficiently
1548 // aligned object on the stack, store each element into it, then load
1549 // the result as a vector.
1550 // Create the stack frame object.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001551 EVT VT = Node->getValueType(0);
Dale Johannesenb91eba32009-11-21 00:53:23 +00001552 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001553 SDLoc dl(Node);
Eli Friedmanaee3f622009-06-06 07:04:42 +00001554 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001555 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001556 MachinePointerInfo PtrInfo =
1557 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
Eli Friedmanaee3f622009-06-06 07:04:42 +00001558
1559 // Emit a store of each element to the stack slot.
1560 SmallVector<SDValue, 8> Stores;
Dan Gohman9b80f862010-02-25 15:20:39 +00001561 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedmanaee3f622009-06-06 07:04:42 +00001562 // Store (in the right endianness) the elements to memory.
1563 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1564 // Ignore undef elements.
1565 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1566
1567 unsigned Offset = TypeByteSize*i;
1568
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001569 SDValue Idx = DAG.getConstant(Offset, dl, FIPtr.getValueType());
Eli Friedmanaee3f622009-06-06 07:04:42 +00001570 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1571
Dan Gohman2a8e3772010-02-25 20:30:49 +00001572 // If the destination vector element type is narrower than the source
1573 // element type, only store the bits necessary.
1574 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesenb91eba32009-11-21 00:53:23 +00001575 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattner1ffcf522010-09-21 16:36:31 +00001576 Node->getOperand(i), Idx,
1577 PtrInfo.getWithOffset(Offset),
David Greene39c6d012010-02-15 17:00:31 +00001578 EltVT, false, false, 0));
Mon P Wang586d9972010-01-24 00:05:03 +00001579 } else
Jim Grosbach9b7755f2010-07-02 17:41:59 +00001580 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner1ffcf522010-09-21 16:36:31 +00001581 Node->getOperand(i), Idx,
1582 PtrInfo.getWithOffset(Offset),
David Greene39c6d012010-02-15 17:00:31 +00001583 false, false, 0));
Eli Friedmanaee3f622009-06-06 07:04:42 +00001584 }
1585
1586 SDValue StoreChain;
1587 if (!Stores.empty()) // Not all undef elements?
Craig Topper48d114b2014-04-26 18:35:24 +00001588 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
Eli Friedmanaee3f622009-06-06 07:04:42 +00001589 else
1590 StoreChain = DAG.getEntryNode();
1591
1592 // Result is a load from the stack slot.
Stephen Lincfe7f352013-07-08 00:37:03 +00001593 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001594 false, false, false, 0);
Eli Friedmanaee3f622009-06-06 07:04:42 +00001595}
1596
Matthias Braunb9610a62015-11-12 01:02:47 +00001597namespace {
1598/// Keeps track of state when getting the sign of a floating-point value as an
1599/// integer.
1600struct FloatSignAsInt {
1601 EVT FloatVT;
1602 SDValue Chain;
1603 SDValue FloatPtr;
1604 SDValue IntPtr;
1605 MachinePointerInfo IntPointerInfo;
1606 MachinePointerInfo FloatPointerInfo;
1607 SDValue IntValue;
1608 APInt SignMask;
1609};
1610}
Duncan Sands4c55f762010-03-12 11:45:06 +00001611
Matthias Braunb9610a62015-11-12 01:02:47 +00001612/// Bitcast a floating-point value to an integer value. Only bitcast the part
1613/// containing the sign bit if the target has no integer value capable of
1614/// holding all bits of the floating-point value.
1615void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
1616 SDLoc DL, SDValue Value) const {
1617 EVT FloatVT = Value.getValueType();
1618 unsigned NumBits = FloatVT.getSizeInBits();
1619 State.FloatVT = FloatVT;
1620 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
1621 // Convert to an integer of the same size.
Dan Gohmane49e7422011-07-15 22:39:09 +00001622 if (TLI.isTypeLegal(IVT)) {
Matthias Braunb9610a62015-11-12 01:02:47 +00001623 State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
1624 State.SignMask = APInt::getSignBit(NumBits);
1625 return;
Eli Friedman2892d822009-05-27 12:20:41 +00001626 }
Matthias Braunb9610a62015-11-12 01:02:47 +00001627
1628 auto &DataLayout = DAG.getDataLayout();
1629 // Store the float to memory, then load the sign part out as an integer.
1630 MVT LoadTy = TLI.getRegisterType(*DAG.getContext(), MVT::i8);
1631 // First create a temporary that is aligned for both the load and store.
1632 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1633 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1634 // Then store the float to it.
1635 State.FloatPtr = StackPtr;
1636 MachineFunction &MF = DAG.getMachineFunction();
1637 State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
1638 State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
1639 State.FloatPointerInfo, false, false, 0);
1640
1641 SDValue IntPtr;
1642 if (DataLayout.isBigEndian()) {
1643 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1644 // Load out a legal integer with the same sign bit as the float.
1645 IntPtr = StackPtr;
1646 State.IntPointerInfo = State.FloatPointerInfo;
1647 } else {
1648 // Advance the pointer so that the loaded byte will contain the sign bit.
1649 unsigned ByteOffset = (FloatVT.getSizeInBits() / 8) - 1;
1650 IntPtr = DAG.getNode(ISD::ADD, DL, StackPtr.getValueType(), StackPtr,
1651 DAG.getConstant(ByteOffset, DL, StackPtr.getValueType()));
1652 State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
1653 ByteOffset);
1654 }
1655
1656 State.IntPtr = IntPtr;
1657 State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain,
1658 IntPtr, State.IntPointerInfo, MVT::i8,
1659 false, false, false, 0);
1660 State.SignMask = APInt::getOneBitSet(LoadTy.getSizeInBits(), 7);
1661}
1662
1663/// Replace the integer value produced by getSignAsIntValue() with a new value
1664/// and cast the result back to a floating-point type.
1665SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
1666 SDLoc DL, SDValue NewIntValue) const {
1667 if (!State.Chain)
1668 return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
1669
1670 // Override the part containing the sign bit in the value stored on the stack.
1671 SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
1672 State.IntPointerInfo, MVT::i8, false, false,
1673 0);
1674 return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
1675 State.FloatPointerInfo, false, false, false, 0);
1676}
1677
1678SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
1679 SDLoc DL(Node);
1680 SDValue Mag = Node->getOperand(0);
1681 SDValue Sign = Node->getOperand(1);
1682
1683 // Get sign bit into an integer value.
1684 FloatSignAsInt SignAsInt;
1685 getSignAsIntValue(SignAsInt, DL, Sign);
1686
1687 EVT IntVT = SignAsInt.IntValue.getValueType();
1688 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1689 SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
1690 SignMask);
1691
1692 // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X)
1693 EVT FloatVT = Mag.getValueType();
1694 if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
1695 TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
1696 SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
1697 SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
1698 SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
1699 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
1700 return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
1701 }
1702
1703 // Transform values to integer, copy the sign bit and transform back.
1704 FloatSignAsInt MagAsInt;
1705 getSignAsIntValue(MagAsInt, DL, Mag);
1706 assert(SignAsInt.SignMask == MagAsInt.SignMask);
1707 SDValue ClearSignMask = DAG.getConstant(~SignAsInt.SignMask, DL, IntVT);
1708 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, MagAsInt.IntValue,
1709 ClearSignMask);
1710 SDValue CopiedSign = DAG.getNode(ISD::OR, DL, IntVT, ClearedSign, SignBit);
1711
1712 return modifySignAsInt(MagAsInt, DL, CopiedSign);
1713}
1714
1715SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
1716 SDLoc DL(Node);
1717 SDValue Value = Node->getOperand(0);
1718
1719 // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
1720 EVT FloatVT = Value.getValueType();
1721 if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
1722 SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
1723 return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
1724 }
1725
1726 // Transform value to integer, clear the sign bit and transform back.
1727 FloatSignAsInt ValueAsInt;
1728 getSignAsIntValue(ValueAsInt, DL, Value);
1729 EVT IntVT = ValueAsInt.IntValue.getValueType();
1730 SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
1731 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
1732 ClearSignMask);
1733 return modifySignAsInt(ValueAsInt, DL, ClearedSign);
Eli Friedman2892d822009-05-27 12:20:41 +00001734}
1735
Eli Friedman2892d822009-05-27 12:20:41 +00001736void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1737 SmallVectorImpl<SDValue> &Results) {
1738 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1739 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1740 " not tell us which reg is the stack pointer!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00001741 SDLoc dl(Node);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001742 EVT VT = Node->getValueType(0);
Eli Friedman2892d822009-05-27 12:20:41 +00001743 SDValue Tmp1 = SDValue(Node, 0);
1744 SDValue Tmp2 = SDValue(Node, 1);
1745 SDValue Tmp3 = Node->getOperand(2);
1746 SDValue Chain = Tmp1.getOperand(0);
1747
1748 // Chain the dynamic stack allocation so that it doesn't modify the stack
1749 // pointer when other instructions are using the stack.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001750 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, dl, true), dl);
Eli Friedman2892d822009-05-27 12:20:41 +00001751
1752 SDValue Size = Tmp2.getOperand(1);
1753 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1754 Chain = SP.getValue(1);
1755 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Eric Christopherd9134482014-08-04 21:25:23 +00001756 unsigned StackAlign =
Eric Christopher85de8f92014-10-09 01:35:27 +00001757 DAG.getSubtarget().getFrameLowering()->getStackAlignment();
Eli Friedman2892d822009-05-27 12:20:41 +00001758 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Elena Demikhovsky82a46eb2013-10-14 07:26:51 +00001759 if (Align > StackAlign)
1760 Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001761 DAG.getConstant(-(uint64_t)Align, dl, VT));
Eli Friedman2892d822009-05-27 12:20:41 +00001762 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1763
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001764 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true),
1765 DAG.getIntPtrConstant(0, dl, true), SDValue(), dl);
Eli Friedman2892d822009-05-27 12:20:41 +00001766
1767 Results.push_back(Tmp1);
1768 Results.push_back(Tmp2);
1769}
1770
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00001771/// Legalize a SETCC with given LHS and RHS and condition code CC on the current
1772/// target.
Daniel Sandersedc071b2013-11-21 13:24:49 +00001773///
Tom Stellard08690a12013-09-28 02:50:32 +00001774/// If the SETCC has been legalized using AND / OR, then the legalized node
Daniel Sandersedc071b2013-11-21 13:24:49 +00001775/// will be stored in LHS. RHS and CC will be set to SDValue(). NeedInvert
1776/// will be set to false.
1777///
Tom Stellard08690a12013-09-28 02:50:32 +00001778/// If the SETCC has been legalized by using getSetCCSwappedOperands(),
Daniel Sandersedc071b2013-11-21 13:24:49 +00001779/// then the values of LHS and RHS will be swapped, CC will be set to the
1780/// new condition, and NeedInvert will be set to false.
1781///
1782/// If the SETCC has been legalized using the inverse condcode, then LHS and
1783/// RHS will be unchanged, CC will set to the inverted condcode, and NeedInvert
1784/// will be set to true. The caller must invert the result of the SETCC with
Pete Cooper7fd1d722014-05-12 23:26:58 +00001785/// SelectionDAG::getLogicalNOT() or take equivalent action to swap the effect
1786/// of a true/false result.
Daniel Sandersedc071b2013-11-21 13:24:49 +00001787///
Tom Stellard08690a12013-09-28 02:50:32 +00001788/// \returns true if the SetCC has been legalized, false if it hasn't.
1789bool SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001790 SDValue &LHS, SDValue &RHS,
Dale Johannesenad00f6e2009-02-02 20:41:04 +00001791 SDValue &CC,
Daniel Sandersedc071b2013-11-21 13:24:49 +00001792 bool &NeedInvert,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001793 SDLoc dl) {
Patrik Hagglunddeee9002012-12-19 10:09:26 +00001794 MVT OpVT = LHS.getSimpleValueType();
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001795 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
Daniel Sandersedc071b2013-11-21 13:24:49 +00001796 NeedInvert = false;
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001797 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Craig Topperee4dab52012-02-05 08:31:47 +00001798 default: llvm_unreachable("Unknown condition code action!");
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001799 case TargetLowering::Legal:
1800 // Nothing to do.
1801 break;
1802 case TargetLowering::Expand: {
Tom Stellardcd428182013-09-28 02:50:38 +00001803 ISD::CondCode InvCC = ISD::getSetCCSwappedOperands(CCCode);
1804 if (TLI.isCondCodeLegal(InvCC, OpVT)) {
1805 std::swap(LHS, RHS);
1806 CC = DAG.getCondCode(InvCC);
1807 return true;
1808 }
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001809 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1810 unsigned Opc = 0;
1811 switch (CCCode) {
Craig Topperee4dab52012-02-05 08:31:47 +00001812 default: llvm_unreachable("Don't know how to expand this condition!");
Stephen Lincfe7f352013-07-08 00:37:03 +00001813 case ISD::SETO:
Micah Villmow0242b9b2012-10-10 20:50:51 +00001814 assert(TLI.getCondCodeAction(ISD::SETOEQ, OpVT)
1815 == TargetLowering::Legal
1816 && "If SETO is expanded, SETOEQ must be legal!");
1817 CC1 = ISD::SETOEQ; CC2 = ISD::SETOEQ; Opc = ISD::AND; break;
Stephen Lincfe7f352013-07-08 00:37:03 +00001818 case ISD::SETUO:
Micah Villmow0242b9b2012-10-10 20:50:51 +00001819 assert(TLI.getCondCodeAction(ISD::SETUNE, OpVT)
1820 == TargetLowering::Legal
1821 && "If SETUO is expanded, SETUNE must be legal!");
1822 CC1 = ISD::SETUNE; CC2 = ISD::SETUNE; Opc = ISD::OR; break;
1823 case ISD::SETOEQ:
1824 case ISD::SETOGT:
1825 case ISD::SETOGE:
1826 case ISD::SETOLT:
1827 case ISD::SETOLE:
Stephen Lincfe7f352013-07-08 00:37:03 +00001828 case ISD::SETONE:
1829 case ISD::SETUEQ:
1830 case ISD::SETUNE:
1831 case ISD::SETUGT:
1832 case ISD::SETUGE:
1833 case ISD::SETULT:
Micah Villmow0242b9b2012-10-10 20:50:51 +00001834 case ISD::SETULE:
1835 // If we are floating point, assign and break, otherwise fall through.
1836 if (!OpVT.isInteger()) {
1837 // We can use the 4th bit to tell if we are the unordered
1838 // or ordered version of the opcode.
1839 CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO;
1840 Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND;
1841 CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10);
1842 break;
1843 }
1844 // Fallthrough if we are unsigned integer.
1845 case ISD::SETLE:
1846 case ISD::SETGT:
1847 case ISD::SETGE:
1848 case ISD::SETLT:
Tom Stellardcd428182013-09-28 02:50:38 +00001849 // We only support using the inverted operation, which is computed above
1850 // and not a different manner of supporting expanding these cases.
1851 llvm_unreachable("Don't know how to expand this condition!");
Daniel Sandersedc071b2013-11-21 13:24:49 +00001852 case ISD::SETNE:
1853 case ISD::SETEQ:
1854 // Try inverting the result of the inverse condition.
1855 InvCC = CCCode == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ;
1856 if (TLI.isCondCodeLegal(InvCC, OpVT)) {
1857 CC = DAG.getCondCode(InvCC);
1858 NeedInvert = true;
1859 return true;
1860 }
1861 // If inverting the condition didn't work then we have no means to expand
1862 // the condition.
1863 llvm_unreachable("Don't know how to expand this condition!");
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001864 }
Stephen Lincfe7f352013-07-08 00:37:03 +00001865
Micah Villmow0242b9b2012-10-10 20:50:51 +00001866 SDValue SetCC1, SetCC2;
1867 if (CCCode != ISD::SETO && CCCode != ISD::SETUO) {
1868 // If we aren't the ordered or unorder operation,
1869 // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS).
1870 SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1871 SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1872 } else {
1873 // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS)
1874 SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1);
1875 SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2);
1876 }
Dale Johannesenad00f6e2009-02-02 20:41:04 +00001877 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001878 RHS = SDValue();
1879 CC = SDValue();
Tom Stellard08690a12013-09-28 02:50:32 +00001880 return true;
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001881 }
1882 }
Tom Stellard08690a12013-09-28 02:50:32 +00001883 return false;
Evan Cheng3b0f5e42008-10-15 02:05:31 +00001884}
1885
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00001886/// Emit a store/load combination to the stack. This stores
Chris Lattner87bc3e72008-01-16 07:45:30 +00001887/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1888/// a load from the stack slot to DestVT, extending it if needed.
1889/// The resultant code need not be legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001890SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001891 EVT SlotVT,
1892 EVT DestVT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001893 SDLoc dl) {
Chris Lattner36e663d2005-12-23 00:16:34 +00001894 // Create the stack frame object.
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00001895 unsigned SrcAlign = DAG.getDataLayout().getPrefTypeAlignment(
1896 SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001897 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001898
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001899 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1900 int SPFI = StackPtrFI->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001901 MachinePointerInfo PtrInfo =
1902 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001903
Duncan Sands13237ac2008-06-06 12:08:01 +00001904 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1905 unsigned SlotSize = SlotVT.getSizeInBits();
1906 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattner229907c2011-07-18 04:54:35 +00001907 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00001908 unsigned DestAlign = DAG.getDataLayout().getPrefTypeAlignment(DestType);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001909
Chris Lattner87bc3e72008-01-16 07:45:30 +00001910 // Emit a store to the stack slot. Use a truncstore if the input value is
1911 // later than DestVT.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001912 SDValue Store;
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001913
Chris Lattner87bc3e72008-01-16 07:45:30 +00001914 if (SrcSize > SlotSize)
Dale Johannesena02e45c2009-02-02 22:12:50 +00001915 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001916 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00001917 else {
1918 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesena02e45c2009-02-02 22:12:50 +00001919 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattner6963c1f2010-09-21 17:42:31 +00001920 PtrInfo, false, false, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00001921 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001922
Chris Lattner36e663d2005-12-23 00:16:34 +00001923 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00001924 if (SlotSize == DestSize)
Chris Lattner6963c1f2010-09-21 17:42:31 +00001925 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Pete Cooper82cd9e82011-11-08 18:42:53 +00001926 false, false, false, DestAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001927
Chris Lattner87bc3e72008-01-16 07:45:30 +00001928 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastings81c43062011-02-16 16:23:55 +00001929 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001930 PtrInfo, SlotVT, false, false, false, DestAlign);
Chris Lattner36e663d2005-12-23 00:16:34 +00001931}
1932
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001933SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001934 SDLoc dl(Node);
Chris Lattner6be79822006-04-04 17:23:26 +00001935 // Create a vector sized/aligned stack slot, store the value to element #0,
1936 // then load the whole vector back out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001937 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman2d489b52008-02-06 22:27:42 +00001938
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00001939 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1940 int SPFI = StackPtrFI->getIndex();
1941
Alex Lorenze40c8a22015-08-11 23:09:45 +00001942 SDValue Ch = DAG.getTruncStore(
1943 DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
1944 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
1945 Node->getValueType(0).getVectorElementType(), false, false, 0);
1946 return DAG.getLoad(
1947 Node->getValueType(0), dl, Ch, StackPtr,
1948 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), false,
1949 false, false, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00001950}
1951
Hal Finkelb811b6d2014-03-31 19:42:55 +00001952static bool
1953ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG,
1954 const TargetLowering &TLI, SDValue &Res) {
1955 unsigned NumElems = Node->getNumOperands();
1956 SDLoc dl(Node);
1957 EVT VT = Node->getValueType(0);
1958
1959 // Try to group the scalars into pairs, shuffle the pairs together, then
1960 // shuffle the pairs of pairs together, etc. until the vector has
1961 // been built. This will work only if all of the necessary shuffle masks
1962 // are legal.
1963
1964 // We do this in two phases; first to check the legality of the shuffles,
1965 // and next, assuming that all shuffles are legal, to create the new nodes.
1966 for (int Phase = 0; Phase < 2; ++Phase) {
1967 SmallVector<std::pair<SDValue, SmallVector<int, 16> >, 16> IntermedVals,
1968 NewIntermedVals;
1969 for (unsigned i = 0; i < NumElems; ++i) {
1970 SDValue V = Node->getOperand(i);
1971 if (V.getOpcode() == ISD::UNDEF)
1972 continue;
1973
1974 SDValue Vec;
1975 if (Phase)
1976 Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
1977 IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
1978 }
1979
1980 while (IntermedVals.size() > 2) {
1981 NewIntermedVals.clear();
1982 for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
1983 // This vector and the next vector are shuffled together (simply to
1984 // append the one to the other).
1985 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1986
1987 SmallVector<int, 16> FinalIndices;
1988 FinalIndices.reserve(IntermedVals[i].second.size() +
1989 IntermedVals[i+1].second.size());
1990
1991 int k = 0;
1992 for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
1993 ++j, ++k) {
1994 ShuffleVec[k] = j;
1995 FinalIndices.push_back(IntermedVals[i].second[j]);
1996 }
1997 for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
1998 ++j, ++k) {
1999 ShuffleVec[k] = NumElems + j;
2000 FinalIndices.push_back(IntermedVals[i+1].second[j]);
2001 }
2002
2003 SDValue Shuffle;
2004 if (Phase)
2005 Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
2006 IntermedVals[i+1].first,
2007 ShuffleVec.data());
2008 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
2009 return false;
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00002010 NewIntermedVals.push_back(
2011 std::make_pair(Shuffle, std::move(FinalIndices)));
Hal Finkelb811b6d2014-03-31 19:42:55 +00002012 }
2013
2014 // If we had an odd number of defined values, then append the last
2015 // element to the array of new vectors.
2016 if ((IntermedVals.size() & 1) != 0)
2017 NewIntermedVals.push_back(IntermedVals.back());
2018
2019 IntermedVals.swap(NewIntermedVals);
2020 }
2021
2022 assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
2023 "Invalid number of intermediate vectors");
2024 SDValue Vec1 = IntermedVals[0].first;
2025 SDValue Vec2;
2026 if (IntermedVals.size() > 1)
2027 Vec2 = IntermedVals[1].first;
2028 else if (Phase)
2029 Vec2 = DAG.getUNDEF(VT);
2030
2031 SmallVector<int, 16> ShuffleVec(NumElems, -1);
2032 for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
2033 ShuffleVec[IntermedVals[0].second[i]] = i;
2034 for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
2035 ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
2036
2037 if (Phase)
2038 Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
2039 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
2040 return false;
2041 }
2042
2043 return true;
2044}
Chris Lattner6be79822006-04-04 17:23:26 +00002045
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002046/// Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00002047/// support the operation, but do support the resultant vector type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002048SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilsonf6c21952009-04-13 20:20:30 +00002049 unsigned NumElems = Node->getNumOperands();
Eli Friedman32345872009-06-07 06:52:44 +00002050 SDValue Value1, Value2;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002051 SDLoc dl(Node);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002052 EVT VT = Node->getValueType(0);
2053 EVT OpVT = Node->getOperand(0).getValueType();
2054 EVT EltVT = VT.getVectorElementType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002055
2056 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00002057 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002058 bool isOnlyLowElement = true;
Eli Friedman32345872009-06-07 06:52:44 +00002059 bool MoreThanTwoValues = false;
Chris Lattner77e271c2006-03-24 07:29:17 +00002060 bool isConstant = true;
Eli Friedman32345872009-06-07 06:52:44 +00002061 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002062 SDValue V = Node->getOperand(i);
Eli Friedman32345872009-06-07 06:52:44 +00002063 if (V.getOpcode() == ISD::UNDEF)
2064 continue;
2065 if (i > 0)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002066 isOnlyLowElement = false;
Eli Friedman32345872009-06-07 06:52:44 +00002067 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner77e271c2006-03-24 07:29:17 +00002068 isConstant = false;
Eli Friedman32345872009-06-07 06:52:44 +00002069
2070 if (!Value1.getNode()) {
2071 Value1 = V;
2072 } else if (!Value2.getNode()) {
2073 if (V != Value1)
2074 Value2 = V;
2075 } else if (V != Value1 && V != Value2) {
2076 MoreThanTwoValues = true;
2077 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002078 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002079
Eli Friedman32345872009-06-07 06:52:44 +00002080 if (!Value1.getNode())
2081 return DAG.getUNDEF(VT);
2082
2083 if (isOnlyLowElement)
Bob Wilsonf6c21952009-04-13 20:20:30 +00002084 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002085
Chris Lattner77e271c2006-03-24 07:29:17 +00002086 // If all elements are constants, create a load from the constant pool.
2087 if (isConstant) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002088 SmallVector<Constant*, 16> CV;
Chris Lattner77e271c2006-03-24 07:29:17 +00002089 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00002090 if (ConstantFPSDNode *V =
Chris Lattner77e271c2006-03-24 07:29:17 +00002091 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00002092 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002093 } else if (ConstantSDNode *V =
Bob Wilsonf074ca72009-04-10 18:48:47 +00002094 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen6f7d5b22009-11-10 23:16:41 +00002095 if (OpVT==EltVT)
2096 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
2097 else {
2098 // If OpVT and EltVT don't match, EltVT is not legal and the
2099 // element values have been promoted/truncated earlier. Undo this;
2100 // we don't want a v16i8 to become a v16i32 for example.
2101 const ConstantInt *CI = V->getConstantIntValue();
2102 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
2103 CI->getZExtValue()));
2104 }
Chris Lattner77e271c2006-03-24 07:29:17 +00002105 } else {
2106 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner229907c2011-07-18 04:54:35 +00002107 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Andersonb292b8c2009-07-30 23:03:37 +00002108 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner77e271c2006-03-24 07:29:17 +00002109 }
2110 }
Owen Anderson4aa32952009-07-28 21:19:26 +00002111 Constant *CP = ConstantVector::get(CV);
Mehdi Amini44ede332015-07-09 02:09:04 +00002112 SDValue CPIdx =
2113 DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002114 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Alex Lorenze40c8a22015-08-11 23:09:45 +00002115 return DAG.getLoad(
2116 VT, dl, DAG.getEntryNode(), CPIdx,
2117 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2118 false, false, Alignment);
Chris Lattner77e271c2006-03-24 07:29:17 +00002119 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002120
Hal Finkel19775142014-03-31 17:48:10 +00002121 SmallSet<SDValue, 16> DefinedValues;
2122 for (unsigned i = 0; i < NumElems; ++i) {
2123 if (Node->getOperand(i).getOpcode() == ISD::UNDEF)
2124 continue;
2125 DefinedValues.insert(Node->getOperand(i));
2126 }
2127
Hal Finkelb811b6d2014-03-31 19:42:55 +00002128 if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
2129 if (!MoreThanTwoValues) {
2130 SmallVector<int, 8> ShuffleVec(NumElems, -1);
2131 for (unsigned i = 0; i < NumElems; ++i) {
2132 SDValue V = Node->getOperand(i);
2133 if (V.getOpcode() == ISD::UNDEF)
2134 continue;
2135 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2136 }
2137 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
2138 // Get the splatted value into the low element of a vector register.
2139 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2140 SDValue Vec2;
2141 if (Value2.getNode())
2142 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2143 else
2144 Vec2 = DAG.getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002145
Hal Finkelb811b6d2014-03-31 19:42:55 +00002146 // Return shuffle(LowValVec, undef, <0,0,0,0>)
2147 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
2148 }
2149 } else {
2150 SDValue Res;
2151 if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
2152 return Res;
Evan Cheng1d2e9952006-03-24 01:17:21 +00002153 }
2154 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002155
Eli Friedmanaee3f622009-06-06 07:04:42 +00002156 // Otherwise, we can't handle this case efficiently.
2157 return ExpandVectorBuildThroughStack(Node);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002158}
2159
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002160// Expand a node into a call to a libcall. If the result value
Chris Lattneraac464e2005-01-21 06:05:23 +00002161// does not fit into a register, return the lo part and set the hi part to the
2162// by-reg argument. If it does fit into a single register, return the result
2163// and leave the Hi part unset.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002164SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedmanb3554152009-05-27 02:21:29 +00002165 bool isSigned) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002166 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00002167 TargetLowering::ArgListEntry Entry;
Pete Cooper8fc121d2015-06-26 19:08:33 +00002168 for (const SDValue &Op : Node->op_values()) {
2169 EVT ArgVT = Op.getValueType();
Chris Lattner229907c2011-07-18 04:54:35 +00002170 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Pete Cooper8fc121d2015-06-26 19:08:33 +00002171 Entry.Node = Op;
2172 Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002173 Entry.isSExt = isSigned;
Duncan Sands4c95dbd2008-02-14 17:28:50 +00002174 Entry.isZExt = !isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00002175 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00002176 }
Bill Wendling24c79f22008-09-16 21:48:12 +00002177 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00002178 TLI.getPointerTy(DAG.getDataLayout()));
Misha Brukman835702a2005-04-21 22:36:52 +00002179
Chris Lattner229907c2011-07-18 04:54:35 +00002180 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Chengd4b08732010-11-30 23:55:39 +00002181
Evan Chengf8bad082012-04-10 01:51:00 +00002182 // By default, the input chain to this libcall is the entry node of the
2183 // function. If the libcall is going to be emitted as a tail call then
2184 // TLI.isUsedByReturnOnly will change it to the right chain if the return
2185 // node which is being folded has a non-entry input chain.
2186 SDValue InChain = DAG.getEntryNode();
2187
Evan Chengd4b08732010-11-30 23:55:39 +00002188 // isTailCall may be true since the callee does not reference caller stack
2189 // frame. Check if it's in the right position.
Evan Cheng136861d2012-04-10 03:15:18 +00002190 SDValue TCChain = InChain;
Tim Northoverf1450d82013-01-09 13:18:15 +00002191 bool isTailCall = TLI.isInTailCallPosition(DAG, Node, TCChain);
Evan Cheng136861d2012-04-10 03:15:18 +00002192 if (isTailCall)
2193 InChain = TCChain;
2194
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002195 TargetLowering::CallLoweringInfo CLI(DAG);
2196 CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002197 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002198 .setTailCall(isTailCall).setSExtResult(isSigned).setZExtResult(!isSigned);
Justin Holewinskiaa583972012-05-25 16:35:28 +00002199
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002200 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002201
Evan Chengd4b08732010-11-30 23:55:39 +00002202 if (!CallInfo.second.getNode())
2203 // It's a tailcall, return the chain (which is the DAG root).
2204 return DAG.getRoot();
2205
Eli Friedman4a951bf2009-05-26 08:55:52 +00002206 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002207}
2208
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002209/// Generate a libcall taking the given operands as arguments
Eric Christopherbcaedb52011-04-20 01:19:45 +00002210/// and returning a result of type RetVT.
2211SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
2212 const SDValue *Ops, unsigned NumOps,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002213 bool isSigned, SDLoc dl) {
Eric Christopherbcaedb52011-04-20 01:19:45 +00002214 TargetLowering::ArgListTy Args;
2215 Args.reserve(NumOps);
Dan Gohmanae9b1682011-05-16 22:09:53 +00002216
Eric Christopherbcaedb52011-04-20 01:19:45 +00002217 TargetLowering::ArgListEntry Entry;
2218 for (unsigned i = 0; i != NumOps; ++i) {
2219 Entry.Node = Ops[i];
2220 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
2221 Entry.isSExt = isSigned;
2222 Entry.isZExt = !isSigned;
2223 Args.push_back(Entry);
2224 }
2225 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00002226 TLI.getPointerTy(DAG.getDataLayout()));
Dan Gohmanae9b1682011-05-16 22:09:53 +00002227
Chris Lattner229907c2011-07-18 04:54:35 +00002228 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002229
2230 TargetLowering::CallLoweringInfo CLI(DAG);
2231 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002232 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002233 .setSExtResult(isSigned).setZExtResult(!isSigned);
2234
Justin Holewinskiaa583972012-05-25 16:35:28 +00002235 std::pair<SDValue,SDValue> CallInfo = TLI.LowerCallTo(CLI);
Dan Gohmanae9b1682011-05-16 22:09:53 +00002236
Eric Christopherbcaedb52011-04-20 01:19:45 +00002237 return CallInfo.first;
2238}
2239
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002240// Expand a node into a call to a libcall. Similar to
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002241// ExpandLibCall except that the first operand is the in-chain.
2242std::pair<SDValue, SDValue>
2243SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2244 SDNode *Node,
2245 bool isSigned) {
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002246 SDValue InChain = Node->getOperand(0);
2247
2248 TargetLowering::ArgListTy Args;
2249 TargetLowering::ArgListEntry Entry;
2250 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2251 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattner229907c2011-07-18 04:54:35 +00002252 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002253 Entry.Node = Node->getOperand(i);
2254 Entry.Ty = ArgTy;
2255 Entry.isSExt = isSigned;
2256 Entry.isZExt = !isSigned;
2257 Args.push_back(Entry);
2258 }
2259 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00002260 TLI.getPointerTy(DAG.getDataLayout()));
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002261
Chris Lattner229907c2011-07-18 04:54:35 +00002262 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002263
2264 TargetLowering::CallLoweringInfo CLI(DAG);
2265 CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002266 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002267 .setSExtResult(isSigned).setZExtResult(!isSigned);
2268
Justin Holewinskiaa583972012-05-25 16:35:28 +00002269 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002270
Jim Grosbachd64dfc12010-06-18 21:43:38 +00002271 return CallInfo;
2272}
2273
Eli Friedmand6f28342009-05-27 03:33:44 +00002274SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2275 RTLIB::Libcall Call_F32,
2276 RTLIB::Libcall Call_F64,
2277 RTLIB::Libcall Call_F80,
Tim Northover4bf47bc2013-01-08 17:09:59 +00002278 RTLIB::Libcall Call_F128,
Eli Friedmand6f28342009-05-27 03:33:44 +00002279 RTLIB::Libcall Call_PPCF128) {
2280 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002281 switch (Node->getSimpleValueType(0).SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002282 default: llvm_unreachable("Unexpected request for libcall!");
Owen Anderson9f944592009-08-11 20:47:22 +00002283 case MVT::f32: LC = Call_F32; break;
2284 case MVT::f64: LC = Call_F64; break;
2285 case MVT::f80: LC = Call_F80; break;
Tim Northover4bf47bc2013-01-08 17:09:59 +00002286 case MVT::f128: LC = Call_F128; break;
Owen Anderson9f944592009-08-11 20:47:22 +00002287 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmand6f28342009-05-27 03:33:44 +00002288 }
2289 return ExpandLibCall(LC, Node, false);
2290}
2291
2292SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikovf93bb392009-11-07 17:14:39 +00002293 RTLIB::Libcall Call_I8,
Eli Friedmand6f28342009-05-27 03:33:44 +00002294 RTLIB::Libcall Call_I16,
2295 RTLIB::Libcall Call_I32,
2296 RTLIB::Libcall Call_I64,
2297 RTLIB::Libcall Call_I128) {
2298 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002299 switch (Node->getSimpleValueType(0).SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002300 default: llvm_unreachable("Unexpected request for libcall!");
Anton Korobeynikovf93bb392009-11-07 17:14:39 +00002301 case MVT::i8: LC = Call_I8; break;
2302 case MVT::i16: LC = Call_I16; break;
2303 case MVT::i32: LC = Call_I32; break;
2304 case MVT::i64: LC = Call_I64; break;
Owen Anderson9f944592009-08-11 20:47:22 +00002305 case MVT::i128: LC = Call_I128; break;
Eli Friedmand6f28342009-05-27 03:33:44 +00002306 }
2307 return ExpandLibCall(LC, Node, isSigned);
2308}
2309
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002310/// Issue libcalls to __{u}divmod to compute div / rem pairs.
Evan Chengb14ce092011-04-16 03:08:26 +00002311void
2312SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2313 SmallVectorImpl<SDValue> &Results) {
2314 unsigned Opcode = Node->getOpcode();
2315 bool isSigned = Opcode == ISD::SDIVREM;
2316
2317 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002318 switch (Node->getSimpleValueType(0).SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002319 default: llvm_unreachable("Unexpected request for libcall!");
Evan Chengb14ce092011-04-16 03:08:26 +00002320 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2321 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2322 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2323 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2324 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Chengbd766792011-04-01 00:42:02 +00002325 }
2326
2327 // The input chain to this libcall is the entry node of the function.
2328 // Legalizing the call will automatically add the previous call to the
2329 // dependence.
2330 SDValue InChain = DAG.getEntryNode();
2331
2332 EVT RetVT = Node->getValueType(0);
Chris Lattner229907c2011-07-18 04:54:35 +00002333 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Chengbd766792011-04-01 00:42:02 +00002334
2335 TargetLowering::ArgListTy Args;
2336 TargetLowering::ArgListEntry Entry;
Pete Cooper8fc121d2015-06-26 19:08:33 +00002337 for (const SDValue &Op : Node->op_values()) {
2338 EVT ArgVT = Op.getValueType();
Chris Lattner229907c2011-07-18 04:54:35 +00002339 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Pete Cooper8fc121d2015-06-26 19:08:33 +00002340 Entry.Node = Op;
2341 Entry.Ty = ArgTy;
Evan Chengbd766792011-04-01 00:42:02 +00002342 Entry.isSExt = isSigned;
2343 Entry.isZExt = !isSigned;
2344 Args.push_back(Entry);
2345 }
2346
2347 // Also pass the return address of the remainder.
2348 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2349 Entry.Node = FIPtr;
Micah Villmow51e72462012-10-24 17:25:11 +00002350 Entry.Ty = RetTy->getPointerTo();
Evan Chengbd766792011-04-01 00:42:02 +00002351 Entry.isSExt = isSigned;
2352 Entry.isZExt = !isSigned;
2353 Args.push_back(Entry);
2354
2355 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00002356 TLI.getPointerTy(DAG.getDataLayout()));
Evan Chengbd766792011-04-01 00:42:02 +00002357
Andrew Trickef9de2a2013-05-25 02:42:55 +00002358 SDLoc dl(Node);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002359 TargetLowering::CallLoweringInfo CLI(DAG);
2360 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002361 .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002362 .setSExtResult(isSigned).setZExtResult(!isSigned);
2363
Justin Holewinskiaa583972012-05-25 16:35:28 +00002364 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Evan Chengbd766792011-04-01 00:42:02 +00002365
Evan Chengbd766792011-04-01 00:42:02 +00002366 // Remainder is loaded back from the stack frame.
Dan Gohman198b7ff2011-11-03 21:49:52 +00002367 SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002368 MachinePointerInfo(), false, false, false, 0);
Evan Chengb14ce092011-04-16 03:08:26 +00002369 Results.push_back(CallInfo.first);
2370 Results.push_back(Rem);
Evan Chengbd766792011-04-01 00:42:02 +00002371}
2372
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002373/// Return true if sincos libcall is available.
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002374static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
2375 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002376 switch (Node->getSimpleValueType(0).SimpleTy) {
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002377 default: llvm_unreachable("Unexpected request for libcall!");
2378 case MVT::f32: LC = RTLIB::SINCOS_F32; break;
2379 case MVT::f64: LC = RTLIB::SINCOS_F64; break;
2380 case MVT::f80: LC = RTLIB::SINCOS_F80; break;
2381 case MVT::f128: LC = RTLIB::SINCOS_F128; break;
2382 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2383 }
Craig Topperc0196b12014-04-14 00:51:57 +00002384 return TLI.getLibcallName(LC) != nullptr;
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002385}
2386
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002387/// Return true if sincos libcall is available and can be used to combine sin
2388/// and cos.
Paul Redmondf29ddfe2013-02-15 18:45:18 +00002389static bool canCombineSinCosLibcall(SDNode *Node, const TargetLowering &TLI,
2390 const TargetMachine &TM) {
2391 if (!isSinCosLibcallAvailable(Node, TLI))
2392 return false;
2393 // GNU sin/cos functions set errno while sincos does not. Therefore
2394 // combining sin and cos is only safe if unsafe-fpmath is enabled.
2395 bool isGNU = Triple(TM.getTargetTriple()).getEnvironment() == Triple::GNU;
2396 if (isGNU && !TM.Options.UnsafeFPMath)
2397 return false;
2398 return true;
2399}
2400
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002401/// Only issue sincos libcall if both sin and cos are needed.
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002402static bool useSinCos(SDNode *Node) {
2403 unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
2404 ? ISD::FCOS : ISD::FSIN;
Stephen Lincfe7f352013-07-08 00:37:03 +00002405
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002406 SDValue Op0 = Node->getOperand(0);
2407 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2408 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2409 SDNode *User = *UI;
2410 if (User == Node)
2411 continue;
2412 // The other user might have been turned into sincos already.
2413 if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
2414 return true;
2415 }
2416 return false;
2417}
2418
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002419/// Issue libcalls to sincos to compute sin / cos pairs.
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002420void
2421SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
2422 SmallVectorImpl<SDValue> &Results) {
2423 RTLIB::Libcall LC;
Craig Topperd9c27832013-08-15 02:44:19 +00002424 switch (Node->getSimpleValueType(0).SimpleTy) {
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002425 default: llvm_unreachable("Unexpected request for libcall!");
2426 case MVT::f32: LC = RTLIB::SINCOS_F32; break;
2427 case MVT::f64: LC = RTLIB::SINCOS_F64; break;
2428 case MVT::f80: LC = RTLIB::SINCOS_F80; break;
2429 case MVT::f128: LC = RTLIB::SINCOS_F128; break;
2430 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2431 }
Stephen Lincfe7f352013-07-08 00:37:03 +00002432
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002433 // The input chain to this libcall is the entry node of the function.
2434 // Legalizing the call will automatically add the previous call to the
2435 // dependence.
2436 SDValue InChain = DAG.getEntryNode();
Stephen Lincfe7f352013-07-08 00:37:03 +00002437
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002438 EVT RetVT = Node->getValueType(0);
2439 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Stephen Lincfe7f352013-07-08 00:37:03 +00002440
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002441 TargetLowering::ArgListTy Args;
2442 TargetLowering::ArgListEntry Entry;
Stephen Lincfe7f352013-07-08 00:37:03 +00002443
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002444 // Pass the argument.
2445 Entry.Node = Node->getOperand(0);
2446 Entry.Ty = RetTy;
2447 Entry.isSExt = false;
2448 Entry.isZExt = false;
2449 Args.push_back(Entry);
Stephen Lincfe7f352013-07-08 00:37:03 +00002450
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002451 // Pass the return address of sin.
2452 SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
2453 Entry.Node = SinPtr;
2454 Entry.Ty = RetTy->getPointerTo();
2455 Entry.isSExt = false;
2456 Entry.isZExt = false;
2457 Args.push_back(Entry);
Stephen Lincfe7f352013-07-08 00:37:03 +00002458
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002459 // Also pass the return address of the cos.
2460 SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
2461 Entry.Node = CosPtr;
2462 Entry.Ty = RetTy->getPointerTo();
2463 Entry.isSExt = false;
2464 Entry.isZExt = false;
2465 Args.push_back(Entry);
Stephen Lincfe7f352013-07-08 00:37:03 +00002466
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002467 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +00002468 TLI.getPointerTy(DAG.getDataLayout()));
Stephen Lincfe7f352013-07-08 00:37:03 +00002469
Andrew Trickef9de2a2013-05-25 02:42:55 +00002470 SDLoc dl(Node);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002471 TargetLowering::CallLoweringInfo CLI(DAG);
2472 CLI.setDebugLoc(dl).setChain(InChain)
2473 .setCallee(TLI.getLibcallCallingConv(LC),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002474 Type::getVoidTy(*DAG.getContext()), Callee, std::move(Args), 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002475
Evan Cheng0e88c7d2013-01-29 02:32:37 +00002476 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2477
2478 Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr,
2479 MachinePointerInfo(), false, false, false, 0));
2480 Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr,
2481 MachinePointerInfo(), false, false, false, 0));
2482}
2483
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002484/// This function is responsible for legalizing a
Chris Lattner689bdcc2006-01-28 08:25:58 +00002485/// INT_TO_FP operation of the specified operand when the target requests that
2486/// we expand it. At this point, we know that the result and operand types are
2487/// legal for the target.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002488SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2489 SDValue Op0,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002490 EVT DestVT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002491 SDLoc dl) {
Sanjay Patela2607012015-09-16 16:31:21 +00002492 // TODO: Should any fast-math-flags be set for the created nodes?
2493
Akira Hatanakaadb14f52012-08-28 02:12:42 +00002494 if (Op0.getValueType() == MVT::i32 && TLI.isTypeLegal(MVT::f64)) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002495 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelcf0da6c2009-02-17 22:15:04 +00002496
Chris Lattnera2c7ff32008-01-16 07:03:22 +00002497 // Get the stack frame index of a 8 byte buffer.
Owen Anderson9f944592009-08-11 20:47:22 +00002498 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002499
Chris Lattner689bdcc2006-01-28 08:25:58 +00002500 // word offset constant for Hi/Lo address computation
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002501 SDValue WordOff = DAG.getConstant(sizeof(int), dl,
2502 StackSlot.getValueType());
Chris Lattner689bdcc2006-01-28 08:25:58 +00002503 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002504 SDValue Hi = StackSlot;
Tom Stellard838e2342013-08-26 15:06:10 +00002505 SDValue Lo = DAG.getNode(ISD::ADD, dl, StackSlot.getValueType(),
2506 StackSlot, WordOff);
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00002507 if (DAG.getDataLayout().isLittleEndian())
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00002508 std::swap(Hi, Lo);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002509
Chris Lattner689bdcc2006-01-28 08:25:58 +00002510 // if signed map to unsigned space
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002511 SDValue Op0Mapped;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002512 if (isSigned) {
2513 // constant used to invert sign bit (signed to unsigned mapping)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002514 SDValue SignBit = DAG.getConstant(0x80000000u, dl, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00002515 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002516 } else {
2517 Op0Mapped = Op0;
2518 }
2519 // store the lo of the constructed double - based on integer input
Dale Johannesen8525d832009-02-02 19:03:57 +00002520 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner676c61d2010-09-21 18:41:36 +00002521 Op0Mapped, Lo, MachinePointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00002522 false, false, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002523 // initial hi portion of constructed double
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002524 SDValue InitialHi = DAG.getConstant(0x43300000u, dl, MVT::i32);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002525 // store the hi of the constructed double - biased exponent
Chris Lattner676c61d2010-09-21 18:41:36 +00002526 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2527 MachinePointerInfo(),
2528 false, false, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002529 // load the constructed double
Chris Lattner1ffcf522010-09-21 16:36:31 +00002530 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002531 MachinePointerInfo(), false, false, false, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002532 // FP constant to bias correct the final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002533 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonf074ca72009-04-10 18:48:47 +00002534 BitsToDouble(0x4330000080000000ULL) :
2535 BitsToDouble(0x4330000000000000ULL),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002536 dl, MVT::f64);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002537 // subtract the bias
Owen Anderson9f944592009-08-11 20:47:22 +00002538 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002539 // final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002540 SDValue Result;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002541 // handle final rounding
Owen Anderson9f944592009-08-11 20:47:22 +00002542 if (DestVT == MVT::f64) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002543 // do nothing
2544 Result = Sub;
Owen Anderson9f944592009-08-11 20:47:22 +00002545 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesen8525d832009-02-02 19:03:57 +00002546 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002547 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00002548 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen8525d832009-02-02 19:03:57 +00002549 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002550 }
2551 return Result;
2552 }
2553 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002554 // Code below here assumes !isSigned without checking again.
Dan Gohman14e450f2010-03-06 00:00:55 +00002555
2556 // Implementation of unsigned i64 to f64 following the algorithm in
2557 // __floatundidf in compiler_rt. This implementation has the advantage
2558 // of performing rounding correctly, both in the default rounding mode
2559 // and in all alternate rounding modes.
2560 // TODO: Generalize this for use with other types.
2561 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2562 SDValue TwoP52 =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002563 DAG.getConstant(UINT64_C(0x4330000000000000), dl, MVT::i64);
Dan Gohman14e450f2010-03-06 00:00:55 +00002564 SDValue TwoP84PlusTwoP52 =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002565 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), dl,
2566 MVT::f64);
Dan Gohman14e450f2010-03-06 00:00:55 +00002567 SDValue TwoP84 =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002568 DAG.getConstant(UINT64_C(0x4530000000000000), dl, MVT::i64);
Dan Gohman14e450f2010-03-06 00:00:55 +00002569
2570 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2571 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002572 DAG.getConstant(32, dl, MVT::i64));
Dan Gohman14e450f2010-03-06 00:00:55 +00002573 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2574 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peck527da1b2010-11-23 03:31:01 +00002575 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2576 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach9b7755f2010-07-02 17:41:59 +00002577 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2578 TwoP84PlusTwoP52);
Dan Gohman14e450f2010-03-06 00:00:55 +00002579 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2580 }
2581
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002582 // Implementation of unsigned i64 to f32.
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002583 // TODO: Generalize this for use with other types.
2584 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002585 // For unsigned conversions, convert them to signed conversions using the
2586 // algorithm from the x86_64 __floatundidf in compiler_rt.
2587 if (!isSigned) {
2588 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peck527da1b2010-11-23 03:31:01 +00002589
Mehdi Amini9639d652015-07-09 02:09:20 +00002590 SDValue ShiftConst = DAG.getConstant(
2591 1, dl, TLI.getShiftAmountTy(Op0.getValueType(), DAG.getDataLayout()));
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002592 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002593 SDValue AndConst = DAG.getConstant(1, dl, MVT::i64);
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002594 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2595 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peck527da1b2010-11-23 03:31:01 +00002596
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002597 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2598 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peck527da1b2010-11-23 03:31:01 +00002599
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002600 // TODO: This really should be implemented using a branch rather than a
Wesley Peck527da1b2010-11-23 03:31:01 +00002601 // select. We happen to get lucky and machinesink does the right
2602 // thing most of the time. This would be a good candidate for a
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002603 //pseudo-op, or, even better, for whole-function isel.
Matt Arsenault758659232013-05-18 00:21:46 +00002604 SDValue SignBitTest = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002605 Op0, DAG.getConstant(0, dl, MVT::i64), ISD::SETLT);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002606 return DAG.getSelect(dl, MVT::f32, SignBitTest, Slow, Fast);
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002607 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002608
Owen Andersond8d1dcc2010-10-05 17:24:05 +00002609 // Otherwise, implement the fully general conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002610
Jim Grosbach9b7755f2010-07-02 17:41:59 +00002611 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002612 DAG.getConstant(UINT64_C(0xfffffffffffff800), dl, MVT::i64));
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002613 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002614 DAG.getConstant(UINT64_C(0x800), dl, MVT::i64));
Jim Grosbach9b7755f2010-07-02 17:41:59 +00002615 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002616 DAG.getConstant(UINT64_C(0x7ff), dl, MVT::i64));
2617 SDValue Ne = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), And2,
2618 DAG.getConstant(UINT64_C(0), dl, MVT::i64),
2619 ISD::SETNE);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002620 SDValue Sel = DAG.getSelect(dl, MVT::i64, Ne, Or, Op0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002621 SDValue Ge = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), Op0,
2622 DAG.getConstant(UINT64_C(0x0020000000000000), dl,
2623 MVT::i64),
2624 ISD::SETUGE);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002625 SDValue Sel2 = DAG.getSelect(dl, MVT::i64, Ge, Sel, Op0);
Mehdi Amini9639d652015-07-09 02:09:20 +00002626 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType(), DAG.getDataLayout());
Wesley Peck527da1b2010-11-23 03:31:01 +00002627
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002628 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002629 DAG.getConstant(32, dl, SHVT));
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002630 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2631 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2632 SDValue TwoP32 =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002633 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), dl,
2634 MVT::f64);
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002635 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2636 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2637 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2638 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2639 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002640 DAG.getIntPtrConstant(0, dl));
Dale Johannesen1ae94b92010-05-13 23:50:42 +00002641 }
2642
Dan Gohman998c7c22010-03-05 02:40:23 +00002643 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002644
Matt Arsenault758659232013-05-18 00:21:46 +00002645 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(Op0.getValueType()),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002646 Op0,
2647 DAG.getConstant(0, dl, Op0.getValueType()),
Dan Gohman998c7c22010-03-05 02:40:23 +00002648 ISD::SETLT);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002649 SDValue Zero = DAG.getIntPtrConstant(0, dl),
2650 Four = DAG.getIntPtrConstant(4, dl);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00002651 SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
Dan Gohman998c7c22010-03-05 02:40:23 +00002652 SignSet, Four, Zero);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002653
Dan Gohman998c7c22010-03-05 02:40:23 +00002654 // If the sign bit of the integer is set, the large number will be treated
2655 // as a negative number. To counteract this, the dynamic code adds an
2656 // offset depending on the data type.
2657 uint64_t FF;
Craig Topperd9c27832013-08-15 02:44:19 +00002658 switch (Op0.getSimpleValueType().SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002659 default: llvm_unreachable("Unsupported integer type!");
Dan Gohman998c7c22010-03-05 02:40:23 +00002660 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2661 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2662 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2663 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2664 }
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00002665 if (DAG.getDataLayout().isLittleEndian())
2666 FF <<= 32;
Dan Gohman998c7c22010-03-05 02:40:23 +00002667 Constant *FudgeFactor = ConstantInt::get(
2668 Type::getInt64Ty(*DAG.getContext()), FF);
2669
Mehdi Amini44ede332015-07-09 02:09:04 +00002670 SDValue CPIdx =
2671 DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
Dan Gohman998c7c22010-03-05 02:40:23 +00002672 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Tom Stellard838e2342013-08-26 15:06:10 +00002673 CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
Dan Gohman998c7c22010-03-05 02:40:23 +00002674 Alignment = std::min(Alignment, 4u);
2675 SDValue FudgeInReg;
2676 if (DestVT == MVT::f32)
Alex Lorenze40c8a22015-08-11 23:09:45 +00002677 FudgeInReg = DAG.getLoad(
2678 MVT::f32, dl, DAG.getEntryNode(), CPIdx,
2679 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2680 false, false, Alignment);
Dan Gohman998c7c22010-03-05 02:40:23 +00002681 else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00002682 SDValue Load = DAG.getExtLoad(
2683 ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
2684 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
2685 false, false, false, Alignment);
Dan Gohman198b7ff2011-11-03 21:49:52 +00002686 HandleSDNode Handle(Load);
2687 LegalizeOp(Load.getNode());
2688 FudgeInReg = Handle.getValue();
Dan Gohman998c7c22010-03-05 02:40:23 +00002689 }
2690
2691 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002692}
2693
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002694/// This function is responsible for legalizing a
Chris Lattner689bdcc2006-01-28 08:25:58 +00002695/// *INT_TO_FP operation of the specified operand when the target requests that
2696/// we promote it. At this point, we know that the result and operand types are
2697/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2698/// operation that takes a larger input.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002699SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002700 EVT DestVT,
Dale Johannesen8525d832009-02-02 19:03:57 +00002701 bool isSigned,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002702 SDLoc dl) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002703 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002704 EVT NewInTy = LegalOp.getValueType();
Chris Lattner689bdcc2006-01-28 08:25:58 +00002705
2706 unsigned OpToUse = 0;
2707
2708 // Scan for the appropriate larger type to use.
2709 while (1) {
Owen Anderson9f944592009-08-11 20:47:22 +00002710 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00002711 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00002712
2713 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedmane1bc3792009-05-28 03:06:16 +00002714 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2715 OpToUse = ISD::SINT_TO_FP;
2716 break;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002717 }
Chris Lattner689bdcc2006-01-28 08:25:58 +00002718 if (isSigned) continue;
2719
2720 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedmane1bc3792009-05-28 03:06:16 +00002721 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2722 OpToUse = ISD::UINT_TO_FP;
2723 break;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002724 }
Chris Lattner689bdcc2006-01-28 08:25:58 +00002725
2726 // Otherwise, try a larger type.
2727 }
2728
2729 // Okay, we found the operation and type to use. Zero extend our input to the
2730 // desired type then run the operation on it.
Dale Johannesen8525d832009-02-02 19:03:57 +00002731 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00002732 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesen8525d832009-02-02 19:03:57 +00002733 dl, NewInTy, LegalOp));
Chris Lattner689bdcc2006-01-28 08:25:58 +00002734}
2735
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002736/// This function is responsible for legalizing a
Chris Lattner689bdcc2006-01-28 08:25:58 +00002737/// FP_TO_*INT operation of the specified operand when the target requests that
2738/// we promote it. At this point, we know that the result and operand types are
2739/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2740/// operation that returns a larger result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002741SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002742 EVT DestVT,
Dale Johannesen8525d832009-02-02 19:03:57 +00002743 bool isSigned,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002744 SDLoc dl) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002745 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002746 EVT NewOutTy = DestVT;
Chris Lattner689bdcc2006-01-28 08:25:58 +00002747
2748 unsigned OpToUse = 0;
2749
2750 // Scan for the appropriate larger type to use.
2751 while (1) {
Owen Anderson9f944592009-08-11 20:47:22 +00002752 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00002753 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00002754
Tim Northover65277a22014-06-15 09:27:20 +00002755 // A larger signed type can hold all unsigned values of the requested type,
2756 // so using FP_TO_SINT is valid
Eli Friedmane1bc3792009-05-28 03:06:16 +00002757 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002758 OpToUse = ISD::FP_TO_SINT;
2759 break;
2760 }
Chris Lattner689bdcc2006-01-28 08:25:58 +00002761
Tim Northover65277a22014-06-15 09:27:20 +00002762 // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2763 if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002764 OpToUse = ISD::FP_TO_UINT;
2765 break;
2766 }
Chris Lattner689bdcc2006-01-28 08:25:58 +00002767
2768 // Otherwise, try a larger type.
2769 }
2770
Scott Michelcf0da6c2009-02-17 22:15:04 +00002771
Chris Lattnerf81d5882007-11-24 07:07:01 +00002772 // Okay, we found the operation and type to use.
Dale Johannesen8525d832009-02-02 19:03:57 +00002773 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands93e180342008-07-04 11:47:58 +00002774
Chris Lattnerf81d5882007-11-24 07:07:01 +00002775 // Truncate the result of the extended FP_TO_*INT operation to the desired
2776 // size.
Dale Johannesen8525d832009-02-02 19:03:57 +00002777 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002778}
2779
James Molloy90111f72015-11-12 12:29:09 +00002780/// Open code the operations for BITREVERSE.
2781SDValue SelectionDAGLegalize::ExpandBITREVERSE(SDValue Op, SDLoc dl) {
2782 EVT VT = Op.getValueType();
2783 EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
James Molloybb1dbf52015-11-13 10:02:36 +00002784 unsigned Sz = VT.getScalarSizeInBits();
James Molloy90111f72015-11-12 12:29:09 +00002785
2786 SDValue Tmp, Tmp2;
2787 Tmp = DAG.getConstant(0, dl, VT);
2788 for (unsigned I = 0, J = Sz-1; I < Sz; ++I, --J) {
2789 if (I < J)
2790 Tmp2 =
2791 DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(J - I, dl, SHVT));
2792 else
2793 Tmp2 =
2794 DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(I - J, dl, SHVT));
James Molloybb1dbf52015-11-13 10:02:36 +00002795
2796 APInt Shift(Sz, 1);
2797 Shift = Shift.shl(J);
2798 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(Shift, dl, VT));
James Molloy90111f72015-11-12 12:29:09 +00002799 Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp, Tmp2);
2800 }
2801
2802 return Tmp;
2803}
2804
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002805/// Open code the operations for BSWAP of the specified operation.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002806SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, SDLoc dl) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002807 EVT VT = Op.getValueType();
Mehdi Amini9639d652015-07-09 02:09:20 +00002808 EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002809 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson9f944592009-08-11 20:47:22 +00002810 switch (VT.getSimpleVT().SimpleTy) {
Craig Topperee4dab52012-02-05 08:31:47 +00002811 default: llvm_unreachable("Unhandled Expand type in BSWAP!");
Owen Anderson9f944592009-08-11 20:47:22 +00002812 case MVT::i16:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002813 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2814 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
Dale Johannesena02e45c2009-02-02 22:12:50 +00002815 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson9f944592009-08-11 20:47:22 +00002816 case MVT::i32:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002817 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2818 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2819 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2820 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2821 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
2822 DAG.getConstant(0xFF0000, dl, VT));
2823 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, dl, VT));
Dale Johannesena02e45c2009-02-02 22:12:50 +00002824 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2825 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2826 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson9f944592009-08-11 20:47:22 +00002827 case MVT::i64:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002828 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
2829 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
2830 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2831 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2832 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2833 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2834 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
2835 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
2836 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7,
2837 DAG.getConstant(255ULL<<48, dl, VT));
2838 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6,
2839 DAG.getConstant(255ULL<<40, dl, VT));
2840 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5,
2841 DAG.getConstant(255ULL<<32, dl, VT));
2842 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4,
2843 DAG.getConstant(255ULL<<24, dl, VT));
2844 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
2845 DAG.getConstant(255ULL<<16, dl, VT));
2846 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2,
2847 DAG.getConstant(255ULL<<8 , dl, VT));
Dale Johannesena02e45c2009-02-02 22:12:50 +00002848 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2849 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2850 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2851 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2852 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2853 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2854 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002855 }
2856}
2857
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00002858/// Expand the specified bitcount instruction into operations.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002859SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002860 SDLoc dl) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00002861 switch (Opc) {
Craig Topperee4dab52012-02-05 08:31:47 +00002862 default: llvm_unreachable("Cannot expand this yet!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00002863 case ISD::CTPOP: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002864 EVT VT = Op.getValueType();
Mehdi Amini9639d652015-07-09 02:09:20 +00002865 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
Benjamin Kramerfff25172011-01-15 20:30:30 +00002866 unsigned Len = VT.getSizeInBits();
2867
Benjamin Kramerbec03ea2011-01-15 21:19:37 +00002868 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2869 "CTPOP not implemented for this type.");
2870
Benjamin Kramerfff25172011-01-15 20:30:30 +00002871 // This is the "best" algorithm from
2872 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2873
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002874 SDValue Mask55 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)),
2875 dl, VT);
2876 SDValue Mask33 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)),
2877 dl, VT);
2878 SDValue Mask0F = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)),
2879 dl, VT);
2880 SDValue Mask01 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)),
2881 dl, VT);
Benjamin Kramerfff25172011-01-15 20:30:30 +00002882
2883 // v = v - ((v >> 1) & 0x55555555...)
2884 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2885 DAG.getNode(ISD::AND, dl, VT,
2886 DAG.getNode(ISD::SRL, dl, VT, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002887 DAG.getConstant(1, dl, ShVT)),
Benjamin Kramerfff25172011-01-15 20:30:30 +00002888 Mask55));
2889 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2890 Op = DAG.getNode(ISD::ADD, dl, VT,
2891 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2892 DAG.getNode(ISD::AND, dl, VT,
2893 DAG.getNode(ISD::SRL, dl, VT, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002894 DAG.getConstant(2, dl, ShVT)),
Benjamin Kramerfff25172011-01-15 20:30:30 +00002895 Mask33));
2896 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2897 Op = DAG.getNode(ISD::AND, dl, VT,
2898 DAG.getNode(ISD::ADD, dl, VT, Op,
2899 DAG.getNode(ISD::SRL, dl, VT, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002900 DAG.getConstant(4, dl, ShVT))),
Benjamin Kramerfff25172011-01-15 20:30:30 +00002901 Mask0F);
2902 // v = (v * 0x01010101...) >> (Len - 8)
2903 Op = DAG.getNode(ISD::SRL, dl, VT,
2904 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002905 DAG.getConstant(Len - 8, dl, ShVT));
Owen Andersonb2c80da2011-02-25 21:41:48 +00002906
Chris Lattner689bdcc2006-01-28 08:25:58 +00002907 return Op;
2908 }
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002909 case ISD::CTLZ_ZERO_UNDEF:
2910 // This trivially expands to CTLZ.
2911 return DAG.getNode(ISD::CTLZ, dl, Op.getValueType(), Op);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002912 case ISD::CTLZ: {
2913 // for now, we do this:
2914 // x = x | (x >> 1);
2915 // x = x | (x >> 2);
2916 // ...
2917 // x = x | (x >>16);
2918 // x = x | (x >>32); // for 64-bit input
2919 // return popcount(~x);
2920 //
Sanjay Patelbb292212014-09-15 19:47:44 +00002921 // Ref: "Hacker's Delight" by Henry Warren
Owen Anderson53aa7a92009-08-10 22:56:29 +00002922 EVT VT = Op.getValueType();
Mehdi Amini9639d652015-07-09 02:09:20 +00002923 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
Duncan Sands13237ac2008-06-06 12:08:01 +00002924 unsigned len = VT.getSizeInBits();
Chris Lattner689bdcc2006-01-28 08:25:58 +00002925 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002926 SDValue Tmp3 = DAG.getConstant(1ULL << i, dl, ShVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002927 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesendc93bbc2009-02-06 21:55:48 +00002928 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner689bdcc2006-01-28 08:25:58 +00002929 }
Dale Johannesena02e45c2009-02-02 22:12:50 +00002930 Op = DAG.getNOT(dl, Op, VT);
2931 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002932 }
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002933 case ISD::CTTZ_ZERO_UNDEF:
2934 // This trivially expands to CTTZ.
2935 return DAG.getNode(ISD::CTTZ, dl, Op.getValueType(), Op);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002936 case ISD::CTTZ: {
2937 // for now, we use: { return popcount(~x & (x - 1)); }
2938 // unless the target has ctlz but not ctpop, in which case we use:
2939 // { return 32 - nlz(~x & (x-1)); }
Sanjay Patelbb292212014-09-15 19:47:44 +00002940 // Ref: "Hacker's Delight" by Henry Warren
Owen Anderson53aa7a92009-08-10 22:56:29 +00002941 EVT VT = Op.getValueType();
Dale Johannesena02e45c2009-02-02 22:12:50 +00002942 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2943 DAG.getNOT(dl, Op, VT),
2944 DAG.getNode(ISD::SUB, dl, VT, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002945 DAG.getConstant(1, dl, VT)));
Chris Lattner689bdcc2006-01-28 08:25:58 +00002946 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohman4aa18462009-01-28 17:46:25 +00002947 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2948 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesena02e45c2009-02-02 22:12:50 +00002949 return DAG.getNode(ISD::SUB, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002950 DAG.getConstant(VT.getSizeInBits(), dl, VT),
Dale Johannesena02e45c2009-02-02 22:12:50 +00002951 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2952 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner689bdcc2006-01-28 08:25:58 +00002953 }
2954 }
2955}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002956
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00002957bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00002958 SmallVector<SDValue, 8> Results;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002959 SDLoc dl(Node);
Eli Friedmane1dc1932009-05-28 20:40:34 +00002960 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Daniel Sandersedc071b2013-11-21 13:24:49 +00002961 bool NeedInvert;
Eli Friedman21d349b2009-05-27 01:25:56 +00002962 switch (Node->getOpcode()) {
2963 case ISD::CTPOP:
2964 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002965 case ISD::CTLZ_ZERO_UNDEF:
Eli Friedman21d349b2009-05-27 01:25:56 +00002966 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002967 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman21d349b2009-05-27 01:25:56 +00002968 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2969 Results.push_back(Tmp1);
2970 break;
James Molloy90111f72015-11-12 12:29:09 +00002971 case ISD::BITREVERSE:
2972 Results.push_back(ExpandBITREVERSE(Node->getOperand(0), dl));
2973 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00002974 case ISD::BSWAP:
Bill Wendlingef408db2009-12-23 00:28:23 +00002975 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman21d349b2009-05-27 01:25:56 +00002976 break;
2977 case ISD::FRAMEADDR:
2978 case ISD::RETURNADDR:
2979 case ISD::FRAME_TO_ARGS_OFFSET:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002980 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
Eli Friedman21d349b2009-05-27 01:25:56 +00002981 break;
2982 case ISD::FLT_ROUNDS_:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002983 Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
Eli Friedman21d349b2009-05-27 01:25:56 +00002984 break;
2985 case ISD::EH_RETURN:
Eli Friedman21d349b2009-05-27 01:25:56 +00002986 case ISD::EH_LABEL:
2987 case ISD::PREFETCH:
Eli Friedman21d349b2009-05-27 01:25:56 +00002988 case ISD::VAEND:
Jim Grosbachdc0a0652010-07-06 23:44:52 +00002989 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbachbbdc5d22010-10-19 23:27:08 +00002990 // If the target didn't expand these, there's nothing to do, so just
2991 // preserve the chain and be done.
Jim Grosbachdc0a0652010-07-06 23:44:52 +00002992 Results.push_back(Node->getOperand(0));
2993 break;
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00002994 case ISD::READCYCLECOUNTER:
2995 // If the target didn't expand this, just return 'zero' and preserve the
2996 // chain.
2997 Results.append(Node->getNumValues() - 1,
2998 DAG.getConstant(0, dl, Node->getValueType(0)));
2999 Results.push_back(Node->getOperand(0));
3000 break;
Jim Grosbachdc0a0652010-07-06 23:44:52 +00003001 case ISD::EH_SJLJ_SETJMP:
Jim Grosbachbbdc5d22010-10-19 23:27:08 +00003002 // If the target didn't expand this, just return 'zero' and preserve the
3003 // chain.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003004 Results.push_back(DAG.getConstant(0, dl, MVT::i32));
Eli Friedman21d349b2009-05-27 01:25:56 +00003005 Results.push_back(Node->getOperand(0));
3006 break;
Eli Friedman452aae62011-08-26 02:59:24 +00003007 case ISD::ATOMIC_LOAD: {
3008 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003009 SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
Tim Northover420a2162014-06-13 14:24:07 +00003010 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3011 SDValue Swap = DAG.getAtomicCmpSwap(
3012 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3013 Node->getOperand(0), Node->getOperand(1), Zero, Zero,
3014 cast<AtomicSDNode>(Node)->getMemOperand(),
3015 cast<AtomicSDNode>(Node)->getOrdering(),
3016 cast<AtomicSDNode>(Node)->getOrdering(),
3017 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman452aae62011-08-26 02:59:24 +00003018 Results.push_back(Swap.getValue(0));
3019 Results.push_back(Swap.getValue(1));
3020 break;
3021 }
3022 case ISD::ATOMIC_STORE: {
3023 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
3024 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
3025 cast<AtomicSDNode>(Node)->getMemoryVT(),
3026 Node->getOperand(0),
3027 Node->getOperand(1), Node->getOperand(2),
3028 cast<AtomicSDNode>(Node)->getMemOperand(),
3029 cast<AtomicSDNode>(Node)->getOrdering(),
3030 cast<AtomicSDNode>(Node)->getSynchScope());
3031 Results.push_back(Swap.getValue(1));
3032 break;
3033 }
Tim Northover420a2162014-06-13 14:24:07 +00003034 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
3035 // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
3036 // splits out the success value as a comparison. Expanding the resulting
3037 // ATOMIC_CMP_SWAP will produce a libcall.
3038 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3039 SDValue Res = DAG.getAtomicCmpSwap(
3040 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3041 Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
3042 Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand(),
3043 cast<AtomicSDNode>(Node)->getSuccessOrdering(),
3044 cast<AtomicSDNode>(Node)->getFailureOrdering(),
3045 cast<AtomicSDNode>(Node)->getSynchScope());
3046
3047 SDValue Success = DAG.getSetCC(SDLoc(Node), Node->getValueType(1),
3048 Res, Node->getOperand(2), ISD::SETEQ);
3049
3050 Results.push_back(Res.getValue(0));
3051 Results.push_back(Success);
3052 Results.push_back(Res.getValue(1));
3053 break;
3054 }
Eli Friedman2892d822009-05-27 12:20:41 +00003055 case ISD::DYNAMIC_STACKALLOC:
3056 ExpandDYNAMIC_STACKALLOC(Node, Results);
3057 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00003058 case ISD::MERGE_VALUES:
3059 for (unsigned i = 0; i < Node->getNumValues(); i++)
3060 Results.push_back(Node->getOperand(i));
3061 break;
3062 case ISD::UNDEF: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003063 EVT VT = Node->getValueType(0);
Eli Friedman21d349b2009-05-27 01:25:56 +00003064 if (VT.isInteger())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003065 Results.push_back(DAG.getConstant(0, dl, VT));
Chris Lattnercd927182010-04-07 23:47:51 +00003066 else {
3067 assert(VT.isFloatingPoint() && "Unknown value type!");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003068 Results.push_back(DAG.getConstantFP(0, dl, VT));
Chris Lattnercd927182010-04-07 23:47:51 +00003069 }
Eli Friedman21d349b2009-05-27 01:25:56 +00003070 break;
3071 }
Eli Friedman21d349b2009-05-27 01:25:56 +00003072 case ISD::FP_ROUND:
Wesley Peck527da1b2010-11-23 03:31:01 +00003073 case ISD::BITCAST:
Eli Friedman21d349b2009-05-27 01:25:56 +00003074 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3075 Node->getValueType(0), dl);
3076 Results.push_back(Tmp1);
3077 break;
3078 case ISD::FP_EXTEND:
3079 Tmp1 = EmitStackConvert(Node->getOperand(0),
3080 Node->getOperand(0).getValueType(),
3081 Node->getValueType(0), dl);
3082 Results.push_back(Tmp1);
3083 break;
3084 case ISD::SIGN_EXTEND_INREG: {
3085 // NOTE: we could fall back on load/store here too for targets without
3086 // SAR. However, it is doubtful that any exist.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003087 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman1d459e42009-12-11 21:31:27 +00003088 EVT VT = Node->getValueType(0);
Mehdi Amini9639d652015-07-09 02:09:20 +00003089 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003090 if (VT.isVector())
Dan Gohman1d459e42009-12-11 21:31:27 +00003091 ShiftAmountTy = VT;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003092 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
3093 ExtraVT.getScalarType().getSizeInBits();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003094 SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
Eli Friedman21d349b2009-05-27 01:25:56 +00003095 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3096 Node->getOperand(0), ShiftCst);
Bill Wendlingef408db2009-12-23 00:28:23 +00003097 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3098 Results.push_back(Tmp1);
Eli Friedman21d349b2009-05-27 01:25:56 +00003099 break;
3100 }
3101 case ISD::FP_ROUND_INREG: {
3102 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00003103 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman21d349b2009-05-27 01:25:56 +00003104
3105 // NOTE: there is a choice here between constantly creating new stack
3106 // slots and always reusing the same one. We currently always create
3107 // new ones, as reuse may inhibit scheduling.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003108 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman21d349b2009-05-27 01:25:56 +00003109 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3110 Node->getValueType(0), dl);
3111 Results.push_back(Tmp1);
3112 break;
3113 }
3114 case ISD::SINT_TO_FP:
3115 case ISD::UINT_TO_FP:
3116 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3117 Node->getOperand(0), Node->getValueType(0), dl);
3118 Results.push_back(Tmp1);
3119 break;
Jan Veselyeca89d22014-07-10 22:40:18 +00003120 case ISD::FP_TO_SINT:
3121 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
3122 Results.push_back(Tmp1);
Tom Stellardaad46592014-06-17 16:53:07 +00003123 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00003124 case ISD::FP_TO_UINT: {
3125 SDValue True, False;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003126 EVT VT = Node->getOperand(0).getValueType();
3127 EVT NVT = Node->getValueType(0);
Tim Northover29178a32013-01-22 09:46:31 +00003128 APFloat apf(DAG.EVTToAPFloatSemantics(VT),
3129 APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman21d349b2009-05-27 01:25:56 +00003130 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3131 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003132 Tmp1 = DAG.getConstantFP(apf, dl, VT);
Matt Arsenault758659232013-05-18 00:21:46 +00003133 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(VT),
Eli Friedman21d349b2009-05-27 01:25:56 +00003134 Node->getOperand(0),
3135 Tmp1, ISD::SETLT);
3136 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Sanjay Patela2607012015-09-16 16:31:21 +00003137 // TODO: Should any fast-math-flags be set for the FSUB?
Bill Wendlingef408db2009-12-23 00:28:23 +00003138 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3139 DAG.getNode(ISD::FSUB, dl, VT,
3140 Node->getOperand(0), Tmp1));
Eli Friedman21d349b2009-05-27 01:25:56 +00003141 False = DAG.getNode(ISD::XOR, dl, NVT, False,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003142 DAG.getConstant(x, dl, NVT));
Matt Arsenaultd2f03322013-06-14 22:04:37 +00003143 Tmp1 = DAG.getSelect(dl, NVT, Tmp2, True, False);
Eli Friedman21d349b2009-05-27 01:25:56 +00003144 Results.push_back(Tmp1);
3145 break;
3146 }
Charles Davis11952592015-08-25 23:27:41 +00003147 case ISD::VAARG:
3148 Results.push_back(DAG.expandVAArg(Node));
Eli Friedman3b251702009-05-27 07:58:35 +00003149 Results.push_back(Results[0].getValue(1));
3150 break;
Charles Davis11952592015-08-25 23:27:41 +00003151 case ISD::VACOPY:
3152 Results.push_back(DAG.expandVACopy(Node));
Eli Friedman21d349b2009-05-27 01:25:56 +00003153 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00003154 case ISD::EXTRACT_VECTOR_ELT:
3155 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3156 // This must be an access of the only element. Return it.
Wesley Peck527da1b2010-11-23 03:31:01 +00003157 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman21d349b2009-05-27 01:25:56 +00003158 Node->getOperand(0));
3159 else
3160 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3161 Results.push_back(Tmp1);
3162 break;
3163 case ISD::EXTRACT_SUBVECTOR:
Bill Wendlingef408db2009-12-23 00:28:23 +00003164 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman21d349b2009-05-27 01:25:56 +00003165 break;
David Greenebab5e6e2011-01-26 19:13:22 +00003166 case ISD::INSERT_SUBVECTOR:
3167 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3168 break;
Eli Friedman3b251702009-05-27 07:58:35 +00003169 case ISD::CONCAT_VECTORS: {
Bill Wendlingef408db2009-12-23 00:28:23 +00003170 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman3b251702009-05-27 07:58:35 +00003171 break;
3172 }
Eli Friedman21d349b2009-05-27 01:25:56 +00003173 case ISD::SCALAR_TO_VECTOR:
Bill Wendlingef408db2009-12-23 00:28:23 +00003174 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman21d349b2009-05-27 01:25:56 +00003175 break;
Eli Friedmana8f9a022009-05-27 02:16:40 +00003176 case ISD::INSERT_VECTOR_ELT:
Bill Wendlingef408db2009-12-23 00:28:23 +00003177 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3178 Node->getOperand(1),
3179 Node->getOperand(2), dl));
Eli Friedmana8f9a022009-05-27 02:16:40 +00003180 break;
Eli Friedman3b251702009-05-27 07:58:35 +00003181 case ISD::VECTOR_SHUFFLE: {
Benjamin Kramer339ced42012-01-15 13:16:05 +00003182 SmallVector<int, 32> NewMask;
3183 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
Eli Friedman3b251702009-05-27 07:58:35 +00003184
Owen Anderson53aa7a92009-08-10 22:56:29 +00003185 EVT VT = Node->getValueType(0);
3186 EVT EltVT = VT.getVectorElementType();
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003187 SDValue Op0 = Node->getOperand(0);
3188 SDValue Op1 = Node->getOperand(1);
3189 if (!TLI.isTypeLegal(EltVT)) {
3190
3191 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
3192
3193 // BUILD_VECTOR operands are allowed to be wider than the element type.
Jack Carter5c0af482013-11-19 23:43:22 +00003194 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
3195 // it.
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003196 if (NewEltVT.bitsLT(EltVT)) {
3197
3198 // Convert shuffle node.
3199 // If original node was v4i64 and the new EltVT is i32,
3200 // cast operands to v8i32 and re-build the mask.
3201
3202 // Calculate new VT, the size of the new VT should be equal to original.
Jack Carter5c0af482013-11-19 23:43:22 +00003203 EVT NewVT =
3204 EVT::getVectorVT(*DAG.getContext(), NewEltVT,
3205 VT.getSizeInBits() / NewEltVT.getSizeInBits());
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003206 assert(NewVT.bitsEq(VT));
3207
3208 // cast operands to new VT
3209 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
3210 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
3211
3212 // Convert the shuffle mask
Jack Carter5c0af482013-11-19 23:43:22 +00003213 unsigned int factor =
3214 NewVT.getVectorNumElements()/VT.getVectorNumElements();
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003215
3216 // EltVT gets smaller
3217 assert(factor > 0);
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003218
3219 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
3220 if (Mask[i] < 0) {
3221 for (unsigned fi = 0; fi < factor; ++fi)
3222 NewMask.push_back(Mask[i]);
3223 }
3224 else {
3225 for (unsigned fi = 0; fi < factor; ++fi)
3226 NewMask.push_back(Mask[i]*factor+fi);
3227 }
3228 }
3229 Mask = NewMask;
3230 VT = NewVT;
3231 }
3232 EltVT = NewEltVT;
3233 }
Eli Friedman3b251702009-05-27 07:58:35 +00003234 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky8ec21a22012-01-03 11:59:04 +00003235 SmallVector<SDValue, 16> Ops;
Eli Friedman3b251702009-05-27 07:58:35 +00003236 for (unsigned i = 0; i != NumElems; ++i) {
3237 if (Mask[i] < 0) {
3238 Ops.push_back(DAG.getUNDEF(EltVT));
3239 continue;
3240 }
3241 unsigned Idx = Mask[i];
3242 if (Idx < NumElems)
Mehdi Amini44ede332015-07-09 02:09:04 +00003243 Ops.push_back(DAG.getNode(
3244 ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
3245 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
Eli Friedman3b251702009-05-27 07:58:35 +00003246 else
Mehdi Amini44ede332015-07-09 02:09:04 +00003247 Ops.push_back(DAG.getNode(
3248 ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
3249 DAG.getConstant(Idx - NumElems, dl,
3250 TLI.getVectorIdxTy(DAG.getDataLayout()))));
Eli Friedman3b251702009-05-27 07:58:35 +00003251 }
Nadav Rotem61bdf792012-01-10 14:28:46 +00003252
Craig Topper48d114b2014-04-26 18:35:24 +00003253 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
Nadav Rotem61bdf792012-01-10 14:28:46 +00003254 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
3255 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
Eli Friedman3b251702009-05-27 07:58:35 +00003256 Results.push_back(Tmp1);
3257 break;
3258 }
Eli Friedman21d349b2009-05-27 01:25:56 +00003259 case ISD::EXTRACT_ELEMENT: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003260 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman21d349b2009-05-27 01:25:56 +00003261 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3262 // 1 -> Hi
3263 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
Mehdi Amini9639d652015-07-09 02:09:20 +00003264 DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
3265 TLI.getShiftAmountTy(
3266 Node->getOperand(0).getValueType(),
3267 DAG.getDataLayout())));
Eli Friedman21d349b2009-05-27 01:25:56 +00003268 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3269 } else {
3270 // 0 -> Lo
3271 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3272 Node->getOperand(0));
3273 }
3274 Results.push_back(Tmp1);
3275 break;
3276 }
Eli Friedmana8f9a022009-05-27 02:16:40 +00003277 case ISD::STACKSAVE:
3278 // Expand to CopyFromReg if the target set
3279 // StackPointerRegisterToSaveRestore.
3280 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendlingef408db2009-12-23 00:28:23 +00003281 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3282 Node->getValueType(0)));
Eli Friedmana8f9a022009-05-27 02:16:40 +00003283 Results.push_back(Results[0].getValue(1));
3284 } else {
Bill Wendlingef408db2009-12-23 00:28:23 +00003285 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedmana8f9a022009-05-27 02:16:40 +00003286 Results.push_back(Node->getOperand(0));
3287 }
3288 break;
3289 case ISD::STACKRESTORE:
Bill Wendlingef408db2009-12-23 00:28:23 +00003290 // Expand to CopyToReg if the target set
3291 // StackPointerRegisterToSaveRestore.
3292 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3293 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3294 Node->getOperand(1)));
3295 } else {
3296 Results.push_back(Node->getOperand(0));
3297 }
Eli Friedmana8f9a022009-05-27 02:16:40 +00003298 break;
Eli Friedman2892d822009-05-27 12:20:41 +00003299 case ISD::FCOPYSIGN:
Bill Wendlingef408db2009-12-23 00:28:23 +00003300 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman2892d822009-05-27 12:20:41 +00003301 break;
Eli Friedmand6f28342009-05-27 03:33:44 +00003302 case ISD::FNEG:
3303 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003304 Tmp1 = DAG.getConstantFP(-0.0, dl, Node->getValueType(0));
Sanjay Patela2607012015-09-16 16:31:21 +00003305 // TODO: If FNEG has fast-math-flags, propagate them to the FSUB.
Eli Friedmand6f28342009-05-27 03:33:44 +00003306 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3307 Node->getOperand(0));
3308 Results.push_back(Tmp1);
3309 break;
Matthias Braunb9610a62015-11-12 01:02:47 +00003310 case ISD::FABS:
3311 Results.push_back(ExpandFABS(Node));
Eli Friedmand6f28342009-05-27 03:33:44 +00003312 break;
James Molloy7e9776b2015-05-15 09:03:15 +00003313 case ISD::SMIN:
3314 case ISD::SMAX:
3315 case ISD::UMIN:
3316 case ISD::UMAX: {
3317 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
3318 ISD::CondCode Pred;
3319 switch (Node->getOpcode()) {
3320 default: llvm_unreachable("How did we get here?");
3321 case ISD::SMAX: Pred = ISD::SETGT; break;
3322 case ISD::SMIN: Pred = ISD::SETLT; break;
3323 case ISD::UMAX: Pred = ISD::SETUGT; break;
3324 case ISD::UMIN: Pred = ISD::SETULT; break;
3325 }
3326 Tmp1 = Node->getOperand(0);
3327 Tmp2 = Node->getOperand(1);
3328 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
3329 Results.push_back(Tmp1);
3330 break;
3331 }
3332
Eli Friedmand6f28342009-05-27 03:33:44 +00003333 case ISD::FSIN:
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003334 case ISD::FCOS: {
3335 EVT VT = Node->getValueType(0);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003336 // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
3337 // fcos which share the same operand and both are used.
3338 if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
Paul Redmondf29ddfe2013-02-15 18:45:18 +00003339 canCombineSinCosLibcall(Node, TLI, TM))
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003340 && useSinCos(Node)) {
3341 SDVTList VTs = DAG.getVTList(VT, VT);
3342 Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003343 if (Node->getOpcode() == ISD::FCOS)
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003344 Tmp1 = Tmp1.getValue(1);
3345 Results.push_back(Tmp1);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003346 }
Eli Friedmand6f28342009-05-27 03:33:44 +00003347 break;
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003348 }
Matt Arsenault0dc54c42015-02-20 22:10:33 +00003349 case ISD::FMAD:
3350 llvm_unreachable("Illegal fmad should never be formed");
3351
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003352 case ISD::FP16_TO_FP:
3353 if (Node->getValueType(0) != MVT::f32) {
3354 // We can extend to types bigger than f32 in two steps without changing
3355 // the result. Since "f16 -> f32" is much more commonly available, give
3356 // CodeGen the option of emitting that before resorting to a libcall.
3357 SDValue Res =
3358 DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
3359 Results.push_back(
3360 DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
Tim Northoverfd7e4242014-07-17 10:51:23 +00003361 }
Anton Korobeynikov59e96002010-03-14 18:42:24 +00003362 break;
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003363 case ISD::FP_TO_FP16:
Eric Christopher824f42f2015-05-12 01:26:05 +00003364 if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
Andrea Di Biagioaf3f3972015-02-23 22:59:02 +00003365 SDValue Op = Node->getOperand(0);
3366 MVT SVT = Op.getSimpleValueType();
3367 if ((SVT == MVT::f64 || SVT == MVT::f80) &&
3368 TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
3369 // Under fastmath, we can expand this node into a fround followed by
3370 // a float-half conversion.
3371 SDValue FloatVal = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003372 DAG.getIntPtrConstant(0, dl));
Andrea Di Biagioaf3f3972015-02-23 22:59:02 +00003373 Results.push_back(
3374 DAG.getNode(ISD::FP_TO_FP16, dl, MVT::i16, FloatVal));
Andrea Di Biagioaf3f3972015-02-23 22:59:02 +00003375 }
3376 }
Anton Korobeynikov59e96002010-03-14 18:42:24 +00003377 break;
Eli Friedman0e494312009-05-27 07:32:27 +00003378 case ISD::ConstantFP: {
3379 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendlingef408db2009-12-23 00:28:23 +00003380 // Check to see if this FP immediate is already legal.
3381 // If this is a legal constant, turn it into a TargetConstantFP node.
Dan Gohman198b7ff2011-11-03 21:49:52 +00003382 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
3383 Results.push_back(ExpandConstantFP(CFP, true));
Eli Friedman0e494312009-05-27 07:32:27 +00003384 break;
3385 }
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00003386 case ISD::FSUB: {
3387 EVT VT = Node->getValueType(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003388 if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
3389 TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
Sanjay Patela2607012015-09-16 16:31:21 +00003390 const SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(Node)->Flags;
Oliver Stannard51b1d462014-08-21 12:50:31 +00003391 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
Sanjay Patela2607012015-09-16 16:31:21 +00003392 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003393 Results.push_back(Tmp1);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003394 }
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00003395 break;
3396 }
Eli Friedman56883962009-05-27 07:05:37 +00003397 case ISD::SUB: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003398 EVT VT = Node->getValueType(0);
Eli Friedman56883962009-05-27 07:05:37 +00003399 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3400 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3401 "Don't know how to expand this subtraction!");
3402 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003403 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
3404 VT));
3405 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
Bill Wendlingef408db2009-12-23 00:28:23 +00003406 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman56883962009-05-27 07:05:37 +00003407 break;
3408 }
Eli Friedman0e494312009-05-27 07:32:27 +00003409 case ISD::UREM:
3410 case ISD::SREM: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003411 EVT VT = Node->getValueType(0);
Eli Friedman0e494312009-05-27 07:32:27 +00003412 bool isSigned = Node->getOpcode() == ISD::SREM;
3413 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3414 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3415 Tmp2 = Node->getOperand(0);
3416 Tmp3 = Node->getOperand(1);
Artyom Skrobovb844fa72015-10-20 13:06:02 +00003417 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
Evan Cheng0e88c7d2013-01-29 02:32:37 +00003418 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmane1bc3792009-05-28 03:06:16 +00003419 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003420 Results.push_back(Tmp1);
Eli Friedmane1bc3792009-05-28 03:06:16 +00003421 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedman0e494312009-05-27 07:32:27 +00003422 // X % Y -> X-X/Y*Y
3423 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3424 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3425 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003426 Results.push_back(Tmp1);
3427 }
Eli Friedman56883962009-05-27 07:05:37 +00003428 break;
3429 }
Eli Friedman0e494312009-05-27 07:32:27 +00003430 case ISD::UDIV:
3431 case ISD::SDIV: {
3432 bool isSigned = Node->getOpcode() == ISD::SDIV;
3433 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003434 EVT VT = Node->getValueType(0);
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003435 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3436 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedman0e494312009-05-27 07:32:27 +00003437 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3438 Node->getOperand(1));
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003439 Results.push_back(Tmp1);
3440 }
Eli Friedman56883962009-05-27 07:05:37 +00003441 break;
3442 }
3443 case ISD::MULHU:
3444 case ISD::MULHS: {
3445 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3446 ISD::SMUL_LOHI;
Owen Anderson53aa7a92009-08-10 22:56:29 +00003447 EVT VT = Node->getValueType(0);
Eli Friedman56883962009-05-27 07:05:37 +00003448 SDVTList VTs = DAG.getVTList(VT, VT);
3449 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3450 "If this wasn't legal, it shouldn't have been created!");
3451 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3452 Node->getOperand(1));
3453 Results.push_back(Tmp1.getValue(1));
3454 break;
3455 }
3456 case ISD::MUL: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003457 EVT VT = Node->getValueType(0);
Eli Friedman56883962009-05-27 07:05:37 +00003458 SDVTList VTs = DAG.getVTList(VT, VT);
3459 // See if multiply or divide can be lowered using two-result operations.
3460 // We just need the low half of the multiply; try both the signed
3461 // and unsigned forms. If the target supports both SMUL_LOHI and
3462 // UMUL_LOHI, form a preference by checking which forms of plain
3463 // MULH it supports.
3464 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3465 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3466 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3467 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3468 unsigned OpToUse = 0;
3469 if (HasSMUL_LOHI && !HasMULHS) {
3470 OpToUse = ISD::SMUL_LOHI;
3471 } else if (HasUMUL_LOHI && !HasMULHU) {
3472 OpToUse = ISD::UMUL_LOHI;
3473 } else if (HasSMUL_LOHI) {
3474 OpToUse = ISD::SMUL_LOHI;
3475 } else if (HasUMUL_LOHI) {
3476 OpToUse = ISD::UMUL_LOHI;
3477 }
3478 if (OpToUse) {
Bill Wendlingef408db2009-12-23 00:28:23 +00003479 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3480 Node->getOperand(1)));
Eli Friedman56883962009-05-27 07:05:37 +00003481 break;
3482 }
Tom Stellarda1a5d9a2014-04-11 16:12:01 +00003483
3484 SDValue Lo, Hi;
3485 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
3486 if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
3487 TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
3488 TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
3489 TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
3490 TLI.expandMUL(Node, Lo, Hi, HalfType, DAG)) {
3491 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
3492 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
Mehdi Amini9639d652015-07-09 02:09:20 +00003493 SDValue Shift =
3494 DAG.getConstant(HalfType.getSizeInBits(), dl,
3495 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
Tom Stellarda1a5d9a2014-04-11 16:12:01 +00003496 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3497 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
Tom Stellarda1a5d9a2014-04-11 16:12:01 +00003498 }
Eli Friedman56883962009-05-27 07:05:37 +00003499 break;
3500 }
Eli Friedman2892d822009-05-27 12:20:41 +00003501 case ISD::SADDO:
3502 case ISD::SSUBO: {
3503 SDValue LHS = Node->getOperand(0);
3504 SDValue RHS = Node->getOperand(1);
3505 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3506 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3507 LHS, RHS);
3508 Results.push_back(Sum);
Matt Arsenault3ee37462014-05-28 20:51:42 +00003509 EVT ResultType = Node->getValueType(1);
3510 EVT OType = getSetCCResultType(Node->getValueType(0));
Bill Wendlingef408db2009-12-23 00:28:23 +00003511
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003512 SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
Eli Friedman2892d822009-05-27 12:20:41 +00003513
3514 // LHSSign -> LHS >= 0
3515 // RHSSign -> RHS >= 0
3516 // SumSign -> Sum >= 0
3517 //
3518 // Add:
3519 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3520 // Sub:
3521 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3522 //
3523 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3524 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3525 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3526 Node->getOpcode() == ISD::SADDO ?
3527 ISD::SETEQ : ISD::SETNE);
3528
3529 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3530 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3531
3532 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Daniel Sanderscbd44c52014-07-10 10:18:12 +00003533 Results.push_back(DAG.getBoolExtOrTrunc(Cmp, dl, ResultType, ResultType));
Eli Friedman2892d822009-05-27 12:20:41 +00003534 break;
3535 }
3536 case ISD::UADDO:
3537 case ISD::USUBO: {
3538 SDValue LHS = Node->getOperand(0);
3539 SDValue RHS = Node->getOperand(1);
3540 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3541 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3542 LHS, RHS);
3543 Results.push_back(Sum);
Matt Arsenault3ee37462014-05-28 20:51:42 +00003544
3545 EVT ResultType = Node->getValueType(1);
3546 EVT SetCCType = getSetCCResultType(Node->getValueType(0));
3547 ISD::CondCode CC
3548 = Node->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT;
3549 SDValue SetCC = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
3550
Daniel Sanderscbd44c52014-07-10 10:18:12 +00003551 Results.push_back(DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType));
Eli Friedman2892d822009-05-27 12:20:41 +00003552 break;
3553 }
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003554 case ISD::UMULO:
3555 case ISD::SMULO: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003556 EVT VT = Node->getValueType(0);
Eric Christopherbcaedb52011-04-20 01:19:45 +00003557 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003558 SDValue LHS = Node->getOperand(0);
3559 SDValue RHS = Node->getOperand(1);
3560 SDValue BottomHalf;
3561 SDValue TopHalf;
Nuno Lopes129819d2009-12-23 17:48:10 +00003562 static const unsigned Ops[2][3] =
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003563 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3564 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3565 bool isSigned = Node->getOpcode() == ISD::SMULO;
3566 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3567 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3568 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3569 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3570 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3571 RHS);
3572 TopHalf = BottomHalf.getValue(1);
Eric Christopher83dd2fa2014-04-28 22:24:57 +00003573 } else if (TLI.isTypeLegal(WideVT)) {
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003574 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3575 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3576 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3577 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003578 DAG.getIntPtrConstant(0, dl));
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003579 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003580 DAG.getIntPtrConstant(1, dl));
Eric Christopherbb14f652011-01-20 00:29:24 +00003581 } else {
3582 // We can fall back to a libcall with an illegal type for the MUL if we
3583 // have a libcall big enough.
3584 // Also, we can fall back to a division in some cases, but that's a big
3585 // performance hit in the general case.
Eric Christopherbb14f652011-01-20 00:29:24 +00003586 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3587 if (WideVT == MVT::i16)
3588 LC = RTLIB::MUL_I16;
3589 else if (WideVT == MVT::i32)
3590 LC = RTLIB::MUL_I32;
3591 else if (WideVT == MVT::i64)
3592 LC = RTLIB::MUL_I64;
3593 else if (WideVT == MVT::i128)
3594 LC = RTLIB::MUL_I128;
3595 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanae9b1682011-05-16 22:09:53 +00003596
3597 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherbcaedb52011-04-20 01:19:45 +00003598 // part.
3599 unsigned LoSize = VT.getSizeInBits();
Mehdi Amini44ede332015-07-09 02:09:04 +00003600 SDValue HiLHS =
3601 DAG.getNode(ISD::SRA, dl, VT, RHS,
3602 DAG.getConstant(LoSize - 1, dl,
3603 TLI.getPointerTy(DAG.getDataLayout())));
3604 SDValue HiRHS =
3605 DAG.getNode(ISD::SRA, dl, VT, LHS,
3606 DAG.getConstant(LoSize - 1, dl,
3607 TLI.getPointerTy(DAG.getDataLayout())));
Owen Andersonb2c80da2011-02-25 21:41:48 +00003608
Eric Christopherbcaedb52011-04-20 01:19:45 +00003609 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3610 // pre-lowered to the correct types. This all depends upon WideVT not
3611 // being a legal type for the architecture and thus has to be split to
3612 // two arguments.
3613 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3614 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3615 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003616 DAG.getIntPtrConstant(0, dl));
Eric Christopherbcaedb52011-04-20 01:19:45 +00003617 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003618 DAG.getIntPtrConstant(1, dl));
Dan Gohman198b7ff2011-11-03 21:49:52 +00003619 // Ret is a node with an illegal type. Because such things are not
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00003620 // generally permitted during this phase of legalization, make sure the
3621 // node has no more uses. The above EXTRACT_ELEMENT nodes should have been
3622 // folded.
3623 assert(Ret->use_empty() &&
3624 "Unexpected uses of illegally type from expanded lib call.");
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003625 }
Dan Gohmanae9b1682011-05-16 22:09:53 +00003626
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003627 if (isSigned) {
Mehdi Amini9639d652015-07-09 02:09:20 +00003628 Tmp1 = DAG.getConstant(
3629 VT.getSizeInBits() - 1, dl,
3630 TLI.getShiftAmountTy(BottomHalf.getValueType(), DAG.getDataLayout()));
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003631 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
Matt Arsenault758659232013-05-18 00:21:46 +00003632 TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf, Tmp1,
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003633 ISD::SETNE);
3634 } else {
Matt Arsenault758659232013-05-18 00:21:46 +00003635 TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003636 DAG.getConstant(0, dl, VT), ISD::SETNE);
Eli Friedmanabfad5d2009-06-16 06:58:29 +00003637 }
3638 Results.push_back(BottomHalf);
3639 Results.push_back(TopHalf);
3640 break;
3641 }
Eli Friedman0e494312009-05-27 07:32:27 +00003642 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003643 EVT PairTy = Node->getValueType(0);
Eli Friedman0e494312009-05-27 07:32:27 +00003644 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3645 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Mehdi Amini9639d652015-07-09 02:09:20 +00003646 Tmp2 = DAG.getNode(
3647 ISD::SHL, dl, PairTy, Tmp2,
3648 DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
3649 TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
Bill Wendlingef408db2009-12-23 00:28:23 +00003650 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedman0e494312009-05-27 07:32:27 +00003651 break;
3652 }
Eli Friedman3b251702009-05-27 07:58:35 +00003653 case ISD::SELECT:
3654 Tmp1 = Node->getOperand(0);
3655 Tmp2 = Node->getOperand(1);
3656 Tmp3 = Node->getOperand(2);
Bill Wendlingef408db2009-12-23 00:28:23 +00003657 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman3b251702009-05-27 07:58:35 +00003658 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3659 Tmp2, Tmp3,
3660 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendlingef408db2009-12-23 00:28:23 +00003661 } else {
Eli Friedman3b251702009-05-27 07:58:35 +00003662 Tmp1 = DAG.getSelectCC(dl, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003663 DAG.getConstant(0, dl, Tmp1.getValueType()),
Eli Friedman3b251702009-05-27 07:58:35 +00003664 Tmp2, Tmp3, ISD::SETNE);
Bill Wendlingef408db2009-12-23 00:28:23 +00003665 }
Eli Friedman3b251702009-05-27 07:58:35 +00003666 Results.push_back(Tmp1);
3667 break;
Eli Friedman2892d822009-05-27 12:20:41 +00003668 case ISD::BR_JT: {
3669 SDValue Chain = Node->getOperand(0);
3670 SDValue Table = Node->getOperand(1);
3671 SDValue Index = Node->getOperand(2);
3672
Mehdi Amini44ede332015-07-09 02:09:04 +00003673 EVT PTy = TLI.getPointerTy(DAG.getDataLayout());
Chris Lattnerb6db2c62010-01-25 23:26:13 +00003674
Mehdi Amini8ac7a9d2015-07-07 19:07:19 +00003675 const DataLayout &TD = DAG.getDataLayout();
Chris Lattnerb6db2c62010-01-25 23:26:13 +00003676 unsigned EntrySize =
3677 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach9b7755f2010-07-02 17:41:59 +00003678
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003679 Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
3680 DAG.getConstant(EntrySize, dl, Index.getValueType()));
Tom Stellard838e2342013-08-26 15:06:10 +00003681 SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
3682 Index, Table);
Eli Friedman2892d822009-05-27 12:20:41 +00003683
Owen Anderson117c9e82009-08-12 00:36:31 +00003684 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003685 SDValue LD = DAG.getExtLoad(
3686 ISD::SEXTLOAD, dl, PTy, Chain, Addr,
3687 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT,
3688 false, false, false, 0);
Eli Friedman2892d822009-05-27 12:20:41 +00003689 Addr = LD;
Dan Gohmanc3349602010-04-19 19:05:59 +00003690 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman2892d822009-05-27 12:20:41 +00003691 // For PIC, the sequence is:
Bill Wendlingef408db2009-12-23 00:28:23 +00003692 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman2892d822009-05-27 12:20:41 +00003693 // RelocBase can be JumpTable, GOT or some sort of global base.
3694 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3695 TLI.getPICJumpTableRelocBase(Table, DAG));
3696 }
Owen Anderson9f944592009-08-11 20:47:22 +00003697 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman2892d822009-05-27 12:20:41 +00003698 Results.push_back(Tmp1);
3699 break;
3700 }
Eli Friedman0e494312009-05-27 07:32:27 +00003701 case ISD::BRCOND:
3702 // Expand brcond's setcc into its constituent parts and create a BR_CC
3703 // Node.
3704 Tmp1 = Node->getOperand(0);
3705 Tmp2 = Node->getOperand(1);
Bill Wendlingef408db2009-12-23 00:28:23 +00003706 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson9f944592009-08-11 20:47:22 +00003707 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedman0e494312009-05-27 07:32:27 +00003708 Tmp1, Tmp2.getOperand(2),
3709 Tmp2.getOperand(0), Tmp2.getOperand(1),
3710 Node->getOperand(2));
Bill Wendlingef408db2009-12-23 00:28:23 +00003711 } else {
Stuart Hastingsaa02c082011-05-13 00:51:54 +00003712 // We test only the i1 bit. Skip the AND if UNDEF.
3713 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3714 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003715 DAG.getConstant(1, dl, Tmp2.getValueType()));
Owen Anderson9f944592009-08-11 20:47:22 +00003716 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastingsaa02c082011-05-13 00:51:54 +00003717 DAG.getCondCode(ISD::SETNE), Tmp3,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003718 DAG.getConstant(0, dl, Tmp3.getValueType()),
Eli Friedman0e494312009-05-27 07:32:27 +00003719 Node->getOperand(2));
Bill Wendlingef408db2009-12-23 00:28:23 +00003720 }
Eli Friedman0e494312009-05-27 07:32:27 +00003721 Results.push_back(Tmp1);
3722 break;
Eli Friedman5df72022009-05-28 03:56:57 +00003723 case ISD::SETCC: {
3724 Tmp1 = Node->getOperand(0);
3725 Tmp2 = Node->getOperand(1);
3726 Tmp3 = Node->getOperand(2);
Tom Stellard08690a12013-09-28 02:50:32 +00003727 bool Legalized = LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2,
Daniel Sandersedc071b2013-11-21 13:24:49 +00003728 Tmp3, NeedInvert, dl);
Eli Friedman5df72022009-05-28 03:56:57 +00003729
Tom Stellard08690a12013-09-28 02:50:32 +00003730 if (Legalized) {
Daniel Sandersedc071b2013-11-21 13:24:49 +00003731 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
3732 // condition code, create a new SETCC node.
Tom Stellard08690a12013-09-28 02:50:32 +00003733 if (Tmp3.getNode())
3734 Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3735 Tmp1, Tmp2, Tmp3);
3736
Daniel Sandersedc071b2013-11-21 13:24:49 +00003737 // If we expanded the SETCC by inverting the condition code, then wrap
3738 // the existing SETCC in a NOT to restore the intended condition.
3739 if (NeedInvert)
Pete Cooper7fd1d722014-05-12 23:26:58 +00003740 Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
Daniel Sandersedc071b2013-11-21 13:24:49 +00003741
Eli Friedman5df72022009-05-28 03:56:57 +00003742 Results.push_back(Tmp1);
3743 break;
3744 }
3745
3746 // Otherwise, SETCC for the given comparison type must be completely
3747 // illegal; expand it into a SELECT_CC.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003748 EVT VT = Node->getValueType(0);
Tom Stellardd93ef7a2013-03-08 15:37:02 +00003749 int TrueValue;
Daniel Sanderscbd44c52014-07-10 10:18:12 +00003750 switch (TLI.getBooleanContents(Tmp1->getValueType(0))) {
Tom Stellardd93ef7a2013-03-08 15:37:02 +00003751 case TargetLowering::ZeroOrOneBooleanContent:
3752 case TargetLowering::UndefinedBooleanContent:
3753 TrueValue = 1;
3754 break;
3755 case TargetLowering::ZeroOrNegativeOneBooleanContent:
3756 TrueValue = -1;
3757 break;
3758 }
Eli Friedman5df72022009-05-28 03:56:57 +00003759 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003760 DAG.getConstant(TrueValue, dl, VT),
3761 DAG.getConstant(0, dl, VT),
Tom Stellardd93ef7a2013-03-08 15:37:02 +00003762 Tmp3);
Eli Friedman5df72022009-05-28 03:56:57 +00003763 Results.push_back(Tmp1);
3764 break;
3765 }
Eli Friedmane1dc1932009-05-28 20:40:34 +00003766 case ISD::SELECT_CC: {
3767 Tmp1 = Node->getOperand(0); // LHS
3768 Tmp2 = Node->getOperand(1); // RHS
3769 Tmp3 = Node->getOperand(2); // True
3770 Tmp4 = Node->getOperand(3); // False
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003771 EVT VT = Node->getValueType(0);
Eli Friedmane1dc1932009-05-28 20:40:34 +00003772 SDValue CC = Node->getOperand(4);
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003773 ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
Eli Friedmane1dc1932009-05-28 20:40:34 +00003774
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003775 if (TLI.isCondCodeLegal(CCOp, Tmp1.getSimpleValueType())) {
3776 // If the condition code is legal, then we need to expand this
3777 // node using SETCC and SELECT.
3778 EVT CmpVT = Tmp1.getValueType();
3779 assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
3780 "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
3781 "expanded.");
Mehdi Amini44ede332015-07-09 02:09:04 +00003782 EVT CCVT =
3783 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), CmpVT);
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003784 SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC);
3785 Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4));
3786 break;
3787 }
3788
3789 // SELECT_CC is legal, so the condition code must not be.
Tom Stellard5694d302013-09-28 02:50:43 +00003790 bool Legalized = false;
3791 // Try to legalize by inverting the condition. This is for targets that
3792 // might support an ordered version of a condition, but not the unordered
3793 // version (or vice versa).
Tom Stellard3ca1bfc2014-06-10 16:01:22 +00003794 ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp,
Tom Stellard5694d302013-09-28 02:50:43 +00003795 Tmp1.getValueType().isInteger());
3796 if (TLI.isCondCodeLegal(InvCC, Tmp1.getSimpleValueType())) {
3797 // Use the new condition code and swap true and false
3798 Legalized = true;
3799 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
Tom Stellard08690a12013-09-28 02:50:32 +00003800 } else {
Tom Stellard5694d302013-09-28 02:50:43 +00003801 // If The inverse is not legal, then try to swap the arguments using
3802 // the inverse condition code.
3803 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC);
3804 if (TLI.isCondCodeLegal(SwapInvCC, Tmp1.getSimpleValueType())) {
3805 // The swapped inverse condition is legal, so swap true and false,
3806 // lhs and rhs.
3807 Legalized = true;
3808 Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
3809 }
3810 }
3811
3812 if (!Legalized) {
3813 Legalized = LegalizeSetCCCondCode(
Daniel Sandersedc071b2013-11-21 13:24:49 +00003814 getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, NeedInvert,
3815 dl);
Tom Stellard5694d302013-09-28 02:50:43 +00003816
3817 assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
Daniel Sandersedc071b2013-11-21 13:24:49 +00003818
3819 // If we expanded the SETCC by inverting the condition code, then swap
3820 // the True/False operands to match.
3821 if (NeedInvert)
3822 std::swap(Tmp3, Tmp4);
3823
3824 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
3825 // condition code, create a new SELECT_CC node.
Tom Stellard5694d302013-09-28 02:50:43 +00003826 if (CC.getNode()) {
3827 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
3828 Tmp1, Tmp2, Tmp3, Tmp4, CC);
3829 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003830 Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
Tom Stellard5694d302013-09-28 02:50:43 +00003831 CC = DAG.getCondCode(ISD::SETNE);
Jack Carter5c0af482013-11-19 23:43:22 +00003832 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
3833 Tmp2, Tmp3, Tmp4, CC);
Tom Stellard5694d302013-09-28 02:50:43 +00003834 }
Tom Stellard08690a12013-09-28 02:50:32 +00003835 }
Eli Friedmane1dc1932009-05-28 20:40:34 +00003836 Results.push_back(Tmp1);
3837 break;
3838 }
3839 case ISD::BR_CC: {
3840 Tmp1 = Node->getOperand(0); // Chain
3841 Tmp2 = Node->getOperand(2); // LHS
3842 Tmp3 = Node->getOperand(3); // RHS
3843 Tmp4 = Node->getOperand(1); // CC
3844
Tom Stellard08690a12013-09-28 02:50:32 +00003845 bool Legalized = LegalizeSetCCCondCode(getSetCCResultType(
Daniel Sandersedc071b2013-11-21 13:24:49 +00003846 Tmp2.getValueType()), Tmp2, Tmp3, Tmp4, NeedInvert, dl);
Tom Stellard45015d92013-09-28 03:10:17 +00003847 (void)Legalized;
Tom Stellard08690a12013-09-28 02:50:32 +00003848 assert(Legalized && "Can't legalize BR_CC with legal condition!");
Eli Friedmane1dc1932009-05-28 20:40:34 +00003849
Daniel Sandersedc071b2013-11-21 13:24:49 +00003850 // If we expanded the SETCC by inverting the condition code, then wrap
3851 // the existing SETCC in a NOT to restore the intended condition.
3852 if (NeedInvert)
3853 Tmp4 = DAG.getNOT(dl, Tmp4, Tmp4->getValueType(0));
3854
3855 // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
Tom Stellard08690a12013-09-28 02:50:32 +00003856 // node.
3857 if (Tmp4.getNode()) {
3858 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
3859 Tmp4, Tmp2, Tmp3, Node->getOperand(4));
3860 } else {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003861 Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
Tom Stellard08690a12013-09-28 02:50:32 +00003862 Tmp4 = DAG.getCondCode(ISD::SETNE);
Jack Carter5c0af482013-11-19 23:43:22 +00003863 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
3864 Tmp2, Tmp3, Node->getOperand(4));
Tom Stellard08690a12013-09-28 02:50:32 +00003865 }
Eli Friedmane1dc1932009-05-28 20:40:34 +00003866 Results.push_back(Tmp1);
3867 break;
3868 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00003869 case ISD::BUILD_VECTOR:
3870 Results.push_back(ExpandBUILD_VECTOR(Node));
3871 break;
3872 case ISD::SRA:
3873 case ISD::SRL:
3874 case ISD::SHL: {
3875 // Scalarize vector SRA/SRL/SHL.
3876 EVT VT = Node->getValueType(0);
3877 assert(VT.isVector() && "Unable to legalize non-vector shift");
3878 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3879 unsigned NumElem = VT.getVectorNumElements();
3880
3881 SmallVector<SDValue, 8> Scalars;
3882 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
Mehdi Amini44ede332015-07-09 02:09:04 +00003883 SDValue Ex = DAG.getNode(
3884 ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(0),
3885 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3886 SDValue Sh = DAG.getNode(
3887 ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(1),
3888 DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
Dan Gohman198b7ff2011-11-03 21:49:52 +00003889 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3890 VT.getScalarType(), Ex, Sh));
3891 }
3892 SDValue Result =
Craig Topper48d114b2014-04-26 18:35:24 +00003893 DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), Scalars);
Eli Friedman13477152011-11-11 23:58:27 +00003894 ReplaceNode(SDValue(Node, 0), Result);
Dan Gohman198b7ff2011-11-03 21:49:52 +00003895 break;
3896 }
Eli Friedmana8f9a022009-05-27 02:16:40 +00003897 case ISD::GLOBAL_OFFSET_TABLE:
3898 case ISD::GlobalAddress:
3899 case ISD::GlobalTLSAddress:
3900 case ISD::ExternalSymbol:
3901 case ISD::ConstantPool:
3902 case ISD::JumpTable:
3903 case ISD::INTRINSIC_W_CHAIN:
3904 case ISD::INTRINSIC_WO_CHAIN:
3905 case ISD::INTRINSIC_VOID:
3906 // FIXME: Custom lowering for these operations shouldn't return null!
Eli Friedmana8f9a022009-05-27 02:16:40 +00003907 break;
Eli Friedman21d349b2009-05-27 01:25:56 +00003908 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00003909
3910 // Replace the original node with the legalized result.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +00003911 if (Results.empty())
3912 return false;
3913
3914 ReplaceNode(Node, Results.data());
3915 return true;
3916}
3917
3918void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
3919 SmallVector<SDValue, 8> Results;
3920 SDLoc dl(Node);
3921 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
3922 unsigned Opc = Node->getOpcode();
3923 switch (Opc) {
3924 case ISD::ATOMIC_FENCE: {
3925 // If the target didn't lower this, lower it to '__sync_synchronize()' call
3926 // FIXME: handle "fence singlethread" more efficiently.
3927 TargetLowering::ArgListTy Args;
3928
3929 TargetLowering::CallLoweringInfo CLI(DAG);
3930 CLI.setDebugLoc(dl)
3931 .setChain(Node->getOperand(0))
3932 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
3933 DAG.getExternalSymbol("__sync_synchronize",
3934 TLI.getPointerTy(DAG.getDataLayout())),
3935 std::move(Args), 0);
3936
3937 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
3938
3939 Results.push_back(CallResult.second);
3940 break;
3941 }
3942 // By default, atomic intrinsics are marked Legal and lowered. Targets
3943 // which don't support them directly, however, may want libcalls, in which
3944 // case they mark them Expand, and we get here.
3945 case ISD::ATOMIC_SWAP:
3946 case ISD::ATOMIC_LOAD_ADD:
3947 case ISD::ATOMIC_LOAD_SUB:
3948 case ISD::ATOMIC_LOAD_AND:
3949 case ISD::ATOMIC_LOAD_OR:
3950 case ISD::ATOMIC_LOAD_XOR:
3951 case ISD::ATOMIC_LOAD_NAND:
3952 case ISD::ATOMIC_LOAD_MIN:
3953 case ISD::ATOMIC_LOAD_MAX:
3954 case ISD::ATOMIC_LOAD_UMIN:
3955 case ISD::ATOMIC_LOAD_UMAX:
3956 case ISD::ATOMIC_CMP_SWAP: {
3957 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
3958 RTLIB::Libcall LC = RTLIB::getATOMIC(Opc, VT);
3959 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
3960
3961 std::pair<SDValue, SDValue> Tmp = ExpandChainLibCall(LC, Node, false);
3962 Results.push_back(Tmp.first);
3963 Results.push_back(Tmp.second);
3964 break;
3965 }
3966 case ISD::TRAP: {
3967 // If this operation is not supported, lower it to 'abort()' call
3968 TargetLowering::ArgListTy Args;
3969 TargetLowering::CallLoweringInfo CLI(DAG);
3970 CLI.setDebugLoc(dl)
3971 .setChain(Node->getOperand(0))
3972 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
3973 DAG.getExternalSymbol("abort",
3974 TLI.getPointerTy(DAG.getDataLayout())),
3975 std::move(Args), 0);
3976 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
3977
3978 Results.push_back(CallResult.second);
3979 break;
3980 }
3981 case ISD::FMINNUM:
3982 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
3983 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
3984 RTLIB::FMIN_PPCF128));
3985 break;
3986 case ISD::FMAXNUM:
3987 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
3988 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
3989 RTLIB::FMAX_PPCF128));
3990 break;
3991 case ISD::FSQRT:
3992 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3993 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
3994 RTLIB::SQRT_PPCF128));
3995 break;
3996 case ISD::FSIN:
3997 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3998 RTLIB::SIN_F80, RTLIB::SIN_F128,
3999 RTLIB::SIN_PPCF128));
4000 break;
4001 case ISD::FCOS:
4002 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
4003 RTLIB::COS_F80, RTLIB::COS_F128,
4004 RTLIB::COS_PPCF128));
4005 break;
4006 case ISD::FSINCOS:
4007 // Expand into sincos libcall.
4008 ExpandSinCosLibCall(Node, Results);
4009 break;
4010 case ISD::FLOG:
4011 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
4012 RTLIB::LOG_F80, RTLIB::LOG_F128,
4013 RTLIB::LOG_PPCF128));
4014 break;
4015 case ISD::FLOG2:
4016 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
4017 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
4018 RTLIB::LOG2_PPCF128));
4019 break;
4020 case ISD::FLOG10:
4021 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
4022 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
4023 RTLIB::LOG10_PPCF128));
4024 break;
4025 case ISD::FEXP:
4026 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
4027 RTLIB::EXP_F80, RTLIB::EXP_F128,
4028 RTLIB::EXP_PPCF128));
4029 break;
4030 case ISD::FEXP2:
4031 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
4032 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
4033 RTLIB::EXP2_PPCF128));
4034 break;
4035 case ISD::FTRUNC:
4036 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
4037 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
4038 RTLIB::TRUNC_PPCF128));
4039 break;
4040 case ISD::FFLOOR:
4041 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
4042 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
4043 RTLIB::FLOOR_PPCF128));
4044 break;
4045 case ISD::FCEIL:
4046 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
4047 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
4048 RTLIB::CEIL_PPCF128));
4049 break;
4050 case ISD::FRINT:
4051 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
4052 RTLIB::RINT_F80, RTLIB::RINT_F128,
4053 RTLIB::RINT_PPCF128));
4054 break;
4055 case ISD::FNEARBYINT:
4056 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
4057 RTLIB::NEARBYINT_F64,
4058 RTLIB::NEARBYINT_F80,
4059 RTLIB::NEARBYINT_F128,
4060 RTLIB::NEARBYINT_PPCF128));
4061 break;
4062 case ISD::FROUND:
4063 Results.push_back(ExpandFPLibCall(Node, RTLIB::ROUND_F32,
4064 RTLIB::ROUND_F64,
4065 RTLIB::ROUND_F80,
4066 RTLIB::ROUND_F128,
4067 RTLIB::ROUND_PPCF128));
4068 break;
4069 case ISD::FPOWI:
4070 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
4071 RTLIB::POWI_F80, RTLIB::POWI_F128,
4072 RTLIB::POWI_PPCF128));
4073 break;
4074 case ISD::FPOW:
4075 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
4076 RTLIB::POW_F80, RTLIB::POW_F128,
4077 RTLIB::POW_PPCF128));
4078 break;
4079 case ISD::FDIV:
4080 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
4081 RTLIB::DIV_F80, RTLIB::DIV_F128,
4082 RTLIB::DIV_PPCF128));
4083 break;
4084 case ISD::FREM:
4085 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
4086 RTLIB::REM_F80, RTLIB::REM_F128,
4087 RTLIB::REM_PPCF128));
4088 break;
4089 case ISD::FMA:
4090 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
4091 RTLIB::FMA_F80, RTLIB::FMA_F128,
4092 RTLIB::FMA_PPCF128));
4093 break;
4094 case ISD::FADD:
4095 Results.push_back(ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
4096 RTLIB::ADD_F80, RTLIB::ADD_F128,
4097 RTLIB::ADD_PPCF128));
4098 break;
4099 case ISD::FMUL:
4100 Results.push_back(ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
4101 RTLIB::MUL_F80, RTLIB::MUL_F128,
4102 RTLIB::MUL_PPCF128));
4103 break;
4104 case ISD::FP16_TO_FP:
4105 if (Node->getValueType(0) == MVT::f32) {
4106 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
4107 }
4108 break;
4109 case ISD::FP_TO_FP16: {
4110 RTLIB::Libcall LC =
4111 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
4112 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
4113 Results.push_back(ExpandLibCall(LC, Node, false));
4114 break;
4115 }
4116 case ISD::FSUB:
4117 Results.push_back(ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
4118 RTLIB::SUB_F80, RTLIB::SUB_F128,
4119 RTLIB::SUB_PPCF128));
4120 break;
4121 case ISD::SREM:
4122 Results.push_back(ExpandIntLibCall(Node, true,
4123 RTLIB::SREM_I8,
4124 RTLIB::SREM_I16, RTLIB::SREM_I32,
4125 RTLIB::SREM_I64, RTLIB::SREM_I128));
4126 break;
4127 case ISD::UREM:
4128 Results.push_back(ExpandIntLibCall(Node, false,
4129 RTLIB::UREM_I8,
4130 RTLIB::UREM_I16, RTLIB::UREM_I32,
4131 RTLIB::UREM_I64, RTLIB::UREM_I128));
4132 break;
4133 case ISD::SDIV:
4134 Results.push_back(ExpandIntLibCall(Node, true,
4135 RTLIB::SDIV_I8,
4136 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
4137 RTLIB::SDIV_I64, RTLIB::SDIV_I128));
4138 break;
4139 case ISD::UDIV:
4140 Results.push_back(ExpandIntLibCall(Node, false,
4141 RTLIB::UDIV_I8,
4142 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
4143 RTLIB::UDIV_I64, RTLIB::UDIV_I128));
4144 break;
4145 case ISD::SDIVREM:
4146 case ISD::UDIVREM:
4147 // Expand into divrem libcall
4148 ExpandDivRemLibCall(Node, Results);
4149 break;
4150 case ISD::MUL:
4151 Results.push_back(ExpandIntLibCall(Node, false,
4152 RTLIB::MUL_I8,
4153 RTLIB::MUL_I16, RTLIB::MUL_I32,
4154 RTLIB::MUL_I64, RTLIB::MUL_I128));
4155 break;
4156 }
4157
4158 // Replace the original node with the legalized result.
Eli Friedman13477152011-11-11 23:58:27 +00004159 if (!Results.empty())
4160 ReplaceNode(Node, Results.data());
Eli Friedman21d349b2009-05-27 01:25:56 +00004161}
Dan Gohman198b7ff2011-11-03 21:49:52 +00004162
Matt Arsenault0b7958a2015-11-10 18:48:04 +00004163// Determine the vector type to use in place of an original scalar element when
4164// promoting equally sized vectors.
4165static MVT getPromotedVectorElementType(const TargetLowering &TLI,
4166 MVT EltVT, MVT NewEltVT) {
4167 unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
4168 MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
4169 assert(TLI.isTypeLegal(MidVT) && "unexpected");
4170 return MidVT;
4171}
4172
Dan Gohman198b7ff2011-11-03 21:49:52 +00004173void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
4174 SmallVector<SDValue, 8> Results;
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +00004175 MVT OVT = Node->getSimpleValueType(0);
Eli Friedman21d349b2009-05-27 01:25:56 +00004176 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedman97f3f962009-07-17 05:16:04 +00004177 Node->getOpcode() == ISD::SINT_TO_FP ||
Matt Arsenault0b7958a2015-11-10 18:48:04 +00004178 Node->getOpcode() == ISD::SETCC ||
Matt Arsenaulta46aa642015-11-10 18:48:08 +00004179 Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
4180 Node->getOpcode() == ISD::INSERT_VECTOR_ELT) {
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +00004181 OVT = Node->getOperand(0).getSimpleValueType();
Bill Wendlingef408db2009-12-23 00:28:23 +00004182 }
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004183 if (Node->getOpcode() == ISD::BR_CC)
4184 OVT = Node->getOperand(2).getSimpleValueType();
Patrik Hagglundfd41b5b2012-12-19 11:21:04 +00004185 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004186 SDLoc dl(Node);
Eli Friedman3b251702009-05-27 07:58:35 +00004187 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman21d349b2009-05-27 01:25:56 +00004188 switch (Node->getOpcode()) {
4189 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004190 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman21d349b2009-05-27 01:25:56 +00004191 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004192 case ISD::CTLZ_ZERO_UNDEF:
Eli Friedman21d349b2009-05-27 01:25:56 +00004193 case ISD::CTPOP:
4194 // Zero extend the argument.
4195 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004196 // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
4197 // already the correct result.
Jakob Stoklund Olesen6b9f63c2009-07-12 17:43:20 +00004198 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman21d349b2009-05-27 01:25:56 +00004199 if (Node->getOpcode() == ISD::CTTZ) {
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004200 // FIXME: This should set a bit in the zero extended value instead.
Matt Arsenault758659232013-05-18 00:21:46 +00004201 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004202 Tmp1, DAG.getConstant(NVT.getSizeInBits(), dl, NVT),
Eli Friedman21d349b2009-05-27 01:25:56 +00004203 ISD::SETEQ);
Matt Arsenaultd2f03322013-06-14 22:04:37 +00004204 Tmp1 = DAG.getSelect(dl, NVT, Tmp2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004205 DAG.getConstant(OVT.getSizeInBits(), dl, NVT), Tmp1);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004206 } else if (Node->getOpcode() == ISD::CTLZ ||
4207 Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
Eli Friedman21d349b2009-05-27 01:25:56 +00004208 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4209 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
4210 DAG.getConstant(NVT.getSizeInBits() -
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004211 OVT.getSizeInBits(), dl, NVT));
Eli Friedman21d349b2009-05-27 01:25:56 +00004212 }
Bill Wendlingef408db2009-12-23 00:28:23 +00004213 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman21d349b2009-05-27 01:25:56 +00004214 break;
4215 case ISD::BSWAP: {
4216 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling70794592009-12-22 22:53:39 +00004217 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendlingef408db2009-12-23 00:28:23 +00004218 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
Mehdi Amini9639d652015-07-09 02:09:20 +00004219 Tmp1 = DAG.getNode(
4220 ISD::SRL, dl, NVT, Tmp1,
4221 DAG.getConstant(DiffBits, dl,
4222 TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
Bill Wendlingef408db2009-12-23 00:28:23 +00004223 Results.push_back(Tmp1);
Eli Friedman21d349b2009-05-27 01:25:56 +00004224 break;
4225 }
4226 case ISD::FP_TO_UINT:
4227 case ISD::FP_TO_SINT:
4228 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
4229 Node->getOpcode() == ISD::FP_TO_SINT, dl);
4230 Results.push_back(Tmp1);
4231 break;
4232 case ISD::UINT_TO_FP:
4233 case ISD::SINT_TO_FP:
4234 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
4235 Node->getOpcode() == ISD::SINT_TO_FP, dl);
4236 Results.push_back(Tmp1);
4237 break;
Hal Finkel71c2ba32012-03-24 03:53:52 +00004238 case ISD::VAARG: {
4239 SDValue Chain = Node->getOperand(0); // Get the chain.
4240 SDValue Ptr = Node->getOperand(1); // Get the pointer.
4241
4242 unsigned TruncOp;
4243 if (OVT.isVector()) {
4244 TruncOp = ISD::BITCAST;
4245 } else {
4246 assert(OVT.isInteger()
4247 && "VAARG promotion is supported only for vectors or integer types");
4248 TruncOp = ISD::TRUNCATE;
4249 }
4250
4251 // Perform the larger operation, then convert back
4252 Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
4253 Node->getConstantOperandVal(3));
4254 Chain = Tmp1.getValue(1);
4255
4256 Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
4257
4258 // Modified the chain result - switch anything that used the old chain to
4259 // use the new one.
4260 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
4261 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
Chandler Carruth411fb402014-07-26 05:49:40 +00004262 if (UpdatedNodes) {
4263 UpdatedNodes->insert(Tmp2.getNode());
4264 UpdatedNodes->insert(Chain.getNode());
4265 }
Hal Finkel71c2ba32012-03-24 03:53:52 +00004266 ReplacedNode(Node);
4267 break;
4268 }
Eli Friedmand6f28342009-05-27 03:33:44 +00004269 case ISD::AND:
4270 case ISD::OR:
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004271 case ISD::XOR: {
4272 unsigned ExtOp, TruncOp;
4273 if (OVT.isVector()) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004274 ExtOp = ISD::BITCAST;
4275 TruncOp = ISD::BITCAST;
Chris Lattnercd927182010-04-07 23:47:51 +00004276 } else {
4277 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004278 ExtOp = ISD::ANY_EXTEND;
4279 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004280 }
4281 // Promote each of the values to the new type.
4282 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
4283 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4284 // Perform the larger operation, then convert back
Bill Wendlingef408db2009-12-23 00:28:23 +00004285 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
4286 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmand6f28342009-05-27 03:33:44 +00004287 break;
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004288 }
4289 case ISD::SELECT: {
Eli Friedman3b251702009-05-27 07:58:35 +00004290 unsigned ExtOp, TruncOp;
Tom Stellardc9a67a22014-03-24 16:07:28 +00004291 if (Node->getValueType(0).isVector() ||
4292 Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
Wesley Peck527da1b2010-11-23 03:31:01 +00004293 ExtOp = ISD::BITCAST;
4294 TruncOp = ISD::BITCAST;
Eli Friedman2892d822009-05-27 12:20:41 +00004295 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman3b251702009-05-27 07:58:35 +00004296 ExtOp = ISD::ANY_EXTEND;
4297 TruncOp = ISD::TRUNCATE;
4298 } else {
4299 ExtOp = ISD::FP_EXTEND;
4300 TruncOp = ISD::FP_ROUND;
4301 }
4302 Tmp1 = Node->getOperand(0);
4303 // Promote each of the values to the new type.
4304 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4305 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
4306 // Perform the larger operation, then round down.
Matt Arsenaultd2f03322013-06-14 22:04:37 +00004307 Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
Eli Friedman3b251702009-05-27 07:58:35 +00004308 if (TruncOp != ISD::FP_ROUND)
Bill Wendlingef408db2009-12-23 00:28:23 +00004309 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman3b251702009-05-27 07:58:35 +00004310 else
Bill Wendlingef408db2009-12-23 00:28:23 +00004311 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004312 DAG.getIntPtrConstant(0, dl));
Bill Wendlingef408db2009-12-23 00:28:23 +00004313 Results.push_back(Tmp1);
Eli Friedman3b251702009-05-27 07:58:35 +00004314 break;
Jakob Stoklund Olesened0e1a02009-07-12 18:10:18 +00004315 }
Eli Friedman3b251702009-05-27 07:58:35 +00004316 case ISD::VECTOR_SHUFFLE: {
Benjamin Kramer339ced42012-01-15 13:16:05 +00004317 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
Eli Friedman3b251702009-05-27 07:58:35 +00004318
4319 // Cast the two input vectors.
Wesley Peck527da1b2010-11-23 03:31:01 +00004320 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
4321 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman3b251702009-05-27 07:58:35 +00004322
4323 // Convert the shuffle mask to the right # elements.
Bill Wendlingef408db2009-12-23 00:28:23 +00004324 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peck527da1b2010-11-23 03:31:01 +00004325 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman3b251702009-05-27 07:58:35 +00004326 Results.push_back(Tmp1);
4327 break;
4328 }
Eli Friedman5df72022009-05-28 03:56:57 +00004329 case ISD::SETCC: {
Jakob Stoklund Olesen1ae07362009-07-24 18:22:59 +00004330 unsigned ExtOp = ISD::FP_EXTEND;
4331 if (NVT.isInteger()) {
4332 ISD::CondCode CCCode =
4333 cast<CondCodeSDNode>(Node->getOperand(2))->get();
4334 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedman5df72022009-05-28 03:56:57 +00004335 }
Jakob Stoklund Olesen1ae07362009-07-24 18:22:59 +00004336 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
4337 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedman5df72022009-05-28 03:56:57 +00004338 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
4339 Tmp1, Tmp2, Node->getOperand(2)));
4340 break;
4341 }
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004342 case ISD::BR_CC: {
4343 unsigned ExtOp = ISD::FP_EXTEND;
4344 if (NVT.isInteger()) {
4345 ISD::CondCode CCCode =
4346 cast<CondCodeSDNode>(Node->getOperand(1))->get();
4347 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4348 }
4349 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
4350 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
4351 Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
4352 Node->getOperand(0), Node->getOperand(1),
4353 Tmp1, Tmp2, Node->getOperand(4)));
4354 break;
4355 }
Oliver Stannardf5469be2014-08-18 14:22:39 +00004356 case ISD::FADD:
4357 case ISD::FSUB:
4358 case ISD::FMUL:
Pete Coopere69be6d2012-03-19 23:38:12 +00004359 case ISD::FDIV:
Pete Cooper8a3dc0e2012-04-04 19:36:31 +00004360 case ISD::FREM:
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004361 case ISD::FMINNUM:
4362 case ISD::FMAXNUM:
Pete Cooper99415fe2012-01-12 21:46:18 +00004363 case ISD::FPOW: {
4364 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4365 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
Sanjay Patela2607012015-09-16 16:31:21 +00004366 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
4367 Node->getFlags());
Pete Cooper99415fe2012-01-12 21:46:18 +00004368 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004369 Tmp3, DAG.getIntPtrConstant(0, dl)));
Pete Cooper99415fe2012-01-12 21:46:18 +00004370 break;
4371 }
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004372 case ISD::FMA: {
4373 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4374 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
4375 Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
4376 Results.push_back(
4377 DAG.getNode(ISD::FP_ROUND, dl, OVT,
4378 DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004379 DAG.getIntPtrConstant(0, dl)));
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004380 break;
4381 }
Ahmed Bougacha40ded502015-08-13 01:09:43 +00004382 case ISD::FCOPYSIGN:
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004383 case ISD::FPOWI: {
4384 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4385 Tmp2 = Node->getOperand(1);
4386 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Ahmed Bougachaa1966612015-08-13 01:32:30 +00004387
4388 // fcopysign doesn't change anything but the sign bit, so
4389 // (fp_round (fcopysign (fpext a), b))
4390 // is as precise as
4391 // (fp_round (fpext a))
4392 // which is a no-op. Mark it as a TRUNCating FP_ROUND.
4393 const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004394 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
Ahmed Bougachaa1966612015-08-13 01:32:30 +00004395 Tmp3, DAG.getIntPtrConstant(isTrunc, dl)));
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004396 break;
4397 }
4398 case ISD::FFLOOR:
4399 case ISD::FCEIL:
4400 case ISD::FRINT:
4401 case ISD::FNEARBYINT:
4402 case ISD::FROUND:
4403 case ISD::FTRUNC:
4404 case ISD::FNEG:
4405 case ISD::FSQRT:
4406 case ISD::FSIN:
4407 case ISD::FCOS:
Pete Cooper99415fe2012-01-12 21:46:18 +00004408 case ISD::FLOG:
Ahmed Bougacha1ffe7c72015-04-10 00:08:48 +00004409 case ISD::FLOG2:
4410 case ISD::FLOG10:
4411 case ISD::FABS:
4412 case ISD::FEXP:
4413 case ISD::FEXP2: {
Pete Cooper99415fe2012-01-12 21:46:18 +00004414 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4415 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
4416 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004417 Tmp2, DAG.getIntPtrConstant(0, dl)));
Pete Cooper99415fe2012-01-12 21:46:18 +00004418 break;
4419 }
Matt Arsenault29f96632015-10-21 21:10:10 +00004420 case ISD::BUILD_VECTOR: {
4421 MVT EltVT = OVT.getVectorElementType();
4422 MVT NewEltVT = NVT.getVectorElementType();
4423
4424 // Handle bitcasts to a different vector type with the same total bit size
4425 //
4426 // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
4427 // =>
4428 // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
4429
4430 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4431 "Invalid promote type for build_vector");
4432 assert(NewEltVT.bitsLT(EltVT) && "not handled");
4433
Matt Arsenault0b7958a2015-11-10 18:48:04 +00004434 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
Matt Arsenault29f96632015-10-21 21:10:10 +00004435
4436 SmallVector<SDValue, 8> NewOps;
4437 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
4438 SDValue Op = Node->getOperand(I);
4439 NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
4440 }
4441
4442 SDLoc SL(Node);
4443 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps);
4444 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
4445 Results.push_back(CvtVec);
4446 break;
4447 }
Matt Arsenault0b7958a2015-11-10 18:48:04 +00004448 case ISD::EXTRACT_VECTOR_ELT: {
4449 MVT EltVT = OVT.getVectorElementType();
4450 MVT NewEltVT = NVT.getVectorElementType();
4451
4452 // Handle bitcasts to a different vector type with the same total bit size.
4453 //
4454 // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
4455 // =>
4456 // v4i32:castx = bitcast x:v2i64
4457 //
4458 // i64 = bitcast
4459 // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
4460 // (i32 (extract_vector_elt castx, (2 * y + 1)))
4461 //
4462
4463 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4464 "Invalid promote type for extract_vector_elt");
4465 assert(NewEltVT.bitsLT(EltVT) && "not handled");
4466
4467 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4468 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
4469
4470 SDValue Idx = Node->getOperand(1);
4471 EVT IdxVT = Idx.getValueType();
4472 SDLoc SL(Node);
4473 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
4474 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
4475
4476 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
4477
4478 SmallVector<SDValue, 8> NewOps;
4479 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
4480 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
4481 SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
4482
4483 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
4484 CastVec, TmpIdx);
4485 NewOps.push_back(Elt);
4486 }
4487
4488 SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, SL, MidVT, NewOps);
4489
4490 Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
4491 break;
4492 }
Matt Arsenaulta46aa642015-11-10 18:48:08 +00004493 case ISD::INSERT_VECTOR_ELT: {
4494 MVT EltVT = OVT.getVectorElementType();
4495 MVT NewEltVT = NVT.getVectorElementType();
4496
4497 // Handle bitcasts to a different vector type with the same total bit size
4498 //
4499 // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
4500 // =>
4501 // v4i32:castx = bitcast x:v2i64
4502 // v2i32:casty = bitcast y:i64
4503 //
4504 // v2i64 = bitcast
4505 // (v4i32 insert_vector_elt
4506 // (v4i32 insert_vector_elt v4i32:castx,
4507 // (extract_vector_elt casty, 0), 2 * z),
4508 // (extract_vector_elt casty, 1), (2 * z + 1))
4509
4510 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4511 "Invalid promote type for insert_vector_elt");
4512 assert(NewEltVT.bitsLT(EltVT) && "not handled");
4513
4514 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4515 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
4516
4517 SDValue Val = Node->getOperand(1);
4518 SDValue Idx = Node->getOperand(2);
4519 EVT IdxVT = Idx.getValueType();
4520 SDLoc SL(Node);
4521
4522 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
4523 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
4524
4525 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
4526 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
4527
4528 SDValue NewVec = CastVec;
4529 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
4530 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
4531 SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
4532
4533 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
4534 CastVal, IdxOffset);
4535
4536 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
4537 NewVec, Elt, InEltIdx);
4538 }
4539
4540 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
4541 break;
4542 }
Matt Arsenaultaa118e22015-11-10 18:48:11 +00004543 case ISD::SCALAR_TO_VECTOR: {
4544 MVT EltVT = OVT.getVectorElementType();
4545 MVT NewEltVT = NVT.getVectorElementType();
4546
4547 // Handle bitcasts to different vector type with the smae total bit size.
4548 //
4549 // e.g. v2i64 = scalar_to_vector x:i64
4550 // =>
4551 // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
4552 //
4553
4554 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4555 SDValue Val = Node->getOperand(0);
4556 SDLoc SL(Node);
4557
4558 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
4559 SDValue Undef = DAG.getUNDEF(MidVT);
4560
4561 SmallVector<SDValue, 8> NewElts;
4562 NewElts.push_back(CastVal);
4563 for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
4564 NewElts.push_back(Undef);
4565
4566 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
4567 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
4568 Results.push_back(CvtVec);
4569 break;
4570 }
Eli Friedman21d349b2009-05-27 01:25:56 +00004571 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00004572
4573 // Replace the original node with the legalized result.
Eli Friedman13477152011-11-11 23:58:27 +00004574 if (!Results.empty())
4575 ReplaceNode(Node, Results.data());
Eli Friedman21d349b2009-05-27 01:25:56 +00004576}
4577
Sanjay Pateleb4a4d52014-11-21 18:58:38 +00004578/// This is the entry point for the file.
Dan Gohmand282f462011-05-16 22:19:54 +00004579void SelectionDAG::Legalize() {
Chandler Carruth411fb402014-07-26 05:49:40 +00004580 AssignTopologicalOrder();
4581
Chandler Carruth411fb402014-07-26 05:49:40 +00004582 SmallPtrSet<SDNode *, 16> LegalizedNodes;
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004583 SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
Chandler Carruth411fb402014-07-26 05:49:40 +00004584
4585 // Visit all the nodes. We start in topological order, so that we see
4586 // nodes with their original operands intact. Legalization can produce
4587 // new nodes which may themselves need to be legalized. Iterate until all
4588 // nodes have been legalized.
4589 for (;;) {
4590 bool AnyLegalized = false;
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004591 for (auto NI = allnodes_end(); NI != allnodes_begin();) {
4592 --NI;
Chandler Carruth411fb402014-07-26 05:49:40 +00004593
Duncan P. N. Exon Smithe400a7d2015-10-13 19:47:46 +00004594 SDNode *N = &*NI;
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004595 if (N->use_empty() && N != getRoot().getNode()) {
4596 ++NI;
4597 DeleteNode(N);
4598 continue;
4599 }
4600
David Blaikie70573dc2014-11-19 07:49:26 +00004601 if (LegalizedNodes.insert(N).second) {
Chandler Carruth411fb402014-07-26 05:49:40 +00004602 AnyLegalized = true;
4603 Legalizer.LegalizeOp(N);
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004604
4605 if (N->use_empty() && N != getRoot().getNode()) {
4606 ++NI;
4607 DeleteNode(N);
4608 }
Chandler Carruth411fb402014-07-26 05:49:40 +00004609 }
4610 }
4611 if (!AnyLegalized)
4612 break;
4613
4614 }
4615
4616 // Remove dead nodes now.
4617 RemoveDeadNodes();
4618}
4619
4620bool SelectionDAG::LegalizeOp(SDNode *N,
4621 SmallSetVector<SDNode *, 16> &UpdatedNodes) {
Chandler Carruth411fb402014-07-26 05:49:40 +00004622 SmallPtrSet<SDNode *, 16> LegalizedNodes;
Chandler Carruth1f52b3d2014-08-01 19:49:59 +00004623 SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
Chandler Carruth411fb402014-07-26 05:49:40 +00004624
4625 // Directly insert the node in question, and legalize it. This will recurse
4626 // as needed through operands.
4627 LegalizedNodes.insert(N);
4628 Legalizer.LegalizeOp(N);
4629
4630 return LegalizedNodes.count(N);
Chris Lattnerdc750592005-01-07 07:47:09 +00004631}