blob: 0e864fecd541e8f8787620114f6a98c02a20404e [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 Gohman65fd6562011-11-03 21:49:52 +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 Gohman65fd6562011-11-03 21:49:52 +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 Gohman65fd6562011-11-03 21:49:52 +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 Gohman65fd6562011-11-03 21:49:52 +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 Gohman65fd6562011-11-03 21:49:52 +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
Dan Gohman65fd6562011-11-03 21:49:52 +0000188 // Visit all the nodes. We start in topological order, so that we see
189 // nodes with their original operands intact. Legalization can produce
190 // new nodes which may themselves need to be legalized. Iterate until all
191 // nodes have been legalized.
192 for (;;) {
193 bool AnyLegalized = false;
194 for (LegalizePosition = DAG.allnodes_end();
195 LegalizePosition != DAG.allnodes_begin(); ) {
196 --LegalizePosition;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000197
Dan Gohman65fd6562011-11-03 21:49:52 +0000198 SDNode *N = LegalizePosition;
199 if (LegalizedNodes.insert(N)) {
200 AnyLegalized = true;
201 LegalizeOp(N);
202 }
203 }
204 if (!AnyLegalized)
205 break;
206
207 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000208
209 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000210 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000211}
212
Evan Cheng9f877882006-12-13 20:57:08 +0000213/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
214/// a load from the constant pool.
Dan Gohman65fd6562011-11-03 21:49:52 +0000215SDValue
216SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
Evan Cheng00495212006-12-12 21:32:44 +0000217 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000218 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000219
220 // If a FP immediate is precise when represented as a float and if the
221 // target can do an extending load from float to double, we put it into
222 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000223 // double. This shrinks FP constants and canonicalizes them for targets where
224 // an FP extending load is the same cost as a normal load (such as on the x87
225 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000226 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000227 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000228 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000230 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000232 }
233
Owen Andersone50ed302009-08-10 22:56:29 +0000234 EVT OrigVT = VT;
235 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 while (SVT != MVT::f32) {
237 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000238 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000239 // Only do this if the target has a native EXTLOAD instruction from
240 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000241 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000242 TLI.ShouldShrinkFPConstant(OrigVT)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000243 Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000244 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000245 VT = SVT;
246 Extend = true;
247 }
Evan Cheng00495212006-12-12 21:32:44 +0000248 }
249
Dan Gohman475871a2008-07-27 21:46:04 +0000250 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000251 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman65fd6562011-11-03 21:49:52 +0000252 if (Extend) {
253 SDValue Result =
254 DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
255 DAG.getEntryNode(),
256 CPIdx, MachinePointerInfo::getConstantPool(),
257 VT, false, false, Alignment);
258 return Result;
259 }
260 SDValue Result =
261 DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000262 MachinePointerInfo::getConstantPool(), false, false, false,
Dan Gohman65fd6562011-11-03 21:49:52 +0000263 Alignment);
264 return Result;
Evan Cheng00495212006-12-12 21:32:44 +0000265}
266
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000267/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
Dan Gohman65fd6562011-11-03 21:49:52 +0000268static void ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
269 const TargetLowering &TLI,
270 SelectionDAG::DAGUpdateListener *DUL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000271 SDValue Chain = ST->getChain();
272 SDValue Ptr = ST->getBasePtr();
273 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000274 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000275 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000276 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000277 if (ST->getMemoryVT().isFloatingPoint() ||
278 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000279 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000280 if (TLI.isTypeLegal(intVT)) {
281 // Expand to a bitconvert of the value to the integer type of the
282 // same size, then a (misaligned) int store.
283 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000284 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Dan Gohman65fd6562011-11-03 21:49:52 +0000285 Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
286 ST->isVolatile(), ST->isNonTemporal(), Alignment);
287 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000288 DAG.RemoveDeadNode(ST, DUL);
Dan Gohman65fd6562011-11-03 21:49:52 +0000289 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000290 }
Dan Gohman1b328962011-05-17 22:22:52 +0000291 // Do a (aligned) store to a stack slot, then copy from the stack slot
292 // to the final destination using (unaligned) integer loads and stores.
293 EVT StoredVT = ST->getMemoryVT();
294 EVT RegVT =
295 TLI.getRegisterType(*DAG.getContext(),
296 EVT::getIntegerVT(*DAG.getContext(),
297 StoredVT.getSizeInBits()));
298 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
299 unsigned RegBytes = RegVT.getSizeInBits() / 8;
300 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
301
302 // Make sure the stack slot is also aligned for the register type.
303 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
304
305 // Perform the original store, only redirected to the stack slot.
306 SDValue Store = DAG.getTruncStore(Chain, dl,
307 Val, StackPtr, MachinePointerInfo(),
308 StoredVT, false, false, 0);
309 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
310 SmallVector<SDValue, 8> Stores;
311 unsigned Offset = 0;
312
313 // Do all but one copies using the full register width.
314 for (unsigned i = 1; i < NumRegs; i++) {
315 // Load one integer register's worth from the stack slot.
316 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
317 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000318 false, false, false, 0);
Dan Gohman1b328962011-05-17 22:22:52 +0000319 // Store it to the final location. Remember the store.
320 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
321 ST->getPointerInfo().getWithOffset(Offset),
322 ST->isVolatile(), ST->isNonTemporal(),
323 MinAlign(ST->getAlignment(), Offset)));
324 // Increment the pointers.
325 Offset += RegBytes;
326 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
327 Increment);
328 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
329 }
330
331 // The last store may be partial. Do a truncating store. On big-endian
332 // machines this requires an extending load from the stack slot to ensure
333 // that the bits are in the right place.
334 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
335 8 * (StoredBytes - Offset));
336
337 // Load from the stack slot.
338 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
339 MachinePointerInfo(),
340 MemVT, false, false, 0);
341
342 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
343 ST->getPointerInfo()
344 .getWithOffset(Offset),
345 MemVT, ST->isVolatile(),
346 ST->isNonTemporal(),
347 MinAlign(ST->getAlignment(), Offset)));
348 // The order of the stores doesn't matter - say it with a TokenFactor.
Dan Gohman65fd6562011-11-03 21:49:52 +0000349 SDValue Result =
350 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
351 Stores.size());
352 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000353 DAG.RemoveDeadNode(ST, DUL);
Dan Gohman65fd6562011-11-03 21:49:52 +0000354 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000355 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000356 assert(ST->getMemoryVT().isInteger() &&
357 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000358 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000359 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000360 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000361 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000362 int IncrementSize = NumBits / 8;
363
364 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000365 SDValue ShiftAmount = DAG.getConstant(NumBits,
366 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000367 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000368 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000369
370 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000371 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000372 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000373 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000374 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000375 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000376 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000377 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000378 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000379 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000380 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
381 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000382
Dan Gohman65fd6562011-11-03 21:49:52 +0000383 SDValue Result =
384 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
385 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000386 DAG.RemoveDeadNode(ST, DUL);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000387}
388
389/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
Dan Gohman65fd6562011-11-03 21:49:52 +0000390static void
391ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
392 const TargetLowering &TLI,
393 SDValue &ValResult, SDValue &ChainResult) {
Dan Gohman475871a2008-07-27 21:46:04 +0000394 SDValue Chain = LD->getChain();
395 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000396 EVT VT = LD->getValueType(0);
397 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000398 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000399 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000400 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000401 if (TLI.isTypeLegal(intVT)) {
402 // Expand to a (misaligned) integer load of the same size,
403 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000404 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
405 LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000406 LD->isNonTemporal(),
407 LD->isInvariant(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000408 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000409 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000410 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000411
Dan Gohman65fd6562011-11-03 21:49:52 +0000412 ValResult = Result;
413 ChainResult = Chain;
414 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000415 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000416
Chris Lattnerecf42c42010-09-21 16:36:31 +0000417 // Copy the value to a (aligned) stack slot using (unaligned) integer
418 // loads and stores, then do a (aligned) load from the stack slot.
419 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
420 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
421 unsigned RegBytes = RegVT.getSizeInBits() / 8;
422 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
423
424 // Make sure the stack slot is also aligned for the register type.
425 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
426
427 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
428 SmallVector<SDValue, 8> Stores;
429 SDValue StackPtr = StackBase;
430 unsigned Offset = 0;
431
432 // Do all but one copies using the full register width.
433 for (unsigned i = 1; i < NumRegs; i++) {
434 // Load one integer register's worth from the original location.
435 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
436 LD->getPointerInfo().getWithOffset(Offset),
437 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000438 LD->isInvariant(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000439 MinAlign(LD->getAlignment(), Offset));
440 // Follow the load with a store to the stack slot. Remember the store.
441 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000442 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000443 // Increment the pointers.
444 Offset += RegBytes;
445 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
446 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
447 Increment);
448 }
449
450 // The last copy may be partial. Do an extending load.
451 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
452 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000453 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000454 LD->getPointerInfo().getWithOffset(Offset),
455 MemVT, LD->isVolatile(),
456 LD->isNonTemporal(),
457 MinAlign(LD->getAlignment(), Offset));
458 // Follow the load with a store to the stack slot. Remember the store.
459 // On big-endian machines this requires a truncating store to ensure
460 // that the bits end up in the right place.
461 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
462 MachinePointerInfo(), MemVT,
463 false, false, 0));
464
465 // The order of the stores doesn't matter - say it with a TokenFactor.
466 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
467 Stores.size());
468
469 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000470 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000471 MachinePointerInfo(), LoadedVT, false, false, 0);
472
473 // Callers expect a MERGE_VALUES node.
Dan Gohman65fd6562011-11-03 21:49:52 +0000474 ValResult = Load;
475 ChainResult = TF;
476 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000477 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000478 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000479 "Unaligned load of unsupported type.");
480
Dale Johannesen8155d642008-02-27 22:36:00 +0000481 // Compute the new VT that is half the size of the old one. This is an
482 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000483 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000484 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000485 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000486 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000487
Chris Lattnere400af82007-11-19 21:38:03 +0000488 unsigned Alignment = LD->getAlignment();
489 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000490 ISD::LoadExtType HiExtType = LD->getExtensionType();
491
492 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
493 if (HiExtType == ISD::NON_EXTLOAD)
494 HiExtType = ISD::ZEXTLOAD;
495
496 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000497 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000498 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000499 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000500 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000501 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000502 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000503 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000504 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000505 LD->getPointerInfo().getWithOffset(IncrementSize),
506 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000507 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000508 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000509 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000510 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000511 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000512 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000513 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000514 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000515 LD->getPointerInfo().getWithOffset(IncrementSize),
516 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000517 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000518 }
519
520 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000521 SDValue ShiftAmount = DAG.getConstant(NumBits,
522 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000523 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
524 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000525
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000527 Hi.getValue(1));
528
Dan Gohman65fd6562011-11-03 21:49:52 +0000529 ValResult = Result;
530 ChainResult = TF;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000531}
Evan Cheng912095b2007-01-04 21:56:39 +0000532
Nate Begeman68679912008-04-25 18:07:40 +0000533/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
534/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
535/// is necessary to spill the vector being inserted into to memory, perform
536/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000537SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000538PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
539 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000540 SDValue Tmp1 = Vec;
541 SDValue Tmp2 = Val;
542 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000543
Nate Begeman68679912008-04-25 18:07:40 +0000544 // If the target doesn't support this, we have to spill the input vector
545 // to a temporary stack slot, update the element, then reload it. This is
546 // badness. We could also load the value into a vector register (either
547 // with a "move to register" or "extload into register" instruction, then
548 // permute it into place, if the idx is a constant and if the idx is
549 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000550 EVT VT = Tmp1.getValueType();
551 EVT EltVT = VT.getVectorElementType();
552 EVT IdxVT = Tmp3.getValueType();
553 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000554 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000555
Evan Chengff89dcb2009-10-18 18:16:27 +0000556 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
557
Nate Begeman68679912008-04-25 18:07:40 +0000558 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000559 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000560 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000561 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000562
563 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000564 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000565 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000566 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000567 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000568 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
569 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000570 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000571 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000572 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000573 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000574 return DAG.getLoad(VT, dl, Ch, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000575 MachinePointerInfo::getFixedStack(SPFI), false, false,
576 false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000577}
578
Mon P Wange9f10152008-12-09 05:46:39 +0000579
Eli Friedman3f727d62009-05-27 02:16:40 +0000580SDValue SelectionDAGLegalize::
581ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
582 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
583 // SCALAR_TO_VECTOR requires that the type of the value being inserted
584 // match the element type of the vector being created, except for
585 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000586 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000587 if (Val.getValueType() == EltVT ||
588 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
589 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
590 Vec.getValueType(), Val);
591
592 unsigned NumElts = Vec.getValueType().getVectorNumElements();
593 // We generate a shuffle of InVec and ScVec, so the shuffle mask
594 // should be 0,1,2,3,4,5... with the appropriate element replaced with
595 // elt 0 of the RHS.
596 SmallVector<int, 8> ShufOps;
597 for (unsigned i = 0; i != NumElts; ++i)
598 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
599
600 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
601 &ShufOps[0]);
602 }
603 }
604 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
605}
606
Eli Friedman7ef3d172009-06-06 07:04:42 +0000607SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
608 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
609 // FIXME: We shouldn't do this for TargetConstantFP's.
610 // FIXME: move this to the DAG Combiner! Note that we can't regress due
611 // to phase ordering between legalized code and the dag combiner. This
612 // probably means that we need to integrate dag combiner and legalizer
613 // together.
614 // We generally can't do this one for long doubles.
615 SDValue Tmp1 = ST->getChain();
616 SDValue Tmp2 = ST->getBasePtr();
617 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000618 unsigned Alignment = ST->getAlignment();
619 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000620 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000621 DebugLoc dl = ST->getDebugLoc();
622 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 if (CFP->getValueType(0) == MVT::f32 &&
Dan Gohman75b10042011-07-15 22:39:09 +0000624 TLI.isTypeLegal(MVT::i32)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000625 Tmp3 = DAG.getConstant(CFP->getValueAPF().
626 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000628 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
629 isVolatile, isNonTemporal, Alignment);
630 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000631
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000632 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000633 // If this target supports 64-bit registers, do a single 64-bit store.
Dan Gohman75b10042011-07-15 22:39:09 +0000634 if (TLI.isTypeLegal(MVT::i64)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000635 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000637 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
638 isVolatile, isNonTemporal, Alignment);
639 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000640
Dan Gohman75b10042011-07-15 22:39:09 +0000641 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000642 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
643 // stores. If the target supports neither 32- nor 64-bits, this
644 // xform is certainly not worth it.
645 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000646 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000648 if (TLI.isBigEndian()) std::swap(Lo, Hi);
649
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000650 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
651 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000652 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
653 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000654 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
655 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000656 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000657
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000659 }
660 }
661 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000662 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000663}
664
Dan Gohman6a109f92011-07-15 21:42:20 +0000665/// LegalizeOp - Return a legal replacement for the given operation, with
666/// all legal operands.
Dan Gohman65fd6562011-11-03 21:49:52 +0000667void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
668 if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
669 return;
Scott Michelfdc40a02009-02-17 22:15:04 +0000670
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000671 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000672
Eli Friedman1fde9c52009-05-24 02:46:31 +0000673 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000674 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
675 TargetLowering::TypeLegal &&
Eli Friedman1fde9c52009-05-24 02:46:31 +0000676 "Unexpected illegal type!");
677
678 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000679 assert((TLI.getTypeAction(*DAG.getContext(),
680 Node->getOperand(i).getValueType()) ==
681 TargetLowering::TypeLegal ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000682 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
683 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000684
Dan Gohman475871a2008-07-27 21:46:04 +0000685 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +0000686 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000687
Eli Friedman8c377c72009-05-27 01:25:56 +0000688 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000689 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000690 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000691 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000692 case ISD::INTRINSIC_W_CHAIN:
693 case ISD::INTRINSIC_WO_CHAIN:
694 case ISD::INTRINSIC_VOID:
695 case ISD::VAARG:
696 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000698 break;
699 case ISD::SINT_TO_FP:
700 case ISD::UINT_TO_FP:
701 case ISD::EXTRACT_VECTOR_ELT:
702 Action = TLI.getOperationAction(Node->getOpcode(),
703 Node->getOperand(0).getValueType());
704 break;
705 case ISD::FP_ROUND_INREG:
706 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000707 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000708 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
709 break;
710 }
Eli Friedman327236c2011-08-24 20:50:09 +0000711 case ISD::ATOMIC_STORE: {
712 Action = TLI.getOperationAction(Node->getOpcode(),
713 Node->getOperand(2).getValueType());
714 break;
715 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000716 case ISD::SELECT_CC:
717 case ISD::SETCC:
718 case ISD::BR_CC: {
719 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
720 Node->getOpcode() == ISD::SETCC ? 2 : 1;
721 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000722 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000723 ISD::CondCode CCCode =
724 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
725 Action = TLI.getCondCodeAction(CCCode, OpVT);
726 if (Action == TargetLowering::Legal) {
727 if (Node->getOpcode() == ISD::SELECT_CC)
728 Action = TLI.getOperationAction(Node->getOpcode(),
729 Node->getValueType(0));
730 else
731 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
732 }
733 break;
734 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000735 case ISD::LOAD:
736 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000737 // FIXME: Model these properly. LOAD and STORE are complicated, and
738 // STORE expects the unlegalized operand in some cases.
739 SimpleFinishLegalizing = false;
740 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000741 case ISD::CALLSEQ_START:
742 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000743 // FIXME: This shouldn't be necessary. These nodes have special properties
744 // dealing with the recursive nature of legalization. Removing this
745 // special case should be done as part of making LegalizeDAG non-recursive.
746 SimpleFinishLegalizing = false;
747 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000748 case ISD::EXTRACT_ELEMENT:
749 case ISD::FLT_ROUNDS_:
750 case ISD::SADDO:
751 case ISD::SSUBO:
752 case ISD::UADDO:
753 case ISD::USUBO:
754 case ISD::SMULO:
755 case ISD::UMULO:
756 case ISD::FPOWI:
757 case ISD::MERGE_VALUES:
758 case ISD::EH_RETURN:
759 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000760 case ISD::EH_SJLJ_SETJMP:
761 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000762 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000763 // These operations lie about being legal: when they claim to be legal,
764 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000765 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
766 if (Action == TargetLowering::Legal)
767 Action = TargetLowering::Expand;
768 break;
Duncan Sands4a544a72011-09-06 13:37:06 +0000769 case ISD::INIT_TRAMPOLINE:
770 case ISD::ADJUST_TRAMPOLINE:
Eli Friedman8c377c72009-05-27 01:25:56 +0000771 case ISD::FRAMEADDR:
772 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000773 // These operations lie about being legal: when they claim to be legal,
774 // they should actually be custom-lowered.
775 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
776 if (Action == TargetLowering::Legal)
777 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000778 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000779 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000780 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000781 Action = TargetLowering::Legal;
782 } else {
783 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000784 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000785 break;
786 }
787
788 if (SimpleFinishLegalizing) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000789 SmallVector<SDValue, 8> Ops;
Eli Friedman8c377c72009-05-27 01:25:56 +0000790 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman65fd6562011-11-03 21:49:52 +0000791 Ops.push_back(Node->getOperand(i));
Eli Friedman8c377c72009-05-27 01:25:56 +0000792 switch (Node->getOpcode()) {
793 default: break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000794 case ISD::SHL:
795 case ISD::SRL:
796 case ISD::SRA:
797 case ISD::ROTL:
798 case ISD::ROTR:
799 // Legalizing shifts/rotates requires adjusting the shift amount
800 // to the appropriate width.
Dan Gohman65fd6562011-11-03 21:49:52 +0000801 if (!Ops[1].getValueType().isVector()) {
802 SDValue SAO = DAG.getShiftAmountOperand(Ops[0].getValueType(), Ops[1]);
803 HandleSDNode Handle(SAO);
804 LegalizeOp(SAO.getNode());
805 Ops[1] = Handle.getValue();
806 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000807 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000808 case ISD::SRL_PARTS:
809 case ISD::SRA_PARTS:
810 case ISD::SHL_PARTS:
811 // Legalizing shifts/rotates requires adjusting the shift amount
812 // to the appropriate width.
Dan Gohman65fd6562011-11-03 21:49:52 +0000813 if (!Ops[2].getValueType().isVector()) {
814 SDValue SAO = DAG.getShiftAmountOperand(Ops[0].getValueType(), Ops[2]);
815 HandleSDNode Handle(SAO);
816 LegalizeOp(SAO.getNode());
817 Ops[2] = Handle.getValue();
818 }
Dan Gohman2c9489d2009-08-18 23:52:48 +0000819 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000820 }
821
Dan Gohman65fd6562011-11-03 21:49:52 +0000822 SDNode *NewNode = DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
823 if (NewNode != Node) {
824 DAG.ReplaceAllUsesWith(Node, NewNode, this);
825 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
826 DAG.TransferDbgValues(SDValue(Node, i), SDValue(NewNode, i));
827 DAG.RemoveDeadNode(Node, this);
828 Node = NewNode;
829 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000830 switch (Action) {
831 case TargetLowering::Legal:
Dan Gohman65fd6562011-11-03 21:49:52 +0000832 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000833 case TargetLowering::Custom:
834 // FIXME: The handling for custom lowering with multiple results is
835 // a complete mess.
Dan Gohman65fd6562011-11-03 21:49:52 +0000836 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Eli Friedman8c377c72009-05-27 01:25:56 +0000837 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000838 SmallVector<SDValue, 8> ResultVals;
Eli Friedman8c377c72009-05-27 01:25:56 +0000839 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
840 if (e == 1)
841 ResultVals.push_back(Tmp1);
842 else
843 ResultVals.push_back(Tmp1.getValue(i));
844 }
Dan Gohman65fd6562011-11-03 21:49:52 +0000845 if (Tmp1.getNode() != Node || Tmp1.getResNo() != 0) {
846 DAG.ReplaceAllUsesWith(Node, ResultVals.data(), this);
847 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
848 DAG.TransferDbgValues(SDValue(Node, i), ResultVals[i]);
849 DAG.RemoveDeadNode(Node, this);
850 }
851 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000852 }
853
854 // FALL THROUGH
855 case TargetLowering::Expand:
Dan Gohman65fd6562011-11-03 21:49:52 +0000856 ExpandNode(Node);
857 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000858 case TargetLowering::Promote:
Dan Gohman65fd6562011-11-03 21:49:52 +0000859 PromoteNode(Node);
860 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000861 }
862 }
863
864 switch (Node->getOpcode()) {
865 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000866#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +0000867 dbgs() << "NODE: ";
868 Node->dump( &DAG);
869 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000870#endif
Chris Lattner35a38932010-04-07 23:47:51 +0000871 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000872
Dan Gohman65fd6562011-11-03 21:49:52 +0000873 case ISD::CALLSEQ_START:
Dan Gohman6f3ddef2011-10-29 00:41:52 +0000874 case ISD::CALLSEQ_END:
Dan Gohman65fd6562011-11-03 21:49:52 +0000875 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000876 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +0000877 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman65fd6562011-11-03 21:49:52 +0000878 Tmp1 = LD->getChain(); // Legalize the chain.
879 Tmp2 = LD->getBasePtr(); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000880
Evan Cheng466685d2006-10-09 20:57:25 +0000881 ISD::LoadExtType ExtType = LD->getExtensionType();
882 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +0000883 EVT VT = Node->getValueType(0);
Dan Gohman65fd6562011-11-03 21:49:52 +0000884 Node = DAG.UpdateNodeOperands(Node, Tmp1, Tmp2, LD->getOffset());
885 Tmp3 = SDValue(Node, 0);
886 Tmp4 = SDValue(Node, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000887
Evan Cheng466685d2006-10-09 20:57:25 +0000888 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +0000889 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000890 case TargetLowering::Legal:
891 // If this is an unaligned load and the target doesn't support it,
892 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +0000893 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000894 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
Evan Chenge96507c2009-08-15 08:38:52 +0000895 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000896 if (LD->getAlignment() < ABIAlignment){
Dan Gohman65fd6562011-11-03 21:49:52 +0000897 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
898 DAG, TLI, Tmp3, Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000899 }
900 }
901 break;
Evan Cheng466685d2006-10-09 20:57:25 +0000902 case TargetLowering::Custom:
903 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000904 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000905 Tmp3 = Tmp1;
906 Tmp4 = Tmp1.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000907 }
Evan Cheng466685d2006-10-09 20:57:25 +0000908 break;
909 case TargetLowering::Promote: {
910 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000911 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +0000912 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +0000913 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000914
Chris Lattnerecf42c42010-09-21 16:36:31 +0000915 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000916 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000917 LD->isInvariant(), LD->getAlignment());
Dan Gohman65fd6562011-11-03 21:49:52 +0000918 Tmp3 = DAG.getNode(ISD::BITCAST, dl, VT, Tmp1);
919 Tmp4 = Tmp1.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000920 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000921 }
Evan Cheng466685d2006-10-09 20:57:25 +0000922 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000923 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +0000924 // legalized both of them.
Dan Gohman65fd6562011-11-03 21:49:52 +0000925 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp3);
926 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp4);
927 return;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000928 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000929
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000930 EVT SrcVT = LD->getMemoryVT();
931 unsigned SrcWidth = SrcVT.getSizeInBits();
932 unsigned Alignment = LD->getAlignment();
933 bool isVolatile = LD->isVolatile();
934 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000935
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000936 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
937 // Some targets pretend to have an i1 loading operation, and actually
938 // load an i8. This trick is correct for ZEXTLOAD because the top 7
939 // bits are guaranteed to be zero; it helps the optimizers understand
940 // that these bits are zero. It is also useful for EXTLOAD, since it
941 // tells the optimizers that those bits are undefined. It would be
942 // nice to have an effective generic way of getting these benefits...
943 // Until such a way is found, don't insist on promoting i1 here.
944 (SrcVT != MVT::i1 ||
945 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
946 // Promote to a byte-sized load if not loading an integral number of
947 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
948 unsigned NewWidth = SrcVT.getStoreSizeInBits();
949 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
950 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000951
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000952 // The extra bits are guaranteed to be zero, since we stored them that
953 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000954
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000955 ISD::LoadExtType NewExtType =
956 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000957
Dan Gohman65fd6562011-11-03 21:49:52 +0000958 SDValue Result =
959 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
960 Tmp1, Tmp2, LD->getPointerInfo(),
961 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000962
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000963 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000964
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000965 if (ExtType == ISD::SEXTLOAD)
966 // Having the top bits zero doesn't help when sign extending.
967 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
968 Result.getValueType(),
969 Result, DAG.getValueType(SrcVT));
970 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
971 // All the top bits are guaranteed to be zero - inform the optimizers.
972 Result = DAG.getNode(ISD::AssertZext, dl,
973 Result.getValueType(), Result,
974 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000975
Dan Gohman65fd6562011-11-03 21:49:52 +0000976 Tmp1 = Result;
977 Tmp2 = Ch;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000978 } else if (SrcWidth & (SrcWidth - 1)) {
979 // If not loading a power-of-2 number of bits, expand as two loads.
980 assert(!SrcVT.isVector() && "Unsupported extload!");
981 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
982 assert(RoundWidth < SrcWidth);
983 unsigned ExtraWidth = SrcWidth - RoundWidth;
984 assert(ExtraWidth < RoundWidth);
985 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
986 "Load size not an integral number of bytes!");
987 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
988 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
989 SDValue Lo, Hi, Ch;
990 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000991
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000992 if (TLI.isLittleEndian()) {
993 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
994 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000995 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000996 Tmp1, Tmp2,
997 LD->getPointerInfo(), RoundVT, isVolatile,
998 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000999
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001000 // Load the remaining ExtraWidth bits.
1001 IncrementSize = RoundWidth / 8;
1002 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1003 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001004 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001005 LD->getPointerInfo().getWithOffset(IncrementSize),
1006 ExtraVT, isVolatile, isNonTemporal,
1007 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001008
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001009 // Build a factor node to remember that this load is independent of
1010 // the other one.
1011 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1012 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001013
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001014 // Move the top bits to the right place.
1015 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001016 DAG.getConstant(RoundWidth,
1017 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001018
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001019 // Join the hi and lo parts.
Dan Gohman65fd6562011-11-03 21:49:52 +00001020 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001021 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001022 // Big endian - avoid unaligned loads.
1023 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1024 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001025 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001026 LD->getPointerInfo(), RoundVT, isVolatile,
1027 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001028
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001029 // Load the remaining ExtraWidth bits.
1030 IncrementSize = RoundWidth / 8;
1031 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1032 DAG.getIntPtrConstant(IncrementSize));
1033 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001034 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001035 LD->getPointerInfo().getWithOffset(IncrementSize),
1036 ExtraVT, isVolatile, isNonTemporal,
1037 MinAlign(Alignment, IncrementSize));
1038
1039 // Build a factor node to remember that this load is independent of
1040 // the other one.
1041 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1042 Hi.getValue(1));
1043
1044 // Move the top bits to the right place.
1045 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001046 DAG.getConstant(ExtraWidth,
1047 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001048
1049 // Join the hi and lo parts.
Dan Gohman65fd6562011-11-03 21:49:52 +00001050 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001051 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001052
Dan Gohman65fd6562011-11-03 21:49:52 +00001053 Tmp2 = Ch;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001054 } else {
1055 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1056 default: assert(0 && "This action is not supported yet!");
1057 case TargetLowering::Custom:
1058 isCustom = true;
1059 // FALLTHROUGH
1060 case TargetLowering::Legal:
Dan Gohman65fd6562011-11-03 21:49:52 +00001061 Node = DAG.UpdateNodeOperands(Node,
1062 Tmp1, Tmp2, LD->getOffset());
1063 Tmp1 = SDValue(Node, 0);
1064 Tmp2 = SDValue(Node, 1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001065
1066 if (isCustom) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001067 Tmp3 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001068 if (Tmp3.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001069 Tmp1 = Tmp3;
1070 Tmp2 = Tmp3.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001071 }
1072 } else {
1073 // If this is an unaligned load and the target doesn't support it,
1074 // expand it.
1075 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001076 Type *Ty =
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001077 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1078 unsigned ABIAlignment =
1079 TLI.getTargetData()->getABITypeAlignment(Ty);
1080 if (LD->getAlignment() < ABIAlignment){
Dan Gohman65fd6562011-11-03 21:49:52 +00001081 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
1082 DAG, TLI, Tmp1, Tmp2);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001083 }
1084 }
1085 }
1086 break;
1087 case TargetLowering::Expand:
Dan Gohman75b10042011-07-15 22:39:09 +00001088 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001089 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1090 LD->getPointerInfo(),
1091 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001092 LD->isInvariant(), LD->getAlignment());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001093 unsigned ExtendOp;
1094 switch (ExtType) {
1095 case ISD::EXTLOAD:
1096 ExtendOp = (SrcVT.isFloatingPoint() ?
1097 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1098 break;
1099 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1100 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1101 default: llvm_unreachable("Unexpected extend load type!");
1102 }
Dan Gohman65fd6562011-11-03 21:49:52 +00001103 Tmp1 = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1104 Tmp2 = Load.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001105 break;
1106 }
Nadav Rotemc2492c22011-06-14 08:11:52 +00001107
Nadav Roteme9b58d02011-10-15 07:41:10 +00001108 assert(!SrcVT.isVector() &&
1109 "Vector Loads are handled in LegalizeVectorOps");
Nadav Rotemc2492c22011-06-14 08:11:52 +00001110
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001111 // FIXME: This does not work for vectors on most targets. Sign- and
1112 // zero-extend operations are currently folded into extending loads,
1113 // whether they are legal or not, and then we end up here without any
1114 // support for legalizing them.
1115 assert(ExtType != ISD::EXTLOAD &&
1116 "EXTLOAD should always be supported!");
1117 // Turn the unsupported load into an EXTLOAD followed by an explicit
1118 // zero/sign extend inreg.
Dan Gohman65fd6562011-11-03 21:49:52 +00001119 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
1120 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1121 LD->isVolatile(), LD->isNonTemporal(),
1122 LD->getAlignment());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001123 SDValue ValRes;
1124 if (ExtType == ISD::SEXTLOAD)
1125 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1126 Result.getValueType(),
1127 Result, DAG.getValueType(SrcVT));
1128 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001129 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Dan Gohman65fd6562011-11-03 21:49:52 +00001130 Tmp1 = ValRes;
1131 Tmp2 = Result.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001132 break;
1133 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001134 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001135
1136 // Since loads produce two values, make sure to remember that we legalized
1137 // both of them.
Dan Gohman65fd6562011-11-03 21:49:52 +00001138 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp1);
1139 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp2);
1140 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001141 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001142 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001143 StoreSDNode *ST = cast<StoreSDNode>(Node);
Dan Gohman65fd6562011-11-03 21:49:52 +00001144 Tmp1 = ST->getChain();
1145 Tmp2 = ST->getBasePtr();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001146 unsigned Alignment = ST->getAlignment();
1147 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001148 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001149
Evan Cheng8b2794a2006-10-13 21:14:26 +00001150 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001151 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001152 DAG.ReplaceAllUsesWith(ST, OptStore, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001153 DAG.RemoveDeadNode(ST, this);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001154 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001155 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001156
Eli Friedman957bffa2009-05-24 08:42:01 +00001157 {
Dan Gohman65fd6562011-11-03 21:49:52 +00001158 Tmp3 = ST->getValue();
1159 Node = DAG.UpdateNodeOperands(Node,
1160 Tmp1, Tmp3, Tmp2,
1161 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001162
Owen Andersone50ed302009-08-10 22:56:29 +00001163 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001164 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001165 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001166 case TargetLowering::Legal:
1167 // If this is an unaligned store and the target doesn't support it,
1168 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001169 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001170 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001171 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001172 if (ST->getAlignment() < ABIAlignment)
Dan Gohman65fd6562011-11-03 21:49:52 +00001173 ExpandUnalignedStore(cast<StoreSDNode>(Node),
1174 DAG, TLI, this);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001175 }
1176 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001177 case TargetLowering::Custom:
Dan Gohman65fd6562011-11-03 21:49:52 +00001178 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001179 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001180 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Tmp1, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001181 DAG.RemoveDeadNode(Node, this);
1182 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001183 break;
Dan Gohman65fd6562011-11-03 21:49:52 +00001184 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001185 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001186 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001187 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dan Gohman65fd6562011-11-03 21:49:52 +00001188 SDValue Result =
1189 DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
1190 ST->getPointerInfo(), isVolatile,
1191 isNonTemporal, Alignment);
1192 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001193 DAG.RemoveDeadNode(Node, this);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001194 break;
1195 }
Dan Gohman65fd6562011-11-03 21:49:52 +00001196 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001197 break;
1198 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001199 } else {
Dan Gohman65fd6562011-11-03 21:49:52 +00001200 Tmp3 = ST->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001201
Owen Andersone50ed302009-08-10 22:56:29 +00001202 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001203 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001204
Duncan Sands83ec4b62008-06-06 12:08:01 +00001205 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001206 // Promote to a byte-sized store with upper bits zero if not
1207 // storing an integral number of bytes. For example, promote
1208 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001209 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1210 StVT.getStoreSizeInBits());
Nadav Rotema1c415c2011-09-27 10:48:29 +00001211 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Dan Gohman65fd6562011-11-03 21:49:52 +00001212 SDValue Result =
1213 DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1214 NVT, isVolatile, isNonTemporal, Alignment);
1215 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001216 DAG.RemoveDeadNode(Node, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001217 } else if (StWidth & (StWidth - 1)) {
1218 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001219 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001220 unsigned RoundWidth = 1 << Log2_32(StWidth);
1221 assert(RoundWidth < StWidth);
1222 unsigned ExtraWidth = StWidth - RoundWidth;
1223 assert(ExtraWidth < RoundWidth);
1224 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1225 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001226 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1227 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001228 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001229 unsigned IncrementSize;
1230
1231 if (TLI.isLittleEndian()) {
1232 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1233 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001234 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1235 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001236 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001237
1238 // Store the remaining ExtraWidth bits.
1239 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001240 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001241 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001242 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001243 DAG.getConstant(RoundWidth,
1244 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001245 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1246 ST->getPointerInfo().getWithOffset(IncrementSize),
1247 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001248 MinAlign(Alignment, IncrementSize));
1249 } else {
1250 // Big endian - avoid unaligned stores.
1251 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1252 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001253 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001254 DAG.getConstant(ExtraWidth,
1255 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001256 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1257 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001258
1259 // Store the remaining ExtraWidth bits.
1260 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001261 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001262 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001263 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1264 ST->getPointerInfo().getWithOffset(IncrementSize),
1265 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001266 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001267 }
Duncan Sands7e857202008-01-22 07:17:34 +00001268
1269 // The order of the stores doesn't matter.
Dan Gohman65fd6562011-11-03 21:49:52 +00001270 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
1271 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001272 DAG.RemoveDeadNode(Node, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001273 } else {
1274 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1275 Tmp2 != ST->getBasePtr())
Dan Gohman65fd6562011-11-03 21:49:52 +00001276 Node = DAG.UpdateNodeOperands(Node, Tmp1, Tmp3, Tmp2,
1277 ST->getOffset());
Duncan Sands7e857202008-01-22 07:17:34 +00001278
1279 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001280 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001281 case TargetLowering::Legal:
1282 // If this is an unaligned store and the target doesn't support it,
1283 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001284 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001285 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001286 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001287 if (ST->getAlignment() < ABIAlignment)
Dan Gohman65fd6562011-11-03 21:49:52 +00001288 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001289 }
1290 break;
1291 case TargetLowering::Custom:
Dan Gohman65fd6562011-11-03 21:49:52 +00001292 DAG.ReplaceAllUsesWith(SDValue(Node, 0),
1293 TLI.LowerOperation(SDValue(Node, 0), DAG),
1294 this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001295 DAG.RemoveDeadNode(Node, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001296 break;
Dan Gohmane63e5ab2011-07-15 22:51:43 +00001297 case TargetLowering::Expand:
Nadav Roteme9b58d02011-10-15 07:41:10 +00001298 assert(!StVT.isVector() &&
1299 "Vector Stores are handled in LegalizeVectorOps");
Nadav Rotemc2492c22011-06-14 08:11:52 +00001300
Duncan Sands7e857202008-01-22 07:17:34 +00001301 // TRUNCSTORE:i16 i32 -> STORE i16
Dan Gohman75b10042011-07-15 22:39:09 +00001302 assert(TLI.isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001303 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Dan Gohman65fd6562011-11-03 21:49:52 +00001304 SDValue Result =
1305 DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1306 isVolatile, isNonTemporal, Alignment);
1307 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001308 DAG.RemoveDeadNode(Node, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001309 break;
1310 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001311 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001312 }
1313 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001314 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001315 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001316}
1317
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001318SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1319 SDValue Vec = Op.getOperand(0);
1320 SDValue Idx = Op.getOperand(1);
1321 DebugLoc dl = Op.getDebugLoc();
1322 // Store the value to a temporary stack slot, then LOAD the returned part.
1323 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001324 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1325 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001326
1327 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001328 unsigned EltSize =
1329 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001330 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1331 DAG.getConstant(EltSize, Idx.getValueType()));
1332
1333 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1334 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1335 else
1336 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1337
1338 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1339
Eli Friedmanc680ac92009-07-09 22:01:03 +00001340 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001341 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001342 false, false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001343 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001344 MachinePointerInfo(),
1345 Vec.getValueType().getVectorElementType(),
1346 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001347}
1348
David Greenecfe33c42011-01-26 19:13:22 +00001349SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1350 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1351
1352 SDValue Vec = Op.getOperand(0);
1353 SDValue Part = Op.getOperand(1);
1354 SDValue Idx = Op.getOperand(2);
1355 DebugLoc dl = Op.getDebugLoc();
1356
1357 // Store the value to a temporary stack slot, then LOAD the returned part.
1358
1359 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1360 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1361 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1362
1363 // First store the whole vector.
1364 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1365 false, false, 0);
1366
1367 // Then store the inserted part.
1368
1369 // Add the offset to the index.
1370 unsigned EltSize =
1371 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1372
1373 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1374 DAG.getConstant(EltSize, Idx.getValueType()));
1375
1376 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1377 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1378 else
1379 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1380
1381 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1382 StackPtr);
1383
1384 // Store the subvector.
1385 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1386 MachinePointerInfo(), false, false, 0);
1387
1388 // Finally, load the updated vector.
1389 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001390 false, false, false, 0);
David Greenecfe33c42011-01-26 19:13:22 +00001391}
1392
Eli Friedman7ef3d172009-06-06 07:04:42 +00001393SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1394 // We can't handle this case efficiently. Allocate a sufficiently
1395 // aligned object on the stack, store each element into it, then load
1396 // the result as a vector.
1397 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001398 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001399 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001400 DebugLoc dl = Node->getDebugLoc();
1401 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001402 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001403 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001404
1405 // Emit a store of each element to the stack slot.
1406 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001407 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001408 // Store (in the right endianness) the elements to memory.
1409 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1410 // Ignore undef elements.
1411 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1412
1413 unsigned Offset = TypeByteSize*i;
1414
1415 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1416 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1417
Dan Gohman9949dd62010-02-25 20:30:49 +00001418 // If the destination vector element type is narrower than the source
1419 // element type, only store the bits necessary.
1420 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001421 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001422 Node->getOperand(i), Idx,
1423 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001424 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001425 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001426 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001427 Node->getOperand(i), Idx,
1428 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001429 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001430 }
1431
1432 SDValue StoreChain;
1433 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001435 &Stores[0], Stores.size());
1436 else
1437 StoreChain = DAG.getEntryNode();
1438
1439 // Result is a load from the stack slot.
Pete Cooperd752e0f2011-11-08 18:42:53 +00001440 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo,
1441 false, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001442}
1443
Eli Friedman4bc8c712009-05-27 12:20:41 +00001444SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1445 DebugLoc dl = Node->getDebugLoc();
1446 SDValue Tmp1 = Node->getOperand(0);
1447 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001448
1449 // Get the sign bit of the RHS. First obtain a value that has the same
1450 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001451 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001452 EVT FloatVT = Tmp2.getValueType();
1453 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Dan Gohman75b10042011-07-15 22:39:09 +00001454 if (TLI.isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001455 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001456 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001457 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001458 // Store the float to memory, then load the sign part out as an integer.
1459 MVT LoadTy = TLI.getPointerTy();
1460 // First create a temporary that is aligned for both the load and store.
1461 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1462 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001463 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001464 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001465 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001466 if (TLI.isBigEndian()) {
1467 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1468 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001469 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001470 false, false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001471 } else { // Little endian
1472 SDValue LoadPtr = StackPtr;
1473 // The float may be wider than the integer we are going to load. Advance
1474 // the pointer so that the loaded integer will contain the sign bit.
1475 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1476 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1477 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1478 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1479 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001480 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001481 false, false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001482 // Move the sign bit to the top bit of the loaded integer.
1483 unsigned BitShift = LoadTy.getSizeInBits() -
1484 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1485 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1486 if (BitShift)
1487 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001488 DAG.getConstant(BitShift,
1489 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001490 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001491 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001492 // Now get the sign bit proper, by seeing whether the value is negative.
1493 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1494 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1495 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001496 // Get the absolute value of the result.
1497 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1498 // Select between the nabs and abs value based on the sign bit of
1499 // the input.
1500 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1501 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1502 AbsVal);
1503}
1504
Eli Friedman4bc8c712009-05-27 12:20:41 +00001505void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1506 SmallVectorImpl<SDValue> &Results) {
1507 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1508 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1509 " not tell us which reg is the stack pointer!");
1510 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001511 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001512 SDValue Tmp1 = SDValue(Node, 0);
1513 SDValue Tmp2 = SDValue(Node, 1);
1514 SDValue Tmp3 = Node->getOperand(2);
1515 SDValue Chain = Tmp1.getOperand(0);
1516
1517 // Chain the dynamic stack allocation so that it doesn't modify the stack
1518 // pointer when other instructions are using the stack.
1519 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1520
1521 SDValue Size = Tmp2.getOperand(1);
1522 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1523 Chain = SP.getValue(1);
1524 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001525 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001526 if (Align > StackAlign)
1527 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1528 DAG.getConstant(-(uint64_t)Align, VT));
1529 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1530 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1531
1532 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1533 DAG.getIntPtrConstant(0, true), SDValue());
1534
1535 Results.push_back(Tmp1);
1536 Results.push_back(Tmp2);
1537}
1538
Evan Cheng7f042682008-10-15 02:05:31 +00001539/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001540/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001541/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001542void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001543 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001544 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001545 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001546 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001547 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1548 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001549 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001550 case TargetLowering::Legal:
1551 // Nothing to do.
1552 break;
1553 case TargetLowering::Expand: {
1554 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1555 unsigned Opc = 0;
1556 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001557 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001558 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1559 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1560 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1561 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1562 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1563 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1564 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1565 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1566 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1567 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1568 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1569 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001570 // FIXME: Implement more expansions.
1571 }
1572
Dale Johannesenbb5da912009-02-02 20:41:04 +00001573 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1574 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1575 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001576 RHS = SDValue();
1577 CC = SDValue();
1578 break;
1579 }
1580 }
1581}
1582
Chris Lattner1401d152008-01-16 07:45:30 +00001583/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1584/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1585/// a load from the stack slot to DestVT, extending it if needed.
1586/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001587SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001588 EVT SlotVT,
1589 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001590 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001591 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001592 unsigned SrcAlign =
1593 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001594 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001595 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001596
Evan Chengff89dcb2009-10-18 18:16:27 +00001597 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1598 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001599 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001600
Duncan Sands83ec4b62008-06-06 12:08:01 +00001601 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1602 unsigned SlotSize = SlotVT.getSizeInBits();
1603 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001604 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Evan Chengadf97992010-04-15 01:25:27 +00001605 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Chris Lattner1401d152008-01-16 07:45:30 +00001607 // Emit a store to the stack slot. Use a truncstore if the input value is
1608 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001609 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001610
Chris Lattner1401d152008-01-16 07:45:30 +00001611 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001612 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001613 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001614 else {
1615 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001616 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001617 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001618 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001619
Chris Lattner35481892005-12-23 00:16:34 +00001620 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001621 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001622 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001623 false, false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001624
Chris Lattner1401d152008-01-16 07:45:30 +00001625 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00001626 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001627 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001628}
1629
Dan Gohman475871a2008-07-27 21:46:04 +00001630SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001631 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001632 // Create a vector sized/aligned stack slot, store the value to element #0,
1633 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001634 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001635
Evan Chengff89dcb2009-10-18 18:16:27 +00001636 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1637 int SPFI = StackPtrFI->getIndex();
1638
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001639 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1640 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001641 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001642 Node->getValueType(0).getVectorElementType(),
1643 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001644 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001645 MachinePointerInfo::getFixedStack(SPFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001646 false, false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001647}
1648
1649
Chris Lattnerce872152006-03-19 06:31:19 +00001650/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001651/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001652SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001653 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001654 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001655 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001656 EVT VT = Node->getValueType(0);
1657 EVT OpVT = Node->getOperand(0).getValueType();
1658 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001659
1660 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001661 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001662 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001663 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001664 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001665 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001666 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001667 if (V.getOpcode() == ISD::UNDEF)
1668 continue;
1669 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001670 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001671 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001672 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001673
1674 if (!Value1.getNode()) {
1675 Value1 = V;
1676 } else if (!Value2.getNode()) {
1677 if (V != Value1)
1678 Value2 = V;
1679 } else if (V != Value1 && V != Value2) {
1680 MoreThanTwoValues = true;
1681 }
Chris Lattnerce872152006-03-19 06:31:19 +00001682 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001683
Eli Friedman7a5e5552009-06-07 06:52:44 +00001684 if (!Value1.getNode())
1685 return DAG.getUNDEF(VT);
1686
1687 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001688 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001689
Chris Lattner2eb86532006-03-24 07:29:17 +00001690 // If all elements are constants, create a load from the constant pool.
1691 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001692 std::vector<Constant*> CV;
1693 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001694 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001695 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001696 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001697 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001698 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001699 if (OpVT==EltVT)
1700 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1701 else {
1702 // If OpVT and EltVT don't match, EltVT is not legal and the
1703 // element values have been promoted/truncated earlier. Undo this;
1704 // we don't want a v16i8 to become a v16i32 for example.
1705 const ConstantInt *CI = V->getConstantIntValue();
1706 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1707 CI->getZExtValue()));
1708 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001709 } else {
1710 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001711 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001712 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001713 }
1714 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001715 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001716 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001717 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001718 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001719 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001720 false, false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001721 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001722
Eli Friedman7a5e5552009-06-07 06:52:44 +00001723 if (!MoreThanTwoValues) {
1724 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1725 for (unsigned i = 0; i < NumElems; ++i) {
1726 SDValue V = Node->getOperand(i);
1727 if (V.getOpcode() == ISD::UNDEF)
1728 continue;
1729 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1730 }
1731 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001732 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001733 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1734 SDValue Vec2;
1735 if (Value2.getNode())
1736 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1737 else
1738 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001739
Chris Lattner87100e02006-03-20 01:52:29 +00001740 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001741 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001742 }
1743 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001744
Eli Friedman7ef3d172009-06-06 07:04:42 +00001745 // Otherwise, we can't handle this case efficiently.
1746 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001747}
1748
Chris Lattner77e77a62005-01-21 06:05:23 +00001749// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1750// does not fit into a register, return the lo part and set the hi part to the
1751// by-reg argument. If it does fit into a single register, return the result
1752// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001753SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001754 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001755 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00001756 // Legalizing the call will automatically add the previous call to the
1757 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001759
Chris Lattner77e77a62005-01-21 06:05:23 +00001760 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00001761 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00001762 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001763 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001764 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00001765 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001766 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00001767 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00001768 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00001769 }
Bill Wendling056292f2008-09-16 21:48:12 +00001770 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00001771 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001772
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001773 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00001774
1775 // isTailCall may be true since the callee does not reference caller stack
1776 // frame. Check if it's in the right position.
1777 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001778 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00001779 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001780 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001781 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001782 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00001783
Evan Cheng3d2125c2010-11-30 23:55:39 +00001784 if (!CallInfo.second.getNode())
1785 // It's a tailcall, return the chain (which is the DAG root).
1786 return DAG.getRoot();
1787
Eli Friedman74807f22009-05-26 08:55:52 +00001788 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00001789}
1790
Dan Gohmanf316eb72011-05-16 22:09:53 +00001791/// ExpandLibCall - Generate a libcall taking the given operands as arguments
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001792/// and returning a result of type RetVT.
1793SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
1794 const SDValue *Ops, unsigned NumOps,
1795 bool isSigned, DebugLoc dl) {
1796 TargetLowering::ArgListTy Args;
1797 Args.reserve(NumOps);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001798
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001799 TargetLowering::ArgListEntry Entry;
1800 for (unsigned i = 0; i != NumOps; ++i) {
1801 Entry.Node = Ops[i];
1802 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
1803 Entry.isSExt = isSigned;
1804 Entry.isZExt = !isSigned;
1805 Args.push_back(Entry);
1806 }
1807 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1808 TLI.getPointerTy());
Dan Gohmanf316eb72011-05-16 22:09:53 +00001809
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001810 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001811 std::pair<SDValue,SDValue> CallInfo =
1812 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
1813 false, 0, TLI.getLibcallCallingConv(LC), false,
1814 /*isReturnValueUsed=*/true,
1815 Callee, Args, DAG, dl);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001816
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001817 return CallInfo.first;
1818}
1819
Jim Grosbache03262f2010-06-18 21:43:38 +00001820// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
1821// ExpandLibCall except that the first operand is the in-chain.
1822std::pair<SDValue, SDValue>
1823SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
1824 SDNode *Node,
1825 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00001826 SDValue InChain = Node->getOperand(0);
1827
1828 TargetLowering::ArgListTy Args;
1829 TargetLowering::ArgListEntry Entry;
1830 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
1831 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001832 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001833 Entry.Node = Node->getOperand(i);
1834 Entry.Ty = ArgTy;
1835 Entry.isSExt = isSigned;
1836 Entry.isZExt = !isSigned;
1837 Args.push_back(Entry);
1838 }
1839 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1840 TLI.getPointerTy());
1841
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001842 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001843 std::pair<SDValue, SDValue> CallInfo =
1844 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001845 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00001846 /*isReturnValueUsed=*/true,
1847 Callee, Args, DAG, Node->getDebugLoc());
1848
Jim Grosbache03262f2010-06-18 21:43:38 +00001849 return CallInfo;
1850}
1851
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001852SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
1853 RTLIB::Libcall Call_F32,
1854 RTLIB::Libcall Call_F64,
1855 RTLIB::Libcall Call_F80,
1856 RTLIB::Libcall Call_PPCF128) {
1857 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001858 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00001859 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 case MVT::f32: LC = Call_F32; break;
1861 case MVT::f64: LC = Call_F64; break;
1862 case MVT::f80: LC = Call_F80; break;
1863 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001864 }
1865 return ExpandLibCall(LC, Node, false);
1866}
1867
1868SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001869 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001870 RTLIB::Libcall Call_I16,
1871 RTLIB::Libcall Call_I32,
1872 RTLIB::Libcall Call_I64,
1873 RTLIB::Libcall Call_I128) {
1874 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00001876 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001877 case MVT::i8: LC = Call_I8; break;
1878 case MVT::i16: LC = Call_I16; break;
1879 case MVT::i32: LC = Call_I32; break;
1880 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001882 }
1883 return ExpandLibCall(LC, Node, isSigned);
1884}
1885
Evan Cheng65279cb2011-04-16 03:08:26 +00001886/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
1887static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
1888 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001889 RTLIB::Libcall LC;
1890 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1891 default: assert(0 && "Unexpected request for libcall!");
1892 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1893 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1894 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1895 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1896 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
1897 }
1898
Evan Cheng65279cb2011-04-16 03:08:26 +00001899 return TLI.getLibcallName(LC) != 0;
1900}
Evan Cheng8e23e812011-04-01 00:42:02 +00001901
Evan Cheng65279cb2011-04-16 03:08:26 +00001902/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
1903/// needed.
1904static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001905 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00001906 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00001907 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001908 else
Evan Cheng8e23e812011-04-01 00:42:02 +00001909 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001910
Evan Cheng8e23e812011-04-01 00:42:02 +00001911 SDValue Op0 = Node->getOperand(0);
1912 SDValue Op1 = Node->getOperand(1);
1913 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
1914 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
1915 SDNode *User = *UI;
1916 if (User == Node)
1917 continue;
1918 if (User->getOpcode() == OtherOpcode &&
1919 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00001920 User->getOperand(1) == Op1)
1921 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00001922 }
Evan Cheng65279cb2011-04-16 03:08:26 +00001923 return false;
1924}
Evan Cheng8e23e812011-04-01 00:42:02 +00001925
Evan Cheng65279cb2011-04-16 03:08:26 +00001926/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
1927/// pairs.
1928void
1929SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
1930 SmallVectorImpl<SDValue> &Results) {
1931 unsigned Opcode = Node->getOpcode();
1932 bool isSigned = Opcode == ISD::SDIVREM;
1933
1934 RTLIB::Libcall LC;
1935 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1936 default: assert(0 && "Unexpected request for libcall!");
1937 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1938 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1939 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1940 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1941 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00001942 }
1943
1944 // The input chain to this libcall is the entry node of the function.
1945 // Legalizing the call will automatically add the previous call to the
1946 // dependence.
1947 SDValue InChain = DAG.getEntryNode();
1948
1949 EVT RetVT = Node->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001950 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001951
1952 TargetLowering::ArgListTy Args;
1953 TargetLowering::ArgListEntry Entry;
1954 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1955 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001956 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001957 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
1958 Entry.isSExt = isSigned;
1959 Entry.isZExt = !isSigned;
1960 Args.push_back(Entry);
1961 }
1962
1963 // Also pass the return address of the remainder.
1964 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
1965 Entry.Node = FIPtr;
1966 Entry.Ty = RetTy->getPointerTo();
1967 Entry.isSExt = isSigned;
1968 Entry.isZExt = !isSigned;
1969 Args.push_back(Entry);
1970
1971 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1972 TLI.getPointerTy());
1973
Evan Cheng8e23e812011-04-01 00:42:02 +00001974 DebugLoc dl = Node->getDebugLoc();
1975 std::pair<SDValue, SDValue> CallInfo =
1976 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
1977 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
1978 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
1979
Evan Cheng8e23e812011-04-01 00:42:02 +00001980 // Remainder is loaded back from the stack frame.
Dan Gohman65fd6562011-11-03 21:49:52 +00001981 SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001982 MachinePointerInfo(), false, false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00001983 Results.push_back(CallInfo.first);
1984 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00001985}
1986
Chris Lattner22cde6a2006-01-28 08:25:58 +00001987/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
1988/// INT_TO_FP operation of the specified operand when the target requests that
1989/// we expand it. At this point, we know that the result and operand types are
1990/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00001991SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
1992 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00001993 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00001994 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00001996 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00001997
Chris Lattner23594d42008-01-16 07:03:22 +00001998 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002000
Chris Lattner22cde6a2006-01-28 08:25:58 +00002001 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002003 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002004 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002005 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002006 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002007 if (TLI.isLittleEndian())
2008 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Chris Lattner22cde6a2006-01-28 08:25:58 +00002010 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002011 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002012 if (isSigned) {
2013 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002014 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2015 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002016 } else {
2017 Op0Mapped = Op0;
2018 }
2019 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002020 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002021 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002022 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002023 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002024 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002025 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002026 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2027 MachinePointerInfo(),
2028 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002029 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002030 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002031 MachinePointerInfo(), false, false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002032 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002033 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002034 BitsToDouble(0x4330000080000000ULL) :
2035 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002037 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002039 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002040 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002041 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002043 // do nothing
2044 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002046 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002047 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002049 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002050 }
2051 return Result;
2052 }
2053 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002054 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002055
2056 // Implementation of unsigned i64 to f64 following the algorithm in
2057 // __floatundidf in compiler_rt. This implementation has the advantage
2058 // of performing rounding correctly, both in the default rounding mode
2059 // and in all alternate rounding modes.
2060 // TODO: Generalize this for use with other types.
2061 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2062 SDValue TwoP52 =
2063 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2064 SDValue TwoP84PlusTwoP52 =
2065 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2066 SDValue TwoP84 =
2067 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2068
2069 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2070 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2071 DAG.getConstant(32, MVT::i64));
2072 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2073 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002074 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2075 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002076 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2077 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002078 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2079 }
2080
Owen Anderson3a9e7692010-10-05 17:24:05 +00002081 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002082 // TODO: Generalize this for use with other types.
2083 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002084 // For unsigned conversions, convert them to signed conversions using the
2085 // algorithm from the x86_64 __floatundidf in compiler_rt.
2086 if (!isSigned) {
2087 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002088
Owen Anderson95771af2011-02-25 21:41:48 +00002089 SDValue ShiftConst =
2090 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002091 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2092 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2093 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2094 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002095
Owen Anderson3a9e7692010-10-05 17:24:05 +00002096 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2097 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002098
Owen Anderson3a9e7692010-10-05 17:24:05 +00002099 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002100 // select. We happen to get lucky and machinesink does the right
2101 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002102 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002103 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002104 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2105 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2106 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002107
Owen Anderson3a9e7692010-10-05 17:24:05 +00002108 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002109
Jim Grosbach6e992612010-07-02 17:41:59 +00002110 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002111 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2112 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2113 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002114 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002115 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2116 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2117 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2118 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2119 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2120 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002121 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002122 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002123 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002124
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002125 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2126 DAG.getConstant(32, SHVT));
2127 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2128 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2129 SDValue TwoP32 =
2130 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2131 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2132 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2133 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2134 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2135 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2136 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002137 }
2138
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002139 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002140
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002141 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2142 Op0, DAG.getConstant(0, Op0.getValueType()),
2143 ISD::SETLT);
2144 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2145 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2146 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002147
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002148 // If the sign bit of the integer is set, the large number will be treated
2149 // as a negative number. To counteract this, the dynamic code adds an
2150 // offset depending on the data type.
2151 uint64_t FF;
2152 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002153 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002154 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2155 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2156 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2157 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2158 }
2159 if (TLI.isLittleEndian()) FF <<= 32;
2160 Constant *FudgeFactor = ConstantInt::get(
2161 Type::getInt64Ty(*DAG.getContext()), FF);
2162
2163 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2164 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2165 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2166 Alignment = std::min(Alignment, 4u);
2167 SDValue FudgeInReg;
2168 if (DestVT == MVT::f32)
2169 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002170 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002171 false, false, false, Alignment);
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002172 else {
Dan Gohman65fd6562011-11-03 21:49:52 +00002173 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
2174 DAG.getEntryNode(), CPIdx,
2175 MachinePointerInfo::getConstantPool(),
2176 MVT::f32, false, false, Alignment);
2177 HandleSDNode Handle(Load);
2178 LegalizeOp(Load.getNode());
2179 FudgeInReg = Handle.getValue();
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002180 }
2181
2182 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002183}
2184
2185/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2186/// *INT_TO_FP operation of the specified operand when the target requests that
2187/// we promote it. At this point, we know that the result and operand types are
2188/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2189/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002190SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002191 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002192 bool isSigned,
2193 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002194 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002195 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002196
2197 unsigned OpToUse = 0;
2198
2199 // Scan for the appropriate larger type to use.
2200 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002202 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002203
2204 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002205 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2206 OpToUse = ISD::SINT_TO_FP;
2207 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002208 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002209 if (isSigned) continue;
2210
2211 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002212 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2213 OpToUse = ISD::UINT_TO_FP;
2214 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002215 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002216
2217 // Otherwise, try a larger type.
2218 }
2219
2220 // Okay, we found the operation and type to use. Zero extend our input to the
2221 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002222 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002223 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002224 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002225}
2226
2227/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2228/// FP_TO_*INT operation of the specified operand when the target requests that
2229/// we promote it. At this point, we know that the result and operand types are
2230/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2231/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002232SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002233 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002234 bool isSigned,
2235 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002236 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002237 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002238
2239 unsigned OpToUse = 0;
2240
2241 // Scan for the appropriate larger type to use.
2242 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002243 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002244 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002245
Eli Friedman3be2e512009-05-28 03:06:16 +00002246 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002247 OpToUse = ISD::FP_TO_SINT;
2248 break;
2249 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002250
Eli Friedman3be2e512009-05-28 03:06:16 +00002251 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002252 OpToUse = ISD::FP_TO_UINT;
2253 break;
2254 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002255
2256 // Otherwise, try a larger type.
2257 }
2258
Scott Michelfdc40a02009-02-17 22:15:04 +00002259
Chris Lattner27a6c732007-11-24 07:07:01 +00002260 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002261 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002262
Chris Lattner27a6c732007-11-24 07:07:01 +00002263 // Truncate the result of the extended FP_TO_*INT operation to the desired
2264 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002265 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002266}
2267
2268/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2269///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002270SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002271 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002272 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002273 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002274 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002275 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002276 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002277 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2278 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2279 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002280 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002281 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2282 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2283 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2284 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2285 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2286 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2287 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2288 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2289 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002290 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002291 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2292 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2293 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2294 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2295 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2296 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2297 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2298 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2299 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2300 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2301 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2302 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2303 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2304 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2305 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2306 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2307 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2308 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2309 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2310 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2311 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002312 }
2313}
2314
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002315/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002316// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002317static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2318 APInt Val = APInt(NumBits, ByteVal);
2319 unsigned Shift = 8;
2320 for (unsigned i = NumBits; i > 8; i >>= 1) {
2321 Val = (Val << Shift) | Val;
2322 Shift <<= 1;
2323 }
2324 return Val;
2325}
2326
Chris Lattner22cde6a2006-01-28 08:25:58 +00002327/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2328///
Scott Michelfdc40a02009-02-17 22:15:04 +00002329SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002330 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002331 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002332 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002333 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002334 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002335 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002336 unsigned Len = VT.getSizeInBits();
2337
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002338 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2339 "CTPOP not implemented for this type.");
2340
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002341 // This is the "best" algorithm from
2342 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2343
2344 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2345 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2346 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2347 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2348
2349 // v = v - ((v >> 1) & 0x55555555...)
2350 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2351 DAG.getNode(ISD::AND, dl, VT,
2352 DAG.getNode(ISD::SRL, dl, VT, Op,
2353 DAG.getConstant(1, ShVT)),
2354 Mask55));
2355 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2356 Op = DAG.getNode(ISD::ADD, dl, VT,
2357 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2358 DAG.getNode(ISD::AND, dl, VT,
2359 DAG.getNode(ISD::SRL, dl, VT, Op,
2360 DAG.getConstant(2, ShVT)),
2361 Mask33));
2362 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2363 Op = DAG.getNode(ISD::AND, dl, VT,
2364 DAG.getNode(ISD::ADD, dl, VT, Op,
2365 DAG.getNode(ISD::SRL, dl, VT, Op,
2366 DAG.getConstant(4, ShVT))),
2367 Mask0F);
2368 // v = (v * 0x01010101...) >> (Len - 8)
2369 Op = DAG.getNode(ISD::SRL, dl, VT,
2370 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2371 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002372
Chris Lattner22cde6a2006-01-28 08:25:58 +00002373 return Op;
2374 }
2375 case ISD::CTLZ: {
2376 // for now, we do this:
2377 // x = x | (x >> 1);
2378 // x = x | (x >> 2);
2379 // ...
2380 // x = x | (x >>16);
2381 // x = x | (x >>32); // for 64-bit input
2382 // return popcount(~x);
2383 //
2384 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002385 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002386 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002387 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002388 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002389 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002390 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002391 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002392 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002393 Op = DAG.getNOT(dl, Op, VT);
2394 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002395 }
2396 case ISD::CTTZ: {
2397 // for now, we use: { return popcount(~x & (x - 1)); }
2398 // unless the target has ctlz but not ctpop, in which case we use:
2399 // { return 32 - nlz(~x & (x-1)); }
2400 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002401 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002402 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2403 DAG.getNOT(dl, Op, VT),
2404 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002405 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002406 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002407 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2408 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002409 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002410 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002411 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2412 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002413 }
2414 }
2415}
Chris Lattnere34b3962005-01-19 04:19:40 +00002416
Jim Grosbache03262f2010-06-18 21:43:38 +00002417std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2418 unsigned Opc = Node->getOpcode();
2419 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2420 RTLIB::Libcall LC;
2421
2422 switch (Opc) {
2423 default:
2424 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2425 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002426 case ISD::ATOMIC_SWAP:
2427 switch (VT.SimpleTy) {
2428 default: llvm_unreachable("Unexpected value type for atomic!");
2429 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2430 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2431 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2432 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2433 }
2434 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002435 case ISD::ATOMIC_CMP_SWAP:
2436 switch (VT.SimpleTy) {
2437 default: llvm_unreachable("Unexpected value type for atomic!");
2438 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2439 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2440 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2441 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2442 }
2443 break;
2444 case ISD::ATOMIC_LOAD_ADD:
2445 switch (VT.SimpleTy) {
2446 default: llvm_unreachable("Unexpected value type for atomic!");
2447 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2448 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2449 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2450 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2451 }
2452 break;
2453 case ISD::ATOMIC_LOAD_SUB:
2454 switch (VT.SimpleTy) {
2455 default: llvm_unreachable("Unexpected value type for atomic!");
2456 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2457 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2458 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2459 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2460 }
2461 break;
2462 case ISD::ATOMIC_LOAD_AND:
2463 switch (VT.SimpleTy) {
2464 default: llvm_unreachable("Unexpected value type for atomic!");
2465 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2466 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2467 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2468 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2469 }
2470 break;
2471 case ISD::ATOMIC_LOAD_OR:
2472 switch (VT.SimpleTy) {
2473 default: llvm_unreachable("Unexpected value type for atomic!");
2474 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2475 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2476 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2477 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2478 }
2479 break;
2480 case ISD::ATOMIC_LOAD_XOR:
2481 switch (VT.SimpleTy) {
2482 default: llvm_unreachable("Unexpected value type for atomic!");
2483 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2484 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2485 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2486 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2487 }
2488 break;
2489 case ISD::ATOMIC_LOAD_NAND:
2490 switch (VT.SimpleTy) {
2491 default: llvm_unreachable("Unexpected value type for atomic!");
2492 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2493 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2494 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2495 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2496 }
2497 break;
2498 }
2499
2500 return ExpandChainLibCall(LC, Node, false);
2501}
2502
Dan Gohman65fd6562011-11-03 21:49:52 +00002503void SelectionDAGLegalize::ExpandNode(SDNode *Node) {
2504 SmallVector<SDValue, 8> Results;
Eli Friedman8c377c72009-05-27 01:25:56 +00002505 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002506 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002507 switch (Node->getOpcode()) {
2508 case ISD::CTPOP:
2509 case ISD::CTLZ:
2510 case ISD::CTTZ:
2511 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2512 Results.push_back(Tmp1);
2513 break;
2514 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002515 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002516 break;
2517 case ISD::FRAMEADDR:
2518 case ISD::RETURNADDR:
2519 case ISD::FRAME_TO_ARGS_OFFSET:
2520 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2521 break;
2522 case ISD::FLT_ROUNDS_:
2523 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2524 break;
2525 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002526 case ISD::EH_LABEL:
2527 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002528 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002529 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002530 case ISD::EH_SJLJ_DISPATCHSETUP:
2531 // If the target didn't expand these, there's nothing to do, so just
2532 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002533 Results.push_back(Node->getOperand(0));
2534 break;
2535 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002536 // If the target didn't expand this, just return 'zero' and preserve the
2537 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002538 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002539 Results.push_back(Node->getOperand(0));
2540 break;
Eli Friedman14648462011-07-27 22:21:52 +00002541 case ISD::ATOMIC_FENCE:
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002542 case ISD::MEMBARRIER: {
2543 // If the target didn't lower this, lower it to '__sync_synchronize()' call
Eli Friedman14648462011-07-27 22:21:52 +00002544 // FIXME: handle "fence singlethread" more efficiently.
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002545 TargetLowering::ArgListTy Args;
2546 std::pair<SDValue, SDValue> CallResult =
2547 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002548 false, false, false, false, 0, CallingConv::C,
2549 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002550 /*isReturnValueUsed=*/true,
2551 DAG.getExternalSymbol("__sync_synchronize",
2552 TLI.getPointerTy()),
2553 Args, DAG, dl);
2554 Results.push_back(CallResult.second);
2555 break;
2556 }
Eli Friedman069e2ed2011-08-26 02:59:24 +00002557 case ISD::ATOMIC_LOAD: {
2558 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
Eli Friedman331120b2011-09-15 21:20:49 +00002559 SDValue Zero = DAG.getConstant(0, Node->getValueType(0));
Eli Friedman069e2ed2011-08-26 02:59:24 +00002560 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
2561 cast<AtomicSDNode>(Node)->getMemoryVT(),
2562 Node->getOperand(0),
2563 Node->getOperand(1), Zero, Zero,
2564 cast<AtomicSDNode>(Node)->getMemOperand(),
2565 cast<AtomicSDNode>(Node)->getOrdering(),
2566 cast<AtomicSDNode>(Node)->getSynchScope());
2567 Results.push_back(Swap.getValue(0));
2568 Results.push_back(Swap.getValue(1));
2569 break;
2570 }
2571 case ISD::ATOMIC_STORE: {
2572 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
2573 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
2574 cast<AtomicSDNode>(Node)->getMemoryVT(),
2575 Node->getOperand(0),
2576 Node->getOperand(1), Node->getOperand(2),
2577 cast<AtomicSDNode>(Node)->getMemOperand(),
2578 cast<AtomicSDNode>(Node)->getOrdering(),
2579 cast<AtomicSDNode>(Node)->getSynchScope());
2580 Results.push_back(Swap.getValue(1));
2581 break;
2582 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002583 // By default, atomic intrinsics are marked Legal and lowered. Targets
2584 // which don't support them directly, however, may want libcalls, in which
2585 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002586 case ISD::ATOMIC_SWAP:
2587 case ISD::ATOMIC_LOAD_ADD:
2588 case ISD::ATOMIC_LOAD_SUB:
2589 case ISD::ATOMIC_LOAD_AND:
2590 case ISD::ATOMIC_LOAD_OR:
2591 case ISD::ATOMIC_LOAD_XOR:
2592 case ISD::ATOMIC_LOAD_NAND:
2593 case ISD::ATOMIC_LOAD_MIN:
2594 case ISD::ATOMIC_LOAD_MAX:
2595 case ISD::ATOMIC_LOAD_UMIN:
2596 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002597 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002598 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2599 Results.push_back(Tmp.first);
2600 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002601 break;
Evan Chenga8457062010-06-18 22:01:37 +00002602 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002603 case ISD::DYNAMIC_STACKALLOC:
2604 ExpandDYNAMIC_STACKALLOC(Node, Results);
2605 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002606 case ISD::MERGE_VALUES:
2607 for (unsigned i = 0; i < Node->getNumValues(); i++)
2608 Results.push_back(Node->getOperand(i));
2609 break;
2610 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002611 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002612 if (VT.isInteger())
2613 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002614 else {
2615 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002616 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002617 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002618 break;
2619 }
2620 case ISD::TRAP: {
2621 // If this operation is not supported, lower it to 'abort()' call
2622 TargetLowering::ArgListTy Args;
2623 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002624 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002625 false, false, false, false, 0, CallingConv::C,
2626 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002627 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002628 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002629 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002630 Results.push_back(CallResult.second);
2631 break;
2632 }
2633 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002634 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002635 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2636 Node->getValueType(0), dl);
2637 Results.push_back(Tmp1);
2638 break;
2639 case ISD::FP_EXTEND:
2640 Tmp1 = EmitStackConvert(Node->getOperand(0),
2641 Node->getOperand(0).getValueType(),
2642 Node->getValueType(0), dl);
2643 Results.push_back(Tmp1);
2644 break;
2645 case ISD::SIGN_EXTEND_INREG: {
2646 // NOTE: we could fall back on load/store here too for targets without
2647 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002648 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002649 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00002650 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00002651 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002652 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002653 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2654 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002655 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002656 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2657 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002658 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2659 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002660 break;
2661 }
2662 case ISD::FP_ROUND_INREG: {
2663 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002664 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00002665
2666 // NOTE: there is a choice here between constantly creating new stack
2667 // slots and always reusing the same one. We currently always create
2668 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002669 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002670 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2671 Node->getValueType(0), dl);
2672 Results.push_back(Tmp1);
2673 break;
2674 }
2675 case ISD::SINT_TO_FP:
2676 case ISD::UINT_TO_FP:
2677 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2678 Node->getOperand(0), Node->getValueType(0), dl);
2679 Results.push_back(Tmp1);
2680 break;
2681 case ISD::FP_TO_UINT: {
2682 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002683 EVT VT = Node->getOperand(0).getValueType();
2684 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002685 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002686 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2687 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2688 Tmp1 = DAG.getConstantFP(apf, VT);
2689 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2690 Node->getOperand(0),
2691 Tmp1, ISD::SETLT);
2692 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002693 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2694 DAG.getNode(ISD::FSUB, dl, VT,
2695 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002696 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2697 DAG.getConstant(x, NVT));
2698 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2699 Results.push_back(Tmp1);
2700 break;
2701 }
Eli Friedman509150f2009-05-27 07:58:35 +00002702 case ISD::VAARG: {
2703 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002704 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002705 Tmp1 = Node->getOperand(0);
2706 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002707 unsigned Align = Node->getConstantOperandVal(3);
2708
Chris Lattnerecf42c42010-09-21 16:36:31 +00002709 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002710 MachinePointerInfo(V),
2711 false, false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002712 SDValue VAList = VAListLoad;
2713
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002714 if (Align > TLI.getMinStackArgumentAlignment()) {
2715 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2716
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002717 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2718 DAG.getConstant(Align - 1,
2719 TLI.getPointerTy()));
2720
2721 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002722 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002723 TLI.getPointerTy()));
2724 }
2725
Eli Friedman509150f2009-05-27 07:58:35 +00002726 // Increment the pointer, VAList, to the next vaarg
2727 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2728 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002729 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002730 TLI.getPointerTy()));
2731 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002732 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2733 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002734 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002735 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002736 false, false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002737 Results.push_back(Results[0].getValue(1));
2738 break;
2739 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002740 case ISD::VACOPY: {
2741 // This defaults to loading a pointer from the input and storing it to the
2742 // output, returning the chain.
2743 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2744 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2745 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002746 Node->getOperand(2), MachinePointerInfo(VS),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002747 false, false, false, 0);
Chris Lattnerecf42c42010-09-21 16:36:31 +00002748 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2749 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002750 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002751 break;
2752 }
2753 case ISD::EXTRACT_VECTOR_ELT:
2754 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2755 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002756 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002757 Node->getOperand(0));
2758 else
2759 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2760 Results.push_back(Tmp1);
2761 break;
2762 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002763 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002764 break;
David Greenecfe33c42011-01-26 19:13:22 +00002765 case ISD::INSERT_SUBVECTOR:
2766 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2767 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002768 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002769 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002770 break;
2771 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002772 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002773 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002774 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002775 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002776 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2777 Node->getOperand(1),
2778 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002779 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002780 case ISD::VECTOR_SHUFFLE: {
2781 SmallVector<int, 8> Mask;
2782 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2783
Owen Andersone50ed302009-08-10 22:56:29 +00002784 EVT VT = Node->getValueType(0);
2785 EVT EltVT = VT.getVectorElementType();
Dan Gohman75b10042011-07-15 22:39:09 +00002786 if (!TLI.isTypeLegal(EltVT))
Bob Wilson14b21412010-05-19 18:48:32 +00002787 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002788 unsigned NumElems = VT.getVectorNumElements();
2789 SmallVector<SDValue, 8> Ops;
2790 for (unsigned i = 0; i != NumElems; ++i) {
2791 if (Mask[i] < 0) {
2792 Ops.push_back(DAG.getUNDEF(EltVT));
2793 continue;
2794 }
2795 unsigned Idx = Mask[i];
2796 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002797 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2798 Node->getOperand(0),
2799 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002800 else
Bill Wendling775db972009-12-23 00:28:23 +00002801 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2802 Node->getOperand(1),
2803 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002804 }
2805 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2806 Results.push_back(Tmp1);
2807 break;
2808 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002809 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002810 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002811 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2812 // 1 -> Hi
2813 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2814 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00002815 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00002816 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2817 } else {
2818 // 0 -> Lo
2819 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2820 Node->getOperand(0));
2821 }
2822 Results.push_back(Tmp1);
2823 break;
2824 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002825 case ISD::STACKSAVE:
2826 // Expand to CopyFromReg if the target set
2827 // StackPointerRegisterToSaveRestore.
2828 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002829 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2830 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002831 Results.push_back(Results[0].getValue(1));
2832 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002833 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002834 Results.push_back(Node->getOperand(0));
2835 }
2836 break;
2837 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002838 // Expand to CopyToReg if the target set
2839 // StackPointerRegisterToSaveRestore.
2840 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2841 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2842 Node->getOperand(1)));
2843 } else {
2844 Results.push_back(Node->getOperand(0));
2845 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002846 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002847 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002848 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002849 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002850 case ISD::FNEG:
2851 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2852 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2853 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2854 Node->getOperand(0));
2855 Results.push_back(Tmp1);
2856 break;
2857 case ISD::FABS: {
2858 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002859 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002860 Tmp1 = Node->getOperand(0);
2861 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002862 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002863 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002864 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2865 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002866 Results.push_back(Tmp1);
2867 break;
2868 }
2869 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002870 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2871 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002872 break;
2873 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002874 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2875 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002876 break;
2877 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002878 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2879 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002880 break;
2881 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002882 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2883 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002884 break;
2885 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002886 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2887 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002888 break;
2889 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002890 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2891 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002892 break;
2893 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002894 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2895 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002896 break;
2897 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002898 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2899 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002900 break;
2901 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00002902 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2903 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002904 break;
2905 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00002906 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2907 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002908 break;
2909 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00002910 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2911 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002912 break;
2913 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00002914 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
2915 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002916 break;
2917 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00002918 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
2919 RTLIB::NEARBYINT_F64,
2920 RTLIB::NEARBYINT_F80,
2921 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002922 break;
2923 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00002924 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
2925 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002926 break;
2927 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00002928 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
2929 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002930 break;
2931 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00002932 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
2933 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002934 break;
2935 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00002936 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
2937 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002938 break;
Cameron Zwarich33390842011-07-08 21:39:21 +00002939 case ISD::FMA:
2940 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
2941 RTLIB::FMA_F80, RTLIB::FMA_PPCF128));
2942 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00002943 case ISD::FP16_TO_FP32:
2944 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
2945 break;
2946 case ISD::FP32_TO_FP16:
2947 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
2948 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002949 case ISD::ConstantFP: {
2950 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00002951 // Check to see if this FP immediate is already legal.
2952 // If this is a legal constant, turn it into a TargetConstantFP node.
Dan Gohman65fd6562011-11-03 21:49:52 +00002953 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
2954 Results.push_back(ExpandConstantFP(CFP, true));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002955 break;
2956 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00002957 case ISD::EHSELECTION: {
2958 unsigned Reg = TLI.getExceptionSelectorRegister();
2959 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002960 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
2961 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002962 Results.push_back(Results[0].getValue(1));
2963 break;
2964 }
2965 case ISD::EXCEPTIONADDR: {
2966 unsigned Reg = TLI.getExceptionAddressRegister();
2967 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002968 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
2969 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002970 Results.push_back(Results[0].getValue(1));
2971 break;
2972 }
2973 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00002974 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002975 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2976 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
2977 "Don't know how to expand this subtraction!");
2978 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
2979 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00002980 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2981 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002982 break;
2983 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002984 case ISD::UREM:
2985 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00002986 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002987 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002988 bool isSigned = Node->getOpcode() == ISD::SREM;
2989 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
2990 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
2991 Tmp2 = Node->getOperand(0);
2992 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00002993 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
2994 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
2995 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00002996 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
2997 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002998 // X % Y -> X-X/Y*Y
2999 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3000 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3001 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003002 } else if (isSigned)
3003 Tmp1 = ExpandIntLibCall(Node, true,
3004 RTLIB::SREM_I8,
3005 RTLIB::SREM_I16, RTLIB::SREM_I32,
3006 RTLIB::SREM_I64, RTLIB::SREM_I128);
3007 else
3008 Tmp1 = ExpandIntLibCall(Node, false,
3009 RTLIB::UREM_I8,
3010 RTLIB::UREM_I16, RTLIB::UREM_I32,
3011 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003012 Results.push_back(Tmp1);
3013 break;
3014 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003015 case ISD::UDIV:
3016 case ISD::SDIV: {
3017 bool isSigned = Node->getOpcode() == ISD::SDIV;
3018 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003019 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003020 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003021 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3022 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3023 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003024 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3025 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003026 else if (isSigned)
3027 Tmp1 = ExpandIntLibCall(Node, true,
3028 RTLIB::SDIV_I8,
3029 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3030 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3031 else
3032 Tmp1 = ExpandIntLibCall(Node, false,
3033 RTLIB::UDIV_I8,
3034 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3035 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003036 Results.push_back(Tmp1);
3037 break;
3038 }
3039 case ISD::MULHU:
3040 case ISD::MULHS: {
3041 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3042 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003043 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003044 SDVTList VTs = DAG.getVTList(VT, VT);
3045 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3046 "If this wasn't legal, it shouldn't have been created!");
3047 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3048 Node->getOperand(1));
3049 Results.push_back(Tmp1.getValue(1));
3050 break;
3051 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003052 case ISD::SDIVREM:
3053 case ISD::UDIVREM:
3054 // Expand into divrem libcall
3055 ExpandDivRemLibCall(Node, Results);
3056 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003057 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003058 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003059 SDVTList VTs = DAG.getVTList(VT, VT);
3060 // See if multiply or divide can be lowered using two-result operations.
3061 // We just need the low half of the multiply; try both the signed
3062 // and unsigned forms. If the target supports both SMUL_LOHI and
3063 // UMUL_LOHI, form a preference by checking which forms of plain
3064 // MULH it supports.
3065 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3066 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3067 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3068 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3069 unsigned OpToUse = 0;
3070 if (HasSMUL_LOHI && !HasMULHS) {
3071 OpToUse = ISD::SMUL_LOHI;
3072 } else if (HasUMUL_LOHI && !HasMULHU) {
3073 OpToUse = ISD::UMUL_LOHI;
3074 } else if (HasSMUL_LOHI) {
3075 OpToUse = ISD::SMUL_LOHI;
3076 } else if (HasUMUL_LOHI) {
3077 OpToUse = ISD::UMUL_LOHI;
3078 }
3079 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003080 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3081 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003082 break;
3083 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003084 Tmp1 = ExpandIntLibCall(Node, false,
3085 RTLIB::MUL_I8,
3086 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003087 RTLIB::MUL_I64, RTLIB::MUL_I128);
3088 Results.push_back(Tmp1);
3089 break;
3090 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003091 case ISD::SADDO:
3092 case ISD::SSUBO: {
3093 SDValue LHS = Node->getOperand(0);
3094 SDValue RHS = Node->getOperand(1);
3095 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3096 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3097 LHS, RHS);
3098 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003099 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003100
Eli Friedman4bc8c712009-05-27 12:20:41 +00003101 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3102
3103 // LHSSign -> LHS >= 0
3104 // RHSSign -> RHS >= 0
3105 // SumSign -> Sum >= 0
3106 //
3107 // Add:
3108 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3109 // Sub:
3110 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3111 //
3112 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3113 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3114 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3115 Node->getOpcode() == ISD::SADDO ?
3116 ISD::SETEQ : ISD::SETNE);
3117
3118 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3119 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3120
3121 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3122 Results.push_back(Cmp);
3123 break;
3124 }
3125 case ISD::UADDO:
3126 case ISD::USUBO: {
3127 SDValue LHS = Node->getOperand(0);
3128 SDValue RHS = Node->getOperand(1);
3129 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3130 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3131 LHS, RHS);
3132 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003133 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3134 Node->getOpcode () == ISD::UADDO ?
3135 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003136 break;
3137 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003138 case ISD::UMULO:
3139 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003140 EVT VT = Node->getValueType(0);
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003141 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003142 SDValue LHS = Node->getOperand(0);
3143 SDValue RHS = Node->getOperand(1);
3144 SDValue BottomHalf;
3145 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003146 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003147 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3148 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3149 bool isSigned = Node->getOpcode() == ISD::SMULO;
3150 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3151 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3152 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3153 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3154 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3155 RHS);
3156 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003157 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3158 VT.getSizeInBits() * 2))) {
Eli Friedmandb3c1692009-06-16 06:58:29 +00003159 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3160 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3161 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3162 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3163 DAG.getIntPtrConstant(0));
3164 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3165 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003166 } else {
3167 // We can fall back to a libcall with an illegal type for the MUL if we
3168 // have a libcall big enough.
3169 // Also, we can fall back to a division in some cases, but that's a big
3170 // performance hit in the general case.
Eric Christopher38a18262011-01-20 00:29:24 +00003171 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3172 if (WideVT == MVT::i16)
3173 LC = RTLIB::MUL_I16;
3174 else if (WideVT == MVT::i32)
3175 LC = RTLIB::MUL_I32;
3176 else if (WideVT == MVT::i64)
3177 LC = RTLIB::MUL_I64;
3178 else if (WideVT == MVT::i128)
3179 LC = RTLIB::MUL_I128;
3180 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanf316eb72011-05-16 22:09:53 +00003181
3182 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003183 // part.
3184 unsigned LoSize = VT.getSizeInBits();
3185 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
3186 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
3187 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
3188 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
Owen Anderson95771af2011-02-25 21:41:48 +00003189
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003190 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3191 // pre-lowered to the correct types. This all depends upon WideVT not
3192 // being a legal type for the architecture and thus has to be split to
3193 // two arguments.
3194 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3195 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3196 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3197 DAG.getIntPtrConstant(0));
3198 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3199 DAG.getIntPtrConstant(1));
Dan Gohman65fd6562011-11-03 21:49:52 +00003200 // Ret is a node with an illegal type. Because such things are not
3201 // generally permitted during this phase of legalization, delete the
3202 // node. The above EXTRACT_ELEMENT nodes should have been folded.
3203 DAG.DeleteNode(Ret.getNode());
Eli Friedmandb3c1692009-06-16 06:58:29 +00003204 }
Dan Gohmanf316eb72011-05-16 22:09:53 +00003205
Eli Friedmandb3c1692009-06-16 06:58:29 +00003206 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003207 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3208 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003209 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3210 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3211 ISD::SETNE);
3212 } else {
3213 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3214 DAG.getConstant(0, VT), ISD::SETNE);
3215 }
3216 Results.push_back(BottomHalf);
3217 Results.push_back(TopHalf);
3218 break;
3219 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003220 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003221 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003222 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3223 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003224 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003225 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003226 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003227 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003228 break;
3229 }
Eli Friedman509150f2009-05-27 07:58:35 +00003230 case ISD::SELECT:
3231 Tmp1 = Node->getOperand(0);
3232 Tmp2 = Node->getOperand(1);
3233 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003234 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003235 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3236 Tmp2, Tmp3,
3237 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003238 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003239 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3240 DAG.getConstant(0, Tmp1.getValueType()),
3241 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003242 }
Eli Friedman509150f2009-05-27 07:58:35 +00003243 Results.push_back(Tmp1);
3244 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003245 case ISD::BR_JT: {
3246 SDValue Chain = Node->getOperand(0);
3247 SDValue Table = Node->getOperand(1);
3248 SDValue Index = Node->getOperand(2);
3249
Owen Andersone50ed302009-08-10 22:56:29 +00003250 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003251
3252 const TargetData &TD = *TLI.getTargetData();
3253 unsigned EntrySize =
3254 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003255
Chris Lattner071c62f2010-01-25 23:26:13 +00003256 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003257 Index, DAG.getConstant(EntrySize, PTy));
3258 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3259
Owen Anderson23b9b192009-08-12 00:36:31 +00003260 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003261 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003262 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003263 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003264 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003265 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003266 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003267 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003268 // RelocBase can be JumpTable, GOT or some sort of global base.
3269 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3270 TLI.getPICJumpTableRelocBase(Table, DAG));
3271 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003272 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003273 Results.push_back(Tmp1);
3274 break;
3275 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003276 case ISD::BRCOND:
3277 // Expand brcond's setcc into its constituent parts and create a BR_CC
3278 // Node.
3279 Tmp1 = Node->getOperand(0);
3280 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003281 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003282 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003283 Tmp1, Tmp2.getOperand(2),
3284 Tmp2.getOperand(0), Tmp2.getOperand(1),
3285 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003286 } else {
Stuart Hastings88882242011-05-13 00:51:54 +00003287 // We test only the i1 bit. Skip the AND if UNDEF.
3288 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3289 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3290 DAG.getConstant(1, Tmp2.getValueType()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastings88882242011-05-13 00:51:54 +00003292 DAG.getCondCode(ISD::SETNE), Tmp3,
3293 DAG.getConstant(0, Tmp3.getValueType()),
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003294 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003295 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003296 Results.push_back(Tmp1);
3297 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003298 case ISD::SETCC: {
3299 Tmp1 = Node->getOperand(0);
3300 Tmp2 = Node->getOperand(1);
3301 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003302 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003303
3304 // If we expanded the SETCC into an AND/OR, return the new node
3305 if (Tmp2.getNode() == 0) {
3306 Results.push_back(Tmp1);
3307 break;
3308 }
3309
3310 // Otherwise, SETCC for the given comparison type must be completely
3311 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003312 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003313 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3314 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3315 Results.push_back(Tmp1);
3316 break;
3317 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003318 case ISD::SELECT_CC: {
3319 Tmp1 = Node->getOperand(0); // LHS
3320 Tmp2 = Node->getOperand(1); // RHS
3321 Tmp3 = Node->getOperand(2); // True
3322 Tmp4 = Node->getOperand(3); // False
3323 SDValue CC = Node->getOperand(4);
3324
3325 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003326 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003327
3328 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3329 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3330 CC = DAG.getCondCode(ISD::SETNE);
3331 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3332 Tmp3, Tmp4, CC);
3333 Results.push_back(Tmp1);
3334 break;
3335 }
3336 case ISD::BR_CC: {
3337 Tmp1 = Node->getOperand(0); // Chain
3338 Tmp2 = Node->getOperand(2); // LHS
3339 Tmp3 = Node->getOperand(3); // RHS
3340 Tmp4 = Node->getOperand(1); // CC
3341
3342 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003343 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003344
3345 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3346 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3347 Tmp4 = DAG.getCondCode(ISD::SETNE);
3348 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3349 Tmp3, Node->getOperand(4));
3350 Results.push_back(Tmp1);
3351 break;
3352 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003353 case ISD::BUILD_VECTOR:
3354 Results.push_back(ExpandBUILD_VECTOR(Node));
3355 break;
3356 case ISD::SRA:
3357 case ISD::SRL:
3358 case ISD::SHL: {
3359 // Scalarize vector SRA/SRL/SHL.
3360 EVT VT = Node->getValueType(0);
3361 assert(VT.isVector() && "Unable to legalize non-vector shift");
3362 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3363 unsigned NumElem = VT.getVectorNumElements();
3364
3365 SmallVector<SDValue, 8> Scalars;
3366 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
3367 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3368 VT.getScalarType(),
3369 Node->getOperand(0), DAG.getIntPtrConstant(Idx));
3370 SDValue Sh = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3371 VT.getScalarType(),
3372 Node->getOperand(1), DAG.getIntPtrConstant(Idx));
3373 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3374 VT.getScalarType(), Ex, Sh));
3375 }
3376 SDValue Result =
3377 DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
3378 &Scalars[0], Scalars.size());
3379 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00003380 DAG.RemoveDeadNode(Node, this);
Dan Gohman65fd6562011-11-03 21:49:52 +00003381 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 Gohman65fd6562011-11-03 21:49:52 +00003395
3396 // Replace the original node with the legalized result.
Eli Friedman2efa35f2011-11-08 01:25:24 +00003397 if (!Results.empty()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00003398 DAG.ReplaceAllUsesWith(Node, Results.data(), this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00003399 DAG.RemoveDeadNode(Node, this);
3400 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003401}
Dan Gohman65fd6562011-11-03 21:49:52 +00003402
3403void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
3404 SmallVector<SDValue, 8> Results;
Owen Andersone50ed302009-08-10 22:56:29 +00003405 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003406 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003407 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003408 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003409 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003410 }
Owen Andersone50ed302009-08-10 22:56:29 +00003411 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003412 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003413 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003414 switch (Node->getOpcode()) {
3415 case ISD::CTTZ:
3416 case ISD::CTLZ:
3417 case ISD::CTPOP:
3418 // Zero extend the argument.
3419 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3420 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003421 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003422 if (Node->getOpcode() == ISD::CTTZ) {
3423 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003424 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003425 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3426 ISD::SETEQ);
3427 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3428 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3429 } else if (Node->getOpcode() == ISD::CTLZ) {
3430 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3431 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3432 DAG.getConstant(NVT.getSizeInBits() -
3433 OVT.getSizeInBits(), NVT));
3434 }
Bill Wendling775db972009-12-23 00:28:23 +00003435 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003436 break;
3437 case ISD::BSWAP: {
3438 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003439 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003440 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3441 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003442 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003443 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003444 break;
3445 }
3446 case ISD::FP_TO_UINT:
3447 case ISD::FP_TO_SINT:
3448 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3449 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3450 Results.push_back(Tmp1);
3451 break;
3452 case ISD::UINT_TO_FP:
3453 case ISD::SINT_TO_FP:
3454 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3455 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3456 Results.push_back(Tmp1);
3457 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003458 case ISD::AND:
3459 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003460 case ISD::XOR: {
3461 unsigned ExtOp, TruncOp;
3462 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003463 ExtOp = ISD::BITCAST;
3464 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003465 } else {
3466 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003467 ExtOp = ISD::ANY_EXTEND;
3468 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003469 }
3470 // Promote each of the values to the new type.
3471 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3472 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3473 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003474 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3475 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003476 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003477 }
3478 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003479 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003480 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003481 ExtOp = ISD::BITCAST;
3482 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003483 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003484 ExtOp = ISD::ANY_EXTEND;
3485 TruncOp = ISD::TRUNCATE;
3486 } else {
3487 ExtOp = ISD::FP_EXTEND;
3488 TruncOp = ISD::FP_ROUND;
3489 }
3490 Tmp1 = Node->getOperand(0);
3491 // Promote each of the values to the new type.
3492 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3493 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3494 // Perform the larger operation, then round down.
3495 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3496 if (TruncOp != ISD::FP_ROUND)
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 else
Bill Wendling775db972009-12-23 00:28:23 +00003499 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003500 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003501 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003502 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003503 }
Eli Friedman509150f2009-05-27 07:58:35 +00003504 case ISD::VECTOR_SHUFFLE: {
3505 SmallVector<int, 8> Mask;
3506 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3507
3508 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003509 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3510 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003511
3512 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003513 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003514 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003515 Results.push_back(Tmp1);
3516 break;
3517 }
Eli Friedmanad754602009-05-28 03:56:57 +00003518 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003519 unsigned ExtOp = ISD::FP_EXTEND;
3520 if (NVT.isInteger()) {
3521 ISD::CondCode CCCode =
3522 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3523 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003524 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003525 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3526 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003527 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3528 Tmp1, Tmp2, Node->getOperand(2)));
3529 break;
3530 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003531 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003532
3533 // Replace the original node with the legalized result.
Eli Friedman2efa35f2011-11-08 01:25:24 +00003534 if (!Results.empty()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00003535 DAG.ReplaceAllUsesWith(Node, Results.data(), this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00003536 DAG.RemoveDeadNode(Node, this);
3537 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003538}
3539
Chris Lattner3e928bb2005-01-07 07:47:09 +00003540// SelectionDAG::Legalize - This is the entry point for the file.
3541//
Dan Gohman975716a2011-05-16 22:19:54 +00003542void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003543 /// run - This is the main entry point to this class.
3544 ///
Dan Gohman975716a2011-05-16 22:19:54 +00003545 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003546}