blob: 742566919227e2f2206b4400c0dfe317cb0be976 [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,
262 MachinePointerInfo::getConstantPool(), false, false,
263 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);
288 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000289 }
Dan Gohman1b328962011-05-17 22:22:52 +0000290 // Do a (aligned) store to a stack slot, then copy from the stack slot
291 // to the final destination using (unaligned) integer loads and stores.
292 EVT StoredVT = ST->getMemoryVT();
293 EVT RegVT =
294 TLI.getRegisterType(*DAG.getContext(),
295 EVT::getIntegerVT(*DAG.getContext(),
296 StoredVT.getSizeInBits()));
297 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
298 unsigned RegBytes = RegVT.getSizeInBits() / 8;
299 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
300
301 // Make sure the stack slot is also aligned for the register type.
302 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
303
304 // Perform the original store, only redirected to the stack slot.
305 SDValue Store = DAG.getTruncStore(Chain, dl,
306 Val, StackPtr, MachinePointerInfo(),
307 StoredVT, false, false, 0);
308 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
309 SmallVector<SDValue, 8> Stores;
310 unsigned Offset = 0;
311
312 // Do all but one copies using the full register width.
313 for (unsigned i = 1; i < NumRegs; i++) {
314 // Load one integer register's worth from the stack slot.
315 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
316 MachinePointerInfo(),
317 false, false, 0);
318 // Store it to the final location. Remember the store.
319 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
320 ST->getPointerInfo().getWithOffset(Offset),
321 ST->isVolatile(), ST->isNonTemporal(),
322 MinAlign(ST->getAlignment(), Offset)));
323 // Increment the pointers.
324 Offset += RegBytes;
325 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
326 Increment);
327 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
328 }
329
330 // The last store may be partial. Do a truncating store. On big-endian
331 // machines this requires an extending load from the stack slot to ensure
332 // that the bits are in the right place.
333 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
334 8 * (StoredBytes - Offset));
335
336 // Load from the stack slot.
337 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
338 MachinePointerInfo(),
339 MemVT, false, false, 0);
340
341 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
342 ST->getPointerInfo()
343 .getWithOffset(Offset),
344 MemVT, ST->isVolatile(),
345 ST->isNonTemporal(),
346 MinAlign(ST->getAlignment(), Offset)));
347 // The order of the stores doesn't matter - say it with a TokenFactor.
Dan Gohman65fd6562011-11-03 21:49:52 +0000348 SDValue Result =
349 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
350 Stores.size());
351 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
352 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000353 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000354 assert(ST->getMemoryVT().isInteger() &&
355 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000356 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000357 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000358 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000359 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000360 int IncrementSize = NumBits / 8;
361
362 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000363 SDValue ShiftAmount = DAG.getConstant(NumBits,
364 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000365 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000366 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000367
368 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000369 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000370 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000371 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000372 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000373 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000374 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000375 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000376 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000377 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000378 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
379 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000380
Dan Gohman65fd6562011-11-03 21:49:52 +0000381 SDValue Result =
382 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
383 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000384}
385
386/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
Dan Gohman65fd6562011-11-03 21:49:52 +0000387static void
388ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
389 const TargetLowering &TLI,
390 SDValue &ValResult, SDValue &ChainResult) {
Dan Gohman475871a2008-07-27 21:46:04 +0000391 SDValue Chain = LD->getChain();
392 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000393 EVT VT = LD->getValueType(0);
394 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000395 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000396 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000397 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000398 if (TLI.isTypeLegal(intVT)) {
399 // Expand to a (misaligned) integer load of the same size,
400 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000401 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
402 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000403 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000404 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000405 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000406 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000407
Dan Gohman65fd6562011-11-03 21:49:52 +0000408 ValResult = Result;
409 ChainResult = Chain;
410 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000411 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000412
Chris Lattnerecf42c42010-09-21 16:36:31 +0000413 // Copy the value to a (aligned) stack slot using (unaligned) integer
414 // loads and stores, then do a (aligned) load from the stack slot.
415 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
416 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
417 unsigned RegBytes = RegVT.getSizeInBits() / 8;
418 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
419
420 // Make sure the stack slot is also aligned for the register type.
421 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
422
423 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
424 SmallVector<SDValue, 8> Stores;
425 SDValue StackPtr = StackBase;
426 unsigned Offset = 0;
427
428 // Do all but one copies using the full register width.
429 for (unsigned i = 1; i < NumRegs; i++) {
430 // Load one integer register's worth from the original location.
431 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
432 LD->getPointerInfo().getWithOffset(Offset),
433 LD->isVolatile(), LD->isNonTemporal(),
434 MinAlign(LD->getAlignment(), Offset));
435 // Follow the load with a store to the stack slot. Remember the store.
436 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000437 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000438 // Increment the pointers.
439 Offset += RegBytes;
440 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
441 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
442 Increment);
443 }
444
445 // The last copy may be partial. Do an extending load.
446 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
447 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000448 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000449 LD->getPointerInfo().getWithOffset(Offset),
450 MemVT, LD->isVolatile(),
451 LD->isNonTemporal(),
452 MinAlign(LD->getAlignment(), Offset));
453 // Follow the load with a store to the stack slot. Remember the store.
454 // On big-endian machines this requires a truncating store to ensure
455 // that the bits end up in the right place.
456 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
457 MachinePointerInfo(), MemVT,
458 false, false, 0));
459
460 // The order of the stores doesn't matter - say it with a TokenFactor.
461 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
462 Stores.size());
463
464 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000465 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000466 MachinePointerInfo(), LoadedVT, false, false, 0);
467
468 // Callers expect a MERGE_VALUES node.
Dan Gohman65fd6562011-11-03 21:49:52 +0000469 ValResult = Load;
470 ChainResult = TF;
471 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000472 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000473 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000474 "Unaligned load of unsupported type.");
475
Dale Johannesen8155d642008-02-27 22:36:00 +0000476 // Compute the new VT that is half the size of the old one. This is an
477 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000478 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000479 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000480 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000481 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000482
Chris Lattnere400af82007-11-19 21:38:03 +0000483 unsigned Alignment = LD->getAlignment();
484 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000485 ISD::LoadExtType HiExtType = LD->getExtensionType();
486
487 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
488 if (HiExtType == ISD::NON_EXTLOAD)
489 HiExtType = ISD::ZEXTLOAD;
490
491 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000492 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000493 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000494 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000495 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000496 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000497 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000498 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000499 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000500 LD->getPointerInfo().getWithOffset(IncrementSize),
501 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000502 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000503 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000504 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000505 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000506 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000507 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000508 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000509 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000510 LD->getPointerInfo().getWithOffset(IncrementSize),
511 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000512 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000513 }
514
515 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000516 SDValue ShiftAmount = DAG.getConstant(NumBits,
517 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000518 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
519 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000520
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000522 Hi.getValue(1));
523
Dan Gohman65fd6562011-11-03 21:49:52 +0000524 ValResult = Result;
525 ChainResult = TF;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000526}
Evan Cheng912095b2007-01-04 21:56:39 +0000527
Nate Begeman68679912008-04-25 18:07:40 +0000528/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
529/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
530/// is necessary to spill the vector being inserted into to memory, perform
531/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000532SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000533PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
534 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000535 SDValue Tmp1 = Vec;
536 SDValue Tmp2 = Val;
537 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000538
Nate Begeman68679912008-04-25 18:07:40 +0000539 // If the target doesn't support this, we have to spill the input vector
540 // to a temporary stack slot, update the element, then reload it. This is
541 // badness. We could also load the value into a vector register (either
542 // with a "move to register" or "extload into register" instruction, then
543 // permute it into place, if the idx is a constant and if the idx is
544 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000545 EVT VT = Tmp1.getValueType();
546 EVT EltVT = VT.getVectorElementType();
547 EVT IdxVT = Tmp3.getValueType();
548 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000549 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000550
Evan Chengff89dcb2009-10-18 18:16:27 +0000551 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
552
Nate Begeman68679912008-04-25 18:07:40 +0000553 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000554 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000555 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000556 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000557
558 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000559 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000560 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000561 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000562 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000563 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
564 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000565 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000566 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000567 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000568 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000569 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000570 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000571}
572
Mon P Wange9f10152008-12-09 05:46:39 +0000573
Eli Friedman3f727d62009-05-27 02:16:40 +0000574SDValue SelectionDAGLegalize::
575ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
576 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
577 // SCALAR_TO_VECTOR requires that the type of the value being inserted
578 // match the element type of the vector being created, except for
579 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000580 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000581 if (Val.getValueType() == EltVT ||
582 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
583 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
584 Vec.getValueType(), Val);
585
586 unsigned NumElts = Vec.getValueType().getVectorNumElements();
587 // We generate a shuffle of InVec and ScVec, so the shuffle mask
588 // should be 0,1,2,3,4,5... with the appropriate element replaced with
589 // elt 0 of the RHS.
590 SmallVector<int, 8> ShufOps;
591 for (unsigned i = 0; i != NumElts; ++i)
592 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
593
594 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
595 &ShufOps[0]);
596 }
597 }
598 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
599}
600
Eli Friedman7ef3d172009-06-06 07:04:42 +0000601SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
602 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
603 // FIXME: We shouldn't do this for TargetConstantFP's.
604 // FIXME: move this to the DAG Combiner! Note that we can't regress due
605 // to phase ordering between legalized code and the dag combiner. This
606 // probably means that we need to integrate dag combiner and legalizer
607 // together.
608 // We generally can't do this one for long doubles.
609 SDValue Tmp1 = ST->getChain();
610 SDValue Tmp2 = ST->getBasePtr();
611 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000612 unsigned Alignment = ST->getAlignment();
613 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000614 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000615 DebugLoc dl = ST->getDebugLoc();
616 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 if (CFP->getValueType(0) == MVT::f32 &&
Dan Gohman75b10042011-07-15 22:39:09 +0000618 TLI.isTypeLegal(MVT::i32)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000619 Tmp3 = DAG.getConstant(CFP->getValueAPF().
620 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000622 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
623 isVolatile, isNonTemporal, Alignment);
624 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000625
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000626 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000627 // If this target supports 64-bit registers, do a single 64-bit store.
Dan Gohman75b10042011-07-15 22:39:09 +0000628 if (TLI.isTypeLegal(MVT::i64)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000629 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000631 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
632 isVolatile, isNonTemporal, Alignment);
633 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000634
Dan Gohman75b10042011-07-15 22:39:09 +0000635 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000636 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
637 // stores. If the target supports neither 32- nor 64-bits, this
638 // xform is certainly not worth it.
639 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000640 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000642 if (TLI.isBigEndian()) std::swap(Lo, Hi);
643
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000644 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
645 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000646 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
647 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000648 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
649 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000650 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000651
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000653 }
654 }
655 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000656 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000657}
658
Dan Gohman6a109f92011-07-15 21:42:20 +0000659/// LegalizeOp - Return a legal replacement for the given operation, with
660/// all legal operands.
Dan Gohman65fd6562011-11-03 21:49:52 +0000661void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
662 if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
663 return;
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000665 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000666
Eli Friedman1fde9c52009-05-24 02:46:31 +0000667 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000668 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
669 TargetLowering::TypeLegal &&
Eli Friedman1fde9c52009-05-24 02:46:31 +0000670 "Unexpected illegal type!");
671
672 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000673 assert((TLI.getTypeAction(*DAG.getContext(),
674 Node->getOperand(i).getValueType()) ==
675 TargetLowering::TypeLegal ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000676 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
677 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000678
Dan Gohman475871a2008-07-27 21:46:04 +0000679 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +0000680 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000681
Eli Friedman8c377c72009-05-27 01:25:56 +0000682 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000683 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000684 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000685 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000686 case ISD::INTRINSIC_W_CHAIN:
687 case ISD::INTRINSIC_WO_CHAIN:
688 case ISD::INTRINSIC_VOID:
689 case ISD::VAARG:
690 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000692 break;
693 case ISD::SINT_TO_FP:
694 case ISD::UINT_TO_FP:
695 case ISD::EXTRACT_VECTOR_ELT:
696 Action = TLI.getOperationAction(Node->getOpcode(),
697 Node->getOperand(0).getValueType());
698 break;
699 case ISD::FP_ROUND_INREG:
700 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000701 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000702 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
703 break;
704 }
Eli Friedman327236c2011-08-24 20:50:09 +0000705 case ISD::ATOMIC_STORE: {
706 Action = TLI.getOperationAction(Node->getOpcode(),
707 Node->getOperand(2).getValueType());
708 break;
709 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000710 case ISD::SELECT_CC:
711 case ISD::SETCC:
712 case ISD::BR_CC: {
713 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
714 Node->getOpcode() == ISD::SETCC ? 2 : 1;
715 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000716 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000717 ISD::CondCode CCCode =
718 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
719 Action = TLI.getCondCodeAction(CCCode, OpVT);
720 if (Action == TargetLowering::Legal) {
721 if (Node->getOpcode() == ISD::SELECT_CC)
722 Action = TLI.getOperationAction(Node->getOpcode(),
723 Node->getValueType(0));
724 else
725 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
726 }
727 break;
728 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000729 case ISD::LOAD:
730 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000731 // FIXME: Model these properly. LOAD and STORE are complicated, and
732 // STORE expects the unlegalized operand in some cases.
733 SimpleFinishLegalizing = false;
734 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000735 case ISD::CALLSEQ_START:
736 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000737 // FIXME: This shouldn't be necessary. These nodes have special properties
738 // dealing with the recursive nature of legalization. Removing this
739 // special case should be done as part of making LegalizeDAG non-recursive.
740 SimpleFinishLegalizing = false;
741 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000742 case ISD::EXTRACT_ELEMENT:
743 case ISD::FLT_ROUNDS_:
744 case ISD::SADDO:
745 case ISD::SSUBO:
746 case ISD::UADDO:
747 case ISD::USUBO:
748 case ISD::SMULO:
749 case ISD::UMULO:
750 case ISD::FPOWI:
751 case ISD::MERGE_VALUES:
752 case ISD::EH_RETURN:
753 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000754 case ISD::EH_SJLJ_SETJMP:
755 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000756 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000757 // These operations lie about being legal: when they claim to be legal,
758 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000759 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
760 if (Action == TargetLowering::Legal)
761 Action = TargetLowering::Expand;
762 break;
Duncan Sands4a544a72011-09-06 13:37:06 +0000763 case ISD::INIT_TRAMPOLINE:
764 case ISD::ADJUST_TRAMPOLINE:
Eli Friedman8c377c72009-05-27 01:25:56 +0000765 case ISD::FRAMEADDR:
766 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000767 // These operations lie about being legal: when they claim to be legal,
768 // they should actually be custom-lowered.
769 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
770 if (Action == TargetLowering::Legal)
771 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000772 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000773 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000774 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000775 Action = TargetLowering::Legal;
776 } else {
777 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000778 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000779 break;
780 }
781
782 if (SimpleFinishLegalizing) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000783 SmallVector<SDValue, 8> Ops;
Eli Friedman8c377c72009-05-27 01:25:56 +0000784 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman65fd6562011-11-03 21:49:52 +0000785 Ops.push_back(Node->getOperand(i));
Eli Friedman8c377c72009-05-27 01:25:56 +0000786 switch (Node->getOpcode()) {
787 default: break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000788 case ISD::SHL:
789 case ISD::SRL:
790 case ISD::SRA:
791 case ISD::ROTL:
792 case ISD::ROTR:
793 // Legalizing shifts/rotates requires adjusting the shift amount
794 // to the appropriate width.
Dan Gohman65fd6562011-11-03 21:49:52 +0000795 if (!Ops[1].getValueType().isVector()) {
796 SDValue SAO = DAG.getShiftAmountOperand(Ops[0].getValueType(), Ops[1]);
797 HandleSDNode Handle(SAO);
798 LegalizeOp(SAO.getNode());
799 Ops[1] = Handle.getValue();
800 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000801 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000802 case ISD::SRL_PARTS:
803 case ISD::SRA_PARTS:
804 case ISD::SHL_PARTS:
805 // Legalizing shifts/rotates requires adjusting the shift amount
806 // to the appropriate width.
Dan Gohman65fd6562011-11-03 21:49:52 +0000807 if (!Ops[2].getValueType().isVector()) {
808 SDValue SAO = DAG.getShiftAmountOperand(Ops[0].getValueType(), Ops[2]);
809 HandleSDNode Handle(SAO);
810 LegalizeOp(SAO.getNode());
811 Ops[2] = Handle.getValue();
812 }
Dan Gohman2c9489d2009-08-18 23:52:48 +0000813 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000814 }
815
Dan Gohman65fd6562011-11-03 21:49:52 +0000816 SDNode *NewNode = DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
817 if (NewNode != Node) {
818 DAG.ReplaceAllUsesWith(Node, NewNode, this);
819 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
820 DAG.TransferDbgValues(SDValue(Node, i), SDValue(NewNode, i));
821 DAG.RemoveDeadNode(Node, this);
822 Node = NewNode;
823 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000824 switch (Action) {
825 case TargetLowering::Legal:
Dan Gohman65fd6562011-11-03 21:49:52 +0000826 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000827 case TargetLowering::Custom:
828 // FIXME: The handling for custom lowering with multiple results is
829 // a complete mess.
Dan Gohman65fd6562011-11-03 21:49:52 +0000830 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Eli Friedman8c377c72009-05-27 01:25:56 +0000831 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000832 SmallVector<SDValue, 8> ResultVals;
Eli Friedman8c377c72009-05-27 01:25:56 +0000833 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
834 if (e == 1)
835 ResultVals.push_back(Tmp1);
836 else
837 ResultVals.push_back(Tmp1.getValue(i));
838 }
Dan Gohman65fd6562011-11-03 21:49:52 +0000839 if (Tmp1.getNode() != Node || Tmp1.getResNo() != 0) {
840 DAG.ReplaceAllUsesWith(Node, ResultVals.data(), this);
841 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
842 DAG.TransferDbgValues(SDValue(Node, i), ResultVals[i]);
843 DAG.RemoveDeadNode(Node, this);
844 }
845 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000846 }
847
848 // FALL THROUGH
849 case TargetLowering::Expand:
Dan Gohman65fd6562011-11-03 21:49:52 +0000850 ExpandNode(Node);
851 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000852 case TargetLowering::Promote:
Dan Gohman65fd6562011-11-03 21:49:52 +0000853 PromoteNode(Node);
854 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000855 }
856 }
857
858 switch (Node->getOpcode()) {
859 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000860#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +0000861 dbgs() << "NODE: ";
862 Node->dump( &DAG);
863 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000864#endif
Chris Lattner35a38932010-04-07 23:47:51 +0000865 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000866
Dan Gohman65fd6562011-11-03 21:49:52 +0000867 case ISD::CALLSEQ_START:
Dan Gohman6f3ddef2011-10-29 00:41:52 +0000868 case ISD::CALLSEQ_END:
Dan Gohman65fd6562011-11-03 21:49:52 +0000869 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000870 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +0000871 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman65fd6562011-11-03 21:49:52 +0000872 Tmp1 = LD->getChain(); // Legalize the chain.
873 Tmp2 = LD->getBasePtr(); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000874
Evan Cheng466685d2006-10-09 20:57:25 +0000875 ISD::LoadExtType ExtType = LD->getExtensionType();
876 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +0000877 EVT VT = Node->getValueType(0);
Dan Gohman65fd6562011-11-03 21:49:52 +0000878 Node = DAG.UpdateNodeOperands(Node, Tmp1, Tmp2, LD->getOffset());
879 Tmp3 = SDValue(Node, 0);
880 Tmp4 = SDValue(Node, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000881
Evan Cheng466685d2006-10-09 20:57:25 +0000882 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +0000883 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000884 case TargetLowering::Legal:
885 // If this is an unaligned load and the target doesn't support it,
886 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +0000887 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000888 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
Evan Chenge96507c2009-08-15 08:38:52 +0000889 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000890 if (LD->getAlignment() < ABIAlignment){
Dan Gohman65fd6562011-11-03 21:49:52 +0000891 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
892 DAG, TLI, Tmp3, Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000893 }
894 }
895 break;
Evan Cheng466685d2006-10-09 20:57:25 +0000896 case TargetLowering::Custom:
897 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000898 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000899 Tmp3 = Tmp1;
900 Tmp4 = Tmp1.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000901 }
Evan Cheng466685d2006-10-09 20:57:25 +0000902 break;
903 case TargetLowering::Promote: {
904 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000905 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +0000906 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +0000907 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000908
Chris Lattnerecf42c42010-09-21 16:36:31 +0000909 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000910 LD->isVolatile(), LD->isNonTemporal(),
911 LD->getAlignment());
Dan Gohman65fd6562011-11-03 21:49:52 +0000912 Tmp3 = DAG.getNode(ISD::BITCAST, dl, VT, Tmp1);
913 Tmp4 = Tmp1.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000914 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000915 }
Evan Cheng466685d2006-10-09 20:57:25 +0000916 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000917 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +0000918 // legalized both of them.
Dan Gohman65fd6562011-11-03 21:49:52 +0000919 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp3);
920 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp4);
921 return;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000922 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000923
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000924 EVT SrcVT = LD->getMemoryVT();
925 unsigned SrcWidth = SrcVT.getSizeInBits();
926 unsigned Alignment = LD->getAlignment();
927 bool isVolatile = LD->isVolatile();
928 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000929
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000930 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
931 // Some targets pretend to have an i1 loading operation, and actually
932 // load an i8. This trick is correct for ZEXTLOAD because the top 7
933 // bits are guaranteed to be zero; it helps the optimizers understand
934 // that these bits are zero. It is also useful for EXTLOAD, since it
935 // tells the optimizers that those bits are undefined. It would be
936 // nice to have an effective generic way of getting these benefits...
937 // Until such a way is found, don't insist on promoting i1 here.
938 (SrcVT != MVT::i1 ||
939 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
940 // Promote to a byte-sized load if not loading an integral number of
941 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
942 unsigned NewWidth = SrcVT.getStoreSizeInBits();
943 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
944 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000945
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000946 // The extra bits are guaranteed to be zero, since we stored them that
947 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000948
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000949 ISD::LoadExtType NewExtType =
950 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000951
Dan Gohman65fd6562011-11-03 21:49:52 +0000952 SDValue Result =
953 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
954 Tmp1, Tmp2, LD->getPointerInfo(),
955 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000956
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000957 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000958
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000959 if (ExtType == ISD::SEXTLOAD)
960 // Having the top bits zero doesn't help when sign extending.
961 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
962 Result.getValueType(),
963 Result, DAG.getValueType(SrcVT));
964 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
965 // All the top bits are guaranteed to be zero - inform the optimizers.
966 Result = DAG.getNode(ISD::AssertZext, dl,
967 Result.getValueType(), Result,
968 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000969
Dan Gohman65fd6562011-11-03 21:49:52 +0000970 Tmp1 = Result;
971 Tmp2 = Ch;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000972 } else if (SrcWidth & (SrcWidth - 1)) {
973 // If not loading a power-of-2 number of bits, expand as two loads.
974 assert(!SrcVT.isVector() && "Unsupported extload!");
975 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
976 assert(RoundWidth < SrcWidth);
977 unsigned ExtraWidth = SrcWidth - RoundWidth;
978 assert(ExtraWidth < RoundWidth);
979 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
980 "Load size not an integral number of bytes!");
981 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
982 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
983 SDValue Lo, Hi, Ch;
984 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000985
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000986 if (TLI.isLittleEndian()) {
987 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
988 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000989 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000990 Tmp1, Tmp2,
991 LD->getPointerInfo(), RoundVT, isVolatile,
992 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000993
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000994 // Load the remaining ExtraWidth bits.
995 IncrementSize = RoundWidth / 8;
996 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
997 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000998 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000999 LD->getPointerInfo().getWithOffset(IncrementSize),
1000 ExtraVT, isVolatile, isNonTemporal,
1001 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001002
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001003 // Build a factor node to remember that this load is independent of
1004 // the other one.
1005 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1006 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001007
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001008 // Move the top bits to the right place.
1009 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001010 DAG.getConstant(RoundWidth,
1011 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001012
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001013 // Join the hi and lo parts.
Dan Gohman65fd6562011-11-03 21:49:52 +00001014 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001015 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001016 // Big endian - avoid unaligned loads.
1017 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1018 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001019 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001020 LD->getPointerInfo(), RoundVT, isVolatile,
1021 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001022
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001023 // Load the remaining ExtraWidth bits.
1024 IncrementSize = RoundWidth / 8;
1025 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1026 DAG.getIntPtrConstant(IncrementSize));
1027 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001028 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001029 LD->getPointerInfo().getWithOffset(IncrementSize),
1030 ExtraVT, isVolatile, isNonTemporal,
1031 MinAlign(Alignment, IncrementSize));
1032
1033 // Build a factor node to remember that this load is independent of
1034 // the other one.
1035 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1036 Hi.getValue(1));
1037
1038 // Move the top bits to the right place.
1039 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001040 DAG.getConstant(ExtraWidth,
1041 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001042
1043 // Join the hi and lo parts.
Dan Gohman65fd6562011-11-03 21:49:52 +00001044 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001045 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001046
Dan Gohman65fd6562011-11-03 21:49:52 +00001047 Tmp2 = Ch;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001048 } else {
1049 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1050 default: assert(0 && "This action is not supported yet!");
1051 case TargetLowering::Custom:
1052 isCustom = true;
1053 // FALLTHROUGH
1054 case TargetLowering::Legal:
Dan Gohman65fd6562011-11-03 21:49:52 +00001055 Node = DAG.UpdateNodeOperands(Node,
1056 Tmp1, Tmp2, LD->getOffset());
1057 Tmp1 = SDValue(Node, 0);
1058 Tmp2 = SDValue(Node, 1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001059
1060 if (isCustom) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001061 Tmp3 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001062 if (Tmp3.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001063 Tmp1 = Tmp3;
1064 Tmp2 = Tmp3.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001065 }
1066 } else {
1067 // If this is an unaligned load and the target doesn't support it,
1068 // expand it.
1069 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001070 Type *Ty =
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001071 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1072 unsigned ABIAlignment =
1073 TLI.getTargetData()->getABITypeAlignment(Ty);
1074 if (LD->getAlignment() < ABIAlignment){
Dan Gohman65fd6562011-11-03 21:49:52 +00001075 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
1076 DAG, TLI, Tmp1, Tmp2);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001077 }
1078 }
1079 }
1080 break;
1081 case TargetLowering::Expand:
Dan Gohman75b10042011-07-15 22:39:09 +00001082 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001083 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1084 LD->getPointerInfo(),
1085 LD->isVolatile(), LD->isNonTemporal(),
1086 LD->getAlignment());
1087 unsigned ExtendOp;
1088 switch (ExtType) {
1089 case ISD::EXTLOAD:
1090 ExtendOp = (SrcVT.isFloatingPoint() ?
1091 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1092 break;
1093 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1094 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1095 default: llvm_unreachable("Unexpected extend load type!");
1096 }
Dan Gohman65fd6562011-11-03 21:49:52 +00001097 Tmp1 = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1098 Tmp2 = Load.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001099 break;
1100 }
Nadav Rotemc2492c22011-06-14 08:11:52 +00001101
Nadav Roteme9b58d02011-10-15 07:41:10 +00001102 assert(!SrcVT.isVector() &&
1103 "Vector Loads are handled in LegalizeVectorOps");
Nadav Rotemc2492c22011-06-14 08:11:52 +00001104
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001105 // FIXME: This does not work for vectors on most targets. Sign- and
1106 // zero-extend operations are currently folded into extending loads,
1107 // whether they are legal or not, and then we end up here without any
1108 // support for legalizing them.
1109 assert(ExtType != ISD::EXTLOAD &&
1110 "EXTLOAD should always be supported!");
1111 // Turn the unsupported load into an EXTLOAD followed by an explicit
1112 // zero/sign extend inreg.
Dan Gohman65fd6562011-11-03 21:49:52 +00001113 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
1114 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1115 LD->isVolatile(), LD->isNonTemporal(),
1116 LD->getAlignment());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001117 SDValue ValRes;
1118 if (ExtType == ISD::SEXTLOAD)
1119 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1120 Result.getValueType(),
1121 Result, DAG.getValueType(SrcVT));
1122 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001123 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Dan Gohman65fd6562011-11-03 21:49:52 +00001124 Tmp1 = ValRes;
1125 Tmp2 = Result.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001126 break;
1127 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001128 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001129
1130 // Since loads produce two values, make sure to remember that we legalized
1131 // both of them.
Dan Gohman65fd6562011-11-03 21:49:52 +00001132 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp1);
1133 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp2);
1134 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001135 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001136 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001137 StoreSDNode *ST = cast<StoreSDNode>(Node);
Dan Gohman65fd6562011-11-03 21:49:52 +00001138 Tmp1 = ST->getChain();
1139 Tmp2 = ST->getBasePtr();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001140 unsigned Alignment = ST->getAlignment();
1141 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001142 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001143
Evan Cheng8b2794a2006-10-13 21:14:26 +00001144 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001145 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001146 DAG.ReplaceAllUsesWith(ST, OptStore, this);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001147 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001148 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001149
Eli Friedman957bffa2009-05-24 08:42:01 +00001150 {
Dan Gohman65fd6562011-11-03 21:49:52 +00001151 Tmp3 = ST->getValue();
1152 Node = DAG.UpdateNodeOperands(Node,
1153 Tmp1, Tmp3, Tmp2,
1154 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001155
Owen Andersone50ed302009-08-10 22:56:29 +00001156 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001157 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001158 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001159 case TargetLowering::Legal:
1160 // If this is an unaligned store and the target doesn't support it,
1161 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001162 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001163 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001164 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001165 if (ST->getAlignment() < ABIAlignment)
Dan Gohman65fd6562011-11-03 21:49:52 +00001166 ExpandUnalignedStore(cast<StoreSDNode>(Node),
1167 DAG, TLI, this);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001168 }
1169 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001170 case TargetLowering::Custom:
Dan Gohman65fd6562011-11-03 21:49:52 +00001171 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
1172 if (Tmp1.getNode())
1173 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Tmp1, this);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001174 break;
Dan Gohman65fd6562011-11-03 21:49:52 +00001175 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001176 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001177 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001178 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dan Gohman65fd6562011-11-03 21:49:52 +00001179 SDValue Result =
1180 DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
1181 ST->getPointerInfo(), isVolatile,
1182 isNonTemporal, Alignment);
1183 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001184 break;
1185 }
Dan Gohman65fd6562011-11-03 21:49:52 +00001186 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001187 break;
1188 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001189 } else {
Dan Gohman65fd6562011-11-03 21:49:52 +00001190 Tmp3 = ST->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001191
Owen Andersone50ed302009-08-10 22:56:29 +00001192 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001193 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001194
Duncan Sands83ec4b62008-06-06 12:08:01 +00001195 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001196 // Promote to a byte-sized store with upper bits zero if not
1197 // storing an integral number of bytes. For example, promote
1198 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001199 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1200 StVT.getStoreSizeInBits());
Nadav Rotema1c415c2011-09-27 10:48:29 +00001201 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Dan Gohman65fd6562011-11-03 21:49:52 +00001202 SDValue Result =
1203 DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1204 NVT, isVolatile, isNonTemporal, Alignment);
1205 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001206 } else if (StWidth & (StWidth - 1)) {
1207 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001208 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001209 unsigned RoundWidth = 1 << Log2_32(StWidth);
1210 assert(RoundWidth < StWidth);
1211 unsigned ExtraWidth = StWidth - RoundWidth;
1212 assert(ExtraWidth < RoundWidth);
1213 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1214 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001215 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1216 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001217 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001218 unsigned IncrementSize;
1219
1220 if (TLI.isLittleEndian()) {
1221 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1222 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001223 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1224 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001225 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001226
1227 // Store the remaining ExtraWidth bits.
1228 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001229 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001230 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001231 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001232 DAG.getConstant(RoundWidth,
1233 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001234 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1235 ST->getPointerInfo().getWithOffset(IncrementSize),
1236 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001237 MinAlign(Alignment, IncrementSize));
1238 } else {
1239 // Big endian - avoid unaligned stores.
1240 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1241 // Store the top RoundWidth bits.
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(ExtraWidth,
1244 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001245 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1246 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001247
1248 // Store the remaining ExtraWidth bits.
1249 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001250 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001251 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001252 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1253 ST->getPointerInfo().getWithOffset(IncrementSize),
1254 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001255 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001256 }
Duncan Sands7e857202008-01-22 07:17:34 +00001257
1258 // The order of the stores doesn't matter.
Dan Gohman65fd6562011-11-03 21:49:52 +00001259 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
1260 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001261 } else {
1262 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1263 Tmp2 != ST->getBasePtr())
Dan Gohman65fd6562011-11-03 21:49:52 +00001264 Node = DAG.UpdateNodeOperands(Node, Tmp1, Tmp3, Tmp2,
1265 ST->getOffset());
Duncan Sands7e857202008-01-22 07:17:34 +00001266
1267 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001268 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001269 case TargetLowering::Legal:
1270 // If this is an unaligned store and the target doesn't support it,
1271 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001272 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001273 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001274 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001275 if (ST->getAlignment() < ABIAlignment)
Dan Gohman65fd6562011-11-03 21:49:52 +00001276 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001277 }
1278 break;
1279 case TargetLowering::Custom:
Dan Gohman65fd6562011-11-03 21:49:52 +00001280 DAG.ReplaceAllUsesWith(SDValue(Node, 0),
1281 TLI.LowerOperation(SDValue(Node, 0), DAG),
1282 this);
Duncan Sands7e857202008-01-22 07:17:34 +00001283 break;
Dan Gohmane63e5ab2011-07-15 22:51:43 +00001284 case TargetLowering::Expand:
Nadav Roteme9b58d02011-10-15 07:41:10 +00001285 assert(!StVT.isVector() &&
1286 "Vector Stores are handled in LegalizeVectorOps");
Nadav Rotemc2492c22011-06-14 08:11:52 +00001287
Duncan Sands7e857202008-01-22 07:17:34 +00001288 // TRUNCSTORE:i16 i32 -> STORE i16
Dan Gohman75b10042011-07-15 22:39:09 +00001289 assert(TLI.isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001290 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Dan Gohman65fd6562011-11-03 21:49:52 +00001291 SDValue Result =
1292 DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1293 isVolatile, isNonTemporal, Alignment);
1294 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001295 break;
1296 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001297 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001298 }
1299 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001300 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001301 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001302}
1303
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001304SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1305 SDValue Vec = Op.getOperand(0);
1306 SDValue Idx = Op.getOperand(1);
1307 DebugLoc dl = Op.getDebugLoc();
1308 // Store the value to a temporary stack slot, then LOAD the returned part.
1309 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001310 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1311 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001312
1313 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001314 unsigned EltSize =
1315 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001316 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1317 DAG.getConstant(EltSize, Idx.getValueType()));
1318
1319 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1320 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1321 else
1322 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1323
1324 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1325
Eli Friedmanc680ac92009-07-09 22:01:03 +00001326 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001327 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001328 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001329 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001330 MachinePointerInfo(),
1331 Vec.getValueType().getVectorElementType(),
1332 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001333}
1334
David Greenecfe33c42011-01-26 19:13:22 +00001335SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1336 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1337
1338 SDValue Vec = Op.getOperand(0);
1339 SDValue Part = Op.getOperand(1);
1340 SDValue Idx = Op.getOperand(2);
1341 DebugLoc dl = Op.getDebugLoc();
1342
1343 // Store the value to a temporary stack slot, then LOAD the returned part.
1344
1345 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1346 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1347 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1348
1349 // First store the whole vector.
1350 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1351 false, false, 0);
1352
1353 // Then store the inserted part.
1354
1355 // Add the offset to the index.
1356 unsigned EltSize =
1357 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1358
1359 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1360 DAG.getConstant(EltSize, Idx.getValueType()));
1361
1362 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1363 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1364 else
1365 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1366
1367 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1368 StackPtr);
1369
1370 // Store the subvector.
1371 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1372 MachinePointerInfo(), false, false, 0);
1373
1374 // Finally, load the updated vector.
1375 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1376 false, false, 0);
1377}
1378
Eli Friedman7ef3d172009-06-06 07:04:42 +00001379SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1380 // We can't handle this case efficiently. Allocate a sufficiently
1381 // aligned object on the stack, store each element into it, then load
1382 // the result as a vector.
1383 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001384 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001385 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001386 DebugLoc dl = Node->getDebugLoc();
1387 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001388 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001389 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001390
1391 // Emit a store of each element to the stack slot.
1392 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001393 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001394 // Store (in the right endianness) the elements to memory.
1395 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1396 // Ignore undef elements.
1397 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1398
1399 unsigned Offset = TypeByteSize*i;
1400
1401 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1402 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1403
Dan Gohman9949dd62010-02-25 20:30:49 +00001404 // If the destination vector element type is narrower than the source
1405 // element type, only store the bits necessary.
1406 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001407 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001408 Node->getOperand(i), Idx,
1409 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001410 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001411 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001412 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001413 Node->getOperand(i), Idx,
1414 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001415 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001416 }
1417
1418 SDValue StoreChain;
1419 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001420 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001421 &Stores[0], Stores.size());
1422 else
1423 StoreChain = DAG.getEntryNode();
1424
1425 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001426 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001427}
1428
Eli Friedman4bc8c712009-05-27 12:20:41 +00001429SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1430 DebugLoc dl = Node->getDebugLoc();
1431 SDValue Tmp1 = Node->getOperand(0);
1432 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001433
1434 // Get the sign bit of the RHS. First obtain a value that has the same
1435 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001436 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001437 EVT FloatVT = Tmp2.getValueType();
1438 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Dan Gohman75b10042011-07-15 22:39:09 +00001439 if (TLI.isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001440 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001441 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001442 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001443 // Store the float to memory, then load the sign part out as an integer.
1444 MVT LoadTy = TLI.getPointerTy();
1445 // First create a temporary that is aligned for both the load and store.
1446 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1447 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001448 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001449 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001450 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001451 if (TLI.isBigEndian()) {
1452 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1453 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001454 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1455 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001456 } else { // Little endian
1457 SDValue LoadPtr = StackPtr;
1458 // The float may be wider than the integer we are going to load. Advance
1459 // the pointer so that the loaded integer will contain the sign bit.
1460 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1461 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1462 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1463 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1464 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001465 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1466 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001467 // Move the sign bit to the top bit of the loaded integer.
1468 unsigned BitShift = LoadTy.getSizeInBits() -
1469 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1470 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1471 if (BitShift)
1472 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001473 DAG.getConstant(BitShift,
1474 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001475 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001476 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001477 // Now get the sign bit proper, by seeing whether the value is negative.
1478 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1479 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1480 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001481 // Get the absolute value of the result.
1482 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1483 // Select between the nabs and abs value based on the sign bit of
1484 // the input.
1485 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1486 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1487 AbsVal);
1488}
1489
Eli Friedman4bc8c712009-05-27 12:20:41 +00001490void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1491 SmallVectorImpl<SDValue> &Results) {
1492 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1493 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1494 " not tell us which reg is the stack pointer!");
1495 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001496 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001497 SDValue Tmp1 = SDValue(Node, 0);
1498 SDValue Tmp2 = SDValue(Node, 1);
1499 SDValue Tmp3 = Node->getOperand(2);
1500 SDValue Chain = Tmp1.getOperand(0);
1501
1502 // Chain the dynamic stack allocation so that it doesn't modify the stack
1503 // pointer when other instructions are using the stack.
1504 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1505
1506 SDValue Size = Tmp2.getOperand(1);
1507 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1508 Chain = SP.getValue(1);
1509 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001510 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001511 if (Align > StackAlign)
1512 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1513 DAG.getConstant(-(uint64_t)Align, VT));
1514 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1515 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1516
1517 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1518 DAG.getIntPtrConstant(0, true), SDValue());
1519
1520 Results.push_back(Tmp1);
1521 Results.push_back(Tmp2);
1522}
1523
Evan Cheng7f042682008-10-15 02:05:31 +00001524/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001525/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001526/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001527void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001528 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001529 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001530 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001531 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001532 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1533 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001534 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001535 case TargetLowering::Legal:
1536 // Nothing to do.
1537 break;
1538 case TargetLowering::Expand: {
1539 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1540 unsigned Opc = 0;
1541 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001542 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001543 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1544 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1545 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1546 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1547 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1548 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1549 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1550 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1551 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1552 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1553 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1554 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001555 // FIXME: Implement more expansions.
1556 }
1557
Dale Johannesenbb5da912009-02-02 20:41:04 +00001558 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1559 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1560 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001561 RHS = SDValue();
1562 CC = SDValue();
1563 break;
1564 }
1565 }
1566}
1567
Chris Lattner1401d152008-01-16 07:45:30 +00001568/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1569/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1570/// a load from the stack slot to DestVT, extending it if needed.
1571/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001572SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001573 EVT SlotVT,
1574 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001575 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001576 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001577 unsigned SrcAlign =
1578 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001579 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001580 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Evan Chengff89dcb2009-10-18 18:16:27 +00001582 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1583 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001584 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001585
Duncan Sands83ec4b62008-06-06 12:08:01 +00001586 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1587 unsigned SlotSize = SlotVT.getSizeInBits();
1588 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001589 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Evan Chengadf97992010-04-15 01:25:27 +00001590 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001591
Chris Lattner1401d152008-01-16 07:45:30 +00001592 // Emit a store to the stack slot. Use a truncstore if the input value is
1593 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001594 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001595
Chris Lattner1401d152008-01-16 07:45:30 +00001596 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001597 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001598 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001599 else {
1600 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001601 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001602 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001603 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001604
Chris Lattner35481892005-12-23 00:16:34 +00001605 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001606 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001607 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001608 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001609
Chris Lattner1401d152008-01-16 07:45:30 +00001610 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00001611 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001612 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001613}
1614
Dan Gohman475871a2008-07-27 21:46:04 +00001615SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001616 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001617 // Create a vector sized/aligned stack slot, store the value to element #0,
1618 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001619 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001620
Evan Chengff89dcb2009-10-18 18:16:27 +00001621 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1622 int SPFI = StackPtrFI->getIndex();
1623
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001624 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1625 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001626 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001627 Node->getValueType(0).getVectorElementType(),
1628 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001629 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001630 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001631 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001632}
1633
1634
Chris Lattnerce872152006-03-19 06:31:19 +00001635/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001636/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001637SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001638 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001639 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001640 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001641 EVT VT = Node->getValueType(0);
1642 EVT OpVT = Node->getOperand(0).getValueType();
1643 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001644
1645 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001646 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001647 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001648 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001649 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001650 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001651 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001652 if (V.getOpcode() == ISD::UNDEF)
1653 continue;
1654 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001655 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001656 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001657 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001658
1659 if (!Value1.getNode()) {
1660 Value1 = V;
1661 } else if (!Value2.getNode()) {
1662 if (V != Value1)
1663 Value2 = V;
1664 } else if (V != Value1 && V != Value2) {
1665 MoreThanTwoValues = true;
1666 }
Chris Lattnerce872152006-03-19 06:31:19 +00001667 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001668
Eli Friedman7a5e5552009-06-07 06:52:44 +00001669 if (!Value1.getNode())
1670 return DAG.getUNDEF(VT);
1671
1672 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001673 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001674
Chris Lattner2eb86532006-03-24 07:29:17 +00001675 // If all elements are constants, create a load from the constant pool.
1676 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001677 std::vector<Constant*> CV;
1678 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001679 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001680 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001681 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001682 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001683 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001684 if (OpVT==EltVT)
1685 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1686 else {
1687 // If OpVT and EltVT don't match, EltVT is not legal and the
1688 // element values have been promoted/truncated earlier. Undo this;
1689 // we don't want a v16i8 to become a v16i32 for example.
1690 const ConstantInt *CI = V->getConstantIntValue();
1691 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1692 CI->getZExtValue()));
1693 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001694 } else {
1695 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001696 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001697 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001698 }
1699 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001700 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001701 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001702 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001703 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001704 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001705 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001706 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001707
Eli Friedman7a5e5552009-06-07 06:52:44 +00001708 if (!MoreThanTwoValues) {
1709 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1710 for (unsigned i = 0; i < NumElems; ++i) {
1711 SDValue V = Node->getOperand(i);
1712 if (V.getOpcode() == ISD::UNDEF)
1713 continue;
1714 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1715 }
1716 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001717 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001718 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1719 SDValue Vec2;
1720 if (Value2.getNode())
1721 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1722 else
1723 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001724
Chris Lattner87100e02006-03-20 01:52:29 +00001725 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001726 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001727 }
1728 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001729
Eli Friedman7ef3d172009-06-06 07:04:42 +00001730 // Otherwise, we can't handle this case efficiently.
1731 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001732}
1733
Chris Lattner77e77a62005-01-21 06:05:23 +00001734// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1735// does not fit into a register, return the lo part and set the hi part to the
1736// by-reg argument. If it does fit into a single register, return the result
1737// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001738SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001739 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001740 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00001741 // Legalizing the call will automatically add the previous call to the
1742 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00001743 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001744
Chris Lattner77e77a62005-01-21 06:05:23 +00001745 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00001746 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00001747 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001748 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001749 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00001750 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001751 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00001752 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00001753 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00001754 }
Bill Wendling056292f2008-09-16 21:48:12 +00001755 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00001756 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001757
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001758 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00001759
1760 // isTailCall may be true since the callee does not reference caller stack
1761 // frame. Check if it's in the right position.
1762 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001763 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00001764 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001765 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001766 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001767 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00001768
Evan Cheng3d2125c2010-11-30 23:55:39 +00001769 if (!CallInfo.second.getNode())
1770 // It's a tailcall, return the chain (which is the DAG root).
1771 return DAG.getRoot();
1772
Eli Friedman74807f22009-05-26 08:55:52 +00001773 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00001774}
1775
Dan Gohmanf316eb72011-05-16 22:09:53 +00001776/// ExpandLibCall - Generate a libcall taking the given operands as arguments
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001777/// and returning a result of type RetVT.
1778SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
1779 const SDValue *Ops, unsigned NumOps,
1780 bool isSigned, DebugLoc dl) {
1781 TargetLowering::ArgListTy Args;
1782 Args.reserve(NumOps);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001783
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001784 TargetLowering::ArgListEntry Entry;
1785 for (unsigned i = 0; i != NumOps; ++i) {
1786 Entry.Node = Ops[i];
1787 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
1788 Entry.isSExt = isSigned;
1789 Entry.isZExt = !isSigned;
1790 Args.push_back(Entry);
1791 }
1792 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1793 TLI.getPointerTy());
Dan Gohmanf316eb72011-05-16 22:09:53 +00001794
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001795 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001796 std::pair<SDValue,SDValue> CallInfo =
1797 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
1798 false, 0, TLI.getLibcallCallingConv(LC), false,
1799 /*isReturnValueUsed=*/true,
1800 Callee, Args, DAG, dl);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001801
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001802 return CallInfo.first;
1803}
1804
Jim Grosbache03262f2010-06-18 21:43:38 +00001805// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
1806// ExpandLibCall except that the first operand is the in-chain.
1807std::pair<SDValue, SDValue>
1808SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
1809 SDNode *Node,
1810 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00001811 SDValue InChain = Node->getOperand(0);
1812
1813 TargetLowering::ArgListTy Args;
1814 TargetLowering::ArgListEntry Entry;
1815 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
1816 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001817 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001818 Entry.Node = Node->getOperand(i);
1819 Entry.Ty = ArgTy;
1820 Entry.isSExt = isSigned;
1821 Entry.isZExt = !isSigned;
1822 Args.push_back(Entry);
1823 }
1824 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1825 TLI.getPointerTy());
1826
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001827 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001828 std::pair<SDValue, SDValue> CallInfo =
1829 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001830 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00001831 /*isReturnValueUsed=*/true,
1832 Callee, Args, DAG, Node->getDebugLoc());
1833
Jim Grosbache03262f2010-06-18 21:43:38 +00001834 return CallInfo;
1835}
1836
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001837SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
1838 RTLIB::Libcall Call_F32,
1839 RTLIB::Libcall Call_F64,
1840 RTLIB::Libcall Call_F80,
1841 RTLIB::Libcall Call_PPCF128) {
1842 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001843 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00001844 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 case MVT::f32: LC = Call_F32; break;
1846 case MVT::f64: LC = Call_F64; break;
1847 case MVT::f80: LC = Call_F80; break;
1848 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001849 }
1850 return ExpandLibCall(LC, Node, false);
1851}
1852
1853SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001854 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001855 RTLIB::Libcall Call_I16,
1856 RTLIB::Libcall Call_I32,
1857 RTLIB::Libcall Call_I64,
1858 RTLIB::Libcall Call_I128) {
1859 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00001861 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001862 case MVT::i8: LC = Call_I8; break;
1863 case MVT::i16: LC = Call_I16; break;
1864 case MVT::i32: LC = Call_I32; break;
1865 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001866 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001867 }
1868 return ExpandLibCall(LC, Node, isSigned);
1869}
1870
Evan Cheng65279cb2011-04-16 03:08:26 +00001871/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
1872static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
1873 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001874 RTLIB::Libcall LC;
1875 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1876 default: assert(0 && "Unexpected request for libcall!");
1877 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1878 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1879 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1880 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1881 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
1882 }
1883
Evan Cheng65279cb2011-04-16 03:08:26 +00001884 return TLI.getLibcallName(LC) != 0;
1885}
Evan Cheng8e23e812011-04-01 00:42:02 +00001886
Evan Cheng65279cb2011-04-16 03:08:26 +00001887/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
1888/// needed.
1889static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001890 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00001891 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00001892 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001893 else
Evan Cheng8e23e812011-04-01 00:42:02 +00001894 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001895
Evan Cheng8e23e812011-04-01 00:42:02 +00001896 SDValue Op0 = Node->getOperand(0);
1897 SDValue Op1 = Node->getOperand(1);
1898 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
1899 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
1900 SDNode *User = *UI;
1901 if (User == Node)
1902 continue;
1903 if (User->getOpcode() == OtherOpcode &&
1904 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00001905 User->getOperand(1) == Op1)
1906 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00001907 }
Evan Cheng65279cb2011-04-16 03:08:26 +00001908 return false;
1909}
Evan Cheng8e23e812011-04-01 00:42:02 +00001910
Evan Cheng65279cb2011-04-16 03:08:26 +00001911/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
1912/// pairs.
1913void
1914SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
1915 SmallVectorImpl<SDValue> &Results) {
1916 unsigned Opcode = Node->getOpcode();
1917 bool isSigned = Opcode == ISD::SDIVREM;
1918
1919 RTLIB::Libcall LC;
1920 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1921 default: assert(0 && "Unexpected request for libcall!");
1922 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1923 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1924 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1925 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1926 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00001927 }
1928
1929 // The input chain to this libcall is the entry node of the function.
1930 // Legalizing the call will automatically add the previous call to the
1931 // dependence.
1932 SDValue InChain = DAG.getEntryNode();
1933
1934 EVT RetVT = Node->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001935 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001936
1937 TargetLowering::ArgListTy Args;
1938 TargetLowering::ArgListEntry Entry;
1939 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1940 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001941 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001942 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
1943 Entry.isSExt = isSigned;
1944 Entry.isZExt = !isSigned;
1945 Args.push_back(Entry);
1946 }
1947
1948 // Also pass the return address of the remainder.
1949 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
1950 Entry.Node = FIPtr;
1951 Entry.Ty = RetTy->getPointerTo();
1952 Entry.isSExt = isSigned;
1953 Entry.isZExt = !isSigned;
1954 Args.push_back(Entry);
1955
1956 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1957 TLI.getPointerTy());
1958
Evan Cheng8e23e812011-04-01 00:42:02 +00001959 DebugLoc dl = Node->getDebugLoc();
1960 std::pair<SDValue, SDValue> CallInfo =
1961 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
1962 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
1963 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
1964
Evan Cheng8e23e812011-04-01 00:42:02 +00001965 // Remainder is loaded back from the stack frame.
Dan Gohman65fd6562011-11-03 21:49:52 +00001966 SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
Evan Cheng8e23e812011-04-01 00:42:02 +00001967 MachinePointerInfo(), false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00001968 Results.push_back(CallInfo.first);
1969 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00001970}
1971
Chris Lattner22cde6a2006-01-28 08:25:58 +00001972/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
1973/// INT_TO_FP operation of the specified operand when the target requests that
1974/// we expand it. At this point, we know that the result and operand types are
1975/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00001976SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
1977 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00001978 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00001979 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001980 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00001981 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00001982
Chris Lattner23594d42008-01-16 07:03:22 +00001983 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00001984 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Chris Lattner22cde6a2006-01-28 08:25:58 +00001986 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00001987 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00001988 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00001989 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00001990 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001991 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00001992 if (TLI.isLittleEndian())
1993 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Chris Lattner22cde6a2006-01-28 08:25:58 +00001995 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00001997 if (isSigned) {
1998 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2000 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002001 } else {
2002 Op0Mapped = Op0;
2003 }
2004 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002005 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002006 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002007 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002008 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002009 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002010 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002011 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2012 MachinePointerInfo(),
2013 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002014 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002015 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2016 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002017 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002018 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002019 BitsToDouble(0x4330000080000000ULL) :
2020 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002022 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002024 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002025 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002026 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002027 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002028 // do nothing
2029 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002030 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002031 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002032 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002034 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002035 }
2036 return Result;
2037 }
2038 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002039 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002040
2041 // Implementation of unsigned i64 to f64 following the algorithm in
2042 // __floatundidf in compiler_rt. This implementation has the advantage
2043 // of performing rounding correctly, both in the default rounding mode
2044 // and in all alternate rounding modes.
2045 // TODO: Generalize this for use with other types.
2046 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2047 SDValue TwoP52 =
2048 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2049 SDValue TwoP84PlusTwoP52 =
2050 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2051 SDValue TwoP84 =
2052 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2053
2054 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2055 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2056 DAG.getConstant(32, MVT::i64));
2057 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2058 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002059 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2060 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002061 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2062 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002063 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2064 }
2065
Owen Anderson3a9e7692010-10-05 17:24:05 +00002066 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002067 // TODO: Generalize this for use with other types.
2068 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002069 // For unsigned conversions, convert them to signed conversions using the
2070 // algorithm from the x86_64 __floatundidf in compiler_rt.
2071 if (!isSigned) {
2072 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002073
Owen Anderson95771af2011-02-25 21:41:48 +00002074 SDValue ShiftConst =
2075 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002076 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2077 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2078 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2079 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002080
Owen Anderson3a9e7692010-10-05 17:24:05 +00002081 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2082 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002083
Owen Anderson3a9e7692010-10-05 17:24:05 +00002084 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002085 // select. We happen to get lucky and machinesink does the right
2086 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002087 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002088 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002089 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2090 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2091 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002092
Owen Anderson3a9e7692010-10-05 17:24:05 +00002093 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002094
Jim Grosbach6e992612010-07-02 17:41:59 +00002095 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002096 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2097 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2098 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002099 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002100 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2101 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2102 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2103 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2104 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2105 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002106 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002107 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002108 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002109
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002110 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2111 DAG.getConstant(32, SHVT));
2112 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2113 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2114 SDValue TwoP32 =
2115 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2116 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2117 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2118 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2119 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2120 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2121 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002122 }
2123
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002124 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002125
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002126 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2127 Op0, DAG.getConstant(0, Op0.getValueType()),
2128 ISD::SETLT);
2129 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2130 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2131 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002132
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002133 // If the sign bit of the integer is set, the large number will be treated
2134 // as a negative number. To counteract this, the dynamic code adds an
2135 // offset depending on the data type.
2136 uint64_t FF;
2137 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002138 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002139 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2140 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2141 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2142 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2143 }
2144 if (TLI.isLittleEndian()) FF <<= 32;
2145 Constant *FudgeFactor = ConstantInt::get(
2146 Type::getInt64Ty(*DAG.getContext()), FF);
2147
2148 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2149 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2150 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2151 Alignment = std::min(Alignment, 4u);
2152 SDValue FudgeInReg;
2153 if (DestVT == MVT::f32)
2154 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002155 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002156 false, false, Alignment);
2157 else {
Dan Gohman65fd6562011-11-03 21:49:52 +00002158 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
2159 DAG.getEntryNode(), CPIdx,
2160 MachinePointerInfo::getConstantPool(),
2161 MVT::f32, false, false, Alignment);
2162 HandleSDNode Handle(Load);
2163 LegalizeOp(Load.getNode());
2164 FudgeInReg = Handle.getValue();
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002165 }
2166
2167 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002168}
2169
2170/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2171/// *INT_TO_FP operation of the specified operand when the target requests that
2172/// we promote it. At this point, we know that the result and operand types are
2173/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2174/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002175SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002176 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002177 bool isSigned,
2178 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002179 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002180 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002181
2182 unsigned OpToUse = 0;
2183
2184 // Scan for the appropriate larger type to use.
2185 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002186 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002187 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002188
2189 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002190 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2191 OpToUse = ISD::SINT_TO_FP;
2192 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002193 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002194 if (isSigned) continue;
2195
2196 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002197 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2198 OpToUse = ISD::UINT_TO_FP;
2199 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002200 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002201
2202 // Otherwise, try a larger type.
2203 }
2204
2205 // Okay, we found the operation and type to use. Zero extend our input to the
2206 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002207 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002208 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002209 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002210}
2211
2212/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2213/// FP_TO_*INT operation of the specified operand when the target requests that
2214/// we promote it. At this point, we know that the result and operand types are
2215/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2216/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002217SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002218 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002219 bool isSigned,
2220 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002221 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002222 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002223
2224 unsigned OpToUse = 0;
2225
2226 // Scan for the appropriate larger type to use.
2227 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002228 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002229 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002230
Eli Friedman3be2e512009-05-28 03:06:16 +00002231 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002232 OpToUse = ISD::FP_TO_SINT;
2233 break;
2234 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002235
Eli Friedman3be2e512009-05-28 03:06:16 +00002236 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002237 OpToUse = ISD::FP_TO_UINT;
2238 break;
2239 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002240
2241 // Otherwise, try a larger type.
2242 }
2243
Scott Michelfdc40a02009-02-17 22:15:04 +00002244
Chris Lattner27a6c732007-11-24 07:07:01 +00002245 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002246 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002247
Chris Lattner27a6c732007-11-24 07:07:01 +00002248 // Truncate the result of the extended FP_TO_*INT operation to the desired
2249 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002250 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002251}
2252
2253/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2254///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002255SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002256 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002257 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002258 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002259 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002260 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002261 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002262 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2263 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2264 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002265 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002266 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2267 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2268 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2269 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2270 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2271 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2272 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2273 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2274 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002276 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2277 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2278 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2279 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2280 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2281 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2282 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2283 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2284 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2285 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2286 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2287 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2288 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2289 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2290 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2291 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2292 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2293 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2294 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2295 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2296 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002297 }
2298}
2299
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002300/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002301// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002302static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2303 APInt Val = APInt(NumBits, ByteVal);
2304 unsigned Shift = 8;
2305 for (unsigned i = NumBits; i > 8; i >>= 1) {
2306 Val = (Val << Shift) | Val;
2307 Shift <<= 1;
2308 }
2309 return Val;
2310}
2311
Chris Lattner22cde6a2006-01-28 08:25:58 +00002312/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2313///
Scott Michelfdc40a02009-02-17 22:15:04 +00002314SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002315 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002316 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002317 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002318 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002319 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002320 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002321 unsigned Len = VT.getSizeInBits();
2322
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002323 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2324 "CTPOP not implemented for this type.");
2325
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002326 // This is the "best" algorithm from
2327 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2328
2329 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2330 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2331 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2332 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2333
2334 // v = v - ((v >> 1) & 0x55555555...)
2335 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2336 DAG.getNode(ISD::AND, dl, VT,
2337 DAG.getNode(ISD::SRL, dl, VT, Op,
2338 DAG.getConstant(1, ShVT)),
2339 Mask55));
2340 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2341 Op = DAG.getNode(ISD::ADD, dl, VT,
2342 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2343 DAG.getNode(ISD::AND, dl, VT,
2344 DAG.getNode(ISD::SRL, dl, VT, Op,
2345 DAG.getConstant(2, ShVT)),
2346 Mask33));
2347 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2348 Op = DAG.getNode(ISD::AND, dl, VT,
2349 DAG.getNode(ISD::ADD, dl, VT, Op,
2350 DAG.getNode(ISD::SRL, dl, VT, Op,
2351 DAG.getConstant(4, ShVT))),
2352 Mask0F);
2353 // v = (v * 0x01010101...) >> (Len - 8)
2354 Op = DAG.getNode(ISD::SRL, dl, VT,
2355 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2356 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002357
Chris Lattner22cde6a2006-01-28 08:25:58 +00002358 return Op;
2359 }
2360 case ISD::CTLZ: {
2361 // for now, we do this:
2362 // x = x | (x >> 1);
2363 // x = x | (x >> 2);
2364 // ...
2365 // x = x | (x >>16);
2366 // x = x | (x >>32); // for 64-bit input
2367 // return popcount(~x);
2368 //
2369 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002370 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002371 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002372 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002373 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002374 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002375 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002376 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002377 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002378 Op = DAG.getNOT(dl, Op, VT);
2379 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002380 }
2381 case ISD::CTTZ: {
2382 // for now, we use: { return popcount(~x & (x - 1)); }
2383 // unless the target has ctlz but not ctpop, in which case we use:
2384 // { return 32 - nlz(~x & (x-1)); }
2385 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002386 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002387 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2388 DAG.getNOT(dl, Op, VT),
2389 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002390 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002391 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002392 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2393 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002394 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002395 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002396 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2397 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002398 }
2399 }
2400}
Chris Lattnere34b3962005-01-19 04:19:40 +00002401
Jim Grosbache03262f2010-06-18 21:43:38 +00002402std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2403 unsigned Opc = Node->getOpcode();
2404 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2405 RTLIB::Libcall LC;
2406
2407 switch (Opc) {
2408 default:
2409 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2410 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002411 case ISD::ATOMIC_SWAP:
2412 switch (VT.SimpleTy) {
2413 default: llvm_unreachable("Unexpected value type for atomic!");
2414 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2415 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2416 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2417 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2418 }
2419 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002420 case ISD::ATOMIC_CMP_SWAP:
2421 switch (VT.SimpleTy) {
2422 default: llvm_unreachable("Unexpected value type for atomic!");
2423 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2424 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2425 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2426 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2427 }
2428 break;
2429 case ISD::ATOMIC_LOAD_ADD:
2430 switch (VT.SimpleTy) {
2431 default: llvm_unreachable("Unexpected value type for atomic!");
2432 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2433 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2434 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2435 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2436 }
2437 break;
2438 case ISD::ATOMIC_LOAD_SUB:
2439 switch (VT.SimpleTy) {
2440 default: llvm_unreachable("Unexpected value type for atomic!");
2441 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2442 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2443 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2444 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2445 }
2446 break;
2447 case ISD::ATOMIC_LOAD_AND:
2448 switch (VT.SimpleTy) {
2449 default: llvm_unreachable("Unexpected value type for atomic!");
2450 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2451 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2452 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2453 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2454 }
2455 break;
2456 case ISD::ATOMIC_LOAD_OR:
2457 switch (VT.SimpleTy) {
2458 default: llvm_unreachable("Unexpected value type for atomic!");
2459 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2460 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2461 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2462 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2463 }
2464 break;
2465 case ISD::ATOMIC_LOAD_XOR:
2466 switch (VT.SimpleTy) {
2467 default: llvm_unreachable("Unexpected value type for atomic!");
2468 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2469 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2470 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2471 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2472 }
2473 break;
2474 case ISD::ATOMIC_LOAD_NAND:
2475 switch (VT.SimpleTy) {
2476 default: llvm_unreachable("Unexpected value type for atomic!");
2477 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2478 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2479 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2480 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2481 }
2482 break;
2483 }
2484
2485 return ExpandChainLibCall(LC, Node, false);
2486}
2487
Dan Gohman65fd6562011-11-03 21:49:52 +00002488void SelectionDAGLegalize::ExpandNode(SDNode *Node) {
2489 SmallVector<SDValue, 8> Results;
Eli Friedman8c377c72009-05-27 01:25:56 +00002490 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002491 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002492 switch (Node->getOpcode()) {
2493 case ISD::CTPOP:
2494 case ISD::CTLZ:
2495 case ISD::CTTZ:
2496 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2497 Results.push_back(Tmp1);
2498 break;
2499 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002500 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002501 break;
2502 case ISD::FRAMEADDR:
2503 case ISD::RETURNADDR:
2504 case ISD::FRAME_TO_ARGS_OFFSET:
2505 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2506 break;
2507 case ISD::FLT_ROUNDS_:
2508 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2509 break;
2510 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002511 case ISD::EH_LABEL:
2512 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002513 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002514 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002515 case ISD::EH_SJLJ_DISPATCHSETUP:
2516 // If the target didn't expand these, there's nothing to do, so just
2517 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002518 Results.push_back(Node->getOperand(0));
2519 break;
2520 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002521 // If the target didn't expand this, just return 'zero' and preserve the
2522 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002523 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002524 Results.push_back(Node->getOperand(0));
2525 break;
Eli Friedman14648462011-07-27 22:21:52 +00002526 case ISD::ATOMIC_FENCE:
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002527 case ISD::MEMBARRIER: {
2528 // If the target didn't lower this, lower it to '__sync_synchronize()' call
Eli Friedman14648462011-07-27 22:21:52 +00002529 // FIXME: handle "fence singlethread" more efficiently.
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002530 TargetLowering::ArgListTy Args;
2531 std::pair<SDValue, SDValue> CallResult =
2532 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002533 false, false, false, false, 0, CallingConv::C,
2534 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002535 /*isReturnValueUsed=*/true,
2536 DAG.getExternalSymbol("__sync_synchronize",
2537 TLI.getPointerTy()),
2538 Args, DAG, dl);
2539 Results.push_back(CallResult.second);
2540 break;
2541 }
Eli Friedman069e2ed2011-08-26 02:59:24 +00002542 case ISD::ATOMIC_LOAD: {
2543 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
Eli Friedman331120b2011-09-15 21:20:49 +00002544 SDValue Zero = DAG.getConstant(0, Node->getValueType(0));
Eli Friedman069e2ed2011-08-26 02:59:24 +00002545 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
2546 cast<AtomicSDNode>(Node)->getMemoryVT(),
2547 Node->getOperand(0),
2548 Node->getOperand(1), Zero, Zero,
2549 cast<AtomicSDNode>(Node)->getMemOperand(),
2550 cast<AtomicSDNode>(Node)->getOrdering(),
2551 cast<AtomicSDNode>(Node)->getSynchScope());
2552 Results.push_back(Swap.getValue(0));
2553 Results.push_back(Swap.getValue(1));
2554 break;
2555 }
2556 case ISD::ATOMIC_STORE: {
2557 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
2558 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
2559 cast<AtomicSDNode>(Node)->getMemoryVT(),
2560 Node->getOperand(0),
2561 Node->getOperand(1), Node->getOperand(2),
2562 cast<AtomicSDNode>(Node)->getMemOperand(),
2563 cast<AtomicSDNode>(Node)->getOrdering(),
2564 cast<AtomicSDNode>(Node)->getSynchScope());
2565 Results.push_back(Swap.getValue(1));
2566 break;
2567 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002568 // By default, atomic intrinsics are marked Legal and lowered. Targets
2569 // which don't support them directly, however, may want libcalls, in which
2570 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002571 case ISD::ATOMIC_SWAP:
2572 case ISD::ATOMIC_LOAD_ADD:
2573 case ISD::ATOMIC_LOAD_SUB:
2574 case ISD::ATOMIC_LOAD_AND:
2575 case ISD::ATOMIC_LOAD_OR:
2576 case ISD::ATOMIC_LOAD_XOR:
2577 case ISD::ATOMIC_LOAD_NAND:
2578 case ISD::ATOMIC_LOAD_MIN:
2579 case ISD::ATOMIC_LOAD_MAX:
2580 case ISD::ATOMIC_LOAD_UMIN:
2581 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002582 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002583 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2584 Results.push_back(Tmp.first);
2585 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002586 break;
Evan Chenga8457062010-06-18 22:01:37 +00002587 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002588 case ISD::DYNAMIC_STACKALLOC:
2589 ExpandDYNAMIC_STACKALLOC(Node, Results);
2590 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002591 case ISD::MERGE_VALUES:
2592 for (unsigned i = 0; i < Node->getNumValues(); i++)
2593 Results.push_back(Node->getOperand(i));
2594 break;
2595 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002596 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002597 if (VT.isInteger())
2598 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002599 else {
2600 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002601 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002602 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002603 break;
2604 }
2605 case ISD::TRAP: {
2606 // If this operation is not supported, lower it to 'abort()' call
2607 TargetLowering::ArgListTy Args;
2608 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002609 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002610 false, false, false, false, 0, CallingConv::C,
2611 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002612 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002613 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002614 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002615 Results.push_back(CallResult.second);
2616 break;
2617 }
2618 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002619 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002620 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2621 Node->getValueType(0), dl);
2622 Results.push_back(Tmp1);
2623 break;
2624 case ISD::FP_EXTEND:
2625 Tmp1 = EmitStackConvert(Node->getOperand(0),
2626 Node->getOperand(0).getValueType(),
2627 Node->getValueType(0), dl);
2628 Results.push_back(Tmp1);
2629 break;
2630 case ISD::SIGN_EXTEND_INREG: {
2631 // NOTE: we could fall back on load/store here too for targets without
2632 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002633 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002634 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00002635 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00002636 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002637 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002638 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2639 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002640 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002641 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2642 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002643 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2644 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002645 break;
2646 }
2647 case ISD::FP_ROUND_INREG: {
2648 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002649 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00002650
2651 // NOTE: there is a choice here between constantly creating new stack
2652 // slots and always reusing the same one. We currently always create
2653 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002654 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002655 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2656 Node->getValueType(0), dl);
2657 Results.push_back(Tmp1);
2658 break;
2659 }
2660 case ISD::SINT_TO_FP:
2661 case ISD::UINT_TO_FP:
2662 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2663 Node->getOperand(0), Node->getValueType(0), dl);
2664 Results.push_back(Tmp1);
2665 break;
2666 case ISD::FP_TO_UINT: {
2667 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002668 EVT VT = Node->getOperand(0).getValueType();
2669 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002670 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002671 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2672 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2673 Tmp1 = DAG.getConstantFP(apf, VT);
2674 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2675 Node->getOperand(0),
2676 Tmp1, ISD::SETLT);
2677 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002678 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2679 DAG.getNode(ISD::FSUB, dl, VT,
2680 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002681 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2682 DAG.getConstant(x, NVT));
2683 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2684 Results.push_back(Tmp1);
2685 break;
2686 }
Eli Friedman509150f2009-05-27 07:58:35 +00002687 case ISD::VAARG: {
2688 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002689 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002690 Tmp1 = Node->getOperand(0);
2691 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002692 unsigned Align = Node->getConstantOperandVal(3);
2693
Chris Lattnerecf42c42010-09-21 16:36:31 +00002694 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2695 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002696 SDValue VAList = VAListLoad;
2697
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002698 if (Align > TLI.getMinStackArgumentAlignment()) {
2699 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2700
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002701 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2702 DAG.getConstant(Align - 1,
2703 TLI.getPointerTy()));
2704
2705 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002706 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002707 TLI.getPointerTy()));
2708 }
2709
Eli Friedman509150f2009-05-27 07:58:35 +00002710 // Increment the pointer, VAList, to the next vaarg
2711 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2712 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002713 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002714 TLI.getPointerTy()));
2715 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002716 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2717 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002718 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002719 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002720 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002721 Results.push_back(Results[0].getValue(1));
2722 break;
2723 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002724 case ISD::VACOPY: {
2725 // This defaults to loading a pointer from the input and storing it to the
2726 // output, returning the chain.
2727 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2728 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2729 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002730 Node->getOperand(2), MachinePointerInfo(VS),
2731 false, false, 0);
2732 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2733 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002734 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002735 break;
2736 }
2737 case ISD::EXTRACT_VECTOR_ELT:
2738 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2739 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002740 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002741 Node->getOperand(0));
2742 else
2743 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2744 Results.push_back(Tmp1);
2745 break;
2746 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002747 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002748 break;
David Greenecfe33c42011-01-26 19:13:22 +00002749 case ISD::INSERT_SUBVECTOR:
2750 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2751 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002752 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002753 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002754 break;
2755 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002756 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002757 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002758 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002759 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002760 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2761 Node->getOperand(1),
2762 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002763 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002764 case ISD::VECTOR_SHUFFLE: {
2765 SmallVector<int, 8> Mask;
2766 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2767
Owen Andersone50ed302009-08-10 22:56:29 +00002768 EVT VT = Node->getValueType(0);
2769 EVT EltVT = VT.getVectorElementType();
Dan Gohman75b10042011-07-15 22:39:09 +00002770 if (!TLI.isTypeLegal(EltVT))
Bob Wilson14b21412010-05-19 18:48:32 +00002771 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002772 unsigned NumElems = VT.getVectorNumElements();
2773 SmallVector<SDValue, 8> Ops;
2774 for (unsigned i = 0; i != NumElems; ++i) {
2775 if (Mask[i] < 0) {
2776 Ops.push_back(DAG.getUNDEF(EltVT));
2777 continue;
2778 }
2779 unsigned Idx = Mask[i];
2780 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002781 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2782 Node->getOperand(0),
2783 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002784 else
Bill Wendling775db972009-12-23 00:28:23 +00002785 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2786 Node->getOperand(1),
2787 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002788 }
2789 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2790 Results.push_back(Tmp1);
2791 break;
2792 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002793 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002794 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002795 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2796 // 1 -> Hi
2797 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2798 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00002799 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00002800 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2801 } else {
2802 // 0 -> Lo
2803 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2804 Node->getOperand(0));
2805 }
2806 Results.push_back(Tmp1);
2807 break;
2808 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002809 case ISD::STACKSAVE:
2810 // Expand to CopyFromReg if the target set
2811 // StackPointerRegisterToSaveRestore.
2812 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002813 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2814 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002815 Results.push_back(Results[0].getValue(1));
2816 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002817 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002818 Results.push_back(Node->getOperand(0));
2819 }
2820 break;
2821 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002822 // Expand to CopyToReg if the target set
2823 // StackPointerRegisterToSaveRestore.
2824 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2825 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2826 Node->getOperand(1)));
2827 } else {
2828 Results.push_back(Node->getOperand(0));
2829 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002830 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002831 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002832 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002833 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002834 case ISD::FNEG:
2835 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2836 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2837 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2838 Node->getOperand(0));
2839 Results.push_back(Tmp1);
2840 break;
2841 case ISD::FABS: {
2842 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002843 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002844 Tmp1 = Node->getOperand(0);
2845 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002846 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002847 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002848 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2849 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002850 Results.push_back(Tmp1);
2851 break;
2852 }
2853 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002854 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2855 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002856 break;
2857 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002858 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2859 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002860 break;
2861 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002862 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2863 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002864 break;
2865 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002866 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2867 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002868 break;
2869 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002870 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2871 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002872 break;
2873 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002874 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2875 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002876 break;
2877 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002878 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2879 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002880 break;
2881 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002882 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2883 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002884 break;
2885 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00002886 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2887 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002888 break;
2889 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00002890 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2891 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002892 break;
2893 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00002894 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2895 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002896 break;
2897 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00002898 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
2899 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002900 break;
2901 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00002902 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
2903 RTLIB::NEARBYINT_F64,
2904 RTLIB::NEARBYINT_F80,
2905 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002906 break;
2907 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00002908 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
2909 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002910 break;
2911 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00002912 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
2913 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002914 break;
2915 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00002916 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
2917 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002918 break;
2919 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00002920 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
2921 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002922 break;
Cameron Zwarich33390842011-07-08 21:39:21 +00002923 case ISD::FMA:
2924 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
2925 RTLIB::FMA_F80, RTLIB::FMA_PPCF128));
2926 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00002927 case ISD::FP16_TO_FP32:
2928 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
2929 break;
2930 case ISD::FP32_TO_FP16:
2931 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
2932 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002933 case ISD::ConstantFP: {
2934 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00002935 // Check to see if this FP immediate is already legal.
2936 // If this is a legal constant, turn it into a TargetConstantFP node.
Dan Gohman65fd6562011-11-03 21:49:52 +00002937 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
2938 Results.push_back(ExpandConstantFP(CFP, true));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002939 break;
2940 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00002941 case ISD::EHSELECTION: {
2942 unsigned Reg = TLI.getExceptionSelectorRegister();
2943 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002944 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
2945 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002946 Results.push_back(Results[0].getValue(1));
2947 break;
2948 }
2949 case ISD::EXCEPTIONADDR: {
2950 unsigned Reg = TLI.getExceptionAddressRegister();
2951 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002952 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
2953 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002954 Results.push_back(Results[0].getValue(1));
2955 break;
2956 }
2957 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00002958 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002959 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2960 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
2961 "Don't know how to expand this subtraction!");
2962 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
2963 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00002964 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2965 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002966 break;
2967 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002968 case ISD::UREM:
2969 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00002970 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002971 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002972 bool isSigned = Node->getOpcode() == ISD::SREM;
2973 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
2974 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
2975 Tmp2 = Node->getOperand(0);
2976 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00002977 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
2978 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
2979 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00002980 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
2981 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002982 // X % Y -> X-X/Y*Y
2983 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
2984 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
2985 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00002986 } else if (isSigned)
2987 Tmp1 = ExpandIntLibCall(Node, true,
2988 RTLIB::SREM_I8,
2989 RTLIB::SREM_I16, RTLIB::SREM_I32,
2990 RTLIB::SREM_I64, RTLIB::SREM_I128);
2991 else
2992 Tmp1 = ExpandIntLibCall(Node, false,
2993 RTLIB::UREM_I8,
2994 RTLIB::UREM_I16, RTLIB::UREM_I32,
2995 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002996 Results.push_back(Tmp1);
2997 break;
2998 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002999 case ISD::UDIV:
3000 case ISD::SDIV: {
3001 bool isSigned = Node->getOpcode() == ISD::SDIV;
3002 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003003 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003004 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003005 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3006 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3007 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003008 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3009 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003010 else if (isSigned)
3011 Tmp1 = ExpandIntLibCall(Node, true,
3012 RTLIB::SDIV_I8,
3013 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3014 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3015 else
3016 Tmp1 = ExpandIntLibCall(Node, false,
3017 RTLIB::UDIV_I8,
3018 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3019 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003020 Results.push_back(Tmp1);
3021 break;
3022 }
3023 case ISD::MULHU:
3024 case ISD::MULHS: {
3025 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3026 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003027 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003028 SDVTList VTs = DAG.getVTList(VT, VT);
3029 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3030 "If this wasn't legal, it shouldn't have been created!");
3031 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3032 Node->getOperand(1));
3033 Results.push_back(Tmp1.getValue(1));
3034 break;
3035 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003036 case ISD::SDIVREM:
3037 case ISD::UDIVREM:
3038 // Expand into divrem libcall
3039 ExpandDivRemLibCall(Node, Results);
3040 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003041 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003042 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003043 SDVTList VTs = DAG.getVTList(VT, VT);
3044 // See if multiply or divide can be lowered using two-result operations.
3045 // We just need the low half of the multiply; try both the signed
3046 // and unsigned forms. If the target supports both SMUL_LOHI and
3047 // UMUL_LOHI, form a preference by checking which forms of plain
3048 // MULH it supports.
3049 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3050 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3051 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3052 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3053 unsigned OpToUse = 0;
3054 if (HasSMUL_LOHI && !HasMULHS) {
3055 OpToUse = ISD::SMUL_LOHI;
3056 } else if (HasUMUL_LOHI && !HasMULHU) {
3057 OpToUse = ISD::UMUL_LOHI;
3058 } else if (HasSMUL_LOHI) {
3059 OpToUse = ISD::SMUL_LOHI;
3060 } else if (HasUMUL_LOHI) {
3061 OpToUse = ISD::UMUL_LOHI;
3062 }
3063 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003064 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3065 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003066 break;
3067 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003068 Tmp1 = ExpandIntLibCall(Node, false,
3069 RTLIB::MUL_I8,
3070 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003071 RTLIB::MUL_I64, RTLIB::MUL_I128);
3072 Results.push_back(Tmp1);
3073 break;
3074 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003075 case ISD::SADDO:
3076 case ISD::SSUBO: {
3077 SDValue LHS = Node->getOperand(0);
3078 SDValue RHS = Node->getOperand(1);
3079 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3080 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3081 LHS, RHS);
3082 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003083 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003084
Eli Friedman4bc8c712009-05-27 12:20:41 +00003085 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3086
3087 // LHSSign -> LHS >= 0
3088 // RHSSign -> RHS >= 0
3089 // SumSign -> Sum >= 0
3090 //
3091 // Add:
3092 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3093 // Sub:
3094 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3095 //
3096 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3097 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3098 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3099 Node->getOpcode() == ISD::SADDO ?
3100 ISD::SETEQ : ISD::SETNE);
3101
3102 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3103 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3104
3105 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3106 Results.push_back(Cmp);
3107 break;
3108 }
3109 case ISD::UADDO:
3110 case ISD::USUBO: {
3111 SDValue LHS = Node->getOperand(0);
3112 SDValue RHS = Node->getOperand(1);
3113 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3114 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3115 LHS, RHS);
3116 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003117 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3118 Node->getOpcode () == ISD::UADDO ?
3119 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003120 break;
3121 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003122 case ISD::UMULO:
3123 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003124 EVT VT = Node->getValueType(0);
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003125 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003126 SDValue LHS = Node->getOperand(0);
3127 SDValue RHS = Node->getOperand(1);
3128 SDValue BottomHalf;
3129 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003130 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003131 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3132 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3133 bool isSigned = Node->getOpcode() == ISD::SMULO;
3134 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3135 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3136 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3137 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3138 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3139 RHS);
3140 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003141 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3142 VT.getSizeInBits() * 2))) {
Eli Friedmandb3c1692009-06-16 06:58:29 +00003143 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3144 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3145 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3146 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3147 DAG.getIntPtrConstant(0));
3148 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3149 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003150 } else {
3151 // We can fall back to a libcall with an illegal type for the MUL if we
3152 // have a libcall big enough.
3153 // Also, we can fall back to a division in some cases, but that's a big
3154 // performance hit in the general case.
Eric Christopher38a18262011-01-20 00:29:24 +00003155 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3156 if (WideVT == MVT::i16)
3157 LC = RTLIB::MUL_I16;
3158 else if (WideVT == MVT::i32)
3159 LC = RTLIB::MUL_I32;
3160 else if (WideVT == MVT::i64)
3161 LC = RTLIB::MUL_I64;
3162 else if (WideVT == MVT::i128)
3163 LC = RTLIB::MUL_I128;
3164 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanf316eb72011-05-16 22:09:53 +00003165
3166 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003167 // part.
3168 unsigned LoSize = VT.getSizeInBits();
3169 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
3170 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
3171 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
3172 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
Owen Anderson95771af2011-02-25 21:41:48 +00003173
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003174 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3175 // pre-lowered to the correct types. This all depends upon WideVT not
3176 // being a legal type for the architecture and thus has to be split to
3177 // two arguments.
3178 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3179 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3180 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3181 DAG.getIntPtrConstant(0));
3182 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3183 DAG.getIntPtrConstant(1));
Dan Gohman65fd6562011-11-03 21:49:52 +00003184 // Ret is a node with an illegal type. Because such things are not
3185 // generally permitted during this phase of legalization, delete the
3186 // node. The above EXTRACT_ELEMENT nodes should have been folded.
3187 DAG.DeleteNode(Ret.getNode());
Eli Friedmandb3c1692009-06-16 06:58:29 +00003188 }
Dan Gohmanf316eb72011-05-16 22:09:53 +00003189
Eli Friedmandb3c1692009-06-16 06:58:29 +00003190 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003191 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3192 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003193 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3194 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3195 ISD::SETNE);
3196 } else {
3197 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3198 DAG.getConstant(0, VT), ISD::SETNE);
3199 }
3200 Results.push_back(BottomHalf);
3201 Results.push_back(TopHalf);
3202 break;
3203 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003204 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003205 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003206 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3207 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003208 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003209 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003210 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003211 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003212 break;
3213 }
Eli Friedman509150f2009-05-27 07:58:35 +00003214 case ISD::SELECT:
3215 Tmp1 = Node->getOperand(0);
3216 Tmp2 = Node->getOperand(1);
3217 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003218 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003219 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3220 Tmp2, Tmp3,
3221 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003222 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003223 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3224 DAG.getConstant(0, Tmp1.getValueType()),
3225 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003226 }
Eli Friedman509150f2009-05-27 07:58:35 +00003227 Results.push_back(Tmp1);
3228 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003229 case ISD::BR_JT: {
3230 SDValue Chain = Node->getOperand(0);
3231 SDValue Table = Node->getOperand(1);
3232 SDValue Index = Node->getOperand(2);
3233
Owen Andersone50ed302009-08-10 22:56:29 +00003234 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003235
3236 const TargetData &TD = *TLI.getTargetData();
3237 unsigned EntrySize =
3238 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003239
Chris Lattner071c62f2010-01-25 23:26:13 +00003240 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003241 Index, DAG.getConstant(EntrySize, PTy));
3242 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3243
Owen Anderson23b9b192009-08-12 00:36:31 +00003244 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003245 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003246 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003247 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003248 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003249 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003250 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003251 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003252 // RelocBase can be JumpTable, GOT or some sort of global base.
3253 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3254 TLI.getPICJumpTableRelocBase(Table, DAG));
3255 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003256 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003257 Results.push_back(Tmp1);
3258 break;
3259 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003260 case ISD::BRCOND:
3261 // Expand brcond's setcc into its constituent parts and create a BR_CC
3262 // Node.
3263 Tmp1 = Node->getOperand(0);
3264 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003265 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003266 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003267 Tmp1, Tmp2.getOperand(2),
3268 Tmp2.getOperand(0), Tmp2.getOperand(1),
3269 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003270 } else {
Stuart Hastings88882242011-05-13 00:51:54 +00003271 // We test only the i1 bit. Skip the AND if UNDEF.
3272 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3273 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3274 DAG.getConstant(1, Tmp2.getValueType()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastings88882242011-05-13 00:51:54 +00003276 DAG.getCondCode(ISD::SETNE), Tmp3,
3277 DAG.getConstant(0, Tmp3.getValueType()),
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003278 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003279 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003280 Results.push_back(Tmp1);
3281 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003282 case ISD::SETCC: {
3283 Tmp1 = Node->getOperand(0);
3284 Tmp2 = Node->getOperand(1);
3285 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003286 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003287
3288 // If we expanded the SETCC into an AND/OR, return the new node
3289 if (Tmp2.getNode() == 0) {
3290 Results.push_back(Tmp1);
3291 break;
3292 }
3293
3294 // Otherwise, SETCC for the given comparison type must be completely
3295 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003296 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003297 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3298 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3299 Results.push_back(Tmp1);
3300 break;
3301 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003302 case ISD::SELECT_CC: {
3303 Tmp1 = Node->getOperand(0); // LHS
3304 Tmp2 = Node->getOperand(1); // RHS
3305 Tmp3 = Node->getOperand(2); // True
3306 Tmp4 = Node->getOperand(3); // False
3307 SDValue CC = Node->getOperand(4);
3308
3309 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003310 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003311
3312 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3313 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3314 CC = DAG.getCondCode(ISD::SETNE);
3315 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3316 Tmp3, Tmp4, CC);
3317 Results.push_back(Tmp1);
3318 break;
3319 }
3320 case ISD::BR_CC: {
3321 Tmp1 = Node->getOperand(0); // Chain
3322 Tmp2 = Node->getOperand(2); // LHS
3323 Tmp3 = Node->getOperand(3); // RHS
3324 Tmp4 = Node->getOperand(1); // CC
3325
3326 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003327 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003328
3329 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3330 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3331 Tmp4 = DAG.getCondCode(ISD::SETNE);
3332 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3333 Tmp3, Node->getOperand(4));
3334 Results.push_back(Tmp1);
3335 break;
3336 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003337 case ISD::BUILD_VECTOR:
3338 Results.push_back(ExpandBUILD_VECTOR(Node));
3339 break;
3340 case ISD::SRA:
3341 case ISD::SRL:
3342 case ISD::SHL: {
3343 // Scalarize vector SRA/SRL/SHL.
3344 EVT VT = Node->getValueType(0);
3345 assert(VT.isVector() && "Unable to legalize non-vector shift");
3346 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3347 unsigned NumElem = VT.getVectorNumElements();
3348
3349 SmallVector<SDValue, 8> Scalars;
3350 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
3351 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3352 VT.getScalarType(),
3353 Node->getOperand(0), DAG.getIntPtrConstant(Idx));
3354 SDValue Sh = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3355 VT.getScalarType(),
3356 Node->getOperand(1), DAG.getIntPtrConstant(Idx));
3357 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3358 VT.getScalarType(), Ex, Sh));
3359 }
3360 SDValue Result =
3361 DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
3362 &Scalars[0], Scalars.size());
3363 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
3364 break;
3365 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003366 case ISD::GLOBAL_OFFSET_TABLE:
3367 case ISD::GlobalAddress:
3368 case ISD::GlobalTLSAddress:
3369 case ISD::ExternalSymbol:
3370 case ISD::ConstantPool:
3371 case ISD::JumpTable:
3372 case ISD::INTRINSIC_W_CHAIN:
3373 case ISD::INTRINSIC_WO_CHAIN:
3374 case ISD::INTRINSIC_VOID:
3375 // FIXME: Custom lowering for these operations shouldn't return null!
Eli Friedman3f727d62009-05-27 02:16:40 +00003376 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003377 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003378
3379 // Replace the original node with the legalized result.
3380 if (!Results.empty())
3381 DAG.ReplaceAllUsesWith(Node, Results.data(), this);
Eli Friedman8c377c72009-05-27 01:25:56 +00003382}
Dan Gohman65fd6562011-11-03 21:49:52 +00003383
3384void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
3385 SmallVector<SDValue, 8> Results;
Owen Andersone50ed302009-08-10 22:56:29 +00003386 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003387 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003388 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003389 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003390 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003391 }
Owen Andersone50ed302009-08-10 22:56:29 +00003392 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003393 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003394 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003395 switch (Node->getOpcode()) {
3396 case ISD::CTTZ:
3397 case ISD::CTLZ:
3398 case ISD::CTPOP:
3399 // Zero extend the argument.
3400 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3401 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003402 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003403 if (Node->getOpcode() == ISD::CTTZ) {
3404 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003405 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003406 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3407 ISD::SETEQ);
3408 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3409 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3410 } else if (Node->getOpcode() == ISD::CTLZ) {
3411 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3412 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3413 DAG.getConstant(NVT.getSizeInBits() -
3414 OVT.getSizeInBits(), NVT));
3415 }
Bill Wendling775db972009-12-23 00:28:23 +00003416 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003417 break;
3418 case ISD::BSWAP: {
3419 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003420 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003421 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3422 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003423 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003424 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003425 break;
3426 }
3427 case ISD::FP_TO_UINT:
3428 case ISD::FP_TO_SINT:
3429 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3430 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3431 Results.push_back(Tmp1);
3432 break;
3433 case ISD::UINT_TO_FP:
3434 case ISD::SINT_TO_FP:
3435 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3436 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3437 Results.push_back(Tmp1);
3438 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003439 case ISD::AND:
3440 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003441 case ISD::XOR: {
3442 unsigned ExtOp, TruncOp;
3443 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003444 ExtOp = ISD::BITCAST;
3445 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003446 } else {
3447 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003448 ExtOp = ISD::ANY_EXTEND;
3449 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003450 }
3451 // Promote each of the values to the new type.
3452 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3453 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3454 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003455 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3456 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003457 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003458 }
3459 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003460 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003461 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003462 ExtOp = ISD::BITCAST;
3463 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003464 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003465 ExtOp = ISD::ANY_EXTEND;
3466 TruncOp = ISD::TRUNCATE;
3467 } else {
3468 ExtOp = ISD::FP_EXTEND;
3469 TruncOp = ISD::FP_ROUND;
3470 }
3471 Tmp1 = Node->getOperand(0);
3472 // Promote each of the values to the new type.
3473 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3474 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3475 // Perform the larger operation, then round down.
3476 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3477 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003478 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003479 else
Bill Wendling775db972009-12-23 00:28:23 +00003480 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003481 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003482 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003483 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003484 }
Eli Friedman509150f2009-05-27 07:58:35 +00003485 case ISD::VECTOR_SHUFFLE: {
3486 SmallVector<int, 8> Mask;
3487 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3488
3489 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003490 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3491 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003492
3493 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003494 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003495 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003496 Results.push_back(Tmp1);
3497 break;
3498 }
Eli Friedmanad754602009-05-28 03:56:57 +00003499 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003500 unsigned ExtOp = ISD::FP_EXTEND;
3501 if (NVT.isInteger()) {
3502 ISD::CondCode CCCode =
3503 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3504 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003505 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003506 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3507 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003508 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3509 Tmp1, Tmp2, Node->getOperand(2)));
3510 break;
3511 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003512 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003513
3514 // Replace the original node with the legalized result.
3515 if (!Results.empty())
3516 DAG.ReplaceAllUsesWith(Node, Results.data(), this);
Eli Friedman8c377c72009-05-27 01:25:56 +00003517}
3518
Chris Lattner3e928bb2005-01-07 07:47:09 +00003519// SelectionDAG::Legalize - This is the entry point for the file.
3520//
Dan Gohman975716a2011-05-16 22:19:54 +00003521void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003522 /// run - This is the main entry point to this class.
3523 ///
Dan Gohman975716a2011-05-16 22:19:54 +00003524 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003525}