blob: 57d6e8a08d021a714db43c4a79cc2a189150f0f7 [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 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000064 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000065
Dan Gohman475871a2008-07-27 21:46:04 +000066 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000067 LegalizedNodes.insert(std::make_pair(From, To));
68 // If someone requests legalization of the new node, return itself.
69 if (From != To)
70 LegalizedNodes.insert(std::make_pair(To, To));
Owen Anderson95771af2011-02-25 21:41:48 +000071
Devang Patela778f5c2011-02-18 22:43:42 +000072 // Transfer SDDbgValues.
73 DAG.TransferDbgValues(From, To);
Chris Lattner8afc48e2005-01-07 22:28:47 +000074 }
75
Chris Lattner3e928bb2005-01-07 07:47:09 +000076public:
Dan Gohman975716a2011-05-16 22:19:54 +000077 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000078
Chris Lattner3e928bb2005-01-07 07:47:09 +000079 void LegalizeDAG();
80
Chris Lattner456a93a2006-01-28 07:39:30 +000081private:
Dan Gohman6a109f92011-07-15 21:42:20 +000082 /// LegalizeOp - Return a legal replacement for the given operation, with
83 /// all legal operands.
Dan Gohman475871a2008-07-27 21:46:04 +000084 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +000085
Eli Friedman7ef3d172009-06-06 07:04:42 +000086 SDValue OptimizeFloatStore(StoreSDNode *ST);
87
Nate Begeman68679912008-04-25 18:07:40 +000088 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
89 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
90 /// is necessary to spill the vector being inserted into to memory, perform
91 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +000092 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +000093 SDValue Idx, DebugLoc dl);
94 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
95 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +000096
Nate Begeman5a5ca152009-04-29 05:20:52 +000097 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
98 /// performs the same shuffe in terms of order or result bytes, but on a type
99 /// whose vector element type is narrower than the original shuffle type.
100 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +0000101 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +0000102 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000103 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000104
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000105 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000106 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000107
Owen Andersone50ed302009-08-10 22:56:29 +0000108 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +0000109 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000110
Eli Friedman47b41f72009-05-27 02:21:29 +0000111 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Eric Christopherabbbfbd2011-04-20 01:19:45 +0000112 SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
113 unsigned NumOps, bool isSigned, DebugLoc dl);
114
Jim Grosbache03262f2010-06-18 21:43:38 +0000115 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
116 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000117 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
118 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
119 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000120 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
121 RTLIB::Libcall Call_I8,
122 RTLIB::Libcall Call_I16,
123 RTLIB::Libcall Call_I32,
124 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000125 RTLIB::Libcall Call_I128);
Evan Cheng65279cb2011-04-16 03:08:26 +0000126 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattnercad063f2005-07-16 00:19:57 +0000127
Owen Andersone50ed302009-08-10 22:56:29 +0000128 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000129 SDValue ExpandBUILD_VECTOR(SDNode *Node);
130 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000131 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
132 SmallVectorImpl<SDValue> &Results);
133 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000134 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000135 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000136 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000137 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000138 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000139 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000140
Dale Johannesen8a782a22009-02-02 22:12:50 +0000141 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
142 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000143
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000144 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenecfe33c42011-01-26 19:13:22 +0000145 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000146 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000147
Jim Grosbache03262f2010-06-18 21:43:38 +0000148 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
149
Eli Friedman8c377c72009-05-27 01:25:56 +0000150 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
151 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Stuart Hastingsfc521632011-04-19 16:16:58 +0000152
Stuart Hastings567cac02011-04-19 20:09:38 +0000153 SDValue getLastCALLSEQ() { return LastCALLSEQ.back(); }
154 void setLastCALLSEQ(const SDValue s) { LastCALLSEQ.back() = s; }
Stuart Hastingsfc521632011-04-19 16:16:58 +0000155 void pushLastCALLSEQ(SDValue s) {
Stuart Hastingsfc521632011-04-19 16:16:58 +0000156 LastCALLSEQ.push_back(s);
157 }
158 void popLastCALLSEQ() {
159 LastCALLSEQ.pop_back();
Stuart Hastingsfc521632011-04-19 16:16:58 +0000160 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000161};
162}
163
Nate Begeman5a5ca152009-04-29 05:20:52 +0000164/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
165/// performs the same shuffe in terms of order or result bytes, but on a type
166/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000167/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000168SDValue
169SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000170 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000171 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000172 unsigned NumMaskElts = VT.getVectorNumElements();
173 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000174 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000175
Nate Begeman9008ca62009-04-27 18:41:29 +0000176 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
177
178 if (NumEltsGrowth == 1)
179 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000180
Nate Begeman9008ca62009-04-27 18:41:29 +0000181 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000182 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000183 int Idx = Mask[i];
184 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000185 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000186 NewMask.push_back(-1);
187 else
188 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000189 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000190 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000191 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000192 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
193 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000194}
195
Dan Gohman975716a2011-05-16 22:19:54 +0000196SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
Dan Gohman55e59c12010-04-19 19:05:59 +0000197 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
Dan Gohmanea027022011-07-15 22:19:02 +0000198 DAG(dag) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000199}
200
Chris Lattner3e928bb2005-01-07 07:47:09 +0000201void SelectionDAGLegalize::LegalizeDAG() {
Stuart Hastingsfc521632011-04-19 16:16:58 +0000202 pushLastCALLSEQ(DAG.getEntryNode());
Mon P Wange5ab34e2009-02-04 19:38:14 +0000203
Chris Lattnerab510a72005-10-02 17:49:46 +0000204 // The legalize process is inherently a bottom-up recursive process (users
205 // legalize their uses before themselves). Given infinite stack space, we
206 // could just start legalizing on the root and traverse the whole graph. In
207 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000208 // blocks. To avoid this problem, compute an ordering of the nodes where each
209 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000210 DAG.AssignTopologicalOrder();
211 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +0000212 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000213 LegalizeOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000214
215 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000216 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000217 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
218 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000219
Chris Lattner3e928bb2005-01-07 07:47:09 +0000220 LegalizedNodes.clear();
221
222 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000223 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000224}
225
Chris Lattner6831a812006-02-13 09:18:02 +0000226
227/// FindCallEndFromCallStart - Given a chained node that is part of a call
228/// sequence, find the CALLSEQ_END node that terminates the call sequence.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000229static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) {
Stuart Hastingsfc521632011-04-19 16:16:58 +0000230 int next_depth = depth;
Stuart Hastingsa304d022010-12-09 21:25:20 +0000231 if (Node->getOpcode() == ISD::CALLSEQ_START)
Stuart Hastingsfc521632011-04-19 16:16:58 +0000232 next_depth = depth + 1;
233 if (Node->getOpcode() == ISD::CALLSEQ_END) {
234 assert(depth > 0 && "negative depth!");
235 if (depth == 1)
Stuart Hastings56500ed2010-12-21 17:16:58 +0000236 return Node;
Stuart Hastingsfc521632011-04-19 16:16:58 +0000237 else
238 next_depth = depth - 1;
Stuart Hastings56500ed2010-12-21 17:16:58 +0000239 }
Chris Lattner6831a812006-02-13 09:18:02 +0000240 if (Node->use_empty())
241 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000242
Chris Lattner6831a812006-02-13 09:18:02 +0000243 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000244 SDValue TheChain(Node, Node->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000246 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000247 TheChain = SDValue(Node, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000249 // Otherwise, hunt for it.
250 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000252 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000253 break;
254 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000255
256 // Otherwise, we walked into a node without a chain.
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 if (TheChain.getValueType() != MVT::Other)
Chris Lattner6831a812006-02-13 09:18:02 +0000258 return 0;
259 }
260 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000261
Chris Lattner6831a812006-02-13 09:18:02 +0000262 for (SDNode::use_iterator UI = Node->use_begin(),
263 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000264
Chris Lattner6831a812006-02-13 09:18:02 +0000265 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000266 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000267 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
268 if (User->getOperand(i) == TheChain)
Stuart Hastingsfc521632011-04-19 16:16:58 +0000269 if (SDNode *Result = FindCallEndFromCallStart(User, next_depth))
Chris Lattner6831a812006-02-13 09:18:02 +0000270 return Result;
271 }
272 return 0;
273}
274
Scott Michelfdc40a02009-02-17 22:15:04 +0000275/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000276/// sequence, find the CALLSEQ_START node that initiates the call sequence.
277static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
Stuart Hastingsa304d022010-12-09 21:25:20 +0000278 int nested = 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000279 assert(Node && "Didn't find callseq_start for a call??");
Stuart Hastingsa304d022010-12-09 21:25:20 +0000280 while (Node->getOpcode() != ISD::CALLSEQ_START || nested) {
281 Node = Node->getOperand(0).getNode();
282 assert(Node->getOperand(0).getValueType() == MVT::Other &&
283 "Node doesn't have a token chain argument!");
284 switch (Node->getOpcode()) {
285 default:
286 break;
287 case ISD::CALLSEQ_START:
288 if (!nested)
289 return Node;
Stuart Hastingsd6730572011-05-10 21:20:03 +0000290 Node = Node->getOperand(0).getNode();
Stuart Hastingsa304d022010-12-09 21:25:20 +0000291 nested--;
292 break;
293 case ISD::CALLSEQ_END:
294 nested++;
295 break;
296 }
297 }
Stuart Hastingsd6730572011-05-10 21:20:03 +0000298 return (Node->getOpcode() == ISD::CALLSEQ_START) ? Node : 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000299}
300
301/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000302/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000303/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000304///
305/// Keep track of the nodes we fine that actually do lead to Dest in
306/// NodesLeadingTo. This avoids retraversing them exponential number of times.
307///
308bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000309 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000310 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000311
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000312 // If we've already processed this node and it does lead to Dest, there is no
313 // need to reprocess it.
314 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Chris Lattner6831a812006-02-13 09:18:02 +0000316 // If the first result of this node has been already legalized, then it cannot
317 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000318 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000319
Chris Lattner6831a812006-02-13 09:18:02 +0000320 // Okay, this node has not already been legalized. Check and legalize all
321 // operands. If none lead to Dest, then we can legalize this node.
322 bool OperandsLeadToDest = false;
323 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
324 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Jim Grosbach6e992612010-07-02 17:41:59 +0000325 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest,
326 NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000327
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000328 if (OperandsLeadToDest) {
329 NodesLeadingTo.insert(N);
330 return true;
331 }
Chris Lattner6831a812006-02-13 09:18:02 +0000332
333 // Okay, this node looks safe, legalize it and return false.
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000334 LegalizeOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000335 return false;
336}
337
Evan Cheng9f877882006-12-13 20:57:08 +0000338/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
339/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000340static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000341 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000342 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000343 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000344
345 // If a FP immediate is precise when represented as a float and if the
346 // target can do an extending load from float to double, we put it into
347 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000348 // double. This shrinks FP constants and canonicalizes them for targets where
349 // an FP extending load is the same cost as a normal load (such as on the x87
350 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000351 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000352 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000353 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000355 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000357 }
358
Owen Andersone50ed302009-08-10 22:56:29 +0000359 EVT OrigVT = VT;
360 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 while (SVT != MVT::f32) {
362 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000363 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000364 // Only do this if the target has a native EXTLOAD instruction from
365 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000366 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000367 TLI.ShouldShrinkFPConstant(OrigVT)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000368 Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000369 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000370 VT = SVT;
371 Extend = true;
372 }
Evan Cheng00495212006-12-12 21:32:44 +0000373 }
374
Dan Gohman475871a2008-07-27 21:46:04 +0000375 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000376 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000377 if (Extend)
Stuart Hastingsa9011292011-02-16 16:23:55 +0000378 return DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
Evan Chengbcc80172010-07-07 22:15:37 +0000379 DAG.getEntryNode(),
Chris Lattner85ca1062010-09-21 07:32:19 +0000380 CPIdx, MachinePointerInfo::getConstantPool(),
381 VT, false, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000382 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +0000383 MachinePointerInfo::getConstantPool(), false, false,
David Greene1e559442010-02-15 17:00:31 +0000384 Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000385}
386
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000387/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
388static
Dan Gohman475871a2008-07-27 21:46:04 +0000389SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000390 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000391 SDValue Chain = ST->getChain();
392 SDValue Ptr = ST->getBasePtr();
393 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000394 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000395 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000396 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000397 if (ST->getMemoryVT().isFloatingPoint() ||
398 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000399 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000400 if (TLI.isTypeLegal(intVT)) {
401 // Expand to a bitconvert of the value to the integer type of the
402 // same size, then a (misaligned) int store.
403 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000404 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000405 return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
406 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000407 }
Dan Gohman1b328962011-05-17 22:22:52 +0000408 // Do a (aligned) store to a stack slot, then copy from the stack slot
409 // to the final destination using (unaligned) integer loads and stores.
410 EVT StoredVT = ST->getMemoryVT();
411 EVT RegVT =
412 TLI.getRegisterType(*DAG.getContext(),
413 EVT::getIntegerVT(*DAG.getContext(),
414 StoredVT.getSizeInBits()));
415 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
416 unsigned RegBytes = RegVT.getSizeInBits() / 8;
417 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
418
419 // Make sure the stack slot is also aligned for the register type.
420 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
421
422 // Perform the original store, only redirected to the stack slot.
423 SDValue Store = DAG.getTruncStore(Chain, dl,
424 Val, StackPtr, MachinePointerInfo(),
425 StoredVT, false, false, 0);
426 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
427 SmallVector<SDValue, 8> Stores;
428 unsigned Offset = 0;
429
430 // Do all but one copies using the full register width.
431 for (unsigned i = 1; i < NumRegs; i++) {
432 // Load one integer register's worth from the stack slot.
433 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
434 MachinePointerInfo(),
435 false, false, 0);
436 // Store it to the final location. Remember the store.
437 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
438 ST->getPointerInfo().getWithOffset(Offset),
439 ST->isVolatile(), ST->isNonTemporal(),
440 MinAlign(ST->getAlignment(), Offset)));
441 // Increment the pointers.
442 Offset += RegBytes;
443 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
444 Increment);
445 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
446 }
447
448 // The last store may be partial. Do a truncating store. On big-endian
449 // machines this requires an extending load from the stack slot to ensure
450 // that the bits are in the right place.
451 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
452 8 * (StoredBytes - Offset));
453
454 // Load from the stack slot.
455 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
456 MachinePointerInfo(),
457 MemVT, false, false, 0);
458
459 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
460 ST->getPointerInfo()
461 .getWithOffset(Offset),
462 MemVT, ST->isVolatile(),
463 ST->isNonTemporal(),
464 MinAlign(ST->getAlignment(), Offset)));
465 // The order of the stores doesn't matter - say it with a TokenFactor.
466 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
467 Stores.size());
Dale Johannesen907f28c2007-09-08 19:29:23 +0000468 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000469 assert(ST->getMemoryVT().isInteger() &&
470 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000471 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000472 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000473 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000474 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000475 int IncrementSize = NumBits / 8;
476
477 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000478 SDValue ShiftAmount = DAG.getConstant(NumBits,
479 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000480 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000481 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000482
483 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000484 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000485 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000486 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000487 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000488 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000489 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000490 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000491 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000492 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000493 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
494 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000495
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000497}
498
499/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
500static
Dan Gohman475871a2008-07-27 21:46:04 +0000501SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000502 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000503 SDValue Chain = LD->getChain();
504 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000505 EVT VT = LD->getValueType(0);
506 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000507 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000508 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000509 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000510 if (TLI.isTypeLegal(intVT)) {
511 // Expand to a (misaligned) integer load of the same size,
512 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000513 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
514 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000515 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000516 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000517 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000518 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000519
Duncan Sands05e11fa2008-12-12 21:47:02 +0000520 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000521 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000522 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000523
Chris Lattnerecf42c42010-09-21 16:36:31 +0000524 // Copy the value to a (aligned) stack slot using (unaligned) integer
525 // loads and stores, then do a (aligned) load from the stack slot.
526 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
527 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
528 unsigned RegBytes = RegVT.getSizeInBits() / 8;
529 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
530
531 // Make sure the stack slot is also aligned for the register type.
532 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
533
534 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
535 SmallVector<SDValue, 8> Stores;
536 SDValue StackPtr = StackBase;
537 unsigned Offset = 0;
538
539 // Do all but one copies using the full register width.
540 for (unsigned i = 1; i < NumRegs; i++) {
541 // Load one integer register's worth from the original location.
542 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
543 LD->getPointerInfo().getWithOffset(Offset),
544 LD->isVolatile(), LD->isNonTemporal(),
545 MinAlign(LD->getAlignment(), Offset));
546 // Follow the load with a store to the stack slot. Remember the store.
547 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000548 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000549 // Increment the pointers.
550 Offset += RegBytes;
551 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
552 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
553 Increment);
554 }
555
556 // The last copy may be partial. Do an extending load.
557 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
558 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000559 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000560 LD->getPointerInfo().getWithOffset(Offset),
561 MemVT, LD->isVolatile(),
562 LD->isNonTemporal(),
563 MinAlign(LD->getAlignment(), Offset));
564 // Follow the load with a store to the stack slot. Remember the store.
565 // On big-endian machines this requires a truncating store to ensure
566 // that the bits end up in the right place.
567 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
568 MachinePointerInfo(), MemVT,
569 false, false, 0));
570
571 // The order of the stores doesn't matter - say it with a TokenFactor.
572 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
573 Stores.size());
574
575 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000576 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000577 MachinePointerInfo(), LoadedVT, false, false, 0);
578
579 // Callers expect a MERGE_VALUES node.
580 SDValue Ops[] = { Load, TF };
581 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000582 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000583 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000584 "Unaligned load of unsupported type.");
585
Dale Johannesen8155d642008-02-27 22:36:00 +0000586 // Compute the new VT that is half the size of the old one. This is an
587 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000588 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000589 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000590 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000591 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000592
Chris Lattnere400af82007-11-19 21:38:03 +0000593 unsigned Alignment = LD->getAlignment();
594 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000595 ISD::LoadExtType HiExtType = LD->getExtensionType();
596
597 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
598 if (HiExtType == ISD::NON_EXTLOAD)
599 HiExtType = ISD::ZEXTLOAD;
600
601 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000602 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000603 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000604 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000605 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000606 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000607 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000608 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000609 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000610 LD->getPointerInfo().getWithOffset(IncrementSize),
611 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000612 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000613 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000614 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000615 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000616 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000617 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000619 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000620 LD->getPointerInfo().getWithOffset(IncrementSize),
621 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000622 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000623 }
624
625 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000626 SDValue ShiftAmount = DAG.getConstant(NumBits,
627 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000628 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
629 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000630
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000632 Hi.getValue(1));
633
Dan Gohman475871a2008-07-27 21:46:04 +0000634 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000635 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000636}
Evan Cheng912095b2007-01-04 21:56:39 +0000637
Nate Begeman68679912008-04-25 18:07:40 +0000638/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
639/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
640/// is necessary to spill the vector being inserted into to memory, perform
641/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000642SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000643PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
644 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000645 SDValue Tmp1 = Vec;
646 SDValue Tmp2 = Val;
647 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000648
Nate Begeman68679912008-04-25 18:07:40 +0000649 // If the target doesn't support this, we have to spill the input vector
650 // to a temporary stack slot, update the element, then reload it. This is
651 // badness. We could also load the value into a vector register (either
652 // with a "move to register" or "extload into register" instruction, then
653 // permute it into place, if the idx is a constant and if the idx is
654 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000655 EVT VT = Tmp1.getValueType();
656 EVT EltVT = VT.getVectorElementType();
657 EVT IdxVT = Tmp3.getValueType();
658 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000659 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000660
Evan Chengff89dcb2009-10-18 18:16:27 +0000661 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
662
Nate Begeman68679912008-04-25 18:07:40 +0000663 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000664 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000665 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000666 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000667
668 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000669 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000670 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000671 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000672 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000673 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
674 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000675 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000676 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000677 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000678 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000679 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000680 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000681}
682
Mon P Wange9f10152008-12-09 05:46:39 +0000683
Eli Friedman3f727d62009-05-27 02:16:40 +0000684SDValue SelectionDAGLegalize::
685ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
686 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
687 // SCALAR_TO_VECTOR requires that the type of the value being inserted
688 // match the element type of the vector being created, except for
689 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000690 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000691 if (Val.getValueType() == EltVT ||
692 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
693 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
694 Vec.getValueType(), Val);
695
696 unsigned NumElts = Vec.getValueType().getVectorNumElements();
697 // We generate a shuffle of InVec and ScVec, so the shuffle mask
698 // should be 0,1,2,3,4,5... with the appropriate element replaced with
699 // elt 0 of the RHS.
700 SmallVector<int, 8> ShufOps;
701 for (unsigned i = 0; i != NumElts; ++i)
702 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
703
704 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
705 &ShufOps[0]);
706 }
707 }
708 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
709}
710
Eli Friedman7ef3d172009-06-06 07:04:42 +0000711SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
712 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
713 // FIXME: We shouldn't do this for TargetConstantFP's.
714 // FIXME: move this to the DAG Combiner! Note that we can't regress due
715 // to phase ordering between legalized code and the dag combiner. This
716 // probably means that we need to integrate dag combiner and legalizer
717 // together.
718 // We generally can't do this one for long doubles.
719 SDValue Tmp1 = ST->getChain();
720 SDValue Tmp2 = ST->getBasePtr();
721 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000722 unsigned Alignment = ST->getAlignment();
723 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000724 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000725 DebugLoc dl = ST->getDebugLoc();
726 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 if (CFP->getValueType(0) == MVT::f32 &&
Dan Gohman75b10042011-07-15 22:39:09 +0000728 TLI.isTypeLegal(MVT::i32)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000729 Tmp3 = DAG.getConstant(CFP->getValueAPF().
730 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000732 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
733 isVolatile, isNonTemporal, Alignment);
734 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000735
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000736 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000737 // If this target supports 64-bit registers, do a single 64-bit store.
Dan Gohman75b10042011-07-15 22:39:09 +0000738 if (TLI.isTypeLegal(MVT::i64)) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000739 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000741 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
742 isVolatile, isNonTemporal, Alignment);
743 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000744
Dan Gohman75b10042011-07-15 22:39:09 +0000745 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000746 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
747 // stores. If the target supports neither 32- nor 64-bits, this
748 // xform is certainly not worth it.
749 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000750 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000752 if (TLI.isBigEndian()) std::swap(Lo, Hi);
753
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000754 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
755 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000756 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
757 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000758 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
759 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000760 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000761
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000763 }
764 }
765 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000766 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000767}
768
Dan Gohman6a109f92011-07-15 21:42:20 +0000769/// LegalizeOp - Return a legal replacement for the given operation, with
770/// all legal operands.
Dan Gohman475871a2008-07-27 21:46:04 +0000771SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000772 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
773 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000774
Gabor Greifba36cb52008-08-28 21:40:38 +0000775 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000776 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000777
Eli Friedman1fde9c52009-05-24 02:46:31 +0000778 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000779 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
780 TargetLowering::TypeLegal &&
Eli Friedman1fde9c52009-05-24 02:46:31 +0000781 "Unexpected illegal type!");
782
783 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Dan Gohman75b10042011-07-15 22:39:09 +0000784 assert((TLI.getTypeAction(*DAG.getContext(),
785 Node->getOperand(i).getValueType()) ==
786 TargetLowering::TypeLegal ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000787 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
788 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000789
Chris Lattner45982da2005-05-12 16:53:42 +0000790 // Note that LegalizeOp may be reentered even from single-use nodes, which
791 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000792 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000793 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000794
Dan Gohman475871a2008-07-27 21:46:04 +0000795 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
796 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000797 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000798
Eli Friedman8c377c72009-05-27 01:25:56 +0000799 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000800 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000801 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000802 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000803 case ISD::INTRINSIC_W_CHAIN:
804 case ISD::INTRINSIC_WO_CHAIN:
805 case ISD::INTRINSIC_VOID:
806 case ISD::VAARG:
807 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000809 break;
810 case ISD::SINT_TO_FP:
811 case ISD::UINT_TO_FP:
812 case ISD::EXTRACT_VECTOR_ELT:
813 Action = TLI.getOperationAction(Node->getOpcode(),
814 Node->getOperand(0).getValueType());
815 break;
816 case ISD::FP_ROUND_INREG:
817 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000818 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000819 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
820 break;
821 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000822 case ISD::SELECT_CC:
823 case ISD::SETCC:
824 case ISD::BR_CC: {
825 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
826 Node->getOpcode() == ISD::SETCC ? 2 : 1;
827 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000828 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000829 ISD::CondCode CCCode =
830 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
831 Action = TLI.getCondCodeAction(CCCode, OpVT);
832 if (Action == TargetLowering::Legal) {
833 if (Node->getOpcode() == ISD::SELECT_CC)
834 Action = TLI.getOperationAction(Node->getOpcode(),
835 Node->getValueType(0));
836 else
837 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
838 }
839 break;
840 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000841 case ISD::LOAD:
842 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000843 // FIXME: Model these properly. LOAD and STORE are complicated, and
844 // STORE expects the unlegalized operand in some cases.
845 SimpleFinishLegalizing = false;
846 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000847 case ISD::CALLSEQ_START:
848 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000849 // FIXME: This shouldn't be necessary. These nodes have special properties
850 // dealing with the recursive nature of legalization. Removing this
851 // special case should be done as part of making LegalizeDAG non-recursive.
852 SimpleFinishLegalizing = false;
853 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000854 case ISD::EXTRACT_ELEMENT:
855 case ISD::FLT_ROUNDS_:
856 case ISD::SADDO:
857 case ISD::SSUBO:
858 case ISD::UADDO:
859 case ISD::USUBO:
860 case ISD::SMULO:
861 case ISD::UMULO:
862 case ISD::FPOWI:
863 case ISD::MERGE_VALUES:
864 case ISD::EH_RETURN:
865 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000866 case ISD::EH_SJLJ_SETJMP:
867 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000868 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000869 // These operations lie about being legal: when they claim to be legal,
870 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000871 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
872 if (Action == TargetLowering::Legal)
873 Action = TargetLowering::Expand;
874 break;
875 case ISD::TRAMPOLINE:
876 case ISD::FRAMEADDR:
877 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000878 // These operations lie about being legal: when they claim to be legal,
879 // they should actually be custom-lowered.
880 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
881 if (Action == TargetLowering::Legal)
882 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000883 break;
884 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000885 // A weird case: legalization for BUILD_VECTOR never legalizes the
886 // operands!
887 // FIXME: This really sucks... changing it isn't semantically incorrect,
888 // but it massively pessimizes the code for floating-point BUILD_VECTORs
889 // because ConstantFP operands get legalized into constant pool loads
890 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
891 // though, because BUILD_VECTORS usually get lowered into other nodes
892 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000893 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000894 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000895 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000896 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000897 Action = TargetLowering::Legal;
898 } else {
899 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000900 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000901 break;
902 }
903
904 if (SimpleFinishLegalizing) {
905 SmallVector<SDValue, 8> Ops, ResultVals;
906 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
907 Ops.push_back(LegalizeOp(Node->getOperand(i)));
908 switch (Node->getOpcode()) {
909 default: break;
910 case ISD::BR:
911 case ISD::BRIND:
912 case ISD::BR_JT:
913 case ISD::BR_CC:
914 case ISD::BRCOND:
Eli Friedman8c377c72009-05-27 01:25:56 +0000915 Ops[0] = LegalizeOp(Ops[0]);
Eli Friedman8c377c72009-05-27 01:25:56 +0000916 break;
917 case ISD::SHL:
918 case ISD::SRL:
919 case ISD::SRA:
920 case ISD::ROTL:
921 case ISD::ROTR:
922 // Legalizing shifts/rotates requires adjusting the shift amount
923 // to the appropriate width.
924 if (!Ops[1].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000925 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
926 Ops[1]));
Eli Friedman8c377c72009-05-27 01:25:56 +0000927 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000928 case ISD::SRL_PARTS:
929 case ISD::SRA_PARTS:
930 case ISD::SHL_PARTS:
931 // Legalizing shifts/rotates requires adjusting the shift amount
932 // to the appropriate width.
933 if (!Ops[2].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000934 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
935 Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000936 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000937 }
938
Dan Gohman027657d2010-06-18 15:30:29 +0000939 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
940 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000941 switch (Action) {
942 case TargetLowering::Legal:
943 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
944 ResultVals.push_back(Result.getValue(i));
945 break;
946 case TargetLowering::Custom:
947 // FIXME: The handling for custom lowering with multiple results is
948 // a complete mess.
949 Tmp1 = TLI.LowerOperation(Result, DAG);
950 if (Tmp1.getNode()) {
951 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
952 if (e == 1)
953 ResultVals.push_back(Tmp1);
954 else
955 ResultVals.push_back(Tmp1.getValue(i));
956 }
957 break;
958 }
959
960 // FALL THROUGH
961 case TargetLowering::Expand:
962 ExpandNode(Result.getNode(), ResultVals);
963 break;
964 case TargetLowering::Promote:
965 PromoteNode(Result.getNode(), ResultVals);
966 break;
967 }
968 if (!ResultVals.empty()) {
969 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
970 if (ResultVals[i] != SDValue(Node, i))
971 ResultVals[i] = LegalizeOp(ResultVals[i]);
972 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
973 }
974 return ResultVals[Op.getResNo()];
975 }
976 }
977
978 switch (Node->getOpcode()) {
979 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000980#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +0000981 dbgs() << "NODE: ";
982 Node->dump( &DAG);
983 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000984#endif
Chris Lattner35a38932010-04-07 23:47:51 +0000985 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000986
Chris Lattnerb2827b02006-03-19 00:52:58 +0000987 case ISD::BUILD_VECTOR:
988 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +0000989 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +0000990 case TargetLowering::Custom:
991 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000992 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000993 Result = Tmp3;
994 break;
995 }
996 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000997 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +0000998 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +0000999 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001000 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001001 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001002 case ISD::CALLSEQ_START: {
1003 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Stuart Hastingsfc521632011-04-19 16:16:58 +00001004 assert(CallEnd && "didn't find CALLSEQ_END!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001005
Chris Lattner6831a812006-02-13 09:18:02 +00001006 // Recursively Legalize all of the inputs of the call end that do not lead
1007 // to this call start. This ensures that any libcalls that need be inserted
1008 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001009 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001010 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001011 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001012 NodesLeadingTo);
1013 }
Chris Lattner6831a812006-02-13 09:18:02 +00001014
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001015 // Now that we have legalized all of the inputs (which may have inserted
1016 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001017 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1018
Chris Lattner6831a812006-02-13 09:18:02 +00001019 // Do not try to legalize the target-specific arguments (#1+).
1020 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001021 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001022 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001023 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1024 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001025 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001026
Chris Lattner6831a812006-02-13 09:18:02 +00001027 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001028 AddLegalizedOperand(Op.getValue(0), Result);
1029 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1030 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
Chris Lattner6831a812006-02-13 09:18:02 +00001032 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001033 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001034 // process, no libcalls can/will be inserted, guaranteeing that no calls
1035 // can overlap.
Chris Lattner6831a812006-02-13 09:18:02 +00001036 // Note that we are selecting this call!
Stuart Hastingsfc521632011-04-19 16:16:58 +00001037 setLastCALLSEQ(SDValue(CallEnd, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001038
Chris Lattner6831a812006-02-13 09:18:02 +00001039 // Legalize the call, starting from the CALLSEQ_END.
Dan Gohmanc680b922011-07-26 22:00:59 +00001040 LegalizeOp(SDValue(CallEnd, 0));
Chris Lattner6831a812006-02-13 09:18:02 +00001041 return Result;
1042 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001043 case ISD::CALLSEQ_END:
Stuart Hastingsfc521632011-04-19 16:16:58 +00001044 {
1045 SDNode *myCALLSEQ_BEGIN = FindCallStartFromCallEnd(Node);
1046
Dan Gohmanf316eb72011-05-16 22:09:53 +00001047 // If the CALLSEQ_START node hasn't been legalized first, legalize it.
1048 // This will cause this node to be legalized as well as handling libcalls
1049 // right.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001050 if (getLastCALLSEQ().getNode() != Node) {
1051 LegalizeOp(SDValue(myCALLSEQ_BEGIN, 0));
1052 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
1053 assert(I != LegalizedNodes.end() &&
1054 "Legalizing the call start should have legalized this node!");
1055 return I->second;
1056 }
1057
1058 pushLastCALLSEQ(SDValue(myCALLSEQ_BEGIN, 0));
Chris Lattner6831a812006-02-13 09:18:02 +00001059 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
1061 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001062 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001063 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001064 // Do not try to legalize the target-specific arguments (#1+), except for
1065 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001066 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001067 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001068 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001069 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001070 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1071 &Ops[0], Ops.size()),
1072 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001073 }
1074 } else {
1075 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1076 if (Tmp1 != Node->getOperand(0) ||
1077 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001078 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001079 Ops[0] = Tmp1;
1080 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001081 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1082 &Ops[0], Ops.size()),
1083 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001084 }
Chris Lattner6a542892006-01-24 05:48:21 +00001085 }
Chris Lattner6831a812006-02-13 09:18:02 +00001086 // This finishes up call legalization.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001087 popLastCALLSEQ();
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001088
Chris Lattner4b653a02006-02-14 00:55:02 +00001089 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001090 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001091 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001092 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001093 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001094 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001095 LoadSDNode *LD = cast<LoadSDNode>(Node);
1096 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1097 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001098
Evan Cheng466685d2006-10-09 20:57:25 +00001099 ISD::LoadExtType ExtType = LD->getExtensionType();
1100 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001101 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001102 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1103 Tmp1, Tmp2, LD->getOffset()),
1104 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001105 Tmp3 = Result.getValue(0);
1106 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001107
Evan Cheng466685d2006-10-09 20:57:25 +00001108 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001109 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001110 case TargetLowering::Legal:
1111 // If this is an unaligned load and the target doesn't support it,
1112 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001113 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001114 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
Evan Chenge96507c2009-08-15 08:38:52 +00001115 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001116 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001117 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001118 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001119 Tmp3 = Result.getOperand(0);
1120 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001121 Tmp3 = LegalizeOp(Tmp3);
1122 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001123 }
1124 }
1125 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001126 case TargetLowering::Custom:
1127 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001128 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001129 Tmp3 = LegalizeOp(Tmp1);
1130 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001131 }
Evan Cheng466685d2006-10-09 20:57:25 +00001132 break;
1133 case TargetLowering::Promote: {
1134 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001135 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001136 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001137 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001138
Chris Lattnerecf42c42010-09-21 16:36:31 +00001139 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001140 LD->isVolatile(), LD->isNonTemporal(),
1141 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001142 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001143 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001144 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001145 }
Evan Cheng466685d2006-10-09 20:57:25 +00001146 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001147 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001148 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001149 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1150 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001151 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001152 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001153
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001154 EVT SrcVT = LD->getMemoryVT();
1155 unsigned SrcWidth = SrcVT.getSizeInBits();
1156 unsigned Alignment = LD->getAlignment();
1157 bool isVolatile = LD->isVolatile();
1158 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001159
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001160 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1161 // Some targets pretend to have an i1 loading operation, and actually
1162 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1163 // bits are guaranteed to be zero; it helps the optimizers understand
1164 // that these bits are zero. It is also useful for EXTLOAD, since it
1165 // tells the optimizers that those bits are undefined. It would be
1166 // nice to have an effective generic way of getting these benefits...
1167 // Until such a way is found, don't insist on promoting i1 here.
1168 (SrcVT != MVT::i1 ||
1169 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1170 // Promote to a byte-sized load if not loading an integral number of
1171 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1172 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1173 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1174 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001175
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001176 // The extra bits are guaranteed to be zero, since we stored them that
1177 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001178
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001179 ISD::LoadExtType NewExtType =
1180 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001181
Stuart Hastingsa9011292011-02-16 16:23:55 +00001182 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001183 Tmp1, Tmp2, LD->getPointerInfo(),
1184 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001185
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001186 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001187
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001188 if (ExtType == ISD::SEXTLOAD)
1189 // Having the top bits zero doesn't help when sign extending.
1190 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1191 Result.getValueType(),
1192 Result, DAG.getValueType(SrcVT));
1193 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1194 // All the top bits are guaranteed to be zero - inform the optimizers.
1195 Result = DAG.getNode(ISD::AssertZext, dl,
1196 Result.getValueType(), Result,
1197 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001198
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001199 Tmp1 = LegalizeOp(Result);
1200 Tmp2 = LegalizeOp(Ch);
1201 } else if (SrcWidth & (SrcWidth - 1)) {
1202 // If not loading a power-of-2 number of bits, expand as two loads.
1203 assert(!SrcVT.isVector() && "Unsupported extload!");
1204 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1205 assert(RoundWidth < SrcWidth);
1206 unsigned ExtraWidth = SrcWidth - RoundWidth;
1207 assert(ExtraWidth < RoundWidth);
1208 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1209 "Load size not an integral number of bytes!");
1210 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1211 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1212 SDValue Lo, Hi, Ch;
1213 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001214
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001215 if (TLI.isLittleEndian()) {
1216 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1217 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001218 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001219 Tmp1, Tmp2,
1220 LD->getPointerInfo(), RoundVT, isVolatile,
1221 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001222
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001223 // Load the remaining ExtraWidth bits.
1224 IncrementSize = RoundWidth / 8;
1225 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1226 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001227 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001228 LD->getPointerInfo().getWithOffset(IncrementSize),
1229 ExtraVT, isVolatile, isNonTemporal,
1230 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001231
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001232 // Build a factor node to remember that this load is independent of
1233 // the other one.
1234 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1235 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001236
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001237 // Move the top bits to the right place.
1238 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001239 DAG.getConstant(RoundWidth,
1240 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001241
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001242 // Join the hi and lo parts.
1243 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001244 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001245 // Big endian - avoid unaligned loads.
1246 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1247 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001248 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001249 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));
1256 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001257 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001258 LD->getPointerInfo().getWithOffset(IncrementSize),
1259 ExtraVT, isVolatile, isNonTemporal,
1260 MinAlign(Alignment, IncrementSize));
1261
1262 // Build a factor node to remember that this load is independent of
1263 // the other one.
1264 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1265 Hi.getValue(1));
1266
1267 // Move the top bits to the right place.
1268 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001269 DAG.getConstant(ExtraWidth,
1270 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001271
1272 // Join the hi and lo parts.
1273 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001274 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001275
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001276 Tmp1 = LegalizeOp(Result);
1277 Tmp2 = LegalizeOp(Ch);
1278 } else {
1279 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1280 default: assert(0 && "This action is not supported yet!");
1281 case TargetLowering::Custom:
1282 isCustom = true;
1283 // FALLTHROUGH
1284 case TargetLowering::Legal:
1285 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1286 Tmp1, Tmp2, LD->getOffset()),
1287 Result.getResNo());
1288 Tmp1 = Result.getValue(0);
1289 Tmp2 = Result.getValue(1);
1290
1291 if (isCustom) {
1292 Tmp3 = TLI.LowerOperation(Result, DAG);
1293 if (Tmp3.getNode()) {
1294 Tmp1 = LegalizeOp(Tmp3);
1295 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1296 }
1297 } else {
1298 // If this is an unaligned load and the target doesn't support it,
1299 // expand it.
1300 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001301 Type *Ty =
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001302 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1303 unsigned ABIAlignment =
1304 TLI.getTargetData()->getABITypeAlignment(Ty);
1305 if (LD->getAlignment() < ABIAlignment){
1306 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1307 DAG, TLI);
1308 Tmp1 = Result.getOperand(0);
1309 Tmp2 = Result.getOperand(1);
1310 Tmp1 = LegalizeOp(Tmp1);
1311 Tmp2 = LegalizeOp(Tmp2);
1312 }
1313 }
1314 }
1315 break;
1316 case TargetLowering::Expand:
Dan Gohman75b10042011-07-15 22:39:09 +00001317 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001318 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1319 LD->getPointerInfo(),
1320 LD->isVolatile(), LD->isNonTemporal(),
1321 LD->getAlignment());
1322 unsigned ExtendOp;
1323 switch (ExtType) {
1324 case ISD::EXTLOAD:
1325 ExtendOp = (SrcVT.isFloatingPoint() ?
1326 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1327 break;
1328 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1329 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1330 default: llvm_unreachable("Unexpected extend load type!");
1331 }
1332 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1333 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1334 Tmp2 = LegalizeOp(Load.getValue(1));
1335 break;
1336 }
Nadav Rotemc2492c22011-06-14 08:11:52 +00001337
1338 // If this is a promoted vector load, and the vector element types are
1339 // legal, then scalarize it.
1340 if (ExtType == ISD::EXTLOAD && SrcVT.isVector() &&
Dan Gohman75b10042011-07-15 22:39:09 +00001341 TLI.isTypeLegal(Node->getValueType(0).getScalarType())) {
Nadav Rotemc2492c22011-06-14 08:11:52 +00001342 SmallVector<SDValue, 8> LoadVals;
1343 SmallVector<SDValue, 8> LoadChains;
1344 unsigned NumElem = SrcVT.getVectorNumElements();
1345 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
1346
1347 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1348 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1349 DAG.getIntPtrConstant(Stride));
1350 SDValue ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl,
1351 Node->getValueType(0).getScalarType(),
1352 Tmp1, Tmp2, LD->getPointerInfo().getWithOffset(Idx * Stride),
1353 SrcVT.getScalarType(),
1354 LD->isVolatile(), LD->isNonTemporal(),
1355 LD->getAlignment());
1356
1357 LoadVals.push_back(ScalarLoad.getValue(0));
1358 LoadChains.push_back(ScalarLoad.getValue(1));
1359 }
1360 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1361 &LoadChains[0], LoadChains.size());
1362 SDValue ValRes = DAG.getNode(ISD::BUILD_VECTOR, dl,
1363 Node->getValueType(0), &LoadVals[0], LoadVals.size());
1364
1365 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1366 Tmp2 = LegalizeOp(Result.getValue(0)); // Relegalize new nodes.
1367 break;
1368 }
1369
1370 // If this is a promoted vector load, and the vector element types are
1371 // illegal, create the promoted vector from bitcasted segments.
1372 if (ExtType == ISD::EXTLOAD && SrcVT.isVector()) {
1373 EVT MemElemTy = Node->getValueType(0).getScalarType();
1374 EVT SrcSclrTy = SrcVT.getScalarType();
1375 unsigned SizeRatio =
1376 (MemElemTy.getSizeInBits() / SrcSclrTy.getSizeInBits());
1377
1378 SmallVector<SDValue, 8> LoadVals;
1379 SmallVector<SDValue, 8> LoadChains;
1380 unsigned NumElem = SrcVT.getVectorNumElements();
1381 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
1382
1383 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1384 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1385 DAG.getIntPtrConstant(Stride));
1386 SDValue ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl,
1387 SrcVT.getScalarType(),
1388 Tmp1, Tmp2, LD->getPointerInfo().getWithOffset(Idx * Stride),
1389 SrcVT.getScalarType(),
1390 LD->isVolatile(), LD->isNonTemporal(),
1391 LD->getAlignment());
1392 if (TLI.isBigEndian()) {
1393 // MSB (which is garbage, comes first)
1394 LoadVals.push_back(ScalarLoad.getValue(0));
1395 for (unsigned i = 0; i<SizeRatio-1; ++i)
1396 LoadVals.push_back(DAG.getUNDEF(SrcVT.getScalarType()));
1397 } else {
1398 // LSB (which is data, comes first)
1399 for (unsigned i = 0; i<SizeRatio-1; ++i)
1400 LoadVals.push_back(DAG.getUNDEF(SrcVT.getScalarType()));
1401 LoadVals.push_back(ScalarLoad.getValue(0));
1402 }
1403 LoadChains.push_back(ScalarLoad.getValue(1));
1404 }
1405
1406 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1407 &LoadChains[0], LoadChains.size());
1408 EVT TempWideVector = EVT::getVectorVT(*DAG.getContext(),
1409 SrcVT.getScalarType(), NumElem*SizeRatio);
1410 SDValue ValRes = DAG.getNode(ISD::BUILD_VECTOR, dl,
1411 TempWideVector, &LoadVals[0], LoadVals.size());
1412
1413 // Cast to the correct type
1414 ValRes = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), ValRes);
1415
1416 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1417 Tmp2 = LegalizeOp(Result.getValue(0)); // Relegalize new nodes.
1418 break;
1419
1420 }
1421
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001422 // FIXME: This does not work for vectors on most targets. Sign- and
1423 // zero-extend operations are currently folded into extending loads,
1424 // whether they are legal or not, and then we end up here without any
1425 // support for legalizing them.
1426 assert(ExtType != ISD::EXTLOAD &&
1427 "EXTLOAD should always be supported!");
1428 // Turn the unsupported load into an EXTLOAD followed by an explicit
1429 // zero/sign extend inreg.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001430 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001431 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1432 LD->isVolatile(), LD->isNonTemporal(),
1433 LD->getAlignment());
1434 SDValue ValRes;
1435 if (ExtType == ISD::SEXTLOAD)
1436 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1437 Result.getValueType(),
1438 Result, DAG.getValueType(SrcVT));
1439 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001440 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001441 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1442 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1443 break;
1444 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001445 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001446
1447 // Since loads produce two values, make sure to remember that we legalized
1448 // both of them.
1449 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1450 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1451 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001452 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001453 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001454 StoreSDNode *ST = cast<StoreSDNode>(Node);
1455 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1456 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001457 unsigned Alignment = ST->getAlignment();
1458 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001459 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001460
Evan Cheng8b2794a2006-10-13 21:14:26 +00001461 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001462 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1463 Result = SDValue(OptStore, 0);
1464 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001465 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001466
Eli Friedman957bffa2009-05-24 08:42:01 +00001467 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001468 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001469 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1470 Tmp1, Tmp3, Tmp2,
1471 ST->getOffset()),
1472 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001473
Owen Andersone50ed302009-08-10 22:56:29 +00001474 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001475 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001476 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001477 case TargetLowering::Legal:
1478 // If this is an unaligned store and the target doesn't support it,
1479 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001480 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001481 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001482 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001483 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001484 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1485 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001486 }
1487 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001488 case TargetLowering::Custom:
1489 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001490 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001491 break;
1492 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001493 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001494 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001495 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001496 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001497 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001498 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001499 break;
1500 }
1501 break;
1502 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001503 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001504 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001505
Owen Andersone50ed302009-08-10 22:56:29 +00001506 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001507 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001508
Duncan Sands83ec4b62008-06-06 12:08:01 +00001509 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001510 // Promote to a byte-sized store with upper bits zero if not
1511 // storing an integral number of bytes. For example, promote
1512 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001513 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1514 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001515 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001516 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1517 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001518 } else if (StWidth & (StWidth - 1)) {
1519 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001520 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001521 unsigned RoundWidth = 1 << Log2_32(StWidth);
1522 assert(RoundWidth < StWidth);
1523 unsigned ExtraWidth = StWidth - RoundWidth;
1524 assert(ExtraWidth < RoundWidth);
1525 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1526 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001527 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1528 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001529 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001530 unsigned IncrementSize;
1531
1532 if (TLI.isLittleEndian()) {
1533 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1534 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001535 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1536 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001537 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001538
1539 // Store the remaining ExtraWidth bits.
1540 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001541 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001542 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001543 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001544 DAG.getConstant(RoundWidth,
1545 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001546 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1547 ST->getPointerInfo().getWithOffset(IncrementSize),
1548 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001549 MinAlign(Alignment, IncrementSize));
1550 } else {
1551 // Big endian - avoid unaligned stores.
1552 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1553 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001554 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001555 DAG.getConstant(ExtraWidth,
1556 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001557 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1558 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001559
1560 // Store the remaining ExtraWidth bits.
1561 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001562 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001563 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001564 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1565 ST->getPointerInfo().getWithOffset(IncrementSize),
1566 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001567 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001568 }
Duncan Sands7e857202008-01-22 07:17:34 +00001569
1570 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001572 } else {
1573 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1574 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001575 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1576 Tmp1, Tmp3, Tmp2,
1577 ST->getOffset()),
1578 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001579
1580 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001581 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001582 case TargetLowering::Legal:
1583 // If this is an unaligned store and the target doesn't support it,
1584 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001585 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001586 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001587 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001588 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001589 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1590 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001591 }
1592 break;
1593 case TargetLowering::Custom:
1594 Result = TLI.LowerOperation(Result, DAG);
1595 break;
Dan Gohmane63e5ab2011-07-15 22:51:43 +00001596 case TargetLowering::Expand:
Nadav Rotemc2492c22011-06-14 08:11:52 +00001597
1598 EVT WideScalarVT = Tmp3.getValueType().getScalarType();
1599 EVT NarrowScalarVT = StVT.getScalarType();
1600
1601 // The Store type is illegal, must scalarize the vector store.
1602 SmallVector<SDValue, 8> Stores;
Dan Gohman75b10042011-07-15 22:39:09 +00001603 bool ScalarLegal = TLI.isTypeLegal(WideScalarVT);
1604 if (!TLI.isTypeLegal(StVT) && StVT.isVector() && ScalarLegal) {
Nadav Rotemc2492c22011-06-14 08:11:52 +00001605 unsigned NumElem = StVT.getVectorNumElements();
1606
1607 unsigned ScalarSize = StVT.getScalarType().getSizeInBits();
1608 // Round odd types to the next pow of two.
1609 if (!isPowerOf2_32(ScalarSize))
1610 ScalarSize = NextPowerOf2(ScalarSize);
1611 // Types smaller than 8 bits are promoted to 8 bits.
1612 ScalarSize = std::max<unsigned>(ScalarSize, 8);
1613 // Store stride
1614 unsigned Stride = ScalarSize/8;
1615 assert(isPowerOf2_32(Stride) && "Stride must be a power of two");
1616
1617 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1618 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1619 WideScalarVT, Tmp3, DAG.getIntPtrConstant(Idx));
1620
1621
1622 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), ScalarSize);
1623
1624 Ex = DAG.getNode(ISD::TRUNCATE, dl, NVT, Ex);
1625 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1626 DAG.getIntPtrConstant(Stride));
1627 SDValue Store = DAG.getStore(Tmp1, dl, Ex, Tmp2,
1628 ST->getPointerInfo().getWithOffset(Idx*Stride),
1629 isVolatile, isNonTemporal, Alignment);
1630 Stores.push_back(Store);
1631 }
1632 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1633 &Stores[0], Stores.size());
1634 break;
1635 }
1636
1637 // The Store type is illegal, must scalarize the vector store.
1638 // However, the scalar type is illegal. Must bitcast the result
1639 // and store it in smaller parts.
Dan Gohman75b10042011-07-15 22:39:09 +00001640 if (!TLI.isTypeLegal(StVT) && StVT.isVector()) {
Nadav Rotemc2492c22011-06-14 08:11:52 +00001641 unsigned WideNumElem = StVT.getVectorNumElements();
1642 unsigned Stride = NarrowScalarVT.getSizeInBits()/8;
1643
1644 unsigned SizeRatio =
1645 (WideScalarVT.getSizeInBits() / NarrowScalarVT.getSizeInBits());
1646
1647 EVT CastValueVT = EVT::getVectorVT(*DAG.getContext(), NarrowScalarVT,
1648 SizeRatio*WideNumElem);
1649
1650 // Cast the wide elem vector to wider vec with smaller elem type.
1651 // Example <2 x i64> -> <4 x i32>
1652 Tmp3 = DAG.getNode(ISD::BITCAST, dl, CastValueVT, Tmp3);
1653
1654 for (unsigned Idx=0; Idx<WideNumElem*SizeRatio; Idx++) {
1655 // Extract elment i
1656 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1657 NarrowScalarVT, Tmp3, DAG.getIntPtrConstant(Idx));
1658 // bump pointer.
1659 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1660 DAG.getIntPtrConstant(Stride));
1661
1662 // Store if, this element is:
1663 // - First element on big endian, or
1664 // - Last element on little endian
1665 if (( TLI.isBigEndian() && (Idx%SizeRatio == 0)) ||
1666 ((!TLI.isBigEndian() && (Idx%SizeRatio == SizeRatio-1)))) {
1667 SDValue Store = DAG.getStore(Tmp1, dl, Ex, Tmp2,
1668 ST->getPointerInfo().getWithOffset(Idx*Stride),
1669 isVolatile, isNonTemporal, Alignment);
1670 Stores.push_back(Store);
1671 }
1672 }
1673 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1674 &Stores[0], Stores.size());
1675 break;
1676 }
1677
1678
Duncan Sands7e857202008-01-22 07:17:34 +00001679 // TRUNCSTORE:i16 i32 -> STORE i16
Dan Gohman75b10042011-07-15 22:39:09 +00001680 assert(TLI.isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001681 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001682 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1683 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001684 break;
1685 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001686 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001687 }
1688 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001689 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001690 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001691 assert(Result.getValueType() == Op.getValueType() &&
1692 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001693
Chris Lattner456a93a2006-01-28 07:39:30 +00001694 // Make sure that the generated code is itself legal.
1695 if (Result != Op)
1696 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001697
Chris Lattner45982da2005-05-12 16:53:42 +00001698 // Note that LegalizeOp may be reentered even from single-use nodes, which
1699 // means that we always must cache transformed nodes.
1700 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001701 return Result;
1702}
1703
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001704SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1705 SDValue Vec = Op.getOperand(0);
1706 SDValue Idx = Op.getOperand(1);
1707 DebugLoc dl = Op.getDebugLoc();
1708 // Store the value to a temporary stack slot, then LOAD the returned part.
1709 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001710 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1711 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001712
1713 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001714 unsigned EltSize =
1715 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001716 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1717 DAG.getConstant(EltSize, Idx.getValueType()));
1718
1719 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1720 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1721 else
1722 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1723
1724 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1725
Eli Friedmanc680ac92009-07-09 22:01:03 +00001726 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001727 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001728 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001729 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001730 MachinePointerInfo(),
1731 Vec.getValueType().getVectorElementType(),
1732 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001733}
1734
David Greenecfe33c42011-01-26 19:13:22 +00001735SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1736 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1737
1738 SDValue Vec = Op.getOperand(0);
1739 SDValue Part = Op.getOperand(1);
1740 SDValue Idx = Op.getOperand(2);
1741 DebugLoc dl = Op.getDebugLoc();
1742
1743 // Store the value to a temporary stack slot, then LOAD the returned part.
1744
1745 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1746 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1747 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1748
1749 // First store the whole vector.
1750 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1751 false, false, 0);
1752
1753 // Then store the inserted part.
1754
1755 // Add the offset to the index.
1756 unsigned EltSize =
1757 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1758
1759 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1760 DAG.getConstant(EltSize, Idx.getValueType()));
1761
1762 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1763 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1764 else
1765 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1766
1767 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1768 StackPtr);
1769
1770 // Store the subvector.
1771 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1772 MachinePointerInfo(), false, false, 0);
1773
1774 // Finally, load the updated vector.
1775 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1776 false, false, 0);
1777}
1778
Eli Friedman7ef3d172009-06-06 07:04:42 +00001779SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1780 // We can't handle this case efficiently. Allocate a sufficiently
1781 // aligned object on the stack, store each element into it, then load
1782 // the result as a vector.
1783 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001784 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001785 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001786 DebugLoc dl = Node->getDebugLoc();
1787 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001788 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001789 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001790
1791 // Emit a store of each element to the stack slot.
1792 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001793 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001794 // Store (in the right endianness) the elements to memory.
1795 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1796 // Ignore undef elements.
1797 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1798
1799 unsigned Offset = TypeByteSize*i;
1800
1801 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1802 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1803
Dan Gohman9949dd62010-02-25 20:30:49 +00001804 // If the destination vector element type is narrower than the source
1805 // element type, only store the bits necessary.
1806 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001807 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001808 Node->getOperand(i), Idx,
1809 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001810 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001811 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001812 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001813 Node->getOperand(i), Idx,
1814 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001815 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001816 }
1817
1818 SDValue StoreChain;
1819 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001821 &Stores[0], Stores.size());
1822 else
1823 StoreChain = DAG.getEntryNode();
1824
1825 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001826 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001827}
1828
Eli Friedman4bc8c712009-05-27 12:20:41 +00001829SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1830 DebugLoc dl = Node->getDebugLoc();
1831 SDValue Tmp1 = Node->getOperand(0);
1832 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001833
1834 // Get the sign bit of the RHS. First obtain a value that has the same
1835 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001836 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001837 EVT FloatVT = Tmp2.getValueType();
1838 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Dan Gohman75b10042011-07-15 22:39:09 +00001839 if (TLI.isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001840 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001841 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001842 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001843 // Store the float to memory, then load the sign part out as an integer.
1844 MVT LoadTy = TLI.getPointerTy();
1845 // First create a temporary that is aligned for both the load and store.
1846 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1847 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001848 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001849 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001850 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001851 if (TLI.isBigEndian()) {
1852 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1853 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001854 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1855 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001856 } else { // Little endian
1857 SDValue LoadPtr = StackPtr;
1858 // The float may be wider than the integer we are going to load. Advance
1859 // the pointer so that the loaded integer will contain the sign bit.
1860 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1861 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1862 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1863 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1864 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001865 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1866 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001867 // Move the sign bit to the top bit of the loaded integer.
1868 unsigned BitShift = LoadTy.getSizeInBits() -
1869 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1870 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1871 if (BitShift)
1872 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001873 DAG.getConstant(BitShift,
1874 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001875 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001876 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001877 // Now get the sign bit proper, by seeing whether the value is negative.
1878 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1879 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1880 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001881 // Get the absolute value of the result.
1882 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1883 // Select between the nabs and abs value based on the sign bit of
1884 // the input.
1885 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1886 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1887 AbsVal);
1888}
1889
Eli Friedman4bc8c712009-05-27 12:20:41 +00001890void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1891 SmallVectorImpl<SDValue> &Results) {
1892 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1893 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1894 " not tell us which reg is the stack pointer!");
1895 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001896 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001897 SDValue Tmp1 = SDValue(Node, 0);
1898 SDValue Tmp2 = SDValue(Node, 1);
1899 SDValue Tmp3 = Node->getOperand(2);
1900 SDValue Chain = Tmp1.getOperand(0);
1901
1902 // Chain the dynamic stack allocation so that it doesn't modify the stack
1903 // pointer when other instructions are using the stack.
1904 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1905
1906 SDValue Size = Tmp2.getOperand(1);
1907 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1908 Chain = SP.getValue(1);
1909 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001910 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001911 if (Align > StackAlign)
1912 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1913 DAG.getConstant(-(uint64_t)Align, VT));
1914 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1915 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1916
1917 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1918 DAG.getIntPtrConstant(0, true), SDValue());
1919
1920 Results.push_back(Tmp1);
1921 Results.push_back(Tmp2);
1922}
1923
Evan Cheng7f042682008-10-15 02:05:31 +00001924/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001925/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001926/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001927void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001928 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001929 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001930 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001931 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001932 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1933 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001934 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001935 case TargetLowering::Legal:
1936 // Nothing to do.
1937 break;
1938 case TargetLowering::Expand: {
1939 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1940 unsigned Opc = 0;
1941 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001942 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001943 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1944 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1945 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1946 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1947 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1948 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1949 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1950 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1951 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1952 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1953 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1954 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001955 // FIXME: Implement more expansions.
1956 }
1957
Dale Johannesenbb5da912009-02-02 20:41:04 +00001958 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1959 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1960 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001961 RHS = SDValue();
1962 CC = SDValue();
1963 break;
1964 }
1965 }
1966}
1967
Chris Lattner1401d152008-01-16 07:45:30 +00001968/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1969/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1970/// a load from the stack slot to DestVT, extending it if needed.
1971/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001972SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001973 EVT SlotVT,
1974 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001975 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001976 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001977 unsigned SrcAlign =
1978 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001979 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001980 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001981
Evan Chengff89dcb2009-10-18 18:16:27 +00001982 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1983 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001984 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001985
Duncan Sands83ec4b62008-06-06 12:08:01 +00001986 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1987 unsigned SlotSize = SlotVT.getSizeInBits();
1988 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001989 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Evan Chengadf97992010-04-15 01:25:27 +00001990 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001991
Chris Lattner1401d152008-01-16 07:45:30 +00001992 // Emit a store to the stack slot. Use a truncstore if the input value is
1993 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001994 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001995
Chris Lattner1401d152008-01-16 07:45:30 +00001996 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001997 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001998 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001999 else {
2000 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002001 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002002 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002003 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002004
Chris Lattner35481892005-12-23 00:16:34 +00002005 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002006 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002007 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00002008 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Chris Lattner1401d152008-01-16 07:45:30 +00002010 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00002011 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002012 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002013}
2014
Dan Gohman475871a2008-07-27 21:46:04 +00002015SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002016 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002017 // Create a vector sized/aligned stack slot, store the value to element #0,
2018 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002020
Evan Chengff89dcb2009-10-18 18:16:27 +00002021 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
2022 int SPFI = StackPtrFI->getIndex();
2023
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002024 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2025 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00002026 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00002027 Node->getValueType(0).getVectorElementType(),
2028 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00002029 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00002030 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00002031 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002032}
2033
2034
Chris Lattnerce872152006-03-19 06:31:19 +00002035/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002036/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002037SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002038 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00002039 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002040 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002041 EVT VT = Node->getValueType(0);
2042 EVT OpVT = Node->getOperand(0).getValueType();
2043 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002044
2045 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002046 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002047 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002048 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00002049 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002050 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002051 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00002052 if (V.getOpcode() == ISD::UNDEF)
2053 continue;
2054 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00002055 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002056 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00002057 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002058
2059 if (!Value1.getNode()) {
2060 Value1 = V;
2061 } else if (!Value2.getNode()) {
2062 if (V != Value1)
2063 Value2 = V;
2064 } else if (V != Value1 && V != Value2) {
2065 MoreThanTwoValues = true;
2066 }
Chris Lattnerce872152006-03-19 06:31:19 +00002067 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002068
Eli Friedman7a5e5552009-06-07 06:52:44 +00002069 if (!Value1.getNode())
2070 return DAG.getUNDEF(VT);
2071
2072 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002073 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002074
Chris Lattner2eb86532006-03-24 07:29:17 +00002075 // If all elements are constants, create a load from the constant pool.
2076 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002077 std::vector<Constant*> CV;
2078 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002079 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002080 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002081 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002082 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002083 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00002084 if (OpVT==EltVT)
2085 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
2086 else {
2087 // If OpVT and EltVT don't match, EltVT is not legal and the
2088 // element values have been promoted/truncated earlier. Undo this;
2089 // we don't want a v16i8 to become a v16i32 for example.
2090 const ConstantInt *CI = V->getConstantIntValue();
2091 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
2092 CI->getZExtValue()));
2093 }
Chris Lattner2eb86532006-03-24 07:29:17 +00002094 } else {
2095 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002096 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002097 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00002098 }
2099 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00002100 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002101 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002102 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002103 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002104 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00002105 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002107
Eli Friedman7a5e5552009-06-07 06:52:44 +00002108 if (!MoreThanTwoValues) {
2109 SmallVector<int, 8> ShuffleVec(NumElems, -1);
2110 for (unsigned i = 0; i < NumElems; ++i) {
2111 SDValue V = Node->getOperand(i);
2112 if (V.getOpcode() == ISD::UNDEF)
2113 continue;
2114 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2115 }
2116 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002117 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00002118 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2119 SDValue Vec2;
2120 if (Value2.getNode())
2121 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2122 else
2123 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002124
Chris Lattner87100e02006-03-20 01:52:29 +00002125 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00002126 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00002127 }
2128 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002129
Eli Friedman7ef3d172009-06-06 07:04:42 +00002130 // Otherwise, we can't handle this case efficiently.
2131 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00002132}
2133
Chris Lattner77e77a62005-01-21 06:05:23 +00002134// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2135// does not fit into a register, return the lo part and set the hi part to the
2136// by-reg argument. If it does fit into a single register, return the result
2137// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002138SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002139 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002140 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002141 // Legalizing the call will automatically add the previous call to the
2142 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002143 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002144
Chris Lattner77e77a62005-01-21 06:05:23 +00002145 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002146 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002147 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002148 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002149 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00002150 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002151 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002152 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002153 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002154 }
Bill Wendling056292f2008-09-16 21:48:12 +00002155 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002156 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002157
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002158 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002159 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00002160
2161 // isTailCall may be true since the callee does not reference caller stack
2162 // frame. Check if it's in the right position.
2163 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002164 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002165 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002166 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002167 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002168 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002169
Evan Cheng3d2125c2010-11-30 23:55:39 +00002170 if (!CallInfo.second.getNode())
2171 // It's a tailcall, return the chain (which is the DAG root).
2172 return DAG.getRoot();
2173
Chris Lattner6831a812006-02-13 09:18:02 +00002174 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002175 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Chris Lattner6831a812006-02-13 09:18:02 +00002176 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2177 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002178 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002179}
2180
Dan Gohmanf316eb72011-05-16 22:09:53 +00002181/// ExpandLibCall - Generate a libcall taking the given operands as arguments
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002182/// and returning a result of type RetVT.
2183SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
2184 const SDValue *Ops, unsigned NumOps,
2185 bool isSigned, DebugLoc dl) {
2186 TargetLowering::ArgListTy Args;
2187 Args.reserve(NumOps);
Dan Gohmanf316eb72011-05-16 22:09:53 +00002188
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002189 TargetLowering::ArgListEntry Entry;
2190 for (unsigned i = 0; i != NumOps; ++i) {
2191 Entry.Node = Ops[i];
2192 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
2193 Entry.isSExt = isSigned;
2194 Entry.isZExt = !isSigned;
2195 Args.push_back(Entry);
2196 }
2197 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2198 TLI.getPointerTy());
Dan Gohmanf316eb72011-05-16 22:09:53 +00002199
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002200 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002201 std::pair<SDValue,SDValue> CallInfo =
2202 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
2203 false, 0, TLI.getLibcallCallingConv(LC), false,
2204 /*isReturnValueUsed=*/true,
2205 Callee, Args, DAG, dl);
Dan Gohmanf316eb72011-05-16 22:09:53 +00002206
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002207 // Legalize the call sequence, starting with the chain. This will advance
2208 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2209 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2210 LegalizeOp(CallInfo.second);
2211
2212 return CallInfo.first;
2213}
2214
Jim Grosbache03262f2010-06-18 21:43:38 +00002215// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
2216// ExpandLibCall except that the first operand is the in-chain.
2217std::pair<SDValue, SDValue>
2218SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2219 SDNode *Node,
2220 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00002221 SDValue InChain = Node->getOperand(0);
2222
2223 TargetLowering::ArgListTy Args;
2224 TargetLowering::ArgListEntry Entry;
2225 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2226 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002227 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00002228 Entry.Node = Node->getOperand(i);
2229 Entry.Ty = ArgTy;
2230 Entry.isSExt = isSigned;
2231 Entry.isZExt = !isSigned;
2232 Args.push_back(Entry);
2233 }
2234 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2235 TLI.getPointerTy());
2236
2237 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002238 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00002239 std::pair<SDValue, SDValue> CallInfo =
2240 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002241 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002242 /*isReturnValueUsed=*/true,
2243 Callee, Args, DAG, Node->getDebugLoc());
2244
2245 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002246 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Jim Grosbache03262f2010-06-18 21:43:38 +00002247 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2248 LegalizeOp(CallInfo.second);
2249 return CallInfo;
2250}
2251
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002252SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2253 RTLIB::Libcall Call_F32,
2254 RTLIB::Libcall Call_F64,
2255 RTLIB::Libcall Call_F80,
2256 RTLIB::Libcall Call_PPCF128) {
2257 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002258 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002259 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002260 case MVT::f32: LC = Call_F32; break;
2261 case MVT::f64: LC = Call_F64; break;
2262 case MVT::f80: LC = Call_F80; break;
2263 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002264 }
2265 return ExpandLibCall(LC, Node, false);
2266}
2267
2268SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002269 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002270 RTLIB::Libcall Call_I16,
2271 RTLIB::Libcall Call_I32,
2272 RTLIB::Libcall Call_I64,
2273 RTLIB::Libcall Call_I128) {
2274 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002276 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002277 case MVT::i8: LC = Call_I8; break;
2278 case MVT::i16: LC = Call_I16; break;
2279 case MVT::i32: LC = Call_I32; break;
2280 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002281 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002282 }
2283 return ExpandLibCall(LC, Node, isSigned);
2284}
2285
Evan Cheng65279cb2011-04-16 03:08:26 +00002286/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
2287static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
2288 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002289 RTLIB::Libcall LC;
2290 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2291 default: assert(0 && "Unexpected request for libcall!");
2292 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2293 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2294 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2295 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2296 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2297 }
2298
Evan Cheng65279cb2011-04-16 03:08:26 +00002299 return TLI.getLibcallName(LC) != 0;
2300}
Evan Cheng8e23e812011-04-01 00:42:02 +00002301
Evan Cheng65279cb2011-04-16 03:08:26 +00002302/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
2303/// needed.
2304static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002305 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00002306 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00002307 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002308 else
Evan Cheng8e23e812011-04-01 00:42:02 +00002309 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002310
Evan Cheng8e23e812011-04-01 00:42:02 +00002311 SDValue Op0 = Node->getOperand(0);
2312 SDValue Op1 = Node->getOperand(1);
2313 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2314 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2315 SDNode *User = *UI;
2316 if (User == Node)
2317 continue;
2318 if (User->getOpcode() == OtherOpcode &&
2319 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00002320 User->getOperand(1) == Op1)
2321 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00002322 }
Evan Cheng65279cb2011-04-16 03:08:26 +00002323 return false;
2324}
Evan Cheng8e23e812011-04-01 00:42:02 +00002325
Evan Cheng65279cb2011-04-16 03:08:26 +00002326/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
2327/// pairs.
2328void
2329SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2330 SmallVectorImpl<SDValue> &Results) {
2331 unsigned Opcode = Node->getOpcode();
2332 bool isSigned = Opcode == ISD::SDIVREM;
2333
2334 RTLIB::Libcall LC;
2335 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2336 default: assert(0 && "Unexpected request for libcall!");
2337 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2338 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2339 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2340 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2341 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00002342 }
2343
2344 // The input chain to this libcall is the entry node of the function.
2345 // Legalizing the call will automatically add the previous call to the
2346 // dependence.
2347 SDValue InChain = DAG.getEntryNode();
2348
2349 EVT RetVT = Node->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002350 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00002351
2352 TargetLowering::ArgListTy Args;
2353 TargetLowering::ArgListEntry Entry;
2354 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2355 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002356 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00002357 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
2358 Entry.isSExt = isSigned;
2359 Entry.isZExt = !isSigned;
2360 Args.push_back(Entry);
2361 }
2362
2363 // Also pass the return address of the remainder.
2364 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2365 Entry.Node = FIPtr;
2366 Entry.Ty = RetTy->getPointerTo();
2367 Entry.isSExt = isSigned;
2368 Entry.isZExt = !isSigned;
2369 Args.push_back(Entry);
2370
2371 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2372 TLI.getPointerTy());
2373
2374 // Splice the libcall in wherever FindInputOutputChains tells us to.
2375 DebugLoc dl = Node->getDebugLoc();
2376 std::pair<SDValue, SDValue> CallInfo =
2377 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
2378 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
2379 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
2380
2381 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002382 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Evan Cheng8e23e812011-04-01 00:42:02 +00002383 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2384 LegalizeOp(CallInfo.second);
2385
2386 // Remainder is loaded back from the stack frame.
Stuart Hastingsfc521632011-04-19 16:16:58 +00002387 SDValue Rem = DAG.getLoad(RetVT, dl, getLastCALLSEQ(), FIPtr,
Evan Cheng8e23e812011-04-01 00:42:02 +00002388 MachinePointerInfo(), false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00002389 Results.push_back(CallInfo.first);
2390 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00002391}
2392
Chris Lattner22cde6a2006-01-28 08:25:58 +00002393/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2394/// INT_TO_FP operation of the specified operand when the target requests that
2395/// we expand it. At this point, we know that the result and operand types are
2396/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002397SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2398 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002400 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002401 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002402 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002403
Chris Lattner23594d42008-01-16 07:03:22 +00002404 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002405 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002406
Chris Lattner22cde6a2006-01-28 08:25:58 +00002407 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002408 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002409 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002410 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002411 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002412 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002413 if (TLI.isLittleEndian())
2414 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002415
Chris Lattner22cde6a2006-01-28 08:25:58 +00002416 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002417 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002418 if (isSigned) {
2419 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002420 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2421 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002422 } else {
2423 Op0Mapped = Op0;
2424 }
2425 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002426 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002427 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002428 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002429 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002430 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002431 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002432 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2433 MachinePointerInfo(),
2434 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002435 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002436 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2437 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002438 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002439 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002440 BitsToDouble(0x4330000080000000ULL) :
2441 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002442 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002443 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002444 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002445 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002446 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002447 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002448 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002449 // do nothing
2450 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002451 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002452 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002453 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002455 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002456 }
2457 return Result;
2458 }
2459 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002460 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002461
2462 // Implementation of unsigned i64 to f64 following the algorithm in
2463 // __floatundidf in compiler_rt. This implementation has the advantage
2464 // of performing rounding correctly, both in the default rounding mode
2465 // and in all alternate rounding modes.
2466 // TODO: Generalize this for use with other types.
2467 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2468 SDValue TwoP52 =
2469 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2470 SDValue TwoP84PlusTwoP52 =
2471 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2472 SDValue TwoP84 =
2473 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2474
2475 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2476 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2477 DAG.getConstant(32, MVT::i64));
2478 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2479 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002480 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2481 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002482 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2483 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002484 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2485 }
2486
Owen Anderson3a9e7692010-10-05 17:24:05 +00002487 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002488 // TODO: Generalize this for use with other types.
2489 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002490 // For unsigned conversions, convert them to signed conversions using the
2491 // algorithm from the x86_64 __floatundidf in compiler_rt.
2492 if (!isSigned) {
2493 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002494
Owen Anderson95771af2011-02-25 21:41:48 +00002495 SDValue ShiftConst =
2496 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002497 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2498 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2499 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2500 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002501
Owen Anderson3a9e7692010-10-05 17:24:05 +00002502 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2503 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002504
Owen Anderson3a9e7692010-10-05 17:24:05 +00002505 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002506 // select. We happen to get lucky and machinesink does the right
2507 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002508 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002509 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002510 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2511 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2512 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002513
Owen Anderson3a9e7692010-10-05 17:24:05 +00002514 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002515
Jim Grosbach6e992612010-07-02 17:41:59 +00002516 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002517 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2518 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2519 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002520 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002521 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2522 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2523 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2524 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2525 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2526 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002527 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002528 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002529 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002530
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002531 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2532 DAG.getConstant(32, SHVT));
2533 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2534 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2535 SDValue TwoP32 =
2536 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2537 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2538 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2539 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2540 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2541 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2542 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002543 }
2544
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002545 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002546
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002547 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2548 Op0, DAG.getConstant(0, Op0.getValueType()),
2549 ISD::SETLT);
2550 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2551 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2552 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002553
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002554 // If the sign bit of the integer is set, the large number will be treated
2555 // as a negative number. To counteract this, the dynamic code adds an
2556 // offset depending on the data type.
2557 uint64_t FF;
2558 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002559 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002560 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2561 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2562 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2563 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2564 }
2565 if (TLI.isLittleEndian()) FF <<= 32;
2566 Constant *FudgeFactor = ConstantInt::get(
2567 Type::getInt64Ty(*DAG.getContext()), FF);
2568
2569 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2570 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2571 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2572 Alignment = std::min(Alignment, 4u);
2573 SDValue FudgeInReg;
2574 if (DestVT == MVT::f32)
2575 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002576 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002577 false, false, Alignment);
2578 else {
2579 FudgeInReg =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002580 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002581 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002582 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002583 MVT::f32, false, false, Alignment));
2584 }
2585
2586 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002587}
2588
2589/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2590/// *INT_TO_FP operation of the specified operand when the target requests that
2591/// we promote it. At this point, we know that the result and operand types are
2592/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2593/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002594SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002595 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002596 bool isSigned,
2597 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002598 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002599 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002600
2601 unsigned OpToUse = 0;
2602
2603 // Scan for the appropriate larger type to use.
2604 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002605 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002606 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002607
2608 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002609 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2610 OpToUse = ISD::SINT_TO_FP;
2611 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002612 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002613 if (isSigned) continue;
2614
2615 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002616 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2617 OpToUse = ISD::UINT_TO_FP;
2618 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002619 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002620
2621 // Otherwise, try a larger type.
2622 }
2623
2624 // Okay, we found the operation and type to use. Zero extend our input to the
2625 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002626 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002627 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002628 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002629}
2630
2631/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2632/// FP_TO_*INT operation of the specified operand when the target requests that
2633/// we promote it. At this point, we know that the result and operand types are
2634/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2635/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002636SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002637 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002638 bool isSigned,
2639 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002640 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002641 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002642
2643 unsigned OpToUse = 0;
2644
2645 // Scan for the appropriate larger type to use.
2646 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002647 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002648 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002649
Eli Friedman3be2e512009-05-28 03:06:16 +00002650 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002651 OpToUse = ISD::FP_TO_SINT;
2652 break;
2653 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002654
Eli Friedman3be2e512009-05-28 03:06:16 +00002655 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002656 OpToUse = ISD::FP_TO_UINT;
2657 break;
2658 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002659
2660 // Otherwise, try a larger type.
2661 }
2662
Scott Michelfdc40a02009-02-17 22:15:04 +00002663
Chris Lattner27a6c732007-11-24 07:07:01 +00002664 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002665 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002666
Chris Lattner27a6c732007-11-24 07:07:01 +00002667 // Truncate the result of the extended FP_TO_*INT operation to the desired
2668 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002669 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002670}
2671
2672/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2673///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002674SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002675 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002676 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002677 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002678 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002679 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002680 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002681 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2682 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2683 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002684 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002685 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2686 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2687 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2688 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2689 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2690 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2691 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2692 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2693 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002694 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002695 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2696 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2697 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2698 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2699 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2700 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2701 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2702 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2703 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2704 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2705 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2706 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2707 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2708 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2709 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2710 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2711 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2712 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2713 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2714 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2715 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002716 }
2717}
2718
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002719/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002720// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002721static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2722 APInt Val = APInt(NumBits, ByteVal);
2723 unsigned Shift = 8;
2724 for (unsigned i = NumBits; i > 8; i >>= 1) {
2725 Val = (Val << Shift) | Val;
2726 Shift <<= 1;
2727 }
2728 return Val;
2729}
2730
Chris Lattner22cde6a2006-01-28 08:25:58 +00002731/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2732///
Scott Michelfdc40a02009-02-17 22:15:04 +00002733SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002734 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002735 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002736 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002737 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002738 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002739 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002740 unsigned Len = VT.getSizeInBits();
2741
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002742 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2743 "CTPOP not implemented for this type.");
2744
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002745 // This is the "best" algorithm from
2746 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2747
2748 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2749 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2750 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2751 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2752
2753 // v = v - ((v >> 1) & 0x55555555...)
2754 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2755 DAG.getNode(ISD::AND, dl, VT,
2756 DAG.getNode(ISD::SRL, dl, VT, Op,
2757 DAG.getConstant(1, ShVT)),
2758 Mask55));
2759 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2760 Op = DAG.getNode(ISD::ADD, dl, VT,
2761 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2762 DAG.getNode(ISD::AND, dl, VT,
2763 DAG.getNode(ISD::SRL, dl, VT, Op,
2764 DAG.getConstant(2, ShVT)),
2765 Mask33));
2766 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2767 Op = DAG.getNode(ISD::AND, dl, VT,
2768 DAG.getNode(ISD::ADD, dl, VT, Op,
2769 DAG.getNode(ISD::SRL, dl, VT, Op,
2770 DAG.getConstant(4, ShVT))),
2771 Mask0F);
2772 // v = (v * 0x01010101...) >> (Len - 8)
2773 Op = DAG.getNode(ISD::SRL, dl, VT,
2774 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2775 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002776
Chris Lattner22cde6a2006-01-28 08:25:58 +00002777 return Op;
2778 }
2779 case ISD::CTLZ: {
2780 // for now, we do this:
2781 // x = x | (x >> 1);
2782 // x = x | (x >> 2);
2783 // ...
2784 // x = x | (x >>16);
2785 // x = x | (x >>32); // for 64-bit input
2786 // return popcount(~x);
2787 //
2788 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002789 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002790 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002791 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002792 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002793 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002794 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002795 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002796 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002797 Op = DAG.getNOT(dl, Op, VT);
2798 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002799 }
2800 case ISD::CTTZ: {
2801 // for now, we use: { return popcount(~x & (x - 1)); }
2802 // unless the target has ctlz but not ctpop, in which case we use:
2803 // { return 32 - nlz(~x & (x-1)); }
2804 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002805 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002806 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2807 DAG.getNOT(dl, Op, VT),
2808 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002809 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002810 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002811 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2812 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002813 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002814 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002815 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2816 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002817 }
2818 }
2819}
Chris Lattnere34b3962005-01-19 04:19:40 +00002820
Jim Grosbache03262f2010-06-18 21:43:38 +00002821std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2822 unsigned Opc = Node->getOpcode();
2823 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2824 RTLIB::Libcall LC;
2825
2826 switch (Opc) {
2827 default:
2828 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2829 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002830 case ISD::ATOMIC_SWAP:
2831 switch (VT.SimpleTy) {
2832 default: llvm_unreachable("Unexpected value type for atomic!");
2833 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2834 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2835 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2836 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2837 }
2838 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002839 case ISD::ATOMIC_CMP_SWAP:
2840 switch (VT.SimpleTy) {
2841 default: llvm_unreachable("Unexpected value type for atomic!");
2842 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2843 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2844 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2845 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2846 }
2847 break;
2848 case ISD::ATOMIC_LOAD_ADD:
2849 switch (VT.SimpleTy) {
2850 default: llvm_unreachable("Unexpected value type for atomic!");
2851 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2852 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2853 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2854 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2855 }
2856 break;
2857 case ISD::ATOMIC_LOAD_SUB:
2858 switch (VT.SimpleTy) {
2859 default: llvm_unreachable("Unexpected value type for atomic!");
2860 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2861 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2862 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2863 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2864 }
2865 break;
2866 case ISD::ATOMIC_LOAD_AND:
2867 switch (VT.SimpleTy) {
2868 default: llvm_unreachable("Unexpected value type for atomic!");
2869 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2870 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2871 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2872 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2873 }
2874 break;
2875 case ISD::ATOMIC_LOAD_OR:
2876 switch (VT.SimpleTy) {
2877 default: llvm_unreachable("Unexpected value type for atomic!");
2878 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2879 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2880 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2881 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2882 }
2883 break;
2884 case ISD::ATOMIC_LOAD_XOR:
2885 switch (VT.SimpleTy) {
2886 default: llvm_unreachable("Unexpected value type for atomic!");
2887 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2888 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2889 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2890 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2891 }
2892 break;
2893 case ISD::ATOMIC_LOAD_NAND:
2894 switch (VT.SimpleTy) {
2895 default: llvm_unreachable("Unexpected value type for atomic!");
2896 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2897 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2898 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2899 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2900 }
2901 break;
2902 }
2903
2904 return ExpandChainLibCall(LC, Node, false);
2905}
2906
Eli Friedman8c377c72009-05-27 01:25:56 +00002907void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2908 SmallVectorImpl<SDValue> &Results) {
2909 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002910 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002911 switch (Node->getOpcode()) {
2912 case ISD::CTPOP:
2913 case ISD::CTLZ:
2914 case ISD::CTTZ:
2915 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2916 Results.push_back(Tmp1);
2917 break;
2918 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002919 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002920 break;
2921 case ISD::FRAMEADDR:
2922 case ISD::RETURNADDR:
2923 case ISD::FRAME_TO_ARGS_OFFSET:
2924 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2925 break;
2926 case ISD::FLT_ROUNDS_:
2927 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2928 break;
2929 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002930 case ISD::EH_LABEL:
2931 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002932 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002933 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002934 case ISD::EH_SJLJ_DISPATCHSETUP:
2935 // If the target didn't expand these, there's nothing to do, so just
2936 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002937 Results.push_back(Node->getOperand(0));
2938 break;
2939 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002940 // If the target didn't expand this, just return 'zero' and preserve the
2941 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002942 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002943 Results.push_back(Node->getOperand(0));
2944 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002945 case ISD::MEMBARRIER: {
2946 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2947 TargetLowering::ArgListTy Args;
2948 std::pair<SDValue, SDValue> CallResult =
2949 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002950 false, false, false, false, 0, CallingConv::C,
2951 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002952 /*isReturnValueUsed=*/true,
2953 DAG.getExternalSymbol("__sync_synchronize",
2954 TLI.getPointerTy()),
2955 Args, DAG, dl);
2956 Results.push_back(CallResult.second);
2957 break;
2958 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002959 // By default, atomic intrinsics are marked Legal and lowered. Targets
2960 // which don't support them directly, however, may want libcalls, in which
2961 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002962 case ISD::ATOMIC_SWAP:
2963 case ISD::ATOMIC_LOAD_ADD:
2964 case ISD::ATOMIC_LOAD_SUB:
2965 case ISD::ATOMIC_LOAD_AND:
2966 case ISD::ATOMIC_LOAD_OR:
2967 case ISD::ATOMIC_LOAD_XOR:
2968 case ISD::ATOMIC_LOAD_NAND:
2969 case ISD::ATOMIC_LOAD_MIN:
2970 case ISD::ATOMIC_LOAD_MAX:
2971 case ISD::ATOMIC_LOAD_UMIN:
2972 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002973 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002974 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2975 Results.push_back(Tmp.first);
2976 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002977 break;
Evan Chenga8457062010-06-18 22:01:37 +00002978 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002979 case ISD::DYNAMIC_STACKALLOC:
2980 ExpandDYNAMIC_STACKALLOC(Node, Results);
2981 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002982 case ISD::MERGE_VALUES:
2983 for (unsigned i = 0; i < Node->getNumValues(); i++)
2984 Results.push_back(Node->getOperand(i));
2985 break;
2986 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002987 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002988 if (VT.isInteger())
2989 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002990 else {
2991 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002992 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002993 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002994 break;
2995 }
2996 case ISD::TRAP: {
2997 // If this operation is not supported, lower it to 'abort()' call
2998 TargetLowering::ArgListTy Args;
2999 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003000 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00003001 false, false, false, false, 0, CallingConv::C,
3002 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003003 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00003004 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003005 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00003006 Results.push_back(CallResult.second);
3007 break;
3008 }
3009 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003010 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00003011 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3012 Node->getValueType(0), dl);
3013 Results.push_back(Tmp1);
3014 break;
3015 case ISD::FP_EXTEND:
3016 Tmp1 = EmitStackConvert(Node->getOperand(0),
3017 Node->getOperand(0).getValueType(),
3018 Node->getValueType(0), dl);
3019 Results.push_back(Tmp1);
3020 break;
3021 case ISD::SIGN_EXTEND_INREG: {
3022 // NOTE: we could fall back on load/store here too for targets without
3023 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00003024 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00003025 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00003026 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00003027 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00003028 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00003029 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
3030 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00003031 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00003032 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3033 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00003034 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3035 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003036 break;
3037 }
3038 case ISD::FP_ROUND_INREG: {
3039 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00003040 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00003041
3042 // NOTE: there is a choice here between constantly creating new stack
3043 // slots and always reusing the same one. We currently always create
3044 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00003045 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00003046 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3047 Node->getValueType(0), dl);
3048 Results.push_back(Tmp1);
3049 break;
3050 }
3051 case ISD::SINT_TO_FP:
3052 case ISD::UINT_TO_FP:
3053 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3054 Node->getOperand(0), Node->getValueType(0), dl);
3055 Results.push_back(Tmp1);
3056 break;
3057 case ISD::FP_TO_UINT: {
3058 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00003059 EVT VT = Node->getOperand(0).getValueType();
3060 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00003061 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00003062 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3063 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3064 Tmp1 = DAG.getConstantFP(apf, VT);
3065 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3066 Node->getOperand(0),
3067 Tmp1, ISD::SETLT);
3068 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003069 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3070 DAG.getNode(ISD::FSUB, dl, VT,
3071 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003072 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3073 DAG.getConstant(x, NVT));
3074 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
3075 Results.push_back(Tmp1);
3076 break;
3077 }
Eli Friedman509150f2009-05-27 07:58:35 +00003078 case ISD::VAARG: {
3079 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00003080 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00003081 Tmp1 = Node->getOperand(0);
3082 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003083 unsigned Align = Node->getConstantOperandVal(3);
3084
Chris Lattnerecf42c42010-09-21 16:36:31 +00003085 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
3086 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003087 SDValue VAList = VAListLoad;
3088
Rafael Espindolacbeeae22010-07-11 04:01:49 +00003089 if (Align > TLI.getMinStackArgumentAlignment()) {
3090 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
3091
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003092 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
3093 DAG.getConstant(Align - 1,
3094 TLI.getPointerTy()));
3095
3096 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00003097 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003098 TLI.getPointerTy()));
3099 }
3100
Eli Friedman509150f2009-05-27 07:58:35 +00003101 // Increment the pointer, VAList, to the next vaarg
3102 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
3103 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00003104 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00003105 TLI.getPointerTy()));
3106 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00003107 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
3108 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00003109 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00003110 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00003111 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00003112 Results.push_back(Results[0].getValue(1));
3113 break;
3114 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003115 case ISD::VACOPY: {
3116 // This defaults to loading a pointer from the input and storing it to the
3117 // output, returning the chain.
3118 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3119 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3120 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00003121 Node->getOperand(2), MachinePointerInfo(VS),
3122 false, false, 0);
3123 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
3124 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00003125 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003126 break;
3127 }
3128 case ISD::EXTRACT_VECTOR_ELT:
3129 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3130 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003131 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00003132 Node->getOperand(0));
3133 else
3134 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3135 Results.push_back(Tmp1);
3136 break;
3137 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00003138 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00003139 break;
David Greenecfe33c42011-01-26 19:13:22 +00003140 case ISD::INSERT_SUBVECTOR:
3141 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3142 break;
Eli Friedman509150f2009-05-27 07:58:35 +00003143 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00003144 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00003145 break;
3146 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003147 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00003148 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00003149 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003150 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00003151 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3152 Node->getOperand(1),
3153 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00003154 break;
Eli Friedman509150f2009-05-27 07:58:35 +00003155 case ISD::VECTOR_SHUFFLE: {
3156 SmallVector<int, 8> Mask;
3157 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3158
Owen Andersone50ed302009-08-10 22:56:29 +00003159 EVT VT = Node->getValueType(0);
3160 EVT EltVT = VT.getVectorElementType();
Dan Gohman75b10042011-07-15 22:39:09 +00003161 if (!TLI.isTypeLegal(EltVT))
Bob Wilson14b21412010-05-19 18:48:32 +00003162 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00003163 unsigned NumElems = VT.getVectorNumElements();
3164 SmallVector<SDValue, 8> Ops;
3165 for (unsigned i = 0; i != NumElems; ++i) {
3166 if (Mask[i] < 0) {
3167 Ops.push_back(DAG.getUNDEF(EltVT));
3168 continue;
3169 }
3170 unsigned Idx = Mask[i];
3171 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00003172 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3173 Node->getOperand(0),
3174 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00003175 else
Bill Wendling775db972009-12-23 00:28:23 +00003176 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3177 Node->getOperand(1),
3178 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00003179 }
3180 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
3181 Results.push_back(Tmp1);
3182 break;
3183 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003184 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00003185 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00003186 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3187 // 1 -> Hi
3188 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3189 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003190 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00003191 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3192 } else {
3193 // 0 -> Lo
3194 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3195 Node->getOperand(0));
3196 }
3197 Results.push_back(Tmp1);
3198 break;
3199 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003200 case ISD::STACKSAVE:
3201 // Expand to CopyFromReg if the target set
3202 // StackPointerRegisterToSaveRestore.
3203 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00003204 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3205 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003206 Results.push_back(Results[0].getValue(1));
3207 } else {
Bill Wendling775db972009-12-23 00:28:23 +00003208 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003209 Results.push_back(Node->getOperand(0));
3210 }
3211 break;
3212 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00003213 // Expand to CopyToReg if the target set
3214 // StackPointerRegisterToSaveRestore.
3215 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3216 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3217 Node->getOperand(1)));
3218 } else {
3219 Results.push_back(Node->getOperand(0));
3220 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003221 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003222 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00003223 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003224 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003225 case ISD::FNEG:
3226 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3227 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3228 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3229 Node->getOperand(0));
3230 Results.push_back(Tmp1);
3231 break;
3232 case ISD::FABS: {
3233 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00003234 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003235 Tmp1 = Node->getOperand(0);
3236 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00003237 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003238 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00003239 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3240 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003241 Results.push_back(Tmp1);
3242 break;
3243 }
3244 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00003245 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3246 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003247 break;
3248 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00003249 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3250 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003251 break;
3252 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00003253 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3254 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003255 break;
3256 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00003257 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3258 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003259 break;
3260 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00003261 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3262 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003263 break;
3264 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00003265 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3266 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003267 break;
3268 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00003269 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3270 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003271 break;
3272 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00003273 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3274 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003275 break;
3276 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00003277 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3278 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003279 break;
3280 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00003281 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3282 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003283 break;
3284 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00003285 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3286 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003287 break;
3288 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003289 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3290 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003291 break;
3292 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003293 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3294 RTLIB::NEARBYINT_F64,
3295 RTLIB::NEARBYINT_F80,
3296 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003297 break;
3298 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003299 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3300 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003301 break;
3302 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003303 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3304 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003305 break;
3306 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003307 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3308 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003309 break;
3310 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003311 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3312 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003313 break;
Cameron Zwarich33390842011-07-08 21:39:21 +00003314 case ISD::FMA:
3315 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
3316 RTLIB::FMA_F80, RTLIB::FMA_PPCF128));
3317 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003318 case ISD::FP16_TO_FP32:
3319 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3320 break;
3321 case ISD::FP32_TO_FP16:
3322 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3323 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003324 case ISD::ConstantFP: {
3325 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003326 // Check to see if this FP immediate is already legal.
3327 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003328 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00003329 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003330 else
Bill Wendling775db972009-12-23 00:28:23 +00003331 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003332 break;
3333 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003334 case ISD::EHSELECTION: {
3335 unsigned Reg = TLI.getExceptionSelectorRegister();
3336 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003337 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3338 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003339 Results.push_back(Results[0].getValue(1));
3340 break;
3341 }
3342 case ISD::EXCEPTIONADDR: {
3343 unsigned Reg = TLI.getExceptionAddressRegister();
3344 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003345 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3346 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003347 Results.push_back(Results[0].getValue(1));
3348 break;
3349 }
3350 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003351 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003352 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3353 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3354 "Don't know how to expand this subtraction!");
3355 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3356 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003357 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3358 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003359 break;
3360 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003361 case ISD::UREM:
3362 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003363 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003364 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003365 bool isSigned = Node->getOpcode() == ISD::SREM;
3366 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3367 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3368 Tmp2 = Node->getOperand(0);
3369 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003370 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3371 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3372 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00003373 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3374 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003375 // X % Y -> X-X/Y*Y
3376 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3377 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3378 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003379 } else if (isSigned)
3380 Tmp1 = ExpandIntLibCall(Node, true,
3381 RTLIB::SREM_I8,
3382 RTLIB::SREM_I16, RTLIB::SREM_I32,
3383 RTLIB::SREM_I64, RTLIB::SREM_I128);
3384 else
3385 Tmp1 = ExpandIntLibCall(Node, false,
3386 RTLIB::UREM_I8,
3387 RTLIB::UREM_I16, RTLIB::UREM_I32,
3388 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003389 Results.push_back(Tmp1);
3390 break;
3391 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003392 case ISD::UDIV:
3393 case ISD::SDIV: {
3394 bool isSigned = Node->getOpcode() == ISD::SDIV;
3395 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003396 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003397 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003398 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3399 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3400 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003401 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3402 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003403 else if (isSigned)
3404 Tmp1 = ExpandIntLibCall(Node, true,
3405 RTLIB::SDIV_I8,
3406 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3407 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3408 else
3409 Tmp1 = ExpandIntLibCall(Node, false,
3410 RTLIB::UDIV_I8,
3411 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3412 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003413 Results.push_back(Tmp1);
3414 break;
3415 }
3416 case ISD::MULHU:
3417 case ISD::MULHS: {
3418 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3419 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003420 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003421 SDVTList VTs = DAG.getVTList(VT, VT);
3422 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3423 "If this wasn't legal, it shouldn't have been created!");
3424 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3425 Node->getOperand(1));
3426 Results.push_back(Tmp1.getValue(1));
3427 break;
3428 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003429 case ISD::SDIVREM:
3430 case ISD::UDIVREM:
3431 // Expand into divrem libcall
3432 ExpandDivRemLibCall(Node, Results);
3433 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003434 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003435 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003436 SDVTList VTs = DAG.getVTList(VT, VT);
3437 // See if multiply or divide can be lowered using two-result operations.
3438 // We just need the low half of the multiply; try both the signed
3439 // and unsigned forms. If the target supports both SMUL_LOHI and
3440 // UMUL_LOHI, form a preference by checking which forms of plain
3441 // MULH it supports.
3442 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3443 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3444 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3445 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3446 unsigned OpToUse = 0;
3447 if (HasSMUL_LOHI && !HasMULHS) {
3448 OpToUse = ISD::SMUL_LOHI;
3449 } else if (HasUMUL_LOHI && !HasMULHU) {
3450 OpToUse = ISD::UMUL_LOHI;
3451 } else if (HasSMUL_LOHI) {
3452 OpToUse = ISD::SMUL_LOHI;
3453 } else if (HasUMUL_LOHI) {
3454 OpToUse = ISD::UMUL_LOHI;
3455 }
3456 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003457 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3458 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003459 break;
3460 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003461 Tmp1 = ExpandIntLibCall(Node, false,
3462 RTLIB::MUL_I8,
3463 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003464 RTLIB::MUL_I64, RTLIB::MUL_I128);
3465 Results.push_back(Tmp1);
3466 break;
3467 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003468 case ISD::SADDO:
3469 case ISD::SSUBO: {
3470 SDValue LHS = Node->getOperand(0);
3471 SDValue RHS = Node->getOperand(1);
3472 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3473 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3474 LHS, RHS);
3475 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003476 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003477
Eli Friedman4bc8c712009-05-27 12:20:41 +00003478 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3479
3480 // LHSSign -> LHS >= 0
3481 // RHSSign -> RHS >= 0
3482 // SumSign -> Sum >= 0
3483 //
3484 // Add:
3485 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3486 // Sub:
3487 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3488 //
3489 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3490 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3491 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3492 Node->getOpcode() == ISD::SADDO ?
3493 ISD::SETEQ : ISD::SETNE);
3494
3495 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3496 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3497
3498 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3499 Results.push_back(Cmp);
3500 break;
3501 }
3502 case ISD::UADDO:
3503 case ISD::USUBO: {
3504 SDValue LHS = Node->getOperand(0);
3505 SDValue RHS = Node->getOperand(1);
3506 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3507 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3508 LHS, RHS);
3509 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003510 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3511 Node->getOpcode () == ISD::UADDO ?
3512 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003513 break;
3514 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003515 case ISD::UMULO:
3516 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003517 EVT VT = Node->getValueType(0);
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003518 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003519 SDValue LHS = Node->getOperand(0);
3520 SDValue RHS = Node->getOperand(1);
3521 SDValue BottomHalf;
3522 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003523 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003524 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3525 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3526 bool isSigned = Node->getOpcode() == ISD::SMULO;
3527 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3528 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3529 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3530 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3531 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3532 RHS);
3533 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003534 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3535 VT.getSizeInBits() * 2))) {
Eli Friedmandb3c1692009-06-16 06:58:29 +00003536 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3537 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3538 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3539 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3540 DAG.getIntPtrConstant(0));
3541 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3542 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003543 } else {
3544 // We can fall back to a libcall with an illegal type for the MUL if we
3545 // have a libcall big enough.
3546 // Also, we can fall back to a division in some cases, but that's a big
3547 // performance hit in the general case.
Eric Christopher38a18262011-01-20 00:29:24 +00003548 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3549 if (WideVT == MVT::i16)
3550 LC = RTLIB::MUL_I16;
3551 else if (WideVT == MVT::i32)
3552 LC = RTLIB::MUL_I32;
3553 else if (WideVT == MVT::i64)
3554 LC = RTLIB::MUL_I64;
3555 else if (WideVT == MVT::i128)
3556 LC = RTLIB::MUL_I128;
3557 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanf316eb72011-05-16 22:09:53 +00003558
3559 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003560 // part.
3561 unsigned LoSize = VT.getSizeInBits();
3562 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
3563 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
3564 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
3565 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
Owen Anderson95771af2011-02-25 21:41:48 +00003566
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003567 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3568 // pre-lowered to the correct types. This all depends upon WideVT not
3569 // being a legal type for the architecture and thus has to be split to
3570 // two arguments.
3571 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3572 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3573 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3574 DAG.getIntPtrConstant(0));
3575 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3576 DAG.getIntPtrConstant(1));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003577 }
Dan Gohmanf316eb72011-05-16 22:09:53 +00003578
Eli Friedmandb3c1692009-06-16 06:58:29 +00003579 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003580 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3581 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003582 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3583 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3584 ISD::SETNE);
3585 } else {
3586 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3587 DAG.getConstant(0, VT), ISD::SETNE);
3588 }
3589 Results.push_back(BottomHalf);
3590 Results.push_back(TopHalf);
3591 break;
3592 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003593 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003594 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003595 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3596 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003597 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003598 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003599 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003600 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003601 break;
3602 }
Eli Friedman509150f2009-05-27 07:58:35 +00003603 case ISD::SELECT:
3604 Tmp1 = Node->getOperand(0);
3605 Tmp2 = Node->getOperand(1);
3606 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003607 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003608 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3609 Tmp2, Tmp3,
3610 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003611 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003612 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3613 DAG.getConstant(0, Tmp1.getValueType()),
3614 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003615 }
Eli Friedman509150f2009-05-27 07:58:35 +00003616 Results.push_back(Tmp1);
3617 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003618 case ISD::BR_JT: {
3619 SDValue Chain = Node->getOperand(0);
3620 SDValue Table = Node->getOperand(1);
3621 SDValue Index = Node->getOperand(2);
3622
Owen Andersone50ed302009-08-10 22:56:29 +00003623 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003624
3625 const TargetData &TD = *TLI.getTargetData();
3626 unsigned EntrySize =
3627 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003628
Chris Lattner071c62f2010-01-25 23:26:13 +00003629 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003630 Index, DAG.getConstant(EntrySize, PTy));
3631 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3632
Owen Anderson23b9b192009-08-12 00:36:31 +00003633 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003634 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003635 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003636 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003637 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003638 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003639 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003640 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003641 // RelocBase can be JumpTable, GOT or some sort of global base.
3642 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3643 TLI.getPICJumpTableRelocBase(Table, DAG));
3644 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003645 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003646 Results.push_back(Tmp1);
3647 break;
3648 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003649 case ISD::BRCOND:
3650 // Expand brcond's setcc into its constituent parts and create a BR_CC
3651 // Node.
3652 Tmp1 = Node->getOperand(0);
3653 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003654 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003656 Tmp1, Tmp2.getOperand(2),
3657 Tmp2.getOperand(0), Tmp2.getOperand(1),
3658 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003659 } else {
Stuart Hastings88882242011-05-13 00:51:54 +00003660 // We test only the i1 bit. Skip the AND if UNDEF.
3661 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3662 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3663 DAG.getConstant(1, Tmp2.getValueType()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003664 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastings88882242011-05-13 00:51:54 +00003665 DAG.getCondCode(ISD::SETNE), Tmp3,
3666 DAG.getConstant(0, Tmp3.getValueType()),
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003667 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003668 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003669 Results.push_back(Tmp1);
3670 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003671 case ISD::SETCC: {
3672 Tmp1 = Node->getOperand(0);
3673 Tmp2 = Node->getOperand(1);
3674 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003675 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003676
3677 // If we expanded the SETCC into an AND/OR, return the new node
3678 if (Tmp2.getNode() == 0) {
3679 Results.push_back(Tmp1);
3680 break;
3681 }
3682
3683 // Otherwise, SETCC for the given comparison type must be completely
3684 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003685 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003686 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3687 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3688 Results.push_back(Tmp1);
3689 break;
3690 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003691 case ISD::SELECT_CC: {
3692 Tmp1 = Node->getOperand(0); // LHS
3693 Tmp2 = Node->getOperand(1); // RHS
3694 Tmp3 = Node->getOperand(2); // True
3695 Tmp4 = Node->getOperand(3); // False
3696 SDValue CC = Node->getOperand(4);
3697
3698 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003699 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003700
3701 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3702 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3703 CC = DAG.getCondCode(ISD::SETNE);
3704 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3705 Tmp3, Tmp4, CC);
3706 Results.push_back(Tmp1);
3707 break;
3708 }
3709 case ISD::BR_CC: {
3710 Tmp1 = Node->getOperand(0); // Chain
3711 Tmp2 = Node->getOperand(2); // LHS
3712 Tmp3 = Node->getOperand(3); // RHS
3713 Tmp4 = Node->getOperand(1); // CC
3714
3715 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003716 Tmp2, Tmp3, Tmp4, dl);
Stuart Hastings567cac02011-04-19 20:09:38 +00003717 assert(LastCALLSEQ.size() == 1 && "branch inside CALLSEQ_BEGIN/END?");
Stuart Hastingsfc521632011-04-19 16:16:58 +00003718 setLastCALLSEQ(DAG.getEntryNode());
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003719
3720 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3721 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3722 Tmp4 = DAG.getCondCode(ISD::SETNE);
3723 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3724 Tmp3, Node->getOperand(4));
3725 Results.push_back(Tmp1);
3726 break;
3727 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003728 case ISD::GLOBAL_OFFSET_TABLE:
3729 case ISD::GlobalAddress:
3730 case ISD::GlobalTLSAddress:
3731 case ISD::ExternalSymbol:
3732 case ISD::ConstantPool:
3733 case ISD::JumpTable:
3734 case ISD::INTRINSIC_W_CHAIN:
3735 case ISD::INTRINSIC_WO_CHAIN:
3736 case ISD::INTRINSIC_VOID:
3737 // FIXME: Custom lowering for these operations shouldn't return null!
3738 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3739 Results.push_back(SDValue(Node, i));
3740 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003741 }
3742}
3743void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3744 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003745 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003746 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003747 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003748 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003749 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003750 }
Owen Andersone50ed302009-08-10 22:56:29 +00003751 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003752 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003753 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003754 switch (Node->getOpcode()) {
3755 case ISD::CTTZ:
3756 case ISD::CTLZ:
3757 case ISD::CTPOP:
3758 // Zero extend the argument.
3759 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3760 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003761 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003762 if (Node->getOpcode() == ISD::CTTZ) {
3763 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003764 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003765 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3766 ISD::SETEQ);
3767 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3768 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3769 } else if (Node->getOpcode() == ISD::CTLZ) {
3770 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3771 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3772 DAG.getConstant(NVT.getSizeInBits() -
3773 OVT.getSizeInBits(), NVT));
3774 }
Bill Wendling775db972009-12-23 00:28:23 +00003775 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003776 break;
3777 case ISD::BSWAP: {
3778 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003779 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003780 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3781 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003782 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003783 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003784 break;
3785 }
3786 case ISD::FP_TO_UINT:
3787 case ISD::FP_TO_SINT:
3788 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3789 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3790 Results.push_back(Tmp1);
3791 break;
3792 case ISD::UINT_TO_FP:
3793 case ISD::SINT_TO_FP:
3794 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3795 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3796 Results.push_back(Tmp1);
3797 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003798 case ISD::AND:
3799 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003800 case ISD::XOR: {
3801 unsigned ExtOp, TruncOp;
3802 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003803 ExtOp = ISD::BITCAST;
3804 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003805 } else {
3806 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003807 ExtOp = ISD::ANY_EXTEND;
3808 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003809 }
3810 // Promote each of the values to the new type.
3811 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3812 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3813 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003814 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3815 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003816 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003817 }
3818 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003819 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003820 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003821 ExtOp = ISD::BITCAST;
3822 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003823 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003824 ExtOp = ISD::ANY_EXTEND;
3825 TruncOp = ISD::TRUNCATE;
3826 } else {
3827 ExtOp = ISD::FP_EXTEND;
3828 TruncOp = ISD::FP_ROUND;
3829 }
3830 Tmp1 = Node->getOperand(0);
3831 // Promote each of the values to the new type.
3832 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3833 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3834 // Perform the larger operation, then round down.
3835 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3836 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003837 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003838 else
Bill Wendling775db972009-12-23 00:28:23 +00003839 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003840 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003841 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003842 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003843 }
Eli Friedman509150f2009-05-27 07:58:35 +00003844 case ISD::VECTOR_SHUFFLE: {
3845 SmallVector<int, 8> Mask;
3846 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3847
3848 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003849 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3850 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003851
3852 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003853 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003854 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003855 Results.push_back(Tmp1);
3856 break;
3857 }
Eli Friedmanad754602009-05-28 03:56:57 +00003858 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003859 unsigned ExtOp = ISD::FP_EXTEND;
3860 if (NVT.isInteger()) {
3861 ISD::CondCode CCCode =
3862 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3863 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003864 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003865 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3866 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003867 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3868 Tmp1, Tmp2, Node->getOperand(2)));
3869 break;
3870 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003871 }
3872}
3873
Chris Lattner3e928bb2005-01-07 07:47:09 +00003874// SelectionDAG::Legalize - This is the entry point for the file.
3875//
Dan Gohman975716a2011-05-16 22:19:54 +00003876void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003877 /// run - This is the main entry point to this class.
3878 ///
Dan Gohman975716a2011-05-16 22:19:54 +00003879 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003880}