blob: 9cce6fe67b8afa3adc8981dbed8de5e2eeff0a08 [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);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000288 DAG.RemoveDeadNode(ST, DUL);
Dan Gohman65fd6562011-11-03 21:49:52 +0000289 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000290 }
Dan Gohman1b328962011-05-17 22:22:52 +0000291 // Do a (aligned) store to a stack slot, then copy from the stack slot
292 // to the final destination using (unaligned) integer loads and stores.
293 EVT StoredVT = ST->getMemoryVT();
294 EVT RegVT =
295 TLI.getRegisterType(*DAG.getContext(),
296 EVT::getIntegerVT(*DAG.getContext(),
297 StoredVT.getSizeInBits()));
298 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
299 unsigned RegBytes = RegVT.getSizeInBits() / 8;
300 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
301
302 // Make sure the stack slot is also aligned for the register type.
303 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
304
305 // Perform the original store, only redirected to the stack slot.
306 SDValue Store = DAG.getTruncStore(Chain, dl,
307 Val, StackPtr, MachinePointerInfo(),
308 StoredVT, false, false, 0);
309 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
310 SmallVector<SDValue, 8> Stores;
311 unsigned Offset = 0;
312
313 // Do all but one copies using the full register width.
314 for (unsigned i = 1; i < NumRegs; i++) {
315 // Load one integer register's worth from the stack slot.
316 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
317 MachinePointerInfo(),
318 false, false, 0);
319 // Store it to the final location. Remember the store.
320 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
321 ST->getPointerInfo().getWithOffset(Offset),
322 ST->isVolatile(), ST->isNonTemporal(),
323 MinAlign(ST->getAlignment(), Offset)));
324 // Increment the pointers.
325 Offset += RegBytes;
326 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
327 Increment);
328 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
329 }
330
331 // The last store may be partial. Do a truncating store. On big-endian
332 // machines this requires an extending load from the stack slot to ensure
333 // that the bits are in the right place.
334 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
335 8 * (StoredBytes - Offset));
336
337 // Load from the stack slot.
338 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
339 MachinePointerInfo(),
340 MemVT, false, false, 0);
341
342 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
343 ST->getPointerInfo()
344 .getWithOffset(Offset),
345 MemVT, ST->isVolatile(),
346 ST->isNonTemporal(),
347 MinAlign(ST->getAlignment(), Offset)));
348 // The order of the stores doesn't matter - say it with a TokenFactor.
Dan Gohman65fd6562011-11-03 21:49:52 +0000349 SDValue Result =
350 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
351 Stores.size());
352 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000353 DAG.RemoveDeadNode(ST, DUL);
Dan Gohman65fd6562011-11-03 21:49:52 +0000354 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000355 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000356 assert(ST->getMemoryVT().isInteger() &&
357 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000358 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000359 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000360 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000361 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000362 int IncrementSize = NumBits / 8;
363
364 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000365 SDValue ShiftAmount = DAG.getConstant(NumBits,
366 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000367 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000368 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000369
370 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000371 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000372 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000373 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000374 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000375 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000376 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000377 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000378 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000379 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000380 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
381 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000382
Dan Gohman65fd6562011-11-03 21:49:52 +0000383 SDValue Result =
384 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
385 DAG.ReplaceAllUsesWith(SDValue(ST, 0), Result, DUL);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000386 DAG.RemoveDeadNode(ST, DUL);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000387}
388
389/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
Dan Gohman65fd6562011-11-03 21:49:52 +0000390static void
391ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
392 const TargetLowering &TLI,
393 SDValue &ValResult, SDValue &ChainResult) {
Dan Gohman475871a2008-07-27 21:46:04 +0000394 SDValue Chain = LD->getChain();
395 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000396 EVT VT = LD->getValueType(0);
397 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000398 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000399 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000400 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000401 if (TLI.isTypeLegal(intVT)) {
402 // Expand to a (misaligned) integer load of the same size,
403 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000404 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
405 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000406 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000407 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000408 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000409 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000410
Dan Gohman65fd6562011-11-03 21:49:52 +0000411 ValResult = Result;
412 ChainResult = Chain;
413 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000414 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000415
Chris Lattnerecf42c42010-09-21 16:36:31 +0000416 // Copy the value to a (aligned) stack slot using (unaligned) integer
417 // loads and stores, then do a (aligned) load from the stack slot.
418 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
419 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
420 unsigned RegBytes = RegVT.getSizeInBits() / 8;
421 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
422
423 // Make sure the stack slot is also aligned for the register type.
424 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
425
426 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
427 SmallVector<SDValue, 8> Stores;
428 SDValue StackPtr = StackBase;
429 unsigned Offset = 0;
430
431 // Do all but one copies using the full register width.
432 for (unsigned i = 1; i < NumRegs; i++) {
433 // Load one integer register's worth from the original location.
434 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
435 LD->getPointerInfo().getWithOffset(Offset),
436 LD->isVolatile(), LD->isNonTemporal(),
437 MinAlign(LD->getAlignment(), Offset));
438 // Follow the load with a store to the stack slot. Remember the store.
439 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000440 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000441 // Increment the pointers.
442 Offset += RegBytes;
443 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
444 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
445 Increment);
446 }
447
448 // The last copy may be partial. Do an extending load.
449 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
450 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000451 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000452 LD->getPointerInfo().getWithOffset(Offset),
453 MemVT, LD->isVolatile(),
454 LD->isNonTemporal(),
455 MinAlign(LD->getAlignment(), Offset));
456 // Follow the load with a store to the stack slot. Remember the store.
457 // On big-endian machines this requires a truncating store to ensure
458 // that the bits end up in the right place.
459 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
460 MachinePointerInfo(), MemVT,
461 false, false, 0));
462
463 // The order of the stores doesn't matter - say it with a TokenFactor.
464 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
465 Stores.size());
466
467 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000468 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000469 MachinePointerInfo(), LoadedVT, false, false, 0);
470
471 // Callers expect a MERGE_VALUES node.
Dan Gohman65fd6562011-11-03 21:49:52 +0000472 ValResult = Load;
473 ChainResult = TF;
474 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000475 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000476 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000477 "Unaligned load of unsupported type.");
478
Dale Johannesen8155d642008-02-27 22:36:00 +0000479 // Compute the new VT that is half the size of the old one. This is an
480 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000481 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000482 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000483 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000484 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000485
Chris Lattnere400af82007-11-19 21:38:03 +0000486 unsigned Alignment = LD->getAlignment();
487 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000488 ISD::LoadExtType HiExtType = LD->getExtensionType();
489
490 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
491 if (HiExtType == ISD::NON_EXTLOAD)
492 HiExtType = ISD::ZEXTLOAD;
493
494 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000495 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000496 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000497 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000498 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000499 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000500 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000501 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000502 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000503 LD->getPointerInfo().getWithOffset(IncrementSize),
504 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000505 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000506 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000507 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000508 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000509 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000510 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000511 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000512 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000513 LD->getPointerInfo().getWithOffset(IncrementSize),
514 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000515 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000516 }
517
518 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000519 SDValue ShiftAmount = DAG.getConstant(NumBits,
520 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000521 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
522 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000523
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000525 Hi.getValue(1));
526
Dan Gohman65fd6562011-11-03 21:49:52 +0000527 ValResult = Result;
528 ChainResult = TF;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000529}
Evan Cheng912095b2007-01-04 21:56:39 +0000530
Nate Begeman68679912008-04-25 18:07:40 +0000531/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
532/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
533/// is necessary to spill the vector being inserted into to memory, perform
534/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000535SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000536PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
537 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000538 SDValue Tmp1 = Vec;
539 SDValue Tmp2 = Val;
540 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000541
Nate Begeman68679912008-04-25 18:07:40 +0000542 // If the target doesn't support this, we have to spill the input vector
543 // to a temporary stack slot, update the element, then reload it. This is
544 // badness. We could also load the value into a vector register (either
545 // with a "move to register" or "extload into register" instruction, then
546 // permute it into place, if the idx is a constant and if the idx is
547 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000548 EVT VT = Tmp1.getValueType();
549 EVT EltVT = VT.getVectorElementType();
550 EVT IdxVT = Tmp3.getValueType();
551 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000552 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000553
Evan Chengff89dcb2009-10-18 18:16:27 +0000554 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
555
Nate Begeman68679912008-04-25 18:07:40 +0000556 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000557 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000558 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000559 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000560
561 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000562 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000563 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000564 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000565 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000566 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
567 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000568 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000569 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000570 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000571 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000572 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000573 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000574}
575
Mon P Wange9f10152008-12-09 05:46:39 +0000576
Eli Friedman3f727d62009-05-27 02:16:40 +0000577SDValue SelectionDAGLegalize::
578ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
579 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
580 // SCALAR_TO_VECTOR requires that the type of the value being inserted
581 // match the element type of the vector being created, except for
582 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000583 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000584 if (Val.getValueType() == EltVT ||
585 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
586 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
587 Vec.getValueType(), Val);
588
589 unsigned NumElts = Vec.getValueType().getVectorNumElements();
590 // We generate a shuffle of InVec and ScVec, so the shuffle mask
591 // should be 0,1,2,3,4,5... with the appropriate element replaced with
592 // elt 0 of the RHS.
593 SmallVector<int, 8> ShufOps;
594 for (unsigned i = 0; i != NumElts; ++i)
595 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
596
597 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
598 &ShufOps[0]);
599 }
600 }
601 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
602}
603
Eli Friedman7ef3d172009-06-06 07:04:42 +0000604SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
605 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
606 // FIXME: We shouldn't do this for TargetConstantFP's.
607 // FIXME: move this to the DAG Combiner! Note that we can't regress due
608 // to phase ordering between legalized code and the dag combiner. This
609 // probably means that we need to integrate dag combiner and legalizer
610 // together.
611 // We generally can't do this one for long doubles.
612 SDValue Tmp1 = ST->getChain();
613 SDValue Tmp2 = ST->getBasePtr();
614 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000615 unsigned Alignment = ST->getAlignment();
616 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000617 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000618 DebugLoc dl = ST->getDebugLoc();
619 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 if (CFP->getValueType(0) == MVT::f32 &&
Dan Gohman75b10042011-07-15 22:39:09 +0000621 TLI.isTypeLegal(MVT::i32)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000622 Tmp3 = DAG.getConstant(CFP->getValueAPF().
623 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000625 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
626 isVolatile, isNonTemporal, Alignment);
627 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000628
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000629 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000630 // If this target supports 64-bit registers, do a single 64-bit store.
Dan Gohman75b10042011-07-15 22:39:09 +0000631 if (TLI.isTypeLegal(MVT::i64)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000632 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000634 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
635 isVolatile, isNonTemporal, Alignment);
636 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000637
Dan Gohman75b10042011-07-15 22:39:09 +0000638 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000639 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
640 // stores. If the target supports neither 32- nor 64-bits, this
641 // xform is certainly not worth it.
642 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000643 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000645 if (TLI.isBigEndian()) std::swap(Lo, Hi);
646
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000647 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
648 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000649 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
650 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000651 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
652 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000653 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000654
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000656 }
657 }
658 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000659 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000660}
661
Dan Gohman6a109f92011-07-15 21:42:20 +0000662/// LegalizeOp - Return a legal replacement for the given operation, with
663/// all legal operands.
Dan Gohman65fd6562011-11-03 21:49:52 +0000664void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
665 if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
666 return;
Scott Michelfdc40a02009-02-17 22:15:04 +0000667
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000668 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000669
Eli Friedman1fde9c52009-05-24 02:46:31 +0000670 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000671 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
672 TargetLowering::TypeLegal &&
Eli Friedman1fde9c52009-05-24 02:46:31 +0000673 "Unexpected illegal type!");
674
675 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000676 assert((TLI.getTypeAction(*DAG.getContext(),
677 Node->getOperand(i).getValueType()) ==
678 TargetLowering::TypeLegal ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000679 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
680 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000681
Dan Gohman475871a2008-07-27 21:46:04 +0000682 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +0000683 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000684
Eli Friedman8c377c72009-05-27 01:25:56 +0000685 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000686 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000687 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000688 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000689 case ISD::INTRINSIC_W_CHAIN:
690 case ISD::INTRINSIC_WO_CHAIN:
691 case ISD::INTRINSIC_VOID:
692 case ISD::VAARG:
693 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000695 break;
696 case ISD::SINT_TO_FP:
697 case ISD::UINT_TO_FP:
698 case ISD::EXTRACT_VECTOR_ELT:
699 Action = TLI.getOperationAction(Node->getOpcode(),
700 Node->getOperand(0).getValueType());
701 break;
702 case ISD::FP_ROUND_INREG:
703 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000704 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000705 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
706 break;
707 }
Eli Friedman327236c2011-08-24 20:50:09 +0000708 case ISD::ATOMIC_STORE: {
709 Action = TLI.getOperationAction(Node->getOpcode(),
710 Node->getOperand(2).getValueType());
711 break;
712 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000713 case ISD::SELECT_CC:
714 case ISD::SETCC:
715 case ISD::BR_CC: {
716 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
717 Node->getOpcode() == ISD::SETCC ? 2 : 1;
718 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000719 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000720 ISD::CondCode CCCode =
721 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
722 Action = TLI.getCondCodeAction(CCCode, OpVT);
723 if (Action == TargetLowering::Legal) {
724 if (Node->getOpcode() == ISD::SELECT_CC)
725 Action = TLI.getOperationAction(Node->getOpcode(),
726 Node->getValueType(0));
727 else
728 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
729 }
730 break;
731 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000732 case ISD::LOAD:
733 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000734 // FIXME: Model these properly. LOAD and STORE are complicated, and
735 // STORE expects the unlegalized operand in some cases.
736 SimpleFinishLegalizing = false;
737 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000738 case ISD::CALLSEQ_START:
739 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000740 // FIXME: This shouldn't be necessary. These nodes have special properties
741 // dealing with the recursive nature of legalization. Removing this
742 // special case should be done as part of making LegalizeDAG non-recursive.
743 SimpleFinishLegalizing = false;
744 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000745 case ISD::EXTRACT_ELEMENT:
746 case ISD::FLT_ROUNDS_:
747 case ISD::SADDO:
748 case ISD::SSUBO:
749 case ISD::UADDO:
750 case ISD::USUBO:
751 case ISD::SMULO:
752 case ISD::UMULO:
753 case ISD::FPOWI:
754 case ISD::MERGE_VALUES:
755 case ISD::EH_RETURN:
756 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000757 case ISD::EH_SJLJ_SETJMP:
758 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000759 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000760 // These operations lie about being legal: when they claim to be legal,
761 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000762 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
763 if (Action == TargetLowering::Legal)
764 Action = TargetLowering::Expand;
765 break;
Duncan Sands4a544a72011-09-06 13:37:06 +0000766 case ISD::INIT_TRAMPOLINE:
767 case ISD::ADJUST_TRAMPOLINE:
Eli Friedman8c377c72009-05-27 01:25:56 +0000768 case ISD::FRAMEADDR:
769 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000770 // These operations lie about being legal: when they claim to be legal,
771 // they should actually be custom-lowered.
772 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
773 if (Action == TargetLowering::Legal)
774 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000775 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000776 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000777 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000778 Action = TargetLowering::Legal;
779 } else {
780 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000781 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000782 break;
783 }
784
785 if (SimpleFinishLegalizing) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000786 SmallVector<SDValue, 8> Ops;
Eli Friedman8c377c72009-05-27 01:25:56 +0000787 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman65fd6562011-11-03 21:49:52 +0000788 Ops.push_back(Node->getOperand(i));
Eli Friedman8c377c72009-05-27 01:25:56 +0000789 switch (Node->getOpcode()) {
790 default: break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000791 case ISD::SHL:
792 case ISD::SRL:
793 case ISD::SRA:
794 case ISD::ROTL:
795 case ISD::ROTR:
796 // Legalizing shifts/rotates requires adjusting the shift amount
797 // to the appropriate width.
Dan Gohman65fd6562011-11-03 21:49:52 +0000798 if (!Ops[1].getValueType().isVector()) {
799 SDValue SAO = DAG.getShiftAmountOperand(Ops[0].getValueType(), Ops[1]);
800 HandleSDNode Handle(SAO);
801 LegalizeOp(SAO.getNode());
802 Ops[1] = Handle.getValue();
803 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000804 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000805 case ISD::SRL_PARTS:
806 case ISD::SRA_PARTS:
807 case ISD::SHL_PARTS:
808 // Legalizing shifts/rotates requires adjusting the shift amount
809 // to the appropriate width.
Dan Gohman65fd6562011-11-03 21:49:52 +0000810 if (!Ops[2].getValueType().isVector()) {
811 SDValue SAO = DAG.getShiftAmountOperand(Ops[0].getValueType(), Ops[2]);
812 HandleSDNode Handle(SAO);
813 LegalizeOp(SAO.getNode());
814 Ops[2] = Handle.getValue();
815 }
Dan Gohman2c9489d2009-08-18 23:52:48 +0000816 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000817 }
818
Dan Gohman65fd6562011-11-03 21:49:52 +0000819 SDNode *NewNode = DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
820 if (NewNode != Node) {
821 DAG.ReplaceAllUsesWith(Node, NewNode, this);
822 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
823 DAG.TransferDbgValues(SDValue(Node, i), SDValue(NewNode, i));
824 DAG.RemoveDeadNode(Node, this);
825 Node = NewNode;
826 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000827 switch (Action) {
828 case TargetLowering::Legal:
Dan Gohman65fd6562011-11-03 21:49:52 +0000829 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000830 case TargetLowering::Custom:
831 // FIXME: The handling for custom lowering with multiple results is
832 // a complete mess.
Dan Gohman65fd6562011-11-03 21:49:52 +0000833 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Eli Friedman8c377c72009-05-27 01:25:56 +0000834 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000835 SmallVector<SDValue, 8> ResultVals;
Eli Friedman8c377c72009-05-27 01:25:56 +0000836 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
837 if (e == 1)
838 ResultVals.push_back(Tmp1);
839 else
840 ResultVals.push_back(Tmp1.getValue(i));
841 }
Dan Gohman65fd6562011-11-03 21:49:52 +0000842 if (Tmp1.getNode() != Node || Tmp1.getResNo() != 0) {
843 DAG.ReplaceAllUsesWith(Node, ResultVals.data(), this);
844 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
845 DAG.TransferDbgValues(SDValue(Node, i), ResultVals[i]);
846 DAG.RemoveDeadNode(Node, this);
847 }
848 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000849 }
850
851 // FALL THROUGH
852 case TargetLowering::Expand:
Dan Gohman65fd6562011-11-03 21:49:52 +0000853 ExpandNode(Node);
854 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000855 case TargetLowering::Promote:
Dan Gohman65fd6562011-11-03 21:49:52 +0000856 PromoteNode(Node);
857 return;
Eli Friedman8c377c72009-05-27 01:25:56 +0000858 }
859 }
860
861 switch (Node->getOpcode()) {
862 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000863#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +0000864 dbgs() << "NODE: ";
865 Node->dump( &DAG);
866 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000867#endif
Chris Lattner35a38932010-04-07 23:47:51 +0000868 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000869
Dan Gohman65fd6562011-11-03 21:49:52 +0000870 case ISD::CALLSEQ_START:
Dan Gohman6f3ddef2011-10-29 00:41:52 +0000871 case ISD::CALLSEQ_END:
Dan Gohman65fd6562011-11-03 21:49:52 +0000872 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000873 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +0000874 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman65fd6562011-11-03 21:49:52 +0000875 Tmp1 = LD->getChain(); // Legalize the chain.
876 Tmp2 = LD->getBasePtr(); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000877
Evan Cheng466685d2006-10-09 20:57:25 +0000878 ISD::LoadExtType ExtType = LD->getExtensionType();
879 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +0000880 EVT VT = Node->getValueType(0);
Dan Gohman65fd6562011-11-03 21:49:52 +0000881 Node = DAG.UpdateNodeOperands(Node, Tmp1, Tmp2, LD->getOffset());
882 Tmp3 = SDValue(Node, 0);
883 Tmp4 = SDValue(Node, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000884
Evan Cheng466685d2006-10-09 20:57:25 +0000885 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +0000886 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000887 case TargetLowering::Legal:
888 // If this is an unaligned load and the target doesn't support it,
889 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +0000890 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000891 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
Evan Chenge96507c2009-08-15 08:38:52 +0000892 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000893 if (LD->getAlignment() < ABIAlignment){
Dan Gohman65fd6562011-11-03 21:49:52 +0000894 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
895 DAG, TLI, Tmp3, Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000896 }
897 }
898 break;
Evan Cheng466685d2006-10-09 20:57:25 +0000899 case TargetLowering::Custom:
900 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000901 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000902 Tmp3 = Tmp1;
903 Tmp4 = Tmp1.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000904 }
Evan Cheng466685d2006-10-09 20:57:25 +0000905 break;
906 case TargetLowering::Promote: {
907 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000908 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +0000909 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +0000910 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +0000911
Chris Lattnerecf42c42010-09-21 16:36:31 +0000912 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000913 LD->isVolatile(), LD->isNonTemporal(),
914 LD->getAlignment());
Dan Gohman65fd6562011-11-03 21:49:52 +0000915 Tmp3 = DAG.getNode(ISD::BITCAST, dl, VT, Tmp1);
916 Tmp4 = Tmp1.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000917 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000918 }
Evan Cheng466685d2006-10-09 20:57:25 +0000919 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000920 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +0000921 // legalized both of them.
Dan Gohman65fd6562011-11-03 21:49:52 +0000922 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp3);
923 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp4);
924 return;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000925 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000926
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000927 EVT SrcVT = LD->getMemoryVT();
928 unsigned SrcWidth = SrcVT.getSizeInBits();
929 unsigned Alignment = LD->getAlignment();
930 bool isVolatile = LD->isVolatile();
931 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000932
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000933 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
934 // Some targets pretend to have an i1 loading operation, and actually
935 // load an i8. This trick is correct for ZEXTLOAD because the top 7
936 // bits are guaranteed to be zero; it helps the optimizers understand
937 // that these bits are zero. It is also useful for EXTLOAD, since it
938 // tells the optimizers that those bits are undefined. It would be
939 // nice to have an effective generic way of getting these benefits...
940 // Until such a way is found, don't insist on promoting i1 here.
941 (SrcVT != MVT::i1 ||
942 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
943 // Promote to a byte-sized load if not loading an integral number of
944 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
945 unsigned NewWidth = SrcVT.getStoreSizeInBits();
946 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
947 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000948
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000949 // The extra bits are guaranteed to be zero, since we stored them that
950 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000951
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000952 ISD::LoadExtType NewExtType =
953 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000954
Dan Gohman65fd6562011-11-03 21:49:52 +0000955 SDValue Result =
956 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
957 Tmp1, Tmp2, LD->getPointerInfo(),
958 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000959
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000960 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000961
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000962 if (ExtType == ISD::SEXTLOAD)
963 // Having the top bits zero doesn't help when sign extending.
964 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
965 Result.getValueType(),
966 Result, DAG.getValueType(SrcVT));
967 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
968 // All the top bits are guaranteed to be zero - inform the optimizers.
969 Result = DAG.getNode(ISD::AssertZext, dl,
970 Result.getValueType(), Result,
971 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000972
Dan Gohman65fd6562011-11-03 21:49:52 +0000973 Tmp1 = Result;
974 Tmp2 = Ch;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000975 } else if (SrcWidth & (SrcWidth - 1)) {
976 // If not loading a power-of-2 number of bits, expand as two loads.
977 assert(!SrcVT.isVector() && "Unsupported extload!");
978 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
979 assert(RoundWidth < SrcWidth);
980 unsigned ExtraWidth = SrcWidth - RoundWidth;
981 assert(ExtraWidth < RoundWidth);
982 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
983 "Load size not an integral number of bytes!");
984 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
985 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
986 SDValue Lo, Hi, Ch;
987 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000988
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000989 if (TLI.isLittleEndian()) {
990 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
991 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000992 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000993 Tmp1, Tmp2,
994 LD->getPointerInfo(), RoundVT, isVolatile,
995 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000996
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000997 // Load the remaining ExtraWidth bits.
998 IncrementSize = RoundWidth / 8;
999 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1000 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001001 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001002 LD->getPointerInfo().getWithOffset(IncrementSize),
1003 ExtraVT, isVolatile, isNonTemporal,
1004 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001005
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001006 // Build a factor node to remember that this load is independent of
1007 // the other one.
1008 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1009 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001010
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001011 // Move the top bits to the right place.
1012 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001013 DAG.getConstant(RoundWidth,
1014 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001015
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001016 // Join the hi and lo parts.
Dan Gohman65fd6562011-11-03 21:49:52 +00001017 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001018 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001019 // Big endian - avoid unaligned loads.
1020 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1021 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001022 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001023 LD->getPointerInfo(), RoundVT, isVolatile,
1024 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001025
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001026 // Load the remaining ExtraWidth bits.
1027 IncrementSize = RoundWidth / 8;
1028 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1029 DAG.getIntPtrConstant(IncrementSize));
1030 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001031 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001032 LD->getPointerInfo().getWithOffset(IncrementSize),
1033 ExtraVT, isVolatile, isNonTemporal,
1034 MinAlign(Alignment, IncrementSize));
1035
1036 // Build a factor node to remember that this load is independent of
1037 // the other one.
1038 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1039 Hi.getValue(1));
1040
1041 // Move the top bits to the right place.
1042 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001043 DAG.getConstant(ExtraWidth,
1044 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001045
1046 // Join the hi and lo parts.
Dan Gohman65fd6562011-11-03 21:49:52 +00001047 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001048 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001049
Dan Gohman65fd6562011-11-03 21:49:52 +00001050 Tmp2 = Ch;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001051 } else {
1052 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1053 default: assert(0 && "This action is not supported yet!");
1054 case TargetLowering::Custom:
1055 isCustom = true;
1056 // FALLTHROUGH
1057 case TargetLowering::Legal:
Dan Gohman65fd6562011-11-03 21:49:52 +00001058 Node = DAG.UpdateNodeOperands(Node,
1059 Tmp1, Tmp2, LD->getOffset());
1060 Tmp1 = SDValue(Node, 0);
1061 Tmp2 = SDValue(Node, 1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001062
1063 if (isCustom) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001064 Tmp3 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001065 if (Tmp3.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001066 Tmp1 = Tmp3;
1067 Tmp2 = Tmp3.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001068 }
1069 } else {
1070 // If this is an unaligned load and the target doesn't support it,
1071 // expand it.
1072 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001073 Type *Ty =
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001074 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1075 unsigned ABIAlignment =
1076 TLI.getTargetData()->getABITypeAlignment(Ty);
1077 if (LD->getAlignment() < ABIAlignment){
Dan Gohman65fd6562011-11-03 21:49:52 +00001078 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
1079 DAG, TLI, Tmp1, Tmp2);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001080 }
1081 }
1082 }
1083 break;
1084 case TargetLowering::Expand:
Dan Gohman75b10042011-07-15 22:39:09 +00001085 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001086 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1087 LD->getPointerInfo(),
1088 LD->isVolatile(), LD->isNonTemporal(),
1089 LD->getAlignment());
1090 unsigned ExtendOp;
1091 switch (ExtType) {
1092 case ISD::EXTLOAD:
1093 ExtendOp = (SrcVT.isFloatingPoint() ?
1094 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1095 break;
1096 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1097 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1098 default: llvm_unreachable("Unexpected extend load type!");
1099 }
Dan Gohman65fd6562011-11-03 21:49:52 +00001100 Tmp1 = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1101 Tmp2 = Load.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001102 break;
1103 }
Nadav Rotemc2492c22011-06-14 08:11:52 +00001104
Nadav Roteme9b58d02011-10-15 07:41:10 +00001105 assert(!SrcVT.isVector() &&
1106 "Vector Loads are handled in LegalizeVectorOps");
Nadav Rotemc2492c22011-06-14 08:11:52 +00001107
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001108 // FIXME: This does not work for vectors on most targets. Sign- and
1109 // zero-extend operations are currently folded into extending loads,
1110 // whether they are legal or not, and then we end up here without any
1111 // support for legalizing them.
1112 assert(ExtType != ISD::EXTLOAD &&
1113 "EXTLOAD should always be supported!");
1114 // Turn the unsupported load into an EXTLOAD followed by an explicit
1115 // zero/sign extend inreg.
Dan Gohman65fd6562011-11-03 21:49:52 +00001116 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
1117 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1118 LD->isVolatile(), LD->isNonTemporal(),
1119 LD->getAlignment());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001120 SDValue ValRes;
1121 if (ExtType == ISD::SEXTLOAD)
1122 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1123 Result.getValueType(),
1124 Result, DAG.getValueType(SrcVT));
1125 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001126 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Dan Gohman65fd6562011-11-03 21:49:52 +00001127 Tmp1 = ValRes;
1128 Tmp2 = Result.getValue(1);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001129 break;
1130 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001131 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001132
1133 // Since loads produce two values, make sure to remember that we legalized
1134 // both of them.
Dan Gohman65fd6562011-11-03 21:49:52 +00001135 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp1);
1136 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp2);
1137 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001138 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001139 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001140 StoreSDNode *ST = cast<StoreSDNode>(Node);
Dan Gohman65fd6562011-11-03 21:49:52 +00001141 Tmp1 = ST->getChain();
1142 Tmp2 = ST->getBasePtr();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001143 unsigned Alignment = ST->getAlignment();
1144 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001145 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001146
Evan Cheng8b2794a2006-10-13 21:14:26 +00001147 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001148 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001149 DAG.ReplaceAllUsesWith(ST, OptStore, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001150 DAG.RemoveDeadNode(ST, this);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001151 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001152 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001153
Eli Friedman957bffa2009-05-24 08:42:01 +00001154 {
Dan Gohman65fd6562011-11-03 21:49:52 +00001155 Tmp3 = ST->getValue();
1156 Node = DAG.UpdateNodeOperands(Node,
1157 Tmp1, Tmp3, Tmp2,
1158 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001159
Owen Andersone50ed302009-08-10 22:56:29 +00001160 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001161 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001162 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001163 case TargetLowering::Legal:
1164 // If this is an unaligned store and the target doesn't support it,
1165 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001166 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001167 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001168 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001169 if (ST->getAlignment() < ABIAlignment)
Dan Gohman65fd6562011-11-03 21:49:52 +00001170 ExpandUnalignedStore(cast<StoreSDNode>(Node),
1171 DAG, TLI, this);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001172 }
1173 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001174 case TargetLowering::Custom:
Dan Gohman65fd6562011-11-03 21:49:52 +00001175 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001176 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001177 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Tmp1, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001178 DAG.RemoveDeadNode(Node, this);
1179 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001180 break;
Dan Gohman65fd6562011-11-03 21:49:52 +00001181 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001182 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001183 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001184 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dan Gohman65fd6562011-11-03 21:49:52 +00001185 SDValue Result =
1186 DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
1187 ST->getPointerInfo(), isVolatile,
1188 isNonTemporal, Alignment);
1189 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001190 DAG.RemoveDeadNode(Node, this);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001191 break;
1192 }
Dan Gohman65fd6562011-11-03 21:49:52 +00001193 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001194 break;
1195 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001196 } else {
Dan Gohman65fd6562011-11-03 21:49:52 +00001197 Tmp3 = ST->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001198
Owen Andersone50ed302009-08-10 22:56:29 +00001199 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001200 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001201
Duncan Sands83ec4b62008-06-06 12:08:01 +00001202 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001203 // Promote to a byte-sized store with upper bits zero if not
1204 // storing an integral number of bytes. For example, promote
1205 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001206 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1207 StVT.getStoreSizeInBits());
Nadav Rotema1c415c2011-09-27 10:48:29 +00001208 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Dan Gohman65fd6562011-11-03 21:49:52 +00001209 SDValue Result =
1210 DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1211 NVT, isVolatile, isNonTemporal, Alignment);
1212 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001213 DAG.RemoveDeadNode(Node, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001214 } else if (StWidth & (StWidth - 1)) {
1215 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001216 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001217 unsigned RoundWidth = 1 << Log2_32(StWidth);
1218 assert(RoundWidth < StWidth);
1219 unsigned ExtraWidth = StWidth - RoundWidth;
1220 assert(ExtraWidth < RoundWidth);
1221 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1222 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001223 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1224 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001225 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001226 unsigned IncrementSize;
1227
1228 if (TLI.isLittleEndian()) {
1229 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1230 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001231 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1232 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001233 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001234
1235 // Store the remaining ExtraWidth bits.
1236 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001237 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001238 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001239 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001240 DAG.getConstant(RoundWidth,
1241 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001242 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1243 ST->getPointerInfo().getWithOffset(IncrementSize),
1244 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001245 MinAlign(Alignment, IncrementSize));
1246 } else {
1247 // Big endian - avoid unaligned stores.
1248 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1249 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001250 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001251 DAG.getConstant(ExtraWidth,
1252 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001253 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1254 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001255
1256 // Store the remaining ExtraWidth bits.
1257 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001258 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001259 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001260 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1261 ST->getPointerInfo().getWithOffset(IncrementSize),
1262 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001263 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001264 }
Duncan Sands7e857202008-01-22 07:17:34 +00001265
1266 // The order of the stores doesn't matter.
Dan Gohman65fd6562011-11-03 21:49:52 +00001267 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
1268 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001269 DAG.RemoveDeadNode(Node, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001270 } else {
1271 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1272 Tmp2 != ST->getBasePtr())
Dan Gohman65fd6562011-11-03 21:49:52 +00001273 Node = DAG.UpdateNodeOperands(Node, Tmp1, Tmp3, Tmp2,
1274 ST->getOffset());
Duncan Sands7e857202008-01-22 07:17:34 +00001275
1276 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001277 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001278 case TargetLowering::Legal:
1279 // If this is an unaligned store and the target doesn't support it,
1280 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001281 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001282 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001283 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001284 if (ST->getAlignment() < ABIAlignment)
Dan Gohman65fd6562011-11-03 21:49:52 +00001285 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001286 }
1287 break;
1288 case TargetLowering::Custom:
Dan Gohman65fd6562011-11-03 21:49:52 +00001289 DAG.ReplaceAllUsesWith(SDValue(Node, 0),
1290 TLI.LowerOperation(SDValue(Node, 0), DAG),
1291 this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001292 DAG.RemoveDeadNode(Node, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001293 break;
Dan Gohmane63e5ab2011-07-15 22:51:43 +00001294 case TargetLowering::Expand:
Nadav Roteme9b58d02011-10-15 07:41:10 +00001295 assert(!StVT.isVector() &&
1296 "Vector Stores are handled in LegalizeVectorOps");
Nadav Rotemc2492c22011-06-14 08:11:52 +00001297
Duncan Sands7e857202008-01-22 07:17:34 +00001298 // TRUNCSTORE:i16 i32 -> STORE i16
Dan Gohman75b10042011-07-15 22:39:09 +00001299 assert(TLI.isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001300 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Dan Gohman65fd6562011-11-03 21:49:52 +00001301 SDValue Result =
1302 DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1303 isVolatile, isNonTemporal, Alignment);
1304 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00001305 DAG.RemoveDeadNode(Node, this);
Duncan Sands7e857202008-01-22 07:17:34 +00001306 break;
1307 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001308 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001309 }
1310 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001311 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001312 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001313}
1314
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001315SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1316 SDValue Vec = Op.getOperand(0);
1317 SDValue Idx = Op.getOperand(1);
1318 DebugLoc dl = Op.getDebugLoc();
1319 // Store the value to a temporary stack slot, then LOAD the returned part.
1320 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001321 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1322 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001323
1324 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001325 unsigned EltSize =
1326 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001327 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1328 DAG.getConstant(EltSize, Idx.getValueType()));
1329
1330 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1331 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1332 else
1333 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1334
1335 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1336
Eli Friedmanc680ac92009-07-09 22:01:03 +00001337 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001338 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001339 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001340 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001341 MachinePointerInfo(),
1342 Vec.getValueType().getVectorElementType(),
1343 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001344}
1345
David Greenecfe33c42011-01-26 19:13:22 +00001346SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1347 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1348
1349 SDValue Vec = Op.getOperand(0);
1350 SDValue Part = Op.getOperand(1);
1351 SDValue Idx = Op.getOperand(2);
1352 DebugLoc dl = Op.getDebugLoc();
1353
1354 // Store the value to a temporary stack slot, then LOAD the returned part.
1355
1356 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1357 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1358 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1359
1360 // First store the whole vector.
1361 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1362 false, false, 0);
1363
1364 // Then store the inserted part.
1365
1366 // Add the offset to the index.
1367 unsigned EltSize =
1368 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1369
1370 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1371 DAG.getConstant(EltSize, Idx.getValueType()));
1372
1373 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1374 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1375 else
1376 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1377
1378 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1379 StackPtr);
1380
1381 // Store the subvector.
1382 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1383 MachinePointerInfo(), false, false, 0);
1384
1385 // Finally, load the updated vector.
1386 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1387 false, false, 0);
1388}
1389
Eli Friedman7ef3d172009-06-06 07:04:42 +00001390SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1391 // We can't handle this case efficiently. Allocate a sufficiently
1392 // aligned object on the stack, store each element into it, then load
1393 // the result as a vector.
1394 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001395 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001396 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001397 DebugLoc dl = Node->getDebugLoc();
1398 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001399 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001400 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001401
1402 // Emit a store of each element to the stack slot.
1403 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001404 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001405 // Store (in the right endianness) the elements to memory.
1406 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1407 // Ignore undef elements.
1408 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1409
1410 unsigned Offset = TypeByteSize*i;
1411
1412 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1413 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1414
Dan Gohman9949dd62010-02-25 20:30:49 +00001415 // If the destination vector element type is narrower than the source
1416 // element type, only store the bits necessary.
1417 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001418 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001419 Node->getOperand(i), Idx,
1420 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001421 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001422 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001423 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001424 Node->getOperand(i), Idx,
1425 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001426 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001427 }
1428
1429 SDValue StoreChain;
1430 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001431 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001432 &Stores[0], Stores.size());
1433 else
1434 StoreChain = DAG.getEntryNode();
1435
1436 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001437 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001438}
1439
Eli Friedman4bc8c712009-05-27 12:20:41 +00001440SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1441 DebugLoc dl = Node->getDebugLoc();
1442 SDValue Tmp1 = Node->getOperand(0);
1443 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001444
1445 // Get the sign bit of the RHS. First obtain a value that has the same
1446 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001447 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001448 EVT FloatVT = Tmp2.getValueType();
1449 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Dan Gohman75b10042011-07-15 22:39:09 +00001450 if (TLI.isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001451 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001452 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001453 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001454 // Store the float to memory, then load the sign part out as an integer.
1455 MVT LoadTy = TLI.getPointerTy();
1456 // First create a temporary that is aligned for both the load and store.
1457 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1458 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001459 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001460 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001461 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001462 if (TLI.isBigEndian()) {
1463 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1464 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001465 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1466 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001467 } else { // Little endian
1468 SDValue LoadPtr = StackPtr;
1469 // The float may be wider than the integer we are going to load. Advance
1470 // the pointer so that the loaded integer will contain the sign bit.
1471 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1472 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1473 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1474 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1475 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001476 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1477 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001478 // Move the sign bit to the top bit of the loaded integer.
1479 unsigned BitShift = LoadTy.getSizeInBits() -
1480 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1481 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1482 if (BitShift)
1483 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001484 DAG.getConstant(BitShift,
1485 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001486 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001487 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001488 // Now get the sign bit proper, by seeing whether the value is negative.
1489 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1490 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1491 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001492 // Get the absolute value of the result.
1493 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1494 // Select between the nabs and abs value based on the sign bit of
1495 // the input.
1496 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1497 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1498 AbsVal);
1499}
1500
Eli Friedman4bc8c712009-05-27 12:20:41 +00001501void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1502 SmallVectorImpl<SDValue> &Results) {
1503 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1504 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1505 " not tell us which reg is the stack pointer!");
1506 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001507 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001508 SDValue Tmp1 = SDValue(Node, 0);
1509 SDValue Tmp2 = SDValue(Node, 1);
1510 SDValue Tmp3 = Node->getOperand(2);
1511 SDValue Chain = Tmp1.getOperand(0);
1512
1513 // Chain the dynamic stack allocation so that it doesn't modify the stack
1514 // pointer when other instructions are using the stack.
1515 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1516
1517 SDValue Size = Tmp2.getOperand(1);
1518 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1519 Chain = SP.getValue(1);
1520 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001521 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001522 if (Align > StackAlign)
1523 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1524 DAG.getConstant(-(uint64_t)Align, VT));
1525 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1526 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1527
1528 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1529 DAG.getIntPtrConstant(0, true), SDValue());
1530
1531 Results.push_back(Tmp1);
1532 Results.push_back(Tmp2);
1533}
1534
Evan Cheng7f042682008-10-15 02:05:31 +00001535/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001536/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001537/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001538void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001539 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001540 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001541 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001542 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001543 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1544 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001545 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001546 case TargetLowering::Legal:
1547 // Nothing to do.
1548 break;
1549 case TargetLowering::Expand: {
1550 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1551 unsigned Opc = 0;
1552 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001553 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001554 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1555 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1556 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1557 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1558 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1559 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1560 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1561 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1562 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1563 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1564 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1565 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001566 // FIXME: Implement more expansions.
1567 }
1568
Dale Johannesenbb5da912009-02-02 20:41:04 +00001569 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1570 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1571 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001572 RHS = SDValue();
1573 CC = SDValue();
1574 break;
1575 }
1576 }
1577}
1578
Chris Lattner1401d152008-01-16 07:45:30 +00001579/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1580/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1581/// a load from the stack slot to DestVT, extending it if needed.
1582/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001583SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001584 EVT SlotVT,
1585 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001586 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001587 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001588 unsigned SrcAlign =
1589 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001590 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001591 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001592
Evan Chengff89dcb2009-10-18 18:16:27 +00001593 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1594 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001595 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001596
Duncan Sands83ec4b62008-06-06 12:08:01 +00001597 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1598 unsigned SlotSize = SlotVT.getSizeInBits();
1599 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001600 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Evan Chengadf97992010-04-15 01:25:27 +00001601 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001602
Chris Lattner1401d152008-01-16 07:45:30 +00001603 // Emit a store to the stack slot. Use a truncstore if the input value is
1604 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001605 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001606
Chris Lattner1401d152008-01-16 07:45:30 +00001607 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001608 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001609 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001610 else {
1611 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001612 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001613 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001614 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001615
Chris Lattner35481892005-12-23 00:16:34 +00001616 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001617 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001618 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001619 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001620
Chris Lattner1401d152008-01-16 07:45:30 +00001621 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00001622 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001623 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001624}
1625
Dan Gohman475871a2008-07-27 21:46:04 +00001626SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001627 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001628 // Create a vector sized/aligned stack slot, store the value to element #0,
1629 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001630 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001631
Evan Chengff89dcb2009-10-18 18:16:27 +00001632 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1633 int SPFI = StackPtrFI->getIndex();
1634
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001635 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1636 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001637 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001638 Node->getValueType(0).getVectorElementType(),
1639 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001640 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001641 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001642 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001643}
1644
1645
Chris Lattnerce872152006-03-19 06:31:19 +00001646/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001647/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001648SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001649 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001650 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001651 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001652 EVT VT = Node->getValueType(0);
1653 EVT OpVT = Node->getOperand(0).getValueType();
1654 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001655
1656 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001657 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001658 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001659 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001660 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001661 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001662 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001663 if (V.getOpcode() == ISD::UNDEF)
1664 continue;
1665 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001666 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001667 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001668 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001669
1670 if (!Value1.getNode()) {
1671 Value1 = V;
1672 } else if (!Value2.getNode()) {
1673 if (V != Value1)
1674 Value2 = V;
1675 } else if (V != Value1 && V != Value2) {
1676 MoreThanTwoValues = true;
1677 }
Chris Lattnerce872152006-03-19 06:31:19 +00001678 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001679
Eli Friedman7a5e5552009-06-07 06:52:44 +00001680 if (!Value1.getNode())
1681 return DAG.getUNDEF(VT);
1682
1683 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001684 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001685
Chris Lattner2eb86532006-03-24 07:29:17 +00001686 // If all elements are constants, create a load from the constant pool.
1687 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001688 std::vector<Constant*> CV;
1689 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001690 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001691 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001692 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001693 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001694 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001695 if (OpVT==EltVT)
1696 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1697 else {
1698 // If OpVT and EltVT don't match, EltVT is not legal and the
1699 // element values have been promoted/truncated earlier. Undo this;
1700 // we don't want a v16i8 to become a v16i32 for example.
1701 const ConstantInt *CI = V->getConstantIntValue();
1702 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1703 CI->getZExtValue()));
1704 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001705 } else {
1706 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001707 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001708 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001709 }
1710 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001711 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001712 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001713 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001714 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001715 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001716 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001717 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Eli Friedman7a5e5552009-06-07 06:52:44 +00001719 if (!MoreThanTwoValues) {
1720 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1721 for (unsigned i = 0; i < NumElems; ++i) {
1722 SDValue V = Node->getOperand(i);
1723 if (V.getOpcode() == ISD::UNDEF)
1724 continue;
1725 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1726 }
1727 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001728 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001729 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1730 SDValue Vec2;
1731 if (Value2.getNode())
1732 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1733 else
1734 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001735
Chris Lattner87100e02006-03-20 01:52:29 +00001736 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001737 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001738 }
1739 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
Eli Friedman7ef3d172009-06-06 07:04:42 +00001741 // Otherwise, we can't handle this case efficiently.
1742 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001743}
1744
Chris Lattner77e77a62005-01-21 06:05:23 +00001745// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1746// does not fit into a register, return the lo part and set the hi part to the
1747// by-reg argument. If it does fit into a single register, return the result
1748// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001749SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001750 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001751 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00001752 // Legalizing the call will automatically add the previous call to the
1753 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00001754 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001755
Chris Lattner77e77a62005-01-21 06:05:23 +00001756 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00001757 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00001758 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001759 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001760 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00001761 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001762 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00001763 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00001764 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00001765 }
Bill Wendling056292f2008-09-16 21:48:12 +00001766 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00001767 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001768
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001769 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00001770
1771 // isTailCall may be true since the callee does not reference caller stack
1772 // frame. Check if it's in the right position.
1773 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001774 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00001775 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001776 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001777 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001778 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00001779
Evan Cheng3d2125c2010-11-30 23:55:39 +00001780 if (!CallInfo.second.getNode())
1781 // It's a tailcall, return the chain (which is the DAG root).
1782 return DAG.getRoot();
1783
Eli Friedman74807f22009-05-26 08:55:52 +00001784 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00001785}
1786
Dan Gohmanf316eb72011-05-16 22:09:53 +00001787/// ExpandLibCall - Generate a libcall taking the given operands as arguments
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001788/// and returning a result of type RetVT.
1789SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
1790 const SDValue *Ops, unsigned NumOps,
1791 bool isSigned, DebugLoc dl) {
1792 TargetLowering::ArgListTy Args;
1793 Args.reserve(NumOps);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001794
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001795 TargetLowering::ArgListEntry Entry;
1796 for (unsigned i = 0; i != NumOps; ++i) {
1797 Entry.Node = Ops[i];
1798 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
1799 Entry.isSExt = isSigned;
1800 Entry.isZExt = !isSigned;
1801 Args.push_back(Entry);
1802 }
1803 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1804 TLI.getPointerTy());
Dan Gohmanf316eb72011-05-16 22:09:53 +00001805
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001806 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001807 std::pair<SDValue,SDValue> CallInfo =
1808 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
1809 false, 0, TLI.getLibcallCallingConv(LC), false,
1810 /*isReturnValueUsed=*/true,
1811 Callee, Args, DAG, dl);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001812
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001813 return CallInfo.first;
1814}
1815
Jim Grosbache03262f2010-06-18 21:43:38 +00001816// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
1817// ExpandLibCall except that the first operand is the in-chain.
1818std::pair<SDValue, SDValue>
1819SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
1820 SDNode *Node,
1821 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00001822 SDValue InChain = Node->getOperand(0);
1823
1824 TargetLowering::ArgListTy Args;
1825 TargetLowering::ArgListEntry Entry;
1826 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
1827 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001828 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001829 Entry.Node = Node->getOperand(i);
1830 Entry.Ty = ArgTy;
1831 Entry.isSExt = isSigned;
1832 Entry.isZExt = !isSigned;
1833 Args.push_back(Entry);
1834 }
1835 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1836 TLI.getPointerTy());
1837
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001838 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001839 std::pair<SDValue, SDValue> CallInfo =
1840 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001841 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00001842 /*isReturnValueUsed=*/true,
1843 Callee, Args, DAG, Node->getDebugLoc());
1844
Jim Grosbache03262f2010-06-18 21:43:38 +00001845 return CallInfo;
1846}
1847
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001848SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
1849 RTLIB::Libcall Call_F32,
1850 RTLIB::Libcall Call_F64,
1851 RTLIB::Libcall Call_F80,
1852 RTLIB::Libcall Call_PPCF128) {
1853 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001854 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00001855 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 case MVT::f32: LC = Call_F32; break;
1857 case MVT::f64: LC = Call_F64; break;
1858 case MVT::f80: LC = Call_F80; break;
1859 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001860 }
1861 return ExpandLibCall(LC, Node, false);
1862}
1863
1864SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001865 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001866 RTLIB::Libcall Call_I16,
1867 RTLIB::Libcall Call_I32,
1868 RTLIB::Libcall Call_I64,
1869 RTLIB::Libcall Call_I128) {
1870 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00001872 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001873 case MVT::i8: LC = Call_I8; break;
1874 case MVT::i16: LC = Call_I16; break;
1875 case MVT::i32: LC = Call_I32; break;
1876 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001877 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001878 }
1879 return ExpandLibCall(LC, Node, isSigned);
1880}
1881
Evan Cheng65279cb2011-04-16 03:08:26 +00001882/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
1883static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
1884 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001885 RTLIB::Libcall LC;
1886 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1887 default: assert(0 && "Unexpected request for libcall!");
1888 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1889 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1890 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1891 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1892 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
1893 }
1894
Evan Cheng65279cb2011-04-16 03:08:26 +00001895 return TLI.getLibcallName(LC) != 0;
1896}
Evan Cheng8e23e812011-04-01 00:42:02 +00001897
Evan Cheng65279cb2011-04-16 03:08:26 +00001898/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
1899/// needed.
1900static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001901 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00001902 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00001903 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001904 else
Evan Cheng8e23e812011-04-01 00:42:02 +00001905 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001906
Evan Cheng8e23e812011-04-01 00:42:02 +00001907 SDValue Op0 = Node->getOperand(0);
1908 SDValue Op1 = Node->getOperand(1);
1909 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
1910 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
1911 SDNode *User = *UI;
1912 if (User == Node)
1913 continue;
1914 if (User->getOpcode() == OtherOpcode &&
1915 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00001916 User->getOperand(1) == Op1)
1917 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00001918 }
Evan Cheng65279cb2011-04-16 03:08:26 +00001919 return false;
1920}
Evan Cheng8e23e812011-04-01 00:42:02 +00001921
Evan Cheng65279cb2011-04-16 03:08:26 +00001922/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
1923/// pairs.
1924void
1925SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
1926 SmallVectorImpl<SDValue> &Results) {
1927 unsigned Opcode = Node->getOpcode();
1928 bool isSigned = Opcode == ISD::SDIVREM;
1929
1930 RTLIB::Libcall LC;
1931 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
1932 default: assert(0 && "Unexpected request for libcall!");
1933 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1934 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1935 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1936 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1937 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00001938 }
1939
1940 // The input chain to this libcall is the entry node of the function.
1941 // Legalizing the call will automatically add the previous call to the
1942 // dependence.
1943 SDValue InChain = DAG.getEntryNode();
1944
1945 EVT RetVT = Node->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001946 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001947
1948 TargetLowering::ArgListTy Args;
1949 TargetLowering::ArgListEntry Entry;
1950 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1951 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001952 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001953 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
1954 Entry.isSExt = isSigned;
1955 Entry.isZExt = !isSigned;
1956 Args.push_back(Entry);
1957 }
1958
1959 // Also pass the return address of the remainder.
1960 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
1961 Entry.Node = FIPtr;
1962 Entry.Ty = RetTy->getPointerTo();
1963 Entry.isSExt = isSigned;
1964 Entry.isZExt = !isSigned;
1965 Args.push_back(Entry);
1966
1967 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1968 TLI.getPointerTy());
1969
Evan Cheng8e23e812011-04-01 00:42:02 +00001970 DebugLoc dl = Node->getDebugLoc();
1971 std::pair<SDValue, SDValue> CallInfo =
1972 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
1973 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
1974 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
1975
Evan Cheng8e23e812011-04-01 00:42:02 +00001976 // Remainder is loaded back from the stack frame.
Dan Gohman65fd6562011-11-03 21:49:52 +00001977 SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
Evan Cheng8e23e812011-04-01 00:42:02 +00001978 MachinePointerInfo(), false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00001979 Results.push_back(CallInfo.first);
1980 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00001981}
1982
Chris Lattner22cde6a2006-01-28 08:25:58 +00001983/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
1984/// INT_TO_FP operation of the specified operand when the target requests that
1985/// we expand it. At this point, we know that the result and operand types are
1986/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00001987SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
1988 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00001989 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00001990 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00001992 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00001993
Chris Lattner23594d42008-01-16 07:03:22 +00001994 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00001996
Chris Lattner22cde6a2006-01-28 08:25:58 +00001997 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00001999 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002001 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002002 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002003 if (TLI.isLittleEndian())
2004 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002005
Chris Lattner22cde6a2006-01-28 08:25:58 +00002006 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002008 if (isSigned) {
2009 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002010 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2011 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002012 } else {
2013 Op0Mapped = Op0;
2014 }
2015 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002016 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002017 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002018 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002019 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002020 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002021 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002022 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2023 MachinePointerInfo(),
2024 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002025 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002026 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2027 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002028 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002030 BitsToDouble(0x4330000080000000ULL) :
2031 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002033 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002035 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002036 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002037 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002039 // do nothing
2040 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002041 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002042 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002043 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002045 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002046 }
2047 return Result;
2048 }
2049 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002050 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002051
2052 // Implementation of unsigned i64 to f64 following the algorithm in
2053 // __floatundidf in compiler_rt. This implementation has the advantage
2054 // of performing rounding correctly, both in the default rounding mode
2055 // and in all alternate rounding modes.
2056 // TODO: Generalize this for use with other types.
2057 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2058 SDValue TwoP52 =
2059 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2060 SDValue TwoP84PlusTwoP52 =
2061 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2062 SDValue TwoP84 =
2063 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2064
2065 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2066 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2067 DAG.getConstant(32, MVT::i64));
2068 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2069 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002070 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2071 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002072 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2073 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002074 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2075 }
2076
Owen Anderson3a9e7692010-10-05 17:24:05 +00002077 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002078 // TODO: Generalize this for use with other types.
2079 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002080 // For unsigned conversions, convert them to signed conversions using the
2081 // algorithm from the x86_64 __floatundidf in compiler_rt.
2082 if (!isSigned) {
2083 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002084
Owen Anderson95771af2011-02-25 21:41:48 +00002085 SDValue ShiftConst =
2086 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002087 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2088 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2089 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2090 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002091
Owen Anderson3a9e7692010-10-05 17:24:05 +00002092 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2093 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002094
Owen Anderson3a9e7692010-10-05 17:24:05 +00002095 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002096 // select. We happen to get lucky and machinesink does the right
2097 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002098 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002099 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002100 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2101 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2102 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002103
Owen Anderson3a9e7692010-10-05 17:24:05 +00002104 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002105
Jim Grosbach6e992612010-07-02 17:41:59 +00002106 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002107 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2108 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2109 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002110 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002111 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2112 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2113 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2114 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2115 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2116 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002117 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002118 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002119 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002120
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002121 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2122 DAG.getConstant(32, SHVT));
2123 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2124 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2125 SDValue TwoP32 =
2126 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2127 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2128 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2129 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2130 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2131 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2132 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002133 }
2134
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002135 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002136
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002137 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2138 Op0, DAG.getConstant(0, Op0.getValueType()),
2139 ISD::SETLT);
2140 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2141 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2142 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002143
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002144 // If the sign bit of the integer is set, the large number will be treated
2145 // as a negative number. To counteract this, the dynamic code adds an
2146 // offset depending on the data type.
2147 uint64_t FF;
2148 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002149 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002150 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2151 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2152 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2153 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2154 }
2155 if (TLI.isLittleEndian()) FF <<= 32;
2156 Constant *FudgeFactor = ConstantInt::get(
2157 Type::getInt64Ty(*DAG.getContext()), FF);
2158
2159 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2160 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2161 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2162 Alignment = std::min(Alignment, 4u);
2163 SDValue FudgeInReg;
2164 if (DestVT == MVT::f32)
2165 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002166 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002167 false, false, Alignment);
2168 else {
Dan Gohman65fd6562011-11-03 21:49:52 +00002169 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
2170 DAG.getEntryNode(), CPIdx,
2171 MachinePointerInfo::getConstantPool(),
2172 MVT::f32, false, false, Alignment);
2173 HandleSDNode Handle(Load);
2174 LegalizeOp(Load.getNode());
2175 FudgeInReg = Handle.getValue();
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002176 }
2177
2178 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002179}
2180
2181/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2182/// *INT_TO_FP operation of the specified operand when the target requests that
2183/// we promote it. At this point, we know that the result and operand types are
2184/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2185/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002186SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002187 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002188 bool isSigned,
2189 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002190 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002191 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002192
2193 unsigned OpToUse = 0;
2194
2195 // Scan for the appropriate larger type to use.
2196 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002197 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002198 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002199
2200 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002201 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2202 OpToUse = ISD::SINT_TO_FP;
2203 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002204 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002205 if (isSigned) continue;
2206
2207 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002208 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2209 OpToUse = ISD::UINT_TO_FP;
2210 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002211 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002212
2213 // Otherwise, try a larger type.
2214 }
2215
2216 // Okay, we found the operation and type to use. Zero extend our input to the
2217 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002218 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002219 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002220 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002221}
2222
2223/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2224/// FP_TO_*INT operation of the specified operand when the target requests that
2225/// we promote it. At this point, we know that the result and operand types are
2226/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2227/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002228SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002229 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002230 bool isSigned,
2231 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002232 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002233 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002234
2235 unsigned OpToUse = 0;
2236
2237 // Scan for the appropriate larger type to use.
2238 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002239 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002240 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002241
Eli Friedman3be2e512009-05-28 03:06:16 +00002242 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002243 OpToUse = ISD::FP_TO_SINT;
2244 break;
2245 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002246
Eli Friedman3be2e512009-05-28 03:06:16 +00002247 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002248 OpToUse = ISD::FP_TO_UINT;
2249 break;
2250 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002251
2252 // Otherwise, try a larger type.
2253 }
2254
Scott Michelfdc40a02009-02-17 22:15:04 +00002255
Chris Lattner27a6c732007-11-24 07:07:01 +00002256 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002257 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002258
Chris Lattner27a6c732007-11-24 07:07:01 +00002259 // Truncate the result of the extended FP_TO_*INT operation to the desired
2260 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002261 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002262}
2263
2264/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2265///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002266SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002267 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002268 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002269 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002270 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002271 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002272 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002273 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2274 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2275 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002276 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002277 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2278 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2279 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2280 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2281 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2282 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2283 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2284 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2285 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002286 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002287 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2288 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2289 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2290 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2291 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2292 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2293 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2294 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2295 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2296 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2297 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2298 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2299 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2300 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2301 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2302 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2303 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2304 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2305 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2306 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2307 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002308 }
2309}
2310
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002311/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002312// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002313static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2314 APInt Val = APInt(NumBits, ByteVal);
2315 unsigned Shift = 8;
2316 for (unsigned i = NumBits; i > 8; i >>= 1) {
2317 Val = (Val << Shift) | Val;
2318 Shift <<= 1;
2319 }
2320 return Val;
2321}
2322
Chris Lattner22cde6a2006-01-28 08:25:58 +00002323/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2324///
Scott Michelfdc40a02009-02-17 22:15:04 +00002325SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002326 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002327 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002328 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002329 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002330 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002331 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002332 unsigned Len = VT.getSizeInBits();
2333
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002334 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2335 "CTPOP not implemented for this type.");
2336
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002337 // This is the "best" algorithm from
2338 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2339
2340 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2341 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2342 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2343 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2344
2345 // v = v - ((v >> 1) & 0x55555555...)
2346 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2347 DAG.getNode(ISD::AND, dl, VT,
2348 DAG.getNode(ISD::SRL, dl, VT, Op,
2349 DAG.getConstant(1, ShVT)),
2350 Mask55));
2351 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2352 Op = DAG.getNode(ISD::ADD, dl, VT,
2353 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2354 DAG.getNode(ISD::AND, dl, VT,
2355 DAG.getNode(ISD::SRL, dl, VT, Op,
2356 DAG.getConstant(2, ShVT)),
2357 Mask33));
2358 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2359 Op = DAG.getNode(ISD::AND, dl, VT,
2360 DAG.getNode(ISD::ADD, dl, VT, Op,
2361 DAG.getNode(ISD::SRL, dl, VT, Op,
2362 DAG.getConstant(4, ShVT))),
2363 Mask0F);
2364 // v = (v * 0x01010101...) >> (Len - 8)
2365 Op = DAG.getNode(ISD::SRL, dl, VT,
2366 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2367 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002368
Chris Lattner22cde6a2006-01-28 08:25:58 +00002369 return Op;
2370 }
2371 case ISD::CTLZ: {
2372 // for now, we do this:
2373 // x = x | (x >> 1);
2374 // x = x | (x >> 2);
2375 // ...
2376 // x = x | (x >>16);
2377 // x = x | (x >>32); // for 64-bit input
2378 // return popcount(~x);
2379 //
2380 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002381 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002382 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002383 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002384 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002385 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002386 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002387 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002388 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002389 Op = DAG.getNOT(dl, Op, VT);
2390 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002391 }
2392 case ISD::CTTZ: {
2393 // for now, we use: { return popcount(~x & (x - 1)); }
2394 // unless the target has ctlz but not ctpop, in which case we use:
2395 // { return 32 - nlz(~x & (x-1)); }
2396 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002397 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002398 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2399 DAG.getNOT(dl, Op, VT),
2400 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002401 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002402 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002403 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2404 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002405 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002406 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002407 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2408 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002409 }
2410 }
2411}
Chris Lattnere34b3962005-01-19 04:19:40 +00002412
Jim Grosbache03262f2010-06-18 21:43:38 +00002413std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2414 unsigned Opc = Node->getOpcode();
2415 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2416 RTLIB::Libcall LC;
2417
2418 switch (Opc) {
2419 default:
2420 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2421 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002422 case ISD::ATOMIC_SWAP:
2423 switch (VT.SimpleTy) {
2424 default: llvm_unreachable("Unexpected value type for atomic!");
2425 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2426 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2427 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2428 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2429 }
2430 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002431 case ISD::ATOMIC_CMP_SWAP:
2432 switch (VT.SimpleTy) {
2433 default: llvm_unreachable("Unexpected value type for atomic!");
2434 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2435 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2436 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2437 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2438 }
2439 break;
2440 case ISD::ATOMIC_LOAD_ADD:
2441 switch (VT.SimpleTy) {
2442 default: llvm_unreachable("Unexpected value type for atomic!");
2443 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2444 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2445 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2446 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2447 }
2448 break;
2449 case ISD::ATOMIC_LOAD_SUB:
2450 switch (VT.SimpleTy) {
2451 default: llvm_unreachable("Unexpected value type for atomic!");
2452 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2453 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2454 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2455 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2456 }
2457 break;
2458 case ISD::ATOMIC_LOAD_AND:
2459 switch (VT.SimpleTy) {
2460 default: llvm_unreachable("Unexpected value type for atomic!");
2461 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2462 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2463 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2464 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2465 }
2466 break;
2467 case ISD::ATOMIC_LOAD_OR:
2468 switch (VT.SimpleTy) {
2469 default: llvm_unreachable("Unexpected value type for atomic!");
2470 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2471 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2472 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2473 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2474 }
2475 break;
2476 case ISD::ATOMIC_LOAD_XOR:
2477 switch (VT.SimpleTy) {
2478 default: llvm_unreachable("Unexpected value type for atomic!");
2479 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2480 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2481 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2482 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2483 }
2484 break;
2485 case ISD::ATOMIC_LOAD_NAND:
2486 switch (VT.SimpleTy) {
2487 default: llvm_unreachable("Unexpected value type for atomic!");
2488 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2489 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2490 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2491 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2492 }
2493 break;
2494 }
2495
2496 return ExpandChainLibCall(LC, Node, false);
2497}
2498
Dan Gohman65fd6562011-11-03 21:49:52 +00002499void SelectionDAGLegalize::ExpandNode(SDNode *Node) {
2500 SmallVector<SDValue, 8> Results;
Eli Friedman8c377c72009-05-27 01:25:56 +00002501 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002502 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002503 switch (Node->getOpcode()) {
2504 case ISD::CTPOP:
2505 case ISD::CTLZ:
2506 case ISD::CTTZ:
2507 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2508 Results.push_back(Tmp1);
2509 break;
2510 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002511 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002512 break;
2513 case ISD::FRAMEADDR:
2514 case ISD::RETURNADDR:
2515 case ISD::FRAME_TO_ARGS_OFFSET:
2516 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2517 break;
2518 case ISD::FLT_ROUNDS_:
2519 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2520 break;
2521 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002522 case ISD::EH_LABEL:
2523 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002524 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002525 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002526 case ISD::EH_SJLJ_DISPATCHSETUP:
2527 // If the target didn't expand these, there's nothing to do, so just
2528 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002529 Results.push_back(Node->getOperand(0));
2530 break;
2531 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002532 // If the target didn't expand this, just return 'zero' and preserve the
2533 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002534 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002535 Results.push_back(Node->getOperand(0));
2536 break;
Eli Friedman14648462011-07-27 22:21:52 +00002537 case ISD::ATOMIC_FENCE:
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002538 case ISD::MEMBARRIER: {
2539 // If the target didn't lower this, lower it to '__sync_synchronize()' call
Eli Friedman14648462011-07-27 22:21:52 +00002540 // FIXME: handle "fence singlethread" more efficiently.
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002541 TargetLowering::ArgListTy Args;
2542 std::pair<SDValue, SDValue> CallResult =
2543 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002544 false, false, false, false, 0, CallingConv::C,
2545 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002546 /*isReturnValueUsed=*/true,
2547 DAG.getExternalSymbol("__sync_synchronize",
2548 TLI.getPointerTy()),
2549 Args, DAG, dl);
2550 Results.push_back(CallResult.second);
2551 break;
2552 }
Eli Friedman069e2ed2011-08-26 02:59:24 +00002553 case ISD::ATOMIC_LOAD: {
2554 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
Eli Friedman331120b2011-09-15 21:20:49 +00002555 SDValue Zero = DAG.getConstant(0, Node->getValueType(0));
Eli Friedman069e2ed2011-08-26 02:59:24 +00002556 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
2557 cast<AtomicSDNode>(Node)->getMemoryVT(),
2558 Node->getOperand(0),
2559 Node->getOperand(1), Zero, Zero,
2560 cast<AtomicSDNode>(Node)->getMemOperand(),
2561 cast<AtomicSDNode>(Node)->getOrdering(),
2562 cast<AtomicSDNode>(Node)->getSynchScope());
2563 Results.push_back(Swap.getValue(0));
2564 Results.push_back(Swap.getValue(1));
2565 break;
2566 }
2567 case ISD::ATOMIC_STORE: {
2568 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
2569 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
2570 cast<AtomicSDNode>(Node)->getMemoryVT(),
2571 Node->getOperand(0),
2572 Node->getOperand(1), Node->getOperand(2),
2573 cast<AtomicSDNode>(Node)->getMemOperand(),
2574 cast<AtomicSDNode>(Node)->getOrdering(),
2575 cast<AtomicSDNode>(Node)->getSynchScope());
2576 Results.push_back(Swap.getValue(1));
2577 break;
2578 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002579 // By default, atomic intrinsics are marked Legal and lowered. Targets
2580 // which don't support them directly, however, may want libcalls, in which
2581 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002582 case ISD::ATOMIC_SWAP:
2583 case ISD::ATOMIC_LOAD_ADD:
2584 case ISD::ATOMIC_LOAD_SUB:
2585 case ISD::ATOMIC_LOAD_AND:
2586 case ISD::ATOMIC_LOAD_OR:
2587 case ISD::ATOMIC_LOAD_XOR:
2588 case ISD::ATOMIC_LOAD_NAND:
2589 case ISD::ATOMIC_LOAD_MIN:
2590 case ISD::ATOMIC_LOAD_MAX:
2591 case ISD::ATOMIC_LOAD_UMIN:
2592 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002593 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002594 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2595 Results.push_back(Tmp.first);
2596 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002597 break;
Evan Chenga8457062010-06-18 22:01:37 +00002598 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002599 case ISD::DYNAMIC_STACKALLOC:
2600 ExpandDYNAMIC_STACKALLOC(Node, Results);
2601 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002602 case ISD::MERGE_VALUES:
2603 for (unsigned i = 0; i < Node->getNumValues(); i++)
2604 Results.push_back(Node->getOperand(i));
2605 break;
2606 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002607 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002608 if (VT.isInteger())
2609 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002610 else {
2611 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002612 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002613 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002614 break;
2615 }
2616 case ISD::TRAP: {
2617 // If this operation is not supported, lower it to 'abort()' call
2618 TargetLowering::ArgListTy Args;
2619 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002620 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002621 false, false, false, false, 0, CallingConv::C,
2622 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002623 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002624 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002625 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002626 Results.push_back(CallResult.second);
2627 break;
2628 }
2629 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002630 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002631 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2632 Node->getValueType(0), dl);
2633 Results.push_back(Tmp1);
2634 break;
2635 case ISD::FP_EXTEND:
2636 Tmp1 = EmitStackConvert(Node->getOperand(0),
2637 Node->getOperand(0).getValueType(),
2638 Node->getValueType(0), dl);
2639 Results.push_back(Tmp1);
2640 break;
2641 case ISD::SIGN_EXTEND_INREG: {
2642 // NOTE: we could fall back on load/store here too for targets without
2643 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002644 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002645 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00002646 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00002647 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002648 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002649 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2650 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002651 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002652 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2653 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002654 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2655 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002656 break;
2657 }
2658 case ISD::FP_ROUND_INREG: {
2659 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002660 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00002661
2662 // NOTE: there is a choice here between constantly creating new stack
2663 // slots and always reusing the same one. We currently always create
2664 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002665 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002666 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2667 Node->getValueType(0), dl);
2668 Results.push_back(Tmp1);
2669 break;
2670 }
2671 case ISD::SINT_TO_FP:
2672 case ISD::UINT_TO_FP:
2673 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2674 Node->getOperand(0), Node->getValueType(0), dl);
2675 Results.push_back(Tmp1);
2676 break;
2677 case ISD::FP_TO_UINT: {
2678 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002679 EVT VT = Node->getOperand(0).getValueType();
2680 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002681 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002682 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2683 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2684 Tmp1 = DAG.getConstantFP(apf, VT);
2685 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2686 Node->getOperand(0),
2687 Tmp1, ISD::SETLT);
2688 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002689 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2690 DAG.getNode(ISD::FSUB, dl, VT,
2691 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002692 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2693 DAG.getConstant(x, NVT));
2694 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2695 Results.push_back(Tmp1);
2696 break;
2697 }
Eli Friedman509150f2009-05-27 07:58:35 +00002698 case ISD::VAARG: {
2699 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002700 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002701 Tmp1 = Node->getOperand(0);
2702 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002703 unsigned Align = Node->getConstantOperandVal(3);
2704
Chris Lattnerecf42c42010-09-21 16:36:31 +00002705 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2706 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002707 SDValue VAList = VAListLoad;
2708
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002709 if (Align > TLI.getMinStackArgumentAlignment()) {
2710 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2711
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002712 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2713 DAG.getConstant(Align - 1,
2714 TLI.getPointerTy()));
2715
2716 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002717 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002718 TLI.getPointerTy()));
2719 }
2720
Eli Friedman509150f2009-05-27 07:58:35 +00002721 // Increment the pointer, VAList, to the next vaarg
2722 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2723 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002724 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002725 TLI.getPointerTy()));
2726 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002727 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2728 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002729 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002730 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002731 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002732 Results.push_back(Results[0].getValue(1));
2733 break;
2734 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002735 case ISD::VACOPY: {
2736 // This defaults to loading a pointer from the input and storing it to the
2737 // output, returning the chain.
2738 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2739 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2740 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002741 Node->getOperand(2), MachinePointerInfo(VS),
2742 false, false, 0);
2743 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2744 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002745 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002746 break;
2747 }
2748 case ISD::EXTRACT_VECTOR_ELT:
2749 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2750 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002751 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002752 Node->getOperand(0));
2753 else
2754 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2755 Results.push_back(Tmp1);
2756 break;
2757 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002758 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002759 break;
David Greenecfe33c42011-01-26 19:13:22 +00002760 case ISD::INSERT_SUBVECTOR:
2761 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2762 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002763 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002764 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002765 break;
2766 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002767 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002768 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002769 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002770 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002771 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2772 Node->getOperand(1),
2773 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002774 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002775 case ISD::VECTOR_SHUFFLE: {
2776 SmallVector<int, 8> Mask;
2777 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2778
Owen Andersone50ed302009-08-10 22:56:29 +00002779 EVT VT = Node->getValueType(0);
2780 EVT EltVT = VT.getVectorElementType();
Dan Gohman75b10042011-07-15 22:39:09 +00002781 if (!TLI.isTypeLegal(EltVT))
Bob Wilson14b21412010-05-19 18:48:32 +00002782 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002783 unsigned NumElems = VT.getVectorNumElements();
2784 SmallVector<SDValue, 8> Ops;
2785 for (unsigned i = 0; i != NumElems; ++i) {
2786 if (Mask[i] < 0) {
2787 Ops.push_back(DAG.getUNDEF(EltVT));
2788 continue;
2789 }
2790 unsigned Idx = Mask[i];
2791 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002792 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2793 Node->getOperand(0),
2794 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002795 else
Bill Wendling775db972009-12-23 00:28:23 +00002796 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2797 Node->getOperand(1),
2798 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002799 }
2800 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2801 Results.push_back(Tmp1);
2802 break;
2803 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002804 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002805 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002806 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2807 // 1 -> Hi
2808 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2809 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00002810 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00002811 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2812 } else {
2813 // 0 -> Lo
2814 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2815 Node->getOperand(0));
2816 }
2817 Results.push_back(Tmp1);
2818 break;
2819 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002820 case ISD::STACKSAVE:
2821 // Expand to CopyFromReg if the target set
2822 // StackPointerRegisterToSaveRestore.
2823 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002824 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2825 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002826 Results.push_back(Results[0].getValue(1));
2827 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002828 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002829 Results.push_back(Node->getOperand(0));
2830 }
2831 break;
2832 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002833 // Expand to CopyToReg if the target set
2834 // StackPointerRegisterToSaveRestore.
2835 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2836 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2837 Node->getOperand(1)));
2838 } else {
2839 Results.push_back(Node->getOperand(0));
2840 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002841 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002842 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002843 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002844 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002845 case ISD::FNEG:
2846 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2847 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2848 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2849 Node->getOperand(0));
2850 Results.push_back(Tmp1);
2851 break;
2852 case ISD::FABS: {
2853 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002854 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002855 Tmp1 = Node->getOperand(0);
2856 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002857 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002858 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002859 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2860 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002861 Results.push_back(Tmp1);
2862 break;
2863 }
2864 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002865 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2866 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002867 break;
2868 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002869 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2870 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002871 break;
2872 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002873 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2874 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002875 break;
2876 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002877 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2878 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002879 break;
2880 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002881 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2882 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002883 break;
2884 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002885 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2886 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002887 break;
2888 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002889 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2890 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002891 break;
2892 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002893 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2894 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002895 break;
2896 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00002897 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2898 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002899 break;
2900 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00002901 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2902 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002903 break;
2904 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00002905 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2906 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002907 break;
2908 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00002909 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
2910 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002911 break;
2912 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00002913 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
2914 RTLIB::NEARBYINT_F64,
2915 RTLIB::NEARBYINT_F80,
2916 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002917 break;
2918 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00002919 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
2920 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002921 break;
2922 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00002923 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
2924 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002925 break;
2926 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00002927 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
2928 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002929 break;
2930 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00002931 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
2932 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002933 break;
Cameron Zwarich33390842011-07-08 21:39:21 +00002934 case ISD::FMA:
2935 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
2936 RTLIB::FMA_F80, RTLIB::FMA_PPCF128));
2937 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00002938 case ISD::FP16_TO_FP32:
2939 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
2940 break;
2941 case ISD::FP32_TO_FP16:
2942 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
2943 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002944 case ISD::ConstantFP: {
2945 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00002946 // Check to see if this FP immediate is already legal.
2947 // If this is a legal constant, turn it into a TargetConstantFP node.
Dan Gohman65fd6562011-11-03 21:49:52 +00002948 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
2949 Results.push_back(ExpandConstantFP(CFP, true));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002950 break;
2951 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00002952 case ISD::EHSELECTION: {
2953 unsigned Reg = TLI.getExceptionSelectorRegister();
2954 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002955 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
2956 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002957 Results.push_back(Results[0].getValue(1));
2958 break;
2959 }
2960 case ISD::EXCEPTIONADDR: {
2961 unsigned Reg = TLI.getExceptionAddressRegister();
2962 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002963 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
2964 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002965 Results.push_back(Results[0].getValue(1));
2966 break;
2967 }
2968 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00002969 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002970 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2971 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
2972 "Don't know how to expand this subtraction!");
2973 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
2974 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00002975 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2976 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00002977 break;
2978 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002979 case ISD::UREM:
2980 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00002981 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00002982 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002983 bool isSigned = Node->getOpcode() == ISD::SREM;
2984 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
2985 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
2986 Tmp2 = Node->getOperand(0);
2987 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00002988 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
2989 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
2990 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00002991 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
2992 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002993 // X % Y -> X-X/Y*Y
2994 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
2995 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
2996 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00002997 } else if (isSigned)
2998 Tmp1 = ExpandIntLibCall(Node, true,
2999 RTLIB::SREM_I8,
3000 RTLIB::SREM_I16, RTLIB::SREM_I32,
3001 RTLIB::SREM_I64, RTLIB::SREM_I128);
3002 else
3003 Tmp1 = ExpandIntLibCall(Node, false,
3004 RTLIB::UREM_I8,
3005 RTLIB::UREM_I16, RTLIB::UREM_I32,
3006 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003007 Results.push_back(Tmp1);
3008 break;
3009 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003010 case ISD::UDIV:
3011 case ISD::SDIV: {
3012 bool isSigned = Node->getOpcode() == ISD::SDIV;
3013 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003014 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003015 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003016 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3017 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3018 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003019 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3020 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003021 else if (isSigned)
3022 Tmp1 = ExpandIntLibCall(Node, true,
3023 RTLIB::SDIV_I8,
3024 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3025 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3026 else
3027 Tmp1 = ExpandIntLibCall(Node, false,
3028 RTLIB::UDIV_I8,
3029 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3030 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003031 Results.push_back(Tmp1);
3032 break;
3033 }
3034 case ISD::MULHU:
3035 case ISD::MULHS: {
3036 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3037 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003038 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003039 SDVTList VTs = DAG.getVTList(VT, VT);
3040 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3041 "If this wasn't legal, it shouldn't have been created!");
3042 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3043 Node->getOperand(1));
3044 Results.push_back(Tmp1.getValue(1));
3045 break;
3046 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003047 case ISD::SDIVREM:
3048 case ISD::UDIVREM:
3049 // Expand into divrem libcall
3050 ExpandDivRemLibCall(Node, Results);
3051 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003052 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003053 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003054 SDVTList VTs = DAG.getVTList(VT, VT);
3055 // See if multiply or divide can be lowered using two-result operations.
3056 // We just need the low half of the multiply; try both the signed
3057 // and unsigned forms. If the target supports both SMUL_LOHI and
3058 // UMUL_LOHI, form a preference by checking which forms of plain
3059 // MULH it supports.
3060 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3061 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3062 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3063 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3064 unsigned OpToUse = 0;
3065 if (HasSMUL_LOHI && !HasMULHS) {
3066 OpToUse = ISD::SMUL_LOHI;
3067 } else if (HasUMUL_LOHI && !HasMULHU) {
3068 OpToUse = ISD::UMUL_LOHI;
3069 } else if (HasSMUL_LOHI) {
3070 OpToUse = ISD::SMUL_LOHI;
3071 } else if (HasUMUL_LOHI) {
3072 OpToUse = ISD::UMUL_LOHI;
3073 }
3074 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003075 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3076 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003077 break;
3078 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003079 Tmp1 = ExpandIntLibCall(Node, false,
3080 RTLIB::MUL_I8,
3081 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003082 RTLIB::MUL_I64, RTLIB::MUL_I128);
3083 Results.push_back(Tmp1);
3084 break;
3085 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003086 case ISD::SADDO:
3087 case ISD::SSUBO: {
3088 SDValue LHS = Node->getOperand(0);
3089 SDValue RHS = Node->getOperand(1);
3090 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3091 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3092 LHS, RHS);
3093 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003094 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003095
Eli Friedman4bc8c712009-05-27 12:20:41 +00003096 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3097
3098 // LHSSign -> LHS >= 0
3099 // RHSSign -> RHS >= 0
3100 // SumSign -> Sum >= 0
3101 //
3102 // Add:
3103 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3104 // Sub:
3105 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3106 //
3107 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3108 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3109 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3110 Node->getOpcode() == ISD::SADDO ?
3111 ISD::SETEQ : ISD::SETNE);
3112
3113 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3114 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3115
3116 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3117 Results.push_back(Cmp);
3118 break;
3119 }
3120 case ISD::UADDO:
3121 case ISD::USUBO: {
3122 SDValue LHS = Node->getOperand(0);
3123 SDValue RHS = Node->getOperand(1);
3124 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3125 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3126 LHS, RHS);
3127 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003128 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3129 Node->getOpcode () == ISD::UADDO ?
3130 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003131 break;
3132 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003133 case ISD::UMULO:
3134 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003135 EVT VT = Node->getValueType(0);
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003136 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003137 SDValue LHS = Node->getOperand(0);
3138 SDValue RHS = Node->getOperand(1);
3139 SDValue BottomHalf;
3140 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003141 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003142 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3143 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3144 bool isSigned = Node->getOpcode() == ISD::SMULO;
3145 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3146 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3147 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3148 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3149 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3150 RHS);
3151 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003152 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3153 VT.getSizeInBits() * 2))) {
Eli Friedmandb3c1692009-06-16 06:58:29 +00003154 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3155 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3156 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3157 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3158 DAG.getIntPtrConstant(0));
3159 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3160 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003161 } else {
3162 // We can fall back to a libcall with an illegal type for the MUL if we
3163 // have a libcall big enough.
3164 // Also, we can fall back to a division in some cases, but that's a big
3165 // performance hit in the general case.
Eric Christopher38a18262011-01-20 00:29:24 +00003166 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3167 if (WideVT == MVT::i16)
3168 LC = RTLIB::MUL_I16;
3169 else if (WideVT == MVT::i32)
3170 LC = RTLIB::MUL_I32;
3171 else if (WideVT == MVT::i64)
3172 LC = RTLIB::MUL_I64;
3173 else if (WideVT == MVT::i128)
3174 LC = RTLIB::MUL_I128;
3175 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanf316eb72011-05-16 22:09:53 +00003176
3177 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003178 // part.
3179 unsigned LoSize = VT.getSizeInBits();
3180 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
3181 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
3182 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
3183 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
Owen Anderson95771af2011-02-25 21:41:48 +00003184
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003185 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3186 // pre-lowered to the correct types. This all depends upon WideVT not
3187 // being a legal type for the architecture and thus has to be split to
3188 // two arguments.
3189 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3190 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3191 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3192 DAG.getIntPtrConstant(0));
3193 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3194 DAG.getIntPtrConstant(1));
Dan Gohman65fd6562011-11-03 21:49:52 +00003195 // Ret is a node with an illegal type. Because such things are not
3196 // generally permitted during this phase of legalization, delete the
3197 // node. The above EXTRACT_ELEMENT nodes should have been folded.
3198 DAG.DeleteNode(Ret.getNode());
Eli Friedmandb3c1692009-06-16 06:58:29 +00003199 }
Dan Gohmanf316eb72011-05-16 22:09:53 +00003200
Eli Friedmandb3c1692009-06-16 06:58:29 +00003201 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003202 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3203 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003204 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3205 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3206 ISD::SETNE);
3207 } else {
3208 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3209 DAG.getConstant(0, VT), ISD::SETNE);
3210 }
3211 Results.push_back(BottomHalf);
3212 Results.push_back(TopHalf);
3213 break;
3214 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003215 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003216 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003217 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3218 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003219 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003220 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003221 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003222 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003223 break;
3224 }
Eli Friedman509150f2009-05-27 07:58:35 +00003225 case ISD::SELECT:
3226 Tmp1 = Node->getOperand(0);
3227 Tmp2 = Node->getOperand(1);
3228 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003229 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003230 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3231 Tmp2, Tmp3,
3232 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003233 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003234 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3235 DAG.getConstant(0, Tmp1.getValueType()),
3236 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003237 }
Eli Friedman509150f2009-05-27 07:58:35 +00003238 Results.push_back(Tmp1);
3239 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003240 case ISD::BR_JT: {
3241 SDValue Chain = Node->getOperand(0);
3242 SDValue Table = Node->getOperand(1);
3243 SDValue Index = Node->getOperand(2);
3244
Owen Andersone50ed302009-08-10 22:56:29 +00003245 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003246
3247 const TargetData &TD = *TLI.getTargetData();
3248 unsigned EntrySize =
3249 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003250
Chris Lattner071c62f2010-01-25 23:26:13 +00003251 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003252 Index, DAG.getConstant(EntrySize, PTy));
3253 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3254
Owen Anderson23b9b192009-08-12 00:36:31 +00003255 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003256 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003257 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003258 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003259 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003260 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003261 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003262 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003263 // RelocBase can be JumpTable, GOT or some sort of global base.
3264 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3265 TLI.getPICJumpTableRelocBase(Table, DAG));
3266 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003267 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003268 Results.push_back(Tmp1);
3269 break;
3270 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003271 case ISD::BRCOND:
3272 // Expand brcond's setcc into its constituent parts and create a BR_CC
3273 // Node.
3274 Tmp1 = Node->getOperand(0);
3275 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003276 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003277 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003278 Tmp1, Tmp2.getOperand(2),
3279 Tmp2.getOperand(0), Tmp2.getOperand(1),
3280 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003281 } else {
Stuart Hastings88882242011-05-13 00:51:54 +00003282 // We test only the i1 bit. Skip the AND if UNDEF.
3283 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3284 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3285 DAG.getConstant(1, Tmp2.getValueType()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003286 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastings88882242011-05-13 00:51:54 +00003287 DAG.getCondCode(ISD::SETNE), Tmp3,
3288 DAG.getConstant(0, Tmp3.getValueType()),
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003289 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003290 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003291 Results.push_back(Tmp1);
3292 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003293 case ISD::SETCC: {
3294 Tmp1 = Node->getOperand(0);
3295 Tmp2 = Node->getOperand(1);
3296 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003297 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003298
3299 // If we expanded the SETCC into an AND/OR, return the new node
3300 if (Tmp2.getNode() == 0) {
3301 Results.push_back(Tmp1);
3302 break;
3303 }
3304
3305 // Otherwise, SETCC for the given comparison type must be completely
3306 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003307 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003308 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3309 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3310 Results.push_back(Tmp1);
3311 break;
3312 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003313 case ISD::SELECT_CC: {
3314 Tmp1 = Node->getOperand(0); // LHS
3315 Tmp2 = Node->getOperand(1); // RHS
3316 Tmp3 = Node->getOperand(2); // True
3317 Tmp4 = Node->getOperand(3); // False
3318 SDValue CC = Node->getOperand(4);
3319
3320 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003321 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003322
3323 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3324 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3325 CC = DAG.getCondCode(ISD::SETNE);
3326 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3327 Tmp3, Tmp4, CC);
3328 Results.push_back(Tmp1);
3329 break;
3330 }
3331 case ISD::BR_CC: {
3332 Tmp1 = Node->getOperand(0); // Chain
3333 Tmp2 = Node->getOperand(2); // LHS
3334 Tmp3 = Node->getOperand(3); // RHS
3335 Tmp4 = Node->getOperand(1); // CC
3336
3337 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003338 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003339
3340 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3341 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3342 Tmp4 = DAG.getCondCode(ISD::SETNE);
3343 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3344 Tmp3, Node->getOperand(4));
3345 Results.push_back(Tmp1);
3346 break;
3347 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003348 case ISD::BUILD_VECTOR:
3349 Results.push_back(ExpandBUILD_VECTOR(Node));
3350 break;
3351 case ISD::SRA:
3352 case ISD::SRL:
3353 case ISD::SHL: {
3354 // Scalarize vector SRA/SRL/SHL.
3355 EVT VT = Node->getValueType(0);
3356 assert(VT.isVector() && "Unable to legalize non-vector shift");
3357 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3358 unsigned NumElem = VT.getVectorNumElements();
3359
3360 SmallVector<SDValue, 8> Scalars;
3361 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
3362 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3363 VT.getScalarType(),
3364 Node->getOperand(0), DAG.getIntPtrConstant(Idx));
3365 SDValue Sh = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3366 VT.getScalarType(),
3367 Node->getOperand(1), DAG.getIntPtrConstant(Idx));
3368 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3369 VT.getScalarType(), Ex, Sh));
3370 }
3371 SDValue Result =
3372 DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
3373 &Scalars[0], Scalars.size());
3374 DAG.ReplaceAllUsesWith(SDValue(Node, 0), Result, this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00003375 DAG.RemoveDeadNode(Node, this);
Dan Gohman65fd6562011-11-03 21:49:52 +00003376 break;
3377 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003378 case ISD::GLOBAL_OFFSET_TABLE:
3379 case ISD::GlobalAddress:
3380 case ISD::GlobalTLSAddress:
3381 case ISD::ExternalSymbol:
3382 case ISD::ConstantPool:
3383 case ISD::JumpTable:
3384 case ISD::INTRINSIC_W_CHAIN:
3385 case ISD::INTRINSIC_WO_CHAIN:
3386 case ISD::INTRINSIC_VOID:
3387 // FIXME: Custom lowering for these operations shouldn't return null!
Eli Friedman3f727d62009-05-27 02:16:40 +00003388 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003389 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003390
3391 // Replace the original node with the legalized result.
Eli Friedman2efa35f2011-11-08 01:25:24 +00003392 if (!Results.empty()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00003393 DAG.ReplaceAllUsesWith(Node, Results.data(), this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00003394 DAG.RemoveDeadNode(Node, this);
3395 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003396}
Dan Gohman65fd6562011-11-03 21:49:52 +00003397
3398void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
3399 SmallVector<SDValue, 8> Results;
Owen Andersone50ed302009-08-10 22:56:29 +00003400 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003401 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003402 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003403 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003404 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003405 }
Owen Andersone50ed302009-08-10 22:56:29 +00003406 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003407 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003408 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003409 switch (Node->getOpcode()) {
3410 case ISD::CTTZ:
3411 case ISD::CTLZ:
3412 case ISD::CTPOP:
3413 // Zero extend the argument.
3414 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3415 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003416 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003417 if (Node->getOpcode() == ISD::CTTZ) {
3418 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003419 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003420 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3421 ISD::SETEQ);
3422 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3423 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3424 } else if (Node->getOpcode() == ISD::CTLZ) {
3425 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3426 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3427 DAG.getConstant(NVT.getSizeInBits() -
3428 OVT.getSizeInBits(), NVT));
3429 }
Bill Wendling775db972009-12-23 00:28:23 +00003430 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003431 break;
3432 case ISD::BSWAP: {
3433 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003434 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003435 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3436 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003437 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003438 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003439 break;
3440 }
3441 case ISD::FP_TO_UINT:
3442 case ISD::FP_TO_SINT:
3443 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3444 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3445 Results.push_back(Tmp1);
3446 break;
3447 case ISD::UINT_TO_FP:
3448 case ISD::SINT_TO_FP:
3449 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3450 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3451 Results.push_back(Tmp1);
3452 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003453 case ISD::AND:
3454 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003455 case ISD::XOR: {
3456 unsigned ExtOp, TruncOp;
3457 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003458 ExtOp = ISD::BITCAST;
3459 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003460 } else {
3461 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003462 ExtOp = ISD::ANY_EXTEND;
3463 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003464 }
3465 // Promote each of the values to the new type.
3466 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3467 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3468 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003469 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3470 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003471 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003472 }
3473 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003474 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003475 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003476 ExtOp = ISD::BITCAST;
3477 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003478 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003479 ExtOp = ISD::ANY_EXTEND;
3480 TruncOp = ISD::TRUNCATE;
3481 } else {
3482 ExtOp = ISD::FP_EXTEND;
3483 TruncOp = ISD::FP_ROUND;
3484 }
3485 Tmp1 = Node->getOperand(0);
3486 // Promote each of the values to the new type.
3487 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3488 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3489 // Perform the larger operation, then round down.
3490 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3491 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003492 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003493 else
Bill Wendling775db972009-12-23 00:28:23 +00003494 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003495 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003496 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003497 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003498 }
Eli Friedman509150f2009-05-27 07:58:35 +00003499 case ISD::VECTOR_SHUFFLE: {
3500 SmallVector<int, 8> Mask;
3501 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3502
3503 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003504 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3505 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003506
3507 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003508 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003509 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003510 Results.push_back(Tmp1);
3511 break;
3512 }
Eli Friedmanad754602009-05-28 03:56:57 +00003513 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003514 unsigned ExtOp = ISD::FP_EXTEND;
3515 if (NVT.isInteger()) {
3516 ISD::CondCode CCCode =
3517 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3518 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003519 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003520 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3521 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003522 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3523 Tmp1, Tmp2, Node->getOperand(2)));
3524 break;
3525 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003526 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003527
3528 // Replace the original node with the legalized result.
Eli Friedman2efa35f2011-11-08 01:25:24 +00003529 if (!Results.empty()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00003530 DAG.ReplaceAllUsesWith(Node, Results.data(), this);
Eli Friedman2efa35f2011-11-08 01:25:24 +00003531 DAG.RemoveDeadNode(Node, this);
3532 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003533}
3534
Chris Lattner3e928bb2005-01-07 07:47:09 +00003535// SelectionDAG::Legalize - This is the entry point for the file.
3536//
Dan Gohman975716a2011-05-16 22:19:54 +00003537void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003538 /// run - This is the main entry point to this class.
3539 ///
Dan Gohman975716a2011-05-16 22:19:54 +00003540 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003541}