blob: 263333de4cc69edca319b14d225141989e110223 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng3d2125c2010-11-30 23:55:39 +000014#include "llvm/Analysis/DebugInfo.h"
15#include "llvm/CodeGen/Analysis.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000016#include "llvm/CodeGen/MachineFunction.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng3d2125c2010-11-30 23:55:39 +000018#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000019#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000020#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000021#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000026#include "llvm/LLVMContext.h"
David Greene993aace2010-01-05 01:24:53 +000027#include "llvm/Support/Debug.h"
Jim Grosbache03262f2010-06-18 21:43:38 +000028#include "llvm/Support/ErrorHandling.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattner79715142007-02-03 01:12:36 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000032#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000033#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000034using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
38/// hacks on it until the target machine can handle it. This involves
39/// eliminating value sizes the machine cannot handle (promoting small sizes to
40/// large sizes or splitting up large values into small values) as well as
41/// eliminating operations the machine cannot handle.
42///
43/// This code also does a small amount of optimization and recognition of idioms
44/// as part of its processing. For example, if a target does not support a
45/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
46/// will attempt merge setcc and brc instructions into brcc's.
47///
48namespace {
Dan Gohman2ba60e52011-10-28 01:29:32 +000049class SelectionDAGLegalize : public SelectionDAG::DAGUpdateListener {
Dan Gohman55e59c12010-04-19 19:05:59 +000050 const TargetMachine &TM;
Dan Gohmand858e902010-04-17 15:26:15 +000051 const TargetLowering &TLI;
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 SelectionDAG &DAG;
53
Dan Gohman2ba60e52011-10-28 01:29:32 +000054 /// LegalizePosition - The iterator for walking through the node list.
55 SelectionDAG::allnodes_iterator LegalizePosition;
56
57 /// LegalizedNodes - The set of nodes which have already been legalized.
58 SmallPtrSet<SDNode *, 16> LegalizedNodes;
59
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000061
Chris Lattner3e928bb2005-01-07 07:47:09 +000062public:
Dan Gohman975716a2011-05-16 22:19:54 +000063 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000064
Chris Lattner3e928bb2005-01-07 07:47:09 +000065 void LegalizeDAG();
66
Chris Lattner456a93a2006-01-28 07:39:30 +000067private:
Dan Gohman2ba60e52011-10-28 01:29:32 +000068 /// LegalizeOp - Legalizes the given operation.
69 void LegalizeOp(SDNode *Node);
Scott Michelfdc40a02009-02-17 22:15:04 +000070
Eli Friedman7ef3d172009-06-06 07:04:42 +000071 SDValue OptimizeFloatStore(StoreSDNode *ST);
72
Nate Begeman68679912008-04-25 18:07:40 +000073 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
74 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
75 /// is necessary to spill the vector being inserted into to memory, perform
76 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +000077 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +000078 SDValue Idx, DebugLoc dl);
79 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
80 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +000081
Nate Begeman5a5ca152009-04-29 05:20:52 +000082 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
83 /// performs the same shuffe in terms of order or result bytes, but on a type
84 /// whose vector element type is narrower than the original shuffle type.
85 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +000086 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +000087 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +000088 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +000089
Owen Andersone50ed302009-08-10 22:56:29 +000090 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +000091 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +000092
Eli Friedman47b41f72009-05-27 02:21:29 +000093 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Eric Christopherabbbfbd2011-04-20 01:19:45 +000094 SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
95 unsigned NumOps, bool isSigned, DebugLoc dl);
96
Jim Grosbache03262f2010-06-18 21:43:38 +000097 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
98 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +000099 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
100 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
101 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000102 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
103 RTLIB::Libcall Call_I8,
104 RTLIB::Libcall Call_I16,
105 RTLIB::Libcall Call_I32,
106 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000107 RTLIB::Libcall Call_I128);
Evan Cheng65279cb2011-04-16 03:08:26 +0000108 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattnercad063f2005-07-16 00:19:57 +0000109
Owen Andersone50ed302009-08-10 22:56:29 +0000110 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000111 SDValue ExpandBUILD_VECTOR(SDNode *Node);
112 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000113 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
114 SmallVectorImpl<SDValue> &Results);
115 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000116 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000117 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000118 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000119 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000120 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000121 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000122
Dale Johannesen8a782a22009-02-02 22:12:50 +0000123 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
124 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000125
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000126 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenecfe33c42011-01-26 19:13:22 +0000127 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000128 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000129
Dan Gohman2ba60e52011-10-28 01:29:32 +0000130 SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
131
Jim Grosbache03262f2010-06-18 21:43:38 +0000132 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
133
Dan Gohman2ba60e52011-10-28 01:29:32 +0000134 void ExpandNode(SDNode *Node);
135 void PromoteNode(SDNode *Node);
136
137 // DAGUpdateListener implementation.
138 virtual void NodeDeleted(SDNode *N, SDNode *E) {
139 LegalizedNodes.erase(N);
140 if (LegalizePosition == SelectionDAG::allnodes_iterator(N))
141 ++LegalizePosition;
142 }
143
144 virtual void NodeUpdated(SDNode *N) {}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000145};
146}
147
Nate Begeman5a5ca152009-04-29 05:20:52 +0000148/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
149/// performs the same shuffe in terms of order or result bytes, but on a type
150/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000151/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000152SDValue
153SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000154 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000155 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000156 unsigned NumMaskElts = VT.getVectorNumElements();
157 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000158 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000159
Nate Begeman9008ca62009-04-27 18:41:29 +0000160 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
161
162 if (NumEltsGrowth == 1)
163 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000164
Nate Begeman9008ca62009-04-27 18:41:29 +0000165 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000166 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000167 int Idx = Mask[i];
168 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000169 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000170 NewMask.push_back(-1);
171 else
172 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000173 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000174 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000175 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000176 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
177 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000178}
179
Dan Gohman975716a2011-05-16 22:19:54 +0000180SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
Dan Gohman55e59c12010-04-19 19:05:59 +0000181 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
Dan Gohmanea027022011-07-15 22:19:02 +0000182 DAG(dag) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000183}
184
Chris Lattner3e928bb2005-01-07 07:47:09 +0000185void SelectionDAGLegalize::LegalizeDAG() {
Dan Gohmanf06c8352008-09-30 18:30:35 +0000186 DAG.AssignTopologicalOrder();
Dan Gohman2ba60e52011-10-28 01:29:32 +0000187
188#if 0
189 SDValue LastChain = DAG.getEntryNode();
Dan Gohmanf06c8352008-09-30 18:30:35 +0000190 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Dan Gohman2ba60e52011-10-28 01:29:32 +0000191 E = DAG.allnodes_end(); I != E; ++I) {
192 SDNode *N = I;
193 if (N->getOpcode() == ISD::CALLSEQ_START) {
194 SmallVector<SDValue, 4> Ops(N->op_begin(), N->op_end());
195 Ops[0] = LastChain;
196 SDNode *New = DAG.UpdateNodeOperands(N, Ops.data(), Ops.size());
197 assert(New == N && "CALLSEQ_START got CSE'd!");
198 }
199 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i)
200 if (N->getValueType(i) == MVT::Other)
201 LastChain = SDValue(N, i);
202 }
203#endif
Chris Lattner32fca002005-10-06 01:20:27 +0000204
Dan Gohman2ba60e52011-10-28 01:29:32 +0000205 // Visit all the nodes. We start in topological order, so that we see
206 // nodes with their original operands intact. Legalization can produce
207 // new nodes which may themselves need to be legalized. Iterate until all
208 // nodes have been legalized.
209 for (;;) {
210 bool AnyLegalized = false;
211 for (LegalizePosition = DAG.allnodes_end();
212 LegalizePosition != DAG.allnodes_begin(); ) {
213 --LegalizePosition;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000214
Dan Gohman2ba60e52011-10-28 01:29:32 +0000215 SDNode *N = LegalizePosition;
216 if (LegalizedNodes.insert(N)) {
217 AnyLegalized = true;
218 LegalizeOp(N);
219 }
220 }
221 if (!AnyLegalized)
222 break;
223
224 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000225
226 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000227 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000228}
229
Evan Cheng9f877882006-12-13 20:57:08 +0000230/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
231/// a load from the constant pool.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000232SDValue
233SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
Evan Cheng00495212006-12-12 21:32:44 +0000234 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000235 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000236
237 // If a FP immediate is precise when represented as a float and if the
238 // target can do an extending load from float to double, we put it into
239 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000240 // double. This shrinks FP constants and canonicalizes them for targets where
241 // an FP extending load is the same cost as a normal load (such as on the x87
242 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000243 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000244 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000245 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000247 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000249 }
250
Owen Andersone50ed302009-08-10 22:56:29 +0000251 EVT OrigVT = VT;
252 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 while (SVT != MVT::f32) {
254 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000255 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000256 // Only do this if the target has a native EXTLOAD instruction from
257 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000258 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000259 TLI.ShouldShrinkFPConstant(OrigVT)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000260 Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000261 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000262 VT = SVT;
263 Extend = true;
264 }
Evan Cheng00495212006-12-12 21:32:44 +0000265 }
266
Dan Gohman475871a2008-07-27 21:46:04 +0000267 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000268 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman2ba60e52011-10-28 01:29:32 +0000269 if (Extend) {
270 SDValue Result =
271 DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
272 DAG.getEntryNode(),
273 CPIdx, MachinePointerInfo::getConstantPool(),
274 VT, false, false, Alignment);
275 return Result;
276 }
277 SDValue Result =
278 DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
279 MachinePointerInfo::getConstantPool(), false, false,
280 Alignment);
281 return Result;
Evan Cheng00495212006-12-12 21:32:44 +0000282}
283
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000284/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000285static void ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
286 const TargetLowering &TLI,
287 SelectionDAG::DAGUpdateListener *DUL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000288 SDValue Chain = ST->getChain();
289 SDValue Ptr = ST->getBasePtr();
290 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000291 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000292 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000293 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000294 if (ST->getMemoryVT().isFloatingPoint() ||
295 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000296 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000297 if (TLI.isTypeLegal(intVT)) {
298 // Expand to a bitconvert of the value to the integer type of the
299 // same size, then a (misaligned) int store.
300 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000301 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Dan Gohman2ba60e52011-10-28 01:29:32 +0000302 Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
303 ST->isVolatile(), ST->isNonTemporal(), Alignment);
304 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
305 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000306 }
Dan Gohman1b328962011-05-17 22:22:52 +0000307 // Do a (aligned) store to a stack slot, then copy from the stack slot
308 // to the final destination using (unaligned) integer loads and stores.
309 EVT StoredVT = ST->getMemoryVT();
310 EVT RegVT =
311 TLI.getRegisterType(*DAG.getContext(),
312 EVT::getIntegerVT(*DAG.getContext(),
313 StoredVT.getSizeInBits()));
314 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
315 unsigned RegBytes = RegVT.getSizeInBits() / 8;
316 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
317
318 // Make sure the stack slot is also aligned for the register type.
319 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
320
321 // Perform the original store, only redirected to the stack slot.
322 SDValue Store = DAG.getTruncStore(Chain, dl,
323 Val, StackPtr, MachinePointerInfo(),
324 StoredVT, false, false, 0);
325 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
326 SmallVector<SDValue, 8> Stores;
327 unsigned Offset = 0;
328
329 // Do all but one copies using the full register width.
330 for (unsigned i = 1; i < NumRegs; i++) {
331 // Load one integer register's worth from the stack slot.
332 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
333 MachinePointerInfo(),
334 false, false, 0);
335 // Store it to the final location. Remember the store.
336 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
337 ST->getPointerInfo().getWithOffset(Offset),
338 ST->isVolatile(), ST->isNonTemporal(),
339 MinAlign(ST->getAlignment(), Offset)));
340 // Increment the pointers.
341 Offset += RegBytes;
342 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
343 Increment);
344 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
345 }
346
347 // The last store may be partial. Do a truncating store. On big-endian
348 // machines this requires an extending load from the stack slot to ensure
349 // that the bits are in the right place.
350 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
351 8 * (StoredBytes - Offset));
352
353 // Load from the stack slot.
354 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
355 MachinePointerInfo(),
356 MemVT, false, false, 0);
357
358 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
359 ST->getPointerInfo()
360 .getWithOffset(Offset),
361 MemVT, ST->isVolatile(),
362 ST->isNonTemporal(),
363 MinAlign(ST->getAlignment(), Offset)));
364 // The order of the stores doesn't matter - say it with a TokenFactor.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000365 SDValue Result =
366 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
367 Stores.size());
368 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
369 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000370 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000371 assert(ST->getMemoryVT().isInteger() &&
372 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000373 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000374 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000375 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000376 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000377 int IncrementSize = NumBits / 8;
378
379 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000380 SDValue ShiftAmount = DAG.getConstant(NumBits,
381 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000382 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000383 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000384
385 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000386 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000387 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000388 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000389 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000390 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000391 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000392 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000393 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000394 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000395 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
396 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000397
Dan Gohman2ba60e52011-10-28 01:29:32 +0000398 SDValue Result =
399 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
400 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000401}
402
403/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000404static void
405ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
406 const TargetLowering &TLI,
407 SDValue &ValResult, SDValue &ChainResult) {
Dan Gohman475871a2008-07-27 21:46:04 +0000408 SDValue Chain = LD->getChain();
409 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000410 EVT VT = LD->getValueType(0);
411 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000412 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000413 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000414 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000415 if (TLI.isTypeLegal(intVT)) {
416 // Expand to a (misaligned) integer load of the same size,
417 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000418 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
419 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000420 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000421 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000422 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000423 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000424
Dan Gohman2ba60e52011-10-28 01:29:32 +0000425 ValResult = Result;
426 ChainResult = Chain;
427 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000428 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000429
Chris Lattnerecf42c42010-09-21 16:36:31 +0000430 // Copy the value to a (aligned) stack slot using (unaligned) integer
431 // loads and stores, then do a (aligned) load from the stack slot.
432 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
433 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
434 unsigned RegBytes = RegVT.getSizeInBits() / 8;
435 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
436
437 // Make sure the stack slot is also aligned for the register type.
438 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
439
440 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
441 SmallVector<SDValue, 8> Stores;
442 SDValue StackPtr = StackBase;
443 unsigned Offset = 0;
444
445 // Do all but one copies using the full register width.
446 for (unsigned i = 1; i < NumRegs; i++) {
447 // Load one integer register's worth from the original location.
448 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
449 LD->getPointerInfo().getWithOffset(Offset),
450 LD->isVolatile(), LD->isNonTemporal(),
451 MinAlign(LD->getAlignment(), Offset));
452 // Follow the load with a store to the stack slot. Remember the store.
453 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000454 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000455 // Increment the pointers.
456 Offset += RegBytes;
457 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
458 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
459 Increment);
460 }
461
462 // The last copy may be partial. Do an extending load.
463 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
464 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000465 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000466 LD->getPointerInfo().getWithOffset(Offset),
467 MemVT, LD->isVolatile(),
468 LD->isNonTemporal(),
469 MinAlign(LD->getAlignment(), Offset));
470 // Follow the load with a store to the stack slot. Remember the store.
471 // On big-endian machines this requires a truncating store to ensure
472 // that the bits end up in the right place.
473 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
474 MachinePointerInfo(), MemVT,
475 false, false, 0));
476
477 // The order of the stores doesn't matter - say it with a TokenFactor.
478 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
479 Stores.size());
480
481 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000482 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000483 MachinePointerInfo(), LoadedVT, false, false, 0);
484
485 // Callers expect a MERGE_VALUES node.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000486 ValResult = Load;
487 ChainResult = TF;
488 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000489 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000490 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000491 "Unaligned load of unsupported type.");
492
Dale Johannesen8155d642008-02-27 22:36:00 +0000493 // Compute the new VT that is half the size of the old one. This is an
494 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000495 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000496 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000497 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000498 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000499
Chris Lattnere400af82007-11-19 21:38:03 +0000500 unsigned Alignment = LD->getAlignment();
501 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000502 ISD::LoadExtType HiExtType = LD->getExtensionType();
503
504 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
505 if (HiExtType == ISD::NON_EXTLOAD)
506 HiExtType = ISD::ZEXTLOAD;
507
508 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000509 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000510 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000511 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000512 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000513 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000514 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000515 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000516 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000517 LD->getPointerInfo().getWithOffset(IncrementSize),
518 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000519 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000520 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000521 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000522 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000523 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000524 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000525 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000526 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000527 LD->getPointerInfo().getWithOffset(IncrementSize),
528 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000529 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000530 }
531
532 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000533 SDValue ShiftAmount = DAG.getConstant(NumBits,
534 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000535 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
536 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000539 Hi.getValue(1));
540
Dan Gohman2ba60e52011-10-28 01:29:32 +0000541 ValResult = Result;
542 ChainResult = TF;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000543}
Evan Cheng912095b2007-01-04 21:56:39 +0000544
Nate Begeman68679912008-04-25 18:07:40 +0000545/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
546/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
547/// is necessary to spill the vector being inserted into to memory, perform
548/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000549SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000550PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
551 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000552 SDValue Tmp1 = Vec;
553 SDValue Tmp2 = Val;
554 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000555
Nate Begeman68679912008-04-25 18:07:40 +0000556 // If the target doesn't support this, we have to spill the input vector
557 // to a temporary stack slot, update the element, then reload it. This is
558 // badness. We could also load the value into a vector register (either
559 // with a "move to register" or "extload into register" instruction, then
560 // permute it into place, if the idx is a constant and if the idx is
561 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000562 EVT VT = Tmp1.getValueType();
563 EVT EltVT = VT.getVectorElementType();
564 EVT IdxVT = Tmp3.getValueType();
565 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000566 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000567
Evan Chengff89dcb2009-10-18 18:16:27 +0000568 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
569
Nate Begeman68679912008-04-25 18:07:40 +0000570 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000571 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000572 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000573 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000574
575 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000576 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000577 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000578 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000579 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000580 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
581 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000582 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000583 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000584 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000585 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000586 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000587 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000588}
589
Mon P Wange9f10152008-12-09 05:46:39 +0000590
Eli Friedman3f727d62009-05-27 02:16:40 +0000591SDValue SelectionDAGLegalize::
592ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
593 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
594 // SCALAR_TO_VECTOR requires that the type of the value being inserted
595 // match the element type of the vector being created, except for
596 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000597 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000598 if (Val.getValueType() == EltVT ||
599 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
600 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
601 Vec.getValueType(), Val);
602
603 unsigned NumElts = Vec.getValueType().getVectorNumElements();
604 // We generate a shuffle of InVec and ScVec, so the shuffle mask
605 // should be 0,1,2,3,4,5... with the appropriate element replaced with
606 // elt 0 of the RHS.
607 SmallVector<int, 8> ShufOps;
608 for (unsigned i = 0; i != NumElts; ++i)
609 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
610
611 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
612 &ShufOps[0]);
613 }
614 }
615 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
616}
617
Eli Friedman7ef3d172009-06-06 07:04:42 +0000618SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
619 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
620 // FIXME: We shouldn't do this for TargetConstantFP's.
621 // FIXME: move this to the DAG Combiner! Note that we can't regress due
622 // to phase ordering between legalized code and the dag combiner. This
623 // probably means that we need to integrate dag combiner and legalizer
624 // together.
625 // We generally can't do this one for long doubles.
626 SDValue Tmp1 = ST->getChain();
627 SDValue Tmp2 = ST->getBasePtr();
628 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000629 unsigned Alignment = ST->getAlignment();
630 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000631 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000632 DebugLoc dl = ST->getDebugLoc();
633 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 if (CFP->getValueType(0) == MVT::f32 &&
Dan Gohman75b10042011-07-15 22:39:09 +0000635 TLI.isTypeLegal(MVT::i32)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000636 Tmp3 = DAG.getConstant(CFP->getValueAPF().
637 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000639 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
640 isVolatile, isNonTemporal, Alignment);
641 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000642
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000643 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000644 // If this target supports 64-bit registers, do a single 64-bit store.
Dan Gohman75b10042011-07-15 22:39:09 +0000645 if (TLI.isTypeLegal(MVT::i64)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000646 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000648 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
649 isVolatile, isNonTemporal, Alignment);
650 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000651
Dan Gohman75b10042011-07-15 22:39:09 +0000652 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000653 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
654 // stores. If the target supports neither 32- nor 64-bits, this
655 // xform is certainly not worth it.
656 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000657 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000659 if (TLI.isBigEndian()) std::swap(Lo, Hi);
660
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000661 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
662 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000663 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
664 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000665 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
666 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000667 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000668
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000670 }
671 }
672 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000673 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000674}
675
Dan Gohman6a109f92011-07-15 21:42:20 +0000676/// LegalizeOp - Return a legal replacement for the given operation, with
677/// all legal operands.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000678void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
679 if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
680 return;
Scott Michelfdc40a02009-02-17 22:15:04 +0000681
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000682 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000683
Eli Friedman1fde9c52009-05-24 02:46:31 +0000684 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000685 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
686 TargetLowering::TypeLegal &&
Eli Friedman1fde9c52009-05-24 02:46:31 +0000687 "Unexpected illegal type!");
688
689 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000690 assert((TLI.getTypeAction(*DAG.getContext(),
691 Node->getOperand(i).getValueType()) ==
692 TargetLowering::TypeLegal ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000693 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
694 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000695
Dan Gohman475871a2008-07-27 21:46:04 +0000696 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +0000697 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000698
Eli Friedman8c377c72009-05-27 01:25:56 +0000699 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000700 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000701 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000702 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000703 case ISD::INTRINSIC_W_CHAIN:
704 case ISD::INTRINSIC_WO_CHAIN:
705 case ISD::INTRINSIC_VOID:
706 case ISD::VAARG:
707 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000709 break;
710 case ISD::SINT_TO_FP:
711 case ISD::UINT_TO_FP:
712 case ISD::EXTRACT_VECTOR_ELT:
713 Action = TLI.getOperationAction(Node->getOpcode(),
714 Node->getOperand(0).getValueType());
715 break;
716 case ISD::FP_ROUND_INREG:
717 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000718 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000719 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
720 break;
721 }
Eli Friedman327236c2011-08-24 20:50:09 +0000722 case ISD::ATOMIC_STORE: {
723 Action = TLI.getOperationAction(Node->getOpcode(),
724 Node->getOperand(2).getValueType());
725 break;
726 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000727 case ISD::SELECT_CC:
728 case ISD::SETCC:
729 case ISD::BR_CC: {
730 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
731 Node->getOpcode() == ISD::SETCC ? 2 : 1;
732 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000733 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000734 ISD::CondCode CCCode =
735 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
736 Action = TLI.getCondCodeAction(CCCode, OpVT);
737 if (Action == TargetLowering::Legal) {
738 if (Node->getOpcode() == ISD::SELECT_CC)
739 Action = TLI.getOperationAction(Node->getOpcode(),
740 Node->getValueType(0));
741 else
742 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
743 }
744 break;
745 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000746 case ISD::LOAD:
747 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000748 // FIXME: Model these properly. LOAD and STORE are complicated, and
749 // STORE expects the unlegalized operand in some cases.
750 SimpleFinishLegalizing = false;
751 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000752 case ISD::CALLSEQ_START:
753 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000754 // FIXME: This shouldn't be necessary. These nodes have special properties
755 // dealing with the recursive nature of legalization. Removing this
756 // special case should be done as part of making LegalizeDAG non-recursive.
757 SimpleFinishLegalizing = false;
758 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000759 case ISD::EXTRACT_ELEMENT:
760 case ISD::FLT_ROUNDS_:
761 case ISD::SADDO:
762 case ISD::SSUBO:
763 case ISD::UADDO:
764 case ISD::USUBO:
765 case ISD::SMULO:
766 case ISD::UMULO:
767 case ISD::FPOWI:
768 case ISD::MERGE_VALUES:
769 case ISD::EH_RETURN:
770 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000771 case ISD::EH_SJLJ_SETJMP:
772 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000773 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000774 // These operations lie about being legal: when they claim to be legal,
775 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000776 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
777 if (Action == TargetLowering::Legal)
778 Action = TargetLowering::Expand;
779 break;
Duncan Sands4a544a72011-09-06 13:37:06 +0000780 case ISD::INIT_TRAMPOLINE:
781 case ISD::ADJUST_TRAMPOLINE:
Eli Friedman8c377c72009-05-27 01:25:56 +0000782 case ISD::FRAMEADDR:
783 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000784 // These operations lie about being legal: when they claim to be legal,
785 // they should actually be custom-lowered.
786 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
787 if (Action == TargetLowering::Legal)
788 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000789 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000790 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000791 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000792 Action = TargetLowering::Legal;
793 } else {
794 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000795 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000796 break;
797 }
798
799 if (SimpleFinishLegalizing) {
Dan Gohman2ba60e52011-10-28 01:29:32 +0000800 SmallVector<SDValue, 8> Ops;
Eli Friedman8c377c72009-05-27 01:25:56 +0000801 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman2ba60e52011-10-28 01:29:32 +0000802 Ops.push_back(Node->getOperand(i));
Eli Friedman8c377c72009-05-27 01:25:56 +0000803 switch (Node->getOpcode()) {
804 default: break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000805 case ISD::SHL:
806 case ISD::SRL:
807 case ISD::SRA:
808 case ISD::ROTL:
809 case ISD::ROTR:
810 // Legalizing shifts/rotates requires adjusting the shift amount
811 // to the appropriate width.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000812 if (!Ops[1].getValueType().isVector()) {
813 SDValue SAO = DAG.getShiftAmountOperand(Ops[0].getValueType(), Ops[1]);
814 HandleSDNode Handle(SAO);
815 LegalizeOp(SAO.getNode());
816 Ops[1] = Handle.getValue();
817 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000818 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000819 case ISD::SRL_PARTS:
820 case ISD::SRA_PARTS:
821 case ISD::SHL_PARTS:
822 // Legalizing shifts/rotates requires adjusting the shift amount
823 // to the appropriate width.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000824 if (!Ops[2].getValueType().isVector()) {
825 SDValue SAO = DAG.getShiftAmountOperand(Ops[0].getValueType(), Ops[2]);
826 HandleSDNode Handle(SAO);
827 LegalizeOp(SAO.getNode());
828 Ops[2] = Handle.getValue();
829 }
Dan Gohman2c9489d2009-08-18 23:52:48 +0000830 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000831 }
832
Dan Gohman2ba60e52011-10-28 01:29:32 +0000833 SDNode *NewNode = DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
834 if (NewNode != Node) {
835 DAG.ReplaceAllUsesWith(Node, NewNode, this);
836 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
837 DAG.TransferDbgValues(SDValue(Node, i), SDValue(NewNode, i));
838 DAG.RemoveDeadNode(Node, this);
839 Node = NewNode;
840 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000841 switch (Action) {
842 case TargetLowering::Legal:
Dan Gohman2ba60e52011-10-28 01:29:32 +0000843 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000844 case TargetLowering::Custom:
845 // FIXME: The handling for custom lowering with multiple results is
846 // a complete mess.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000847 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Eli Friedman8c377c72009-05-27 01:25:56 +0000848 if (Tmp1.getNode()) {
Dan Gohman2ba60e52011-10-28 01:29:32 +0000849 SmallVector<SDValue, 8> ResultVals;
Eli Friedman8c377c72009-05-27 01:25:56 +0000850 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
851 if (e == 1)
852 ResultVals.push_back(Tmp1);
853 else
854 ResultVals.push_back(Tmp1.getValue(i));
855 }
Dan Gohman2ba60e52011-10-28 01:29:32 +0000856 if (Tmp1.getNode() != Node || Tmp1.getResNo() != 0) {
857 DAG.ReplaceAllUsesWith(Node, ResultVals.data(), this);
858 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
859 DAG.TransferDbgValues(SDValue(Node, i), ResultVals[i]);
860 DAG.RemoveDeadNode(Node, this);
861 }
862 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000863 }
864
865 // FALL THROUGH
866 case TargetLowering::Expand:
Dan Gohman2ba60e52011-10-28 01:29:32 +0000867 ExpandNode(Node);
868 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000869 case TargetLowering::Promote:
Dan Gohman2ba60e52011-10-28 01:29:32 +0000870 PromoteNode(Node);
871 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000872 }
873 }
874
875 switch (Node->getOpcode()) {
876 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000877#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +0000878 dbgs() << "NODE: ";
879 Node->dump( &DAG);
880 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000881#endif
Chris Lattner35a38932010-04-07 23:47:51 +0000882 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000883
Dan Gohman2ba60e52011-10-28 01:29:32 +0000884 case ISD::CALLSEQ_START:
Chris Lattner16cd04d2005-05-12 23:24:06 +0000885 case ISD::CALLSEQ_END:
Dan Gohman2ba60e52011-10-28 01:29:32 +0000886 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000887 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +0000888 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ba60e52011-10-28 01:29:32 +0000889 Tmp1 = LD->getChain(); // Legalize the chain.
890 Tmp2 = LD->getBasePtr(); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000891
Evan Cheng466685d2006-10-09 20:57:25 +0000892 ISD::LoadExtType ExtType = LD->getExtensionType();
893 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +0000894 EVT VT = Node->getValueType(0);
Dan Gohman2ba60e52011-10-28 01:29:32 +0000895 Node = DAG.UpdateNodeOperands(Node, Tmp1, Tmp2, LD->getOffset());
896 Tmp3 = SDValue(Node, 0);
897 Tmp4 = SDValue(Node, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000898
Evan Cheng466685d2006-10-09 20:57:25 +0000899 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +0000900 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000901 case TargetLowering::Legal:
902 // If this is an unaligned load and the target doesn't support it,
903 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +0000904 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000905 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
Evan Chenge96507c2009-08-15 08:38:52 +0000906 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000907 if (LD->getAlignment() < ABIAlignment){
Dan Gohman2ba60e52011-10-28 01:29:32 +0000908 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
909 DAG, TLI, Tmp3, Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000910 }
911 }
912 break;
Evan Cheng466685d2006-10-09 20:57:25 +0000913 case TargetLowering::Custom:
914 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000915 if (Tmp1.getNode()) {
Dan Gohman2ba60e52011-10-28 01:29:32 +0000916 Tmp3 = Tmp1;
917 Tmp4 = Tmp1.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000918 }
Evan Cheng466685d2006-10-09 20:57:25 +0000919 break;
920 case TargetLowering::Promote: {
921 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000922 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +0000923 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +0000924 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000925
Chris Lattnerecf42c42010-09-21 16:36:31 +0000926 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000927 LD->isVolatile(), LD->isNonTemporal(),
928 LD->getAlignment());
Dan Gohman2ba60e52011-10-28 01:29:32 +0000929 Tmp3 = DAG.getNode(ISD::BITCAST, dl, VT, Tmp1);
930 Tmp4 = Tmp1.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000931 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000932 }
Evan Cheng466685d2006-10-09 20:57:25 +0000933 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000934 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +0000935 // legalized both of them.
Dan Gohman2ba60e52011-10-28 01:29:32 +0000936 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp3);
937 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp4);
938 return;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000939 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000940
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000941 EVT SrcVT = LD->getMemoryVT();
942 unsigned SrcWidth = SrcVT.getSizeInBits();
943 unsigned Alignment = LD->getAlignment();
944 bool isVolatile = LD->isVolatile();
945 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000946
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000947 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
948 // Some targets pretend to have an i1 loading operation, and actually
949 // load an i8. This trick is correct for ZEXTLOAD because the top 7
950 // bits are guaranteed to be zero; it helps the optimizers understand
951 // that these bits are zero. It is also useful for EXTLOAD, since it
952 // tells the optimizers that those bits are undefined. It would be
953 // nice to have an effective generic way of getting these benefits...
954 // Until such a way is found, don't insist on promoting i1 here.
955 (SrcVT != MVT::i1 ||
956 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
957 // Promote to a byte-sized load if not loading an integral number of
958 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
959 unsigned NewWidth = SrcVT.getStoreSizeInBits();
960 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
961 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000962
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000963 // The extra bits are guaranteed to be zero, since we stored them that
964 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000965
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000966 ISD::LoadExtType NewExtType =
967 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000968
Dan Gohman2ba60e52011-10-28 01:29:32 +0000969 SDValue Result =
970 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
971 Tmp1, Tmp2, LD->getPointerInfo(),
972 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000973
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000974 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000975
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000976 if (ExtType == ISD::SEXTLOAD)
977 // Having the top bits zero doesn't help when sign extending.
978 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
979 Result.getValueType(),
980 Result, DAG.getValueType(SrcVT));
981 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
982 // All the top bits are guaranteed to be zero - inform the optimizers.
983 Result = DAG.getNode(ISD::AssertZext, dl,
984 Result.getValueType(), Result,
985 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000986
Dan Gohman2ba60e52011-10-28 01:29:32 +0000987 Tmp1 = Result;
988 Tmp2 = Ch;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000989 } else if (SrcWidth & (SrcWidth - 1)) {
990 // If not loading a power-of-2 number of bits, expand as two loads.
991 assert(!SrcVT.isVector() && "Unsupported extload!");
992 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
993 assert(RoundWidth < SrcWidth);
994 unsigned ExtraWidth = SrcWidth - RoundWidth;
995 assert(ExtraWidth < RoundWidth);
996 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
997 "Load size not an integral number of bytes!");
998 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
999 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1000 SDValue Lo, Hi, Ch;
1001 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001002
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001003 if (TLI.isLittleEndian()) {
1004 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1005 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001006 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001007 Tmp1, Tmp2,
1008 LD->getPointerInfo(), RoundVT, isVolatile,
1009 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001010
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001011 // Load the remaining ExtraWidth bits.
1012 IncrementSize = RoundWidth / 8;
1013 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1014 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001015 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001016 LD->getPointerInfo().getWithOffset(IncrementSize),
1017 ExtraVT, isVolatile, isNonTemporal,
1018 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001019
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001020 // Build a factor node to remember that this load is independent of
1021 // the other one.
1022 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1023 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001024
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001025 // Move the top bits to the right place.
1026 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001027 DAG.getConstant(RoundWidth,
1028 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001029
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001030 // Join the hi and lo parts.
Dan Gohman2ba60e52011-10-28 01:29:32 +00001031 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001032 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001033 // Big endian - avoid unaligned loads.
1034 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1035 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001036 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001037 LD->getPointerInfo(), RoundVT, isVolatile,
1038 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001039
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001040 // Load the remaining ExtraWidth bits.
1041 IncrementSize = RoundWidth / 8;
1042 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1043 DAG.getIntPtrConstant(IncrementSize));
1044 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001045 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001046 LD->getPointerInfo().getWithOffset(IncrementSize),
1047 ExtraVT, isVolatile, isNonTemporal,
1048 MinAlign(Alignment, IncrementSize));
1049
1050 // Build a factor node to remember that this load is independent of
1051 // the other one.
1052 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1053 Hi.getValue(1));
1054
1055 // Move the top bits to the right place.
1056 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001057 DAG.getConstant(ExtraWidth,
1058 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001059
1060 // Join the hi and lo parts.
Dan Gohman2ba60e52011-10-28 01:29:32 +00001061 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001062 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001063
Dan Gohman2ba60e52011-10-28 01:29:32 +00001064 Tmp2 = Ch;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001065 } else {
1066 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1067 default: assert(0 && "This action is not supported yet!");
1068 case TargetLowering::Custom:
1069 isCustom = true;
1070 // FALLTHROUGH
1071 case TargetLowering::Legal:
Dan Gohman2ba60e52011-10-28 01:29:32 +00001072 Node = DAG.UpdateNodeOperands(Node,
1073 Tmp1, Tmp2, LD->getOffset());
1074 Tmp1 = SDValue(Node, 0);
1075 Tmp2 = SDValue(Node, 1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001076
1077 if (isCustom) {
Dan Gohman2ba60e52011-10-28 01:29:32 +00001078 Tmp3 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001079 if (Tmp3.getNode()) {
Dan Gohman2ba60e52011-10-28 01:29:32 +00001080 Tmp1 = Tmp3;
1081 Tmp2 = Tmp3.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001082 }
1083 } else {
1084 // If this is an unaligned load and the target doesn't support it,
1085 // expand it.
1086 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001087 Type *Ty =
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001088 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1089 unsigned ABIAlignment =
1090 TLI.getTargetData()->getABITypeAlignment(Ty);
1091 if (LD->getAlignment() < ABIAlignment){
Dan Gohman2ba60e52011-10-28 01:29:32 +00001092 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
1093 DAG, TLI, Tmp1, Tmp2);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001094 }
1095 }
1096 }
1097 break;
1098 case TargetLowering::Expand:
Dan Gohman75b10042011-07-15 22:39:09 +00001099 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001100 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1101 LD->getPointerInfo(),
1102 LD->isVolatile(), LD->isNonTemporal(),
1103 LD->getAlignment());
1104 unsigned ExtendOp;
1105 switch (ExtType) {
1106 case ISD::EXTLOAD:
1107 ExtendOp = (SrcVT.isFloatingPoint() ?
1108 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1109 break;
1110 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1111 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1112 default: llvm_unreachable("Unexpected extend load type!");
1113 }
Dan Gohman2ba60e52011-10-28 01:29:32 +00001114 Tmp1 = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1115 Tmp2 = Load.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001116 break;
1117 }
Nadav Rotemc2492c22011-06-14 08:11:52 +00001118
Nadav Roteme9b58d02011-10-15 07:41:10 +00001119 assert(!SrcVT.isVector() &&
1120 "Vector Loads are handled in LegalizeVectorOps");
Nadav Rotemc2492c22011-06-14 08:11:52 +00001121
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001122 // FIXME: This does not work for vectors on most targets. Sign- and
1123 // zero-extend operations are currently folded into extending loads,
1124 // whether they are legal or not, and then we end up here without any
1125 // support for legalizing them.
1126 assert(ExtType != ISD::EXTLOAD &&
1127 "EXTLOAD should always be supported!");
1128 // Turn the unsupported load into an EXTLOAD followed by an explicit
1129 // zero/sign extend inreg.
Dan Gohman2ba60e52011-10-28 01:29:32 +00001130 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
1131 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1132 LD->isVolatile(), LD->isNonTemporal(),
1133 LD->getAlignment());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001134 SDValue ValRes;
1135 if (ExtType == ISD::SEXTLOAD)
1136 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1137 Result.getValueType(),
1138 Result, DAG.getValueType(SrcVT));
1139 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001140 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Dan Gohman2ba60e52011-10-28 01:29:32 +00001141 Tmp1 = ValRes;
1142 Tmp2 = Result.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001143 break;
1144 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001145 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001146
1147 // Since loads produce two values, make sure to remember that we legalized
1148 // both of them.
Dan Gohman2ba60e52011-10-28 01:29:32 +00001149 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp1);
1150 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp2);
1151 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001152 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001153 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001154 StoreSDNode *ST = cast<StoreSDNode>(Node);
Dan Gohman2ba60e52011-10-28 01:29:32 +00001155 Tmp1 = ST->getChain();
1156 Tmp2 = ST->getBasePtr();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001157 unsigned Alignment = ST->getAlignment();
1158 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001159 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001160
Evan Cheng8b2794a2006-10-13 21:14:26 +00001161 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001162 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
Dan Gohman2ba60e52011-10-28 01:29:32 +00001163 DAG.ReplaceAllUsesWith(ST, OptStore, this);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001164 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001165 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001166
Eli Friedman957bffa2009-05-24 08:42:01 +00001167 {
Dan Gohman2ba60e52011-10-28 01:29:32 +00001168 Tmp3 = ST->getValue();
1169 Node = DAG.UpdateNodeOperands(Node,
1170 Tmp1, Tmp3, Tmp2,
1171 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001172
Owen Andersone50ed302009-08-10 22:56:29 +00001173 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001174 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001175 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001176 case TargetLowering::Legal:
1177 // If this is an unaligned store and the target doesn't support it,
1178 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001179 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001180 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001181 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001182 if (ST->getAlignment() < ABIAlignment)
Dan Gohman2ba60e52011-10-28 01:29:32 +00001183 ExpandUnalignedStore(cast<StoreSDNode>(Node),
1184 DAG, TLI, this);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001185 }
1186 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001187 case TargetLowering::Custom:
Dan Gohman2ba60e52011-10-28 01:29:32 +00001188 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
1189 if (Tmp1.getNode())
1190 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Tmp1, this);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001191 break;
Dan Gohman2ba60e52011-10-28 01:29:32 +00001192 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001193 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001194 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001195 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dan Gohman2ba60e52011-10-28 01:29:32 +00001196 SDValue Result =
1197 DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
1198 ST->getPointerInfo(), isVolatile,
1199 isNonTemporal, Alignment);
1200 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001201 break;
1202 }
Dan Gohman2ba60e52011-10-28 01:29:32 +00001203 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001204 break;
1205 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001206 } else {
Dan Gohman2ba60e52011-10-28 01:29:32 +00001207 Tmp3 = ST->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001208
Owen Andersone50ed302009-08-10 22:56:29 +00001209 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001210 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001211
Duncan Sands83ec4b62008-06-06 12:08:01 +00001212 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001213 // Promote to a byte-sized store with upper bits zero if not
1214 // storing an integral number of bytes. For example, promote
1215 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001216 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1217 StVT.getStoreSizeInBits());
Nadav Rotema1c415c2011-09-27 10:48:29 +00001218 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Dan Gohman2ba60e52011-10-28 01:29:32 +00001219 SDValue Result =
1220 DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1221 NVT, isVolatile, isNonTemporal, Alignment);
1222 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001223 } else if (StWidth & (StWidth - 1)) {
1224 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001225 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001226 unsigned RoundWidth = 1 << Log2_32(StWidth);
1227 assert(RoundWidth < StWidth);
1228 unsigned ExtraWidth = StWidth - RoundWidth;
1229 assert(ExtraWidth < RoundWidth);
1230 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1231 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001232 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1233 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001234 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001235 unsigned IncrementSize;
1236
1237 if (TLI.isLittleEndian()) {
1238 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1239 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001240 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1241 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001242 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001243
1244 // Store the remaining ExtraWidth bits.
1245 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001246 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001247 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001248 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001249 DAG.getConstant(RoundWidth,
1250 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001251 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1252 ST->getPointerInfo().getWithOffset(IncrementSize),
1253 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001254 MinAlign(Alignment, IncrementSize));
1255 } else {
1256 // Big endian - avoid unaligned stores.
1257 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1258 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001259 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001260 DAG.getConstant(ExtraWidth,
1261 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001262 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1263 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001264
1265 // Store the remaining ExtraWidth bits.
1266 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001267 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001268 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001269 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1270 ST->getPointerInfo().getWithOffset(IncrementSize),
1271 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001272 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001273 }
Duncan Sands7e857202008-01-22 07:17:34 +00001274
1275 // The order of the stores doesn't matter.
Dan Gohman2ba60e52011-10-28 01:29:32 +00001276 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
1277 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001278 } else {
1279 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1280 Tmp2 != ST->getBasePtr())
Dan Gohman2ba60e52011-10-28 01:29:32 +00001281 Node = DAG.UpdateNodeOperands(Node, Tmp1, Tmp3, Tmp2,
1282 ST->getOffset());
Duncan Sands7e857202008-01-22 07:17:34 +00001283
1284 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001285 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001286 case TargetLowering::Legal:
1287 // If this is an unaligned store and the target doesn't support it,
1288 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001289 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001290 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001291 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001292 if (ST->getAlignment() < ABIAlignment)
Dan Gohman2ba60e52011-10-28 01:29:32 +00001293 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001294 }
1295 break;
1296 case TargetLowering::Custom:
Dan Gohman2ba60e52011-10-28 01:29:32 +00001297 DAG.ReplaceAllUsesWith(SDValue(Node, 0),
1298 TLI.LowerOperation(SDValue(Node, 0), DAG),
1299 this);
Duncan Sands7e857202008-01-22 07:17:34 +00001300 break;
Dan Gohmane63e5ab2011-07-15 22:51:43 +00001301 case TargetLowering::Expand:
Nadav Roteme9b58d02011-10-15 07:41:10 +00001302 assert(!StVT.isVector() &&
1303 "Vector Stores are handled in LegalizeVectorOps");
Nadav Rotemc2492c22011-06-14 08:11:52 +00001304
Duncan Sands7e857202008-01-22 07:17:34 +00001305 // TRUNCSTORE:i16 i32 -> STORE i16
Dan Gohman75b10042011-07-15 22:39:09 +00001306 assert(TLI.isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001307 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Dan Gohman2ba60e52011-10-28 01:29:32 +00001308 SDValue Result =
1309 DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1310 isVolatile, isNonTemporal, Alignment);
1311 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001312 break;
1313 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001314 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001315 }
1316 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001317 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001318 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001319}
1320
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001321SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1322 SDValue Vec = Op.getOperand(0);
1323 SDValue Idx = Op.getOperand(1);
1324 DebugLoc dl = Op.getDebugLoc();
1325 // Store the value to a temporary stack slot, then LOAD the returned part.
1326 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001327 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1328 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001329
1330 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001331 unsigned EltSize =
1332 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001333 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1334 DAG.getConstant(EltSize, Idx.getValueType()));
1335
1336 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1337 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1338 else
1339 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1340
1341 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1342
Eli Friedmanc680ac92009-07-09 22:01:03 +00001343 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001344 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001345 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001346 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001347 MachinePointerInfo(),
1348 Vec.getValueType().getVectorElementType(),
1349 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001350}
1351
David Greenecfe33c42011-01-26 19:13:22 +00001352SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1353 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1354
1355 SDValue Vec = Op.getOperand(0);
1356 SDValue Part = Op.getOperand(1);
1357 SDValue Idx = Op.getOperand(2);
1358 DebugLoc dl = Op.getDebugLoc();
1359
1360 // Store the value to a temporary stack slot, then LOAD the returned part.
1361
1362 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1363 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1364 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1365
1366 // First store the whole vector.
1367 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1368 false, false, 0);
1369
1370 // Then store the inserted part.
1371
1372 // Add the offset to the index.
1373 unsigned EltSize =
1374 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1375
1376 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1377 DAG.getConstant(EltSize, Idx.getValueType()));
1378
1379 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1380 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1381 else
1382 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1383
1384 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1385 StackPtr);
1386
1387 // Store the subvector.
1388 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1389 MachinePointerInfo(), false, false, 0);
1390
1391 // Finally, load the updated vector.
1392 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1393 false, false, 0);
1394}
1395
Eli Friedman7ef3d172009-06-06 07:04:42 +00001396SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1397 // We can't handle this case efficiently. Allocate a sufficiently
1398 // aligned object on the stack, store each element into it, then load
1399 // the result as a vector.
1400 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001401 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001402 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001403 DebugLoc dl = Node->getDebugLoc();
1404 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001405 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001406 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001407
1408 // Emit a store of each element to the stack slot.
1409 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001410 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001411 // Store (in the right endianness) the elements to memory.
1412 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1413 // Ignore undef elements.
1414 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1415
1416 unsigned Offset = TypeByteSize*i;
1417
1418 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1419 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1420
Dan Gohman9949dd62010-02-25 20:30:49 +00001421 // If the destination vector element type is narrower than the source
1422 // element type, only store the bits necessary.
1423 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001424 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001425 Node->getOperand(i), Idx,
1426 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001427 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001428 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001429 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001430 Node->getOperand(i), Idx,
1431 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001432 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001433 }
1434
1435 SDValue StoreChain;
1436 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001437 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001438 &Stores[0], Stores.size());
1439 else
1440 StoreChain = DAG.getEntryNode();
1441
1442 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001443 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001444}
1445
Eli Friedman4bc8c712009-05-27 12:20:41 +00001446SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1447 DebugLoc dl = Node->getDebugLoc();
1448 SDValue Tmp1 = Node->getOperand(0);
1449 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001450
1451 // Get the sign bit of the RHS. First obtain a value that has the same
1452 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001453 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001454 EVT FloatVT = Tmp2.getValueType();
1455 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Dan Gohman75b10042011-07-15 22:39:09 +00001456 if (TLI.isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001457 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001458 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001459 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001460 // Store the float to memory, then load the sign part out as an integer.
1461 MVT LoadTy = TLI.getPointerTy();
1462 // First create a temporary that is aligned for both the load and store.
1463 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1464 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001465 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001466 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001467 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001468 if (TLI.isBigEndian()) {
1469 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1470 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001471 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1472 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001473 } else { // Little endian
1474 SDValue LoadPtr = StackPtr;
1475 // The float may be wider than the integer we are going to load. Advance
1476 // the pointer so that the loaded integer will contain the sign bit.
1477 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1478 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1479 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1480 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1481 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001482 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1483 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001484 // Move the sign bit to the top bit of the loaded integer.
1485 unsigned BitShift = LoadTy.getSizeInBits() -
1486 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1487 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1488 if (BitShift)
1489 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001490 DAG.getConstant(BitShift,
1491 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001492 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001493 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001494 // Now get the sign bit proper, by seeing whether the value is negative.
1495 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1496 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1497 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001498 // Get the absolute value of the result.
1499 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1500 // Select between the nabs and abs value based on the sign bit of
1501 // the input.
1502 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1503 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1504 AbsVal);
1505}
1506
Eli Friedman4bc8c712009-05-27 12:20:41 +00001507void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1508 SmallVectorImpl<SDValue> &Results) {
1509 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1510 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1511 " not tell us which reg is the stack pointer!");
1512 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001513 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001514 SDValue Tmp1 = SDValue(Node, 0);
1515 SDValue Tmp2 = SDValue(Node, 1);
1516 SDValue Tmp3 = Node->getOperand(2);
1517 SDValue Chain = Tmp1.getOperand(0);
1518
1519 // Chain the dynamic stack allocation so that it doesn't modify the stack
1520 // pointer when other instructions are using the stack.
1521 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1522
1523 SDValue Size = Tmp2.getOperand(1);
1524 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1525 Chain = SP.getValue(1);
1526 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001527 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001528 if (Align > StackAlign)
1529 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1530 DAG.getConstant(-(uint64_t)Align, VT));
1531 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1532 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1533
1534 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1535 DAG.getIntPtrConstant(0, true), SDValue());
1536
1537 Results.push_back(Tmp1);
1538 Results.push_back(Tmp2);
1539}
1540
Evan Cheng7f042682008-10-15 02:05:31 +00001541/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001542/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001543/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001544void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001545 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001546 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001547 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001548 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001549 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1550 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001551 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001552 case TargetLowering::Legal:
1553 // Nothing to do.
1554 break;
1555 case TargetLowering::Expand: {
1556 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1557 unsigned Opc = 0;
1558 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001559 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001560 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1561 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1562 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1563 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1564 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1565 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1566 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1567 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1568 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1569 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1570 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1571 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001572 // FIXME: Implement more expansions.
1573 }
1574
Dale Johannesenbb5da912009-02-02 20:41:04 +00001575 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1576 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1577 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001578 RHS = SDValue();
1579 CC = SDValue();
1580 break;
1581 }
1582 }
1583}
1584
Chris Lattner1401d152008-01-16 07:45:30 +00001585/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1586/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1587/// a load from the stack slot to DestVT, extending it if needed.
1588/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001589SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001590 EVT SlotVT,
1591 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001592 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001593 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001594 unsigned SrcAlign =
1595 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001596 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001597 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001598
Evan Chengff89dcb2009-10-18 18:16:27 +00001599 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1600 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001601 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001602
Duncan Sands83ec4b62008-06-06 12:08:01 +00001603 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1604 unsigned SlotSize = SlotVT.getSizeInBits();
1605 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001606 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Evan Chengadf97992010-04-15 01:25:27 +00001607 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001608
Chris Lattner1401d152008-01-16 07:45:30 +00001609 // Emit a store to the stack slot. Use a truncstore if the input value is
1610 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001611 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001612
Chris Lattner1401d152008-01-16 07:45:30 +00001613 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001614 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001615 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001616 else {
1617 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001618 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001619 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001620 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001621
Chris Lattner35481892005-12-23 00:16:34 +00001622 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001623 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001624 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001625 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001626
Chris Lattner1401d152008-01-16 07:45:30 +00001627 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00001628 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001629 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001630}
1631
Dan Gohman475871a2008-07-27 21:46:04 +00001632SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001633 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001634 // Create a vector sized/aligned stack slot, store the value to element #0,
1635 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001636 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001637
Evan Chengff89dcb2009-10-18 18:16:27 +00001638 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1639 int SPFI = StackPtrFI->getIndex();
1640
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001641 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1642 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001643 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001644 Node->getValueType(0).getVectorElementType(),
1645 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001646 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001647 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001648 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001649}
1650
1651
Chris Lattnerce872152006-03-19 06:31:19 +00001652/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001653/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001654SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001655 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001656 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001657 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001658 EVT VT = Node->getValueType(0);
1659 EVT OpVT = Node->getOperand(0).getValueType();
1660 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001661
1662 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001663 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001664 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001665 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001666 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001667 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001668 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001669 if (V.getOpcode() == ISD::UNDEF)
1670 continue;
1671 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001672 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001673 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001674 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001675
1676 if (!Value1.getNode()) {
1677 Value1 = V;
1678 } else if (!Value2.getNode()) {
1679 if (V != Value1)
1680 Value2 = V;
1681 } else if (V != Value1 && V != Value2) {
1682 MoreThanTwoValues = true;
1683 }
Chris Lattnerce872152006-03-19 06:31:19 +00001684 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001685
Eli Friedman7a5e5552009-06-07 06:52:44 +00001686 if (!Value1.getNode())
1687 return DAG.getUNDEF(VT);
1688
1689 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001690 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001691
Chris Lattner2eb86532006-03-24 07:29:17 +00001692 // If all elements are constants, create a load from the constant pool.
1693 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001694 std::vector<Constant*> CV;
1695 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001696 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001697 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001698 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001699 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001700 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001701 if (OpVT==EltVT)
1702 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1703 else {
1704 // If OpVT and EltVT don't match, EltVT is not legal and the
1705 // element values have been promoted/truncated earlier. Undo this;
1706 // we don't want a v16i8 to become a v16i32 for example.
1707 const ConstantInt *CI = V->getConstantIntValue();
1708 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1709 CI->getZExtValue()));
1710 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001711 } else {
1712 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001713 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001714 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001715 }
1716 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001717 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001718 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001719 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001720 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001721 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001722 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001723 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001724
Eli Friedman7a5e5552009-06-07 06:52:44 +00001725 if (!MoreThanTwoValues) {
1726 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1727 for (unsigned i = 0; i < NumElems; ++i) {
1728 SDValue V = Node->getOperand(i);
1729 if (V.getOpcode() == ISD::UNDEF)
1730 continue;
1731 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1732 }
1733 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001734 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001735 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1736 SDValue Vec2;
1737 if (Value2.getNode())
1738 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1739 else
1740 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001741
Chris Lattner87100e02006-03-20 01:52:29 +00001742 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001743 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001744 }
1745 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001746
Eli Friedman7ef3d172009-06-06 07:04:42 +00001747 // Otherwise, we can't handle this case efficiently.
1748 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001749}
1750
Chris Lattner77e77a62005-01-21 06:05:23 +00001751// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1752// does not fit into a register, return the lo part and set the hi part to the
1753// by-reg argument. If it does fit into a single register, return the result
1754// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001755SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001756 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001757 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00001758 // Legalizing the call will automatically add the previous call to the
1759 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00001760 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001761
Chris Lattner77e77a62005-01-21 06:05:23 +00001762 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00001763 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00001764 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001765 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001766 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00001767 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001768 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00001769 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00001770 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00001771 }
Bill Wendling056292f2008-09-16 21:48:12 +00001772 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00001773 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001774
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001775 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00001776
1777 // isTailCall may be true since the callee does not reference caller stack
1778 // frame. Check if it's in the right position.
1779 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001780 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00001781 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001782 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001783 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001784 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00001785
Evan Cheng3d2125c2010-11-30 23:55:39 +00001786 if (!CallInfo.second.getNode())
1787 // It's a tailcall, return the chain (which is the DAG root).
1788 return DAG.getRoot();
1789
Eli Friedman74807f22009-05-26 08:55:52 +00001790 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00001791}
1792
Dan Gohmanf316eb72011-05-16 22:09:53 +00001793/// ExpandLibCall - Generate a libcall taking the given operands as arguments
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001794/// and returning a result of type RetVT.
1795SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
1796 const SDValue *Ops, unsigned NumOps,
1797 bool isSigned, DebugLoc dl) {
1798 TargetLowering::ArgListTy Args;
1799 Args.reserve(NumOps);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001800
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001801 TargetLowering::ArgListEntry Entry;
1802 for (unsigned i = 0; i != NumOps; ++i) {
1803 Entry.Node = Ops[i];
1804 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
1805 Entry.isSExt = isSigned;
1806 Entry.isZExt = !isSigned;
1807 Args.push_back(Entry);
1808 }
1809 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1810 TLI.getPointerTy());
Dan Gohmanf316eb72011-05-16 22:09:53 +00001811
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001812 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001813 std::pair<SDValue,SDValue> CallInfo =
1814 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
1815 false, 0, TLI.getLibcallCallingConv(LC), false,
1816 /*isReturnValueUsed=*/true,
1817 Callee, Args, DAG, dl);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001818
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001819 return CallInfo.first;
1820}
1821
Jim Grosbache03262f2010-06-18 21:43:38 +00001822// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
1823// ExpandLibCall except that the first operand is the in-chain.
1824std::pair<SDValue, SDValue>
1825SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
1826 SDNode *Node,
1827 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00001828 SDValue InChain = Node->getOperand(0);
1829
1830 TargetLowering::ArgListTy Args;
1831 TargetLowering::ArgListEntry Entry;
1832 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
1833 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001834 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001835 Entry.Node = Node->getOperand(i);
1836 Entry.Ty = ArgTy;
1837 Entry.isSExt = isSigned;
1838 Entry.isZExt = !isSigned;
1839 Args.push_back(Entry);
1840 }
1841 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1842 TLI.getPointerTy());
1843
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001844 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001845 std::pair<SDValue, SDValue> CallInfo =
1846 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001847 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00001848 /*isReturnValueUsed=*/true,
1849 Callee, Args, DAG, Node->getDebugLoc());
1850
Jim Grosbache03262f2010-06-18 21:43:38 +00001851 return CallInfo;
1852}
1853
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001854SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
1855 RTLIB::Libcall Call_F32,
1856 RTLIB::Libcall Call_F64,
1857 RTLIB::Libcall Call_F80,
1858 RTLIB::Libcall Call_PPCF128) {
1859 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00001861 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001862 case MVT::f32: LC = Call_F32; break;
1863 case MVT::f64: LC = Call_F64; break;
1864 case MVT::f80: LC = Call_F80; break;
1865 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001866 }
1867 return ExpandLibCall(LC, Node, false);
1868}
1869
1870SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001871 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001872 RTLIB::Libcall Call_I16,
1873 RTLIB::Libcall Call_I32,
1874 RTLIB::Libcall Call_I64,
1875 RTLIB::Libcall Call_I128) {
1876 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00001878 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001879 case MVT::i8: LC = Call_I8; break;
1880 case MVT::i16: LC = Call_I16; break;
1881 case MVT::i32: LC = Call_I32; break;
1882 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001884 }
1885 return ExpandLibCall(LC, Node, isSigned);
1886}
1887
Evan Cheng65279cb2011-04-16 03:08:26 +00001888/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
1889static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
1890 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001891 RTLIB::Libcall LC;
1892 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1893 default: assert(0 && "Unexpected request for libcall!");
1894 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1895 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1896 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1897 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1898 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
1899 }
1900
Evan Cheng65279cb2011-04-16 03:08:26 +00001901 return TLI.getLibcallName(LC) != 0;
1902}
Evan Cheng8e23e812011-04-01 00:42:02 +00001903
Evan Cheng65279cb2011-04-16 03:08:26 +00001904/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
1905/// needed.
1906static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001907 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00001908 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00001909 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001910 else
Evan Cheng8e23e812011-04-01 00:42:02 +00001911 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001912
Evan Cheng8e23e812011-04-01 00:42:02 +00001913 SDValue Op0 = Node->getOperand(0);
1914 SDValue Op1 = Node->getOperand(1);
1915 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
1916 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
1917 SDNode *User = *UI;
1918 if (User == Node)
1919 continue;
1920 if (User->getOpcode() == OtherOpcode &&
1921 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00001922 User->getOperand(1) == Op1)
1923 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00001924 }
Evan Cheng65279cb2011-04-16 03:08:26 +00001925 return false;
1926}
Evan Cheng8e23e812011-04-01 00:42:02 +00001927
Evan Cheng65279cb2011-04-16 03:08:26 +00001928/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
1929/// pairs.
1930void
1931SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
1932 SmallVectorImpl<SDValue> &Results) {
1933 unsigned Opcode = Node->getOpcode();
1934 bool isSigned = Opcode == ISD::SDIVREM;
1935
1936 RTLIB::Libcall LC;
1937 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1938 default: assert(0 && "Unexpected request for libcall!");
1939 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1940 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1941 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1942 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1943 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00001944 }
1945
1946 // The input chain to this libcall is the entry node of the function.
1947 // Legalizing the call will automatically add the previous call to the
1948 // dependence.
1949 SDValue InChain = DAG.getEntryNode();
1950
1951 EVT RetVT = Node->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001952 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001953
1954 TargetLowering::ArgListTy Args;
1955 TargetLowering::ArgListEntry Entry;
1956 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1957 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001958 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001959 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
1960 Entry.isSExt = isSigned;
1961 Entry.isZExt = !isSigned;
1962 Args.push_back(Entry);
1963 }
1964
1965 // Also pass the return address of the remainder.
1966 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
1967 Entry.Node = FIPtr;
1968 Entry.Ty = RetTy->getPointerTo();
1969 Entry.isSExt = isSigned;
1970 Entry.isZExt = !isSigned;
1971 Args.push_back(Entry);
1972
1973 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1974 TLI.getPointerTy());
1975
Evan Cheng8e23e812011-04-01 00:42:02 +00001976 DebugLoc dl = Node->getDebugLoc();
1977 std::pair<SDValue, SDValue> CallInfo =
1978 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
1979 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
1980 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
1981
Evan Cheng8e23e812011-04-01 00:42:02 +00001982 // Remainder is loaded back from the stack frame.
Dan Gohman2ba60e52011-10-28 01:29:32 +00001983 SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
Evan Cheng8e23e812011-04-01 00:42:02 +00001984 MachinePointerInfo(), false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00001985 Results.push_back(CallInfo.first);
1986 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00001987}
1988
Chris Lattner22cde6a2006-01-28 08:25:58 +00001989/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
1990/// INT_TO_FP operation of the specified operand when the target requests that
1991/// we expand it. At this point, we know that the result and operand types are
1992/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00001993SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
1994 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00001995 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00001996 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00001998 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00001999
Chris Lattner23594d42008-01-16 07:03:22 +00002000 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002002
Chris Lattner22cde6a2006-01-28 08:25:58 +00002003 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002004 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002005 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002006 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002007 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002008 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002009 if (TLI.isLittleEndian())
2010 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002011
Chris Lattner22cde6a2006-01-28 08:25:58 +00002012 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002013 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002014 if (isSigned) {
2015 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002016 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2017 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002018 } else {
2019 Op0Mapped = Op0;
2020 }
2021 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002022 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002023 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002024 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002025 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002027 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002028 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2029 MachinePointerInfo(),
2030 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002031 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002032 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2033 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002034 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002036 BitsToDouble(0x4330000080000000ULL) :
2037 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002039 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002041 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002042 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002043 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002045 // do nothing
2046 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002048 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002049 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002051 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002052 }
2053 return Result;
2054 }
2055 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002056 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002057
2058 // Implementation of unsigned i64 to f64 following the algorithm in
2059 // __floatundidf in compiler_rt. This implementation has the advantage
2060 // of performing rounding correctly, both in the default rounding mode
2061 // and in all alternate rounding modes.
2062 // TODO: Generalize this for use with other types.
2063 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2064 SDValue TwoP52 =
2065 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2066 SDValue TwoP84PlusTwoP52 =
2067 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2068 SDValue TwoP84 =
2069 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2070
2071 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2072 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2073 DAG.getConstant(32, MVT::i64));
2074 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2075 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002076 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2077 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002078 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2079 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002080 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2081 }
2082
Owen Anderson3a9e7692010-10-05 17:24:05 +00002083 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002084 // TODO: Generalize this for use with other types.
2085 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002086 // For unsigned conversions, convert them to signed conversions using the
2087 // algorithm from the x86_64 __floatundidf in compiler_rt.
2088 if (!isSigned) {
2089 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002090
Owen Anderson95771af2011-02-25 21:41:48 +00002091 SDValue ShiftConst =
2092 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002093 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2094 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2095 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2096 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002097
Owen Anderson3a9e7692010-10-05 17:24:05 +00002098 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2099 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002100
Owen Anderson3a9e7692010-10-05 17:24:05 +00002101 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002102 // select. We happen to get lucky and machinesink does the right
2103 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002104 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002105 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002106 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2107 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2108 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002109
Owen Anderson3a9e7692010-10-05 17:24:05 +00002110 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002111
Jim Grosbach6e992612010-07-02 17:41:59 +00002112 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002113 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2114 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2115 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002116 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002117 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2118 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2119 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2120 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2121 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2122 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002123 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002124 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002125 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002126
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002127 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2128 DAG.getConstant(32, SHVT));
2129 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2130 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2131 SDValue TwoP32 =
2132 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2133 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2134 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2135 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2136 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2137 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2138 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002139 }
2140
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002141 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002142
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002143 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2144 Op0, DAG.getConstant(0, Op0.getValueType()),
2145 ISD::SETLT);
2146 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2147 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2148 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002149
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002150 // If the sign bit of the integer is set, the large number will be treated
2151 // as a negative number. To counteract this, the dynamic code adds an
2152 // offset depending on the data type.
2153 uint64_t FF;
2154 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002155 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002156 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2157 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2158 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2159 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2160 }
2161 if (TLI.isLittleEndian()) FF <<= 32;
2162 Constant *FudgeFactor = ConstantInt::get(
2163 Type::getInt64Ty(*DAG.getContext()), FF);
2164
2165 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2166 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2167 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2168 Alignment = std::min(Alignment, 4u);
2169 SDValue FudgeInReg;
2170 if (DestVT == MVT::f32)
2171 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002172 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002173 false, false, Alignment);
2174 else {
Dan Gohman2ba60e52011-10-28 01:29:32 +00002175 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
2176 DAG.getEntryNode(), CPIdx,
2177 MachinePointerInfo::getConstantPool(),
2178 MVT::f32, false, false, Alignment);
2179 HandleSDNode Handle(Load);
2180 LegalizeOp(Load.getNode());
2181 FudgeInReg = Handle.getValue();
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002182 }
2183
2184 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002185}
2186
2187/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2188/// *INT_TO_FP operation of the specified operand when the target requests that
2189/// we promote it. At this point, we know that the result and operand types are
2190/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2191/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002192SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002193 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002194 bool isSigned,
2195 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002196 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002197 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002198
2199 unsigned OpToUse = 0;
2200
2201 // Scan for the appropriate larger type to use.
2202 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002203 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002204 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002205
2206 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002207 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2208 OpToUse = ISD::SINT_TO_FP;
2209 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002210 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002211 if (isSigned) continue;
2212
2213 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002214 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2215 OpToUse = ISD::UINT_TO_FP;
2216 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002217 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002218
2219 // Otherwise, try a larger type.
2220 }
2221
2222 // Okay, we found the operation and type to use. Zero extend our input to the
2223 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002224 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002225 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002226 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002227}
2228
2229/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2230/// FP_TO_*INT operation of the specified operand when the target requests that
2231/// we promote it. At this point, we know that the result and operand types are
2232/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2233/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002234SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002235 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002236 bool isSigned,
2237 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002238 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002239 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002240
2241 unsigned OpToUse = 0;
2242
2243 // Scan for the appropriate larger type to use.
2244 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002245 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002246 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002247
Eli Friedman3be2e512009-05-28 03:06:16 +00002248 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002249 OpToUse = ISD::FP_TO_SINT;
2250 break;
2251 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002252
Eli Friedman3be2e512009-05-28 03:06:16 +00002253 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002254 OpToUse = ISD::FP_TO_UINT;
2255 break;
2256 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002257
2258 // Otherwise, try a larger type.
2259 }
2260
Scott Michelfdc40a02009-02-17 22:15:04 +00002261
Chris Lattner27a6c732007-11-24 07:07:01 +00002262 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002263 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002264
Chris Lattner27a6c732007-11-24 07:07:01 +00002265 // Truncate the result of the extended FP_TO_*INT operation to the desired
2266 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002267 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002268}
2269
2270/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2271///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002272SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002273 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002274 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002275 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002276 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002277 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002278 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002279 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2280 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2281 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002282 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002283 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2284 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2285 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2286 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2287 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2288 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2289 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2290 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2291 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002293 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2294 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2295 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2296 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2297 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2298 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2299 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2300 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2301 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2302 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2303 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2304 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2305 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2306 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2307 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2308 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2309 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2310 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2311 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2312 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2313 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002314 }
2315}
2316
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002317/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002318// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002319static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2320 APInt Val = APInt(NumBits, ByteVal);
2321 unsigned Shift = 8;
2322 for (unsigned i = NumBits; i > 8; i >>= 1) {
2323 Val = (Val << Shift) | Val;
2324 Shift <<= 1;
2325 }
2326 return Val;
2327}
2328
Chris Lattner22cde6a2006-01-28 08:25:58 +00002329/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2330///
Scott Michelfdc40a02009-02-17 22:15:04 +00002331SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002332 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002333 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002334 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002335 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002336 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002337 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002338 unsigned Len = VT.getSizeInBits();
2339
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002340 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2341 "CTPOP not implemented for this type.");
2342
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002343 // This is the "best" algorithm from
2344 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2345
2346 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2347 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2348 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2349 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2350
2351 // v = v - ((v >> 1) & 0x55555555...)
2352 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2353 DAG.getNode(ISD::AND, dl, VT,
2354 DAG.getNode(ISD::SRL, dl, VT, Op,
2355 DAG.getConstant(1, ShVT)),
2356 Mask55));
2357 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2358 Op = DAG.getNode(ISD::ADD, dl, VT,
2359 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2360 DAG.getNode(ISD::AND, dl, VT,
2361 DAG.getNode(ISD::SRL, dl, VT, Op,
2362 DAG.getConstant(2, ShVT)),
2363 Mask33));
2364 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2365 Op = DAG.getNode(ISD::AND, dl, VT,
2366 DAG.getNode(ISD::ADD, dl, VT, Op,
2367 DAG.getNode(ISD::SRL, dl, VT, Op,
2368 DAG.getConstant(4, ShVT))),
2369 Mask0F);
2370 // v = (v * 0x01010101...) >> (Len - 8)
2371 Op = DAG.getNode(ISD::SRL, dl, VT,
2372 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2373 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002374
Chris Lattner22cde6a2006-01-28 08:25:58 +00002375 return Op;
2376 }
2377 case ISD::CTLZ: {
2378 // for now, we do this:
2379 // x = x | (x >> 1);
2380 // x = x | (x >> 2);
2381 // ...
2382 // x = x | (x >>16);
2383 // x = x | (x >>32); // for 64-bit input
2384 // return popcount(~x);
2385 //
2386 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002387 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002388 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002389 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002390 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002391 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002392 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002393 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002394 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002395 Op = DAG.getNOT(dl, Op, VT);
2396 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002397 }
2398 case ISD::CTTZ: {
2399 // for now, we use: { return popcount(~x & (x - 1)); }
2400 // unless the target has ctlz but not ctpop, in which case we use:
2401 // { return 32 - nlz(~x & (x-1)); }
2402 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002403 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002404 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2405 DAG.getNOT(dl, Op, VT),
2406 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002407 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002408 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002409 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2410 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002411 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002412 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002413 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2414 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002415 }
2416 }
2417}
Chris Lattnere34b3962005-01-19 04:19:40 +00002418
Jim Grosbache03262f2010-06-18 21:43:38 +00002419std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2420 unsigned Opc = Node->getOpcode();
2421 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2422 RTLIB::Libcall LC;
2423
2424 switch (Opc) {
2425 default:
2426 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2427 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002428 case ISD::ATOMIC_SWAP:
2429 switch (VT.SimpleTy) {
2430 default: llvm_unreachable("Unexpected value type for atomic!");
2431 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2432 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2433 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2434 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2435 }
2436 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002437 case ISD::ATOMIC_CMP_SWAP:
2438 switch (VT.SimpleTy) {
2439 default: llvm_unreachable("Unexpected value type for atomic!");
2440 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2441 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2442 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2443 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2444 }
2445 break;
2446 case ISD::ATOMIC_LOAD_ADD:
2447 switch (VT.SimpleTy) {
2448 default: llvm_unreachable("Unexpected value type for atomic!");
2449 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2450 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2451 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2452 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2453 }
2454 break;
2455 case ISD::ATOMIC_LOAD_SUB:
2456 switch (VT.SimpleTy) {
2457 default: llvm_unreachable("Unexpected value type for atomic!");
2458 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2459 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2460 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2461 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2462 }
2463 break;
2464 case ISD::ATOMIC_LOAD_AND:
2465 switch (VT.SimpleTy) {
2466 default: llvm_unreachable("Unexpected value type for atomic!");
2467 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2468 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2469 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2470 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2471 }
2472 break;
2473 case ISD::ATOMIC_LOAD_OR:
2474 switch (VT.SimpleTy) {
2475 default: llvm_unreachable("Unexpected value type for atomic!");
2476 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2477 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2478 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2479 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2480 }
2481 break;
2482 case ISD::ATOMIC_LOAD_XOR:
2483 switch (VT.SimpleTy) {
2484 default: llvm_unreachable("Unexpected value type for atomic!");
2485 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2486 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2487 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2488 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2489 }
2490 break;
2491 case ISD::ATOMIC_LOAD_NAND:
2492 switch (VT.SimpleTy) {
2493 default: llvm_unreachable("Unexpected value type for atomic!");
2494 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2495 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2496 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2497 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2498 }
2499 break;
2500 }
2501
2502 return ExpandChainLibCall(LC, Node, false);
2503}
2504
Dan Gohman2ba60e52011-10-28 01:29:32 +00002505void SelectionDAGLegalize::ExpandNode(SDNode *Node) {
2506 SmallVector<SDValue, 8> Results;
Eli Friedman8c377c72009-05-27 01:25:56 +00002507 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002508 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002509 switch (Node->getOpcode()) {
2510 case ISD::CTPOP:
2511 case ISD::CTLZ:
2512 case ISD::CTTZ:
2513 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2514 Results.push_back(Tmp1);
2515 break;
2516 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002517 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002518 break;
2519 case ISD::FRAMEADDR:
2520 case ISD::RETURNADDR:
2521 case ISD::FRAME_TO_ARGS_OFFSET:
2522 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2523 break;
2524 case ISD::FLT_ROUNDS_:
2525 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2526 break;
2527 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002528 case ISD::EH_LABEL:
2529 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002530 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002531 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002532 case ISD::EH_SJLJ_DISPATCHSETUP:
2533 // If the target didn't expand these, there's nothing to do, so just
2534 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002535 Results.push_back(Node->getOperand(0));
2536 break;
2537 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002538 // If the target didn't expand this, just return 'zero' and preserve the
2539 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002540 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002541 Results.push_back(Node->getOperand(0));
2542 break;
Eli Friedman14648462011-07-27 22:21:52 +00002543 case ISD::ATOMIC_FENCE:
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002544 case ISD::MEMBARRIER: {
2545 // If the target didn't lower this, lower it to '__sync_synchronize()' call
Eli Friedman14648462011-07-27 22:21:52 +00002546 // FIXME: handle "fence singlethread" more efficiently.
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002547 TargetLowering::ArgListTy Args;
2548 std::pair<SDValue, SDValue> CallResult =
2549 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002550 false, false, false, false, 0, CallingConv::C,
2551 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002552 /*isReturnValueUsed=*/true,
2553 DAG.getExternalSymbol("__sync_synchronize",
2554 TLI.getPointerTy()),
2555 Args, DAG, dl);
2556 Results.push_back(CallResult.second);
2557 break;
2558 }
Eli Friedman069e2ed2011-08-26 02:59:24 +00002559 case ISD::ATOMIC_LOAD: {
2560 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
Eli Friedman331120b2011-09-15 21:20:49 +00002561 SDValue Zero = DAG.getConstant(0, Node->getValueType(0));
Eli Friedman069e2ed2011-08-26 02:59:24 +00002562 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
2563 cast<AtomicSDNode>(Node)->getMemoryVT(),
2564 Node->getOperand(0),
2565 Node->getOperand(1), Zero, Zero,
2566 cast<AtomicSDNode>(Node)->getMemOperand(),
2567 cast<AtomicSDNode>(Node)->getOrdering(),
2568 cast<AtomicSDNode>(Node)->getSynchScope());
2569 Results.push_back(Swap.getValue(0));
2570 Results.push_back(Swap.getValue(1));
2571 break;
2572 }
2573 case ISD::ATOMIC_STORE: {
2574 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
2575 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
2576 cast<AtomicSDNode>(Node)->getMemoryVT(),
2577 Node->getOperand(0),
2578 Node->getOperand(1), Node->getOperand(2),
2579 cast<AtomicSDNode>(Node)->getMemOperand(),
2580 cast<AtomicSDNode>(Node)->getOrdering(),
2581 cast<AtomicSDNode>(Node)->getSynchScope());
2582 Results.push_back(Swap.getValue(1));
2583 break;
2584 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002585 // By default, atomic intrinsics are marked Legal and lowered. Targets
2586 // which don't support them directly, however, may want libcalls, in which
2587 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002588 case ISD::ATOMIC_SWAP:
2589 case ISD::ATOMIC_LOAD_ADD:
2590 case ISD::ATOMIC_LOAD_SUB:
2591 case ISD::ATOMIC_LOAD_AND:
2592 case ISD::ATOMIC_LOAD_OR:
2593 case ISD::ATOMIC_LOAD_XOR:
2594 case ISD::ATOMIC_LOAD_NAND:
2595 case ISD::ATOMIC_LOAD_MIN:
2596 case ISD::ATOMIC_LOAD_MAX:
2597 case ISD::ATOMIC_LOAD_UMIN:
2598 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002599 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002600 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2601 Results.push_back(Tmp.first);
2602 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002603 break;
Evan Chenga8457062010-06-18 22:01:37 +00002604 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002605 case ISD::DYNAMIC_STACKALLOC:
2606 ExpandDYNAMIC_STACKALLOC(Node, Results);
2607 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002608 case ISD::MERGE_VALUES:
2609 for (unsigned i = 0; i < Node->getNumValues(); i++)
2610 Results.push_back(Node->getOperand(i));
2611 break;
2612 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002613 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002614 if (VT.isInteger())
2615 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002616 else {
2617 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002618 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002619 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002620 break;
2621 }
2622 case ISD::TRAP: {
2623 // If this operation is not supported, lower it to 'abort()' call
2624 TargetLowering::ArgListTy Args;
2625 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002626 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002627 false, false, false, false, 0, CallingConv::C,
2628 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002629 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002630 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002631 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002632 Results.push_back(CallResult.second);
2633 break;
2634 }
2635 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002636 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002637 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2638 Node->getValueType(0), dl);
2639 Results.push_back(Tmp1);
2640 break;
2641 case ISD::FP_EXTEND:
2642 Tmp1 = EmitStackConvert(Node->getOperand(0),
2643 Node->getOperand(0).getValueType(),
2644 Node->getValueType(0), dl);
2645 Results.push_back(Tmp1);
2646 break;
2647 case ISD::SIGN_EXTEND_INREG: {
2648 // NOTE: we could fall back on load/store here too for targets without
2649 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002650 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002651 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00002652 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00002653 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002654 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002655 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2656 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002657 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002658 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2659 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002660 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2661 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002662 break;
2663 }
2664 case ISD::FP_ROUND_INREG: {
2665 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002666 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00002667
2668 // NOTE: there is a choice here between constantly creating new stack
2669 // slots and always reusing the same one. We currently always create
2670 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002671 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002672 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2673 Node->getValueType(0), dl);
2674 Results.push_back(Tmp1);
2675 break;
2676 }
2677 case ISD::SINT_TO_FP:
2678 case ISD::UINT_TO_FP:
2679 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2680 Node->getOperand(0), Node->getValueType(0), dl);
2681 Results.push_back(Tmp1);
2682 break;
2683 case ISD::FP_TO_UINT: {
2684 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002685 EVT VT = Node->getOperand(0).getValueType();
2686 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002687 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002688 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2689 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2690 Tmp1 = DAG.getConstantFP(apf, VT);
2691 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2692 Node->getOperand(0),
2693 Tmp1, ISD::SETLT);
2694 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002695 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2696 DAG.getNode(ISD::FSUB, dl, VT,
2697 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002698 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2699 DAG.getConstant(x, NVT));
2700 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2701 Results.push_back(Tmp1);
2702 break;
2703 }
Eli Friedman509150f2009-05-27 07:58:35 +00002704 case ISD::VAARG: {
2705 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002706 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002707 Tmp1 = Node->getOperand(0);
2708 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002709 unsigned Align = Node->getConstantOperandVal(3);
2710
Chris Lattnerecf42c42010-09-21 16:36:31 +00002711 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2712 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002713 SDValue VAList = VAListLoad;
2714
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002715 if (Align > TLI.getMinStackArgumentAlignment()) {
2716 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2717
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002718 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2719 DAG.getConstant(Align - 1,
2720 TLI.getPointerTy()));
2721
2722 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002723 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002724 TLI.getPointerTy()));
2725 }
2726
Eli Friedman509150f2009-05-27 07:58:35 +00002727 // Increment the pointer, VAList, to the next vaarg
2728 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2729 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002730 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002731 TLI.getPointerTy()));
2732 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002733 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2734 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002735 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002736 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002737 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002738 Results.push_back(Results[0].getValue(1));
2739 break;
2740 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002741 case ISD::VACOPY: {
2742 // This defaults to loading a pointer from the input and storing it to the
2743 // output, returning the chain.
2744 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2745 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2746 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002747 Node->getOperand(2), MachinePointerInfo(VS),
2748 false, false, 0);
2749 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2750 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002751 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002752 break;
2753 }
2754 case ISD::EXTRACT_VECTOR_ELT:
2755 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2756 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002757 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002758 Node->getOperand(0));
2759 else
2760 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2761 Results.push_back(Tmp1);
2762 break;
2763 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002764 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002765 break;
David Greenecfe33c42011-01-26 19:13:22 +00002766 case ISD::INSERT_SUBVECTOR:
2767 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2768 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002769 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002770 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002771 break;
2772 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002773 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002774 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002775 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002776 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002777 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2778 Node->getOperand(1),
2779 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002780 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002781 case ISD::VECTOR_SHUFFLE: {
2782 SmallVector<int, 8> Mask;
2783 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2784
Owen Andersone50ed302009-08-10 22:56:29 +00002785 EVT VT = Node->getValueType(0);
2786 EVT EltVT = VT.getVectorElementType();
Dan Gohman75b10042011-07-15 22:39:09 +00002787 if (!TLI.isTypeLegal(EltVT))
Bob Wilson14b21412010-05-19 18:48:32 +00002788 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002789 unsigned NumElems = VT.getVectorNumElements();
2790 SmallVector<SDValue, 8> Ops;
2791 for (unsigned i = 0; i != NumElems; ++i) {
2792 if (Mask[i] < 0) {
2793 Ops.push_back(DAG.getUNDEF(EltVT));
2794 continue;
2795 }
2796 unsigned Idx = Mask[i];
2797 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002798 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2799 Node->getOperand(0),
2800 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002801 else
Bill Wendling775db972009-12-23 00:28:23 +00002802 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2803 Node->getOperand(1),
2804 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002805 }
2806 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2807 Results.push_back(Tmp1);
2808 break;
2809 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002810 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002811 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002812 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2813 // 1 -> Hi
2814 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2815 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00002816 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00002817 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2818 } else {
2819 // 0 -> Lo
2820 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2821 Node->getOperand(0));
2822 }
2823 Results.push_back(Tmp1);
2824 break;
2825 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002826 case ISD::STACKSAVE:
2827 // Expand to CopyFromReg if the target set
2828 // StackPointerRegisterToSaveRestore.
2829 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002830 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2831 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002832 Results.push_back(Results[0].getValue(1));
2833 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002834 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002835 Results.push_back(Node->getOperand(0));
2836 }
2837 break;
2838 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002839 // Expand to CopyToReg if the target set
2840 // StackPointerRegisterToSaveRestore.
2841 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2842 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2843 Node->getOperand(1)));
2844 } else {
2845 Results.push_back(Node->getOperand(0));
2846 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002847 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002848 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002849 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002850 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002851 case ISD::FNEG:
2852 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2853 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2854 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2855 Node->getOperand(0));
2856 Results.push_back(Tmp1);
2857 break;
2858 case ISD::FABS: {
2859 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002860 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002861 Tmp1 = Node->getOperand(0);
2862 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002863 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002864 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002865 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2866 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002867 Results.push_back(Tmp1);
2868 break;
2869 }
2870 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002871 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2872 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002873 break;
2874 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002875 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2876 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002877 break;
2878 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002879 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2880 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002881 break;
2882 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002883 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2884 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002885 break;
2886 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002887 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2888 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002889 break;
2890 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002891 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2892 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002893 break;
2894 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002895 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2896 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002897 break;
2898 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002899 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2900 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002901 break;
2902 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00002903 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2904 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002905 break;
2906 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00002907 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2908 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002909 break;
2910 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00002911 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2912 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002913 break;
2914 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00002915 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
2916 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002917 break;
2918 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00002919 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
2920 RTLIB::NEARBYINT_F64,
2921 RTLIB::NEARBYINT_F80,
2922 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002923 break;
2924 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00002925 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
2926 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002927 break;
2928 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00002929 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
2930 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002931 break;
2932 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00002933 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
2934 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002935 break;
2936 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00002937 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
2938 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002939 break;
Cameron Zwarich33390842011-07-08 21:39:21 +00002940 case ISD::FMA:
2941 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
2942 RTLIB::FMA_F80, RTLIB::FMA_PPCF128));
2943 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00002944 case ISD::FP16_TO_FP32:
2945 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
2946 break;
2947 case ISD::FP32_TO_FP16:
2948 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
2949 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002950 case ISD::ConstantFP: {
2951 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00002952 // Check to see if this FP immediate is already legal.
2953 // If this is a legal constant, turn it into a TargetConstantFP node.
Dan Gohman2ba60e52011-10-28 01:29:32 +00002954 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
2955 Results.push_back(ExpandConstantFP(CFP, true));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002956 break;
2957 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00002958 case ISD::EHSELECTION: {
2959 unsigned Reg = TLI.getExceptionSelectorRegister();
2960 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002961 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
2962 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002963 Results.push_back(Results[0].getValue(1));
2964 break;
2965 }
2966 case ISD::EXCEPTIONADDR: {
2967 unsigned Reg = TLI.getExceptionAddressRegister();
2968 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002969 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
2970 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002971 Results.push_back(Results[0].getValue(1));
2972 break;
2973 }
2974 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00002975 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002976 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2977 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
2978 "Don't know how to expand this subtraction!");
2979 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
2980 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00002981 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2982 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002983 break;
2984 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002985 case ISD::UREM:
2986 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00002987 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002988 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002989 bool isSigned = Node->getOpcode() == ISD::SREM;
2990 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
2991 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
2992 Tmp2 = Node->getOperand(0);
2993 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00002994 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
2995 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
2996 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00002997 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
2998 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002999 // X % Y -> X-X/Y*Y
3000 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3001 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3002 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003003 } else if (isSigned)
3004 Tmp1 = ExpandIntLibCall(Node, true,
3005 RTLIB::SREM_I8,
3006 RTLIB::SREM_I16, RTLIB::SREM_I32,
3007 RTLIB::SREM_I64, RTLIB::SREM_I128);
3008 else
3009 Tmp1 = ExpandIntLibCall(Node, false,
3010 RTLIB::UREM_I8,
3011 RTLIB::UREM_I16, RTLIB::UREM_I32,
3012 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003013 Results.push_back(Tmp1);
3014 break;
3015 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003016 case ISD::UDIV:
3017 case ISD::SDIV: {
3018 bool isSigned = Node->getOpcode() == ISD::SDIV;
3019 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003020 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003021 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003022 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3023 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3024 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003025 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3026 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003027 else if (isSigned)
3028 Tmp1 = ExpandIntLibCall(Node, true,
3029 RTLIB::SDIV_I8,
3030 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3031 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3032 else
3033 Tmp1 = ExpandIntLibCall(Node, false,
3034 RTLIB::UDIV_I8,
3035 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3036 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003037 Results.push_back(Tmp1);
3038 break;
3039 }
3040 case ISD::MULHU:
3041 case ISD::MULHS: {
3042 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3043 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003044 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003045 SDVTList VTs = DAG.getVTList(VT, VT);
3046 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3047 "If this wasn't legal, it shouldn't have been created!");
3048 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3049 Node->getOperand(1));
3050 Results.push_back(Tmp1.getValue(1));
3051 break;
3052 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003053 case ISD::SDIVREM:
3054 case ISD::UDIVREM:
3055 // Expand into divrem libcall
3056 ExpandDivRemLibCall(Node, Results);
3057 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003058 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003059 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003060 SDVTList VTs = DAG.getVTList(VT, VT);
3061 // See if multiply or divide can be lowered using two-result operations.
3062 // We just need the low half of the multiply; try both the signed
3063 // and unsigned forms. If the target supports both SMUL_LOHI and
3064 // UMUL_LOHI, form a preference by checking which forms of plain
3065 // MULH it supports.
3066 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3067 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3068 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3069 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3070 unsigned OpToUse = 0;
3071 if (HasSMUL_LOHI && !HasMULHS) {
3072 OpToUse = ISD::SMUL_LOHI;
3073 } else if (HasUMUL_LOHI && !HasMULHU) {
3074 OpToUse = ISD::UMUL_LOHI;
3075 } else if (HasSMUL_LOHI) {
3076 OpToUse = ISD::SMUL_LOHI;
3077 } else if (HasUMUL_LOHI) {
3078 OpToUse = ISD::UMUL_LOHI;
3079 }
3080 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003081 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3082 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003083 break;
3084 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003085 Tmp1 = ExpandIntLibCall(Node, false,
3086 RTLIB::MUL_I8,
3087 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003088 RTLIB::MUL_I64, RTLIB::MUL_I128);
3089 Results.push_back(Tmp1);
3090 break;
3091 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003092 case ISD::SADDO:
3093 case ISD::SSUBO: {
3094 SDValue LHS = Node->getOperand(0);
3095 SDValue RHS = Node->getOperand(1);
3096 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3097 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3098 LHS, RHS);
3099 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003100 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003101
Eli Friedman4bc8c712009-05-27 12:20:41 +00003102 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3103
3104 // LHSSign -> LHS >= 0
3105 // RHSSign -> RHS >= 0
3106 // SumSign -> Sum >= 0
3107 //
3108 // Add:
3109 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3110 // Sub:
3111 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3112 //
3113 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3114 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3115 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3116 Node->getOpcode() == ISD::SADDO ?
3117 ISD::SETEQ : ISD::SETNE);
3118
3119 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3120 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3121
3122 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3123 Results.push_back(Cmp);
3124 break;
3125 }
3126 case ISD::UADDO:
3127 case ISD::USUBO: {
3128 SDValue LHS = Node->getOperand(0);
3129 SDValue RHS = Node->getOperand(1);
3130 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3131 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3132 LHS, RHS);
3133 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003134 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3135 Node->getOpcode () == ISD::UADDO ?
3136 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003137 break;
3138 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003139 case ISD::UMULO:
3140 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003141 EVT VT = Node->getValueType(0);
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003142 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003143 SDValue LHS = Node->getOperand(0);
3144 SDValue RHS = Node->getOperand(1);
3145 SDValue BottomHalf;
3146 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003147 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003148 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3149 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3150 bool isSigned = Node->getOpcode() == ISD::SMULO;
3151 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3152 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3153 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3154 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3155 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3156 RHS);
3157 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003158 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3159 VT.getSizeInBits() * 2))) {
Eli Friedmandb3c1692009-06-16 06:58:29 +00003160 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3161 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3162 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3163 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3164 DAG.getIntPtrConstant(0));
3165 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3166 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003167 } else {
3168 // We can fall back to a libcall with an illegal type for the MUL if we
3169 // have a libcall big enough.
3170 // Also, we can fall back to a division in some cases, but that's a big
3171 // performance hit in the general case.
Eric Christopher38a18262011-01-20 00:29:24 +00003172 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3173 if (WideVT == MVT::i16)
3174 LC = RTLIB::MUL_I16;
3175 else if (WideVT == MVT::i32)
3176 LC = RTLIB::MUL_I32;
3177 else if (WideVT == MVT::i64)
3178 LC = RTLIB::MUL_I64;
3179 else if (WideVT == MVT::i128)
3180 LC = RTLIB::MUL_I128;
3181 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanf316eb72011-05-16 22:09:53 +00003182
3183 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003184 // part.
3185 unsigned LoSize = VT.getSizeInBits();
3186 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
3187 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
3188 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
3189 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
Owen Anderson95771af2011-02-25 21:41:48 +00003190
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003191 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3192 // pre-lowered to the correct types. This all depends upon WideVT not
3193 // being a legal type for the architecture and thus has to be split to
3194 // two arguments.
3195 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3196 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3197 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3198 DAG.getIntPtrConstant(0));
3199 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3200 DAG.getIntPtrConstant(1));
Dan Gohman2ba60e52011-10-28 01:29:32 +00003201 // Ret is a node with an illegal type. Because such things are not
3202 // generally permitted during this phase of legalization, delete the
3203 // node. The above EXTRACT_ELEMENT nodes should have been folded.
3204 DAG.DeleteNode(Ret.getNode());
Eli Friedmandb3c1692009-06-16 06:58:29 +00003205 }
Dan Gohmanf316eb72011-05-16 22:09:53 +00003206
Eli Friedmandb3c1692009-06-16 06:58:29 +00003207 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003208 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3209 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003210 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3211 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3212 ISD::SETNE);
3213 } else {
3214 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3215 DAG.getConstant(0, VT), ISD::SETNE);
3216 }
3217 Results.push_back(BottomHalf);
3218 Results.push_back(TopHalf);
3219 break;
3220 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003221 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003222 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003223 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3224 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003225 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003226 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003227 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003228 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003229 break;
3230 }
Eli Friedman509150f2009-05-27 07:58:35 +00003231 case ISD::SELECT:
3232 Tmp1 = Node->getOperand(0);
3233 Tmp2 = Node->getOperand(1);
3234 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003235 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003236 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3237 Tmp2, Tmp3,
3238 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003239 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003240 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3241 DAG.getConstant(0, Tmp1.getValueType()),
3242 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003243 }
Eli Friedman509150f2009-05-27 07:58:35 +00003244 Results.push_back(Tmp1);
3245 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003246 case ISD::BR_JT: {
3247 SDValue Chain = Node->getOperand(0);
3248 SDValue Table = Node->getOperand(1);
3249 SDValue Index = Node->getOperand(2);
3250
Owen Andersone50ed302009-08-10 22:56:29 +00003251 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003252
3253 const TargetData &TD = *TLI.getTargetData();
3254 unsigned EntrySize =
3255 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003256
Chris Lattner071c62f2010-01-25 23:26:13 +00003257 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003258 Index, DAG.getConstant(EntrySize, PTy));
3259 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3260
Owen Anderson23b9b192009-08-12 00:36:31 +00003261 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003262 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003263 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003264 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003265 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003266 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003267 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003268 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003269 // RelocBase can be JumpTable, GOT or some sort of global base.
3270 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3271 TLI.getPICJumpTableRelocBase(Table, DAG));
3272 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003273 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003274 Results.push_back(Tmp1);
3275 break;
3276 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003277 case ISD::BRCOND:
3278 // Expand brcond's setcc into its constituent parts and create a BR_CC
3279 // Node.
3280 Tmp1 = Node->getOperand(0);
3281 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003282 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003283 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003284 Tmp1, Tmp2.getOperand(2),
3285 Tmp2.getOperand(0), Tmp2.getOperand(1),
3286 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003287 } else {
Stuart Hastings88882242011-05-13 00:51:54 +00003288 // We test only the i1 bit. Skip the AND if UNDEF.
3289 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3290 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3291 DAG.getConstant(1, Tmp2.getValueType()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastings88882242011-05-13 00:51:54 +00003293 DAG.getCondCode(ISD::SETNE), Tmp3,
3294 DAG.getConstant(0, Tmp3.getValueType()),
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003295 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003296 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003297 Results.push_back(Tmp1);
3298 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003299 case ISD::SETCC: {
3300 Tmp1 = Node->getOperand(0);
3301 Tmp2 = Node->getOperand(1);
3302 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003303 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003304
3305 // If we expanded the SETCC into an AND/OR, return the new node
3306 if (Tmp2.getNode() == 0) {
3307 Results.push_back(Tmp1);
3308 break;
3309 }
3310
3311 // Otherwise, SETCC for the given comparison type must be completely
3312 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003313 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003314 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3315 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3316 Results.push_back(Tmp1);
3317 break;
3318 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003319 case ISD::SELECT_CC: {
3320 Tmp1 = Node->getOperand(0); // LHS
3321 Tmp2 = Node->getOperand(1); // RHS
3322 Tmp3 = Node->getOperand(2); // True
3323 Tmp4 = Node->getOperand(3); // False
3324 SDValue CC = Node->getOperand(4);
3325
3326 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003327 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003328
3329 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3330 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3331 CC = DAG.getCondCode(ISD::SETNE);
3332 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3333 Tmp3, Tmp4, CC);
3334 Results.push_back(Tmp1);
3335 break;
3336 }
3337 case ISD::BR_CC: {
3338 Tmp1 = Node->getOperand(0); // Chain
3339 Tmp2 = Node->getOperand(2); // LHS
3340 Tmp3 = Node->getOperand(3); // RHS
3341 Tmp4 = Node->getOperand(1); // CC
3342
3343 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003344 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003345
3346 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3347 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3348 Tmp4 = DAG.getCondCode(ISD::SETNE);
3349 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3350 Tmp3, Node->getOperand(4));
3351 Results.push_back(Tmp1);
3352 break;
3353 }
Dan Gohman2ba60e52011-10-28 01:29:32 +00003354 case ISD::BUILD_VECTOR:
3355 Results.push_back(ExpandBUILD_VECTOR(Node));
3356 break;
3357 case ISD::SRA:
3358 case ISD::SRL:
3359 case ISD::SHL: {
3360 // Scalarize vector SRA/SRL/SHL.
3361 EVT VT = Node->getValueType(0);
3362 assert(VT.isVector() && "Unable to legalize non-vector shift");
3363 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3364 unsigned NumElem = VT.getVectorNumElements();
3365
3366 SmallVector<SDValue, 8> Scalars;
3367 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
3368 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3369 VT.getScalarType(),
3370 Node->getOperand(0), DAG.getIntPtrConstant(Idx));
3371 SDValue Sh = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3372 VT.getScalarType(),
3373 Node->getOperand(1), DAG.getIntPtrConstant(Idx));
3374 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3375 VT.getScalarType(), Ex, Sh));
3376 }
3377 SDValue Result =
3378 DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
3379 &Scalars[0], Scalars.size());
3380 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
3381 break;
3382 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003383 case ISD::GLOBAL_OFFSET_TABLE:
3384 case ISD::GlobalAddress:
3385 case ISD::GlobalTLSAddress:
3386 case ISD::ExternalSymbol:
3387 case ISD::ConstantPool:
3388 case ISD::JumpTable:
3389 case ISD::INTRINSIC_W_CHAIN:
3390 case ISD::INTRINSIC_WO_CHAIN:
3391 case ISD::INTRINSIC_VOID:
3392 // FIXME: Custom lowering for these operations shouldn't return null!
Eli Friedman3f727d62009-05-27 02:16:40 +00003393 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003394 }
Dan Gohman2ba60e52011-10-28 01:29:32 +00003395
3396 // Replace the original node with the legalized result.
3397 if (!Results.empty())
3398 DAG.ReplaceAllUsesWith(Node, Results.data(), this);
Eli Friedman8c377c72009-05-27 01:25:56 +00003399}
Dan Gohman2ba60e52011-10-28 01:29:32 +00003400
3401void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
3402 SmallVector<SDValue, 8> Results;
Owen Andersone50ed302009-08-10 22:56:29 +00003403 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003404 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003405 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003406 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003407 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003408 }
Owen Andersone50ed302009-08-10 22:56:29 +00003409 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003410 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003411 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003412 switch (Node->getOpcode()) {
3413 case ISD::CTTZ:
3414 case ISD::CTLZ:
3415 case ISD::CTPOP:
3416 // Zero extend the argument.
3417 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3418 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003419 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003420 if (Node->getOpcode() == ISD::CTTZ) {
3421 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003422 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003423 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3424 ISD::SETEQ);
3425 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3426 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3427 } else if (Node->getOpcode() == ISD::CTLZ) {
3428 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3429 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3430 DAG.getConstant(NVT.getSizeInBits() -
3431 OVT.getSizeInBits(), NVT));
3432 }
Bill Wendling775db972009-12-23 00:28:23 +00003433 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003434 break;
3435 case ISD::BSWAP: {
3436 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003437 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003438 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3439 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003440 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003441 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003442 break;
3443 }
3444 case ISD::FP_TO_UINT:
3445 case ISD::FP_TO_SINT:
3446 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3447 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3448 Results.push_back(Tmp1);
3449 break;
3450 case ISD::UINT_TO_FP:
3451 case ISD::SINT_TO_FP:
3452 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3453 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3454 Results.push_back(Tmp1);
3455 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003456 case ISD::AND:
3457 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003458 case ISD::XOR: {
3459 unsigned ExtOp, TruncOp;
3460 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003461 ExtOp = ISD::BITCAST;
3462 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003463 } else {
3464 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003465 ExtOp = ISD::ANY_EXTEND;
3466 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003467 }
3468 // Promote each of the values to the new type.
3469 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3470 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3471 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003472 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3473 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003474 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003475 }
3476 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003477 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003478 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003479 ExtOp = ISD::BITCAST;
3480 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003481 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003482 ExtOp = ISD::ANY_EXTEND;
3483 TruncOp = ISD::TRUNCATE;
3484 } else {
3485 ExtOp = ISD::FP_EXTEND;
3486 TruncOp = ISD::FP_ROUND;
3487 }
3488 Tmp1 = Node->getOperand(0);
3489 // Promote each of the values to the new type.
3490 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3491 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3492 // Perform the larger operation, then round down.
3493 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3494 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003495 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003496 else
Bill Wendling775db972009-12-23 00:28:23 +00003497 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003498 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003499 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003500 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003501 }
Eli Friedman509150f2009-05-27 07:58:35 +00003502 case ISD::VECTOR_SHUFFLE: {
3503 SmallVector<int, 8> Mask;
3504 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3505
3506 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003507 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3508 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003509
3510 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003511 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003512 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003513 Results.push_back(Tmp1);
3514 break;
3515 }
Eli Friedmanad754602009-05-28 03:56:57 +00003516 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003517 unsigned ExtOp = ISD::FP_EXTEND;
3518 if (NVT.isInteger()) {
3519 ISD::CondCode CCCode =
3520 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3521 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003522 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003523 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3524 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003525 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3526 Tmp1, Tmp2, Node->getOperand(2)));
3527 break;
3528 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003529 }
Dan Gohman2ba60e52011-10-28 01:29:32 +00003530
3531 // Replace the original node with the legalized result.
3532 if (!Results.empty())
3533 DAG.ReplaceAllUsesWith(Node, Results.data(), this);
Eli Friedman8c377c72009-05-27 01:25:56 +00003534}
3535
Chris Lattner3e928bb2005-01-07 07:47:09 +00003536// SelectionDAG::Legalize - This is the entry point for the file.
3537//
Dan Gohman975716a2011-05-16 22:19:54 +00003538void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003539 /// run - This is the main entry point to this class.
3540 ///
Dan Gohman975716a2011-05-16 22:19:54 +00003541 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003542}