blob: 288b9f76cc8a7f82b2ef4cb4b7ea83bdd65eeaef [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng3d2125c2010-11-30 23:55:39 +000014#include "llvm/Analysis/DebugInfo.h"
15#include "llvm/CodeGen/Analysis.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000016#include "llvm/CodeGen/MachineFunction.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng3d2125c2010-11-30 23:55:39 +000018#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000019#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000020#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000021#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000026#include "llvm/LLVMContext.h"
David Greene993aace2010-01-05 01:24:53 +000027#include "llvm/Support/Debug.h"
Jim Grosbache03262f2010-06-18 21:43:38 +000028#include "llvm/Support/ErrorHandling.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattner79715142007-02-03 01:12:36 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000032#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000033#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000034using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
38/// hacks on it until the target machine can handle it. This involves
39/// eliminating value sizes the machine cannot handle (promoting small sizes to
40/// large sizes or splitting up large values into small values) as well as
41/// eliminating operations the machine cannot handle.
42///
43/// This code also does a small amount of optimization and recognition of idioms
44/// as part of its processing. For example, if a target does not support a
45/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
46/// will attempt merge setcc and brc instructions into brcc's.
47///
48namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000049class SelectionDAGLegalize {
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
Chris Lattner6831a812006-02-13 09:18:02 +000054 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000055
Stuart Hastingsfc521632011-04-19 16:16:58 +000056 /// LastCALLSEQ - This keeps track of the CALLSEQ_END node that has been
Chris Lattner6831a812006-02-13 09:18:02 +000057 /// legalized. We use this to ensure that calls are properly serialized
58 /// against each other, including inserted libcalls.
Stuart Hastingsfc521632011-04-19 16:16:58 +000059 SmallVector<SDValue, 8> LastCALLSEQ;
Scott Michelfdc40a02009-02-17 22:15:04 +000060
Chris Lattner3e928bb2005-01-07 07:47:09 +000061 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000062 Legal, // The target natively supports this operation.
63 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000064 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000065 };
Scott Michelfdc40a02009-02-17 22:15:04 +000066
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 /// LegalizedNodes - For nodes that are of legal width, and that have more
68 /// than one use, this map indicates what regularized operand to use. This
69 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000070 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000071
Dan Gohman475871a2008-07-27 21:46:04 +000072 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000073 LegalizedNodes.insert(std::make_pair(From, To));
74 // If someone requests legalization of the new node, return itself.
75 if (From != To)
76 LegalizedNodes.insert(std::make_pair(To, To));
Owen Anderson95771af2011-02-25 21:41:48 +000077
Devang Patela778f5c2011-02-18 22:43:42 +000078 // Transfer SDDbgValues.
79 DAG.TransferDbgValues(From, To);
Chris Lattner8afc48e2005-01-07 22:28:47 +000080 }
81
Chris Lattner3e928bb2005-01-07 07:47:09 +000082public:
Dan Gohman975716a2011-05-16 22:19:54 +000083 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000084
Chris Lattner3e928bb2005-01-07 07:47:09 +000085 /// getTypeAction - Return how we should legalize values of this type, either
86 /// it is already legal or we need to expand it into multiple registers of
87 /// smaller integer type, or we need to promote it to a larger type.
Owen Andersone50ed302009-08-10 22:56:29 +000088 LegalizeAction getTypeAction(EVT VT) const {
Nadav Rotem2d6dcb32011-05-27 21:03:13 +000089 return (LegalizeAction)TLI.getTypeAction(*DAG.getContext(), VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +000090 }
91
92 /// isTypeLegal - Return true if this type is legal on this target.
93 ///
Owen Andersone50ed302009-08-10 22:56:29 +000094 bool isTypeLegal(EVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +000095 return getTypeAction(VT) == Legal;
96 }
97
Chris Lattner3e928bb2005-01-07 07:47:09 +000098 void LegalizeDAG();
99
Chris Lattner456a93a2006-01-28 07:39:30 +0000100private:
Dan Gohman6a109f92011-07-15 21:42:20 +0000101 /// LegalizeOp - Return a legal replacement for the given operation, with
102 /// all legal operands.
Dan Gohman475871a2008-07-27 21:46:04 +0000103 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000104
Eli Friedman7ef3d172009-06-06 07:04:42 +0000105 SDValue OptimizeFloatStore(StoreSDNode *ST);
106
Nate Begeman68679912008-04-25 18:07:40 +0000107 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
108 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
109 /// is necessary to spill the vector being inserted into to memory, perform
110 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000111 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000112 SDValue Idx, DebugLoc dl);
113 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
114 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000115
Nate Begeman5a5ca152009-04-29 05:20:52 +0000116 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
117 /// performs the same shuffe in terms of order or result bytes, but on a type
118 /// whose vector element type is narrower than the original shuffle type.
119 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +0000120 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +0000121 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000122 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000123
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000124 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000125 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000126
Owen Andersone50ed302009-08-10 22:56:29 +0000127 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +0000128 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000129
Eli Friedman47b41f72009-05-27 02:21:29 +0000130 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Eric Christopherabbbfbd2011-04-20 01:19:45 +0000131 SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
132 unsigned NumOps, bool isSigned, DebugLoc dl);
133
Jim Grosbache03262f2010-06-18 21:43:38 +0000134 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
135 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000136 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
137 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
138 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000139 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
140 RTLIB::Libcall Call_I8,
141 RTLIB::Libcall Call_I16,
142 RTLIB::Libcall Call_I32,
143 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000144 RTLIB::Libcall Call_I128);
Evan Cheng65279cb2011-04-16 03:08:26 +0000145 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattnercad063f2005-07-16 00:19:57 +0000146
Owen Andersone50ed302009-08-10 22:56:29 +0000147 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000148 SDValue ExpandBUILD_VECTOR(SDNode *Node);
149 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000150 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
151 SmallVectorImpl<SDValue> &Results);
152 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000153 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000154 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000155 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000156 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000157 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000158 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000159
Dale Johannesen8a782a22009-02-02 22:12:50 +0000160 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
161 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000162
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000163 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenecfe33c42011-01-26 19:13:22 +0000164 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000165 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000166
Jim Grosbache03262f2010-06-18 21:43:38 +0000167 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
168
Eli Friedman8c377c72009-05-27 01:25:56 +0000169 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
170 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Stuart Hastingsfc521632011-04-19 16:16:58 +0000171
Stuart Hastings567cac02011-04-19 20:09:38 +0000172 SDValue getLastCALLSEQ() { return LastCALLSEQ.back(); }
173 void setLastCALLSEQ(const SDValue s) { LastCALLSEQ.back() = s; }
Stuart Hastingsfc521632011-04-19 16:16:58 +0000174 void pushLastCALLSEQ(SDValue s) {
Stuart Hastingsfc521632011-04-19 16:16:58 +0000175 LastCALLSEQ.push_back(s);
176 }
177 void popLastCALLSEQ() {
178 LastCALLSEQ.pop_back();
Stuart Hastingsfc521632011-04-19 16:16:58 +0000179 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000180};
181}
182
Nate Begeman5a5ca152009-04-29 05:20:52 +0000183/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
184/// performs the same shuffe in terms of order or result bytes, but on a type
185/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000186/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000187SDValue
188SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000189 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000190 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000191 unsigned NumMaskElts = VT.getVectorNumElements();
192 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000193 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000194
Nate Begeman9008ca62009-04-27 18:41:29 +0000195 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
196
197 if (NumEltsGrowth == 1)
198 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000199
Nate Begeman9008ca62009-04-27 18:41:29 +0000200 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000201 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000202 int Idx = Mask[i];
203 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000204 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000205 NewMask.push_back(-1);
206 else
207 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000208 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000209 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000210 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000211 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
212 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000213}
214
Dan Gohman975716a2011-05-16 22:19:54 +0000215SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
Dan Gohman55e59c12010-04-19 19:05:59 +0000216 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
Dan Gohmanea027022011-07-15 22:19:02 +0000217 DAG(dag) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000218}
219
Chris Lattner3e928bb2005-01-07 07:47:09 +0000220void SelectionDAGLegalize::LegalizeDAG() {
Stuart Hastingsfc521632011-04-19 16:16:58 +0000221 pushLastCALLSEQ(DAG.getEntryNode());
Mon P Wange5ab34e2009-02-04 19:38:14 +0000222
Chris Lattnerab510a72005-10-02 17:49:46 +0000223 // The legalize process is inherently a bottom-up recursive process (users
224 // legalize their uses before themselves). Given infinite stack space, we
225 // could just start legalizing on the root and traverse the whole graph. In
226 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000227 // blocks. To avoid this problem, compute an ordering of the nodes where each
228 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000229 DAG.AssignTopologicalOrder();
230 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +0000231 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000232 LegalizeOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000233
234 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000236 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
237 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000238
Chris Lattner3e928bb2005-01-07 07:47:09 +0000239 LegalizedNodes.clear();
240
241 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000242 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000243}
244
Chris Lattner6831a812006-02-13 09:18:02 +0000245
246/// FindCallEndFromCallStart - Given a chained node that is part of a call
247/// sequence, find the CALLSEQ_END node that terminates the call sequence.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000248static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) {
Stuart Hastingsfc521632011-04-19 16:16:58 +0000249 int next_depth = depth;
Stuart Hastingsa304d022010-12-09 21:25:20 +0000250 if (Node->getOpcode() == ISD::CALLSEQ_START)
Stuart Hastingsfc521632011-04-19 16:16:58 +0000251 next_depth = depth + 1;
252 if (Node->getOpcode() == ISD::CALLSEQ_END) {
253 assert(depth > 0 && "negative depth!");
254 if (depth == 1)
Stuart Hastings56500ed2010-12-21 17:16:58 +0000255 return Node;
Stuart Hastingsfc521632011-04-19 16:16:58 +0000256 else
257 next_depth = depth - 1;
Stuart Hastings56500ed2010-12-21 17:16:58 +0000258 }
Chris Lattner6831a812006-02-13 09:18:02 +0000259 if (Node->use_empty())
260 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000261
Chris Lattner6831a812006-02-13 09:18:02 +0000262 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000263 SDValue TheChain(Node, Node->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000265 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000266 TheChain = SDValue(Node, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000268 // Otherwise, hunt for it.
269 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000271 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000272 break;
273 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000274
275 // Otherwise, we walked into a node without a chain.
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 if (TheChain.getValueType() != MVT::Other)
Chris Lattner6831a812006-02-13 09:18:02 +0000277 return 0;
278 }
279 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000280
Chris Lattner6831a812006-02-13 09:18:02 +0000281 for (SDNode::use_iterator UI = Node->use_begin(),
282 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000283
Chris Lattner6831a812006-02-13 09:18:02 +0000284 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000285 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000286 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
287 if (User->getOperand(i) == TheChain)
Stuart Hastingsfc521632011-04-19 16:16:58 +0000288 if (SDNode *Result = FindCallEndFromCallStart(User, next_depth))
Chris Lattner6831a812006-02-13 09:18:02 +0000289 return Result;
290 }
291 return 0;
292}
293
Scott Michelfdc40a02009-02-17 22:15:04 +0000294/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000295/// sequence, find the CALLSEQ_START node that initiates the call sequence.
296static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
Stuart Hastingsa304d022010-12-09 21:25:20 +0000297 int nested = 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000298 assert(Node && "Didn't find callseq_start for a call??");
Stuart Hastingsa304d022010-12-09 21:25:20 +0000299 while (Node->getOpcode() != ISD::CALLSEQ_START || nested) {
300 Node = Node->getOperand(0).getNode();
301 assert(Node->getOperand(0).getValueType() == MVT::Other &&
302 "Node doesn't have a token chain argument!");
303 switch (Node->getOpcode()) {
304 default:
305 break;
306 case ISD::CALLSEQ_START:
307 if (!nested)
308 return Node;
Stuart Hastingsd6730572011-05-10 21:20:03 +0000309 Node = Node->getOperand(0).getNode();
Stuart Hastingsa304d022010-12-09 21:25:20 +0000310 nested--;
311 break;
312 case ISD::CALLSEQ_END:
313 nested++;
314 break;
315 }
316 }
Stuart Hastingsd6730572011-05-10 21:20:03 +0000317 return (Node->getOpcode() == ISD::CALLSEQ_START) ? Node : 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000318}
319
320/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000321/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000322/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000323///
324/// Keep track of the nodes we fine that actually do lead to Dest in
325/// NodesLeadingTo. This avoids retraversing them exponential number of times.
326///
327bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000328 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000329 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000330
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000331 // If we've already processed this node and it does lead to Dest, there is no
332 // need to reprocess it.
333 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000334
Chris Lattner6831a812006-02-13 09:18:02 +0000335 // If the first result of this node has been already legalized, then it cannot
336 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000337 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000338
Chris Lattner6831a812006-02-13 09:18:02 +0000339 // Okay, this node has not already been legalized. Check and legalize all
340 // operands. If none lead to Dest, then we can legalize this node.
341 bool OperandsLeadToDest = false;
342 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
343 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Jim Grosbach6e992612010-07-02 17:41:59 +0000344 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest,
345 NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000346
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000347 if (OperandsLeadToDest) {
348 NodesLeadingTo.insert(N);
349 return true;
350 }
Chris Lattner6831a812006-02-13 09:18:02 +0000351
352 // Okay, this node looks safe, legalize it and return false.
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000353 LegalizeOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000354 return false;
355}
356
Evan Cheng9f877882006-12-13 20:57:08 +0000357/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
358/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000359static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000360 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000361 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000362 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000363
364 // If a FP immediate is precise when represented as a float and if the
365 // target can do an extending load from float to double, we put it into
366 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000367 // double. This shrinks FP constants and canonicalizes them for targets where
368 // an FP extending load is the same cost as a normal load (such as on the x87
369 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000370 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000371 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000372 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000374 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000376 }
377
Owen Andersone50ed302009-08-10 22:56:29 +0000378 EVT OrigVT = VT;
379 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 while (SVT != MVT::f32) {
381 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000382 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000383 // Only do this if the target has a native EXTLOAD instruction from
384 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000385 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000386 TLI.ShouldShrinkFPConstant(OrigVT)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000387 const Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000388 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000389 VT = SVT;
390 Extend = true;
391 }
Evan Cheng00495212006-12-12 21:32:44 +0000392 }
393
Dan Gohman475871a2008-07-27 21:46:04 +0000394 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000395 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000396 if (Extend)
Stuart Hastingsa9011292011-02-16 16:23:55 +0000397 return DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
Evan Chengbcc80172010-07-07 22:15:37 +0000398 DAG.getEntryNode(),
Chris Lattner85ca1062010-09-21 07:32:19 +0000399 CPIdx, MachinePointerInfo::getConstantPool(),
400 VT, false, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000401 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +0000402 MachinePointerInfo::getConstantPool(), false, false,
David Greene1e559442010-02-15 17:00:31 +0000403 Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000404}
405
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000406/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
407static
Dan Gohman475871a2008-07-27 21:46:04 +0000408SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000409 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000410 SDValue Chain = ST->getChain();
411 SDValue Ptr = ST->getBasePtr();
412 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000413 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000414 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000415 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000416 if (ST->getMemoryVT().isFloatingPoint() ||
417 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000418 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000419 if (TLI.isTypeLegal(intVT)) {
420 // Expand to a bitconvert of the value to the integer type of the
421 // same size, then a (misaligned) int store.
422 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000423 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000424 return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
425 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000426 }
Dan Gohman1b328962011-05-17 22:22:52 +0000427 // Do a (aligned) store to a stack slot, then copy from the stack slot
428 // to the final destination using (unaligned) integer loads and stores.
429 EVT StoredVT = ST->getMemoryVT();
430 EVT RegVT =
431 TLI.getRegisterType(*DAG.getContext(),
432 EVT::getIntegerVT(*DAG.getContext(),
433 StoredVT.getSizeInBits()));
434 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
435 unsigned RegBytes = RegVT.getSizeInBits() / 8;
436 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
437
438 // Make sure the stack slot is also aligned for the register type.
439 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
440
441 // Perform the original store, only redirected to the stack slot.
442 SDValue Store = DAG.getTruncStore(Chain, dl,
443 Val, StackPtr, MachinePointerInfo(),
444 StoredVT, false, false, 0);
445 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
446 SmallVector<SDValue, 8> Stores;
447 unsigned Offset = 0;
448
449 // Do all but one copies using the full register width.
450 for (unsigned i = 1; i < NumRegs; i++) {
451 // Load one integer register's worth from the stack slot.
452 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
453 MachinePointerInfo(),
454 false, false, 0);
455 // Store it to the final location. Remember the store.
456 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
457 ST->getPointerInfo().getWithOffset(Offset),
458 ST->isVolatile(), ST->isNonTemporal(),
459 MinAlign(ST->getAlignment(), Offset)));
460 // Increment the pointers.
461 Offset += RegBytes;
462 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
463 Increment);
464 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
465 }
466
467 // The last store may be partial. Do a truncating store. On big-endian
468 // machines this requires an extending load from the stack slot to ensure
469 // that the bits are in the right place.
470 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
471 8 * (StoredBytes - Offset));
472
473 // Load from the stack slot.
474 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
475 MachinePointerInfo(),
476 MemVT, false, false, 0);
477
478 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
479 ST->getPointerInfo()
480 .getWithOffset(Offset),
481 MemVT, ST->isVolatile(),
482 ST->isNonTemporal(),
483 MinAlign(ST->getAlignment(), Offset)));
484 // The order of the stores doesn't matter - say it with a TokenFactor.
485 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
486 Stores.size());
Dale Johannesen907f28c2007-09-08 19:29:23 +0000487 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000488 assert(ST->getMemoryVT().isInteger() &&
489 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000490 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000491 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000492 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000493 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000494 int IncrementSize = NumBits / 8;
495
496 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000497 SDValue ShiftAmount = DAG.getConstant(NumBits,
498 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000499 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000500 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000501
502 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000503 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000504 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000505 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000506 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000507 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000508 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000509 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000510 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000511 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000512 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
513 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000514
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000516}
517
518/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
519static
Dan Gohman475871a2008-07-27 21:46:04 +0000520SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000521 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000522 SDValue Chain = LD->getChain();
523 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000524 EVT VT = LD->getValueType(0);
525 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000526 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000527 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000528 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000529 if (TLI.isTypeLegal(intVT)) {
530 // Expand to a (misaligned) integer load of the same size,
531 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000532 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
533 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000534 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000535 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000536 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000537 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000538
Duncan Sands05e11fa2008-12-12 21:47:02 +0000539 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000540 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000541 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000542
Chris Lattnerecf42c42010-09-21 16:36:31 +0000543 // Copy the value to a (aligned) stack slot using (unaligned) integer
544 // loads and stores, then do a (aligned) load from the stack slot.
545 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
546 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
547 unsigned RegBytes = RegVT.getSizeInBits() / 8;
548 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
549
550 // Make sure the stack slot is also aligned for the register type.
551 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
552
553 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
554 SmallVector<SDValue, 8> Stores;
555 SDValue StackPtr = StackBase;
556 unsigned Offset = 0;
557
558 // Do all but one copies using the full register width.
559 for (unsigned i = 1; i < NumRegs; i++) {
560 // Load one integer register's worth from the original location.
561 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
562 LD->getPointerInfo().getWithOffset(Offset),
563 LD->isVolatile(), LD->isNonTemporal(),
564 MinAlign(LD->getAlignment(), Offset));
565 // Follow the load with a store to the stack slot. Remember the store.
566 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000567 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000568 // Increment the pointers.
569 Offset += RegBytes;
570 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
571 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
572 Increment);
573 }
574
575 // The last copy may be partial. Do an extending load.
576 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
577 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000578 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000579 LD->getPointerInfo().getWithOffset(Offset),
580 MemVT, LD->isVolatile(),
581 LD->isNonTemporal(),
582 MinAlign(LD->getAlignment(), Offset));
583 // Follow the load with a store to the stack slot. Remember the store.
584 // On big-endian machines this requires a truncating store to ensure
585 // that the bits end up in the right place.
586 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
587 MachinePointerInfo(), MemVT,
588 false, false, 0));
589
590 // The order of the stores doesn't matter - say it with a TokenFactor.
591 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
592 Stores.size());
593
594 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000595 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000596 MachinePointerInfo(), LoadedVT, false, false, 0);
597
598 // Callers expect a MERGE_VALUES node.
599 SDValue Ops[] = { Load, TF };
600 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000601 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000602 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000603 "Unaligned load of unsupported type.");
604
Dale Johannesen8155d642008-02-27 22:36:00 +0000605 // Compute the new VT that is half the size of the old one. This is an
606 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000607 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000608 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000609 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000610 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000611
Chris Lattnere400af82007-11-19 21:38:03 +0000612 unsigned Alignment = LD->getAlignment();
613 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000614 ISD::LoadExtType HiExtType = LD->getExtensionType();
615
616 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
617 if (HiExtType == ISD::NON_EXTLOAD)
618 HiExtType = ISD::ZEXTLOAD;
619
620 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000621 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000622 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000623 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000624 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000625 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000626 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000627 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000628 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000629 LD->getPointerInfo().getWithOffset(IncrementSize),
630 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000631 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000632 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000633 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000634 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000635 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000636 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000637 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000638 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000639 LD->getPointerInfo().getWithOffset(IncrementSize),
640 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000641 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000642 }
643
644 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000645 SDValue ShiftAmount = DAG.getConstant(NumBits,
646 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000647 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
648 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000649
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000651 Hi.getValue(1));
652
Dan Gohman475871a2008-07-27 21:46:04 +0000653 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000654 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000655}
Evan Cheng912095b2007-01-04 21:56:39 +0000656
Nate Begeman68679912008-04-25 18:07:40 +0000657/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
658/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
659/// is necessary to spill the vector being inserted into to memory, perform
660/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000661SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000662PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
663 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000664 SDValue Tmp1 = Vec;
665 SDValue Tmp2 = Val;
666 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000667
Nate Begeman68679912008-04-25 18:07:40 +0000668 // If the target doesn't support this, we have to spill the input vector
669 // to a temporary stack slot, update the element, then reload it. This is
670 // badness. We could also load the value into a vector register (either
671 // with a "move to register" or "extload into register" instruction, then
672 // permute it into place, if the idx is a constant and if the idx is
673 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000674 EVT VT = Tmp1.getValueType();
675 EVT EltVT = VT.getVectorElementType();
676 EVT IdxVT = Tmp3.getValueType();
677 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000678 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000679
Evan Chengff89dcb2009-10-18 18:16:27 +0000680 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
681
Nate Begeman68679912008-04-25 18:07:40 +0000682 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000683 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000684 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000685 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000686
687 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000688 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000689 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000690 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000691 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000692 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
693 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000694 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000695 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000696 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000697 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000698 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000699 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000700}
701
Mon P Wange9f10152008-12-09 05:46:39 +0000702
Eli Friedman3f727d62009-05-27 02:16:40 +0000703SDValue SelectionDAGLegalize::
704ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
705 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
706 // SCALAR_TO_VECTOR requires that the type of the value being inserted
707 // match the element type of the vector being created, except for
708 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000709 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000710 if (Val.getValueType() == EltVT ||
711 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
712 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
713 Vec.getValueType(), Val);
714
715 unsigned NumElts = Vec.getValueType().getVectorNumElements();
716 // We generate a shuffle of InVec and ScVec, so the shuffle mask
717 // should be 0,1,2,3,4,5... with the appropriate element replaced with
718 // elt 0 of the RHS.
719 SmallVector<int, 8> ShufOps;
720 for (unsigned i = 0; i != NumElts; ++i)
721 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
722
723 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
724 &ShufOps[0]);
725 }
726 }
727 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
728}
729
Eli Friedman7ef3d172009-06-06 07:04:42 +0000730SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
731 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
732 // FIXME: We shouldn't do this for TargetConstantFP's.
733 // FIXME: move this to the DAG Combiner! Note that we can't regress due
734 // to phase ordering between legalized code and the dag combiner. This
735 // probably means that we need to integrate dag combiner and legalizer
736 // together.
737 // We generally can't do this one for long doubles.
738 SDValue Tmp1 = ST->getChain();
739 SDValue Tmp2 = ST->getBasePtr();
740 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000741 unsigned Alignment = ST->getAlignment();
742 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000743 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000744 DebugLoc dl = ST->getDebugLoc();
745 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 if (CFP->getValueType(0) == MVT::f32 &&
747 getTypeAction(MVT::i32) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000748 Tmp3 = DAG.getConstant(CFP->getValueAPF().
749 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000751 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
752 isVolatile, isNonTemporal, Alignment);
753 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000754
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000755 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000756 // If this target supports 64-bit registers, do a single 64-bit store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 if (getTypeAction(MVT::i64) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000758 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000760 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
761 isVolatile, isNonTemporal, Alignment);
762 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000763
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000764 if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000765 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
766 // stores. If the target supports neither 32- nor 64-bits, this
767 // xform is certainly not worth it.
768 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000769 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000771 if (TLI.isBigEndian()) std::swap(Lo, Hi);
772
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000773 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
774 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000775 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
776 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000777 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
778 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000779 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000780
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000782 }
783 }
784 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000785 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000786}
787
Dan Gohman6a109f92011-07-15 21:42:20 +0000788/// LegalizeOp - Return a legal replacement for the given operation, with
789/// all legal operands.
Dan Gohman475871a2008-07-27 21:46:04 +0000790SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000791 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
792 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000793
Gabor Greifba36cb52008-08-28 21:40:38 +0000794 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000795 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000796
Eli Friedman1fde9c52009-05-24 02:46:31 +0000797 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
798 assert(getTypeAction(Node->getValueType(i)) == Legal &&
799 "Unexpected illegal type!");
800
801 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Jim Grosbach6e992612010-07-02 17:41:59 +0000802 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000803 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
804 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000805
Chris Lattner45982da2005-05-12 16:53:42 +0000806 // Note that LegalizeOp may be reentered even from single-use nodes, which
807 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000808 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000809 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000810
Dan Gohman475871a2008-07-27 21:46:04 +0000811 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
812 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000813 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000814
Eli Friedman8c377c72009-05-27 01:25:56 +0000815 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000816 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000817 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000818 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000819 case ISD::INTRINSIC_W_CHAIN:
820 case ISD::INTRINSIC_WO_CHAIN:
821 case ISD::INTRINSIC_VOID:
822 case ISD::VAARG:
823 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000825 break;
826 case ISD::SINT_TO_FP:
827 case ISD::UINT_TO_FP:
828 case ISD::EXTRACT_VECTOR_ELT:
829 Action = TLI.getOperationAction(Node->getOpcode(),
830 Node->getOperand(0).getValueType());
831 break;
832 case ISD::FP_ROUND_INREG:
833 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000834 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000835 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
836 break;
837 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000838 case ISD::SELECT_CC:
839 case ISD::SETCC:
840 case ISD::BR_CC: {
841 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
842 Node->getOpcode() == ISD::SETCC ? 2 : 1;
843 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000844 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000845 ISD::CondCode CCCode =
846 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
847 Action = TLI.getCondCodeAction(CCCode, OpVT);
848 if (Action == TargetLowering::Legal) {
849 if (Node->getOpcode() == ISD::SELECT_CC)
850 Action = TLI.getOperationAction(Node->getOpcode(),
851 Node->getValueType(0));
852 else
853 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
854 }
855 break;
856 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000857 case ISD::LOAD:
858 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000859 // FIXME: Model these properly. LOAD and STORE are complicated, and
860 // STORE expects the unlegalized operand in some cases.
861 SimpleFinishLegalizing = false;
862 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000863 case ISD::CALLSEQ_START:
864 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000865 // FIXME: This shouldn't be necessary. These nodes have special properties
866 // dealing with the recursive nature of legalization. Removing this
867 // special case should be done as part of making LegalizeDAG non-recursive.
868 SimpleFinishLegalizing = false;
869 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000870 case ISD::EXTRACT_ELEMENT:
871 case ISD::FLT_ROUNDS_:
872 case ISD::SADDO:
873 case ISD::SSUBO:
874 case ISD::UADDO:
875 case ISD::USUBO:
876 case ISD::SMULO:
877 case ISD::UMULO:
878 case ISD::FPOWI:
879 case ISD::MERGE_VALUES:
880 case ISD::EH_RETURN:
881 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000882 case ISD::EH_SJLJ_SETJMP:
883 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000884 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000885 // These operations lie about being legal: when they claim to be legal,
886 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000887 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
888 if (Action == TargetLowering::Legal)
889 Action = TargetLowering::Expand;
890 break;
891 case ISD::TRAMPOLINE:
892 case ISD::FRAMEADDR:
893 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000894 // These operations lie about being legal: when they claim to be legal,
895 // they should actually be custom-lowered.
896 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
897 if (Action == TargetLowering::Legal)
898 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000899 break;
900 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000901 // A weird case: legalization for BUILD_VECTOR never legalizes the
902 // operands!
903 // FIXME: This really sucks... changing it isn't semantically incorrect,
904 // but it massively pessimizes the code for floating-point BUILD_VECTORs
905 // because ConstantFP operands get legalized into constant pool loads
906 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
907 // though, because BUILD_VECTORS usually get lowered into other nodes
908 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000909 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000910 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000911 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000912 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000913 Action = TargetLowering::Legal;
914 } else {
915 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000916 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000917 break;
918 }
919
920 if (SimpleFinishLegalizing) {
921 SmallVector<SDValue, 8> Ops, ResultVals;
922 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
923 Ops.push_back(LegalizeOp(Node->getOperand(i)));
924 switch (Node->getOpcode()) {
925 default: break;
926 case ISD::BR:
927 case ISD::BRIND:
928 case ISD::BR_JT:
929 case ISD::BR_CC:
930 case ISD::BRCOND:
Stuart Hastings567cac02011-04-19 20:09:38 +0000931 assert(LastCALLSEQ.size() == 1 && "branch inside CALLSEQ_BEGIN/END?");
Stuart Hastingsfc521632011-04-19 16:16:58 +0000932 // Branches tweak the chain to include LastCALLSEQ
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Stuart Hastingsfc521632011-04-19 16:16:58 +0000934 getLastCALLSEQ());
Eli Friedman8c377c72009-05-27 01:25:56 +0000935 Ops[0] = LegalizeOp(Ops[0]);
Stuart Hastingsfc521632011-04-19 16:16:58 +0000936 setLastCALLSEQ(DAG.getEntryNode());
Eli Friedman8c377c72009-05-27 01:25:56 +0000937 break;
938 case ISD::SHL:
939 case ISD::SRL:
940 case ISD::SRA:
941 case ISD::ROTL:
942 case ISD::ROTR:
943 // Legalizing shifts/rotates requires adjusting the shift amount
944 // to the appropriate width.
945 if (!Ops[1].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000946 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
947 Ops[1]));
Eli Friedman8c377c72009-05-27 01:25:56 +0000948 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000949 case ISD::SRL_PARTS:
950 case ISD::SRA_PARTS:
951 case ISD::SHL_PARTS:
952 // Legalizing shifts/rotates requires adjusting the shift amount
953 // to the appropriate width.
954 if (!Ops[2].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000955 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
956 Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000957 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000958 }
959
Dan Gohman027657d2010-06-18 15:30:29 +0000960 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
961 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000962 switch (Action) {
963 case TargetLowering::Legal:
964 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
965 ResultVals.push_back(Result.getValue(i));
966 break;
967 case TargetLowering::Custom:
968 // FIXME: The handling for custom lowering with multiple results is
969 // a complete mess.
970 Tmp1 = TLI.LowerOperation(Result, DAG);
971 if (Tmp1.getNode()) {
972 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
973 if (e == 1)
974 ResultVals.push_back(Tmp1);
975 else
976 ResultVals.push_back(Tmp1.getValue(i));
977 }
978 break;
979 }
980
981 // FALL THROUGH
982 case TargetLowering::Expand:
983 ExpandNode(Result.getNode(), ResultVals);
984 break;
985 case TargetLowering::Promote:
986 PromoteNode(Result.getNode(), ResultVals);
987 break;
988 }
989 if (!ResultVals.empty()) {
990 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
991 if (ResultVals[i] != SDValue(Node, i))
992 ResultVals[i] = LegalizeOp(ResultVals[i]);
993 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
994 }
995 return ResultVals[Op.getResNo()];
996 }
997 }
998
999 switch (Node->getOpcode()) {
1000 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001001#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001002 dbgs() << "NODE: ";
1003 Node->dump( &DAG);
1004 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001005#endif
Chris Lattner35a38932010-04-07 23:47:51 +00001006 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001007
Chris Lattnerb2827b02006-03-19 00:52:58 +00001008 case ISD::BUILD_VECTOR:
1009 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +00001010 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +00001011 case TargetLowering::Custom:
1012 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001013 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001014 Result = Tmp3;
1015 break;
1016 }
1017 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001018 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001019 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001020 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001021 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001022 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001023 case ISD::CALLSEQ_START: {
1024 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Stuart Hastingsfc521632011-04-19 16:16:58 +00001025 assert(CallEnd && "didn't find CALLSEQ_END!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001026
Chris Lattner6831a812006-02-13 09:18:02 +00001027 // Recursively Legalize all of the inputs of the call end that do not lead
1028 // to this call start. This ensures that any libcalls that need be inserted
1029 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001030 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001031 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001032 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001033 NodesLeadingTo);
1034 }
Chris Lattner6831a812006-02-13 09:18:02 +00001035
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001036 // Now that we have legalized all of the inputs (which may have inserted
1037 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1039
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001040 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001041 // call ended.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001042 if (getLastCALLSEQ().getOpcode() != ISD::EntryToken) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Stuart Hastingsfc521632011-04-19 16:16:58 +00001044 Tmp1, getLastCALLSEQ());
Chris Lattnerb248e162006-05-17 17:55:45 +00001045 Tmp1 = LegalizeOp(Tmp1);
1046 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001047
Chris Lattner6831a812006-02-13 09:18:02 +00001048 // Do not try to legalize the target-specific arguments (#1+).
1049 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001050 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001051 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001052 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1053 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001054 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001055
Chris Lattner6831a812006-02-13 09:18:02 +00001056 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001057 AddLegalizedOperand(Op.getValue(0), Result);
1058 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1059 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Chris Lattner6831a812006-02-13 09:18:02 +00001061 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001062 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001063 // process, no libcalls can/will be inserted, guaranteeing that no calls
1064 // can overlap.
Chris Lattner6831a812006-02-13 09:18:02 +00001065 // Note that we are selecting this call!
Stuart Hastingsfc521632011-04-19 16:16:58 +00001066 setLastCALLSEQ(SDValue(CallEnd, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001067
Chris Lattner6831a812006-02-13 09:18:02 +00001068 // Legalize the call, starting from the CALLSEQ_END.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001069 LegalizeOp(getLastCALLSEQ());
Chris Lattner6831a812006-02-13 09:18:02 +00001070 return Result;
1071 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001072 case ISD::CALLSEQ_END:
Stuart Hastingsfc521632011-04-19 16:16:58 +00001073 {
1074 SDNode *myCALLSEQ_BEGIN = FindCallStartFromCallEnd(Node);
1075
Dan Gohmanf316eb72011-05-16 22:09:53 +00001076 // If the CALLSEQ_START node hasn't been legalized first, legalize it.
1077 // This will cause this node to be legalized as well as handling libcalls
1078 // right.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001079 if (getLastCALLSEQ().getNode() != Node) {
1080 LegalizeOp(SDValue(myCALLSEQ_BEGIN, 0));
1081 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
1082 assert(I != LegalizedNodes.end() &&
1083 "Legalizing the call start should have legalized this node!");
1084 return I->second;
1085 }
1086
1087 pushLastCALLSEQ(SDValue(myCALLSEQ_BEGIN, 0));
Chris Lattner6831a812006-02-13 09:18:02 +00001088 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001089
1090 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001091 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001092 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001093 // Do not try to legalize the target-specific arguments (#1+), except for
1094 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001095 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001096 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001097 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001098 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001099 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1100 &Ops[0], Ops.size()),
1101 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001102 }
1103 } else {
1104 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1105 if (Tmp1 != Node->getOperand(0) ||
1106 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001107 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001108 Ops[0] = Tmp1;
1109 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001110 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1111 &Ops[0], Ops.size()),
1112 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001113 }
Chris Lattner6a542892006-01-24 05:48:21 +00001114 }
Chris Lattner6831a812006-02-13 09:18:02 +00001115 // This finishes up call legalization.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001116 popLastCALLSEQ();
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001117
Chris Lattner4b653a02006-02-14 00:55:02 +00001118 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001119 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001120 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001121 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001122 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001123 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001124 LoadSDNode *LD = cast<LoadSDNode>(Node);
1125 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1126 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001127
Evan Cheng466685d2006-10-09 20:57:25 +00001128 ISD::LoadExtType ExtType = LD->getExtensionType();
1129 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001130 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001131 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1132 Tmp1, Tmp2, LD->getOffset()),
1133 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001134 Tmp3 = Result.getValue(0);
1135 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001136
Evan Cheng466685d2006-10-09 20:57:25 +00001137 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001138 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001139 case TargetLowering::Legal:
1140 // If this is an unaligned load and the target doesn't support it,
1141 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001142 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001143 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1144 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001145 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001146 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001147 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001148 Tmp3 = Result.getOperand(0);
1149 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001150 Tmp3 = LegalizeOp(Tmp3);
1151 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001152 }
1153 }
1154 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001155 case TargetLowering::Custom:
1156 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001157 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001158 Tmp3 = LegalizeOp(Tmp1);
1159 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001160 }
Evan Cheng466685d2006-10-09 20:57:25 +00001161 break;
1162 case TargetLowering::Promote: {
1163 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001164 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001165 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001166 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001167
Chris Lattnerecf42c42010-09-21 16:36:31 +00001168 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001169 LD->isVolatile(), LD->isNonTemporal(),
1170 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001171 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001172 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001173 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001174 }
Evan Cheng466685d2006-10-09 20:57:25 +00001175 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001176 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001177 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001178 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1179 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001180 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001181 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001182
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001183 EVT SrcVT = LD->getMemoryVT();
1184 unsigned SrcWidth = SrcVT.getSizeInBits();
1185 unsigned Alignment = LD->getAlignment();
1186 bool isVolatile = LD->isVolatile();
1187 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001188
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001189 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1190 // Some targets pretend to have an i1 loading operation, and actually
1191 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1192 // bits are guaranteed to be zero; it helps the optimizers understand
1193 // that these bits are zero. It is also useful for EXTLOAD, since it
1194 // tells the optimizers that those bits are undefined. It would be
1195 // nice to have an effective generic way of getting these benefits...
1196 // Until such a way is found, don't insist on promoting i1 here.
1197 (SrcVT != MVT::i1 ||
1198 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1199 // Promote to a byte-sized load if not loading an integral number of
1200 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1201 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1202 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1203 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001204
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001205 // The extra bits are guaranteed to be zero, since we stored them that
1206 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001207
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001208 ISD::LoadExtType NewExtType =
1209 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001210
Stuart Hastingsa9011292011-02-16 16:23:55 +00001211 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001212 Tmp1, Tmp2, LD->getPointerInfo(),
1213 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001214
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001215 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001216
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001217 if (ExtType == ISD::SEXTLOAD)
1218 // Having the top bits zero doesn't help when sign extending.
1219 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1220 Result.getValueType(),
1221 Result, DAG.getValueType(SrcVT));
1222 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1223 // All the top bits are guaranteed to be zero - inform the optimizers.
1224 Result = DAG.getNode(ISD::AssertZext, dl,
1225 Result.getValueType(), Result,
1226 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001227
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001228 Tmp1 = LegalizeOp(Result);
1229 Tmp2 = LegalizeOp(Ch);
1230 } else if (SrcWidth & (SrcWidth - 1)) {
1231 // If not loading a power-of-2 number of bits, expand as two loads.
1232 assert(!SrcVT.isVector() && "Unsupported extload!");
1233 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1234 assert(RoundWidth < SrcWidth);
1235 unsigned ExtraWidth = SrcWidth - RoundWidth;
1236 assert(ExtraWidth < RoundWidth);
1237 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1238 "Load size not an integral number of bytes!");
1239 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1240 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1241 SDValue Lo, Hi, Ch;
1242 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001243
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001244 if (TLI.isLittleEndian()) {
1245 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1246 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001247 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001248 Tmp1, Tmp2,
1249 LD->getPointerInfo(), RoundVT, isVolatile,
1250 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001251
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001252 // Load the remaining ExtraWidth bits.
1253 IncrementSize = RoundWidth / 8;
1254 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1255 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001256 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001257 LD->getPointerInfo().getWithOffset(IncrementSize),
1258 ExtraVT, isVolatile, isNonTemporal,
1259 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001260
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001261 // Build a factor node to remember that this load is independent of
1262 // the other one.
1263 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1264 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001265
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001266 // Move the top bits to the right place.
1267 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001268 DAG.getConstant(RoundWidth,
1269 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001270
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001271 // Join the hi and lo parts.
1272 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001273 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001274 // Big endian - avoid unaligned loads.
1275 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1276 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001277 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001278 LD->getPointerInfo(), RoundVT, isVolatile,
1279 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001280
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001281 // Load the remaining ExtraWidth bits.
1282 IncrementSize = RoundWidth / 8;
1283 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1284 DAG.getIntPtrConstant(IncrementSize));
1285 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001286 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001287 LD->getPointerInfo().getWithOffset(IncrementSize),
1288 ExtraVT, isVolatile, isNonTemporal,
1289 MinAlign(Alignment, IncrementSize));
1290
1291 // Build a factor node to remember that this load is independent of
1292 // the other one.
1293 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1294 Hi.getValue(1));
1295
1296 // Move the top bits to the right place.
1297 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001298 DAG.getConstant(ExtraWidth,
1299 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001300
1301 // Join the hi and lo parts.
1302 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001303 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001304
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001305 Tmp1 = LegalizeOp(Result);
1306 Tmp2 = LegalizeOp(Ch);
1307 } else {
1308 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1309 default: assert(0 && "This action is not supported yet!");
1310 case TargetLowering::Custom:
1311 isCustom = true;
1312 // FALLTHROUGH
1313 case TargetLowering::Legal:
1314 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1315 Tmp1, Tmp2, LD->getOffset()),
1316 Result.getResNo());
1317 Tmp1 = Result.getValue(0);
1318 Tmp2 = Result.getValue(1);
1319
1320 if (isCustom) {
1321 Tmp3 = TLI.LowerOperation(Result, DAG);
1322 if (Tmp3.getNode()) {
1323 Tmp1 = LegalizeOp(Tmp3);
1324 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1325 }
1326 } else {
1327 // If this is an unaligned load and the target doesn't support it,
1328 // expand it.
1329 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1330 const Type *Ty =
1331 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1332 unsigned ABIAlignment =
1333 TLI.getTargetData()->getABITypeAlignment(Ty);
1334 if (LD->getAlignment() < ABIAlignment){
1335 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1336 DAG, TLI);
1337 Tmp1 = Result.getOperand(0);
1338 Tmp2 = Result.getOperand(1);
1339 Tmp1 = LegalizeOp(Tmp1);
1340 Tmp2 = LegalizeOp(Tmp2);
1341 }
1342 }
1343 }
1344 break;
1345 case TargetLowering::Expand:
1346 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && isTypeLegal(SrcVT)) {
1347 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1348 LD->getPointerInfo(),
1349 LD->isVolatile(), LD->isNonTemporal(),
1350 LD->getAlignment());
1351 unsigned ExtendOp;
1352 switch (ExtType) {
1353 case ISD::EXTLOAD:
1354 ExtendOp = (SrcVT.isFloatingPoint() ?
1355 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1356 break;
1357 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1358 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1359 default: llvm_unreachable("Unexpected extend load type!");
1360 }
1361 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1362 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1363 Tmp2 = LegalizeOp(Load.getValue(1));
1364 break;
1365 }
Nadav Rotemc2492c22011-06-14 08:11:52 +00001366
1367 // If this is a promoted vector load, and the vector element types are
1368 // legal, then scalarize it.
1369 if (ExtType == ISD::EXTLOAD && SrcVT.isVector() &&
1370 isTypeLegal(Node->getValueType(0).getScalarType())) {
1371 SmallVector<SDValue, 8> LoadVals;
1372 SmallVector<SDValue, 8> LoadChains;
1373 unsigned NumElem = SrcVT.getVectorNumElements();
1374 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
1375
1376 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1377 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1378 DAG.getIntPtrConstant(Stride));
1379 SDValue ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl,
1380 Node->getValueType(0).getScalarType(),
1381 Tmp1, Tmp2, LD->getPointerInfo().getWithOffset(Idx * Stride),
1382 SrcVT.getScalarType(),
1383 LD->isVolatile(), LD->isNonTemporal(),
1384 LD->getAlignment());
1385
1386 LoadVals.push_back(ScalarLoad.getValue(0));
1387 LoadChains.push_back(ScalarLoad.getValue(1));
1388 }
1389 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1390 &LoadChains[0], LoadChains.size());
1391 SDValue ValRes = DAG.getNode(ISD::BUILD_VECTOR, dl,
1392 Node->getValueType(0), &LoadVals[0], LoadVals.size());
1393
1394 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1395 Tmp2 = LegalizeOp(Result.getValue(0)); // Relegalize new nodes.
1396 break;
1397 }
1398
1399 // If this is a promoted vector load, and the vector element types are
1400 // illegal, create the promoted vector from bitcasted segments.
1401 if (ExtType == ISD::EXTLOAD && SrcVT.isVector()) {
1402 EVT MemElemTy = Node->getValueType(0).getScalarType();
1403 EVT SrcSclrTy = SrcVT.getScalarType();
1404 unsigned SizeRatio =
1405 (MemElemTy.getSizeInBits() / SrcSclrTy.getSizeInBits());
1406
1407 SmallVector<SDValue, 8> LoadVals;
1408 SmallVector<SDValue, 8> LoadChains;
1409 unsigned NumElem = SrcVT.getVectorNumElements();
1410 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
1411
1412 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1413 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1414 DAG.getIntPtrConstant(Stride));
1415 SDValue ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl,
1416 SrcVT.getScalarType(),
1417 Tmp1, Tmp2, LD->getPointerInfo().getWithOffset(Idx * Stride),
1418 SrcVT.getScalarType(),
1419 LD->isVolatile(), LD->isNonTemporal(),
1420 LD->getAlignment());
1421 if (TLI.isBigEndian()) {
1422 // MSB (which is garbage, comes first)
1423 LoadVals.push_back(ScalarLoad.getValue(0));
1424 for (unsigned i = 0; i<SizeRatio-1; ++i)
1425 LoadVals.push_back(DAG.getUNDEF(SrcVT.getScalarType()));
1426 } else {
1427 // LSB (which is data, comes first)
1428 for (unsigned i = 0; i<SizeRatio-1; ++i)
1429 LoadVals.push_back(DAG.getUNDEF(SrcVT.getScalarType()));
1430 LoadVals.push_back(ScalarLoad.getValue(0));
1431 }
1432 LoadChains.push_back(ScalarLoad.getValue(1));
1433 }
1434
1435 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1436 &LoadChains[0], LoadChains.size());
1437 EVT TempWideVector = EVT::getVectorVT(*DAG.getContext(),
1438 SrcVT.getScalarType(), NumElem*SizeRatio);
1439 SDValue ValRes = DAG.getNode(ISD::BUILD_VECTOR, dl,
1440 TempWideVector, &LoadVals[0], LoadVals.size());
1441
1442 // Cast to the correct type
1443 ValRes = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), ValRes);
1444
1445 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1446 Tmp2 = LegalizeOp(Result.getValue(0)); // Relegalize new nodes.
1447 break;
1448
1449 }
1450
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001451 // FIXME: This does not work for vectors on most targets. Sign- and
1452 // zero-extend operations are currently folded into extending loads,
1453 // whether they are legal or not, and then we end up here without any
1454 // support for legalizing them.
1455 assert(ExtType != ISD::EXTLOAD &&
1456 "EXTLOAD should always be supported!");
1457 // Turn the unsupported load into an EXTLOAD followed by an explicit
1458 // zero/sign extend inreg.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001459 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001460 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1461 LD->isVolatile(), LD->isNonTemporal(),
1462 LD->getAlignment());
1463 SDValue ValRes;
1464 if (ExtType == ISD::SEXTLOAD)
1465 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1466 Result.getValueType(),
1467 Result, DAG.getValueType(SrcVT));
1468 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001469 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001470 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1471 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1472 break;
1473 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001474 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001475
1476 // Since loads produce two values, make sure to remember that we legalized
1477 // both of them.
1478 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1479 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1480 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001481 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001482 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001483 StoreSDNode *ST = cast<StoreSDNode>(Node);
1484 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1485 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001486 unsigned Alignment = ST->getAlignment();
1487 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001488 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001489
Evan Cheng8b2794a2006-10-13 21:14:26 +00001490 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001491 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1492 Result = SDValue(OptStore, 0);
1493 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001494 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001495
Eli Friedman957bffa2009-05-24 08:42:01 +00001496 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001497 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001498 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1499 Tmp1, Tmp3, Tmp2,
1500 ST->getOffset()),
1501 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001502
Owen Andersone50ed302009-08-10 22:56:29 +00001503 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001504 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001505 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001506 case TargetLowering::Legal:
1507 // If this is an unaligned store and the target doesn't support it,
1508 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001509 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001510 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001511 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001512 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001513 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1514 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001515 }
1516 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001517 case TargetLowering::Custom:
1518 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001519 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001520 break;
1521 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001522 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001523 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001524 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001525 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001526 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001527 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001528 break;
1529 }
1530 break;
1531 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001532 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001533 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001534
Owen Andersone50ed302009-08-10 22:56:29 +00001535 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001536 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001537
Duncan Sands83ec4b62008-06-06 12:08:01 +00001538 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001539 // Promote to a byte-sized store with upper bits zero if not
1540 // storing an integral number of bytes. For example, promote
1541 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001542 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1543 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001544 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001545 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1546 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001547 } else if (StWidth & (StWidth - 1)) {
1548 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001549 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001550 unsigned RoundWidth = 1 << Log2_32(StWidth);
1551 assert(RoundWidth < StWidth);
1552 unsigned ExtraWidth = StWidth - RoundWidth;
1553 assert(ExtraWidth < RoundWidth);
1554 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1555 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001556 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1557 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001558 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001559 unsigned IncrementSize;
1560
1561 if (TLI.isLittleEndian()) {
1562 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1563 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001564 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1565 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001566 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001567
1568 // Store the remaining ExtraWidth bits.
1569 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001570 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001571 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001572 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001573 DAG.getConstant(RoundWidth,
1574 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001575 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1576 ST->getPointerInfo().getWithOffset(IncrementSize),
1577 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001578 MinAlign(Alignment, IncrementSize));
1579 } else {
1580 // Big endian - avoid unaligned stores.
1581 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1582 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001583 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001584 DAG.getConstant(ExtraWidth,
1585 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001586 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1587 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001588
1589 // Store the remaining ExtraWidth bits.
1590 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001591 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001592 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001593 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1594 ST->getPointerInfo().getWithOffset(IncrementSize),
1595 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001596 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001597 }
Duncan Sands7e857202008-01-22 07:17:34 +00001598
1599 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001600 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001601 } else {
1602 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1603 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001604 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1605 Tmp1, Tmp3, Tmp2,
1606 ST->getOffset()),
1607 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001608
1609 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001610 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001611 case TargetLowering::Legal:
1612 // If this is an unaligned store and the target doesn't support it,
1613 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001614 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001615 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001616 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001617 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001618 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1619 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001620 }
1621 break;
1622 case TargetLowering::Custom:
1623 Result = TLI.LowerOperation(Result, DAG);
1624 break;
1625 case Expand:
Nadav Rotemc2492c22011-06-14 08:11:52 +00001626
1627 EVT WideScalarVT = Tmp3.getValueType().getScalarType();
1628 EVT NarrowScalarVT = StVT.getScalarType();
1629
1630 // The Store type is illegal, must scalarize the vector store.
1631 SmallVector<SDValue, 8> Stores;
1632 bool ScalarLegal = isTypeLegal(WideScalarVT);
1633 if (!isTypeLegal(StVT) && StVT.isVector() && ScalarLegal) {
1634 unsigned NumElem = StVT.getVectorNumElements();
1635
1636 unsigned ScalarSize = StVT.getScalarType().getSizeInBits();
1637 // Round odd types to the next pow of two.
1638 if (!isPowerOf2_32(ScalarSize))
1639 ScalarSize = NextPowerOf2(ScalarSize);
1640 // Types smaller than 8 bits are promoted to 8 bits.
1641 ScalarSize = std::max<unsigned>(ScalarSize, 8);
1642 // Store stride
1643 unsigned Stride = ScalarSize/8;
1644 assert(isPowerOf2_32(Stride) && "Stride must be a power of two");
1645
1646 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1647 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1648 WideScalarVT, Tmp3, DAG.getIntPtrConstant(Idx));
1649
1650
1651 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), ScalarSize);
1652
1653 Ex = DAG.getNode(ISD::TRUNCATE, dl, NVT, Ex);
1654 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1655 DAG.getIntPtrConstant(Stride));
1656 SDValue Store = DAG.getStore(Tmp1, dl, Ex, Tmp2,
1657 ST->getPointerInfo().getWithOffset(Idx*Stride),
1658 isVolatile, isNonTemporal, Alignment);
1659 Stores.push_back(Store);
1660 }
1661 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1662 &Stores[0], Stores.size());
1663 break;
1664 }
1665
1666 // The Store type is illegal, must scalarize the vector store.
1667 // However, the scalar type is illegal. Must bitcast the result
1668 // and store it in smaller parts.
1669 if (!isTypeLegal(StVT) && StVT.isVector()) {
1670 unsigned WideNumElem = StVT.getVectorNumElements();
1671 unsigned Stride = NarrowScalarVT.getSizeInBits()/8;
1672
1673 unsigned SizeRatio =
1674 (WideScalarVT.getSizeInBits() / NarrowScalarVT.getSizeInBits());
1675
1676 EVT CastValueVT = EVT::getVectorVT(*DAG.getContext(), NarrowScalarVT,
1677 SizeRatio*WideNumElem);
1678
1679 // Cast the wide elem vector to wider vec with smaller elem type.
1680 // Example <2 x i64> -> <4 x i32>
1681 Tmp3 = DAG.getNode(ISD::BITCAST, dl, CastValueVT, Tmp3);
1682
1683 for (unsigned Idx=0; Idx<WideNumElem*SizeRatio; Idx++) {
1684 // Extract elment i
1685 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1686 NarrowScalarVT, Tmp3, DAG.getIntPtrConstant(Idx));
1687 // bump pointer.
1688 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1689 DAG.getIntPtrConstant(Stride));
1690
1691 // Store if, this element is:
1692 // - First element on big endian, or
1693 // - Last element on little endian
1694 if (( TLI.isBigEndian() && (Idx%SizeRatio == 0)) ||
1695 ((!TLI.isBigEndian() && (Idx%SizeRatio == SizeRatio-1)))) {
1696 SDValue Store = DAG.getStore(Tmp1, dl, Ex, Tmp2,
1697 ST->getPointerInfo().getWithOffset(Idx*Stride),
1698 isVolatile, isNonTemporal, Alignment);
1699 Stores.push_back(Store);
1700 }
1701 }
1702 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1703 &Stores[0], Stores.size());
1704 break;
1705 }
1706
1707
Duncan Sands7e857202008-01-22 07:17:34 +00001708 // TRUNCSTORE:i16 i32 -> STORE i16
1709 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001710 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001711 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1712 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001713 break;
1714 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001715 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001716 }
1717 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001718 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001719 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001720 assert(Result.getValueType() == Op.getValueType() &&
1721 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001722
Chris Lattner456a93a2006-01-28 07:39:30 +00001723 // Make sure that the generated code is itself legal.
1724 if (Result != Op)
1725 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001726
Chris Lattner45982da2005-05-12 16:53:42 +00001727 // Note that LegalizeOp may be reentered even from single-use nodes, which
1728 // means that we always must cache transformed nodes.
1729 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001730 return Result;
1731}
1732
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001733SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1734 SDValue Vec = Op.getOperand(0);
1735 SDValue Idx = Op.getOperand(1);
1736 DebugLoc dl = Op.getDebugLoc();
1737 // Store the value to a temporary stack slot, then LOAD the returned part.
1738 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001739 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1740 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001741
1742 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001743 unsigned EltSize =
1744 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001745 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1746 DAG.getConstant(EltSize, Idx.getValueType()));
1747
1748 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1749 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1750 else
1751 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1752
1753 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1754
Eli Friedmanc680ac92009-07-09 22:01:03 +00001755 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001756 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001757 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001758 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001759 MachinePointerInfo(),
1760 Vec.getValueType().getVectorElementType(),
1761 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001762}
1763
David Greenecfe33c42011-01-26 19:13:22 +00001764SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1765 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1766
1767 SDValue Vec = Op.getOperand(0);
1768 SDValue Part = Op.getOperand(1);
1769 SDValue Idx = Op.getOperand(2);
1770 DebugLoc dl = Op.getDebugLoc();
1771
1772 // Store the value to a temporary stack slot, then LOAD the returned part.
1773
1774 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1775 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1776 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1777
1778 // First store the whole vector.
1779 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1780 false, false, 0);
1781
1782 // Then store the inserted part.
1783
1784 // Add the offset to the index.
1785 unsigned EltSize =
1786 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1787
1788 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1789 DAG.getConstant(EltSize, Idx.getValueType()));
1790
1791 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1792 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1793 else
1794 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1795
1796 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1797 StackPtr);
1798
1799 // Store the subvector.
1800 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1801 MachinePointerInfo(), false, false, 0);
1802
1803 // Finally, load the updated vector.
1804 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1805 false, false, 0);
1806}
1807
Eli Friedman7ef3d172009-06-06 07:04:42 +00001808SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1809 // We can't handle this case efficiently. Allocate a sufficiently
1810 // aligned object on the stack, store each element into it, then load
1811 // the result as a vector.
1812 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001813 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001814 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001815 DebugLoc dl = Node->getDebugLoc();
1816 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001817 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001818 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001819
1820 // Emit a store of each element to the stack slot.
1821 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001822 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001823 // Store (in the right endianness) the elements to memory.
1824 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1825 // Ignore undef elements.
1826 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1827
1828 unsigned Offset = TypeByteSize*i;
1829
1830 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1831 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1832
Dan Gohman9949dd62010-02-25 20:30:49 +00001833 // If the destination vector element type is narrower than the source
1834 // element type, only store the bits necessary.
1835 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001836 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001837 Node->getOperand(i), Idx,
1838 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001839 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001840 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001841 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001842 Node->getOperand(i), Idx,
1843 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001844 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001845 }
1846
1847 SDValue StoreChain;
1848 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001850 &Stores[0], Stores.size());
1851 else
1852 StoreChain = DAG.getEntryNode();
1853
1854 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001855 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001856}
1857
Eli Friedman4bc8c712009-05-27 12:20:41 +00001858SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1859 DebugLoc dl = Node->getDebugLoc();
1860 SDValue Tmp1 = Node->getOperand(0);
1861 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001862
1863 // Get the sign bit of the RHS. First obtain a value that has the same
1864 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001865 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001866 EVT FloatVT = Tmp2.getValueType();
1867 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Eli Friedman4bc8c712009-05-27 12:20:41 +00001868 if (isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001869 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001870 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001871 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001872 // Store the float to memory, then load the sign part out as an integer.
1873 MVT LoadTy = TLI.getPointerTy();
1874 // First create a temporary that is aligned for both the load and store.
1875 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1876 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001877 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001878 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001879 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001880 if (TLI.isBigEndian()) {
1881 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1882 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001883 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1884 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001885 } else { // Little endian
1886 SDValue LoadPtr = StackPtr;
1887 // The float may be wider than the integer we are going to load. Advance
1888 // the pointer so that the loaded integer will contain the sign bit.
1889 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1890 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1891 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1892 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1893 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001894 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1895 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001896 // Move the sign bit to the top bit of the loaded integer.
1897 unsigned BitShift = LoadTy.getSizeInBits() -
1898 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1899 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1900 if (BitShift)
1901 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001902 DAG.getConstant(BitShift,
1903 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001904 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001905 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001906 // Now get the sign bit proper, by seeing whether the value is negative.
1907 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1908 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1909 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001910 // Get the absolute value of the result.
1911 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1912 // Select between the nabs and abs value based on the sign bit of
1913 // the input.
1914 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1915 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1916 AbsVal);
1917}
1918
Eli Friedman4bc8c712009-05-27 12:20:41 +00001919void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1920 SmallVectorImpl<SDValue> &Results) {
1921 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1922 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1923 " not tell us which reg is the stack pointer!");
1924 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001925 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001926 SDValue Tmp1 = SDValue(Node, 0);
1927 SDValue Tmp2 = SDValue(Node, 1);
1928 SDValue Tmp3 = Node->getOperand(2);
1929 SDValue Chain = Tmp1.getOperand(0);
1930
1931 // Chain the dynamic stack allocation so that it doesn't modify the stack
1932 // pointer when other instructions are using the stack.
1933 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1934
1935 SDValue Size = Tmp2.getOperand(1);
1936 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1937 Chain = SP.getValue(1);
1938 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001939 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001940 if (Align > StackAlign)
1941 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1942 DAG.getConstant(-(uint64_t)Align, VT));
1943 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1944 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1945
1946 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1947 DAG.getIntPtrConstant(0, true), SDValue());
1948
1949 Results.push_back(Tmp1);
1950 Results.push_back(Tmp2);
1951}
1952
Evan Cheng7f042682008-10-15 02:05:31 +00001953/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001954/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001955/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001956void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001957 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001958 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001959 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001960 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001961 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1962 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001963 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001964 case TargetLowering::Legal:
1965 // Nothing to do.
1966 break;
1967 case TargetLowering::Expand: {
1968 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1969 unsigned Opc = 0;
1970 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001971 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001972 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1973 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1974 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1975 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1976 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1977 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1978 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1979 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1980 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1981 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1982 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1983 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001984 // FIXME: Implement more expansions.
1985 }
1986
Dale Johannesenbb5da912009-02-02 20:41:04 +00001987 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1988 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1989 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001990 RHS = SDValue();
1991 CC = SDValue();
1992 break;
1993 }
1994 }
1995}
1996
Chris Lattner1401d152008-01-16 07:45:30 +00001997/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1998/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1999/// a load from the stack slot to DestVT, extending it if needed.
2000/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00002001SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002002 EVT SlotVT,
2003 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002004 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00002005 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002006 unsigned SrcAlign =
2007 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00002008 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00002009 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002010
Evan Chengff89dcb2009-10-18 18:16:27 +00002011 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
2012 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002013 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00002014
Duncan Sands83ec4b62008-06-06 12:08:01 +00002015 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2016 unsigned SlotSize = SlotVT.getSizeInBits();
2017 unsigned DestSize = DestVT.getSizeInBits();
Evan Chengadf97992010-04-15 01:25:27 +00002018 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
2019 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00002020
Chris Lattner1401d152008-01-16 07:45:30 +00002021 // Emit a store to the stack slot. Use a truncstore if the input value is
2022 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002023 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00002024
Chris Lattner1401d152008-01-16 07:45:30 +00002025 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002026 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002027 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002028 else {
2029 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002030 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002031 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002032 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002033
Chris Lattner35481892005-12-23 00:16:34 +00002034 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002035 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002036 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00002037 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002038
Chris Lattner1401d152008-01-16 07:45:30 +00002039 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00002040 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002041 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002042}
2043
Dan Gohman475871a2008-07-27 21:46:04 +00002044SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002045 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002046 // Create a vector sized/aligned stack slot, store the value to element #0,
2047 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002049
Evan Chengff89dcb2009-10-18 18:16:27 +00002050 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
2051 int SPFI = StackPtrFI->getIndex();
2052
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002053 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2054 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00002055 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00002056 Node->getValueType(0).getVectorElementType(),
2057 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00002058 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00002059 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00002060 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002061}
2062
2063
Chris Lattnerce872152006-03-19 06:31:19 +00002064/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002065/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002066SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002067 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00002068 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002069 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002070 EVT VT = Node->getValueType(0);
2071 EVT OpVT = Node->getOperand(0).getValueType();
2072 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002073
2074 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002075 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002076 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002077 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00002078 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002079 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002080 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00002081 if (V.getOpcode() == ISD::UNDEF)
2082 continue;
2083 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00002084 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002085 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00002086 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002087
2088 if (!Value1.getNode()) {
2089 Value1 = V;
2090 } else if (!Value2.getNode()) {
2091 if (V != Value1)
2092 Value2 = V;
2093 } else if (V != Value1 && V != Value2) {
2094 MoreThanTwoValues = true;
2095 }
Chris Lattnerce872152006-03-19 06:31:19 +00002096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002097
Eli Friedman7a5e5552009-06-07 06:52:44 +00002098 if (!Value1.getNode())
2099 return DAG.getUNDEF(VT);
2100
2101 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002102 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002103
Chris Lattner2eb86532006-03-24 07:29:17 +00002104 // If all elements are constants, create a load from the constant pool.
2105 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002106 std::vector<Constant*> CV;
2107 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002108 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002109 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002110 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002111 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002112 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00002113 if (OpVT==EltVT)
2114 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
2115 else {
2116 // If OpVT and EltVT don't match, EltVT is not legal and the
2117 // element values have been promoted/truncated earlier. Undo this;
2118 // we don't want a v16i8 to become a v16i32 for example.
2119 const ConstantInt *CI = V->getConstantIntValue();
2120 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
2121 CI->getZExtValue()));
2122 }
Chris Lattner2eb86532006-03-24 07:29:17 +00002123 } else {
2124 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Dale Johannesen9a645cd2009-11-10 23:16:41 +00002125 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002126 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00002127 }
2128 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00002129 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002130 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002131 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002132 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002133 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00002134 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002135 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002136
Eli Friedman7a5e5552009-06-07 06:52:44 +00002137 if (!MoreThanTwoValues) {
2138 SmallVector<int, 8> ShuffleVec(NumElems, -1);
2139 for (unsigned i = 0; i < NumElems; ++i) {
2140 SDValue V = Node->getOperand(i);
2141 if (V.getOpcode() == ISD::UNDEF)
2142 continue;
2143 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2144 }
2145 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002146 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00002147 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2148 SDValue Vec2;
2149 if (Value2.getNode())
2150 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2151 else
2152 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002153
Chris Lattner87100e02006-03-20 01:52:29 +00002154 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00002155 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00002156 }
2157 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002158
Eli Friedman7ef3d172009-06-06 07:04:42 +00002159 // Otherwise, we can't handle this case efficiently.
2160 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00002161}
2162
Chris Lattner77e77a62005-01-21 06:05:23 +00002163// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2164// does not fit into a register, return the lo part and set the hi part to the
2165// by-reg argument. If it does fit into a single register, return the result
2166// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002167SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002168 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002169 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002170 // Legalizing the call will automatically add the previous call to the
2171 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002172 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002173
Chris Lattner77e77a62005-01-21 06:05:23 +00002174 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002175 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002176 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002177 EVT ArgVT = Node->getOperand(i).getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +00002178 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00002179 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002180 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002181 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002182 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002183 }
Bill Wendling056292f2008-09-16 21:48:12 +00002184 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002185 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002186
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002187 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson23b9b192009-08-12 00:36:31 +00002188 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00002189
2190 // isTailCall may be true since the callee does not reference caller stack
2191 // frame. Check if it's in the right position.
2192 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002193 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002194 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002195 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002196 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002197 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002198
Evan Cheng3d2125c2010-11-30 23:55:39 +00002199 if (!CallInfo.second.getNode())
2200 // It's a tailcall, return the chain (which is the DAG root).
2201 return DAG.getRoot();
2202
Chris Lattner6831a812006-02-13 09:18:02 +00002203 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002204 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Chris Lattner6831a812006-02-13 09:18:02 +00002205 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2206 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002207 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002208}
2209
Dan Gohmanf316eb72011-05-16 22:09:53 +00002210/// ExpandLibCall - Generate a libcall taking the given operands as arguments
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002211/// and returning a result of type RetVT.
2212SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
2213 const SDValue *Ops, unsigned NumOps,
2214 bool isSigned, DebugLoc dl) {
2215 TargetLowering::ArgListTy Args;
2216 Args.reserve(NumOps);
Dan Gohmanf316eb72011-05-16 22:09:53 +00002217
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002218 TargetLowering::ArgListEntry Entry;
2219 for (unsigned i = 0; i != NumOps; ++i) {
2220 Entry.Node = Ops[i];
2221 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
2222 Entry.isSExt = isSigned;
2223 Entry.isZExt = !isSigned;
2224 Args.push_back(Entry);
2225 }
2226 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2227 TLI.getPointerTy());
Dan Gohmanf316eb72011-05-16 22:09:53 +00002228
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002229 const Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2230 std::pair<SDValue,SDValue> CallInfo =
2231 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
2232 false, 0, TLI.getLibcallCallingConv(LC), false,
2233 /*isReturnValueUsed=*/true,
2234 Callee, Args, DAG, dl);
Dan Gohmanf316eb72011-05-16 22:09:53 +00002235
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002236 // Legalize the call sequence, starting with the chain. This will advance
2237 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2238 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2239 LegalizeOp(CallInfo.second);
2240
2241 return CallInfo.first;
2242}
2243
Jim Grosbache03262f2010-06-18 21:43:38 +00002244// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
2245// ExpandLibCall except that the first operand is the in-chain.
2246std::pair<SDValue, SDValue>
2247SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2248 SDNode *Node,
2249 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00002250 SDValue InChain = Node->getOperand(0);
2251
2252 TargetLowering::ArgListTy Args;
2253 TargetLowering::ArgListEntry Entry;
2254 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2255 EVT ArgVT = Node->getOperand(i).getValueType();
2256 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2257 Entry.Node = Node->getOperand(i);
2258 Entry.Ty = ArgTy;
2259 Entry.isSExt = isSigned;
2260 Entry.isZExt = !isSigned;
2261 Args.push_back(Entry);
2262 }
2263 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2264 TLI.getPointerTy());
2265
2266 // Splice the libcall in wherever FindInputOutputChains tells us to.
2267 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2268 std::pair<SDValue, SDValue> CallInfo =
2269 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002270 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002271 /*isReturnValueUsed=*/true,
2272 Callee, Args, DAG, Node->getDebugLoc());
2273
2274 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002275 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Jim Grosbache03262f2010-06-18 21:43:38 +00002276 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2277 LegalizeOp(CallInfo.second);
2278 return CallInfo;
2279}
2280
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002281SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2282 RTLIB::Libcall Call_F32,
2283 RTLIB::Libcall Call_F64,
2284 RTLIB::Libcall Call_F80,
2285 RTLIB::Libcall Call_PPCF128) {
2286 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002288 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002289 case MVT::f32: LC = Call_F32; break;
2290 case MVT::f64: LC = Call_F64; break;
2291 case MVT::f80: LC = Call_F80; break;
2292 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002293 }
2294 return ExpandLibCall(LC, Node, false);
2295}
2296
2297SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002298 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002299 RTLIB::Libcall Call_I16,
2300 RTLIB::Libcall Call_I32,
2301 RTLIB::Libcall Call_I64,
2302 RTLIB::Libcall Call_I128) {
2303 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002304 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002305 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002306 case MVT::i8: LC = Call_I8; break;
2307 case MVT::i16: LC = Call_I16; break;
2308 case MVT::i32: LC = Call_I32; break;
2309 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002310 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002311 }
2312 return ExpandLibCall(LC, Node, isSigned);
2313}
2314
Evan Cheng65279cb2011-04-16 03:08:26 +00002315/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
2316static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
2317 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002318 RTLIB::Libcall LC;
2319 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2320 default: assert(0 && "Unexpected request for libcall!");
2321 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2322 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2323 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2324 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2325 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2326 }
2327
Evan Cheng65279cb2011-04-16 03:08:26 +00002328 return TLI.getLibcallName(LC) != 0;
2329}
Evan Cheng8e23e812011-04-01 00:42:02 +00002330
Evan Cheng65279cb2011-04-16 03:08:26 +00002331/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
2332/// needed.
2333static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002334 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00002335 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00002336 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002337 else
Evan Cheng8e23e812011-04-01 00:42:02 +00002338 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002339
Evan Cheng8e23e812011-04-01 00:42:02 +00002340 SDValue Op0 = Node->getOperand(0);
2341 SDValue Op1 = Node->getOperand(1);
2342 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2343 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2344 SDNode *User = *UI;
2345 if (User == Node)
2346 continue;
2347 if (User->getOpcode() == OtherOpcode &&
2348 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00002349 User->getOperand(1) == Op1)
2350 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00002351 }
Evan Cheng65279cb2011-04-16 03:08:26 +00002352 return false;
2353}
Evan Cheng8e23e812011-04-01 00:42:02 +00002354
Evan Cheng65279cb2011-04-16 03:08:26 +00002355/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
2356/// pairs.
2357void
2358SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2359 SmallVectorImpl<SDValue> &Results) {
2360 unsigned Opcode = Node->getOpcode();
2361 bool isSigned = Opcode == ISD::SDIVREM;
2362
2363 RTLIB::Libcall LC;
2364 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2365 default: assert(0 && "Unexpected request for libcall!");
2366 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2367 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2368 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2369 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2370 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00002371 }
2372
2373 // The input chain to this libcall is the entry node of the function.
2374 // Legalizing the call will automatically add the previous call to the
2375 // dependence.
2376 SDValue InChain = DAG.getEntryNode();
2377
2378 EVT RetVT = Node->getValueType(0);
2379 const Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2380
2381 TargetLowering::ArgListTy Args;
2382 TargetLowering::ArgListEntry Entry;
2383 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2384 EVT ArgVT = Node->getOperand(i).getValueType();
2385 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2386 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
2387 Entry.isSExt = isSigned;
2388 Entry.isZExt = !isSigned;
2389 Args.push_back(Entry);
2390 }
2391
2392 // Also pass the return address of the remainder.
2393 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2394 Entry.Node = FIPtr;
2395 Entry.Ty = RetTy->getPointerTo();
2396 Entry.isSExt = isSigned;
2397 Entry.isZExt = !isSigned;
2398 Args.push_back(Entry);
2399
2400 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2401 TLI.getPointerTy());
2402
2403 // Splice the libcall in wherever FindInputOutputChains tells us to.
2404 DebugLoc dl = Node->getDebugLoc();
2405 std::pair<SDValue, SDValue> CallInfo =
2406 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
2407 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
2408 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
2409
2410 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002411 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Evan Cheng8e23e812011-04-01 00:42:02 +00002412 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2413 LegalizeOp(CallInfo.second);
2414
2415 // Remainder is loaded back from the stack frame.
Stuart Hastingsfc521632011-04-19 16:16:58 +00002416 SDValue Rem = DAG.getLoad(RetVT, dl, getLastCALLSEQ(), FIPtr,
Evan Cheng8e23e812011-04-01 00:42:02 +00002417 MachinePointerInfo(), false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00002418 Results.push_back(CallInfo.first);
2419 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00002420}
2421
Chris Lattner22cde6a2006-01-28 08:25:58 +00002422/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2423/// INT_TO_FP operation of the specified operand when the target requests that
2424/// we expand it. At this point, we know that the result and operand types are
2425/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002426SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2427 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002428 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002429 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002430 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002431 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002432
Chris Lattner23594d42008-01-16 07:03:22 +00002433 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002434 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002435
Chris Lattner22cde6a2006-01-28 08:25:58 +00002436 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002437 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002438 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002439 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002440 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002441 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002442 if (TLI.isLittleEndian())
2443 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002444
Chris Lattner22cde6a2006-01-28 08:25:58 +00002445 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002446 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002447 if (isSigned) {
2448 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002449 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2450 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002451 } else {
2452 Op0Mapped = Op0;
2453 }
2454 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002455 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002456 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002457 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002458 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002459 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002460 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002461 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2462 MachinePointerInfo(),
2463 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002464 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002465 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2466 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002467 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002468 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002469 BitsToDouble(0x4330000080000000ULL) :
2470 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002471 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002472 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002473 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002474 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002475 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002476 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002477 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002478 // do nothing
2479 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002480 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002481 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002482 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002483 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002484 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002485 }
2486 return Result;
2487 }
2488 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002489 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002490
2491 // Implementation of unsigned i64 to f64 following the algorithm in
2492 // __floatundidf in compiler_rt. This implementation has the advantage
2493 // of performing rounding correctly, both in the default rounding mode
2494 // and in all alternate rounding modes.
2495 // TODO: Generalize this for use with other types.
2496 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2497 SDValue TwoP52 =
2498 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2499 SDValue TwoP84PlusTwoP52 =
2500 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2501 SDValue TwoP84 =
2502 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2503
2504 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2505 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2506 DAG.getConstant(32, MVT::i64));
2507 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2508 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002509 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2510 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002511 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2512 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002513 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2514 }
2515
Owen Anderson3a9e7692010-10-05 17:24:05 +00002516 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002517 // TODO: Generalize this for use with other types.
2518 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002519 // For unsigned conversions, convert them to signed conversions using the
2520 // algorithm from the x86_64 __floatundidf in compiler_rt.
2521 if (!isSigned) {
2522 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002523
Owen Anderson95771af2011-02-25 21:41:48 +00002524 SDValue ShiftConst =
2525 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002526 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2527 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2528 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2529 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002530
Owen Anderson3a9e7692010-10-05 17:24:05 +00002531 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2532 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002533
Owen Anderson3a9e7692010-10-05 17:24:05 +00002534 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002535 // select. We happen to get lucky and machinesink does the right
2536 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002537 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002538 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002539 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2540 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2541 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002542
Owen Anderson3a9e7692010-10-05 17:24:05 +00002543 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002544
Jim Grosbach6e992612010-07-02 17:41:59 +00002545 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002546 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2547 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2548 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002549 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002550 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2551 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2552 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2553 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2554 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2555 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002556 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002557 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002558 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002559
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002560 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2561 DAG.getConstant(32, SHVT));
2562 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2563 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2564 SDValue TwoP32 =
2565 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2566 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2567 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2568 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2569 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2570 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2571 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002572 }
2573
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002574 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002575
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002576 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2577 Op0, DAG.getConstant(0, Op0.getValueType()),
2578 ISD::SETLT);
2579 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2580 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2581 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002582
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002583 // If the sign bit of the integer is set, the large number will be treated
2584 // as a negative number. To counteract this, the dynamic code adds an
2585 // offset depending on the data type.
2586 uint64_t FF;
2587 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002588 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002589 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2590 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2591 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2592 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2593 }
2594 if (TLI.isLittleEndian()) FF <<= 32;
2595 Constant *FudgeFactor = ConstantInt::get(
2596 Type::getInt64Ty(*DAG.getContext()), FF);
2597
2598 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2599 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2600 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2601 Alignment = std::min(Alignment, 4u);
2602 SDValue FudgeInReg;
2603 if (DestVT == MVT::f32)
2604 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002605 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002606 false, false, Alignment);
2607 else {
2608 FudgeInReg =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002609 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002610 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002611 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002612 MVT::f32, false, false, Alignment));
2613 }
2614
2615 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002616}
2617
2618/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2619/// *INT_TO_FP operation of the specified operand when the target requests that
2620/// we promote it. At this point, we know that the result and operand types are
2621/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2622/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002623SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002624 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002625 bool isSigned,
2626 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002627 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002628 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002629
2630 unsigned OpToUse = 0;
2631
2632 // Scan for the appropriate larger type to use.
2633 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002635 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002636
2637 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002638 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2639 OpToUse = ISD::SINT_TO_FP;
2640 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002641 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002642 if (isSigned) continue;
2643
2644 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002645 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2646 OpToUse = ISD::UINT_TO_FP;
2647 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002648 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002649
2650 // Otherwise, try a larger type.
2651 }
2652
2653 // Okay, we found the operation and type to use. Zero extend our input to the
2654 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002655 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002656 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002657 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002658}
2659
2660/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2661/// FP_TO_*INT operation of the specified operand when the target requests that
2662/// we promote it. At this point, we know that the result and operand types are
2663/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2664/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002665SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002666 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002667 bool isSigned,
2668 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002669 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002670 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002671
2672 unsigned OpToUse = 0;
2673
2674 // Scan for the appropriate larger type to use.
2675 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002676 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002677 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002678
Eli Friedman3be2e512009-05-28 03:06:16 +00002679 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002680 OpToUse = ISD::FP_TO_SINT;
2681 break;
2682 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002683
Eli Friedman3be2e512009-05-28 03:06:16 +00002684 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002685 OpToUse = ISD::FP_TO_UINT;
2686 break;
2687 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002688
2689 // Otherwise, try a larger type.
2690 }
2691
Scott Michelfdc40a02009-02-17 22:15:04 +00002692
Chris Lattner27a6c732007-11-24 07:07:01 +00002693 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002694 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002695
Chris Lattner27a6c732007-11-24 07:07:01 +00002696 // Truncate the result of the extended FP_TO_*INT operation to the desired
2697 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002698 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002699}
2700
2701/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2702///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002703SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002704 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002705 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002706 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002707 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002708 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002709 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002710 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2711 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2712 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002713 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002714 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2715 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2716 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2717 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2718 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2719 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2720 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2721 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2722 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002723 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002724 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2725 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2726 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2727 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2728 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2729 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2730 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2731 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2732 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2733 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2734 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2735 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2736 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2737 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2738 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2739 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2740 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2741 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2742 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2743 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2744 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002745 }
2746}
2747
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002748/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002749// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002750static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2751 APInt Val = APInt(NumBits, ByteVal);
2752 unsigned Shift = 8;
2753 for (unsigned i = NumBits; i > 8; i >>= 1) {
2754 Val = (Val << Shift) | Val;
2755 Shift <<= 1;
2756 }
2757 return Val;
2758}
2759
Chris Lattner22cde6a2006-01-28 08:25:58 +00002760/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2761///
Scott Michelfdc40a02009-02-17 22:15:04 +00002762SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002763 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002764 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002765 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002766 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002767 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002768 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002769 unsigned Len = VT.getSizeInBits();
2770
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002771 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2772 "CTPOP not implemented for this type.");
2773
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002774 // This is the "best" algorithm from
2775 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2776
2777 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2778 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2779 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2780 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2781
2782 // v = v - ((v >> 1) & 0x55555555...)
2783 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2784 DAG.getNode(ISD::AND, dl, VT,
2785 DAG.getNode(ISD::SRL, dl, VT, Op,
2786 DAG.getConstant(1, ShVT)),
2787 Mask55));
2788 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2789 Op = DAG.getNode(ISD::ADD, dl, VT,
2790 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2791 DAG.getNode(ISD::AND, dl, VT,
2792 DAG.getNode(ISD::SRL, dl, VT, Op,
2793 DAG.getConstant(2, ShVT)),
2794 Mask33));
2795 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2796 Op = DAG.getNode(ISD::AND, dl, VT,
2797 DAG.getNode(ISD::ADD, dl, VT, Op,
2798 DAG.getNode(ISD::SRL, dl, VT, Op,
2799 DAG.getConstant(4, ShVT))),
2800 Mask0F);
2801 // v = (v * 0x01010101...) >> (Len - 8)
2802 Op = DAG.getNode(ISD::SRL, dl, VT,
2803 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2804 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002805
Chris Lattner22cde6a2006-01-28 08:25:58 +00002806 return Op;
2807 }
2808 case ISD::CTLZ: {
2809 // for now, we do this:
2810 // x = x | (x >> 1);
2811 // x = x | (x >> 2);
2812 // ...
2813 // x = x | (x >>16);
2814 // x = x | (x >>32); // for 64-bit input
2815 // return popcount(~x);
2816 //
2817 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002818 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002819 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002820 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002821 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002822 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002823 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002824 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002825 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002826 Op = DAG.getNOT(dl, Op, VT);
2827 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002828 }
2829 case ISD::CTTZ: {
2830 // for now, we use: { return popcount(~x & (x - 1)); }
2831 // unless the target has ctlz but not ctpop, in which case we use:
2832 // { return 32 - nlz(~x & (x-1)); }
2833 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002834 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002835 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2836 DAG.getNOT(dl, Op, VT),
2837 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002838 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002839 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002840 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2841 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002842 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002843 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002844 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2845 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002846 }
2847 }
2848}
Chris Lattnere34b3962005-01-19 04:19:40 +00002849
Jim Grosbache03262f2010-06-18 21:43:38 +00002850std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2851 unsigned Opc = Node->getOpcode();
2852 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2853 RTLIB::Libcall LC;
2854
2855 switch (Opc) {
2856 default:
2857 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2858 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002859 case ISD::ATOMIC_SWAP:
2860 switch (VT.SimpleTy) {
2861 default: llvm_unreachable("Unexpected value type for atomic!");
2862 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2863 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2864 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2865 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2866 }
2867 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002868 case ISD::ATOMIC_CMP_SWAP:
2869 switch (VT.SimpleTy) {
2870 default: llvm_unreachable("Unexpected value type for atomic!");
2871 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2872 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2873 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2874 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2875 }
2876 break;
2877 case ISD::ATOMIC_LOAD_ADD:
2878 switch (VT.SimpleTy) {
2879 default: llvm_unreachable("Unexpected value type for atomic!");
2880 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2881 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2882 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2883 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2884 }
2885 break;
2886 case ISD::ATOMIC_LOAD_SUB:
2887 switch (VT.SimpleTy) {
2888 default: llvm_unreachable("Unexpected value type for atomic!");
2889 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2890 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2891 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2892 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2893 }
2894 break;
2895 case ISD::ATOMIC_LOAD_AND:
2896 switch (VT.SimpleTy) {
2897 default: llvm_unreachable("Unexpected value type for atomic!");
2898 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2899 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2900 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2901 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2902 }
2903 break;
2904 case ISD::ATOMIC_LOAD_OR:
2905 switch (VT.SimpleTy) {
2906 default: llvm_unreachable("Unexpected value type for atomic!");
2907 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2908 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2909 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2910 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2911 }
2912 break;
2913 case ISD::ATOMIC_LOAD_XOR:
2914 switch (VT.SimpleTy) {
2915 default: llvm_unreachable("Unexpected value type for atomic!");
2916 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2917 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2918 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2919 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2920 }
2921 break;
2922 case ISD::ATOMIC_LOAD_NAND:
2923 switch (VT.SimpleTy) {
2924 default: llvm_unreachable("Unexpected value type for atomic!");
2925 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2926 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2927 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2928 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2929 }
2930 break;
2931 }
2932
2933 return ExpandChainLibCall(LC, Node, false);
2934}
2935
Eli Friedman8c377c72009-05-27 01:25:56 +00002936void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2937 SmallVectorImpl<SDValue> &Results) {
2938 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002939 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002940 switch (Node->getOpcode()) {
2941 case ISD::CTPOP:
2942 case ISD::CTLZ:
2943 case ISD::CTTZ:
2944 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2945 Results.push_back(Tmp1);
2946 break;
2947 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002948 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002949 break;
2950 case ISD::FRAMEADDR:
2951 case ISD::RETURNADDR:
2952 case ISD::FRAME_TO_ARGS_OFFSET:
2953 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2954 break;
2955 case ISD::FLT_ROUNDS_:
2956 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2957 break;
2958 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002959 case ISD::EH_LABEL:
2960 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002961 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002962 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002963 case ISD::EH_SJLJ_DISPATCHSETUP:
2964 // If the target didn't expand these, there's nothing to do, so just
2965 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002966 Results.push_back(Node->getOperand(0));
2967 break;
2968 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002969 // If the target didn't expand this, just return 'zero' and preserve the
2970 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002971 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002972 Results.push_back(Node->getOperand(0));
2973 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002974 case ISD::MEMBARRIER: {
2975 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2976 TargetLowering::ArgListTy Args;
2977 std::pair<SDValue, SDValue> CallResult =
2978 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002979 false, false, false, false, 0, CallingConv::C,
2980 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002981 /*isReturnValueUsed=*/true,
2982 DAG.getExternalSymbol("__sync_synchronize",
2983 TLI.getPointerTy()),
2984 Args, DAG, dl);
2985 Results.push_back(CallResult.second);
2986 break;
2987 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002988 // By default, atomic intrinsics are marked Legal and lowered. Targets
2989 // which don't support them directly, however, may want libcalls, in which
2990 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002991 case ISD::ATOMIC_SWAP:
2992 case ISD::ATOMIC_LOAD_ADD:
2993 case ISD::ATOMIC_LOAD_SUB:
2994 case ISD::ATOMIC_LOAD_AND:
2995 case ISD::ATOMIC_LOAD_OR:
2996 case ISD::ATOMIC_LOAD_XOR:
2997 case ISD::ATOMIC_LOAD_NAND:
2998 case ISD::ATOMIC_LOAD_MIN:
2999 case ISD::ATOMIC_LOAD_MAX:
3000 case ISD::ATOMIC_LOAD_UMIN:
3001 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00003002 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00003003 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
3004 Results.push_back(Tmp.first);
3005 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00003006 break;
Evan Chenga8457062010-06-18 22:01:37 +00003007 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003008 case ISD::DYNAMIC_STACKALLOC:
3009 ExpandDYNAMIC_STACKALLOC(Node, Results);
3010 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003011 case ISD::MERGE_VALUES:
3012 for (unsigned i = 0; i < Node->getNumValues(); i++)
3013 Results.push_back(Node->getOperand(i));
3014 break;
3015 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00003016 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003017 if (VT.isInteger())
3018 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00003019 else {
3020 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00003021 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00003022 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003023 break;
3024 }
3025 case ISD::TRAP: {
3026 // If this operation is not supported, lower it to 'abort()' call
3027 TargetLowering::ArgListTy Args;
3028 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003029 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00003030 false, false, false, false, 0, CallingConv::C,
3031 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003032 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00003033 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003034 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00003035 Results.push_back(CallResult.second);
3036 break;
3037 }
3038 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003039 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00003040 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3041 Node->getValueType(0), dl);
3042 Results.push_back(Tmp1);
3043 break;
3044 case ISD::FP_EXTEND:
3045 Tmp1 = EmitStackConvert(Node->getOperand(0),
3046 Node->getOperand(0).getValueType(),
3047 Node->getValueType(0), dl);
3048 Results.push_back(Tmp1);
3049 break;
3050 case ISD::SIGN_EXTEND_INREG: {
3051 // NOTE: we could fall back on load/store here too for targets without
3052 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00003053 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00003054 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00003055 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00003056 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00003057 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00003058 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
3059 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00003060 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00003061 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3062 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00003063 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3064 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003065 break;
3066 }
3067 case ISD::FP_ROUND_INREG: {
3068 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00003069 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00003070
3071 // NOTE: there is a choice here between constantly creating new stack
3072 // slots and always reusing the same one. We currently always create
3073 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00003074 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00003075 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3076 Node->getValueType(0), dl);
3077 Results.push_back(Tmp1);
3078 break;
3079 }
3080 case ISD::SINT_TO_FP:
3081 case ISD::UINT_TO_FP:
3082 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3083 Node->getOperand(0), Node->getValueType(0), dl);
3084 Results.push_back(Tmp1);
3085 break;
3086 case ISD::FP_TO_UINT: {
3087 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00003088 EVT VT = Node->getOperand(0).getValueType();
3089 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00003090 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00003091 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3092 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3093 Tmp1 = DAG.getConstantFP(apf, VT);
3094 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3095 Node->getOperand(0),
3096 Tmp1, ISD::SETLT);
3097 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003098 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3099 DAG.getNode(ISD::FSUB, dl, VT,
3100 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003101 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3102 DAG.getConstant(x, NVT));
3103 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
3104 Results.push_back(Tmp1);
3105 break;
3106 }
Eli Friedman509150f2009-05-27 07:58:35 +00003107 case ISD::VAARG: {
3108 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00003109 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00003110 Tmp1 = Node->getOperand(0);
3111 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003112 unsigned Align = Node->getConstantOperandVal(3);
3113
Chris Lattnerecf42c42010-09-21 16:36:31 +00003114 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
3115 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003116 SDValue VAList = VAListLoad;
3117
Rafael Espindolacbeeae22010-07-11 04:01:49 +00003118 if (Align > TLI.getMinStackArgumentAlignment()) {
3119 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
3120
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003121 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
3122 DAG.getConstant(Align - 1,
3123 TLI.getPointerTy()));
3124
3125 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00003126 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003127 TLI.getPointerTy()));
3128 }
3129
Eli Friedman509150f2009-05-27 07:58:35 +00003130 // Increment the pointer, VAList, to the next vaarg
3131 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
3132 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00003133 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00003134 TLI.getPointerTy()));
3135 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00003136 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
3137 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00003138 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00003139 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00003140 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00003141 Results.push_back(Results[0].getValue(1));
3142 break;
3143 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003144 case ISD::VACOPY: {
3145 // This defaults to loading a pointer from the input and storing it to the
3146 // output, returning the chain.
3147 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3148 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3149 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00003150 Node->getOperand(2), MachinePointerInfo(VS),
3151 false, false, 0);
3152 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
3153 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00003154 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003155 break;
3156 }
3157 case ISD::EXTRACT_VECTOR_ELT:
3158 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3159 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003160 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00003161 Node->getOperand(0));
3162 else
3163 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3164 Results.push_back(Tmp1);
3165 break;
3166 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00003167 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00003168 break;
David Greenecfe33c42011-01-26 19:13:22 +00003169 case ISD::INSERT_SUBVECTOR:
3170 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3171 break;
Eli Friedman509150f2009-05-27 07:58:35 +00003172 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00003173 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00003174 break;
3175 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003176 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00003177 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00003178 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003179 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00003180 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3181 Node->getOperand(1),
3182 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00003183 break;
Eli Friedman509150f2009-05-27 07:58:35 +00003184 case ISD::VECTOR_SHUFFLE: {
3185 SmallVector<int, 8> Mask;
3186 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3187
Owen Andersone50ed302009-08-10 22:56:29 +00003188 EVT VT = Node->getValueType(0);
3189 EVT EltVT = VT.getVectorElementType();
Bob Wilson14b21412010-05-19 18:48:32 +00003190 if (getTypeAction(EltVT) == Promote)
3191 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00003192 unsigned NumElems = VT.getVectorNumElements();
3193 SmallVector<SDValue, 8> Ops;
3194 for (unsigned i = 0; i != NumElems; ++i) {
3195 if (Mask[i] < 0) {
3196 Ops.push_back(DAG.getUNDEF(EltVT));
3197 continue;
3198 }
3199 unsigned Idx = Mask[i];
3200 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00003201 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3202 Node->getOperand(0),
3203 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00003204 else
Bill Wendling775db972009-12-23 00:28:23 +00003205 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3206 Node->getOperand(1),
3207 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00003208 }
3209 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
3210 Results.push_back(Tmp1);
3211 break;
3212 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003213 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00003214 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00003215 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3216 // 1 -> Hi
3217 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3218 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003219 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00003220 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3221 } else {
3222 // 0 -> Lo
3223 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3224 Node->getOperand(0));
3225 }
3226 Results.push_back(Tmp1);
3227 break;
3228 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003229 case ISD::STACKSAVE:
3230 // Expand to CopyFromReg if the target set
3231 // StackPointerRegisterToSaveRestore.
3232 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00003233 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3234 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003235 Results.push_back(Results[0].getValue(1));
3236 } else {
Bill Wendling775db972009-12-23 00:28:23 +00003237 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003238 Results.push_back(Node->getOperand(0));
3239 }
3240 break;
3241 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00003242 // Expand to CopyToReg if the target set
3243 // StackPointerRegisterToSaveRestore.
3244 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3245 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3246 Node->getOperand(1)));
3247 } else {
3248 Results.push_back(Node->getOperand(0));
3249 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003250 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003251 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00003252 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003253 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003254 case ISD::FNEG:
3255 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3256 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3257 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3258 Node->getOperand(0));
3259 Results.push_back(Tmp1);
3260 break;
3261 case ISD::FABS: {
3262 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00003263 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003264 Tmp1 = Node->getOperand(0);
3265 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00003266 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003267 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00003268 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3269 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003270 Results.push_back(Tmp1);
3271 break;
3272 }
3273 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00003274 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3275 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003276 break;
3277 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00003278 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3279 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003280 break;
3281 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00003282 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3283 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003284 break;
3285 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00003286 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3287 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003288 break;
3289 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00003290 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3291 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003292 break;
3293 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00003294 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3295 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003296 break;
3297 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00003298 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3299 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003300 break;
3301 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00003302 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3303 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003304 break;
3305 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00003306 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3307 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003308 break;
3309 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00003310 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3311 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003312 break;
3313 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00003314 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3315 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003316 break;
3317 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003318 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3319 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003320 break;
3321 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003322 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3323 RTLIB::NEARBYINT_F64,
3324 RTLIB::NEARBYINT_F80,
3325 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003326 break;
3327 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003328 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3329 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003330 break;
3331 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003332 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3333 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003334 break;
3335 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003336 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3337 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003338 break;
3339 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003340 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3341 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003342 break;
Cameron Zwarich33390842011-07-08 21:39:21 +00003343 case ISD::FMA:
3344 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
3345 RTLIB::FMA_F80, RTLIB::FMA_PPCF128));
3346 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003347 case ISD::FP16_TO_FP32:
3348 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3349 break;
3350 case ISD::FP32_TO_FP16:
3351 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3352 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003353 case ISD::ConstantFP: {
3354 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003355 // Check to see if this FP immediate is already legal.
3356 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003357 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00003358 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003359 else
Bill Wendling775db972009-12-23 00:28:23 +00003360 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003361 break;
3362 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003363 case ISD::EHSELECTION: {
3364 unsigned Reg = TLI.getExceptionSelectorRegister();
3365 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003366 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3367 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003368 Results.push_back(Results[0].getValue(1));
3369 break;
3370 }
3371 case ISD::EXCEPTIONADDR: {
3372 unsigned Reg = TLI.getExceptionAddressRegister();
3373 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003374 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3375 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003376 Results.push_back(Results[0].getValue(1));
3377 break;
3378 }
3379 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003380 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003381 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3382 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3383 "Don't know how to expand this subtraction!");
3384 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3385 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003386 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3387 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003388 break;
3389 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003390 case ISD::UREM:
3391 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003392 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003393 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003394 bool isSigned = Node->getOpcode() == ISD::SREM;
3395 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3396 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3397 Tmp2 = Node->getOperand(0);
3398 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003399 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3400 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3401 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00003402 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3403 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003404 // X % Y -> X-X/Y*Y
3405 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3406 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3407 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003408 } else if (isSigned)
3409 Tmp1 = ExpandIntLibCall(Node, true,
3410 RTLIB::SREM_I8,
3411 RTLIB::SREM_I16, RTLIB::SREM_I32,
3412 RTLIB::SREM_I64, RTLIB::SREM_I128);
3413 else
3414 Tmp1 = ExpandIntLibCall(Node, false,
3415 RTLIB::UREM_I8,
3416 RTLIB::UREM_I16, RTLIB::UREM_I32,
3417 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003418 Results.push_back(Tmp1);
3419 break;
3420 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003421 case ISD::UDIV:
3422 case ISD::SDIV: {
3423 bool isSigned = Node->getOpcode() == ISD::SDIV;
3424 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003425 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003426 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003427 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3428 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3429 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003430 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3431 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003432 else if (isSigned)
3433 Tmp1 = ExpandIntLibCall(Node, true,
3434 RTLIB::SDIV_I8,
3435 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3436 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3437 else
3438 Tmp1 = ExpandIntLibCall(Node, false,
3439 RTLIB::UDIV_I8,
3440 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3441 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003442 Results.push_back(Tmp1);
3443 break;
3444 }
3445 case ISD::MULHU:
3446 case ISD::MULHS: {
3447 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3448 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003449 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003450 SDVTList VTs = DAG.getVTList(VT, VT);
3451 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3452 "If this wasn't legal, it shouldn't have been created!");
3453 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3454 Node->getOperand(1));
3455 Results.push_back(Tmp1.getValue(1));
3456 break;
3457 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003458 case ISD::SDIVREM:
3459 case ISD::UDIVREM:
3460 // Expand into divrem libcall
3461 ExpandDivRemLibCall(Node, Results);
3462 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003463 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003464 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003465 SDVTList VTs = DAG.getVTList(VT, VT);
3466 // See if multiply or divide can be lowered using two-result operations.
3467 // We just need the low half of the multiply; try both the signed
3468 // and unsigned forms. If the target supports both SMUL_LOHI and
3469 // UMUL_LOHI, form a preference by checking which forms of plain
3470 // MULH it supports.
3471 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3472 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3473 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3474 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3475 unsigned OpToUse = 0;
3476 if (HasSMUL_LOHI && !HasMULHS) {
3477 OpToUse = ISD::SMUL_LOHI;
3478 } else if (HasUMUL_LOHI && !HasMULHU) {
3479 OpToUse = ISD::UMUL_LOHI;
3480 } else if (HasSMUL_LOHI) {
3481 OpToUse = ISD::SMUL_LOHI;
3482 } else if (HasUMUL_LOHI) {
3483 OpToUse = ISD::UMUL_LOHI;
3484 }
3485 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003486 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3487 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003488 break;
3489 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003490 Tmp1 = ExpandIntLibCall(Node, false,
3491 RTLIB::MUL_I8,
3492 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003493 RTLIB::MUL_I64, RTLIB::MUL_I128);
3494 Results.push_back(Tmp1);
3495 break;
3496 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003497 case ISD::SADDO:
3498 case ISD::SSUBO: {
3499 SDValue LHS = Node->getOperand(0);
3500 SDValue RHS = Node->getOperand(1);
3501 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3502 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3503 LHS, RHS);
3504 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003505 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003506
Eli Friedman4bc8c712009-05-27 12:20:41 +00003507 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3508
3509 // LHSSign -> LHS >= 0
3510 // RHSSign -> RHS >= 0
3511 // SumSign -> Sum >= 0
3512 //
3513 // Add:
3514 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3515 // Sub:
3516 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3517 //
3518 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3519 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3520 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3521 Node->getOpcode() == ISD::SADDO ?
3522 ISD::SETEQ : ISD::SETNE);
3523
3524 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3525 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3526
3527 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3528 Results.push_back(Cmp);
3529 break;
3530 }
3531 case ISD::UADDO:
3532 case ISD::USUBO: {
3533 SDValue LHS = Node->getOperand(0);
3534 SDValue RHS = Node->getOperand(1);
3535 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3536 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3537 LHS, RHS);
3538 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003539 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3540 Node->getOpcode () == ISD::UADDO ?
3541 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003542 break;
3543 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003544 case ISD::UMULO:
3545 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003546 EVT VT = Node->getValueType(0);
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003547 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003548 SDValue LHS = Node->getOperand(0);
3549 SDValue RHS = Node->getOperand(1);
3550 SDValue BottomHalf;
3551 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003552 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003553 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3554 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3555 bool isSigned = Node->getOpcode() == ISD::SMULO;
3556 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3557 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3558 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3559 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3560 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3561 RHS);
3562 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003563 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3564 VT.getSizeInBits() * 2))) {
Eli Friedmandb3c1692009-06-16 06:58:29 +00003565 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3566 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3567 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3568 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3569 DAG.getIntPtrConstant(0));
3570 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3571 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003572 } else {
3573 // We can fall back to a libcall with an illegal type for the MUL if we
3574 // have a libcall big enough.
3575 // Also, we can fall back to a division in some cases, but that's a big
3576 // performance hit in the general case.
Eric Christopher38a18262011-01-20 00:29:24 +00003577 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3578 if (WideVT == MVT::i16)
3579 LC = RTLIB::MUL_I16;
3580 else if (WideVT == MVT::i32)
3581 LC = RTLIB::MUL_I32;
3582 else if (WideVT == MVT::i64)
3583 LC = RTLIB::MUL_I64;
3584 else if (WideVT == MVT::i128)
3585 LC = RTLIB::MUL_I128;
3586 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanf316eb72011-05-16 22:09:53 +00003587
3588 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003589 // part.
3590 unsigned LoSize = VT.getSizeInBits();
3591 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
3592 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
3593 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
3594 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
Owen Anderson95771af2011-02-25 21:41:48 +00003595
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003596 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3597 // pre-lowered to the correct types. This all depends upon WideVT not
3598 // being a legal type for the architecture and thus has to be split to
3599 // two arguments.
3600 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3601 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3602 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3603 DAG.getIntPtrConstant(0));
3604 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3605 DAG.getIntPtrConstant(1));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003606 }
Dan Gohmanf316eb72011-05-16 22:09:53 +00003607
Eli Friedmandb3c1692009-06-16 06:58:29 +00003608 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003609 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3610 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003611 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3612 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3613 ISD::SETNE);
3614 } else {
3615 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3616 DAG.getConstant(0, VT), ISD::SETNE);
3617 }
3618 Results.push_back(BottomHalf);
3619 Results.push_back(TopHalf);
3620 break;
3621 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003622 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003623 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003624 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3625 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003626 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003627 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003628 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003629 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003630 break;
3631 }
Eli Friedman509150f2009-05-27 07:58:35 +00003632 case ISD::SELECT:
3633 Tmp1 = Node->getOperand(0);
3634 Tmp2 = Node->getOperand(1);
3635 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003636 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003637 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3638 Tmp2, Tmp3,
3639 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003640 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003641 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3642 DAG.getConstant(0, Tmp1.getValueType()),
3643 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003644 }
Eli Friedman509150f2009-05-27 07:58:35 +00003645 Results.push_back(Tmp1);
3646 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003647 case ISD::BR_JT: {
3648 SDValue Chain = Node->getOperand(0);
3649 SDValue Table = Node->getOperand(1);
3650 SDValue Index = Node->getOperand(2);
3651
Owen Andersone50ed302009-08-10 22:56:29 +00003652 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003653
3654 const TargetData &TD = *TLI.getTargetData();
3655 unsigned EntrySize =
3656 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003657
Chris Lattner071c62f2010-01-25 23:26:13 +00003658 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003659 Index, DAG.getConstant(EntrySize, PTy));
3660 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3661
Owen Anderson23b9b192009-08-12 00:36:31 +00003662 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003663 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003664 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003665 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003666 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003667 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003668 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003669 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003670 // RelocBase can be JumpTable, GOT or some sort of global base.
3671 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3672 TLI.getPICJumpTableRelocBase(Table, DAG));
3673 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003674 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003675 Results.push_back(Tmp1);
3676 break;
3677 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003678 case ISD::BRCOND:
3679 // Expand brcond's setcc into its constituent parts and create a BR_CC
3680 // Node.
3681 Tmp1 = Node->getOperand(0);
3682 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003683 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003684 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003685 Tmp1, Tmp2.getOperand(2),
3686 Tmp2.getOperand(0), Tmp2.getOperand(1),
3687 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003688 } else {
Stuart Hastings88882242011-05-13 00:51:54 +00003689 // We test only the i1 bit. Skip the AND if UNDEF.
3690 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3691 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3692 DAG.getConstant(1, Tmp2.getValueType()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003693 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastings88882242011-05-13 00:51:54 +00003694 DAG.getCondCode(ISD::SETNE), Tmp3,
3695 DAG.getConstant(0, Tmp3.getValueType()),
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003696 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003697 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003698 Results.push_back(Tmp1);
3699 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003700 case ISD::SETCC: {
3701 Tmp1 = Node->getOperand(0);
3702 Tmp2 = Node->getOperand(1);
3703 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003704 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003705
3706 // If we expanded the SETCC into an AND/OR, return the new node
3707 if (Tmp2.getNode() == 0) {
3708 Results.push_back(Tmp1);
3709 break;
3710 }
3711
3712 // Otherwise, SETCC for the given comparison type must be completely
3713 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003714 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003715 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3716 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3717 Results.push_back(Tmp1);
3718 break;
3719 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003720 case ISD::SELECT_CC: {
3721 Tmp1 = Node->getOperand(0); // LHS
3722 Tmp2 = Node->getOperand(1); // RHS
3723 Tmp3 = Node->getOperand(2); // True
3724 Tmp4 = Node->getOperand(3); // False
3725 SDValue CC = Node->getOperand(4);
3726
3727 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003728 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003729
3730 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3731 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3732 CC = DAG.getCondCode(ISD::SETNE);
3733 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3734 Tmp3, Tmp4, CC);
3735 Results.push_back(Tmp1);
3736 break;
3737 }
3738 case ISD::BR_CC: {
3739 Tmp1 = Node->getOperand(0); // Chain
3740 Tmp2 = Node->getOperand(2); // LHS
3741 Tmp3 = Node->getOperand(3); // RHS
3742 Tmp4 = Node->getOperand(1); // CC
3743
3744 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003745 Tmp2, Tmp3, Tmp4, dl);
Stuart Hastings567cac02011-04-19 20:09:38 +00003746 assert(LastCALLSEQ.size() == 1 && "branch inside CALLSEQ_BEGIN/END?");
Stuart Hastingsfc521632011-04-19 16:16:58 +00003747 setLastCALLSEQ(DAG.getEntryNode());
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003748
3749 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3750 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3751 Tmp4 = DAG.getCondCode(ISD::SETNE);
3752 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3753 Tmp3, Node->getOperand(4));
3754 Results.push_back(Tmp1);
3755 break;
3756 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003757 case ISD::GLOBAL_OFFSET_TABLE:
3758 case ISD::GlobalAddress:
3759 case ISD::GlobalTLSAddress:
3760 case ISD::ExternalSymbol:
3761 case ISD::ConstantPool:
3762 case ISD::JumpTable:
3763 case ISD::INTRINSIC_W_CHAIN:
3764 case ISD::INTRINSIC_WO_CHAIN:
3765 case ISD::INTRINSIC_VOID:
3766 // FIXME: Custom lowering for these operations shouldn't return null!
3767 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3768 Results.push_back(SDValue(Node, i));
3769 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003770 }
3771}
3772void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3773 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003774 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003775 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003776 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003777 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003778 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003779 }
Owen Andersone50ed302009-08-10 22:56:29 +00003780 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003781 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003782 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003783 switch (Node->getOpcode()) {
3784 case ISD::CTTZ:
3785 case ISD::CTLZ:
3786 case ISD::CTPOP:
3787 // Zero extend the argument.
3788 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3789 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003790 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003791 if (Node->getOpcode() == ISD::CTTZ) {
3792 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003793 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003794 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3795 ISD::SETEQ);
3796 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3797 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3798 } else if (Node->getOpcode() == ISD::CTLZ) {
3799 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3800 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3801 DAG.getConstant(NVT.getSizeInBits() -
3802 OVT.getSizeInBits(), NVT));
3803 }
Bill Wendling775db972009-12-23 00:28:23 +00003804 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003805 break;
3806 case ISD::BSWAP: {
3807 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003808 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003809 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3810 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003811 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003812 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003813 break;
3814 }
3815 case ISD::FP_TO_UINT:
3816 case ISD::FP_TO_SINT:
3817 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3818 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3819 Results.push_back(Tmp1);
3820 break;
3821 case ISD::UINT_TO_FP:
3822 case ISD::SINT_TO_FP:
3823 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3824 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3825 Results.push_back(Tmp1);
3826 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003827 case ISD::AND:
3828 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003829 case ISD::XOR: {
3830 unsigned ExtOp, TruncOp;
3831 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003832 ExtOp = ISD::BITCAST;
3833 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003834 } else {
3835 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003836 ExtOp = ISD::ANY_EXTEND;
3837 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003838 }
3839 // Promote each of the values to the new type.
3840 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3841 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3842 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003843 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3844 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003845 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003846 }
3847 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003848 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003849 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003850 ExtOp = ISD::BITCAST;
3851 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003852 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003853 ExtOp = ISD::ANY_EXTEND;
3854 TruncOp = ISD::TRUNCATE;
3855 } else {
3856 ExtOp = ISD::FP_EXTEND;
3857 TruncOp = ISD::FP_ROUND;
3858 }
3859 Tmp1 = Node->getOperand(0);
3860 // Promote each of the values to the new type.
3861 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3862 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3863 // Perform the larger operation, then round down.
3864 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3865 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003866 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003867 else
Bill Wendling775db972009-12-23 00:28:23 +00003868 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003869 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003870 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003871 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003872 }
Eli Friedman509150f2009-05-27 07:58:35 +00003873 case ISD::VECTOR_SHUFFLE: {
3874 SmallVector<int, 8> Mask;
3875 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3876
3877 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003878 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3879 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003880
3881 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003882 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003883 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003884 Results.push_back(Tmp1);
3885 break;
3886 }
Eli Friedmanad754602009-05-28 03:56:57 +00003887 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003888 unsigned ExtOp = ISD::FP_EXTEND;
3889 if (NVT.isInteger()) {
3890 ISD::CondCode CCCode =
3891 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3892 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003893 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003894 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3895 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003896 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3897 Tmp1, Tmp2, Node->getOperand(2)));
3898 break;
3899 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003900 }
3901}
3902
Chris Lattner3e928bb2005-01-07 07:47:09 +00003903// SelectionDAG::Legalize - This is the entry point for the file.
3904//
Dan Gohman975716a2011-05-16 22:19:54 +00003905void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003906 /// run - This is the main entry point to this class.
3907 ///
Dan Gohman975716a2011-05-16 22:19:54 +00003908 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003909}