blob: bc9fa53bd72ce2b9957351ca2fbca566b6367b4a [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
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000028#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000029#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000030#include "llvm/DerivedTypes.h"
Bill Wendling86e6cb92009-02-17 01:04:54 +000031#include "llvm/Function.h"
Devang Patel83489bb2009-01-13 00:35:13 +000032#include "llvm/GlobalVariable.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000033#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000035#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000036#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000037#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000038#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000039#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000040using namespace llvm;
41
42//===----------------------------------------------------------------------===//
43/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44/// hacks on it until the target machine can handle it. This involves
45/// eliminating value sizes the machine cannot handle (promoting small sizes to
46/// large sizes or splitting up large values into small values) as well as
47/// eliminating operations the machine cannot handle.
48///
49/// This code also does a small amount of optimization and recognition of idioms
50/// as part of its processing. For example, if a target does not support a
51/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52/// will attempt merge setcc and brc instructions into brcc's.
53///
54namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
Bill Wendling98a366d2009-04-29 23:29:43 +000058 CodeGenOpt::Level OptLevel;
Chris Lattner3e928bb2005-01-07 07:47:09 +000059
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000061
Chris Lattner6831a812006-02-13 09:18:02 +000062 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000065 SDValue LastCALLSEQ_END;
Scott Michelfdc40a02009-02-17 22:15:04 +000066
Chris Lattner6831a812006-02-13 09:18:02 +000067 /// IsLegalizingCall - This member is used *only* for purposes of providing
Scott Michelfdc40a02009-02-17 22:15:04 +000068 /// helpful assertions that a libcall isn't created while another call is
Chris Lattner6831a812006-02-13 09:18:02 +000069 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
Scott Michelfdc40a02009-02-17 22:15:04 +000071
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 };
Scott Michelfdc40a02009-02-17 22:15:04 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Dan Gohman475871a2008-07-27 21:46:04 +000088 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000089 LegalizedNodes.insert(std::make_pair(From, To));
90 // If someone requests legalization of the new node, return itself.
91 if (From != To)
92 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000093 }
94
Chris Lattner3e928bb2005-01-07 07:47:09 +000095public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000096 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattner3e928bb2005-01-07 07:47:09 +000098 /// getTypeAction - Return how we should legalize values of this type, either
99 /// it is already legal or we need to expand it into multiple registers of
100 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000101 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000102 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 }
104
105 /// isTypeLegal - Return true if this type is legal on this target.
106 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000107 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108 return getTypeAction(VT) == Legal;
109 }
110
Chris Lattner3e928bb2005-01-07 07:47:09 +0000111 void LegalizeDAG();
112
Chris Lattner456a93a2006-01-28 07:39:30 +0000113private:
Dan Gohman7f321562007-06-25 16:23:39 +0000114 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000115 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000116 void HandleOp(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000117
Chris Lattnerc7029802006-03-18 01:44:44 +0000118 /// LegalizeOp - We know that the specified value has a legal type.
119 /// Recursively ensure that the operands have legal types, then return the
120 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Nate Begeman68679912008-04-25 18:07:40 +0000123 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
124 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
125 /// is necessary to spill the vector being inserted into to memory, perform
126 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000127 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000128 SDValue Idx, DebugLoc dl);
129 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
130 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000131
Mon P Wangf007a8b2008-11-06 05:31:54 +0000132 /// Useful 16 element vector type that is used to pass operands for widening.
Scott Michelfdc40a02009-02-17 22:15:04 +0000133 typedef SmallVector<SDValue, 16> SDValueVector;
134
Nate Begeman5a5ca152009-04-29 05:20:52 +0000135 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
136 /// performs the same shuffe in terms of order or result bytes, but on a type
137 /// whose vector element type is narrower than the original shuffle type.
138 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
139 SDValue ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
140 SDValue N1, SDValue N2,
141 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000142
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000143 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000144 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000145
Dale Johannesenbb5da912009-02-02 20:41:04 +0000146 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
147 DebugLoc dl);
148 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
149 DebugLoc dl);
150 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
151 DebugLoc dl) {
152 LegalizeSetCCOperands(LHS, RHS, CC, dl);
153 LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
Evan Cheng7f042682008-10-15 02:05:31 +0000154 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Eli Friedman47b41f72009-05-27 02:21:29 +0000156 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000157 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
158 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
159 RTLIB::Libcall Call_PPCF128);
160 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, RTLIB::Libcall Call_I16,
161 RTLIB::Libcall Call_I32, RTLIB::Libcall Call_I64,
162 RTLIB::Libcall Call_I128);
Chris Lattnercad063f2005-07-16 00:19:57 +0000163
Dale Johannesen8a782a22009-02-02 22:12:50 +0000164 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue ExpandBUILD_VECTOR(SDNode *Node);
166 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesenaf435272009-02-02 19:03:57 +0000167 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
168 DebugLoc dl);
169 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
170 DebugLoc dl);
171 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
172 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000173
Dale Johannesen8a782a22009-02-02 22:12:50 +0000174 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
175 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000176
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000177 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Eli Friedman8c377c72009-05-27 01:25:56 +0000178
179 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
180 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000181};
182}
183
Nate Begeman5a5ca152009-04-29 05:20:52 +0000184/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
185/// performs the same shuffe in terms of order or result bytes, but on a type
186/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000187/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +0000188SDValue
189SelectionDAGLegalize::ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
190 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000191 SmallVectorImpl<int> &Mask) const {
192 MVT EltVT = NVT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +0000193 unsigned NumMaskElts = VT.getVectorNumElements();
194 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000195 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000196
Nate Begeman9008ca62009-04-27 18:41:29 +0000197 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
198
199 if (NumEltsGrowth == 1)
200 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
201
202 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000203 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000204 int Idx = Mask[i];
205 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
206 if (Idx < 0)
207 NewMask.push_back(-1);
208 else
209 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000210 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000211 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000212 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000213 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
214 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000215}
216
Bill Wendling5aa49772009-02-24 02:35:30 +0000217SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000218 CodeGenOpt::Level ol)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000219 : TLI(dag.getTargetLoweringInfo()), DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000220 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000221 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000222 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000223}
224
Chris Lattner3e928bb2005-01-07 07:47:09 +0000225void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000226 LastCALLSEQ_END = DAG.getEntryNode();
227 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000228
Chris Lattnerab510a72005-10-02 17:49:46 +0000229 // The legalize process is inherently a bottom-up recursive process (users
230 // legalize their uses before themselves). Given infinite stack space, we
231 // could just start legalizing on the root and traverse the whole graph. In
232 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000233 // blocks. To avoid this problem, compute an ordering of the nodes where each
234 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000235 DAG.AssignTopologicalOrder();
236 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
237 E = prior(DAG.allnodes_end()); I != next(E); ++I)
238 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000239
240 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000241 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000242 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
243 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000244
Chris Lattner3e928bb2005-01-07 07:47:09 +0000245 LegalizedNodes.clear();
246
247 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000248 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000249}
250
Chris Lattner6831a812006-02-13 09:18:02 +0000251
252/// FindCallEndFromCallStart - Given a chained node that is part of a call
253/// sequence, find the CALLSEQ_END node that terminates the call sequence.
254static SDNode *FindCallEndFromCallStart(SDNode *Node) {
255 if (Node->getOpcode() == ISD::CALLSEQ_END)
256 return Node;
257 if (Node->use_empty())
258 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000259
Chris Lattner6831a812006-02-13 09:18:02 +0000260 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000261 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000262 if (TheChain.getValueType() != MVT::Other) {
263 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000264 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000265 if (TheChain.getValueType() != MVT::Other) {
266 // Otherwise, hunt for it.
267 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
268 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000269 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000270 break;
271 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
273 // Otherwise, we walked into a node without a chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000274 if (TheChain.getValueType() != MVT::Other)
275 return 0;
276 }
277 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
Chris Lattner6831a812006-02-13 09:18:02 +0000279 for (SDNode::use_iterator UI = Node->use_begin(),
280 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Chris Lattner6831a812006-02-13 09:18:02 +0000282 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000283 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000284 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
285 if (User->getOperand(i) == TheChain)
286 if (SDNode *Result = FindCallEndFromCallStart(User))
287 return Result;
288 }
289 return 0;
290}
291
Scott Michelfdc40a02009-02-17 22:15:04 +0000292/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000293/// sequence, find the CALLSEQ_START node that initiates the call sequence.
294static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
295 assert(Node && "Didn't find callseq_start for a call??");
296 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Scott Michelfdc40a02009-02-17 22:15:04 +0000297
Chris Lattner6831a812006-02-13 09:18:02 +0000298 assert(Node->getOperand(0).getValueType() == MVT::Other &&
299 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000300 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000301}
302
303/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000304/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000305/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000306///
307/// Keep track of the nodes we fine that actually do lead to Dest in
308/// NodesLeadingTo. This avoids retraversing them exponential number of times.
309///
310bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000311 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000312 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000313
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000314 // If we've already processed this node and it does lead to Dest, there is no
315 // need to reprocess it.
316 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000317
Chris Lattner6831a812006-02-13 09:18:02 +0000318 // If the first result of this node has been already legalized, then it cannot
319 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000320 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000321
Chris Lattner6831a812006-02-13 09:18:02 +0000322 // Okay, this node has not already been legalized. Check and legalize all
323 // operands. If none lead to Dest, then we can legalize this node.
324 bool OperandsLeadToDest = false;
325 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
326 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000327 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000328
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000329 if (OperandsLeadToDest) {
330 NodesLeadingTo.insert(N);
331 return true;
332 }
Chris Lattner6831a812006-02-13 09:18:02 +0000333
334 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000335 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000336 return false;
337}
338
Mon P Wang0c397192008-10-30 08:01:45 +0000339/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000340/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000341void SelectionDAGLegalize::HandleOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +0000342 // Don't touch TargetConstants
343 if (Op.getOpcode() == ISD::TargetConstant)
344 return;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000345 MVT VT = Op.getValueType();
Eli Friedman1fde9c52009-05-24 02:46:31 +0000346 // We should never see any illegal result types here.
347 assert(isTypeLegal(VT) && "Illegal type introduced after type legalization?");
Eli Friedman74807f22009-05-26 08:55:52 +0000348 (void)LegalizeOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000349}
Chris Lattner6831a812006-02-13 09:18:02 +0000350
Evan Cheng9f877882006-12-13 20:57:08 +0000351/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
352/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000353static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000354 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000355 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000356 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000357
358 // If a FP immediate is precise when represented as a float and if the
359 // target can do an extending load from float to double, we put it into
360 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000361 // double. This shrinks FP constants and canonicalizes them for targets where
362 // an FP extending load is the same cost as a normal load (such as on the x87
363 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000364 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000365 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000366 if (!UseCP) {
Chris Lattnerd2e936a2009-03-08 01:47:41 +0000367 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000368 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000369 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000370 }
371
Duncan Sands83ec4b62008-06-06 12:08:01 +0000372 MVT OrigVT = VT;
373 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000374 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000375 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000376 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
377 // Only do this if the target has a native EXTLOAD instruction from
378 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000379 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000380 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000381 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000382 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
383 VT = SVT;
384 Extend = true;
385 }
Evan Cheng00495212006-12-12 21:32:44 +0000386 }
387
Dan Gohman475871a2008-07-27 21:46:04 +0000388 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000389 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000390 if (Extend)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000391 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +0000392 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000393 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000394 0, VT, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000395 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000396 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000397}
398
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000399/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
400static
Dan Gohman475871a2008-07-27 21:46:04 +0000401SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000402 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000403 SDValue Chain = ST->getChain();
404 SDValue Ptr = ST->getBasePtr();
405 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000406 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000407 int Alignment = ST->getAlignment();
408 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000409 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000410 if (ST->getMemoryVT().isFloatingPoint() ||
411 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000412 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
413 if (TLI.isTypeLegal(intVT)) {
414 // Expand to a bitconvert of the value to the integer type of the
415 // same size, then a (misaligned) int store.
416 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000417 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
418 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000419 SVOffset, ST->isVolatile(), Alignment);
420 } else {
421 // Do a (aligned) store to a stack slot, then copy from the stack slot
422 // to the final destination using (unaligned) integer loads and stores.
423 MVT StoredVT = ST->getMemoryVT();
424 MVT RegVT =
425 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
426 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
427 unsigned RegBytes = RegVT.getSizeInBits() / 8;
428 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000429
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000430 // Make sure the stack slot is also aligned for the register type.
431 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
432
433 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000434 SDValue Store = DAG.getTruncStore(Chain, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000435 Val, StackPtr, NULL, 0, StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000436 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
437 SmallVector<SDValue, 8> Stores;
438 unsigned Offset = 0;
439
440 // Do all but one copies using the full register width.
441 for (unsigned i = 1; i < NumRegs; i++) {
442 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000443 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000444 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000445 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000446 ST->getSrcValue(), SVOffset + Offset,
447 ST->isVolatile(),
448 MinAlign(ST->getAlignment(), Offset)));
449 // Increment the pointers.
450 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000451 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000452 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000453 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000454 }
455
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000456 // The last store may be partial. Do a truncating store. On big-endian
457 // machines this requires an extending load from the stack slot to ensure
458 // that the bits are in the right place.
459 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000460
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000461 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000462 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000463 NULL, 0, MemVT);
464
Dale Johannesenbb5da912009-02-02 20:41:04 +0000465 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000466 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000467 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000468 MinAlign(ST->getAlignment(), Offset)));
469 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000470 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000471 Stores.size());
472 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000473 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000474 assert(ST->getMemoryVT().isInteger() &&
475 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000476 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000477 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000478 MVT NewStoredVT =
479 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
480 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000481 int IncrementSize = NumBits / 8;
482
483 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000484 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
485 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000486 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000487
488 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000489 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000490 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000491 ST->getSrcValue(), SVOffset, NewStoredVT,
492 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000493 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000494 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000495 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000496 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000497 ST->getSrcValue(), SVOffset + IncrementSize,
498 NewStoredVT, ST->isVolatile(), Alignment);
499
Dale Johannesenbb5da912009-02-02 20:41:04 +0000500 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000501}
502
503/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
504static
Dan Gohman475871a2008-07-27 21:46:04 +0000505SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000506 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000507 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000508 SDValue Chain = LD->getChain();
509 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000510 MVT VT = LD->getValueType(0);
511 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000512 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000513 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000514 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
515 if (TLI.isTypeLegal(intVT)) {
516 // Expand to a (misaligned) integer load of the same size,
517 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000518 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000519 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000520 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000521 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000522 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000523 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000524
Duncan Sands05e11fa2008-12-12 21:47:02 +0000525 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000526 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000527 } else {
528 // Copy the value to a (aligned) stack slot using (unaligned) integer
529 // loads and stores, then do a (aligned) load from the stack slot.
530 MVT RegVT = TLI.getRegisterType(intVT);
531 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
532 unsigned RegBytes = RegVT.getSizeInBits() / 8;
533 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
534
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000535 // Make sure the stack slot is also aligned for the register type.
536 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
537
Duncan Sands05e11fa2008-12-12 21:47:02 +0000538 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
539 SmallVector<SDValue, 8> Stores;
540 SDValue StackPtr = StackBase;
541 unsigned Offset = 0;
542
543 // Do all but one copies using the full register width.
544 for (unsigned i = 1; i < NumRegs; i++) {
545 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000546 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000547 SVOffset + Offset, LD->isVolatile(),
548 MinAlign(LD->getAlignment(), Offset));
549 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000550 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000551 NULL, 0));
552 // Increment the pointers.
553 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000554 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
555 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000556 Increment);
557 }
558
559 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000560 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000561 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000562 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000563 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000564 MinAlign(LD->getAlignment(), Offset));
565 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000566 // On big-endian machines this requires a truncating store to ensure
567 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000568 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000569 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000570
571 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000572 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000573 Stores.size());
574
575 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000576 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000577 NULL, 0, LoadedVT);
578
579 // Callers expect a MERGE_VALUES node.
580 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000581 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000582 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000583 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000584 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000585 "Unaligned load of unsupported type.");
586
Dale Johannesen8155d642008-02-27 22:36:00 +0000587 // Compute the new VT that is half the size of the old one. This is an
588 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000589 unsigned NumBits = LoadedVT.getSizeInBits();
590 MVT NewLoadedVT;
591 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000592 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000593
Chris Lattnere400af82007-11-19 21:38:03 +0000594 unsigned Alignment = LD->getAlignment();
595 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000596 ISD::LoadExtType HiExtType = LD->getExtensionType();
597
598 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
599 if (HiExtType == ISD::NON_EXTLOAD)
600 HiExtType = ISD::ZEXTLOAD;
601
602 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000603 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000604 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000605 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000606 SVOffset, NewLoadedVT, LD->isVolatile(), 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()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000609 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000610 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000611 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000612 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000613 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000614 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000615 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000616 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000617 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000619 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000620 }
621
622 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000624 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
625 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000626
Dale Johannesenbb5da912009-02-02 20:41:04 +0000627 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000628 Hi.getValue(1));
629
Dan Gohman475871a2008-07-27 21:46:04 +0000630 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000631 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000632}
Evan Cheng912095b2007-01-04 21:56:39 +0000633
Nate Begeman68679912008-04-25 18:07:40 +0000634/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
635/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
636/// is necessary to spill the vector being inserted into to memory, perform
637/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000638SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000639PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
640 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000641 SDValue Tmp1 = Vec;
642 SDValue Tmp2 = Val;
643 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000644
Nate Begeman68679912008-04-25 18:07:40 +0000645 // If the target doesn't support this, we have to spill the input vector
646 // to a temporary stack slot, update the element, then reload it. This is
647 // badness. We could also load the value into a vector register (either
648 // with a "move to register" or "extload into register" instruction, then
649 // permute it into place, if the idx is a constant and if the idx is
650 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000651 MVT VT = Tmp1.getValueType();
652 MVT EltVT = VT.getVectorElementType();
653 MVT IdxVT = Tmp3.getValueType();
654 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000655 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000656
Gabor Greifba36cb52008-08-28 21:40:38 +0000657 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000658
659 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000660 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000661 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000662
663 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000664 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000665 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000666 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000667 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000668 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
669 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000670 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000671 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000672 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000673 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000674 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000675 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000676}
677
Mon P Wange9f10152008-12-09 05:46:39 +0000678
Eli Friedman3f727d62009-05-27 02:16:40 +0000679SDValue SelectionDAGLegalize::
680ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
681 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
682 // SCALAR_TO_VECTOR requires that the type of the value being inserted
683 // match the element type of the vector being created, except for
684 // integers in which case the inserted value can be over width.
685 MVT EltVT = Vec.getValueType().getVectorElementType();
686 if (Val.getValueType() == EltVT ||
687 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
688 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
689 Vec.getValueType(), Val);
690
691 unsigned NumElts = Vec.getValueType().getVectorNumElements();
692 // We generate a shuffle of InVec and ScVec, so the shuffle mask
693 // should be 0,1,2,3,4,5... with the appropriate element replaced with
694 // elt 0 of the RHS.
695 SmallVector<int, 8> ShufOps;
696 for (unsigned i = 0; i != NumElts; ++i)
697 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
698
699 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
700 &ShufOps[0]);
701 }
702 }
703 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
704}
705
Dan Gohmana3466152007-07-13 20:14:11 +0000706/// LegalizeOp - We know that the specified value has a legal type, and
707/// that its operands are legal. Now ensure that the operation itself
708/// is legal, recursively ensuring that the operands' operations remain
709/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000710SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000711 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
712 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000713
Gabor Greifba36cb52008-08-28 21:40:38 +0000714 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000715 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000716
Eli Friedman1fde9c52009-05-24 02:46:31 +0000717 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
718 assert(getTypeAction(Node->getValueType(i)) == Legal &&
719 "Unexpected illegal type!");
720
721 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
722 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
723 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
724 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000725
Chris Lattner45982da2005-05-12 16:53:42 +0000726 // Note that LegalizeOp may be reentered even from single-use nodes, which
727 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000728 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000729 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000730
Dan Gohman475871a2008-07-27 21:46:04 +0000731 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
732 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000733 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000734
Eli Friedman8c377c72009-05-27 01:25:56 +0000735 // Figure out the correct action; the way to query this varies by opcode
736 TargetLowering::LegalizeAction Action;
737 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000738 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000739 case ISD::INTRINSIC_W_CHAIN:
740 case ISD::INTRINSIC_WO_CHAIN:
741 case ISD::INTRINSIC_VOID:
742 case ISD::VAARG:
743 case ISD::STACKSAVE:
744 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
745 break;
746 case ISD::SINT_TO_FP:
747 case ISD::UINT_TO_FP:
748 case ISD::EXTRACT_VECTOR_ELT:
749 Action = TLI.getOperationAction(Node->getOpcode(),
750 Node->getOperand(0).getValueType());
751 break;
752 case ISD::FP_ROUND_INREG:
753 case ISD::SIGN_EXTEND_INREG: {
754 MVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
755 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
756 break;
757 }
758 case ISD::LOAD:
759 case ISD::STORE:
760 case ISD::BR_CC:
761 case ISD::FORMAL_ARGUMENTS:
762 case ISD::CALL:
763 case ISD::CALLSEQ_START:
764 case ISD::CALLSEQ_END:
765 case ISD::SELECT_CC:
766 case ISD::SETCC:
Eli Friedman8c377c72009-05-27 01:25:56 +0000767 // These instructions have properties that aren't modeled in the
768 // generic codepath
769 SimpleFinishLegalizing = false;
770 break;
771 case ISD::EXTRACT_ELEMENT:
772 case ISD::FLT_ROUNDS_:
773 case ISD::SADDO:
774 case ISD::SSUBO:
775 case ISD::UADDO:
776 case ISD::USUBO:
777 case ISD::SMULO:
778 case ISD::UMULO:
779 case ISD::FPOWI:
780 case ISD::MERGE_VALUES:
781 case ISD::EH_RETURN:
782 case ISD::FRAME_TO_ARGS_OFFSET:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000783 case ISD::EXCEPTIONADDR:
784 case ISD::EHSELECTION:
785 // These operations lie about being legal: when they claim to be legal,
786 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000787 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
788 if (Action == TargetLowering::Legal)
789 Action = TargetLowering::Expand;
790 break;
791 case ISD::TRAMPOLINE:
792 case ISD::FRAMEADDR:
793 case ISD::RETURNADDR:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000794 // These operations lie about being legal: they must always be
795 // custom-lowered.
Eli Friedman8c377c72009-05-27 01:25:56 +0000796 Action = TargetLowering::Custom;
797 break;
798 case ISD::BUILD_VECTOR:
799 // A weird case: when a BUILD_VECTOR is custom-lowered, it doesn't legalize
800 // its operands first!
801 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000802 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000803 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000804 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000805 Action = TargetLowering::Legal;
806 } else {
807 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000808 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000809 break;
810 }
811
812 if (SimpleFinishLegalizing) {
813 SmallVector<SDValue, 8> Ops, ResultVals;
814 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
815 Ops.push_back(LegalizeOp(Node->getOperand(i)));
816 switch (Node->getOpcode()) {
817 default: break;
818 case ISD::BR:
819 case ISD::BRIND:
820 case ISD::BR_JT:
821 case ISD::BR_CC:
822 case ISD::BRCOND:
823 case ISD::RET:
824 // Branches tweak the chain to include LastCALLSEQ_END
825 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
826 LastCALLSEQ_END);
827 Ops[0] = LegalizeOp(Ops[0]);
828 LastCALLSEQ_END = DAG.getEntryNode();
829 break;
830 case ISD::SHL:
831 case ISD::SRL:
832 case ISD::SRA:
833 case ISD::ROTL:
834 case ISD::ROTR:
835 // Legalizing shifts/rotates requires adjusting the shift amount
836 // to the appropriate width.
837 if (!Ops[1].getValueType().isVector())
838 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
839 break;
840 }
841
842 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(),
843 Ops.size());
844 switch (Action) {
845 case TargetLowering::Legal:
846 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
847 ResultVals.push_back(Result.getValue(i));
848 break;
849 case TargetLowering::Custom:
850 // FIXME: The handling for custom lowering with multiple results is
851 // a complete mess.
852 Tmp1 = TLI.LowerOperation(Result, DAG);
853 if (Tmp1.getNode()) {
854 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
855 if (e == 1)
856 ResultVals.push_back(Tmp1);
857 else
858 ResultVals.push_back(Tmp1.getValue(i));
859 }
860 break;
861 }
862
863 // FALL THROUGH
864 case TargetLowering::Expand:
865 ExpandNode(Result.getNode(), ResultVals);
866 break;
867 case TargetLowering::Promote:
868 PromoteNode(Result.getNode(), ResultVals);
869 break;
870 }
871 if (!ResultVals.empty()) {
872 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
873 if (ResultVals[i] != SDValue(Node, i))
874 ResultVals[i] = LegalizeOp(ResultVals[i]);
875 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
876 }
877 return ResultVals[Op.getResNo()];
878 }
879 }
880
881 switch (Node->getOpcode()) {
882 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000883#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000884 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000885#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000886 assert(0 && "Do not know how to legalize this operator!");
887 abort();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000888 case ISD::EXCEPTIONADDR: {
889 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000890 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000891 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
892 default: assert(0 && "This action is not supported yet!");
893 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000894 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000895 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000896 }
897 break;
898 case TargetLowering::Custom:
899 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000900 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000901 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000902 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000903 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000904 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000905 break;
906 }
907 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000908 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000909 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000910
Gabor Greifba36cb52008-08-28 21:40:38 +0000911 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000912 "Cannot return more than two values!");
913
914 // Since we produced two values, make sure to remember that we
915 // legalized both of them.
916 Tmp1 = LegalizeOp(Result);
917 Tmp2 = LegalizeOp(Result.getValue(1));
918 AddLegalizedOperand(Op.getValue(0), Tmp1);
919 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000920 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000921 case ISD::EHSELECTION: {
922 Tmp1 = LegalizeOp(Node->getOperand(0));
923 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000924 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +0000925 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
926 default: assert(0 && "This action is not supported yet!");
927 case TargetLowering::Expand: {
928 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000929 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000930 }
931 break;
932 case TargetLowering::Custom:
933 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000934 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +0000935 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000936 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000937 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000938 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey8782d482007-02-28 20:43:58 +0000939 break;
940 }
941 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000942 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000943 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000944
Gabor Greifba36cb52008-08-28 21:40:38 +0000945 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000946 "Cannot return more than two values!");
947
948 // Since we produced two values, make sure to remember that we
949 // legalized both of them.
950 Tmp1 = LegalizeOp(Result);
951 Tmp2 = LegalizeOp(Result.getValue(1));
952 AddLegalizedOperand(Op.getValue(0), Tmp1);
953 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000954 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner36ce6912005-11-29 06:21:05 +0000955
Dan Gohman7f460202008-06-30 20:59:49 +0000956 case ISD::DBG_STOPPOINT:
957 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +0000958 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
Scott Michelfdc40a02009-02-17 22:15:04 +0000959
Dan Gohman7f460202008-06-30 20:59:49 +0000960 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000961 case TargetLowering::Promote:
962 default: assert(0 && "This action is not supported yet!");
Bill Wendling92c1e122009-02-13 02:16:35 +0000963 case TargetLowering::Expand: {
964 DwarfWriter *DW = DAG.getDwarfWriter();
965 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
966 MVT::Other);
967 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +0000968
Bill Wendling92c1e122009-02-13 02:16:35 +0000969 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
970 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
971 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
972 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
Scott Michelfdc40a02009-02-17 22:15:04 +0000973
Bill Wendling92c1e122009-02-13 02:16:35 +0000974 unsigned Line = DSP->getLine();
975 unsigned Col = DSP->getColumn();
Bill Wendling86e6cb92009-02-17 01:04:54 +0000976
Bill Wendling98a366d2009-04-29 23:29:43 +0000977 if (OptLevel == CodeGenOpt::None) {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000978 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it
979 // won't hurt anything.
980 if (useDEBUG_LOC) {
981 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Bill Wendling92c1e122009-02-13 02:16:35 +0000982 DAG.getConstant(Col, MVT::i32),
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000983 DAG.getSrcValue(CU.getGV()) };
Bill Wendling86e6cb92009-02-17 01:04:54 +0000984 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
985 } else {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000986 unsigned ID = DW->RecordSourceLine(Line, Col, CU);
Bill Wendling86e6cb92009-02-17 01:04:54 +0000987 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
988 }
Bill Wendling92c1e122009-02-13 02:16:35 +0000989 } else {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000990 Result = Tmp1; // chain
Bill Wendling92c1e122009-02-13 02:16:35 +0000991 }
992 } else {
993 Result = Tmp1; // chain
994 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000995 break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000996 }
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000997 case TargetLowering::Custom:
998 Result = TLI.LowerOperation(Op, DAG);
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000999 if (Result.getNode())
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001000 break;
Evan Cheng71e86852008-07-08 20:06:39 +00001001 case TargetLowering::Legal: {
Eli Friedman74807f22009-05-26 08:55:52 +00001002 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +00001003 break;
1004
Dan Gohman475871a2008-07-27 21:46:04 +00001005 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001006 Ops.push_back(Tmp1);
Eli Friedman74807f22009-05-26 08:55:52 +00001007 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1008 Ops.push_back(Node->getOperand(2)); // col # must be legal.
Evan Cheng71e86852008-07-08 20:06:39 +00001009 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1010 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1011 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001012 break;
1013 }
Evan Cheng71e86852008-07-08 20:06:39 +00001014 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001015 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001016 case ISD::ConstantFP: {
1017 // Spill FP immediates to the constant pool if the target cannot directly
1018 // codegen them. Targets often have some immediate values that can be
1019 // efficiently generated into an FP register without a load. We explicitly
1020 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001021 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1022
Chris Lattner3181a772006-01-29 06:26:56 +00001023 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1024 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001025 case TargetLowering::Legal:
1026 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001027 case TargetLowering::Custom:
1028 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001029 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001030 Result = Tmp3;
1031 break;
1032 }
1033 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001034 case TargetLowering::Expand: {
1035 // Check to see if this FP immediate is already legal.
1036 bool isLegal = false;
1037 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1038 E = TLI.legal_fpimm_end(); I != E; ++I) {
1039 if (CFP->isExactlyValue(*I)) {
1040 isLegal = true;
1041 break;
1042 }
1043 }
1044 // If this is a legal constant, turn it into a TargetConstantFP node.
1045 if (isLegal)
1046 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001047 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001048 }
Nate Begemane1795842008-02-14 08:57:00 +00001049 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001050 break;
1051 }
Chris Lattnerfdfded52006-04-12 16:20:43 +00001052 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001053 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001054 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001055 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001056 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001057 // A call within a calling sequence must be legalized to something
1058 // other than the normal CALLSEQ_END. Violating this gets Legalize
1059 // into an infinite loop.
1060 assert ((!IsLegalizingCall ||
1061 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001062 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001063 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001064
1065 // The number of incoming and outgoing values should match; unless the final
1066 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001067 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1068 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1069 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001070 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001071 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001072
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001073 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001074 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1076 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001077 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001078 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001079 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001080 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001081 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001082 }
1083 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001084 case ISD::BUILD_VECTOR:
1085 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001086 default: assert(0 && "This action is not supported yet!");
1087 case TargetLowering::Custom:
1088 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001089 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001090 Result = Tmp3;
1091 break;
1092 }
1093 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001094 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001095 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001096 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001097 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001098 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001099 case ISD::VECTOR_SHUFFLE: {
Chris Lattner87100e02006-03-20 01:52:29 +00001100 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1101 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00001102 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1103 MVT VT = Result.getValueType();
1104
Nate Begeman5a5ca152009-04-29 05:20:52 +00001105 // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal.
Nate Begeman9008ca62009-04-27 18:41:29 +00001106 SmallVector<int, 8> Mask;
1107 cast<ShuffleVectorSDNode>(Result)->getMask(Mask);
Chris Lattner87100e02006-03-20 01:52:29 +00001108
1109 // Allow targets to custom lower the SHUFFLEs they support.
Nate Begeman9008ca62009-04-27 18:41:29 +00001110 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
Chris Lattner4352cc92006-04-04 17:23:26 +00001111 default: assert(0 && "Unknown operation action!");
1112 case TargetLowering::Legal:
Nate Begeman9008ca62009-04-27 18:41:29 +00001113 assert(TLI.isShuffleMaskLegal(Mask, VT) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00001114 "vector shuffle should not be created if not legal!");
1115 break;
1116 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001117 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001118 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001119 Result = Tmp3;
1120 break;
1121 }
1122 // FALLTHROUGH
1123 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001124 MVT EltVT = VT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00001125 unsigned NumElems = VT.getVectorNumElements();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001126 SmallVector<SDValue, 8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001127 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001128 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001129 Ops.push_back(DAG.getUNDEF(EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00001130 continue;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001131 }
Nate Begeman5a5ca152009-04-29 05:20:52 +00001132 unsigned Idx = Mask[i];
Nate Begeman9008ca62009-04-27 18:41:29 +00001133 if (Idx < NumElems)
1134 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
1135 DAG.getIntPtrConstant(Idx)));
1136 else
1137 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
1138 DAG.getIntPtrConstant(Idx - NumElems)));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001139 }
Evan Chenga87008d2009-02-25 22:49:59 +00001140 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001141 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001142 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001143 case TargetLowering::Promote: {
1144 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001145 MVT OVT = Node->getValueType(0);
1146 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001147
1148 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001149 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1150 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001151
Chris Lattner4352cc92006-04-04 17:23:26 +00001152 // Convert the shuffle mask to the right # elements.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001153 Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Dale Johannesenca57b842009-02-02 23:46:53 +00001154 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001155 break;
1156 }
Chris Lattner87100e02006-03-20 01:52:29 +00001157 }
1158 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001159 }
Mon P Wang0c397192008-10-30 08:01:45 +00001160 case ISD::CONCAT_VECTORS: {
Bob Wilson5ee24e52009-05-01 17:55:32 +00001161 // Legalize the operands.
Mon P Wang0c397192008-10-30 08:01:45 +00001162 SmallVector<SDValue, 8> Ops;
Bob Wilson5ee24e52009-05-01 17:55:32 +00001163 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1164 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1165 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1166
1167 switch (TLI.getOperationAction(ISD::CONCAT_VECTORS,
1168 Node->getValueType(0))) {
1169 default: assert(0 && "Unknown operation action!");
1170 case TargetLowering::Legal:
1171 break;
1172 case TargetLowering::Custom:
1173 Tmp3 = TLI.LowerOperation(Result, DAG);
1174 if (Tmp3.getNode()) {
1175 Result = Tmp3;
1176 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001177 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001178 // FALLTHROUGH
1179 case TargetLowering::Expand: {
1180 // Use extract/insert/build vector for now. We might try to be
1181 // more clever later.
1182 MVT PtrVT = TLI.getPointerTy();
1183 SmallVector<SDValue, 8> Ops;
1184 unsigned NumOperands = Node->getNumOperands();
1185 for (unsigned i=0; i < NumOperands; ++i) {
1186 SDValue SubOp = Node->getOperand(i);
1187 MVT VVT = SubOp.getNode()->getValueType(0);
1188 MVT EltVT = VVT.getVectorElementType();
1189 unsigned NumSubElem = VVT.getVectorNumElements();
1190 for (unsigned j=0; j < NumSubElem; ++j) {
1191 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1192 DAG.getConstant(j, PtrVT)));
1193 }
1194 }
1195 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl,
1196 Node->getValueType(0),
1197 &Ops[0], Ops.size()));
Mon P Wang0c397192008-10-30 08:01:45 +00001198 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001199 }
1200 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001201 }
1202
Chris Lattner6831a812006-02-13 09:18:02 +00001203 case ISD::CALLSEQ_START: {
1204 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001205
Chris Lattner6831a812006-02-13 09:18:02 +00001206 // Recursively Legalize all of the inputs of the call end that do not lead
1207 // to this call start. This ensures that any libcalls that need be inserted
1208 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001209 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001210 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001211 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001212 NodesLeadingTo);
1213 }
Chris Lattner6831a812006-02-13 09:18:02 +00001214
1215 // Now that we legalized all of the inputs (which may have inserted
1216 // libcalls) create the new CALLSEQ_START node.
1217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1218
1219 // Merge in the last call, to ensure that this call start after the last
1220 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001221 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001222 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001223 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001224 Tmp1 = LegalizeOp(Tmp1);
1225 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Chris Lattner6831a812006-02-13 09:18:02 +00001227 // Do not try to legalize the target-specific arguments (#1+).
1228 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001229 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001230 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001231 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001232 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001233
Chris Lattner6831a812006-02-13 09:18:02 +00001234 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001235 AddLegalizedOperand(Op.getValue(0), Result);
1236 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1237 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001238
Chris Lattner6831a812006-02-13 09:18:02 +00001239 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001240 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001241 // process, no libcalls can/will be inserted, guaranteeing that no calls
1242 // can overlap.
1243 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001244 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001245 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001246 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001247
Chris Lattner6831a812006-02-13 09:18:02 +00001248 // Legalize the call, starting from the CALLSEQ_END.
1249 LegalizeOp(LastCALLSEQ_END);
1250 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1251 return Result;
1252 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001253 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001254 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1255 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001256 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001257 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1258 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001259 assert(I != LegalizedNodes.end() &&
1260 "Legalizing the call start should have legalized this node!");
1261 return I->second;
1262 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001263
1264 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001265 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001266 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001267 // Do not try to legalize the target-specific arguments (#1+), except for
1268 // an optional flag input.
1269 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1270 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001271 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001272 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001273 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001274 }
1275 } else {
1276 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1277 if (Tmp1 != Node->getOperand(0) ||
1278 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001279 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001280 Ops[0] = Tmp1;
1281 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001282 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001283 }
Chris Lattner6a542892006-01-24 05:48:21 +00001284 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001285 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001286 // This finishes up call legalization.
1287 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001288
Chris Lattner4b653a02006-02-14 00:55:02 +00001289 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001290 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001291 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001292 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001293 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001294 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001295 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001296 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1297 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1298 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001299 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001300
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001301 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001302 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001303 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001304 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001305 case TargetLowering::Expand: {
1306 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1307 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1308 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001309 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001310
1311 // Chain the dynamic stack allocation so that it doesn't modify the stack
1312 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001313 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001314
Dan Gohman475871a2008-07-27 21:46:04 +00001315 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001316 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001317 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001318 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001319 unsigned StackAlign =
1320 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1321 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001322 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001323 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001324 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001325 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001326
Dale Johannesenca57b842009-02-02 23:46:53 +00001327 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001328 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001329
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001330 Tmp1 = LegalizeOp(Tmp1);
1331 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001332 break;
1333 }
1334 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001335 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001336 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001337 Tmp1 = LegalizeOp(Tmp3);
1338 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001339 }
Chris Lattner903d2782006-01-15 08:54:32 +00001340 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001341 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001342 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001343 }
Chris Lattner903d2782006-01-15 08:54:32 +00001344 // Since this op produce two values, make sure to remember that we
1345 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001346 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1347 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001348 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001349 }
Evan Cheng3d4ce112006-10-30 08:00:44 +00001350 case ISD::BR_JT:
1351 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1352 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001353 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001354 Tmp1 = LegalizeOp(Tmp1);
1355 LastCALLSEQ_END = DAG.getEntryNode();
1356
1357 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1359
Scott Michelfdc40a02009-02-17 22:15:04 +00001360 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001361 default: assert(0 && "This action is not supported yet!");
1362 case TargetLowering::Legal: break;
1363 case TargetLowering::Custom:
1364 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001365 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001366 break;
1367 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001368 SDValue Chain = Result.getOperand(0);
1369 SDValue Table = Result.getOperand(1);
1370 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001371
Duncan Sands83ec4b62008-06-06 12:08:01 +00001372 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001373 MachineFunction &MF = DAG.getMachineFunction();
1374 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001375 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00001376 Index, DAG.getConstant(EntrySize, PTy));
1377 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001378
Duncan Sands712f7b32008-12-12 08:13:38 +00001379 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00001380 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00001381 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00001382 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001383 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001384 // For PIC, the sequence is:
1385 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001386 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00001387 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00001388 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001389 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001390 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001391 }
1392 }
1393 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001394 case ISD::BRCOND:
1395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001396 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00001397 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001398 Tmp1 = LegalizeOp(Tmp1);
1399 LastCALLSEQ_END = DAG.getEntryNode();
1400
Eli Friedman957bffa2009-05-24 08:42:01 +00001401 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Chris Lattner456a93a2006-01-28 07:39:30 +00001402
1403 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001404 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00001405
1406 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001407 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001408 case TargetLowering::Legal: break;
1409 case TargetLowering::Custom:
1410 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001411 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001412 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001413 case TargetLowering::Expand:
1414 // Expand brcond's setcc into its constituent parts and create a BR_CC
1415 // Node.
1416 if (Tmp2.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001417 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001418 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00001419 Tmp2.getOperand(0), Tmp2.getOperand(1),
1420 Node->getOperand(2));
1421 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001422 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00001423 DAG.getCondCode(ISD::SETNE), Tmp2,
1424 DAG.getConstant(0, Tmp2.getValueType()),
1425 Node->getOperand(2));
1426 }
1427 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001428 }
1429 break;
1430 case ISD::BR_CC:
1431 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001432 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001433 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001434 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001435 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00001436 Tmp3 = Node->getOperand(3); // RHS
1437 Tmp4 = Node->getOperand(1); // CC
1438
Scott Michelfdc40a02009-02-17 22:15:04 +00001439 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001440 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00001441 LastCALLSEQ_END = DAG.getEntryNode();
1442
Evan Cheng7f042682008-10-15 02:05:31 +00001443 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001444 // the LHS is a legal SETCC itself. In this case, we need to compare
1445 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001446 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001447 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1448 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
1451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001452 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00001453
Chris Lattner181b7a32005-12-17 23:46:46 +00001454 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1455 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001456 case TargetLowering::Legal: break;
1457 case TargetLowering::Custom:
1458 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001459 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001460 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001461 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001462 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001463 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001464 LoadSDNode *LD = cast<LoadSDNode>(Node);
1465 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1466 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001467
Evan Cheng466685d2006-10-09 20:57:25 +00001468 ISD::LoadExtType ExtType = LD->getExtensionType();
1469 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001470 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001471 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1472 Tmp3 = Result.getValue(0);
1473 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001474
Evan Cheng466685d2006-10-09 20:57:25 +00001475 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1476 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001477 case TargetLowering::Legal:
1478 // If this is an unaligned load and the target doesn't support it,
1479 // expand it.
1480 if (!TLI.allowsUnalignedMemoryAccesses()) {
1481 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001482 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001483 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001484 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001485 TLI);
1486 Tmp3 = Result.getOperand(0);
1487 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001488 Tmp3 = LegalizeOp(Tmp3);
1489 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001490 }
1491 }
1492 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001493 case TargetLowering::Custom:
1494 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001495 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001496 Tmp3 = LegalizeOp(Tmp1);
1497 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001498 }
Evan Cheng466685d2006-10-09 20:57:25 +00001499 break;
1500 case TargetLowering::Promote: {
1501 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001502 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001503 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001504 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001505
Dale Johannesenca57b842009-02-02 23:46:53 +00001506 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001507 LD->getSrcValueOffset(),
1508 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001509 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001510 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001511 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001512 }
Evan Cheng466685d2006-10-09 20:57:25 +00001513 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001514 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001515 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001516 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1517 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001518 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00001519 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001520 MVT SrcVT = LD->getMemoryVT();
1521 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001522 int SVOffset = LD->getSrcValueOffset();
1523 unsigned Alignment = LD->getAlignment();
1524 bool isVolatile = LD->isVolatile();
1525
Duncan Sands83ec4b62008-06-06 12:08:01 +00001526 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001527 // Some targets pretend to have an i1 loading operation, and actually
1528 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1529 // bits are guaranteed to be zero; it helps the optimizers understand
1530 // that these bits are zero. It is also useful for EXTLOAD, since it
1531 // tells the optimizers that those bits are undefined. It would be
1532 // nice to have an effective generic way of getting these benefits...
1533 // Until such a way is found, don't insist on promoting i1 here.
1534 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00001535 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001536 // Promote to a byte-sized load if not loading an integral number of
1537 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001538 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1539 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001540 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001541
1542 // The extra bits are guaranteed to be zero, since we stored them that
1543 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1544
1545 ISD::LoadExtType NewExtType =
1546 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1547
Dale Johannesenca57b842009-02-02 23:46:53 +00001548 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001549 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1550 NVT, isVolatile, Alignment);
1551
1552 Ch = Result.getValue(1); // The chain.
1553
1554 if (ExtType == ISD::SEXTLOAD)
1555 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00001556 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001557 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001558 Result, DAG.getValueType(SrcVT));
1559 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1560 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001561 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001562 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001563 DAG.getValueType(SrcVT));
1564
1565 Tmp1 = LegalizeOp(Result);
1566 Tmp2 = LegalizeOp(Ch);
1567 } else if (SrcWidth & (SrcWidth - 1)) {
1568 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001569 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001570 "Unsupported extload!");
1571 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1572 assert(RoundWidth < SrcWidth);
1573 unsigned ExtraWidth = SrcWidth - RoundWidth;
1574 assert(ExtraWidth < RoundWidth);
1575 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1576 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001577 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1578 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001579 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001580 unsigned IncrementSize;
1581
1582 if (TLI.isLittleEndian()) {
1583 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1584 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001585 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1586 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001587 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1588 Alignment);
1589
1590 // Load the remaining ExtraWidth bits.
1591 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001592 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001593 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001594 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001595 LD->getSrcValue(), SVOffset + IncrementSize,
1596 ExtraVT, isVolatile,
1597 MinAlign(Alignment, IncrementSize));
1598
1599 // Build a factor node to remember that this load is independent of the
1600 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001601 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001602 Hi.getValue(1));
1603
1604 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001605 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001606 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1607
1608 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001609 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001610 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001611 // Big endian - avoid unaligned loads.
1612 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1613 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001614 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001615 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1616 Alignment);
1617
1618 // Load the remaining ExtraWidth bits.
1619 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001620 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001621 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00001622 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001623 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001624 LD->getSrcValue(), SVOffset + IncrementSize,
1625 ExtraVT, isVolatile,
1626 MinAlign(Alignment, IncrementSize));
1627
1628 // Build a factor node to remember that this load is independent of the
1629 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001630 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001631 Hi.getValue(1));
1632
1633 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001634 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001635 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1636
1637 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001638 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001639 }
1640
1641 Tmp1 = LegalizeOp(Result);
1642 Tmp2 = LegalizeOp(Ch);
1643 } else {
Evan Cheng03294662008-10-14 21:26:46 +00001644 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001645 default: assert(0 && "This action is not supported yet!");
1646 case TargetLowering::Custom:
1647 isCustom = true;
1648 // FALLTHROUGH
1649 case TargetLowering::Legal:
1650 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1651 Tmp1 = Result.getValue(0);
1652 Tmp2 = Result.getValue(1);
1653
1654 if (isCustom) {
1655 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001656 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001657 Tmp1 = LegalizeOp(Tmp3);
1658 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1659 }
1660 } else {
1661 // If this is an unaligned load and the target doesn't support it,
1662 // expand it.
1663 if (!TLI.allowsUnalignedMemoryAccesses()) {
1664 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001665 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001666 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001667 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001668 TLI);
1669 Tmp1 = Result.getOperand(0);
1670 Tmp2 = Result.getOperand(1);
1671 Tmp1 = LegalizeOp(Tmp1);
1672 Tmp2 = LegalizeOp(Tmp2);
1673 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001674 }
1675 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001676 break;
1677 case TargetLowering::Expand:
1678 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1679 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001680 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001681 LD->getSrcValueOffset(),
1682 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00001683 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001684 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001685 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1686 Tmp2 = LegalizeOp(Load.getValue(1));
1687 break;
1688 }
1689 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1690 // Turn the unsupported load into an EXTLOAD followed by an explicit
1691 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00001692 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001693 Tmp1, Tmp2, LD->getSrcValue(),
1694 LD->getSrcValueOffset(), SrcVT,
1695 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00001696 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001697 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00001698 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1699 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001700 Result, DAG.getValueType(SrcVT));
1701 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001702 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001703 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1704 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00001705 break;
1706 }
Evan Cheng466685d2006-10-09 20:57:25 +00001707 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001708
Evan Cheng466685d2006-10-09 20:57:25 +00001709 // Since loads produce two values, make sure to remember that we legalized
1710 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001711 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1712 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001713 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001714 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001715 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001716 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001717 StoreSDNode *ST = cast<StoreSDNode>(Node);
1718 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1719 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001720 int SVOffset = ST->getSrcValueOffset();
1721 unsigned Alignment = ST->getAlignment();
1722 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001723
Evan Cheng8b2794a2006-10-13 21:14:26 +00001724 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001725 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1726 // FIXME: We shouldn't do this for TargetConstantFP's.
1727 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1728 // to phase ordering between legalized code and the dag combiner. This
1729 // probably means that we need to integrate dag combiner and legalizer
1730 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001731 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00001732 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001733 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00001734 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001735 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00001736 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001737 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00001738 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001739 SVOffset, isVolatile, Alignment);
1740 break;
1741 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001742 // If this target supports 64-bit registers, do a single 64-bit store.
1743 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00001744 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001745 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00001746 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001747 SVOffset, isVolatile, Alignment);
1748 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001749 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001750 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
1751 // stores. If the target supports neither 32- nor 64-bits, this
1752 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00001753 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00001754 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
1755 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00001756 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001757
Dale Johannesenca57b842009-02-02 23:46:53 +00001758 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001759 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00001760 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00001761 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00001762 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00001763 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00001764
Dale Johannesenca57b842009-02-02 23:46:53 +00001765 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001766 break;
1767 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001768 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001769 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001770
Eli Friedman957bffa2009-05-24 08:42:01 +00001771 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001772 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001773 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001774 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001775
Duncan Sands83ec4b62008-06-06 12:08:01 +00001776 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001777 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1778 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001779 case TargetLowering::Legal:
1780 // If this is an unaligned store and the target doesn't support it,
1781 // expand it.
1782 if (!TLI.allowsUnalignedMemoryAccesses()) {
1783 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001784 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001785 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001786 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001787 TLI);
1788 }
1789 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001790 case TargetLowering::Custom:
1791 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001792 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001793 break;
1794 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001795 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001796 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001797 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001798 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001799 ST->getSrcValue(), SVOffset, isVolatile,
1800 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001801 break;
1802 }
1803 break;
1804 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001805 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001806 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001807
Duncan Sands83ec4b62008-06-06 12:08:01 +00001808 MVT StVT = ST->getMemoryVT();
1809 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001810
Duncan Sands83ec4b62008-06-06 12:08:01 +00001811 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001812 // Promote to a byte-sized store with upper bits zero if not
1813 // storing an integral number of bytes. For example, promote
1814 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001815 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001816 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
1817 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001818 SVOffset, NVT, isVolatile, Alignment);
1819 } else if (StWidth & (StWidth - 1)) {
1820 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001821 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00001822 "Unsupported truncstore!");
1823 unsigned RoundWidth = 1 << Log2_32(StWidth);
1824 assert(RoundWidth < StWidth);
1825 unsigned ExtraWidth = StWidth - RoundWidth;
1826 assert(ExtraWidth < RoundWidth);
1827 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1828 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001829 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1830 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001831 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001832 unsigned IncrementSize;
1833
1834 if (TLI.isLittleEndian()) {
1835 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1836 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001837 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001838 SVOffset, RoundVT,
1839 isVolatile, Alignment);
1840
1841 // Store the remaining ExtraWidth bits.
1842 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001843 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001844 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001845 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001846 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001847 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001848 SVOffset + IncrementSize, ExtraVT, isVolatile,
1849 MinAlign(Alignment, IncrementSize));
1850 } else {
1851 // Big endian - avoid unaligned stores.
1852 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1853 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001854 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001855 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001856 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1857 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001858
1859 // Store the remaining ExtraWidth bits.
1860 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001861 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001862 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001863 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001864 SVOffset + IncrementSize, ExtraVT, isVolatile,
1865 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001866 }
Duncan Sands7e857202008-01-22 07:17:34 +00001867
1868 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00001869 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001870 } else {
1871 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1872 Tmp2 != ST->getBasePtr())
1873 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1874 ST->getOffset());
1875
1876 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
1877 default: assert(0 && "This action is not supported yet!");
1878 case TargetLowering::Legal:
1879 // If this is an unaligned store and the target doesn't support it,
1880 // expand it.
1881 if (!TLI.allowsUnalignedMemoryAccesses()) {
1882 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001883 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00001884 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001885 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00001886 TLI);
1887 }
1888 break;
1889 case TargetLowering::Custom:
1890 Result = TLI.LowerOperation(Result, DAG);
1891 break;
1892 case Expand:
1893 // TRUNCSTORE:i16 i32 -> STORE i16
1894 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001895 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
1896 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1897 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001898 break;
1899 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001900 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001901 }
1902 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001903 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001904 case ISD::SELECT:
Eli Friedman957bffa2009-05-24 08:42:01 +00001905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001906 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001907 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001908
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00001910
Nate Begemanb942a3d2005-08-23 04:29:48 +00001911 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001912 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001913 case TargetLowering::Legal: break;
1914 case TargetLowering::Custom: {
1915 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001916 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001917 break;
1918 }
Nate Begeman9373a812005-08-10 20:51:12 +00001919 case TargetLowering::Expand:
1920 if (Tmp1.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001921 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00001922 Tmp2, Tmp3,
1923 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1924 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001925 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00001926 DAG.getConstant(0, Tmp1.getValueType()),
1927 Tmp2, Tmp3, ISD::SETNE);
1928 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001929 break;
1930 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001931 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001932 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1933 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001934 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001935 ExtOp = ISD::BIT_CONVERT;
1936 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001937 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001938 ExtOp = ISD::ANY_EXTEND;
1939 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001940 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001941 ExtOp = ISD::FP_EXTEND;
1942 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001943 }
1944 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00001945 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
1946 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001947 // Perform the larger operation, then round down.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001948 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00001949 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00001950 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00001951 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001952 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00001953 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001954 break;
1955 }
1956 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001957 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001958 case ISD::SELECT_CC: {
1959 Tmp1 = Node->getOperand(0); // LHS
1960 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001961 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1962 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00001964
1965 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001966 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001967
Evan Cheng7f042682008-10-15 02:05:31 +00001968 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001969 // the LHS is a legal SETCC itself. In this case, we need to compare
1970 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001971 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001972 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1973 CC = DAG.getCondCode(ISD::SETNE);
1974 }
1975 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1976
1977 // Everything is legal, see if we should expand this op or something.
1978 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1979 default: assert(0 && "This action is not supported yet!");
1980 case TargetLowering::Legal: break;
1981 case TargetLowering::Custom:
1982 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001983 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001984 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001985 }
1986 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001987 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001988 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001989 Tmp1 = Node->getOperand(0);
1990 Tmp2 = Node->getOperand(1);
1991 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00001992 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001993
1994 // If we had to Expand the SetCC operands into a SELECT node, then it may
1995 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00001996 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00001997 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001998 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001999 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002000 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002001
Chris Lattner73e142f2006-01-30 22:43:50 +00002002 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002003 default: assert(0 && "Cannot handle this action for SETCC yet!");
2004 case TargetLowering::Custom:
2005 isCustom = true;
2006 // FALLTHROUGH.
2007 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002009 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002010 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002011 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002012 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002013 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002014 case TargetLowering::Promote: {
2015 // First step, figure out the appropriate operation to use.
2016 // Allow SETCC to not be supported for all legal data types
2017 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002018 MVT NewInTy = Node->getOperand(0).getValueType();
2019 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002020
2021 // Scan for the appropriate larger type to use.
2022 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002023 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002024
Duncan Sands83ec4b62008-06-06 12:08:01 +00002025 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002026 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002027 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002028 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00002029
Andrew Lenharthae355752005-11-30 17:12:26 +00002030 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002031 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002032 break;
2033 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002034 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002035 assert(0 && "Cannot promote Legal Integer SETCC yet");
2036 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002037 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
2038 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00002039 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002040 Tmp1 = LegalizeOp(Tmp1);
2041 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002043 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002044 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002045 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002046 case TargetLowering::Expand:
2047 // Expand a setcc node into a select_cc of the same condition, lhs, and
2048 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002049 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002050 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00002051 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002052 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002053 break;
2054 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002055 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002056
2057 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002058 case ISD::SUB:
2059 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002060 case ISD::MULHS:
2061 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002062 case ISD::UDIV:
2063 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002064 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00002065 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002066
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002068
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002069 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002070 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002071 case TargetLowering::Legal: break;
2072 case TargetLowering::Custom:
2073 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002074 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002075 Result = Tmp1;
2076 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00002077 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002078 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002079 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002080 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002081
Dan Gohman525178c2007-10-08 18:33:35 +00002082 // See if multiply or divide can be lowered using two-result operations.
2083 SDVTList VTs = DAG.getVTList(VT, VT);
2084 if (Node->getOpcode() == ISD::MUL) {
2085 // We just need the low half of the multiply; try both the signed
2086 // and unsigned forms. If the target supports both SMUL_LOHI and
2087 // UMUL_LOHI, form a preference by checking which forms of plain
2088 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002089 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
2090 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
2091 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
2092 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00002093 unsigned OpToUse = 0;
2094 if (HasSMUL_LOHI && !HasMULHS) {
2095 OpToUse = ISD::SMUL_LOHI;
2096 } else if (HasUMUL_LOHI && !HasMULHU) {
2097 OpToUse = ISD::UMUL_LOHI;
2098 } else if (HasSMUL_LOHI) {
2099 OpToUse = ISD::SMUL_LOHI;
2100 } else if (HasUMUL_LOHI) {
2101 OpToUse = ISD::UMUL_LOHI;
2102 }
2103 if (OpToUse) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002104 Result = DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002105 break;
2106 }
2107 }
2108 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002109 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002110 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002111 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002112 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002113 break;
2114 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002115 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002116 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002117 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
2118 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002119 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002120 break;
2121 }
2122 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002123 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002124 Result = DAG.getNode(ISD::SDIVREM, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002125 break;
2126 }
2127 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002128 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002129 Result = DAG.getNode(ISD::UDIVREM, dl, VTs, Tmp1, Tmp2);
2130 break;
2131 }
2132 if (Node->getOpcode() == ISD::SUB &&
2133 TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2134 TLI.isOperationLegalOrCustom(ISD::XOR, VT)) {
2135 Tmp2 = DAG.getNode(ISD::XOR, dl, VT, Tmp2,
2136 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
2137 Tmp2 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2138 Result = DAG.getNode(ISD::ADD, dl, VT, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002139 break;
2140 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00002141
Dan Gohman82669522007-10-11 23:57:53 +00002142 // Check to see if we have a libcall for this operator.
2143 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2144 bool isSigned = false;
2145 switch (Node->getOpcode()) {
2146 case ISD::UDIV:
2147 case ISD::SDIV:
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002148 isSigned = Node->getOpcode() == ISD::SDIV;
2149 if (VT == MVT::i16)
2150 LC = (isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16);
2151 else if (VT == MVT::i32)
2152 LC = (isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32);
2153 else if (VT == MVT::i64)
2154 LC = (isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64);
2155 else if (VT == MVT::i128)
2156 LC = (isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128);
2157 break;
Chris Lattner31d71612008-10-04 21:27:46 +00002158 case ISD::MUL:
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002159 if (VT == MVT::i16)
2160 LC = RTLIB::MUL_I16;
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002161 else if (VT == MVT::i32)
Chris Lattner31d71612008-10-04 21:27:46 +00002162 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00002163 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00002164 LC = RTLIB::MUL_I64;
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002165 else if (VT == MVT::i128)
2166 LC = RTLIB::MUL_I128;
Chris Lattner31d71612008-10-04 21:27:46 +00002167 break;
Dan Gohman82669522007-10-11 23:57:53 +00002168 default: break;
2169 }
2170 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Eli Friedman47b41f72009-05-27 02:21:29 +00002171 Result = ExpandLibCall(LC, Node, isSigned);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002172 break;
2173 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002174
Eli Friedman74807f22009-05-26 08:55:52 +00002175 assert(0 && "Cannot expand this binary operator!");
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002176 break;
2177 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002178 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002179 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002180 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2181 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00002182 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00002183
Chris Lattnera09f8482006-03-05 05:09:38 +00002184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002185
Chris Lattnera09f8482006-03-05 05:09:38 +00002186 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2187 default: assert(0 && "Operation not supported");
2188 case TargetLowering::Custom:
2189 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002190 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002191 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002192 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002193 case TargetLowering::Expand: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002194 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002195 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002196 "Ugly special-cased code!");
2197 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002198 SDValue SignBit;
2199 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
2200 if (isTypeLegal(IVT)) {
2201 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
2202 } else {
2203 assert(isTypeLegal(TLI.getPointerTy()) &&
2204 (TLI.getPointerTy() == MVT::i32 ||
2205 TLI.getPointerTy() == MVT::i64) &&
2206 "Legal type for load?!");
2207 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
2208 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
2209 SDValue Ch =
2210 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
2211 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
2212 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
2213 LoadPtr, DAG.getIntPtrConstant(4));
2214 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
2215 Ch, LoadPtr, NULL, 0, MVT::i32);
2216 }
2217 SignBit =
2218 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
2219 SignBit, DAG.getConstant(0, SignBit.getValueType()),
2220 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002221 // Get the absolute value of the result.
2222 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
2223 // Select between the nabs and abs value based on the sign bit of
2224 // the input.
2225 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
2226 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
2227 AbsVal),
2228 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00002229 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002230 break;
2231 }
Evan Cheng912095b2007-01-04 21:56:39 +00002232 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002233 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002234 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002235 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00002236 // TODO: handle the case where the Lo and Hi operands are not of legal type
2237 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2238 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2239 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002240 case TargetLowering::Promote:
2241 case TargetLowering::Custom:
2242 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002243 case TargetLowering::Legal:
2244 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00002245 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002246 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002247 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00002248 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
2249 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
2250 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002251 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00002252 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002253 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002254 break;
2255 }
2256 break;
2257 }
2258
Nate Begemanc105e192005-04-06 00:23:54 +00002259 case ISD::UREM:
2260 case ISD::SREM:
2261 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2262 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002263
Nate Begemanc105e192005-04-06 00:23:54 +00002264 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002265 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2266 case TargetLowering::Custom:
2267 isCustom = true;
2268 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002269 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002270 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002271 if (isCustom) {
2272 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002273 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002274 }
Nate Begemanc105e192005-04-06 00:23:54 +00002275 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002276 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002277 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002278 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002279 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002280
Dan Gohman525178c2007-10-08 18:33:35 +00002281 // See if remainder can be lowered using two-result operations.
2282 SDVTList VTs = DAG.getVTList(VT, VT);
2283 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002284 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002285 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002286 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002287 break;
2288 }
2289 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002290 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002291 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
2292 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002293 break;
2294 }
2295
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002296 if (VT.isInteger() &&
2297 TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
2298 // X % Y -> X-X/Y*Y
2299 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
2300 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
2301 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
2302 break;
Nate Begemanc105e192005-04-06 00:23:54 +00002303 }
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002304
2305 // Check to see if we have a libcall for this operator.
2306 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2307 switch (Node->getOpcode()) {
2308 default: break;
2309 case ISD::UREM:
2310 case ISD::SREM:
2311 if (VT == MVT::i16)
2312 LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16);
2313 else if (VT == MVT::i32)
2314 LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32);
2315 else if (VT == MVT::i64)
2316 LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64);
2317 else if (VT == MVT::i128)
2318 LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128);
2319 break;
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002320 }
2321
2322 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Eli Friedman47b41f72009-05-27 02:21:29 +00002323 Result = ExpandLibCall(LC, Node, isSigned);
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002324 break;
2325 }
2326
Eli Friedman74807f22009-05-26 08:55:52 +00002327 assert(0 && "Cannot expand this binary operator!");
Nate Begemanc105e192005-04-06 00:23:54 +00002328 break;
2329 }
Dan Gohman525178c2007-10-08 18:33:35 +00002330 }
Nate Begemanc105e192005-04-06 00:23:54 +00002331 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002332 case ISD::VAARG: {
2333 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2334 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2335
Duncan Sands83ec4b62008-06-06 12:08:01 +00002336 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002337 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2338 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002339 case TargetLowering::Custom:
2340 isCustom = true;
2341 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002342 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002343 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2344 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002345 Tmp1 = Result.getValue(1);
2346
2347 if (isCustom) {
2348 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002349 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002350 Result = LegalizeOp(Tmp2);
2351 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2352 }
2353 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002354 break;
2355 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00002356 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00002357 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002358 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00002359 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002360 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00002361 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002362 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00002363 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00002364 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002365 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00002366 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002367 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002368 Result = LegalizeOp(Result);
2369 break;
2370 }
2371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002372 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00002373 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002374 AddLegalizedOperand(SDValue(Node, 0), Result);
2375 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002376 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002377 }
Bill Wendling74c37652008-12-09 22:08:41 +00002378 case ISD::SADDO:
2379 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002380 MVT VT = Node->getValueType(0);
2381 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2382 default: assert(0 && "This action not supported for this op yet!");
2383 case TargetLowering::Custom:
2384 Result = TLI.LowerOperation(Op, DAG);
2385 if (Result.getNode()) break;
2386 // FALLTHROUGH
2387 case TargetLowering::Legal: {
2388 SDValue LHS = LegalizeOp(Node->getOperand(0));
2389 SDValue RHS = LegalizeOp(Node->getOperand(1));
2390
Scott Michelfdc40a02009-02-17 22:15:04 +00002391 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002392 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002393 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002394 MVT OType = Node->getValueType(1);
2395
Bill Wendlinga6af91a2008-11-25 08:19:22 +00002396 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002397
Bill Wendling740464e2008-11-25 19:40:17 +00002398 // LHSSign -> LHS >= 0
2399 // RHSSign -> RHS >= 0
2400 // SumSign -> Sum >= 0
2401 //
Bill Wendling74c37652008-12-09 22:08:41 +00002402 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00002403 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00002404 // Sub:
2405 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00002406 //
Dale Johannesenca57b842009-02-02 23:46:53 +00002407 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
2408 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00002409 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
2410 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002411 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002412
Dale Johannesenca57b842009-02-02 23:46:53 +00002413 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
2414 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002415
Dale Johannesenca57b842009-02-02 23:46:53 +00002416 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002417
2418 MVT ValueVTs[] = { LHS.getValueType(), OType };
2419 SDValue Ops[] = { Sum, Cmp };
2420
Scott Michelfdc40a02009-02-17 22:15:04 +00002421 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002422 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002423 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002424 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002425 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002426 break;
2427 }
2428 }
2429
2430 break;
2431 }
Bill Wendling74c37652008-12-09 22:08:41 +00002432 case ISD::UADDO:
2433 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00002434 MVT VT = Node->getValueType(0);
2435 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2436 default: assert(0 && "This action not supported for this op yet!");
2437 case TargetLowering::Custom:
2438 Result = TLI.LowerOperation(Op, DAG);
2439 if (Result.getNode()) break;
2440 // FALLTHROUGH
2441 case TargetLowering::Legal: {
2442 SDValue LHS = LegalizeOp(Node->getOperand(0));
2443 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002444
Bill Wendling74c37652008-12-09 22:08:41 +00002445 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002446 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002447 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002448 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00002449 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00002450 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002451 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002452
Bill Wendling41ea7e72008-11-24 19:21:46 +00002453 MVT ValueVTs[] = { LHS.getValueType(), OType };
2454 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002455
Scott Michelfdc40a02009-02-17 22:15:04 +00002456 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002457 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002458 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002459 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002460 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002461 break;
2462 }
2463 }
2464
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002465 break;
2466 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002467 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002468
Chris Lattner4ddd2832006-04-08 04:13:17 +00002469 assert(Result.getValueType() == Op.getValueType() &&
2470 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002471
Chris Lattner456a93a2006-01-28 07:39:30 +00002472 // Make sure that the generated code is itself legal.
2473 if (Result != Op)
2474 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002475
Chris Lattner45982da2005-05-12 16:53:42 +00002476 // Note that LegalizeOp may be reentered even from single-use nodes, which
2477 // means that we always must cache transformed nodes.
2478 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002479 return Result;
2480}
2481
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002482SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
2483 SDValue Vec = Op.getOperand(0);
2484 SDValue Idx = Op.getOperand(1);
2485 DebugLoc dl = Op.getDebugLoc();
2486 // Store the value to a temporary stack slot, then LOAD the returned part.
2487 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
2488 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
2489
2490 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00002491 unsigned EltSize =
2492 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002493 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
2494 DAG.getConstant(EltSize, Idx.getValueType()));
2495
2496 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
2497 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
2498 else
2499 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
2500
2501 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
2502
2503 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
2504}
2505
Nate Begeman750ac1b2006-02-01 07:19:44 +00002506/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2507/// with condition CC on the current target. This usually involves legalizing
2508/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2509/// there may be no choice but to create a new SetCC node to represent the
2510/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00002511/// LHS, and the SDValue returned in RHS has a nil SDNode value.
2512void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
2513 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002514 SDValue &CC,
2515 DebugLoc dl) {
Eli Friedman74807f22009-05-26 08:55:52 +00002516 LHS = LegalizeOp(LHS);
2517 RHS = LegalizeOp(RHS);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002518}
2519
Evan Cheng7f042682008-10-15 02:05:31 +00002520/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
2521/// condition code CC on the current target. This routine assumes LHS and rHS
2522/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
2523/// illegal condition code into AND / OR of multiple SETCC values.
2524void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
2525 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002526 SDValue &CC,
2527 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00002528 MVT OpVT = LHS.getValueType();
2529 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
2530 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
2531 default: assert(0 && "Unknown condition code action!");
2532 case TargetLowering::Legal:
2533 // Nothing to do.
2534 break;
2535 case TargetLowering::Expand: {
2536 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
2537 unsigned Opc = 0;
2538 switch (CCCode) {
2539 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00002540 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
2541 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2542 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2543 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2544 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2545 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2546 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2547 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2548 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2549 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2550 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2551 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00002552 // FIXME: Implement more expansions.
2553 }
2554
Dale Johannesenbb5da912009-02-02 20:41:04 +00002555 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
2556 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
2557 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00002558 RHS = SDValue();
2559 CC = SDValue();
2560 break;
2561 }
2562 }
2563}
2564
Chris Lattner1401d152008-01-16 07:45:30 +00002565/// EmitStackConvert - Emit a store/load combination to the stack. This stores
2566/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
2567/// a load from the stack slot to DestVT, extending it if needed.
2568/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00002569SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
2570 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002571 MVT DestVT,
2572 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00002573 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002574 unsigned SrcAlign =
2575 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
2576 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00002577 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002578
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002579 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002580 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00002581 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
2582
Duncan Sands83ec4b62008-06-06 12:08:01 +00002583 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2584 unsigned SlotSize = SlotVT.getSizeInBits();
2585 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002586 unsigned DestAlign =
2587 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00002588
Chris Lattner1401d152008-01-16 07:45:30 +00002589 // Emit a store to the stack slot. Use a truncstore if the input value is
2590 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002591 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00002592
Chris Lattner1401d152008-01-16 07:45:30 +00002593 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002594 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002595 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002596 else {
2597 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002598 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002599 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002600 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002601
Chris Lattner35481892005-12-23 00:16:34 +00002602 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002603 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002604 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002605
Chris Lattner1401d152008-01-16 07:45:30 +00002606 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002607 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00002608 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002609}
2610
Dan Gohman475871a2008-07-27 21:46:04 +00002611SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002612 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002613 // Create a vector sized/aligned stack slot, store the value to element #0,
2614 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002615 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002616
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002617 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002618 int SPFI = StackPtrFI->getIndex();
2619
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002620 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2621 StackPtr,
2622 PseudoSourceValue::getFixedStack(SPFI), 0,
2623 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002624 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00002625 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002626}
2627
2628
Chris Lattnerce872152006-03-19 06:31:19 +00002629/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002630/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002631SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002632 unsigned NumElems = Node->getNumOperands();
2633 SDValue SplatValue = Node->getOperand(0);
2634 DebugLoc dl = Node->getDebugLoc();
2635 MVT VT = Node->getValueType(0);
2636 MVT OpVT = SplatValue.getValueType();
2637 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002638
2639 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002640 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002641 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00002642
Dan Gohman475871a2008-07-27 21:46:04 +00002643 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002644 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00002645 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00002646 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00002647 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002648 bool isConstant = true;
2649 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
2650 SplatValue.getOpcode() != ISD::UNDEF)
2651 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00002652
Evan Cheng033e6812006-03-24 01:17:21 +00002653 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002654 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00002655 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00002656 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00002657 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00002658 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002659 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002660
2661 // If this isn't a constant element or an undef, we can't use a constant
2662 // pool load.
2663 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
2664 V.getOpcode() != ISD::UNDEF)
2665 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00002666 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002667
Chris Lattnerce872152006-03-19 06:31:19 +00002668 if (isOnlyLowElement) {
2669 // If the low element is an undef too, then this whole things is an undef.
2670 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002671 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00002672 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002673 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00002674 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002675
Chris Lattner2eb86532006-03-24 07:29:17 +00002676 // If all elements are constants, create a load from the constant pool.
2677 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002678 std::vector<Constant*> CV;
2679 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002680 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002681 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002682 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002683 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002684 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002685 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00002686 } else {
2687 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002688 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00002689 CV.push_back(UndefValue::get(OpNTy));
2690 }
2691 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002692 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002693 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002694 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002695 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002696 PseudoSourceValue::getConstantPool(), 0,
2697 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002698 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002699
Gabor Greifba36cb52008-08-28 21:40:38 +00002700 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00002701 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00002702 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00002703
2704 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00002705 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002706 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00002707 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002708 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002709
Chris Lattner87100e02006-03-20 01:52:29 +00002710 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00002711 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
2712 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00002713 }
2714 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002715
Evan Cheng033e6812006-03-24 01:17:21 +00002716 // If there are only two unique elements, we may be able to turn this into a
2717 // vector shuffle.
2718 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002719 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00002720 SDValue Val1 = Node->getOperand(1);
2721 SDValue Val2;
2722 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002723 if (MI->first != Val1)
2724 Val2 = MI->first;
2725 else
2726 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00002727
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002728 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002729 // vector shuffle has the undef vector on the RHS.
2730 if (Val1.getOpcode() == ISD::UNDEF)
2731 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002732
Evan Cheng033e6812006-03-24 01:17:21 +00002733 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00002734 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002735
2736 // Set elements of the shuffle mask for Val1.
2737 std::vector<unsigned> &Val1Elts = Values[Val1];
2738 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002739 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002740
2741 // Set elements of the shuffle mask for Val2.
2742 std::vector<unsigned> &Val2Elts = Values[Val2];
2743 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
2744 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00002746
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002747 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002748 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00002749 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002750 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
2751 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00002752 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00002753 }
2754 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002755
Chris Lattnerce872152006-03-19 06:31:19 +00002756 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
2757 // aligned object on the stack, store each element into it, then load
2758 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00002759 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00002760 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00002761 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
2762 const Value *SV = PseudoSourceValue::getFixedStack(FI);
2763
Chris Lattnerce872152006-03-19 06:31:19 +00002764 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00002765 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002766 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00002767 // Store (in the right endianness) the elements to memory.
2768 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2769 // Ignore undef elements.
2770 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00002771
Chris Lattner841c8822006-03-22 01:46:54 +00002772 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00002773
Dan Gohman475871a2008-07-27 21:46:04 +00002774 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002775 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00002776
Dale Johannesen8a782a22009-02-02 22:12:50 +00002777 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
2778 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00002779 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002780
Dan Gohman475871a2008-07-27 21:46:04 +00002781 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00002782 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00002783 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002784 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00002785 else
2786 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002787
Chris Lattnerce872152006-03-19 06:31:19 +00002788 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00002789 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00002790}
2791
Chris Lattner77e77a62005-01-21 06:05:23 +00002792// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2793// does not fit into a register, return the lo part and set the hi part to the
2794// by-reg argument. If it does fit into a single register, return the result
2795// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002796SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002797 bool isSigned) {
Chris Lattner6831a812006-02-13 09:18:02 +00002798 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002799 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002800 // Legalizing the call will automatically add the previous call to the
2801 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002802 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002803
Chris Lattner77e77a62005-01-21 06:05:23 +00002804 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002805 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002806 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002807 MVT ArgVT = Node->getOperand(i).getValueType();
2808 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00002809 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002810 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002811 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002812 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002813 }
Bill Wendling056292f2008-09-16 21:48:12 +00002814 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002815 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002816
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002817 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002818 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002819 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002820 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002821 CallingConv::C, false, Callee, Args, DAG,
2822 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002823
Chris Lattner6831a812006-02-13 09:18:02 +00002824 // Legalize the call sequence, starting with the chain. This will advance
2825 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2826 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2827 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002828 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002829}
2830
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002831SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2832 RTLIB::Libcall Call_F32,
2833 RTLIB::Libcall Call_F64,
2834 RTLIB::Libcall Call_F80,
2835 RTLIB::Libcall Call_PPCF128) {
2836 RTLIB::Libcall LC;
2837 switch (Node->getValueType(0).getSimpleVT()) {
2838 default: assert(0 && "Unexpected request for libcall!");
2839 case MVT::f32: LC = Call_F32; break;
2840 case MVT::f64: LC = Call_F64; break;
2841 case MVT::f80: LC = Call_F80; break;
2842 case MVT::ppcf128: LC = Call_PPCF128; break;
2843 }
2844 return ExpandLibCall(LC, Node, false);
2845}
2846
2847SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2848 RTLIB::Libcall Call_I16,
2849 RTLIB::Libcall Call_I32,
2850 RTLIB::Libcall Call_I64,
2851 RTLIB::Libcall Call_I128) {
2852 RTLIB::Libcall LC;
2853 switch (Node->getValueType(0).getSimpleVT()) {
2854 default: assert(0 && "Unexpected request for libcall!");
2855 case MVT::i16: LC = Call_I16; break;
2856 case MVT::i32: LC = Call_I32; break;
2857 case MVT::i64: LC = Call_I64; break;
2858 case MVT::i128: LC = Call_I128; break;
2859 }
2860 return ExpandLibCall(LC, Node, isSigned);
2861}
2862
Chris Lattner22cde6a2006-01-28 08:25:58 +00002863/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2864/// INT_TO_FP operation of the specified operand when the target requests that
2865/// we expand it. At this point, we know that the result and operand types are
2866/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002867SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2868 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00002869 MVT DestVT,
2870 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002871 if (Op0.getValueType() == MVT::i32) {
2872 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002873
Chris Lattner23594d42008-01-16 07:03:22 +00002874 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00002875 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002876
Chris Lattner22cde6a2006-01-28 08:25:58 +00002877 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002878 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002879 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002880 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002881 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002882 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002883 if (TLI.isLittleEndian())
2884 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002885
Chris Lattner22cde6a2006-01-28 08:25:58 +00002886 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002887 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002888 if (isSigned) {
2889 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00002890 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00002891 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002892 } else {
2893 Op0Mapped = Op0;
2894 }
2895 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002896 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002897 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002898 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00002899 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002900 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00002901 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002902 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00002903 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002904 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002905 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002906 BitsToDouble(0x4330000080000000ULL) :
2907 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00002908 MVT::f64);
2909 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00002910 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002911 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002912 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002913 // handle final rounding
2914 if (DestVT == MVT::f64) {
2915 // do nothing
2916 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00002917 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002918 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002919 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002920 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002921 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002922 }
2923 return Result;
2924 }
2925 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00002926 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002927
Dale Johannesenaf435272009-02-02 19:03:57 +00002928 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00002929 Op0, DAG.getConstant(0, Op0.getValueType()),
2930 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00002931 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00002932 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00002933 SignSet, Four, Zero);
2934
2935 // If the sign bit of the integer is set, the large number will be treated
2936 // as a negative number. To counteract this, the dynamic code adds an
2937 // offset depending on the data type.
2938 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002939 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002940 default: assert(0 && "Unsupported integer type!");
2941 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2942 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2943 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2944 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2945 }
2946 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00002947 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002948
Dan Gohman475871a2008-07-27 21:46:04 +00002949 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002950 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00002951 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00002952 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00002953 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002954 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00002955 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002956 PseudoSourceValue::getConstantPool(), 0,
2957 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002958 else {
Dan Gohman69de1932008-02-06 22:27:42 +00002959 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00002960 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00002961 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00002962 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00002963 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002964 }
2965
Dale Johannesenaf435272009-02-02 19:03:57 +00002966 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002967}
2968
2969/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2970/// *INT_TO_FP operation of the specified operand when the target requests that
2971/// we promote it. At this point, we know that the result and operand types are
2972/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2973/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002974SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
2975 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002976 bool isSigned,
2977 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002978 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002979 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002980
2981 unsigned OpToUse = 0;
2982
2983 // Scan for the appropriate larger type to use.
2984 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002985 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
2986 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002987
2988 // If the target supports SINT_TO_FP of this type, use it.
2989 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
2990 default: break;
2991 case TargetLowering::Legal:
2992 if (!TLI.isTypeLegal(NewInTy))
2993 break; // Can't use this datatype.
2994 // FALL THROUGH.
2995 case TargetLowering::Custom:
2996 OpToUse = ISD::SINT_TO_FP;
2997 break;
2998 }
2999 if (OpToUse) break;
3000 if (isSigned) continue;
3001
3002 // If the target supports UINT_TO_FP of this type, use it.
3003 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3004 default: break;
3005 case TargetLowering::Legal:
3006 if (!TLI.isTypeLegal(NewInTy))
3007 break; // Can't use this datatype.
3008 // FALL THROUGH.
3009 case TargetLowering::Custom:
3010 OpToUse = ISD::UINT_TO_FP;
3011 break;
3012 }
3013 if (OpToUse) break;
3014
3015 // Otherwise, try a larger type.
3016 }
3017
3018 // Okay, we found the operation and type to use. Zero extend our input to the
3019 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00003020 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00003021 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00003022 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003023}
3024
3025/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3026/// FP_TO_*INT operation of the specified operand when the target requests that
3027/// we promote it. At this point, we know that the result and operand types are
3028/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3029/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00003030SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
3031 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00003032 bool isSigned,
3033 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003034 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003035 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003036
3037 unsigned OpToUse = 0;
3038
3039 // Scan for the appropriate larger type to use.
3040 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003041 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
3042 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00003043
3044 // If the target supports FP_TO_SINT returning this type, use it.
3045 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3046 default: break;
3047 case TargetLowering::Legal:
3048 if (!TLI.isTypeLegal(NewOutTy))
3049 break; // Can't use this datatype.
3050 // FALL THROUGH.
3051 case TargetLowering::Custom:
3052 OpToUse = ISD::FP_TO_SINT;
3053 break;
3054 }
3055 if (OpToUse) break;
3056
3057 // If the target supports FP_TO_UINT of this type, use it.
3058 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3059 default: break;
3060 case TargetLowering::Legal:
3061 if (!TLI.isTypeLegal(NewOutTy))
3062 break; // Can't use this datatype.
3063 // FALL THROUGH.
3064 case TargetLowering::Custom:
3065 OpToUse = ISD::FP_TO_UINT;
3066 break;
3067 }
3068 if (OpToUse) break;
3069
3070 // Otherwise, try a larger type.
3071 }
3072
Scott Michelfdc40a02009-02-17 22:15:04 +00003073
Chris Lattner27a6c732007-11-24 07:07:01 +00003074 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00003075 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00003076
Chris Lattner27a6c732007-11-24 07:07:01 +00003077 // If the operation produces an invalid type, it must be custom lowered. Use
3078 // the target lowering hooks to expand it. Just keep the low part of the
3079 // expanded operation, we know that we're truncating anyway.
3080 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00003081 SmallVector<SDValue, 2> Results;
3082 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
3083 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
3084 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00003085 }
Duncan Sands126d9072008-07-04 11:47:58 +00003086
Chris Lattner27a6c732007-11-24 07:07:01 +00003087 // Truncate the result of the extended FP_TO_*INT operation to the desired
3088 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00003089 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003090}
3091
3092/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3093///
Dale Johannesen8a782a22009-02-02 22:12:50 +00003094SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 MVT VT = Op.getValueType();
3096 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003097 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003098 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003099 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3100 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003101 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3102 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3103 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003104 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003105 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
3106 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3107 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3108 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
3109 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3110 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3111 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
3112 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
3113 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003114 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003115 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
3116 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
3117 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
3118 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3119 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3120 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
3121 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
3122 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
3123 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3124 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3125 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3126 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3127 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3128 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3129 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
3130 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
3131 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
3132 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
3133 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
3134 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
3135 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003136 }
3137}
3138
3139/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3140///
Scott Michelfdc40a02009-02-17 22:15:04 +00003141SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00003142 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003143 switch (Opc) {
3144 default: assert(0 && "Cannot expand this yet!");
3145 case ISD::CTPOP: {
3146 static const uint64_t mask[6] = {
3147 0x5555555555555555ULL, 0x3333333333333333ULL,
3148 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3149 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3150 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00003151 MVT VT = Op.getValueType();
3152 MVT ShVT = TLI.getShiftAmountTy();
3153 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003154 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3155 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003156 unsigned EltSize = VT.isVector() ?
3157 VT.getVectorElementType().getSizeInBits() : len;
3158 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003159 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003160 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00003161 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003162 DAG.getNode(ISD::AND, dl, VT,
3163 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
3164 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003165 }
3166 return Op;
3167 }
3168 case ISD::CTLZ: {
3169 // for now, we do this:
3170 // x = x | (x >> 1);
3171 // x = x | (x >> 2);
3172 // ...
3173 // x = x | (x >>16);
3174 // x = x | (x >>32); // for 64-bit input
3175 // return popcount(~x);
3176 //
3177 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003178 MVT VT = Op.getValueType();
3179 MVT ShVT = TLI.getShiftAmountTy();
3180 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003181 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003182 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003183 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00003184 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003185 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00003186 Op = DAG.getNOT(dl, Op, VT);
3187 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003188 }
3189 case ISD::CTTZ: {
3190 // for now, we use: { return popcount(~x & (x - 1)); }
3191 // unless the target has ctlz but not ctpop, in which case we use:
3192 // { return 32 - nlz(~x & (x-1)); }
3193 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003194 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00003195 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
3196 DAG.getNOT(dl, Op, VT),
3197 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00003198 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003199 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003200 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
3201 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00003202 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003203 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003204 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
3205 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003206 }
3207 }
3208}
Chris Lattnere34b3962005-01-19 04:19:40 +00003209
Eli Friedman8c377c72009-05-27 01:25:56 +00003210void SelectionDAGLegalize::ExpandNode(SDNode *Node,
3211 SmallVectorImpl<SDValue> &Results) {
3212 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003213 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003214 switch (Node->getOpcode()) {
3215 case ISD::CTPOP:
3216 case ISD::CTLZ:
3217 case ISD::CTTZ:
3218 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
3219 Results.push_back(Tmp1);
3220 break;
3221 case ISD::BSWAP:
3222 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
3223 break;
3224 case ISD::FRAMEADDR:
3225 case ISD::RETURNADDR:
3226 case ISD::FRAME_TO_ARGS_OFFSET:
3227 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
3228 break;
3229 case ISD::FLT_ROUNDS_:
3230 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
3231 break;
3232 case ISD::EH_RETURN:
3233 case ISD::DECLARE:
3234 case ISD::DBG_LABEL:
3235 case ISD::EH_LABEL:
3236 case ISD::PREFETCH:
3237 case ISD::MEMBARRIER:
3238 case ISD::VAEND:
3239 Results.push_back(Node->getOperand(0));
3240 break;
3241 case ISD::MERGE_VALUES:
3242 for (unsigned i = 0; i < Node->getNumValues(); i++)
3243 Results.push_back(Node->getOperand(i));
3244 break;
3245 case ISD::UNDEF: {
3246 MVT VT = Node->getValueType(0);
3247 if (VT.isInteger())
3248 Results.push_back(DAG.getConstant(0, VT));
3249 else if (VT.isFloatingPoint())
3250 Results.push_back(DAG.getConstantFP(0, VT));
3251 else
3252 assert(0 && "Unknown value type!");
3253 break;
3254 }
3255 case ISD::TRAP: {
3256 // If this operation is not supported, lower it to 'abort()' call
3257 TargetLowering::ArgListTy Args;
3258 std::pair<SDValue, SDValue> CallResult =
3259 TLI.LowerCallTo(Node->getOperand(0), Type::VoidTy,
3260 false, false, false, false, CallingConv::C, false,
3261 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3262 Args, DAG, dl);
3263 Results.push_back(CallResult.second);
3264 break;
3265 }
3266 case ISD::FP_ROUND:
3267 case ISD::BIT_CONVERT:
3268 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3269 Node->getValueType(0), dl);
3270 Results.push_back(Tmp1);
3271 break;
3272 case ISD::FP_EXTEND:
3273 Tmp1 = EmitStackConvert(Node->getOperand(0),
3274 Node->getOperand(0).getValueType(),
3275 Node->getValueType(0), dl);
3276 Results.push_back(Tmp1);
3277 break;
3278 case ISD::SIGN_EXTEND_INREG: {
3279 // NOTE: we could fall back on load/store here too for targets without
3280 // SAR. However, it is doubtful that any exist.
3281 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3282 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3283 ExtraVT.getSizeInBits();
3284 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3285 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3286 Node->getOperand(0), ShiftCst);
3287 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3288 Results.push_back(Tmp1);
3289 break;
3290 }
3291 case ISD::FP_ROUND_INREG: {
3292 // The only way we can lower this is to turn it into a TRUNCSTORE,
3293 // EXTLOAD pair, targetting a temporary location (a stack slot).
3294
3295 // NOTE: there is a choice here between constantly creating new stack
3296 // slots and always reusing the same one. We currently always create
3297 // new ones, as reuse may inhibit scheduling.
3298 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3299 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3300 Node->getValueType(0), dl);
3301 Results.push_back(Tmp1);
3302 break;
3303 }
3304 case ISD::SINT_TO_FP:
3305 case ISD::UINT_TO_FP:
3306 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3307 Node->getOperand(0), Node->getValueType(0), dl);
3308 Results.push_back(Tmp1);
3309 break;
3310 case ISD::FP_TO_UINT: {
3311 SDValue True, False;
3312 MVT VT = Node->getOperand(0).getValueType();
3313 MVT NVT = Node->getValueType(0);
3314 const uint64_t zero[] = {0, 0};
3315 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3316 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3317 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3318 Tmp1 = DAG.getConstantFP(apf, VT);
3319 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3320 Node->getOperand(0),
3321 Tmp1, ISD::SETLT);
3322 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3323 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3324 DAG.getNode(ISD::FSUB, dl, VT,
3325 Node->getOperand(0), Tmp1));
3326 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3327 DAG.getConstant(x, NVT));
3328 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
3329 Results.push_back(Tmp1);
3330 break;
3331 }
3332 case ISD::VACOPY: {
3333 // This defaults to loading a pointer from the input and storing it to the
3334 // output, returning the chain.
3335 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3336 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3337 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
3338 Node->getOperand(2), VS, 0);
3339 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0);
3340 Results.push_back(Tmp1);
3341 break;
3342 }
3343 case ISD::EXTRACT_VECTOR_ELT:
3344 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3345 // This must be an access of the only element. Return it.
3346 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
3347 Node->getOperand(0));
3348 else
3349 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3350 Results.push_back(Tmp1);
3351 break;
3352 case ISD::EXTRACT_SUBVECTOR:
3353 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3354 break;
3355 case ISD::SCALAR_TO_VECTOR:
3356 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3357 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003358 case ISD::INSERT_VECTOR_ELT:
3359 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3360 Node->getOperand(1),
3361 Node->getOperand(2), dl));
3362 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003363 case ISD::EXTRACT_ELEMENT: {
3364 MVT OpTy = Node->getOperand(0).getValueType();
3365 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3366 // 1 -> Hi
3367 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3368 DAG.getConstant(OpTy.getSizeInBits()/2,
3369 TLI.getShiftAmountTy()));
3370 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3371 } else {
3372 // 0 -> Lo
3373 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3374 Node->getOperand(0));
3375 }
3376 Results.push_back(Tmp1);
3377 break;
3378 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003379 case ISD::STACKSAVE:
3380 // Expand to CopyFromReg if the target set
3381 // StackPointerRegisterToSaveRestore.
3382 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3383 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3384 Node->getValueType(0)));
3385 Results.push_back(Results[0].getValue(1));
3386 } else {
3387 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3388 Results.push_back(Node->getOperand(0));
3389 }
3390 break;
3391 case ISD::STACKRESTORE:
3392 // Expand to CopyToReg if the target set
3393 // StackPointerRegisterToSaveRestore.
3394 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3395 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3396 Node->getOperand(1)));
3397 } else {
3398 Results.push_back(Node->getOperand(0));
3399 }
3400 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003401 case ISD::FNEG:
3402 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3403 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3404 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3405 Node->getOperand(0));
3406 Results.push_back(Tmp1);
3407 break;
3408 case ISD::FABS: {
3409 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3410 MVT VT = Node->getValueType(0);
3411 Tmp1 = Node->getOperand(0);
3412 Tmp2 = DAG.getConstantFP(0.0, VT);
3413 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3414 Tmp1, Tmp2, ISD::SETUGT);
3415 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3416 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
3417 Results.push_back(Tmp1);
3418 break;
3419 }
3420 case ISD::FSQRT:
3421 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3422 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
3423 break;
3424 case ISD::FSIN:
3425 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3426 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
3427 break;
3428 case ISD::FCOS:
3429 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3430 RTLIB::COS_F80, RTLIB::COS_PPCF128));
3431 break;
3432 case ISD::FLOG:
3433 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3434 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
3435 break;
3436 case ISD::FLOG2:
3437 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3438 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
3439 break;
3440 case ISD::FLOG10:
3441 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3442 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
3443 break;
3444 case ISD::FEXP:
3445 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3446 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
3447 break;
3448 case ISD::FEXP2:
3449 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3450 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
3451 break;
3452 case ISD::FTRUNC:
3453 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3454 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
3455 break;
3456 case ISD::FFLOOR:
3457 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3458 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
3459 break;
3460 case ISD::FCEIL:
3461 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3462 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
3463 break;
3464 case ISD::FRINT:
3465 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3466 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
3467 break;
3468 case ISD::FNEARBYINT:
3469 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3470 RTLIB::NEARBYINT_F64,
3471 RTLIB::NEARBYINT_F80,
3472 RTLIB::NEARBYINT_PPCF128));
3473 break;
3474 case ISD::FPOWI:
3475 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3476 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
3477 break;
3478 case ISD::FPOW:
3479 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3480 RTLIB::POW_F80, RTLIB::POW_PPCF128));
3481 break;
3482 case ISD::FDIV:
3483 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3484 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
3485 break;
3486 case ISD::FREM:
3487 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3488 RTLIB::REM_F80, RTLIB::REM_PPCF128));
3489 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003490 case ISD::GLOBAL_OFFSET_TABLE:
3491 case ISD::GlobalAddress:
3492 case ISD::GlobalTLSAddress:
3493 case ISD::ExternalSymbol:
3494 case ISD::ConstantPool:
3495 case ISD::JumpTable:
3496 case ISD::INTRINSIC_W_CHAIN:
3497 case ISD::INTRINSIC_WO_CHAIN:
3498 case ISD::INTRINSIC_VOID:
3499 // FIXME: Custom lowering for these operations shouldn't return null!
3500 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3501 Results.push_back(SDValue(Node, i));
3502 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003503 }
3504}
3505void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3506 SmallVectorImpl<SDValue> &Results) {
3507 MVT OVT = Node->getValueType(0);
3508 if (Node->getOpcode() == ISD::UINT_TO_FP ||
3509 Node->getOpcode() == ISD::SINT_TO_FP) {
3510 OVT = Node->getOperand(0).getValueType();
3511 }
3512 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3513 DebugLoc dl = Node->getDebugLoc();
3514 SDValue Tmp1, Tmp2;
3515 switch (Node->getOpcode()) {
3516 case ISD::CTTZ:
3517 case ISD::CTLZ:
3518 case ISD::CTPOP:
3519 // Zero extend the argument.
3520 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3521 // Perform the larger operation.
3522 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
3523 if (Node->getOpcode() == ISD::CTTZ) {
3524 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3525 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3526 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3527 ISD::SETEQ);
3528 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3529 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3530 } else if (Node->getOpcode() == ISD::CTLZ) {
3531 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3532 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3533 DAG.getConstant(NVT.getSizeInBits() -
3534 OVT.getSizeInBits(), NVT));
3535 }
3536 Results.push_back(Tmp1);
3537 break;
3538 case ISD::BSWAP: {
3539 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3540 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3541 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3542 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3543 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3544 Results.push_back(Tmp1);
3545 break;
3546 }
3547 case ISD::FP_TO_UINT:
3548 case ISD::FP_TO_SINT:
3549 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3550 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3551 Results.push_back(Tmp1);
3552 break;
3553 case ISD::UINT_TO_FP:
3554 case ISD::SINT_TO_FP:
3555 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3556 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3557 Results.push_back(Tmp1);
3558 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003559 case ISD::AND:
3560 case ISD::OR:
3561 case ISD::XOR:
3562 assert(OVT.isVector() && "Don't know how to promote scalar logic ops");
3563 // Bit convert each of the values to the new type.
3564 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
3565 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(1));
3566 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3567 // Bit convert the result back the original type.
3568 Results.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Tmp1));
3569 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003570 }
3571}
3572
Chris Lattner3e928bb2005-01-07 07:47:09 +00003573// SelectionDAG::Legalize - This is the entry point for the file.
3574//
Bill Wendling98a366d2009-04-29 23:29:43 +00003575void SelectionDAG::Legalize(bool TypesNeedLegalizing,
3576 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003577 /// run - This is the main entry point to this class.
3578 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003579 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003580}
3581