blob: 8a5d2947d0b924d40ab59742c165410fb83dee39 [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
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000014#include "llvm/CallingConv.h"
15#include "llvm/Constants.h"
16#include "llvm/DebugInfo.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/LLVMContext.h"
Evan Cheng3d2125c2010-11-30 23:55:39 +000019#include "llvm/CodeGen/Analysis.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000020#include "llvm/CodeGen/MachineFunction.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng3d2125c2010-11-30 23:55:39 +000022#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000023#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000025#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000026#include "llvm/Target/TargetMachine.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
Nadav Rotemb6e89f02012-07-11 08:52:09 +000073 void LegalizeLoadOps(SDNode *Node);
74 void LegalizeStoreOps(SDNode *Node);
75
Nate Begeman68679912008-04-25 18:07:40 +000076 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
77 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
78 /// is necessary to spill the vector being inserted into to memory, perform
79 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +000080 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +000081 SDValue Idx, DebugLoc dl);
82 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
83 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +000084
Nate Begeman5a5ca152009-04-29 05:20:52 +000085 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
86 /// performs the same shuffe in terms of order or result bytes, but on a type
87 /// whose vector element type is narrower than the original shuffle type.
88 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +000089 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +000090 SDValue N1, SDValue N2,
Benjamin Kramered4c8c62012-01-15 13:16:05 +000091 ArrayRef<int> Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +000092
Owen Andersone50ed302009-08-10 22:56:29 +000093 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +000094 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +000095
Eli Friedman47b41f72009-05-27 02:21:29 +000096 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Eric Christopherabbbfbd2011-04-20 01:19:45 +000097 SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
98 unsigned NumOps, bool isSigned, DebugLoc dl);
99
Jim Grosbache03262f2010-06-18 21:43:38 +0000100 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
101 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000102 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
103 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
104 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000105 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
106 RTLIB::Libcall Call_I8,
107 RTLIB::Libcall Call_I16,
108 RTLIB::Libcall Call_I32,
109 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000110 RTLIB::Libcall Call_I128);
Evan Cheng65279cb2011-04-16 03:08:26 +0000111 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattnercad063f2005-07-16 00:19:57 +0000112
Owen Andersone50ed302009-08-10 22:56:29 +0000113 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000114 SDValue ExpandBUILD_VECTOR(SDNode *Node);
115 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000116 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
117 SmallVectorImpl<SDValue> &Results);
118 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000119 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000120 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000121 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000122 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000123 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000124 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000125
Dale Johannesen8a782a22009-02-02 22:12:50 +0000126 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
127 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000128
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000129 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenecfe33c42011-01-26 19:13:22 +0000130 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000131 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000132
Dan Gohman65fd6562011-11-03 21:49:52 +0000133 SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
134
Jim Grosbache03262f2010-06-18 21:43:38 +0000135 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
136
Dan Gohman65fd6562011-11-03 21:49:52 +0000137 void ExpandNode(SDNode *Node);
138 void PromoteNode(SDNode *Node);
139
Eli Friedman0e3642a2011-11-11 23:58:27 +0000140 void ForgetNode(SDNode *N) {
Dan Gohman65fd6562011-11-03 21:49:52 +0000141 LegalizedNodes.erase(N);
142 if (LegalizePosition == SelectionDAG::allnodes_iterator(N))
143 ++LegalizePosition;
144 }
145
Eli Friedman0e3642a2011-11-11 23:58:27 +0000146public:
147 // DAGUpdateListener implementation.
148 virtual void NodeDeleted(SDNode *N, SDNode *E) {
149 ForgetNode(N);
150 }
Dan Gohman65fd6562011-11-03 21:49:52 +0000151 virtual void NodeUpdated(SDNode *N) {}
Eli Friedman0e3642a2011-11-11 23:58:27 +0000152
153 // Node replacement helpers
154 void ReplacedNode(SDNode *N) {
155 if (N->use_empty()) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000156 DAG.RemoveDeadNode(N);
Eli Friedman0e3642a2011-11-11 23:58:27 +0000157 } else {
158 ForgetNode(N);
159 }
160 }
161 void ReplaceNode(SDNode *Old, SDNode *New) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000162 DAG.ReplaceAllUsesWith(Old, New);
Eli Friedman0e3642a2011-11-11 23:58:27 +0000163 ReplacedNode(Old);
164 }
165 void ReplaceNode(SDValue Old, SDValue New) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000166 DAG.ReplaceAllUsesWith(Old, New);
Eli Friedman0e3642a2011-11-11 23:58:27 +0000167 ReplacedNode(Old.getNode());
168 }
169 void ReplaceNode(SDNode *Old, const SDValue *New) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000170 DAG.ReplaceAllUsesWith(Old, New);
Eli Friedman0e3642a2011-11-11 23:58:27 +0000171 ReplacedNode(Old);
172 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000173};
174}
175
Nate Begeman5a5ca152009-04-29 05:20:52 +0000176/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
177/// performs the same shuffe in terms of order or result bytes, but on a type
178/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000179/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000180SDValue
181SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000182 SDValue N1, SDValue N2,
Benjamin Kramered4c8c62012-01-15 13:16:05 +0000183 ArrayRef<int> Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000184 unsigned NumMaskElts = VT.getVectorNumElements();
185 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000186 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000187
Nate Begeman9008ca62009-04-27 18:41:29 +0000188 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
189
190 if (NumEltsGrowth == 1)
191 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000192
Nate Begeman9008ca62009-04-27 18:41:29 +0000193 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000194 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000195 int Idx = Mask[i];
196 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000197 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000198 NewMask.push_back(-1);
199 else
200 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000201 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000202 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000203 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000204 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
205 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000206}
207
Dan Gohman975716a2011-05-16 22:19:54 +0000208SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000209 : SelectionDAG::DAGUpdateListener(dag),
210 TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
Dan Gohmanea027022011-07-15 22:19:02 +0000211 DAG(dag) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000212}
213
Chris Lattner3e928bb2005-01-07 07:47:09 +0000214void SelectionDAGLegalize::LegalizeDAG() {
Dan Gohmanf06c8352008-09-30 18:30:35 +0000215 DAG.AssignTopologicalOrder();
Dan Gohman2ba60e52011-10-28 01:29:32 +0000216
Dan Gohman65fd6562011-11-03 21:49:52 +0000217 // Visit all the nodes. We start in topological order, so that we see
218 // nodes with their original operands intact. Legalization can produce
219 // new nodes which may themselves need to be legalized. Iterate until all
220 // nodes have been legalized.
221 for (;;) {
222 bool AnyLegalized = false;
223 for (LegalizePosition = DAG.allnodes_end();
224 LegalizePosition != DAG.allnodes_begin(); ) {
225 --LegalizePosition;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000226
Dan Gohman65fd6562011-11-03 21:49:52 +0000227 SDNode *N = LegalizePosition;
228 if (LegalizedNodes.insert(N)) {
229 AnyLegalized = true;
230 LegalizeOp(N);
231 }
232 }
233 if (!AnyLegalized)
234 break;
235
236 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000237
238 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000239 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000240}
241
Evan Cheng9f877882006-12-13 20:57:08 +0000242/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
243/// a load from the constant pool.
Dan Gohman65fd6562011-11-03 21:49:52 +0000244SDValue
245SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
Evan Cheng00495212006-12-12 21:32:44 +0000246 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000247 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000248
249 // If a FP immediate is precise when represented as a float and if the
250 // target can do an extending load from float to double, we put it into
251 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000252 // double. This shrinks FP constants and canonicalizes them for targets where
253 // an FP extending load is the same cost as a normal load (such as on the x87
254 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000255 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000256 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000257 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000259 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000261 }
262
Owen Andersone50ed302009-08-10 22:56:29 +0000263 EVT OrigVT = VT;
264 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 while (SVT != MVT::f32) {
266 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000267 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000268 // Only do this if the target has a native EXTLOAD instruction from
269 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000270 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000271 TLI.ShouldShrinkFPConstant(OrigVT)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000272 Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000273 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000274 VT = SVT;
275 Extend = true;
276 }
Evan Cheng00495212006-12-12 21:32:44 +0000277 }
278
Dan Gohman475871a2008-07-27 21:46:04 +0000279 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000280 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman65fd6562011-11-03 21:49:52 +0000281 if (Extend) {
282 SDValue Result =
283 DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
284 DAG.getEntryNode(),
285 CPIdx, MachinePointerInfo::getConstantPool(),
286 VT, false, false, Alignment);
287 return Result;
288 }
289 SDValue Result =
290 DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000291 MachinePointerInfo::getConstantPool(), false, false, false,
Dan Gohman65fd6562011-11-03 21:49:52 +0000292 Alignment);
293 return Result;
Evan Cheng00495212006-12-12 21:32:44 +0000294}
295
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000296/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
Dan Gohman65fd6562011-11-03 21:49:52 +0000297static void ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
298 const TargetLowering &TLI,
Eli Friedman0e3642a2011-11-11 23:58:27 +0000299 SelectionDAGLegalize *DAGLegalize) {
Eli Friedmanb91b6002011-11-16 02:43:15 +0000300 assert(ST->getAddressingMode() == ISD::UNINDEXED &&
301 "unaligned indexed stores not implemented!");
Dan Gohman475871a2008-07-27 21:46:04 +0000302 SDValue Chain = ST->getChain();
303 SDValue Ptr = ST->getBasePtr();
304 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000305 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000306 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000307 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000308 if (ST->getMemoryVT().isFloatingPoint() ||
309 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000310 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000311 if (TLI.isTypeLegal(intVT)) {
312 // Expand to a bitconvert of the value to the integer type of the
313 // same size, then a (misaligned) int store.
314 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000315 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Dan Gohman65fd6562011-11-03 21:49:52 +0000316 Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
317 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Eli Friedman0e3642a2011-11-11 23:58:27 +0000318 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
Dan Gohman65fd6562011-11-03 21:49:52 +0000319 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000320 }
Dan Gohman1b328962011-05-17 22:22:52 +0000321 // Do a (aligned) store to a stack slot, then copy from the stack slot
322 // to the final destination using (unaligned) integer loads and stores.
323 EVT StoredVT = ST->getMemoryVT();
324 EVT RegVT =
325 TLI.getRegisterType(*DAG.getContext(),
326 EVT::getIntegerVT(*DAG.getContext(),
327 StoredVT.getSizeInBits()));
328 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
329 unsigned RegBytes = RegVT.getSizeInBits() / 8;
330 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
331
332 // Make sure the stack slot is also aligned for the register type.
333 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
334
335 // Perform the original store, only redirected to the stack slot.
336 SDValue Store = DAG.getTruncStore(Chain, dl,
337 Val, StackPtr, MachinePointerInfo(),
338 StoredVT, false, false, 0);
339 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
340 SmallVector<SDValue, 8> Stores;
341 unsigned Offset = 0;
342
343 // Do all but one copies using the full register width.
344 for (unsigned i = 1; i < NumRegs; i++) {
345 // Load one integer register's worth from the stack slot.
346 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
347 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000348 false, false, false, 0);
Dan Gohman1b328962011-05-17 22:22:52 +0000349 // Store it to the final location. Remember the store.
350 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
351 ST->getPointerInfo().getWithOffset(Offset),
352 ST->isVolatile(), ST->isNonTemporal(),
353 MinAlign(ST->getAlignment(), Offset)));
354 // Increment the pointers.
355 Offset += RegBytes;
356 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
357 Increment);
358 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
359 }
360
361 // The last store may be partial. Do a truncating store. On big-endian
362 // machines this requires an extending load from the stack slot to ensure
363 // that the bits are in the right place.
364 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
365 8 * (StoredBytes - Offset));
366
367 // Load from the stack slot.
368 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
369 MachinePointerInfo(),
370 MemVT, false, false, 0);
371
372 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
373 ST->getPointerInfo()
374 .getWithOffset(Offset),
375 MemVT, ST->isVolatile(),
376 ST->isNonTemporal(),
377 MinAlign(ST->getAlignment(), Offset)));
378 // The order of the stores doesn't matter - say it with a TokenFactor.
Dan Gohman65fd6562011-11-03 21:49:52 +0000379 SDValue Result =
380 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
381 Stores.size());
Eli Friedman0e3642a2011-11-11 23:58:27 +0000382 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
Dan Gohman65fd6562011-11-03 21:49:52 +0000383 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000384 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000385 assert(ST->getMemoryVT().isInteger() &&
386 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000387 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000388 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000389 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000390 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000391 int IncrementSize = NumBits / 8;
392
393 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000394 SDValue ShiftAmount = DAG.getConstant(NumBits,
395 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000396 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000397 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000398
399 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000400 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000401 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000402 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000403 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000404 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000405 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000406 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000407 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000408 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000409 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
410 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000411
Dan Gohman65fd6562011-11-03 21:49:52 +0000412 SDValue Result =
413 DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Eli Friedman0e3642a2011-11-11 23:58:27 +0000414 DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000415}
416
417/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
Dan Gohman65fd6562011-11-03 21:49:52 +0000418static void
419ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
420 const TargetLowering &TLI,
421 SDValue &ValResult, SDValue &ChainResult) {
Eli Friedmanb91b6002011-11-16 02:43:15 +0000422 assert(LD->getAddressingMode() == ISD::UNINDEXED &&
423 "unaligned indexed loads not implemented!");
Dan Gohman475871a2008-07-27 21:46:04 +0000424 SDValue Chain = LD->getChain();
425 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000426 EVT VT = LD->getValueType(0);
427 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000428 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000429 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000430 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000431 if (TLI.isTypeLegal(intVT)) {
432 // Expand to a (misaligned) integer load of the same size,
433 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000434 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
435 LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000436 LD->isNonTemporal(),
437 LD->isInvariant(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000438 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000439 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000440 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000441
Dan Gohman65fd6562011-11-03 21:49:52 +0000442 ValResult = Result;
443 ChainResult = Chain;
444 return;
Duncan Sands05e11fa2008-12-12 21:47:02 +0000445 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000446
Chris Lattnerecf42c42010-09-21 16:36:31 +0000447 // Copy the value to a (aligned) stack slot using (unaligned) integer
448 // loads and stores, then do a (aligned) load from the stack slot.
449 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
450 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
451 unsigned RegBytes = RegVT.getSizeInBits() / 8;
452 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
453
454 // Make sure the stack slot is also aligned for the register type.
455 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
456
457 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
458 SmallVector<SDValue, 8> Stores;
459 SDValue StackPtr = StackBase;
460 unsigned Offset = 0;
461
462 // Do all but one copies using the full register width.
463 for (unsigned i = 1; i < NumRegs; i++) {
464 // Load one integer register's worth from the original location.
465 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
466 LD->getPointerInfo().getWithOffset(Offset),
467 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000468 LD->isInvariant(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000469 MinAlign(LD->getAlignment(), Offset));
470 // Follow the load with a store to the stack slot. Remember the store.
471 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000472 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000473 // Increment the pointers.
474 Offset += RegBytes;
475 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
476 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
477 Increment);
478 }
479
480 // The last copy may be partial. Do an extending load.
481 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
482 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000483 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000484 LD->getPointerInfo().getWithOffset(Offset),
485 MemVT, LD->isVolatile(),
486 LD->isNonTemporal(),
487 MinAlign(LD->getAlignment(), Offset));
488 // Follow the load with a store to the stack slot. Remember the store.
489 // On big-endian machines this requires a truncating store to ensure
490 // that the bits end up in the right place.
491 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
492 MachinePointerInfo(), MemVT,
493 false, false, 0));
494
495 // The order of the stores doesn't matter - say it with a TokenFactor.
496 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
497 Stores.size());
498
499 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000500 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000501 MachinePointerInfo(), LoadedVT, false, false, 0);
502
503 // Callers expect a MERGE_VALUES node.
Dan Gohman65fd6562011-11-03 21:49:52 +0000504 ValResult = Load;
505 ChainResult = TF;
506 return;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000507 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000508 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000509 "Unaligned load of unsupported type.");
510
Dale Johannesen8155d642008-02-27 22:36:00 +0000511 // Compute the new VT that is half the size of the old one. This is an
512 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000513 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000514 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000515 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000516 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000517
Chris Lattnere400af82007-11-19 21:38:03 +0000518 unsigned Alignment = LD->getAlignment();
519 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000520 ISD::LoadExtType HiExtType = LD->getExtensionType();
521
522 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
523 if (HiExtType == ISD::NON_EXTLOAD)
524 HiExtType = ISD::ZEXTLOAD;
525
526 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000527 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000528 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000529 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000530 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000531 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000532 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000533 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000534 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000535 LD->getPointerInfo().getWithOffset(IncrementSize),
536 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000537 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000538 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000539 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000540 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000541 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000542 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000543 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000544 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000545 LD->getPointerInfo().getWithOffset(IncrementSize),
546 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000547 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000548 }
549
550 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000551 SDValue ShiftAmount = DAG.getConstant(NumBits,
552 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000553 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
554 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000555
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000557 Hi.getValue(1));
558
Dan Gohman65fd6562011-11-03 21:49:52 +0000559 ValResult = Result;
560 ChainResult = TF;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000561}
Evan Cheng912095b2007-01-04 21:56:39 +0000562
Nate Begeman68679912008-04-25 18:07:40 +0000563/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
564/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
565/// is necessary to spill the vector being inserted into to memory, perform
566/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000567SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000568PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
569 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000570 SDValue Tmp1 = Vec;
571 SDValue Tmp2 = Val;
572 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000573
Nate Begeman68679912008-04-25 18:07:40 +0000574 // If the target doesn't support this, we have to spill the input vector
575 // to a temporary stack slot, update the element, then reload it. This is
576 // badness. We could also load the value into a vector register (either
577 // with a "move to register" or "extload into register" instruction, then
578 // permute it into place, if the idx is a constant and if the idx is
579 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000580 EVT VT = Tmp1.getValueType();
581 EVT EltVT = VT.getVectorElementType();
582 EVT IdxVT = Tmp3.getValueType();
583 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000584 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000585
Evan Chengff89dcb2009-10-18 18:16:27 +0000586 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
587
Nate Begeman68679912008-04-25 18:07:40 +0000588 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000589 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000590 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000591 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000592
593 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000594 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000595 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000596 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000597 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000598 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
599 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000600 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000601 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000602 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000603 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000604 return DAG.getLoad(VT, dl, Ch, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000605 MachinePointerInfo::getFixedStack(SPFI), false, false,
606 false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000607}
608
Mon P Wange9f10152008-12-09 05:46:39 +0000609
Eli Friedman3f727d62009-05-27 02:16:40 +0000610SDValue SelectionDAGLegalize::
611ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
612 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
613 // SCALAR_TO_VECTOR requires that the type of the value being inserted
614 // match the element type of the vector being created, except for
615 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000616 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000617 if (Val.getValueType() == EltVT ||
618 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
619 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
620 Vec.getValueType(), Val);
621
622 unsigned NumElts = Vec.getValueType().getVectorNumElements();
623 // We generate a shuffle of InVec and ScVec, so the shuffle mask
624 // should be 0,1,2,3,4,5... with the appropriate element replaced with
625 // elt 0 of the RHS.
626 SmallVector<int, 8> ShufOps;
627 for (unsigned i = 0; i != NumElts; ++i)
628 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
629
630 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
631 &ShufOps[0]);
632 }
633 }
634 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
635}
636
Eli Friedman7ef3d172009-06-06 07:04:42 +0000637SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
638 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
639 // FIXME: We shouldn't do this for TargetConstantFP's.
640 // FIXME: move this to the DAG Combiner! Note that we can't regress due
641 // to phase ordering between legalized code and the dag combiner. This
642 // probably means that we need to integrate dag combiner and legalizer
643 // together.
644 // We generally can't do this one for long doubles.
645 SDValue Tmp1 = ST->getChain();
646 SDValue Tmp2 = ST->getBasePtr();
647 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000648 unsigned Alignment = ST->getAlignment();
649 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000650 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000651 DebugLoc dl = ST->getDebugLoc();
652 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 if (CFP->getValueType(0) == MVT::f32 &&
Dan Gohman75b10042011-07-15 22:39:09 +0000654 TLI.isTypeLegal(MVT::i32)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000655 Tmp3 = DAG.getConstant(CFP->getValueAPF().
656 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000658 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
659 isVolatile, isNonTemporal, Alignment);
660 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000661
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000662 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000663 // If this target supports 64-bit registers, do a single 64-bit store.
Dan Gohman75b10042011-07-15 22:39:09 +0000664 if (TLI.isTypeLegal(MVT::i64)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000665 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000667 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
668 isVolatile, isNonTemporal, Alignment);
669 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000670
Dan Gohman75b10042011-07-15 22:39:09 +0000671 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000672 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
673 // stores. If the target supports neither 32- nor 64-bits, this
674 // xform is certainly not worth it.
675 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000676 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000678 if (TLI.isBigEndian()) std::swap(Lo, Hi);
679
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000680 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
681 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000682 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
683 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000684 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
685 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000686 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000687
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000689 }
690 }
691 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000692 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000693}
694
Nadav Rotemb6e89f02012-07-11 08:52:09 +0000695void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
696 StoreSDNode *ST = cast<StoreSDNode>(Node);
697 SDValue Tmp1 = ST->getChain();
698 SDValue Tmp2 = ST->getBasePtr();
699 DebugLoc dl = Node->getDebugLoc();
700
701 unsigned Alignment = ST->getAlignment();
702 bool isVolatile = ST->isVolatile();
703 bool isNonTemporal = ST->isNonTemporal();
704
705 if (!ST->isTruncatingStore()) {
706 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
707 ReplaceNode(ST, OptStore);
708 return;
709 }
710
711 {
712 SDValue Tmp3 = ST->getValue();
713 EVT VT = Tmp3.getValueType();
714 switch (TLI.getOperationAction(ISD::STORE, VT)) {
715 default: llvm_unreachable("This action is not supported yet!");
716 case TargetLowering::Legal:
717 // If this is an unaligned store and the target doesn't support it,
718 // expand it.
719 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
720 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
721 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
722 if (ST->getAlignment() < ABIAlignment)
723 ExpandUnalignedStore(cast<StoreSDNode>(Node),
724 DAG, TLI, this);
725 }
726 break;
727 case TargetLowering::Custom:
728 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
729 if (Tmp1.getNode())
730 ReplaceNode(SDValue(Node, 0), Tmp1);
731 break;
732 case TargetLowering::Promote: {
733 assert(VT.isVector() && "Unknown legal promote case!");
734 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
735 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
736 SDValue Result =
737 DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
738 ST->getPointerInfo(), isVolatile,
739 isNonTemporal, Alignment);
740 ReplaceNode(SDValue(Node, 0), Result);
741 break;
742 }
743 }
744 return;
745 }
746 } else {
747 SDValue Tmp3 = ST->getValue();
748
749 EVT StVT = ST->getMemoryVT();
750 unsigned StWidth = StVT.getSizeInBits();
751
752 if (StWidth != StVT.getStoreSizeInBits()) {
753 // Promote to a byte-sized store with upper bits zero if not
754 // storing an integral number of bytes. For example, promote
755 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
756 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
757 StVT.getStoreSizeInBits());
758 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
759 SDValue Result =
760 DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
761 NVT, isVolatile, isNonTemporal, Alignment);
762 ReplaceNode(SDValue(Node, 0), Result);
763 } else if (StWidth & (StWidth - 1)) {
764 // If not storing a power-of-2 number of bits, expand as two stores.
765 assert(!StVT.isVector() && "Unsupported truncstore!");
766 unsigned RoundWidth = 1 << Log2_32(StWidth);
767 assert(RoundWidth < StWidth);
768 unsigned ExtraWidth = StWidth - RoundWidth;
769 assert(ExtraWidth < RoundWidth);
770 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
771 "Store size not an integral number of bytes!");
772 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
773 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
774 SDValue Lo, Hi;
775 unsigned IncrementSize;
776
777 if (TLI.isLittleEndian()) {
778 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
779 // Store the bottom RoundWidth bits.
780 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
781 RoundVT,
782 isVolatile, isNonTemporal, Alignment);
783
784 // Store the remaining ExtraWidth bits.
785 IncrementSize = RoundWidth / 8;
786 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
787 DAG.getIntPtrConstant(IncrementSize));
788 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
789 DAG.getConstant(RoundWidth,
790 TLI.getShiftAmountTy(Tmp3.getValueType())));
791 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
792 ST->getPointerInfo().getWithOffset(IncrementSize),
793 ExtraVT, isVolatile, isNonTemporal,
794 MinAlign(Alignment, IncrementSize));
795 } else {
796 // Big endian - avoid unaligned stores.
797 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
798 // Store the top RoundWidth bits.
799 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
800 DAG.getConstant(ExtraWidth,
801 TLI.getShiftAmountTy(Tmp3.getValueType())));
802 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
803 RoundVT, isVolatile, isNonTemporal, Alignment);
804
805 // Store the remaining ExtraWidth bits.
806 IncrementSize = RoundWidth / 8;
807 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
808 DAG.getIntPtrConstant(IncrementSize));
809 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
810 ST->getPointerInfo().getWithOffset(IncrementSize),
811 ExtraVT, isVolatile, isNonTemporal,
812 MinAlign(Alignment, IncrementSize));
813 }
814
815 // The order of the stores doesn't matter.
816 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
817 ReplaceNode(SDValue(Node, 0), Result);
818 } else {
819 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
820 default: llvm_unreachable("This action is not supported yet!");
821 case TargetLowering::Legal:
822 // If this is an unaligned store and the target doesn't support it,
823 // expand it.
824 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
825 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
826 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
827 if (ST->getAlignment() < ABIAlignment)
828 ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
829 }
830 break;
831 case TargetLowering::Custom:
832 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
833 if (Tmp1.getNode())
834 ReplaceNode(SDValue(Node, 0), Tmp1);
835 break;
836 case TargetLowering::Expand:
837 assert(!StVT.isVector() &&
838 "Vector Stores are handled in LegalizeVectorOps");
839
840 // TRUNCSTORE:i16 i32 -> STORE i16
841 assert(TLI.isTypeLegal(StVT) && "Do not know how to expand this store!");
842 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
843 SDValue Result =
844 DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
845 isVolatile, isNonTemporal, Alignment);
846 ReplaceNode(SDValue(Node, 0), Result);
847 break;
848 }
849 }
850 }
851}
852
853void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
854 LoadSDNode *LD = cast<LoadSDNode>(Node);
855 SDValue Tmp1 = LD->getChain(); // Legalize the chain.
856 SDValue Tmp2 = LD->getBasePtr(); // Legalize the base pointer.
857 DebugLoc dl = Node->getDebugLoc();
858
859 ISD::LoadExtType ExtType = LD->getExtensionType();
860 if (ExtType == ISD::NON_EXTLOAD) {
861 EVT VT = Node->getValueType(0);
862 SDValue Tmp3 = SDValue(Node, 0);
863 SDValue Tmp4 = SDValue(Node, 1);
864
865 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
866 default: llvm_unreachable("This action is not supported yet!");
867 case TargetLowering::Legal:
868 // If this is an unaligned load and the target doesn't support it,
869 // expand it.
870 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
871 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
872 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
873 if (LD->getAlignment() < ABIAlignment){
874 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
875 DAG, TLI, Tmp3, Tmp4);
876 }
877 }
878 break;
879 case TargetLowering::Custom:
880 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
881 if (Tmp1.getNode()) {
882 Tmp3 = Tmp1;
883 Tmp4 = Tmp1.getValue(1);
884 }
885 break;
886 case TargetLowering::Promote: {
887 // Only promote a load of vector type to another.
888 assert(VT.isVector() && "Cannot promote this load!");
889 // Change base type to a different vector type.
890 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
891
892 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
893 LD->isVolatile(), LD->isNonTemporal(),
894 LD->isInvariant(), LD->getAlignment());
895 Tmp3 = DAG.getNode(ISD::BITCAST, dl, VT, Tmp1);
896 Tmp4 = Tmp1.getValue(1);
897 break;
898 }
899 }
900 if (Tmp4.getNode() != Node) {
901 assert(Tmp3.getNode() != Node && "Load must be completely replaced");
902 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp3);
903 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp4);
904 ReplacedNode(Node);
905 }
906 return;
907 }
908
909 EVT SrcVT = LD->getMemoryVT();
910 unsigned SrcWidth = SrcVT.getSizeInBits();
911 unsigned Alignment = LD->getAlignment();
912 bool isVolatile = LD->isVolatile();
913 bool isNonTemporal = LD->isNonTemporal();
914
915 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
916 // Some targets pretend to have an i1 loading operation, and actually
917 // load an i8. This trick is correct for ZEXTLOAD because the top 7
918 // bits are guaranteed to be zero; it helps the optimizers understand
919 // that these bits are zero. It is also useful for EXTLOAD, since it
920 // tells the optimizers that those bits are undefined. It would be
921 // nice to have an effective generic way of getting these benefits...
922 // Until such a way is found, don't insist on promoting i1 here.
923 (SrcVT != MVT::i1 ||
924 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
925 // Promote to a byte-sized load if not loading an integral number of
926 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
927 unsigned NewWidth = SrcVT.getStoreSizeInBits();
928 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
929 SDValue Ch;
930
931 // The extra bits are guaranteed to be zero, since we stored them that
932 // way. A zext load from NVT thus automatically gives zext from SrcVT.
933
934 ISD::LoadExtType NewExtType =
935 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
936
937 SDValue Result =
938 DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
939 Tmp1, Tmp2, LD->getPointerInfo(),
940 NVT, isVolatile, isNonTemporal, Alignment);
941
942 Ch = Result.getValue(1); // The chain.
943
944 if (ExtType == ISD::SEXTLOAD)
945 // Having the top bits zero doesn't help when sign extending.
946 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
947 Result.getValueType(),
948 Result, DAG.getValueType(SrcVT));
949 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
950 // All the top bits are guaranteed to be zero - inform the optimizers.
951 Result = DAG.getNode(ISD::AssertZext, dl,
952 Result.getValueType(), Result,
953 DAG.getValueType(SrcVT));
954
955 Tmp1 = Result;
956 Tmp2 = Ch;
957 } else if (SrcWidth & (SrcWidth - 1)) {
958 // If not loading a power-of-2 number of bits, expand as two loads.
959 assert(!SrcVT.isVector() && "Unsupported extload!");
960 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
961 assert(RoundWidth < SrcWidth);
962 unsigned ExtraWidth = SrcWidth - RoundWidth;
963 assert(ExtraWidth < RoundWidth);
964 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
965 "Load size not an integral number of bytes!");
966 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
967 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
968 SDValue Lo, Hi, Ch;
969 unsigned IncrementSize;
970
971 if (TLI.isLittleEndian()) {
972 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
973 // Load the bottom RoundWidth bits.
974 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
975 Tmp1, Tmp2,
976 LD->getPointerInfo(), RoundVT, isVolatile,
977 isNonTemporal, Alignment);
978
979 // Load the remaining ExtraWidth bits.
980 IncrementSize = RoundWidth / 8;
981 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
982 DAG.getIntPtrConstant(IncrementSize));
983 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
984 LD->getPointerInfo().getWithOffset(IncrementSize),
985 ExtraVT, isVolatile, isNonTemporal,
986 MinAlign(Alignment, IncrementSize));
987
988 // Build a factor node to remember that this load is independent of
989 // the other one.
990 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
991 Hi.getValue(1));
992
993 // Move the top bits to the right place.
994 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
995 DAG.getConstant(RoundWidth,
996 TLI.getShiftAmountTy(Hi.getValueType())));
997
998 // Join the hi and lo parts.
999 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
1000 } else {
1001 // Big endian - avoid unaligned loads.
1002 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1003 // Load the top RoundWidth bits.
1004 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
1005 LD->getPointerInfo(), RoundVT, isVolatile,
1006 isNonTemporal, Alignment);
1007
1008 // Load the remaining ExtraWidth bits.
1009 IncrementSize = RoundWidth / 8;
1010 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1011 DAG.getIntPtrConstant(IncrementSize));
1012 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
1013 dl, Node->getValueType(0), Tmp1, Tmp2,
1014 LD->getPointerInfo().getWithOffset(IncrementSize),
1015 ExtraVT, isVolatile, isNonTemporal,
1016 MinAlign(Alignment, IncrementSize));
1017
1018 // Build a factor node to remember that this load is independent of
1019 // the other one.
1020 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1021 Hi.getValue(1));
1022
1023 // Move the top bits to the right place.
1024 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1025 DAG.getConstant(ExtraWidth,
1026 TLI.getShiftAmountTy(Hi.getValueType())));
1027
1028 // Join the hi and lo parts.
1029 Tmp1 = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
1030 }
1031
1032 Tmp2 = Ch;
1033 } else {
1034 bool isCustom = false;
1035 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1036 default: llvm_unreachable("This action is not supported yet!");
1037 case TargetLowering::Custom:
1038 isCustom = true;
1039 // FALLTHROUGH
1040 case TargetLowering::Legal:
1041 Tmp1 = SDValue(Node, 0);
1042 Tmp2 = SDValue(Node, 1);
1043
1044 if (isCustom) {
1045 SDValue Tmp3 = TLI.LowerOperation(SDValue(Node, 0), DAG);
1046 if (Tmp3.getNode()) {
1047 Tmp1 = Tmp3;
1048 Tmp2 = Tmp3.getValue(1);
1049 }
1050 } else {
1051 // If this is an unaligned load and the target doesn't support it,
1052 // expand it.
1053 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1054 Type *Ty =
1055 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1056 unsigned ABIAlignment =
1057 TLI.getTargetData()->getABITypeAlignment(Ty);
1058 if (LD->getAlignment() < ABIAlignment){
1059 ExpandUnalignedLoad(cast<LoadSDNode>(Node),
1060 DAG, TLI, Tmp1, Tmp2);
1061 }
1062 }
1063 }
1064 break;
1065 case TargetLowering::Expand:
1066 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) {
1067 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1068 LD->getPointerInfo(),
1069 LD->isVolatile(), LD->isNonTemporal(),
1070 LD->isInvariant(), LD->getAlignment());
1071 unsigned ExtendOp;
1072 switch (ExtType) {
1073 case ISD::EXTLOAD:
1074 ExtendOp = (SrcVT.isFloatingPoint() ?
1075 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1076 break;
1077 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1078 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1079 default: llvm_unreachable("Unexpected extend load type!");
1080 }
1081 Tmp1 = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1082 Tmp2 = Load.getValue(1);
1083 break;
1084 }
1085
1086 assert(!SrcVT.isVector() &&
1087 "Vector Loads are handled in LegalizeVectorOps");
1088
1089 // FIXME: This does not work for vectors on most targets. Sign- and
1090 // zero-extend operations are currently folded into extending loads,
1091 // whether they are legal or not, and then we end up here without any
1092 // support for legalizing them.
1093 assert(ExtType != ISD::EXTLOAD &&
1094 "EXTLOAD should always be supported!");
1095 // Turn the unsupported load into an EXTLOAD followed by an explicit
1096 // zero/sign extend inreg.
1097 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
1098 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1099 LD->isVolatile(), LD->isNonTemporal(),
1100 LD->getAlignment());
1101 SDValue ValRes;
1102 if (ExtType == ISD::SEXTLOAD)
1103 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1104 Result.getValueType(),
1105 Result, DAG.getValueType(SrcVT));
1106 else
1107 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
1108 Tmp1 = ValRes;
1109 Tmp2 = Result.getValue(1);
1110 break;
1111 }
1112 }
1113
1114 // Since loads produce two values, make sure to remember that we legalized
1115 // both of them.
1116 if (Tmp2.getNode() != Node) {
1117 assert(Tmp1.getNode() != Node && "Load must be completely replaced");
1118 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp1);
1119 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Tmp2);
1120 ReplacedNode(Node);
1121 }
1122}
1123
Dan Gohman6a109f92011-07-15 21:42:20 +00001124/// LegalizeOp - Return a legal replacement for the given operation, with
1125/// all legal operands.
Dan Gohman65fd6562011-11-03 21:49:52 +00001126void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
1127 if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
1128 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00001129
Eli Friedman1fde9c52009-05-24 02:46:31 +00001130 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +00001131 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
1132 TargetLowering::TypeLegal &&
Eli Friedman1fde9c52009-05-24 02:46:31 +00001133 "Unexpected illegal type!");
1134
1135 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +00001136 assert((TLI.getTypeAction(*DAG.getContext(),
1137 Node->getOperand(i).getValueType()) ==
1138 TargetLowering::TypeLegal ||
Eli Friedman1fde9c52009-05-24 02:46:31 +00001139 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
1140 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001141
Dan Gohman475871a2008-07-27 21:46:04 +00001142 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001143
Eli Friedman8c377c72009-05-27 01:25:56 +00001144 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +00001145 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +00001146 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001147 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +00001148 case ISD::INTRINSIC_W_CHAIN:
1149 case ISD::INTRINSIC_WO_CHAIN:
1150 case ISD::INTRINSIC_VOID:
Eli Friedman8c377c72009-05-27 01:25:56 +00001151 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +00001152 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +00001153 break;
Hal Finkel5194d6d2012-03-24 03:53:52 +00001154 case ISD::VAARG:
1155 Action = TLI.getOperationAction(Node->getOpcode(),
1156 Node->getValueType(0));
1157 if (Action != TargetLowering::Promote)
1158 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
1159 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00001160 case ISD::SINT_TO_FP:
1161 case ISD::UINT_TO_FP:
1162 case ISD::EXTRACT_VECTOR_ELT:
1163 Action = TLI.getOperationAction(Node->getOpcode(),
1164 Node->getOperand(0).getValueType());
1165 break;
1166 case ISD::FP_ROUND_INREG:
1167 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001168 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00001169 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
1170 break;
1171 }
Eli Friedman327236c2011-08-24 20:50:09 +00001172 case ISD::ATOMIC_STORE: {
1173 Action = TLI.getOperationAction(Node->getOpcode(),
1174 Node->getOperand(2).getValueType());
1175 break;
1176 }
Eli Friedman3be2e512009-05-28 03:06:16 +00001177 case ISD::SELECT_CC:
1178 case ISD::SETCC:
1179 case ISD::BR_CC: {
1180 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
1181 Node->getOpcode() == ISD::SETCC ? 2 : 1;
1182 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001183 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +00001184 ISD::CondCode CCCode =
1185 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
1186 Action = TLI.getCondCodeAction(CCCode, OpVT);
1187 if (Action == TargetLowering::Legal) {
1188 if (Node->getOpcode() == ISD::SELECT_CC)
1189 Action = TLI.getOperationAction(Node->getOpcode(),
1190 Node->getValueType(0));
1191 else
1192 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
1193 }
1194 break;
1195 }
Eli Friedman8c377c72009-05-27 01:25:56 +00001196 case ISD::LOAD:
1197 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +00001198 // FIXME: Model these properly. LOAD and STORE are complicated, and
1199 // STORE expects the unlegalized operand in some cases.
1200 SimpleFinishLegalizing = false;
1201 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00001202 case ISD::CALLSEQ_START:
1203 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +00001204 // FIXME: This shouldn't be necessary. These nodes have special properties
1205 // dealing with the recursive nature of legalization. Removing this
1206 // special case should be done as part of making LegalizeDAG non-recursive.
1207 SimpleFinishLegalizing = false;
1208 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00001209 case ISD::EXTRACT_ELEMENT:
1210 case ISD::FLT_ROUNDS_:
1211 case ISD::SADDO:
1212 case ISD::SSUBO:
1213 case ISD::UADDO:
1214 case ISD::USUBO:
1215 case ISD::SMULO:
1216 case ISD::UMULO:
1217 case ISD::FPOWI:
1218 case ISD::MERGE_VALUES:
1219 case ISD::EH_RETURN:
1220 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00001221 case ISD::EH_SJLJ_SETJMP:
1222 case ISD::EH_SJLJ_LONGJMP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001223 // These operations lie about being legal: when they claim to be legal,
1224 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +00001225 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1226 if (Action == TargetLowering::Legal)
1227 Action = TargetLowering::Expand;
1228 break;
Duncan Sands4a544a72011-09-06 13:37:06 +00001229 case ISD::INIT_TRAMPOLINE:
1230 case ISD::ADJUST_TRAMPOLINE:
Eli Friedman8c377c72009-05-27 01:25:56 +00001231 case ISD::FRAMEADDR:
1232 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +00001233 // These operations lie about being legal: when they claim to be legal,
1234 // they should actually be custom-lowered.
1235 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1236 if (Action == TargetLowering::Legal)
1237 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +00001238 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001239 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001240 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +00001241 Action = TargetLowering::Legal;
1242 } else {
1243 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001244 }
Eli Friedman8c377c72009-05-27 01:25:56 +00001245 break;
1246 }
1247
1248 if (SimpleFinishLegalizing) {
Peter Collingbourne92d63cc2012-05-20 18:36:15 +00001249 SDNode *NewNode = Node;
Eli Friedman8c377c72009-05-27 01:25:56 +00001250 switch (Node->getOpcode()) {
1251 default: break;
Eli Friedman8c377c72009-05-27 01:25:56 +00001252 case ISD::SHL:
1253 case ISD::SRL:
1254 case ISD::SRA:
1255 case ISD::ROTL:
1256 case ISD::ROTR:
1257 // Legalizing shifts/rotates requires adjusting the shift amount
1258 // to the appropriate width.
Peter Collingbourne92d63cc2012-05-20 18:36:15 +00001259 if (!Node->getOperand(1).getValueType().isVector()) {
1260 SDValue SAO =
1261 DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
1262 Node->getOperand(1));
Dan Gohman65fd6562011-11-03 21:49:52 +00001263 HandleSDNode Handle(SAO);
1264 LegalizeOp(SAO.getNode());
Peter Collingbourne92d63cc2012-05-20 18:36:15 +00001265 NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
1266 Handle.getValue());
Dan Gohman65fd6562011-11-03 21:49:52 +00001267 }
Eli Friedman8c377c72009-05-27 01:25:56 +00001268 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +00001269 case ISD::SRL_PARTS:
1270 case ISD::SRA_PARTS:
1271 case ISD::SHL_PARTS:
1272 // Legalizing shifts/rotates requires adjusting the shift amount
1273 // to the appropriate width.
Peter Collingbourne92d63cc2012-05-20 18:36:15 +00001274 if (!Node->getOperand(2).getValueType().isVector()) {
1275 SDValue SAO =
1276 DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
1277 Node->getOperand(2));
Dan Gohman65fd6562011-11-03 21:49:52 +00001278 HandleSDNode Handle(SAO);
1279 LegalizeOp(SAO.getNode());
Peter Collingbourne92d63cc2012-05-20 18:36:15 +00001280 NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
1281 Node->getOperand(1),
1282 Handle.getValue());
Dan Gohman65fd6562011-11-03 21:49:52 +00001283 }
Dan Gohman2c9489d2009-08-18 23:52:48 +00001284 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00001285 }
1286
Dan Gohman65fd6562011-11-03 21:49:52 +00001287 if (NewNode != Node) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001288 DAG.ReplaceAllUsesWith(Node, NewNode);
Dan Gohman65fd6562011-11-03 21:49:52 +00001289 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1290 DAG.TransferDbgValues(SDValue(Node, i), SDValue(NewNode, i));
Eli Friedman0e3642a2011-11-11 23:58:27 +00001291 ReplacedNode(Node);
Dan Gohman65fd6562011-11-03 21:49:52 +00001292 Node = NewNode;
1293 }
Eli Friedman8c377c72009-05-27 01:25:56 +00001294 switch (Action) {
1295 case TargetLowering::Legal:
Dan Gohman65fd6562011-11-03 21:49:52 +00001296 return;
Eli Friedman8c377c72009-05-27 01:25:56 +00001297 case TargetLowering::Custom:
1298 // FIXME: The handling for custom lowering with multiple results is
1299 // a complete mess.
Dan Gohman65fd6562011-11-03 21:49:52 +00001300 Tmp1 = TLI.LowerOperation(SDValue(Node, 0), DAG);
Eli Friedman8c377c72009-05-27 01:25:56 +00001301 if (Tmp1.getNode()) {
Dan Gohman65fd6562011-11-03 21:49:52 +00001302 SmallVector<SDValue, 8> ResultVals;
Eli Friedman8c377c72009-05-27 01:25:56 +00001303 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
1304 if (e == 1)
1305 ResultVals.push_back(Tmp1);
1306 else
1307 ResultVals.push_back(Tmp1.getValue(i));
1308 }
Dan Gohman65fd6562011-11-03 21:49:52 +00001309 if (Tmp1.getNode() != Node || Tmp1.getResNo() != 0) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001310 DAG.ReplaceAllUsesWith(Node, ResultVals.data());
Dan Gohman65fd6562011-11-03 21:49:52 +00001311 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1312 DAG.TransferDbgValues(SDValue(Node, i), ResultVals[i]);
Eli Friedman0e3642a2011-11-11 23:58:27 +00001313 ReplacedNode(Node);
Dan Gohman65fd6562011-11-03 21:49:52 +00001314 }
1315 return;
Eli Friedman8c377c72009-05-27 01:25:56 +00001316 }
1317
1318 // FALL THROUGH
1319 case TargetLowering::Expand:
Dan Gohman65fd6562011-11-03 21:49:52 +00001320 ExpandNode(Node);
1321 return;
Eli Friedman8c377c72009-05-27 01:25:56 +00001322 case TargetLowering::Promote:
Dan Gohman65fd6562011-11-03 21:49:52 +00001323 PromoteNode(Node);
1324 return;
Eli Friedman8c377c72009-05-27 01:25:56 +00001325 }
1326 }
1327
1328 switch (Node->getOpcode()) {
1329 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001330#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001331 dbgs() << "NODE: ";
1332 Node->dump( &DAG);
1333 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001334#endif
Craig Topper5e25ee82012-02-05 08:31:47 +00001335 llvm_unreachable("Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001336
Dan Gohman65fd6562011-11-03 21:49:52 +00001337 case ISD::CALLSEQ_START:
Dan Gohman6f3ddef2011-10-29 00:41:52 +00001338 case ISD::CALLSEQ_END:
Dan Gohman65fd6562011-11-03 21:49:52 +00001339 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001340 case ISD::LOAD: {
Nadav Rotemb6e89f02012-07-11 08:52:09 +00001341 return LegalizeLoadOps(Node);
Chris Lattner01ff7212005-04-10 22:54:25 +00001342 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001343 case ISD::STORE: {
Nadav Rotemb6e89f02012-07-11 08:52:09 +00001344 return LegalizeStoreOps(Node);
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001345 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001346 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001347}
1348
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001349SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1350 SDValue Vec = Op.getOperand(0);
1351 SDValue Idx = Op.getOperand(1);
1352 DebugLoc dl = Op.getDebugLoc();
1353 // Store the value to a temporary stack slot, then LOAD the returned part.
1354 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001355 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1356 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001357
1358 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001359 unsigned EltSize =
1360 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001361 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1362 DAG.getConstant(EltSize, Idx.getValueType()));
1363
1364 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1365 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1366 else
1367 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1368
1369 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1370
Eli Friedmanc680ac92009-07-09 22:01:03 +00001371 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001372 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001373 false, false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001374 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001375 MachinePointerInfo(),
1376 Vec.getValueType().getVectorElementType(),
1377 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001378}
1379
David Greenecfe33c42011-01-26 19:13:22 +00001380SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1381 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1382
1383 SDValue Vec = Op.getOperand(0);
1384 SDValue Part = Op.getOperand(1);
1385 SDValue Idx = Op.getOperand(2);
1386 DebugLoc dl = Op.getDebugLoc();
1387
1388 // Store the value to a temporary stack slot, then LOAD the returned part.
1389
1390 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1391 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1392 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1393
1394 // First store the whole vector.
1395 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1396 false, false, 0);
1397
1398 // Then store the inserted part.
1399
1400 // Add the offset to the index.
1401 unsigned EltSize =
1402 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1403
1404 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1405 DAG.getConstant(EltSize, Idx.getValueType()));
1406
1407 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1408 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1409 else
1410 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1411
1412 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1413 StackPtr);
1414
1415 // Store the subvector.
1416 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1417 MachinePointerInfo(), false, false, 0);
1418
1419 // Finally, load the updated vector.
1420 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001421 false, false, false, 0);
David Greenecfe33c42011-01-26 19:13:22 +00001422}
1423
Eli Friedman7ef3d172009-06-06 07:04:42 +00001424SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1425 // We can't handle this case efficiently. Allocate a sufficiently
1426 // aligned object on the stack, store each element into it, then load
1427 // the result as a vector.
1428 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001429 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001430 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001431 DebugLoc dl = Node->getDebugLoc();
1432 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001433 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001434 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001435
1436 // Emit a store of each element to the stack slot.
1437 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001438 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001439 // Store (in the right endianness) the elements to memory.
1440 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1441 // Ignore undef elements.
1442 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1443
1444 unsigned Offset = TypeByteSize*i;
1445
1446 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1447 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1448
Dan Gohman9949dd62010-02-25 20:30:49 +00001449 // If the destination vector element type is narrower than the source
1450 // element type, only store the bits necessary.
1451 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001452 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001453 Node->getOperand(i), Idx,
1454 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001455 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001456 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001457 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001458 Node->getOperand(i), Idx,
1459 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001460 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001461 }
1462
1463 SDValue StoreChain;
1464 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001465 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001466 &Stores[0], Stores.size());
1467 else
1468 StoreChain = DAG.getEntryNode();
1469
1470 // Result is a load from the stack slot.
Pete Cooperd752e0f2011-11-08 18:42:53 +00001471 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo,
1472 false, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001473}
1474
Eli Friedman4bc8c712009-05-27 12:20:41 +00001475SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1476 DebugLoc dl = Node->getDebugLoc();
1477 SDValue Tmp1 = Node->getOperand(0);
1478 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001479
1480 // Get the sign bit of the RHS. First obtain a value that has the same
1481 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001482 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001483 EVT FloatVT = Tmp2.getValueType();
1484 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Dan Gohman75b10042011-07-15 22:39:09 +00001485 if (TLI.isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001486 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001487 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001488 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001489 // Store the float to memory, then load the sign part out as an integer.
1490 MVT LoadTy = TLI.getPointerTy();
1491 // First create a temporary that is aligned for both the load and store.
1492 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1493 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001494 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001495 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001496 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001497 if (TLI.isBigEndian()) {
1498 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1499 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001500 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001501 false, false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001502 } else { // Little endian
1503 SDValue LoadPtr = StackPtr;
1504 // The float may be wider than the integer we are going to load. Advance
1505 // the pointer so that the loaded integer will contain the sign bit.
1506 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1507 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1508 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1509 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1510 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001511 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001512 false, false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001513 // Move the sign bit to the top bit of the loaded integer.
1514 unsigned BitShift = LoadTy.getSizeInBits() -
1515 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1516 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1517 if (BitShift)
1518 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001519 DAG.getConstant(BitShift,
1520 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001521 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001522 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001523 // Now get the sign bit proper, by seeing whether the value is negative.
1524 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1525 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1526 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001527 // Get the absolute value of the result.
1528 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1529 // Select between the nabs and abs value based on the sign bit of
1530 // the input.
1531 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1532 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1533 AbsVal);
1534}
1535
Eli Friedman4bc8c712009-05-27 12:20:41 +00001536void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1537 SmallVectorImpl<SDValue> &Results) {
1538 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1539 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1540 " not tell us which reg is the stack pointer!");
1541 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001542 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001543 SDValue Tmp1 = SDValue(Node, 0);
1544 SDValue Tmp2 = SDValue(Node, 1);
1545 SDValue Tmp3 = Node->getOperand(2);
1546 SDValue Chain = Tmp1.getOperand(0);
1547
1548 // Chain the dynamic stack allocation so that it doesn't modify the stack
1549 // pointer when other instructions are using the stack.
1550 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1551
1552 SDValue Size = Tmp2.getOperand(1);
1553 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1554 Chain = SP.getValue(1);
1555 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001556 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001557 if (Align > StackAlign)
1558 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1559 DAG.getConstant(-(uint64_t)Align, VT));
1560 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1561 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1562
1563 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1564 DAG.getIntPtrConstant(0, true), SDValue());
1565
1566 Results.push_back(Tmp1);
1567 Results.push_back(Tmp2);
1568}
1569
Evan Cheng7f042682008-10-15 02:05:31 +00001570/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001571/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001572/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001573void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001574 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001575 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001576 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001577 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001578 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1579 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Craig Topper5e25ee82012-02-05 08:31:47 +00001580 default: llvm_unreachable("Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001581 case TargetLowering::Legal:
1582 // Nothing to do.
1583 break;
1584 case TargetLowering::Expand: {
1585 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1586 unsigned Opc = 0;
1587 switch (CCCode) {
Craig Topper5e25ee82012-02-05 08:31:47 +00001588 default: llvm_unreachable("Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001589 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1590 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1591 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1592 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1593 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1594 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1595 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1596 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1597 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1598 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1599 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1600 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001601 // FIXME: Implement more expansions.
1602 }
1603
Dale Johannesenbb5da912009-02-02 20:41:04 +00001604 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1605 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1606 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001607 RHS = SDValue();
1608 CC = SDValue();
1609 break;
1610 }
1611 }
1612}
1613
Chris Lattner1401d152008-01-16 07:45:30 +00001614/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1615/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1616/// a load from the stack slot to DestVT, extending it if needed.
1617/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001618SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001619 EVT SlotVT,
1620 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001621 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001622 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001623 unsigned SrcAlign =
1624 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001625 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001626 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001627
Evan Chengff89dcb2009-10-18 18:16:27 +00001628 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1629 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001630 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001631
Duncan Sands83ec4b62008-06-06 12:08:01 +00001632 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1633 unsigned SlotSize = SlotVT.getSizeInBits();
1634 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001635 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Evan Chengadf97992010-04-15 01:25:27 +00001636 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001637
Chris Lattner1401d152008-01-16 07:45:30 +00001638 // Emit a store to the stack slot. Use a truncstore if the input value is
1639 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001640 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001641
Chris Lattner1401d152008-01-16 07:45:30 +00001642 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001643 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001644 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001645 else {
1646 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001647 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001648 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001649 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001650
Chris Lattner35481892005-12-23 00:16:34 +00001651 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001652 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001653 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001654 false, false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001655
Chris Lattner1401d152008-01-16 07:45:30 +00001656 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00001657 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001658 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001659}
1660
Dan Gohman475871a2008-07-27 21:46:04 +00001661SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001662 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001663 // Create a vector sized/aligned stack slot, store the value to element #0,
1664 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001665 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001666
Evan Chengff89dcb2009-10-18 18:16:27 +00001667 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1668 int SPFI = StackPtrFI->getIndex();
1669
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001670 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1671 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001672 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001673 Node->getValueType(0).getVectorElementType(),
1674 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001675 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001676 MachinePointerInfo::getFixedStack(SPFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001677 false, false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001678}
1679
1680
Chris Lattnerce872152006-03-19 06:31:19 +00001681/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001682/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001683SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001684 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001685 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001686 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001687 EVT VT = Node->getValueType(0);
1688 EVT OpVT = Node->getOperand(0).getValueType();
1689 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001690
1691 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001692 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001693 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001694 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001695 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001696 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001697 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001698 if (V.getOpcode() == ISD::UNDEF)
1699 continue;
1700 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001701 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001702 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001703 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001704
1705 if (!Value1.getNode()) {
1706 Value1 = V;
1707 } else if (!Value2.getNode()) {
1708 if (V != Value1)
1709 Value2 = V;
1710 } else if (V != Value1 && V != Value2) {
1711 MoreThanTwoValues = true;
1712 }
Chris Lattnerce872152006-03-19 06:31:19 +00001713 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001714
Eli Friedman7a5e5552009-06-07 06:52:44 +00001715 if (!Value1.getNode())
1716 return DAG.getUNDEF(VT);
1717
1718 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001719 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001720
Chris Lattner2eb86532006-03-24 07:29:17 +00001721 // If all elements are constants, create a load from the constant pool.
1722 if (isConstant) {
Chris Lattner4ca829e2012-01-25 06:02:56 +00001723 SmallVector<Constant*, 16> CV;
Chris Lattner2eb86532006-03-24 07:29:17 +00001724 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001725 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001726 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001727 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001728 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001729 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001730 if (OpVT==EltVT)
1731 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1732 else {
1733 // If OpVT and EltVT don't match, EltVT is not legal and the
1734 // element values have been promoted/truncated earlier. Undo this;
1735 // we don't want a v16i8 to become a v16i32 for example.
1736 const ConstantInt *CI = V->getConstantIntValue();
1737 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1738 CI->getZExtValue()));
1739 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001740 } else {
1741 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001742 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001743 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001744 }
1745 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001746 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001747 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001748 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001749 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001750 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001751 false, false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001752 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001753
Eli Friedman7a5e5552009-06-07 06:52:44 +00001754 if (!MoreThanTwoValues) {
1755 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1756 for (unsigned i = 0; i < NumElems; ++i) {
1757 SDValue V = Node->getOperand(i);
1758 if (V.getOpcode() == ISD::UNDEF)
1759 continue;
1760 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1761 }
1762 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001763 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001764 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1765 SDValue Vec2;
1766 if (Value2.getNode())
1767 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1768 else
1769 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001770
Chris Lattner87100e02006-03-20 01:52:29 +00001771 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001772 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001773 }
1774 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001775
Eli Friedman7ef3d172009-06-06 07:04:42 +00001776 // Otherwise, we can't handle this case efficiently.
1777 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001778}
1779
Chris Lattner77e77a62005-01-21 06:05:23 +00001780// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1781// does not fit into a register, return the lo part and set the hi part to the
1782// by-reg argument. If it does fit into a single register, return the result
1783// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001784SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001785 bool isSigned) {
Chris Lattner77e77a62005-01-21 06:05:23 +00001786 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00001787 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00001788 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001789 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001790 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00001791 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001792 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00001793 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00001794 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00001795 }
Bill Wendling056292f2008-09-16 21:48:12 +00001796 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00001797 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001798
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001799 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00001800
Evan Chengbf010eb2012-04-10 01:51:00 +00001801 // By default, the input chain to this libcall is the entry node of the
1802 // function. If the libcall is going to be emitted as a tail call then
1803 // TLI.isUsedByReturnOnly will change it to the right chain if the return
1804 // node which is being folded has a non-entry input chain.
1805 SDValue InChain = DAG.getEntryNode();
1806
Evan Cheng3d2125c2010-11-30 23:55:39 +00001807 // isTailCall may be true since the callee does not reference caller stack
1808 // frame. Check if it's in the right position.
Evan Chengb52ba492012-04-10 03:15:18 +00001809 SDValue TCChain = InChain;
1810 bool isTailCall = isInTailCallPosition(DAG, Node, TCChain, TLI);
1811 if (isTailCall)
1812 InChain = TCChain;
1813
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001814 TargetLowering::
1815 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001816 0, TLI.getLibcallCallingConv(LC), isTailCall,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001817 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001818 Callee, Args, DAG, Node->getDebugLoc());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001819 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
1820
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00001821
Evan Cheng3d2125c2010-11-30 23:55:39 +00001822 if (!CallInfo.second.getNode())
1823 // It's a tailcall, return the chain (which is the DAG root).
1824 return DAG.getRoot();
1825
Eli Friedman74807f22009-05-26 08:55:52 +00001826 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00001827}
1828
Dan Gohmanf316eb72011-05-16 22:09:53 +00001829/// ExpandLibCall - Generate a libcall taking the given operands as arguments
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001830/// and returning a result of type RetVT.
1831SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
1832 const SDValue *Ops, unsigned NumOps,
1833 bool isSigned, DebugLoc dl) {
1834 TargetLowering::ArgListTy Args;
1835 Args.reserve(NumOps);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001836
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001837 TargetLowering::ArgListEntry Entry;
1838 for (unsigned i = 0; i != NumOps; ++i) {
1839 Entry.Node = Ops[i];
1840 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
1841 Entry.isSExt = isSigned;
1842 Entry.isZExt = !isSigned;
1843 Args.push_back(Entry);
1844 }
1845 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1846 TLI.getPointerTy());
Dan Gohmanf316eb72011-05-16 22:09:53 +00001847
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001848 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001849 TargetLowering::
1850 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
1851 false, 0, TLI.getLibcallCallingConv(LC),
1852 /*isTailCall=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001853 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001854 Callee, Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001855 std::pair<SDValue,SDValue> CallInfo = TLI.LowerCallTo(CLI);
Dan Gohmanf316eb72011-05-16 22:09:53 +00001856
Eric Christopherabbbfbd2011-04-20 01:19:45 +00001857 return CallInfo.first;
1858}
1859
Jim Grosbache03262f2010-06-18 21:43:38 +00001860// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
1861// ExpandLibCall except that the first operand is the in-chain.
1862std::pair<SDValue, SDValue>
1863SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
1864 SDNode *Node,
1865 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00001866 SDValue InChain = Node->getOperand(0);
1867
1868 TargetLowering::ArgListTy Args;
1869 TargetLowering::ArgListEntry Entry;
1870 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
1871 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001872 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00001873 Entry.Node = Node->getOperand(i);
1874 Entry.Ty = ArgTy;
1875 Entry.isSExt = isSigned;
1876 Entry.isZExt = !isSigned;
1877 Args.push_back(Entry);
1878 }
1879 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
1880 TLI.getPointerTy());
1881
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001882 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001883 TargetLowering::
1884 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001885 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001886 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
Jim Grosbache03262f2010-06-18 21:43:38 +00001887 Callee, Args, DAG, Node->getDebugLoc());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001888 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Jim Grosbache03262f2010-06-18 21:43:38 +00001889
Jim Grosbache03262f2010-06-18 21:43:38 +00001890 return CallInfo;
1891}
1892
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001893SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
1894 RTLIB::Libcall Call_F32,
1895 RTLIB::Libcall Call_F64,
1896 RTLIB::Libcall Call_F80,
1897 RTLIB::Libcall Call_PPCF128) {
1898 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001899 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Craig Topper5e25ee82012-02-05 08:31:47 +00001900 default: llvm_unreachable("Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001901 case MVT::f32: LC = Call_F32; break;
1902 case MVT::f64: LC = Call_F64; break;
1903 case MVT::f80: LC = Call_F80; break;
1904 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001905 }
1906 return ExpandLibCall(LC, Node, false);
1907}
1908
1909SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001910 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001911 RTLIB::Libcall Call_I16,
1912 RTLIB::Libcall Call_I32,
1913 RTLIB::Libcall Call_I64,
1914 RTLIB::Libcall Call_I128) {
1915 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Craig Topper5e25ee82012-02-05 08:31:47 +00001917 default: llvm_unreachable("Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00001918 case MVT::i8: LC = Call_I8; break;
1919 case MVT::i16: LC = Call_I16; break;
1920 case MVT::i32: LC = Call_I32; break;
1921 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001922 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00001923 }
1924 return ExpandLibCall(LC, Node, isSigned);
1925}
1926
Evan Cheng65279cb2011-04-16 03:08:26 +00001927/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
1928static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
1929 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00001930 RTLIB::Libcall LC;
1931 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Craig Topper5e25ee82012-02-05 08:31:47 +00001932 default: llvm_unreachable("Unexpected request for libcall!");
Evan Cheng8e23e812011-04-01 00:42:02 +00001933 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;
1938 }
1939
Evan Cheng65279cb2011-04-16 03:08:26 +00001940 return TLI.getLibcallName(LC) != 0;
1941}
Evan Cheng8e23e812011-04-01 00:42:02 +00001942
Evan Cheng8ef09682012-06-21 05:56:05 +00001943/// useDivRem - Only issue divrem libcall if both quotient and remainder are
Evan Cheng65279cb2011-04-16 03:08:26 +00001944/// needed.
Evan Cheng8ef09682012-06-21 05:56:05 +00001945static bool useDivRem(SDNode *Node, bool isSigned, bool isDIV) {
1946 // The other use might have been replaced with a divrem already.
1947 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Evan Cheng8e23e812011-04-01 00:42:02 +00001948 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00001949 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00001950 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001951 else
Evan Cheng8e23e812011-04-01 00:42:02 +00001952 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00001953
Evan Cheng8e23e812011-04-01 00:42:02 +00001954 SDValue Op0 = Node->getOperand(0);
1955 SDValue Op1 = Node->getOperand(1);
1956 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
1957 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
1958 SDNode *User = *UI;
1959 if (User == Node)
1960 continue;
Evan Cheng8ef09682012-06-21 05:56:05 +00001961 if ((User->getOpcode() == OtherOpcode || User->getOpcode() == DivRemOpc) &&
Evan Cheng8e23e812011-04-01 00:42:02 +00001962 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00001963 User->getOperand(1) == Op1)
1964 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00001965 }
Evan Cheng65279cb2011-04-16 03:08:26 +00001966 return false;
1967}
Evan Cheng8e23e812011-04-01 00:42:02 +00001968
Evan Cheng65279cb2011-04-16 03:08:26 +00001969/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
1970/// pairs.
1971void
1972SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
1973 SmallVectorImpl<SDValue> &Results) {
1974 unsigned Opcode = Node->getOpcode();
1975 bool isSigned = Opcode == ISD::SDIVREM;
1976
1977 RTLIB::Libcall LC;
1978 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Craig Topper5e25ee82012-02-05 08:31:47 +00001979 default: llvm_unreachable("Unexpected request for libcall!");
Evan Cheng65279cb2011-04-16 03:08:26 +00001980 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
1981 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
1982 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
1983 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
1984 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00001985 }
1986
1987 // The input chain to this libcall is the entry node of the function.
1988 // Legalizing the call will automatically add the previous call to the
1989 // dependence.
1990 SDValue InChain = DAG.getEntryNode();
1991
1992 EVT RetVT = Node->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001993 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00001994
1995 TargetLowering::ArgListTy Args;
1996 TargetLowering::ArgListEntry Entry;
1997 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1998 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001999 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00002000 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
2001 Entry.isSExt = isSigned;
2002 Entry.isZExt = !isSigned;
2003 Args.push_back(Entry);
2004 }
2005
2006 // Also pass the return address of the remainder.
2007 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2008 Entry.Node = FIPtr;
2009 Entry.Ty = RetTy->getPointerTo();
2010 Entry.isSExt = isSigned;
2011 Entry.isZExt = !isSigned;
2012 Args.push_back(Entry);
2013
2014 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2015 TLI.getPointerTy());
2016
Evan Cheng8e23e812011-04-01 00:42:02 +00002017 DebugLoc dl = Node->getDebugLoc();
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002018 TargetLowering::
2019 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng8e23e812011-04-01 00:42:02 +00002020 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002021 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
2022 Callee, Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002023 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
Evan Cheng8e23e812011-04-01 00:42:02 +00002024
Evan Cheng8e23e812011-04-01 00:42:02 +00002025 // Remainder is loaded back from the stack frame.
Dan Gohman65fd6562011-11-03 21:49:52 +00002026 SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002027 MachinePointerInfo(), false, false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00002028 Results.push_back(CallInfo.first);
2029 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00002030}
2031
Chris Lattner22cde6a2006-01-28 08:25:58 +00002032/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2033/// INT_TO_FP operation of the specified operand when the target requests that
2034/// we expand it. At this point, we know that the result and operand types are
2035/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002036SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2037 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002038 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002039 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002041 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002042
Chris Lattner23594d42008-01-16 07:03:22 +00002043 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002045
Chris Lattner22cde6a2006-01-28 08:25:58 +00002046 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002047 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002048 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002050 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002051 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002052 if (TLI.isLittleEndian())
2053 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002054
Chris Lattner22cde6a2006-01-28 08:25:58 +00002055 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002056 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002057 if (isSigned) {
2058 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2060 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002061 } else {
2062 Op0Mapped = Op0;
2063 }
2064 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002065 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002066 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002067 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002068 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002069 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002070 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002071 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2072 MachinePointerInfo(),
2073 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002074 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002075 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002076 MachinePointerInfo(), false, false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002077 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002078 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002079 BitsToDouble(0x4330000080000000ULL) :
2080 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002081 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002082 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002083 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002084 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002085 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002086 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002087 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002088 // do nothing
2089 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002090 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002091 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002092 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002094 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002095 }
2096 return Result;
2097 }
2098 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002099 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002100
2101 // Implementation of unsigned i64 to f64 following the algorithm in
2102 // __floatundidf in compiler_rt. This implementation has the advantage
2103 // of performing rounding correctly, both in the default rounding mode
2104 // and in all alternate rounding modes.
2105 // TODO: Generalize this for use with other types.
2106 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2107 SDValue TwoP52 =
2108 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2109 SDValue TwoP84PlusTwoP52 =
2110 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2111 SDValue TwoP84 =
2112 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2113
2114 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2115 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2116 DAG.getConstant(32, MVT::i64));
2117 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2118 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002119 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2120 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002121 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2122 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002123 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2124 }
2125
Owen Anderson3a9e7692010-10-05 17:24:05 +00002126 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002127 // TODO: Generalize this for use with other types.
2128 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002129 // For unsigned conversions, convert them to signed conversions using the
2130 // algorithm from the x86_64 __floatundidf in compiler_rt.
2131 if (!isSigned) {
2132 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002133
Owen Anderson95771af2011-02-25 21:41:48 +00002134 SDValue ShiftConst =
2135 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002136 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2137 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2138 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2139 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002140
Owen Anderson3a9e7692010-10-05 17:24:05 +00002141 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2142 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002143
Owen Anderson3a9e7692010-10-05 17:24:05 +00002144 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002145 // select. We happen to get lucky and machinesink does the right
2146 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002147 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002148 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002149 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2150 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2151 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002152
Owen Anderson3a9e7692010-10-05 17:24:05 +00002153 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002154
Jim Grosbach6e992612010-07-02 17:41:59 +00002155 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002156 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2157 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2158 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002159 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002160 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2161 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2162 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2163 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2164 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2165 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002166 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002167 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002168 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002169
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002170 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2171 DAG.getConstant(32, SHVT));
2172 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2173 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2174 SDValue TwoP32 =
2175 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2176 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2177 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2178 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2179 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2180 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2181 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002182 }
2183
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002184 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002185
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002186 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2187 Op0, DAG.getConstant(0, Op0.getValueType()),
2188 ISD::SETLT);
2189 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2190 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2191 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002192
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002193 // If the sign bit of the integer is set, the large number will be treated
2194 // as a negative number. To counteract this, the dynamic code adds an
2195 // offset depending on the data type.
2196 uint64_t FF;
2197 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Craig Topper5e25ee82012-02-05 08:31:47 +00002198 default: llvm_unreachable("Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002199 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2200 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2201 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2202 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2203 }
2204 if (TLI.isLittleEndian()) FF <<= 32;
2205 Constant *FudgeFactor = ConstantInt::get(
2206 Type::getInt64Ty(*DAG.getContext()), FF);
2207
2208 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2209 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2210 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2211 Alignment = std::min(Alignment, 4u);
2212 SDValue FudgeInReg;
2213 if (DestVT == MVT::f32)
2214 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002215 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002216 false, false, false, Alignment);
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002217 else {
Dan Gohman65fd6562011-11-03 21:49:52 +00002218 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
2219 DAG.getEntryNode(), CPIdx,
2220 MachinePointerInfo::getConstantPool(),
2221 MVT::f32, false, false, Alignment);
2222 HandleSDNode Handle(Load);
2223 LegalizeOp(Load.getNode());
2224 FudgeInReg = Handle.getValue();
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002225 }
2226
2227 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002228}
2229
2230/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2231/// *INT_TO_FP operation of the specified operand when the target requests that
2232/// we promote it. At this point, we know that the result and operand types are
2233/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2234/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002235SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002236 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002237 bool isSigned,
2238 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002239 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002240 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002241
2242 unsigned OpToUse = 0;
2243
2244 // Scan for the appropriate larger type to use.
2245 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002246 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002247 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002248
2249 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002250 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2251 OpToUse = ISD::SINT_TO_FP;
2252 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002253 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002254 if (isSigned) continue;
2255
2256 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002257 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2258 OpToUse = ISD::UINT_TO_FP;
2259 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002260 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002261
2262 // Otherwise, try a larger type.
2263 }
2264
2265 // Okay, we found the operation and type to use. Zero extend our input to the
2266 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002267 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002268 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002269 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002270}
2271
2272/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2273/// FP_TO_*INT operation of the specified operand when the target requests that
2274/// we promote it. At this point, we know that the result and operand types are
2275/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2276/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002277SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002278 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002279 bool isSigned,
2280 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002281 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002282 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002283
2284 unsigned OpToUse = 0;
2285
2286 // Scan for the appropriate larger type to use.
2287 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002289 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002290
Eli Friedman3be2e512009-05-28 03:06:16 +00002291 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002292 OpToUse = ISD::FP_TO_SINT;
2293 break;
2294 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002295
Eli Friedman3be2e512009-05-28 03:06:16 +00002296 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002297 OpToUse = ISD::FP_TO_UINT;
2298 break;
2299 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002300
2301 // Otherwise, try a larger type.
2302 }
2303
Scott Michelfdc40a02009-02-17 22:15:04 +00002304
Chris Lattner27a6c732007-11-24 07:07:01 +00002305 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002306 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002307
Chris Lattner27a6c732007-11-24 07:07:01 +00002308 // Truncate the result of the extended FP_TO_*INT operation to the desired
2309 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002310 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002311}
2312
2313/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2314///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002315SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002316 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002317 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002318 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002319 switch (VT.getSimpleVT().SimpleTy) {
Craig Topper5e25ee82012-02-05 08:31:47 +00002320 default: llvm_unreachable("Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002321 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002322 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2323 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2324 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002325 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002326 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2327 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2328 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2329 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2330 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2331 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2332 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2333 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2334 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002335 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002336 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2337 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2338 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2339 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2340 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2341 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2342 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2343 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2344 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2345 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2346 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2347 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2348 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2349 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2350 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2351 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2352 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2353 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2354 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2355 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2356 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002357 }
2358}
2359
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002360/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002361// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002362static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2363 APInt Val = APInt(NumBits, ByteVal);
2364 unsigned Shift = 8;
2365 for (unsigned i = NumBits; i > 8; i >>= 1) {
2366 Val = (Val << Shift) | Val;
2367 Shift <<= 1;
2368 }
2369 return Val;
2370}
2371
Chris Lattner22cde6a2006-01-28 08:25:58 +00002372/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2373///
Scott Michelfdc40a02009-02-17 22:15:04 +00002374SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002375 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002376 switch (Opc) {
Craig Topper5e25ee82012-02-05 08:31:47 +00002377 default: llvm_unreachable("Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002378 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002379 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002380 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002381 unsigned Len = VT.getSizeInBits();
2382
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002383 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2384 "CTPOP not implemented for this type.");
2385
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002386 // This is the "best" algorithm from
2387 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2388
2389 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2390 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2391 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2392 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2393
2394 // v = v - ((v >> 1) & 0x55555555...)
2395 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2396 DAG.getNode(ISD::AND, dl, VT,
2397 DAG.getNode(ISD::SRL, dl, VT, Op,
2398 DAG.getConstant(1, ShVT)),
2399 Mask55));
2400 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2401 Op = DAG.getNode(ISD::ADD, dl, VT,
2402 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2403 DAG.getNode(ISD::AND, dl, VT,
2404 DAG.getNode(ISD::SRL, dl, VT, Op,
2405 DAG.getConstant(2, ShVT)),
2406 Mask33));
2407 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2408 Op = DAG.getNode(ISD::AND, dl, VT,
2409 DAG.getNode(ISD::ADD, dl, VT, Op,
2410 DAG.getNode(ISD::SRL, dl, VT, Op,
2411 DAG.getConstant(4, ShVT))),
2412 Mask0F);
2413 // v = (v * 0x01010101...) >> (Len - 8)
2414 Op = DAG.getNode(ISD::SRL, dl, VT,
2415 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2416 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002417
Chris Lattner22cde6a2006-01-28 08:25:58 +00002418 return Op;
2419 }
Chandler Carruth63974b22011-12-13 01:56:10 +00002420 case ISD::CTLZ_ZERO_UNDEF:
2421 // This trivially expands to CTLZ.
2422 return DAG.getNode(ISD::CTLZ, dl, Op.getValueType(), Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002423 case ISD::CTLZ: {
2424 // for now, we do this:
2425 // x = x | (x >> 1);
2426 // x = x | (x >> 2);
2427 // ...
2428 // x = x | (x >>16);
2429 // x = x | (x >>32); // for 64-bit input
2430 // return popcount(~x);
2431 //
2432 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002433 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002434 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002435 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002436 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002437 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002438 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002439 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002440 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002441 Op = DAG.getNOT(dl, Op, VT);
2442 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002443 }
Chandler Carruth63974b22011-12-13 01:56:10 +00002444 case ISD::CTTZ_ZERO_UNDEF:
2445 // This trivially expands to CTTZ.
2446 return DAG.getNode(ISD::CTTZ, dl, Op.getValueType(), Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002447 case ISD::CTTZ: {
2448 // for now, we use: { return popcount(~x & (x - 1)); }
2449 // unless the target has ctlz but not ctpop, in which case we use:
2450 // { return 32 - nlz(~x & (x-1)); }
2451 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002452 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002453 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2454 DAG.getNOT(dl, Op, VT),
2455 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002456 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002457 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002458 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2459 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002460 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002461 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002462 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2463 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002464 }
2465 }
2466}
Chris Lattnere34b3962005-01-19 04:19:40 +00002467
Jim Grosbache03262f2010-06-18 21:43:38 +00002468std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2469 unsigned Opc = Node->getOpcode();
2470 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2471 RTLIB::Libcall LC;
2472
2473 switch (Opc) {
2474 default:
2475 llvm_unreachable("Unhandled atomic intrinsic Expand!");
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002476 case ISD::ATOMIC_SWAP:
2477 switch (VT.SimpleTy) {
2478 default: llvm_unreachable("Unexpected value type for atomic!");
2479 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2480 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2481 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2482 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2483 }
2484 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002485 case ISD::ATOMIC_CMP_SWAP:
2486 switch (VT.SimpleTy) {
2487 default: llvm_unreachable("Unexpected value type for atomic!");
2488 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2489 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2490 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2491 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2492 }
2493 break;
2494 case ISD::ATOMIC_LOAD_ADD:
2495 switch (VT.SimpleTy) {
2496 default: llvm_unreachable("Unexpected value type for atomic!");
2497 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2498 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2499 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2500 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2501 }
2502 break;
2503 case ISD::ATOMIC_LOAD_SUB:
2504 switch (VT.SimpleTy) {
2505 default: llvm_unreachable("Unexpected value type for atomic!");
2506 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2507 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2508 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2509 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2510 }
2511 break;
2512 case ISD::ATOMIC_LOAD_AND:
2513 switch (VT.SimpleTy) {
2514 default: llvm_unreachable("Unexpected value type for atomic!");
2515 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2516 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2517 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2518 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2519 }
2520 break;
2521 case ISD::ATOMIC_LOAD_OR:
2522 switch (VT.SimpleTy) {
2523 default: llvm_unreachable("Unexpected value type for atomic!");
2524 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2525 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2526 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2527 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2528 }
2529 break;
2530 case ISD::ATOMIC_LOAD_XOR:
2531 switch (VT.SimpleTy) {
2532 default: llvm_unreachable("Unexpected value type for atomic!");
2533 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2534 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2535 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2536 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2537 }
2538 break;
2539 case ISD::ATOMIC_LOAD_NAND:
2540 switch (VT.SimpleTy) {
2541 default: llvm_unreachable("Unexpected value type for atomic!");
2542 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2543 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2544 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2545 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2546 }
2547 break;
2548 }
2549
2550 return ExpandChainLibCall(LC, Node, false);
2551}
2552
Dan Gohman65fd6562011-11-03 21:49:52 +00002553void SelectionDAGLegalize::ExpandNode(SDNode *Node) {
2554 SmallVector<SDValue, 8> Results;
Eli Friedman8c377c72009-05-27 01:25:56 +00002555 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002556 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002557 switch (Node->getOpcode()) {
2558 case ISD::CTPOP:
2559 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002560 case ISD::CTLZ_ZERO_UNDEF:
Eli Friedman8c377c72009-05-27 01:25:56 +00002561 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002562 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman8c377c72009-05-27 01:25:56 +00002563 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2564 Results.push_back(Tmp1);
2565 break;
2566 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002567 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002568 break;
2569 case ISD::FRAMEADDR:
2570 case ISD::RETURNADDR:
2571 case ISD::FRAME_TO_ARGS_OFFSET:
2572 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2573 break;
2574 case ISD::FLT_ROUNDS_:
2575 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2576 break;
2577 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002578 case ISD::EH_LABEL:
2579 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002580 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002581 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002582 // If the target didn't expand these, there's nothing to do, so just
2583 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002584 Results.push_back(Node->getOperand(0));
2585 break;
2586 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002587 // If the target didn't expand this, just return 'zero' and preserve the
2588 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002589 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002590 Results.push_back(Node->getOperand(0));
2591 break;
Eli Friedman14648462011-07-27 22:21:52 +00002592 case ISD::ATOMIC_FENCE:
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002593 case ISD::MEMBARRIER: {
2594 // If the target didn't lower this, lower it to '__sync_synchronize()' call
Eli Friedman14648462011-07-27 22:21:52 +00002595 // FIXME: handle "fence singlethread" more efficiently.
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002596 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002597 TargetLowering::
2598 CallLoweringInfo CLI(Node->getOperand(0),
2599 Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002600 false, false, false, false, 0, CallingConv::C,
2601 /*isTailCall=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002602 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002603 DAG.getExternalSymbol("__sync_synchronize",
2604 TLI.getPointerTy()),
2605 Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002606 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
2607
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002608 Results.push_back(CallResult.second);
2609 break;
2610 }
Eli Friedman069e2ed2011-08-26 02:59:24 +00002611 case ISD::ATOMIC_LOAD: {
2612 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
Eli Friedman331120b2011-09-15 21:20:49 +00002613 SDValue Zero = DAG.getConstant(0, Node->getValueType(0));
Eli Friedman069e2ed2011-08-26 02:59:24 +00002614 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
2615 cast<AtomicSDNode>(Node)->getMemoryVT(),
2616 Node->getOperand(0),
2617 Node->getOperand(1), Zero, Zero,
2618 cast<AtomicSDNode>(Node)->getMemOperand(),
2619 cast<AtomicSDNode>(Node)->getOrdering(),
2620 cast<AtomicSDNode>(Node)->getSynchScope());
2621 Results.push_back(Swap.getValue(0));
2622 Results.push_back(Swap.getValue(1));
2623 break;
2624 }
2625 case ISD::ATOMIC_STORE: {
2626 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
2627 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
2628 cast<AtomicSDNode>(Node)->getMemoryVT(),
2629 Node->getOperand(0),
2630 Node->getOperand(1), Node->getOperand(2),
2631 cast<AtomicSDNode>(Node)->getMemOperand(),
2632 cast<AtomicSDNode>(Node)->getOrdering(),
2633 cast<AtomicSDNode>(Node)->getSynchScope());
2634 Results.push_back(Swap.getValue(1));
2635 break;
2636 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002637 // By default, atomic intrinsics are marked Legal and lowered. Targets
2638 // which don't support them directly, however, may want libcalls, in which
2639 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002640 case ISD::ATOMIC_SWAP:
2641 case ISD::ATOMIC_LOAD_ADD:
2642 case ISD::ATOMIC_LOAD_SUB:
2643 case ISD::ATOMIC_LOAD_AND:
2644 case ISD::ATOMIC_LOAD_OR:
2645 case ISD::ATOMIC_LOAD_XOR:
2646 case ISD::ATOMIC_LOAD_NAND:
2647 case ISD::ATOMIC_LOAD_MIN:
2648 case ISD::ATOMIC_LOAD_MAX:
2649 case ISD::ATOMIC_LOAD_UMIN:
2650 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002651 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002652 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2653 Results.push_back(Tmp.first);
2654 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002655 break;
Evan Chenga8457062010-06-18 22:01:37 +00002656 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002657 case ISD::DYNAMIC_STACKALLOC:
2658 ExpandDYNAMIC_STACKALLOC(Node, Results);
2659 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002660 case ISD::MERGE_VALUES:
2661 for (unsigned i = 0; i < Node->getNumValues(); i++)
2662 Results.push_back(Node->getOperand(i));
2663 break;
2664 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002665 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002666 if (VT.isInteger())
2667 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002668 else {
2669 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002670 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002671 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002672 break;
2673 }
2674 case ISD::TRAP: {
2675 // If this operation is not supported, lower it to 'abort()' call
2676 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002677 TargetLowering::
2678 CallLoweringInfo CLI(Node->getOperand(0),
2679 Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002680 false, false, false, false, 0, CallingConv::C,
2681 /*isTailCall=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002682 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002683 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002684 Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002685 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
2686
Eli Friedman8c377c72009-05-27 01:25:56 +00002687 Results.push_back(CallResult.second);
2688 break;
2689 }
2690 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002691 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002692 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2693 Node->getValueType(0), dl);
2694 Results.push_back(Tmp1);
2695 break;
2696 case ISD::FP_EXTEND:
2697 Tmp1 = EmitStackConvert(Node->getOperand(0),
2698 Node->getOperand(0).getValueType(),
2699 Node->getValueType(0), dl);
2700 Results.push_back(Tmp1);
2701 break;
2702 case ISD::SIGN_EXTEND_INREG: {
2703 // NOTE: we could fall back on load/store here too for targets without
2704 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002705 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002706 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00002707 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00002708 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002709 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002710 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2711 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002712 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002713 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2714 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002715 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2716 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002717 break;
2718 }
2719 case ISD::FP_ROUND_INREG: {
2720 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002721 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00002722
2723 // NOTE: there is a choice here between constantly creating new stack
2724 // slots and always reusing the same one. We currently always create
2725 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002726 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002727 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2728 Node->getValueType(0), dl);
2729 Results.push_back(Tmp1);
2730 break;
2731 }
2732 case ISD::SINT_TO_FP:
2733 case ISD::UINT_TO_FP:
2734 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2735 Node->getOperand(0), Node->getValueType(0), dl);
2736 Results.push_back(Tmp1);
2737 break;
2738 case ISD::FP_TO_UINT: {
2739 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002740 EVT VT = Node->getOperand(0).getValueType();
2741 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002742 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002743 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2744 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2745 Tmp1 = DAG.getConstantFP(apf, VT);
2746 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2747 Node->getOperand(0),
2748 Tmp1, ISD::SETLT);
2749 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002750 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2751 DAG.getNode(ISD::FSUB, dl, VT,
2752 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002753 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2754 DAG.getConstant(x, NVT));
2755 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2756 Results.push_back(Tmp1);
2757 break;
2758 }
Eli Friedman509150f2009-05-27 07:58:35 +00002759 case ISD::VAARG: {
2760 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002761 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002762 Tmp1 = Node->getOperand(0);
2763 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002764 unsigned Align = Node->getConstantOperandVal(3);
2765
Chris Lattnerecf42c42010-09-21 16:36:31 +00002766 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002767 MachinePointerInfo(V),
2768 false, false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002769 SDValue VAList = VAListLoad;
2770
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002771 if (Align > TLI.getMinStackArgumentAlignment()) {
2772 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2773
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002774 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2775 DAG.getConstant(Align - 1,
2776 TLI.getPointerTy()));
2777
2778 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002779 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002780 TLI.getPointerTy()));
2781 }
2782
Eli Friedman509150f2009-05-27 07:58:35 +00002783 // Increment the pointer, VAList, to the next vaarg
2784 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2785 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002786 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002787 TLI.getPointerTy()));
2788 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002789 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2790 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002791 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002792 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002793 false, false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002794 Results.push_back(Results[0].getValue(1));
2795 break;
2796 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002797 case ISD::VACOPY: {
2798 // This defaults to loading a pointer from the input and storing it to the
2799 // output, returning the chain.
2800 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2801 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2802 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002803 Node->getOperand(2), MachinePointerInfo(VS),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002804 false, false, false, 0);
Chris Lattnerecf42c42010-09-21 16:36:31 +00002805 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2806 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002807 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002808 break;
2809 }
2810 case ISD::EXTRACT_VECTOR_ELT:
2811 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2812 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002813 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002814 Node->getOperand(0));
2815 else
2816 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2817 Results.push_back(Tmp1);
2818 break;
2819 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002820 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002821 break;
David Greenecfe33c42011-01-26 19:13:22 +00002822 case ISD::INSERT_SUBVECTOR:
2823 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2824 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002825 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002826 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002827 break;
2828 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002829 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002830 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002831 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002832 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002833 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2834 Node->getOperand(1),
2835 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002836 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002837 case ISD::VECTOR_SHUFFLE: {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00002838 SmallVector<int, 32> NewMask;
2839 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
Eli Friedman509150f2009-05-27 07:58:35 +00002840
Owen Andersone50ed302009-08-10 22:56:29 +00002841 EVT VT = Node->getValueType(0);
2842 EVT EltVT = VT.getVectorElementType();
Elena Demikhovskyce58a032012-01-03 11:59:04 +00002843 SDValue Op0 = Node->getOperand(0);
2844 SDValue Op1 = Node->getOperand(1);
2845 if (!TLI.isTypeLegal(EltVT)) {
2846
2847 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
2848
2849 // BUILD_VECTOR operands are allowed to be wider than the element type.
2850 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept it
2851 if (NewEltVT.bitsLT(EltVT)) {
2852
2853 // Convert shuffle node.
2854 // If original node was v4i64 and the new EltVT is i32,
2855 // cast operands to v8i32 and re-build the mask.
2856
2857 // Calculate new VT, the size of the new VT should be equal to original.
2858 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltVT,
2859 VT.getSizeInBits()/NewEltVT.getSizeInBits());
2860 assert(NewVT.bitsEq(VT));
2861
2862 // cast operands to new VT
2863 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
2864 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
2865
2866 // Convert the shuffle mask
2867 unsigned int factor = NewVT.getVectorNumElements()/VT.getVectorNumElements();
2868
2869 // EltVT gets smaller
2870 assert(factor > 0);
Elena Demikhovskyce58a032012-01-03 11:59:04 +00002871
2872 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
2873 if (Mask[i] < 0) {
2874 for (unsigned fi = 0; fi < factor; ++fi)
2875 NewMask.push_back(Mask[i]);
2876 }
2877 else {
2878 for (unsigned fi = 0; fi < factor; ++fi)
2879 NewMask.push_back(Mask[i]*factor+fi);
2880 }
2881 }
2882 Mask = NewMask;
2883 VT = NewVT;
2884 }
2885 EltVT = NewEltVT;
2886 }
Eli Friedman509150f2009-05-27 07:58:35 +00002887 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovskyce58a032012-01-03 11:59:04 +00002888 SmallVector<SDValue, 16> Ops;
Eli Friedman509150f2009-05-27 07:58:35 +00002889 for (unsigned i = 0; i != NumElems; ++i) {
2890 if (Mask[i] < 0) {
2891 Ops.push_back(DAG.getUNDEF(EltVT));
2892 continue;
2893 }
2894 unsigned Idx = Mask[i];
2895 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002896 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
Elena Demikhovskyce58a032012-01-03 11:59:04 +00002897 Op0,
Bill Wendling775db972009-12-23 00:28:23 +00002898 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002899 else
Bill Wendling775db972009-12-23 00:28:23 +00002900 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
Elena Demikhovskyce58a032012-01-03 11:59:04 +00002901 Op1,
Bill Wendling775db972009-12-23 00:28:23 +00002902 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002903 }
Nadav Rotem6c0366c2012-01-10 14:28:46 +00002904
Eli Friedman509150f2009-05-27 07:58:35 +00002905 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Nadav Rotem6c0366c2012-01-10 14:28:46 +00002906 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
2907 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00002908 Results.push_back(Tmp1);
2909 break;
2910 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002911 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002912 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002913 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2914 // 1 -> Hi
2915 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2916 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00002917 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00002918 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2919 } else {
2920 // 0 -> Lo
2921 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2922 Node->getOperand(0));
2923 }
2924 Results.push_back(Tmp1);
2925 break;
2926 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002927 case ISD::STACKSAVE:
2928 // Expand to CopyFromReg if the target set
2929 // StackPointerRegisterToSaveRestore.
2930 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002931 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2932 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002933 Results.push_back(Results[0].getValue(1));
2934 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002935 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002936 Results.push_back(Node->getOperand(0));
2937 }
2938 break;
2939 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002940 // Expand to CopyToReg if the target set
2941 // StackPointerRegisterToSaveRestore.
2942 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2943 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2944 Node->getOperand(1)));
2945 } else {
2946 Results.push_back(Node->getOperand(0));
2947 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002948 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002949 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002950 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002951 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002952 case ISD::FNEG:
2953 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2954 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2955 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2956 Node->getOperand(0));
2957 Results.push_back(Tmp1);
2958 break;
2959 case ISD::FABS: {
2960 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002961 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002962 Tmp1 = Node->getOperand(0);
2963 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002964 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002965 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002966 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2967 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002968 Results.push_back(Tmp1);
2969 break;
2970 }
2971 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002972 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2973 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002974 break;
2975 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002976 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2977 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002978 break;
2979 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002980 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2981 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002982 break;
2983 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002984 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2985 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002986 break;
2987 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002988 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2989 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002990 break;
2991 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002992 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2993 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002994 break;
2995 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002996 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2997 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002998 break;
2999 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00003000 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3001 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003002 break;
3003 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00003004 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3005 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003006 break;
3007 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00003008 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3009 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003010 break;
3011 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00003012 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3013 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003014 break;
3015 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003016 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3017 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003018 break;
3019 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003020 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3021 RTLIB::NEARBYINT_F64,
3022 RTLIB::NEARBYINT_F80,
3023 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003024 break;
3025 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003026 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3027 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003028 break;
3029 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003030 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3031 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003032 break;
3033 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003034 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3035 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003036 break;
3037 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003038 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3039 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003040 break;
Cameron Zwarich33390842011-07-08 21:39:21 +00003041 case ISD::FMA:
3042 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
3043 RTLIB::FMA_F80, RTLIB::FMA_PPCF128));
3044 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003045 case ISD::FP16_TO_FP32:
3046 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3047 break;
3048 case ISD::FP32_TO_FP16:
3049 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3050 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003051 case ISD::ConstantFP: {
3052 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003053 // Check to see if this FP immediate is already legal.
3054 // If this is a legal constant, turn it into a TargetConstantFP node.
Dan Gohman65fd6562011-11-03 21:49:52 +00003055 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
3056 Results.push_back(ExpandConstantFP(CFP, true));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003057 break;
3058 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003059 case ISD::EHSELECTION: {
3060 unsigned Reg = TLI.getExceptionSelectorRegister();
3061 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003062 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3063 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003064 Results.push_back(Results[0].getValue(1));
3065 break;
3066 }
3067 case ISD::EXCEPTIONADDR: {
Lang Hames07961342012-02-14 04:45:49 +00003068 unsigned Reg = TLI.getExceptionPointerRegister();
Eli Friedman26ea8f92009-05-27 07:05:37 +00003069 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003070 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3071 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003072 Results.push_back(Results[0].getValue(1));
3073 break;
3074 }
Owen Andersonafd3d562012-03-06 00:29:31 +00003075 case ISD::FSUB: {
3076 EVT VT = Node->getValueType(0);
3077 assert(TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
3078 TLI.isOperationLegalOrCustom(ISD::FNEG, VT) &&
3079 "Don't know how to expand this FP subtraction!");
3080 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
3081 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1);
3082 Results.push_back(Tmp1);
3083 break;
3084 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003085 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003086 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003087 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3088 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3089 "Don't know how to expand this subtraction!");
3090 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3091 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Owen Anderson4b6e6752012-05-21 22:39:20 +00003092 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, VT));
Bill Wendling775db972009-12-23 00:28:23 +00003093 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003094 break;
3095 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003096 case ISD::UREM:
3097 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003098 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003099 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003100 bool isSigned = Node->getOpcode() == ISD::SREM;
3101 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3102 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3103 Tmp2 = Node->getOperand(0);
3104 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003105 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3106 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
Evan Cheng8ef09682012-06-21 05:56:05 +00003107 useDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00003108 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3109 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003110 // X % Y -> X-X/Y*Y
3111 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3112 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3113 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003114 } else if (isSigned)
3115 Tmp1 = ExpandIntLibCall(Node, true,
3116 RTLIB::SREM_I8,
3117 RTLIB::SREM_I16, RTLIB::SREM_I32,
3118 RTLIB::SREM_I64, RTLIB::SREM_I128);
3119 else
3120 Tmp1 = ExpandIntLibCall(Node, false,
3121 RTLIB::UREM_I8,
3122 RTLIB::UREM_I16, RTLIB::UREM_I32,
3123 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003124 Results.push_back(Tmp1);
3125 break;
3126 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003127 case ISD::UDIV:
3128 case ISD::SDIV: {
3129 bool isSigned = Node->getOpcode() == ISD::SDIV;
3130 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003131 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003132 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003133 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3134 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
Evan Cheng8ef09682012-06-21 05:56:05 +00003135 useDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003136 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3137 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003138 else if (isSigned)
3139 Tmp1 = ExpandIntLibCall(Node, true,
3140 RTLIB::SDIV_I8,
3141 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3142 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3143 else
3144 Tmp1 = ExpandIntLibCall(Node, false,
3145 RTLIB::UDIV_I8,
3146 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3147 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003148 Results.push_back(Tmp1);
3149 break;
3150 }
3151 case ISD::MULHU:
3152 case ISD::MULHS: {
3153 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3154 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003155 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003156 SDVTList VTs = DAG.getVTList(VT, VT);
3157 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3158 "If this wasn't legal, it shouldn't have been created!");
3159 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3160 Node->getOperand(1));
3161 Results.push_back(Tmp1.getValue(1));
3162 break;
3163 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003164 case ISD::SDIVREM:
3165 case ISD::UDIVREM:
3166 // Expand into divrem libcall
3167 ExpandDivRemLibCall(Node, Results);
3168 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003169 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003170 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003171 SDVTList VTs = DAG.getVTList(VT, VT);
3172 // See if multiply or divide can be lowered using two-result operations.
3173 // We just need the low half of the multiply; try both the signed
3174 // and unsigned forms. If the target supports both SMUL_LOHI and
3175 // UMUL_LOHI, form a preference by checking which forms of plain
3176 // MULH it supports.
3177 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3178 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3179 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3180 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3181 unsigned OpToUse = 0;
3182 if (HasSMUL_LOHI && !HasMULHS) {
3183 OpToUse = ISD::SMUL_LOHI;
3184 } else if (HasUMUL_LOHI && !HasMULHU) {
3185 OpToUse = ISD::UMUL_LOHI;
3186 } else if (HasSMUL_LOHI) {
3187 OpToUse = ISD::SMUL_LOHI;
3188 } else if (HasUMUL_LOHI) {
3189 OpToUse = ISD::UMUL_LOHI;
3190 }
3191 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003192 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3193 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003194 break;
3195 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003196 Tmp1 = ExpandIntLibCall(Node, false,
3197 RTLIB::MUL_I8,
3198 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003199 RTLIB::MUL_I64, RTLIB::MUL_I128);
3200 Results.push_back(Tmp1);
3201 break;
3202 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003203 case ISD::SADDO:
3204 case ISD::SSUBO: {
3205 SDValue LHS = Node->getOperand(0);
3206 SDValue RHS = Node->getOperand(1);
3207 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3208 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3209 LHS, RHS);
3210 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003211 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003212
Eli Friedman4bc8c712009-05-27 12:20:41 +00003213 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3214
3215 // LHSSign -> LHS >= 0
3216 // RHSSign -> RHS >= 0
3217 // SumSign -> Sum >= 0
3218 //
3219 // Add:
3220 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3221 // Sub:
3222 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3223 //
3224 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3225 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3226 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3227 Node->getOpcode() == ISD::SADDO ?
3228 ISD::SETEQ : ISD::SETNE);
3229
3230 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3231 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3232
3233 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3234 Results.push_back(Cmp);
3235 break;
3236 }
3237 case ISD::UADDO:
3238 case ISD::USUBO: {
3239 SDValue LHS = Node->getOperand(0);
3240 SDValue RHS = Node->getOperand(1);
3241 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3242 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3243 LHS, RHS);
3244 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003245 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3246 Node->getOpcode () == ISD::UADDO ?
3247 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003248 break;
3249 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003250 case ISD::UMULO:
3251 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003252 EVT VT = Node->getValueType(0);
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003253 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003254 SDValue LHS = Node->getOperand(0);
3255 SDValue RHS = Node->getOperand(1);
3256 SDValue BottomHalf;
3257 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003258 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003259 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3260 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3261 bool isSigned = Node->getOpcode() == ISD::SMULO;
3262 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3263 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3264 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3265 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3266 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3267 RHS);
3268 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003269 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3270 VT.getSizeInBits() * 2))) {
Eli Friedmandb3c1692009-06-16 06:58:29 +00003271 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3272 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3273 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3274 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3275 DAG.getIntPtrConstant(0));
3276 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3277 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003278 } else {
3279 // We can fall back to a libcall with an illegal type for the MUL if we
3280 // have a libcall big enough.
3281 // Also, we can fall back to a division in some cases, but that's a big
3282 // performance hit in the general case.
Eric Christopher38a18262011-01-20 00:29:24 +00003283 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3284 if (WideVT == MVT::i16)
3285 LC = RTLIB::MUL_I16;
3286 else if (WideVT == MVT::i32)
3287 LC = RTLIB::MUL_I32;
3288 else if (WideVT == MVT::i64)
3289 LC = RTLIB::MUL_I64;
3290 else if (WideVT == MVT::i128)
3291 LC = RTLIB::MUL_I128;
3292 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanf316eb72011-05-16 22:09:53 +00003293
3294 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003295 // part.
3296 unsigned LoSize = VT.getSizeInBits();
3297 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
3298 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
3299 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
3300 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
Owen Anderson95771af2011-02-25 21:41:48 +00003301
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003302 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3303 // pre-lowered to the correct types. This all depends upon WideVT not
3304 // being a legal type for the architecture and thus has to be split to
3305 // two arguments.
3306 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3307 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3308 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3309 DAG.getIntPtrConstant(0));
3310 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3311 DAG.getIntPtrConstant(1));
Dan Gohman65fd6562011-11-03 21:49:52 +00003312 // Ret is a node with an illegal type. Because such things are not
3313 // generally permitted during this phase of legalization, delete the
3314 // node. The above EXTRACT_ELEMENT nodes should have been folded.
3315 DAG.DeleteNode(Ret.getNode());
Eli Friedmandb3c1692009-06-16 06:58:29 +00003316 }
Dan Gohmanf316eb72011-05-16 22:09:53 +00003317
Eli Friedmandb3c1692009-06-16 06:58:29 +00003318 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003319 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3320 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003321 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3322 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3323 ISD::SETNE);
3324 } else {
3325 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3326 DAG.getConstant(0, VT), ISD::SETNE);
3327 }
3328 Results.push_back(BottomHalf);
3329 Results.push_back(TopHalf);
3330 break;
3331 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003332 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003333 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003334 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3335 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003336 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003337 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003338 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003339 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003340 break;
3341 }
Eli Friedman509150f2009-05-27 07:58:35 +00003342 case ISD::SELECT:
3343 Tmp1 = Node->getOperand(0);
3344 Tmp2 = Node->getOperand(1);
3345 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003346 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003347 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3348 Tmp2, Tmp3,
3349 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003350 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003351 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3352 DAG.getConstant(0, Tmp1.getValueType()),
3353 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003354 }
Eli Friedman509150f2009-05-27 07:58:35 +00003355 Results.push_back(Tmp1);
3356 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003357 case ISD::BR_JT: {
3358 SDValue Chain = Node->getOperand(0);
3359 SDValue Table = Node->getOperand(1);
3360 SDValue Index = Node->getOperand(2);
3361
Owen Andersone50ed302009-08-10 22:56:29 +00003362 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003363
3364 const TargetData &TD = *TLI.getTargetData();
3365 unsigned EntrySize =
3366 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003367
Chris Lattner071c62f2010-01-25 23:26:13 +00003368 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003369 Index, DAG.getConstant(EntrySize, PTy));
3370 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3371
Owen Anderson23b9b192009-08-12 00:36:31 +00003372 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003373 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003374 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003375 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003376 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003377 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003378 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003379 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003380 // RelocBase can be JumpTable, GOT or some sort of global base.
3381 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3382 TLI.getPICJumpTableRelocBase(Table, DAG));
3383 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003384 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003385 Results.push_back(Tmp1);
3386 break;
3387 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003388 case ISD::BRCOND:
3389 // Expand brcond's setcc into its constituent parts and create a BR_CC
3390 // Node.
3391 Tmp1 = Node->getOperand(0);
3392 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003393 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003394 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003395 Tmp1, Tmp2.getOperand(2),
3396 Tmp2.getOperand(0), Tmp2.getOperand(1),
3397 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003398 } else {
Stuart Hastings88882242011-05-13 00:51:54 +00003399 // We test only the i1 bit. Skip the AND if UNDEF.
3400 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3401 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3402 DAG.getConstant(1, Tmp2.getValueType()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastings88882242011-05-13 00:51:54 +00003404 DAG.getCondCode(ISD::SETNE), Tmp3,
3405 DAG.getConstant(0, Tmp3.getValueType()),
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003406 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003407 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003408 Results.push_back(Tmp1);
3409 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003410 case ISD::SETCC: {
3411 Tmp1 = Node->getOperand(0);
3412 Tmp2 = Node->getOperand(1);
3413 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003414 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003415
3416 // If we expanded the SETCC into an AND/OR, return the new node
3417 if (Tmp2.getNode() == 0) {
3418 Results.push_back(Tmp1);
3419 break;
3420 }
3421
3422 // Otherwise, SETCC for the given comparison type must be completely
3423 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003424 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003425 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3426 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3427 Results.push_back(Tmp1);
3428 break;
3429 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003430 case ISD::SELECT_CC: {
3431 Tmp1 = Node->getOperand(0); // LHS
3432 Tmp2 = Node->getOperand(1); // RHS
3433 Tmp3 = Node->getOperand(2); // True
3434 Tmp4 = Node->getOperand(3); // False
3435 SDValue CC = Node->getOperand(4);
3436
3437 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003438 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003439
3440 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3441 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3442 CC = DAG.getCondCode(ISD::SETNE);
3443 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3444 Tmp3, Tmp4, CC);
3445 Results.push_back(Tmp1);
3446 break;
3447 }
3448 case ISD::BR_CC: {
3449 Tmp1 = Node->getOperand(0); // Chain
3450 Tmp2 = Node->getOperand(2); // LHS
3451 Tmp3 = Node->getOperand(3); // RHS
3452 Tmp4 = Node->getOperand(1); // CC
3453
3454 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003455 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003456
3457 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3458 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3459 Tmp4 = DAG.getCondCode(ISD::SETNE);
3460 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3461 Tmp3, Node->getOperand(4));
3462 Results.push_back(Tmp1);
3463 break;
3464 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003465 case ISD::BUILD_VECTOR:
3466 Results.push_back(ExpandBUILD_VECTOR(Node));
3467 break;
3468 case ISD::SRA:
3469 case ISD::SRL:
3470 case ISD::SHL: {
3471 // Scalarize vector SRA/SRL/SHL.
3472 EVT VT = Node->getValueType(0);
3473 assert(VT.isVector() && "Unable to legalize non-vector shift");
3474 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
3475 unsigned NumElem = VT.getVectorNumElements();
3476
3477 SmallVector<SDValue, 8> Scalars;
3478 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
3479 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3480 VT.getScalarType(),
3481 Node->getOperand(0), DAG.getIntPtrConstant(Idx));
3482 SDValue Sh = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3483 VT.getScalarType(),
3484 Node->getOperand(1), DAG.getIntPtrConstant(Idx));
3485 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
3486 VT.getScalarType(), Ex, Sh));
3487 }
3488 SDValue Result =
3489 DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
3490 &Scalars[0], Scalars.size());
Eli Friedman0e3642a2011-11-11 23:58:27 +00003491 ReplaceNode(SDValue(Node, 0), Result);
Dan Gohman65fd6562011-11-03 21:49:52 +00003492 break;
3493 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003494 case ISD::GLOBAL_OFFSET_TABLE:
3495 case ISD::GlobalAddress:
3496 case ISD::GlobalTLSAddress:
3497 case ISD::ExternalSymbol:
3498 case ISD::ConstantPool:
3499 case ISD::JumpTable:
3500 case ISD::INTRINSIC_W_CHAIN:
3501 case ISD::INTRINSIC_WO_CHAIN:
3502 case ISD::INTRINSIC_VOID:
3503 // FIXME: Custom lowering for these operations shouldn't return null!
Eli Friedman3f727d62009-05-27 02:16:40 +00003504 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003505 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003506
3507 // Replace the original node with the legalized result.
Eli Friedman0e3642a2011-11-11 23:58:27 +00003508 if (!Results.empty())
3509 ReplaceNode(Node, Results.data());
Eli Friedman8c377c72009-05-27 01:25:56 +00003510}
Dan Gohman65fd6562011-11-03 21:49:52 +00003511
3512void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
3513 SmallVector<SDValue, 8> Results;
Owen Andersone50ed302009-08-10 22:56:29 +00003514 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003515 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003516 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003517 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003518 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003519 }
Owen Andersone50ed302009-08-10 22:56:29 +00003520 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003521 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003522 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003523 switch (Node->getOpcode()) {
3524 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00003525 case ISD::CTTZ_ZERO_UNDEF:
Eli Friedman8c377c72009-05-27 01:25:56 +00003526 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00003527 case ISD::CTLZ_ZERO_UNDEF:
Eli Friedman8c377c72009-05-27 01:25:56 +00003528 case ISD::CTPOP:
3529 // Zero extend the argument.
3530 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00003531 // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
3532 // already the correct result.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003533 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003534 if (Node->getOpcode() == ISD::CTTZ) {
Chandler Carruth63974b22011-12-13 01:56:10 +00003535 // FIXME: This should set a bit in the zero extended value instead.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003536 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003537 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3538 ISD::SETEQ);
3539 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3540 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Chandler Carruth63974b22011-12-13 01:56:10 +00003541 } else if (Node->getOpcode() == ISD::CTLZ ||
3542 Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003543 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3544 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3545 DAG.getConstant(NVT.getSizeInBits() -
3546 OVT.getSizeInBits(), NVT));
3547 }
Bill Wendling775db972009-12-23 00:28:23 +00003548 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003549 break;
3550 case ISD::BSWAP: {
3551 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003552 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003553 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3554 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003555 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003556 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003557 break;
3558 }
3559 case ISD::FP_TO_UINT:
3560 case ISD::FP_TO_SINT:
3561 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3562 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3563 Results.push_back(Tmp1);
3564 break;
3565 case ISD::UINT_TO_FP:
3566 case ISD::SINT_TO_FP:
3567 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3568 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3569 Results.push_back(Tmp1);
3570 break;
Hal Finkel5194d6d2012-03-24 03:53:52 +00003571 case ISD::VAARG: {
3572 SDValue Chain = Node->getOperand(0); // Get the chain.
3573 SDValue Ptr = Node->getOperand(1); // Get the pointer.
3574
3575 unsigned TruncOp;
3576 if (OVT.isVector()) {
3577 TruncOp = ISD::BITCAST;
3578 } else {
3579 assert(OVT.isInteger()
3580 && "VAARG promotion is supported only for vectors or integer types");
3581 TruncOp = ISD::TRUNCATE;
3582 }
3583
3584 // Perform the larger operation, then convert back
3585 Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
3586 Node->getConstantOperandVal(3));
3587 Chain = Tmp1.getValue(1);
3588
3589 Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
3590
3591 // Modified the chain result - switch anything that used the old chain to
3592 // use the new one.
3593 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
3594 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
3595 ReplacedNode(Node);
3596 break;
3597 }
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003598 case ISD::AND:
3599 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003600 case ISD::XOR: {
3601 unsigned ExtOp, TruncOp;
3602 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003603 ExtOp = ISD::BITCAST;
3604 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003605 } else {
3606 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003607 ExtOp = ISD::ANY_EXTEND;
3608 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003609 }
3610 // Promote each of the values to the new type.
3611 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3612 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3613 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003614 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3615 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003616 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003617 }
3618 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003619 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003620 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003621 ExtOp = ISD::BITCAST;
3622 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003623 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003624 ExtOp = ISD::ANY_EXTEND;
3625 TruncOp = ISD::TRUNCATE;
3626 } else {
3627 ExtOp = ISD::FP_EXTEND;
3628 TruncOp = ISD::FP_ROUND;
3629 }
3630 Tmp1 = Node->getOperand(0);
3631 // Promote each of the values to the new type.
3632 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3633 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3634 // Perform the larger operation, then round down.
3635 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3636 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003637 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003638 else
Bill Wendling775db972009-12-23 00:28:23 +00003639 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003640 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003641 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003642 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003643 }
Eli Friedman509150f2009-05-27 07:58:35 +00003644 case ISD::VECTOR_SHUFFLE: {
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003645 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
Eli Friedman509150f2009-05-27 07:58:35 +00003646
3647 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003648 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3649 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003650
3651 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003652 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003653 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003654 Results.push_back(Tmp1);
3655 break;
3656 }
Eli Friedmanad754602009-05-28 03:56:57 +00003657 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003658 unsigned ExtOp = ISD::FP_EXTEND;
3659 if (NVT.isInteger()) {
3660 ISD::CondCode CCCode =
3661 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3662 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003663 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003664 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3665 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003666 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3667 Tmp1, Tmp2, Node->getOperand(2)));
3668 break;
3669 }
Pete Coopercfe29982012-03-19 23:38:12 +00003670 case ISD::FDIV:
Pete Cooper9751b812012-04-04 19:36:31 +00003671 case ISD::FREM:
Pete Cooperd578b902012-01-12 21:46:18 +00003672 case ISD::FPOW: {
3673 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
3674 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
Pete Coopercfe29982012-03-19 23:38:12 +00003675 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Pete Cooperd578b902012-01-12 21:46:18 +00003676 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
3677 Tmp3, DAG.getIntPtrConstant(0)));
3678 break;
3679 }
3680 case ISD::FLOG2:
3681 case ISD::FEXP2:
3682 case ISD::FLOG:
3683 case ISD::FEXP: {
3684 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
3685 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
3686 Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
3687 Tmp2, DAG.getIntPtrConstant(0)));
3688 break;
3689 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003690 }
Dan Gohman65fd6562011-11-03 21:49:52 +00003691
3692 // Replace the original node with the legalized result.
Eli Friedman0e3642a2011-11-11 23:58:27 +00003693 if (!Results.empty())
3694 ReplaceNode(Node, Results.data());
Eli Friedman8c377c72009-05-27 01:25:56 +00003695}
3696
Chris Lattner3e928bb2005-01-07 07:47:09 +00003697// SelectionDAG::Legalize - This is the entry point for the file.
3698//
Dan Gohman975716a2011-05-16 22:19:54 +00003699void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003700 /// run - This is the main entry point to this class.
3701 ///
Dan Gohman975716a2011-05-16 22:19:54 +00003702 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003703}