blob: aadfa2642034a2c0eeba32f7cc13402df581342a [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:
Stuart Hastings567cac02011-04-19 20:09:38 +0000915 assert(LastCALLSEQ.size() == 1 && "branch inside CALLSEQ_BEGIN/END?");
Stuart Hastingsfc521632011-04-19 16:16:58 +0000916 // Branches tweak the chain to include LastCALLSEQ
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Stuart Hastingsfc521632011-04-19 16:16:58 +0000918 getLastCALLSEQ());
Eli Friedman8c377c72009-05-27 01:25:56 +0000919 Ops[0] = LegalizeOp(Ops[0]);
Stuart Hastingsfc521632011-04-19 16:16:58 +0000920 setLastCALLSEQ(DAG.getEntryNode());
Eli Friedman8c377c72009-05-27 01:25:56 +0000921 break;
922 case ISD::SHL:
923 case ISD::SRL:
924 case ISD::SRA:
925 case ISD::ROTL:
926 case ISD::ROTR:
927 // Legalizing shifts/rotates requires adjusting the shift amount
928 // to the appropriate width.
929 if (!Ops[1].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000930 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
931 Ops[1]));
Eli Friedman8c377c72009-05-27 01:25:56 +0000932 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000933 case ISD::SRL_PARTS:
934 case ISD::SRA_PARTS:
935 case ISD::SHL_PARTS:
936 // Legalizing shifts/rotates requires adjusting the shift amount
937 // to the appropriate width.
938 if (!Ops[2].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000939 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
940 Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000941 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000942 }
943
Dan Gohman027657d2010-06-18 15:30:29 +0000944 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
945 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000946 switch (Action) {
947 case TargetLowering::Legal:
948 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
949 ResultVals.push_back(Result.getValue(i));
950 break;
951 case TargetLowering::Custom:
952 // FIXME: The handling for custom lowering with multiple results is
953 // a complete mess.
954 Tmp1 = TLI.LowerOperation(Result, DAG);
955 if (Tmp1.getNode()) {
956 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
957 if (e == 1)
958 ResultVals.push_back(Tmp1);
959 else
960 ResultVals.push_back(Tmp1.getValue(i));
961 }
962 break;
963 }
964
965 // FALL THROUGH
966 case TargetLowering::Expand:
967 ExpandNode(Result.getNode(), ResultVals);
968 break;
969 case TargetLowering::Promote:
970 PromoteNode(Result.getNode(), ResultVals);
971 break;
972 }
973 if (!ResultVals.empty()) {
974 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
975 if (ResultVals[i] != SDValue(Node, i))
976 ResultVals[i] = LegalizeOp(ResultVals[i]);
977 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
978 }
979 return ResultVals[Op.getResNo()];
980 }
981 }
982
983 switch (Node->getOpcode()) {
984 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000985#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +0000986 dbgs() << "NODE: ";
987 Node->dump( &DAG);
988 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000989#endif
Chris Lattner35a38932010-04-07 23:47:51 +0000990 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000991
Chris Lattnerb2827b02006-03-19 00:52:58 +0000992 case ISD::BUILD_VECTOR:
993 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +0000994 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +0000995 case TargetLowering::Custom:
996 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000997 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000998 Result = Tmp3;
999 break;
1000 }
1001 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001002 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001003 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001004 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001005 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001006 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001007 case ISD::CALLSEQ_START: {
1008 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Stuart Hastingsfc521632011-04-19 16:16:58 +00001009 assert(CallEnd && "didn't find CALLSEQ_END!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001010
Chris Lattner6831a812006-02-13 09:18:02 +00001011 // Recursively Legalize all of the inputs of the call end that do not lead
1012 // to this call start. This ensures that any libcalls that need be inserted
1013 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001014 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001015 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001016 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001017 NodesLeadingTo);
1018 }
Chris Lattner6831a812006-02-13 09:18:02 +00001019
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001020 // Now that we have legalized all of the inputs (which may have inserted
1021 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001022 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1023
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001024 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001025 // call ended.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001026 if (getLastCALLSEQ().getOpcode() != ISD::EntryToken) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001027 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Stuart Hastingsfc521632011-04-19 16:16:58 +00001028 Tmp1, getLastCALLSEQ());
Chris Lattnerb248e162006-05-17 17:55:45 +00001029 Tmp1 = LegalizeOp(Tmp1);
1030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
Chris Lattner6831a812006-02-13 09:18:02 +00001032 // Do not try to legalize the target-specific arguments (#1+).
1033 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001034 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001035 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001036 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1037 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001038 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001039
Chris Lattner6831a812006-02-13 09:18:02 +00001040 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001041 AddLegalizedOperand(Op.getValue(0), Result);
1042 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1043 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001044
Chris Lattner6831a812006-02-13 09:18:02 +00001045 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001046 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001047 // process, no libcalls can/will be inserted, guaranteeing that no calls
1048 // can overlap.
Chris Lattner6831a812006-02-13 09:18:02 +00001049 // Note that we are selecting this call!
Stuart Hastingsfc521632011-04-19 16:16:58 +00001050 setLastCALLSEQ(SDValue(CallEnd, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001051
Chris Lattner6831a812006-02-13 09:18:02 +00001052 // Legalize the call, starting from the CALLSEQ_END.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001053 LegalizeOp(getLastCALLSEQ());
Chris Lattner6831a812006-02-13 09:18:02 +00001054 return Result;
1055 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001056 case ISD::CALLSEQ_END:
Stuart Hastingsfc521632011-04-19 16:16:58 +00001057 {
1058 SDNode *myCALLSEQ_BEGIN = FindCallStartFromCallEnd(Node);
1059
Dan Gohmanf316eb72011-05-16 22:09:53 +00001060 // If the CALLSEQ_START node hasn't been legalized first, legalize it.
1061 // This will cause this node to be legalized as well as handling libcalls
1062 // right.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001063 if (getLastCALLSEQ().getNode() != Node) {
1064 LegalizeOp(SDValue(myCALLSEQ_BEGIN, 0));
1065 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
1066 assert(I != LegalizedNodes.end() &&
1067 "Legalizing the call start should have legalized this node!");
1068 return I->second;
1069 }
1070
1071 pushLastCALLSEQ(SDValue(myCALLSEQ_BEGIN, 0));
Chris Lattner6831a812006-02-13 09:18:02 +00001072 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001073
1074 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001075 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001076 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001077 // Do not try to legalize the target-specific arguments (#1+), except for
1078 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001079 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001080 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001081 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001082 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001083 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1084 &Ops[0], Ops.size()),
1085 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001086 }
1087 } else {
1088 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1089 if (Tmp1 != Node->getOperand(0) ||
1090 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001091 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001092 Ops[0] = Tmp1;
1093 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001094 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1095 &Ops[0], Ops.size()),
1096 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001097 }
Chris Lattner6a542892006-01-24 05:48:21 +00001098 }
Chris Lattner6831a812006-02-13 09:18:02 +00001099 // This finishes up call legalization.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001100 popLastCALLSEQ();
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001101
Chris Lattner4b653a02006-02-14 00:55:02 +00001102 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001103 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001104 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001105 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001106 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001107 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001108 LoadSDNode *LD = cast<LoadSDNode>(Node);
1109 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1110 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001111
Evan Cheng466685d2006-10-09 20:57:25 +00001112 ISD::LoadExtType ExtType = LD->getExtensionType();
1113 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001114 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001115 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1116 Tmp1, Tmp2, LD->getOffset()),
1117 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001118 Tmp3 = Result.getValue(0);
1119 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001120
Evan Cheng466685d2006-10-09 20:57:25 +00001121 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001122 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001123 case TargetLowering::Legal:
1124 // If this is an unaligned load and the target doesn't support it,
1125 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001126 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001127 Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
Evan Chenge96507c2009-08-15 08:38:52 +00001128 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001129 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001130 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001131 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001132 Tmp3 = Result.getOperand(0);
1133 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001134 Tmp3 = LegalizeOp(Tmp3);
1135 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001136 }
1137 }
1138 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001139 case TargetLowering::Custom:
1140 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001141 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001142 Tmp3 = LegalizeOp(Tmp1);
1143 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001144 }
Evan Cheng466685d2006-10-09 20:57:25 +00001145 break;
1146 case TargetLowering::Promote: {
1147 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001148 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001149 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001150 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001151
Chris Lattnerecf42c42010-09-21 16:36:31 +00001152 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001153 LD->isVolatile(), LD->isNonTemporal(),
1154 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001155 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001156 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001157 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001158 }
Evan Cheng466685d2006-10-09 20:57:25 +00001159 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001160 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001161 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001162 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1163 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001164 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001165 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001166
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001167 EVT SrcVT = LD->getMemoryVT();
1168 unsigned SrcWidth = SrcVT.getSizeInBits();
1169 unsigned Alignment = LD->getAlignment();
1170 bool isVolatile = LD->isVolatile();
1171 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001172
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001173 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1174 // Some targets pretend to have an i1 loading operation, and actually
1175 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1176 // bits are guaranteed to be zero; it helps the optimizers understand
1177 // that these bits are zero. It is also useful for EXTLOAD, since it
1178 // tells the optimizers that those bits are undefined. It would be
1179 // nice to have an effective generic way of getting these benefits...
1180 // Until such a way is found, don't insist on promoting i1 here.
1181 (SrcVT != MVT::i1 ||
1182 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1183 // Promote to a byte-sized load if not loading an integral number of
1184 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1185 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1186 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1187 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001188
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001189 // The extra bits are guaranteed to be zero, since we stored them that
1190 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001191
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001192 ISD::LoadExtType NewExtType =
1193 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001194
Stuart Hastingsa9011292011-02-16 16:23:55 +00001195 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001196 Tmp1, Tmp2, LD->getPointerInfo(),
1197 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001198
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001199 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001200
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001201 if (ExtType == ISD::SEXTLOAD)
1202 // Having the top bits zero doesn't help when sign extending.
1203 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1204 Result.getValueType(),
1205 Result, DAG.getValueType(SrcVT));
1206 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1207 // All the top bits are guaranteed to be zero - inform the optimizers.
1208 Result = DAG.getNode(ISD::AssertZext, dl,
1209 Result.getValueType(), Result,
1210 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001211
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001212 Tmp1 = LegalizeOp(Result);
1213 Tmp2 = LegalizeOp(Ch);
1214 } else if (SrcWidth & (SrcWidth - 1)) {
1215 // If not loading a power-of-2 number of bits, expand as two loads.
1216 assert(!SrcVT.isVector() && "Unsupported extload!");
1217 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1218 assert(RoundWidth < SrcWidth);
1219 unsigned ExtraWidth = SrcWidth - RoundWidth;
1220 assert(ExtraWidth < RoundWidth);
1221 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1222 "Load size not an integral number of bytes!");
1223 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1224 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1225 SDValue Lo, Hi, Ch;
1226 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001227
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001228 if (TLI.isLittleEndian()) {
1229 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1230 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001231 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001232 Tmp1, Tmp2,
1233 LD->getPointerInfo(), RoundVT, isVolatile,
1234 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001235
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001236 // Load the remaining ExtraWidth bits.
1237 IncrementSize = RoundWidth / 8;
1238 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1239 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001240 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001241 LD->getPointerInfo().getWithOffset(IncrementSize),
1242 ExtraVT, isVolatile, isNonTemporal,
1243 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001244
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001245 // Build a factor node to remember that this load is independent of
1246 // the other one.
1247 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1248 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001249
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001250 // Move the top bits to the right place.
1251 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001252 DAG.getConstant(RoundWidth,
1253 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001254
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001255 // Join the hi and lo parts.
1256 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001257 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001258 // Big endian - avoid unaligned loads.
1259 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1260 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001261 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001262 LD->getPointerInfo(), RoundVT, isVolatile,
1263 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001264
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001265 // Load the remaining ExtraWidth bits.
1266 IncrementSize = RoundWidth / 8;
1267 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1268 DAG.getIntPtrConstant(IncrementSize));
1269 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001270 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001271 LD->getPointerInfo().getWithOffset(IncrementSize),
1272 ExtraVT, isVolatile, isNonTemporal,
1273 MinAlign(Alignment, IncrementSize));
1274
1275 // Build a factor node to remember that this load is independent of
1276 // the other one.
1277 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1278 Hi.getValue(1));
1279
1280 // Move the top bits to the right place.
1281 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001282 DAG.getConstant(ExtraWidth,
1283 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001284
1285 // Join the hi and lo parts.
1286 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001287 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001288
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001289 Tmp1 = LegalizeOp(Result);
1290 Tmp2 = LegalizeOp(Ch);
1291 } else {
1292 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1293 default: assert(0 && "This action is not supported yet!");
1294 case TargetLowering::Custom:
1295 isCustom = true;
1296 // FALLTHROUGH
1297 case TargetLowering::Legal:
1298 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1299 Tmp1, Tmp2, LD->getOffset()),
1300 Result.getResNo());
1301 Tmp1 = Result.getValue(0);
1302 Tmp2 = Result.getValue(1);
1303
1304 if (isCustom) {
1305 Tmp3 = TLI.LowerOperation(Result, DAG);
1306 if (Tmp3.getNode()) {
1307 Tmp1 = LegalizeOp(Tmp3);
1308 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1309 }
1310 } else {
1311 // If this is an unaligned load and the target doesn't support it,
1312 // expand it.
1313 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001314 Type *Ty =
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001315 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1316 unsigned ABIAlignment =
1317 TLI.getTargetData()->getABITypeAlignment(Ty);
1318 if (LD->getAlignment() < ABIAlignment){
1319 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1320 DAG, TLI);
1321 Tmp1 = Result.getOperand(0);
1322 Tmp2 = Result.getOperand(1);
1323 Tmp1 = LegalizeOp(Tmp1);
1324 Tmp2 = LegalizeOp(Tmp2);
1325 }
1326 }
1327 }
1328 break;
1329 case TargetLowering::Expand:
Dan Gohman75b10042011-07-15 22:39:09 +00001330 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001331 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1332 LD->getPointerInfo(),
1333 LD->isVolatile(), LD->isNonTemporal(),
1334 LD->getAlignment());
1335 unsigned ExtendOp;
1336 switch (ExtType) {
1337 case ISD::EXTLOAD:
1338 ExtendOp = (SrcVT.isFloatingPoint() ?
1339 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1340 break;
1341 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1342 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1343 default: llvm_unreachable("Unexpected extend load type!");
1344 }
1345 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1346 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1347 Tmp2 = LegalizeOp(Load.getValue(1));
1348 break;
1349 }
Nadav Rotemc2492c22011-06-14 08:11:52 +00001350
1351 // If this is a promoted vector load, and the vector element types are
1352 // legal, then scalarize it.
1353 if (ExtType == ISD::EXTLOAD && SrcVT.isVector() &&
Dan Gohman75b10042011-07-15 22:39:09 +00001354 TLI.isTypeLegal(Node->getValueType(0).getScalarType())) {
Nadav Rotemc2492c22011-06-14 08:11:52 +00001355 SmallVector<SDValue, 8> LoadVals;
1356 SmallVector<SDValue, 8> LoadChains;
1357 unsigned NumElem = SrcVT.getVectorNumElements();
1358 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
1359
1360 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1361 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1362 DAG.getIntPtrConstant(Stride));
1363 SDValue ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl,
1364 Node->getValueType(0).getScalarType(),
1365 Tmp1, Tmp2, LD->getPointerInfo().getWithOffset(Idx * Stride),
1366 SrcVT.getScalarType(),
1367 LD->isVolatile(), LD->isNonTemporal(),
1368 LD->getAlignment());
1369
1370 LoadVals.push_back(ScalarLoad.getValue(0));
1371 LoadChains.push_back(ScalarLoad.getValue(1));
1372 }
1373 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1374 &LoadChains[0], LoadChains.size());
1375 SDValue ValRes = DAG.getNode(ISD::BUILD_VECTOR, dl,
1376 Node->getValueType(0), &LoadVals[0], LoadVals.size());
1377
1378 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1379 Tmp2 = LegalizeOp(Result.getValue(0)); // Relegalize new nodes.
1380 break;
1381 }
1382
1383 // If this is a promoted vector load, and the vector element types are
1384 // illegal, create the promoted vector from bitcasted segments.
1385 if (ExtType == ISD::EXTLOAD && SrcVT.isVector()) {
1386 EVT MemElemTy = Node->getValueType(0).getScalarType();
1387 EVT SrcSclrTy = SrcVT.getScalarType();
1388 unsigned SizeRatio =
1389 (MemElemTy.getSizeInBits() / SrcSclrTy.getSizeInBits());
1390
1391 SmallVector<SDValue, 8> LoadVals;
1392 SmallVector<SDValue, 8> LoadChains;
1393 unsigned NumElem = SrcVT.getVectorNumElements();
1394 unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8;
1395
1396 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1397 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1398 DAG.getIntPtrConstant(Stride));
1399 SDValue ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl,
1400 SrcVT.getScalarType(),
1401 Tmp1, Tmp2, LD->getPointerInfo().getWithOffset(Idx * Stride),
1402 SrcVT.getScalarType(),
1403 LD->isVolatile(), LD->isNonTemporal(),
1404 LD->getAlignment());
1405 if (TLI.isBigEndian()) {
1406 // MSB (which is garbage, comes first)
1407 LoadVals.push_back(ScalarLoad.getValue(0));
1408 for (unsigned i = 0; i<SizeRatio-1; ++i)
1409 LoadVals.push_back(DAG.getUNDEF(SrcVT.getScalarType()));
1410 } else {
1411 // LSB (which is data, comes first)
1412 for (unsigned i = 0; i<SizeRatio-1; ++i)
1413 LoadVals.push_back(DAG.getUNDEF(SrcVT.getScalarType()));
1414 LoadVals.push_back(ScalarLoad.getValue(0));
1415 }
1416 LoadChains.push_back(ScalarLoad.getValue(1));
1417 }
1418
1419 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1420 &LoadChains[0], LoadChains.size());
1421 EVT TempWideVector = EVT::getVectorVT(*DAG.getContext(),
1422 SrcVT.getScalarType(), NumElem*SizeRatio);
1423 SDValue ValRes = DAG.getNode(ISD::BUILD_VECTOR, dl,
1424 TempWideVector, &LoadVals[0], LoadVals.size());
1425
1426 // Cast to the correct type
1427 ValRes = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), ValRes);
1428
1429 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1430 Tmp2 = LegalizeOp(Result.getValue(0)); // Relegalize new nodes.
1431 break;
1432
1433 }
1434
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001435 // FIXME: This does not work for vectors on most targets. Sign- and
1436 // zero-extend operations are currently folded into extending loads,
1437 // whether they are legal or not, and then we end up here without any
1438 // support for legalizing them.
1439 assert(ExtType != ISD::EXTLOAD &&
1440 "EXTLOAD should always be supported!");
1441 // Turn the unsupported load into an EXTLOAD followed by an explicit
1442 // zero/sign extend inreg.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001443 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001444 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1445 LD->isVolatile(), LD->isNonTemporal(),
1446 LD->getAlignment());
1447 SDValue ValRes;
1448 if (ExtType == ISD::SEXTLOAD)
1449 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1450 Result.getValueType(),
1451 Result, DAG.getValueType(SrcVT));
1452 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001453 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001454 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1455 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1456 break;
1457 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001458 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001459
1460 // Since loads produce two values, make sure to remember that we legalized
1461 // both of them.
1462 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1463 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1464 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001465 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001466 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001467 StoreSDNode *ST = cast<StoreSDNode>(Node);
1468 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1469 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001470 unsigned Alignment = ST->getAlignment();
1471 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001472 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001473
Evan Cheng8b2794a2006-10-13 21:14:26 +00001474 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001475 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1476 Result = SDValue(OptStore, 0);
1477 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001478 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001479
Eli Friedman957bffa2009-05-24 08:42:01 +00001480 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001481 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001482 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1483 Tmp1, Tmp3, Tmp2,
1484 ST->getOffset()),
1485 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001486
Owen Andersone50ed302009-08-10 22:56:29 +00001487 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001488 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001489 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001490 case TargetLowering::Legal:
1491 // If this is an unaligned store and the target doesn't support it,
1492 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001493 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001494 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001495 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001496 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001497 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1498 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001499 }
1500 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001501 case TargetLowering::Custom:
1502 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001503 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001504 break;
1505 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001506 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001507 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001508 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001509 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001510 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001511 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001512 break;
1513 }
1514 break;
1515 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001516 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001517 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001518
Owen Andersone50ed302009-08-10 22:56:29 +00001519 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001520 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001521
Duncan Sands83ec4b62008-06-06 12:08:01 +00001522 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001523 // Promote to a byte-sized store with upper bits zero if not
1524 // storing an integral number of bytes. For example, promote
1525 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001526 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1527 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001528 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001529 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1530 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001531 } else if (StWidth & (StWidth - 1)) {
1532 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001533 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001534 unsigned RoundWidth = 1 << Log2_32(StWidth);
1535 assert(RoundWidth < StWidth);
1536 unsigned ExtraWidth = StWidth - RoundWidth;
1537 assert(ExtraWidth < RoundWidth);
1538 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1539 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001540 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1541 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001542 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001543 unsigned IncrementSize;
1544
1545 if (TLI.isLittleEndian()) {
1546 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1547 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001548 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1549 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001550 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001551
1552 // Store the remaining ExtraWidth bits.
1553 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001554 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001555 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001556 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001557 DAG.getConstant(RoundWidth,
1558 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001559 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1560 ST->getPointerInfo().getWithOffset(IncrementSize),
1561 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001562 MinAlign(Alignment, IncrementSize));
1563 } else {
1564 // Big endian - avoid unaligned stores.
1565 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1566 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001567 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001568 DAG.getConstant(ExtraWidth,
1569 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001570 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1571 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001572
1573 // Store the remaining ExtraWidth bits.
1574 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001575 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001576 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001577 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1578 ST->getPointerInfo().getWithOffset(IncrementSize),
1579 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001580 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001581 }
Duncan Sands7e857202008-01-22 07:17:34 +00001582
1583 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001585 } else {
1586 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1587 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001588 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1589 Tmp1, Tmp3, Tmp2,
1590 ST->getOffset()),
1591 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001592
1593 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001594 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001595 case TargetLowering::Legal:
1596 // If this is an unaligned store and the target doesn't support it,
1597 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001598 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001599 Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001600 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001601 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001602 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1603 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001604 }
1605 break;
1606 case TargetLowering::Custom:
1607 Result = TLI.LowerOperation(Result, DAG);
1608 break;
Dan Gohmane63e5ab2011-07-15 22:51:43 +00001609 case TargetLowering::Expand:
Nadav Rotemc2492c22011-06-14 08:11:52 +00001610
1611 EVT WideScalarVT = Tmp3.getValueType().getScalarType();
1612 EVT NarrowScalarVT = StVT.getScalarType();
1613
1614 // The Store type is illegal, must scalarize the vector store.
1615 SmallVector<SDValue, 8> Stores;
Dan Gohman75b10042011-07-15 22:39:09 +00001616 bool ScalarLegal = TLI.isTypeLegal(WideScalarVT);
1617 if (!TLI.isTypeLegal(StVT) && StVT.isVector() && ScalarLegal) {
Nadav Rotemc2492c22011-06-14 08:11:52 +00001618 unsigned NumElem = StVT.getVectorNumElements();
1619
1620 unsigned ScalarSize = StVT.getScalarType().getSizeInBits();
1621 // Round odd types to the next pow of two.
1622 if (!isPowerOf2_32(ScalarSize))
1623 ScalarSize = NextPowerOf2(ScalarSize);
1624 // Types smaller than 8 bits are promoted to 8 bits.
1625 ScalarSize = std::max<unsigned>(ScalarSize, 8);
1626 // Store stride
1627 unsigned Stride = ScalarSize/8;
1628 assert(isPowerOf2_32(Stride) && "Stride must be a power of two");
1629
1630 for (unsigned Idx=0; Idx<NumElem; Idx++) {
1631 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1632 WideScalarVT, Tmp3, DAG.getIntPtrConstant(Idx));
1633
1634
1635 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), ScalarSize);
1636
1637 Ex = DAG.getNode(ISD::TRUNCATE, dl, NVT, Ex);
1638 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1639 DAG.getIntPtrConstant(Stride));
1640 SDValue Store = DAG.getStore(Tmp1, dl, Ex, Tmp2,
1641 ST->getPointerInfo().getWithOffset(Idx*Stride),
1642 isVolatile, isNonTemporal, Alignment);
1643 Stores.push_back(Store);
1644 }
1645 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1646 &Stores[0], Stores.size());
1647 break;
1648 }
1649
1650 // The Store type is illegal, must scalarize the vector store.
1651 // However, the scalar type is illegal. Must bitcast the result
1652 // and store it in smaller parts.
Dan Gohman75b10042011-07-15 22:39:09 +00001653 if (!TLI.isTypeLegal(StVT) && StVT.isVector()) {
Nadav Rotemc2492c22011-06-14 08:11:52 +00001654 unsigned WideNumElem = StVT.getVectorNumElements();
1655 unsigned Stride = NarrowScalarVT.getSizeInBits()/8;
1656
1657 unsigned SizeRatio =
1658 (WideScalarVT.getSizeInBits() / NarrowScalarVT.getSizeInBits());
1659
1660 EVT CastValueVT = EVT::getVectorVT(*DAG.getContext(), NarrowScalarVT,
1661 SizeRatio*WideNumElem);
1662
1663 // Cast the wide elem vector to wider vec with smaller elem type.
1664 // Example <2 x i64> -> <4 x i32>
1665 Tmp3 = DAG.getNode(ISD::BITCAST, dl, CastValueVT, Tmp3);
1666
1667 for (unsigned Idx=0; Idx<WideNumElem*SizeRatio; Idx++) {
1668 // Extract elment i
1669 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
1670 NarrowScalarVT, Tmp3, DAG.getIntPtrConstant(Idx));
1671 // bump pointer.
1672 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1673 DAG.getIntPtrConstant(Stride));
1674
1675 // Store if, this element is:
1676 // - First element on big endian, or
1677 // - Last element on little endian
1678 if (( TLI.isBigEndian() && (Idx%SizeRatio == 0)) ||
1679 ((!TLI.isBigEndian() && (Idx%SizeRatio == SizeRatio-1)))) {
1680 SDValue Store = DAG.getStore(Tmp1, dl, Ex, Tmp2,
1681 ST->getPointerInfo().getWithOffset(Idx*Stride),
1682 isVolatile, isNonTemporal, Alignment);
1683 Stores.push_back(Store);
1684 }
1685 }
1686 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1687 &Stores[0], Stores.size());
1688 break;
1689 }
1690
1691
Duncan Sands7e857202008-01-22 07:17:34 +00001692 // TRUNCSTORE:i16 i32 -> STORE i16
Dan Gohman75b10042011-07-15 22:39:09 +00001693 assert(TLI.isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001694 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001695 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1696 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001697 break;
1698 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001699 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001700 }
1701 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001702 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001703 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001704 assert(Result.getValueType() == Op.getValueType() &&
1705 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001706
Chris Lattner456a93a2006-01-28 07:39:30 +00001707 // Make sure that the generated code is itself legal.
1708 if (Result != Op)
1709 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001710
Chris Lattner45982da2005-05-12 16:53:42 +00001711 // Note that LegalizeOp may be reentered even from single-use nodes, which
1712 // means that we always must cache transformed nodes.
1713 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001714 return Result;
1715}
1716
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001717SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1718 SDValue Vec = Op.getOperand(0);
1719 SDValue Idx = Op.getOperand(1);
1720 DebugLoc dl = Op.getDebugLoc();
1721 // Store the value to a temporary stack slot, then LOAD the returned part.
1722 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001723 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1724 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001725
1726 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001727 unsigned EltSize =
1728 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001729 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1730 DAG.getConstant(EltSize, Idx.getValueType()));
1731
1732 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1733 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1734 else
1735 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1736
1737 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1738
Eli Friedmanc680ac92009-07-09 22:01:03 +00001739 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001740 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001741 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001742 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001743 MachinePointerInfo(),
1744 Vec.getValueType().getVectorElementType(),
1745 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001746}
1747
David Greenecfe33c42011-01-26 19:13:22 +00001748SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1749 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1750
1751 SDValue Vec = Op.getOperand(0);
1752 SDValue Part = Op.getOperand(1);
1753 SDValue Idx = Op.getOperand(2);
1754 DebugLoc dl = Op.getDebugLoc();
1755
1756 // Store the value to a temporary stack slot, then LOAD the returned part.
1757
1758 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1759 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1760 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1761
1762 // First store the whole vector.
1763 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1764 false, false, 0);
1765
1766 // Then store the inserted part.
1767
1768 // Add the offset to the index.
1769 unsigned EltSize =
1770 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1771
1772 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1773 DAG.getConstant(EltSize, Idx.getValueType()));
1774
1775 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1776 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1777 else
1778 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1779
1780 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1781 StackPtr);
1782
1783 // Store the subvector.
1784 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1785 MachinePointerInfo(), false, false, 0);
1786
1787 // Finally, load the updated vector.
1788 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1789 false, false, 0);
1790}
1791
Eli Friedman7ef3d172009-06-06 07:04:42 +00001792SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1793 // We can't handle this case efficiently. Allocate a sufficiently
1794 // aligned object on the stack, store each element into it, then load
1795 // the result as a vector.
1796 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001797 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001798 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001799 DebugLoc dl = Node->getDebugLoc();
1800 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001801 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001802 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001803
1804 // Emit a store of each element to the stack slot.
1805 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001806 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001807 // Store (in the right endianness) the elements to memory.
1808 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1809 // Ignore undef elements.
1810 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1811
1812 unsigned Offset = TypeByteSize*i;
1813
1814 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1815 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1816
Dan Gohman9949dd62010-02-25 20:30:49 +00001817 // If the destination vector element type is narrower than the source
1818 // element type, only store the bits necessary.
1819 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001820 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001821 Node->getOperand(i), Idx,
1822 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001823 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001824 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001825 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001826 Node->getOperand(i), Idx,
1827 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001828 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001829 }
1830
1831 SDValue StoreChain;
1832 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001834 &Stores[0], Stores.size());
1835 else
1836 StoreChain = DAG.getEntryNode();
1837
1838 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001839 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001840}
1841
Eli Friedman4bc8c712009-05-27 12:20:41 +00001842SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1843 DebugLoc dl = Node->getDebugLoc();
1844 SDValue Tmp1 = Node->getOperand(0);
1845 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001846
1847 // Get the sign bit of the RHS. First obtain a value that has the same
1848 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001849 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001850 EVT FloatVT = Tmp2.getValueType();
1851 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Dan Gohman75b10042011-07-15 22:39:09 +00001852 if (TLI.isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001853 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001854 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001855 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001856 // Store the float to memory, then load the sign part out as an integer.
1857 MVT LoadTy = TLI.getPointerTy();
1858 // First create a temporary that is aligned for both the load and store.
1859 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1860 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001861 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001862 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001863 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001864 if (TLI.isBigEndian()) {
1865 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1866 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001867 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1868 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001869 } else { // Little endian
1870 SDValue LoadPtr = StackPtr;
1871 // The float may be wider than the integer we are going to load. Advance
1872 // the pointer so that the loaded integer will contain the sign bit.
1873 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1874 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1875 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1876 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1877 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001878 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1879 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001880 // Move the sign bit to the top bit of the loaded integer.
1881 unsigned BitShift = LoadTy.getSizeInBits() -
1882 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1883 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1884 if (BitShift)
1885 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001886 DAG.getConstant(BitShift,
1887 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001888 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001889 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001890 // Now get the sign bit proper, by seeing whether the value is negative.
1891 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1892 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1893 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001894 // Get the absolute value of the result.
1895 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1896 // Select between the nabs and abs value based on the sign bit of
1897 // the input.
1898 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1899 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1900 AbsVal);
1901}
1902
Eli Friedman4bc8c712009-05-27 12:20:41 +00001903void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1904 SmallVectorImpl<SDValue> &Results) {
1905 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1906 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1907 " not tell us which reg is the stack pointer!");
1908 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001909 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001910 SDValue Tmp1 = SDValue(Node, 0);
1911 SDValue Tmp2 = SDValue(Node, 1);
1912 SDValue Tmp3 = Node->getOperand(2);
1913 SDValue Chain = Tmp1.getOperand(0);
1914
1915 // Chain the dynamic stack allocation so that it doesn't modify the stack
1916 // pointer when other instructions are using the stack.
1917 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1918
1919 SDValue Size = Tmp2.getOperand(1);
1920 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1921 Chain = SP.getValue(1);
1922 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001923 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001924 if (Align > StackAlign)
1925 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1926 DAG.getConstant(-(uint64_t)Align, VT));
1927 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1928 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1929
1930 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1931 DAG.getIntPtrConstant(0, true), SDValue());
1932
1933 Results.push_back(Tmp1);
1934 Results.push_back(Tmp2);
1935}
1936
Evan Cheng7f042682008-10-15 02:05:31 +00001937/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001938/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001939/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001940void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001941 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001942 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001943 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001944 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001945 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1946 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001947 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001948 case TargetLowering::Legal:
1949 // Nothing to do.
1950 break;
1951 case TargetLowering::Expand: {
1952 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1953 unsigned Opc = 0;
1954 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001955 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001956 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1957 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1958 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1959 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1960 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1961 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1962 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1963 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1964 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1965 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1966 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1967 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001968 // FIXME: Implement more expansions.
1969 }
1970
Dale Johannesenbb5da912009-02-02 20:41:04 +00001971 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1972 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1973 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001974 RHS = SDValue();
1975 CC = SDValue();
1976 break;
1977 }
1978 }
1979}
1980
Chris Lattner1401d152008-01-16 07:45:30 +00001981/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1982/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1983/// a load from the stack slot to DestVT, extending it if needed.
1984/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001985SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001986 EVT SlotVT,
1987 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001988 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001989 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001990 unsigned SrcAlign =
1991 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001992 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001993 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Evan Chengff89dcb2009-10-18 18:16:27 +00001995 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1996 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001997 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001998
Duncan Sands83ec4b62008-06-06 12:08:01 +00001999 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2000 unsigned SlotSize = SlotVT.getSizeInBits();
2001 unsigned DestSize = DestVT.getSizeInBits();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002002 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
Evan Chengadf97992010-04-15 01:25:27 +00002003 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00002004
Chris Lattner1401d152008-01-16 07:45:30 +00002005 // Emit a store to the stack slot. Use a truncstore if the input value is
2006 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00002008
Chris Lattner1401d152008-01-16 07:45:30 +00002009 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002010 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002011 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002012 else {
2013 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002014 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002015 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002016 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002017
Chris Lattner35481892005-12-23 00:16:34 +00002018 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002019 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002020 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00002021 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002022
Chris Lattner1401d152008-01-16 07:45:30 +00002023 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00002024 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00002025 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002026}
2027
Dan Gohman475871a2008-07-27 21:46:04 +00002028SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002029 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002030 // Create a vector sized/aligned stack slot, store the value to element #0,
2031 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002033
Evan Chengff89dcb2009-10-18 18:16:27 +00002034 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
2035 int SPFI = StackPtrFI->getIndex();
2036
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002037 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2038 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00002039 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00002040 Node->getValueType(0).getVectorElementType(),
2041 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00002042 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00002043 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00002044 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002045}
2046
2047
Chris Lattnerce872152006-03-19 06:31:19 +00002048/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002049/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002050SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002051 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00002052 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002053 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002054 EVT VT = Node->getValueType(0);
2055 EVT OpVT = Node->getOperand(0).getValueType();
2056 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002057
2058 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002059 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002060 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002061 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00002062 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002063 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002064 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00002065 if (V.getOpcode() == ISD::UNDEF)
2066 continue;
2067 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00002068 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002069 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00002070 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00002071
2072 if (!Value1.getNode()) {
2073 Value1 = V;
2074 } else if (!Value2.getNode()) {
2075 if (V != Value1)
2076 Value2 = V;
2077 } else if (V != Value1 && V != Value2) {
2078 MoreThanTwoValues = true;
2079 }
Chris Lattnerce872152006-03-19 06:31:19 +00002080 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002081
Eli Friedman7a5e5552009-06-07 06:52:44 +00002082 if (!Value1.getNode())
2083 return DAG.getUNDEF(VT);
2084
2085 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002086 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002087
Chris Lattner2eb86532006-03-24 07:29:17 +00002088 // If all elements are constants, create a load from the constant pool.
2089 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002090 std::vector<Constant*> CV;
2091 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002092 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002093 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002094 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002095 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002096 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00002097 if (OpVT==EltVT)
2098 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
2099 else {
2100 // If OpVT and EltVT don't match, EltVT is not legal and the
2101 // element values have been promoted/truncated earlier. Undo this;
2102 // we don't want a v16i8 to become a v16i32 for example.
2103 const ConstantInt *CI = V->getConstantIntValue();
2104 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
2105 CI->getZExtValue()));
2106 }
Chris Lattner2eb86532006-03-24 07:29:17 +00002107 } else {
2108 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002109 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002110 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00002111 }
2112 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00002113 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002114 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002115 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002116 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002117 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00002118 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002119 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002120
Eli Friedman7a5e5552009-06-07 06:52:44 +00002121 if (!MoreThanTwoValues) {
2122 SmallVector<int, 8> ShuffleVec(NumElems, -1);
2123 for (unsigned i = 0; i < NumElems; ++i) {
2124 SDValue V = Node->getOperand(i);
2125 if (V.getOpcode() == ISD::UNDEF)
2126 continue;
2127 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2128 }
2129 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002130 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00002131 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2132 SDValue Vec2;
2133 if (Value2.getNode())
2134 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2135 else
2136 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Chris Lattner87100e02006-03-20 01:52:29 +00002138 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00002139 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00002140 }
2141 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002142
Eli Friedman7ef3d172009-06-06 07:04:42 +00002143 // Otherwise, we can't handle this case efficiently.
2144 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00002145}
2146
Chris Lattner77e77a62005-01-21 06:05:23 +00002147// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2148// does not fit into a register, return the lo part and set the hi part to the
2149// by-reg argument. If it does fit into a single register, return the result
2150// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002151SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002152 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002153 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002154 // Legalizing the call will automatically add the previous call to the
2155 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002156 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002157
Chris Lattner77e77a62005-01-21 06:05:23 +00002158 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002159 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002160 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002161 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002162 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00002163 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002164 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002165 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002166 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002167 }
Bill Wendling056292f2008-09-16 21:48:12 +00002168 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002169 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002170
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002171 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002172 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00002173
2174 // isTailCall may be true since the callee does not reference caller stack
2175 // frame. Check if it's in the right position.
2176 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002177 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002178 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002179 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002180 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002181 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002182
Evan Cheng3d2125c2010-11-30 23:55:39 +00002183 if (!CallInfo.second.getNode())
2184 // It's a tailcall, return the chain (which is the DAG root).
2185 return DAG.getRoot();
2186
Chris Lattner6831a812006-02-13 09:18:02 +00002187 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002188 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Chris Lattner6831a812006-02-13 09:18:02 +00002189 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2190 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002191 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002192}
2193
Dan Gohmanf316eb72011-05-16 22:09:53 +00002194/// ExpandLibCall - Generate a libcall taking the given operands as arguments
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002195/// and returning a result of type RetVT.
2196SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
2197 const SDValue *Ops, unsigned NumOps,
2198 bool isSigned, DebugLoc dl) {
2199 TargetLowering::ArgListTy Args;
2200 Args.reserve(NumOps);
Dan Gohmanf316eb72011-05-16 22:09:53 +00002201
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002202 TargetLowering::ArgListEntry Entry;
2203 for (unsigned i = 0; i != NumOps; ++i) {
2204 Entry.Node = Ops[i];
2205 Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
2206 Entry.isSExt = isSigned;
2207 Entry.isZExt = !isSigned;
2208 Args.push_back(Entry);
2209 }
2210 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2211 TLI.getPointerTy());
Dan Gohmanf316eb72011-05-16 22:09:53 +00002212
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002213 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002214 std::pair<SDValue,SDValue> CallInfo =
2215 TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
2216 false, 0, TLI.getLibcallCallingConv(LC), false,
2217 /*isReturnValueUsed=*/true,
2218 Callee, Args, DAG, dl);
Dan Gohmanf316eb72011-05-16 22:09:53 +00002219
Eric Christopherabbbfbd2011-04-20 01:19:45 +00002220 // Legalize the call sequence, starting with the chain. This will advance
2221 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2222 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2223 LegalizeOp(CallInfo.second);
2224
2225 return CallInfo.first;
2226}
2227
Jim Grosbache03262f2010-06-18 21:43:38 +00002228// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
2229// ExpandLibCall except that the first operand is the in-chain.
2230std::pair<SDValue, SDValue>
2231SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2232 SDNode *Node,
2233 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00002234 SDValue InChain = Node->getOperand(0);
2235
2236 TargetLowering::ArgListTy Args;
2237 TargetLowering::ArgListEntry Entry;
2238 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2239 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002240 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00002241 Entry.Node = Node->getOperand(i);
2242 Entry.Ty = ArgTy;
2243 Entry.isSExt = isSigned;
2244 Entry.isZExt = !isSigned;
2245 Args.push_back(Entry);
2246 }
2247 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2248 TLI.getPointerTy());
2249
2250 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002251 Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Jim Grosbache03262f2010-06-18 21:43:38 +00002252 std::pair<SDValue, SDValue> CallInfo =
2253 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002254 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002255 /*isReturnValueUsed=*/true,
2256 Callee, Args, DAG, Node->getDebugLoc());
2257
2258 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002259 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Jim Grosbache03262f2010-06-18 21:43:38 +00002260 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2261 LegalizeOp(CallInfo.second);
2262 return CallInfo;
2263}
2264
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002265SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2266 RTLIB::Libcall Call_F32,
2267 RTLIB::Libcall Call_F64,
2268 RTLIB::Libcall Call_F80,
2269 RTLIB::Libcall Call_PPCF128) {
2270 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002272 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002273 case MVT::f32: LC = Call_F32; break;
2274 case MVT::f64: LC = Call_F64; break;
2275 case MVT::f80: LC = Call_F80; break;
2276 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002277 }
2278 return ExpandLibCall(LC, Node, false);
2279}
2280
2281SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002282 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002283 RTLIB::Libcall Call_I16,
2284 RTLIB::Libcall Call_I32,
2285 RTLIB::Libcall Call_I64,
2286 RTLIB::Libcall Call_I128) {
2287 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002289 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002290 case MVT::i8: LC = Call_I8; break;
2291 case MVT::i16: LC = Call_I16; break;
2292 case MVT::i32: LC = Call_I32; break;
2293 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002294 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002295 }
2296 return ExpandLibCall(LC, Node, isSigned);
2297}
2298
Evan Cheng65279cb2011-04-16 03:08:26 +00002299/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
2300static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
2301 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002302 RTLIB::Libcall LC;
2303 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2304 default: assert(0 && "Unexpected request for libcall!");
2305 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2306 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2307 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2308 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2309 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2310 }
2311
Evan Cheng65279cb2011-04-16 03:08:26 +00002312 return TLI.getLibcallName(LC) != 0;
2313}
Evan Cheng8e23e812011-04-01 00:42:02 +00002314
Evan Cheng65279cb2011-04-16 03:08:26 +00002315/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
2316/// needed.
2317static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002318 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00002319 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00002320 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002321 else
Evan Cheng8e23e812011-04-01 00:42:02 +00002322 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002323
Evan Cheng8e23e812011-04-01 00:42:02 +00002324 SDValue Op0 = Node->getOperand(0);
2325 SDValue Op1 = Node->getOperand(1);
2326 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2327 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2328 SDNode *User = *UI;
2329 if (User == Node)
2330 continue;
2331 if (User->getOpcode() == OtherOpcode &&
2332 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00002333 User->getOperand(1) == Op1)
2334 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00002335 }
Evan Cheng65279cb2011-04-16 03:08:26 +00002336 return false;
2337}
Evan Cheng8e23e812011-04-01 00:42:02 +00002338
Evan Cheng65279cb2011-04-16 03:08:26 +00002339/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
2340/// pairs.
2341void
2342SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2343 SmallVectorImpl<SDValue> &Results) {
2344 unsigned Opcode = Node->getOpcode();
2345 bool isSigned = Opcode == ISD::SDIVREM;
2346
2347 RTLIB::Libcall LC;
2348 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2349 default: assert(0 && "Unexpected request for libcall!");
2350 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2351 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2352 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2353 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2354 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00002355 }
2356
2357 // The input chain to this libcall is the entry node of the function.
2358 // Legalizing the call will automatically add the previous call to the
2359 // dependence.
2360 SDValue InChain = DAG.getEntryNode();
2361
2362 EVT RetVT = Node->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002363 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00002364
2365 TargetLowering::ArgListTy Args;
2366 TargetLowering::ArgListEntry Entry;
2367 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2368 EVT ArgVT = Node->getOperand(i).getValueType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002369 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Evan Cheng8e23e812011-04-01 00:42:02 +00002370 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
2371 Entry.isSExt = isSigned;
2372 Entry.isZExt = !isSigned;
2373 Args.push_back(Entry);
2374 }
2375
2376 // Also pass the return address of the remainder.
2377 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2378 Entry.Node = FIPtr;
2379 Entry.Ty = RetTy->getPointerTo();
2380 Entry.isSExt = isSigned;
2381 Entry.isZExt = !isSigned;
2382 Args.push_back(Entry);
2383
2384 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2385 TLI.getPointerTy());
2386
2387 // Splice the libcall in wherever FindInputOutputChains tells us to.
2388 DebugLoc dl = Node->getDebugLoc();
2389 std::pair<SDValue, SDValue> CallInfo =
2390 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
2391 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
2392 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
2393
2394 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002395 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Evan Cheng8e23e812011-04-01 00:42:02 +00002396 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2397 LegalizeOp(CallInfo.second);
2398
2399 // Remainder is loaded back from the stack frame.
Stuart Hastingsfc521632011-04-19 16:16:58 +00002400 SDValue Rem = DAG.getLoad(RetVT, dl, getLastCALLSEQ(), FIPtr,
Evan Cheng8e23e812011-04-01 00:42:02 +00002401 MachinePointerInfo(), false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00002402 Results.push_back(CallInfo.first);
2403 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00002404}
2405
Chris Lattner22cde6a2006-01-28 08:25:58 +00002406/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2407/// INT_TO_FP operation of the specified operand when the target requests that
2408/// we expand it. At this point, we know that the result and operand types are
2409/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002410SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2411 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002412 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002413 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002414 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002415 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002416
Chris Lattner23594d42008-01-16 07:03:22 +00002417 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002418 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002419
Chris Lattner22cde6a2006-01-28 08:25:58 +00002420 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002421 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002422 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002423 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002424 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002425 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002426 if (TLI.isLittleEndian())
2427 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002428
Chris Lattner22cde6a2006-01-28 08:25:58 +00002429 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002430 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002431 if (isSigned) {
2432 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002433 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2434 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002435 } else {
2436 Op0Mapped = Op0;
2437 }
2438 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002439 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002440 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002441 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002442 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002443 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002444 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002445 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2446 MachinePointerInfo(),
2447 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002448 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002449 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2450 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002451 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002452 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002453 BitsToDouble(0x4330000080000000ULL) :
2454 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002455 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002456 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002457 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002458 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002459 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002460 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002461 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002462 // do nothing
2463 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002464 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002465 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002466 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002468 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002469 }
2470 return Result;
2471 }
2472 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002473 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002474
2475 // Implementation of unsigned i64 to f64 following the algorithm in
2476 // __floatundidf in compiler_rt. This implementation has the advantage
2477 // of performing rounding correctly, both in the default rounding mode
2478 // and in all alternate rounding modes.
2479 // TODO: Generalize this for use with other types.
2480 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2481 SDValue TwoP52 =
2482 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2483 SDValue TwoP84PlusTwoP52 =
2484 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2485 SDValue TwoP84 =
2486 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2487
2488 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2489 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2490 DAG.getConstant(32, MVT::i64));
2491 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2492 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002493 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2494 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002495 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2496 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002497 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2498 }
2499
Owen Anderson3a9e7692010-10-05 17:24:05 +00002500 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002501 // TODO: Generalize this for use with other types.
2502 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002503 // For unsigned conversions, convert them to signed conversions using the
2504 // algorithm from the x86_64 __floatundidf in compiler_rt.
2505 if (!isSigned) {
2506 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002507
Owen Anderson95771af2011-02-25 21:41:48 +00002508 SDValue ShiftConst =
2509 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002510 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2511 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2512 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2513 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002514
Owen Anderson3a9e7692010-10-05 17:24:05 +00002515 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2516 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002517
Owen Anderson3a9e7692010-10-05 17:24:05 +00002518 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002519 // select. We happen to get lucky and machinesink does the right
2520 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002521 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002522 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002523 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2524 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2525 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002526
Owen Anderson3a9e7692010-10-05 17:24:05 +00002527 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002528
Jim Grosbach6e992612010-07-02 17:41:59 +00002529 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002530 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2531 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2532 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002533 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002534 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2535 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2536 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2537 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2538 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2539 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002540 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002541 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002542 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002543
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002544 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2545 DAG.getConstant(32, SHVT));
2546 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2547 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2548 SDValue TwoP32 =
2549 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2550 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2551 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2552 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2553 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2554 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2555 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002556 }
2557
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002558 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002559
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002560 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2561 Op0, DAG.getConstant(0, Op0.getValueType()),
2562 ISD::SETLT);
2563 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2564 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2565 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002566
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002567 // If the sign bit of the integer is set, the large number will be treated
2568 // as a negative number. To counteract this, the dynamic code adds an
2569 // offset depending on the data type.
2570 uint64_t FF;
2571 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002572 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002573 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2574 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2575 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2576 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2577 }
2578 if (TLI.isLittleEndian()) FF <<= 32;
2579 Constant *FudgeFactor = ConstantInt::get(
2580 Type::getInt64Ty(*DAG.getContext()), FF);
2581
2582 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2583 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2584 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2585 Alignment = std::min(Alignment, 4u);
2586 SDValue FudgeInReg;
2587 if (DestVT == MVT::f32)
2588 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002589 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002590 false, false, Alignment);
2591 else {
2592 FudgeInReg =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002593 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002594 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002595 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002596 MVT::f32, false, false, Alignment));
2597 }
2598
2599 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002600}
2601
2602/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2603/// *INT_TO_FP operation of the specified operand when the target requests that
2604/// we promote it. At this point, we know that the result and operand types are
2605/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2606/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002607SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002608 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002609 bool isSigned,
2610 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002611 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002612 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002613
2614 unsigned OpToUse = 0;
2615
2616 // Scan for the appropriate larger type to use.
2617 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002618 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002619 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002620
2621 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002622 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2623 OpToUse = ISD::SINT_TO_FP;
2624 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002625 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002626 if (isSigned) continue;
2627
2628 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002629 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2630 OpToUse = ISD::UINT_TO_FP;
2631 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002632 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002633
2634 // Otherwise, try a larger type.
2635 }
2636
2637 // Okay, we found the operation and type to use. Zero extend our input to the
2638 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002639 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002640 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002641 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002642}
2643
2644/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2645/// FP_TO_*INT operation of the specified operand when the target requests that
2646/// we promote it. At this point, we know that the result and operand types are
2647/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2648/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002649SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002650 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002651 bool isSigned,
2652 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002653 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002654 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002655
2656 unsigned OpToUse = 0;
2657
2658 // Scan for the appropriate larger type to use.
2659 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002660 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002661 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002662
Eli Friedman3be2e512009-05-28 03:06:16 +00002663 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002664 OpToUse = ISD::FP_TO_SINT;
2665 break;
2666 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002667
Eli Friedman3be2e512009-05-28 03:06:16 +00002668 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002669 OpToUse = ISD::FP_TO_UINT;
2670 break;
2671 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002672
2673 // Otherwise, try a larger type.
2674 }
2675
Scott Michelfdc40a02009-02-17 22:15:04 +00002676
Chris Lattner27a6c732007-11-24 07:07:01 +00002677 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002678 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002679
Chris Lattner27a6c732007-11-24 07:07:01 +00002680 // Truncate the result of the extended FP_TO_*INT operation to the desired
2681 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002682 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002683}
2684
2685/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2686///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002687SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002688 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002689 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002690 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002691 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002692 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002694 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2695 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2696 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002697 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002698 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2699 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2700 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2701 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2702 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2703 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2704 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2705 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2706 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002707 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002708 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2709 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2710 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2711 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2712 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2713 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2714 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2715 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2716 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2717 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2718 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2719 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2720 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2721 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2722 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2723 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2724 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2725 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2726 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2727 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2728 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002729 }
2730}
2731
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002732/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002733// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002734static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2735 APInt Val = APInt(NumBits, ByteVal);
2736 unsigned Shift = 8;
2737 for (unsigned i = NumBits; i > 8; i >>= 1) {
2738 Val = (Val << Shift) | Val;
2739 Shift <<= 1;
2740 }
2741 return Val;
2742}
2743
Chris Lattner22cde6a2006-01-28 08:25:58 +00002744/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2745///
Scott Michelfdc40a02009-02-17 22:15:04 +00002746SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002747 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002748 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002749 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002750 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002751 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002752 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002753 unsigned Len = VT.getSizeInBits();
2754
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002755 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2756 "CTPOP not implemented for this type.");
2757
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002758 // This is the "best" algorithm from
2759 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2760
2761 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2762 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2763 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2764 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2765
2766 // v = v - ((v >> 1) & 0x55555555...)
2767 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2768 DAG.getNode(ISD::AND, dl, VT,
2769 DAG.getNode(ISD::SRL, dl, VT, Op,
2770 DAG.getConstant(1, ShVT)),
2771 Mask55));
2772 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2773 Op = DAG.getNode(ISD::ADD, dl, VT,
2774 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2775 DAG.getNode(ISD::AND, dl, VT,
2776 DAG.getNode(ISD::SRL, dl, VT, Op,
2777 DAG.getConstant(2, ShVT)),
2778 Mask33));
2779 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2780 Op = DAG.getNode(ISD::AND, dl, VT,
2781 DAG.getNode(ISD::ADD, dl, VT, Op,
2782 DAG.getNode(ISD::SRL, dl, VT, Op,
2783 DAG.getConstant(4, ShVT))),
2784 Mask0F);
2785 // v = (v * 0x01010101...) >> (Len - 8)
2786 Op = DAG.getNode(ISD::SRL, dl, VT,
2787 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2788 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002789
Chris Lattner22cde6a2006-01-28 08:25:58 +00002790 return Op;
2791 }
2792 case ISD::CTLZ: {
2793 // for now, we do this:
2794 // x = x | (x >> 1);
2795 // x = x | (x >> 2);
2796 // ...
2797 // x = x | (x >>16);
2798 // x = x | (x >>32); // for 64-bit input
2799 // return popcount(~x);
2800 //
2801 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002802 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002803 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002804 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002805 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002806 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002807 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002808 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002809 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002810 Op = DAG.getNOT(dl, Op, VT);
2811 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002812 }
2813 case ISD::CTTZ: {
2814 // for now, we use: { return popcount(~x & (x - 1)); }
2815 // unless the target has ctlz but not ctpop, in which case we use:
2816 // { return 32 - nlz(~x & (x-1)); }
2817 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002818 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002819 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2820 DAG.getNOT(dl, Op, VT),
2821 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002822 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002823 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002824 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2825 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002826 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002827 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002828 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2829 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002830 }
2831 }
2832}
Chris Lattnere34b3962005-01-19 04:19:40 +00002833
Jim Grosbache03262f2010-06-18 21:43:38 +00002834std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2835 unsigned Opc = Node->getOpcode();
2836 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2837 RTLIB::Libcall LC;
2838
2839 switch (Opc) {
2840 default:
2841 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2842 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002843 case ISD::ATOMIC_SWAP:
2844 switch (VT.SimpleTy) {
2845 default: llvm_unreachable("Unexpected value type for atomic!");
2846 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2847 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2848 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2849 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2850 }
2851 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002852 case ISD::ATOMIC_CMP_SWAP:
2853 switch (VT.SimpleTy) {
2854 default: llvm_unreachable("Unexpected value type for atomic!");
2855 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2856 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2857 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2858 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2859 }
2860 break;
2861 case ISD::ATOMIC_LOAD_ADD:
2862 switch (VT.SimpleTy) {
2863 default: llvm_unreachable("Unexpected value type for atomic!");
2864 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2865 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2866 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2867 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2868 }
2869 break;
2870 case ISD::ATOMIC_LOAD_SUB:
2871 switch (VT.SimpleTy) {
2872 default: llvm_unreachable("Unexpected value type for atomic!");
2873 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2874 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2875 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2876 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2877 }
2878 break;
2879 case ISD::ATOMIC_LOAD_AND:
2880 switch (VT.SimpleTy) {
2881 default: llvm_unreachable("Unexpected value type for atomic!");
2882 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2883 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2884 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2885 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2886 }
2887 break;
2888 case ISD::ATOMIC_LOAD_OR:
2889 switch (VT.SimpleTy) {
2890 default: llvm_unreachable("Unexpected value type for atomic!");
2891 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2892 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2893 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2894 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2895 }
2896 break;
2897 case ISD::ATOMIC_LOAD_XOR:
2898 switch (VT.SimpleTy) {
2899 default: llvm_unreachable("Unexpected value type for atomic!");
2900 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2901 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2902 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2903 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2904 }
2905 break;
2906 case ISD::ATOMIC_LOAD_NAND:
2907 switch (VT.SimpleTy) {
2908 default: llvm_unreachable("Unexpected value type for atomic!");
2909 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2910 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2911 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2912 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2913 }
2914 break;
2915 }
2916
2917 return ExpandChainLibCall(LC, Node, false);
2918}
2919
Eli Friedman8c377c72009-05-27 01:25:56 +00002920void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2921 SmallVectorImpl<SDValue> &Results) {
2922 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002923 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002924 switch (Node->getOpcode()) {
2925 case ISD::CTPOP:
2926 case ISD::CTLZ:
2927 case ISD::CTTZ:
2928 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2929 Results.push_back(Tmp1);
2930 break;
2931 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002932 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002933 break;
2934 case ISD::FRAMEADDR:
2935 case ISD::RETURNADDR:
2936 case ISD::FRAME_TO_ARGS_OFFSET:
2937 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2938 break;
2939 case ISD::FLT_ROUNDS_:
2940 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2941 break;
2942 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002943 case ISD::EH_LABEL:
2944 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002945 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002946 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002947 case ISD::EH_SJLJ_DISPATCHSETUP:
2948 // If the target didn't expand these, there's nothing to do, so just
2949 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002950 Results.push_back(Node->getOperand(0));
2951 break;
2952 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002953 // If the target didn't expand this, just return 'zero' and preserve the
2954 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002955 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002956 Results.push_back(Node->getOperand(0));
2957 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002958 case ISD::MEMBARRIER: {
2959 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2960 TargetLowering::ArgListTy Args;
2961 std::pair<SDValue, SDValue> CallResult =
2962 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002963 false, false, false, false, 0, CallingConv::C,
2964 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002965 /*isReturnValueUsed=*/true,
2966 DAG.getExternalSymbol("__sync_synchronize",
2967 TLI.getPointerTy()),
2968 Args, DAG, dl);
2969 Results.push_back(CallResult.second);
2970 break;
2971 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002972 // By default, atomic intrinsics are marked Legal and lowered. Targets
2973 // which don't support them directly, however, may want libcalls, in which
2974 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002975 case ISD::ATOMIC_SWAP:
2976 case ISD::ATOMIC_LOAD_ADD:
2977 case ISD::ATOMIC_LOAD_SUB:
2978 case ISD::ATOMIC_LOAD_AND:
2979 case ISD::ATOMIC_LOAD_OR:
2980 case ISD::ATOMIC_LOAD_XOR:
2981 case ISD::ATOMIC_LOAD_NAND:
2982 case ISD::ATOMIC_LOAD_MIN:
2983 case ISD::ATOMIC_LOAD_MAX:
2984 case ISD::ATOMIC_LOAD_UMIN:
2985 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002986 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002987 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2988 Results.push_back(Tmp.first);
2989 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002990 break;
Evan Chenga8457062010-06-18 22:01:37 +00002991 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002992 case ISD::DYNAMIC_STACKALLOC:
2993 ExpandDYNAMIC_STACKALLOC(Node, Results);
2994 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002995 case ISD::MERGE_VALUES:
2996 for (unsigned i = 0; i < Node->getNumValues(); i++)
2997 Results.push_back(Node->getOperand(i));
2998 break;
2999 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00003000 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003001 if (VT.isInteger())
3002 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00003003 else {
3004 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00003005 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00003006 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003007 break;
3008 }
3009 case ISD::TRAP: {
3010 // If this operation is not supported, lower it to 'abort()' call
3011 TargetLowering::ArgListTy Args;
3012 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003013 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00003014 false, false, false, false, 0, CallingConv::C,
3015 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003016 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00003017 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003018 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00003019 Results.push_back(CallResult.second);
3020 break;
3021 }
3022 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003023 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00003024 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3025 Node->getValueType(0), dl);
3026 Results.push_back(Tmp1);
3027 break;
3028 case ISD::FP_EXTEND:
3029 Tmp1 = EmitStackConvert(Node->getOperand(0),
3030 Node->getOperand(0).getValueType(),
3031 Node->getValueType(0), dl);
3032 Results.push_back(Tmp1);
3033 break;
3034 case ISD::SIGN_EXTEND_INREG: {
3035 // NOTE: we could fall back on load/store here too for targets without
3036 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00003037 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00003038 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00003039 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00003040 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00003041 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00003042 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
3043 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00003044 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00003045 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3046 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00003047 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3048 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003049 break;
3050 }
3051 case ISD::FP_ROUND_INREG: {
3052 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00003053 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00003054
3055 // NOTE: there is a choice here between constantly creating new stack
3056 // slots and always reusing the same one. We currently always create
3057 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00003058 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00003059 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3060 Node->getValueType(0), dl);
3061 Results.push_back(Tmp1);
3062 break;
3063 }
3064 case ISD::SINT_TO_FP:
3065 case ISD::UINT_TO_FP:
3066 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3067 Node->getOperand(0), Node->getValueType(0), dl);
3068 Results.push_back(Tmp1);
3069 break;
3070 case ISD::FP_TO_UINT: {
3071 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00003072 EVT VT = Node->getOperand(0).getValueType();
3073 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00003074 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00003075 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3076 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3077 Tmp1 = DAG.getConstantFP(apf, VT);
3078 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3079 Node->getOperand(0),
3080 Tmp1, ISD::SETLT);
3081 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003082 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3083 DAG.getNode(ISD::FSUB, dl, VT,
3084 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003085 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3086 DAG.getConstant(x, NVT));
3087 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
3088 Results.push_back(Tmp1);
3089 break;
3090 }
Eli Friedman509150f2009-05-27 07:58:35 +00003091 case ISD::VAARG: {
3092 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00003093 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00003094 Tmp1 = Node->getOperand(0);
3095 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003096 unsigned Align = Node->getConstantOperandVal(3);
3097
Chris Lattnerecf42c42010-09-21 16:36:31 +00003098 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
3099 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003100 SDValue VAList = VAListLoad;
3101
Rafael Espindolacbeeae22010-07-11 04:01:49 +00003102 if (Align > TLI.getMinStackArgumentAlignment()) {
3103 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
3104
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003105 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
3106 DAG.getConstant(Align - 1,
3107 TLI.getPointerTy()));
3108
3109 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00003110 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00003111 TLI.getPointerTy()));
3112 }
3113
Eli Friedman509150f2009-05-27 07:58:35 +00003114 // Increment the pointer, VAList, to the next vaarg
3115 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
3116 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00003117 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00003118 TLI.getPointerTy()));
3119 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00003120 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
3121 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00003122 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00003123 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00003124 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00003125 Results.push_back(Results[0].getValue(1));
3126 break;
3127 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003128 case ISD::VACOPY: {
3129 // This defaults to loading a pointer from the input and storing it to the
3130 // output, returning the chain.
3131 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3132 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3133 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00003134 Node->getOperand(2), MachinePointerInfo(VS),
3135 false, false, 0);
3136 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
3137 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00003138 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003139 break;
3140 }
3141 case ISD::EXTRACT_VECTOR_ELT:
3142 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3143 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003144 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00003145 Node->getOperand(0));
3146 else
3147 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3148 Results.push_back(Tmp1);
3149 break;
3150 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00003151 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00003152 break;
David Greenecfe33c42011-01-26 19:13:22 +00003153 case ISD::INSERT_SUBVECTOR:
3154 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3155 break;
Eli Friedman509150f2009-05-27 07:58:35 +00003156 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00003157 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00003158 break;
3159 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003160 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00003161 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00003162 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003163 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00003164 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3165 Node->getOperand(1),
3166 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00003167 break;
Eli Friedman509150f2009-05-27 07:58:35 +00003168 case ISD::VECTOR_SHUFFLE: {
3169 SmallVector<int, 8> Mask;
3170 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3171
Owen Andersone50ed302009-08-10 22:56:29 +00003172 EVT VT = Node->getValueType(0);
3173 EVT EltVT = VT.getVectorElementType();
Dan Gohman75b10042011-07-15 22:39:09 +00003174 if (!TLI.isTypeLegal(EltVT))
Bob Wilson14b21412010-05-19 18:48:32 +00003175 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00003176 unsigned NumElems = VT.getVectorNumElements();
3177 SmallVector<SDValue, 8> Ops;
3178 for (unsigned i = 0; i != NumElems; ++i) {
3179 if (Mask[i] < 0) {
3180 Ops.push_back(DAG.getUNDEF(EltVT));
3181 continue;
3182 }
3183 unsigned Idx = Mask[i];
3184 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00003185 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3186 Node->getOperand(0),
3187 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00003188 else
Bill Wendling775db972009-12-23 00:28:23 +00003189 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3190 Node->getOperand(1),
3191 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00003192 }
3193 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
3194 Results.push_back(Tmp1);
3195 break;
3196 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003197 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00003198 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00003199 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3200 // 1 -> Hi
3201 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3202 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003203 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00003204 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3205 } else {
3206 // 0 -> Lo
3207 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3208 Node->getOperand(0));
3209 }
3210 Results.push_back(Tmp1);
3211 break;
3212 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003213 case ISD::STACKSAVE:
3214 // Expand to CopyFromReg if the target set
3215 // StackPointerRegisterToSaveRestore.
3216 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00003217 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3218 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003219 Results.push_back(Results[0].getValue(1));
3220 } else {
Bill Wendling775db972009-12-23 00:28:23 +00003221 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003222 Results.push_back(Node->getOperand(0));
3223 }
3224 break;
3225 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00003226 // Expand to CopyToReg if the target set
3227 // StackPointerRegisterToSaveRestore.
3228 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3229 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3230 Node->getOperand(1)));
3231 } else {
3232 Results.push_back(Node->getOperand(0));
3233 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003234 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003235 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00003236 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003237 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003238 case ISD::FNEG:
3239 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3240 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3241 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3242 Node->getOperand(0));
3243 Results.push_back(Tmp1);
3244 break;
3245 case ISD::FABS: {
3246 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00003247 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003248 Tmp1 = Node->getOperand(0);
3249 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00003250 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003251 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00003252 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3253 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003254 Results.push_back(Tmp1);
3255 break;
3256 }
3257 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00003258 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3259 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003260 break;
3261 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00003262 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3263 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003264 break;
3265 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00003266 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3267 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003268 break;
3269 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00003270 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3271 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003272 break;
3273 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00003274 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3275 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003276 break;
3277 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00003278 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3279 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003280 break;
3281 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00003282 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3283 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003284 break;
3285 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00003286 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3287 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003288 break;
3289 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00003290 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3291 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003292 break;
3293 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00003294 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3295 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003296 break;
3297 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00003298 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3299 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003300 break;
3301 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003302 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3303 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003304 break;
3305 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003306 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3307 RTLIB::NEARBYINT_F64,
3308 RTLIB::NEARBYINT_F80,
3309 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003310 break;
3311 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003312 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3313 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003314 break;
3315 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003316 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3317 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003318 break;
3319 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003320 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3321 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003322 break;
3323 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003324 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3325 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003326 break;
Cameron Zwarich33390842011-07-08 21:39:21 +00003327 case ISD::FMA:
3328 Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
3329 RTLIB::FMA_F80, RTLIB::FMA_PPCF128));
3330 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003331 case ISD::FP16_TO_FP32:
3332 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3333 break;
3334 case ISD::FP32_TO_FP16:
3335 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3336 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003337 case ISD::ConstantFP: {
3338 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003339 // Check to see if this FP immediate is already legal.
3340 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003341 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00003342 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003343 else
Bill Wendling775db972009-12-23 00:28:23 +00003344 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003345 break;
3346 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003347 case ISD::EHSELECTION: {
3348 unsigned Reg = TLI.getExceptionSelectorRegister();
3349 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003350 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3351 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003352 Results.push_back(Results[0].getValue(1));
3353 break;
3354 }
3355 case ISD::EXCEPTIONADDR: {
3356 unsigned Reg = TLI.getExceptionAddressRegister();
3357 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003358 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3359 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003360 Results.push_back(Results[0].getValue(1));
3361 break;
3362 }
3363 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003364 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003365 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3366 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3367 "Don't know how to expand this subtraction!");
3368 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3369 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003370 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3371 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003372 break;
3373 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003374 case ISD::UREM:
3375 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003376 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003377 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003378 bool isSigned = Node->getOpcode() == ISD::SREM;
3379 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3380 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3381 Tmp2 = Node->getOperand(0);
3382 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003383 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3384 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3385 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00003386 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3387 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003388 // X % Y -> X-X/Y*Y
3389 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3390 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3391 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003392 } else if (isSigned)
3393 Tmp1 = ExpandIntLibCall(Node, true,
3394 RTLIB::SREM_I8,
3395 RTLIB::SREM_I16, RTLIB::SREM_I32,
3396 RTLIB::SREM_I64, RTLIB::SREM_I128);
3397 else
3398 Tmp1 = ExpandIntLibCall(Node, false,
3399 RTLIB::UREM_I8,
3400 RTLIB::UREM_I16, RTLIB::UREM_I32,
3401 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003402 Results.push_back(Tmp1);
3403 break;
3404 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003405 case ISD::UDIV:
3406 case ISD::SDIV: {
3407 bool isSigned = Node->getOpcode() == ISD::SDIV;
3408 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003409 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003410 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003411 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3412 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3413 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003414 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3415 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003416 else if (isSigned)
3417 Tmp1 = ExpandIntLibCall(Node, true,
3418 RTLIB::SDIV_I8,
3419 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3420 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3421 else
3422 Tmp1 = ExpandIntLibCall(Node, false,
3423 RTLIB::UDIV_I8,
3424 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3425 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003426 Results.push_back(Tmp1);
3427 break;
3428 }
3429 case ISD::MULHU:
3430 case ISD::MULHS: {
3431 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3432 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003433 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003434 SDVTList VTs = DAG.getVTList(VT, VT);
3435 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3436 "If this wasn't legal, it shouldn't have been created!");
3437 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3438 Node->getOperand(1));
3439 Results.push_back(Tmp1.getValue(1));
3440 break;
3441 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003442 case ISD::SDIVREM:
3443 case ISD::UDIVREM:
3444 // Expand into divrem libcall
3445 ExpandDivRemLibCall(Node, Results);
3446 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003447 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003448 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003449 SDVTList VTs = DAG.getVTList(VT, VT);
3450 // See if multiply or divide can be lowered using two-result operations.
3451 // We just need the low half of the multiply; try both the signed
3452 // and unsigned forms. If the target supports both SMUL_LOHI and
3453 // UMUL_LOHI, form a preference by checking which forms of plain
3454 // MULH it supports.
3455 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3456 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3457 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3458 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3459 unsigned OpToUse = 0;
3460 if (HasSMUL_LOHI && !HasMULHS) {
3461 OpToUse = ISD::SMUL_LOHI;
3462 } else if (HasUMUL_LOHI && !HasMULHU) {
3463 OpToUse = ISD::UMUL_LOHI;
3464 } else if (HasSMUL_LOHI) {
3465 OpToUse = ISD::SMUL_LOHI;
3466 } else if (HasUMUL_LOHI) {
3467 OpToUse = ISD::UMUL_LOHI;
3468 }
3469 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003470 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3471 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003472 break;
3473 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003474 Tmp1 = ExpandIntLibCall(Node, false,
3475 RTLIB::MUL_I8,
3476 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003477 RTLIB::MUL_I64, RTLIB::MUL_I128);
3478 Results.push_back(Tmp1);
3479 break;
3480 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003481 case ISD::SADDO:
3482 case ISD::SSUBO: {
3483 SDValue LHS = Node->getOperand(0);
3484 SDValue RHS = Node->getOperand(1);
3485 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3486 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3487 LHS, RHS);
3488 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003489 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003490
Eli Friedman4bc8c712009-05-27 12:20:41 +00003491 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3492
3493 // LHSSign -> LHS >= 0
3494 // RHSSign -> RHS >= 0
3495 // SumSign -> Sum >= 0
3496 //
3497 // Add:
3498 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3499 // Sub:
3500 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3501 //
3502 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3503 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3504 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3505 Node->getOpcode() == ISD::SADDO ?
3506 ISD::SETEQ : ISD::SETNE);
3507
3508 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3509 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3510
3511 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3512 Results.push_back(Cmp);
3513 break;
3514 }
3515 case ISD::UADDO:
3516 case ISD::USUBO: {
3517 SDValue LHS = Node->getOperand(0);
3518 SDValue RHS = Node->getOperand(1);
3519 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3520 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3521 LHS, RHS);
3522 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003523 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3524 Node->getOpcode () == ISD::UADDO ?
3525 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003526 break;
3527 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003528 case ISD::UMULO:
3529 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003530 EVT VT = Node->getValueType(0);
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003531 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003532 SDValue LHS = Node->getOperand(0);
3533 SDValue RHS = Node->getOperand(1);
3534 SDValue BottomHalf;
3535 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003536 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003537 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3538 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3539 bool isSigned = Node->getOpcode() == ISD::SMULO;
3540 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3541 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3542 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3543 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3544 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3545 RHS);
3546 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003547 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3548 VT.getSizeInBits() * 2))) {
Eli Friedmandb3c1692009-06-16 06:58:29 +00003549 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3550 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3551 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3552 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3553 DAG.getIntPtrConstant(0));
3554 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3555 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003556 } else {
3557 // We can fall back to a libcall with an illegal type for the MUL if we
3558 // have a libcall big enough.
3559 // Also, we can fall back to a division in some cases, but that's a big
3560 // performance hit in the general case.
Eric Christopher38a18262011-01-20 00:29:24 +00003561 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3562 if (WideVT == MVT::i16)
3563 LC = RTLIB::MUL_I16;
3564 else if (WideVT == MVT::i32)
3565 LC = RTLIB::MUL_I32;
3566 else if (WideVT == MVT::i64)
3567 LC = RTLIB::MUL_I64;
3568 else if (WideVT == MVT::i128)
3569 LC = RTLIB::MUL_I128;
3570 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
Dan Gohmanf316eb72011-05-16 22:09:53 +00003571
3572 // The high part is obtained by SRA'ing all but one of the bits of low
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003573 // part.
3574 unsigned LoSize = VT.getSizeInBits();
3575 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
3576 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
3577 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
3578 DAG.getConstant(LoSize-1, TLI.getPointerTy()));
Owen Anderson95771af2011-02-25 21:41:48 +00003579
Eric Christopherabbbfbd2011-04-20 01:19:45 +00003580 // Here we're passing the 2 arguments explicitly as 4 arguments that are
3581 // pre-lowered to the correct types. This all depends upon WideVT not
3582 // being a legal type for the architecture and thus has to be split to
3583 // two arguments.
3584 SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
3585 SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
3586 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3587 DAG.getIntPtrConstant(0));
3588 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
3589 DAG.getIntPtrConstant(1));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003590 }
Dan Gohmanf316eb72011-05-16 22:09:53 +00003591
Eli Friedmandb3c1692009-06-16 06:58:29 +00003592 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003593 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3594 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003595 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3596 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3597 ISD::SETNE);
3598 } else {
3599 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3600 DAG.getConstant(0, VT), ISD::SETNE);
3601 }
3602 Results.push_back(BottomHalf);
3603 Results.push_back(TopHalf);
3604 break;
3605 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003606 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003607 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003608 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3609 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003610 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003611 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003612 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003613 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003614 break;
3615 }
Eli Friedman509150f2009-05-27 07:58:35 +00003616 case ISD::SELECT:
3617 Tmp1 = Node->getOperand(0);
3618 Tmp2 = Node->getOperand(1);
3619 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003620 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003621 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3622 Tmp2, Tmp3,
3623 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003624 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003625 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3626 DAG.getConstant(0, Tmp1.getValueType()),
3627 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003628 }
Eli Friedman509150f2009-05-27 07:58:35 +00003629 Results.push_back(Tmp1);
3630 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003631 case ISD::BR_JT: {
3632 SDValue Chain = Node->getOperand(0);
3633 SDValue Table = Node->getOperand(1);
3634 SDValue Index = Node->getOperand(2);
3635
Owen Andersone50ed302009-08-10 22:56:29 +00003636 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003637
3638 const TargetData &TD = *TLI.getTargetData();
3639 unsigned EntrySize =
3640 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003641
Chris Lattner071c62f2010-01-25 23:26:13 +00003642 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003643 Index, DAG.getConstant(EntrySize, PTy));
3644 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3645
Owen Anderson23b9b192009-08-12 00:36:31 +00003646 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003647 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003648 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003649 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003650 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003651 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003652 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003653 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003654 // RelocBase can be JumpTable, GOT or some sort of global base.
3655 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3656 TLI.getPICJumpTableRelocBase(Table, DAG));
3657 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003658 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003659 Results.push_back(Tmp1);
3660 break;
3661 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003662 case ISD::BRCOND:
3663 // Expand brcond's setcc into its constituent parts and create a BR_CC
3664 // Node.
3665 Tmp1 = Node->getOperand(0);
3666 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003667 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003669 Tmp1, Tmp2.getOperand(2),
3670 Tmp2.getOperand(0), Tmp2.getOperand(1),
3671 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003672 } else {
Stuart Hastings88882242011-05-13 00:51:54 +00003673 // We test only the i1 bit. Skip the AND if UNDEF.
3674 Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
3675 DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
3676 DAG.getConstant(1, Tmp2.getValueType()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003677 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Stuart Hastings88882242011-05-13 00:51:54 +00003678 DAG.getCondCode(ISD::SETNE), Tmp3,
3679 DAG.getConstant(0, Tmp3.getValueType()),
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003680 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003681 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003682 Results.push_back(Tmp1);
3683 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003684 case ISD::SETCC: {
3685 Tmp1 = Node->getOperand(0);
3686 Tmp2 = Node->getOperand(1);
3687 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003688 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003689
3690 // If we expanded the SETCC into an AND/OR, return the new node
3691 if (Tmp2.getNode() == 0) {
3692 Results.push_back(Tmp1);
3693 break;
3694 }
3695
3696 // Otherwise, SETCC for the given comparison type must be completely
3697 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003698 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003699 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3700 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3701 Results.push_back(Tmp1);
3702 break;
3703 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003704 case ISD::SELECT_CC: {
3705 Tmp1 = Node->getOperand(0); // LHS
3706 Tmp2 = Node->getOperand(1); // RHS
3707 Tmp3 = Node->getOperand(2); // True
3708 Tmp4 = Node->getOperand(3); // False
3709 SDValue CC = Node->getOperand(4);
3710
3711 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003712 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003713
3714 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3715 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3716 CC = DAG.getCondCode(ISD::SETNE);
3717 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3718 Tmp3, Tmp4, CC);
3719 Results.push_back(Tmp1);
3720 break;
3721 }
3722 case ISD::BR_CC: {
3723 Tmp1 = Node->getOperand(0); // Chain
3724 Tmp2 = Node->getOperand(2); // LHS
3725 Tmp3 = Node->getOperand(3); // RHS
3726 Tmp4 = Node->getOperand(1); // CC
3727
3728 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003729 Tmp2, Tmp3, Tmp4, dl);
Stuart Hastings567cac02011-04-19 20:09:38 +00003730 assert(LastCALLSEQ.size() == 1 && "branch inside CALLSEQ_BEGIN/END?");
Stuart Hastingsfc521632011-04-19 16:16:58 +00003731 setLastCALLSEQ(DAG.getEntryNode());
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003732
3733 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3734 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3735 Tmp4 = DAG.getCondCode(ISD::SETNE);
3736 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3737 Tmp3, Node->getOperand(4));
3738 Results.push_back(Tmp1);
3739 break;
3740 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003741 case ISD::GLOBAL_OFFSET_TABLE:
3742 case ISD::GlobalAddress:
3743 case ISD::GlobalTLSAddress:
3744 case ISD::ExternalSymbol:
3745 case ISD::ConstantPool:
3746 case ISD::JumpTable:
3747 case ISD::INTRINSIC_W_CHAIN:
3748 case ISD::INTRINSIC_WO_CHAIN:
3749 case ISD::INTRINSIC_VOID:
3750 // FIXME: Custom lowering for these operations shouldn't return null!
3751 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3752 Results.push_back(SDValue(Node, i));
3753 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003754 }
3755}
3756void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3757 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003758 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003759 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003760 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003761 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003762 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003763 }
Owen Andersone50ed302009-08-10 22:56:29 +00003764 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003765 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003766 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003767 switch (Node->getOpcode()) {
3768 case ISD::CTTZ:
3769 case ISD::CTLZ:
3770 case ISD::CTPOP:
3771 // Zero extend the argument.
3772 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3773 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003774 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003775 if (Node->getOpcode() == ISD::CTTZ) {
3776 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003777 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003778 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3779 ISD::SETEQ);
3780 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3781 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3782 } else if (Node->getOpcode() == ISD::CTLZ) {
3783 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3784 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3785 DAG.getConstant(NVT.getSizeInBits() -
3786 OVT.getSizeInBits(), NVT));
3787 }
Bill Wendling775db972009-12-23 00:28:23 +00003788 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003789 break;
3790 case ISD::BSWAP: {
3791 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003792 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003793 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3794 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003795 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003796 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003797 break;
3798 }
3799 case ISD::FP_TO_UINT:
3800 case ISD::FP_TO_SINT:
3801 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3802 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3803 Results.push_back(Tmp1);
3804 break;
3805 case ISD::UINT_TO_FP:
3806 case ISD::SINT_TO_FP:
3807 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3808 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3809 Results.push_back(Tmp1);
3810 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003811 case ISD::AND:
3812 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003813 case ISD::XOR: {
3814 unsigned ExtOp, TruncOp;
3815 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003816 ExtOp = ISD::BITCAST;
3817 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003818 } else {
3819 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003820 ExtOp = ISD::ANY_EXTEND;
3821 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003822 }
3823 // Promote each of the values to the new type.
3824 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3825 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3826 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003827 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3828 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003829 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003830 }
3831 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003832 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003833 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003834 ExtOp = ISD::BITCAST;
3835 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003836 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003837 ExtOp = ISD::ANY_EXTEND;
3838 TruncOp = ISD::TRUNCATE;
3839 } else {
3840 ExtOp = ISD::FP_EXTEND;
3841 TruncOp = ISD::FP_ROUND;
3842 }
3843 Tmp1 = Node->getOperand(0);
3844 // Promote each of the values to the new type.
3845 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3846 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3847 // Perform the larger operation, then round down.
3848 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3849 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003850 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003851 else
Bill Wendling775db972009-12-23 00:28:23 +00003852 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003853 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003854 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003855 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003856 }
Eli Friedman509150f2009-05-27 07:58:35 +00003857 case ISD::VECTOR_SHUFFLE: {
3858 SmallVector<int, 8> Mask;
3859 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3860
3861 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003862 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3863 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003864
3865 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003866 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003867 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003868 Results.push_back(Tmp1);
3869 break;
3870 }
Eli Friedmanad754602009-05-28 03:56:57 +00003871 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003872 unsigned ExtOp = ISD::FP_EXTEND;
3873 if (NVT.isInteger()) {
3874 ISD::CondCode CCCode =
3875 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3876 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003877 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003878 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3879 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003880 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3881 Tmp1, Tmp2, Node->getOperand(2)));
3882 break;
3883 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003884 }
3885}
3886
Chris Lattner3e928bb2005-01-07 07:47:09 +00003887// SelectionDAG::Legalize - This is the entry point for the file.
3888//
Dan Gohman975716a2011-05-16 22:19:54 +00003889void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003890 /// run - This is the main entry point to this class.
3891 ///
Dan Gohman975716a2011-05-16 22:19:54 +00003892 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003893}