1*9880d681SAndroid Build Coastguard Worker //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the SelectionDAG::Legalize method.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Triple.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineJumpTableInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/SelectionDAG.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/SelectionDAGNodes.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallingConv.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetFrameLowering.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLowering.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
38*9880d681SAndroid Build Coastguard Worker using namespace llvm;
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "legalizedag"
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker namespace {
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker struct FloatSignAsInt;
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
47*9880d681SAndroid Build Coastguard Worker /// This takes an arbitrary SelectionDAG as input and
48*9880d681SAndroid Build Coastguard Worker /// hacks on it until the target machine can handle it. This involves
49*9880d681SAndroid Build Coastguard Worker /// eliminating value sizes the machine cannot handle (promoting small sizes to
50*9880d681SAndroid Build Coastguard Worker /// large sizes or splitting up large values into small values) as well as
51*9880d681SAndroid Build Coastguard Worker /// eliminating operations the machine cannot handle.
52*9880d681SAndroid Build Coastguard Worker ///
53*9880d681SAndroid Build Coastguard Worker /// This code also does a small amount of optimization and recognition of idioms
54*9880d681SAndroid Build Coastguard Worker /// as part of its processing. For example, if a target does not support a
55*9880d681SAndroid Build Coastguard Worker /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
56*9880d681SAndroid Build Coastguard Worker /// will attempt merge setcc and brc instructions into brcc's.
57*9880d681SAndroid Build Coastguard Worker ///
58*9880d681SAndroid Build Coastguard Worker class SelectionDAGLegalize {
59*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM;
60*9880d681SAndroid Build Coastguard Worker const TargetLowering &TLI;
61*9880d681SAndroid Build Coastguard Worker SelectionDAG &DAG;
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker /// \brief The set of nodes which have already been legalized. We hold a
64*9880d681SAndroid Build Coastguard Worker /// reference to it in order to update as necessary on node deletion.
65*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<SDNode *> &LegalizedNodes;
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker /// \brief A set of all the nodes updated during legalization.
68*9880d681SAndroid Build Coastguard Worker SmallSetVector<SDNode *, 16> *UpdatedNodes;
69*9880d681SAndroid Build Coastguard Worker
getSetCCResultType(EVT VT) const70*9880d681SAndroid Build Coastguard Worker EVT getSetCCResultType(EVT VT) const {
71*9880d681SAndroid Build Coastguard Worker return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
72*9880d681SAndroid Build Coastguard Worker }
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker // Libcall insertion helpers.
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker public:
SelectionDAGLegalize(SelectionDAG & DAG,SmallPtrSetImpl<SDNode * > & LegalizedNodes,SmallSetVector<SDNode *,16> * UpdatedNodes=nullptr)77*9880d681SAndroid Build Coastguard Worker SelectionDAGLegalize(SelectionDAG &DAG,
78*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<SDNode *> &LegalizedNodes,
79*9880d681SAndroid Build Coastguard Worker SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
80*9880d681SAndroid Build Coastguard Worker : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
81*9880d681SAndroid Build Coastguard Worker LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker /// \brief Legalizes the given operation.
84*9880d681SAndroid Build Coastguard Worker void LegalizeOp(SDNode *Node);
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker private:
87*9880d681SAndroid Build Coastguard Worker SDValue OptimizeFloatStore(StoreSDNode *ST);
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker void LegalizeLoadOps(SDNode *Node);
90*9880d681SAndroid Build Coastguard Worker void LegalizeStoreOps(SDNode *Node);
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker /// Some targets cannot handle a variable
93*9880d681SAndroid Build Coastguard Worker /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
94*9880d681SAndroid Build Coastguard Worker /// is necessary to spill the vector being inserted into to memory, perform
95*9880d681SAndroid Build Coastguard Worker /// the insert there, and then read the result back.
96*9880d681SAndroid Build Coastguard Worker SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
97*9880d681SAndroid Build Coastguard Worker const SDLoc &dl);
98*9880d681SAndroid Build Coastguard Worker SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx,
99*9880d681SAndroid Build Coastguard Worker const SDLoc &dl);
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker /// Return a vector shuffle operation which
102*9880d681SAndroid Build Coastguard Worker /// performs the same shuffe in terms of order or result bytes, but on a type
103*9880d681SAndroid Build Coastguard Worker /// whose vector element type is narrower than the original shuffle type.
104*9880d681SAndroid Build Coastguard Worker /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
105*9880d681SAndroid Build Coastguard Worker SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl,
106*9880d681SAndroid Build Coastguard Worker SDValue N1, SDValue N2,
107*9880d681SAndroid Build Coastguard Worker ArrayRef<int> Mask) const;
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker bool LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
110*9880d681SAndroid Build Coastguard Worker bool &NeedInvert, const SDLoc &dl);
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
113*9880d681SAndroid Build Coastguard Worker SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
114*9880d681SAndroid Build Coastguard Worker unsigned NumOps, bool isSigned, const SDLoc &dl);
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
117*9880d681SAndroid Build Coastguard Worker SDNode *Node, bool isSigned);
118*9880d681SAndroid Build Coastguard Worker SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
119*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
120*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_F128,
121*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_PPCF128);
122*9880d681SAndroid Build Coastguard Worker SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
123*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I8,
124*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I16,
125*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I32,
126*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I64,
127*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I128);
128*9880d681SAndroid Build Coastguard Worker void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
129*9880d681SAndroid Build Coastguard Worker void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
132*9880d681SAndroid Build Coastguard Worker const SDLoc &dl);
133*9880d681SAndroid Build Coastguard Worker SDValue ExpandBUILD_VECTOR(SDNode *Node);
134*9880d681SAndroid Build Coastguard Worker SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
135*9880d681SAndroid Build Coastguard Worker void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
136*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<SDValue> &Results);
137*9880d681SAndroid Build Coastguard Worker void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL,
138*9880d681SAndroid Build Coastguard Worker SDValue Value) const;
139*9880d681SAndroid Build Coastguard Worker SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL,
140*9880d681SAndroid Build Coastguard Worker SDValue NewIntValue) const;
141*9880d681SAndroid Build Coastguard Worker SDValue ExpandFCOPYSIGN(SDNode *Node) const;
142*9880d681SAndroid Build Coastguard Worker SDValue ExpandFABS(SDNode *Node) const;
143*9880d681SAndroid Build Coastguard Worker SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
144*9880d681SAndroid Build Coastguard Worker const SDLoc &dl);
145*9880d681SAndroid Build Coastguard Worker SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
146*9880d681SAndroid Build Coastguard Worker const SDLoc &dl);
147*9880d681SAndroid Build Coastguard Worker SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
148*9880d681SAndroid Build Coastguard Worker const SDLoc &dl);
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker SDValue ExpandBITREVERSE(SDValue Op, const SDLoc &dl);
151*9880d681SAndroid Build Coastguard Worker SDValue ExpandBSWAP(SDValue Op, const SDLoc &dl);
152*9880d681SAndroid Build Coastguard Worker SDValue ExpandBitCount(unsigned Opc, SDValue Op, const SDLoc &dl);
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard Worker SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
155*9880d681SAndroid Build Coastguard Worker SDValue ExpandInsertToVectorThroughStack(SDValue Op);
156*9880d681SAndroid Build Coastguard Worker SDValue ExpandVectorBuildThroughStack(SDNode* Node);
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
159*9880d681SAndroid Build Coastguard Worker SDValue ExpandConstant(ConstantSDNode *CP);
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
162*9880d681SAndroid Build Coastguard Worker bool ExpandNode(SDNode *Node);
163*9880d681SAndroid Build Coastguard Worker void ConvertNodeToLibcall(SDNode *Node);
164*9880d681SAndroid Build Coastguard Worker void PromoteNode(SDNode *Node);
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker public:
167*9880d681SAndroid Build Coastguard Worker // Node replacement helpers
ReplacedNode(SDNode * N)168*9880d681SAndroid Build Coastguard Worker void ReplacedNode(SDNode *N) {
169*9880d681SAndroid Build Coastguard Worker LegalizedNodes.erase(N);
170*9880d681SAndroid Build Coastguard Worker if (UpdatedNodes)
171*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(N);
172*9880d681SAndroid Build Coastguard Worker }
ReplaceNode(SDNode * Old,SDNode * New)173*9880d681SAndroid Build Coastguard Worker void ReplaceNode(SDNode *Old, SDNode *New) {
174*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
175*9880d681SAndroid Build Coastguard Worker dbgs() << " with: "; New->dump(&DAG));
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker assert(Old->getNumValues() == New->getNumValues() &&
178*9880d681SAndroid Build Coastguard Worker "Replacing one node with another that produces a different number "
179*9880d681SAndroid Build Coastguard Worker "of values!");
180*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesWith(Old, New);
181*9880d681SAndroid Build Coastguard Worker if (UpdatedNodes)
182*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(New);
183*9880d681SAndroid Build Coastguard Worker ReplacedNode(Old);
184*9880d681SAndroid Build Coastguard Worker }
ReplaceNode(SDValue Old,SDValue New)185*9880d681SAndroid Build Coastguard Worker void ReplaceNode(SDValue Old, SDValue New) {
186*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
187*9880d681SAndroid Build Coastguard Worker dbgs() << " with: "; New->dump(&DAG));
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesWith(Old, New);
190*9880d681SAndroid Build Coastguard Worker if (UpdatedNodes)
191*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(New.getNode());
192*9880d681SAndroid Build Coastguard Worker ReplacedNode(Old.getNode());
193*9880d681SAndroid Build Coastguard Worker }
ReplaceNode(SDNode * Old,const SDValue * New)194*9880d681SAndroid Build Coastguard Worker void ReplaceNode(SDNode *Old, const SDValue *New) {
195*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesWith(Old, New);
198*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
199*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << (i == 0 ? " with: "
200*9880d681SAndroid Build Coastguard Worker : " and: ");
201*9880d681SAndroid Build Coastguard Worker New[i]->dump(&DAG));
202*9880d681SAndroid Build Coastguard Worker if (UpdatedNodes)
203*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(New[i].getNode());
204*9880d681SAndroid Build Coastguard Worker }
205*9880d681SAndroid Build Coastguard Worker ReplacedNode(Old);
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker };
208*9880d681SAndroid Build Coastguard Worker }
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker /// Return a vector shuffle operation which
211*9880d681SAndroid Build Coastguard Worker /// performs the same shuffe in terms of order or result bytes, but on a type
212*9880d681SAndroid Build Coastguard Worker /// whose vector element type is narrower than the original shuffle type.
213*9880d681SAndroid Build Coastguard Worker /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
ShuffleWithNarrowerEltType(EVT NVT,EVT VT,const SDLoc & dl,SDValue N1,SDValue N2,ArrayRef<int> Mask) const214*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType(
215*9880d681SAndroid Build Coastguard Worker EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
216*9880d681SAndroid Build Coastguard Worker ArrayRef<int> Mask) const {
217*9880d681SAndroid Build Coastguard Worker unsigned NumMaskElts = VT.getVectorNumElements();
218*9880d681SAndroid Build Coastguard Worker unsigned NumDestElts = NVT.getVectorNumElements();
219*9880d681SAndroid Build Coastguard Worker unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
222*9880d681SAndroid Build Coastguard Worker
223*9880d681SAndroid Build Coastguard Worker if (NumEltsGrowth == 1)
224*9880d681SAndroid Build Coastguard Worker return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask);
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker SmallVector<int, 8> NewMask;
227*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumMaskElts; ++i) {
228*9880d681SAndroid Build Coastguard Worker int Idx = Mask[i];
229*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0; j != NumEltsGrowth; ++j) {
230*9880d681SAndroid Build Coastguard Worker if (Idx < 0)
231*9880d681SAndroid Build Coastguard Worker NewMask.push_back(-1);
232*9880d681SAndroid Build Coastguard Worker else
233*9880d681SAndroid Build Coastguard Worker NewMask.push_back(Idx * NumEltsGrowth + j);
234*9880d681SAndroid Build Coastguard Worker }
235*9880d681SAndroid Build Coastguard Worker }
236*9880d681SAndroid Build Coastguard Worker assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
237*9880d681SAndroid Build Coastguard Worker assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
238*9880d681SAndroid Build Coastguard Worker return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask);
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard Worker /// Expands the ConstantFP node to an integer constant or
242*9880d681SAndroid Build Coastguard Worker /// a load from the constant pool.
243*9880d681SAndroid Build Coastguard Worker SDValue
ExpandConstantFP(ConstantFPSDNode * CFP,bool UseCP)244*9880d681SAndroid Build Coastguard Worker SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
245*9880d681SAndroid Build Coastguard Worker bool Extend = false;
246*9880d681SAndroid Build Coastguard Worker SDLoc dl(CFP);
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard Worker // If a FP immediate is precise when represented as a float and if the
249*9880d681SAndroid Build Coastguard Worker // target can do an extending load from float to double, we put it into
250*9880d681SAndroid Build Coastguard Worker // the constant pool as a float, even if it's is statically typed as a
251*9880d681SAndroid Build Coastguard Worker // double. This shrinks FP constants and canonicalizes them for targets where
252*9880d681SAndroid Build Coastguard Worker // an FP extending load is the same cost as a normal load (such as on the x87
253*9880d681SAndroid Build Coastguard Worker // fp stack or PPC FP unit).
254*9880d681SAndroid Build Coastguard Worker EVT VT = CFP->getValueType(0);
255*9880d681SAndroid Build Coastguard Worker ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
256*9880d681SAndroid Build Coastguard Worker if (!UseCP) {
257*9880d681SAndroid Build Coastguard Worker assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
258*9880d681SAndroid Build Coastguard Worker return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
259*9880d681SAndroid Build Coastguard Worker (VT == MVT::f64) ? MVT::i64 : MVT::i32);
260*9880d681SAndroid Build Coastguard Worker }
261*9880d681SAndroid Build Coastguard Worker
262*9880d681SAndroid Build Coastguard Worker EVT OrigVT = VT;
263*9880d681SAndroid Build Coastguard Worker EVT SVT = VT;
264*9880d681SAndroid Build Coastguard Worker while (SVT != MVT::f32 && SVT != MVT::f16) {
265*9880d681SAndroid Build Coastguard Worker SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
266*9880d681SAndroid Build Coastguard Worker if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
267*9880d681SAndroid Build Coastguard Worker // Only do this if the target has a native EXTLOAD instruction from
268*9880d681SAndroid Build Coastguard Worker // smaller type.
269*9880d681SAndroid Build Coastguard Worker TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
270*9880d681SAndroid Build Coastguard Worker TLI.ShouldShrinkFPConstant(OrigVT)) {
271*9880d681SAndroid Build Coastguard Worker Type *SType = SVT.getTypeForEVT(*DAG.getContext());
272*9880d681SAndroid Build Coastguard Worker LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
273*9880d681SAndroid Build Coastguard Worker VT = SVT;
274*9880d681SAndroid Build Coastguard Worker Extend = true;
275*9880d681SAndroid Build Coastguard Worker }
276*9880d681SAndroid Build Coastguard Worker }
277*9880d681SAndroid Build Coastguard Worker
278*9880d681SAndroid Build Coastguard Worker SDValue CPIdx =
279*9880d681SAndroid Build Coastguard Worker DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
280*9880d681SAndroid Build Coastguard Worker unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
281*9880d681SAndroid Build Coastguard Worker if (Extend) {
282*9880d681SAndroid Build Coastguard Worker SDValue Result = DAG.getExtLoad(
283*9880d681SAndroid Build Coastguard Worker ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
284*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT,
285*9880d681SAndroid Build Coastguard Worker false, false, false, Alignment);
286*9880d681SAndroid Build Coastguard Worker return Result;
287*9880d681SAndroid Build Coastguard Worker }
288*9880d681SAndroid Build Coastguard Worker SDValue Result =
289*9880d681SAndroid Build Coastguard Worker DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
290*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
291*9880d681SAndroid Build Coastguard Worker false, false, false, Alignment);
292*9880d681SAndroid Build Coastguard Worker return Result;
293*9880d681SAndroid Build Coastguard Worker }
294*9880d681SAndroid Build Coastguard Worker
295*9880d681SAndroid Build Coastguard Worker /// Expands the Constant node to a load from the constant pool.
ExpandConstant(ConstantSDNode * CP)296*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) {
297*9880d681SAndroid Build Coastguard Worker SDLoc dl(CP);
298*9880d681SAndroid Build Coastguard Worker EVT VT = CP->getValueType(0);
299*9880d681SAndroid Build Coastguard Worker SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(),
300*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout()));
301*9880d681SAndroid Build Coastguard Worker unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
302*9880d681SAndroid Build Coastguard Worker SDValue Result =
303*9880d681SAndroid Build Coastguard Worker DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
304*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
305*9880d681SAndroid Build Coastguard Worker false, false, false, Alignment);
306*9880d681SAndroid Build Coastguard Worker return Result;
307*9880d681SAndroid Build Coastguard Worker }
308*9880d681SAndroid Build Coastguard Worker
309*9880d681SAndroid Build Coastguard Worker /// Some target cannot handle a variable insertion index for the
310*9880d681SAndroid Build Coastguard Worker /// INSERT_VECTOR_ELT instruction. In this case, it
311*9880d681SAndroid Build Coastguard Worker /// is necessary to spill the vector being inserted into to memory, perform
312*9880d681SAndroid Build Coastguard Worker /// the insert there, and then read the result back.
PerformInsertVectorEltInMemory(SDValue Vec,SDValue Val,SDValue Idx,const SDLoc & dl)313*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::PerformInsertVectorEltInMemory(SDValue Vec,
314*9880d681SAndroid Build Coastguard Worker SDValue Val,
315*9880d681SAndroid Build Coastguard Worker SDValue Idx,
316*9880d681SAndroid Build Coastguard Worker const SDLoc &dl) {
317*9880d681SAndroid Build Coastguard Worker SDValue Tmp1 = Vec;
318*9880d681SAndroid Build Coastguard Worker SDValue Tmp2 = Val;
319*9880d681SAndroid Build Coastguard Worker SDValue Tmp3 = Idx;
320*9880d681SAndroid Build Coastguard Worker
321*9880d681SAndroid Build Coastguard Worker // If the target doesn't support this, we have to spill the input vector
322*9880d681SAndroid Build Coastguard Worker // to a temporary stack slot, update the element, then reload it. This is
323*9880d681SAndroid Build Coastguard Worker // badness. We could also load the value into a vector register (either
324*9880d681SAndroid Build Coastguard Worker // with a "move to register" or "extload into register" instruction, then
325*9880d681SAndroid Build Coastguard Worker // permute it into place, if the idx is a constant and if the idx is
326*9880d681SAndroid Build Coastguard Worker // supported by the target.
327*9880d681SAndroid Build Coastguard Worker EVT VT = Tmp1.getValueType();
328*9880d681SAndroid Build Coastguard Worker EVT EltVT = VT.getVectorElementType();
329*9880d681SAndroid Build Coastguard Worker EVT IdxVT = Tmp3.getValueType();
330*9880d681SAndroid Build Coastguard Worker EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
331*9880d681SAndroid Build Coastguard Worker SDValue StackPtr = DAG.CreateStackTemporary(VT);
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard Worker int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker // Store the vector.
336*9880d681SAndroid Build Coastguard Worker SDValue Ch = DAG.getStore(
337*9880d681SAndroid Build Coastguard Worker DAG.getEntryNode(), dl, Tmp1, StackPtr,
338*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), false,
339*9880d681SAndroid Build Coastguard Worker false, 0);
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker // Truncate or zero extend offset to target pointer type.
342*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getZExtOrTrunc(Tmp3, dl, PtrVT);
343*9880d681SAndroid Build Coastguard Worker // Add the offset to the index.
344*9880d681SAndroid Build Coastguard Worker unsigned EltSize = EltVT.getSizeInBits()/8;
345*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,
346*9880d681SAndroid Build Coastguard Worker DAG.getConstant(EltSize, dl, IdxVT));
347*9880d681SAndroid Build Coastguard Worker SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
348*9880d681SAndroid Build Coastguard Worker // Store the scalar value.
349*9880d681SAndroid Build Coastguard Worker Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
350*9880d681SAndroid Build Coastguard Worker false, false, 0);
351*9880d681SAndroid Build Coastguard Worker // Load the updated vector.
352*9880d681SAndroid Build Coastguard Worker return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack(
353*9880d681SAndroid Build Coastguard Worker DAG.getMachineFunction(), SPFI),
354*9880d681SAndroid Build Coastguard Worker false, false, false, 0);
355*9880d681SAndroid Build Coastguard Worker }
356*9880d681SAndroid Build Coastguard Worker
ExpandINSERT_VECTOR_ELT(SDValue Vec,SDValue Val,SDValue Idx,const SDLoc & dl)357*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
358*9880d681SAndroid Build Coastguard Worker SDValue Idx,
359*9880d681SAndroid Build Coastguard Worker const SDLoc &dl) {
360*9880d681SAndroid Build Coastguard Worker if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
361*9880d681SAndroid Build Coastguard Worker // SCALAR_TO_VECTOR requires that the type of the value being inserted
362*9880d681SAndroid Build Coastguard Worker // match the element type of the vector being created, except for
363*9880d681SAndroid Build Coastguard Worker // integers in which case the inserted value can be over width.
364*9880d681SAndroid Build Coastguard Worker EVT EltVT = Vec.getValueType().getVectorElementType();
365*9880d681SAndroid Build Coastguard Worker if (Val.getValueType() == EltVT ||
366*9880d681SAndroid Build Coastguard Worker (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
367*9880d681SAndroid Build Coastguard Worker SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
368*9880d681SAndroid Build Coastguard Worker Vec.getValueType(), Val);
369*9880d681SAndroid Build Coastguard Worker
370*9880d681SAndroid Build Coastguard Worker unsigned NumElts = Vec.getValueType().getVectorNumElements();
371*9880d681SAndroid Build Coastguard Worker // We generate a shuffle of InVec and ScVec, so the shuffle mask
372*9880d681SAndroid Build Coastguard Worker // should be 0,1,2,3,4,5... with the appropriate element replaced with
373*9880d681SAndroid Build Coastguard Worker // elt 0 of the RHS.
374*9880d681SAndroid Build Coastguard Worker SmallVector<int, 8> ShufOps;
375*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElts; ++i)
376*9880d681SAndroid Build Coastguard Worker ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard Worker return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps);
379*9880d681SAndroid Build Coastguard Worker }
380*9880d681SAndroid Build Coastguard Worker }
381*9880d681SAndroid Build Coastguard Worker return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
382*9880d681SAndroid Build Coastguard Worker }
383*9880d681SAndroid Build Coastguard Worker
OptimizeFloatStore(StoreSDNode * ST)384*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
385*9880d681SAndroid Build Coastguard Worker // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
386*9880d681SAndroid Build Coastguard Worker // FIXME: We shouldn't do this for TargetConstantFP's.
387*9880d681SAndroid Build Coastguard Worker // FIXME: move this to the DAG Combiner! Note that we can't regress due
388*9880d681SAndroid Build Coastguard Worker // to phase ordering between legalized code and the dag combiner. This
389*9880d681SAndroid Build Coastguard Worker // probably means that we need to integrate dag combiner and legalizer
390*9880d681SAndroid Build Coastguard Worker // together.
391*9880d681SAndroid Build Coastguard Worker // We generally can't do this one for long doubles.
392*9880d681SAndroid Build Coastguard Worker SDValue Chain = ST->getChain();
393*9880d681SAndroid Build Coastguard Worker SDValue Ptr = ST->getBasePtr();
394*9880d681SAndroid Build Coastguard Worker unsigned Alignment = ST->getAlignment();
395*9880d681SAndroid Build Coastguard Worker bool isVolatile = ST->isVolatile();
396*9880d681SAndroid Build Coastguard Worker bool isNonTemporal = ST->isNonTemporal();
397*9880d681SAndroid Build Coastguard Worker AAMDNodes AAInfo = ST->getAAInfo();
398*9880d681SAndroid Build Coastguard Worker SDLoc dl(ST);
399*9880d681SAndroid Build Coastguard Worker if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
400*9880d681SAndroid Build Coastguard Worker if (CFP->getValueType(0) == MVT::f32 &&
401*9880d681SAndroid Build Coastguard Worker TLI.isTypeLegal(MVT::i32)) {
402*9880d681SAndroid Build Coastguard Worker SDValue Con = DAG.getConstant(CFP->getValueAPF().
403*9880d681SAndroid Build Coastguard Worker bitcastToAPInt().zextOrTrunc(32),
404*9880d681SAndroid Build Coastguard Worker SDLoc(CFP), MVT::i32);
405*9880d681SAndroid Build Coastguard Worker return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
406*9880d681SAndroid Build Coastguard Worker isVolatile, isNonTemporal, Alignment, AAInfo);
407*9880d681SAndroid Build Coastguard Worker }
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard Worker if (CFP->getValueType(0) == MVT::f64) {
410*9880d681SAndroid Build Coastguard Worker // If this target supports 64-bit registers, do a single 64-bit store.
411*9880d681SAndroid Build Coastguard Worker if (TLI.isTypeLegal(MVT::i64)) {
412*9880d681SAndroid Build Coastguard Worker SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
413*9880d681SAndroid Build Coastguard Worker zextOrTrunc(64), SDLoc(CFP), MVT::i64);
414*9880d681SAndroid Build Coastguard Worker return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
415*9880d681SAndroid Build Coastguard Worker isVolatile, isNonTemporal, Alignment, AAInfo);
416*9880d681SAndroid Build Coastguard Worker }
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard Worker if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
419*9880d681SAndroid Build Coastguard Worker // Otherwise, if the target supports 32-bit registers, use 2 32-bit
420*9880d681SAndroid Build Coastguard Worker // stores. If the target supports neither 32- nor 64-bits, this
421*9880d681SAndroid Build Coastguard Worker // xform is certainly not worth it.
422*9880d681SAndroid Build Coastguard Worker const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
423*9880d681SAndroid Build Coastguard Worker SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
424*9880d681SAndroid Build Coastguard Worker SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
425*9880d681SAndroid Build Coastguard Worker if (DAG.getDataLayout().isBigEndian())
426*9880d681SAndroid Build Coastguard Worker std::swap(Lo, Hi);
427*9880d681SAndroid Build Coastguard Worker
428*9880d681SAndroid Build Coastguard Worker Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), isVolatile,
429*9880d681SAndroid Build Coastguard Worker isNonTemporal, Alignment, AAInfo);
430*9880d681SAndroid Build Coastguard Worker Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
431*9880d681SAndroid Build Coastguard Worker DAG.getConstant(4, dl, Ptr.getValueType()));
432*9880d681SAndroid Build Coastguard Worker Hi = DAG.getStore(Chain, dl, Hi, Ptr,
433*9880d681SAndroid Build Coastguard Worker ST->getPointerInfo().getWithOffset(4),
434*9880d681SAndroid Build Coastguard Worker isVolatile, isNonTemporal, MinAlign(Alignment, 4U),
435*9880d681SAndroid Build Coastguard Worker AAInfo);
436*9880d681SAndroid Build Coastguard Worker
437*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
438*9880d681SAndroid Build Coastguard Worker }
439*9880d681SAndroid Build Coastguard Worker }
440*9880d681SAndroid Build Coastguard Worker }
441*9880d681SAndroid Build Coastguard Worker return SDValue(nullptr, 0);
442*9880d681SAndroid Build Coastguard Worker }
443*9880d681SAndroid Build Coastguard Worker
LegalizeStoreOps(SDNode * Node)444*9880d681SAndroid Build Coastguard Worker void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
445*9880d681SAndroid Build Coastguard Worker StoreSDNode *ST = cast<StoreSDNode>(Node);
446*9880d681SAndroid Build Coastguard Worker SDValue Chain = ST->getChain();
447*9880d681SAndroid Build Coastguard Worker SDValue Ptr = ST->getBasePtr();
448*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Worker unsigned Alignment = ST->getAlignment();
451*9880d681SAndroid Build Coastguard Worker bool isVolatile = ST->isVolatile();
452*9880d681SAndroid Build Coastguard Worker bool isNonTemporal = ST->isNonTemporal();
453*9880d681SAndroid Build Coastguard Worker AAMDNodes AAInfo = ST->getAAInfo();
454*9880d681SAndroid Build Coastguard Worker
455*9880d681SAndroid Build Coastguard Worker if (!ST->isTruncatingStore()) {
456*9880d681SAndroid Build Coastguard Worker if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
457*9880d681SAndroid Build Coastguard Worker ReplaceNode(ST, OptStore);
458*9880d681SAndroid Build Coastguard Worker return;
459*9880d681SAndroid Build Coastguard Worker }
460*9880d681SAndroid Build Coastguard Worker
461*9880d681SAndroid Build Coastguard Worker {
462*9880d681SAndroid Build Coastguard Worker SDValue Value = ST->getValue();
463*9880d681SAndroid Build Coastguard Worker MVT VT = Value.getSimpleValueType();
464*9880d681SAndroid Build Coastguard Worker switch (TLI.getOperationAction(ISD::STORE, VT)) {
465*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("This action is not supported yet!");
466*9880d681SAndroid Build Coastguard Worker case TargetLowering::Legal: {
467*9880d681SAndroid Build Coastguard Worker // If this is an unaligned store and the target doesn't support it,
468*9880d681SAndroid Build Coastguard Worker // expand it.
469*9880d681SAndroid Build Coastguard Worker EVT MemVT = ST->getMemoryVT();
470*9880d681SAndroid Build Coastguard Worker unsigned AS = ST->getAddressSpace();
471*9880d681SAndroid Build Coastguard Worker unsigned Align = ST->getAlignment();
472*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = DAG.getDataLayout();
473*9880d681SAndroid Build Coastguard Worker if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) {
474*9880d681SAndroid Build Coastguard Worker SDValue Result = TLI.expandUnalignedStore(ST, DAG);
475*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(ST, 0), Result);
476*9880d681SAndroid Build Coastguard Worker }
477*9880d681SAndroid Build Coastguard Worker break;
478*9880d681SAndroid Build Coastguard Worker }
479*9880d681SAndroid Build Coastguard Worker case TargetLowering::Custom: {
480*9880d681SAndroid Build Coastguard Worker SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
481*9880d681SAndroid Build Coastguard Worker if (Res && Res != SDValue(Node, 0))
482*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(Node, 0), Res);
483*9880d681SAndroid Build Coastguard Worker return;
484*9880d681SAndroid Build Coastguard Worker }
485*9880d681SAndroid Build Coastguard Worker case TargetLowering::Promote: {
486*9880d681SAndroid Build Coastguard Worker MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
487*9880d681SAndroid Build Coastguard Worker assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
488*9880d681SAndroid Build Coastguard Worker "Can only promote stores to same size type");
489*9880d681SAndroid Build Coastguard Worker Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
490*9880d681SAndroid Build Coastguard Worker SDValue Result =
491*9880d681SAndroid Build Coastguard Worker DAG.getStore(Chain, dl, Value, Ptr,
492*9880d681SAndroid Build Coastguard Worker ST->getPointerInfo(), isVolatile,
493*9880d681SAndroid Build Coastguard Worker isNonTemporal, Alignment, AAInfo);
494*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(Node, 0), Result);
495*9880d681SAndroid Build Coastguard Worker break;
496*9880d681SAndroid Build Coastguard Worker }
497*9880d681SAndroid Build Coastguard Worker }
498*9880d681SAndroid Build Coastguard Worker return;
499*9880d681SAndroid Build Coastguard Worker }
500*9880d681SAndroid Build Coastguard Worker } else {
501*9880d681SAndroid Build Coastguard Worker SDValue Value = ST->getValue();
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard Worker EVT StVT = ST->getMemoryVT();
504*9880d681SAndroid Build Coastguard Worker unsigned StWidth = StVT.getSizeInBits();
505*9880d681SAndroid Build Coastguard Worker auto &DL = DAG.getDataLayout();
506*9880d681SAndroid Build Coastguard Worker
507*9880d681SAndroid Build Coastguard Worker if (StWidth != StVT.getStoreSizeInBits()) {
508*9880d681SAndroid Build Coastguard Worker // Promote to a byte-sized store with upper bits zero if not
509*9880d681SAndroid Build Coastguard Worker // storing an integral number of bytes. For example, promote
510*9880d681SAndroid Build Coastguard Worker // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
511*9880d681SAndroid Build Coastguard Worker EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
512*9880d681SAndroid Build Coastguard Worker StVT.getStoreSizeInBits());
513*9880d681SAndroid Build Coastguard Worker Value = DAG.getZeroExtendInReg(Value, dl, StVT);
514*9880d681SAndroid Build Coastguard Worker SDValue Result =
515*9880d681SAndroid Build Coastguard Worker DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
516*9880d681SAndroid Build Coastguard Worker NVT, isVolatile, isNonTemporal, Alignment, AAInfo);
517*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(Node, 0), Result);
518*9880d681SAndroid Build Coastguard Worker } else if (StWidth & (StWidth - 1)) {
519*9880d681SAndroid Build Coastguard Worker // If not storing a power-of-2 number of bits, expand as two stores.
520*9880d681SAndroid Build Coastguard Worker assert(!StVT.isVector() && "Unsupported truncstore!");
521*9880d681SAndroid Build Coastguard Worker unsigned RoundWidth = 1 << Log2_32(StWidth);
522*9880d681SAndroid Build Coastguard Worker assert(RoundWidth < StWidth);
523*9880d681SAndroid Build Coastguard Worker unsigned ExtraWidth = StWidth - RoundWidth;
524*9880d681SAndroid Build Coastguard Worker assert(ExtraWidth < RoundWidth);
525*9880d681SAndroid Build Coastguard Worker assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
526*9880d681SAndroid Build Coastguard Worker "Store size not an integral number of bytes!");
527*9880d681SAndroid Build Coastguard Worker EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
528*9880d681SAndroid Build Coastguard Worker EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
529*9880d681SAndroid Build Coastguard Worker SDValue Lo, Hi;
530*9880d681SAndroid Build Coastguard Worker unsigned IncrementSize;
531*9880d681SAndroid Build Coastguard Worker
532*9880d681SAndroid Build Coastguard Worker if (DL.isLittleEndian()) {
533*9880d681SAndroid Build Coastguard Worker // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
534*9880d681SAndroid Build Coastguard Worker // Store the bottom RoundWidth bits.
535*9880d681SAndroid Build Coastguard Worker Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
536*9880d681SAndroid Build Coastguard Worker RoundVT,
537*9880d681SAndroid Build Coastguard Worker isVolatile, isNonTemporal, Alignment,
538*9880d681SAndroid Build Coastguard Worker AAInfo);
539*9880d681SAndroid Build Coastguard Worker
540*9880d681SAndroid Build Coastguard Worker // Store the remaining ExtraWidth bits.
541*9880d681SAndroid Build Coastguard Worker IncrementSize = RoundWidth / 8;
542*9880d681SAndroid Build Coastguard Worker Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
543*9880d681SAndroid Build Coastguard Worker DAG.getConstant(IncrementSize, dl,
544*9880d681SAndroid Build Coastguard Worker Ptr.getValueType()));
545*9880d681SAndroid Build Coastguard Worker Hi = DAG.getNode(
546*9880d681SAndroid Build Coastguard Worker ISD::SRL, dl, Value.getValueType(), Value,
547*9880d681SAndroid Build Coastguard Worker DAG.getConstant(RoundWidth, dl,
548*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(Value.getValueType(), DL)));
549*9880d681SAndroid Build Coastguard Worker Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
550*9880d681SAndroid Build Coastguard Worker ST->getPointerInfo().getWithOffset(IncrementSize),
551*9880d681SAndroid Build Coastguard Worker ExtraVT, isVolatile, isNonTemporal,
552*9880d681SAndroid Build Coastguard Worker MinAlign(Alignment, IncrementSize), AAInfo);
553*9880d681SAndroid Build Coastguard Worker } else {
554*9880d681SAndroid Build Coastguard Worker // Big endian - avoid unaligned stores.
555*9880d681SAndroid Build Coastguard Worker // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
556*9880d681SAndroid Build Coastguard Worker // Store the top RoundWidth bits.
557*9880d681SAndroid Build Coastguard Worker Hi = DAG.getNode(
558*9880d681SAndroid Build Coastguard Worker ISD::SRL, dl, Value.getValueType(), Value,
559*9880d681SAndroid Build Coastguard Worker DAG.getConstant(ExtraWidth, dl,
560*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(Value.getValueType(), DL)));
561*9880d681SAndroid Build Coastguard Worker Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(),
562*9880d681SAndroid Build Coastguard Worker RoundVT, isVolatile, isNonTemporal, Alignment,
563*9880d681SAndroid Build Coastguard Worker AAInfo);
564*9880d681SAndroid Build Coastguard Worker
565*9880d681SAndroid Build Coastguard Worker // Store the remaining ExtraWidth bits.
566*9880d681SAndroid Build Coastguard Worker IncrementSize = RoundWidth / 8;
567*9880d681SAndroid Build Coastguard Worker Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
568*9880d681SAndroid Build Coastguard Worker DAG.getConstant(IncrementSize, dl,
569*9880d681SAndroid Build Coastguard Worker Ptr.getValueType()));
570*9880d681SAndroid Build Coastguard Worker Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
571*9880d681SAndroid Build Coastguard Worker ST->getPointerInfo().getWithOffset(IncrementSize),
572*9880d681SAndroid Build Coastguard Worker ExtraVT, isVolatile, isNonTemporal,
573*9880d681SAndroid Build Coastguard Worker MinAlign(Alignment, IncrementSize), AAInfo);
574*9880d681SAndroid Build Coastguard Worker }
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker // The order of the stores doesn't matter.
577*9880d681SAndroid Build Coastguard Worker SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
578*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(Node, 0), Result);
579*9880d681SAndroid Build Coastguard Worker } else {
580*9880d681SAndroid Build Coastguard Worker switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
581*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("This action is not supported yet!");
582*9880d681SAndroid Build Coastguard Worker case TargetLowering::Legal: {
583*9880d681SAndroid Build Coastguard Worker EVT MemVT = ST->getMemoryVT();
584*9880d681SAndroid Build Coastguard Worker unsigned AS = ST->getAddressSpace();
585*9880d681SAndroid Build Coastguard Worker unsigned Align = ST->getAlignment();
586*9880d681SAndroid Build Coastguard Worker // If this is an unaligned store and the target doesn't support it,
587*9880d681SAndroid Build Coastguard Worker // expand it.
588*9880d681SAndroid Build Coastguard Worker if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) {
589*9880d681SAndroid Build Coastguard Worker SDValue Result = TLI.expandUnalignedStore(ST, DAG);
590*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(ST, 0), Result);
591*9880d681SAndroid Build Coastguard Worker }
592*9880d681SAndroid Build Coastguard Worker break;
593*9880d681SAndroid Build Coastguard Worker }
594*9880d681SAndroid Build Coastguard Worker case TargetLowering::Custom: {
595*9880d681SAndroid Build Coastguard Worker SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
596*9880d681SAndroid Build Coastguard Worker if (Res && Res != SDValue(Node, 0))
597*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(Node, 0), Res);
598*9880d681SAndroid Build Coastguard Worker return;
599*9880d681SAndroid Build Coastguard Worker }
600*9880d681SAndroid Build Coastguard Worker case TargetLowering::Expand:
601*9880d681SAndroid Build Coastguard Worker assert(!StVT.isVector() &&
602*9880d681SAndroid Build Coastguard Worker "Vector Stores are handled in LegalizeVectorOps");
603*9880d681SAndroid Build Coastguard Worker
604*9880d681SAndroid Build Coastguard Worker // TRUNCSTORE:i16 i32 -> STORE i16
605*9880d681SAndroid Build Coastguard Worker assert(TLI.isTypeLegal(StVT) &&
606*9880d681SAndroid Build Coastguard Worker "Do not know how to expand this store!");
607*9880d681SAndroid Build Coastguard Worker Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
608*9880d681SAndroid Build Coastguard Worker SDValue Result =
609*9880d681SAndroid Build Coastguard Worker DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
610*9880d681SAndroid Build Coastguard Worker isVolatile, isNonTemporal, Alignment, AAInfo);
611*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(Node, 0), Result);
612*9880d681SAndroid Build Coastguard Worker break;
613*9880d681SAndroid Build Coastguard Worker }
614*9880d681SAndroid Build Coastguard Worker }
615*9880d681SAndroid Build Coastguard Worker }
616*9880d681SAndroid Build Coastguard Worker }
617*9880d681SAndroid Build Coastguard Worker
LegalizeLoadOps(SDNode * Node)618*9880d681SAndroid Build Coastguard Worker void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
619*9880d681SAndroid Build Coastguard Worker LoadSDNode *LD = cast<LoadSDNode>(Node);
620*9880d681SAndroid Build Coastguard Worker SDValue Chain = LD->getChain(); // The chain.
621*9880d681SAndroid Build Coastguard Worker SDValue Ptr = LD->getBasePtr(); // The base pointer.
622*9880d681SAndroid Build Coastguard Worker SDValue Value; // The value returned by the load op.
623*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
624*9880d681SAndroid Build Coastguard Worker
625*9880d681SAndroid Build Coastguard Worker ISD::LoadExtType ExtType = LD->getExtensionType();
626*9880d681SAndroid Build Coastguard Worker if (ExtType == ISD::NON_EXTLOAD) {
627*9880d681SAndroid Build Coastguard Worker MVT VT = Node->getSimpleValueType(0);
628*9880d681SAndroid Build Coastguard Worker SDValue RVal = SDValue(Node, 0);
629*9880d681SAndroid Build Coastguard Worker SDValue RChain = SDValue(Node, 1);
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
632*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("This action is not supported yet!");
633*9880d681SAndroid Build Coastguard Worker case TargetLowering::Legal: {
634*9880d681SAndroid Build Coastguard Worker EVT MemVT = LD->getMemoryVT();
635*9880d681SAndroid Build Coastguard Worker unsigned AS = LD->getAddressSpace();
636*9880d681SAndroid Build Coastguard Worker unsigned Align = LD->getAlignment();
637*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = DAG.getDataLayout();
638*9880d681SAndroid Build Coastguard Worker // If this is an unaligned load and the target doesn't support it,
639*9880d681SAndroid Build Coastguard Worker // expand it.
640*9880d681SAndroid Build Coastguard Worker if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) {
641*9880d681SAndroid Build Coastguard Worker std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG);
642*9880d681SAndroid Build Coastguard Worker }
643*9880d681SAndroid Build Coastguard Worker break;
644*9880d681SAndroid Build Coastguard Worker }
645*9880d681SAndroid Build Coastguard Worker case TargetLowering::Custom: {
646*9880d681SAndroid Build Coastguard Worker if (SDValue Res = TLI.LowerOperation(RVal, DAG)) {
647*9880d681SAndroid Build Coastguard Worker RVal = Res;
648*9880d681SAndroid Build Coastguard Worker RChain = Res.getValue(1);
649*9880d681SAndroid Build Coastguard Worker }
650*9880d681SAndroid Build Coastguard Worker break;
651*9880d681SAndroid Build Coastguard Worker }
652*9880d681SAndroid Build Coastguard Worker case TargetLowering::Promote: {
653*9880d681SAndroid Build Coastguard Worker MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
654*9880d681SAndroid Build Coastguard Worker assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
655*9880d681SAndroid Build Coastguard Worker "Can only promote loads to same size type");
656*9880d681SAndroid Build Coastguard Worker
657*9880d681SAndroid Build Coastguard Worker SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
658*9880d681SAndroid Build Coastguard Worker RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
659*9880d681SAndroid Build Coastguard Worker RChain = Res.getValue(1);
660*9880d681SAndroid Build Coastguard Worker break;
661*9880d681SAndroid Build Coastguard Worker }
662*9880d681SAndroid Build Coastguard Worker }
663*9880d681SAndroid Build Coastguard Worker if (RChain.getNode() != Node) {
664*9880d681SAndroid Build Coastguard Worker assert(RVal.getNode() != Node && "Load must be completely replaced");
665*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
666*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
667*9880d681SAndroid Build Coastguard Worker if (UpdatedNodes) {
668*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(RVal.getNode());
669*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(RChain.getNode());
670*9880d681SAndroid Build Coastguard Worker }
671*9880d681SAndroid Build Coastguard Worker ReplacedNode(Node);
672*9880d681SAndroid Build Coastguard Worker }
673*9880d681SAndroid Build Coastguard Worker return;
674*9880d681SAndroid Build Coastguard Worker }
675*9880d681SAndroid Build Coastguard Worker
676*9880d681SAndroid Build Coastguard Worker EVT SrcVT = LD->getMemoryVT();
677*9880d681SAndroid Build Coastguard Worker unsigned SrcWidth = SrcVT.getSizeInBits();
678*9880d681SAndroid Build Coastguard Worker unsigned Alignment = LD->getAlignment();
679*9880d681SAndroid Build Coastguard Worker bool isVolatile = LD->isVolatile();
680*9880d681SAndroid Build Coastguard Worker bool isNonTemporal = LD->isNonTemporal();
681*9880d681SAndroid Build Coastguard Worker bool isInvariant = LD->isInvariant();
682*9880d681SAndroid Build Coastguard Worker AAMDNodes AAInfo = LD->getAAInfo();
683*9880d681SAndroid Build Coastguard Worker
684*9880d681SAndroid Build Coastguard Worker if (SrcWidth != SrcVT.getStoreSizeInBits() &&
685*9880d681SAndroid Build Coastguard Worker // Some targets pretend to have an i1 loading operation, and actually
686*9880d681SAndroid Build Coastguard Worker // load an i8. This trick is correct for ZEXTLOAD because the top 7
687*9880d681SAndroid Build Coastguard Worker // bits are guaranteed to be zero; it helps the optimizers understand
688*9880d681SAndroid Build Coastguard Worker // that these bits are zero. It is also useful for EXTLOAD, since it
689*9880d681SAndroid Build Coastguard Worker // tells the optimizers that those bits are undefined. It would be
690*9880d681SAndroid Build Coastguard Worker // nice to have an effective generic way of getting these benefits...
691*9880d681SAndroid Build Coastguard Worker // Until such a way is found, don't insist on promoting i1 here.
692*9880d681SAndroid Build Coastguard Worker (SrcVT != MVT::i1 ||
693*9880d681SAndroid Build Coastguard Worker TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
694*9880d681SAndroid Build Coastguard Worker TargetLowering::Promote)) {
695*9880d681SAndroid Build Coastguard Worker // Promote to a byte-sized load if not loading an integral number of
696*9880d681SAndroid Build Coastguard Worker // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
697*9880d681SAndroid Build Coastguard Worker unsigned NewWidth = SrcVT.getStoreSizeInBits();
698*9880d681SAndroid Build Coastguard Worker EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
699*9880d681SAndroid Build Coastguard Worker SDValue Ch;
700*9880d681SAndroid Build Coastguard Worker
701*9880d681SAndroid Build Coastguard Worker // The extra bits are guaranteed to be zero, since we stored them that
702*9880d681SAndroid Build Coastguard Worker // way. A zext load from NVT thus automatically gives zext from SrcVT.
703*9880d681SAndroid Build Coastguard Worker
704*9880d681SAndroid Build Coastguard Worker ISD::LoadExtType NewExtType =
705*9880d681SAndroid Build Coastguard Worker ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
706*9880d681SAndroid Build Coastguard Worker
707*9880d681SAndroid Build Coastguard Worker SDValue Result =
708*9880d681SAndroid Build Coastguard Worker DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
709*9880d681SAndroid Build Coastguard Worker Chain, Ptr, LD->getPointerInfo(),
710*9880d681SAndroid Build Coastguard Worker NVT, isVolatile, isNonTemporal, isInvariant, Alignment,
711*9880d681SAndroid Build Coastguard Worker AAInfo);
712*9880d681SAndroid Build Coastguard Worker
713*9880d681SAndroid Build Coastguard Worker Ch = Result.getValue(1); // The chain.
714*9880d681SAndroid Build Coastguard Worker
715*9880d681SAndroid Build Coastguard Worker if (ExtType == ISD::SEXTLOAD)
716*9880d681SAndroid Build Coastguard Worker // Having the top bits zero doesn't help when sign extending.
717*9880d681SAndroid Build Coastguard Worker Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
718*9880d681SAndroid Build Coastguard Worker Result.getValueType(),
719*9880d681SAndroid Build Coastguard Worker Result, DAG.getValueType(SrcVT));
720*9880d681SAndroid Build Coastguard Worker else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
721*9880d681SAndroid Build Coastguard Worker // All the top bits are guaranteed to be zero - inform the optimizers.
722*9880d681SAndroid Build Coastguard Worker Result = DAG.getNode(ISD::AssertZext, dl,
723*9880d681SAndroid Build Coastguard Worker Result.getValueType(), Result,
724*9880d681SAndroid Build Coastguard Worker DAG.getValueType(SrcVT));
725*9880d681SAndroid Build Coastguard Worker
726*9880d681SAndroid Build Coastguard Worker Value = Result;
727*9880d681SAndroid Build Coastguard Worker Chain = Ch;
728*9880d681SAndroid Build Coastguard Worker } else if (SrcWidth & (SrcWidth - 1)) {
729*9880d681SAndroid Build Coastguard Worker // If not loading a power-of-2 number of bits, expand as two loads.
730*9880d681SAndroid Build Coastguard Worker assert(!SrcVT.isVector() && "Unsupported extload!");
731*9880d681SAndroid Build Coastguard Worker unsigned RoundWidth = 1 << Log2_32(SrcWidth);
732*9880d681SAndroid Build Coastguard Worker assert(RoundWidth < SrcWidth);
733*9880d681SAndroid Build Coastguard Worker unsigned ExtraWidth = SrcWidth - RoundWidth;
734*9880d681SAndroid Build Coastguard Worker assert(ExtraWidth < RoundWidth);
735*9880d681SAndroid Build Coastguard Worker assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
736*9880d681SAndroid Build Coastguard Worker "Load size not an integral number of bytes!");
737*9880d681SAndroid Build Coastguard Worker EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
738*9880d681SAndroid Build Coastguard Worker EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
739*9880d681SAndroid Build Coastguard Worker SDValue Lo, Hi, Ch;
740*9880d681SAndroid Build Coastguard Worker unsigned IncrementSize;
741*9880d681SAndroid Build Coastguard Worker auto &DL = DAG.getDataLayout();
742*9880d681SAndroid Build Coastguard Worker
743*9880d681SAndroid Build Coastguard Worker if (DL.isLittleEndian()) {
744*9880d681SAndroid Build Coastguard Worker // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
745*9880d681SAndroid Build Coastguard Worker // Load the bottom RoundWidth bits.
746*9880d681SAndroid Build Coastguard Worker Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
747*9880d681SAndroid Build Coastguard Worker Chain, Ptr,
748*9880d681SAndroid Build Coastguard Worker LD->getPointerInfo(), RoundVT, isVolatile,
749*9880d681SAndroid Build Coastguard Worker isNonTemporal, isInvariant, Alignment, AAInfo);
750*9880d681SAndroid Build Coastguard Worker
751*9880d681SAndroid Build Coastguard Worker // Load the remaining ExtraWidth bits.
752*9880d681SAndroid Build Coastguard Worker IncrementSize = RoundWidth / 8;
753*9880d681SAndroid Build Coastguard Worker Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
754*9880d681SAndroid Build Coastguard Worker DAG.getConstant(IncrementSize, dl,
755*9880d681SAndroid Build Coastguard Worker Ptr.getValueType()));
756*9880d681SAndroid Build Coastguard Worker Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
757*9880d681SAndroid Build Coastguard Worker LD->getPointerInfo().getWithOffset(IncrementSize),
758*9880d681SAndroid Build Coastguard Worker ExtraVT, isVolatile, isNonTemporal, isInvariant,
759*9880d681SAndroid Build Coastguard Worker MinAlign(Alignment, IncrementSize), AAInfo);
760*9880d681SAndroid Build Coastguard Worker
761*9880d681SAndroid Build Coastguard Worker // Build a factor node to remember that this load is independent of
762*9880d681SAndroid Build Coastguard Worker // the other one.
763*9880d681SAndroid Build Coastguard Worker Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
764*9880d681SAndroid Build Coastguard Worker Hi.getValue(1));
765*9880d681SAndroid Build Coastguard Worker
766*9880d681SAndroid Build Coastguard Worker // Move the top bits to the right place.
767*9880d681SAndroid Build Coastguard Worker Hi = DAG.getNode(
768*9880d681SAndroid Build Coastguard Worker ISD::SHL, dl, Hi.getValueType(), Hi,
769*9880d681SAndroid Build Coastguard Worker DAG.getConstant(RoundWidth, dl,
770*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(Hi.getValueType(), DL)));
771*9880d681SAndroid Build Coastguard Worker
772*9880d681SAndroid Build Coastguard Worker // Join the hi and lo parts.
773*9880d681SAndroid Build Coastguard Worker Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
774*9880d681SAndroid Build Coastguard Worker } else {
775*9880d681SAndroid Build Coastguard Worker // Big endian - avoid unaligned loads.
776*9880d681SAndroid Build Coastguard Worker // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
777*9880d681SAndroid Build Coastguard Worker // Load the top RoundWidth bits.
778*9880d681SAndroid Build Coastguard Worker Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
779*9880d681SAndroid Build Coastguard Worker LD->getPointerInfo(), RoundVT, isVolatile,
780*9880d681SAndroid Build Coastguard Worker isNonTemporal, isInvariant, Alignment, AAInfo);
781*9880d681SAndroid Build Coastguard Worker
782*9880d681SAndroid Build Coastguard Worker // Load the remaining ExtraWidth bits.
783*9880d681SAndroid Build Coastguard Worker IncrementSize = RoundWidth / 8;
784*9880d681SAndroid Build Coastguard Worker Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
785*9880d681SAndroid Build Coastguard Worker DAG.getConstant(IncrementSize, dl,
786*9880d681SAndroid Build Coastguard Worker Ptr.getValueType()));
787*9880d681SAndroid Build Coastguard Worker Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
788*9880d681SAndroid Build Coastguard Worker dl, Node->getValueType(0), Chain, Ptr,
789*9880d681SAndroid Build Coastguard Worker LD->getPointerInfo().getWithOffset(IncrementSize),
790*9880d681SAndroid Build Coastguard Worker ExtraVT, isVolatile, isNonTemporal, isInvariant,
791*9880d681SAndroid Build Coastguard Worker MinAlign(Alignment, IncrementSize), AAInfo);
792*9880d681SAndroid Build Coastguard Worker
793*9880d681SAndroid Build Coastguard Worker // Build a factor node to remember that this load is independent of
794*9880d681SAndroid Build Coastguard Worker // the other one.
795*9880d681SAndroid Build Coastguard Worker Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
796*9880d681SAndroid Build Coastguard Worker Hi.getValue(1));
797*9880d681SAndroid Build Coastguard Worker
798*9880d681SAndroid Build Coastguard Worker // Move the top bits to the right place.
799*9880d681SAndroid Build Coastguard Worker Hi = DAG.getNode(
800*9880d681SAndroid Build Coastguard Worker ISD::SHL, dl, Hi.getValueType(), Hi,
801*9880d681SAndroid Build Coastguard Worker DAG.getConstant(ExtraWidth, dl,
802*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(Hi.getValueType(), DL)));
803*9880d681SAndroid Build Coastguard Worker
804*9880d681SAndroid Build Coastguard Worker // Join the hi and lo parts.
805*9880d681SAndroid Build Coastguard Worker Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
806*9880d681SAndroid Build Coastguard Worker }
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker Chain = Ch;
809*9880d681SAndroid Build Coastguard Worker } else {
810*9880d681SAndroid Build Coastguard Worker bool isCustom = false;
811*9880d681SAndroid Build Coastguard Worker switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
812*9880d681SAndroid Build Coastguard Worker SrcVT.getSimpleVT())) {
813*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("This action is not supported yet!");
814*9880d681SAndroid Build Coastguard Worker case TargetLowering::Custom:
815*9880d681SAndroid Build Coastguard Worker isCustom = true;
816*9880d681SAndroid Build Coastguard Worker // FALLTHROUGH
817*9880d681SAndroid Build Coastguard Worker case TargetLowering::Legal: {
818*9880d681SAndroid Build Coastguard Worker Value = SDValue(Node, 0);
819*9880d681SAndroid Build Coastguard Worker Chain = SDValue(Node, 1);
820*9880d681SAndroid Build Coastguard Worker
821*9880d681SAndroid Build Coastguard Worker if (isCustom) {
822*9880d681SAndroid Build Coastguard Worker if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
823*9880d681SAndroid Build Coastguard Worker Value = Res;
824*9880d681SAndroid Build Coastguard Worker Chain = Res.getValue(1);
825*9880d681SAndroid Build Coastguard Worker }
826*9880d681SAndroid Build Coastguard Worker } else {
827*9880d681SAndroid Build Coastguard Worker // If this is an unaligned load and the target doesn't support it,
828*9880d681SAndroid Build Coastguard Worker // expand it.
829*9880d681SAndroid Build Coastguard Worker EVT MemVT = LD->getMemoryVT();
830*9880d681SAndroid Build Coastguard Worker unsigned AS = LD->getAddressSpace();
831*9880d681SAndroid Build Coastguard Worker unsigned Align = LD->getAlignment();
832*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = DAG.getDataLayout();
833*9880d681SAndroid Build Coastguard Worker if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, AS, Align)) {
834*9880d681SAndroid Build Coastguard Worker std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG);
835*9880d681SAndroid Build Coastguard Worker }
836*9880d681SAndroid Build Coastguard Worker }
837*9880d681SAndroid Build Coastguard Worker break;
838*9880d681SAndroid Build Coastguard Worker }
839*9880d681SAndroid Build Coastguard Worker case TargetLowering::Expand:
840*9880d681SAndroid Build Coastguard Worker EVT DestVT = Node->getValueType(0);
841*9880d681SAndroid Build Coastguard Worker if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
842*9880d681SAndroid Build Coastguard Worker // If the source type is not legal, see if there is a legal extload to
843*9880d681SAndroid Build Coastguard Worker // an intermediate type that we can then extend further.
844*9880d681SAndroid Build Coastguard Worker EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
845*9880d681SAndroid Build Coastguard Worker if (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
846*9880d681SAndroid Build Coastguard Worker TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT)) {
847*9880d681SAndroid Build Coastguard Worker // If we are loading a legal type, this is a non-extload followed by a
848*9880d681SAndroid Build Coastguard Worker // full extend.
849*9880d681SAndroid Build Coastguard Worker ISD::LoadExtType MidExtType =
850*9880d681SAndroid Build Coastguard Worker (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
851*9880d681SAndroid Build Coastguard Worker
852*9880d681SAndroid Build Coastguard Worker SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
853*9880d681SAndroid Build Coastguard Worker SrcVT, LD->getMemOperand());
854*9880d681SAndroid Build Coastguard Worker unsigned ExtendOp =
855*9880d681SAndroid Build Coastguard Worker ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType);
856*9880d681SAndroid Build Coastguard Worker Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
857*9880d681SAndroid Build Coastguard Worker Chain = Load.getValue(1);
858*9880d681SAndroid Build Coastguard Worker break;
859*9880d681SAndroid Build Coastguard Worker }
860*9880d681SAndroid Build Coastguard Worker
861*9880d681SAndroid Build Coastguard Worker // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
862*9880d681SAndroid Build Coastguard Worker // normal undefined upper bits behavior to allow using an in-reg extend
863*9880d681SAndroid Build Coastguard Worker // with the illegal FP type, so load as an integer and do the
864*9880d681SAndroid Build Coastguard Worker // from-integer conversion.
865*9880d681SAndroid Build Coastguard Worker if (SrcVT.getScalarType() == MVT::f16) {
866*9880d681SAndroid Build Coastguard Worker EVT ISrcVT = SrcVT.changeTypeToInteger();
867*9880d681SAndroid Build Coastguard Worker EVT IDestVT = DestVT.changeTypeToInteger();
868*9880d681SAndroid Build Coastguard Worker EVT LoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
869*9880d681SAndroid Build Coastguard Worker
870*9880d681SAndroid Build Coastguard Worker SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, LoadVT,
871*9880d681SAndroid Build Coastguard Worker Chain, Ptr, ISrcVT,
872*9880d681SAndroid Build Coastguard Worker LD->getMemOperand());
873*9880d681SAndroid Build Coastguard Worker Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result);
874*9880d681SAndroid Build Coastguard Worker Chain = Result.getValue(1);
875*9880d681SAndroid Build Coastguard Worker break;
876*9880d681SAndroid Build Coastguard Worker }
877*9880d681SAndroid Build Coastguard Worker }
878*9880d681SAndroid Build Coastguard Worker
879*9880d681SAndroid Build Coastguard Worker assert(!SrcVT.isVector() &&
880*9880d681SAndroid Build Coastguard Worker "Vector Loads are handled in LegalizeVectorOps");
881*9880d681SAndroid Build Coastguard Worker
882*9880d681SAndroid Build Coastguard Worker // FIXME: This does not work for vectors on most targets. Sign-
883*9880d681SAndroid Build Coastguard Worker // and zero-extend operations are currently folded into extending
884*9880d681SAndroid Build Coastguard Worker // loads, whether they are legal or not, and then we end up here
885*9880d681SAndroid Build Coastguard Worker // without any support for legalizing them.
886*9880d681SAndroid Build Coastguard Worker assert(ExtType != ISD::EXTLOAD &&
887*9880d681SAndroid Build Coastguard Worker "EXTLOAD should always be supported!");
888*9880d681SAndroid Build Coastguard Worker // Turn the unsupported load into an EXTLOAD followed by an
889*9880d681SAndroid Build Coastguard Worker // explicit zero/sign extend inreg.
890*9880d681SAndroid Build Coastguard Worker SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
891*9880d681SAndroid Build Coastguard Worker Node->getValueType(0),
892*9880d681SAndroid Build Coastguard Worker Chain, Ptr, SrcVT,
893*9880d681SAndroid Build Coastguard Worker LD->getMemOperand());
894*9880d681SAndroid Build Coastguard Worker SDValue ValRes;
895*9880d681SAndroid Build Coastguard Worker if (ExtType == ISD::SEXTLOAD)
896*9880d681SAndroid Build Coastguard Worker ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
897*9880d681SAndroid Build Coastguard Worker Result.getValueType(),
898*9880d681SAndroid Build Coastguard Worker Result, DAG.getValueType(SrcVT));
899*9880d681SAndroid Build Coastguard Worker else
900*9880d681SAndroid Build Coastguard Worker ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
901*9880d681SAndroid Build Coastguard Worker Value = ValRes;
902*9880d681SAndroid Build Coastguard Worker Chain = Result.getValue(1);
903*9880d681SAndroid Build Coastguard Worker break;
904*9880d681SAndroid Build Coastguard Worker }
905*9880d681SAndroid Build Coastguard Worker }
906*9880d681SAndroid Build Coastguard Worker
907*9880d681SAndroid Build Coastguard Worker // Since loads produce two values, make sure to remember that we legalized
908*9880d681SAndroid Build Coastguard Worker // both of them.
909*9880d681SAndroid Build Coastguard Worker if (Chain.getNode() != Node) {
910*9880d681SAndroid Build Coastguard Worker assert(Value.getNode() != Node && "Load must be completely replaced");
911*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
912*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
913*9880d681SAndroid Build Coastguard Worker if (UpdatedNodes) {
914*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(Value.getNode());
915*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(Chain.getNode());
916*9880d681SAndroid Build Coastguard Worker }
917*9880d681SAndroid Build Coastguard Worker ReplacedNode(Node);
918*9880d681SAndroid Build Coastguard Worker }
919*9880d681SAndroid Build Coastguard Worker }
920*9880d681SAndroid Build Coastguard Worker
921*9880d681SAndroid Build Coastguard Worker /// Return a legal replacement for the given operation, with all legal operands.
LegalizeOp(SDNode * Node)922*9880d681SAndroid Build Coastguard Worker void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
923*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
924*9880d681SAndroid Build Coastguard Worker
925*9880d681SAndroid Build Coastguard Worker if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
926*9880d681SAndroid Build Coastguard Worker return;
927*9880d681SAndroid Build Coastguard Worker
928*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
929*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
930*9880d681SAndroid Build Coastguard Worker assert((TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
931*9880d681SAndroid Build Coastguard Worker TargetLowering::TypeLegal ||
932*9880d681SAndroid Build Coastguard Worker TLI.isTypeLegal(Node->getValueType(i))) &&
933*9880d681SAndroid Build Coastguard Worker "Unexpected illegal type!");
934*9880d681SAndroid Build Coastguard Worker
935*9880d681SAndroid Build Coastguard Worker for (const SDValue &Op : Node->op_values())
936*9880d681SAndroid Build Coastguard Worker assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) ==
937*9880d681SAndroid Build Coastguard Worker TargetLowering::TypeLegal ||
938*9880d681SAndroid Build Coastguard Worker TLI.isTypeLegal(Op.getValueType()) ||
939*9880d681SAndroid Build Coastguard Worker Op.getOpcode() == ISD::TargetConstant) &&
940*9880d681SAndroid Build Coastguard Worker "Unexpected illegal type!");
941*9880d681SAndroid Build Coastguard Worker #endif
942*9880d681SAndroid Build Coastguard Worker
943*9880d681SAndroid Build Coastguard Worker // Figure out the correct action; the way to query this varies by opcode
944*9880d681SAndroid Build Coastguard Worker TargetLowering::LegalizeAction Action = TargetLowering::Legal;
945*9880d681SAndroid Build Coastguard Worker bool SimpleFinishLegalizing = true;
946*9880d681SAndroid Build Coastguard Worker switch (Node->getOpcode()) {
947*9880d681SAndroid Build Coastguard Worker case ISD::INTRINSIC_W_CHAIN:
948*9880d681SAndroid Build Coastguard Worker case ISD::INTRINSIC_WO_CHAIN:
949*9880d681SAndroid Build Coastguard Worker case ISD::INTRINSIC_VOID:
950*9880d681SAndroid Build Coastguard Worker case ISD::STACKSAVE:
951*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
952*9880d681SAndroid Build Coastguard Worker break;
953*9880d681SAndroid Build Coastguard Worker case ISD::GET_DYNAMIC_AREA_OFFSET:
954*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(),
955*9880d681SAndroid Build Coastguard Worker Node->getValueType(0));
956*9880d681SAndroid Build Coastguard Worker break;
957*9880d681SAndroid Build Coastguard Worker case ISD::VAARG:
958*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(),
959*9880d681SAndroid Build Coastguard Worker Node->getValueType(0));
960*9880d681SAndroid Build Coastguard Worker if (Action != TargetLowering::Promote)
961*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
962*9880d681SAndroid Build Coastguard Worker break;
963*9880d681SAndroid Build Coastguard Worker case ISD::FP_TO_FP16:
964*9880d681SAndroid Build Coastguard Worker case ISD::SINT_TO_FP:
965*9880d681SAndroid Build Coastguard Worker case ISD::UINT_TO_FP:
966*9880d681SAndroid Build Coastguard Worker case ISD::EXTRACT_VECTOR_ELT:
967*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(),
968*9880d681SAndroid Build Coastguard Worker Node->getOperand(0).getValueType());
969*9880d681SAndroid Build Coastguard Worker break;
970*9880d681SAndroid Build Coastguard Worker case ISD::FP_ROUND_INREG:
971*9880d681SAndroid Build Coastguard Worker case ISD::SIGN_EXTEND_INREG: {
972*9880d681SAndroid Build Coastguard Worker EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
973*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
974*9880d681SAndroid Build Coastguard Worker break;
975*9880d681SAndroid Build Coastguard Worker }
976*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_STORE: {
977*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(),
978*9880d681SAndroid Build Coastguard Worker Node->getOperand(2).getValueType());
979*9880d681SAndroid Build Coastguard Worker break;
980*9880d681SAndroid Build Coastguard Worker }
981*9880d681SAndroid Build Coastguard Worker case ISD::SELECT_CC:
982*9880d681SAndroid Build Coastguard Worker case ISD::SETCC:
983*9880d681SAndroid Build Coastguard Worker case ISD::BR_CC: {
984*9880d681SAndroid Build Coastguard Worker unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
985*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::SETCC ? 2 :
986*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::SETCCE ? 3 : 1;
987*9880d681SAndroid Build Coastguard Worker unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
988*9880d681SAndroid Build Coastguard Worker MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
989*9880d681SAndroid Build Coastguard Worker ISD::CondCode CCCode =
990*9880d681SAndroid Build Coastguard Worker cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
991*9880d681SAndroid Build Coastguard Worker Action = TLI.getCondCodeAction(CCCode, OpVT);
992*9880d681SAndroid Build Coastguard Worker if (Action == TargetLowering::Legal) {
993*9880d681SAndroid Build Coastguard Worker if (Node->getOpcode() == ISD::SELECT_CC)
994*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(),
995*9880d681SAndroid Build Coastguard Worker Node->getValueType(0));
996*9880d681SAndroid Build Coastguard Worker else
997*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
998*9880d681SAndroid Build Coastguard Worker }
999*9880d681SAndroid Build Coastguard Worker break;
1000*9880d681SAndroid Build Coastguard Worker }
1001*9880d681SAndroid Build Coastguard Worker case ISD::LOAD:
1002*9880d681SAndroid Build Coastguard Worker case ISD::STORE:
1003*9880d681SAndroid Build Coastguard Worker // FIXME: Model these properly. LOAD and STORE are complicated, and
1004*9880d681SAndroid Build Coastguard Worker // STORE expects the unlegalized operand in some cases.
1005*9880d681SAndroid Build Coastguard Worker SimpleFinishLegalizing = false;
1006*9880d681SAndroid Build Coastguard Worker break;
1007*9880d681SAndroid Build Coastguard Worker case ISD::CALLSEQ_START:
1008*9880d681SAndroid Build Coastguard Worker case ISD::CALLSEQ_END:
1009*9880d681SAndroid Build Coastguard Worker // FIXME: This shouldn't be necessary. These nodes have special properties
1010*9880d681SAndroid Build Coastguard Worker // dealing with the recursive nature of legalization. Removing this
1011*9880d681SAndroid Build Coastguard Worker // special case should be done as part of making LegalizeDAG non-recursive.
1012*9880d681SAndroid Build Coastguard Worker SimpleFinishLegalizing = false;
1013*9880d681SAndroid Build Coastguard Worker break;
1014*9880d681SAndroid Build Coastguard Worker case ISD::EXTRACT_ELEMENT:
1015*9880d681SAndroid Build Coastguard Worker case ISD::FLT_ROUNDS_:
1016*9880d681SAndroid Build Coastguard Worker case ISD::FPOWI:
1017*9880d681SAndroid Build Coastguard Worker case ISD::MERGE_VALUES:
1018*9880d681SAndroid Build Coastguard Worker case ISD::EH_RETURN:
1019*9880d681SAndroid Build Coastguard Worker case ISD::FRAME_TO_ARGS_OFFSET:
1020*9880d681SAndroid Build Coastguard Worker case ISD::EH_SJLJ_SETJMP:
1021*9880d681SAndroid Build Coastguard Worker case ISD::EH_SJLJ_LONGJMP:
1022*9880d681SAndroid Build Coastguard Worker case ISD::EH_SJLJ_SETUP_DISPATCH:
1023*9880d681SAndroid Build Coastguard Worker // These operations lie about being legal: when they claim to be legal,
1024*9880d681SAndroid Build Coastguard Worker // they should actually be expanded.
1025*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1026*9880d681SAndroid Build Coastguard Worker if (Action == TargetLowering::Legal)
1027*9880d681SAndroid Build Coastguard Worker Action = TargetLowering::Expand;
1028*9880d681SAndroid Build Coastguard Worker break;
1029*9880d681SAndroid Build Coastguard Worker case ISD::INIT_TRAMPOLINE:
1030*9880d681SAndroid Build Coastguard Worker case ISD::ADJUST_TRAMPOLINE:
1031*9880d681SAndroid Build Coastguard Worker case ISD::FRAMEADDR:
1032*9880d681SAndroid Build Coastguard Worker case ISD::RETURNADDR:
1033*9880d681SAndroid Build Coastguard Worker // These operations lie about being legal: when they claim to be legal,
1034*9880d681SAndroid Build Coastguard Worker // they should actually be custom-lowered.
1035*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1036*9880d681SAndroid Build Coastguard Worker if (Action == TargetLowering::Legal)
1037*9880d681SAndroid Build Coastguard Worker Action = TargetLowering::Custom;
1038*9880d681SAndroid Build Coastguard Worker break;
1039*9880d681SAndroid Build Coastguard Worker case ISD::READCYCLECOUNTER:
1040*9880d681SAndroid Build Coastguard Worker // READCYCLECOUNTER returns an i64, even if type legalization might have
1041*9880d681SAndroid Build Coastguard Worker // expanded that to several smaller types.
1042*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
1043*9880d681SAndroid Build Coastguard Worker break;
1044*9880d681SAndroid Build Coastguard Worker case ISD::READ_REGISTER:
1045*9880d681SAndroid Build Coastguard Worker case ISD::WRITE_REGISTER:
1046*9880d681SAndroid Build Coastguard Worker // Named register is legal in the DAG, but blocked by register name
1047*9880d681SAndroid Build Coastguard Worker // selection if not implemented by target (to chose the correct register)
1048*9880d681SAndroid Build Coastguard Worker // They'll be converted to Copy(To/From)Reg.
1049*9880d681SAndroid Build Coastguard Worker Action = TargetLowering::Legal;
1050*9880d681SAndroid Build Coastguard Worker break;
1051*9880d681SAndroid Build Coastguard Worker case ISD::DEBUGTRAP:
1052*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1053*9880d681SAndroid Build Coastguard Worker if (Action == TargetLowering::Expand) {
1054*9880d681SAndroid Build Coastguard Worker // replace ISD::DEBUGTRAP with ISD::TRAP
1055*9880d681SAndroid Build Coastguard Worker SDValue NewVal;
1056*9880d681SAndroid Build Coastguard Worker NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1057*9880d681SAndroid Build Coastguard Worker Node->getOperand(0));
1058*9880d681SAndroid Build Coastguard Worker ReplaceNode(Node, NewVal.getNode());
1059*9880d681SAndroid Build Coastguard Worker LegalizeOp(NewVal.getNode());
1060*9880d681SAndroid Build Coastguard Worker return;
1061*9880d681SAndroid Build Coastguard Worker }
1062*9880d681SAndroid Build Coastguard Worker break;
1063*9880d681SAndroid Build Coastguard Worker
1064*9880d681SAndroid Build Coastguard Worker default:
1065*9880d681SAndroid Build Coastguard Worker if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1066*9880d681SAndroid Build Coastguard Worker Action = TargetLowering::Legal;
1067*9880d681SAndroid Build Coastguard Worker } else {
1068*9880d681SAndroid Build Coastguard Worker Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1069*9880d681SAndroid Build Coastguard Worker }
1070*9880d681SAndroid Build Coastguard Worker break;
1071*9880d681SAndroid Build Coastguard Worker }
1072*9880d681SAndroid Build Coastguard Worker
1073*9880d681SAndroid Build Coastguard Worker if (SimpleFinishLegalizing) {
1074*9880d681SAndroid Build Coastguard Worker SDNode *NewNode = Node;
1075*9880d681SAndroid Build Coastguard Worker switch (Node->getOpcode()) {
1076*9880d681SAndroid Build Coastguard Worker default: break;
1077*9880d681SAndroid Build Coastguard Worker case ISD::SHL:
1078*9880d681SAndroid Build Coastguard Worker case ISD::SRL:
1079*9880d681SAndroid Build Coastguard Worker case ISD::SRA:
1080*9880d681SAndroid Build Coastguard Worker case ISD::ROTL:
1081*9880d681SAndroid Build Coastguard Worker case ISD::ROTR:
1082*9880d681SAndroid Build Coastguard Worker // Legalizing shifts/rotates requires adjusting the shift amount
1083*9880d681SAndroid Build Coastguard Worker // to the appropriate width.
1084*9880d681SAndroid Build Coastguard Worker if (!Node->getOperand(1).getValueType().isVector()) {
1085*9880d681SAndroid Build Coastguard Worker SDValue SAO =
1086*9880d681SAndroid Build Coastguard Worker DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
1087*9880d681SAndroid Build Coastguard Worker Node->getOperand(1));
1088*9880d681SAndroid Build Coastguard Worker HandleSDNode Handle(SAO);
1089*9880d681SAndroid Build Coastguard Worker LegalizeOp(SAO.getNode());
1090*9880d681SAndroid Build Coastguard Worker NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
1091*9880d681SAndroid Build Coastguard Worker Handle.getValue());
1092*9880d681SAndroid Build Coastguard Worker }
1093*9880d681SAndroid Build Coastguard Worker break;
1094*9880d681SAndroid Build Coastguard Worker case ISD::SRL_PARTS:
1095*9880d681SAndroid Build Coastguard Worker case ISD::SRA_PARTS:
1096*9880d681SAndroid Build Coastguard Worker case ISD::SHL_PARTS:
1097*9880d681SAndroid Build Coastguard Worker // Legalizing shifts/rotates requires adjusting the shift amount
1098*9880d681SAndroid Build Coastguard Worker // to the appropriate width.
1099*9880d681SAndroid Build Coastguard Worker if (!Node->getOperand(2).getValueType().isVector()) {
1100*9880d681SAndroid Build Coastguard Worker SDValue SAO =
1101*9880d681SAndroid Build Coastguard Worker DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
1102*9880d681SAndroid Build Coastguard Worker Node->getOperand(2));
1103*9880d681SAndroid Build Coastguard Worker HandleSDNode Handle(SAO);
1104*9880d681SAndroid Build Coastguard Worker LegalizeOp(SAO.getNode());
1105*9880d681SAndroid Build Coastguard Worker NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
1106*9880d681SAndroid Build Coastguard Worker Node->getOperand(1),
1107*9880d681SAndroid Build Coastguard Worker Handle.getValue());
1108*9880d681SAndroid Build Coastguard Worker }
1109*9880d681SAndroid Build Coastguard Worker break;
1110*9880d681SAndroid Build Coastguard Worker }
1111*9880d681SAndroid Build Coastguard Worker
1112*9880d681SAndroid Build Coastguard Worker if (NewNode != Node) {
1113*9880d681SAndroid Build Coastguard Worker ReplaceNode(Node, NewNode);
1114*9880d681SAndroid Build Coastguard Worker Node = NewNode;
1115*9880d681SAndroid Build Coastguard Worker }
1116*9880d681SAndroid Build Coastguard Worker switch (Action) {
1117*9880d681SAndroid Build Coastguard Worker case TargetLowering::Legal:
1118*9880d681SAndroid Build Coastguard Worker return;
1119*9880d681SAndroid Build Coastguard Worker case TargetLowering::Custom: {
1120*9880d681SAndroid Build Coastguard Worker // FIXME: The handling for custom lowering with multiple results is
1121*9880d681SAndroid Build Coastguard Worker // a complete mess.
1122*9880d681SAndroid Build Coastguard Worker if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
1123*9880d681SAndroid Build Coastguard Worker if (!(Res.getNode() != Node || Res.getResNo() != 0))
1124*9880d681SAndroid Build Coastguard Worker return;
1125*9880d681SAndroid Build Coastguard Worker
1126*9880d681SAndroid Build Coastguard Worker if (Node->getNumValues() == 1) {
1127*9880d681SAndroid Build Coastguard Worker // We can just directly replace this node with the lowered value.
1128*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(Node, 0), Res);
1129*9880d681SAndroid Build Coastguard Worker return;
1130*9880d681SAndroid Build Coastguard Worker }
1131*9880d681SAndroid Build Coastguard Worker
1132*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> ResultVals;
1133*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1134*9880d681SAndroid Build Coastguard Worker ResultVals.push_back(Res.getValue(i));
1135*9880d681SAndroid Build Coastguard Worker ReplaceNode(Node, ResultVals.data());
1136*9880d681SAndroid Build Coastguard Worker return;
1137*9880d681SAndroid Build Coastguard Worker }
1138*9880d681SAndroid Build Coastguard Worker }
1139*9880d681SAndroid Build Coastguard Worker // FALL THROUGH
1140*9880d681SAndroid Build Coastguard Worker case TargetLowering::Expand:
1141*9880d681SAndroid Build Coastguard Worker if (ExpandNode(Node))
1142*9880d681SAndroid Build Coastguard Worker return;
1143*9880d681SAndroid Build Coastguard Worker // FALL THROUGH
1144*9880d681SAndroid Build Coastguard Worker case TargetLowering::LibCall:
1145*9880d681SAndroid Build Coastguard Worker ConvertNodeToLibcall(Node);
1146*9880d681SAndroid Build Coastguard Worker return;
1147*9880d681SAndroid Build Coastguard Worker case TargetLowering::Promote:
1148*9880d681SAndroid Build Coastguard Worker PromoteNode(Node);
1149*9880d681SAndroid Build Coastguard Worker return;
1150*9880d681SAndroid Build Coastguard Worker }
1151*9880d681SAndroid Build Coastguard Worker }
1152*9880d681SAndroid Build Coastguard Worker
1153*9880d681SAndroid Build Coastguard Worker switch (Node->getOpcode()) {
1154*9880d681SAndroid Build Coastguard Worker default:
1155*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
1156*9880d681SAndroid Build Coastguard Worker dbgs() << "NODE: ";
1157*9880d681SAndroid Build Coastguard Worker Node->dump( &DAG);
1158*9880d681SAndroid Build Coastguard Worker dbgs() << "\n";
1159*9880d681SAndroid Build Coastguard Worker #endif
1160*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Do not know how to legalize this operator!");
1161*9880d681SAndroid Build Coastguard Worker
1162*9880d681SAndroid Build Coastguard Worker case ISD::CALLSEQ_START:
1163*9880d681SAndroid Build Coastguard Worker case ISD::CALLSEQ_END:
1164*9880d681SAndroid Build Coastguard Worker break;
1165*9880d681SAndroid Build Coastguard Worker case ISD::LOAD: {
1166*9880d681SAndroid Build Coastguard Worker return LegalizeLoadOps(Node);
1167*9880d681SAndroid Build Coastguard Worker }
1168*9880d681SAndroid Build Coastguard Worker case ISD::STORE: {
1169*9880d681SAndroid Build Coastguard Worker return LegalizeStoreOps(Node);
1170*9880d681SAndroid Build Coastguard Worker }
1171*9880d681SAndroid Build Coastguard Worker }
1172*9880d681SAndroid Build Coastguard Worker }
1173*9880d681SAndroid Build Coastguard Worker
ExpandExtractFromVectorThroughStack(SDValue Op)1174*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1175*9880d681SAndroid Build Coastguard Worker SDValue Vec = Op.getOperand(0);
1176*9880d681SAndroid Build Coastguard Worker SDValue Idx = Op.getOperand(1);
1177*9880d681SAndroid Build Coastguard Worker SDLoc dl(Op);
1178*9880d681SAndroid Build Coastguard Worker
1179*9880d681SAndroid Build Coastguard Worker // Before we generate a new store to a temporary stack slot, see if there is
1180*9880d681SAndroid Build Coastguard Worker // already one that we can use. There often is because when we scalarize
1181*9880d681SAndroid Build Coastguard Worker // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
1182*9880d681SAndroid Build Coastguard Worker // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
1183*9880d681SAndroid Build Coastguard Worker // the vector. If all are expanded here, we don't want one store per vector
1184*9880d681SAndroid Build Coastguard Worker // element.
1185*9880d681SAndroid Build Coastguard Worker
1186*9880d681SAndroid Build Coastguard Worker // Caches for hasPredecessorHelper
1187*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const SDNode *, 32> Visited;
1188*9880d681SAndroid Build Coastguard Worker SmallVector<const SDNode *, 16> Worklist;
1189*9880d681SAndroid Build Coastguard Worker Worklist.push_back(Idx.getNode());
1190*9880d681SAndroid Build Coastguard Worker SDValue StackPtr, Ch;
1191*9880d681SAndroid Build Coastguard Worker for (SDNode::use_iterator UI = Vec.getNode()->use_begin(),
1192*9880d681SAndroid Build Coastguard Worker UE = Vec.getNode()->use_end(); UI != UE; ++UI) {
1193*9880d681SAndroid Build Coastguard Worker SDNode *User = *UI;
1194*9880d681SAndroid Build Coastguard Worker if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
1195*9880d681SAndroid Build Coastguard Worker if (ST->isIndexed() || ST->isTruncatingStore() ||
1196*9880d681SAndroid Build Coastguard Worker ST->getValue() != Vec)
1197*9880d681SAndroid Build Coastguard Worker continue;
1198*9880d681SAndroid Build Coastguard Worker
1199*9880d681SAndroid Build Coastguard Worker // Make sure that nothing else could have stored into the destination of
1200*9880d681SAndroid Build Coastguard Worker // this store.
1201*9880d681SAndroid Build Coastguard Worker if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
1202*9880d681SAndroid Build Coastguard Worker continue;
1203*9880d681SAndroid Build Coastguard Worker
1204*9880d681SAndroid Build Coastguard Worker // If the index is dependent on the store we will introduce a cycle when
1205*9880d681SAndroid Build Coastguard Worker // creating the load (the load uses the index, and by replacing the chain
1206*9880d681SAndroid Build Coastguard Worker // we will make the index dependent on the load).
1207*9880d681SAndroid Build Coastguard Worker if (SDNode::hasPredecessorHelper(ST, Visited, Worklist))
1208*9880d681SAndroid Build Coastguard Worker continue;
1209*9880d681SAndroid Build Coastguard Worker
1210*9880d681SAndroid Build Coastguard Worker StackPtr = ST->getBasePtr();
1211*9880d681SAndroid Build Coastguard Worker Ch = SDValue(ST, 0);
1212*9880d681SAndroid Build Coastguard Worker break;
1213*9880d681SAndroid Build Coastguard Worker }
1214*9880d681SAndroid Build Coastguard Worker }
1215*9880d681SAndroid Build Coastguard Worker
1216*9880d681SAndroid Build Coastguard Worker if (!Ch.getNode()) {
1217*9880d681SAndroid Build Coastguard Worker // Store the value to a temporary stack slot, then LOAD the returned part.
1218*9880d681SAndroid Build Coastguard Worker StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1219*9880d681SAndroid Build Coastguard Worker Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1220*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(), false, false, 0);
1221*9880d681SAndroid Build Coastguard Worker }
1222*9880d681SAndroid Build Coastguard Worker
1223*9880d681SAndroid Build Coastguard Worker // Add the offset to the index.
1224*9880d681SAndroid Build Coastguard Worker unsigned EltSize =
1225*9880d681SAndroid Build Coastguard Worker Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1226*9880d681SAndroid Build Coastguard Worker Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1227*9880d681SAndroid Build Coastguard Worker DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType()));
1228*9880d681SAndroid Build Coastguard Worker
1229*9880d681SAndroid Build Coastguard Worker Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout()));
1230*9880d681SAndroid Build Coastguard Worker StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1231*9880d681SAndroid Build Coastguard Worker
1232*9880d681SAndroid Build Coastguard Worker SDValue NewLoad;
1233*9880d681SAndroid Build Coastguard Worker
1234*9880d681SAndroid Build Coastguard Worker if (Op.getValueType().isVector())
1235*9880d681SAndroid Build Coastguard Worker NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1236*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(), false, false, false, 0);
1237*9880d681SAndroid Build Coastguard Worker else
1238*9880d681SAndroid Build Coastguard Worker NewLoad = DAG.getExtLoad(
1239*9880d681SAndroid Build Coastguard Worker ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, MachinePointerInfo(),
1240*9880d681SAndroid Build Coastguard Worker Vec.getValueType().getVectorElementType(), false, false, false, 0);
1241*9880d681SAndroid Build Coastguard Worker
1242*9880d681SAndroid Build Coastguard Worker // Replace the chain going out of the store, by the one out of the load.
1243*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
1244*9880d681SAndroid Build Coastguard Worker
1245*9880d681SAndroid Build Coastguard Worker // We introduced a cycle though, so update the loads operands, making sure
1246*9880d681SAndroid Build Coastguard Worker // to use the original store's chain as an incoming chain.
1247*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(),
1248*9880d681SAndroid Build Coastguard Worker NewLoad->op_end());
1249*9880d681SAndroid Build Coastguard Worker NewLoadOperands[0] = Ch;
1250*9880d681SAndroid Build Coastguard Worker NewLoad =
1251*9880d681SAndroid Build Coastguard Worker SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
1252*9880d681SAndroid Build Coastguard Worker return NewLoad;
1253*9880d681SAndroid Build Coastguard Worker }
1254*9880d681SAndroid Build Coastguard Worker
ExpandInsertToVectorThroughStack(SDValue Op)1255*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1256*9880d681SAndroid Build Coastguard Worker assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1257*9880d681SAndroid Build Coastguard Worker
1258*9880d681SAndroid Build Coastguard Worker SDValue Vec = Op.getOperand(0);
1259*9880d681SAndroid Build Coastguard Worker SDValue Part = Op.getOperand(1);
1260*9880d681SAndroid Build Coastguard Worker SDValue Idx = Op.getOperand(2);
1261*9880d681SAndroid Build Coastguard Worker SDLoc dl(Op);
1262*9880d681SAndroid Build Coastguard Worker
1263*9880d681SAndroid Build Coastguard Worker // Store the value to a temporary stack slot, then LOAD the returned part.
1264*9880d681SAndroid Build Coastguard Worker
1265*9880d681SAndroid Build Coastguard Worker SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1266*9880d681SAndroid Build Coastguard Worker int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1267*9880d681SAndroid Build Coastguard Worker MachinePointerInfo PtrInfo =
1268*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
1269*9880d681SAndroid Build Coastguard Worker
1270*9880d681SAndroid Build Coastguard Worker // First store the whole vector.
1271*9880d681SAndroid Build Coastguard Worker SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1272*9880d681SAndroid Build Coastguard Worker false, false, 0);
1273*9880d681SAndroid Build Coastguard Worker
1274*9880d681SAndroid Build Coastguard Worker // Then store the inserted part.
1275*9880d681SAndroid Build Coastguard Worker
1276*9880d681SAndroid Build Coastguard Worker // Add the offset to the index.
1277*9880d681SAndroid Build Coastguard Worker unsigned EltSize =
1278*9880d681SAndroid Build Coastguard Worker Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1279*9880d681SAndroid Build Coastguard Worker
1280*9880d681SAndroid Build Coastguard Worker Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1281*9880d681SAndroid Build Coastguard Worker DAG.getConstant(EltSize, SDLoc(Vec), Idx.getValueType()));
1282*9880d681SAndroid Build Coastguard Worker Idx = DAG.getZExtOrTrunc(Idx, dl, TLI.getPointerTy(DAG.getDataLayout()));
1283*9880d681SAndroid Build Coastguard Worker
1284*9880d681SAndroid Build Coastguard Worker SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1285*9880d681SAndroid Build Coastguard Worker StackPtr);
1286*9880d681SAndroid Build Coastguard Worker
1287*9880d681SAndroid Build Coastguard Worker // Store the subvector.
1288*9880d681SAndroid Build Coastguard Worker Ch = DAG.getStore(Ch, dl, Part, SubStackPtr,
1289*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(), false, false, 0);
1290*9880d681SAndroid Build Coastguard Worker
1291*9880d681SAndroid Build Coastguard Worker // Finally, load the updated vector.
1292*9880d681SAndroid Build Coastguard Worker return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1293*9880d681SAndroid Build Coastguard Worker false, false, false, 0);
1294*9880d681SAndroid Build Coastguard Worker }
1295*9880d681SAndroid Build Coastguard Worker
ExpandVectorBuildThroughStack(SDNode * Node)1296*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1297*9880d681SAndroid Build Coastguard Worker // We can't handle this case efficiently. Allocate a sufficiently
1298*9880d681SAndroid Build Coastguard Worker // aligned object on the stack, store each element into it, then load
1299*9880d681SAndroid Build Coastguard Worker // the result as a vector.
1300*9880d681SAndroid Build Coastguard Worker // Create the stack frame object.
1301*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
1302*9880d681SAndroid Build Coastguard Worker EVT EltVT = VT.getVectorElementType();
1303*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
1304*9880d681SAndroid Build Coastguard Worker SDValue FIPtr = DAG.CreateStackTemporary(VT);
1305*9880d681SAndroid Build Coastguard Worker int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
1306*9880d681SAndroid Build Coastguard Worker MachinePointerInfo PtrInfo =
1307*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
1308*9880d681SAndroid Build Coastguard Worker
1309*9880d681SAndroid Build Coastguard Worker // Emit a store of each element to the stack slot.
1310*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> Stores;
1311*9880d681SAndroid Build Coastguard Worker unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
1312*9880d681SAndroid Build Coastguard Worker // Store (in the right endianness) the elements to memory.
1313*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1314*9880d681SAndroid Build Coastguard Worker // Ignore undef elements.
1315*9880d681SAndroid Build Coastguard Worker if (Node->getOperand(i).isUndef()) continue;
1316*9880d681SAndroid Build Coastguard Worker
1317*9880d681SAndroid Build Coastguard Worker unsigned Offset = TypeByteSize*i;
1318*9880d681SAndroid Build Coastguard Worker
1319*9880d681SAndroid Build Coastguard Worker SDValue Idx = DAG.getConstant(Offset, dl, FIPtr.getValueType());
1320*9880d681SAndroid Build Coastguard Worker Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1321*9880d681SAndroid Build Coastguard Worker
1322*9880d681SAndroid Build Coastguard Worker // If the destination vector element type is narrower than the source
1323*9880d681SAndroid Build Coastguard Worker // element type, only store the bits necessary.
1324*9880d681SAndroid Build Coastguard Worker if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
1325*9880d681SAndroid Build Coastguard Worker Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
1326*9880d681SAndroid Build Coastguard Worker Node->getOperand(i), Idx,
1327*9880d681SAndroid Build Coastguard Worker PtrInfo.getWithOffset(Offset),
1328*9880d681SAndroid Build Coastguard Worker EltVT, false, false, 0));
1329*9880d681SAndroid Build Coastguard Worker } else
1330*9880d681SAndroid Build Coastguard Worker Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
1331*9880d681SAndroid Build Coastguard Worker Node->getOperand(i), Idx,
1332*9880d681SAndroid Build Coastguard Worker PtrInfo.getWithOffset(Offset),
1333*9880d681SAndroid Build Coastguard Worker false, false, 0));
1334*9880d681SAndroid Build Coastguard Worker }
1335*9880d681SAndroid Build Coastguard Worker
1336*9880d681SAndroid Build Coastguard Worker SDValue StoreChain;
1337*9880d681SAndroid Build Coastguard Worker if (!Stores.empty()) // Not all undef elements?
1338*9880d681SAndroid Build Coastguard Worker StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
1339*9880d681SAndroid Build Coastguard Worker else
1340*9880d681SAndroid Build Coastguard Worker StoreChain = DAG.getEntryNode();
1341*9880d681SAndroid Build Coastguard Worker
1342*9880d681SAndroid Build Coastguard Worker // Result is a load from the stack slot.
1343*9880d681SAndroid Build Coastguard Worker return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo,
1344*9880d681SAndroid Build Coastguard Worker false, false, false, 0);
1345*9880d681SAndroid Build Coastguard Worker }
1346*9880d681SAndroid Build Coastguard Worker
1347*9880d681SAndroid Build Coastguard Worker namespace {
1348*9880d681SAndroid Build Coastguard Worker /// Keeps track of state when getting the sign of a floating-point value as an
1349*9880d681SAndroid Build Coastguard Worker /// integer.
1350*9880d681SAndroid Build Coastguard Worker struct FloatSignAsInt {
1351*9880d681SAndroid Build Coastguard Worker EVT FloatVT;
1352*9880d681SAndroid Build Coastguard Worker SDValue Chain;
1353*9880d681SAndroid Build Coastguard Worker SDValue FloatPtr;
1354*9880d681SAndroid Build Coastguard Worker SDValue IntPtr;
1355*9880d681SAndroid Build Coastguard Worker MachinePointerInfo IntPointerInfo;
1356*9880d681SAndroid Build Coastguard Worker MachinePointerInfo FloatPointerInfo;
1357*9880d681SAndroid Build Coastguard Worker SDValue IntValue;
1358*9880d681SAndroid Build Coastguard Worker APInt SignMask;
1359*9880d681SAndroid Build Coastguard Worker uint8_t SignBit;
1360*9880d681SAndroid Build Coastguard Worker };
1361*9880d681SAndroid Build Coastguard Worker }
1362*9880d681SAndroid Build Coastguard Worker
1363*9880d681SAndroid Build Coastguard Worker /// Bitcast a floating-point value to an integer value. Only bitcast the part
1364*9880d681SAndroid Build Coastguard Worker /// containing the sign bit if the target has no integer value capable of
1365*9880d681SAndroid Build Coastguard Worker /// holding all bits of the floating-point value.
getSignAsIntValue(FloatSignAsInt & State,const SDLoc & DL,SDValue Value) const1366*9880d681SAndroid Build Coastguard Worker void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
1367*9880d681SAndroid Build Coastguard Worker const SDLoc &DL,
1368*9880d681SAndroid Build Coastguard Worker SDValue Value) const {
1369*9880d681SAndroid Build Coastguard Worker EVT FloatVT = Value.getValueType();
1370*9880d681SAndroid Build Coastguard Worker unsigned NumBits = FloatVT.getSizeInBits();
1371*9880d681SAndroid Build Coastguard Worker State.FloatVT = FloatVT;
1372*9880d681SAndroid Build Coastguard Worker EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
1373*9880d681SAndroid Build Coastguard Worker // Convert to an integer of the same size.
1374*9880d681SAndroid Build Coastguard Worker if (TLI.isTypeLegal(IVT)) {
1375*9880d681SAndroid Build Coastguard Worker State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
1376*9880d681SAndroid Build Coastguard Worker State.SignMask = APInt::getSignBit(NumBits);
1377*9880d681SAndroid Build Coastguard Worker State.SignBit = NumBits - 1;
1378*9880d681SAndroid Build Coastguard Worker return;
1379*9880d681SAndroid Build Coastguard Worker }
1380*9880d681SAndroid Build Coastguard Worker
1381*9880d681SAndroid Build Coastguard Worker auto &DataLayout = DAG.getDataLayout();
1382*9880d681SAndroid Build Coastguard Worker // Store the float to memory, then load the sign part out as an integer.
1383*9880d681SAndroid Build Coastguard Worker MVT LoadTy = TLI.getRegisterType(*DAG.getContext(), MVT::i8);
1384*9880d681SAndroid Build Coastguard Worker // First create a temporary that is aligned for both the load and store.
1385*9880d681SAndroid Build Coastguard Worker SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1386*9880d681SAndroid Build Coastguard Worker int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1387*9880d681SAndroid Build Coastguard Worker // Then store the float to it.
1388*9880d681SAndroid Build Coastguard Worker State.FloatPtr = StackPtr;
1389*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = DAG.getMachineFunction();
1390*9880d681SAndroid Build Coastguard Worker State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
1391*9880d681SAndroid Build Coastguard Worker State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
1392*9880d681SAndroid Build Coastguard Worker State.FloatPointerInfo, false, false, 0);
1393*9880d681SAndroid Build Coastguard Worker
1394*9880d681SAndroid Build Coastguard Worker SDValue IntPtr;
1395*9880d681SAndroid Build Coastguard Worker if (DataLayout.isBigEndian()) {
1396*9880d681SAndroid Build Coastguard Worker assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1397*9880d681SAndroid Build Coastguard Worker // Load out a legal integer with the same sign bit as the float.
1398*9880d681SAndroid Build Coastguard Worker IntPtr = StackPtr;
1399*9880d681SAndroid Build Coastguard Worker State.IntPointerInfo = State.FloatPointerInfo;
1400*9880d681SAndroid Build Coastguard Worker } else {
1401*9880d681SAndroid Build Coastguard Worker // Advance the pointer so that the loaded byte will contain the sign bit.
1402*9880d681SAndroid Build Coastguard Worker unsigned ByteOffset = (FloatVT.getSizeInBits() / 8) - 1;
1403*9880d681SAndroid Build Coastguard Worker IntPtr = DAG.getNode(ISD::ADD, DL, StackPtr.getValueType(), StackPtr,
1404*9880d681SAndroid Build Coastguard Worker DAG.getConstant(ByteOffset, DL, StackPtr.getValueType()));
1405*9880d681SAndroid Build Coastguard Worker State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
1406*9880d681SAndroid Build Coastguard Worker ByteOffset);
1407*9880d681SAndroid Build Coastguard Worker }
1408*9880d681SAndroid Build Coastguard Worker
1409*9880d681SAndroid Build Coastguard Worker State.IntPtr = IntPtr;
1410*9880d681SAndroid Build Coastguard Worker State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain,
1411*9880d681SAndroid Build Coastguard Worker IntPtr, State.IntPointerInfo, MVT::i8,
1412*9880d681SAndroid Build Coastguard Worker false, false, false, 0);
1413*9880d681SAndroid Build Coastguard Worker State.SignMask = APInt::getOneBitSet(LoadTy.getSizeInBits(), 7);
1414*9880d681SAndroid Build Coastguard Worker State.SignBit = 7;
1415*9880d681SAndroid Build Coastguard Worker }
1416*9880d681SAndroid Build Coastguard Worker
1417*9880d681SAndroid Build Coastguard Worker /// Replace the integer value produced by getSignAsIntValue() with a new value
1418*9880d681SAndroid Build Coastguard Worker /// and cast the result back to a floating-point type.
modifySignAsInt(const FloatSignAsInt & State,const SDLoc & DL,SDValue NewIntValue) const1419*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
1420*9880d681SAndroid Build Coastguard Worker const SDLoc &DL,
1421*9880d681SAndroid Build Coastguard Worker SDValue NewIntValue) const {
1422*9880d681SAndroid Build Coastguard Worker if (!State.Chain)
1423*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
1424*9880d681SAndroid Build Coastguard Worker
1425*9880d681SAndroid Build Coastguard Worker // Override the part containing the sign bit in the value stored on the stack.
1426*9880d681SAndroid Build Coastguard Worker SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
1427*9880d681SAndroid Build Coastguard Worker State.IntPointerInfo, MVT::i8, false, false,
1428*9880d681SAndroid Build Coastguard Worker 0);
1429*9880d681SAndroid Build Coastguard Worker return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
1430*9880d681SAndroid Build Coastguard Worker State.FloatPointerInfo, false, false, false, 0);
1431*9880d681SAndroid Build Coastguard Worker }
1432*9880d681SAndroid Build Coastguard Worker
ExpandFCOPYSIGN(SDNode * Node) const1433*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
1434*9880d681SAndroid Build Coastguard Worker SDLoc DL(Node);
1435*9880d681SAndroid Build Coastguard Worker SDValue Mag = Node->getOperand(0);
1436*9880d681SAndroid Build Coastguard Worker SDValue Sign = Node->getOperand(1);
1437*9880d681SAndroid Build Coastguard Worker
1438*9880d681SAndroid Build Coastguard Worker // Get sign bit into an integer value.
1439*9880d681SAndroid Build Coastguard Worker FloatSignAsInt SignAsInt;
1440*9880d681SAndroid Build Coastguard Worker getSignAsIntValue(SignAsInt, DL, Sign);
1441*9880d681SAndroid Build Coastguard Worker
1442*9880d681SAndroid Build Coastguard Worker EVT IntVT = SignAsInt.IntValue.getValueType();
1443*9880d681SAndroid Build Coastguard Worker SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1444*9880d681SAndroid Build Coastguard Worker SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
1445*9880d681SAndroid Build Coastguard Worker SignMask);
1446*9880d681SAndroid Build Coastguard Worker
1447*9880d681SAndroid Build Coastguard Worker // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X)
1448*9880d681SAndroid Build Coastguard Worker EVT FloatVT = Mag.getValueType();
1449*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
1450*9880d681SAndroid Build Coastguard Worker TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
1451*9880d681SAndroid Build Coastguard Worker SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
1452*9880d681SAndroid Build Coastguard Worker SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
1453*9880d681SAndroid Build Coastguard Worker SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
1454*9880d681SAndroid Build Coastguard Worker DAG.getConstant(0, DL, IntVT), ISD::SETNE);
1455*9880d681SAndroid Build Coastguard Worker return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
1456*9880d681SAndroid Build Coastguard Worker }
1457*9880d681SAndroid Build Coastguard Worker
1458*9880d681SAndroid Build Coastguard Worker // Transform Mag value to integer, and clear the sign bit.
1459*9880d681SAndroid Build Coastguard Worker FloatSignAsInt MagAsInt;
1460*9880d681SAndroid Build Coastguard Worker getSignAsIntValue(MagAsInt, DL, Mag);
1461*9880d681SAndroid Build Coastguard Worker EVT MagVT = MagAsInt.IntValue.getValueType();
1462*9880d681SAndroid Build Coastguard Worker SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
1463*9880d681SAndroid Build Coastguard Worker SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
1464*9880d681SAndroid Build Coastguard Worker ClearSignMask);
1465*9880d681SAndroid Build Coastguard Worker
1466*9880d681SAndroid Build Coastguard Worker // Get the signbit at the right position for MagAsInt.
1467*9880d681SAndroid Build Coastguard Worker int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
1468*9880d681SAndroid Build Coastguard Worker if (SignBit.getValueSizeInBits() > ClearedSign.getValueSizeInBits()) {
1469*9880d681SAndroid Build Coastguard Worker if (ShiftAmount > 0) {
1470*9880d681SAndroid Build Coastguard Worker SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, IntVT);
1471*9880d681SAndroid Build Coastguard Worker SignBit = DAG.getNode(ISD::SRL, DL, IntVT, SignBit, ShiftCnst);
1472*9880d681SAndroid Build Coastguard Worker } else if (ShiftAmount < 0) {
1473*9880d681SAndroid Build Coastguard Worker SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, IntVT);
1474*9880d681SAndroid Build Coastguard Worker SignBit = DAG.getNode(ISD::SHL, DL, IntVT, SignBit, ShiftCnst);
1475*9880d681SAndroid Build Coastguard Worker }
1476*9880d681SAndroid Build Coastguard Worker SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
1477*9880d681SAndroid Build Coastguard Worker } else if (SignBit.getValueSizeInBits() < ClearedSign.getValueSizeInBits()) {
1478*9880d681SAndroid Build Coastguard Worker SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
1479*9880d681SAndroid Build Coastguard Worker if (ShiftAmount > 0) {
1480*9880d681SAndroid Build Coastguard Worker SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, MagVT);
1481*9880d681SAndroid Build Coastguard Worker SignBit = DAG.getNode(ISD::SRL, DL, MagVT, SignBit, ShiftCnst);
1482*9880d681SAndroid Build Coastguard Worker } else if (ShiftAmount < 0) {
1483*9880d681SAndroid Build Coastguard Worker SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, MagVT);
1484*9880d681SAndroid Build Coastguard Worker SignBit = DAG.getNode(ISD::SHL, DL, MagVT, SignBit, ShiftCnst);
1485*9880d681SAndroid Build Coastguard Worker }
1486*9880d681SAndroid Build Coastguard Worker }
1487*9880d681SAndroid Build Coastguard Worker
1488*9880d681SAndroid Build Coastguard Worker // Store the part with the modified sign and convert back to float.
1489*9880d681SAndroid Build Coastguard Worker SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit);
1490*9880d681SAndroid Build Coastguard Worker return modifySignAsInt(MagAsInt, DL, CopiedSign);
1491*9880d681SAndroid Build Coastguard Worker }
1492*9880d681SAndroid Build Coastguard Worker
ExpandFABS(SDNode * Node) const1493*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
1494*9880d681SAndroid Build Coastguard Worker SDLoc DL(Node);
1495*9880d681SAndroid Build Coastguard Worker SDValue Value = Node->getOperand(0);
1496*9880d681SAndroid Build Coastguard Worker
1497*9880d681SAndroid Build Coastguard Worker // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
1498*9880d681SAndroid Build Coastguard Worker EVT FloatVT = Value.getValueType();
1499*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
1500*9880d681SAndroid Build Coastguard Worker SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
1501*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
1502*9880d681SAndroid Build Coastguard Worker }
1503*9880d681SAndroid Build Coastguard Worker
1504*9880d681SAndroid Build Coastguard Worker // Transform value to integer, clear the sign bit and transform back.
1505*9880d681SAndroid Build Coastguard Worker FloatSignAsInt ValueAsInt;
1506*9880d681SAndroid Build Coastguard Worker getSignAsIntValue(ValueAsInt, DL, Value);
1507*9880d681SAndroid Build Coastguard Worker EVT IntVT = ValueAsInt.IntValue.getValueType();
1508*9880d681SAndroid Build Coastguard Worker SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
1509*9880d681SAndroid Build Coastguard Worker SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
1510*9880d681SAndroid Build Coastguard Worker ClearSignMask);
1511*9880d681SAndroid Build Coastguard Worker return modifySignAsInt(ValueAsInt, DL, ClearedSign);
1512*9880d681SAndroid Build Coastguard Worker }
1513*9880d681SAndroid Build Coastguard Worker
ExpandDYNAMIC_STACKALLOC(SDNode * Node,SmallVectorImpl<SDValue> & Results)1514*9880d681SAndroid Build Coastguard Worker void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1515*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<SDValue> &Results) {
1516*9880d681SAndroid Build Coastguard Worker unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1517*9880d681SAndroid Build Coastguard Worker assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1518*9880d681SAndroid Build Coastguard Worker " not tell us which reg is the stack pointer!");
1519*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
1520*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
1521*9880d681SAndroid Build Coastguard Worker SDValue Tmp1 = SDValue(Node, 0);
1522*9880d681SAndroid Build Coastguard Worker SDValue Tmp2 = SDValue(Node, 1);
1523*9880d681SAndroid Build Coastguard Worker SDValue Tmp3 = Node->getOperand(2);
1524*9880d681SAndroid Build Coastguard Worker SDValue Chain = Tmp1.getOperand(0);
1525*9880d681SAndroid Build Coastguard Worker
1526*9880d681SAndroid Build Coastguard Worker // Chain the dynamic stack allocation so that it doesn't modify the stack
1527*9880d681SAndroid Build Coastguard Worker // pointer when other instructions are using the stack.
1528*9880d681SAndroid Build Coastguard Worker Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, dl, true), dl);
1529*9880d681SAndroid Build Coastguard Worker
1530*9880d681SAndroid Build Coastguard Worker SDValue Size = Tmp2.getOperand(1);
1531*9880d681SAndroid Build Coastguard Worker SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1532*9880d681SAndroid Build Coastguard Worker Chain = SP.getValue(1);
1533*9880d681SAndroid Build Coastguard Worker unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
1534*9880d681SAndroid Build Coastguard Worker unsigned StackAlign =
1535*9880d681SAndroid Build Coastguard Worker DAG.getSubtarget().getFrameLowering()->getStackAlignment();
1536*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1537*9880d681SAndroid Build Coastguard Worker if (Align > StackAlign)
1538*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
1539*9880d681SAndroid Build Coastguard Worker DAG.getConstant(-(uint64_t)Align, dl, VT));
1540*9880d681SAndroid Build Coastguard Worker Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1541*9880d681SAndroid Build Coastguard Worker
1542*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true),
1543*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(0, dl, true), SDValue(), dl);
1544*9880d681SAndroid Build Coastguard Worker
1545*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
1546*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp2);
1547*9880d681SAndroid Build Coastguard Worker }
1548*9880d681SAndroid Build Coastguard Worker
1549*9880d681SAndroid Build Coastguard Worker /// Legalize a SETCC with given LHS and RHS and condition code CC on the current
1550*9880d681SAndroid Build Coastguard Worker /// target.
1551*9880d681SAndroid Build Coastguard Worker ///
1552*9880d681SAndroid Build Coastguard Worker /// If the SETCC has been legalized using AND / OR, then the legalized node
1553*9880d681SAndroid Build Coastguard Worker /// will be stored in LHS. RHS and CC will be set to SDValue(). NeedInvert
1554*9880d681SAndroid Build Coastguard Worker /// will be set to false.
1555*9880d681SAndroid Build Coastguard Worker ///
1556*9880d681SAndroid Build Coastguard Worker /// If the SETCC has been legalized by using getSetCCSwappedOperands(),
1557*9880d681SAndroid Build Coastguard Worker /// then the values of LHS and RHS will be swapped, CC will be set to the
1558*9880d681SAndroid Build Coastguard Worker /// new condition, and NeedInvert will be set to false.
1559*9880d681SAndroid Build Coastguard Worker ///
1560*9880d681SAndroid Build Coastguard Worker /// If the SETCC has been legalized using the inverse condcode, then LHS and
1561*9880d681SAndroid Build Coastguard Worker /// RHS will be unchanged, CC will set to the inverted condcode, and NeedInvert
1562*9880d681SAndroid Build Coastguard Worker /// will be set to true. The caller must invert the result of the SETCC with
1563*9880d681SAndroid Build Coastguard Worker /// SelectionDAG::getLogicalNOT() or take equivalent action to swap the effect
1564*9880d681SAndroid Build Coastguard Worker /// of a true/false result.
1565*9880d681SAndroid Build Coastguard Worker ///
1566*9880d681SAndroid Build Coastguard Worker /// \returns true if the SetCC has been legalized, false if it hasn't.
LegalizeSetCCCondCode(EVT VT,SDValue & LHS,SDValue & RHS,SDValue & CC,bool & NeedInvert,const SDLoc & dl)1567*9880d681SAndroid Build Coastguard Worker bool SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT, SDValue &LHS,
1568*9880d681SAndroid Build Coastguard Worker SDValue &RHS, SDValue &CC,
1569*9880d681SAndroid Build Coastguard Worker bool &NeedInvert,
1570*9880d681SAndroid Build Coastguard Worker const SDLoc &dl) {
1571*9880d681SAndroid Build Coastguard Worker MVT OpVT = LHS.getSimpleValueType();
1572*9880d681SAndroid Build Coastguard Worker ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1573*9880d681SAndroid Build Coastguard Worker NeedInvert = false;
1574*9880d681SAndroid Build Coastguard Worker switch (TLI.getCondCodeAction(CCCode, OpVT)) {
1575*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unknown condition code action!");
1576*9880d681SAndroid Build Coastguard Worker case TargetLowering::Legal:
1577*9880d681SAndroid Build Coastguard Worker // Nothing to do.
1578*9880d681SAndroid Build Coastguard Worker break;
1579*9880d681SAndroid Build Coastguard Worker case TargetLowering::Expand: {
1580*9880d681SAndroid Build Coastguard Worker ISD::CondCode InvCC = ISD::getSetCCSwappedOperands(CCCode);
1581*9880d681SAndroid Build Coastguard Worker if (TLI.isCondCodeLegal(InvCC, OpVT)) {
1582*9880d681SAndroid Build Coastguard Worker std::swap(LHS, RHS);
1583*9880d681SAndroid Build Coastguard Worker CC = DAG.getCondCode(InvCC);
1584*9880d681SAndroid Build Coastguard Worker return true;
1585*9880d681SAndroid Build Coastguard Worker }
1586*9880d681SAndroid Build Coastguard Worker ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1587*9880d681SAndroid Build Coastguard Worker unsigned Opc = 0;
1588*9880d681SAndroid Build Coastguard Worker switch (CCCode) {
1589*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Don't know how to expand this condition!");
1590*9880d681SAndroid Build Coastguard Worker case ISD::SETO:
1591*9880d681SAndroid Build Coastguard Worker assert(TLI.getCondCodeAction(ISD::SETOEQ, OpVT)
1592*9880d681SAndroid Build Coastguard Worker == TargetLowering::Legal
1593*9880d681SAndroid Build Coastguard Worker && "If SETO is expanded, SETOEQ must be legal!");
1594*9880d681SAndroid Build Coastguard Worker CC1 = ISD::SETOEQ; CC2 = ISD::SETOEQ; Opc = ISD::AND; break;
1595*9880d681SAndroid Build Coastguard Worker case ISD::SETUO:
1596*9880d681SAndroid Build Coastguard Worker assert(TLI.getCondCodeAction(ISD::SETUNE, OpVT)
1597*9880d681SAndroid Build Coastguard Worker == TargetLowering::Legal
1598*9880d681SAndroid Build Coastguard Worker && "If SETUO is expanded, SETUNE must be legal!");
1599*9880d681SAndroid Build Coastguard Worker CC1 = ISD::SETUNE; CC2 = ISD::SETUNE; Opc = ISD::OR; break;
1600*9880d681SAndroid Build Coastguard Worker case ISD::SETOEQ:
1601*9880d681SAndroid Build Coastguard Worker case ISD::SETOGT:
1602*9880d681SAndroid Build Coastguard Worker case ISD::SETOGE:
1603*9880d681SAndroid Build Coastguard Worker case ISD::SETOLT:
1604*9880d681SAndroid Build Coastguard Worker case ISD::SETOLE:
1605*9880d681SAndroid Build Coastguard Worker case ISD::SETONE:
1606*9880d681SAndroid Build Coastguard Worker case ISD::SETUEQ:
1607*9880d681SAndroid Build Coastguard Worker case ISD::SETUNE:
1608*9880d681SAndroid Build Coastguard Worker case ISD::SETUGT:
1609*9880d681SAndroid Build Coastguard Worker case ISD::SETUGE:
1610*9880d681SAndroid Build Coastguard Worker case ISD::SETULT:
1611*9880d681SAndroid Build Coastguard Worker case ISD::SETULE:
1612*9880d681SAndroid Build Coastguard Worker // If we are floating point, assign and break, otherwise fall through.
1613*9880d681SAndroid Build Coastguard Worker if (!OpVT.isInteger()) {
1614*9880d681SAndroid Build Coastguard Worker // We can use the 4th bit to tell if we are the unordered
1615*9880d681SAndroid Build Coastguard Worker // or ordered version of the opcode.
1616*9880d681SAndroid Build Coastguard Worker CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO;
1617*9880d681SAndroid Build Coastguard Worker Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND;
1618*9880d681SAndroid Build Coastguard Worker CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10);
1619*9880d681SAndroid Build Coastguard Worker break;
1620*9880d681SAndroid Build Coastguard Worker }
1621*9880d681SAndroid Build Coastguard Worker // Fallthrough if we are unsigned integer.
1622*9880d681SAndroid Build Coastguard Worker case ISD::SETLE:
1623*9880d681SAndroid Build Coastguard Worker case ISD::SETGT:
1624*9880d681SAndroid Build Coastguard Worker case ISD::SETGE:
1625*9880d681SAndroid Build Coastguard Worker case ISD::SETLT:
1626*9880d681SAndroid Build Coastguard Worker // We only support using the inverted operation, which is computed above
1627*9880d681SAndroid Build Coastguard Worker // and not a different manner of supporting expanding these cases.
1628*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Don't know how to expand this condition!");
1629*9880d681SAndroid Build Coastguard Worker case ISD::SETNE:
1630*9880d681SAndroid Build Coastguard Worker case ISD::SETEQ:
1631*9880d681SAndroid Build Coastguard Worker // Try inverting the result of the inverse condition.
1632*9880d681SAndroid Build Coastguard Worker InvCC = CCCode == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ;
1633*9880d681SAndroid Build Coastguard Worker if (TLI.isCondCodeLegal(InvCC, OpVT)) {
1634*9880d681SAndroid Build Coastguard Worker CC = DAG.getCondCode(InvCC);
1635*9880d681SAndroid Build Coastguard Worker NeedInvert = true;
1636*9880d681SAndroid Build Coastguard Worker return true;
1637*9880d681SAndroid Build Coastguard Worker }
1638*9880d681SAndroid Build Coastguard Worker // If inverting the condition didn't work then we have no means to expand
1639*9880d681SAndroid Build Coastguard Worker // the condition.
1640*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Don't know how to expand this condition!");
1641*9880d681SAndroid Build Coastguard Worker }
1642*9880d681SAndroid Build Coastguard Worker
1643*9880d681SAndroid Build Coastguard Worker SDValue SetCC1, SetCC2;
1644*9880d681SAndroid Build Coastguard Worker if (CCCode != ISD::SETO && CCCode != ISD::SETUO) {
1645*9880d681SAndroid Build Coastguard Worker // If we aren't the ordered or unorder operation,
1646*9880d681SAndroid Build Coastguard Worker // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS).
1647*9880d681SAndroid Build Coastguard Worker SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1648*9880d681SAndroid Build Coastguard Worker SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1649*9880d681SAndroid Build Coastguard Worker } else {
1650*9880d681SAndroid Build Coastguard Worker // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS)
1651*9880d681SAndroid Build Coastguard Worker SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1);
1652*9880d681SAndroid Build Coastguard Worker SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2);
1653*9880d681SAndroid Build Coastguard Worker }
1654*9880d681SAndroid Build Coastguard Worker LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
1655*9880d681SAndroid Build Coastguard Worker RHS = SDValue();
1656*9880d681SAndroid Build Coastguard Worker CC = SDValue();
1657*9880d681SAndroid Build Coastguard Worker return true;
1658*9880d681SAndroid Build Coastguard Worker }
1659*9880d681SAndroid Build Coastguard Worker }
1660*9880d681SAndroid Build Coastguard Worker return false;
1661*9880d681SAndroid Build Coastguard Worker }
1662*9880d681SAndroid Build Coastguard Worker
1663*9880d681SAndroid Build Coastguard Worker /// Emit a store/load combination to the stack. This stores
1664*9880d681SAndroid Build Coastguard Worker /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1665*9880d681SAndroid Build Coastguard Worker /// a load from the stack slot to DestVT, extending it if needed.
1666*9880d681SAndroid Build Coastguard Worker /// The resultant code need not be legal.
EmitStackConvert(SDValue SrcOp,EVT SlotVT,EVT DestVT,const SDLoc & dl)1667*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
1668*9880d681SAndroid Build Coastguard Worker EVT DestVT, const SDLoc &dl) {
1669*9880d681SAndroid Build Coastguard Worker // Create the stack frame object.
1670*9880d681SAndroid Build Coastguard Worker unsigned SrcAlign = DAG.getDataLayout().getPrefTypeAlignment(
1671*9880d681SAndroid Build Coastguard Worker SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
1672*9880d681SAndroid Build Coastguard Worker SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
1673*9880d681SAndroid Build Coastguard Worker
1674*9880d681SAndroid Build Coastguard Worker FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1675*9880d681SAndroid Build Coastguard Worker int SPFI = StackPtrFI->getIndex();
1676*9880d681SAndroid Build Coastguard Worker MachinePointerInfo PtrInfo =
1677*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
1678*9880d681SAndroid Build Coastguard Worker
1679*9880d681SAndroid Build Coastguard Worker unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1680*9880d681SAndroid Build Coastguard Worker unsigned SlotSize = SlotVT.getSizeInBits();
1681*9880d681SAndroid Build Coastguard Worker unsigned DestSize = DestVT.getSizeInBits();
1682*9880d681SAndroid Build Coastguard Worker Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1683*9880d681SAndroid Build Coastguard Worker unsigned DestAlign = DAG.getDataLayout().getPrefTypeAlignment(DestType);
1684*9880d681SAndroid Build Coastguard Worker
1685*9880d681SAndroid Build Coastguard Worker // Emit a store to the stack slot. Use a truncstore if the input value is
1686*9880d681SAndroid Build Coastguard Worker // later than DestVT.
1687*9880d681SAndroid Build Coastguard Worker SDValue Store;
1688*9880d681SAndroid Build Coastguard Worker
1689*9880d681SAndroid Build Coastguard Worker if (SrcSize > SlotSize)
1690*9880d681SAndroid Build Coastguard Worker Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
1691*9880d681SAndroid Build Coastguard Worker PtrInfo, SlotVT, false, false, SrcAlign);
1692*9880d681SAndroid Build Coastguard Worker else {
1693*9880d681SAndroid Build Coastguard Worker assert(SrcSize == SlotSize && "Invalid store");
1694*9880d681SAndroid Build Coastguard Worker Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
1695*9880d681SAndroid Build Coastguard Worker PtrInfo, false, false, SrcAlign);
1696*9880d681SAndroid Build Coastguard Worker }
1697*9880d681SAndroid Build Coastguard Worker
1698*9880d681SAndroid Build Coastguard Worker // Result is a load from the stack slot.
1699*9880d681SAndroid Build Coastguard Worker if (SlotSize == DestSize)
1700*9880d681SAndroid Build Coastguard Worker return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
1701*9880d681SAndroid Build Coastguard Worker false, false, false, DestAlign);
1702*9880d681SAndroid Build Coastguard Worker
1703*9880d681SAndroid Build Coastguard Worker assert(SlotSize < DestSize && "Unknown extension!");
1704*9880d681SAndroid Build Coastguard Worker return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
1705*9880d681SAndroid Build Coastguard Worker PtrInfo, SlotVT, false, false, false, DestAlign);
1706*9880d681SAndroid Build Coastguard Worker }
1707*9880d681SAndroid Build Coastguard Worker
ExpandSCALAR_TO_VECTOR(SDNode * Node)1708*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
1709*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
1710*9880d681SAndroid Build Coastguard Worker // Create a vector sized/aligned stack slot, store the value to element #0,
1711*9880d681SAndroid Build Coastguard Worker // then load the whole vector back out.
1712*9880d681SAndroid Build Coastguard Worker SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
1713*9880d681SAndroid Build Coastguard Worker
1714*9880d681SAndroid Build Coastguard Worker FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1715*9880d681SAndroid Build Coastguard Worker int SPFI = StackPtrFI->getIndex();
1716*9880d681SAndroid Build Coastguard Worker
1717*9880d681SAndroid Build Coastguard Worker SDValue Ch = DAG.getTruncStore(
1718*9880d681SAndroid Build Coastguard Worker DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
1719*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
1720*9880d681SAndroid Build Coastguard Worker Node->getValueType(0).getVectorElementType(), false, false, 0);
1721*9880d681SAndroid Build Coastguard Worker return DAG.getLoad(
1722*9880d681SAndroid Build Coastguard Worker Node->getValueType(0), dl, Ch, StackPtr,
1723*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), false,
1724*9880d681SAndroid Build Coastguard Worker false, false, 0);
1725*9880d681SAndroid Build Coastguard Worker }
1726*9880d681SAndroid Build Coastguard Worker
1727*9880d681SAndroid Build Coastguard Worker static bool
ExpandBVWithShuffles(SDNode * Node,SelectionDAG & DAG,const TargetLowering & TLI,SDValue & Res)1728*9880d681SAndroid Build Coastguard Worker ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG,
1729*9880d681SAndroid Build Coastguard Worker const TargetLowering &TLI, SDValue &Res) {
1730*9880d681SAndroid Build Coastguard Worker unsigned NumElems = Node->getNumOperands();
1731*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
1732*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
1733*9880d681SAndroid Build Coastguard Worker
1734*9880d681SAndroid Build Coastguard Worker // Try to group the scalars into pairs, shuffle the pairs together, then
1735*9880d681SAndroid Build Coastguard Worker // shuffle the pairs of pairs together, etc. until the vector has
1736*9880d681SAndroid Build Coastguard Worker // been built. This will work only if all of the necessary shuffle masks
1737*9880d681SAndroid Build Coastguard Worker // are legal.
1738*9880d681SAndroid Build Coastguard Worker
1739*9880d681SAndroid Build Coastguard Worker // We do this in two phases; first to check the legality of the shuffles,
1740*9880d681SAndroid Build Coastguard Worker // and next, assuming that all shuffles are legal, to create the new nodes.
1741*9880d681SAndroid Build Coastguard Worker for (int Phase = 0; Phase < 2; ++Phase) {
1742*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<SDValue, SmallVector<int, 16> >, 16> IntermedVals,
1743*9880d681SAndroid Build Coastguard Worker NewIntermedVals;
1744*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NumElems; ++i) {
1745*9880d681SAndroid Build Coastguard Worker SDValue V = Node->getOperand(i);
1746*9880d681SAndroid Build Coastguard Worker if (V.isUndef())
1747*9880d681SAndroid Build Coastguard Worker continue;
1748*9880d681SAndroid Build Coastguard Worker
1749*9880d681SAndroid Build Coastguard Worker SDValue Vec;
1750*9880d681SAndroid Build Coastguard Worker if (Phase)
1751*9880d681SAndroid Build Coastguard Worker Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
1752*9880d681SAndroid Build Coastguard Worker IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
1753*9880d681SAndroid Build Coastguard Worker }
1754*9880d681SAndroid Build Coastguard Worker
1755*9880d681SAndroid Build Coastguard Worker while (IntermedVals.size() > 2) {
1756*9880d681SAndroid Build Coastguard Worker NewIntermedVals.clear();
1757*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
1758*9880d681SAndroid Build Coastguard Worker // This vector and the next vector are shuffled together (simply to
1759*9880d681SAndroid Build Coastguard Worker // append the one to the other).
1760*9880d681SAndroid Build Coastguard Worker SmallVector<int, 16> ShuffleVec(NumElems, -1);
1761*9880d681SAndroid Build Coastguard Worker
1762*9880d681SAndroid Build Coastguard Worker SmallVector<int, 16> FinalIndices;
1763*9880d681SAndroid Build Coastguard Worker FinalIndices.reserve(IntermedVals[i].second.size() +
1764*9880d681SAndroid Build Coastguard Worker IntermedVals[i+1].second.size());
1765*9880d681SAndroid Build Coastguard Worker
1766*9880d681SAndroid Build Coastguard Worker int k = 0;
1767*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
1768*9880d681SAndroid Build Coastguard Worker ++j, ++k) {
1769*9880d681SAndroid Build Coastguard Worker ShuffleVec[k] = j;
1770*9880d681SAndroid Build Coastguard Worker FinalIndices.push_back(IntermedVals[i].second[j]);
1771*9880d681SAndroid Build Coastguard Worker }
1772*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
1773*9880d681SAndroid Build Coastguard Worker ++j, ++k) {
1774*9880d681SAndroid Build Coastguard Worker ShuffleVec[k] = NumElems + j;
1775*9880d681SAndroid Build Coastguard Worker FinalIndices.push_back(IntermedVals[i+1].second[j]);
1776*9880d681SAndroid Build Coastguard Worker }
1777*9880d681SAndroid Build Coastguard Worker
1778*9880d681SAndroid Build Coastguard Worker SDValue Shuffle;
1779*9880d681SAndroid Build Coastguard Worker if (Phase)
1780*9880d681SAndroid Build Coastguard Worker Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
1781*9880d681SAndroid Build Coastguard Worker IntermedVals[i+1].first,
1782*9880d681SAndroid Build Coastguard Worker ShuffleVec);
1783*9880d681SAndroid Build Coastguard Worker else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1784*9880d681SAndroid Build Coastguard Worker return false;
1785*9880d681SAndroid Build Coastguard Worker NewIntermedVals.push_back(
1786*9880d681SAndroid Build Coastguard Worker std::make_pair(Shuffle, std::move(FinalIndices)));
1787*9880d681SAndroid Build Coastguard Worker }
1788*9880d681SAndroid Build Coastguard Worker
1789*9880d681SAndroid Build Coastguard Worker // If we had an odd number of defined values, then append the last
1790*9880d681SAndroid Build Coastguard Worker // element to the array of new vectors.
1791*9880d681SAndroid Build Coastguard Worker if ((IntermedVals.size() & 1) != 0)
1792*9880d681SAndroid Build Coastguard Worker NewIntermedVals.push_back(IntermedVals.back());
1793*9880d681SAndroid Build Coastguard Worker
1794*9880d681SAndroid Build Coastguard Worker IntermedVals.swap(NewIntermedVals);
1795*9880d681SAndroid Build Coastguard Worker }
1796*9880d681SAndroid Build Coastguard Worker
1797*9880d681SAndroid Build Coastguard Worker assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
1798*9880d681SAndroid Build Coastguard Worker "Invalid number of intermediate vectors");
1799*9880d681SAndroid Build Coastguard Worker SDValue Vec1 = IntermedVals[0].first;
1800*9880d681SAndroid Build Coastguard Worker SDValue Vec2;
1801*9880d681SAndroid Build Coastguard Worker if (IntermedVals.size() > 1)
1802*9880d681SAndroid Build Coastguard Worker Vec2 = IntermedVals[1].first;
1803*9880d681SAndroid Build Coastguard Worker else if (Phase)
1804*9880d681SAndroid Build Coastguard Worker Vec2 = DAG.getUNDEF(VT);
1805*9880d681SAndroid Build Coastguard Worker
1806*9880d681SAndroid Build Coastguard Worker SmallVector<int, 16> ShuffleVec(NumElems, -1);
1807*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
1808*9880d681SAndroid Build Coastguard Worker ShuffleVec[IntermedVals[0].second[i]] = i;
1809*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
1810*9880d681SAndroid Build Coastguard Worker ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
1811*9880d681SAndroid Build Coastguard Worker
1812*9880d681SAndroid Build Coastguard Worker if (Phase)
1813*9880d681SAndroid Build Coastguard Worker Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
1814*9880d681SAndroid Build Coastguard Worker else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1815*9880d681SAndroid Build Coastguard Worker return false;
1816*9880d681SAndroid Build Coastguard Worker }
1817*9880d681SAndroid Build Coastguard Worker
1818*9880d681SAndroid Build Coastguard Worker return true;
1819*9880d681SAndroid Build Coastguard Worker }
1820*9880d681SAndroid Build Coastguard Worker
1821*9880d681SAndroid Build Coastguard Worker /// Expand a BUILD_VECTOR node on targets that don't
1822*9880d681SAndroid Build Coastguard Worker /// support the operation, but do support the resultant vector type.
ExpandBUILD_VECTOR(SDNode * Node)1823*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
1824*9880d681SAndroid Build Coastguard Worker unsigned NumElems = Node->getNumOperands();
1825*9880d681SAndroid Build Coastguard Worker SDValue Value1, Value2;
1826*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
1827*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
1828*9880d681SAndroid Build Coastguard Worker EVT OpVT = Node->getOperand(0).getValueType();
1829*9880d681SAndroid Build Coastguard Worker EVT EltVT = VT.getVectorElementType();
1830*9880d681SAndroid Build Coastguard Worker
1831*9880d681SAndroid Build Coastguard Worker // If the only non-undef value is the low element, turn this into a
1832*9880d681SAndroid Build Coastguard Worker // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
1833*9880d681SAndroid Build Coastguard Worker bool isOnlyLowElement = true;
1834*9880d681SAndroid Build Coastguard Worker bool MoreThanTwoValues = false;
1835*9880d681SAndroid Build Coastguard Worker bool isConstant = true;
1836*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NumElems; ++i) {
1837*9880d681SAndroid Build Coastguard Worker SDValue V = Node->getOperand(i);
1838*9880d681SAndroid Build Coastguard Worker if (V.isUndef())
1839*9880d681SAndroid Build Coastguard Worker continue;
1840*9880d681SAndroid Build Coastguard Worker if (i > 0)
1841*9880d681SAndroid Build Coastguard Worker isOnlyLowElement = false;
1842*9880d681SAndroid Build Coastguard Worker if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
1843*9880d681SAndroid Build Coastguard Worker isConstant = false;
1844*9880d681SAndroid Build Coastguard Worker
1845*9880d681SAndroid Build Coastguard Worker if (!Value1.getNode()) {
1846*9880d681SAndroid Build Coastguard Worker Value1 = V;
1847*9880d681SAndroid Build Coastguard Worker } else if (!Value2.getNode()) {
1848*9880d681SAndroid Build Coastguard Worker if (V != Value1)
1849*9880d681SAndroid Build Coastguard Worker Value2 = V;
1850*9880d681SAndroid Build Coastguard Worker } else if (V != Value1 && V != Value2) {
1851*9880d681SAndroid Build Coastguard Worker MoreThanTwoValues = true;
1852*9880d681SAndroid Build Coastguard Worker }
1853*9880d681SAndroid Build Coastguard Worker }
1854*9880d681SAndroid Build Coastguard Worker
1855*9880d681SAndroid Build Coastguard Worker if (!Value1.getNode())
1856*9880d681SAndroid Build Coastguard Worker return DAG.getUNDEF(VT);
1857*9880d681SAndroid Build Coastguard Worker
1858*9880d681SAndroid Build Coastguard Worker if (isOnlyLowElement)
1859*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
1860*9880d681SAndroid Build Coastguard Worker
1861*9880d681SAndroid Build Coastguard Worker // If all elements are constants, create a load from the constant pool.
1862*9880d681SAndroid Build Coastguard Worker if (isConstant) {
1863*9880d681SAndroid Build Coastguard Worker SmallVector<Constant*, 16> CV;
1864*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = NumElems; i != e; ++i) {
1865*9880d681SAndroid Build Coastguard Worker if (ConstantFPSDNode *V =
1866*9880d681SAndroid Build Coastguard Worker dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
1867*9880d681SAndroid Build Coastguard Worker CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
1868*9880d681SAndroid Build Coastguard Worker } else if (ConstantSDNode *V =
1869*9880d681SAndroid Build Coastguard Worker dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
1870*9880d681SAndroid Build Coastguard Worker if (OpVT==EltVT)
1871*9880d681SAndroid Build Coastguard Worker CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1872*9880d681SAndroid Build Coastguard Worker else {
1873*9880d681SAndroid Build Coastguard Worker // If OpVT and EltVT don't match, EltVT is not legal and the
1874*9880d681SAndroid Build Coastguard Worker // element values have been promoted/truncated earlier. Undo this;
1875*9880d681SAndroid Build Coastguard Worker // we don't want a v16i8 to become a v16i32 for example.
1876*9880d681SAndroid Build Coastguard Worker const ConstantInt *CI = V->getConstantIntValue();
1877*9880d681SAndroid Build Coastguard Worker CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1878*9880d681SAndroid Build Coastguard Worker CI->getZExtValue()));
1879*9880d681SAndroid Build Coastguard Worker }
1880*9880d681SAndroid Build Coastguard Worker } else {
1881*9880d681SAndroid Build Coastguard Worker assert(Node->getOperand(i).isUndef());
1882*9880d681SAndroid Build Coastguard Worker Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
1883*9880d681SAndroid Build Coastguard Worker CV.push_back(UndefValue::get(OpNTy));
1884*9880d681SAndroid Build Coastguard Worker }
1885*9880d681SAndroid Build Coastguard Worker }
1886*9880d681SAndroid Build Coastguard Worker Constant *CP = ConstantVector::get(CV);
1887*9880d681SAndroid Build Coastguard Worker SDValue CPIdx =
1888*9880d681SAndroid Build Coastguard Worker DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
1889*9880d681SAndroid Build Coastguard Worker unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
1890*9880d681SAndroid Build Coastguard Worker return DAG.getLoad(
1891*9880d681SAndroid Build Coastguard Worker VT, dl, DAG.getEntryNode(), CPIdx,
1892*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1893*9880d681SAndroid Build Coastguard Worker false, false, Alignment);
1894*9880d681SAndroid Build Coastguard Worker }
1895*9880d681SAndroid Build Coastguard Worker
1896*9880d681SAndroid Build Coastguard Worker SmallSet<SDValue, 16> DefinedValues;
1897*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NumElems; ++i) {
1898*9880d681SAndroid Build Coastguard Worker if (Node->getOperand(i).isUndef())
1899*9880d681SAndroid Build Coastguard Worker continue;
1900*9880d681SAndroid Build Coastguard Worker DefinedValues.insert(Node->getOperand(i));
1901*9880d681SAndroid Build Coastguard Worker }
1902*9880d681SAndroid Build Coastguard Worker
1903*9880d681SAndroid Build Coastguard Worker if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
1904*9880d681SAndroid Build Coastguard Worker if (!MoreThanTwoValues) {
1905*9880d681SAndroid Build Coastguard Worker SmallVector<int, 8> ShuffleVec(NumElems, -1);
1906*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NumElems; ++i) {
1907*9880d681SAndroid Build Coastguard Worker SDValue V = Node->getOperand(i);
1908*9880d681SAndroid Build Coastguard Worker if (V.isUndef())
1909*9880d681SAndroid Build Coastguard Worker continue;
1910*9880d681SAndroid Build Coastguard Worker ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1911*9880d681SAndroid Build Coastguard Worker }
1912*9880d681SAndroid Build Coastguard Worker if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
1913*9880d681SAndroid Build Coastguard Worker // Get the splatted value into the low element of a vector register.
1914*9880d681SAndroid Build Coastguard Worker SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1915*9880d681SAndroid Build Coastguard Worker SDValue Vec2;
1916*9880d681SAndroid Build Coastguard Worker if (Value2.getNode())
1917*9880d681SAndroid Build Coastguard Worker Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1918*9880d681SAndroid Build Coastguard Worker else
1919*9880d681SAndroid Build Coastguard Worker Vec2 = DAG.getUNDEF(VT);
1920*9880d681SAndroid Build Coastguard Worker
1921*9880d681SAndroid Build Coastguard Worker // Return shuffle(LowValVec, undef, <0,0,0,0>)
1922*9880d681SAndroid Build Coastguard Worker return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
1923*9880d681SAndroid Build Coastguard Worker }
1924*9880d681SAndroid Build Coastguard Worker } else {
1925*9880d681SAndroid Build Coastguard Worker SDValue Res;
1926*9880d681SAndroid Build Coastguard Worker if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
1927*9880d681SAndroid Build Coastguard Worker return Res;
1928*9880d681SAndroid Build Coastguard Worker }
1929*9880d681SAndroid Build Coastguard Worker }
1930*9880d681SAndroid Build Coastguard Worker
1931*9880d681SAndroid Build Coastguard Worker // Otherwise, we can't handle this case efficiently.
1932*9880d681SAndroid Build Coastguard Worker return ExpandVectorBuildThroughStack(Node);
1933*9880d681SAndroid Build Coastguard Worker }
1934*9880d681SAndroid Build Coastguard Worker
1935*9880d681SAndroid Build Coastguard Worker // Expand a node into a call to a libcall. If the result value
1936*9880d681SAndroid Build Coastguard Worker // does not fit into a register, return the lo part and set the hi part to the
1937*9880d681SAndroid Build Coastguard Worker // by-reg argument. If it does fit into a single register, return the result
1938*9880d681SAndroid Build Coastguard Worker // and leave the Hi part unset.
ExpandLibCall(RTLIB::Libcall LC,SDNode * Node,bool isSigned)1939*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
1940*9880d681SAndroid Build Coastguard Worker bool isSigned) {
1941*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListTy Args;
1942*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListEntry Entry;
1943*9880d681SAndroid Build Coastguard Worker for (const SDValue &Op : Node->op_values()) {
1944*9880d681SAndroid Build Coastguard Worker EVT ArgVT = Op.getValueType();
1945*9880d681SAndroid Build Coastguard Worker Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1946*9880d681SAndroid Build Coastguard Worker Entry.Node = Op;
1947*9880d681SAndroid Build Coastguard Worker Entry.Ty = ArgTy;
1948*9880d681SAndroid Build Coastguard Worker Entry.isSExt = isSigned;
1949*9880d681SAndroid Build Coastguard Worker Entry.isZExt = !isSigned;
1950*9880d681SAndroid Build Coastguard Worker Args.push_back(Entry);
1951*9880d681SAndroid Build Coastguard Worker }
1952*9880d681SAndroid Build Coastguard Worker SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1953*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout()));
1954*9880d681SAndroid Build Coastguard Worker
1955*9880d681SAndroid Build Coastguard Worker Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
1956*9880d681SAndroid Build Coastguard Worker
1957*9880d681SAndroid Build Coastguard Worker // By default, the input chain to this libcall is the entry node of the
1958*9880d681SAndroid Build Coastguard Worker // function. If the libcall is going to be emitted as a tail call then
1959*9880d681SAndroid Build Coastguard Worker // TLI.isUsedByReturnOnly will change it to the right chain if the return
1960*9880d681SAndroid Build Coastguard Worker // node which is being folded has a non-entry input chain.
1961*9880d681SAndroid Build Coastguard Worker SDValue InChain = DAG.getEntryNode();
1962*9880d681SAndroid Build Coastguard Worker
1963*9880d681SAndroid Build Coastguard Worker // isTailCall may be true since the callee does not reference caller stack
1964*9880d681SAndroid Build Coastguard Worker // frame. Check if it's in the right position and that the return types match.
1965*9880d681SAndroid Build Coastguard Worker SDValue TCChain = InChain;
1966*9880d681SAndroid Build Coastguard Worker const Function *F = DAG.getMachineFunction().getFunction();
1967*9880d681SAndroid Build Coastguard Worker bool isTailCall =
1968*9880d681SAndroid Build Coastguard Worker TLI.isInTailCallPosition(DAG, Node, TCChain) &&
1969*9880d681SAndroid Build Coastguard Worker (RetTy == F->getReturnType() || F->getReturnType()->isVoidTy());
1970*9880d681SAndroid Build Coastguard Worker if (isTailCall)
1971*9880d681SAndroid Build Coastguard Worker InChain = TCChain;
1972*9880d681SAndroid Build Coastguard Worker
1973*9880d681SAndroid Build Coastguard Worker TargetLowering::CallLoweringInfo CLI(DAG);
1974*9880d681SAndroid Build Coastguard Worker CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
1975*9880d681SAndroid Build Coastguard Worker .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
1976*9880d681SAndroid Build Coastguard Worker .setTailCall(isTailCall).setSExtResult(isSigned).setZExtResult(!isSigned);
1977*9880d681SAndroid Build Coastguard Worker
1978*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
1979*9880d681SAndroid Build Coastguard Worker
1980*9880d681SAndroid Build Coastguard Worker if (!CallInfo.second.getNode())
1981*9880d681SAndroid Build Coastguard Worker // It's a tailcall, return the chain (which is the DAG root).
1982*9880d681SAndroid Build Coastguard Worker return DAG.getRoot();
1983*9880d681SAndroid Build Coastguard Worker
1984*9880d681SAndroid Build Coastguard Worker return CallInfo.first;
1985*9880d681SAndroid Build Coastguard Worker }
1986*9880d681SAndroid Build Coastguard Worker
1987*9880d681SAndroid Build Coastguard Worker /// Generate a libcall taking the given operands as arguments
1988*9880d681SAndroid Build Coastguard Worker /// and returning a result of type RetVT.
ExpandLibCall(RTLIB::Libcall LC,EVT RetVT,const SDValue * Ops,unsigned NumOps,bool isSigned,const SDLoc & dl)1989*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
1990*9880d681SAndroid Build Coastguard Worker const SDValue *Ops, unsigned NumOps,
1991*9880d681SAndroid Build Coastguard Worker bool isSigned, const SDLoc &dl) {
1992*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListTy Args;
1993*9880d681SAndroid Build Coastguard Worker Args.reserve(NumOps);
1994*9880d681SAndroid Build Coastguard Worker
1995*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListEntry Entry;
1996*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumOps; ++i) {
1997*9880d681SAndroid Build Coastguard Worker Entry.Node = Ops[i];
1998*9880d681SAndroid Build Coastguard Worker Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
1999*9880d681SAndroid Build Coastguard Worker Entry.isSExt = isSigned;
2000*9880d681SAndroid Build Coastguard Worker Entry.isZExt = !isSigned;
2001*9880d681SAndroid Build Coastguard Worker Args.push_back(Entry);
2002*9880d681SAndroid Build Coastguard Worker }
2003*9880d681SAndroid Build Coastguard Worker SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2004*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout()));
2005*9880d681SAndroid Build Coastguard Worker
2006*9880d681SAndroid Build Coastguard Worker Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2007*9880d681SAndroid Build Coastguard Worker
2008*9880d681SAndroid Build Coastguard Worker TargetLowering::CallLoweringInfo CLI(DAG);
2009*9880d681SAndroid Build Coastguard Worker CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
2010*9880d681SAndroid Build Coastguard Worker .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
2011*9880d681SAndroid Build Coastguard Worker .setSExtResult(isSigned).setZExtResult(!isSigned);
2012*9880d681SAndroid Build Coastguard Worker
2013*9880d681SAndroid Build Coastguard Worker std::pair<SDValue,SDValue> CallInfo = TLI.LowerCallTo(CLI);
2014*9880d681SAndroid Build Coastguard Worker
2015*9880d681SAndroid Build Coastguard Worker return CallInfo.first;
2016*9880d681SAndroid Build Coastguard Worker }
2017*9880d681SAndroid Build Coastguard Worker
2018*9880d681SAndroid Build Coastguard Worker // Expand a node into a call to a libcall. Similar to
2019*9880d681SAndroid Build Coastguard Worker // ExpandLibCall except that the first operand is the in-chain.
2020*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue>
ExpandChainLibCall(RTLIB::Libcall LC,SDNode * Node,bool isSigned)2021*9880d681SAndroid Build Coastguard Worker SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2022*9880d681SAndroid Build Coastguard Worker SDNode *Node,
2023*9880d681SAndroid Build Coastguard Worker bool isSigned) {
2024*9880d681SAndroid Build Coastguard Worker SDValue InChain = Node->getOperand(0);
2025*9880d681SAndroid Build Coastguard Worker
2026*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListTy Args;
2027*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListEntry Entry;
2028*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2029*9880d681SAndroid Build Coastguard Worker EVT ArgVT = Node->getOperand(i).getValueType();
2030*9880d681SAndroid Build Coastguard Worker Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2031*9880d681SAndroid Build Coastguard Worker Entry.Node = Node->getOperand(i);
2032*9880d681SAndroid Build Coastguard Worker Entry.Ty = ArgTy;
2033*9880d681SAndroid Build Coastguard Worker Entry.isSExt = isSigned;
2034*9880d681SAndroid Build Coastguard Worker Entry.isZExt = !isSigned;
2035*9880d681SAndroid Build Coastguard Worker Args.push_back(Entry);
2036*9880d681SAndroid Build Coastguard Worker }
2037*9880d681SAndroid Build Coastguard Worker SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2038*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout()));
2039*9880d681SAndroid Build Coastguard Worker
2040*9880d681SAndroid Build Coastguard Worker Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2041*9880d681SAndroid Build Coastguard Worker
2042*9880d681SAndroid Build Coastguard Worker TargetLowering::CallLoweringInfo CLI(DAG);
2043*9880d681SAndroid Build Coastguard Worker CLI.setDebugLoc(SDLoc(Node)).setChain(InChain)
2044*9880d681SAndroid Build Coastguard Worker .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
2045*9880d681SAndroid Build Coastguard Worker .setSExtResult(isSigned).setZExtResult(!isSigned);
2046*9880d681SAndroid Build Coastguard Worker
2047*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2048*9880d681SAndroid Build Coastguard Worker
2049*9880d681SAndroid Build Coastguard Worker return CallInfo;
2050*9880d681SAndroid Build Coastguard Worker }
2051*9880d681SAndroid Build Coastguard Worker
ExpandFPLibCall(SDNode * Node,RTLIB::Libcall Call_F32,RTLIB::Libcall Call_F64,RTLIB::Libcall Call_F80,RTLIB::Libcall Call_F128,RTLIB::Libcall Call_PPCF128)2052*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2053*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_F32,
2054*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_F64,
2055*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_F80,
2056*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_F128,
2057*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_PPCF128) {
2058*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall LC;
2059*9880d681SAndroid Build Coastguard Worker switch (Node->getSimpleValueType(0).SimpleTy) {
2060*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unexpected request for libcall!");
2061*9880d681SAndroid Build Coastguard Worker case MVT::f32: LC = Call_F32; break;
2062*9880d681SAndroid Build Coastguard Worker case MVT::f64: LC = Call_F64; break;
2063*9880d681SAndroid Build Coastguard Worker case MVT::f80: LC = Call_F80; break;
2064*9880d681SAndroid Build Coastguard Worker case MVT::f128: LC = Call_F128; break;
2065*9880d681SAndroid Build Coastguard Worker case MVT::ppcf128: LC = Call_PPCF128; break;
2066*9880d681SAndroid Build Coastguard Worker }
2067*9880d681SAndroid Build Coastguard Worker return ExpandLibCall(LC, Node, false);
2068*9880d681SAndroid Build Coastguard Worker }
2069*9880d681SAndroid Build Coastguard Worker
ExpandIntLibCall(SDNode * Node,bool isSigned,RTLIB::Libcall Call_I8,RTLIB::Libcall Call_I16,RTLIB::Libcall Call_I32,RTLIB::Libcall Call_I64,RTLIB::Libcall Call_I128)2070*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2071*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I8,
2072*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I16,
2073*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I32,
2074*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I64,
2075*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall Call_I128) {
2076*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall LC;
2077*9880d681SAndroid Build Coastguard Worker switch (Node->getSimpleValueType(0).SimpleTy) {
2078*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unexpected request for libcall!");
2079*9880d681SAndroid Build Coastguard Worker case MVT::i8: LC = Call_I8; break;
2080*9880d681SAndroid Build Coastguard Worker case MVT::i16: LC = Call_I16; break;
2081*9880d681SAndroid Build Coastguard Worker case MVT::i32: LC = Call_I32; break;
2082*9880d681SAndroid Build Coastguard Worker case MVT::i64: LC = Call_I64; break;
2083*9880d681SAndroid Build Coastguard Worker case MVT::i128: LC = Call_I128; break;
2084*9880d681SAndroid Build Coastguard Worker }
2085*9880d681SAndroid Build Coastguard Worker return ExpandLibCall(LC, Node, isSigned);
2086*9880d681SAndroid Build Coastguard Worker }
2087*9880d681SAndroid Build Coastguard Worker
2088*9880d681SAndroid Build Coastguard Worker /// Issue libcalls to __{u}divmod to compute div / rem pairs.
2089*9880d681SAndroid Build Coastguard Worker void
ExpandDivRemLibCall(SDNode * Node,SmallVectorImpl<SDValue> & Results)2090*9880d681SAndroid Build Coastguard Worker SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2091*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<SDValue> &Results) {
2092*9880d681SAndroid Build Coastguard Worker unsigned Opcode = Node->getOpcode();
2093*9880d681SAndroid Build Coastguard Worker bool isSigned = Opcode == ISD::SDIVREM;
2094*9880d681SAndroid Build Coastguard Worker
2095*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall LC;
2096*9880d681SAndroid Build Coastguard Worker switch (Node->getSimpleValueType(0).SimpleTy) {
2097*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unexpected request for libcall!");
2098*9880d681SAndroid Build Coastguard Worker case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2099*9880d681SAndroid Build Coastguard Worker case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2100*9880d681SAndroid Build Coastguard Worker case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2101*9880d681SAndroid Build Coastguard Worker case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2102*9880d681SAndroid Build Coastguard Worker case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2103*9880d681SAndroid Build Coastguard Worker }
2104*9880d681SAndroid Build Coastguard Worker
2105*9880d681SAndroid Build Coastguard Worker // The input chain to this libcall is the entry node of the function.
2106*9880d681SAndroid Build Coastguard Worker // Legalizing the call will automatically add the previous call to the
2107*9880d681SAndroid Build Coastguard Worker // dependence.
2108*9880d681SAndroid Build Coastguard Worker SDValue InChain = DAG.getEntryNode();
2109*9880d681SAndroid Build Coastguard Worker
2110*9880d681SAndroid Build Coastguard Worker EVT RetVT = Node->getValueType(0);
2111*9880d681SAndroid Build Coastguard Worker Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2112*9880d681SAndroid Build Coastguard Worker
2113*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListTy Args;
2114*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListEntry Entry;
2115*9880d681SAndroid Build Coastguard Worker for (const SDValue &Op : Node->op_values()) {
2116*9880d681SAndroid Build Coastguard Worker EVT ArgVT = Op.getValueType();
2117*9880d681SAndroid Build Coastguard Worker Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2118*9880d681SAndroid Build Coastguard Worker Entry.Node = Op;
2119*9880d681SAndroid Build Coastguard Worker Entry.Ty = ArgTy;
2120*9880d681SAndroid Build Coastguard Worker Entry.isSExt = isSigned;
2121*9880d681SAndroid Build Coastguard Worker Entry.isZExt = !isSigned;
2122*9880d681SAndroid Build Coastguard Worker Args.push_back(Entry);
2123*9880d681SAndroid Build Coastguard Worker }
2124*9880d681SAndroid Build Coastguard Worker
2125*9880d681SAndroid Build Coastguard Worker // Also pass the return address of the remainder.
2126*9880d681SAndroid Build Coastguard Worker SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2127*9880d681SAndroid Build Coastguard Worker Entry.Node = FIPtr;
2128*9880d681SAndroid Build Coastguard Worker Entry.Ty = RetTy->getPointerTo();
2129*9880d681SAndroid Build Coastguard Worker Entry.isSExt = isSigned;
2130*9880d681SAndroid Build Coastguard Worker Entry.isZExt = !isSigned;
2131*9880d681SAndroid Build Coastguard Worker Args.push_back(Entry);
2132*9880d681SAndroid Build Coastguard Worker
2133*9880d681SAndroid Build Coastguard Worker SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2134*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout()));
2135*9880d681SAndroid Build Coastguard Worker
2136*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
2137*9880d681SAndroid Build Coastguard Worker TargetLowering::CallLoweringInfo CLI(DAG);
2138*9880d681SAndroid Build Coastguard Worker CLI.setDebugLoc(dl).setChain(InChain)
2139*9880d681SAndroid Build Coastguard Worker .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
2140*9880d681SAndroid Build Coastguard Worker .setSExtResult(isSigned).setZExtResult(!isSigned);
2141*9880d681SAndroid Build Coastguard Worker
2142*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2143*9880d681SAndroid Build Coastguard Worker
2144*9880d681SAndroid Build Coastguard Worker // Remainder is loaded back from the stack frame.
2145*9880d681SAndroid Build Coastguard Worker SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
2146*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(), false, false, false, 0);
2147*9880d681SAndroid Build Coastguard Worker Results.push_back(CallInfo.first);
2148*9880d681SAndroid Build Coastguard Worker Results.push_back(Rem);
2149*9880d681SAndroid Build Coastguard Worker }
2150*9880d681SAndroid Build Coastguard Worker
2151*9880d681SAndroid Build Coastguard Worker /// Return true if sincos libcall is available.
isSinCosLibcallAvailable(SDNode * Node,const TargetLowering & TLI)2152*9880d681SAndroid Build Coastguard Worker static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
2153*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall LC;
2154*9880d681SAndroid Build Coastguard Worker switch (Node->getSimpleValueType(0).SimpleTy) {
2155*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unexpected request for libcall!");
2156*9880d681SAndroid Build Coastguard Worker case MVT::f32: LC = RTLIB::SINCOS_F32; break;
2157*9880d681SAndroid Build Coastguard Worker case MVT::f64: LC = RTLIB::SINCOS_F64; break;
2158*9880d681SAndroid Build Coastguard Worker case MVT::f80: LC = RTLIB::SINCOS_F80; break;
2159*9880d681SAndroid Build Coastguard Worker case MVT::f128: LC = RTLIB::SINCOS_F128; break;
2160*9880d681SAndroid Build Coastguard Worker case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2161*9880d681SAndroid Build Coastguard Worker }
2162*9880d681SAndroid Build Coastguard Worker return TLI.getLibcallName(LC) != nullptr;
2163*9880d681SAndroid Build Coastguard Worker }
2164*9880d681SAndroid Build Coastguard Worker
2165*9880d681SAndroid Build Coastguard Worker /// Return true if sincos libcall is available and can be used to combine sin
2166*9880d681SAndroid Build Coastguard Worker /// and cos.
canCombineSinCosLibcall(SDNode * Node,const TargetLowering & TLI,const TargetMachine & TM)2167*9880d681SAndroid Build Coastguard Worker static bool canCombineSinCosLibcall(SDNode *Node, const TargetLowering &TLI,
2168*9880d681SAndroid Build Coastguard Worker const TargetMachine &TM) {
2169*9880d681SAndroid Build Coastguard Worker if (!isSinCosLibcallAvailable(Node, TLI))
2170*9880d681SAndroid Build Coastguard Worker return false;
2171*9880d681SAndroid Build Coastguard Worker // GNU sin/cos functions set errno while sincos does not. Therefore
2172*9880d681SAndroid Build Coastguard Worker // combining sin and cos is only safe if unsafe-fpmath is enabled.
2173*9880d681SAndroid Build Coastguard Worker if (TM.getTargetTriple().isGNUEnvironment() && !TM.Options.UnsafeFPMath)
2174*9880d681SAndroid Build Coastguard Worker return false;
2175*9880d681SAndroid Build Coastguard Worker return true;
2176*9880d681SAndroid Build Coastguard Worker }
2177*9880d681SAndroid Build Coastguard Worker
2178*9880d681SAndroid Build Coastguard Worker /// Only issue sincos libcall if both sin and cos are needed.
useSinCos(SDNode * Node)2179*9880d681SAndroid Build Coastguard Worker static bool useSinCos(SDNode *Node) {
2180*9880d681SAndroid Build Coastguard Worker unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
2181*9880d681SAndroid Build Coastguard Worker ? ISD::FCOS : ISD::FSIN;
2182*9880d681SAndroid Build Coastguard Worker
2183*9880d681SAndroid Build Coastguard Worker SDValue Op0 = Node->getOperand(0);
2184*9880d681SAndroid Build Coastguard Worker for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2185*9880d681SAndroid Build Coastguard Worker UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2186*9880d681SAndroid Build Coastguard Worker SDNode *User = *UI;
2187*9880d681SAndroid Build Coastguard Worker if (User == Node)
2188*9880d681SAndroid Build Coastguard Worker continue;
2189*9880d681SAndroid Build Coastguard Worker // The other user might have been turned into sincos already.
2190*9880d681SAndroid Build Coastguard Worker if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
2191*9880d681SAndroid Build Coastguard Worker return true;
2192*9880d681SAndroid Build Coastguard Worker }
2193*9880d681SAndroid Build Coastguard Worker return false;
2194*9880d681SAndroid Build Coastguard Worker }
2195*9880d681SAndroid Build Coastguard Worker
2196*9880d681SAndroid Build Coastguard Worker /// Issue libcalls to sincos to compute sin / cos pairs.
2197*9880d681SAndroid Build Coastguard Worker void
ExpandSinCosLibCall(SDNode * Node,SmallVectorImpl<SDValue> & Results)2198*9880d681SAndroid Build Coastguard Worker SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
2199*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<SDValue> &Results) {
2200*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall LC;
2201*9880d681SAndroid Build Coastguard Worker switch (Node->getSimpleValueType(0).SimpleTy) {
2202*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unexpected request for libcall!");
2203*9880d681SAndroid Build Coastguard Worker case MVT::f32: LC = RTLIB::SINCOS_F32; break;
2204*9880d681SAndroid Build Coastguard Worker case MVT::f64: LC = RTLIB::SINCOS_F64; break;
2205*9880d681SAndroid Build Coastguard Worker case MVT::f80: LC = RTLIB::SINCOS_F80; break;
2206*9880d681SAndroid Build Coastguard Worker case MVT::f128: LC = RTLIB::SINCOS_F128; break;
2207*9880d681SAndroid Build Coastguard Worker case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2208*9880d681SAndroid Build Coastguard Worker }
2209*9880d681SAndroid Build Coastguard Worker
2210*9880d681SAndroid Build Coastguard Worker // The input chain to this libcall is the entry node of the function.
2211*9880d681SAndroid Build Coastguard Worker // Legalizing the call will automatically add the previous call to the
2212*9880d681SAndroid Build Coastguard Worker // dependence.
2213*9880d681SAndroid Build Coastguard Worker SDValue InChain = DAG.getEntryNode();
2214*9880d681SAndroid Build Coastguard Worker
2215*9880d681SAndroid Build Coastguard Worker EVT RetVT = Node->getValueType(0);
2216*9880d681SAndroid Build Coastguard Worker Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2217*9880d681SAndroid Build Coastguard Worker
2218*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListTy Args;
2219*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListEntry Entry;
2220*9880d681SAndroid Build Coastguard Worker
2221*9880d681SAndroid Build Coastguard Worker // Pass the argument.
2222*9880d681SAndroid Build Coastguard Worker Entry.Node = Node->getOperand(0);
2223*9880d681SAndroid Build Coastguard Worker Entry.Ty = RetTy;
2224*9880d681SAndroid Build Coastguard Worker Entry.isSExt = false;
2225*9880d681SAndroid Build Coastguard Worker Entry.isZExt = false;
2226*9880d681SAndroid Build Coastguard Worker Args.push_back(Entry);
2227*9880d681SAndroid Build Coastguard Worker
2228*9880d681SAndroid Build Coastguard Worker // Pass the return address of sin.
2229*9880d681SAndroid Build Coastguard Worker SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
2230*9880d681SAndroid Build Coastguard Worker Entry.Node = SinPtr;
2231*9880d681SAndroid Build Coastguard Worker Entry.Ty = RetTy->getPointerTo();
2232*9880d681SAndroid Build Coastguard Worker Entry.isSExt = false;
2233*9880d681SAndroid Build Coastguard Worker Entry.isZExt = false;
2234*9880d681SAndroid Build Coastguard Worker Args.push_back(Entry);
2235*9880d681SAndroid Build Coastguard Worker
2236*9880d681SAndroid Build Coastguard Worker // Also pass the return address of the cos.
2237*9880d681SAndroid Build Coastguard Worker SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
2238*9880d681SAndroid Build Coastguard Worker Entry.Node = CosPtr;
2239*9880d681SAndroid Build Coastguard Worker Entry.Ty = RetTy->getPointerTo();
2240*9880d681SAndroid Build Coastguard Worker Entry.isSExt = false;
2241*9880d681SAndroid Build Coastguard Worker Entry.isZExt = false;
2242*9880d681SAndroid Build Coastguard Worker Args.push_back(Entry);
2243*9880d681SAndroid Build Coastguard Worker
2244*9880d681SAndroid Build Coastguard Worker SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2245*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout()));
2246*9880d681SAndroid Build Coastguard Worker
2247*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
2248*9880d681SAndroid Build Coastguard Worker TargetLowering::CallLoweringInfo CLI(DAG);
2249*9880d681SAndroid Build Coastguard Worker CLI.setDebugLoc(dl).setChain(InChain)
2250*9880d681SAndroid Build Coastguard Worker .setCallee(TLI.getLibcallCallingConv(LC),
2251*9880d681SAndroid Build Coastguard Worker Type::getVoidTy(*DAG.getContext()), Callee, std::move(Args));
2252*9880d681SAndroid Build Coastguard Worker
2253*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2254*9880d681SAndroid Build Coastguard Worker
2255*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr,
2256*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(), false, false, false, 0));
2257*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr,
2258*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(), false, false, false, 0));
2259*9880d681SAndroid Build Coastguard Worker }
2260*9880d681SAndroid Build Coastguard Worker
2261*9880d681SAndroid Build Coastguard Worker /// This function is responsible for legalizing a
2262*9880d681SAndroid Build Coastguard Worker /// INT_TO_FP operation of the specified operand when the target requests that
2263*9880d681SAndroid Build Coastguard Worker /// we expand it. At this point, we know that the result and operand types are
2264*9880d681SAndroid Build Coastguard Worker /// legal for the target.
ExpandLegalINT_TO_FP(bool isSigned,SDValue Op0,EVT DestVT,const SDLoc & dl)2265*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, SDValue Op0,
2266*9880d681SAndroid Build Coastguard Worker EVT DestVT,
2267*9880d681SAndroid Build Coastguard Worker const SDLoc &dl) {
2268*9880d681SAndroid Build Coastguard Worker // TODO: Should any fast-math-flags be set for the created nodes?
2269*9880d681SAndroid Build Coastguard Worker
2270*9880d681SAndroid Build Coastguard Worker if (Op0.getValueType() == MVT::i32 && TLI.isTypeLegal(MVT::f64)) {
2271*9880d681SAndroid Build Coastguard Worker // simple 32-bit [signed|unsigned] integer to float/double expansion
2272*9880d681SAndroid Build Coastguard Worker
2273*9880d681SAndroid Build Coastguard Worker // Get the stack frame index of a 8 byte buffer.
2274*9880d681SAndroid Build Coastguard Worker SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
2275*9880d681SAndroid Build Coastguard Worker
2276*9880d681SAndroid Build Coastguard Worker // word offset constant for Hi/Lo address computation
2277*9880d681SAndroid Build Coastguard Worker SDValue WordOff = DAG.getConstant(sizeof(int), dl,
2278*9880d681SAndroid Build Coastguard Worker StackSlot.getValueType());
2279*9880d681SAndroid Build Coastguard Worker // set up Hi and Lo (into buffer) address based on endian
2280*9880d681SAndroid Build Coastguard Worker SDValue Hi = StackSlot;
2281*9880d681SAndroid Build Coastguard Worker SDValue Lo = DAG.getNode(ISD::ADD, dl, StackSlot.getValueType(),
2282*9880d681SAndroid Build Coastguard Worker StackSlot, WordOff);
2283*9880d681SAndroid Build Coastguard Worker if (DAG.getDataLayout().isLittleEndian())
2284*9880d681SAndroid Build Coastguard Worker std::swap(Hi, Lo);
2285*9880d681SAndroid Build Coastguard Worker
2286*9880d681SAndroid Build Coastguard Worker // if signed map to unsigned space
2287*9880d681SAndroid Build Coastguard Worker SDValue Op0Mapped;
2288*9880d681SAndroid Build Coastguard Worker if (isSigned) {
2289*9880d681SAndroid Build Coastguard Worker // constant used to invert sign bit (signed to unsigned mapping)
2290*9880d681SAndroid Build Coastguard Worker SDValue SignBit = DAG.getConstant(0x80000000u, dl, MVT::i32);
2291*9880d681SAndroid Build Coastguard Worker Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
2292*9880d681SAndroid Build Coastguard Worker } else {
2293*9880d681SAndroid Build Coastguard Worker Op0Mapped = Op0;
2294*9880d681SAndroid Build Coastguard Worker }
2295*9880d681SAndroid Build Coastguard Worker // store the lo of the constructed double - based on integer input
2296*9880d681SAndroid Build Coastguard Worker SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
2297*9880d681SAndroid Build Coastguard Worker Op0Mapped, Lo, MachinePointerInfo(),
2298*9880d681SAndroid Build Coastguard Worker false, false, 0);
2299*9880d681SAndroid Build Coastguard Worker // initial hi portion of constructed double
2300*9880d681SAndroid Build Coastguard Worker SDValue InitialHi = DAG.getConstant(0x43300000u, dl, MVT::i32);
2301*9880d681SAndroid Build Coastguard Worker // store the hi of the constructed double - biased exponent
2302*9880d681SAndroid Build Coastguard Worker SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2303*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(),
2304*9880d681SAndroid Build Coastguard Worker false, false, 0);
2305*9880d681SAndroid Build Coastguard Worker // load the constructed double
2306*9880d681SAndroid Build Coastguard Worker SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2307*9880d681SAndroid Build Coastguard Worker MachinePointerInfo(), false, false, false, 0);
2308*9880d681SAndroid Build Coastguard Worker // FP constant to bias correct the final result
2309*9880d681SAndroid Build Coastguard Worker SDValue Bias = DAG.getConstantFP(isSigned ?
2310*9880d681SAndroid Build Coastguard Worker BitsToDouble(0x4330000080000000ULL) :
2311*9880d681SAndroid Build Coastguard Worker BitsToDouble(0x4330000000000000ULL),
2312*9880d681SAndroid Build Coastguard Worker dl, MVT::f64);
2313*9880d681SAndroid Build Coastguard Worker // subtract the bias
2314*9880d681SAndroid Build Coastguard Worker SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2315*9880d681SAndroid Build Coastguard Worker // final result
2316*9880d681SAndroid Build Coastguard Worker SDValue Result;
2317*9880d681SAndroid Build Coastguard Worker // handle final rounding
2318*9880d681SAndroid Build Coastguard Worker if (DestVT == MVT::f64) {
2319*9880d681SAndroid Build Coastguard Worker // do nothing
2320*9880d681SAndroid Build Coastguard Worker Result = Sub;
2321*9880d681SAndroid Build Coastguard Worker } else if (DestVT.bitsLT(MVT::f64)) {
2322*9880d681SAndroid Build Coastguard Worker Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
2323*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(0, dl));
2324*9880d681SAndroid Build Coastguard Worker } else if (DestVT.bitsGT(MVT::f64)) {
2325*9880d681SAndroid Build Coastguard Worker Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
2326*9880d681SAndroid Build Coastguard Worker }
2327*9880d681SAndroid Build Coastguard Worker return Result;
2328*9880d681SAndroid Build Coastguard Worker }
2329*9880d681SAndroid Build Coastguard Worker assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
2330*9880d681SAndroid Build Coastguard Worker // Code below here assumes !isSigned without checking again.
2331*9880d681SAndroid Build Coastguard Worker
2332*9880d681SAndroid Build Coastguard Worker // Implementation of unsigned i64 to f64 following the algorithm in
2333*9880d681SAndroid Build Coastguard Worker // __floatundidf in compiler_rt. This implementation has the advantage
2334*9880d681SAndroid Build Coastguard Worker // of performing rounding correctly, both in the default rounding mode
2335*9880d681SAndroid Build Coastguard Worker // and in all alternate rounding modes.
2336*9880d681SAndroid Build Coastguard Worker // TODO: Generalize this for use with other types.
2337*9880d681SAndroid Build Coastguard Worker if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2338*9880d681SAndroid Build Coastguard Worker SDValue TwoP52 =
2339*9880d681SAndroid Build Coastguard Worker DAG.getConstant(UINT64_C(0x4330000000000000), dl, MVT::i64);
2340*9880d681SAndroid Build Coastguard Worker SDValue TwoP84PlusTwoP52 =
2341*9880d681SAndroid Build Coastguard Worker DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), dl,
2342*9880d681SAndroid Build Coastguard Worker MVT::f64);
2343*9880d681SAndroid Build Coastguard Worker SDValue TwoP84 =
2344*9880d681SAndroid Build Coastguard Worker DAG.getConstant(UINT64_C(0x4530000000000000), dl, MVT::i64);
2345*9880d681SAndroid Build Coastguard Worker
2346*9880d681SAndroid Build Coastguard Worker SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2347*9880d681SAndroid Build Coastguard Worker SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2348*9880d681SAndroid Build Coastguard Worker DAG.getConstant(32, dl, MVT::i64));
2349*9880d681SAndroid Build Coastguard Worker SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2350*9880d681SAndroid Build Coastguard Worker SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
2351*9880d681SAndroid Build Coastguard Worker SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2352*9880d681SAndroid Build Coastguard Worker SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
2353*9880d681SAndroid Build Coastguard Worker SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2354*9880d681SAndroid Build Coastguard Worker TwoP84PlusTwoP52);
2355*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2356*9880d681SAndroid Build Coastguard Worker }
2357*9880d681SAndroid Build Coastguard Worker
2358*9880d681SAndroid Build Coastguard Worker // Implementation of unsigned i64 to f32.
2359*9880d681SAndroid Build Coastguard Worker // TODO: Generalize this for use with other types.
2360*9880d681SAndroid Build Coastguard Worker if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
2361*9880d681SAndroid Build Coastguard Worker // For unsigned conversions, convert them to signed conversions using the
2362*9880d681SAndroid Build Coastguard Worker // algorithm from the x86_64 __floatundidf in compiler_rt.
2363*9880d681SAndroid Build Coastguard Worker if (!isSigned) {
2364*9880d681SAndroid Build Coastguard Worker SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
2365*9880d681SAndroid Build Coastguard Worker
2366*9880d681SAndroid Build Coastguard Worker SDValue ShiftConst = DAG.getConstant(
2367*9880d681SAndroid Build Coastguard Worker 1, dl, TLI.getShiftAmountTy(Op0.getValueType(), DAG.getDataLayout()));
2368*9880d681SAndroid Build Coastguard Worker SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2369*9880d681SAndroid Build Coastguard Worker SDValue AndConst = DAG.getConstant(1, dl, MVT::i64);
2370*9880d681SAndroid Build Coastguard Worker SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2371*9880d681SAndroid Build Coastguard Worker SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
2372*9880d681SAndroid Build Coastguard Worker
2373*9880d681SAndroid Build Coastguard Worker SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2374*9880d681SAndroid Build Coastguard Worker SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
2375*9880d681SAndroid Build Coastguard Worker
2376*9880d681SAndroid Build Coastguard Worker // TODO: This really should be implemented using a branch rather than a
2377*9880d681SAndroid Build Coastguard Worker // select. We happen to get lucky and machinesink does the right
2378*9880d681SAndroid Build Coastguard Worker // thing most of the time. This would be a good candidate for a
2379*9880d681SAndroid Build Coastguard Worker //pseudo-op, or, even better, for whole-function isel.
2380*9880d681SAndroid Build Coastguard Worker SDValue SignBitTest = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
2381*9880d681SAndroid Build Coastguard Worker Op0, DAG.getConstant(0, dl, MVT::i64), ISD::SETLT);
2382*9880d681SAndroid Build Coastguard Worker return DAG.getSelect(dl, MVT::f32, SignBitTest, Slow, Fast);
2383*9880d681SAndroid Build Coastguard Worker }
2384*9880d681SAndroid Build Coastguard Worker
2385*9880d681SAndroid Build Coastguard Worker // Otherwise, implement the fully general conversion.
2386*9880d681SAndroid Build Coastguard Worker
2387*9880d681SAndroid Build Coastguard Worker SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
2388*9880d681SAndroid Build Coastguard Worker DAG.getConstant(UINT64_C(0xfffffffffffff800), dl, MVT::i64));
2389*9880d681SAndroid Build Coastguard Worker SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2390*9880d681SAndroid Build Coastguard Worker DAG.getConstant(UINT64_C(0x800), dl, MVT::i64));
2391*9880d681SAndroid Build Coastguard Worker SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
2392*9880d681SAndroid Build Coastguard Worker DAG.getConstant(UINT64_C(0x7ff), dl, MVT::i64));
2393*9880d681SAndroid Build Coastguard Worker SDValue Ne = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), And2,
2394*9880d681SAndroid Build Coastguard Worker DAG.getConstant(UINT64_C(0), dl, MVT::i64),
2395*9880d681SAndroid Build Coastguard Worker ISD::SETNE);
2396*9880d681SAndroid Build Coastguard Worker SDValue Sel = DAG.getSelect(dl, MVT::i64, Ne, Or, Op0);
2397*9880d681SAndroid Build Coastguard Worker SDValue Ge = DAG.getSetCC(dl, getSetCCResultType(MVT::i64), Op0,
2398*9880d681SAndroid Build Coastguard Worker DAG.getConstant(UINT64_C(0x0020000000000000), dl,
2399*9880d681SAndroid Build Coastguard Worker MVT::i64),
2400*9880d681SAndroid Build Coastguard Worker ISD::SETUGE);
2401*9880d681SAndroid Build Coastguard Worker SDValue Sel2 = DAG.getSelect(dl, MVT::i64, Ge, Sel, Op0);
2402*9880d681SAndroid Build Coastguard Worker EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType(), DAG.getDataLayout());
2403*9880d681SAndroid Build Coastguard Worker
2404*9880d681SAndroid Build Coastguard Worker SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2405*9880d681SAndroid Build Coastguard Worker DAG.getConstant(32, dl, SHVT));
2406*9880d681SAndroid Build Coastguard Worker SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2407*9880d681SAndroid Build Coastguard Worker SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2408*9880d681SAndroid Build Coastguard Worker SDValue TwoP32 =
2409*9880d681SAndroid Build Coastguard Worker DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), dl,
2410*9880d681SAndroid Build Coastguard Worker MVT::f64);
2411*9880d681SAndroid Build Coastguard Worker SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2412*9880d681SAndroid Build Coastguard Worker SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2413*9880d681SAndroid Build Coastguard Worker SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2414*9880d681SAndroid Build Coastguard Worker SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2415*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2416*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(0, dl));
2417*9880d681SAndroid Build Coastguard Worker }
2418*9880d681SAndroid Build Coastguard Worker
2419*9880d681SAndroid Build Coastguard Worker SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2420*9880d681SAndroid Build Coastguard Worker
2421*9880d681SAndroid Build Coastguard Worker SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(Op0.getValueType()),
2422*9880d681SAndroid Build Coastguard Worker Op0,
2423*9880d681SAndroid Build Coastguard Worker DAG.getConstant(0, dl, Op0.getValueType()),
2424*9880d681SAndroid Build Coastguard Worker ISD::SETLT);
2425*9880d681SAndroid Build Coastguard Worker SDValue Zero = DAG.getIntPtrConstant(0, dl),
2426*9880d681SAndroid Build Coastguard Worker Four = DAG.getIntPtrConstant(4, dl);
2427*9880d681SAndroid Build Coastguard Worker SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
2428*9880d681SAndroid Build Coastguard Worker SignSet, Four, Zero);
2429*9880d681SAndroid Build Coastguard Worker
2430*9880d681SAndroid Build Coastguard Worker // If the sign bit of the integer is set, the large number will be treated
2431*9880d681SAndroid Build Coastguard Worker // as a negative number. To counteract this, the dynamic code adds an
2432*9880d681SAndroid Build Coastguard Worker // offset depending on the data type.
2433*9880d681SAndroid Build Coastguard Worker uint64_t FF;
2434*9880d681SAndroid Build Coastguard Worker switch (Op0.getSimpleValueType().SimpleTy) {
2435*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unsupported integer type!");
2436*9880d681SAndroid Build Coastguard Worker case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2437*9880d681SAndroid Build Coastguard Worker case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2438*9880d681SAndroid Build Coastguard Worker case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2439*9880d681SAndroid Build Coastguard Worker case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2440*9880d681SAndroid Build Coastguard Worker }
2441*9880d681SAndroid Build Coastguard Worker if (DAG.getDataLayout().isLittleEndian())
2442*9880d681SAndroid Build Coastguard Worker FF <<= 32;
2443*9880d681SAndroid Build Coastguard Worker Constant *FudgeFactor = ConstantInt::get(
2444*9880d681SAndroid Build Coastguard Worker Type::getInt64Ty(*DAG.getContext()), FF);
2445*9880d681SAndroid Build Coastguard Worker
2446*9880d681SAndroid Build Coastguard Worker SDValue CPIdx =
2447*9880d681SAndroid Build Coastguard Worker DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
2448*9880d681SAndroid Build Coastguard Worker unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2449*9880d681SAndroid Build Coastguard Worker CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
2450*9880d681SAndroid Build Coastguard Worker Alignment = std::min(Alignment, 4u);
2451*9880d681SAndroid Build Coastguard Worker SDValue FudgeInReg;
2452*9880d681SAndroid Build Coastguard Worker if (DestVT == MVT::f32)
2453*9880d681SAndroid Build Coastguard Worker FudgeInReg = DAG.getLoad(
2454*9880d681SAndroid Build Coastguard Worker MVT::f32, dl, DAG.getEntryNode(), CPIdx,
2455*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2456*9880d681SAndroid Build Coastguard Worker false, false, Alignment);
2457*9880d681SAndroid Build Coastguard Worker else {
2458*9880d681SAndroid Build Coastguard Worker SDValue Load = DAG.getExtLoad(
2459*9880d681SAndroid Build Coastguard Worker ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
2460*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
2461*9880d681SAndroid Build Coastguard Worker false, false, false, Alignment);
2462*9880d681SAndroid Build Coastguard Worker HandleSDNode Handle(Load);
2463*9880d681SAndroid Build Coastguard Worker LegalizeOp(Load.getNode());
2464*9880d681SAndroid Build Coastguard Worker FudgeInReg = Handle.getValue();
2465*9880d681SAndroid Build Coastguard Worker }
2466*9880d681SAndroid Build Coastguard Worker
2467*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
2468*9880d681SAndroid Build Coastguard Worker }
2469*9880d681SAndroid Build Coastguard Worker
2470*9880d681SAndroid Build Coastguard Worker /// This function is responsible for legalizing a
2471*9880d681SAndroid Build Coastguard Worker /// *INT_TO_FP operation of the specified operand when the target requests that
2472*9880d681SAndroid Build Coastguard Worker /// we promote it. At this point, we know that the result and operand types are
2473*9880d681SAndroid Build Coastguard Worker /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2474*9880d681SAndroid Build Coastguard Worker /// operation that takes a larger input.
PromoteLegalINT_TO_FP(SDValue LegalOp,EVT DestVT,bool isSigned,const SDLoc & dl)2475*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT,
2476*9880d681SAndroid Build Coastguard Worker bool isSigned,
2477*9880d681SAndroid Build Coastguard Worker const SDLoc &dl) {
2478*9880d681SAndroid Build Coastguard Worker // First step, figure out the appropriate *INT_TO_FP operation to use.
2479*9880d681SAndroid Build Coastguard Worker EVT NewInTy = LegalOp.getValueType();
2480*9880d681SAndroid Build Coastguard Worker
2481*9880d681SAndroid Build Coastguard Worker unsigned OpToUse = 0;
2482*9880d681SAndroid Build Coastguard Worker
2483*9880d681SAndroid Build Coastguard Worker // Scan for the appropriate larger type to use.
2484*9880d681SAndroid Build Coastguard Worker while (1) {
2485*9880d681SAndroid Build Coastguard Worker NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
2486*9880d681SAndroid Build Coastguard Worker assert(NewInTy.isInteger() && "Ran out of possibilities!");
2487*9880d681SAndroid Build Coastguard Worker
2488*9880d681SAndroid Build Coastguard Worker // If the target supports SINT_TO_FP of this type, use it.
2489*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2490*9880d681SAndroid Build Coastguard Worker OpToUse = ISD::SINT_TO_FP;
2491*9880d681SAndroid Build Coastguard Worker break;
2492*9880d681SAndroid Build Coastguard Worker }
2493*9880d681SAndroid Build Coastguard Worker if (isSigned) continue;
2494*9880d681SAndroid Build Coastguard Worker
2495*9880d681SAndroid Build Coastguard Worker // If the target supports UINT_TO_FP of this type, use it.
2496*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2497*9880d681SAndroid Build Coastguard Worker OpToUse = ISD::UINT_TO_FP;
2498*9880d681SAndroid Build Coastguard Worker break;
2499*9880d681SAndroid Build Coastguard Worker }
2500*9880d681SAndroid Build Coastguard Worker
2501*9880d681SAndroid Build Coastguard Worker // Otherwise, try a larger type.
2502*9880d681SAndroid Build Coastguard Worker }
2503*9880d681SAndroid Build Coastguard Worker
2504*9880d681SAndroid Build Coastguard Worker // Okay, we found the operation and type to use. Zero extend our input to the
2505*9880d681SAndroid Build Coastguard Worker // desired type then run the operation on it.
2506*9880d681SAndroid Build Coastguard Worker return DAG.getNode(OpToUse, dl, DestVT,
2507*9880d681SAndroid Build Coastguard Worker DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2508*9880d681SAndroid Build Coastguard Worker dl, NewInTy, LegalOp));
2509*9880d681SAndroid Build Coastguard Worker }
2510*9880d681SAndroid Build Coastguard Worker
2511*9880d681SAndroid Build Coastguard Worker /// This function is responsible for legalizing a
2512*9880d681SAndroid Build Coastguard Worker /// FP_TO_*INT operation of the specified operand when the target requests that
2513*9880d681SAndroid Build Coastguard Worker /// we promote it. At this point, we know that the result and operand types are
2514*9880d681SAndroid Build Coastguard Worker /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2515*9880d681SAndroid Build Coastguard Worker /// operation that returns a larger result.
PromoteLegalFP_TO_INT(SDValue LegalOp,EVT DestVT,bool isSigned,const SDLoc & dl)2516*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT,
2517*9880d681SAndroid Build Coastguard Worker bool isSigned,
2518*9880d681SAndroid Build Coastguard Worker const SDLoc &dl) {
2519*9880d681SAndroid Build Coastguard Worker // First step, figure out the appropriate FP_TO*INT operation to use.
2520*9880d681SAndroid Build Coastguard Worker EVT NewOutTy = DestVT;
2521*9880d681SAndroid Build Coastguard Worker
2522*9880d681SAndroid Build Coastguard Worker unsigned OpToUse = 0;
2523*9880d681SAndroid Build Coastguard Worker
2524*9880d681SAndroid Build Coastguard Worker // Scan for the appropriate larger type to use.
2525*9880d681SAndroid Build Coastguard Worker while (1) {
2526*9880d681SAndroid Build Coastguard Worker NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
2527*9880d681SAndroid Build Coastguard Worker assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2528*9880d681SAndroid Build Coastguard Worker
2529*9880d681SAndroid Build Coastguard Worker // A larger signed type can hold all unsigned values of the requested type,
2530*9880d681SAndroid Build Coastguard Worker // so using FP_TO_SINT is valid
2531*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
2532*9880d681SAndroid Build Coastguard Worker OpToUse = ISD::FP_TO_SINT;
2533*9880d681SAndroid Build Coastguard Worker break;
2534*9880d681SAndroid Build Coastguard Worker }
2535*9880d681SAndroid Build Coastguard Worker
2536*9880d681SAndroid Build Coastguard Worker // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2537*9880d681SAndroid Build Coastguard Worker if (!isSigned && TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
2538*9880d681SAndroid Build Coastguard Worker OpToUse = ISD::FP_TO_UINT;
2539*9880d681SAndroid Build Coastguard Worker break;
2540*9880d681SAndroid Build Coastguard Worker }
2541*9880d681SAndroid Build Coastguard Worker
2542*9880d681SAndroid Build Coastguard Worker // Otherwise, try a larger type.
2543*9880d681SAndroid Build Coastguard Worker }
2544*9880d681SAndroid Build Coastguard Worker
2545*9880d681SAndroid Build Coastguard Worker
2546*9880d681SAndroid Build Coastguard Worker // Okay, we found the operation and type to use.
2547*9880d681SAndroid Build Coastguard Worker SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
2548*9880d681SAndroid Build Coastguard Worker
2549*9880d681SAndroid Build Coastguard Worker // Truncate the result of the extended FP_TO_*INT operation to the desired
2550*9880d681SAndroid Build Coastguard Worker // size.
2551*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2552*9880d681SAndroid Build Coastguard Worker }
2553*9880d681SAndroid Build Coastguard Worker
2554*9880d681SAndroid Build Coastguard Worker /// Open code the operations for BITREVERSE.
ExpandBITREVERSE(SDValue Op,const SDLoc & dl)2555*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandBITREVERSE(SDValue Op, const SDLoc &dl) {
2556*9880d681SAndroid Build Coastguard Worker EVT VT = Op.getValueType();
2557*9880d681SAndroid Build Coastguard Worker EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2558*9880d681SAndroid Build Coastguard Worker unsigned Sz = VT.getScalarSizeInBits();
2559*9880d681SAndroid Build Coastguard Worker
2560*9880d681SAndroid Build Coastguard Worker SDValue Tmp, Tmp2;
2561*9880d681SAndroid Build Coastguard Worker Tmp = DAG.getConstant(0, dl, VT);
2562*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, J = Sz-1; I < Sz; ++I, --J) {
2563*9880d681SAndroid Build Coastguard Worker if (I < J)
2564*9880d681SAndroid Build Coastguard Worker Tmp2 =
2565*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(J - I, dl, SHVT));
2566*9880d681SAndroid Build Coastguard Worker else
2567*9880d681SAndroid Build Coastguard Worker Tmp2 =
2568*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(I - J, dl, SHVT));
2569*9880d681SAndroid Build Coastguard Worker
2570*9880d681SAndroid Build Coastguard Worker APInt Shift(Sz, 1);
2571*9880d681SAndroid Build Coastguard Worker Shift = Shift.shl(J);
2572*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(Shift, dl, VT));
2573*9880d681SAndroid Build Coastguard Worker Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp, Tmp2);
2574*9880d681SAndroid Build Coastguard Worker }
2575*9880d681SAndroid Build Coastguard Worker
2576*9880d681SAndroid Build Coastguard Worker return Tmp;
2577*9880d681SAndroid Build Coastguard Worker }
2578*9880d681SAndroid Build Coastguard Worker
2579*9880d681SAndroid Build Coastguard Worker /// Open code the operations for BSWAP of the specified operation.
ExpandBSWAP(SDValue Op,const SDLoc & dl)2580*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, const SDLoc &dl) {
2581*9880d681SAndroid Build Coastguard Worker EVT VT = Op.getValueType();
2582*9880d681SAndroid Build Coastguard Worker EVT SHVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2583*9880d681SAndroid Build Coastguard Worker SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
2584*9880d681SAndroid Build Coastguard Worker switch (VT.getSimpleVT().SimpleTy) {
2585*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Unhandled Expand type in BSWAP!");
2586*9880d681SAndroid Build Coastguard Worker case MVT::i16:
2587*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2588*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2589*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2590*9880d681SAndroid Build Coastguard Worker case MVT::i32:
2591*9880d681SAndroid Build Coastguard Worker Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2592*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2593*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2594*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2595*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
2596*9880d681SAndroid Build Coastguard Worker DAG.getConstant(0xFF0000, dl, VT));
2597*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, dl, VT));
2598*9880d681SAndroid Build Coastguard Worker Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2599*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2600*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2601*9880d681SAndroid Build Coastguard Worker case MVT::i64:
2602*9880d681SAndroid Build Coastguard Worker Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
2603*9880d681SAndroid Build Coastguard Worker Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
2604*9880d681SAndroid Build Coastguard Worker Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2605*9880d681SAndroid Build Coastguard Worker Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2606*9880d681SAndroid Build Coastguard Worker Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
2607*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
2608*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
2609*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
2610*9880d681SAndroid Build Coastguard Worker Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7,
2611*9880d681SAndroid Build Coastguard Worker DAG.getConstant(255ULL<<48, dl, VT));
2612*9880d681SAndroid Build Coastguard Worker Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6,
2613*9880d681SAndroid Build Coastguard Worker DAG.getConstant(255ULL<<40, dl, VT));
2614*9880d681SAndroid Build Coastguard Worker Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5,
2615*9880d681SAndroid Build Coastguard Worker DAG.getConstant(255ULL<<32, dl, VT));
2616*9880d681SAndroid Build Coastguard Worker Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4,
2617*9880d681SAndroid Build Coastguard Worker DAG.getConstant(255ULL<<24, dl, VT));
2618*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
2619*9880d681SAndroid Build Coastguard Worker DAG.getConstant(255ULL<<16, dl, VT));
2620*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2,
2621*9880d681SAndroid Build Coastguard Worker DAG.getConstant(255ULL<<8 , dl, VT));
2622*9880d681SAndroid Build Coastguard Worker Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2623*9880d681SAndroid Build Coastguard Worker Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2624*9880d681SAndroid Build Coastguard Worker Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2625*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2626*9880d681SAndroid Build Coastguard Worker Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2627*9880d681SAndroid Build Coastguard Worker Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2628*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
2629*9880d681SAndroid Build Coastguard Worker }
2630*9880d681SAndroid Build Coastguard Worker }
2631*9880d681SAndroid Build Coastguard Worker
2632*9880d681SAndroid Build Coastguard Worker /// Expand the specified bitcount instruction into operations.
ExpandBitCount(unsigned Opc,SDValue Op,const SDLoc & dl)2633*9880d681SAndroid Build Coastguard Worker SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
2634*9880d681SAndroid Build Coastguard Worker const SDLoc &dl) {
2635*9880d681SAndroid Build Coastguard Worker switch (Opc) {
2636*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Cannot expand this yet!");
2637*9880d681SAndroid Build Coastguard Worker case ISD::CTPOP: {
2638*9880d681SAndroid Build Coastguard Worker EVT VT = Op.getValueType();
2639*9880d681SAndroid Build Coastguard Worker EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2640*9880d681SAndroid Build Coastguard Worker unsigned Len = VT.getSizeInBits();
2641*9880d681SAndroid Build Coastguard Worker
2642*9880d681SAndroid Build Coastguard Worker assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2643*9880d681SAndroid Build Coastguard Worker "CTPOP not implemented for this type.");
2644*9880d681SAndroid Build Coastguard Worker
2645*9880d681SAndroid Build Coastguard Worker // This is the "best" algorithm from
2646*9880d681SAndroid Build Coastguard Worker // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2647*9880d681SAndroid Build Coastguard Worker
2648*9880d681SAndroid Build Coastguard Worker SDValue Mask55 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)),
2649*9880d681SAndroid Build Coastguard Worker dl, VT);
2650*9880d681SAndroid Build Coastguard Worker SDValue Mask33 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)),
2651*9880d681SAndroid Build Coastguard Worker dl, VT);
2652*9880d681SAndroid Build Coastguard Worker SDValue Mask0F = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)),
2653*9880d681SAndroid Build Coastguard Worker dl, VT);
2654*9880d681SAndroid Build Coastguard Worker SDValue Mask01 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)),
2655*9880d681SAndroid Build Coastguard Worker dl, VT);
2656*9880d681SAndroid Build Coastguard Worker
2657*9880d681SAndroid Build Coastguard Worker // v = v - ((v >> 1) & 0x55555555...)
2658*9880d681SAndroid Build Coastguard Worker Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2659*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::AND, dl, VT,
2660*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SRL, dl, VT, Op,
2661*9880d681SAndroid Build Coastguard Worker DAG.getConstant(1, dl, ShVT)),
2662*9880d681SAndroid Build Coastguard Worker Mask55));
2663*9880d681SAndroid Build Coastguard Worker // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2664*9880d681SAndroid Build Coastguard Worker Op = DAG.getNode(ISD::ADD, dl, VT,
2665*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2666*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::AND, dl, VT,
2667*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SRL, dl, VT, Op,
2668*9880d681SAndroid Build Coastguard Worker DAG.getConstant(2, dl, ShVT)),
2669*9880d681SAndroid Build Coastguard Worker Mask33));
2670*9880d681SAndroid Build Coastguard Worker // v = (v + (v >> 4)) & 0x0F0F0F0F...
2671*9880d681SAndroid Build Coastguard Worker Op = DAG.getNode(ISD::AND, dl, VT,
2672*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::ADD, dl, VT, Op,
2673*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SRL, dl, VT, Op,
2674*9880d681SAndroid Build Coastguard Worker DAG.getConstant(4, dl, ShVT))),
2675*9880d681SAndroid Build Coastguard Worker Mask0F);
2676*9880d681SAndroid Build Coastguard Worker // v = (v * 0x01010101...) >> (Len - 8)
2677*9880d681SAndroid Build Coastguard Worker Op = DAG.getNode(ISD::SRL, dl, VT,
2678*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2679*9880d681SAndroid Build Coastguard Worker DAG.getConstant(Len - 8, dl, ShVT));
2680*9880d681SAndroid Build Coastguard Worker
2681*9880d681SAndroid Build Coastguard Worker return Op;
2682*9880d681SAndroid Build Coastguard Worker }
2683*9880d681SAndroid Build Coastguard Worker case ISD::CTLZ_ZERO_UNDEF:
2684*9880d681SAndroid Build Coastguard Worker // This trivially expands to CTLZ.
2685*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::CTLZ, dl, Op.getValueType(), Op);
2686*9880d681SAndroid Build Coastguard Worker case ISD::CTLZ: {
2687*9880d681SAndroid Build Coastguard Worker EVT VT = Op.getValueType();
2688*9880d681SAndroid Build Coastguard Worker unsigned len = VT.getSizeInBits();
2689*9880d681SAndroid Build Coastguard Worker
2690*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(ISD::CTLZ_ZERO_UNDEF, VT)) {
2691*9880d681SAndroid Build Coastguard Worker EVT SetCCVT = getSetCCResultType(VT);
2692*9880d681SAndroid Build Coastguard Worker SDValue CTLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, VT, Op);
2693*9880d681SAndroid Build Coastguard Worker SDValue Zero = DAG.getConstant(0, dl, VT);
2694*9880d681SAndroid Build Coastguard Worker SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ);
2695*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero,
2696*9880d681SAndroid Build Coastguard Worker DAG.getConstant(len, dl, VT), CTLZ);
2697*9880d681SAndroid Build Coastguard Worker }
2698*9880d681SAndroid Build Coastguard Worker
2699*9880d681SAndroid Build Coastguard Worker // for now, we do this:
2700*9880d681SAndroid Build Coastguard Worker // x = x | (x >> 1);
2701*9880d681SAndroid Build Coastguard Worker // x = x | (x >> 2);
2702*9880d681SAndroid Build Coastguard Worker // ...
2703*9880d681SAndroid Build Coastguard Worker // x = x | (x >>16);
2704*9880d681SAndroid Build Coastguard Worker // x = x | (x >>32); // for 64-bit input
2705*9880d681SAndroid Build Coastguard Worker // return popcount(~x);
2706*9880d681SAndroid Build Coastguard Worker //
2707*9880d681SAndroid Build Coastguard Worker // Ref: "Hacker's Delight" by Henry Warren
2708*9880d681SAndroid Build Coastguard Worker EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2709*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2710*9880d681SAndroid Build Coastguard Worker SDValue Tmp3 = DAG.getConstant(1ULL << i, dl, ShVT);
2711*9880d681SAndroid Build Coastguard Worker Op = DAG.getNode(ISD::OR, dl, VT, Op,
2712*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
2713*9880d681SAndroid Build Coastguard Worker }
2714*9880d681SAndroid Build Coastguard Worker Op = DAG.getNOT(dl, Op, VT);
2715*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::CTPOP, dl, VT, Op);
2716*9880d681SAndroid Build Coastguard Worker }
2717*9880d681SAndroid Build Coastguard Worker case ISD::CTTZ_ZERO_UNDEF:
2718*9880d681SAndroid Build Coastguard Worker // This trivially expands to CTTZ.
2719*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::CTTZ, dl, Op.getValueType(), Op);
2720*9880d681SAndroid Build Coastguard Worker case ISD::CTTZ: {
2721*9880d681SAndroid Build Coastguard Worker // for now, we use: { return popcount(~x & (x - 1)); }
2722*9880d681SAndroid Build Coastguard Worker // unless the target has ctlz but not ctpop, in which case we use:
2723*9880d681SAndroid Build Coastguard Worker // { return 32 - nlz(~x & (x-1)); }
2724*9880d681SAndroid Build Coastguard Worker // Ref: "Hacker's Delight" by Henry Warren
2725*9880d681SAndroid Build Coastguard Worker EVT VT = Op.getValueType();
2726*9880d681SAndroid Build Coastguard Worker SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2727*9880d681SAndroid Build Coastguard Worker DAG.getNOT(dl, Op, VT),
2728*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SUB, dl, VT, Op,
2729*9880d681SAndroid Build Coastguard Worker DAG.getConstant(1, dl, VT)));
2730*9880d681SAndroid Build Coastguard Worker // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
2731*9880d681SAndroid Build Coastguard Worker if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2732*9880d681SAndroid Build Coastguard Worker TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
2733*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::SUB, dl, VT,
2734*9880d681SAndroid Build Coastguard Worker DAG.getConstant(VT.getSizeInBits(), dl, VT),
2735*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2736*9880d681SAndroid Build Coastguard Worker return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
2737*9880d681SAndroid Build Coastguard Worker }
2738*9880d681SAndroid Build Coastguard Worker }
2739*9880d681SAndroid Build Coastguard Worker }
2740*9880d681SAndroid Build Coastguard Worker
ExpandNode(SDNode * Node)2741*9880d681SAndroid Build Coastguard Worker bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
2742*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> Results;
2743*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
2744*9880d681SAndroid Build Coastguard Worker SDValue Tmp1, Tmp2, Tmp3, Tmp4;
2745*9880d681SAndroid Build Coastguard Worker bool NeedInvert;
2746*9880d681SAndroid Build Coastguard Worker switch (Node->getOpcode()) {
2747*9880d681SAndroid Build Coastguard Worker case ISD::CTPOP:
2748*9880d681SAndroid Build Coastguard Worker case ISD::CTLZ:
2749*9880d681SAndroid Build Coastguard Worker case ISD::CTLZ_ZERO_UNDEF:
2750*9880d681SAndroid Build Coastguard Worker case ISD::CTTZ:
2751*9880d681SAndroid Build Coastguard Worker case ISD::CTTZ_ZERO_UNDEF:
2752*9880d681SAndroid Build Coastguard Worker Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2753*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2754*9880d681SAndroid Build Coastguard Worker break;
2755*9880d681SAndroid Build Coastguard Worker case ISD::BITREVERSE:
2756*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandBITREVERSE(Node->getOperand(0), dl));
2757*9880d681SAndroid Build Coastguard Worker break;
2758*9880d681SAndroid Build Coastguard Worker case ISD::BSWAP:
2759*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
2760*9880d681SAndroid Build Coastguard Worker break;
2761*9880d681SAndroid Build Coastguard Worker case ISD::FRAMEADDR:
2762*9880d681SAndroid Build Coastguard Worker case ISD::RETURNADDR:
2763*9880d681SAndroid Build Coastguard Worker case ISD::FRAME_TO_ARGS_OFFSET:
2764*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
2765*9880d681SAndroid Build Coastguard Worker break;
2766*9880d681SAndroid Build Coastguard Worker case ISD::FLT_ROUNDS_:
2767*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
2768*9880d681SAndroid Build Coastguard Worker break;
2769*9880d681SAndroid Build Coastguard Worker case ISD::EH_RETURN:
2770*9880d681SAndroid Build Coastguard Worker case ISD::EH_LABEL:
2771*9880d681SAndroid Build Coastguard Worker case ISD::PREFETCH:
2772*9880d681SAndroid Build Coastguard Worker case ISD::VAEND:
2773*9880d681SAndroid Build Coastguard Worker case ISD::EH_SJLJ_LONGJMP:
2774*9880d681SAndroid Build Coastguard Worker // If the target didn't expand these, there's nothing to do, so just
2775*9880d681SAndroid Build Coastguard Worker // preserve the chain and be done.
2776*9880d681SAndroid Build Coastguard Worker Results.push_back(Node->getOperand(0));
2777*9880d681SAndroid Build Coastguard Worker break;
2778*9880d681SAndroid Build Coastguard Worker case ISD::READCYCLECOUNTER:
2779*9880d681SAndroid Build Coastguard Worker // If the target didn't expand this, just return 'zero' and preserve the
2780*9880d681SAndroid Build Coastguard Worker // chain.
2781*9880d681SAndroid Build Coastguard Worker Results.append(Node->getNumValues() - 1,
2782*9880d681SAndroid Build Coastguard Worker DAG.getConstant(0, dl, Node->getValueType(0)));
2783*9880d681SAndroid Build Coastguard Worker Results.push_back(Node->getOperand(0));
2784*9880d681SAndroid Build Coastguard Worker break;
2785*9880d681SAndroid Build Coastguard Worker case ISD::EH_SJLJ_SETJMP:
2786*9880d681SAndroid Build Coastguard Worker // If the target didn't expand this, just return 'zero' and preserve the
2787*9880d681SAndroid Build Coastguard Worker // chain.
2788*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getConstant(0, dl, MVT::i32));
2789*9880d681SAndroid Build Coastguard Worker Results.push_back(Node->getOperand(0));
2790*9880d681SAndroid Build Coastguard Worker break;
2791*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD: {
2792*9880d681SAndroid Build Coastguard Worker // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
2793*9880d681SAndroid Build Coastguard Worker SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
2794*9880d681SAndroid Build Coastguard Worker SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
2795*9880d681SAndroid Build Coastguard Worker SDValue Swap = DAG.getAtomicCmpSwap(
2796*9880d681SAndroid Build Coastguard Worker ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
2797*9880d681SAndroid Build Coastguard Worker Node->getOperand(0), Node->getOperand(1), Zero, Zero,
2798*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getMemOperand(),
2799*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getOrdering(),
2800*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getOrdering(),
2801*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getSynchScope());
2802*9880d681SAndroid Build Coastguard Worker Results.push_back(Swap.getValue(0));
2803*9880d681SAndroid Build Coastguard Worker Results.push_back(Swap.getValue(1));
2804*9880d681SAndroid Build Coastguard Worker break;
2805*9880d681SAndroid Build Coastguard Worker }
2806*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_STORE: {
2807*9880d681SAndroid Build Coastguard Worker // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
2808*9880d681SAndroid Build Coastguard Worker SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
2809*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getMemoryVT(),
2810*9880d681SAndroid Build Coastguard Worker Node->getOperand(0),
2811*9880d681SAndroid Build Coastguard Worker Node->getOperand(1), Node->getOperand(2),
2812*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getMemOperand(),
2813*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getOrdering(),
2814*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getSynchScope());
2815*9880d681SAndroid Build Coastguard Worker Results.push_back(Swap.getValue(1));
2816*9880d681SAndroid Build Coastguard Worker break;
2817*9880d681SAndroid Build Coastguard Worker }
2818*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
2819*9880d681SAndroid Build Coastguard Worker // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
2820*9880d681SAndroid Build Coastguard Worker // splits out the success value as a comparison. Expanding the resulting
2821*9880d681SAndroid Build Coastguard Worker // ATOMIC_CMP_SWAP will produce a libcall.
2822*9880d681SAndroid Build Coastguard Worker SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
2823*9880d681SAndroid Build Coastguard Worker SDValue Res = DAG.getAtomicCmpSwap(
2824*9880d681SAndroid Build Coastguard Worker ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
2825*9880d681SAndroid Build Coastguard Worker Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
2826*9880d681SAndroid Build Coastguard Worker Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand(),
2827*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getSuccessOrdering(),
2828*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getFailureOrdering(),
2829*9880d681SAndroid Build Coastguard Worker cast<AtomicSDNode>(Node)->getSynchScope());
2830*9880d681SAndroid Build Coastguard Worker
2831*9880d681SAndroid Build Coastguard Worker SDValue ExtRes = Res;
2832*9880d681SAndroid Build Coastguard Worker SDValue LHS = Res;
2833*9880d681SAndroid Build Coastguard Worker SDValue RHS = Node->getOperand(1);
2834*9880d681SAndroid Build Coastguard Worker
2835*9880d681SAndroid Build Coastguard Worker EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
2836*9880d681SAndroid Build Coastguard Worker EVT OuterType = Node->getValueType(0);
2837*9880d681SAndroid Build Coastguard Worker switch (TLI.getExtendForAtomicOps()) {
2838*9880d681SAndroid Build Coastguard Worker case ISD::SIGN_EXTEND:
2839*9880d681SAndroid Build Coastguard Worker LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
2840*9880d681SAndroid Build Coastguard Worker DAG.getValueType(AtomicType));
2841*9880d681SAndroid Build Coastguard Worker RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
2842*9880d681SAndroid Build Coastguard Worker Node->getOperand(2), DAG.getValueType(AtomicType));
2843*9880d681SAndroid Build Coastguard Worker ExtRes = LHS;
2844*9880d681SAndroid Build Coastguard Worker break;
2845*9880d681SAndroid Build Coastguard Worker case ISD::ZERO_EXTEND:
2846*9880d681SAndroid Build Coastguard Worker LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
2847*9880d681SAndroid Build Coastguard Worker DAG.getValueType(AtomicType));
2848*9880d681SAndroid Build Coastguard Worker RHS = DAG.getNode(ISD::ZERO_EXTEND, dl, OuterType, Node->getOperand(2));
2849*9880d681SAndroid Build Coastguard Worker ExtRes = LHS;
2850*9880d681SAndroid Build Coastguard Worker break;
2851*9880d681SAndroid Build Coastguard Worker case ISD::ANY_EXTEND:
2852*9880d681SAndroid Build Coastguard Worker LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
2853*9880d681SAndroid Build Coastguard Worker RHS = DAG.getNode(ISD::ZERO_EXTEND, dl, OuterType, Node->getOperand(2));
2854*9880d681SAndroid Build Coastguard Worker break;
2855*9880d681SAndroid Build Coastguard Worker default:
2856*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Invalid atomic op extension");
2857*9880d681SAndroid Build Coastguard Worker }
2858*9880d681SAndroid Build Coastguard Worker
2859*9880d681SAndroid Build Coastguard Worker SDValue Success =
2860*9880d681SAndroid Build Coastguard Worker DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
2861*9880d681SAndroid Build Coastguard Worker
2862*9880d681SAndroid Build Coastguard Worker Results.push_back(ExtRes.getValue(0));
2863*9880d681SAndroid Build Coastguard Worker Results.push_back(Success);
2864*9880d681SAndroid Build Coastguard Worker Results.push_back(Res.getValue(1));
2865*9880d681SAndroid Build Coastguard Worker break;
2866*9880d681SAndroid Build Coastguard Worker }
2867*9880d681SAndroid Build Coastguard Worker case ISD::DYNAMIC_STACKALLOC:
2868*9880d681SAndroid Build Coastguard Worker ExpandDYNAMIC_STACKALLOC(Node, Results);
2869*9880d681SAndroid Build Coastguard Worker break;
2870*9880d681SAndroid Build Coastguard Worker case ISD::MERGE_VALUES:
2871*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < Node->getNumValues(); i++)
2872*9880d681SAndroid Build Coastguard Worker Results.push_back(Node->getOperand(i));
2873*9880d681SAndroid Build Coastguard Worker break;
2874*9880d681SAndroid Build Coastguard Worker case ISD::UNDEF: {
2875*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
2876*9880d681SAndroid Build Coastguard Worker if (VT.isInteger())
2877*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getConstant(0, dl, VT));
2878*9880d681SAndroid Build Coastguard Worker else {
2879*9880d681SAndroid Build Coastguard Worker assert(VT.isFloatingPoint() && "Unknown value type!");
2880*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getConstantFP(0, dl, VT));
2881*9880d681SAndroid Build Coastguard Worker }
2882*9880d681SAndroid Build Coastguard Worker break;
2883*9880d681SAndroid Build Coastguard Worker }
2884*9880d681SAndroid Build Coastguard Worker case ISD::FP_ROUND:
2885*9880d681SAndroid Build Coastguard Worker case ISD::BITCAST:
2886*9880d681SAndroid Build Coastguard Worker Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2887*9880d681SAndroid Build Coastguard Worker Node->getValueType(0), dl);
2888*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2889*9880d681SAndroid Build Coastguard Worker break;
2890*9880d681SAndroid Build Coastguard Worker case ISD::FP_EXTEND:
2891*9880d681SAndroid Build Coastguard Worker Tmp1 = EmitStackConvert(Node->getOperand(0),
2892*9880d681SAndroid Build Coastguard Worker Node->getOperand(0).getValueType(),
2893*9880d681SAndroid Build Coastguard Worker Node->getValueType(0), dl);
2894*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2895*9880d681SAndroid Build Coastguard Worker break;
2896*9880d681SAndroid Build Coastguard Worker case ISD::SIGN_EXTEND_INREG: {
2897*9880d681SAndroid Build Coastguard Worker // NOTE: we could fall back on load/store here too for targets without
2898*9880d681SAndroid Build Coastguard Worker // SAR. However, it is doubtful that any exist.
2899*9880d681SAndroid Build Coastguard Worker EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
2900*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
2901*9880d681SAndroid Build Coastguard Worker EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2902*9880d681SAndroid Build Coastguard Worker if (VT.isVector())
2903*9880d681SAndroid Build Coastguard Worker ShiftAmountTy = VT;
2904*9880d681SAndroid Build Coastguard Worker unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2905*9880d681SAndroid Build Coastguard Worker ExtraVT.getScalarType().getSizeInBits();
2906*9880d681SAndroid Build Coastguard Worker SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
2907*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2908*9880d681SAndroid Build Coastguard Worker Node->getOperand(0), ShiftCst);
2909*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2910*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2911*9880d681SAndroid Build Coastguard Worker break;
2912*9880d681SAndroid Build Coastguard Worker }
2913*9880d681SAndroid Build Coastguard Worker case ISD::FP_ROUND_INREG: {
2914*9880d681SAndroid Build Coastguard Worker // The only way we can lower this is to turn it into a TRUNCSTORE,
2915*9880d681SAndroid Build Coastguard Worker // EXTLOAD pair, targeting a temporary location (a stack slot).
2916*9880d681SAndroid Build Coastguard Worker
2917*9880d681SAndroid Build Coastguard Worker // NOTE: there is a choice here between constantly creating new stack
2918*9880d681SAndroid Build Coastguard Worker // slots and always reusing the same one. We currently always create
2919*9880d681SAndroid Build Coastguard Worker // new ones, as reuse may inhibit scheduling.
2920*9880d681SAndroid Build Coastguard Worker EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
2921*9880d681SAndroid Build Coastguard Worker Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2922*9880d681SAndroid Build Coastguard Worker Node->getValueType(0), dl);
2923*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2924*9880d681SAndroid Build Coastguard Worker break;
2925*9880d681SAndroid Build Coastguard Worker }
2926*9880d681SAndroid Build Coastguard Worker case ISD::SINT_TO_FP:
2927*9880d681SAndroid Build Coastguard Worker case ISD::UINT_TO_FP:
2928*9880d681SAndroid Build Coastguard Worker Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2929*9880d681SAndroid Build Coastguard Worker Node->getOperand(0), Node->getValueType(0), dl);
2930*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2931*9880d681SAndroid Build Coastguard Worker break;
2932*9880d681SAndroid Build Coastguard Worker case ISD::FP_TO_SINT:
2933*9880d681SAndroid Build Coastguard Worker if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
2934*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2935*9880d681SAndroid Build Coastguard Worker break;
2936*9880d681SAndroid Build Coastguard Worker case ISD::FP_TO_UINT: {
2937*9880d681SAndroid Build Coastguard Worker SDValue True, False;
2938*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getOperand(0).getValueType();
2939*9880d681SAndroid Build Coastguard Worker EVT NVT = Node->getValueType(0);
2940*9880d681SAndroid Build Coastguard Worker APFloat apf(DAG.EVTToAPFloatSemantics(VT),
2941*9880d681SAndroid Build Coastguard Worker APInt::getNullValue(VT.getSizeInBits()));
2942*9880d681SAndroid Build Coastguard Worker APInt x = APInt::getSignBit(NVT.getSizeInBits());
2943*9880d681SAndroid Build Coastguard Worker (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2944*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getConstantFP(apf, dl, VT);
2945*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getSetCC(dl, getSetCCResultType(VT),
2946*9880d681SAndroid Build Coastguard Worker Node->getOperand(0),
2947*9880d681SAndroid Build Coastguard Worker Tmp1, ISD::SETLT);
2948*9880d681SAndroid Build Coastguard Worker True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
2949*9880d681SAndroid Build Coastguard Worker // TODO: Should any fast-math-flags be set for the FSUB?
2950*9880d681SAndroid Build Coastguard Worker False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2951*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::FSUB, dl, VT,
2952*9880d681SAndroid Build Coastguard Worker Node->getOperand(0), Tmp1));
2953*9880d681SAndroid Build Coastguard Worker False = DAG.getNode(ISD::XOR, dl, NVT, False,
2954*9880d681SAndroid Build Coastguard Worker DAG.getConstant(x, dl, NVT));
2955*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getSelect(dl, NVT, Tmp2, True, False);
2956*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2957*9880d681SAndroid Build Coastguard Worker break;
2958*9880d681SAndroid Build Coastguard Worker }
2959*9880d681SAndroid Build Coastguard Worker case ISD::VAARG:
2960*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.expandVAArg(Node));
2961*9880d681SAndroid Build Coastguard Worker Results.push_back(Results[0].getValue(1));
2962*9880d681SAndroid Build Coastguard Worker break;
2963*9880d681SAndroid Build Coastguard Worker case ISD::VACOPY:
2964*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.expandVACopy(Node));
2965*9880d681SAndroid Build Coastguard Worker break;
2966*9880d681SAndroid Build Coastguard Worker case ISD::EXTRACT_VECTOR_ELT:
2967*9880d681SAndroid Build Coastguard Worker if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2968*9880d681SAndroid Build Coastguard Worker // This must be an access of the only element. Return it.
2969*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
2970*9880d681SAndroid Build Coastguard Worker Node->getOperand(0));
2971*9880d681SAndroid Build Coastguard Worker else
2972*9880d681SAndroid Build Coastguard Worker Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2973*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
2974*9880d681SAndroid Build Coastguard Worker break;
2975*9880d681SAndroid Build Coastguard Worker case ISD::EXTRACT_SUBVECTOR:
2976*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
2977*9880d681SAndroid Build Coastguard Worker break;
2978*9880d681SAndroid Build Coastguard Worker case ISD::INSERT_SUBVECTOR:
2979*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2980*9880d681SAndroid Build Coastguard Worker break;
2981*9880d681SAndroid Build Coastguard Worker case ISD::CONCAT_VECTORS: {
2982*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandVectorBuildThroughStack(Node));
2983*9880d681SAndroid Build Coastguard Worker break;
2984*9880d681SAndroid Build Coastguard Worker }
2985*9880d681SAndroid Build Coastguard Worker case ISD::SCALAR_TO_VECTOR:
2986*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
2987*9880d681SAndroid Build Coastguard Worker break;
2988*9880d681SAndroid Build Coastguard Worker case ISD::INSERT_VECTOR_ELT:
2989*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2990*9880d681SAndroid Build Coastguard Worker Node->getOperand(1),
2991*9880d681SAndroid Build Coastguard Worker Node->getOperand(2), dl));
2992*9880d681SAndroid Build Coastguard Worker break;
2993*9880d681SAndroid Build Coastguard Worker case ISD::VECTOR_SHUFFLE: {
2994*9880d681SAndroid Build Coastguard Worker SmallVector<int, 32> NewMask;
2995*9880d681SAndroid Build Coastguard Worker ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
2996*9880d681SAndroid Build Coastguard Worker
2997*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
2998*9880d681SAndroid Build Coastguard Worker EVT EltVT = VT.getVectorElementType();
2999*9880d681SAndroid Build Coastguard Worker SDValue Op0 = Node->getOperand(0);
3000*9880d681SAndroid Build Coastguard Worker SDValue Op1 = Node->getOperand(1);
3001*9880d681SAndroid Build Coastguard Worker if (!TLI.isTypeLegal(EltVT)) {
3002*9880d681SAndroid Build Coastguard Worker
3003*9880d681SAndroid Build Coastguard Worker EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
3004*9880d681SAndroid Build Coastguard Worker
3005*9880d681SAndroid Build Coastguard Worker // BUILD_VECTOR operands are allowed to be wider than the element type.
3006*9880d681SAndroid Build Coastguard Worker // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
3007*9880d681SAndroid Build Coastguard Worker // it.
3008*9880d681SAndroid Build Coastguard Worker if (NewEltVT.bitsLT(EltVT)) {
3009*9880d681SAndroid Build Coastguard Worker
3010*9880d681SAndroid Build Coastguard Worker // Convert shuffle node.
3011*9880d681SAndroid Build Coastguard Worker // If original node was v4i64 and the new EltVT is i32,
3012*9880d681SAndroid Build Coastguard Worker // cast operands to v8i32 and re-build the mask.
3013*9880d681SAndroid Build Coastguard Worker
3014*9880d681SAndroid Build Coastguard Worker // Calculate new VT, the size of the new VT should be equal to original.
3015*9880d681SAndroid Build Coastguard Worker EVT NewVT =
3016*9880d681SAndroid Build Coastguard Worker EVT::getVectorVT(*DAG.getContext(), NewEltVT,
3017*9880d681SAndroid Build Coastguard Worker VT.getSizeInBits() / NewEltVT.getSizeInBits());
3018*9880d681SAndroid Build Coastguard Worker assert(NewVT.bitsEq(VT));
3019*9880d681SAndroid Build Coastguard Worker
3020*9880d681SAndroid Build Coastguard Worker // cast operands to new VT
3021*9880d681SAndroid Build Coastguard Worker Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
3022*9880d681SAndroid Build Coastguard Worker Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
3023*9880d681SAndroid Build Coastguard Worker
3024*9880d681SAndroid Build Coastguard Worker // Convert the shuffle mask
3025*9880d681SAndroid Build Coastguard Worker unsigned int factor =
3026*9880d681SAndroid Build Coastguard Worker NewVT.getVectorNumElements()/VT.getVectorNumElements();
3027*9880d681SAndroid Build Coastguard Worker
3028*9880d681SAndroid Build Coastguard Worker // EltVT gets smaller
3029*9880d681SAndroid Build Coastguard Worker assert(factor > 0);
3030*9880d681SAndroid Build Coastguard Worker
3031*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
3032*9880d681SAndroid Build Coastguard Worker if (Mask[i] < 0) {
3033*9880d681SAndroid Build Coastguard Worker for (unsigned fi = 0; fi < factor; ++fi)
3034*9880d681SAndroid Build Coastguard Worker NewMask.push_back(Mask[i]);
3035*9880d681SAndroid Build Coastguard Worker }
3036*9880d681SAndroid Build Coastguard Worker else {
3037*9880d681SAndroid Build Coastguard Worker for (unsigned fi = 0; fi < factor; ++fi)
3038*9880d681SAndroid Build Coastguard Worker NewMask.push_back(Mask[i]*factor+fi);
3039*9880d681SAndroid Build Coastguard Worker }
3040*9880d681SAndroid Build Coastguard Worker }
3041*9880d681SAndroid Build Coastguard Worker Mask = NewMask;
3042*9880d681SAndroid Build Coastguard Worker VT = NewVT;
3043*9880d681SAndroid Build Coastguard Worker }
3044*9880d681SAndroid Build Coastguard Worker EltVT = NewEltVT;
3045*9880d681SAndroid Build Coastguard Worker }
3046*9880d681SAndroid Build Coastguard Worker unsigned NumElems = VT.getVectorNumElements();
3047*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 16> Ops;
3048*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumElems; ++i) {
3049*9880d681SAndroid Build Coastguard Worker if (Mask[i] < 0) {
3050*9880d681SAndroid Build Coastguard Worker Ops.push_back(DAG.getUNDEF(EltVT));
3051*9880d681SAndroid Build Coastguard Worker continue;
3052*9880d681SAndroid Build Coastguard Worker }
3053*9880d681SAndroid Build Coastguard Worker unsigned Idx = Mask[i];
3054*9880d681SAndroid Build Coastguard Worker if (Idx < NumElems)
3055*9880d681SAndroid Build Coastguard Worker Ops.push_back(DAG.getNode(
3056*9880d681SAndroid Build Coastguard Worker ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
3057*9880d681SAndroid Build Coastguard Worker DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout()))));
3058*9880d681SAndroid Build Coastguard Worker else
3059*9880d681SAndroid Build Coastguard Worker Ops.push_back(DAG.getNode(
3060*9880d681SAndroid Build Coastguard Worker ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
3061*9880d681SAndroid Build Coastguard Worker DAG.getConstant(Idx - NumElems, dl,
3062*9880d681SAndroid Build Coastguard Worker TLI.getVectorIdxTy(DAG.getDataLayout()))));
3063*9880d681SAndroid Build Coastguard Worker }
3064*9880d681SAndroid Build Coastguard Worker
3065*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
3066*9880d681SAndroid Build Coastguard Worker // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
3067*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
3068*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3069*9880d681SAndroid Build Coastguard Worker break;
3070*9880d681SAndroid Build Coastguard Worker }
3071*9880d681SAndroid Build Coastguard Worker case ISD::EXTRACT_ELEMENT: {
3072*9880d681SAndroid Build Coastguard Worker EVT OpTy = Node->getOperand(0).getValueType();
3073*9880d681SAndroid Build Coastguard Worker if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3074*9880d681SAndroid Build Coastguard Worker // 1 -> Hi
3075*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3076*9880d681SAndroid Build Coastguard Worker DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
3077*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(
3078*9880d681SAndroid Build Coastguard Worker Node->getOperand(0).getValueType(),
3079*9880d681SAndroid Build Coastguard Worker DAG.getDataLayout())));
3080*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3081*9880d681SAndroid Build Coastguard Worker } else {
3082*9880d681SAndroid Build Coastguard Worker // 0 -> Lo
3083*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3084*9880d681SAndroid Build Coastguard Worker Node->getOperand(0));
3085*9880d681SAndroid Build Coastguard Worker }
3086*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3087*9880d681SAndroid Build Coastguard Worker break;
3088*9880d681SAndroid Build Coastguard Worker }
3089*9880d681SAndroid Build Coastguard Worker case ISD::STACKSAVE:
3090*9880d681SAndroid Build Coastguard Worker // Expand to CopyFromReg if the target set
3091*9880d681SAndroid Build Coastguard Worker // StackPointerRegisterToSaveRestore.
3092*9880d681SAndroid Build Coastguard Worker if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3093*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3094*9880d681SAndroid Build Coastguard Worker Node->getValueType(0)));
3095*9880d681SAndroid Build Coastguard Worker Results.push_back(Results[0].getValue(1));
3096*9880d681SAndroid Build Coastguard Worker } else {
3097*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3098*9880d681SAndroid Build Coastguard Worker Results.push_back(Node->getOperand(0));
3099*9880d681SAndroid Build Coastguard Worker }
3100*9880d681SAndroid Build Coastguard Worker break;
3101*9880d681SAndroid Build Coastguard Worker case ISD::STACKRESTORE:
3102*9880d681SAndroid Build Coastguard Worker // Expand to CopyToReg if the target set
3103*9880d681SAndroid Build Coastguard Worker // StackPointerRegisterToSaveRestore.
3104*9880d681SAndroid Build Coastguard Worker if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3105*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3106*9880d681SAndroid Build Coastguard Worker Node->getOperand(1)));
3107*9880d681SAndroid Build Coastguard Worker } else {
3108*9880d681SAndroid Build Coastguard Worker Results.push_back(Node->getOperand(0));
3109*9880d681SAndroid Build Coastguard Worker }
3110*9880d681SAndroid Build Coastguard Worker break;
3111*9880d681SAndroid Build Coastguard Worker case ISD::GET_DYNAMIC_AREA_OFFSET:
3112*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3113*9880d681SAndroid Build Coastguard Worker Results.push_back(Results[0].getValue(0));
3114*9880d681SAndroid Build Coastguard Worker break;
3115*9880d681SAndroid Build Coastguard Worker case ISD::FCOPYSIGN:
3116*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFCOPYSIGN(Node));
3117*9880d681SAndroid Build Coastguard Worker break;
3118*9880d681SAndroid Build Coastguard Worker case ISD::FNEG:
3119*9880d681SAndroid Build Coastguard Worker // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3120*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getConstantFP(-0.0, dl, Node->getValueType(0));
3121*9880d681SAndroid Build Coastguard Worker // TODO: If FNEG has fast-math-flags, propagate them to the FSUB.
3122*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3123*9880d681SAndroid Build Coastguard Worker Node->getOperand(0));
3124*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3125*9880d681SAndroid Build Coastguard Worker break;
3126*9880d681SAndroid Build Coastguard Worker case ISD::FABS:
3127*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFABS(Node));
3128*9880d681SAndroid Build Coastguard Worker break;
3129*9880d681SAndroid Build Coastguard Worker case ISD::SMIN:
3130*9880d681SAndroid Build Coastguard Worker case ISD::SMAX:
3131*9880d681SAndroid Build Coastguard Worker case ISD::UMIN:
3132*9880d681SAndroid Build Coastguard Worker case ISD::UMAX: {
3133*9880d681SAndroid Build Coastguard Worker // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
3134*9880d681SAndroid Build Coastguard Worker ISD::CondCode Pred;
3135*9880d681SAndroid Build Coastguard Worker switch (Node->getOpcode()) {
3136*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("How did we get here?");
3137*9880d681SAndroid Build Coastguard Worker case ISD::SMAX: Pred = ISD::SETGT; break;
3138*9880d681SAndroid Build Coastguard Worker case ISD::SMIN: Pred = ISD::SETLT; break;
3139*9880d681SAndroid Build Coastguard Worker case ISD::UMAX: Pred = ISD::SETUGT; break;
3140*9880d681SAndroid Build Coastguard Worker case ISD::UMIN: Pred = ISD::SETULT; break;
3141*9880d681SAndroid Build Coastguard Worker }
3142*9880d681SAndroid Build Coastguard Worker Tmp1 = Node->getOperand(0);
3143*9880d681SAndroid Build Coastguard Worker Tmp2 = Node->getOperand(1);
3144*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
3145*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3146*9880d681SAndroid Build Coastguard Worker break;
3147*9880d681SAndroid Build Coastguard Worker }
3148*9880d681SAndroid Build Coastguard Worker
3149*9880d681SAndroid Build Coastguard Worker case ISD::FSIN:
3150*9880d681SAndroid Build Coastguard Worker case ISD::FCOS: {
3151*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3152*9880d681SAndroid Build Coastguard Worker // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
3153*9880d681SAndroid Build Coastguard Worker // fcos which share the same operand and both are used.
3154*9880d681SAndroid Build Coastguard Worker if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
3155*9880d681SAndroid Build Coastguard Worker canCombineSinCosLibcall(Node, TLI, TM))
3156*9880d681SAndroid Build Coastguard Worker && useSinCos(Node)) {
3157*9880d681SAndroid Build Coastguard Worker SDVTList VTs = DAG.getVTList(VT, VT);
3158*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
3159*9880d681SAndroid Build Coastguard Worker if (Node->getOpcode() == ISD::FCOS)
3160*9880d681SAndroid Build Coastguard Worker Tmp1 = Tmp1.getValue(1);
3161*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3162*9880d681SAndroid Build Coastguard Worker }
3163*9880d681SAndroid Build Coastguard Worker break;
3164*9880d681SAndroid Build Coastguard Worker }
3165*9880d681SAndroid Build Coastguard Worker case ISD::FMAD:
3166*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Illegal fmad should never be formed");
3167*9880d681SAndroid Build Coastguard Worker
3168*9880d681SAndroid Build Coastguard Worker case ISD::FP16_TO_FP:
3169*9880d681SAndroid Build Coastguard Worker if (Node->getValueType(0) != MVT::f32) {
3170*9880d681SAndroid Build Coastguard Worker // We can extend to types bigger than f32 in two steps without changing
3171*9880d681SAndroid Build Coastguard Worker // the result. Since "f16 -> f32" is much more commonly available, give
3172*9880d681SAndroid Build Coastguard Worker // CodeGen the option of emitting that before resorting to a libcall.
3173*9880d681SAndroid Build Coastguard Worker SDValue Res =
3174*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
3175*9880d681SAndroid Build Coastguard Worker Results.push_back(
3176*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
3177*9880d681SAndroid Build Coastguard Worker }
3178*9880d681SAndroid Build Coastguard Worker break;
3179*9880d681SAndroid Build Coastguard Worker case ISD::FP_TO_FP16:
3180*9880d681SAndroid Build Coastguard Worker if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
3181*9880d681SAndroid Build Coastguard Worker SDValue Op = Node->getOperand(0);
3182*9880d681SAndroid Build Coastguard Worker MVT SVT = Op.getSimpleValueType();
3183*9880d681SAndroid Build Coastguard Worker if ((SVT == MVT::f64 || SVT == MVT::f80) &&
3184*9880d681SAndroid Build Coastguard Worker TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
3185*9880d681SAndroid Build Coastguard Worker // Under fastmath, we can expand this node into a fround followed by
3186*9880d681SAndroid Build Coastguard Worker // a float-half conversion.
3187*9880d681SAndroid Build Coastguard Worker SDValue FloatVal = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3188*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(0, dl));
3189*9880d681SAndroid Build Coastguard Worker Results.push_back(
3190*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
3191*9880d681SAndroid Build Coastguard Worker }
3192*9880d681SAndroid Build Coastguard Worker }
3193*9880d681SAndroid Build Coastguard Worker break;
3194*9880d681SAndroid Build Coastguard Worker case ISD::ConstantFP: {
3195*9880d681SAndroid Build Coastguard Worker ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
3196*9880d681SAndroid Build Coastguard Worker // Check to see if this FP immediate is already legal.
3197*9880d681SAndroid Build Coastguard Worker // If this is a legal constant, turn it into a TargetConstantFP node.
3198*9880d681SAndroid Build Coastguard Worker if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
3199*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandConstantFP(CFP, true));
3200*9880d681SAndroid Build Coastguard Worker break;
3201*9880d681SAndroid Build Coastguard Worker }
3202*9880d681SAndroid Build Coastguard Worker case ISD::Constant: {
3203*9880d681SAndroid Build Coastguard Worker ConstantSDNode *CP = cast<ConstantSDNode>(Node);
3204*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandConstant(CP));
3205*9880d681SAndroid Build Coastguard Worker break;
3206*9880d681SAndroid Build Coastguard Worker }
3207*9880d681SAndroid Build Coastguard Worker case ISD::FSUB: {
3208*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3209*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
3210*9880d681SAndroid Build Coastguard Worker TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
3211*9880d681SAndroid Build Coastguard Worker const SDNodeFlags *Flags = &cast<BinaryWithFlagsSDNode>(Node)->Flags;
3212*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
3213*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
3214*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3215*9880d681SAndroid Build Coastguard Worker }
3216*9880d681SAndroid Build Coastguard Worker break;
3217*9880d681SAndroid Build Coastguard Worker }
3218*9880d681SAndroid Build Coastguard Worker case ISD::SUB: {
3219*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3220*9880d681SAndroid Build Coastguard Worker assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3221*9880d681SAndroid Build Coastguard Worker TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3222*9880d681SAndroid Build Coastguard Worker "Don't know how to expand this subtraction!");
3223*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3224*9880d681SAndroid Build Coastguard Worker DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
3225*9880d681SAndroid Build Coastguard Worker VT));
3226*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
3227*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
3228*9880d681SAndroid Build Coastguard Worker break;
3229*9880d681SAndroid Build Coastguard Worker }
3230*9880d681SAndroid Build Coastguard Worker case ISD::UREM:
3231*9880d681SAndroid Build Coastguard Worker case ISD::SREM: {
3232*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3233*9880d681SAndroid Build Coastguard Worker bool isSigned = Node->getOpcode() == ISD::SREM;
3234*9880d681SAndroid Build Coastguard Worker unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3235*9880d681SAndroid Build Coastguard Worker unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3236*9880d681SAndroid Build Coastguard Worker Tmp2 = Node->getOperand(0);
3237*9880d681SAndroid Build Coastguard Worker Tmp3 = Node->getOperand(1);
3238*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3239*9880d681SAndroid Build Coastguard Worker SDVTList VTs = DAG.getVTList(VT, VT);
3240*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3241*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3242*9880d681SAndroid Build Coastguard Worker } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
3243*9880d681SAndroid Build Coastguard Worker // X % Y -> X-X/Y*Y
3244*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3245*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3246*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
3247*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3248*9880d681SAndroid Build Coastguard Worker }
3249*9880d681SAndroid Build Coastguard Worker break;
3250*9880d681SAndroid Build Coastguard Worker }
3251*9880d681SAndroid Build Coastguard Worker case ISD::UDIV:
3252*9880d681SAndroid Build Coastguard Worker case ISD::SDIV: {
3253*9880d681SAndroid Build Coastguard Worker bool isSigned = Node->getOpcode() == ISD::SDIV;
3254*9880d681SAndroid Build Coastguard Worker unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3255*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3256*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3257*9880d681SAndroid Build Coastguard Worker SDVTList VTs = DAG.getVTList(VT, VT);
3258*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3259*9880d681SAndroid Build Coastguard Worker Node->getOperand(1));
3260*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3261*9880d681SAndroid Build Coastguard Worker }
3262*9880d681SAndroid Build Coastguard Worker break;
3263*9880d681SAndroid Build Coastguard Worker }
3264*9880d681SAndroid Build Coastguard Worker case ISD::MULHU:
3265*9880d681SAndroid Build Coastguard Worker case ISD::MULHS: {
3266*9880d681SAndroid Build Coastguard Worker unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3267*9880d681SAndroid Build Coastguard Worker ISD::SMUL_LOHI;
3268*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3269*9880d681SAndroid Build Coastguard Worker SDVTList VTs = DAG.getVTList(VT, VT);
3270*9880d681SAndroid Build Coastguard Worker assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3271*9880d681SAndroid Build Coastguard Worker "If this wasn't legal, it shouldn't have been created!");
3272*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3273*9880d681SAndroid Build Coastguard Worker Node->getOperand(1));
3274*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1.getValue(1));
3275*9880d681SAndroid Build Coastguard Worker break;
3276*9880d681SAndroid Build Coastguard Worker }
3277*9880d681SAndroid Build Coastguard Worker case ISD::MUL: {
3278*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3279*9880d681SAndroid Build Coastguard Worker SDVTList VTs = DAG.getVTList(VT, VT);
3280*9880d681SAndroid Build Coastguard Worker // See if multiply or divide can be lowered using two-result operations.
3281*9880d681SAndroid Build Coastguard Worker // We just need the low half of the multiply; try both the signed
3282*9880d681SAndroid Build Coastguard Worker // and unsigned forms. If the target supports both SMUL_LOHI and
3283*9880d681SAndroid Build Coastguard Worker // UMUL_LOHI, form a preference by checking which forms of plain
3284*9880d681SAndroid Build Coastguard Worker // MULH it supports.
3285*9880d681SAndroid Build Coastguard Worker bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3286*9880d681SAndroid Build Coastguard Worker bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3287*9880d681SAndroid Build Coastguard Worker bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3288*9880d681SAndroid Build Coastguard Worker bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3289*9880d681SAndroid Build Coastguard Worker unsigned OpToUse = 0;
3290*9880d681SAndroid Build Coastguard Worker if (HasSMUL_LOHI && !HasMULHS) {
3291*9880d681SAndroid Build Coastguard Worker OpToUse = ISD::SMUL_LOHI;
3292*9880d681SAndroid Build Coastguard Worker } else if (HasUMUL_LOHI && !HasMULHU) {
3293*9880d681SAndroid Build Coastguard Worker OpToUse = ISD::UMUL_LOHI;
3294*9880d681SAndroid Build Coastguard Worker } else if (HasSMUL_LOHI) {
3295*9880d681SAndroid Build Coastguard Worker OpToUse = ISD::SMUL_LOHI;
3296*9880d681SAndroid Build Coastguard Worker } else if (HasUMUL_LOHI) {
3297*9880d681SAndroid Build Coastguard Worker OpToUse = ISD::UMUL_LOHI;
3298*9880d681SAndroid Build Coastguard Worker }
3299*9880d681SAndroid Build Coastguard Worker if (OpToUse) {
3300*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3301*9880d681SAndroid Build Coastguard Worker Node->getOperand(1)));
3302*9880d681SAndroid Build Coastguard Worker break;
3303*9880d681SAndroid Build Coastguard Worker }
3304*9880d681SAndroid Build Coastguard Worker
3305*9880d681SAndroid Build Coastguard Worker SDValue Lo, Hi;
3306*9880d681SAndroid Build Coastguard Worker EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
3307*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
3308*9880d681SAndroid Build Coastguard Worker TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
3309*9880d681SAndroid Build Coastguard Worker TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
3310*9880d681SAndroid Build Coastguard Worker TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
3311*9880d681SAndroid Build Coastguard Worker TLI.expandMUL(Node, Lo, Hi, HalfType, DAG)) {
3312*9880d681SAndroid Build Coastguard Worker Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
3313*9880d681SAndroid Build Coastguard Worker Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
3314*9880d681SAndroid Build Coastguard Worker SDValue Shift =
3315*9880d681SAndroid Build Coastguard Worker DAG.getConstant(HalfType.getSizeInBits(), dl,
3316*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
3317*9880d681SAndroid Build Coastguard Worker Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3318*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
3319*9880d681SAndroid Build Coastguard Worker }
3320*9880d681SAndroid Build Coastguard Worker break;
3321*9880d681SAndroid Build Coastguard Worker }
3322*9880d681SAndroid Build Coastguard Worker case ISD::SADDO:
3323*9880d681SAndroid Build Coastguard Worker case ISD::SSUBO: {
3324*9880d681SAndroid Build Coastguard Worker SDValue LHS = Node->getOperand(0);
3325*9880d681SAndroid Build Coastguard Worker SDValue RHS = Node->getOperand(1);
3326*9880d681SAndroid Build Coastguard Worker SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3327*9880d681SAndroid Build Coastguard Worker ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3328*9880d681SAndroid Build Coastguard Worker LHS, RHS);
3329*9880d681SAndroid Build Coastguard Worker Results.push_back(Sum);
3330*9880d681SAndroid Build Coastguard Worker EVT ResultType = Node->getValueType(1);
3331*9880d681SAndroid Build Coastguard Worker EVT OType = getSetCCResultType(Node->getValueType(0));
3332*9880d681SAndroid Build Coastguard Worker
3333*9880d681SAndroid Build Coastguard Worker SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
3334*9880d681SAndroid Build Coastguard Worker
3335*9880d681SAndroid Build Coastguard Worker // LHSSign -> LHS >= 0
3336*9880d681SAndroid Build Coastguard Worker // RHSSign -> RHS >= 0
3337*9880d681SAndroid Build Coastguard Worker // SumSign -> Sum >= 0
3338*9880d681SAndroid Build Coastguard Worker //
3339*9880d681SAndroid Build Coastguard Worker // Add:
3340*9880d681SAndroid Build Coastguard Worker // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3341*9880d681SAndroid Build Coastguard Worker // Sub:
3342*9880d681SAndroid Build Coastguard Worker // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3343*9880d681SAndroid Build Coastguard Worker //
3344*9880d681SAndroid Build Coastguard Worker SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3345*9880d681SAndroid Build Coastguard Worker SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3346*9880d681SAndroid Build Coastguard Worker SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3347*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::SADDO ?
3348*9880d681SAndroid Build Coastguard Worker ISD::SETEQ : ISD::SETNE);
3349*9880d681SAndroid Build Coastguard Worker
3350*9880d681SAndroid Build Coastguard Worker SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3351*9880d681SAndroid Build Coastguard Worker SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3352*9880d681SAndroid Build Coastguard Worker
3353*9880d681SAndroid Build Coastguard Worker SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3354*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getBoolExtOrTrunc(Cmp, dl, ResultType, ResultType));
3355*9880d681SAndroid Build Coastguard Worker break;
3356*9880d681SAndroid Build Coastguard Worker }
3357*9880d681SAndroid Build Coastguard Worker case ISD::UADDO:
3358*9880d681SAndroid Build Coastguard Worker case ISD::USUBO: {
3359*9880d681SAndroid Build Coastguard Worker SDValue LHS = Node->getOperand(0);
3360*9880d681SAndroid Build Coastguard Worker SDValue RHS = Node->getOperand(1);
3361*9880d681SAndroid Build Coastguard Worker SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3362*9880d681SAndroid Build Coastguard Worker ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3363*9880d681SAndroid Build Coastguard Worker LHS, RHS);
3364*9880d681SAndroid Build Coastguard Worker Results.push_back(Sum);
3365*9880d681SAndroid Build Coastguard Worker
3366*9880d681SAndroid Build Coastguard Worker EVT ResultType = Node->getValueType(1);
3367*9880d681SAndroid Build Coastguard Worker EVT SetCCType = getSetCCResultType(Node->getValueType(0));
3368*9880d681SAndroid Build Coastguard Worker ISD::CondCode CC
3369*9880d681SAndroid Build Coastguard Worker = Node->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT;
3370*9880d681SAndroid Build Coastguard Worker SDValue SetCC = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
3371*9880d681SAndroid Build Coastguard Worker
3372*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType));
3373*9880d681SAndroid Build Coastguard Worker break;
3374*9880d681SAndroid Build Coastguard Worker }
3375*9880d681SAndroid Build Coastguard Worker case ISD::UMULO:
3376*9880d681SAndroid Build Coastguard Worker case ISD::SMULO: {
3377*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3378*9880d681SAndroid Build Coastguard Worker EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
3379*9880d681SAndroid Build Coastguard Worker SDValue LHS = Node->getOperand(0);
3380*9880d681SAndroid Build Coastguard Worker SDValue RHS = Node->getOperand(1);
3381*9880d681SAndroid Build Coastguard Worker SDValue BottomHalf;
3382*9880d681SAndroid Build Coastguard Worker SDValue TopHalf;
3383*9880d681SAndroid Build Coastguard Worker static const unsigned Ops[2][3] =
3384*9880d681SAndroid Build Coastguard Worker { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3385*9880d681SAndroid Build Coastguard Worker { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3386*9880d681SAndroid Build Coastguard Worker bool isSigned = Node->getOpcode() == ISD::SMULO;
3387*9880d681SAndroid Build Coastguard Worker if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3388*9880d681SAndroid Build Coastguard Worker BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3389*9880d681SAndroid Build Coastguard Worker TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3390*9880d681SAndroid Build Coastguard Worker } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3391*9880d681SAndroid Build Coastguard Worker BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3392*9880d681SAndroid Build Coastguard Worker RHS);
3393*9880d681SAndroid Build Coastguard Worker TopHalf = BottomHalf.getValue(1);
3394*9880d681SAndroid Build Coastguard Worker } else if (TLI.isTypeLegal(WideVT)) {
3395*9880d681SAndroid Build Coastguard Worker LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3396*9880d681SAndroid Build Coastguard Worker RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3397*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3398*9880d681SAndroid Build Coastguard Worker BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3399*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(0, dl));
3400*9880d681SAndroid Build Coastguard Worker TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3401*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(1, dl));
3402*9880d681SAndroid Build Coastguard Worker } else {
3403*9880d681SAndroid Build Coastguard Worker // We can fall back to a libcall with an illegal type for the MUL if we
3404*9880d681SAndroid Build Coastguard Worker // have a libcall big enough.
3405*9880d681SAndroid Build Coastguard Worker // Also, we can fall back to a division in some cases, but that's a big
3406*9880d681SAndroid Build Coastguard Worker // performance hit in the general case.
3407*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3408*9880d681SAndroid Build Coastguard Worker if (WideVT == MVT::i16)
3409*9880d681SAndroid Build Coastguard Worker LC = RTLIB::MUL_I16;
3410*9880d681SAndroid Build Coastguard Worker else if (WideVT == MVT::i32)
3411*9880d681SAndroid Build Coastguard Worker LC = RTLIB::MUL_I32;
3412*9880d681SAndroid Build Coastguard Worker else if (WideVT == MVT::i64)
3413*9880d681SAndroid Build Coastguard Worker LC = RTLIB::MUL_I64;
3414*9880d681SAndroid Build Coastguard Worker else if (WideVT == MVT::i128)
3415*9880d681SAndroid Build Coastguard Worker LC = RTLIB::MUL_I128;
3416*9880d681SAndroid Build Coastguard Worker assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
3417*9880d681SAndroid Build Coastguard Worker
3418*9880d681SAndroid Build Coastguard Worker // The high part is obtained by SRA'ing all but one of the bits of low
3419*9880d681SAndroid Build Coastguard Worker // part.
3420*9880d681SAndroid Build Coastguard Worker unsigned LoSize = VT.getSizeInBits();
3421*9880d681SAndroid Build Coastguard Worker SDValue HiLHS =
3422*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SRA, dl, VT, RHS,
3423*9880d681SAndroid Build Coastguard Worker DAG.getConstant(LoSize - 1, dl,
3424*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout())));
3425*9880d681SAndroid Build Coastguard Worker SDValue HiRHS =
3426*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::SRA, dl, VT, LHS,
3427*9880d681SAndroid Build Coastguard Worker DAG.getConstant(LoSize - 1, dl,
3428*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout())));
3429*9880d681SAndroid Build Coastguard Worker
3430*9880d681SAndroid Build Coastguard Worker // Here we're passing the 2 arguments explicitly as 4 arguments that are
3431*9880d681SAndroid Build Coastguard Worker // pre-lowered to the correct types. This all depends upon WideVT not
3432*9880d681SAndroid Build Coastguard Worker // being a legal type for the architecture and thus has to be split to
3433*9880d681SAndroid Build Coastguard Worker // two arguments.
3434*9880d681SAndroid Build Coastguard Worker SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3435*9880d681SAndroid Build Coastguard Worker SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3436*9880d681SAndroid Build Coastguard Worker BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3437*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(0, dl));
3438*9880d681SAndroid Build Coastguard Worker TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3439*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(1, dl));
3440*9880d681SAndroid Build Coastguard Worker // Ret is a node with an illegal type. Because such things are not
3441*9880d681SAndroid Build Coastguard Worker // generally permitted during this phase of legalization, make sure the
3442*9880d681SAndroid Build Coastguard Worker // node has no more uses. The above EXTRACT_ELEMENT nodes should have been
3443*9880d681SAndroid Build Coastguard Worker // folded.
3444*9880d681SAndroid Build Coastguard Worker assert(Ret->use_empty() &&
3445*9880d681SAndroid Build Coastguard Worker "Unexpected uses of illegally type from expanded lib call.");
3446*9880d681SAndroid Build Coastguard Worker }
3447*9880d681SAndroid Build Coastguard Worker
3448*9880d681SAndroid Build Coastguard Worker if (isSigned) {
3449*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getConstant(
3450*9880d681SAndroid Build Coastguard Worker VT.getSizeInBits() - 1, dl,
3451*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(BottomHalf.getValueType(), DAG.getDataLayout()));
3452*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3453*9880d681SAndroid Build Coastguard Worker TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf, Tmp1,
3454*9880d681SAndroid Build Coastguard Worker ISD::SETNE);
3455*9880d681SAndroid Build Coastguard Worker } else {
3456*9880d681SAndroid Build Coastguard Worker TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf,
3457*9880d681SAndroid Build Coastguard Worker DAG.getConstant(0, dl, VT), ISD::SETNE);
3458*9880d681SAndroid Build Coastguard Worker }
3459*9880d681SAndroid Build Coastguard Worker Results.push_back(BottomHalf);
3460*9880d681SAndroid Build Coastguard Worker Results.push_back(TopHalf);
3461*9880d681SAndroid Build Coastguard Worker break;
3462*9880d681SAndroid Build Coastguard Worker }
3463*9880d681SAndroid Build Coastguard Worker case ISD::BUILD_PAIR: {
3464*9880d681SAndroid Build Coastguard Worker EVT PairTy = Node->getValueType(0);
3465*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3466*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
3467*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(
3468*9880d681SAndroid Build Coastguard Worker ISD::SHL, dl, PairTy, Tmp2,
3469*9880d681SAndroid Build Coastguard Worker DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
3470*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
3471*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
3472*9880d681SAndroid Build Coastguard Worker break;
3473*9880d681SAndroid Build Coastguard Worker }
3474*9880d681SAndroid Build Coastguard Worker case ISD::SELECT:
3475*9880d681SAndroid Build Coastguard Worker Tmp1 = Node->getOperand(0);
3476*9880d681SAndroid Build Coastguard Worker Tmp2 = Node->getOperand(1);
3477*9880d681SAndroid Build Coastguard Worker Tmp3 = Node->getOperand(2);
3478*9880d681SAndroid Build Coastguard Worker if (Tmp1.getOpcode() == ISD::SETCC) {
3479*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3480*9880d681SAndroid Build Coastguard Worker Tmp2, Tmp3,
3481*9880d681SAndroid Build Coastguard Worker cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
3482*9880d681SAndroid Build Coastguard Worker } else {
3483*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getSelectCC(dl, Tmp1,
3484*9880d681SAndroid Build Coastguard Worker DAG.getConstant(0, dl, Tmp1.getValueType()),
3485*9880d681SAndroid Build Coastguard Worker Tmp2, Tmp3, ISD::SETNE);
3486*9880d681SAndroid Build Coastguard Worker }
3487*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3488*9880d681SAndroid Build Coastguard Worker break;
3489*9880d681SAndroid Build Coastguard Worker case ISD::BR_JT: {
3490*9880d681SAndroid Build Coastguard Worker SDValue Chain = Node->getOperand(0);
3491*9880d681SAndroid Build Coastguard Worker SDValue Table = Node->getOperand(1);
3492*9880d681SAndroid Build Coastguard Worker SDValue Index = Node->getOperand(2);
3493*9880d681SAndroid Build Coastguard Worker
3494*9880d681SAndroid Build Coastguard Worker EVT PTy = TLI.getPointerTy(DAG.getDataLayout());
3495*9880d681SAndroid Build Coastguard Worker
3496*9880d681SAndroid Build Coastguard Worker const DataLayout &TD = DAG.getDataLayout();
3497*9880d681SAndroid Build Coastguard Worker unsigned EntrySize =
3498*9880d681SAndroid Build Coastguard Worker DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
3499*9880d681SAndroid Build Coastguard Worker
3500*9880d681SAndroid Build Coastguard Worker Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
3501*9880d681SAndroid Build Coastguard Worker DAG.getConstant(EntrySize, dl, Index.getValueType()));
3502*9880d681SAndroid Build Coastguard Worker SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
3503*9880d681SAndroid Build Coastguard Worker Index, Table);
3504*9880d681SAndroid Build Coastguard Worker
3505*9880d681SAndroid Build Coastguard Worker EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
3506*9880d681SAndroid Build Coastguard Worker SDValue LD = DAG.getExtLoad(
3507*9880d681SAndroid Build Coastguard Worker ISD::SEXTLOAD, dl, PTy, Chain, Addr,
3508*9880d681SAndroid Build Coastguard Worker MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT,
3509*9880d681SAndroid Build Coastguard Worker false, false, false, 0);
3510*9880d681SAndroid Build Coastguard Worker Addr = LD;
3511*9880d681SAndroid Build Coastguard Worker if (TM.isPositionIndependent()) {
3512*9880d681SAndroid Build Coastguard Worker // For PIC, the sequence is:
3513*9880d681SAndroid Build Coastguard Worker // BRIND(load(Jumptable + index) + RelocBase)
3514*9880d681SAndroid Build Coastguard Worker // RelocBase can be JumpTable, GOT or some sort of global base.
3515*9880d681SAndroid Build Coastguard Worker Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3516*9880d681SAndroid Build Coastguard Worker TLI.getPICJumpTableRelocBase(Table, DAG));
3517*9880d681SAndroid Build Coastguard Worker }
3518*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
3519*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3520*9880d681SAndroid Build Coastguard Worker break;
3521*9880d681SAndroid Build Coastguard Worker }
3522*9880d681SAndroid Build Coastguard Worker case ISD::BRCOND:
3523*9880d681SAndroid Build Coastguard Worker // Expand brcond's setcc into its constituent parts and create a BR_CC
3524*9880d681SAndroid Build Coastguard Worker // Node.
3525*9880d681SAndroid Build Coastguard Worker Tmp1 = Node->getOperand(0);
3526*9880d681SAndroid Build Coastguard Worker Tmp2 = Node->getOperand(1);
3527*9880d681SAndroid Build Coastguard Worker if (Tmp2.getOpcode() == ISD::SETCC) {
3528*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
3529*9880d681SAndroid Build Coastguard Worker Tmp1, Tmp2.getOperand(2),
3530*9880d681SAndroid Build Coastguard Worker Tmp2.getOperand(0), Tmp2.getOperand(1),
3531*9880d681SAndroid Build Coastguard Worker Node->getOperand(2));
3532*9880d681SAndroid Build Coastguard Worker } else {
3533*9880d681SAndroid Build Coastguard Worker // We test only the i1 bit. Skip the AND if UNDEF.
3534*9880d681SAndroid Build Coastguard Worker Tmp3 = (Tmp2.isUndef()) ? Tmp2 :
3535*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3536*9880d681SAndroid Build Coastguard Worker DAG.getConstant(1, dl, Tmp2.getValueType()));
3537*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
3538*9880d681SAndroid Build Coastguard Worker DAG.getCondCode(ISD::SETNE), Tmp3,
3539*9880d681SAndroid Build Coastguard Worker DAG.getConstant(0, dl, Tmp3.getValueType()),
3540*9880d681SAndroid Build Coastguard Worker Node->getOperand(2));
3541*9880d681SAndroid Build Coastguard Worker }
3542*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3543*9880d681SAndroid Build Coastguard Worker break;
3544*9880d681SAndroid Build Coastguard Worker case ISD::SETCC: {
3545*9880d681SAndroid Build Coastguard Worker Tmp1 = Node->getOperand(0);
3546*9880d681SAndroid Build Coastguard Worker Tmp2 = Node->getOperand(1);
3547*9880d681SAndroid Build Coastguard Worker Tmp3 = Node->getOperand(2);
3548*9880d681SAndroid Build Coastguard Worker bool Legalized = LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2,
3549*9880d681SAndroid Build Coastguard Worker Tmp3, NeedInvert, dl);
3550*9880d681SAndroid Build Coastguard Worker
3551*9880d681SAndroid Build Coastguard Worker if (Legalized) {
3552*9880d681SAndroid Build Coastguard Worker // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
3553*9880d681SAndroid Build Coastguard Worker // condition code, create a new SETCC node.
3554*9880d681SAndroid Build Coastguard Worker if (Tmp3.getNode())
3555*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3556*9880d681SAndroid Build Coastguard Worker Tmp1, Tmp2, Tmp3);
3557*9880d681SAndroid Build Coastguard Worker
3558*9880d681SAndroid Build Coastguard Worker // If we expanded the SETCC by inverting the condition code, then wrap
3559*9880d681SAndroid Build Coastguard Worker // the existing SETCC in a NOT to restore the intended condition.
3560*9880d681SAndroid Build Coastguard Worker if (NeedInvert)
3561*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
3562*9880d681SAndroid Build Coastguard Worker
3563*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3564*9880d681SAndroid Build Coastguard Worker break;
3565*9880d681SAndroid Build Coastguard Worker }
3566*9880d681SAndroid Build Coastguard Worker
3567*9880d681SAndroid Build Coastguard Worker // Otherwise, SETCC for the given comparison type must be completely
3568*9880d681SAndroid Build Coastguard Worker // illegal; expand it into a SELECT_CC.
3569*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3570*9880d681SAndroid Build Coastguard Worker int TrueValue;
3571*9880d681SAndroid Build Coastguard Worker switch (TLI.getBooleanContents(Tmp1->getValueType(0))) {
3572*9880d681SAndroid Build Coastguard Worker case TargetLowering::ZeroOrOneBooleanContent:
3573*9880d681SAndroid Build Coastguard Worker case TargetLowering::UndefinedBooleanContent:
3574*9880d681SAndroid Build Coastguard Worker TrueValue = 1;
3575*9880d681SAndroid Build Coastguard Worker break;
3576*9880d681SAndroid Build Coastguard Worker case TargetLowering::ZeroOrNegativeOneBooleanContent:
3577*9880d681SAndroid Build Coastguard Worker TrueValue = -1;
3578*9880d681SAndroid Build Coastguard Worker break;
3579*9880d681SAndroid Build Coastguard Worker }
3580*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3581*9880d681SAndroid Build Coastguard Worker DAG.getConstant(TrueValue, dl, VT),
3582*9880d681SAndroid Build Coastguard Worker DAG.getConstant(0, dl, VT),
3583*9880d681SAndroid Build Coastguard Worker Tmp3);
3584*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3585*9880d681SAndroid Build Coastguard Worker break;
3586*9880d681SAndroid Build Coastguard Worker }
3587*9880d681SAndroid Build Coastguard Worker case ISD::SELECT_CC: {
3588*9880d681SAndroid Build Coastguard Worker Tmp1 = Node->getOperand(0); // LHS
3589*9880d681SAndroid Build Coastguard Worker Tmp2 = Node->getOperand(1); // RHS
3590*9880d681SAndroid Build Coastguard Worker Tmp3 = Node->getOperand(2); // True
3591*9880d681SAndroid Build Coastguard Worker Tmp4 = Node->getOperand(3); // False
3592*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3593*9880d681SAndroid Build Coastguard Worker SDValue CC = Node->getOperand(4);
3594*9880d681SAndroid Build Coastguard Worker ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
3595*9880d681SAndroid Build Coastguard Worker
3596*9880d681SAndroid Build Coastguard Worker if (TLI.isCondCodeLegal(CCOp, Tmp1.getSimpleValueType())) {
3597*9880d681SAndroid Build Coastguard Worker // If the condition code is legal, then we need to expand this
3598*9880d681SAndroid Build Coastguard Worker // node using SETCC and SELECT.
3599*9880d681SAndroid Build Coastguard Worker EVT CmpVT = Tmp1.getValueType();
3600*9880d681SAndroid Build Coastguard Worker assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
3601*9880d681SAndroid Build Coastguard Worker "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
3602*9880d681SAndroid Build Coastguard Worker "expanded.");
3603*9880d681SAndroid Build Coastguard Worker EVT CCVT =
3604*9880d681SAndroid Build Coastguard Worker TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), CmpVT);
3605*9880d681SAndroid Build Coastguard Worker SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC);
3606*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4));
3607*9880d681SAndroid Build Coastguard Worker break;
3608*9880d681SAndroid Build Coastguard Worker }
3609*9880d681SAndroid Build Coastguard Worker
3610*9880d681SAndroid Build Coastguard Worker // SELECT_CC is legal, so the condition code must not be.
3611*9880d681SAndroid Build Coastguard Worker bool Legalized = false;
3612*9880d681SAndroid Build Coastguard Worker // Try to legalize by inverting the condition. This is for targets that
3613*9880d681SAndroid Build Coastguard Worker // might support an ordered version of a condition, but not the unordered
3614*9880d681SAndroid Build Coastguard Worker // version (or vice versa).
3615*9880d681SAndroid Build Coastguard Worker ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp,
3616*9880d681SAndroid Build Coastguard Worker Tmp1.getValueType().isInteger());
3617*9880d681SAndroid Build Coastguard Worker if (TLI.isCondCodeLegal(InvCC, Tmp1.getSimpleValueType())) {
3618*9880d681SAndroid Build Coastguard Worker // Use the new condition code and swap true and false
3619*9880d681SAndroid Build Coastguard Worker Legalized = true;
3620*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
3621*9880d681SAndroid Build Coastguard Worker } else {
3622*9880d681SAndroid Build Coastguard Worker // If The inverse is not legal, then try to swap the arguments using
3623*9880d681SAndroid Build Coastguard Worker // the inverse condition code.
3624*9880d681SAndroid Build Coastguard Worker ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC);
3625*9880d681SAndroid Build Coastguard Worker if (TLI.isCondCodeLegal(SwapInvCC, Tmp1.getSimpleValueType())) {
3626*9880d681SAndroid Build Coastguard Worker // The swapped inverse condition is legal, so swap true and false,
3627*9880d681SAndroid Build Coastguard Worker // lhs and rhs.
3628*9880d681SAndroid Build Coastguard Worker Legalized = true;
3629*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
3630*9880d681SAndroid Build Coastguard Worker }
3631*9880d681SAndroid Build Coastguard Worker }
3632*9880d681SAndroid Build Coastguard Worker
3633*9880d681SAndroid Build Coastguard Worker if (!Legalized) {
3634*9880d681SAndroid Build Coastguard Worker Legalized = LegalizeSetCCCondCode(
3635*9880d681SAndroid Build Coastguard Worker getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, NeedInvert,
3636*9880d681SAndroid Build Coastguard Worker dl);
3637*9880d681SAndroid Build Coastguard Worker
3638*9880d681SAndroid Build Coastguard Worker assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
3639*9880d681SAndroid Build Coastguard Worker
3640*9880d681SAndroid Build Coastguard Worker // If we expanded the SETCC by inverting the condition code, then swap
3641*9880d681SAndroid Build Coastguard Worker // the True/False operands to match.
3642*9880d681SAndroid Build Coastguard Worker if (NeedInvert)
3643*9880d681SAndroid Build Coastguard Worker std::swap(Tmp3, Tmp4);
3644*9880d681SAndroid Build Coastguard Worker
3645*9880d681SAndroid Build Coastguard Worker // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
3646*9880d681SAndroid Build Coastguard Worker // condition code, create a new SELECT_CC node.
3647*9880d681SAndroid Build Coastguard Worker if (CC.getNode()) {
3648*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
3649*9880d681SAndroid Build Coastguard Worker Tmp1, Tmp2, Tmp3, Tmp4, CC);
3650*9880d681SAndroid Build Coastguard Worker } else {
3651*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
3652*9880d681SAndroid Build Coastguard Worker CC = DAG.getCondCode(ISD::SETNE);
3653*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
3654*9880d681SAndroid Build Coastguard Worker Tmp2, Tmp3, Tmp4, CC);
3655*9880d681SAndroid Build Coastguard Worker }
3656*9880d681SAndroid Build Coastguard Worker }
3657*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3658*9880d681SAndroid Build Coastguard Worker break;
3659*9880d681SAndroid Build Coastguard Worker }
3660*9880d681SAndroid Build Coastguard Worker case ISD::BR_CC: {
3661*9880d681SAndroid Build Coastguard Worker Tmp1 = Node->getOperand(0); // Chain
3662*9880d681SAndroid Build Coastguard Worker Tmp2 = Node->getOperand(2); // LHS
3663*9880d681SAndroid Build Coastguard Worker Tmp3 = Node->getOperand(3); // RHS
3664*9880d681SAndroid Build Coastguard Worker Tmp4 = Node->getOperand(1); // CC
3665*9880d681SAndroid Build Coastguard Worker
3666*9880d681SAndroid Build Coastguard Worker bool Legalized = LegalizeSetCCCondCode(getSetCCResultType(
3667*9880d681SAndroid Build Coastguard Worker Tmp2.getValueType()), Tmp2, Tmp3, Tmp4, NeedInvert, dl);
3668*9880d681SAndroid Build Coastguard Worker (void)Legalized;
3669*9880d681SAndroid Build Coastguard Worker assert(Legalized && "Can't legalize BR_CC with legal condition!");
3670*9880d681SAndroid Build Coastguard Worker
3671*9880d681SAndroid Build Coastguard Worker // If we expanded the SETCC by inverting the condition code, then wrap
3672*9880d681SAndroid Build Coastguard Worker // the existing SETCC in a NOT to restore the intended condition.
3673*9880d681SAndroid Build Coastguard Worker if (NeedInvert)
3674*9880d681SAndroid Build Coastguard Worker Tmp4 = DAG.getNOT(dl, Tmp4, Tmp4->getValueType(0));
3675*9880d681SAndroid Build Coastguard Worker
3676*9880d681SAndroid Build Coastguard Worker // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
3677*9880d681SAndroid Build Coastguard Worker // node.
3678*9880d681SAndroid Build Coastguard Worker if (Tmp4.getNode()) {
3679*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
3680*9880d681SAndroid Build Coastguard Worker Tmp4, Tmp2, Tmp3, Node->getOperand(4));
3681*9880d681SAndroid Build Coastguard Worker } else {
3682*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
3683*9880d681SAndroid Build Coastguard Worker Tmp4 = DAG.getCondCode(ISD::SETNE);
3684*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
3685*9880d681SAndroid Build Coastguard Worker Tmp2, Tmp3, Node->getOperand(4));
3686*9880d681SAndroid Build Coastguard Worker }
3687*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
3688*9880d681SAndroid Build Coastguard Worker break;
3689*9880d681SAndroid Build Coastguard Worker }
3690*9880d681SAndroid Build Coastguard Worker case ISD::BUILD_VECTOR:
3691*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandBUILD_VECTOR(Node));
3692*9880d681SAndroid Build Coastguard Worker break;
3693*9880d681SAndroid Build Coastguard Worker case ISD::SRA:
3694*9880d681SAndroid Build Coastguard Worker case ISD::SRL:
3695*9880d681SAndroid Build Coastguard Worker case ISD::SHL: {
3696*9880d681SAndroid Build Coastguard Worker // Scalarize vector SRA/SRL/SHL.
3697*9880d681SAndroid Build Coastguard Worker EVT VT = Node->getValueType(0);
3698*9880d681SAndroid Build Coastguard Worker assert(VT.isVector() && "Unable to legalize non-vector shift");
3699*9880d681SAndroid Build Coastguard Worker assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3700*9880d681SAndroid Build Coastguard Worker unsigned NumElem = VT.getVectorNumElements();
3701*9880d681SAndroid Build Coastguard Worker
3702*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> Scalars;
3703*9880d681SAndroid Build Coastguard Worker for (unsigned Idx = 0; Idx < NumElem; Idx++) {
3704*9880d681SAndroid Build Coastguard Worker SDValue Ex = DAG.getNode(
3705*9880d681SAndroid Build Coastguard Worker ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(0),
3706*9880d681SAndroid Build Coastguard Worker DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3707*9880d681SAndroid Build Coastguard Worker SDValue Sh = DAG.getNode(
3708*9880d681SAndroid Build Coastguard Worker ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), Node->getOperand(1),
3709*9880d681SAndroid Build Coastguard Worker DAG.getConstant(Idx, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
3710*9880d681SAndroid Build Coastguard Worker Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3711*9880d681SAndroid Build Coastguard Worker VT.getScalarType(), Ex, Sh));
3712*9880d681SAndroid Build Coastguard Worker }
3713*9880d681SAndroid Build Coastguard Worker SDValue Result =
3714*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0), Scalars);
3715*9880d681SAndroid Build Coastguard Worker ReplaceNode(SDValue(Node, 0), Result);
3716*9880d681SAndroid Build Coastguard Worker break;
3717*9880d681SAndroid Build Coastguard Worker }
3718*9880d681SAndroid Build Coastguard Worker case ISD::GLOBAL_OFFSET_TABLE:
3719*9880d681SAndroid Build Coastguard Worker case ISD::GlobalAddress:
3720*9880d681SAndroid Build Coastguard Worker case ISD::GlobalTLSAddress:
3721*9880d681SAndroid Build Coastguard Worker case ISD::ExternalSymbol:
3722*9880d681SAndroid Build Coastguard Worker case ISD::ConstantPool:
3723*9880d681SAndroid Build Coastguard Worker case ISD::JumpTable:
3724*9880d681SAndroid Build Coastguard Worker case ISD::INTRINSIC_W_CHAIN:
3725*9880d681SAndroid Build Coastguard Worker case ISD::INTRINSIC_WO_CHAIN:
3726*9880d681SAndroid Build Coastguard Worker case ISD::INTRINSIC_VOID:
3727*9880d681SAndroid Build Coastguard Worker // FIXME: Custom lowering for these operations shouldn't return null!
3728*9880d681SAndroid Build Coastguard Worker break;
3729*9880d681SAndroid Build Coastguard Worker }
3730*9880d681SAndroid Build Coastguard Worker
3731*9880d681SAndroid Build Coastguard Worker // Replace the original node with the legalized result.
3732*9880d681SAndroid Build Coastguard Worker if (Results.empty())
3733*9880d681SAndroid Build Coastguard Worker return false;
3734*9880d681SAndroid Build Coastguard Worker
3735*9880d681SAndroid Build Coastguard Worker ReplaceNode(Node, Results.data());
3736*9880d681SAndroid Build Coastguard Worker return true;
3737*9880d681SAndroid Build Coastguard Worker }
3738*9880d681SAndroid Build Coastguard Worker
ConvertNodeToLibcall(SDNode * Node)3739*9880d681SAndroid Build Coastguard Worker void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
3740*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> Results;
3741*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
3742*9880d681SAndroid Build Coastguard Worker SDValue Tmp1, Tmp2, Tmp3, Tmp4;
3743*9880d681SAndroid Build Coastguard Worker unsigned Opc = Node->getOpcode();
3744*9880d681SAndroid Build Coastguard Worker switch (Opc) {
3745*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_FENCE: {
3746*9880d681SAndroid Build Coastguard Worker // If the target didn't lower this, lower it to '__sync_synchronize()' call
3747*9880d681SAndroid Build Coastguard Worker // FIXME: handle "fence singlethread" more efficiently.
3748*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListTy Args;
3749*9880d681SAndroid Build Coastguard Worker
3750*9880d681SAndroid Build Coastguard Worker TargetLowering::CallLoweringInfo CLI(DAG);
3751*9880d681SAndroid Build Coastguard Worker CLI.setDebugLoc(dl)
3752*9880d681SAndroid Build Coastguard Worker .setChain(Node->getOperand(0))
3753*9880d681SAndroid Build Coastguard Worker .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
3754*9880d681SAndroid Build Coastguard Worker DAG.getExternalSymbol("__sync_synchronize",
3755*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout())),
3756*9880d681SAndroid Build Coastguard Worker std::move(Args));
3757*9880d681SAndroid Build Coastguard Worker
3758*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
3759*9880d681SAndroid Build Coastguard Worker
3760*9880d681SAndroid Build Coastguard Worker Results.push_back(CallResult.second);
3761*9880d681SAndroid Build Coastguard Worker break;
3762*9880d681SAndroid Build Coastguard Worker }
3763*9880d681SAndroid Build Coastguard Worker // By default, atomic intrinsics are marked Legal and lowered. Targets
3764*9880d681SAndroid Build Coastguard Worker // which don't support them directly, however, may want libcalls, in which
3765*9880d681SAndroid Build Coastguard Worker // case they mark them Expand, and we get here.
3766*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_SWAP:
3767*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_ADD:
3768*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_SUB:
3769*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_AND:
3770*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_OR:
3771*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_XOR:
3772*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_NAND:
3773*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_MIN:
3774*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_MAX:
3775*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_UMIN:
3776*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_LOAD_UMAX:
3777*9880d681SAndroid Build Coastguard Worker case ISD::ATOMIC_CMP_SWAP: {
3778*9880d681SAndroid Build Coastguard Worker MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
3779*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall LC = RTLIB::getSYNC(Opc, VT);
3780*9880d681SAndroid Build Coastguard Worker assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
3781*9880d681SAndroid Build Coastguard Worker
3782*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue> Tmp = ExpandChainLibCall(LC, Node, false);
3783*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp.first);
3784*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp.second);
3785*9880d681SAndroid Build Coastguard Worker break;
3786*9880d681SAndroid Build Coastguard Worker }
3787*9880d681SAndroid Build Coastguard Worker case ISD::TRAP: {
3788*9880d681SAndroid Build Coastguard Worker // If this operation is not supported, lower it to 'abort()' call
3789*9880d681SAndroid Build Coastguard Worker TargetLowering::ArgListTy Args;
3790*9880d681SAndroid Build Coastguard Worker TargetLowering::CallLoweringInfo CLI(DAG);
3791*9880d681SAndroid Build Coastguard Worker CLI.setDebugLoc(dl)
3792*9880d681SAndroid Build Coastguard Worker .setChain(Node->getOperand(0))
3793*9880d681SAndroid Build Coastguard Worker .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
3794*9880d681SAndroid Build Coastguard Worker DAG.getExternalSymbol("abort",
3795*9880d681SAndroid Build Coastguard Worker TLI.getPointerTy(DAG.getDataLayout())),
3796*9880d681SAndroid Build Coastguard Worker std::move(Args));
3797*9880d681SAndroid Build Coastguard Worker std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
3798*9880d681SAndroid Build Coastguard Worker
3799*9880d681SAndroid Build Coastguard Worker Results.push_back(CallResult.second);
3800*9880d681SAndroid Build Coastguard Worker break;
3801*9880d681SAndroid Build Coastguard Worker }
3802*9880d681SAndroid Build Coastguard Worker case ISD::FMINNUM:
3803*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
3804*9880d681SAndroid Build Coastguard Worker RTLIB::FMIN_F80, RTLIB::FMIN_F128,
3805*9880d681SAndroid Build Coastguard Worker RTLIB::FMIN_PPCF128));
3806*9880d681SAndroid Build Coastguard Worker break;
3807*9880d681SAndroid Build Coastguard Worker case ISD::FMAXNUM:
3808*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
3809*9880d681SAndroid Build Coastguard Worker RTLIB::FMAX_F80, RTLIB::FMAX_F128,
3810*9880d681SAndroid Build Coastguard Worker RTLIB::FMAX_PPCF128));
3811*9880d681SAndroid Build Coastguard Worker break;
3812*9880d681SAndroid Build Coastguard Worker case ISD::FSQRT:
3813*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3814*9880d681SAndroid Build Coastguard Worker RTLIB::SQRT_F80, RTLIB::SQRT_F128,
3815*9880d681SAndroid Build Coastguard Worker RTLIB::SQRT_PPCF128));
3816*9880d681SAndroid Build Coastguard Worker break;
3817*9880d681SAndroid Build Coastguard Worker case ISD::FSIN:
3818*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3819*9880d681SAndroid Build Coastguard Worker RTLIB::SIN_F80, RTLIB::SIN_F128,
3820*9880d681SAndroid Build Coastguard Worker RTLIB::SIN_PPCF128));
3821*9880d681SAndroid Build Coastguard Worker break;
3822*9880d681SAndroid Build Coastguard Worker case ISD::FCOS:
3823*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3824*9880d681SAndroid Build Coastguard Worker RTLIB::COS_F80, RTLIB::COS_F128,
3825*9880d681SAndroid Build Coastguard Worker RTLIB::COS_PPCF128));
3826*9880d681SAndroid Build Coastguard Worker break;
3827*9880d681SAndroid Build Coastguard Worker case ISD::FSINCOS:
3828*9880d681SAndroid Build Coastguard Worker // Expand into sincos libcall.
3829*9880d681SAndroid Build Coastguard Worker ExpandSinCosLibCall(Node, Results);
3830*9880d681SAndroid Build Coastguard Worker break;
3831*9880d681SAndroid Build Coastguard Worker case ISD::FLOG:
3832*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3833*9880d681SAndroid Build Coastguard Worker RTLIB::LOG_F80, RTLIB::LOG_F128,
3834*9880d681SAndroid Build Coastguard Worker RTLIB::LOG_PPCF128));
3835*9880d681SAndroid Build Coastguard Worker break;
3836*9880d681SAndroid Build Coastguard Worker case ISD::FLOG2:
3837*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3838*9880d681SAndroid Build Coastguard Worker RTLIB::LOG2_F80, RTLIB::LOG2_F128,
3839*9880d681SAndroid Build Coastguard Worker RTLIB::LOG2_PPCF128));
3840*9880d681SAndroid Build Coastguard Worker break;
3841*9880d681SAndroid Build Coastguard Worker case ISD::FLOG10:
3842*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3843*9880d681SAndroid Build Coastguard Worker RTLIB::LOG10_F80, RTLIB::LOG10_F128,
3844*9880d681SAndroid Build Coastguard Worker RTLIB::LOG10_PPCF128));
3845*9880d681SAndroid Build Coastguard Worker break;
3846*9880d681SAndroid Build Coastguard Worker case ISD::FEXP:
3847*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3848*9880d681SAndroid Build Coastguard Worker RTLIB::EXP_F80, RTLIB::EXP_F128,
3849*9880d681SAndroid Build Coastguard Worker RTLIB::EXP_PPCF128));
3850*9880d681SAndroid Build Coastguard Worker break;
3851*9880d681SAndroid Build Coastguard Worker case ISD::FEXP2:
3852*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3853*9880d681SAndroid Build Coastguard Worker RTLIB::EXP2_F80, RTLIB::EXP2_F128,
3854*9880d681SAndroid Build Coastguard Worker RTLIB::EXP2_PPCF128));
3855*9880d681SAndroid Build Coastguard Worker break;
3856*9880d681SAndroid Build Coastguard Worker case ISD::FTRUNC:
3857*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3858*9880d681SAndroid Build Coastguard Worker RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
3859*9880d681SAndroid Build Coastguard Worker RTLIB::TRUNC_PPCF128));
3860*9880d681SAndroid Build Coastguard Worker break;
3861*9880d681SAndroid Build Coastguard Worker case ISD::FFLOOR:
3862*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3863*9880d681SAndroid Build Coastguard Worker RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
3864*9880d681SAndroid Build Coastguard Worker RTLIB::FLOOR_PPCF128));
3865*9880d681SAndroid Build Coastguard Worker break;
3866*9880d681SAndroid Build Coastguard Worker case ISD::FCEIL:
3867*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3868*9880d681SAndroid Build Coastguard Worker RTLIB::CEIL_F80, RTLIB::CEIL_F128,
3869*9880d681SAndroid Build Coastguard Worker RTLIB::CEIL_PPCF128));
3870*9880d681SAndroid Build Coastguard Worker break;
3871*9880d681SAndroid Build Coastguard Worker case ISD::FRINT:
3872*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3873*9880d681SAndroid Build Coastguard Worker RTLIB::RINT_F80, RTLIB::RINT_F128,
3874*9880d681SAndroid Build Coastguard Worker RTLIB::RINT_PPCF128));
3875*9880d681SAndroid Build Coastguard Worker break;
3876*9880d681SAndroid Build Coastguard Worker case ISD::FNEARBYINT:
3877*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3878*9880d681SAndroid Build Coastguard Worker RTLIB::NEARBYINT_F64,
3879*9880d681SAndroid Build Coastguard Worker RTLIB::NEARBYINT_F80,
3880*9880d681SAndroid Build Coastguard Worker RTLIB::NEARBYINT_F128,
3881*9880d681SAndroid Build Coastguard Worker RTLIB::NEARBYINT_PPCF128));
3882*9880d681SAndroid Build Coastguard Worker break;
3883*9880d681SAndroid Build Coastguard Worker case ISD::FROUND:
3884*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::ROUND_F32,
3885*9880d681SAndroid Build Coastguard Worker RTLIB::ROUND_F64,
3886*9880d681SAndroid Build Coastguard Worker RTLIB::ROUND_F80,
3887*9880d681SAndroid Build Coastguard Worker RTLIB::ROUND_F128,
3888*9880d681SAndroid Build Coastguard Worker RTLIB::ROUND_PPCF128));
3889*9880d681SAndroid Build Coastguard Worker break;
3890*9880d681SAndroid Build Coastguard Worker case ISD::FPOWI:
3891*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3892*9880d681SAndroid Build Coastguard Worker RTLIB::POWI_F80, RTLIB::POWI_F128,
3893*9880d681SAndroid Build Coastguard Worker RTLIB::POWI_PPCF128));
3894*9880d681SAndroid Build Coastguard Worker break;
3895*9880d681SAndroid Build Coastguard Worker case ISD::FPOW:
3896*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3897*9880d681SAndroid Build Coastguard Worker RTLIB::POW_F80, RTLIB::POW_F128,
3898*9880d681SAndroid Build Coastguard Worker RTLIB::POW_PPCF128));
3899*9880d681SAndroid Build Coastguard Worker break;
3900*9880d681SAndroid Build Coastguard Worker case ISD::FDIV:
3901*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3902*9880d681SAndroid Build Coastguard Worker RTLIB::DIV_F80, RTLIB::DIV_F128,
3903*9880d681SAndroid Build Coastguard Worker RTLIB::DIV_PPCF128));
3904*9880d681SAndroid Build Coastguard Worker break;
3905*9880d681SAndroid Build Coastguard Worker case ISD::FREM:
3906*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3907*9880d681SAndroid Build Coastguard Worker RTLIB::REM_F80, RTLIB::REM_F128,
3908*9880d681SAndroid Build Coastguard Worker RTLIB::REM_PPCF128));
3909*9880d681SAndroid Build Coastguard Worker break;
3910*9880d681SAndroid Build Coastguard Worker case ISD::FMA:
3911*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
3912*9880d681SAndroid Build Coastguard Worker RTLIB::FMA_F80, RTLIB::FMA_F128,
3913*9880d681SAndroid Build Coastguard Worker RTLIB::FMA_PPCF128));
3914*9880d681SAndroid Build Coastguard Worker break;
3915*9880d681SAndroid Build Coastguard Worker case ISD::FADD:
3916*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
3917*9880d681SAndroid Build Coastguard Worker RTLIB::ADD_F80, RTLIB::ADD_F128,
3918*9880d681SAndroid Build Coastguard Worker RTLIB::ADD_PPCF128));
3919*9880d681SAndroid Build Coastguard Worker break;
3920*9880d681SAndroid Build Coastguard Worker case ISD::FMUL:
3921*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
3922*9880d681SAndroid Build Coastguard Worker RTLIB::MUL_F80, RTLIB::MUL_F128,
3923*9880d681SAndroid Build Coastguard Worker RTLIB::MUL_PPCF128));
3924*9880d681SAndroid Build Coastguard Worker break;
3925*9880d681SAndroid Build Coastguard Worker case ISD::FP16_TO_FP:
3926*9880d681SAndroid Build Coastguard Worker if (Node->getValueType(0) == MVT::f32) {
3927*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3928*9880d681SAndroid Build Coastguard Worker }
3929*9880d681SAndroid Build Coastguard Worker break;
3930*9880d681SAndroid Build Coastguard Worker case ISD::FP_TO_FP16: {
3931*9880d681SAndroid Build Coastguard Worker RTLIB::Libcall LC =
3932*9880d681SAndroid Build Coastguard Worker RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
3933*9880d681SAndroid Build Coastguard Worker assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
3934*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandLibCall(LC, Node, false));
3935*9880d681SAndroid Build Coastguard Worker break;
3936*9880d681SAndroid Build Coastguard Worker }
3937*9880d681SAndroid Build Coastguard Worker case ISD::FSUB:
3938*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
3939*9880d681SAndroid Build Coastguard Worker RTLIB::SUB_F80, RTLIB::SUB_F128,
3940*9880d681SAndroid Build Coastguard Worker RTLIB::SUB_PPCF128));
3941*9880d681SAndroid Build Coastguard Worker break;
3942*9880d681SAndroid Build Coastguard Worker case ISD::SREM:
3943*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandIntLibCall(Node, true,
3944*9880d681SAndroid Build Coastguard Worker RTLIB::SREM_I8,
3945*9880d681SAndroid Build Coastguard Worker RTLIB::SREM_I16, RTLIB::SREM_I32,
3946*9880d681SAndroid Build Coastguard Worker RTLIB::SREM_I64, RTLIB::SREM_I128));
3947*9880d681SAndroid Build Coastguard Worker break;
3948*9880d681SAndroid Build Coastguard Worker case ISD::UREM:
3949*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandIntLibCall(Node, false,
3950*9880d681SAndroid Build Coastguard Worker RTLIB::UREM_I8,
3951*9880d681SAndroid Build Coastguard Worker RTLIB::UREM_I16, RTLIB::UREM_I32,
3952*9880d681SAndroid Build Coastguard Worker RTLIB::UREM_I64, RTLIB::UREM_I128));
3953*9880d681SAndroid Build Coastguard Worker break;
3954*9880d681SAndroid Build Coastguard Worker case ISD::SDIV:
3955*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandIntLibCall(Node, true,
3956*9880d681SAndroid Build Coastguard Worker RTLIB::SDIV_I8,
3957*9880d681SAndroid Build Coastguard Worker RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3958*9880d681SAndroid Build Coastguard Worker RTLIB::SDIV_I64, RTLIB::SDIV_I128));
3959*9880d681SAndroid Build Coastguard Worker break;
3960*9880d681SAndroid Build Coastguard Worker case ISD::UDIV:
3961*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandIntLibCall(Node, false,
3962*9880d681SAndroid Build Coastguard Worker RTLIB::UDIV_I8,
3963*9880d681SAndroid Build Coastguard Worker RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3964*9880d681SAndroid Build Coastguard Worker RTLIB::UDIV_I64, RTLIB::UDIV_I128));
3965*9880d681SAndroid Build Coastguard Worker break;
3966*9880d681SAndroid Build Coastguard Worker case ISD::SDIVREM:
3967*9880d681SAndroid Build Coastguard Worker case ISD::UDIVREM:
3968*9880d681SAndroid Build Coastguard Worker // Expand into divrem libcall
3969*9880d681SAndroid Build Coastguard Worker ExpandDivRemLibCall(Node, Results);
3970*9880d681SAndroid Build Coastguard Worker break;
3971*9880d681SAndroid Build Coastguard Worker case ISD::MUL:
3972*9880d681SAndroid Build Coastguard Worker Results.push_back(ExpandIntLibCall(Node, false,
3973*9880d681SAndroid Build Coastguard Worker RTLIB::MUL_I8,
3974*9880d681SAndroid Build Coastguard Worker RTLIB::MUL_I16, RTLIB::MUL_I32,
3975*9880d681SAndroid Build Coastguard Worker RTLIB::MUL_I64, RTLIB::MUL_I128));
3976*9880d681SAndroid Build Coastguard Worker break;
3977*9880d681SAndroid Build Coastguard Worker }
3978*9880d681SAndroid Build Coastguard Worker
3979*9880d681SAndroid Build Coastguard Worker // Replace the original node with the legalized result.
3980*9880d681SAndroid Build Coastguard Worker if (!Results.empty())
3981*9880d681SAndroid Build Coastguard Worker ReplaceNode(Node, Results.data());
3982*9880d681SAndroid Build Coastguard Worker }
3983*9880d681SAndroid Build Coastguard Worker
3984*9880d681SAndroid Build Coastguard Worker // Determine the vector type to use in place of an original scalar element when
3985*9880d681SAndroid Build Coastguard Worker // promoting equally sized vectors.
getPromotedVectorElementType(const TargetLowering & TLI,MVT EltVT,MVT NewEltVT)3986*9880d681SAndroid Build Coastguard Worker static MVT getPromotedVectorElementType(const TargetLowering &TLI,
3987*9880d681SAndroid Build Coastguard Worker MVT EltVT, MVT NewEltVT) {
3988*9880d681SAndroid Build Coastguard Worker unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
3989*9880d681SAndroid Build Coastguard Worker MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
3990*9880d681SAndroid Build Coastguard Worker assert(TLI.isTypeLegal(MidVT) && "unexpected");
3991*9880d681SAndroid Build Coastguard Worker return MidVT;
3992*9880d681SAndroid Build Coastguard Worker }
3993*9880d681SAndroid Build Coastguard Worker
PromoteNode(SDNode * Node)3994*9880d681SAndroid Build Coastguard Worker void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
3995*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> Results;
3996*9880d681SAndroid Build Coastguard Worker MVT OVT = Node->getSimpleValueType(0);
3997*9880d681SAndroid Build Coastguard Worker if (Node->getOpcode() == ISD::UINT_TO_FP ||
3998*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::SINT_TO_FP ||
3999*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::SETCC ||
4000*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
4001*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::INSERT_VECTOR_ELT) {
4002*9880d681SAndroid Build Coastguard Worker OVT = Node->getOperand(0).getSimpleValueType();
4003*9880d681SAndroid Build Coastguard Worker }
4004*9880d681SAndroid Build Coastguard Worker if (Node->getOpcode() == ISD::BR_CC)
4005*9880d681SAndroid Build Coastguard Worker OVT = Node->getOperand(2).getSimpleValueType();
4006*9880d681SAndroid Build Coastguard Worker MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
4007*9880d681SAndroid Build Coastguard Worker SDLoc dl(Node);
4008*9880d681SAndroid Build Coastguard Worker SDValue Tmp1, Tmp2, Tmp3;
4009*9880d681SAndroid Build Coastguard Worker switch (Node->getOpcode()) {
4010*9880d681SAndroid Build Coastguard Worker case ISD::CTTZ:
4011*9880d681SAndroid Build Coastguard Worker case ISD::CTTZ_ZERO_UNDEF:
4012*9880d681SAndroid Build Coastguard Worker case ISD::CTLZ:
4013*9880d681SAndroid Build Coastguard Worker case ISD::CTLZ_ZERO_UNDEF:
4014*9880d681SAndroid Build Coastguard Worker case ISD::CTPOP:
4015*9880d681SAndroid Build Coastguard Worker // Zero extend the argument.
4016*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
4017*9880d681SAndroid Build Coastguard Worker if (Node->getOpcode() == ISD::CTTZ) {
4018*9880d681SAndroid Build Coastguard Worker // The count is the same in the promoted type except if the original
4019*9880d681SAndroid Build Coastguard Worker // value was zero. This can be handled by setting the bit just off
4020*9880d681SAndroid Build Coastguard Worker // the top of the original type.
4021*9880d681SAndroid Build Coastguard Worker auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(),
4022*9880d681SAndroid Build Coastguard Worker OVT.getSizeInBits());
4023*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1,
4024*9880d681SAndroid Build Coastguard Worker DAG.getConstant(TopBit, dl, NVT));
4025*9880d681SAndroid Build Coastguard Worker }
4026*9880d681SAndroid Build Coastguard Worker // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
4027*9880d681SAndroid Build Coastguard Worker // already the correct result.
4028*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
4029*9880d681SAndroid Build Coastguard Worker if (Node->getOpcode() == ISD::CTLZ ||
4030*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
4031*9880d681SAndroid Build Coastguard Worker // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
4032*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
4033*9880d681SAndroid Build Coastguard Worker DAG.getConstant(NVT.getSizeInBits() -
4034*9880d681SAndroid Build Coastguard Worker OVT.getSizeInBits(), dl, NVT));
4035*9880d681SAndroid Build Coastguard Worker }
4036*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
4037*9880d681SAndroid Build Coastguard Worker break;
4038*9880d681SAndroid Build Coastguard Worker case ISD::BSWAP: {
4039*9880d681SAndroid Build Coastguard Worker unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
4040*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
4041*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
4042*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(
4043*9880d681SAndroid Build Coastguard Worker ISD::SRL, dl, NVT, Tmp1,
4044*9880d681SAndroid Build Coastguard Worker DAG.getConstant(DiffBits, dl,
4045*9880d681SAndroid Build Coastguard Worker TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
4046*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
4047*9880d681SAndroid Build Coastguard Worker break;
4048*9880d681SAndroid Build Coastguard Worker }
4049*9880d681SAndroid Build Coastguard Worker case ISD::FP_TO_UINT:
4050*9880d681SAndroid Build Coastguard Worker case ISD::FP_TO_SINT:
4051*9880d681SAndroid Build Coastguard Worker Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
4052*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::FP_TO_SINT, dl);
4053*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
4054*9880d681SAndroid Build Coastguard Worker break;
4055*9880d681SAndroid Build Coastguard Worker case ISD::UINT_TO_FP:
4056*9880d681SAndroid Build Coastguard Worker case ISD::SINT_TO_FP:
4057*9880d681SAndroid Build Coastguard Worker Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
4058*9880d681SAndroid Build Coastguard Worker Node->getOpcode() == ISD::SINT_TO_FP, dl);
4059*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
4060*9880d681SAndroid Build Coastguard Worker break;
4061*9880d681SAndroid Build Coastguard Worker case ISD::VAARG: {
4062*9880d681SAndroid Build Coastguard Worker SDValue Chain = Node->getOperand(0); // Get the chain.
4063*9880d681SAndroid Build Coastguard Worker SDValue Ptr = Node->getOperand(1); // Get the pointer.
4064*9880d681SAndroid Build Coastguard Worker
4065*9880d681SAndroid Build Coastguard Worker unsigned TruncOp;
4066*9880d681SAndroid Build Coastguard Worker if (OVT.isVector()) {
4067*9880d681SAndroid Build Coastguard Worker TruncOp = ISD::BITCAST;
4068*9880d681SAndroid Build Coastguard Worker } else {
4069*9880d681SAndroid Build Coastguard Worker assert(OVT.isInteger()
4070*9880d681SAndroid Build Coastguard Worker && "VAARG promotion is supported only for vectors or integer types");
4071*9880d681SAndroid Build Coastguard Worker TruncOp = ISD::TRUNCATE;
4072*9880d681SAndroid Build Coastguard Worker }
4073*9880d681SAndroid Build Coastguard Worker
4074*9880d681SAndroid Build Coastguard Worker // Perform the larger operation, then convert back
4075*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
4076*9880d681SAndroid Build Coastguard Worker Node->getConstantOperandVal(3));
4077*9880d681SAndroid Build Coastguard Worker Chain = Tmp1.getValue(1);
4078*9880d681SAndroid Build Coastguard Worker
4079*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
4080*9880d681SAndroid Build Coastguard Worker
4081*9880d681SAndroid Build Coastguard Worker // Modified the chain result - switch anything that used the old chain to
4082*9880d681SAndroid Build Coastguard Worker // use the new one.
4083*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
4084*9880d681SAndroid Build Coastguard Worker DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
4085*9880d681SAndroid Build Coastguard Worker if (UpdatedNodes) {
4086*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(Tmp2.getNode());
4087*9880d681SAndroid Build Coastguard Worker UpdatedNodes->insert(Chain.getNode());
4088*9880d681SAndroid Build Coastguard Worker }
4089*9880d681SAndroid Build Coastguard Worker ReplacedNode(Node);
4090*9880d681SAndroid Build Coastguard Worker break;
4091*9880d681SAndroid Build Coastguard Worker }
4092*9880d681SAndroid Build Coastguard Worker case ISD::AND:
4093*9880d681SAndroid Build Coastguard Worker case ISD::OR:
4094*9880d681SAndroid Build Coastguard Worker case ISD::XOR: {
4095*9880d681SAndroid Build Coastguard Worker unsigned ExtOp, TruncOp;
4096*9880d681SAndroid Build Coastguard Worker if (OVT.isVector()) {
4097*9880d681SAndroid Build Coastguard Worker ExtOp = ISD::BITCAST;
4098*9880d681SAndroid Build Coastguard Worker TruncOp = ISD::BITCAST;
4099*9880d681SAndroid Build Coastguard Worker } else {
4100*9880d681SAndroid Build Coastguard Worker assert(OVT.isInteger() && "Cannot promote logic operation");
4101*9880d681SAndroid Build Coastguard Worker ExtOp = ISD::ANY_EXTEND;
4102*9880d681SAndroid Build Coastguard Worker TruncOp = ISD::TRUNCATE;
4103*9880d681SAndroid Build Coastguard Worker }
4104*9880d681SAndroid Build Coastguard Worker // Promote each of the values to the new type.
4105*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
4106*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4107*9880d681SAndroid Build Coastguard Worker // Perform the larger operation, then convert back
4108*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
4109*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
4110*9880d681SAndroid Build Coastguard Worker break;
4111*9880d681SAndroid Build Coastguard Worker }
4112*9880d681SAndroid Build Coastguard Worker case ISD::SELECT: {
4113*9880d681SAndroid Build Coastguard Worker unsigned ExtOp, TruncOp;
4114*9880d681SAndroid Build Coastguard Worker if (Node->getValueType(0).isVector() ||
4115*9880d681SAndroid Build Coastguard Worker Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
4116*9880d681SAndroid Build Coastguard Worker ExtOp = ISD::BITCAST;
4117*9880d681SAndroid Build Coastguard Worker TruncOp = ISD::BITCAST;
4118*9880d681SAndroid Build Coastguard Worker } else if (Node->getValueType(0).isInteger()) {
4119*9880d681SAndroid Build Coastguard Worker ExtOp = ISD::ANY_EXTEND;
4120*9880d681SAndroid Build Coastguard Worker TruncOp = ISD::TRUNCATE;
4121*9880d681SAndroid Build Coastguard Worker } else {
4122*9880d681SAndroid Build Coastguard Worker ExtOp = ISD::FP_EXTEND;
4123*9880d681SAndroid Build Coastguard Worker TruncOp = ISD::FP_ROUND;
4124*9880d681SAndroid Build Coastguard Worker }
4125*9880d681SAndroid Build Coastguard Worker Tmp1 = Node->getOperand(0);
4126*9880d681SAndroid Build Coastguard Worker // Promote each of the values to the new type.
4127*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4128*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
4129*9880d681SAndroid Build Coastguard Worker // Perform the larger operation, then round down.
4130*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
4131*9880d681SAndroid Build Coastguard Worker if (TruncOp != ISD::FP_ROUND)
4132*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
4133*9880d681SAndroid Build Coastguard Worker else
4134*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
4135*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(0, dl));
4136*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
4137*9880d681SAndroid Build Coastguard Worker break;
4138*9880d681SAndroid Build Coastguard Worker }
4139*9880d681SAndroid Build Coastguard Worker case ISD::VECTOR_SHUFFLE: {
4140*9880d681SAndroid Build Coastguard Worker ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
4141*9880d681SAndroid Build Coastguard Worker
4142*9880d681SAndroid Build Coastguard Worker // Cast the two input vectors.
4143*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
4144*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
4145*9880d681SAndroid Build Coastguard Worker
4146*9880d681SAndroid Build Coastguard Worker // Convert the shuffle mask to the right # elements.
4147*9880d681SAndroid Build Coastguard Worker Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
4148*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
4149*9880d681SAndroid Build Coastguard Worker Results.push_back(Tmp1);
4150*9880d681SAndroid Build Coastguard Worker break;
4151*9880d681SAndroid Build Coastguard Worker }
4152*9880d681SAndroid Build Coastguard Worker case ISD::SETCC: {
4153*9880d681SAndroid Build Coastguard Worker unsigned ExtOp = ISD::FP_EXTEND;
4154*9880d681SAndroid Build Coastguard Worker if (NVT.isInteger()) {
4155*9880d681SAndroid Build Coastguard Worker ISD::CondCode CCCode =
4156*9880d681SAndroid Build Coastguard Worker cast<CondCodeSDNode>(Node->getOperand(2))->get();
4157*9880d681SAndroid Build Coastguard Worker ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4158*9880d681SAndroid Build Coastguard Worker }
4159*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
4160*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
4161*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
4162*9880d681SAndroid Build Coastguard Worker Tmp1, Tmp2, Node->getOperand(2)));
4163*9880d681SAndroid Build Coastguard Worker break;
4164*9880d681SAndroid Build Coastguard Worker }
4165*9880d681SAndroid Build Coastguard Worker case ISD::BR_CC: {
4166*9880d681SAndroid Build Coastguard Worker unsigned ExtOp = ISD::FP_EXTEND;
4167*9880d681SAndroid Build Coastguard Worker if (NVT.isInteger()) {
4168*9880d681SAndroid Build Coastguard Worker ISD::CondCode CCCode =
4169*9880d681SAndroid Build Coastguard Worker cast<CondCodeSDNode>(Node->getOperand(1))->get();
4170*9880d681SAndroid Build Coastguard Worker ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4171*9880d681SAndroid Build Coastguard Worker }
4172*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
4173*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
4174*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
4175*9880d681SAndroid Build Coastguard Worker Node->getOperand(0), Node->getOperand(1),
4176*9880d681SAndroid Build Coastguard Worker Tmp1, Tmp2, Node->getOperand(4)));
4177*9880d681SAndroid Build Coastguard Worker break;
4178*9880d681SAndroid Build Coastguard Worker }
4179*9880d681SAndroid Build Coastguard Worker case ISD::FADD:
4180*9880d681SAndroid Build Coastguard Worker case ISD::FSUB:
4181*9880d681SAndroid Build Coastguard Worker case ISD::FMUL:
4182*9880d681SAndroid Build Coastguard Worker case ISD::FDIV:
4183*9880d681SAndroid Build Coastguard Worker case ISD::FREM:
4184*9880d681SAndroid Build Coastguard Worker case ISD::FMINNUM:
4185*9880d681SAndroid Build Coastguard Worker case ISD::FMAXNUM:
4186*9880d681SAndroid Build Coastguard Worker case ISD::FPOW: {
4187*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4188*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
4189*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
4190*9880d681SAndroid Build Coastguard Worker Node->getFlags());
4191*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
4192*9880d681SAndroid Build Coastguard Worker Tmp3, DAG.getIntPtrConstant(0, dl)));
4193*9880d681SAndroid Build Coastguard Worker break;
4194*9880d681SAndroid Build Coastguard Worker }
4195*9880d681SAndroid Build Coastguard Worker case ISD::FMA: {
4196*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4197*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
4198*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
4199*9880d681SAndroid Build Coastguard Worker Results.push_back(
4200*9880d681SAndroid Build Coastguard Worker DAG.getNode(ISD::FP_ROUND, dl, OVT,
4201*9880d681SAndroid Build Coastguard Worker DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
4202*9880d681SAndroid Build Coastguard Worker DAG.getIntPtrConstant(0, dl)));
4203*9880d681SAndroid Build Coastguard Worker break;
4204*9880d681SAndroid Build Coastguard Worker }
4205*9880d681SAndroid Build Coastguard Worker case ISD::FCOPYSIGN:
4206*9880d681SAndroid Build Coastguard Worker case ISD::FPOWI: {
4207*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4208*9880d681SAndroid Build Coastguard Worker Tmp2 = Node->getOperand(1);
4209*9880d681SAndroid Build Coastguard Worker Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
4210*9880d681SAndroid Build Coastguard Worker
4211*9880d681SAndroid Build Coastguard Worker // fcopysign doesn't change anything but the sign bit, so
4212*9880d681SAndroid Build Coastguard Worker // (fp_round (fcopysign (fpext a), b))
4213*9880d681SAndroid Build Coastguard Worker // is as precise as
4214*9880d681SAndroid Build Coastguard Worker // (fp_round (fpext a))
4215*9880d681SAndroid Build Coastguard Worker // which is a no-op. Mark it as a TRUNCating FP_ROUND.
4216*9880d681SAndroid Build Coastguard Worker const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
4217*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
4218*9880d681SAndroid Build Coastguard Worker Tmp3, DAG.getIntPtrConstant(isTrunc, dl)));
4219*9880d681SAndroid Build Coastguard Worker break;
4220*9880d681SAndroid Build Coastguard Worker }
4221*9880d681SAndroid Build Coastguard Worker case ISD::FFLOOR:
4222*9880d681SAndroid Build Coastguard Worker case ISD::FCEIL:
4223*9880d681SAndroid Build Coastguard Worker case ISD::FRINT:
4224*9880d681SAndroid Build Coastguard Worker case ISD::FNEARBYINT:
4225*9880d681SAndroid Build Coastguard Worker case ISD::FROUND:
4226*9880d681SAndroid Build Coastguard Worker case ISD::FTRUNC:
4227*9880d681SAndroid Build Coastguard Worker case ISD::FNEG:
4228*9880d681SAndroid Build Coastguard Worker case ISD::FSQRT:
4229*9880d681SAndroid Build Coastguard Worker case ISD::FSIN:
4230*9880d681SAndroid Build Coastguard Worker case ISD::FCOS:
4231*9880d681SAndroid Build Coastguard Worker case ISD::FLOG:
4232*9880d681SAndroid Build Coastguard Worker case ISD::FLOG2:
4233*9880d681SAndroid Build Coastguard Worker case ISD::FLOG10:
4234*9880d681SAndroid Build Coastguard Worker case ISD::FABS:
4235*9880d681SAndroid Build Coastguard Worker case ISD::FEXP:
4236*9880d681SAndroid Build Coastguard Worker case ISD::FEXP2: {
4237*9880d681SAndroid Build Coastguard Worker Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
4238*9880d681SAndroid Build Coastguard Worker Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
4239*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
4240*9880d681SAndroid Build Coastguard Worker Tmp2, DAG.getIntPtrConstant(0, dl)));
4241*9880d681SAndroid Build Coastguard Worker break;
4242*9880d681SAndroid Build Coastguard Worker }
4243*9880d681SAndroid Build Coastguard Worker case ISD::BUILD_VECTOR: {
4244*9880d681SAndroid Build Coastguard Worker MVT EltVT = OVT.getVectorElementType();
4245*9880d681SAndroid Build Coastguard Worker MVT NewEltVT = NVT.getVectorElementType();
4246*9880d681SAndroid Build Coastguard Worker
4247*9880d681SAndroid Build Coastguard Worker // Handle bitcasts to a different vector type with the same total bit size
4248*9880d681SAndroid Build Coastguard Worker //
4249*9880d681SAndroid Build Coastguard Worker // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
4250*9880d681SAndroid Build Coastguard Worker // =>
4251*9880d681SAndroid Build Coastguard Worker // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
4252*9880d681SAndroid Build Coastguard Worker
4253*9880d681SAndroid Build Coastguard Worker assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4254*9880d681SAndroid Build Coastguard Worker "Invalid promote type for build_vector");
4255*9880d681SAndroid Build Coastguard Worker assert(NewEltVT.bitsLT(EltVT) && "not handled");
4256*9880d681SAndroid Build Coastguard Worker
4257*9880d681SAndroid Build Coastguard Worker MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4258*9880d681SAndroid Build Coastguard Worker
4259*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> NewOps;
4260*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
4261*9880d681SAndroid Build Coastguard Worker SDValue Op = Node->getOperand(I);
4262*9880d681SAndroid Build Coastguard Worker NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
4263*9880d681SAndroid Build Coastguard Worker }
4264*9880d681SAndroid Build Coastguard Worker
4265*9880d681SAndroid Build Coastguard Worker SDLoc SL(Node);
4266*9880d681SAndroid Build Coastguard Worker SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps);
4267*9880d681SAndroid Build Coastguard Worker SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
4268*9880d681SAndroid Build Coastguard Worker Results.push_back(CvtVec);
4269*9880d681SAndroid Build Coastguard Worker break;
4270*9880d681SAndroid Build Coastguard Worker }
4271*9880d681SAndroid Build Coastguard Worker case ISD::EXTRACT_VECTOR_ELT: {
4272*9880d681SAndroid Build Coastguard Worker MVT EltVT = OVT.getVectorElementType();
4273*9880d681SAndroid Build Coastguard Worker MVT NewEltVT = NVT.getVectorElementType();
4274*9880d681SAndroid Build Coastguard Worker
4275*9880d681SAndroid Build Coastguard Worker // Handle bitcasts to a different vector type with the same total bit size.
4276*9880d681SAndroid Build Coastguard Worker //
4277*9880d681SAndroid Build Coastguard Worker // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
4278*9880d681SAndroid Build Coastguard Worker // =>
4279*9880d681SAndroid Build Coastguard Worker // v4i32:castx = bitcast x:v2i64
4280*9880d681SAndroid Build Coastguard Worker //
4281*9880d681SAndroid Build Coastguard Worker // i64 = bitcast
4282*9880d681SAndroid Build Coastguard Worker // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
4283*9880d681SAndroid Build Coastguard Worker // (i32 (extract_vector_elt castx, (2 * y + 1)))
4284*9880d681SAndroid Build Coastguard Worker //
4285*9880d681SAndroid Build Coastguard Worker
4286*9880d681SAndroid Build Coastguard Worker assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4287*9880d681SAndroid Build Coastguard Worker "Invalid promote type for extract_vector_elt");
4288*9880d681SAndroid Build Coastguard Worker assert(NewEltVT.bitsLT(EltVT) && "not handled");
4289*9880d681SAndroid Build Coastguard Worker
4290*9880d681SAndroid Build Coastguard Worker MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4291*9880d681SAndroid Build Coastguard Worker unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
4292*9880d681SAndroid Build Coastguard Worker
4293*9880d681SAndroid Build Coastguard Worker SDValue Idx = Node->getOperand(1);
4294*9880d681SAndroid Build Coastguard Worker EVT IdxVT = Idx.getValueType();
4295*9880d681SAndroid Build Coastguard Worker SDLoc SL(Node);
4296*9880d681SAndroid Build Coastguard Worker SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
4297*9880d681SAndroid Build Coastguard Worker SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
4298*9880d681SAndroid Build Coastguard Worker
4299*9880d681SAndroid Build Coastguard Worker SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
4300*9880d681SAndroid Build Coastguard Worker
4301*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> NewOps;
4302*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
4303*9880d681SAndroid Build Coastguard Worker SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
4304*9880d681SAndroid Build Coastguard Worker SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
4305*9880d681SAndroid Build Coastguard Worker
4306*9880d681SAndroid Build Coastguard Worker SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
4307*9880d681SAndroid Build Coastguard Worker CastVec, TmpIdx);
4308*9880d681SAndroid Build Coastguard Worker NewOps.push_back(Elt);
4309*9880d681SAndroid Build Coastguard Worker }
4310*9880d681SAndroid Build Coastguard Worker
4311*9880d681SAndroid Build Coastguard Worker SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, SL, MidVT, NewOps);
4312*9880d681SAndroid Build Coastguard Worker
4313*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
4314*9880d681SAndroid Build Coastguard Worker break;
4315*9880d681SAndroid Build Coastguard Worker }
4316*9880d681SAndroid Build Coastguard Worker case ISD::INSERT_VECTOR_ELT: {
4317*9880d681SAndroid Build Coastguard Worker MVT EltVT = OVT.getVectorElementType();
4318*9880d681SAndroid Build Coastguard Worker MVT NewEltVT = NVT.getVectorElementType();
4319*9880d681SAndroid Build Coastguard Worker
4320*9880d681SAndroid Build Coastguard Worker // Handle bitcasts to a different vector type with the same total bit size
4321*9880d681SAndroid Build Coastguard Worker //
4322*9880d681SAndroid Build Coastguard Worker // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
4323*9880d681SAndroid Build Coastguard Worker // =>
4324*9880d681SAndroid Build Coastguard Worker // v4i32:castx = bitcast x:v2i64
4325*9880d681SAndroid Build Coastguard Worker // v2i32:casty = bitcast y:i64
4326*9880d681SAndroid Build Coastguard Worker //
4327*9880d681SAndroid Build Coastguard Worker // v2i64 = bitcast
4328*9880d681SAndroid Build Coastguard Worker // (v4i32 insert_vector_elt
4329*9880d681SAndroid Build Coastguard Worker // (v4i32 insert_vector_elt v4i32:castx,
4330*9880d681SAndroid Build Coastguard Worker // (extract_vector_elt casty, 0), 2 * z),
4331*9880d681SAndroid Build Coastguard Worker // (extract_vector_elt casty, 1), (2 * z + 1))
4332*9880d681SAndroid Build Coastguard Worker
4333*9880d681SAndroid Build Coastguard Worker assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
4334*9880d681SAndroid Build Coastguard Worker "Invalid promote type for insert_vector_elt");
4335*9880d681SAndroid Build Coastguard Worker assert(NewEltVT.bitsLT(EltVT) && "not handled");
4336*9880d681SAndroid Build Coastguard Worker
4337*9880d681SAndroid Build Coastguard Worker MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4338*9880d681SAndroid Build Coastguard Worker unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
4339*9880d681SAndroid Build Coastguard Worker
4340*9880d681SAndroid Build Coastguard Worker SDValue Val = Node->getOperand(1);
4341*9880d681SAndroid Build Coastguard Worker SDValue Idx = Node->getOperand(2);
4342*9880d681SAndroid Build Coastguard Worker EVT IdxVT = Idx.getValueType();
4343*9880d681SAndroid Build Coastguard Worker SDLoc SL(Node);
4344*9880d681SAndroid Build Coastguard Worker
4345*9880d681SAndroid Build Coastguard Worker SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
4346*9880d681SAndroid Build Coastguard Worker SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
4347*9880d681SAndroid Build Coastguard Worker
4348*9880d681SAndroid Build Coastguard Worker SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
4349*9880d681SAndroid Build Coastguard Worker SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
4350*9880d681SAndroid Build Coastguard Worker
4351*9880d681SAndroid Build Coastguard Worker SDValue NewVec = CastVec;
4352*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
4353*9880d681SAndroid Build Coastguard Worker SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
4354*9880d681SAndroid Build Coastguard Worker SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
4355*9880d681SAndroid Build Coastguard Worker
4356*9880d681SAndroid Build Coastguard Worker SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
4357*9880d681SAndroid Build Coastguard Worker CastVal, IdxOffset);
4358*9880d681SAndroid Build Coastguard Worker
4359*9880d681SAndroid Build Coastguard Worker NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
4360*9880d681SAndroid Build Coastguard Worker NewVec, Elt, InEltIdx);
4361*9880d681SAndroid Build Coastguard Worker }
4362*9880d681SAndroid Build Coastguard Worker
4363*9880d681SAndroid Build Coastguard Worker Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
4364*9880d681SAndroid Build Coastguard Worker break;
4365*9880d681SAndroid Build Coastguard Worker }
4366*9880d681SAndroid Build Coastguard Worker case ISD::SCALAR_TO_VECTOR: {
4367*9880d681SAndroid Build Coastguard Worker MVT EltVT = OVT.getVectorElementType();
4368*9880d681SAndroid Build Coastguard Worker MVT NewEltVT = NVT.getVectorElementType();
4369*9880d681SAndroid Build Coastguard Worker
4370*9880d681SAndroid Build Coastguard Worker // Handle bitcasts to different vector type with the smae total bit size.
4371*9880d681SAndroid Build Coastguard Worker //
4372*9880d681SAndroid Build Coastguard Worker // e.g. v2i64 = scalar_to_vector x:i64
4373*9880d681SAndroid Build Coastguard Worker // =>
4374*9880d681SAndroid Build Coastguard Worker // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
4375*9880d681SAndroid Build Coastguard Worker //
4376*9880d681SAndroid Build Coastguard Worker
4377*9880d681SAndroid Build Coastguard Worker MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
4378*9880d681SAndroid Build Coastguard Worker SDValue Val = Node->getOperand(0);
4379*9880d681SAndroid Build Coastguard Worker SDLoc SL(Node);
4380*9880d681SAndroid Build Coastguard Worker
4381*9880d681SAndroid Build Coastguard Worker SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
4382*9880d681SAndroid Build Coastguard Worker SDValue Undef = DAG.getUNDEF(MidVT);
4383*9880d681SAndroid Build Coastguard Worker
4384*9880d681SAndroid Build Coastguard Worker SmallVector<SDValue, 8> NewElts;
4385*9880d681SAndroid Build Coastguard Worker NewElts.push_back(CastVal);
4386*9880d681SAndroid Build Coastguard Worker for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
4387*9880d681SAndroid Build Coastguard Worker NewElts.push_back(Undef);
4388*9880d681SAndroid Build Coastguard Worker
4389*9880d681SAndroid Build Coastguard Worker SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
4390*9880d681SAndroid Build Coastguard Worker SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
4391*9880d681SAndroid Build Coastguard Worker Results.push_back(CvtVec);
4392*9880d681SAndroid Build Coastguard Worker break;
4393*9880d681SAndroid Build Coastguard Worker }
4394*9880d681SAndroid Build Coastguard Worker }
4395*9880d681SAndroid Build Coastguard Worker
4396*9880d681SAndroid Build Coastguard Worker // Replace the original node with the legalized result.
4397*9880d681SAndroid Build Coastguard Worker if (!Results.empty())
4398*9880d681SAndroid Build Coastguard Worker ReplaceNode(Node, Results.data());
4399*9880d681SAndroid Build Coastguard Worker }
4400*9880d681SAndroid Build Coastguard Worker
4401*9880d681SAndroid Build Coastguard Worker /// This is the entry point for the file.
Legalize()4402*9880d681SAndroid Build Coastguard Worker void SelectionDAG::Legalize() {
4403*9880d681SAndroid Build Coastguard Worker AssignTopologicalOrder();
4404*9880d681SAndroid Build Coastguard Worker
4405*9880d681SAndroid Build Coastguard Worker SmallPtrSet<SDNode *, 16> LegalizedNodes;
4406*9880d681SAndroid Build Coastguard Worker SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
4407*9880d681SAndroid Build Coastguard Worker
4408*9880d681SAndroid Build Coastguard Worker // Visit all the nodes. We start in topological order, so that we see
4409*9880d681SAndroid Build Coastguard Worker // nodes with their original operands intact. Legalization can produce
4410*9880d681SAndroid Build Coastguard Worker // new nodes which may themselves need to be legalized. Iterate until all
4411*9880d681SAndroid Build Coastguard Worker // nodes have been legalized.
4412*9880d681SAndroid Build Coastguard Worker for (;;) {
4413*9880d681SAndroid Build Coastguard Worker bool AnyLegalized = false;
4414*9880d681SAndroid Build Coastguard Worker for (auto NI = allnodes_end(); NI != allnodes_begin();) {
4415*9880d681SAndroid Build Coastguard Worker --NI;
4416*9880d681SAndroid Build Coastguard Worker
4417*9880d681SAndroid Build Coastguard Worker SDNode *N = &*NI;
4418*9880d681SAndroid Build Coastguard Worker if (N->use_empty() && N != getRoot().getNode()) {
4419*9880d681SAndroid Build Coastguard Worker ++NI;
4420*9880d681SAndroid Build Coastguard Worker DeleteNode(N);
4421*9880d681SAndroid Build Coastguard Worker continue;
4422*9880d681SAndroid Build Coastguard Worker }
4423*9880d681SAndroid Build Coastguard Worker
4424*9880d681SAndroid Build Coastguard Worker if (LegalizedNodes.insert(N).second) {
4425*9880d681SAndroid Build Coastguard Worker AnyLegalized = true;
4426*9880d681SAndroid Build Coastguard Worker Legalizer.LegalizeOp(N);
4427*9880d681SAndroid Build Coastguard Worker
4428*9880d681SAndroid Build Coastguard Worker if (N->use_empty() && N != getRoot().getNode()) {
4429*9880d681SAndroid Build Coastguard Worker ++NI;
4430*9880d681SAndroid Build Coastguard Worker DeleteNode(N);
4431*9880d681SAndroid Build Coastguard Worker }
4432*9880d681SAndroid Build Coastguard Worker }
4433*9880d681SAndroid Build Coastguard Worker }
4434*9880d681SAndroid Build Coastguard Worker if (!AnyLegalized)
4435*9880d681SAndroid Build Coastguard Worker break;
4436*9880d681SAndroid Build Coastguard Worker
4437*9880d681SAndroid Build Coastguard Worker }
4438*9880d681SAndroid Build Coastguard Worker
4439*9880d681SAndroid Build Coastguard Worker // Remove dead nodes now.
4440*9880d681SAndroid Build Coastguard Worker RemoveDeadNodes();
4441*9880d681SAndroid Build Coastguard Worker }
4442*9880d681SAndroid Build Coastguard Worker
LegalizeOp(SDNode * N,SmallSetVector<SDNode *,16> & UpdatedNodes)4443*9880d681SAndroid Build Coastguard Worker bool SelectionDAG::LegalizeOp(SDNode *N,
4444*9880d681SAndroid Build Coastguard Worker SmallSetVector<SDNode *, 16> &UpdatedNodes) {
4445*9880d681SAndroid Build Coastguard Worker SmallPtrSet<SDNode *, 16> LegalizedNodes;
4446*9880d681SAndroid Build Coastguard Worker SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
4447*9880d681SAndroid Build Coastguard Worker
4448*9880d681SAndroid Build Coastguard Worker // Directly insert the node in question, and legalize it. This will recurse
4449*9880d681SAndroid Build Coastguard Worker // as needed through operands.
4450*9880d681SAndroid Build Coastguard Worker LegalizedNodes.insert(N);
4451*9880d681SAndroid Build Coastguard Worker Legalizer.LegalizeOp(N);
4452*9880d681SAndroid Build Coastguard Worker
4453*9880d681SAndroid Build Coastguard Worker return LegalizedNodes.count(N);
4454*9880d681SAndroid Build Coastguard Worker }
4455